mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] sbitmap: reset alloc_hint to NULL after freeing
@ 2026-12-02  4:15 Thanh Nguyen
  2026-12-02 15:02 ` Bart Van Assche
  0 siblings, 1 reply; 4+ messages in thread
From: Thanh Nguyen @ 2026-12-02  4:15 UTC (permalink / raw)
  To: Jens Axboe, Andrew Morton
  Cc: Ming Lei, linux-block, linux-scsi, linux-kernel, stable,
	syzbot+4138704b4276d9a57251

In sbitmap_init_node(), if init_alloc_hint() succeeds and the subsequent
kvzalloc_node() for sb->map fails, the error path calls
free_percpu(sb->alloc_hint) without resetting sb->alloc_hint to NULL.

When a caller tears down the enclosing object on failure (for example,
scsi_alloc_sdev() failing in scsi_realloc_sdev_budget_map() and calling
__scsi_remove_device() -> scsi_device_dev_release() -> sbitmap_free()),
the dangling sb->alloc_hint pointer is passed to free_percpu() a second
time, triggering WARN_ON_ONCE(1) in free_percpu().

Reset sb->alloc_hint to NULL after free_percpu() in both
sbitmap_init_node() and sbitmap_free() (consistent with sb->map = NULL),
and also initialize sb->alloc_hint to NULL in the depth == 0 early return
path of sbitmap_init_node().

Fixes: c548e62bcf6a ("scsi: sbitmap: Move allocation hint into sbitmap")
Reported-by: syzbot+4138704b4276d9a57251@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=4138704b4276d9a57251
Cc: stable@vger.kernel.org
Signed-off-by: Thanh Nguyen <thanhnguyentuan2007@gmail.com>
---
 include/linux/sbitmap.h | 1 +
 lib/sbitmap.c           | 2 ++
 2 files changed, 3 insertions(+)

diff --git a/include/linux/sbitmap.h b/include/linux/sbitmap.h
index cc7ad189c..620480e33 100644
--- a/include/linux/sbitmap.h
+++ b/include/linux/sbitmap.h
@@ -185,6 +185,7 @@ static inline unsigned int __map_depth(const struct sbitmap *sb, int index)
 static inline void sbitmap_free(struct sbitmap *sb)
 {
 	free_percpu(sb->alloc_hint);
+	sb->alloc_hint = NULL;
 	kvfree(sb->map);
 	sb->map = NULL;
 }
diff --git a/lib/sbitmap.c b/lib/sbitmap.c
index 4d188d05d..59879e27d 100644
--- a/lib/sbitmap.c
+++ b/lib/sbitmap.c
@@ -119,6 +119,7 @@ int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
 
 	if (depth == 0) {
 		sb->map = NULL;
+		sb->alloc_hint = NULL;
 		return 0;
 	}
 
@@ -132,6 +133,7 @@ int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
 	sb->map = kvzalloc_node(sb->map_nr * sizeof(*sb->map), flags, node);
 	if (!sb->map) {
 		free_percpu(sb->alloc_hint);
+		sb->alloc_hint = NULL;
 		return -ENOMEM;
 	}
 
-- 
2.56.1


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

* Re: [PATCH] sbitmap: reset alloc_hint to NULL after freeing
  2026-12-02  4:15 [PATCH] sbitmap: reset alloc_hint to NULL after freeing Thanh Nguyen
@ 2026-12-02 15:02 ` Bart Van Assche
  2026-12-02 15:18   ` thanhnguyentuan2007
  0 siblings, 1 reply; 4+ messages in thread
From: Bart Van Assche @ 2026-12-02 15:02 UTC (permalink / raw)
  To: Thanh Nguyen, Jens Axboe, Andrew Morton
  Cc: Ming Lei, linux-block, linux-scsi, linux-kernel, stable,
	syzbot+4138704b4276d9a57251

On 12/1/26 8:15 PM, Thanh Nguyen wrote:
> diff --git a/include/linux/sbitmap.h b/include/linux/sbitmap.h
> index cc7ad189c..620480e33 100644
> --- a/include/linux/sbitmap.h
> +++ b/include/linux/sbitmap.h
> @@ -185,6 +185,7 @@ static inline unsigned int __map_depth(const struct sbitmap *sb, int index)
>   static inline void sbitmap_free(struct sbitmap *sb)
>   {
>   	free_percpu(sb->alloc_hint);
> +	sb->alloc_hint = NULL;
>   	kvfree(sb->map);
>   	sb->map = NULL;
>   }
> diff --git a/lib/sbitmap.c b/lib/sbitmap.c
> index 4d188d05d..59879e27d 100644
> --- a/lib/sbitmap.c
> +++ b/lib/sbitmap.c
> @@ -119,6 +119,7 @@ int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
>   
>   	if (depth == 0) {
>   		sb->map = NULL;
> +		sb->alloc_hint = NULL;
>   		return 0;
>   	}
>   
> @@ -132,6 +133,7 @@ int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
>   	sb->map = kvzalloc_node(sb->map_nr * sizeof(*sb->map), flags, node);
>   	if (!sb->map) {
>   		free_percpu(sb->alloc_hint);
> +		sb->alloc_hint = NULL;
>   		return -ENOMEM;
>   	}
>   

This patch looks more complete to me:
https://lore.kernel.org/linux-block/d8da37180d44f60c0d4345683bfa2e3a2cfb8335.1795999192.git.bvanassche@acm.org/

Thanks,

Bart.

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

* Re: [PATCH] sbitmap: reset alloc_hint to NULL after freeing
  2026-12-02 15:02 ` Bart Van Assche
