Root Cause Analysis
The warning in free_percpu() (mm/percpu.c:2262) is caused by a double free of struct sbitmap::alloc_hint when sbitmap_init_node() fails partway through initialization during SCSI device allocation.
1. Dangling sb->alloc_hint on sbitmap_init_node() failure
In lib/sbitmap.c, sbitmap_init_node() allocates sb->alloc_hint via init_alloc_hint() (alloc_percpu_gfp(unsigned int, flags)) before allocating sb->map:
if (alloc_hint) {
if (init_alloc_hint(sb, flags))
return -ENOMEM;
} else {
sb->alloc_hint = NULL;
}
sb->map = kvzalloc_node(sb->map_nr * sizeof(*sb->map), flags, node);
if (!sb->map) {
free_percpu(sb->alloc_hint);
return -ENOMEM;
}
If init_alloc_hint() succeeds (sb->alloc_hint is non-NULL) and the subsequent kvzalloc_node() for sb->map fails, sbitmap_init_node() calls free_percpu(sb->alloc_hint) and returns -ENOMEM without setting sb->alloc_hint = NULL. This leaves sb->alloc_hint containing a dangling per-CPU pointer.
Additionally, sbitmap_free() in include/linux/sbitmap.h resets sb->map = NULL after freeing it, but likewise does not reset sb->alloc_hint = NULL after free_percpu(sb->alloc_hint).
2. Trigger path through SCSI device scanning
- A write to
/sys/class/scsi_host/host*/scan (store_scan() -> scsi_scan_host_selected() -> ... -> scsi_probe_and_add_lun()) calls scsi_alloc_sdev() (drivers/scsi/scsi_scan.c).
scsi_alloc_sdev() zero-allocates struct scsi_device *sdev (kzalloc), calls scsi_sysfs_device_initialize(sdev) (which sets sdev->sdev_gendev.type = &scsi_dev_type whose release callback is scsi_device_dev_release), and then calls scsi_realloc_sdev_budget_map(sdev, depth).
- On initial allocation (
sdev->budget_map.map == NULL, need_free == false), scsi_realloc_sdev_budget_map() calls:
ret = sbitmap_init_node(&sdev->budget_map,
scsi_device_max_queue_depth(sdev),
new_shift, GFP_NOIO,
sdev->request_queue->node, false, true);
- When
init_alloc_hint() succeeds and kvzalloc_node() fails inside sbitmap_init_node(), sdev->budget_map.alloc_hint is freed once and left dangling, and scsi_realloc_sdev_budget_map() returns -ENOMEM.
scsi_alloc_sdev() jumps to out_device_destroy: and calls __scsi_remove_device(sdev) (drivers/scsi/scsi_sysfs.c), which cleans up the queue/work and calls put_device(&sdev->sdev_gendev).
- Dropping the last reference invokes
scsi_device_dev_release() (drivers/scsi/scsi_sysfs.c:482), which unconditionally calls sbitmap_free(&sdev->budget_map) (relying on sbitmap_free() being safe on uninitialized or failed budget_map structs).
sbitmap_free() calls free_percpu(sb->alloc_hint) a second time on the already-freed per-CPU pointer. pcpu_free_area() in mm/percpu.c sees the allocation bit already cleared in chunk->alloc_map, returns 0, and free_percpu() hits WARN_ON_ONCE(1) at mm/percpu.c:2262.
Fix Direction
- In
lib/sbitmap.c (sbitmap_init_node()), set sb->alloc_hint = NULL; after free_percpu(sb->alloc_hint); on the !sb->map error path.
- In
include/linux/sbitmap.h (sbitmap_free()), set sb->alloc_hint = NULL; after free_percpu(sb->alloc_hint); to keep the teardown symmetric with sb->map = NULL;.