From nobody Wed Oct 8 05:47:00 2025 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.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 81E6425F98A for ; Tue, 1 Jul 2025 21:03:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751403802; cv=none; b=CqSPzlHJaS4uEfudWTjbiBZ/ecyFglnKGVermBg97k2FKVjqo3fshAnTNXGXav4af1a1eYiYdZEYs6M4U3ejF+BPywmA7w0x9es9hnTSxISoEYeb1RgYuuV1MK9YuBTvHYkJ5YkfmCFGXkLFtDrHujnma2l2bt1BjmdarZcxYDY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751403802; c=relaxed/simple; bh=pwHlLQfORhjW4/C5V75CliIjDTJcJfGKliHIazOkgxY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=Dcgw00AojZeHnHBecfCxlOcnnhtvPAg+1LAZ5Z+7fweJ/95c7LHV2wLEXlXmug3678+VscXKw0AQ9g0NV2X1kRa+Q27jQovJdhLofT3SBzmZgYR8sCPCBCPgwmu6FMTXqe9mndemoa/HEBxhhd2HZKoIz7J2DsM78uB7ZqZ2GqU= 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=xjv0yIWh; arc=none smtp.client-ip=209.85.219.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="xjv0yIWh" Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-e87af703ef2so4719068276.3 for ; Tue, 01 Jul 2025 14:03:20 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1751403799; x=1752008599; 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=J2m3w6cd5O8puHWWtgeTVQTKU9Uuv7a92ubmbeQ56F8=; b=xjv0yIWhSnJzjguEdGLupiSWgo2J8/NZNZCsrxc1KcbakKhEW6nmN8Me1BdcRtlvQ/ 8JM4EcL5lrpu2wi52yM/Z3Jc2ZGeheONtNltkWVSDJHcHO8ATCt5CyYnVkhYngyNmVt/ OsG49rELLqmXlSUDQbVhjMl9A5vUT8ex/2PgDTW8yGyjB3U2hZI0usCRIE/jnRWfX2dv V/RJvmTL6dDb7T+Lw94iIkbgzOjhdGxrIaqsP/d8FHzUJQDUeIx2pa/FKeCgnwD2RkG6 6FM5/ypaAyw3AlNuauCZl0dFRV3/ryh9CHmmvSIATUKuFEHZSnNzEQr7NFr+7bHjuXvC zM3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1751403799; x=1752008599; 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=J2m3w6cd5O8puHWWtgeTVQTKU9Uuv7a92ubmbeQ56F8=; b=TYIu0/MQiQWlSEWhnb/eaKVVMMhK1+w//cawjistfiOU0jEowMVdBt7Bx8xYIjo51n +yQka0hSAGy2igqjiEIUOhf2t/VX/M2HixQlr85jRws8IzmZ6cYFsNtTZzkKcaWdiEu+ VbEMYBNGyI32rv8ZBtvsIKzYgRc6MrEifFX8fs9qXGtSrM4zkYbhyftfXpn/gl6pfetF k/l5e6I1tyvC1C+3dVoRuBvmhgh9wcpY/rA2Wex5Dlcfc2qACLeqKJNCZO847kSTWmVo bSihLdAqJcFlFtej8bmS5G/sBoSfRqVZY4QFZli3dMDrEuR6ECQuZEvsBchp+dTwnMrA BQ5w== X-Forwarded-Encrypted: i=1; AJvYcCV4PUxIE/ZgspNLu2jHAU/cGMLgj9BBxoNpNAcSpiMg4kcS5bdLseoa2Q9PS56NFGVDOaF4crdG07zJWjs=@vger.kernel.org X-Gm-Message-State: AOJu0Yxiqw5YqLurgPixgkkfpIEjCMy8O1fXtAHjD2a4PfVfXfxfzu8N rVyyrod4mWxOSj8BRyqaDWNIy3J7XfqjCjlJGFxojkENP/fVliW4KP7l3f1QAnz4aJ4zNKJWDs3 nJWYBVdNyTg== X-Google-Smtp-Source: AGHT+IFvDODHQS9w1n6gMVIZDFwfF9DaUNpieWFQP7GJtD852sSSCnuu1E/wnmL94zofYsS7nb3WFs6M3xM3 X-Received: from ybbdg18.prod.google.com ([2002:a05:6902:e92:b0:e81:d47f:318b]) (user=zecheng job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6902:450e:b0:e84:4721:919b with SMTP id 3f1490d57ef6-e897e40cd36mr278347276.43.1751403799388; Tue, 01 Jul 2025 14:03:19 -0700 (PDT) Date: Tue, 1 Jul 2025 21:02:27 +0000 In-Reply-To: <20250701210230.2985885-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: <20250701210230.2985885-1-zecheng@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250701210230.2985885-2-zecheng@google.com> Subject: [PATCH v3 1/3] sched/fair: Co-locate cfs_rq and sched_entity From: Zecheng Li To: Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot Cc: Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , Xu Liu , Blake Jones , Namhyung Kim , Josh Don , Madadi Vineeth Reddy , linux-kernel@vger.kernel.org, Zecheng Li Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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_rq_with_se that holds both objects in a single allocation. This patch: - Defines the new struct cfs_rq_with_se. - 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 --- kernel/sched/fair.c | 23 ++++++++++------------- kernel/sched/sched.h | 8 ++++++++ 2 files changed, 18 insertions(+), 13 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index 7a14da5396fb..3a1b55b74203 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -13356,10 +13356,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_rq_with_se *combined =3D + container_of(tg->cfs_rq[i], struct cfs_rq_with_se, cfs_rq); + kfree(combined); + } } =20 kfree(tg->cfs_rq); @@ -13368,6 +13369,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_rq_with_se *combined; struct sched_entity *se; struct cfs_rq *cfs_rq; int i; @@ -13384,16 +13386,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), + combined =3D kzalloc_node(sizeof(struct cfs_rq_with_se), GFP_KERNEL, cpu_to_node(i)); - if (!cfs_rq) + if (!combined) 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 &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_entity_runnable_average(se); @@ -13401,8 +13400,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 475bb5998295..6f32a76d38c2 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -740,6 +740,14 @@ struct cfs_rq { #endif /* CONFIG_FAIR_GROUP_SCHED */ }; =20 +#ifdef CONFIG_FAIR_GROUP_SCHED +struct cfs_rq_with_se { + struct cfs_rq cfs_rq; + /* cfs_rq's sched_entity on parent runqueue */ + struct sched_entity se ____cacheline_aligned; +}; +#endif + #ifdef CONFIG_SCHED_CLASS_EXT /* scx_rq->flags, protected by the rq lock */ enum scx_rq_flags { --=20 2.50.0 From nobody Wed Oct 8 05:47:00 2025 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.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 983F7302073 for ; Tue, 1 Jul 2025 21:03:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751403804; cv=none; b=h/y4BkieTtcmjfRA6/QR9TlWQbO8UU6IyXSAbfzBthjyDhr7amYJWiqI3QJdM+KlRwbd9pTYGJCznPYJ2QGV5XA4+tl1wq+cMvslOsfHH9QyDTMitC+04ttlNeWIc18bh4ziW4C9GUXwq49r68sH7/v16tkGHsDAxMlWPPDXbTM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751403804; c=relaxed/simple; bh=iMdEmbqqWv/mctVDH0cm5IBO7GWi/Jx0pS5fSB9u014=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=iIX4WR62pxXvZkKrpRsF+jciFqLovUkBta7hsTLKlJpZm08nVfLkLFv6RcpfQO90VKPWMOJwW21Zxgn7PKjYaCUd+eCERljnft/hu+5GRZawoPcl5nARGEHZc3WMYLBAtf+QQ6jsIhZ2dmwOFWIixW46I+AqNcJnD6xGQgtLm+Q= 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=hg2hnucQ; arc=none smtp.client-ip=209.85.128.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="hg2hnucQ" Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-7151c64e0a2so50150397b3.3 for ; Tue, 01 Jul 2025 14:03:22 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1751403801; x=1752008601; 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=tD4uncLzktAbG/AuX+h8OA+SvR06kudvrM8EhJ99BOQ=; b=hg2hnucQIoglRy/0A0viCq5cDu3CLiceSogzB4MS2ALstOC+QgTfD5UGwOU5uqGeHt 5WpTESkEqnFj5+Xp+jynb1jhigLQ/ZeXHnbV3CYxeoL3W8Pyy3j7vJawsJfpeg5rEc9l ZWalfMZUjpF3cKc3tRv7PwaeGFIa9NrVIXFDCcT+ioK/4W1JV8MROOrnhEKy8k4BrTEB 5X2t1jlmN1LOLg+c9S99ZCctaQT7gth97Emym3fZaUKx/xjn7tvrtxmlr8Xzj+lPKDtL 4AkvKqH8I2qJAeYXP2KCDDlFUQrzrB4rHqz8fObp5kTUdMx75DY9SSq+r5wwx+qY4CiQ JtZQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1751403801; x=1752008601; 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=tD4uncLzktAbG/AuX+h8OA+SvR06kudvrM8EhJ99BOQ=; b=gJEuDvxeSFtD2Rc3neU33ulLV8tpRHuy9Ybw7YlzbRgqBTLk8Ihv20kH85QIgDTEQL T1y9hQfYuYCcKJtcJvv6YI96asFelO4F8qjU9z1XYqV4gGg6df3XZyvn6eSvfYKNRa3/ D/t1iAKIKZw6agSLKxpHIfVDppF3Gekplz5pcpi4iDBtlPj3sTKpwkWfbScp/3cUQaBC 1etVUWk/9VocObFGmRkfdLe5wKLlvGqjD5msczhBBlV9ZRTQt7mJqtArpKPSUMKVmyNi EFJm/G6MxfLSTJVsveZrxy9SK2E/WUwARXLk6Xh5InizkjrqWkPANQA1jztE/hJ/MKUD U2yw== X-Forwarded-Encrypted: i=1; AJvYcCXF4UkvAqFjGnJ3qVm5MYGs1yJqQgvFHNSP4HfUjyePYMaAkFnJqBe2zTeKpRR4SwoSUx1ILJcLFmKYxBc=@vger.kernel.org X-Gm-Message-State: AOJu0YzkAJ+axqCM8gR2iC2mJVryki2TF1TqAYo5YV2yMN1LiVqiBUde F0Di0ncNE8DefCFGJOMFRe19pp/F5FyakU67whScGS5EU6VKcJMSvBeOzrDcVJxj9+HO2FT75DC s26MTt2olmA== X-Google-Smtp-Source: AGHT+IHjJGLOCr68mOyM3hy66j1Fm1HZYpf7uSn2yYGyAhQySZ7Q7LZGH3caQE/gw7TaZATbcSkCTPW01KeX X-Received: from ybbed6.prod.google.com ([2002:a05:6902:2806:b0:e82:3cde:855f]) (user=zecheng job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6902:1b89:b0:e87:a485:b426 with SMTP id 3f1490d57ef6-e897e140effmr527758276.4.1751403801505; Tue, 01 Jul 2025 14:03:21 -0700 (PDT) Date: Tue, 1 Jul 2025 21:02:28 +0000 In-Reply-To: <20250701210230.2985885-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: <20250701210230.2985885-1-zecheng@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250701210230.2985885-3-zecheng@google.com> Subject: [PATCH v3 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 , Xu Liu , Blake Jones , Namhyung Kim , Josh Don , Madadi Vineeth Reddy , 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 8988d38d46a3..2efa7e9590c7 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -8526,7 +8526,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 **); @@ -8535,9 +8535,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 @@ -9729,7 +9726,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 9d71baf08075..c2868367e17e 100644 --- a/kernel/sched/debug.c +++ b/kernel/sched/debug.c @@ -657,7 +657,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 3a1b55b74203..244b20222eb5 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -5912,7 +5912,7 @@ static bool throttle_cfs_rq(struct cfs_rq *cfs_rq) if (!dequeue) return false; /* Throttle no longer required. */ =20 - se =3D cfs_rq->tg->se[cpu_of(rq_of(cfs_rq))]; + se =3D cfs_rq_se(cfs_rq); =20 /* freeze hierarchy runnable averages while throttled */ rcu_read_lock(); @@ -5997,7 +5997,7 @@ void unthrottle_cfs_rq(struct cfs_rq *cfs_rq) long queued_delta, runnable_delta, idle_delta; long rq_h_nr_queued =3D rq->cfs.h_nr_queued; =20 - se =3D cfs_rq->tg->se[cpu_of(rq)]; + se =3D cfs_rq_se(cfs_rq); =20 cfs_rq->throttled =3D 0; =20 @@ -9801,7 +9801,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 @@ -9821,7 +9820,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 @@ -9847,8 +9846,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 @@ -13364,7 +13362,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) @@ -13377,9 +13374,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 @@ -13394,7 +13388,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 @@ -13413,7 +13407,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); @@ -13430,7 +13424,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) { @@ -13467,7 +13461,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) @@ -13498,7 +13491,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)); @@ -13509,7 +13502,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 */ @@ -13560,7 +13553,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 6f32a76d38c2..3fdcdcdab76c 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -437,8 +437,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; @@ -903,7 +901,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 @@ -1594,6 +1593,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; @@ -2168,8 +2187,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.50.0 From nobody Wed Oct 8 05:47:00 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 7AF72302CA0 for ; Tue, 1 Jul 2025 21:03:24 +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=1751403806; cv=none; b=nd4N57PqbPLNy+h3KgjAFSFM89RfOrT0uF9gBMXTSLOSr6zjfsaWPPv/jwsjU/cvYml2okuN8cCM2eXUWf227wWUNSKEgKXXXZiADgV4l6M9hoNoeE8O3aGoWPM7tIIQHACtJdckai+k0toOdAwsSQfvXxIsu3RGMWe9hMiTp5A= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1751403806; c=relaxed/simple; bh=Ue3SCAEdWkLPW6OAVsDp0Sxn9ZP2EGlzvmPr0joTd78=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=sGBoSnM2jnf4h9IRN/G++7ZcOHXWtjC6PSUNBD/qs9n1IIHfbf4GLSIEtIw3PLEiIKLbaH3eM0OGYaPaw0UTcOoRoi9FAcXXvw2Wj46Nlv2Pgd9QrYkftOObLkhr6NE/F053wxhWU2wlDaAYHNp3FN84CWHjxQtUYtNC/W8294s= 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=xltoAX2m; 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="xltoAX2m" Received: by mail-qk1-f201.google.com with SMTP id af79cd13be357-7d44ff55c98so729686685a.0 for ; Tue, 01 Jul 2025 14:03:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1751403803; x=1752008603; 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=nWvV9IB3LWilvDOqw9mRlW4vvOFk9V2aST9191hrxn4=; b=xltoAX2mJEhvldsvAsZTz0pXtNlypbHKHVABeI46fni4ROFRH4c7l3CcwRmB8k/R6/ FGRbYksWwT6klTkLShAymenW6lOw9GYmFxu0dCWs2KJLLf0lCzSWduwyW66afbnx3wvy q2U45MsD7H8iuyvMPhXF1kIdt/xMwHzv5xdBgnn2QVzOGdXrSqy7R48wzhuw7VLH5BX7 K1t305tHMgAXDQy46vYRvP0mQ6EIEcldC7mt54IaiWraByLNHM/1h6xQE1nboX6W0hHa DLHRVtuIMkh6KtOGNx6gkrrmk8/UbfmqyExuCrRkeQk7a4F3V+gxcbLdQ3A8Mkdqzh0h CIeg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1751403803; x=1752008603; 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=nWvV9IB3LWilvDOqw9mRlW4vvOFk9V2aST9191hrxn4=; b=n+g4LNh53PgvLXtGhW803J746Fy2GCys/qA+nVQcBmexoBFo0QHUTJg9QLPE9WViYa NyOEe3Kqgwo3fQBXiZWJU+wKdMSeDp5pQWlLTFid+CEUpZh+F+Lpf0PB4FjXheVJLOUs O/WHaDaClY7QmQruDdMFOoyPDQkcNWcitZkOkpCFCQvLOdp37EWExY8PauEIeNT0K+AC xhoWwss4m2Wf71nyztBqv9kbNpnKPVPAXJ1Tl6cJPKyn+qCLI1dTTkXYs67LlSafAgq3 1qsPvDOMmlU8MBpSK3n4lB0vonPGX4Ot9v/y7B1eJH0SnKQYq/62lqU5bWROJ93pwrzG DF4Q== X-Forwarded-Encrypted: i=1; AJvYcCVJ1UJNjVFZxu4ePCJxJAOc99wjoudsz0DddC7Hsw3T1W2XfO87t+LfKruGb0oeqtyE5NPXdRI041l+Xjk=@vger.kernel.org X-Gm-Message-State: AOJu0YxoE7wx8taWX0WSlBCpDuinga4Sa1+/bV5dtlq7l0weAF4M7GLL G/qBCXhptDXlgL2DOi1dQ7pikTcn1y0+Fpzx7d+O+ttdh03VENPwcYkaEsl1//yuSo7QDXywZPN j5VMuKpgZsw== X-Google-Smtp-Source: AGHT+IEX9ow6rH8hzYurHsyQQT+iv8ojy7bt0rVKqcgaEZTQc7d9rmWg0PQKxUv4mPFOYGh4mJ7HGn9gYn1a X-Received: from qkni19.prod.google.com ([2002:a05:620a:2493:b0:7ce:c274:8d6]) (user=zecheng job=prod-delivery.src-stubby-dispatcher) by 2002:a05:620a:3181:b0:7d3:b8eb:b32f with SMTP id af79cd13be357-7d5c478e870mr57422585a.37.1751403803365; Tue, 01 Jul 2025 14:03:23 -0700 (PDT) Date: Tue, 1 Jul 2025 21:02:29 +0000 In-Reply-To: <20250701210230.2985885-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: <20250701210230.2985885-1-zecheng@google.com> X-Mailer: git-send-email 2.50.0.727.gbf7dc18ff4-goog Message-ID: <20250701210230.2985885-4-zecheng@google.com> Subject: [PATCH v3 3/3] sched/fair: Allocate both cfs_rq and sched_entity with per-cpu From: Zecheng Li To: Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot Cc: Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , Xu Liu , Blake Jones , Namhyung Kim , Josh Don , Madadi Vineeth Reddy , linux-kernel@vger.kernel.org, Zecheng Li Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 --- kernel/sched/core.c | 35 ++++++++++---------------- kernel/sched/fair.c | 59 +++++++++++++++++--------------------------- kernel/sched/sched.h | 13 +++++++--- 3 files changed, 45 insertions(+), 62 deletions(-) diff --git a/kernel/sched/core.c b/kernel/sched/core.c index 2efa7e9590c7..377361fae8e8 100644 --- a/kernel/sched/core.c +++ b/kernel/sched/core.c @@ -8508,7 +8508,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 */ @@ -8526,33 +8526,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 #ifdef CONFIG_SMP init_defrootdomain(); @@ -9497,7 +9488,7 @@ static int tg_set_cfs_bandwidth(struct task_group *tg= , u64 period, u64 quota, } =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); @@ -9745,7 +9736,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 244b20222eb5..37d6b00b3a3b 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -329,7 +329,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 @@ -337,7 +337,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 @@ -4180,7 +4180,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); } @@ -5828,8 +5828,8 @@ static inline int throttled_lb_pair(struct task_group= *tg, { struct cfs_rq *src_cfs_rq, *dest_cfs_rq; =20 - src_cfs_rq =3D tg->cfs_rq[src_cpu]; - dest_cfs_rq =3D tg->cfs_rq[dest_cpu]; + src_cfs_rq =3D tg_cfs_rq(tg, src_cpu); + dest_cfs_rq =3D tg_cfs_rq(tg, dest_cpu); =20 return throttled_hierarchy(src_cfs_rq) || throttled_hierarchy(dest_cfs_rq); @@ -5838,7 +5838,7 @@ static inline int throttled_lb_pair(struct task_group= *tg, 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)); =20 cfs_rq->throttle_count--; if (!cfs_rq->throttle_count) { @@ -5867,7 +5867,7 @@ static int tg_unthrottle_up(struct task_group *tg, vo= id *data) 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 /* group is entering throttled state, stop time */ if (!cfs_rq->throttle_count) { @@ -6454,8 +6454,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)); @@ -6640,7 +6640,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; @@ -6669,7 +6669,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; @@ -9378,7 +9378,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 @@ -13095,7 +13095,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 @@ -13351,42 +13351,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_rq_with_se *combined =3D - container_of(tg->cfs_rq[i], struct cfs_rq_with_se, cfs_rq); - kfree(combined); - } - } - - 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_rq_with_se *combined; + struct cfs_rq_with_se __percpu *combined; 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) + combined =3D alloc_percpu_gfp(struct cfs_rq_with_se, GFP_KERNEL); + if (!combined) goto err; =20 + tg->cfs_rq =3D &combined->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) { - combined =3D kzalloc_node(sizeof(struct cfs_rq_with_se), - GFP_KERNEL, cpu_to_node(i)); - if (!combined) + cfs_rq =3D tg_cfs_rq(tg, i); + if (!cfs_rq) goto err; =20 - cfs_rq =3D &combined->cfs_rq; - se =3D &combined->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); @@ -13423,7 +13412,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 @@ -13460,8 +13449,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; @@ -13554,7 +13541,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 3fdcdcdab76c..a794bec99604 100644 --- a/kernel/sched/sched.h +++ b/kernel/sched/sched.h @@ -438,7 +438,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; #ifdef CONFIG_SMP /* @@ -1592,6 +1592,11 @@ static inline struct task_struct *task_of(struct sch= ed_entity *se) WARN_ON_ONCE(!entity_is_task(se)); return container_of(se, struct task_struct, se); } +/* 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); +} =20 static inline struct sched_entity *tg_se(struct task_group *tg, int cpu) { @@ -1599,7 +1604,7 @@ static inline struct sched_entity *tg_se(struct task_= group *tg, int cpu) return NULL; =20 struct cfs_rq_with_se *combined =3D - container_of(tg->cfs_rq[cpu], struct cfs_rq_with_se, cfs_rq); + container_of(tg_cfs_rq(tg, cpu), struct cfs_rq_with_se, cfs_rq); return &combined->se; } =20 @@ -2185,8 +2190,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.50.0