Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp2771624rwd; Fri, 2 Jun 2023 14:38:57 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5U3nj6iYq9NgUq8j1r4WU8zu8GddpeFRPrNQyLF01YNqfZm1eUg2+QtDN3QIs6RnhGVFBr X-Received: by 2002:a05:6830:144:b0:6af:9589:d9a7 with SMTP id j4-20020a056830014400b006af9589d9a7mr3625006otp.16.1685741936794; Fri, 02 Jun 2023 14:38:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1685741936; cv=none; d=google.com; s=arc-20160816; b=tdt1Q0FLGLp1KuBoG71Ozd1hRsHjeeU1nkocjGXBdRnFMEbIyNGs4a/MQvxhtSa0GL Y+0w7Y9PSyADtXNZVhiNyIHXVYxmEGD38TXM9QVuzuePJBKG8QIKeaEgsFNAu6J8FZcM 31suMIVE5gu5nrw4W315YUI4gmytAwflEIo1XYq9T5pmpgm1WaWCGTDsd4GjFKPDyj7O +Lqu9geu/BRx3UIWQX7yoIxN8HLEvpCQXmFRcGCiNPlGSKpfLpXRI3uFcT4QIlqzRVB0 7r2p/Ms7MrjmIwi57Bkz3tt0HdSMv6oV38Kw+lis7D6nxeHbuwH+P2yEykNw1Kazi6IO 2cZQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:from:subject :message-id:mime-version:date:dkim-signature; bh=L/1Q2qDDMWaACRPVl1R6+9lO9W70yn5VWzLSuHliJyk=; b=M4DhG6EgBxpmxHyKk16Z8NARfNViD+0RRRyEzTvr/uBovw1451XDF+p34iA7gqNeNE 8A4R6X+62UYIlWMkYmDrFL3O7C6TMCKwc+PNCnGZaSXPuqOM94TOKWxeVe5wq2RifUsA GpibnSFXLy8zTfniBnzb0BTxSODzeS2YteCjZQydEWrttgWZRDSL/Uc41OCqz9sytMKa Lqkp3aRkCYGU2ledwOutklQJYCCsKwlYWLSsmv8Kq9UhU80K2aZHGHPoh3OtT1VoPf0y v5IP7iztwN6iOq3j0l93wy1HMKAMvl+L4dEQ0VUuxsQbIU9/trskA2FnTCOkzZHtxO0Y mPLQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20221208 header.b=VlmUMMHE; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id k69-20020a638448000000b00542b9f14ca7si629638pgd.24.2023.06.02.14.38.44; Fri, 02 Jun 2023 14:38:56 -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=@google.com header.s=20221208 header.b=VlmUMMHE; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236422AbjFBVX4 (ORCPT + 99 others); Fri, 2 Jun 2023 17:23:56 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235681AbjFBVXz (ORCPT ); Fri, 2 Jun 2023 17:23:55 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5FA9E45 for ; Fri, 2 Jun 2023 14:23:54 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id 98e67ed59e1d1-258aad18260so1593135a91.1 for ; Fri, 02 Jun 2023 14:23:54 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1685741034; x=1688333034; h=content-transfer-encoding:cc:to:from:subject:message-id :mime-version:date:from:to:cc:subject:date:message-id:reply-to; bh=L/1Q2qDDMWaACRPVl1R6+9lO9W70yn5VWzLSuHliJyk=; b=VlmUMMHEiDO1nG8h/zvGub43Mqcm0B736wIlUkxKK+ArEJ+wnz+hVFtjbLq5aUi8K4 uWJt9fFM3KhseewsCZLxak+1pSd3Zd4m8IMQVAyDe0MYYUnbNljGzM2TRUgZTO/opB4N YD9RmUNQYR2wiEVEtivLuqBA6FTfKAW78ABjcDMZqpAbk0aCv3+5aanFo+1rdH2v9Viz qI/wbb8Dq2yyhLFpUlr1WIdQOKaiUMTCSn0YuVMYqnF2nbNbOGr3jOTjRnISBI9qfIjq Xn3fDGaL/p3XEz/tWZDjiuVRkWqyZQRj3dMgXBjlf9oz2sMl648euk4P9ZhxcIQmLTDU eEyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685741034; x=1688333034; h=content-transfer-encoding:cc:to:from:subject:message-id :mime-version:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=L/1Q2qDDMWaACRPVl1R6+9lO9W70yn5VWzLSuHliJyk=; b=MTX9PLQdFLaQRDfD6Ike3i9Ror/0mJZGVhLPCz8v+ZRduKZQvbQWJKRCqppdnszFNv moxuk+Sb6ng4717eJumPYFZvZYrt0V4WaHsbHRmF1YcxtCS31RxgZFZEP0ANj+Rk7PG9 hxjpCS4v52mQboo1SCiKPa4zvMsSRTGoRZyFXFBe8GQILmyY11vwz9pueStY8d230KFz hiQVYIeQhIU5b+GQjYkvNOe9rzk0ukemFY6jumNTxSkR+nzVSwAFZtdSJe8nMCbhTjDr qPeoqni1071egSX8owrAh8qmHQjZwLCL0wieMe+5k5982D2C+rZV9e8Jn7OYXYaIlGP0 ZzaQ== X-Gm-Message-State: AC+VfDzF1PlswkbxHlFnB+qc92poWMJvyMZVqm4N3lS72x1b8qv2DJsJ 0N3imb5d4H2GpqK3YE50jdzoMzH1x/DlMel/w3YPZdAOPCJyNOyI097ODlHn/O6VtAmonM0Zvb8 Znlqm+qS4obaggYm5ggbT9adhkgTYnL+DSZ5NHdMwCsuXq0T5i5Ejw2p/622mljNiEOItkG8= X-Received: from jstultz-noogler2.c.googlers.com ([fda3:e722:ac3:cc00:24:72f4:c0a8:600]) (user=jstultz job=sendgmr) by 2002:a17:90a:55ce:b0:253:32b1:e567 with SMTP id o14-20020a17090a55ce00b0025332b1e567mr227725pjm.2.1685741034033; Fri, 02 Jun 2023 14:23:54 -0700 (PDT) Date: Fri, 2 Jun 2023 21:23:46 +0000 Mime-Version: 1.0 X-Mailer: git-send-email 2.41.0.rc2.161.g9c6817b8e7-goog Message-ID: <20230602212350.535358-1-jstultz@google.com> Subject: [RESEND][PATCH v2] sched/wait: Fix a kthread_park race with wait_woken() From: John Stultz To: LKML Cc: "=?UTF-8?q?Arve=20Hj=C3=B8nnev=C3=A5g?=" , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Daniel Bristot de Oliveira , Valentin Schneider , android-kernel-team , John Stultz Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL 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 From: Arve Hj=C3=B8nnev=C3=A5g kthread_park and wait_woken have a similar race that kthread_stop and wait_woken used to have before it was fixed in commit cb6538e740d7 ("sched/wait: Fix a kthread race with wait_woken()"). Extend that fix to also cover kthread_park. Cc: Ingo Molnar Cc: Peter Zijlstra Cc: Juri Lelli Cc: Vincent Guittot Cc: Dietmar Eggemann Cc: Steven Rostedt Cc: Ben Segall Cc: Mel Gorman Cc: Daniel Bristot de Oliveira Cc: Valentin Schneider Cc: android-kernel-team Signed-off-by: Arve Hj=C3=B8nnev=C3=A5g [jstultz: Made changes suggested by Peter to optimize memory loads] Signed-off-by: John Stultz --- v2: * Commit message tweaks, suggested by Peter * Move logic to kthread.c & optimize to avoid duplicate memory loads, also suggested by Peter --- include/linux/kthread.h | 1 + kernel/kthread.c | 10 ++++++++++ kernel/sched/wait.c | 7 +------ 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/include/linux/kthread.h b/include/linux/kthread.h index 30e5bec81d2b..f1f95a71a4bc 100644 --- a/include/linux/kthread.h +++ b/include/linux/kthread.h @@ -89,6 +89,7 @@ int kthread_stop(struct task_struct *k); bool kthread_should_stop(void); bool kthread_should_park(void); bool __kthread_should_park(struct task_struct *k); +bool kthread_should_stop_or_park(void); bool kthread_freezable_should_stop(bool *was_frozen); void *kthread_func(struct task_struct *k); void *kthread_data(struct task_struct *k); diff --git a/kernel/kthread.c b/kernel/kthread.c index 490792b1066e..07a057086d26 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -182,6 +182,16 @@ bool kthread_should_park(void) } EXPORT_SYMBOL_GPL(kthread_should_park); =20 +bool kthread_should_stop_or_park(void) +{ + struct kthread *kthread =3D __to_kthread(current); + + if (!kthread) + return false; + + return kthread->flags & (BIT(KTHREAD_SHOULD_STOP) | BIT(KTHREAD_SHOULD_PA= RK)); +} + /** * kthread_freezable_should_stop - should this freezable kthread return no= w? * @was_frozen: optional out parameter, indicates whether %current was fro= zen diff --git a/kernel/sched/wait.c b/kernel/sched/wait.c index 133b74730738..48c53e4739ea 100644 --- a/kernel/sched/wait.c +++ b/kernel/sched/wait.c @@ -425,11 +425,6 @@ int autoremove_wake_function(struct wait_queue_entry *= wq_entry, unsigned mode, i } EXPORT_SYMBOL(autoremove_wake_function); =20 -static inline bool is_kthread_should_stop(void) -{ - return (current->flags & PF_KTHREAD) && kthread_should_stop(); -} - /* * DEFINE_WAIT_FUNC(wait, woken_wake_func); * @@ -459,7 +454,7 @@ long wait_woken(struct wait_queue_entry *wq_entry, unsi= gned mode, long timeout) * or woken_wake_function() sees our store to current->state. */ set_current_state(mode); /* A */ - if (!(wq_entry->flags & WQ_FLAG_WOKEN) && !is_kthread_should_stop()) + if (!(wq_entry->flags & WQ_FLAG_WOKEN) && !kthread_should_stop_or_park()) timeout =3D schedule_timeout(timeout); __set_current_state(TASK_RUNNING); =20 --=20 2.41.0.rc2.161.g9c6817b8e7-goog