From nobody Mon Feb 9 00:06:44 2026 Received: from mail-yx1-f43.google.com (mail-yx1-f43.google.com [74.125.224.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BB9E51FECCD for ; Sun, 18 Jan 2026 03:34:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.224.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768707283; cv=none; b=BcfUCqfEPoCRWhcvbC36bF1lJG5UEha3b9DIwPs8ETriFe3l77sTF2ZJs04r5Lbr4Jq1UAxvNme9rQy7JUoYOgL2FSYzNx9LAIKtT12gP5s5qrduTntC3gfZU3b4S3nOYaI7WbvB8oeF5JOmyJaXQUD6sHgn7F0Kg7PnuOvhE7U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768707283; c=relaxed/simple; bh=cp3CcsjOowTItZRLs99rQor7AK0AXH9C3064oV2AQLU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=P3G/eG6PD5vreX07QvUQLX6SG0h6F+WhaUZDsxOhqgSa+q0KMMW6YiHI18qcNu5Qb5sQc75uEINIgqUXEm09JBlG4UmOTwmr4FMHuFuSlhZQi72OR7nPfrQRrFuHZiYJkRFpPg69uATUJNz5HPXrEn36YLuUKESbYZ8YcxX+I5I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=ncsu.edu; spf=pass smtp.mailfrom=ncsu.edu; dkim=pass (2048-bit key) header.d=ncsu.edu header.i=@ncsu.edu header.b=cedtpg5J; arc=none smtp.client-ip=74.125.224.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=ncsu.edu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ncsu.edu Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ncsu.edu header.i=@ncsu.edu header.b="cedtpg5J" Received: by mail-yx1-f43.google.com with SMTP id 956f58d0204a3-644715aad1aso3655620d50.0 for ; Sat, 17 Jan 2026 19:34:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ncsu.edu; s=google; t=1768707281; x=1769312081; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=oeTWICU1rnHmY5pG3T1Iu+H5wz88ZwNtaW//ZDioqfw=; b=cedtpg5J+fcVDqVPV7SOijQ9niiKbr1FiwQozVmHJ54O6J9gSCI0ceXFkMiqkXkdj5 Y/q64uOEHj2Goi3zWuf7XpR+3jAMItgZide24TG9MLru2n9W8vQx6E9027FQgRNoicZd zf8VSSbmKBgJDJ6vLZzzhYPLBNtfaNwZgYNMHOMOWHWXxhOmEJoWXbDNYBeHKvQhzeO+ NXYP8jVokjoyqQFtgOsBnXEWXwna99/iu9RJQMCXpB4AGShhXMmgNVTV9d+7+GBOz7rd aIV00tkxW886gyBVL+c2l6+1LfZlRLYI1TmDwoRXZfvzd7+32w10hpYJCuPAnhj6lpue /icA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768707281; x=1769312081; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=oeTWICU1rnHmY5pG3T1Iu+H5wz88ZwNtaW//ZDioqfw=; b=siuq06snq2F+ofmYkexyD+tzoY3Dl1BGzXDLv0nayQat08MtVAxthy+8GoC+I96PGY 8WBKnhjOI1E4OHQaLv0ASYtWJA6X+ozEPYmXW8qncG2W+SXgXUZ5330jUrTiQLFT4aV+ noxV+zml9kPxiTAvUIzgdB3FwtG8aN0y1x0kbO0jJA4HnXCUsh2jA7KFxfX9RJ86ljV2 ch5tdMqIsrdFxspG3Prv3c8gdZgPLjRLg6LInSoXWYmYwtrifvgJ2S66RxUe09BwFqPN EVrx5wYsn+PNkwqYUS+B2wjpWpRN3pSMd0acMd5LwcwGLHq0j9XQb2wPnJ1dG0nLlEUK 5hWA== X-Forwarded-Encrypted: i=1; AJvYcCU93IXIGAAdf31eYmygQiSpH9RWRXNjAD2P9AdPRcU2ubR6hb21Gujnql2svNT8fLEqwDQ8DCQgQgYQm58=@vger.kernel.org X-Gm-Message-State: AOJu0YwXbSQzmhq6NxcipE/Xqp3IU2EXjkjS7yVQil2i9dJCb/1xTW3I 5SLn/Td+wt5rOWMGRd6nczvlweRkU44xETVcWn4RZ07CV5aCGmTVVLJTUkNLq32fOQ== X-Gm-Gg: AY/fxX7vF2NStihdxHO6gAm5z/ZiRCuiy1sUqgZhmsxDaNTM0vzRVTCkgelVHZE8H0G o9K5J50KxCmVuJvpS1jf3C0SNQcUiU1LS7xD8KmRK9vQz/L4as/y/hqSLDD+lROOr1buvWA7xiF onK5v/NM3LapW7MNOd3YyUaTHfzBKum3YgqxQ8uGnRnuSL3BT8GnyNVt7UFq9TgOby8gtF6NFJ5 rxY5xrq/J1aiPq55ZSm6e1PXICkmARWTDSQWn2wpM47+dI73dhYN2weM3twlrAivPugyO88ESeY Lwrr+fam6Vf1EUG3p18VZJXtXQDIM1VayOGeDkWIC3HLC92odq7hwD8IoULScDVNsPaCMyO/T0l fPDd68Lyua567cSSIjGiK6UwydQErZBok8Vc9UhSvc8FUBGtKqhC4oe87KLwyDIK0GhZZCliOIZ 3bM1u3cEAoyw== X-Received: by 2002:a05:690e:1919:b0:644:7b19:c207 with SMTP id 956f58d0204a3-64916b7d34emr5698225d50.38.1768707280561; Sat, 17 Jan 2026 19:34:40 -0800 (PST) Received: from um773-cachyos ([2600:1700:cc0:94af:eca6:9ff9:6f3c:5de9]) by smtp.gmail.com with ESMTPSA id 956f58d0204a3-649170acbdbsm3235607d50.13.2026.01.17.19.34.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Jan 2026 19:34:39 -0800 (PST) From: Zecheng Li To: Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot Cc: Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , Rik van Riel , Chris Mason , Madadi Vineeth Reddy , Xu Liu , Blake Jones , Josh Don , Nilay Vaish , linux-kernel@vger.kernel.org, Zecheng Li , Zecheng Li Subject: [PATCH v7 1/3] sched/fair: Co-locate cfs_rq and sched_entity in cfs_tg_state Date: Sat, 17 Jan 2026 22:34:11 -0500 Message-ID: <20260118033431.158464-2-zli94@ncsu.edu> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260118033431.158464-1-zli94@ncsu.edu> References: <20260118033431.158464-1-zli94@ncsu.edu> 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 Content-Type: text/plain; charset="utf-8" From: Zecheng Li Improve data locality and reduce pointer chasing by allocating struct cfs_rq and struct sched_entity together for non-root task groups. This is achieved by introducing a new combined struct cfs_tg_state that holds both objects in a single allocation. This patch: - Introduces struct cfs_tg_state that embeds cfs_rq, sched_entity, and sched_statistics together in a single structure. - Updates __schedstats_from_se() in stats.h to use cfs_tg_state for accessing sched_statistics from a group sched_entity. - Modifies alloc_fair_sched_group() and free_fair_sched_group() to allocate and free the new struct as a single unit. - Modifies the per-CPU pointers in task_group->se and task_group->cfs_rq to point to the members in the new combined structure. Signed-off-by: Zecheng Li Signed-off-by: Zecheng Li --- kernel/sched/fair.c | 25 +++++++++++-------------- kernel/sched/sched.h | 12 ++++++++++++ kernel/sched/stats.h | 9 +-------- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 04993c763a06..0897dab69236 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -13617,10 +13617,11 @@ void free_fair_sched_group(struct task_group *tg) int i; =20 for_each_possible_cpu(i) { - if (tg->cfs_rq) - kfree(tg->cfs_rq[i]); - if (tg->se) - kfree(tg->se[i]); + if (tg->cfs_rq && tg->cfs_rq[i]) { + struct cfs_tg_state *state =3D + container_of(tg->cfs_rq[i], struct cfs_tg_state, cfs_rq); + kfree(state); + } } =20 kfree(tg->cfs_rq); @@ -13629,6 +13630,7 @@ void free_fair_sched_group(struct task_group *tg) =20 int alloc_fair_sched_group(struct task_group *tg, struct task_group *paren= t) { + struct cfs_tg_state *state; struct sched_entity *se; struct cfs_rq *cfs_rq; int i; @@ -13645,16 +13647,13 @@ int alloc_fair_sched_group(struct task_group *tg,= struct task_group *parent) init_cfs_bandwidth(tg_cfs_bandwidth(tg), tg_cfs_bandwidth(parent)); =20 for_each_possible_cpu(i) { - cfs_rq =3D kzalloc_node(sizeof(struct cfs_rq), - GFP_KERNEL, cpu_to_node(i)); - if (!cfs_rq) + state =3D kzalloc_node(sizeof(*state), + GFP_KERNEL, cpu_to_node(i)); + if (!state) goto err; =20 - se =3D kzalloc_node(sizeof(struct sched_entity_stats), - GFP_KERNEL, cpu_to_node(i)); - if (!se) - goto err_free_rq; - + cfs_rq =3D &state->cfs_rq; + se =3D &state->se; init_cfs_rq(cfs_rq); init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]); init_entity_runnable_average(se); @@ -13662,8 +13661,6 @@ int alloc_fair_sched_group(struct task_group *tg, s= truct task_group *parent) =20 return 1; =20 -err_free_rq: - kfree(cfs_rq); err: return 0; } diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 58c9d244f12b..50b37ed2f7d6 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -2191,6 +2191,18 @@ static inline struct task_group *task_group(struct t= ask_struct *p) return p->sched_task_group; } =20 +#ifdef CONFIG_FAIR_GROUP_SCHED +/* + * Defined here to be available before stats.h is included, since + * stats.h has dependencies on things defined later in this file. + */ +struct cfs_tg_state { + struct cfs_rq cfs_rq; + struct sched_entity se; + struct sched_statistics stats; +} __no_randomize_layout; +#endif + /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups= */ static inline void set_task_rq(struct task_struct *p, unsigned int cpu) { diff --git a/kernel/sched/stats.h b/kernel/sched/stats.h index c903f1a42891..63b9a800a354 100644 --- a/kernel/sched/stats.h +++ b/kernel/sched/stats.h @@ -89,19 +89,12 @@ static inline void rq_sched_info_depart (struct rq *rq= , unsigned long long delt =20 #endif /* CONFIG_SCHEDSTATS */ =20 -#ifdef CONFIG_FAIR_GROUP_SCHED -struct sched_entity_stats { - struct sched_entity se; - struct sched_statistics stats; -} __no_randomize_layout; -#endif - static inline struct sched_statistics * __schedstats_from_se(struct sched_entity *se) { #ifdef CONFIG_FAIR_GROUP_SCHED if (!entity_is_task(se)) - return &container_of(se, struct sched_entity_stats, se)->stats; + return &container_of(se, struct cfs_tg_state, se)->stats; #endif return &task_of(se)->stats; } --=20 2.52.0 From nobody Mon Feb 9 00:06:44 2026 Received: from mail-yx1-f50.google.com (mail-yx1-f50.google.com [74.125.224.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5BE13309EFF for ; Sun, 18 Jan 2026 03:34:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.224.50 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768707285; cv=none; b=RvIYtPqmnEX1wYpK5YHWJBHuE1qSFw1lLtHVXzIzEX+SNmB1+FL1cJQV97JUkDQh/LnD2u1Xp8y71ur95fgqhXFeKgwv/9QJGaa0CcaWXBtOUIbeje9OGiUG7CERMRfLifuP0+PQvXElwTAOfYwY/lZGFviWXczgJv3rfPHzDro= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768707285; c=relaxed/simple; bh=21zHA8vKZMvZv8RKf+sV3YuXukuThD+OULWzxLF7it4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=qDA0nv1q/aq607KJXmWSTtscM4FMTjGRCoYcMyZGWzzES7Zzf1Pa1v27C5UyYYX6s7m3KLBYDXkzRtkoXtrE832+odXeeePbOiYVAPT2pBsiMXjIhbQGtxTIvdWkwsTC8ZvTM8ad56pEeFOK2azi/3BAN6IEsuA1xWxzCHOBtZQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=ncsu.edu; spf=pass smtp.mailfrom=ncsu.edu; dkim=pass (2048-bit key) header.d=ncsu.edu header.i=@ncsu.edu header.b=AJ7kCs9L; arc=none smtp.client-ip=74.125.224.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=ncsu.edu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ncsu.edu Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ncsu.edu header.i=@ncsu.edu header.b="AJ7kCs9L" Received: by mail-yx1-f50.google.com with SMTP id 956f58d0204a3-64924c41044so405328d50.2 for ; Sat, 17 Jan 2026 19:34:43 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ncsu.edu; s=google; t=1768707282; x=1769312082; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=hhB0xDMATrKFmT/CHnFpcsZ3KkYOgHbrt+rbo3Npwlo=; b=AJ7kCs9LQxHY3oMzXrsPffzLYk41QIlVLBd22WlymqRq3PPG+hzZMO+/94xyXEuh2L Dd0hz6SZqLF/Y0qKFgAMpB5cfjFPs4djJJQLM7LPdgUaLLecUGV3rVoWyp8M/N6FLGbO vta4oVAcPz16RG7bYB3aSNRc9O3CTQ5dPQ1Yr5X1gkUPjBlRM3eimwuvFM0LdxD6C2it AtU2Mg2uoRzv0inCQlAzvuyY6uUU11YGPyhp5GF7TUHc9/IfBuofF8FzRHI9wa8xVw+e udbIdRjVlKu960tFRFMXFK55kcHyHMkqEXlunh7zmf6tpymzs2eM7H1C0c+eCPy5qpLV /Y0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768707282; x=1769312082; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=hhB0xDMATrKFmT/CHnFpcsZ3KkYOgHbrt+rbo3Npwlo=; b=urAWcWFFmnKnsdVElJEf3Z8sa5h7WtC/X3S3qZ/xfzE1lKlX55/I2YiXrOwafAg7Hm UXO59+sAuQIsA4AltZwvD9HNpeE/9KGOuPAcEz6RzWV0m2u8N0I7/X26WpkFddeA4gcA xFBZa1qYXIL4SCvOBtlhW01VbxdrgWoZy4VHUcpfxhgPWxf964iVF4ByLlW3Uv2Vhy9T OQBXigD+bGRivbIl1+2NbvLBxX9+l3KvrTKPmCLYSNOeU1axgd2IKfoVzJmGJ7myanQq 0sBXhYf0vqe/Na/u2dRtquRiYcDGiBygVwJ0bX/Ikv0+J7DfEYVfrT7Z2nkc8+ghL2eI xVLA== X-Forwarded-Encrypted: i=1; AJvYcCXwV6Hxftd3TfkJxnFXzpvIE6A3paBNVtZqLX+NooaDITeVfRCaho3yzPLkGXIUcS81ql+QSKvzT+u7g20=@vger.kernel.org X-Gm-Message-State: AOJu0YxtaxnHMvmQcxW5rC3E+5thzLFSAcRqzx6gQEA18srb7Q1ir7GZ eiQTn316QtNPc7IZGJH2QUBon7bgxNaZ0DLPhH2vf+rX+kBZdkOgR34AiD17+nwTcQ== X-Gm-Gg: AY/fxX5BdLp420175l/q8ONjZeUZsSbrGB9/oZayuiyJEIpDLW2vRfJju20St4eCG5G 1pMopL/NiGAqKkPsG4pXPlqzUDRFiCwQqaq4RmVz1Hyv2iQ06M39bW2g8WO49NEjeHML2N28Sxm M3oApkhEJ0BwsxQJhMJsilk1Pjd+fCzsznfkgZkgMvCdZzg56qYvnWTSlu5/xsp4oSBWdVIZ6JU veF6F0bmVUGz3R/rFWG9IGnGEDhMfkm9Ts1c61lIWTAq00lvi2dMvEXughne9j0riroS7bXyv0k VnOwocOuTNRd5aGd6pIqo8xvvvlUWL21Rjtc6xCwDoJundlQiNSW5VYCiQJ7QLEahJEMUnyujM3 0wExh5T2ABL9wYLr+ijqeo2a4stqjZdhonVCKpT50+r3RguePsntsHxbpV25i5MhoKA3WamV2AK RwThwfGxIg6g== X-Received: by 2002:a05:690e:1381:b0:644:60d9:8674 with SMTP id 956f58d0204a3-6491773104amr5822041d50.89.1768707282075; Sat, 17 Jan 2026 19:34:42 -0800 (PST) Received: from um773-cachyos ([2600:1700:cc0:94af:eca6:9ff9:6f3c:5de9]) by smtp.gmail.com with ESMTPSA id 956f58d0204a3-649170acbdbsm3235607d50.13.2026.01.17.19.34.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Jan 2026 19:34:41 -0800 (PST) From: Zecheng Li To: Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot Cc: Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , Rik van Riel , Chris Mason , Madadi Vineeth Reddy , Xu Liu , Blake Jones , Josh Don , Nilay Vaish , linux-kernel@vger.kernel.org, Zecheng Li , Zecheng Li Subject: [PATCH v7 2/3] sched/fair: Remove task_group->se pointer array Date: Sat, 17 Jan 2026 22:34:12 -0500 Message-ID: <20260118033431.158464-3-zli94@ncsu.edu> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260118033431.158464-1-zli94@ncsu.edu> References: <20260118033431.158464-1-zli94@ncsu.edu> 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 Content-Type: text/plain; charset="utf-8" From: Zecheng Li Now that struct sched_entity is co-located with struct cfs_rq for non-root task groups, the task_group->se pointer array is redundant. The associated sched_entity can be loaded directly from the cfs_rq. This patch performs the access conversion with the helpers: - is_root_task_group(tg): checks if a task group is the root task group. It compares the task group's address with the global root_task_group variable. - tg_se(tg, cpu): retrieves the cfs_rq and returns the address of the co-located se. This function checks if tg is the root task group to ensure behaving the same of previous tg->se[cpu]. Replaces all accesses that use the tg->se[cpu] pointer array with calls to the new tg_se(tg, cpu) accessor. - cfs_rq_se(cfs_rq): simplifies access paths like cfs_rq->tg->se[...] to use the co-located sched_entity. This function also checks if tg is the root task group to ensure same behavior. Since tg_se is not in very hot code paths, and the branch is a register comparison with an immediate value (`&root_task_group`), the performance impact is expected to be negligible. Signed-off-by: Zecheng Li Signed-off-by: Zecheng Li --- kernel/sched/core.c | 7 ++----- kernel/sched/debug.c | 2 +- kernel/sched/fair.c | 25 +++++++++---------------- kernel/sched/sched.h | 29 ++++++++++++++++++++++++----- 4 files changed, 36 insertions(+), 27 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 3cca012d1259..8e2a67cecee9 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -8565,7 +8565,7 @@ void __init sched_init(void) wait_bit_init(); =20 #ifdef CONFIG_FAIR_GROUP_SCHED - ptr +=3D 2 * nr_cpu_ids * sizeof(void **); + ptr +=3D nr_cpu_ids * sizeof(void **); #endif #ifdef CONFIG_RT_GROUP_SCHED ptr +=3D 2 * nr_cpu_ids * sizeof(void **); @@ -8574,9 +8574,6 @@ void __init sched_init(void) ptr =3D (unsigned long)kzalloc(ptr, GFP_NOWAIT); =20 #ifdef CONFIG_FAIR_GROUP_SCHED - root_task_group.se =3D (struct sched_entity **)ptr; - ptr +=3D nr_cpu_ids * sizeof(void **); - root_task_group.cfs_rq =3D (struct cfs_rq **)ptr; ptr +=3D nr_cpu_ids * sizeof(void **); =20 @@ -9644,7 +9641,7 @@ static int cpu_cfs_stat_show(struct seq_file *sf, voi= d *v) int i; =20 for_each_possible_cpu(i) { - stats =3D __schedstats_from_se(tg->se[i]); + stats =3D __schedstats_from_se(tg_se(tg, i)); ws +=3D schedstat_val(stats->wait_sum); } =20 diff --git a/kernel/sched/debug.c b/kernel/sched/debug.c index 5f9b77195159..544d9ae4e0df 100644 --- a/kernel/sched/debug.c +++ b/kernel/sched/debug.c @@ -644,7 +644,7 @@ void dirty_sched_domain_sysctl(int cpu) #ifdef CONFIG_FAIR_GROUP_SCHED static void print_cfs_group_stats(struct seq_file *m, int cpu, struct task= _group *tg) { - struct sched_entity *se =3D tg->se[cpu]; + struct sched_entity *se =3D tg_se(tg, cpu); =20 #define P(F) SEQ_printf(m, " .%-30s: %lld\n", #F, (long long)F) #define P_SCHEDSTAT(F) SEQ_printf(m, " .%-30s: %lld\n", \ diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 0897dab69236..e44bd5448fa5 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -5971,7 +5971,7 @@ void unthrottle_cfs_rq(struct cfs_rq *cfs_rq) { struct rq *rq =3D rq_of(cfs_rq); struct cfs_bandwidth *cfs_b =3D tg_cfs_bandwidth(cfs_rq->tg); - struct sched_entity *se =3D cfs_rq->tg->se[cpu_of(rq)]; + struct sched_entity *se =3D cfs_rq_se(cfs_rq); =20 /* * It's possible we are called with runtime_remaining < 0 due to things @@ -9839,7 +9839,6 @@ static bool __update_blocked_fair(struct rq *rq, bool= *done) { struct cfs_rq *cfs_rq, *pos; bool decayed =3D false; - int cpu =3D cpu_of(rq); =20 /* * Iterates the task_group tree in a bottom up fashion, see @@ -9859,7 +9858,7 @@ static bool __update_blocked_fair(struct rq *rq, bool= *done) } =20 /* Propagate pending load changes to the parent, if any: */ - se =3D cfs_rq->tg->se[cpu]; + se =3D cfs_rq_se(cfs_rq); if (se && !skip_blocked_update(se)) update_load_avg(cfs_rq_of(se), se, UPDATE_TG); =20 @@ -9885,8 +9884,7 @@ static bool __update_blocked_fair(struct rq *rq, bool= *done) */ static void update_cfs_rq_h_load(struct cfs_rq *cfs_rq) { - struct rq *rq =3D rq_of(cfs_rq); - struct sched_entity *se =3D cfs_rq->tg->se[cpu_of(rq)]; + struct sched_entity *se =3D cfs_rq_se(cfs_rq); unsigned long now =3D jiffies; unsigned long load; =20 @@ -13625,7 +13623,6 @@ void free_fair_sched_group(struct task_group *tg) } =20 kfree(tg->cfs_rq); - kfree(tg->se); } =20 int alloc_fair_sched_group(struct task_group *tg, struct task_group *paren= t) @@ -13638,9 +13635,6 @@ int alloc_fair_sched_group(struct task_group *tg, s= truct task_group *parent) tg->cfs_rq =3D kcalloc(nr_cpu_ids, sizeof(cfs_rq), GFP_KERNEL); if (!tg->cfs_rq) goto err; - tg->se =3D kcalloc(nr_cpu_ids, sizeof(se), GFP_KERNEL); - if (!tg->se) - goto err; =20 tg->shares =3D NICE_0_LOAD; =20 @@ -13655,7 +13649,7 @@ int alloc_fair_sched_group(struct task_group *tg, s= truct task_group *parent) cfs_rq =3D &state->cfs_rq; se =3D &state->se; init_cfs_rq(cfs_rq); - init_tg_cfs_entry(tg, cfs_rq, se, i, parent->se[i]); + init_tg_cfs_entry(tg, cfs_rq, se, i, tg_se(parent, i)); init_entity_runnable_average(se); } =20 @@ -13674,7 +13668,7 @@ void online_fair_sched_group(struct task_group *tg) =20 for_each_possible_cpu(i) { rq =3D cpu_rq(i); - se =3D tg->se[i]; + se =3D tg_se(tg, i); rq_lock_irq(rq, &rf); update_rq_clock(rq); attach_entity_cfs_rq(se); @@ -13691,7 +13685,7 @@ void unregister_fair_sched_group(struct task_group = *tg) =20 for_each_possible_cpu(cpu) { struct cfs_rq *cfs_rq =3D tg->cfs_rq[cpu]; - struct sched_entity *se =3D tg->se[cpu]; + struct sched_entity *se =3D tg_se(tg, cpu); struct rq *rq =3D cpu_rq(cpu); =20 if (se) { @@ -13728,7 +13722,6 @@ void init_tg_cfs_entry(struct task_group *tg, struc= t cfs_rq *cfs_rq, init_cfs_rq_runtime(cfs_rq); =20 tg->cfs_rq[cpu] =3D cfs_rq; - tg->se[cpu] =3D se; =20 /* se could be NULL for root_task_group */ if (!se) @@ -13759,7 +13752,7 @@ static int __sched_group_set_shares(struct task_gro= up *tg, unsigned long shares) /* * We can't change the weight of the root cgroup. */ - if (!tg->se[0]) + if (is_root_task_group(tg)) return -EINVAL; =20 shares =3D clamp(shares, scale_load(MIN_SHARES), scale_load(MAX_SHARES)); @@ -13770,7 +13763,7 @@ static int __sched_group_set_shares(struct task_gro= up *tg, unsigned long shares) tg->shares =3D shares; for_each_possible_cpu(i) { struct rq *rq =3D cpu_rq(i); - struct sched_entity *se =3D tg->se[i]; + struct sched_entity *se =3D tg_se(tg, i); struct rq_flags rf; =20 /* Propagate contribution to hierarchy */ @@ -13821,7 +13814,7 @@ int sched_group_set_idle(struct task_group *tg, lon= g idle) =20 for_each_possible_cpu(i) { struct rq *rq =3D cpu_rq(i); - struct sched_entity *se =3D tg->se[i]; + struct sched_entity *se =3D tg_se(tg, i); struct cfs_rq *grp_cfs_rq =3D tg->cfs_rq[i]; bool was_idle =3D cfs_rq_is_idle(grp_cfs_rq); long idle_task_delta; diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index 50b37ed2f7d6..bb6daf2c1e79 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -476,8 +476,6 @@ struct task_group { #endif =20 #ifdef CONFIG_FAIR_GROUP_SCHED - /* schedulable entities of this group on each CPU */ - struct sched_entity **se; /* runqueue "owned" by this group on each CPU */ struct cfs_rq **cfs_rq; unsigned long shares; @@ -915,7 +913,8 @@ struct dl_rq { }; =20 #ifdef CONFIG_FAIR_GROUP_SCHED - +/* Check whether a task group is root tg */ +#define is_root_task_group(tg) ((tg) =3D=3D &root_task_group) /* An entity is a task if it doesn't "own" a runqueue */ #define entity_is_task(se) (!se->my_q) =20 @@ -2201,6 +2200,26 @@ struct cfs_tg_state { struct sched_entity se; struct sched_statistics stats; } __no_randomize_layout; + +static inline struct sched_entity *tg_se(struct task_group *tg, int cpu) +{ + if (is_root_task_group(tg)) + return NULL; + + struct cfs_tg_state *state =3D + container_of(tg->cfs_rq[cpu], struct cfs_tg_state, cfs_rq); + return &state->se; +} + +static inline struct sched_entity *cfs_rq_se(struct cfs_rq *cfs_rq) +{ + if (is_root_task_group(cfs_rq->tg)) + return NULL; + + struct cfs_tg_state *state =3D + container_of(cfs_rq, struct cfs_tg_state, cfs_rq); + return &state->se; +} #endif =20 /* Change a task's cfs_rq and parent entity if it moves across CPUs/groups= */ @@ -2213,8 +2232,8 @@ static inline void set_task_rq(struct task_struct *p,= unsigned int cpu) #ifdef CONFIG_FAIR_GROUP_SCHED set_task_rq_fair(&p->se, p->se.cfs_rq, tg->cfs_rq[cpu]); p->se.cfs_rq =3D tg->cfs_rq[cpu]; - p->se.parent =3D tg->se[cpu]; - p->se.depth =3D tg->se[cpu] ? tg->se[cpu]->depth + 1 : 0; + p->se.parent =3D tg_se(tg, cpu); + p->se.depth =3D p->se.parent ? p->se.parent->depth + 1 : 0; #endif =20 #ifdef CONFIG_RT_GROUP_SCHED --=20 2.52.0 From nobody Mon Feb 9 00:06:44 2026 Received: from mail-yw1-f180.google.com (mail-yw1-f180.google.com [209.85.128.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CE00830AACF for ; Sun, 18 Jan 2026 03:34:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768707286; cv=none; b=NMYHuLIj+Y9LNhJzjING+hkAtYXdhVpnMfcEmNb/BvzsqMN65743nUu+hTn9pI+7rCYvRxo5MD+9MCNJvqTeZzUoEpBPjeq2I10Ajs4O1gnrRyJOcN3dl6mra8Akz8B2f0m3q3hmJSNzmo3Y5VdZ/jNQt/o/C6vp2kzB3/tAI30= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1768707286; c=relaxed/simple; bh=JDVBg1asLl9hCvVnq+f2UK9kBgZIflowYofTd6kVYRQ=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=l0ZpIS5K2c+LQJl6sEYCabEZXrmK1aVxv/8NqTgf5K2W86/1LyFui4lXkTdC8/X9gX0I8845LgrmG4r1vTOmn03kYVYlqVkgA4BtiwXW/BSZ3lLtiE1Q4/c10grcz8rucJDPUASEvcMiD7veqm9iGBagskCmlLPY3ooXsVptX18= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=ncsu.edu; spf=pass smtp.mailfrom=ncsu.edu; dkim=pass (2048-bit key) header.d=ncsu.edu header.i=@ncsu.edu header.b=N45Ld0AK; arc=none smtp.client-ip=209.85.128.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=ncsu.edu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=ncsu.edu Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=ncsu.edu header.i=@ncsu.edu header.b="N45Ld0AK" Received: by mail-yw1-f180.google.com with SMTP id 00721157ae682-78d6a3c3b77so45553267b3.0 for ; Sat, 17 Jan 2026 19:34:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ncsu.edu; s=google; t=1768707284; x=1769312084; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=YHFSbkVsGokBOlkb/5FmgNU6UygCe9PQmRIl15x6sYE=; b=N45Ld0AKoO1gyZvOOQEpN2COvj70cZHW/Zzyvkk8K+976GH7GHN11WSheq2BW38C24 3BfQV6m0nelWz7aYhgYu/Tmtot8j2i2k3NDx5gLYgrCPQdWlm0kHA7aokVl/FONY33LG nPoCklssYahBq++rENVKTrqJ8WDCUzfdL2vzXEs2zVwhmbmLGaOPuQQbckWK7MogMNXs wEG+q+L60ti/MfnCy9NmyAhQ0cRrafQd3c2e1BXw8u+tbaZpnHwYDz8/W8eSlXZxBAfb y7fbEzGc48AQGyw93K7vbT7HQQaPYE0JiYRn3Xg882m86umR1+tn6HpJTHDrOo+f79xg cTUw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1768707284; x=1769312084; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=YHFSbkVsGokBOlkb/5FmgNU6UygCe9PQmRIl15x6sYE=; b=QCtOYjbcNFNxnP22yF+WhWQ/ueomvjdYtTa2Eetlhp9V8RrS0MzYUiryo9eZD4yLEC vzBYwVG1o0an88X9/z8i4HbiBAA//b2kFYVZFdV+VAv9TnYjC/KmTRLWv60OsMBRVOpB E0Cy8RX6udsQnk5X8BJVNrjqS3RcODlGdv7Xe9VYN4XkhkDvLjR3wCAM+Z/JKubBoodQ q6Z7Wvhic9LN+3JO+g3Qzia2Ih42lXZmHnwhkYHXObJ3Wp/nbHp8XDqwx6TZfRzGTEdu zxjT/BdWO5Vz1unzU0iO7+wINEO6TP1dEjDi2Xg5Ym0vVXz4ptfoJf3jsgOp4AFsWIbE nu1g== X-Forwarded-Encrypted: i=1; AJvYcCVMFu5g2j51iYBWlIn36FJ/ussN/1z8ZDD41rxcm8fUtJp+Z6GaXUj+HFNFS5YjztOjCAtxh1VtAJSawNw=@vger.kernel.org X-Gm-Message-State: AOJu0Yyvf5TiMuxVZXFpjAMt/wj1IGVcKLiG77adC5ork+pBr6tCCEyv VnvAxmxkkgcQxC/jXIWsf3R71u0OOE+1bwwtDYkbwdEvbpP8APaoc7jLL5qkq6Xhiw== X-Gm-Gg: AY/fxX6wtBYDhzjBI2HMLM+jxmKQLz8UTI/+4IVKR3PP6vm0wAl3gvczS9ZSNwPpdDW jwerRmOGEnMcOTLawKw0qimgH4kI8U/wURhGtJ11qmrtdETx1xwfyAE36nLPzi5hm5KfRGclJiV hR1TjRURfDJtZg54Lm9UhCcBZh0UsKehg9eCBwWnUM67za4BESekSyB13ctzi1SZHHGsCmfAKye 1vI/4xlfbST86+SXmu7NsK94R98K66iZin/8tpGHiQ9mVlwkkIFf7xS25hQGl4gBExzi85cGMts 3wA3y6LTzM/TTXG4E6Jj9UHfPEyjMGxhEH2WMbpYTYtFlSBjg9UIFOecN7ourhvw6E4vAaydC8+ HweevquFyQq1GycIps7ySq+YbasnlQFk6/66dW5iPR+McvXsMEjJf7/x06yniuIWPSRLqhU6RCV KPiNdupoDVwg== X-Received: by 2002:a05:690e:dcd:b0:649:3b3:90a9 with SMTP id 956f58d0204a3-6490a5eee14mr8139885d50.9.1768707283597; Sat, 17 Jan 2026 19:34:43 -0800 (PST) Received: from um773-cachyos ([2600:1700:cc0:94af:eca6:9ff9:6f3c:5de9]) by smtp.gmail.com with ESMTPSA id 956f58d0204a3-649170acbdbsm3235607d50.13.2026.01.17.19.34.42 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 17 Jan 2026 19:34:43 -0800 (PST) From: Zecheng Li To: Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot Cc: Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , Rik van Riel , Chris Mason , Madadi Vineeth Reddy , Xu Liu , Blake Jones , Josh Don , Nilay Vaish , linux-kernel@vger.kernel.org, Zecheng Li , Zecheng Li Subject: [PATCH v7 3/3] sched/fair: Allocate both cfs_tg_state with percpu allocator Date: Sat, 17 Jan 2026 22:34:13 -0500 Message-ID: <20260118033431.158464-4-zli94@ncsu.edu> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260118033431.158464-1-zli94@ncsu.edu> References: <20260118033431.158464-1-zli94@ncsu.edu> 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 Content-Type: text/plain; charset="utf-8" From: Zecheng Li To remove the cfs_rq pointer array in task_group, allocate the combined cfs_rq and sched_entity using the per-cpu allocator. This patch implements the following: - Changes task_group->cfs_rq from struct cfs_rq ** to struct cfs_rq __percpu *. - Updates memory allocation in alloc_fair_sched_group() and free_fair_sched_group() to use alloc_percpu() and free_percpu() respectively. - Uses the inline accessor tg_cfs_rq(tg, cpu) with per_cpu_ptr() to retrieve the pointer to cfs_rq for the given task group and CPU. - Replaces direct accesses tg->cfs_rq[cpu] with calls to the new tg_cfs_rq(tg, cpu) helper. - Handles the root_task_group: since struct rq is already a per-cpu variable (runqueues), its embedded cfs_rq (rq->cfs) is also per-cpu. Therefore, we assign root_task_group.cfs_rq =3D &runqueues.cfs. - Cleanup the code in initializing the root task group. This change places each CPU's cfs_rq and sched_entity in its local per-cpu memory area to remove the per-task_group pointer arrays. Signed-off-by: Zecheng Li Signed-off-by: Zecheng Li --- kernel/sched/core.c | 35 ++++++++++----------------- kernel/sched/fair.c | 57 +++++++++++++++++--------------------------- kernel/sched/sched.h | 14 +++++++---- 3 files changed, 45 insertions(+), 61 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 8e2a67cecee9..80e8f4eb3f87 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -8549,7 +8549,7 @@ static struct kmem_cache *task_group_cache __ro_after= _init; =20 void __init sched_init(void) { - unsigned long ptr =3D 0; + unsigned long __maybe_unused ptr =3D 0; int i; =20 /* Make sure the linker didn't screw up */ @@ -8565,33 +8565,24 @@ void __init sched_init(void) wait_bit_init(); =20 #ifdef CONFIG_FAIR_GROUP_SCHED - ptr +=3D nr_cpu_ids * sizeof(void **); -#endif -#ifdef CONFIG_RT_GROUP_SCHED - ptr +=3D 2 * nr_cpu_ids * sizeof(void **); -#endif - if (ptr) { - ptr =3D (unsigned long)kzalloc(ptr, GFP_NOWAIT); + root_task_group.cfs_rq =3D &runqueues.cfs; =20 -#ifdef CONFIG_FAIR_GROUP_SCHED - root_task_group.cfs_rq =3D (struct cfs_rq **)ptr; - ptr +=3D nr_cpu_ids * sizeof(void **); - - root_task_group.shares =3D ROOT_TASK_GROUP_LOAD; - init_cfs_bandwidth(&root_task_group.cfs_bandwidth, NULL); + root_task_group.shares =3D ROOT_TASK_GROUP_LOAD; + init_cfs_bandwidth(&root_task_group.cfs_bandwidth, NULL); #endif /* CONFIG_FAIR_GROUP_SCHED */ #ifdef CONFIG_EXT_GROUP_SCHED - scx_tg_init(&root_task_group); + scx_tg_init(&root_task_group); #endif /* CONFIG_EXT_GROUP_SCHED */ #ifdef CONFIG_RT_GROUP_SCHED - root_task_group.rt_se =3D (struct sched_rt_entity **)ptr; - ptr +=3D nr_cpu_ids * sizeof(void **); + ptr +=3D 2 * nr_cpu_ids * sizeof(void **); + ptr =3D (unsigned long)kzalloc(ptr, GFP_NOWAIT); + root_task_group.rt_se =3D (struct sched_rt_entity **)ptr; + ptr +=3D nr_cpu_ids * sizeof(void **); =20 - root_task_group.rt_rq =3D (struct rt_rq **)ptr; - ptr +=3D nr_cpu_ids * sizeof(void **); + root_task_group.rt_rq =3D (struct rt_rq **)ptr; + ptr +=3D nr_cpu_ids * sizeof(void **); =20 #endif /* CONFIG_RT_GROUP_SCHED */ - } =20 init_defrootdomain(); =20 @@ -9492,7 +9483,7 @@ static int tg_set_cfs_bandwidth(struct task_group *tg, } =20 for_each_online_cpu(i) { - struct cfs_rq *cfs_rq =3D tg->cfs_rq[i]; + struct cfs_rq *cfs_rq =3D tg_cfs_rq(tg, i); struct rq *rq =3D cfs_rq->rq; =20 guard(rq_lock_irq)(rq); @@ -9660,7 +9651,7 @@ static u64 throttled_time_self(struct task_group *tg) u64 total =3D 0; =20 for_each_possible_cpu(i) { - total +=3D READ_ONCE(tg->cfs_rq[i]->throttled_clock_self_time); + total +=3D READ_ONCE(tg_cfs_rq(tg, i)->throttled_clock_self_time); } =20 return total; diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index e44bd5448fa5..bc023704acd1 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -327,7 +327,7 @@ static inline bool list_add_leaf_cfs_rq(struct cfs_rq *= cfs_rq) * to a tree or when we reach the top of the tree */ if (cfs_rq->tg->parent && - cfs_rq->tg->parent->cfs_rq[cpu]->on_list) { + tg_cfs_rq(cfs_rq->tg->parent, cpu)->on_list) { /* * If parent is already on the list, we add the child * just before. Thanks to circular linked property of @@ -335,7 +335,7 @@ static inline bool list_add_leaf_cfs_rq(struct cfs_rq *= cfs_rq) * of the list that starts by parent. */ list_add_tail_rcu(&cfs_rq->leaf_cfs_rq_list, - &(cfs_rq->tg->parent->cfs_rq[cpu]->leaf_cfs_rq_list)); + &(tg_cfs_rq(cfs_rq->tg->parent, cpu)->leaf_cfs_rq_list)); /* * The branch is now connected to its tree so we can * reset tmp_alone_branch to the beginning of the @@ -4153,7 +4153,7 @@ static void __maybe_unused clear_tg_offline_cfs_rqs(s= truct rq *rq) =20 rcu_read_lock(); list_for_each_entry_rcu(tg, &task_groups, list) { - struct cfs_rq *cfs_rq =3D tg->cfs_rq[cpu_of(rq)]; + struct cfs_rq *cfs_rq =3D tg_cfs_rq(tg, cpu_of(rq)); =20 clear_tg_load_avg(cfs_rq); } @@ -5689,7 +5689,7 @@ static inline int throttled_hierarchy(struct cfs_rq *= cfs_rq) =20 static inline int lb_throttled_hierarchy(struct task_struct *p, int dst_cp= u) { - return throttled_hierarchy(task_group(p)->cfs_rq[dst_cpu]); + return throttled_hierarchy(tg_cfs_rq(task_group(p), dst_cpu)); } =20 static inline bool task_is_throttled(struct task_struct *p) @@ -5835,7 +5835,7 @@ static void enqueue_task_fair(struct rq *rq, struct t= ask_struct *p, int flags); static int tg_unthrottle_up(struct task_group *tg, void *data) { struct rq *rq =3D data; - struct cfs_rq *cfs_rq =3D tg->cfs_rq[cpu_of(rq)]; + struct cfs_rq *cfs_rq =3D tg_cfs_rq(tg, cpu_of(rq)); struct task_struct *p, *tmp; =20 if (--cfs_rq->throttle_count) @@ -5906,7 +5906,7 @@ static void record_throttle_clock(struct cfs_rq *cfs_= rq) static int tg_throttle_down(struct task_group *tg, void *data) { struct rq *rq =3D data; - struct cfs_rq *cfs_rq =3D tg->cfs_rq[cpu_of(rq)]; + struct cfs_rq *cfs_rq =3D tg_cfs_rq(tg, cpu_of(rq)); =20 if (cfs_rq->throttle_count++) return 0; @@ -6379,8 +6379,8 @@ static void sync_throttle(struct task_group *tg, int = cpu) if (!tg->parent) return; =20 - cfs_rq =3D tg->cfs_rq[cpu]; - pcfs_rq =3D tg->parent->cfs_rq[cpu]; + cfs_rq =3D tg_cfs_rq(tg, cpu); + pcfs_rq =3D tg_cfs_rq(tg->parent, cpu); =20 cfs_rq->throttle_count =3D pcfs_rq->throttle_count; cfs_rq->throttled_clock_pelt =3D rq_clock_pelt(cpu_rq(cpu)); @@ -6572,7 +6572,7 @@ static void __maybe_unused update_runtime_enabled(str= uct rq *rq) rcu_read_lock(); list_for_each_entry_rcu(tg, &task_groups, list) { struct cfs_bandwidth *cfs_b =3D &tg->cfs_bandwidth; - struct cfs_rq *cfs_rq =3D tg->cfs_rq[cpu_of(rq)]; + struct cfs_rq *cfs_rq =3D tg_cfs_rq(tg, cpu_of(rq)); =20 raw_spin_lock(&cfs_b->lock); cfs_rq->runtime_enabled =3D cfs_b->quota !=3D RUNTIME_INF; @@ -6601,7 +6601,7 @@ static void __maybe_unused unthrottle_offline_cfs_rqs= (struct rq *rq) =20 rcu_read_lock(); list_for_each_entry_rcu(tg, &task_groups, list) { - struct cfs_rq *cfs_rq =3D tg->cfs_rq[cpu_of(rq)]; + struct cfs_rq *cfs_rq =3D tg_cfs_rq(tg, cpu_of(rq)); =20 if (!cfs_rq->runtime_enabled) continue; @@ -9408,7 +9408,7 @@ static inline int task_is_ineligible_on_dst_cpu(struc= t task_struct *p, int dest_ struct cfs_rq *dst_cfs_rq; =20 #ifdef CONFIG_FAIR_GROUP_SCHED - dst_cfs_rq =3D task_group(p)->cfs_rq[dest_cpu]; + dst_cfs_rq =3D tg_cfs_rq(task_group(p), dest_cpu); #else dst_cfs_rq =3D &cpu_rq(dest_cpu)->cfs; #endif @@ -13346,7 +13346,7 @@ static int task_is_throttled_fair(struct task_struc= t *p, int cpu) struct cfs_rq *cfs_rq; =20 #ifdef CONFIG_FAIR_GROUP_SCHED - cfs_rq =3D task_group(p)->cfs_rq[cpu]; + cfs_rq =3D tg_cfs_rq(task_group(p), cpu); #else cfs_rq =3D &cpu_rq(cpu)->cfs; #endif @@ -13612,42 +13612,31 @@ static void task_change_group_fair(struct task_st= ruct *p) =20 void free_fair_sched_group(struct task_group *tg) { - int i; - - for_each_possible_cpu(i) { - if (tg->cfs_rq && tg->cfs_rq[i]) { - struct cfs_tg_state *state =3D - container_of(tg->cfs_rq[i], struct cfs_tg_state, cfs_rq); - kfree(state); - } - } - - kfree(tg->cfs_rq); + free_percpu(tg->cfs_rq); } =20 int alloc_fair_sched_group(struct task_group *tg, struct task_group *paren= t) { - struct cfs_tg_state *state; + struct cfs_tg_state __percpu *state; struct sched_entity *se; struct cfs_rq *cfs_rq; int i; =20 - tg->cfs_rq =3D kcalloc(nr_cpu_ids, sizeof(cfs_rq), GFP_KERNEL); - if (!tg->cfs_rq) + state =3D alloc_percpu_gfp(struct cfs_tg_state, GFP_KERNEL); + if (!state) goto err; =20 + tg->cfs_rq =3D &state->cfs_rq; tg->shares =3D NICE_0_LOAD; =20 init_cfs_bandwidth(tg_cfs_bandwidth(tg), tg_cfs_bandwidth(parent)); =20 for_each_possible_cpu(i) { - state =3D kzalloc_node(sizeof(*state), - GFP_KERNEL, cpu_to_node(i)); - if (!state) + cfs_rq =3D tg_cfs_rq(tg, i); + if (!cfs_rq) goto err; =20 - cfs_rq =3D &state->cfs_rq; - se =3D &state->se; + se =3D tg_se(tg, i); init_cfs_rq(cfs_rq); init_tg_cfs_entry(tg, cfs_rq, se, i, tg_se(parent, i)); init_entity_runnable_average(se); @@ -13684,7 +13673,7 @@ void unregister_fair_sched_group(struct task_group = *tg) destroy_cfs_bandwidth(tg_cfs_bandwidth(tg)); =20 for_each_possible_cpu(cpu) { - struct cfs_rq *cfs_rq =3D tg->cfs_rq[cpu]; + struct cfs_rq *cfs_rq =3D tg_cfs_rq(tg, cpu); struct sched_entity *se =3D tg_se(tg, cpu); struct rq *rq =3D cpu_rq(cpu); =20 @@ -13721,8 +13710,6 @@ void init_tg_cfs_entry(struct task_group *tg, struc= t cfs_rq *cfs_rq, cfs_rq->rq =3D rq; init_cfs_rq_runtime(cfs_rq); =20 - tg->cfs_rq[cpu] =3D cfs_rq; - /* se could be NULL for root_task_group */ if (!se) return; @@ -13815,7 +13802,7 @@ int sched_group_set_idle(struct task_group *tg, lon= g idle) for_each_possible_cpu(i) { struct rq *rq =3D cpu_rq(i); struct sched_entity *se =3D tg_se(tg, i); - struct cfs_rq *grp_cfs_rq =3D tg->cfs_rq[i]; + struct cfs_rq *grp_cfs_rq =3D tg_cfs_rq(tg, i); bool was_idle =3D cfs_rq_is_idle(grp_cfs_rq); long idle_task_delta; struct rq_flags rf; diff --git a/kernel/sched/sched.h b/kernel/sched/sched.h index bb6daf2c1e79..8ad1780edfe8 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -477,7 +477,7 @@ struct task_group { =20 #ifdef CONFIG_FAIR_GROUP_SCHED /* runqueue "owned" by this group on each CPU */ - struct cfs_rq **cfs_rq; + struct cfs_rq __percpu *cfs_rq; unsigned long shares; /* * load_avg can be heavily contended at clock tick time, so put @@ -2201,13 +2201,19 @@ struct cfs_tg_state { struct sched_statistics stats; } __no_randomize_layout; =20 +/* Access a specific CPU's cfs_rq from a task group */ +static inline struct cfs_rq *tg_cfs_rq(struct task_group *tg, int cpu) +{ + return per_cpu_ptr(tg->cfs_rq, cpu); +} + static inline struct sched_entity *tg_se(struct task_group *tg, int cpu) { if (is_root_task_group(tg)) return NULL; =20 struct cfs_tg_state *state =3D - container_of(tg->cfs_rq[cpu], struct cfs_tg_state, cfs_rq); + container_of(tg_cfs_rq(tg, cpu), struct cfs_tg_state, cfs_rq); return &state->se; } =20 @@ -2230,8 +2236,8 @@ static inline void set_task_rq(struct task_struct *p,= unsigned int cpu) #endif =20 #ifdef CONFIG_FAIR_GROUP_SCHED - set_task_rq_fair(&p->se, p->se.cfs_rq, tg->cfs_rq[cpu]); - p->se.cfs_rq =3D tg->cfs_rq[cpu]; + set_task_rq_fair(&p->se, p->se.cfs_rq, tg_cfs_rq(tg, cpu)); + p->se.cfs_rq =3D tg_cfs_rq(tg, cpu); p->se.parent =3D tg_se(tg, cpu); p->se.depth =3D p->se.parent ? p->se.parent->depth + 1 : 0; #endif --=20 2.52.0