[PATCH] brd: normalize non-positive max_part before rounding it up
From: Samuel Moelius
Date: Mon Jun 08 2026 - 20:04:38 EST
`max_part` is an `int` module parameter, but brd only resets zero before
rounding non-divisor values with `1UL << fls(max_part)`.
A negative value such as -1 passes the initial zero check. The modulo
test then reaches the roundup, where `fls(-1)` yields 32. On 64-bit
builds that produces 4294967296, which is then assigned back to `int
max_part` as zero. `brd_alloc()` passes that zero value to
`disk->minors`, and block core warns and rejects the disk.
Normalize non-positive values to the existing one-partition fallback
before the modulo/roundup, and apply the existing `DISK_MAX_PARTS` clamp
before the roundup so it only operates on representable, in-range
values.
Assisted-by: Codex:gpt-5.5-cyber-preview
Signed-off-by: Samuel Moelius <sam.moelius@xxxxxxxxxxxxxxx>
---
drivers/block/brd.c | 13 +++++++------
1 file changed, 7 insertions(+), 6 deletions(-)
diff --git a/drivers/block/brd.c b/drivers/block/brd.c
index 00cc8122068f..ed9567f74579 100644
--- a/drivers/block/brd.c
+++ b/drivers/block/brd.c
@@ -371,9 +371,15 @@ static void brd_cleanup(void)
static inline void brd_check_and_reset_par(void)
{
- if (unlikely(!max_part))
+ if (unlikely(max_part <= 0))
max_part = 1;
+ if (max_part > DISK_MAX_PARTS) {
+ pr_info("brd: max_part can't be larger than %d, reset max_part = %d.\n",
+ DISK_MAX_PARTS, DISK_MAX_PARTS);
+ max_part = DISK_MAX_PARTS;
+ }
+
/*
* make sure 'max_part' can be divided exactly by (1U << MINORBITS),
* otherwise, it is possiable to get same dev_t when adding partitions.
@@ -381,11 +387,6 @@ static inline void brd_check_and_reset_par(void)
if ((1U << MINORBITS) % max_part != 0)
max_part = 1UL << fls(max_part);
- if (max_part > DISK_MAX_PARTS) {
- pr_info("brd: max_part can't be larger than %d, reset max_part = %d.\n",
- DISK_MAX_PARTS, DISK_MAX_PARTS);
- max_part = DISK_MAX_PARTS;
- }
}
static int __init brd_init(void)
--
2.43.0