[PATCH v2 1/8] char: xillybus: Improve sanity check

From: Eli Billauer

Date: Tue Nov 17 2026 - 06:59:44 EST


The sanity check in fifo_init() added in commit 1ce74f6f2216 ("char:
xillybus: Add defensive sanity checks") is unnecessarily complicated
and may not work as expected due to the incorrect assumption that
1 << n == 0 when n>31 due to overflow. Replace it with a single
check at the function's invocation. This check also eliminates any
possibility for overflow in this function's left-shift operations.

Fixes: 1ce74f6f2216 ("char: xillybus: Add defensive sanity checks")
Assisted-by: Sashiko-0.2.5:gemini-3.1-pro-preview
Link: https://sashiko.dev/#/patchset/20261109112800.48663-1-eli.billauer@xxxxxxxxx
Signed-off-by: Eli Billauer <eli.billauer@xxxxxxxxx>
---

Notes:
Changes in v2:
- New patch, replacing v1's "char: xillybus: Use unsigned int
literal". Following Sashiko's review, reject log2_size >
ilog2(SZ_1G) before the left-shift operations, instead of relying
on the overflow check, which a shift by 32 bits or more could
bypass.

drivers/char/xillybus/xillyusb.c | 9 +++------
1 file changed, 3 insertions(+), 6 deletions(-)

diff --git a/drivers/char/xillybus/xillyusb.c b/drivers/char/xillybus/xillyusb.c
index bd3babd39f35..c4ddde221f0b 100644
--- a/drivers/char/xillybus/xillyusb.c
+++ b/drivers/char/xillybus/xillyusb.c
@@ -374,6 +374,9 @@ static int fifo_init(struct xillyfifo *fifo,

unsigned int log2_fifo_buf_size;

+ if (log2_size > ilog2(SZ_1G))
+ return -ENOMEM; /* Reported as greed for memory */
+
mutex_lock(&fifo_buf_order_mutex);

retry:
@@ -390,12 +393,6 @@ static int fifo_init(struct xillyfifo *fifo,
fifo->bufnum = 1 << log2_bufnum;
fifo->size = fifo->bufnum * fifo->bufsize;

- if (!fifo->size || /* Unsigned integer overflow */
- fifo->size > SZ_1G) { /* Avoid signed int issues */
- mutex_unlock(&fifo_buf_order_mutex);
- return -ENOMEM; /* Reported as greed for memory */
- }
-
fifo->mem = kmalloc_array(fifo->bufnum, sizeof(void *), GFP_KERNEL);

if (!fifo->mem) {
--
2.17.1