All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] sched/psi: use for_each_set_bit() in psi_group_change() task-count walk
@ 2026-07-14 14:20 Usama Arif
  2026-07-15  3:49 ` K Prateek Nayak
  2026-07-15 13:54 ` Johannes Weiner
  0 siblings, 2 replies; 5+ messages in thread
From: Usama Arif @ 2026-07-14 14:20 UTC (permalink / raw)
  To: bsegall, dietmar.eggemann, hannes, juri.lelli, kprateek.nayak,
	linux-kernel, mgorman, mingo, peterz, rostedt, surenb,
	vincent.guittot, vschneid, shakeel.butt, riel, kernel-team
  Cc: Usama Arif

psi_group_change() walks the @clear and @set bitmasks to
decrement/increment groupc->tasks[t]. Both masks are at most
NR_PSI_TASK_COUNTS (=4) wide, dense at [0, 4), and typically
sparse. Today's form visits every position up to the highest set
bit:

	for (t = 0, m = clear; m; m &= ~(1 << t), t++) {
		if (!(m & (1 << t)))
			continue;
		...
	}

so a mask with only bit 3 set still spins four times; the same
open-coded shape repeats for @set. The code is also unnecessarily
hard to read.

Switch both walks to for_each_set_bit() which is easier to read
and also more efficient. As NR_PSI_TASK_COUNTS is a compile-time
constant <= BITS_PER_LONG, find_next_bit() folds into its
small_const_nbits() fast path (single load + GENMASK + __ffs), lowering
to a bit-scan where one exists (x86 TZCNT/BSF, arm64 RBIT+CLZ).

psi_group_change() runs from psi_task_switch() and psi_task_change()
once per ancestor psi_group per event, so the saved iterations
multiply out on any hot scheduler workload.

No functional change intended.

Signed-off-by: Usama Arif <usama.arif@linux.dev>
---
 kernel/sched/psi.c | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/kernel/sched/psi.c b/kernel/sched/psi.c
index d9c9d9480a45..f5ae1ceb21a7 100644
--- a/kernel/sched/psi.c
+++ b/kernel/sched/psi.c
@@ -798,7 +798,8 @@ static void psi_group_change(struct psi_group *group, int cpu,
 			     u64 now, bool wake_clock)
 {
 	struct psi_group_cpu *groupc;
-	unsigned int t, m;
+	unsigned long clear_bits, set_bits;
+	unsigned int t;
 	u32 state_mask;
 
 	lockdep_assert_rq_held(cpu_rq(cpu));
@@ -824,9 +825,8 @@ static void psi_group_change(struct psi_group *group, int cpu,
 	 * The rest of the state mask is calculated based on the task
 	 * counts. Update those first, then construct the mask.
 	 */
-	for (t = 0, m = clear; m; m &= ~(1 << t), t++) {
-		if (!(m & (1 << t)))
-			continue;
+	clear_bits = clear;
+	for_each_set_bit(t, &clear_bits, NR_PSI_TASK_COUNTS) {
 		if (groupc->tasks[t]) {
 			groupc->tasks[t]--;
 		} else if (!psi_bug) {
@@ -838,9 +838,9 @@ static void psi_group_change(struct psi_group *group, int cpu,
 		}
 	}
 
-	for (t = 0; set; set &= ~(1 << t), t++)
-		if (set & (1 << t))
-			groupc->tasks[t]++;
+	set_bits = set;
+	for_each_set_bit(t, &set_bits, NR_PSI_TASK_COUNTS)
+		groupc->tasks[t]++;
 
 	if (!group->enabled) {
 		/*
-- 
2.53.0-Meta


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-07-15 17:04 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-14 14:20 [PATCH] sched/psi: use for_each_set_bit() in psi_group_change() task-count walk Usama Arif
2026-07-15  3:49 ` K Prateek Nayak
2026-07-15 10:47   ` Usama Arif
2026-07-15 13:54 ` Johannes Weiner
2026-07-15 17:03   ` Usama Arif

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.