linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Oliver Neukum <oneukum@suse.com>
To: Alan Stern <stern@rowland.harvard.edu>, Jiri Kosina <jikos@kernel.org>
Cc: USB list <linux-usb@vger.kernel.org>
Subject: Re: Duplicated code in hiddev_open()
Date: Mon, 19 Aug 2019 12:41:08 +0200	[thread overview]
Message-ID: <1566211268.5663.9.camel@suse.com> (raw)
In-Reply-To: <Pine.LNX.4.44L0.1908161228310.1525-100000@iolanthe.rowland.org>

Am Freitag, den 16.08.2019, 13:10 -0400 schrieb Alan Stern:
> Oliver and Jiri:
> 
> Why is there duplicated code in
> drivers/hid/usbhid/hiddev.c:hiddev_open()?
> 
> Line 267:
> 	/*
> 	 * no need for locking because the USB major number
> 	 * is shared which usbcore guards against disconnect
> 	 */
> 	if (list->hiddev->exist) {
> 		if (!list->hiddev->open++) {
> 			res = hid_hw_open(hiddev->hid);
> 			if (res < 0)
> 				goto bail;
> 		}
> 	} else {
> 		res = -ENODEV;
> 		goto bail;
> 	}
> 
> Line 286:
> 	mutex_lock(&hiddev->existancelock);
> 	if (!list->hiddev->open++)
> 		if (list->hiddev->exist) {
> 			struct hid_device *hid = hiddev->hid;
> 			res = hid_hw_power(hid, PM_HINT_FULLON);
> 			if (res < 0)
> 				goto bail_unlock;
> 			res = hid_hw_open(hid);
> 			if (res < 0)
> 				goto bail_normal_power;
> 		}
> 	mutex_unlock(&hiddev->existancelock);
> 
> The second part can never execute, because the first part ensures that 
> list->hiddev->open > 0 by the time the second part runs.
> 
> Even more disturbing, why is one of these code sections protected by a 
> mutex and the other not?

I suppose the comment I made back then:

079034073faf9 drivers/hid/usbhid/hiddev.c (Oliver Neukum               2008-12-16 10:55:15 +0100 268)    * no need for locking because the USB major number
079034073faf9 drivers/hid/usbhid/hiddev.c (Oliver Neukum               2008-12-16 10:55:15 +0100 269)    * is shared which usbcore guards against disconnect

has ceased to be true, but the section was not removed, as the check
for existance was duplicated.

> Note: The second section was added in commit 0361a28d3f9a ("HID: 
> autosuspend support for USB HID") over ten years ago!

Yes and I remember how frustrating keyboards were in testing, but
no further details.

	Regards
		Oliver


  reply	other threads:[~2019-08-19 10:41 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-08-16 17:10 Duplicated code in hiddev_open() Alan Stern
2019-08-19 10:41 ` Oliver Neukum [this message]
2019-08-19 14:17   ` Alan Stern
2019-08-20 14:34     ` Oliver Neukum

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=1566211268.5663.9.camel@suse.com \
    --to=oneukum@suse.com \
    --cc=jikos@kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=stern@rowland.harvard.edu \
    /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;
as well as URLs for NNTP newsgroup(s).