From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id C5237C433EF for ; Mon, 27 Jun 2022 12:06:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235543AbiF0MGv (ORCPT ); Mon, 27 Jun 2022 08:06:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41700 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239618AbiF0MGg (ORCPT ); Mon, 27 Jun 2022 08:06:36 -0400 Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.220.29]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BADF111468 for ; Mon, 27 Jun 2022 05:04:06 -0700 (PDT) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id 7BCAE1FDA0; Mon, 27 Jun 2022 12:04:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1656331445; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=IknD+S/Ug98rdUWi3kX5IaMecoyMVDRuNUY4RiPkYJ8=; b=KT6jK2oSaruv2td+ar4LCoLADmz+XlR4XIPPouBlmDHNm5MenzMvirj6dosmqzTKQKTJXN MOnDNu8w2JZJcC4/5SoU3rlH7jzCeDoYzZD4Gc6z1Ms5rzdrtJOCwQgf2cznGfTJb17XWc ysYCeX3YDd4t/+iB5sv/eYlzdfCqTvw= Received: from suse.cz (unknown [10.100.201.86]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 554B62C143; Mon, 27 Jun 2022 12:04:05 +0000 (UTC) Date: Mon, 27 Jun 2022 14:04:02 +0200 From: Michal Hocko To: Linus Torvalds Cc: Tejun Heo , Petr Mladek , Lai Jiangshan , Linux Kernel Mailing List , Peter Zijlstra , Thomas Gleixner , Ingo Molnar , Andrew Morton , Oleg Nesterov , "Eric W. Biederman" Subject: Re: re. Spurious wakeup on a newly created kthread Message-ID: References: <20220622140853.31383-1-pmladek@suse.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat 25-06-22 19:53:34, Linus Torvalds wrote: > On Sat, Jun 25, 2022 at 6:58 PM Tejun Heo wrote: [...] > > * If there are no true spurious wakeups, where did the racing wakeup > > come from? The task just got created w/ TASK_NEW and woken up once > > with wake_up_new_task(). It hasn't been on any wait queue or > > advertised itself to anything. > > I don't think it was ever a spurious wakeup at all. > > The create_worker() code does: > > worker->task = kthread_create_on_node(.. > .. > worker_attach_to_pool(worker, pool); > .. > wake_up_process(worker->task); > > and thinks that the wake_up_process() happens after the worker_attach_to_pool(). > > But I don't see that at all. > > The reality seems to be that the wake_up_process() is a complete > no-op, because the task was already woken up by > kthread_create_on_node(). Just for the record. the newly created thread is not running our thread function at this stage. It is rather subtle and took me some time to decypher but kthread_create_on_node will create and wake up kernel thread running kthread() function: [...] /* * Thread is going to call schedule(), do not preempt it, * or the creator may spend more time in wait_task_inactive(). */ preempt_disable(); complete(done); schedule_preempt_disabled(); preempt_enable(); ret = -EINTR; if (!test_bit(KTHREAD_SHOULD_STOP, &self->flags)) { cgroup_kthread_ready(); __kthread_parkme(self); ret = threadfn(data); } so the newly created thread will go into sleep before calling the threadfn (worker_thread here). Somebody must have woken it up other than create_worker. I couldn't have found out who that was (see my other email with some notes from the crash dump). I do agree that a simple schedule without checking for a condition is dubious, fragile and wrong. If anything wait_for_completion would be less confusing and targetted waiting. Petr has added that completion into worker_thread to address this specific case and a better fix would be to address all callers because who knows how many of those are similarly broken. I also do agree that this whole scheme is rather convoluted and having an init() callback to be executed before threadfn would be much more easier to follow. -- Michal Hocko SUSE Labs