From nobody Fri Dec 19 13:09:33 2025 Received: from PH8PR06CU001.outbound.protection.outlook.com (mail-westus3azon11012024.outbound.protection.outlook.com [40.107.209.24]) (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 9658A328B56 for ; Mon, 8 Dec 2025 09:32:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=fail smtp.client-ip=40.107.209.24 ARC-Seal: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765186370; cv=fail; b=sQIsTlTIdZerHWCLuaNg+Lh07xWnRWlQ+IVOQFpZH2Do6lwIsiQLb5uzVCCj+XthzUwfVH3AVe0QZLAYHKUEUwh6pc/fU3nOyKRiWKnNRGSomuOifoj1XgqP7I5zvFGCWXMvJqqUIqOhLLzOS1ZB+j3VC5OL6QIsVvLMe2fiDZc= ARC-Message-Signature: i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1765186370; c=relaxed/simple; bh=pxUzTc3Wg2lgEzONKim0SDlvVTNcDObm5dJiDeQbpKc=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=MuxIpx4NhOsawuzE9djc1dsKqLfMnaiC8WxbzrHFQGrgNKwtNKHItGamcS9QajSMuVg4X5wwCKkzQKqntRH9dlf9kf2UUj2gIZV4qLgU4m6+I5537Djy417m+W3i/YCFv/XS0y7RxLruANG49ejiAW4CYWUKmJRIUYrtHBmadBs= 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=bJW8yDu5; arc=fail smtp.client-ip=40.107.209.24 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="bJW8yDu5" ARC-Seal: i=1; a=rsa-sha256; s=arcselector10001; d=microsoft.com; cv=none; b=lNUTlqaKw5Ym1nET4DfjXTfW+Heh3l6sR189q0SvjadmwFrtlsSjEZz4OI2Ml81Uxott909Pk1x76apRfWVqA1uZvvja/cOLw7oD2ZhNEkqcPvWnQ1g61WWpceTfDE0QcuhA9+nXkQX1p/CJkGbp+1vUNifAtumgO5yyNmAaZgW0OmNX2pfYlYngkss3pCCBtL1qLQvmR7/sapHo1sf36VuD+bxZJ+Qc+kJeUp2ZNnCQ34cSmswLo1vh0jEFHTXzDZT8sODzpz1ckSmQBZaVZYPe63REaYoYBV1YMYunMm9fRnhMeWrLNfnj/DCAO88oDJsRYHa9Fslt/uqmubi+Aw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector10001; 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=X6uECB03Y8/g/H3gMbd0YgviARMW+cJY0sLyxbmsoe8=; b=uY/T34R5Z2zAjchUNo1+2wj9qvOsR7XOpnx1HElaAlXMNU9DvAZTIuA3YPeJ+weqZthmsOwdc+cOEjLYq5KH6u4NTtw8WUZLc3GRW7MEv7moY7Vpt6K/WX1I9ZWoypbnq3T+K2jJyOu7i7XtVL88Y4DIT0WcvWBUWheCcfYQOlyljYoa2nccvbtb/wY3Puz/OcWrBE3fEl5wQmxFqYsKp7RCpm21v9paJ7NUDyAg1tjyRO+dZde7y8SS55vtm5EHKW64O5vG7huHN195g23WLXQRZdJLpLSLkljyDAC5Nf9iN8HL56oyEvA//fcqA2on0LSnAHS/mlXWkMZhq3QiMw== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 165.204.84.17) smtp.rcpttodomain=redhat.com 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=X6uECB03Y8/g/H3gMbd0YgviARMW+cJY0sLyxbmsoe8=; b=bJW8yDu54sF0IfmWA/ABmU0jacEVPA8YIyHaB1N3HJZEdeIXkfXtIOqfJ2HG/VXl0yQAT0XTPSotzd4PCJmK06rBJGOwK/1gieb4YdUgRopfkQRz9bef1IPhshuBtluqiIIPySehppnM50RuQZ0ANkXnCKCSpIwNHJCXSuW9vzk= Received: from DM6PR11CA0060.namprd11.prod.outlook.com (2603:10b6:5:14c::37) by MN2PR12MB4336.namprd12.prod.outlook.com (2603:10b6:208:1df::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9388.14; Mon, 8 Dec 2025 09:32:43 +0000 Received: from DS3PEPF0000C37C.namprd04.prod.outlook.com (2603:10b6:5:14c:cafe::ea) by DM6PR11CA0060.outlook.office365.com (2603:10b6:5:14c::37) with Microsoft SMTP Server (version=TLS1_3, cipher=TLS_AES_256_GCM_SHA384) id 15.20.9388.14 via Frontend Transport; Mon, 8 Dec 2025 09:32:46 +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=satlexmb07.amd.com; pr=C Received: from satlexmb07.amd.com (165.204.84.17) by DS3PEPF0000C37C.mail.protection.outlook.com (10.167.23.6) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.9412.4 via Frontend Transport; Mon, 8 Dec 2025 09:32:43 +0000 Received: from BLRKPRNAYAK.amd.com (10.180.168.240) by satlexmb07.amd.com (10.181.42.216) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.2562.17; Mon, 8 Dec 2025 03:32:37 -0600 From: K Prateek Nayak To: Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Anna-Maria Behnsen , Frederic Weisbecker , Thomas Gleixner CC: , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , K Prateek Nayak , "Gautham R. Shenoy" , Swapnil Sapkal , Shrikanth Hegde , Chen Yu Subject: [RESEND RFC PATCH v2 17/29] sched/topology: Introduce "nohz_shared_list" to keep track of sd->shared Date: Mon, 8 Dec 2025 09:27:03 +0000 Message-ID: <20251208092744.32737-17-kprateek.nayak@amd.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20251208083602.31898-1-kprateek.nayak@amd.com> References: <20251208083602.31898-1-kprateek.nayak@amd.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ClientProxiedBy: satlexmb07.amd.com (10.181.42.216) To satlexmb07.amd.com (10.181.42.216) X-EOPAttributedMessage: 0 X-MS-PublicTrafficType: Email X-MS-TrafficTypeDiagnostic: DS3PEPF0000C37C:EE_|MN2PR12MB4336:EE_ X-MS-Office365-Filtering-Correlation-Id: 46ceb58c-7dcb-4d7b-a08c-08de363cbd57 X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam: BCL:0;ARA:13230040|1800799024|376014|82310400026|7416014|36860700013; X-Microsoft-Antispam-Message-Info: =?us-ascii?Q?NyniLVE2Nsc9Y80nAVMjmWdjD74wcDGcgxlyuha1luQau567+7bWhS2wBr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J+Mg8UY+0k/0nJu70ygEJ86Mc3P/BC2zWGkhqYMXGlql?= =?us-ascii?Q?U2q+J7n5+gaLZmDsTpq+rmtY8qUikNQU7DGw6wR2UQSHplWXh3eECqw4WrGu?= =?us-ascii?Q?2qCFM4t/we8EeIOcEpFjQRKahs8Eqn4TOcxceQdLVOGg/kkKhH0xEk7W1kgR?= =?us-ascii?Q?ebqERsSl4VIm9n/tn02kw8eD5euXGNdyz2Fzny/732v20HONX7t10HULsm69?= =?us-ascii?Q?Kn447yE7oVwSYHXStNcGkaf0CDuUaRyCL2zaesn/gYLwpB7i2Koz4karxrIP?= =?us-ascii?Q?Gx0iMf9q/r18sggFmhiTwBSntWQKo9eggao23QHqmghn9xaXJeQmFlXFEkD5?= =?us-ascii?Q?xa8RZU25uzHfmgPTFPWpQGS9Mmp+egvtRU20ISPPSZB0vKWSu9QhyVYebZ/M?= =?us-ascii?Q?eUIwD6oA0zItWDXbBVFS4UxgoQlCeGQXIR1dO9bCpDgAPGIN3DFQxCTcPClh?= =?us-ascii?Q?8UjYK1LlTuUck2yABFLMWQBhVOTxkpgVzskWAXKi0Dy4UH3oPBzXFzHSooh5?= =?us-ascii?Q?ZYOMLr/8mdeviLty+dtM0/Da4bzD9Nf1eOW9FvRKk4CdUVvW0v2tbUu459Jw?= =?us-ascii?Q?wLeOXj/Yrv9zyrA3aPhNS3oVMoSTpnCPF8KVL0MlF+a1oli9gAnBcuTCI9DA?= =?us-ascii?Q?/wMC/Xb6ipQkyZVKmwo=3D?= X-Forefront-Antispam-Report: CIP:165.204.84.17;CTRY:US;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:satlexmb07.amd.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230040)(1800799024)(376014)(82310400026)(7416014)(36860700013);DIR:OUT;SFP:1101; X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 08 Dec 2025 09:32:43.5345 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: 46ceb58c-7dcb-4d7b-a08c-08de363cbd57 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=[satlexmb07.amd.com] X-MS-Exchange-CrossTenant-AuthSource: DS3PEPF0000C37C.namprd04.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: MN2PR12MB4336 Content-Type: text/plain; charset="utf-8" Chain together all the "sd_nohz->shared" objects in a RCU protected "nohz_shared_list" to get the full view of system for nohz idle balancing. Since __sds_nohz_idle_alloc() is also responsible for initializing the list_head, rename it to __sds_nohz_idle_alloc_init() to fit the bill. List modifications are protected by the "nohz_shared_list_lock" and freeing of sd->shared on the list is delayed by one grace period to ensure all the untracked references to the shared object under RCU are dropped. Users of "nohz_shared_list" will be added in subsequent patches where all the current users of "nohz.idle_cpus" will be converted to use the distributed tracking. Reviewed-by: Gautham R. Shenoy Signed-off-by: K Prateek Nayak --- include/linux/sched/topology.h | 12 ++++--- kernel/sched/sched.h | 1 + kernel/sched/topology.c | 62 +++++++++++++++++++++++++++++++--- 3 files changed, 65 insertions(+), 10 deletions(-) diff --git a/include/linux/sched/topology.h b/include/linux/sched/topology.h index 280915ff7acf..074ee2980cdf 100644 --- a/include/linux/sched/topology.h +++ b/include/linux/sched/topology.h @@ -64,13 +64,15 @@ extern int sched_domain_level_max; struct sched_group; =20 struct sched_domain_shared { - atomic_t ref; + atomic_t ref; #ifdef CONFIG_NO_HZ_COMMON - atomic_t nr_idle_cpus; - struct cpumask *nohz_idle_cpus_mask; + atomic_t nr_idle_cpus; + struct cpumask *nohz_idle_cpus_mask; + struct list_head nohz_list_node; + struct rcu_head rcu; #endif - int has_idle_cores; - int nr_idle_scan; + int has_idle_cores; + int nr_idle_scan; }; =20 struct sched_domain { diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index be52ebaf0143..159c24981ead 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -3264,6 +3264,7 @@ extern void cfs_bandwidth_usage_dec(void); #define nohz_flags(cpu) (&cpu_rq(cpu)->nohz_flags) =20 DECLARE_PER_CPU(struct sched_domain __rcu *, sd_nohz); +extern struct list_head nohz_shared_list; =20 extern void nohz_balance_exit_idle(struct rq *rq); #else /* !CONFIG_NO_HZ_COMMON: */ diff --git a/kernel/sched/topology.c b/kernel/sched/topology.c index 59089ed3b840..af4ce9451cd0 100644 --- a/kernel/sched/topology.c +++ b/kernel/sched/topology.c @@ -468,8 +468,12 @@ struct s_data { =20 DEFINE_PER_CPU(struct sched_domain __rcu *, sd_nohz); =20 -static int __sds_nohz_idle_alloc(struct sched_domain_shared *sds, int node) +static DEFINE_RAW_SPINLOCK(nohz_shared_list_lock); +LIST_HEAD(nohz_shared_list); + +static int __sds_nohz_idle_alloc_init(struct sched_domain_shared *sds, int= node) { + sds->nohz_list_node =3D (struct list_head)LIST_HEAD_INIT(sds->nohz_list_n= ode); sds->nohz_idle_cpus_mask =3D kzalloc_node(cpumask_size(), GFP_KERNEL, nod= e); =20 if (!sds->nohz_idle_cpus_mask) @@ -483,6 +487,7 @@ static void __sds_nohz_idle_free(struct sched_domain_sh= ared *sds) if (!sds) return; =20 + WARN_ON_ONCE(!list_empty(&sds->nohz_list_node)); kfree(sds->nohz_idle_cpus_mask); } =20 @@ -509,7 +514,7 @@ static int __fallback_sds_alloc(struct s_data *d, unsig= ned long *visited_nodes) =20 d->fallback_nohz_sds[j] =3D sds; =20 - if (__sds_nohz_idle_alloc(sds, j)) + if (__sds_nohz_idle_alloc_init(sds, j)) return -ENOMEM; } =20 @@ -560,6 +565,7 @@ static void claim_fallback_sds(struct s_data *d) static void update_nohz_domain(int cpu) { struct sched_domain *sd =3D highest_flag_domain(cpu, SD_SHARE_LLC); + struct sched_domain_shared *sds =3D NULL; =20 /* * If sd_llc doesn't exist, use the lowest sd for nohz idle @@ -570,13 +576,52 @@ static void update_nohz_domain(int cpu) if (!sd) sd =3D rcu_dereference(cpu_rq(cpu)->sd); =20 - WARN_ON_ONCE(sd && !sd->shared); + if (sd) + sds =3D sd->shared; + + if (sds && list_empty(&sds->nohz_list_node)) { + /* + * IRQs should be disabled by the caller since they + * hold the rq_lock. + */ + lockdep_assert_irqs_disabled(); + + guard(raw_spinlock)(&nohz_shared_list_lock); + list_add(&sds->nohz_list_node, &nohz_shared_list); + } + + WARN_ON_ONCE(sd && !sds); rcu_assign_pointer(per_cpu(sd_nohz, cpu), sd); } =20 +static void destroy_sched_domain_shared_rcu(struct rcu_head *rcu) +{ + struct sched_domain_shared *sds =3D container_of(rcu, struct sched_domain= _shared, rcu); + + kfree(sds->nohz_idle_cpus_mask); + kfree(sds); +} + +/* + * If sd->shared is on the rcu protected nohz_shared_list, + * remove it the list and wait once grace period before + * freeing. + */ +static int sds_delayed_free(struct sched_domain_shared *sds) +{ + if (list_empty(&sds->nohz_list_node)) + return 0; + + scoped_guard(raw_spinlock_irqsave, &nohz_shared_list_lock) + list_del_rcu(&sds->nohz_list_node); + + call_rcu(&sds->rcu, destroy_sched_domain_shared_rcu); + return 1; +} + #else /* !CONFIG_NO_HZ_COMMON */ =20 -static int __sds_nohz_idle_alloc(struct sched_domain_shared *sds, int node) +static int __sds_nohz_idle_alloc_init(struct sched_domain_shared *sds, int= node) { return 0; } @@ -592,6 +637,7 @@ static inline void __fallback_sds_free(struct s_data *d= ) { } static inline void assign_fallback_sds(struct s_data *d, struct sched_doma= in *sd, int cpu) { } static inline void claim_fallback_sds(struct s_data *d) { } static inline void update_nohz_domain(int cpu) { } +static inline int sds_delayed_free(struct sched_domain_shared *sds) { retu= rn 0; } =20 #endif /* CONFIG_NO_HZ_COMMON */ =20 @@ -771,9 +817,15 @@ static void destroy_sched_domain(struct sched_domain *= sd) free_sched_groups(sd->groups, 1); =20 if (sd->shared && atomic_dec_and_test(&sd->shared->ref)) { + if (sds_delayed_free(sd->shared)) { + sd->shared =3D NULL; + goto out; + } + __sds_nohz_idle_free(sd->shared); kfree(sd->shared); } +out: kfree(sd); } =20 @@ -2632,7 +2684,7 @@ static int __sds_alloc(struct s_data *d, const struct= cpumask *cpu_map) bitmap_set(visited_nodes, cpu_to_node(j), 1); *per_cpu_ptr(d->sds, j) =3D sds; =20 - if (__sds_nohz_idle_alloc(sds, cpu_to_node(j))) + if (__sds_nohz_idle_alloc_init(sds, cpu_to_node(j))) return -ENOMEM; } =20 --=20 2.43.0