* [PATCH] iio: buffer: kfifo: reject lengths exceeding the kmalloc limit
@ 2026-11-20 13:21 YuanZhe Liu
2026-11-20 16:23 ` Andy Shevchenko
0 siblings, 1 reply; 3+ messages in thread
From: YuanZhe Liu @ 2026-11-20 13:21 UTC (permalink / raw)
To: Jonathan Cameron
Cc: David Lechner, Nuno Sá,
Andy Shevchenko, linux-iio, linux-kernel, Bin Yu, Cheng Wen
__iio_allocate_kfifo() rejects lengths that would overflow an unsigned
int after kfifo rounds them up to the next power of two, but the byte
size handed to __kfifo_alloc() can still be beyond what kmalloc() can
serve.
The kfifo buffer is allocated with kmalloc(), which is limited to
KMALLOC_MAX_SIZE. A larger request has an allocation order above
MAX_PAGE_ORDER, so the page allocator's order sanity check fires and
enabling the buffer ends with a WARNING instead of a clean error.
The buffer length is set through buffer/length, which accepts any value
up to UINT_MAX. With the voltage and timestamp scan channels enabled,
a length of 262349 makes the rounded up size exceed KMALLOC_MAX_SIZE
and triggers the warning on buffer/enable.
Also reject lengths whose rounded up size does not fit into
KMALLOC_MAX_SIZE, so that enabling the buffer fails with -EINVAL.
Co-developed-by: Bin Yu <byu@xidian.edu.cn>
Signed-off-by: Bin Yu <byu@xidian.edu.cn>
Co-developed-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn>
---
drivers/iio/buffer/kfifo_buf.c | 9 +++++++++
1 file changed, 9 insertions(+)
diff --git a/drivers/iio/buffer/kfifo_buf.c b/drivers/iio/buffer/kfifo_buf.c
index b67ea6468226..2069d7ca4409 100644
--- a/drivers/iio/buffer/kfifo_buf.c
+++ b/drivers/iio/buffer/kfifo_buf.c
@@ -35,6 +35,15 @@ static inline int __iio_allocate_kfifo(struct iio_kfifo *buf,
if (roundup_pow_of_two(length) > UINT_MAX / bytes_per_datum)
return -EINVAL;
+ /*
+ * The kfifo memory is allocated with kmalloc(), so the size must also
+ * fit into the largest allocation kmalloc() can serve. Larger
+ * requests need an order above MAX_PAGE_ORDER and are refused by the
+ * page allocator with a warning.
+ */
+ if (roundup_pow_of_two(length) > KMALLOC_MAX_SIZE / bytes_per_datum)
+ return -EINVAL;
+
return __kfifo_alloc((struct __kfifo *)&buf->kf, length,
bytes_per_datum, GFP_KERNEL);
}
--
2.34.1
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] iio: buffer: kfifo: reject lengths exceeding the kmalloc limit
2026-11-20 13:21 [PATCH] iio: buffer: kfifo: reject lengths exceeding the kmalloc limit YuanZhe Liu
@ 2026-11-20 16:23 ` Andy Shevchenko
2026-11-20 16:44 ` [PATCH v2] " YuanZhe Liu
0 siblings, 1 reply; 3+ messages in thread
From: Andy Shevchenko @ 2026-11-20 16:23 UTC (permalink / raw)
To: YuanZhe Liu
Cc: Jonathan Cameron, David Lechner, Nuno Sá,
Andy Shevchenko, linux-iio, linux-kernel, Bin Yu, Cheng Wen
On Fri, Nov 20, 2026 at 09:21:29PM +0800, YuanZhe Liu wrote:
> __iio_allocate_kfifo() rejects lengths that would overflow an unsigned
> int after kfifo rounds them up to the next power of two, but the byte
> size handed to __kfifo_alloc() can still be beyond what kmalloc() can
> serve.
>
> The kfifo buffer is allocated with kmalloc(), which is limited to
> KMALLOC_MAX_SIZE. A larger request has an allocation order above
> MAX_PAGE_ORDER, so the page allocator's order sanity check fires and
> enabling the buffer ends with a WARNING instead of a clean error.
>
> The buffer length is set through buffer/length, which accepts any value
> up to UINT_MAX. With the voltage and timestamp scan channels enabled,
> a length of 262349 makes the rounded up size exceed KMALLOC_MAX_SIZE
> and triggers the warning on buffer/enable.
>
> Also reject lengths whose rounded up size does not fit into
> KMALLOC_MAX_SIZE, so that enabling the buffer fails with -EINVAL.
...
> if (roundup_pow_of_two(length) > UINT_MAX / bytes_per_datum)
> return -EINVAL;
So many developers of this patch and there is no clarification
why we need to keep this check.
> + /*
> + * The kfifo memory is allocated with kmalloc(), so the size must also
> + * fit into the largest allocation kmalloc() can serve. Larger
> + * requests need an order above MAX_PAGE_ORDER and are refused by the
> + * page allocator with a warning.
> + */
> + if (roundup_pow_of_two(length) > KMALLOC_MAX_SIZE / bytes_per_datum)
> + return -EINVAL;
--
With Best Regards,
Andy Shevchenko
^ permalink raw reply [flat|nested] 3+ messages in thread
* [PATCH v2] iio: buffer: kfifo: reject lengths exceeding the kmalloc limit
2026-11-20 16:23 ` Andy Shevchenko
@ 2026-11-20 16:44 ` YuanZhe Liu
0 siblings, 0 replies; 3+ messages in thread
From: YuanZhe Liu @ 2026-11-20 16:44 UTC (permalink / raw)
To: Jonathan Cameron
Cc: Andy Shevchenko, David Lechner, Nuno Sá,
linux-iio, linux-kernel, Bin Yu, Cheng Wen
__iio_allocate_kfifo() only rejects lengths that would overflow an
unsigned int after kfifo rounds them up to the next power of two, but
the byte size handed to __kfifo_alloc() can still be beyond what
kmalloc() can serve.
The kfifo buffer is allocated with kmalloc(), which is limited to
KMALLOC_MAX_SIZE. A larger request has an allocation order above
MAX_PAGE_ORDER, so the page allocator's order sanity check fires and
enabling the buffer ends with a WARNING instead of a clean error.
The buffer length is set through buffer/length, which accepts any value
up to UINT_MAX. With the voltage and timestamp scan channels enabled,
a length of 262349 makes the rounded up size exceed KMALLOC_MAX_SIZE
and triggers the warning on buffer/enable.
Reject lengths whose rounded up size does not fit into KMALLOC_MAX_SIZE
so that enabling the buffer fails with -EINVAL. This bound is stricter
than the unsigned int overflow check it replaces, because
KMALLOC_MAX_SIZE is far below UINT_MAX, so that check is removed.
Co-developed-by: Bin Yu <byu@xidian.edu.cn>
Signed-off-by: Bin Yu <byu@xidian.edu.cn>
Co-developed-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: Cheng Wen <wencheng@xidian.edu.cn>
Signed-off-by: YuanZhe Liu <25031212351@stu.xidian.edu.cn>
---
v2:
- Drop the unsigned int overflow check. It is subsumed by the new
KMALLOC_MAX_SIZE bound: a length that would overflow the kfifo
element counter exceeds KMALLOC_MAX_SIZE as well (Andy Shevchenko).
---
drivers/iio/buffer/kfifo_buf.c | 10 +++++++---
1 file changed, 7 insertions(+), 3 deletions(-)
diff --git a/drivers/iio/buffer/kfifo_buf.c b/drivers/iio/buffer/kfifo_buf.c
index b67ea6468226..65906dfffc04 100644
--- a/drivers/iio/buffer/kfifo_buf.c
+++ b/drivers/iio/buffer/kfifo_buf.c
@@ -29,10 +29,14 @@ static inline int __iio_allocate_kfifo(struct iio_kfifo *buf,
return -EINVAL;
/*
- * Make sure we don't overflow an unsigned int after kfifo rounds up to
- * the next power of 2.
+ * The kfifo memory is allocated with kmalloc(), which cannot serve
+ * more than KMALLOC_MAX_SIZE. A larger request needs an allocation
+ * order above MAX_PAGE_ORDER and is refused by the page allocator
+ * with a warning. KMALLOC_MAX_SIZE is far below UINT_MAX, so this
+ * also keeps the element count rounded up by kfifo within an
+ * unsigned int.
*/
- if (roundup_pow_of_two(length) > UINT_MAX / bytes_per_datum)
+ if (roundup_pow_of_two(length) > KMALLOC_MAX_SIZE / bytes_per_datum)
return -EINVAL;
return __kfifo_alloc((struct __kfifo *)&buf->kf, length,
--
2.34.1
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-11-20 16:44 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-20 13:21 [PATCH] iio: buffer: kfifo: reject lengths exceeding the kmalloc limit YuanZhe Liu
2026-11-20 16:23 ` Andy Shevchenko
2026-11-20 16:44 ` [PATCH v2] " YuanZhe Liu
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®