From: Roman Pen <roman.penyaev@profitbricks.com>
To: unlisted-recipients:; (no To-header on input)
Cc: Roman Pen <roman.penyaev@profitbricks.com>,
Andy Lutomirski <luto@kernel.org>,
Oleg Nesterov <oleg@redhat.com>,
Peter Zijlstra <peterz@infradead.org>,
Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>, Tejun Heo <tj@kernel.org>,
linux-kernel@vger.kernel.org
Subject: [PATCH 1/1] kthread: fix possible infinite wait for parking when kthread exits meanwhile
Date: Tue, 25 Oct 2016 13:05:25 +0200 [thread overview]
Message-ID: <20161025110525.9100-1-roman.penyaev@profitbricks.com> (raw)
The patch handles the case, when someone waits on parked completion but
kthread exits meanwhile. To avoid infinite wait the waiter has to spin
once more in a loop and simply try to get an alive kthread. If the
kthread has been died, put_kthread_cb() wakes up possible waiter when
kthread->vfork_done is already NULL, so next attempt to grab alive
kthread pointer will fail.
Signed-off-by: Roman Pen <roman.penyaev@profitbricks.com>
Cc: Andy Lutomirski <luto@kernel.org>
Cc: Oleg Nesterov <oleg@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Thomas Gleixner <tglx@linutronix.de>
Cc: Ingo Molnar <mingo@redhat.com>
Cc: Tejun Heo <tj@kernel.org>
Cc: linux-kernel@vger.kernel.org
---
kernel/kthread.c | 41 ++++++++++++++++++++++++++++++++++++-----
1 file changed, 36 insertions(+), 5 deletions(-)
diff --git a/kernel/kthread.c b/kernel/kthread.c
index e8adc10556e0..a001c1ec489b 100644
--- a/kernel/kthread.c
+++ b/kernel/kthread.c
@@ -77,6 +77,12 @@ static void put_kthread_cb(struct callback_head *work)
struct kthread *kthread;
kthread = container_of(work, struct kthread, put_work);
+ /*
+ * Kick out possible waiter on a parked completion before
+ * ref put. That will force them to spin in a loop once
+ * more and eventually get the NULL kthread pointer.
+ */
+ complete(&kthread->parked);
put_kthread(kthread);
}
@@ -449,6 +455,11 @@ static void __kthread_unpark(struct task_struct *k, struct kthread *kthread)
}
}
+static bool __kthread_isparked(struct kthread *kthread)
+{
+ return test_bit(KTHREAD_IS_PARKED, &kthread->flags);
+}
+
/**
* kthread_unpark - unpark a thread created by kthread_create().
* @k: thread created by kthread_create().
@@ -479,23 +490,43 @@ EXPORT_SYMBOL_GPL(kthread_unpark);
*
* Returns 0 if the thread is parked, -ENOSYS if the thread exited.
* If called by the kthread itself just the park bit is set.
+ *
+ * BEWARE: The caller is responsible for ensuring the validity of @k when
+ * calling this function.
+ *
+ * BEWARE: Only one simultaneous caller is possible. Others will hang
+ * forever. You have been warned.
*/
int kthread_park(struct task_struct *k)
{
- struct kthread *kthread = to_live_kthread_and_get(k);
+ struct kthread *kthread;
int ret = -ENOSYS;
- if (kthread) {
- if (!test_bit(KTHREAD_IS_PARKED, &kthread->flags)) {
+ /*
+ * Here we try to be careful and handle the case, when kthread
+ * is going to die and will never park. In that particular case
+ * put_kthread_cb() is called when kthread->vfork_done is already
+ * NULL. put_kthread_cb() does the last completion on kthread->parked,
+ * thus we will spin once more and next attempt to get an alive
+ * kthread will fail.
+ */
+ do {
+ kthread = to_live_kthread_and_get(k);
+ if (!kthread)
+ break;
+ if (!__kthread_isparked(kthread)) {
set_bit(KTHREAD_SHOULD_PARK, &kthread->flags);
if (k != current) {
wake_up_process(k);
wait_for_completion(&kthread->parked);
}
}
+ if (k == current || __kthread_isparked(kthread))
+ /* The way out */
+ ret = 0;
put_kthread(kthread);
- ret = 0;
- }
+ } while (ret);
+
return ret;
}
EXPORT_SYMBOL_GPL(kthread_park);
--
2.9.3
next reply other threads:[~2016-10-25 11:05 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-10-25 11:05 Roman Pen [this message]
2016-10-25 15:16 ` Oleg Nesterov
2016-10-25 17:34 ` Roman Penyaev
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20161025110525.9100-1-roman.penyaev@profitbricks.com \
--to=roman.penyaev@profitbricks.com \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@kernel.org \
--cc=mingo@redhat.com \
--cc=oleg@redhat.com \
--cc=peterz@infradead.org \
--cc=tglx@linutronix.de \
--cc=tj@kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®