2021-04-23 15:10:20

by Guenter Roeck

[permalink] [raw]
Subject: [RFC PATCH] usb: gadget: Drop unnecessary NULL checks after container_of

The parameters passed to allow_link and drop_link functions are never NULL.
That means the result of container_of() on those parameters is also
never NULL, even if the reference into the structure points to the first
element of the structure. Remove the subsequent NULL checks.

The changes in this patch were made automatically using the following
Coccinelle script.

@@
type t;
identifier v;
statement s;
@@

<+...
(
t v = container_of(...);
|
v = container_of(...);
)
...
when != v
- if (\( !v \| v == NULL \) ) s
...+>

Cc: Laurent Pinchart <[email protected]>
Cc: Felipe Balbi <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>
Signed-off-by: Guenter Roeck <[email protected]>
---
After the recent discussion about a patch which tried to add a check
against NULL after container_of(), I realized that there are a number
of such checks in the kernel.

Now the big question: Are patches like this acceptable, or do they count
as noise ?

Guenter

drivers/usb/gadget/function/uvc_configfs.c | 4 ----
1 file changed, 4 deletions(-)

diff --git a/drivers/usb/gadget/function/uvc_configfs.c b/drivers/usb/gadget/function/uvc_configfs.c
index 00fb58e50a15..b9d1bcb4f4ff 100644
--- a/drivers/usb/gadget/function/uvc_configfs.c
+++ b/drivers/usb/gadget/function/uvc_configfs.c
@@ -914,8 +914,6 @@ static int uvcg_streaming_header_allow_link(struct config_item *src,

target_fmt = container_of(to_config_group(target), struct uvcg_format,
group);
- if (!target_fmt)
- goto out;

uvcg_format_set_indices(to_config_group(target));

@@ -955,8 +953,6 @@ static void uvcg_streaming_header_drop_link(struct config_item *src,
mutex_lock(&opts->lock);
target_fmt = container_of(to_config_group(target), struct uvcg_format,
group);
- if (!target_fmt)
- goto out;

list_for_each_entry_safe(format_ptr, tmp, &src_hdr->formats, entry)
if (format_ptr->fmt == target_fmt) {
--
2.17.1


2021-04-24 06:08:01

by Greg Kroah-Hartman

[permalink] [raw]
Subject: Re: [RFC PATCH] usb: gadget: Drop unnecessary NULL checks after container_of

On Fri, Apr 23, 2021 at 08:06:26AM -0700, Guenter Roeck wrote:
> The parameters passed to allow_link and drop_link functions are never NULL.
> That means the result of container_of() on those parameters is also
> never NULL, even if the reference into the structure points to the first
> element of the structure. Remove the subsequent NULL checks.
>
> The changes in this patch were made automatically using the following
> Coccinelle script.
>
> @@
> type t;
> identifier v;
> statement s;
> @@
>
> <+...
> (
> t v = container_of(...);
> |
> v = container_of(...);
> )
> ...
> when != v
> - if (\( !v \| v == NULL \) ) s
> ...+>
>
> Cc: Laurent Pinchart <[email protected]>
> Cc: Felipe Balbi <[email protected]>
> Cc: Greg Kroah-Hartman <[email protected]>
> Signed-off-by: Guenter Roeck <[email protected]>
> ---
> After the recent discussion about a patch which tried to add a check
> against NULL after container_of(), I realized that there are a number
> of such checks in the kernel.
>
> Now the big question: Are patches like this acceptable, or do they count
> as noise ?

Yes they are acceptable, and no, they are not noise.

I will be glad to take this after -rc1 is out, thanks.

thanks,

greg k-h

2021-04-24 08:11:54

by Felipe Balbi

[permalink] [raw]
Subject: Re: [RFC PATCH] usb: gadget: Drop unnecessary NULL checks after container_of


Hi Guenter,

Guenter Roeck <[email protected]> writes:
> The parameters passed to allow_link and drop_link functions are never NULL.
> That means the result of container_of() on those parameters is also
> never NULL, even if the reference into the structure points to the first
> element of the structure. Remove the subsequent NULL checks.
>
> The changes in this patch were made automatically using the following
> Coccinelle script.
>
> @@
> type t;
> identifier v;
> statement s;
> @@
>
> <+...
> (
> t v = container_of(...);
> |
> v = container_of(...);
> )
> ...
> when != v
> - if (\( !v \| v == NULL \) ) s
> ...+>
>
> Cc: Laurent Pinchart <[email protected]>
> Cc: Felipe Balbi <[email protected]>
> Cc: Greg Kroah-Hartman <[email protected]>
> Signed-off-by: Guenter Roeck <[email protected]>
> ---
> After the recent discussion about a patch which tried to add a check
> against NULL after container_of(), I realized that there are a number
> of such checks in the kernel.
>
> Now the big question: Are patches like this acceptable, or do they count
> as noise ?

Not noise in my book :-)

Acked-by: Felipe Balbi <[email protected]>

--
balbi


Attachments:
signature.asc (873.00 B)

2021-04-24 22:11:20

by Laurent Pinchart

[permalink] [raw]
Subject: Re: [RFC PATCH] usb: gadget: Drop unnecessary NULL checks after container_of

Hi Guenter,

On Sat, Apr 24, 2021 at 11:03:19AM +0300, Felipe Balbi wrote:
> Guenter Roeck <[email protected]> writes:
> > The parameters passed to allow_link and drop_link functions are never NULL.
> > That means the result of container_of() on those parameters is also
> > never NULL, even if the reference into the structure points to the first
> > element of the structure. Remove the subsequent NULL checks.
> >
> > The changes in this patch were made automatically using the following
> > Coccinelle script.
> >
> > @@
> > type t;
> > identifier v;
> > statement s;
> > @@
> >
> > <+...
> > (
> > t v = container_of(...);
> > |
> > v = container_of(...);
> > )
> > ...
> > when != v
> > - if (\( !v \| v == NULL \) ) s
> > ...+>
> >
> > Cc: Laurent Pinchart <[email protected]>
> > Cc: Felipe Balbi <[email protected]>
> > Cc: Greg Kroah-Hartman <[email protected]>
> > Signed-off-by: Guenter Roeck <[email protected]>
> > ---
> > After the recent discussion about a patch which tried to add a check
> > against NULL after container_of(), I realized that there are a number
> > of such checks in the kernel.
> >
> > Now the big question: Are patches like this acceptable, or do they count
> > as noise ?
>
> Not noise in my book :-)
>
> Acked-by: Felipe Balbi <[email protected]>

Likewise,

Reviewed-by: Laurent Pinchart <[email protected]>

And thank you for the patch.

--
Regards,

Laurent Pinchart