All of lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "K Prateek Nayak" <kprateek.nayak@amd.com>
Cc: "Christian Borntraeger" <borntraeger@linux.ibm.com>,
	"Alexander Gordeev" <agordeev@linux.ibm.com>,
	linux-s390@vger.kernel.org, "Vasily Gorbik" <gor@linux.ibm.com>,
	"Heiko Carstens" <hca@linux.ibm.com>
Subject: Re: [RFC PATCH v3 11/13] lib/sbm: Add helpers to allocate, set, clear, and traverse the bits on sbm
Date: Fri, 02 Oct 2026 09:13:27 +0000	[thread overview]
Message-ID: <20261002091327.F173F1F00899@smtp.kernel.org> (raw)
In-Reply-To: <20261001192849.74788-12-kprateek.nayak@amd.com>

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] sched/fair: out-of-bounds memory corruption in __sbm_op() during CPU hotplug
- [High] lib/sbm: premature decrement of max index in free_sbm_index() hides active CPUs
- [Low] lib/sbm: sbm_alloc() massively over-allocates cacheline-aligned memory
--

commit 8d84c933216fc8041e9a487dfb8a2121359eb99f
Author: Peter Zijlstra <peterz@infradead.org>

lib/sbm: Add helpers to allocate, set, clear, and traverse the bits on sbm

This commit introduces a set of helpers to allocate a sparsebitmap (sbm) 
structure based on architecture-configured lengths. It provides macros and 
inline functions to set, clear, test, and iterate over bits in the sbm array.

> diff --git a/include/linux/sbm.h b/include/linux/sbm.h
> --- a/include/linux/sbm.h
> +++ b/include/linux/sbm.h
[ ... ]
> @@ -9,12 +11,95 @@
> +#define __sbm_op(sbm, func)				\
> +({							\
> +	int idx = sbm_cpu_to_idx(cpu);			\
> +	int nr = idx >> __sbm_shift;			\
> +	int bit = idx & __sbm_mask;			\
> +							\
> +	func(bit, &sbm[nr].bitmap);			\
> +})

[Severity: High]
Can this sequence lead to out-of-bounds memory corruption during CPU
hotplug?

When a CPU is brought offline, free_sbm_index() sets its index to -1.
If the dying CPU continues to run the idle task to tear down, it can
trigger tick_nohz_idle_stop_tick() and consequently call sbm_cpu_set().

Because __sbm_op lacks bounds checking on the mapped idx, computing
idx = -1 results in nr = -1, which then unconditionally executes func()
on &sbm[-1].bitmap.

> diff --git a/lib/sbm.c b/lib/sbm.c
> --- a/lib/sbm.c
> +++ b/lib/sbm.c
[ ... ]
> @@ -45,6 +51,44 @@ int sbm_idx_to_cpu(int idx)
[ ... ]
> +struct sbm *sbm_alloc(void)
> +{
> +	return kzalloc_objs(struct sbm, sbm_max_threads_per_instance * sbm_num_instance);
> +}

[Severity: Low]
Does this code allocate significantly more memory than needed?

Each struct sbm encapsulates a full bitmask that accommodates
sbm_max_threads_per_instance CPUs. Since the index calculation
nr = idx >> __sbm_shift directly corresponds to the leaf index, the
array only seems to require exactly sbm_num_instance elements.

Allocating sbm_max_threads_per_instance * sbm_num_instance elements
appears to request considerably more cacheline-aligned memory than is
actually necessary to map all bits.

[ ... ]
> @@ -127,6 +173,15 @@ void free_sbm_index(int cpu)
[ ... ]
>  	if (find_first_bit(&__sbm_idx_metadata[leaf].allocated_mask, BITS_PER_LONG) ==
>  	    BITS_PER_LONG)
>  		__sbm_idx_metadata[leaf].instance_id = -1;
> +
> +	if (leaf == sbm_max_populated_index) {
> +		for (idx = leaf - 1; idx > -1; idx--) {
> +			if (__sbm_idx_metadata[idx].instance_id != -1)
> +				break;
> +		}
> +
> +		WRITE_ONCE(sbm_max_populated_index, max(idx, 0));
> +	}
>  }

[Severity: High]
Could this prematurely shrink the maximum index and strand active CPUs?

When clearing a bit in a leaf's allocated_mask, if the leaf matches
sbm_max_populated_index, this loop unconditionally scans backward to find
the next non-empty leaf and overwrites sbm_max_populated_index.

However, it does this without verifying if the current maximum leaf
actually became completely empty (instance_id == -1). If other CPUs
remain actively allocated in that same leaf, they appear to be
immediately hidden since the global max index is truncated below them.

