mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jonathan Cameron <jic23@kernel.org>
To: "YuanZhe Liu" <25031212351@stu.xidian.edu.cn>,
	"Nuno Sá" <nuno.sa@analog.com>
Cc: Andy Shevchenko <andy@kernel.org>,
	David Lechner <dlechner@baylibre.com>,
	linux-iio@vger.kernel.org, linux-kernel@vger.kernel.org,
	Bin Yu <byu@xidian.edu.cn>, Cheng Wen <wencheng@xidian.edu.cn>
Subject: Re: [PATCH v2] iio: buffer: kfifo: reject lengths exceeding the kmalloc limit
Date: Sat, 21 Nov 2026 01:55:49 +0000	[thread overview]
Message-ID: <20261121015549.4b6148b3@jic23-hlaptop> (raw)
In-Reply-To: <20261120164403.5691-1-25031212351@stu.xidian.edu.cn>

On Sat, 21 Nov 2026 00:44:03 +0800
YuanZhe Liu <25031212351@stu.xidian.edu.cn> wrote:

> __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.

There is a lot here, when it could just say

__kfifo_alloc() uses kmalloc to allocate the FIFO. Tighten the limit
of __iio_allocate_kfifo() to match.  This results in -EINVAL being
returned on oversized requests rather than XXXX.


Note I can't tell from this description what that ABI change is
as you don't include that information which is neeeded.

However that's an implementation detail of KFIFOs sneaking
across the boundary.  Given the allocation call fails anyway, why
do we care about short cutting it?

> 
> 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).
Two process things.

1) Slow down.  Even for a small patch like this, a few days for
first round of reviews.

2) Don't send new versions in replies to old ones.  Generally means
   your emails are hidden away and can occasionally trip up tooling.
   I'm not aware of any area of the kernel that takes things like
   this.  Seems to be a pattern the LLMs have learnt from other
   projects.

> ---
>  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,


      reply	other threads:[~2026-11-21  1:55 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-20 13:21 [PATCH] " YuanZhe Liu
2026-11-20 16:23 ` Andy Shevchenko
2026-11-20 16:44   ` [PATCH v2] " YuanZhe Liu
2026-11-21  1:55     ` Jonathan Cameron [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261121015549.4b6148b3@jic23-hlaptop \
    --to=jic23@kernel.org \
    --cc=25031212351@stu.xidian.edu.cn \
    --cc=andy@kernel.org \
    --cc=byu@xidian.edu.cn \
    --cc=dlechner@baylibre.com \
    --cc=linux-iio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nuno.sa@analog.com \
    --cc=wencheng@xidian.edu.cn \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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®