From: "Eric W. Biederman" <ebiederm@xmission.com>
To: Linus Torvalds <torvalds@linux-foundation.org>
Cc: Tejun Heo <tj@kernel.org>, Petr Mladek <pmladek@suse.com>,
Lai Jiangshan <jiangshanlai@gmail.com>,
Michal Hocko <mhocko@suse.com>,
Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
Peter Zijlstra <peterz@infradead.org>,
Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@redhat.com>,
Andrew Morton <akpm@linux-foundation.org>,
Oleg Nesterov <oleg@redhat.com>
Subject: Re: re. Spurious wakeup on a newly created kthread
Date: Sat, 25 Jun 2022 12:36:35 -0500 [thread overview]
Message-ID: <874k0863x8.fsf@email.froward.int.ebiederm.org> (raw)
In-Reply-To: <CAHk-=wiC7rj1o7vTnYUPfD7YxAu09MZiZbahHqvLm9+Cgg1dFw@mail.gmail.com> (Linus Torvalds's message of "Sat, 25 Jun 2022 10:01:35 -0700")
Linus Torvalds <torvalds@linux-foundation.org> writes:
> On Fri, Jun 24, 2022 at 10:00 PM Tejun Heo <tj@kernel.org> wrote:
>>
>> So, Petr debugged a NULL deref in workqueue code to a spurious wakeup
>> on a newly created kthread.
>
> What? No. That patch can't be right for several reasons.
>
> What we call "spurious wakeups" exist, but they are about wakeups that
> happen from being on a _previous_ wait-queue, and having already been
> removed from it.
>
> They aren't "really" spurious, they are just asynchronous enough (and
> thus unexpected) that you basically should never have a "sleep on
> wait-queue" without then looping and re-testing the condition.
>
> There is no _truly_ spurious wakeup. You were always woken up for a
> reason, it's just that there are more reasons than the entirely
> obvious ones.
>
> For example, the reason that quoted patch cannot be right is that this
> code pattern:
>
> while (wait_for_completion_interruptible(&worker->ready_to_start))
> ;
>
> is not valid kernel code. EVER. There is absolutely no way that can be correct.
>
> Either that code can take a signal, or it cannot. If it can take a
> signal, it had better react to said signal. If it cannot, it must not
> use an interruptble sleep - since now that loop turned into a
> kernel-side busy-loop.
>
> So NAK on this kind of crazy "I don't know what happened, so I'll just
> add *more* bugs to the code" voodoo programming.
>
> And no, we don't "fix" that by then adding a timeout.
>
> Stop this "add random code" thing.
>
> If you cannot be woken up before X happens, then you should:
>
> - don't go to sleep before X happens
>
> - don't add yourself to any wait-queues before X happens
>
> - don't expose your process to others before X happens
>
> The solution is *not* to add some completion with random "wait for
> this before waking".
>
> I think the problem here is much more fundamental: you expect a new
> thread to not wake up until you've told it to.
>
> We do have that infrastructure in the kernel: when you create a new
> thread, you can do various setup, and the thread won't actually run
> until you do "wake_up_new_task()" on it.
>
> However, that's not how kernel_thread() (or kernel_clone()) works.
> Those will call wake_up_new_task(p) for you, and as such a new kernel
> thread will immediately start running.
>
> So I think the expectations here are entirely wrong. I think
> create_worker() is fundamentally buggy, in how it does that
>
> /* start the newly created worker */
> ..
> wake_up_process(worker->task);
>
> because that wake_up_process() is already much too late. The process
> got woken up already, because it was created by create_kthread() ->
> kernel_thread() -> kernel_clone, which does that wake_up_new_task()
> and it starts running.
>
> If you want to do thread setup *bnefore* the kernel is running, it
> needs to be done before that wake_up_new_task().
>
> That's very possible. Look at what create_io_thread() does, for
> example: it never calls wake_up_new_process() at all, and leaves that
> to the caller, which has done the extra setup.
>
> Or the kernel_clone_args could have a "init" function that gets called
> before doing the wake_up_new_task() is done. Or a number of other
> solutions.
>
> But no, we're not randomly adding some new completion because people
> were confused and thought they were waking things up when it was
> already awake from before.
Ugh. The use of kthread_bind_mask is buggy and kthread_bind_mask and
kthread_bind look buggy by definition. As far as I know the kind of
games that involve wait_task_inactive are only safe in special stop
states which TASK_UNINTERRUPTIBLE is not.
I don't know if we have any current code in the kernel that does this
but I have seen code proposed that for suspend/resume that would wake up
everything not in a special stop state, and have every kernel task
figure out if it should be sleeping or not after such a resume.
This implies that kthread_park needs to be used for this logic to work
at all. So the only legitimate user of the __kthread_bind and
__kthread_bind_mask appears to be kthread_unpark.
Let me suggest someone create a new variant of kthread_create that takes
all of the parameters the workqueue code wants to set.
Hopefully that will be enough that we can use it to remove the handful
of buggy cases that call kthread_bind_mask and kthread_bind.
Eric
next prev parent reply other threads:[~2022-06-25 17:36 UTC|newest]
Thread overview: 51+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-06-22 14:08 [PATCH] workqueue: Make create_worker() safe against spurious wakeups Petr Mladek
2022-06-23 7:00 ` Petr Mladek
2022-06-23 7:14 ` Michal Hocko
2022-06-25 5:00 ` re. Spurious wakeup on a newly created kthread Tejun Heo
2022-06-25 17:01 ` Linus Torvalds
2022-06-25 17:36 ` Eric W. Biederman [this message]
2022-06-25 18:25 ` Linus Torvalds
2022-06-25 18:43 ` Linus Torvalds
2022-06-25 23:28 ` Eric W. Biederman
2022-06-25 23:41 ` Eric W. Biederman
2022-06-25 23:43 ` Linus Torvalds
2022-06-25 23:48 ` Linus Torvalds
2022-06-26 0:19 ` Eric W. Biederman
2022-06-27 0:01 ` Wedson Almeida Filho
2022-06-27 7:11 ` Peter Zijlstra
2022-06-27 18:23 ` Wedson Almeida Filho
2022-06-27 18:45 ` Linus Torvalds
2022-06-26 19:14 ` [PATCH 0/3] kthread: Stop using TASK_UNINTERRUPTIBLE Eric W. Biederman
2022-06-26 19:15 ` [PATCH 1/3] kthread: Remove the flags argument from kernel_thread Eric W. Biederman
2022-06-26 21:20 ` Linus Torvalds
2022-06-26 19:16 ` [PATCH 2/3] kthread: Replace kernel_thread with new_kthread Eric W. Biederman
2022-06-26 19:16 ` [PATCH 3/3] kthread: Stop abusing TASK_UNINTERRUPTIBLE (INCOMPLETE) Eric W. Biederman
2022-06-26 19:59 ` Linus Torvalds
2022-06-26 20:23 ` Tejun Heo
2022-06-26 20:55 ` Linus Torvalds
2022-06-27 7:22 ` Peter Zijlstra
2022-06-27 8:11 ` Tejun Heo
2022-06-27 18:04 ` Wedson Almeida Filho
2022-06-27 22:06 ` Peter Zijlstra
2022-06-27 22:34 ` Linus Torvalds
2022-06-27 22:45 ` Wedson Almeida Filho
2022-06-28 0:32 ` Wedson Almeida Filho
2022-06-28 7:58 ` Peter Zijlstra
2022-06-30 0:57 ` Wedson Almeida Filho
2022-06-26 22:14 ` kernel test robot
2022-06-26 22:34 ` kernel test robot
2022-06-26 0:21 ` re. Spurious wakeup on a newly created kthread Eric W. Biederman
2022-06-28 14:16 ` Christian Brauner
2022-06-26 0:26 ` Eric W. Biederman
2022-06-26 1:58 ` Tejun Heo
2022-06-26 2:53 ` Linus Torvalds
2022-06-26 6:09 ` Tejun Heo
2022-06-27 12:04 ` Michal Hocko
2022-06-28 9:51 ` Petr Mladek
2022-06-28 10:07 ` Tejun Heo
2022-06-27 8:07 ` Michal Hocko
2022-06-27 8:21 ` Tejun Heo
2022-06-27 10:18 ` Michal Hocko
2022-06-28 15:08 ` Petr Mladek
2022-08-04 8:57 ` [PATCH] workqueue: Make create_worker() safe against spurious wakeups Lai Jiangshan
2022-08-04 10:19 ` Lai Jiangshan
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=874k0863x8.fsf@email.froward.int.ebiederm.org \
--to=ebiederm@xmission.com \
--cc=akpm@linux-foundation.org \
--cc=jiangshanlai@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mhocko@suse.com \
--cc=mingo@redhat.com \
--cc=oleg@redhat.com \
--cc=peterz@infradead.org \
--cc=pmladek@suse.com \
--cc=tglx@linutronix.de \
--cc=tj@kernel.org \
--cc=torvalds@linux-foundation.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