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 --- 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