From: Vlastimil Babka <vbabka@suse.cz>
To: Frederic Weisbecker <frederic@kernel.org>,
LKML <linux-kernel@vger.kernel.org>
Cc: Andrew Morton <akpm@linux-foundation.org>,
Kees Cook <kees@kernel.org>,
Peter Zijlstra <peterz@infradead.org>,
Thomas Gleixner <tglx@linutronix.de>,
Michal Hocko <mhocko@kernel.org>,
linux-mm@kvack.org, "Paul E. McKenney" <paulmck@kernel.org>,
Neeraj Upadhyay <neeraj.upadhyay@kernel.org>,
Joel Fernandes <joel@joelfernandes.org>,
Boqun Feng <boqun.feng@gmail.com>,
Zqiang <qiang.zhang1211@gmail.com>,
rcu@vger.kernel.org
Subject: Re: [RFC PATCH 11/20] kthread: Make sure kthread hasn't started while binding it
Date: Tue, 30 Jul 2024 17:20:12 +0200 [thread overview]
Message-ID: <f977c691-4d51-4b23-90b1-8ebcd1f36d73@suse.cz> (raw)
In-Reply-To: <20240726215701.19459-12-frederic@kernel.org>
On 7/26/24 11:56 PM, Frederic Weisbecker wrote:
> Make sure the kthread is sleeping in the schedule_preempt_disabled()
> call before calling its handler when kthread_bind[_mask]() is called
> on it. This provides a sanity check verifying that the task is not
> randomly blocked later at some point within its function handler, in
> which case it could be just concurrently awaken, leaving the call to
> do_set_cpus_allowed() without any effect until the next voluntary sleep.
>
> Rely on the wake-up ordering to ensure that the newly introduced "started"
> field returns the expected value:
>
> TASK A TASK B
> ------ ------
> READ kthread->started
> wake_up_process(B)
> rq_lock()
> ...
> rq_unlock() // RELEASE
> schedule()
> rq_lock() // ACQUIRE
> // schedule task B
> rq_unlock()
> WRITE kthread->started
>
> Similarly, writing kthread->started before subsequent voluntary sleeps
> will be visible after calling wait_task_inactive() in
> __kthread_bind_mask(), reporting potential misuse of the API.
>
> Upcoming patches will make further use of this facility.
>
> Signed-off-by: Frederic Weisbecker <frederic@kernel.org>
Acked-by: Vlastimil Babka <vbabka@suse.cz>
> ---
> kernel/kthread.c | 7 +++++++
> 1 file changed, 7 insertions(+)
>
> diff --git a/kernel/kthread.c b/kernel/kthread.c
> index f7be976ff88a..ecb719f54f7a 100644
> --- a/kernel/kthread.c
> +++ b/kernel/kthread.c
> @@ -53,6 +53,7 @@ struct kthread_create_info
> struct kthread {
> unsigned long flags;
> unsigned int cpu;
> + int started;
> int result;
> int (*threadfn)(void *);
> void *data;
> @@ -382,6 +383,8 @@ static int kthread(void *_create)
> schedule_preempt_disabled();
> preempt_enable();
>
> + self->started = 1;
> +
> ret = -EINTR;
> if (!test_bit(KTHREAD_SHOULD_STOP, &self->flags)) {
> cgroup_kthread_ready();
> @@ -540,7 +543,9 @@ static void __kthread_bind(struct task_struct *p, unsigned int cpu, unsigned int
>
> void kthread_bind_mask(struct task_struct *p, const struct cpumask *mask)
> {
> + struct kthread *kthread = to_kthread(p);
> __kthread_bind_mask(p, mask, TASK_UNINTERRUPTIBLE);
> + WARN_ON_ONCE(kthread->started);
> }
>
> /**
> @@ -554,7 +559,9 @@ void kthread_bind_mask(struct task_struct *p, const struct cpumask *mask)
> */
> void kthread_bind(struct task_struct *p, unsigned int cpu)
> {
> + struct kthread *kthread = to_kthread(p);
> __kthread_bind(p, cpu, TASK_UNINTERRUPTIBLE);
> + WARN_ON_ONCE(kthread->started);
> }
> EXPORT_SYMBOL(kthread_bind);
>
next prev parent reply other threads:[~2024-07-30 15:18 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <20240726215701.19459-1-frederic@kernel.org>
2024-07-26 21:56 ` Frederic Weisbecker
2024-07-30 15:20 ` Vlastimil Babka [this message]
2024-07-26 21:56 ` [RFC PATCH 12/20] kthread: Implement preferred affinity Frederic Weisbecker
2024-07-26 22:31 ` Frederic Weisbecker
2024-07-30 15:49 ` Vlastimil Babka
2024-08-05 14:28 ` Frederic Weisbecker
2024-08-05 14:53 ` Vlastimil Babka
2024-08-05 16:23 ` Frederic Weisbecker
2024-08-05 21:25 ` Vlastimil Babka
2024-08-05 23:59 ` Frederic Weisbecker
2024-08-06 11:08 ` Vlastimil Babka
2024-07-26 21:56 ` [RFC PATCH 13/20] mm: Make Kcompactd use kthread's " Frederic Weisbecker
2024-07-31 15:03 ` Vlastimil Babka
2024-07-26 21:56 ` [RFC PATCH 14/20] mm: Allocate kcompactd on its node Frederic Weisbecker
2024-07-31 15:07 ` Vlastimil Babka
2024-08-05 14:30 ` Frederic Weisbecker
2024-07-26 21:56 ` [RFC PATCH 15/20] mm: Make kswapd use kthread's preferred affinity Frederic Weisbecker
2024-07-31 15:08 ` Vlastimil Babka
2024-07-26 21:56 ` [RFC PATCH 16/20] mm: Allocate kswapd on its node Frederic Weisbecker
2024-07-31 15:08 ` Vlastimil Babka
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=f977c691-4d51-4b23-90b1-8ebcd1f36d73@suse.cz \
--to=vbabka@suse.cz \
--cc=akpm@linux-foundation.org \
--cc=boqun.feng@gmail.com \
--cc=frederic@kernel.org \
--cc=joel@joelfernandes.org \
--cc=kees@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=mhocko@kernel.org \
--cc=neeraj.upadhyay@kernel.org \
--cc=paulmck@kernel.org \
--cc=peterz@infradead.org \
--cc=qiang.zhang1211@gmail.com \
--cc=rcu@vger.kernel.org \
--cc=tglx@linutronix.de \
/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