The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Usama Arif <usama.arif@linux.dev>
To: Usama Arif <usama.arif@linux.dev>
Cc: bsegall@google.com, dietmar.eggemann@arm.com, hannes@cmpxchg.org,
	juri.lelli@redhat.com, kprateek.nayak@amd.com,
	linux-kernel@vger.kernel.org, mgorman@suse.de, mingo@redhat.com,
	peterz@infradead.org, rostedt@goodmis.org, surenb@google.com,
	vincent.guittot@linaro.org, vschneid@redhat.com,
	shakeel.butt@linux.dev, riel@surriel.com, kernel-team@meta.com
Subject: Re: [PATCH v2] sched/psi: use __ffs() to walk task-count bitmasks in psi_group_change()
Date: Fri,  7 Aug 2026 02:52:45 -0700	[thread overview]
Message-ID: <20260807095247.1010546-1-usama.arif@linux.dev> (raw)
In-Reply-To: <20260717105939.203685-1-usama.arif@linux.dev>

On Fri, 17 Jul 2026 03:59:39 -0700 Usama Arif <usama.arif@linux.dev> wrote:

> 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 and typically have one or two bits
> set. 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 through the
> skip path. Switch both walks to __ffs() + m &= m-1 form:
> 
> 	while (clear) {
> 		t = __ffs(clear);
> 		clear &= clear - 1;
> 		...
> 	}
> 
> which iterates only over the set bits and terminates naturally on
> m == 0. m & (m - 1) clears the lowest set bit. This code is easier
> to read as well.
> 
> An in-kernel microbench (noinline, same body, IRQs off, pinned CPU
> on Zen4c, min-of-10 cyc/call) over mask distributions produced by
> common scheduler PSI paths:
> 
>   mask pattern                        old    new    delta
>   empty          (clear=0x0, set=0x0)  3.68   3.68    +0%
>   sleep          (clear=0x4, set=0x0)  9.60   3.74   -61%
>   iowait-sleep   (clear=0x4, set=0x1) 12.32   4.45   -63%
>   memstall-sleep (clear=0xc, set=0x0) 11.87   5.54   -53%
>   wake           (clear=0x0, set=0x4)  7.10   3.70   -47%
>   iowait-wake    (clear=0x1, set=0x4) 10.83   4.48   -58%
> 
> Every non-empty case wins 47-63%: old cost tracks the highest set bit
> (linear walk), new cost tracks the count of set bits (skip zeros via
> TZCNT). Single-bit patterns run at the empty-case floor.
> 
> The generated psi_group_change() text also shrinks by 67 bytes under
> -O2 -march=x86-64 (756 -> 689): no scratch register for a "constant 1"
> (only __ffs's operand is needed), simpler bit-clear (LEA+AND vs
> SHL+NOT+AND after the test), and no skip-if-unset check per position.
> 
> Acked-by: Johannes Weiner <hannes@cmpxchg.org>
> Signed-off-by: Usama Arif <usama.arif@linux.dev>


Hello Peter!

Just wanted to check if there were any comments or feedback on this patch?

I checked sched/core branch and didn't see the patch there.

Thanks!
Usama 

      reply	other threads:[~2026-08-07  9:52 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-17 10:59 [PATCH v2] sched/psi: use __ffs() to walk task-count bitmasks in psi_group_change() Usama Arif
2026-08-07  9:52 ` Usama Arif [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260807095247.1010546-1-usama.arif@linux.dev \
    --to=usama.arif@linux.dev \
    --cc=bsegall@google.com \
    --cc=dietmar.eggemann@arm.com \
    --cc=hannes@cmpxchg.org \
    --cc=juri.lelli@redhat.com \
    --cc=kernel-team@meta.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mgorman@suse.de \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=riel@surriel.com \
    --cc=rostedt@goodmis.org \
    --cc=shakeel.butt@linux.dev \
    --cc=surenb@google.com \
    --cc=vincent.guittot@linaro.org \
    --cc=vschneid@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox