linux/drivers/media
Alan Stern fb2d3ede3c USB: core: Fix bug caused by duplicate interface PM usage counter
commit c2b71462d2 upstream.

The syzkaller fuzzer reported a bug in the USB hub driver which turned
out to be caused by a negative runtime-PM usage counter.  This allowed
a hub to be runtime suspended at a time when the driver did not expect
it.  The symptom is a WARNING issued because the hub's status URB is
submitted while it is already active:

	URB 0000000031fb463e submitted while active
	WARNING: CPU: 0 PID: 2917 at drivers/usb/core/urb.c:363

The negative runtime-PM usage count was caused by an unfortunate
design decision made when runtime PM was first implemented for USB.
At that time, USB class drivers were allowed to unbind from their
interfaces without balancing the usage counter (i.e., leaving it with
a positive count).  The core code would take care of setting the
counter back to 0 before allowing another driver to bind to the
interface.

Later on when runtime PM was implemented for the entire kernel, the
opposite decision was made: Drivers were required to balance their
runtime-PM get and put calls.  In order to maintain backward
compatibility, however, the USB subsystem adapted to the new
implementation by keeping an independent usage counter for each
interface and using it to automatically adjust the normal usage
counter back to 0 whenever a driver was unbound.

This approach involves duplicating information, but what is worse, it
doesn't work properly in cases where a USB class driver delays
decrementing the usage counter until after the driver's disconnect()
routine has returned and the counter has been adjusted back to 0.
Doing so would cause the usage counter to become negative.  There's
even a warning about this in the USB power management documentation!

As it happens, this is exactly what the hub driver does.  The
kick_hub_wq() routine increments the runtime-PM usage counter, and the
corresponding decrement is carried out by hub_event() in the context
of the hub_wq work-queue thread.  This work routine may sometimes run
after the driver has been unbound from its interface, and when it does
it causes the usage counter to go negative.

It is not possible for hub_disconnect() to wait for a pending
hub_event() call to finish, because hub_disconnect() is called with
the device lock held and hub_event() acquires that lock.  The only
feasible fix is to reverse the original design decision: remove the
duplicate interface-specific usage counter and require USB drivers to
balance their runtime PM gets and puts.  As far as I know, all
existing drivers currently do this.

Signed-off-by: Alan Stern <stern@rowland.harvard.edu>
Reported-and-tested-by: syzbot+7634edaea4d0b341c625@syzkaller.appspotmail.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[bwh: Backported to 3.16:
 - Adjust documentation filename
 - Don't add ReST markup in documentation
 - Update use of pm_usage_cnt in poseidon driver, which has been
   removed upstream]
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
History 2019-08-13 12:39:12 +01:00
..
common [media] siano: add support for PCTV 77e 2014-11-17 14:11:49 +00:00
dvb-core media: dvb_frontend: fix locking issues at dvb_frontend_get_event() 2018-11-20 18:04:47 +00:00
dvb-frontends drxd_hard: fix bad alignments 2018-11-20 18:04:45 +00:00
firewire firewire: introduce fw_driver.probe and .remove methods 2013-06-09 18:15:00 +02:00
i2c media: v4l2: i2c: ov7670: Fix PLL bypass register values 2019-07-09 22:03:47 +01:00
mmc [media] siano: get rid of CammelCase from smscoreapi.h 2013-03-21 10:03:16 -03:00
parport [media] media: parport: Fix format string mismatch in bw-qcam.c 2014-05-23 12:18:53 -03:00
pci media: pci: cx23885: handle adding to list failure 2019-02-11 17:53:22 +00:00
platform media: s5p-jpeg: Correct step and max values for V4L2_CID_JPEG_RESTART_INTERVAL 2019-07-09 22:03:47 +01:00
radio [media] si4713: fix Kconfig dependencies 2014-03-28 18:06:55 -03:00
rc ir-core: fix gcc-7 warning on bool arithmetic 2018-11-20 18:06:02 +00:00
tuners xc2028: Fix use-after-free bug properly 2017-02-23 03:54:39 +00:00
usb USB: core: Fix bug caused by duplicate interface PM usage counter 2019-08-13 12:39:12 +01:00
v4l2-core media: v4l: ioctl: Validate num_planes for debug messages 2019-05-02 21:41:23 +01:00
Kconfig [media] subdev autoselect only works if I2C and I2C_MUX is selected 2013-12-21 06:03:06 -02:00
Makefile [media] move i2c files into drivers/media/i2c 2012-09-06 18:42:54 -03:00
media-device.c media: media-device: Remove duplicated memset() in media_enum_entities() 2014-09-17 09:21:49 -07:00
media-devnode.c [media] media: Use a better owner for the media device 2014-05-13 13:39:00 -03:00
media-entity.c [media] media: Check for active links on pads with MEDIA_PAD_FL_MUST_CONNECT flag 2013-12-04 15:03:45 -02:00