public inbox for linux-usb@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] usb: gadget: f_ncm: replace kzalloc with kzalloc_obj
@ 2026-03-15 11:18 Loke Forsberg
  2026-03-16  6:07 ` Greg KH
  2026-03-19 20:38 ` Kees Cook
  0 siblings, 2 replies; 5+ messages in thread
From: Loke Forsberg @ 2026-03-15 11:18 UTC (permalink / raw)
  To: linux-usb; +Cc: gregkh, khtsai, raubcameo, kees, linux-kernel, Loke Forsberg

Replace kzalloc(sizeof(*ptr)) with kzalloc_obj(), to improve type safety.

Signed-off-by: Loke Forsberg <Loke.Forsberg@gmail.com>
---
 drivers/usb/gadget/function/f_ncm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/drivers/usb/gadget/function/f_ncm.c b/drivers/usb/gadget/function/f_ncm.c
index 14fc7dce6f39..e1e6763293ef 100644
--- a/drivers/usb/gadget/function/f_ncm.c
+++ b/drivers/usb/gadget/function/f_ncm.c
@@ -1456,7 +1456,7 @@ static int ncm_bind(struct usb_configuration *c, struct usb_function *f)
 		return -EINVAL;
 
 	if (cdev->use_os_string) {
-		os_desc_table = kzalloc(sizeof(*os_desc_table), GFP_KERNEL);
+		os_desc_table = kzalloc_obj(os_desc_table, GFP_KERNEL);
 		if (!os_desc_table)
 			return -ENOMEM;
 	}
@@ -1753,7 +1753,7 @@ static struct usb_function *ncm_alloc(struct usb_function_instance *fi)
 	struct f_ncm_opts	*opts;
 
 	/* allocate and initialize one new instance */
-	ncm = kzalloc(sizeof(*ncm), GFP_KERNEL);
+	ncm = kzalloc_obj(ncm, GFP_KERNEL);
 	if (!ncm)
 		return ERR_PTR(-ENOMEM);
 
-- 
2.53.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] usb: gadget: f_ncm: replace kzalloc with kzalloc_obj
  2026-03-15 11:18 [PATCH] usb: gadget: f_ncm: replace kzalloc with kzalloc_obj Loke Forsberg
@ 2026-03-16  6:07 ` Greg KH
  2026-03-16  9:08   ` Loke Forsberg
  2026-03-19 20:38 ` Kees Cook
  1 sibling, 1 reply; 5+ messages in thread
From: Greg KH @ 2026-03-16  6:07 UTC (permalink / raw)
  To: Loke Forsberg; +Cc: linux-usb, khtsai, raubcameo, kees, linux-kernel

On Sun, Mar 15, 2026 at 12:18:27PM +0100, Loke Forsberg wrote:
> Replace kzalloc(sizeof(*ptr)) with kzalloc_obj(), to improve type safety.
> 
> Signed-off-by: Loke Forsberg <Loke.Forsberg@gmail.com>
> ---
>  drivers/usb/gadget/function/f_ncm.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/usb/gadget/function/f_ncm.c b/drivers/usb/gadget/function/f_ncm.c
> index 14fc7dce6f39..e1e6763293ef 100644
> --- a/drivers/usb/gadget/function/f_ncm.c
> +++ b/drivers/usb/gadget/function/f_ncm.c
> @@ -1456,7 +1456,7 @@ static int ncm_bind(struct usb_configuration *c, struct usb_function *f)
>  		return -EINVAL;
>  
>  	if (cdev->use_os_string) {
> -		os_desc_table = kzalloc(sizeof(*os_desc_table), GFP_KERNEL);
> +		os_desc_table = kzalloc_obj(os_desc_table, GFP_KERNEL);

Why have you used GFP_KERNEL when kzalloc_obj() does not need that for
the default case?


>  		if (!os_desc_table)
>  			return -ENOMEM;
>  	}
> @@ -1753,7 +1753,7 @@ static struct usb_function *ncm_alloc(struct usb_function_instance *fi)
>  	struct f_ncm_opts	*opts;
>  
>  	/* allocate and initialize one new instance */
> -	ncm = kzalloc(sizeof(*ncm), GFP_KERNEL);
> +	ncm = kzalloc_obj(ncm, GFP_KERNEL);

Same here.

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] usb: gadget: f_ncm: replace kzalloc with kzalloc_obj
  2026-03-16  6:07 ` Greg KH