This would cause functions like sbm_empty() and sbm_find_next_bit() to
permanently ignore these active CPUs during NOHZ idle load balancing.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261001192849.74788-1-kprateek.nayak@amd.com?part=11

  reply	other threads:[~2026-10-02  9:13 UTC|newest]

Thread overview: 42+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-01 19:28 [RFC PATCH v3 00/13] lib, sched: Introduce sparsebitmap (sbm) K Prateek Nayak
2026-10-01 19:28 ` [RFC PATCH v3 01/13] lib/sbm: Introduce helpers for architectures to configure LLC properties K Prateek Nayak
2026-10-02  9:13   ` sashiko-bot
2026-10-07  5:43   ` Shrikanth Hegde
2026-10-12  2:11     ` K Prateek Nayak
2026-10-12 22:22   ` Tim Chen
2026-10-01 19:28 ` [RFC PATCH v3 02/13] drivers/base/arch_topology: Add support for initializing sbm topology K Prateek Nayak
2026-10-02  9:13   ` sashiko-bot
2026-10-12 23:41   ` Tim Chen
2026-10-01 19:28 ` [RFC PATCH v3 03/13] LoongArch: Initialize CPU _PXM relation for disabled CPUs from SRAT K Prateek Nayak
2026-10-02  9:13   ` sashiko-bot
2026-10-01 19:28 ` [RFC PATCH v3 04/13] LoongArch: Configure sbm topology during SMP preparation K Prateek Nayak
2026-10-02  9:13   ` sashiko-bot
2026-10-07  3:51   ` [RFC PATCH v3.1 " K Prateek Nayak
2026-10-01 19:28 ` [RFC PATCH v3 05/13] MIPS: Initialize sbm topology on multi-node systems K Prateek Nayak
2026-10-02  9:13   ` sashiko-bot
2026-10-01 19:28 ` [RFC PATCH v3 06/13] powerpc/setup: Initialize sbm topology based on coregroup / NUMA topology K Prateek Nayak
2026-10-02  9:13   ` sashiko-bot
2026-10-07 14:43   ` Shrikanth Hegde
2026-10-12  2:29     ` K Prateek Nayak
2026-10-01 19:28 ` [RFC PATCH v3 07/13] s390/topology: Initialize sbm topology during topology_init_early() K Prateek Nayak
2026-10-02  9:13   ` sashiko-bot
2026-10-07 10:19   ` Mete Durlu
2026-10-12  2:20     ` K Prateek Nayak
2026-10-13 10:14       ` Mete Durlu
2026-10-01 19:28 ` [RFC PATCH v3 08/13] sparc64: Initialize sbm topology on multi-LLC system K Prateek Nayak
2026-10-02  9:13   ` sashiko-bot
2026-10-01 19:28 ` [RFC PATCH v3 09/13] x86/cpu/topology: Initialize sbm topology after topology parsing K Prateek Nayak
2026-10-02  9:13   ` sashiko-bot
2026-10-03  8:27   ` Chen Yu
2026-10-04  6:17     ` K Prateek Nayak
2026-10-07  3:52   ` [RFC PATCH v3.1 " K Prateek Nayak
2026-10-01 19:28 ` [RFC PATCH v3 10/13] lib/sbm: Dynamically allocate sbm index when CPU is activated K Prateek Nayak
2026-10-02  9:13   ` sashiko-bot
2026-10-01 19:28 ` [RFC PATCH v3 11/13] lib/sbm: Add helpers to allocate, set, clear, and traverse the bits on sbm K Prateek Nayak
2026-10-02  9:13   ` sashiko-bot [this message]
2026-10-01 19:28 ` [RFC PATCH v3 12/13] sched/fair: Allocate nohz.idle_cpus_mask during sched_init_smp() K Prateek Nayak
2026-10-02  9:13   ` sashiko-bot
2026-10-01 19:28 ` [RFC PATCH v3 13/13] sched/fair: Switch nohz.idle_cpus to use sbm K Prateek Nayak
2026-10-02  9:13   ` sashiko-bot
2026-10-03  9:10 ` [RFC PATCH v3 00/13] lib, sched: Introduce sparsebitmap (sbm) Chen Yu
2026-10-04  6:13   ` K Prateek Nayak

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=20261002091327.F173F1F00899@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=agordeev@linux.ibm.com \
    --cc=borntraeger@linux.ibm.com \
    --cc=gor@linux.ibm.com \
    --cc=hca@linux.ibm.com \
    --cc=kprateek.nayak@amd.com \
    --cc=linux-s390@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /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 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.