Linux USB
 help / color / mirror / Atom feed
From: Jakob Koschel <jakobkoschel@gmail.com>
To: Greg KH <gregkh@linuxfoundation.org>
Cc: Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Felipe Balbi <balbi@kernel.org>, Joel Stanley <joel@jms.id.au>,
	Andrew Jeffery <andrew@aj.id.au>,
	Nicolas Ferre <nicolas.ferre@microchip.com>,
	Alexandre Belloni <alexandre.belloni@bootlin.com>,
	Claudiu Beznea <claudiu.beznea@microchip.com>,
	Cristian Birsan <cristian.birsan@microchip.com>,
	Al Cooper <alcooperx@gmail.com>, Li Yang <leoyang.li@nxp.com>,
	Vladimir Zapolskiy <vz@mleia.com>,
	Daniel Mack <daniel@zonque.org>,
	Haojian Zhuang <haojian.zhuang@gmail.com>,
	Robert Jarzmik <robert.jarzmik@free.fr>,
	Krzysztof Kozlowski <krzysztof.kozlowski@canonical.com>,
	Alim Akhtar <alim.akhtar@samsung.com>,
	Thierry Reding <thierry.reding@gmail.com>,
	Jonathan Hunter <jonathanh@nvidia.com>,
	Michal Simek <michal.simek@xilinx.com>,
	"open list:USB GADGET/PERIPHERAL SUBSYSTEM" 
	<linux-usb@vger.kernel.org>, Mike Rapoport <rppt@kernel.org>,
	Brian Johannesmeyer <bjohannesmeyer@gmail.com>,
	Cristiano Giuffrida <c.giuffrida@vu.nl>,
	"Bos, H.J." <h.j.bos@vu.nl>
Subject: Re: [PATCH 25/26] usb: gadget: dummy_hcd: replace usage of rc to check if a list element was found
Date: Sun, 6 Mar 2022 20:16:46 +0100	[thread overview]
Message-ID: <024FB75A-A6AD-4ED1-8E1D-5126A4692FD8@gmail.com> (raw)
In-Reply-To: <YiT2odfvXhp4nsK4@kroah.com>



> On 6. Mar 2022, at 19:00, Greg KH <gregkh@linuxfoundation.org> wrote:
> 
> On Sun, Mar 06, 2022 at 06:50:33PM +0100, Jakob Koschel wrote:
>> To move the list iterator variable into the list_for_each_entry_*()
>> macro in the future it should be avoided to use the list iterator
>> variable after the loop body.
>> 
>> To *never* use the list iterator variable after the loop it was
>> concluded to use a separate iterator variable [1].
>> 
>> This removes the need to check the rc value to determine if the
>> break/goto was hit and can be made more obvious
>> by checking if the variable was set within the list traversal loop.
>> 
>> Link: https://lore.kernel.org/all/YhdfEIwI4EdtHdym@kroah.com/
>> Signed-off-by: Jakob Koschel <jakobkoschel@gmail.com>
>> ---
>> drivers/usb/gadget/udc/dummy_hcd.c | 11 ++++++-----
>> 1 file changed, 6 insertions(+), 5 deletions(-)
>> 
>> diff --git a/drivers/usb/gadget/udc/dummy_hcd.c b/drivers/usb/gadget/udc/dummy_hcd.c
>> index a2d956af42a2..f21944707707 100644
>> --- a/drivers/usb/gadget/udc/dummy_hcd.c
>> +++ b/drivers/usb/gadget/udc/dummy_hcd.c
>> @@ -751,7 +751,7 @@ static int dummy_dequeue(struct usb_ep *_ep, struct usb_request *_req)
>> 	struct dummy		*dum;
>> 	int			retval = -EINVAL;
>> 	unsigned long		flags;
>> -	struct dummy_request	*req = NULL;
>> +	struct dummy_request	*req = NULL, *tmp;
>> 
>> 	if (!_ep || !_req)
>> 		return retval;
>> @@ -763,17 +763,18 @@ static int dummy_dequeue(struct usb_ep *_ep, struct usb_request *_req)
>> 
>> 	local_irq_save(flags);
>> 	spin_lock(&dum->lock);
>> -	list_for_each_entry(req, &ep->queue, queue) {
>> -		if (&req->req == _req) {
>> -			list_del_init(&req->queue);
>> +	list_for_each_entry(tmp, &ep->queue, queue) {
>> +		if (&tmp->req == _req) {
>> +			list_del_init(&tmp->queue);
>> 			_req->status = -ECONNRESET;
>> +			req = tmp;
>> 			retval = 0;
>> 			break;
>> 		}
>> 	}
>> 	spin_unlock(&dum->lock);
>> 
>> -	if (retval == 0) {
>> +	if (req) {
> 
> There's no need for this change as we are testing retval, not req here,
> unlike the other udc drivers.
> 
> So this one I think is correct as-is, or am I mistaken somehow?

The check is correct as-is. I just felt it would be more explicit to
actually check if the pointer that is used within the block is not
NULL than implicitly checking this through retval. There are other
blocks which do like:

list_for_each_entry(pos, head, list) {
	if (...) {
		rc = -1;
		goto fail;
	}
}

rc = call_unrelated_function(...);
if (rc == -1)
	goto fail;

...

return 0;

fail:
	*pos->member;

While this code is obviously broken and then one in this patch works fine,
I feel like it's easier to follow the rule of always checking of pos != NULL.

It might also make it easier for some static analyzers to find potential
NULL pointer dereferences but it probably doesn't matter.

If you prefer keeping retval I'll just do that instead.

> 
> thanks,
> 
> greg k-h

Jakob


  reply	other threads:[~2022-03-06 19:16 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-06 17:50 [PATCH 00/26] usb: gadget: remove usage of list iterator past the loop Jakob Koschel
2022-03-06 17:50 ` [PATCH 01/26] usb: gadget: fsl: remove usage of list iterator past the loop body Jakob Koschel
2022-03-06 18:39   ` Linus Torvalds
2022-03-06 19:19     ` Jakob Koschel
2022-03-06 17:50 ` [PATCH 02/26] usb: gadget: bdc: " Jakob Koschel
2022-03-06 17:50 ` [PATCH 03/26] usb: gadget: udc: atmel: " Jakob Koschel
2022-03-06 17:50 ` [PATCH 04/26] usb: gadget: udc: pxa25x: " Jakob Koschel
2022-03-06 17:50 ` [PATCH 05/26] usb: gadget: udc: at91: " Jakob Koschel
2022-03-06 17:50 ` [PATCH 06/26] usb: gadget: goku_udc: " Jakob Koschel
2022-03-06 17:50 ` [PATCH 07/26] usb: gadget: udc: gr_udc: " Jakob Koschel
2022-03-06 17:50 ` [PATCH 08/26] usb: gadget: lpc32xx_udc: " Jakob Koschel
2022-03-06 17:50 ` [PATCH 09/26] usb: gadget: mv_u3d: " Jakob Koschel
2022-03-06 17:50 ` [PATCH 10/26] usb: gadget: udc: mv_udc_core: " Jakob Koschel
2022-03-06 17:50 ` [PATCH 11/26] usb: gadget: net2272: " Jakob Koschel
2022-03-06 17:50 ` [PATCH 12/26] usb: gadget: udc: net2280: " Jakob Koschel
2022-03-06 17:50 ` [PATCH 13/26] usb: gadget: omap_udc: " Jakob Koschel
2022-03-06 17:50 ` [PATCH 14/26] usb: gadget: s3c-hsudc: " Jakob Koschel
2022-03-06 17:50 ` [PATCH 15/26] usb: gadget: udc-xilinx: " Jakob Koschel
2022-03-06 17:50 ` [PATCH 16/26] usb: gadget: aspeed: " Jakob Koschel
2022-03-06 17:50 ` [PATCH 17/26] usb: gadget: configfs: remove using list iterator after loop body as a ptr Jakob Koschel
2022-03-06 17:50 ` [PATCH 18/26] usb: gadget: legacy: " Jakob Koschel
2022-03-06 17:50 ` [PATCH 19/26] usb: gadget: udc: max3420_udc: " Jakob Koschel
2022-03-06 17:50 ` [PATCH 20/26] usb: gadget: tegra-xudc: " Jakob Koschel
2022-03-06 17:50 ` [PATCH 21/26] usb: gadget: composite: remove check of list iterator against head past the loop body Jakob Koschel
2022-03-06 17:50 ` [PATCH 22/26] usb: gadget: pxa27x_udc: replace usage of rc to check if a list element was found Jakob Koschel
2022-03-06 17:50 ` [PATCH 23/26] usb: gadget: udc: s3c2410: " Jakob Koschel
2022-03-06 17:50 ` [PATCH 24/26] usb: gadget: udc: core: " Jakob Koschel
2022-03-06 17:50 ` [PATCH 25/26] usb: gadget: dummy_hcd: " Jakob Koschel
2022-03-06 18:00   ` Greg KH
2022-03-06 19:16     ` Jakob Koschel [this message]
2022-03-06 17:50 ` [PATCH 26/26] usb: gadget: udc: s3c2410: " Jakob Koschel
2022-03-07 12:13   ` Krzysztof Kozlowski

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=024FB75A-A6AD-4ED1-8E1D-5126A4692FD8@gmail.com \
    --to=jakobkoschel@gmail.com \
    --cc=alcooperx@gmail.com \
    --cc=alexandre.belloni@bootlin.com \
    --cc=alim.akhtar@samsung.com \
    --cc=andrew@aj.id.au \
    --cc=balbi@kernel.org \
    --cc=bjohannesmeyer@gmail.com \
    --cc=c.giuffrida@vu.nl \
    --cc=claudiu.beznea@microchip.com \
    --cc=cristian.birsan@microchip.com \
    --cc=daniel@zonque.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=h.j.bos@vu.nl \
    --cc=haojian.zhuang@gmail.com \
    --cc=joel@jms.id.au \
    --cc=jonathanh@nvidia.com \
    --cc=krzysztof.kozlowski@canonical.com \
    --cc=leoyang.li@nxp.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=michal.simek@xilinx.com \
    --cc=nicolas.ferre@microchip.com \
    --cc=robert.jarzmik@free.fr \
    --cc=rppt@kernel.org \
    --cc=thierry.reding@gmail.com \
    --cc=torvalds@linux-foundation.org \
    --cc=vz@mleia.com \
    /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