@ 2026-03-16  9:08   ` Loke Forsberg
  0 siblings, 0 replies; 5+ messages in thread
From: Loke Forsberg @ 2026-03-16  9:08 UTC (permalink / raw)
  To: Greg KH; +Cc: linux-usb, khtsai, raubcameo, kees, linux-kernel

> Why have you used GFP_KERNEL when kzalloc_obj() does not need that for
> the default case?

I was following the pattern from the original kzalloc and didnt
realize it defaults to it. I will fix it and send a v2

Regards,
Loke Forsberg

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] usb: gadget: f_ncm: replace kzalloc with kzalloc_obj
  2026-03-15 11:18 [PATCH] usb: gadget: f_ncm: replace kzalloc with kzalloc_obj Loke Forsberg
  2026-03-16  6:07 ` Greg KH
@ 2026-03-19 20:38 ` Kees Cook
  2026-03-19 21:11   ` Kees Cook
  1 sibling, 1 reply; 5+ messages in thread
From: Kees Cook @ 2026-03-19 20:38 UTC (permalink / raw)
  To: Loke Forsberg; +Cc: linux-usb, gregkh, khtsai, raubcameo, linux-kernel

On Sun, Mar 15, 2026 at 12:18:27PM +0100, Loke Forsberg wrote:
> Replace kzalloc(sizeof(*ptr)) with kzalloc_obj(), to improve type safety.
> 
> Signed-off-by: Loke Forsberg <Loke.Forsberg@gmail.com>
> ---
>  drivers/usb/gadget/function/f_ncm.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/drivers/usb/gadget/function/f_ncm.c b/drivers/usb/gadget/function/f_ncm.c
> index 14fc7dce6f39..e1e6763293ef 100644
> --- a/drivers/usb/gadget/function/f_ncm.c
> +++ b/drivers/usb/gadget/function/f_ncm.c
> @@ -1456,7 +1456,7 @@ static int ncm_bind(struct usb_configuration *c, struct usb_function *f)
>  		return -EINVAL;
>  
>  	if (cdev->use_os_string) {
> -		os_desc_table = kzalloc(sizeof(*os_desc_table), GFP_KERNEL);
> +		os_desc_table = kzalloc_obj(os_desc_table, GFP_KERNEL);

I was surprised to find this wasn't caught by the original conversion
script, but I suspect Coccinelle is choking on the use of the cleanup
attributes:

        struct usb_os_desc_table        *os_desc_table __free(kfree) = NULL;
	...
        if (cdev->use_os_string) {
                os_desc_table = kzalloc(sizeof(*os_desc_table), GFP_KERNEL);


I'll see if I can figure this out. I may need to send another set of
bulk replacements...

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] usb: gadget: f_ncm: replace kzalloc with kzalloc_obj
  2026-03-19 20:38 ` Kees Cook
@ 2026-03-19 21:11   ` Kees Cook
  0 siblings, 0 replies; 5+ messages in thread
From: Kees Cook @ 2026-03-19 21:11 UTC (permalink / raw)
  To: Loke Forsberg; +Cc: linux-usb, gregkh, khtsai, raubcameo, linux-kernel

On Thu, Mar 19, 2026 at 01:38:23PM -0700, Kees Cook wrote:
> I was surprised to find this wasn't caught by the original conversion
> script, but I suspect Coccinelle is choking on the use of the cleanup
> attributes:
> 
>         struct usb_os_desc_table        *os_desc_table __free(kfree) = NULL;
> 	...
>         if (cdev->use_os_string) {
>                 os_desc_table = kzalloc(sizeof(*os_desc_table), GFP_KERNEL);
> 
> 
> I'll see if I can figure this out. I may need to send another set of
> bulk replacements...

Ah, it's not so bad. Latest Coccinelle handles the cleanup attributes
correctly:

 72 files changed, 104 insertions(+), 117 deletions(-)

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-03-19 21:11 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-03-15 11:18 [PATCH] usb: gadget: f_ncm: replace kzalloc with kzalloc_obj Loke Forsberg
2026-03-16  6:07 ` Greg KH
2026-03-16  9:08   ` Loke Forsberg
2026-03-19 20:38 ` Kees Cook
2026-03-19 21:11   ` Kees Cook

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox