Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp2910695iog; Mon, 27 Jun 2022 05:35:32 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sNVqiJAfr4Ze+JzAvveTxUZOhcO1+horVbWrfCnLrcvOJ74b8nLITauofWNy2mayP3EDn0 X-Received: by 2002:a05:6a00:811:b0:525:50c2:4c2f with SMTP id m17-20020a056a00081100b0052550c24c2fmr14774329pfk.62.1656333332270; Mon, 27 Jun 2022 05:35:32 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656333332; cv=none; d=google.com; s=arc-20160816; b=GiZ02SBSeK/lmTjkQP2aOou7y6Ski8XlyvpTtV+6xdiMLH4Oa8AWpi+FTp0pFi6lKN 7i/rybvHgFmot+yugu+wG3L59Nruc1r9TGRTS3fdvxAUjZH/L4Z3oFmyjxpO+SjOAh3+ PeexatOkaXDDuzo08Udo6g9N0UgpuLQzcruVWVwB3BXMZynt1ZcV47+5rqbDo/MrBmRJ y6VfxsQHQrPaZZhgrHc9zZDgwRBji1FemE4zO6CNRCLcS4JaXVTb+vWidgDlbHV1LFCV zE/4v3ol96DtkzDYWHQE50B6WaEi54Lmki/zibgq8RjXjma7KU19ekTCIutrZ8lm5XQh pSnA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=IknD+S/Ug98rdUWi3kX5IaMecoyMVDRuNUY4RiPkYJ8=; b=yA6uoz+BSnknCENayKVHiFgMTj8Atca3/9tSv75cSvLSvNEFPIYFS5OXufzE6X5SHO YDBl/HqAwyVsm9fXlEzQxWOcNCvCEvNLGVIrQCfQ85PEbic9j1mftcZww3pCJfOO+zsT GI9zTQEWqxLa2YYH7fxuvGuD/xwgk8NPnHGZDwq5s8o8k+RKfHO1MLK8fBFCLb2E39QG yTA3WH1VXKQJis3CG4ZQGSz8XuM3FlYttmGiqv2vGTA/CP7sU2KzL2S8brO+/NFpaiJB ziyCXqQYPEVrmglyjG/mxDj7kdL4BiV7HPPR4OHPJ3ONsq3AVOIfucsvcnWLUSp8zKDw jQgw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=KT6jK2oS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id pc11-20020a17090b3b8b00b001eaac606001si18061495pjb.118.2022.06.27.05.35.16; Mon, 27 Jun 2022 05:35:32 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=KT6jK2oS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239881AbiF0MGv (ORCPT + 99 others); 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: X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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