Prevent out-of-bounds array accesses in raid5_wakeup_stripe_thread() and __get_priority_stripe() on systems with non-contiguous NUMA node IDs. alloc_thread_groups() sized the worker_groups array using num_possible_nodes(), which returns the count of possible NUMA nodes. However, the array is indexed by cpu_to_group(), which maps to cpu_to_node() and returns the raw NUMA node ID. On systems with non-contiguous NUMA node IDs (e.g. POWER9 with nodes 0 and 8), num_possible_nodes() returns 2 while cpu_to_node() can return 8, causing an out-of-bounds access into conf->worker_groups. Avoid the following crash when group_thread_cnt is set to any non-zero value via sysfs: BUG: Unable to handle kernel data access at 0x6f776b6f6f625f73 NIP: __list_add_valid+0x2c/0x100 LR: raid5_wakeup_stripe_thread+0x258/0x340 [raid456] Call Trace: raid5_wakeup_stripe_thread+0x258/0x340 [raid456] raid5d+0x2a4/0x9c0 [raid456] md_thread+0x108/0x280 [md_mod] Use nr_node_ids (the highest possible node ID + 1) so that the array is correctly sized for indexing by raw NUMA node ID. Fixes: 851c30c9badf ("raid5: offload stripe handle to workqueue") Cc: stable@vger.kernel.org Reported-by: Patrick Bernier Signed-off-by: Bart Van Assche --- drivers/md/raid5.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/md/raid5.c b/drivers/md/raid5.c index d52adf81065a..c7b4576b8f91 100644 --- a/drivers/md/raid5.c +++ b/drivers/md/raid5.c @@ -7465,7 +7465,7 @@ static int alloc_thread_groups(struct r5conf *conf, int cnt, int *group_cnt, *worker_groups = NULL; return 0; } - *group_cnt = num_possible_nodes(); + *group_cnt = nr_node_ids; size = sizeof(struct r5worker) * cnt; workers = kcalloc(size, *group_cnt, GFP_NOIO); *worker_groups = kzalloc_objs(struct r5worker_group, *group_cnt,