From patchwork Tue Feb 20 17:14:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: K Prateek Nayak X-Patchwork-Id: 774782 Received: from NAM12-BN8-obe.outbound.protection.outlook.com (mail-bn8nam12on2069.outbound.protection.outlook.com [40.107.237.69]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B1784626C6; Tue, 20 Feb 2024 17:16:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.237.69 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708449419; cv=fail; b=HDFEG/ouS1psBd5FsumM7U4EglthtgutQa3+DCCct/KLyT2e71KxvSgpbe6vPb7LjQtuOtslP1Cp4Zb1J/fazRAzCPUnSIuJw8HFZiyWHF+tWLtkzy4ZbbIvSApBh1tyC38o60WSroza7vbrDqggWnCYbQt0vxxHC8vCm/Lotpw= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708449419; c=relaxed/simple; bh=nTX0Yqbs+ChiTX9rrkVuJ/FiD1fwWJ2d1FI7naJp33w=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=KBr7ikyC8Q19TlTrw7gW7ey3QQITt/fB5H17Q1NiVBn3b+l5tl2XVfJFvpcOMoqk/JjcBrmRSFDOooevBJxH6/0IurY5XzWUdTknhXuL3q8ShDtg1pdG8uR22KQV+eAROftI0OS9wBm2YIE3LrU4+0qsVQ3irQRePIvyRhZRBcQ= ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com; spf=fail smtp.mailfrom=amd.com; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b=R4B8JA2I; arc=fail smtp.client-ip=40.107.237.69 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amd.com Authentication-Results: smtp.subspace.kernel.org; spf=fail smtp.mailfrom=amd.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=amd.com header.i=@amd.com header.b="R4B8JA2I" ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=IGkWBz2TPnDVod1/AewZap0NV7EfFsj7N1ZIRbBQMupcwSWZyQY7DCSP2nDtpK2FjxQ2gk3AW68tsTB5gxao+3Hz44Zm7KPZjLVP4di0DxkDwu56Q1xCIff22pGHolXIm2g4yjLy/BZ29fx/DSb/cmjdQ33mW5EpL9BrWhY4TQwozZUtab4kYXHL6T0heiCV25PdT2F/QzUhNRaJJDHsiK51lVGPH9s/PM48/zoh7x9A3fnHiuKM43NWjMeUQ39KJmDT4u26RyGVDXCJF4HyCts10skuMag5rqM63COt+y7z/cQEF8UOpAMcYX/iKXO5fpPUp5GuomYPii0ixGGFqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=OQ5Bg8ms8TlJV83NKqR3M1spyrSm2VyPoRqbg+vdDVY=; b=GWA85CBg4a47oZAYm1aACfr+K8T8GG3esui1scEJNak5FeUVCSOp2zIO0Gg9Sp4cZ5xaoGTgFDF36dt7sfgdt43R/ypRKHPSIA5BjbzzmY1BYP7abU4Ejco5H2vIZ337VEORC2uijPfoYHjGazkXZ++lAffByDt8pZ/4MgmfFsuyIxxXIvZ8F7T36VmKw47bZegrXhPwkXLrvS0eUbSWTllwunrO0WqUUV7Q84Fr67Qfdr7mTsKataBnF+3Mq6mgnX2eOG/+Gz4XMtJe+WTm3DVeYvcIBbJzqx0L+k4mv5Tcl4QKHiWZ6s7oUXsKCjnfEdYcYgvbMnMq71lf0QqiYg== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=vger.kernel.org smtp.mailfrom=amd.com; dmarc=pass (p=quarantine sp=quarantine pct=100) action=none header.from=amd.com; dkim=none (message not signed); arc=none (0) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amd.com; s=selector1; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=OQ5Bg8ms8TlJV83NKqR3M1spyrSm2VyPoRqbg+vdDVY=; b=R4B8JA2IbzadnZHtnXAd6+wqwGR6xeo/wqY7So7qk+0A8FAGJbw0V2T7/TaKSJVDTCvV3AoVl6LQWiJdYE/VGmdRtZrBQS6xTKn8GOG8pF3MpdgPGm+p9c/MvtWAihm5AKqYncuS3g2dcfbqAM2ySd7x/DR4j115en5y4I9CYKo= Received: from DM6PR07CA0057.namprd07.prod.outlook.com (2603:10b6:5:74::34) by IA0PR12MB8973.namprd12.prod.outlook.com (2603:10b6:208:48e::18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7316.19; Tue, 20 Feb 2024 17:16:53 +0000 Received: from DS1PEPF00017091.namprd03.prod.outlook.com (2603:10b6:5:74:cafe::d) by DM6PR07CA0057.outlook.office365.com (2603:10b6:5:74::34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.7292.40 via Frontend Transport; Tue, 20 Feb 2024 17:16:53 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 165.204.84.17) smtp.mailfrom=amd.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=amd.com; Received-SPF: Pass (protection.outlook.com: domain of amd.com designates 165.204.84.17 as permitted sender) receiver=protection.outlook.com; client-ip=165.204.84.17; helo=SATLEXMB04.amd.com; pr=C Received: from SATLEXMB04.amd.com (165.204.84.17) by DS1PEPF00017091.mail.protection.outlook.com (10.167.17.133) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.7292.25 via Frontend Transport; Tue, 20 Feb 2024 17:16:53 +0000 Received: from BLR5CG134614W.amd.com (10.180.168.240) by SATLEXMB04.amd.com (10.181.40.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.35; Tue, 20 Feb 2024 11:16:32 -0600 From: K Prateek Nayak To: CC: "Gautham R. Shenoy" , Richard Henderson , Ivan Kokshaysky , Matt Turner , Russell King , "Guo Ren" , Michal Simek , Dinh Nguyen , Jonas Bonn , Stefan Kristiansson , Stafford Horne , "James E.J. Bottomley" , Helge Deller , Michael Ellerman , Nicholas Piggin , Christophe Leroy , "Aneesh Kumar K.V" , "Naveen N. Rao" , Yoshinori Sato , Rich Felker , John Paul Adrian Glaubitz , "David S. Miller" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" , "Rafael J. Wysocki" , Daniel Lezcano , Peter Zijlstra , Juri Lelli , Vincent Guittot , Dietmar Eggemann , Steven Rostedt , Ben Segall , "Mel Gorman" , Daniel Bristot de Oliveira , Valentin Schneider , Al Viro , Linus Walleij , Ard Biesheuvel , Andrew Donnellan , Nicholas Miehlbradt , Andrew Morton , "Arnd Bergmann" , Josh Poimboeuf , "Kirill A. Shutemov" , Rick Edgecombe , Tony Battersby , "Brian Gerst" , Tim Chen , "David Vernet" , , , , , , , , , , , K Prateek Nayak Subject: [RFC PATCH 02/14] sched: Define a need_resched_or_ipi() helper and use it treewide Date: Tue, 20 Feb 2024 22:44:45 +0530 Message-ID: <20240220171457.703-3-kprateek.nayak@amd.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240220171457.703-1-kprateek.nayak@amd.com> References: <20240220171457.703-1-kprateek.nayak@amd.com> Precedence: bulk X-Mailing-List: linux-pm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-ClientProxiedBy: SATLEXMB04.amd.com (10.181.40.145) To SATLEXMB04.amd.com (10.181.40.145) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS1PEPF00017091:EE_|IA0PR12MB8973:EE_ X-MS-Office365-Filtering-Correlation-Id: 4538b806-c25a-4501-e9cd-08dc3237bba1 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: gb3AWsI9V6f7lyIRtbyfoEfeN8iiv3gh+6Rll8d3JYM2FzPIuZpBaUItICl59D5B4OKr824hL7zcbvB5ZGm50UhhsD/Ihhl9eRrP8zPzbhYYV0eQ2hz3ooNM+XwXMmV87douEQtUUlZ5DpJTxR1mplG0P/eURhdzY9+6mXCF5ClNEMdsgWMuKjckxwnKb3pbaGxmOIOgYpmaPyLbqyJfW7MbJvrDqfyWY5+7drXpebbW6qx/eMi6uEs01WEXil3RAeIVNIKL+wDZb5d6+0bM+05SewSJ+Y8PCajl9GmOtVECXGljCte3j9IU2w4TIJBmr2rKlJBWMlpwOHQq7C5Fo5238bLvcsjSNSRCfzmgHcH8fNQ9Kfp0hpT3pHORb8+sUfvvKARD3L9VYH5sBBIG9E90mbOq+ZRzh77kFNmB6IF8j7PpCv9YrNp3uwfv8pk7ukRiGoG8s5ioE/NhNxx126uvmf1f2RokXKh/qeXczd7gwy/2kbiDghldUiNltwCUWJ0VXp8cGuRS/SrFcdH//f/RsDbvHc3imNHQha38CWMwF3diofli8wx4VDJaVHnXSL4SAOEqlvgfinoD2zNoDuMxs7zHl25Ckjp2JvtwLQtnJSb0bs4AzuF8Rb8U9LnvydUAiqiJjOAdDa6yVvZNAJhs1R1VAOmkF/g7aI7RoUs= X-Forefront-Antispam-Report: CIP:165.204.84.17; CTRY:US; LANG:en; SCL:1; SRV:; IPV:CAL; SFV:NSPM; H:SATLEXMB04.amd.com; PTR:InfoDomainNonexistent; CAT:NONE; SFS:(13230031)(36860700004)(46966006)(40470700004); DIR:OUT; SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 20 Feb 2024 17:16:53.1214 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 4538b806-c25a-4501-e9cd-08dc3237bba1 X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=3dd8961f-e488-4e60-8e11-a82d994e183d; Ip=[165.204.84.17]; Helo=[SATLEXMB04.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DS1PEPF00017091.namprd03.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: IA0PR12MB8973 From: "Gautham R. Shenoy" Currently TIF_NEED_RESCHED is being overloaded, to wakeup an idle CPU in TIF_POLLING mode to service an IPI even if there are no new tasks being woken up on the said CPU. In preparation of a proper fix, introduce a new helper "need_resched_or_ipi()" which is intended to return true if either the TIF_NEED_RESCHED flag or if TIF_NOTIFY_IPI flag is set. Use this helper function in place of need_resched() in idle loops where TIF_POLLING_NRFLAG is set. To preserve bisectibility and avoid unbreakable idle loops, all the need_resched() checks within TIF_POLLING_NRFLAGS sections, have been replaced tree-wide with the need_resched_or_ipi() check. [ prateek: Replaced some of the missed out occurrences of need_resched() within a TIF_POLLING sections with need_resched_or_ipi() ] Cc: Richard Henderson Cc: Ivan Kokshaysky Cc: Matt Turner Cc: Russell King Cc: Guo Ren Cc: Michal Simek Cc: Dinh Nguyen Cc: Jonas Bonn Cc: Stefan Kristiansson Cc: Stafford Horne Cc: "James E.J. Bottomley" Cc: Helge Deller Cc: Michael Ellerman Cc: Nicholas Piggin Cc: Christophe Leroy Cc: "Aneesh Kumar K.V" Cc: "Naveen N. Rao" Cc: Yoshinori Sato Cc: Rich Felker Cc: John Paul Adrian Glaubitz Cc: "David S. Miller" Cc: Thomas Gleixner Cc: Ingo Molnar Cc: Borislav Petkov Cc: Dave Hansen Cc: "H. Peter Anvin" Cc: "Rafael J. Wysocki" Cc: Daniel Lezcano 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: Al Viro Cc: Linus Walleij Cc: Ard Biesheuvel Cc: Andrew Donnellan Cc: Nicholas Miehlbradt Cc: Andrew Morton Cc: Arnd Bergmann Cc: Josh Poimboeuf Cc: "Kirill A. Shutemov" Cc: Rick Edgecombe Cc: Tony Battersby Cc: Brian Gerst Cc: Tim Chen Cc: David Vernet Cc: x86@kernel.org Cc: linux-kernel@vger.kernel.org Cc: linux-alpha@vger.kernel.org Cc: linux-arm-kernel@lists.infradead.org Cc: linux-csky@vger.kernel.org Cc: linux-openrisc@vger.kernel.org Cc: linux-parisc@vger.kernel.org Cc: linuxppc-dev@lists.ozlabs.org Cc: linux-sh@vger.kernel.org Cc: sparclinux@vger.kernel.org Cc: linux-pm@vger.kernel.org Signed-off-by: Gautham R. Shenoy Co-developed-by: K Prateek Nayak Signed-off-by: K Prateek Nayak --- arch/x86/include/asm/mwait.h | 2 +- arch/x86/kernel/process.c | 2 +- drivers/cpuidle/cpuidle-powernv.c | 2 +- drivers/cpuidle/cpuidle-pseries.c | 2 +- drivers/cpuidle/poll_state.c | 2 +- include/linux/sched.h | 5 +++++ include/linux/sched/idle.h | 4 ++-- kernel/sched/idle.c | 7 ++++--- 8 files changed, 16 insertions(+), 10 deletions(-) diff --git a/arch/x86/include/asm/mwait.h b/arch/x86/include/asm/mwait.h index 778df05f8539..ac1370143407 100644 --- a/arch/x86/include/asm/mwait.h +++ b/arch/x86/include/asm/mwait.h @@ -115,7 +115,7 @@ static __always_inline void mwait_idle_with_hints(unsigned long eax, unsigned lo } __monitor((void *)¤t_thread_info()->flags, 0, 0); - if (!need_resched()) + if (!need_resched_or_ipi()) __mwait(eax, ecx); } current_clr_polling(); diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c index b6f4e8399fca..ca6cb7e28cba 100644 --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c @@ -925,7 +925,7 @@ static __cpuidle void mwait_idle(void) } __monitor((void *)¤t_thread_info()->flags, 0, 0); - if (!need_resched()) { + if (!need_resched_or_ipi()) { __sti_mwait(0, 0); raw_local_irq_disable(); } diff --git a/drivers/cpuidle/cpuidle-powernv.c b/drivers/cpuidle/cpuidle-powernv.c index 9ebedd972df0..77c3bb371f56 100644 --- a/drivers/cpuidle/cpuidle-powernv.c +++ b/drivers/cpuidle/cpuidle-powernv.c @@ -79,7 +79,7 @@ static int snooze_loop(struct cpuidle_device *dev, dev->poll_time_limit = false; ppc64_runlatch_off(); HMT_very_low(); - while (!need_resched()) { + while (!need_resched_or_ipi()) { if (likely(snooze_timeout_en) && get_tb() > snooze_exit_time) { /* * Task has not woken up but we are exiting the polling diff --git a/drivers/cpuidle/cpuidle-pseries.c b/drivers/cpuidle/cpuidle-pseries.c index 14db9b7d985d..4f2b490f8b73 100644 --- a/drivers/cpuidle/cpuidle-pseries.c +++ b/drivers/cpuidle/cpuidle-pseries.c @@ -46,7 +46,7 @@ int snooze_loop(struct cpuidle_device *dev, struct cpuidle_driver *drv, snooze_exit_time = get_tb() + snooze_timeout; dev->poll_time_limit = false; - while (!need_resched()) { + while (!need_resched_or_ipi()) { HMT_low(); HMT_very_low(); if (likely(snooze_timeout_en) && get_tb() > snooze_exit_time) { diff --git a/drivers/cpuidle/poll_state.c b/drivers/cpuidle/poll_state.c index 9b6d90a72601..225f37897e45 100644 --- a/drivers/cpuidle/poll_state.c +++ b/drivers/cpuidle/poll_state.c @@ -26,7 +26,7 @@ static int __cpuidle poll_idle(struct cpuidle_device *dev, limit = cpuidle_poll_time(drv, dev); - while (!need_resched()) { + while (!need_resched_or_ipi()) { cpu_relax(); if (loop_count++ < POLL_IDLE_RELAX_COUNT) continue; diff --git a/include/linux/sched.h b/include/linux/sched.h index 03bfe9ab2951..63451f6f25b9 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -2258,6 +2258,11 @@ static __always_inline bool need_resched(void) return unlikely(tif_need_resched()); } +static __always_inline bool need_resched_or_ipi(void) +{ + return unlikely(tif_need_resched() || tif_notify_ipi()); +} + /* * Wrappers for p->thread_info->cpu access. No-op on UP. */ diff --git a/include/linux/sched/idle.h b/include/linux/sched/idle.h index 478084f9105e..d739ab810e00 100644 --- a/include/linux/sched/idle.h +++ b/include/linux/sched/idle.h @@ -63,7 +63,7 @@ static __always_inline bool __must_check current_set_polling_and_test(void) */ smp_mb__after_atomic(); - return unlikely(tif_need_resched()); + return unlikely(need_resched_or_ipi()); } static __always_inline bool __must_check current_clr_polling_and_test(void) @@ -76,7 +76,7 @@ static __always_inline bool __must_check current_clr_polling_and_test(void) */ smp_mb__after_atomic(); - return unlikely(tif_need_resched()); + return unlikely(need_resched_or_ipi()); } #else diff --git a/kernel/sched/idle.c b/kernel/sched/idle.c index 31231925f1ec..fcc734f45a2a 100644 --- a/kernel/sched/idle.c +++ b/kernel/sched/idle.c @@ -57,7 +57,7 @@ static noinline int __cpuidle cpu_idle_poll(void) ct_cpuidle_enter(); raw_local_irq_enable(); - while (!tif_need_resched() && + while (!need_resched_or_ipi() && (cpu_idle_force_poll || tick_check_broadcast_expired())) cpu_relax(); raw_local_irq_disable(); @@ -153,7 +153,7 @@ static void cpuidle_idle_call(void) * Check if the idle task must be rescheduled. If it is the * case, exit the function after re-enabling the local irq. */ - if (need_resched()) { + if (need_resched_or_ipi()) { local_irq_enable(); return; } @@ -255,7 +255,7 @@ static void do_idle(void) __current_set_polling(); tick_nohz_idle_enter(); - while (!need_resched()) { + while (!need_resched_or_ipi()) { rmb(); /* @@ -336,6 +336,7 @@ static void do_idle(void) * RCU relies on this call to be done outside of an RCU read-side * critical section. */ + current_clr_notify_ipi(); flush_smp_call_function_queue(); schedule_idle();