From nobody Fri Oct 3 08:50:45 2025 Received: from mail-qk1-f201.google.com (mail-qk1-f201.google.com [209.85.222.201]) (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 65B6A369963 for ; Wed, 3 Sep 2025 19:45:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.222.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756928713; cv=none; b=twIztyVr9u0JJjkfrlRY5+ZrTUrxm9p6aQKGZDFD0EgoWwn4x/0NUPaPzkNXuh1OY+6FNgkJ93ixbLBxiZP8URaIuUJLu1vA40x6WFQBF0G1zJvxWe2nuC4tiZ0kuIyCotCDKBU/sn06Vbs4S+kHQKFG2t+ihdqdDcGVVme627w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1756928713; c=relaxed/simple; bh=FMBiWzNwTzzlN5GTyRqSv0FYEs/GD6WMfrSmQIzif10=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=lz9AVhFWLJGc6kwEYvAYx58RDlb7kLVy4/nMdGjSgVboF+KgtmcV6xtSoT0nFbLHnPZoIXxyVy+zi+U8YbM99mbI5lB+BB6TXQpLW/GEMjr+JeVDVEITwoUi7ibe+8+nyh4Bvs30PqHsB9ue+k36mZEx0SA9rLxKyxpBkxptOMM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=DYnWsYgd; arc=none smtp.client-ip=209.85.222.201 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--zecheng.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="DYnWsYgd" Received: by mail-qk1-f201.google.com with SMTP id af79cd13be357-80584b42d15so62266785a.3 for ; Wed, 03 Sep 2025 12:45:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1756928710; x=1757533510; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=2AZ1QGVNuCg4PV0LCwZKcF9MIi5NqekDv2CLRHczJ3E=; b=DYnWsYgdyze23OWrCGqLqTTUGGQjydDNL3Amzn3kN1As4Pz1gFUtbPPL/WuwY20mcb +F5eFr44hEdsUWpUtt5VGA49xV72vxPy+zJj32pc+dOl13tF5kwo1H5vc+sKdv2cFJrG LM0VOcl9BFChwzY+wopmfASGiPLtfephzc5MCARbjEw1oQN7xbWflMvfqhiqDpCW2H32 qeDXSvKwK1hw/Y13/zO1PWacxiPqyfI0o/xsGk1/YEgT5PczW8myAoIpmzqX2gOGNcPT lEwd/MwmYpnF/ZU4nORgnpy80twa+a+F3Y74OnDWBMNNilKd5sPVZpjFSgC6LAEQDAHl Zp3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1756928710; x=1757533510; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=2AZ1QGVNuCg4PV0LCwZKcF9MIi5NqekDv2CLRHczJ3E=; b=lNsWdSeHPpMK5afJmup+7U2IsIJ82zMg9KyQgOns2YCsNrItGeQV8oV2Cx2utRT1TT 8LGgg9bQhb/v6a7qcL1FJ40eyCQqC8PcQAmipZyPhVI1oHF8ja7hS/3rCJi3WJwC/78q rSeL8olkKcKEKLowrjmlUtlD9bogiUBy9dYNMu8mGguJ3dGvnO92D/BO11LhPksrUEpp af46VOtgo4dgVeQXLD0al7eYytBcWXqm9YAcrdXd0hwFSanw2dR3AQyMEnmBlFIyxy4j DfITqGAAKyKZRc3VO9nSFHT/5mK1ktjuWh6+/osJKuVvQfH2aE4d0iibZyBNmhOuqZ1p Xvkw== X-Forwarded-Encrypted: i=1; AJvYcCXP3hYhvUJsbKPJPxNUmRo+En/+0A1tgcwtBVNVhQRFAYfnKzIN1bINyi4GdWwUcXUfRsqJCTJgem2o2Jo=@vger.kernel.org X-Gm-Message-State: AOJu0YxCKpm9fqwoCpVyO4nYddz8xXQbBMO//NL069p8amwq08+MLM4t kHSUN3XxAKjrv6OKyHdaQx4C/kFLFdIkQ/Kkw1dGuLP7RBOb8f7ZJIr2QDRN+m1XMa1H46H/6eT vpGluWXmG9w== X-Google-Smtp-Source: AGHT+IHv23SMUkd7fwdpw2+BVynRXXTL1YDNbdD21QcOQYgGUL0ZBwr2CMUnzubMXyKpHgBGLDOSQuQ2JBL2 X-Received: from qknqb2.prod.google.com ([2002:a05:620a:6502:b0:80e:b15:b019]) (user=zecheng job=prod-delivery.src-stubby-dispatcher) by 2002:a05:620a:3909:b0:7e9:f820:2b7e with SMTP id af79cd13be357-7ff2cbd845dmr1659910385a.84.1756928710177; Wed, 03 Sep 2025 12:45:10 -0700 (PDT) Date: Wed, 3 Sep 2025 19:45:01 +0000 In-Reply-To: <20250903194503.1679687-1-zecheng@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250903194503.1679687-1-zecheng@google.com> X-Mailer: git-send-email 2.51.0.338.gd7d06c2dae-goog Message-ID: <20250903194503.1679687-3-zecheng@google.com> Subject: [PATCH v4 2/3] sched/fair: Remove task_group->se pointer array 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 , linux-kernel@vger.kernel.org, Zecheng Li Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- kernel/sched/core.c | 7 ++----- kernel/sched/debug.c | 2 +- kernel/sched/fair.c | 27 ++++++++++----------------- kernel/sched/sched.h | 29 ++++++++++++++++++++++++----- 4 files changed, 37 insertions(+), 28 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index feb750aae71b..11efddf383ed 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -8691,7 +8691,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 **); @@ -8700,9 +8700,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 @@ -9785,7 +9782,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 02e16b70a790..16542596d4b0 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 fc8bc2d4614f..8777e288f0e0 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -6014,7 +6014,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; =20 /* * It's possible we are called with !runtime_remaining due to things @@ -6029,7 +6029,7 @@ void unthrottle_cfs_rq(struct cfs_rq *cfs_rq) if (cfs_rq->runtime_enabled && cfs_rq->runtime_remaining <=3D 0) return; =20 - se =3D cfs_rq->tg->se[cpu_of(rq)]; + se =3D cfs_rq_se(cfs_rq); =20 cfs_rq->throttled =3D 0; =20 @@ -9763,7 +9763,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 @@ -9783,7 +9782,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 @@ -9809,8 +9808,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 @@ -13329,7 +13327,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) @@ -13342,9 +13339,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 @@ -13359,7 +13353,7 @@ int alloc_fair_sched_group(struct task_group *tg, s= truct task_group *parent) cfs_rq =3D &combined->cfs_rq; se =3D &combined->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 @@ -13378,7 +13372,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); @@ -13395,7 +13389,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) { @@ -13432,7 +13426,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) @@ -13463,7 +13456,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)); @@ -13474,7 +13467,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 */ @@ -13525,7 +13518,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 8b37de788eeb..2b350c25f116 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -452,8 +452,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; @@ -899,7 +897,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 @@ -1583,6 +1582,26 @@ static inline struct task_struct *task_of(struct sch= ed_entity *se) return container_of(se, struct task_struct, se); } =20 +static inline struct sched_entity *tg_se(struct task_group *tg, int cpu) +{ + if (is_root_task_group(tg)) + return NULL; + + struct cfs_rq_with_se *combined =3D + container_of(tg->cfs_rq[cpu], struct cfs_rq_with_se, cfs_rq); + return &combined->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_rq_with_se *combined =3D + container_of(cfs_rq, struct cfs_rq_with_se, cfs_rq); + return &combined->se; +} + static inline struct cfs_rq *task_cfs_rq(struct task_struct *p) { return p->se.cfs_rq; @@ -2151,8 +2170,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.51.0.338.gd7d06c2dae-goog