From nobody Sat Feb 7 11:38:27 2026 Received: from mail-dl1-f43.google.com (mail-dl1-f43.google.com [74.125.82.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 BEE46396B87 for ; Wed, 4 Feb 2026 12:05:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.82.43 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770206721; cv=none; b=DdXgkmrqKMzhizuOSE9SRCOZ47XL3m6YCie5HW2+pmwZf0aT8SN/Rg0TC1ptCx9V1SYL+dwJq9DqSQVDHicN1QQ0w8fkZmzF/tOsU5NSzjzgIIwybFPk+M3gFXGa77SHmVU/+5Jd/NCsDBoTObBBFpkc5Du0J2qcjF7u9/P/W1k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770206721; c=relaxed/simple; bh=bhWcCySN9D+BMxZO58jgp9odb3FbY3p3opLMhHQeIX4=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=EItsOU/PRsCYjbL2+ly1u+AMfNkjikYJQJ+1NV2F+wDzWbTKoCj+CxR5FTMEt7pq0UUcnJkfWBhEFntRh7+fJ5LUgLHSaLTf2QMv8j4RR+EoyI2DUu9rz4RUYt0rcqgXWKjI0mCq9nzCd9VS1ps+1DBUEYJa9hgpLvJJBJHscO8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=UJZRksAl; arc=none smtp.client-ip=74.125.82.43 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="UJZRksAl" Received: by mail-dl1-f43.google.com with SMTP id a92af1059eb24-11f36012fb2so7826453c88.1 for ; Wed, 04 Feb 2026 04:05:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1770206721; x=1770811521; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=zsYuxjXnJfSdhkvcbMS9w1pjcINoSOnOZ0n0TI7+Mzs=; b=UJZRksAlrZFoL5rDlszGO5DKldOj0fRnB6GbVNFAlqbtOMUFHNlfcf+x4cAq/sG18h Xdy/mdiQssAwOXAcKVBv+LtrM+QCB5fBpK8UnUrIL0qU4ygcI9yLUTdWr1It/8e7yH6p pKYQSH1QL8OBOOZsGO0zgHQa5h0PmLcluREWY8B8JbDNsAZlhnlC089mYBMlCqaSHBb2 fTTJmZOyGuSc/qGzcKYqDrHp+1HwGrvg90hLeLFADcV8/PFJRb2sopy+9ZsBsbpUwu// sRsgGzmMomPiyqjxtXhx+/gFCUqBrNN4xc+AM2sHVtJ+fyOx0YtLMXgqfU3vod97gXTS OV1w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1770206721; x=1770811521; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=zsYuxjXnJfSdhkvcbMS9w1pjcINoSOnOZ0n0TI7+Mzs=; b=WL8xdy3sTddFhBkxhSI+JPr1E3QjsuU0GjAVlba9XJE22I+QU3HVstfPsQbIAimvVw Mg2LGowututFCXkXkZLQcgPBuxhNr1sC/2tQ9l72lCNgPCHLEgFvsS9jDzgPhsQKhrWJ pt0CbFNwPsUSNZoRoEVgEai1Dmt5x28JS9zk08H8UuugD1eb4+pb2dDMY+JsvR/bBVKR CK1+KmOOriHwdkYgukM3aIiuwCghcSvlRT4EnXfquX/kpWnqKcu8KmZbfNYUl0suZj4R Ux1R50lDLlFD/KBNIhdKZFTyyA1PIwoMzxbNrLnzG0kUQZW2x23Xz0PS0/ISVYRqMCrG k+YA== X-Forwarded-Encrypted: i=1; AJvYcCVwBYZr5xIHEKE8GmCUALlQKT2HZMgN1gdDFRJ117ho5Rj+4Rtx5yZkD4vI4/9pxrqHJMX61/RA273Q1qA=@vger.kernel.org X-Gm-Message-State: AOJu0YwqP9hEWrMFy6VMvgYH5dAR8yBN4B87iVphOdZPovd09Q0A9TvK XwHFHoMJN9pmbr3sle5H9z7/cQldOUM8qLnogwdm1QELpy7shY8Ic4ZX X-Gm-Gg: AZuq6aIz3t+pnsHb6sI7hq5w+yRULpx4rDvAw9NFB52TxJD3rGJgHtXz5CQS8MKwbvE WPLIwhtUcF5yXSMHNuxZS5oshFKopGbEZy/o8I7Prte44cFHebkVL8Tt3AScjZCUcN7ZaHOA1NR ACkNVG0BcTJA2fVYzmICWJvtbWIQo5oC5HuvVDVjfntrXYi7ttW3ll6/4LHm+jt0TlLbcK2+ASN 4uYVUtOX577C41nr+20kKGrIXRcZNTcshCNciV2W2AOyogfAZfOgHKB4y+8Yc5W6EnMMf+xwHXW 98bqK1VuISd8Gz+rRraGYdnGTFyBBw2BzjegQ00OLxZgygdo9yYIDeqSXgrRJejRmOkrBCkbZW+ j1JQLq5IWjXjya1buxPRk8uzJSF/yRb1H1EHH56UbsNAhB2Dl0dHjX5m1Wa4sUe/S7NwBN/ayMz vIMjo= X-Received: by 2002:a05:7022:670d:b0:119:e56b:91da with SMTP id a92af1059eb24-126f478fa00mr1272947c88.11.1770206720491; Wed, 04 Feb 2026 04:05:20 -0800 (PST) Received: from debian ([74.48.213.230]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-126f4e05ce7sm1762061c88.1.2026.02.04.04.05.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 04 Feb 2026 04:05:19 -0800 (PST) From: Qiliang Yuan To: Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot Cc: Qiliang Yuan , Qiliang Yuan , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , linux-kernel@vger.kernel.org Subject: [PATCH v3] sched/fair: Optimize EAS by reducing redundant performance domain scans Date: Wed, 4 Feb 2026 07:05:05 -0500 Message-ID: <20260204120509.3950227-1-realwujing@gmail.com> X-Mailer: git-send-email 2.51.0 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" Consolidate performance domain (PD) statistic calculations in the find_energy_efficient_cpu() wake-up path. Calculate 'pd_max_util' and 'pd_busy_time' during the initial CPU iteration within the performance domain. Cache these values in the local 'energy_env' structure to eliminate subsequent redundancy. This reduces the number of full PD scans from three to one per performance domain. This optimization significantly lowers the constant factor of the Energy-Aware Scheduling calculation, minimizing wake-up latency on systems with large performance domains or complex topologies. Signed-off-by: Qiliang Yuan Signed-off-by: Qiliang Yuan --- v3: - Further optimize by consolidating pd_busy_time calculation into the main loop, reducing PD scans from 3 to 1. - Rename patch title to accurately reflect "reducing redundant scans" instead of a total complexity change from O(N) to O(1), addressing reviewers' feedback. v2: - Ensure RCU safety by using local 'energy_env' for caching instead of modifying the shared 'perf_domain' structure. - Consolidate pre-calculation into the main loop to avoid an extra pass over the performance domains. v1: - Initial optimization of energy calculation by pre-calculating performance domain max utilization. kernel/sched/fair.c | 44 +++++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 19 deletions(-) diff --git a/kernel/sched/fair.c b/kernel/sched/fair.c index e71302282671..4ed10cb9e8e0 100644 --- a/kernel/sched/fair.c +++ b/kernel/sched/fair.c @@ -8148,6 +8148,7 @@ struct energy_env { unsigned long pd_busy_time; unsigned long cpu_cap; unsigned long pd_cap; + unsigned long pd_max_util; }; =20 /* @@ -8215,41 +8216,32 @@ static inline void eenv_pd_busy_time(struct energy_= env *eenv, * exceed @eenv->cpu_cap. */ static inline unsigned long -eenv_pd_max_util(struct energy_env *eenv, struct cpumask *pd_cpus, +eenv_pd_max_util(struct energy_env *eenv, struct perf_domain *pd, struct task_struct *p, int dst_cpu) { - unsigned long max_util =3D 0; - int cpu; + unsigned long max_util =3D eenv->pd_max_util; =20 - for_each_cpu(cpu, pd_cpus) { - struct task_struct *tsk =3D (cpu =3D=3D dst_cpu) ? p : NULL; - unsigned long util =3D cpu_util(cpu, p, dst_cpu, 1); + if (dst_cpu >=3D 0 && cpumask_test_cpu(dst_cpu, perf_domain_span(pd))) { + unsigned long util =3D cpu_util(dst_cpu, p, dst_cpu, 1); unsigned long eff_util, min, max; =20 - /* - * Performance domain frequency: utilization clamping - * must be considered since it affects the selection - * of the performance domain frequency. - * NOTE: in case RT tasks are running, by default the min - * utilization can be max OPP. - */ - eff_util =3D effective_cpu_util(cpu, util, &min, &max); + eff_util =3D effective_cpu_util(dst_cpu, util, &min, &max); =20 /* Task's uclamp can modify min and max value */ - if (tsk && uclamp_is_used()) { + if (uclamp_is_used()) { min =3D max(min, uclamp_eff_value(p, UCLAMP_MIN)); =20 /* * If there is no active max uclamp constraint, * directly use task's one, otherwise keep max. */ - if (uclamp_rq_is_idle(cpu_rq(cpu))) + if (uclamp_rq_is_idle(cpu_rq(dst_cpu))) max =3D uclamp_eff_value(p, UCLAMP_MAX); else max =3D max(max, uclamp_eff_value(p, UCLAMP_MAX)); } =20 - eff_util =3D sugov_effective_cpu_perf(cpu, eff_util, min, max); + eff_util =3D sugov_effective_cpu_perf(dst_cpu, eff_util, min, max); max_util =3D max(max_util, eff_util); } =20 @@ -8265,7 +8257,7 @@ static inline unsigned long compute_energy(struct energy_env *eenv, struct perf_domain *pd, struct cpumask *pd_cpus, struct task_struct *p, int dst_cpu) { - unsigned long max_util =3D eenv_pd_max_util(eenv, pd_cpus, p, dst_cpu); + unsigned long max_util =3D eenv_pd_max_util(eenv, pd, p, dst_cpu); unsigned long busy_time =3D eenv->pd_busy_time; unsigned long energy; =20 @@ -8376,12 +8368,26 @@ static int find_energy_efficient_cpu(struct task_st= ruct *p, int prev_cpu) =20 eenv.cpu_cap =3D cpu_actual_cap; eenv.pd_cap =3D 0; + eenv.pd_max_util =3D 0; + eenv.pd_busy_time =3D 0; =20 for_each_cpu(cpu, cpus) { struct rq *rq =3D cpu_rq(cpu); + unsigned long util_b, eff_util_b, min_b, max_b; + unsigned long util_bt; =20 eenv.pd_cap +=3D cpu_actual_cap; =20 + /* Pre-calculate base max utilization for the performance domain */ + util_b =3D cpu_util(cpu, p, -1, 1); + eff_util_b =3D effective_cpu_util(cpu, util_b, &min_b, &max_b); + eff_util_b =3D sugov_effective_cpu_perf(cpu, eff_util_b, min_b, max_b); + eenv.pd_max_util =3D max(eenv.pd_max_util, eff_util_b); + + /* Pre-calculate base busy time for the performance domain */ + util_bt =3D cpu_util(cpu, p, -1, 0); + eenv.pd_busy_time +=3D effective_cpu_util(cpu, util_bt, NULL, NULL); + if (!cpumask_test_cpu(cpu, sched_domain_span(sd))) continue; =20 @@ -8439,7 +8445,7 @@ static int find_energy_efficient_cpu(struct task_stru= ct *p, int prev_cpu) if (max_spare_cap_cpu < 0 && prev_spare_cap < 0) continue; =20 - eenv_pd_busy_time(&eenv, cpus, p); + eenv.pd_busy_time =3D min(eenv.pd_cap, eenv.pd_busy_time); /* Compute the 'base' energy of the pd, without @p */ base_energy =3D compute_energy(&eenv, pd, cpus, p, -1); =20 --=20 2.51.0