From patchwork Thu Jun 15 11:38:12 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: 108462 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp588543vqr; Thu, 15 Jun 2023 05:17:23 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ6ZxRbUFsvf/51g9Mo8HT9RnVyTNfE1Oxub4Uijd62bqygADetNo7N/t2gVr9h4Qi058aRE X-Received: by 2002:a17:903:32cc:b0:1ad:b5b4:e424 with SMTP id i12-20020a17090332cc00b001adb5b4e424mr17607476plr.38.1686831442755; Thu, 15 Jun 2023 05:17:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686831442; cv=none; d=google.com; s=arc-20160816; b=F55HoUXxVMl+5xyj/TYff/ZvKSCZfpOzSvcfnlV4JdmLsW5IPUkiZ/eWivwgBS05wA 6LqHdzyVvAy78rr8GdXrIZgjB335Eg8Q0CnfiCbiEl4H+Y6io5b9esKyeuTT+LjfAZ38 WVZZooZU+LvkPiV1koIwAue8kd0MrByQPV4qRrHvn/KNnKRePmM4xp2YjfMzUQ6PwhKS A1zd+ynGOsbS+7Nlpxf8Y7F26VOiz66s3YTpEAkH9S4GmCU5O9tDjWxYauLyoIZHVTla r5QOdOktCngQPRPnH/5ncZPrMCsdFoG2+f78f2BHcYH7HOZX0CyPmaSF/5DKdGExfRsR RyjA== 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=QTaI/jPZ+a/Pf15JySdI0RMrIx//5I7shAXr7QX8aj0=; b=PrYAomaqhR/CHCSNUhuQzI2RmSFpF+WBxjCD+G9mLrRKlIvLVztd7EVWP/pCLrJOnO Icz2iW74wnmmXkJJvsCRoTvSzcx7Fllcmwj/Kk5ST43hn0jw+FeGyJyosmoWtYCkuUFF 1kYjAFUAGrnpkXRWnUwMHl/wvf6MpO+dD7/t6zqOfWMh40SbCnL4QNNgGQf/i3Sc0WL+ 6RPT+cJHJiKR/xpjV3+pDHEp3nErrDTdrYIDjTCREosNnFvI6IlheC0mRGILcz8jRH1B 3seX9fCAvplVl1f5Anf6XcPcuDTwf0Ky9h0B17GNy/eAGyTS71mbs4l2B0rgn2sArDnm K/UA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=vb5gO97y; dkim=neutral (no key) header.i=@linutronix.de; 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 e8-20020a170902ef4800b001ab18eacb8csi12935289plx.526.2023.06.15.05.17.09; Thu, 15 Jun 2023 05:17:22 -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=vb5gO97y; dkim=neutral (no key) header.i=@linutronix.de; 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 S1344043AbjFOLkQ (ORCPT + 99 others); Thu, 15 Jun 2023 07:40:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52812 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343946AbjFOLiz (ORCPT ); Thu, 15 Jun 2023 07:38:55 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 32A9430F2; Thu, 15 Jun 2023 04:38:15 -0700 (PDT) Date: Thu, 15 Jun 2023 11:38:12 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686829093; 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=QTaI/jPZ+a/Pf15JySdI0RMrIx//5I7shAXr7QX8aj0=; b=vb5gO97yB+2BrODwrlonZV1VQGqdebUak+lUPZS5VR7Eg0SN81CvL+1/Ac7QWSeNBXgm6h 9JSqn+Wv83o75u1yb4sHPmzu+m7KwPkHehPljqBQ4yxAITUZetV5Vyt68MJwccwXP8eMJt zKHrU0b0wyOZivu9DkIlGZm+Q8e7tICX6SECIQ/bDtrd/c1NMpx5zzsLb8Ls9rRB741vN8 aRHluWGq+6jFmLcGcK8eFwaZB+IXwpLFoN+Kkwx/rsxZYFpqgA8CDxzDfBrwhhH4nBNNt4 uL5gg1NYqIItfvBQptqkSu4VlvclyzmvsuamDQIUIbfs45AL1d5FyBUYzPIUrA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686829093; 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=QTaI/jPZ+a/Pf15JySdI0RMrIx//5I7shAXr7QX8aj0=; b=2wzcfvFrVMIHLshOe9k55rNwzjoVW1MnzpMbZGblJ1vcKRo/dBGGk2Z6ttR5HLMkKYj80a KP4aVcQali0mQ9Dw== 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: <168682909289.404.8831438391422157920.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 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?1768770966886361262?= The following commit has been merged into the sched/core branch of tip: Commit-ID: 4b85308dac750b16bd273b0f4e9c9b478fbb886b Gitweb: https://git.kernel.org/tip/4b85308dac750b16bd273b0f4e9c9b478fbb886b Author: Arve Hjønnevåg AuthorDate: Fri, 02 Jun 2023 21:23:46 Committer: Peter Zijlstra CommitterDate: Thu, 15 Jun 2023 13:28:21 +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);