From patchwork Fri Jun 16 15:16:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 109220 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1444620vqr; Fri, 16 Jun 2023 09:00:12 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ4UOXW8vVxIHUON40BF7Y/Fcde6daK4ic6Chw6GKFwSIenQ2f8tDtyTVRUHcAvToTfATuiu X-Received: by 2002:a9d:7686:0:b0:6b4:5358:8a07 with SMTP id j6-20020a9d7686000000b006b453588a07mr1234127otl.9.1686931211081; Fri, 16 Jun 2023 09:00:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686931211; cv=none; d=google.com; s=arc-20160816; b=hpDp7sJ3HXR0e9NsbnwuQJrVJ+O9n8gqGR6SUXliJlKFpiRPG8lx8SIsbWBr3DK1yU QDF8RUbhHCh2i7JiVARvpmUELbNiU3yTeHFhYDjC4mKoG6OaKzQbL375GImkl0FNaOLj VoCWLyqB03lmX/JM7c1aLY5mwI8qMdG2jNJ613OrpXZSGn62GHegKEZC1KUMBgIHHRT2 BofxYFwHZbBNgFYNwom6c3vqKirmS3J9v2yimI2YY5JY9I8+W/S2JSOmO63+9YeV+piS odyd7hw9vBxQ9CyZEm8jj0cxSAQt4CEruiZmKW9fCGzAAQusUFe1Pc9Zs4bw49HnM5sG vRpg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:references:in-reply-to:cc:subject :to:reply-to:sender:from:dkim-signature:dkim-signature:date; bh=OhPj1jzUP7z+D2MYdptA9f+z018Cf2TbfZdtWXdgl8A=; b=pXfwKEejL+KKD3yotXwTLPhI70UKHDpAvphr1fY905y19AD5xkL/bnIJc45diaVroK Br6QCTr5GLCiV0+EUYp+WwFYZKCmZmLU4cxsqAxPUD/KydHU1X6phoKd74fBjZ7Ph5Bo cprdm+7j/tdJ0zrTgvtmgSlp8xRauAeZWS8VE3IZ3JzY/LRJdjJd/HsH97DorBpRoDMd NRD4ami2uK0mxFSRidTneOkWdoL72UvvLD0q/7VtookX3ZueKhN+UZkcRdI3GhrTfaiR tpdDpxDFjiP6nx1oqj3m0e6foEAp7IkbJH9Z/XBzQ1UmueVukyZYoIe4TGmzRvUZoRCM 0uhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Llgvm61z; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id v6-20020a17090a898600b002564edc1320si1911553pjn.51.2023.06.16.08.59.58; Fri, 16 Jun 2023 09:00:11 -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=@linutronix.de header.s=2020 header.b=Llgvm61z; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345694AbjFPPQq (ORCPT + 99 others); Fri, 16 Jun 2023 11:16:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40158 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230027AbjFPPQo (ORCPT ); Fri, 16 Jun 2023 11:16:44 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 55ABB273C; Fri, 16 Jun 2023 08:16:43 -0700 (PDT) Date: Fri, 16 Jun 2023 15:16:41 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686928602; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OhPj1jzUP7z+D2MYdptA9f+z018Cf2TbfZdtWXdgl8A=; b=Llgvm61zlunkPGHzvxKJ8lt8dppLl30fk4joaGmYdKU3j6+qge6/PffLiE8O4YZ2/MrYVK a9hTtlxG6ud6UdUoMZV0Vm0cDvzgze6syAhxrtq+KtV9NYahBXn2znhbSwOvEP2oDC56oj HcvRI49Id9AAhotmZ8HARzTk3NFqih0eRGjCAstVfKI/5/CHbqtUKayGe6eRCpsfym4gc7 wWxfwJ2UJVYP338thK+czwToWwHpXiBcGd+41fKTEePETFmbBBNAth+hkoISR1WtS0OEkd 0kfDnfDqM0V4jyvn0AGmnzx/YFEu70OvUTRynFhYO92P7OEzjcTLIz4FlB6T5Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686928602; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=OhPj1jzUP7z+D2MYdptA9f+z018Cf2TbfZdtWXdgl8A=; b=FXTLb0pmVUSdYdEgnikYKKrLsP+RJoL7SHJWMBxNkzDg8+pWXZILxUBN+l+GCqe7iceFLQ pspR4piFcQ/t1hCw== From: tip-bot2 for Arve =?utf-8?b?SGrDuG5uZXbDpWc=?= Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: sched/core] sched/wait: Fix a kthread_park race with wait_woken() Cc: arve@android.com, John Stultz , "Peter Zijlstra (Intel)" , Valentin Schneider , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: <20230602212350.535358-1-jstultz@google.com> References: <20230602212350.535358-1-jstultz@google.com> MIME-Version: 1.0 Message-ID: <168692860128.404.10971094774968505077.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails 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,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1767628544239328088?= X-GMAIL-MSGID: =?utf-8?q?1768875581283684359?= The following commit has been merged into the sched/core branch of tip: Commit-ID: ef73d6a4ef0b35524125c3cfc6deafc26a0c966a Gitweb: https://git.kernel.org/tip/ef73d6a4ef0b35524125c3cfc6deafc26a0c966a Author: Arve Hjønnevåg AuthorDate: Fri, 02 Jun 2023 21:23:46 Committer: Peter Zijlstra CommitterDate: Fri, 16 Jun 2023 17:08:01 +02:00 sched/wait: Fix a kthread_park race with wait_woken() 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. [jstultz: Made changes suggested by Peter to optimize memory loads] Signed-off-by: Arve Hjønnevåg Signed-off-by: John Stultz Signed-off-by: Peter Zijlstra (Intel) Reviewed-by: Valentin Schneider Link: https://lore.kernel.org/r/20230602212350.535358-1-jstultz@google.com --- 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 30e5bec..f1f95a7 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 490792b..07a0570 100644 --- a/kernel/kthread.c +++ b/kernel/kthread.c @@ -182,6 +182,16 @@ bool kthread_should_park(void) } EXPORT_SYMBOL_GPL(kthread_should_park); +bool kthread_should_stop_or_park(void) +{ + struct kthread *kthread = __to_kthread(current); + + if (!kthread) + return false; + + return kthread->flags & (BIT(KTHREAD_SHOULD_STOP) | BIT(KTHREAD_SHOULD_PARK)); +} + /** * kthread_freezable_should_stop - should this freezable kthread return now? * @was_frozen: optional out parameter, indicates whether %current was frozen diff --git a/kernel/sched/wait.c b/kernel/sched/wait.c index 133b747..48c53e4 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); -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, unsigned 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 = schedule_timeout(timeout); __set_current_state(TASK_RUNNING);