@ 2026-12-02 15:18   ` thanhnguyentuan2007
  2026-12-02 16:59     ` Bart Van Assche
  0 siblings, 1 reply; 4+ messages in thread
From: thanhnguyentuan2007 @ 2026-12-02 15:18 UTC (permalink / raw)
  To: Bart Van Assche
  Cc: Jens Axboe, Andrew Morton, Ming Lei, linux-block, linux-scsi,
	linux-kernel, stable, syzbot+4138704b4276d9a57251

Hi Bart,

Thanks for pointing this out. Your patch is indeed more complete — I
missed the dangling sbq->ws in sbitmap_queue_free() and the
init_alloc_hint() failure path. I hadn't seen your patch before
sending mine.

I'll drop my patch.

Thanks,
Thanh

On Wed, 2 Dec 2026 07:02:33 -0800, Bart Van Assche <bvanassche@acm.org> wrote:
> On 12/1/26 8:15 PM, Thanh Nguyen wrote:
> > diff --git a/include/linux/sbitmap.h b/include/linux/sbitmap.h
> > index cc7ad189c..620480e33 100644
> > --- a/include/linux/sbitmap.h
> > +++ b/include/linux/sbitmap.h
> > @@ -185,6 +185,7 @@ static inline unsigned int __map_depth(const struct sbitmap *sb, int index)
> >   static inline void sbitmap_free(struct sbitmap *sb)
> >   {
> >   	free_percpu(sb->alloc_hint);
> > +	sb->alloc_hint = NULL;
> >   	kvfree(sb->map);
> >   	sb->map = NULL;
> >   }
> > diff --git a/lib/sbitmap.c b/lib/sbitmap.c
> > index 4d188d05d..59879e27d 100644
> > --- a/lib/sbitmap.c
> > +++ b/lib/sbitmap.c
> > @@ -119,6 +119,7 @@ int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
> >
> >   	if (depth == 0) {
> >   		sb->map = NULL;
> > +		sb->alloc_hint = NULL;
> >   		return 0;
> >   	}
> >
> > @@ -132,6 +133,7 @@ int sbitmap_init_node(struct sbitmap *sb, unsigned int depth, int shift,
> >   	sb->map = kvzalloc_node(sb->map_nr * sizeof(*sb->map), flags, node);
> >   	if (!sb->map) {
> >   		free_percpu(sb->alloc_hint);
> > +		sb->alloc_hint = NULL;
> >   		return -ENOMEM;
> >   	}
> >
>
> This patch looks more complete to me:
> https://lore.kernel.org/linux-block/d8da37180d44f60c0d4345683bfa2e3a2cfb8335.1795999192.git.bvanassche@acm.org/
>
> Thanks,
>
> Bart.

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

* Re: [PATCH] sbitmap: reset alloc_hint to NULL after freeing
  2026-12-02 15:18   ` thanhnguyentuan2007
@ 2026-12-02 16:59     ` Bart Van Assche
  0 siblings, 0 replies; 4+ messages in thread
From: Bart Van Assche @ 2026-12-02 16:59 UTC (permalink / raw)
  To: thanhnguyentuan2007
  Cc: Jens Axboe, Andrew Morton, Ming Lei, linux-block, linux-scsi,
	linux-kernel, stable, syzbot+4138704b4276d9a57251

On 12/2/26 7:18 AM, thanhnguyentuan2007@gmail.com wrote:
> Thanks for pointing this out. Your patch is indeed more complete — I
> missed the dangling sbq->ws in sbitmap_queue_free() and the
> init_alloc_hint() failure path. I hadn't seen your patch before
> sending mine.
Does this count as a Reviewed-by?

Thanks,

Bart.

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

end of thread, other threads:[~2026-12-02 16:59 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-12-02  4:15 [PATCH] sbitmap: reset alloc_hint to NULL after freeing Thanh Nguyen
2026-12-02 15:02 ` Bart Van Assche
2026-12-02 15:18   ` thanhnguyentuan2007
2026-12-02 16:59     ` Bart Van Assche

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®