public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Kees Cook <kees@kernel.org>
To: Guenter Roeck <linux@roeck-us.net>
Cc: linux-kernel@vger.kernel.org
Subject: Re: [PATCH] treewide: Replace kmalloc with kmalloc_obj for non-scalar types
Date: Mon, 23 Feb 2026 14:23:27 -0800	[thread overview]
Message-ID: <202602231422.9AF121F@keescook> (raw)
In-Reply-To: <b04b6c13-7d0e-4a89-9e68-b572b6c686ac@roeck-us.net>

On Mon, Feb 23, 2026 at 08:05:10AM -0800, Guenter Roeck wrote:
> On Fri, Feb 20, 2026 at 11:49:23PM -0800, Kees Cook wrote:
> > This is the result of running the Coccinelle script from
> > scripts/coccinelle/api/kmalloc_objs.cocci. The script is designed to
> > avoid scalar types (which need careful case-by-case checking), and
> > instead replace kmalloc-family calls that allocate struct or union
> > object instances:
> > 
> > Single allocations:	kmalloc(sizeof(TYPE), ...)
> > are replaced with:	kmalloc_obj(TYPE, ...)
> > 
> > Array allocations:	kmalloc_array(COUNT, sizeof(TYPE), ...)
> > are replaced with:	kmalloc_objs(TYPE, COUNT, ...)
> > 
> > Flex array allocations:	kmalloc(struct_size(PTR, FAM, COUNT), ...)
> > are replaced with:	kmalloc_flex(*PTR, FAM, COUNT, ...)
> > 
> > (where TYPE may also be *VAR)
> > 
> > The resulting allocations no longer return "void *", instead returning
> > "TYPE *".
> > 
> > Signed-off-by: Kees Cook <kees@kernel.org>
> > ---
> > diff --git a/arch/um/drivers/ubd_kern.c b/arch/um/drivers/ubd_kern.c
> > index 37455e74d314..42f392e6add3 100644
> > --- a/arch/um/drivers/ubd_kern.c
> > +++ b/arch/um/drivers/ubd_kern.c
> > @@ -1069,20 +1069,16 @@ static int __init ubd_init(void)
> >  	if (register_blkdev(UBD_MAJOR, "ubd"))
> >  		return -1;
> >  
> > -	irq_req_buffer = kmalloc_array(UBD_REQ_BUFFER_SIZE,
> > -				       sizeof(struct io_thread_req *),
> > -				       GFP_KERNEL
> > -		);
> > +	irq_req_buffer = kmalloc_objs(struct io_thread_req *,
> > +				      UBD_REQ_BUFFER_SIZE, GFP_KERNEL);
> >  	irq_remainder = 0;
> >  
> >  	if (irq_req_buffer == NULL) {
> >  		printk(KERN_ERR "Failed to initialize ubd buffering\n");
> >  		return -ENOMEM;
> >  	}
> > -	io_req_buffer = kmalloc_array(UBD_REQ_BUFFER_SIZE,
> > -				      sizeof(struct io_thread_req *),
> > -				      GFP_KERNEL
> > -		);
> > +	io_req_buffer = kmalloc_objs(struct io_thread_req *,
> > +				     UBD_REQ_BUFFER_SIZE, GFP_KERNEL);
> 
> Building um:defconfig ... failed
> --------------
> Error log:
> arch/um/drivers/ubd_kern.c: In function 'ubd_init':
> arch/um/drivers/ubd_kern.c:1072:24: error: assignment to 'struct io_thread_req * (*)[]' from incompatible pointer type 'struct io_thread_req **' [-Wincompatible-pointer-types]
>  1072 |         irq_req_buffer = kmalloc_objs(struct io_thread_req *,
>       |                        ^
> arch/um/drivers/ubd_kern.c:1080:23: error: assignment to 'struct io_thread_req * (*)[]' from incompatible pointer type 'struct io_thread_req **' [-Wincompatible-pointer-types]
>  1080 |         io_req_buffer = kmalloc_objs(struct io_thread_req *,
> 
> Guenter

Proposed solution sent now:
https://lore.kernel.org/all/20260223214341.work.846-kees@kernel.org/

-- 
Kees Cook

      parent reply	other threads:[~2026-02-23 22:23 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-02-23 16:05 [PATCH] treewide: Replace kmalloc with kmalloc_obj for non-scalar types Guenter Roeck
2026-02-23 17:48 ` Kees Cook
2026-02-23 22:23 ` Kees Cook [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=202602231422.9AF121F@keescook \
    --to=kees@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@roeck-us.net \
    /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