<feed xmlns='http://www.w3.org/2005/Atom'>
<title>linux-stable.git/drivers/usb/gadget, branch linux-3.2.y</title>
<subtitle>Linux kernel stable tree</subtitle>
<link rel='alternate' type='text/html' href='https://git.tavy.me/linux-stable.git/'/>
<entry>
<title>USB: dummy-hcd: Fix deadlock caused by disconnect detection</title>
<updated>2018-01-01T20:50:56+00:00</updated>
<author>
<name>Alan Stern</name>
<email>stern@rowland.harvard.edu</email>
</author>
<published>2017-10-06T14:27:44+00:00</published>
<link rel='alternate' type='text/html' href='https://git.tavy.me/linux-stable.git/commit/?id=64a4896ee653aa0ff4541a60f7b6da6094ba652b'/>
<id>64a4896ee653aa0ff4541a60f7b6da6094ba652b</id>
<content type='text'>
commit ab219221a5064abfff9f78c323c4a257b16cdb81 upstream.

The dummy-hcd driver calls the gadget driver's disconnect callback
under the wrong conditions.  It should invoke the callback when Vbus
power is turned off, but instead it does so when the D+ pullup is
turned off.

This can cause a deadlock in the composite core when a gadget driver
is unregistered:

[   88.361471] ============================================
[   88.362014] WARNING: possible recursive locking detected
[   88.362580] 4.14.0-rc2+ #9 Not tainted
[   88.363010] --------------------------------------------
[   88.363561] v4l_id/526 is trying to acquire lock:
[   88.364062]  (&amp;(&amp;cdev-&gt;lock)-&gt;rlock){....}, at: [&lt;ffffffffa0547e03&gt;] composite_disconnect+0x43/0x100 [libcomposite]
[   88.365051]
[   88.365051] but task is already holding lock:
[   88.365826]  (&amp;(&amp;cdev-&gt;lock)-&gt;rlock){....}, at: [&lt;ffffffffa0547b09&gt;] usb_function_deactivate+0x29/0x80 [libcomposite]
[   88.366858]
[   88.366858] other info that might help us debug this:
[   88.368301]  Possible unsafe locking scenario:
[   88.368301]
[   88.369304]        CPU0
[   88.369701]        ----
[   88.370101]   lock(&amp;(&amp;cdev-&gt;lock)-&gt;rlock);
[   88.370623]   lock(&amp;(&amp;cdev-&gt;lock)-&gt;rlock);
[   88.371145]
[   88.371145]  *** DEADLOCK ***
[   88.371145]
[   88.372211]  May be due to missing lock nesting notation
[   88.372211]
[   88.373191] 2 locks held by v4l_id/526:
[   88.373715]  #0:  (&amp;(&amp;cdev-&gt;lock)-&gt;rlock){....}, at: [&lt;ffffffffa0547b09&gt;] usb_function_deactivate+0x29/0x80 [libcomposite]
[   88.374814]  #1:  (&amp;(&amp;dum_hcd-&gt;dum-&gt;lock)-&gt;rlock){....}, at: [&lt;ffffffffa05bd48d&gt;] dummy_pullup+0x7d/0xf0 [dummy_hcd]
[   88.376289]
[   88.376289] stack backtrace:
[   88.377726] CPU: 0 PID: 526 Comm: v4l_id Not tainted 4.14.0-rc2+ #9
[   88.378557] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-1 04/01/2014
[   88.379504] Call Trace:
[   88.380019]  dump_stack+0x86/0xc7
[   88.380605]  __lock_acquire+0x841/0x1120
[   88.381252]  lock_acquire+0xd5/0x1c0
[   88.381865]  ? composite_disconnect+0x43/0x100 [libcomposite]
[   88.382668]  _raw_spin_lock_irqsave+0x40/0x54
[   88.383357]  ? composite_disconnect+0x43/0x100 [libcomposite]
[   88.384290]  composite_disconnect+0x43/0x100 [libcomposite]
[   88.385490]  set_link_state+0x2d4/0x3c0 [dummy_hcd]
[   88.386436]  dummy_pullup+0xa7/0xf0 [dummy_hcd]
[   88.387195]  usb_gadget_disconnect+0xd8/0x160 [udc_core]
[   88.387990]  usb_gadget_deactivate+0xd3/0x160 [udc_core]
[   88.388793]  usb_function_deactivate+0x64/0x80 [libcomposite]
[   88.389628]  uvc_function_disconnect+0x1e/0x40 [usb_f_uvc]

This patch changes the code to test the port-power status bit rather
than the port-connect status bit when deciding whether to isue the
callback.

Signed-off-by: Alan Stern &lt;stern@rowland.harvard.edu&gt;
Reported-by: David Tulloh &lt;david@tulloh.id.au&gt;
Signed-off-by: Felipe Balbi &lt;felipe.balbi@linux.intel.com&gt;
[bwh: Backported to 3.2: adjust filename, context]
Signed-off-by: Ben Hutchings &lt;ben@decadent.org.uk&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
commit ab219221a5064abfff9f78c323c4a257b16cdb81 upstream.

The dummy-hcd driver calls the gadget driver's disconnect callback
under the wrong conditions.  It should invoke the callback when Vbus
power is turned off, but instead it does so when the D+ pullup is
turned off.

This can cause a deadlock in the composite core when a gadget driver
is unregistered:

[   88.361471] ============================================
[   88.362014] WARNING: possible recursive locking detected
[   88.362580] 4.14.0-rc2+ #9 Not tainted
[   88.363010] --------------------------------------------
[   88.363561] v4l_id/526 is trying to acquire lock:
[   88.364062]  (&amp;(&amp;cdev-&gt;lock)-&gt;rlock){....}, at: [&lt;ffffffffa0547e03&gt;] composite_disconnect+0x43/0x100 [libcomposite]
[   88.365051]
[   88.365051] but task is already holding lock:
[   88.365826]  (&amp;(&amp;cdev-&gt;lock)-&gt;rlock){....}, at: [&lt;ffffffffa0547b09&gt;] usb_function_deactivate+0x29/0x80 [libcomposite]
[   88.366858]
[   88.366858] other info that might help us debug this:
[   88.368301]  Possible unsafe locking scenario:
[   88.368301]
[   88.369304]        CPU0
[   88.369701]        ----
[   88.370101]   lock(&amp;(&amp;cdev-&gt;lock)-&gt;rlock);
[   88.370623]   lock(&amp;(&amp;cdev-&gt;lock)-&gt;rlock);
[   88.371145]
[   88.371145]  *** DEADLOCK ***
[   88.371145]
[   88.372211]  May be due to missing lock nesting notation
[   88.372211]
[   88.373191] 2 locks held by v4l_id/526:
[   88.373715]  #0:  (&amp;(&amp;cdev-&gt;lock)-&gt;rlock){....}, at: [&lt;ffffffffa0547b09&gt;] usb_function_deactivate+0x29/0x80 [libcomposite]
[   88.374814]  #1:  (&amp;(&amp;dum_hcd-&gt;dum-&gt;lock)-&gt;rlock){....}, at: [&lt;ffffffffa05bd48d&gt;] dummy_pullup+0x7d/0xf0 [dummy_hcd]
[   88.376289]
[   88.376289] stack backtrace:
[   88.377726] CPU: 0 PID: 526 Comm: v4l_id Not tainted 4.14.0-rc2+ #9
[   88.378557] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-1 04/01/2014
[   88.379504] Call Trace:
[   88.380019]  dump_stack+0x86/0xc7
[   88.380605]  __lock_acquire+0x841/0x1120
[   88.381252]  lock_acquire+0xd5/0x1c0
[   88.381865]  ? composite_disconnect+0x43/0x100 [libcomposite]
[   88.382668]  _raw_spin_lock_irqsave+0x40/0x54
[   88.383357]  ? composite_disconnect+0x43/0x100 [libcomposite]
[   88.384290]  composite_disconnect+0x43/0x100 [libcomposite]
[   88.385490]  set_link_state+0x2d4/0x3c0 [dummy_hcd]
[   88.386436]  dummy_pullup+0xa7/0xf0 [dummy_hcd]
[   88.387195]  usb_gadget_disconnect+0xd8/0x160 [udc_core]
[   88.387990]  usb_gadget_deactivate+0xd3/0x160 [udc_core]
[   88.388793]  usb_function_deactivate+0x64/0x80 [libcomposite]
[   88.389628]  uvc_function_disconnect+0x1e/0x40 [usb_f_uvc]

This patch changes the code to test the port-power status bit rather
than the port-connect status bit when deciding whether to isue the
callback.

Signed-off-by: Alan Stern &lt;stern@rowland.harvard.edu&gt;
Reported-by: David Tulloh &lt;david@tulloh.id.au&gt;
Signed-off-by: Felipe Balbi &lt;felipe.balbi@linux.intel.com&gt;
[bwh: Backported to 3.2: adjust filename, context]
Signed-off-by: Ben Hutchings &lt;ben@decadent.org.uk&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>USB: dummy-hcd: Fix erroneous synchronization change</title>
<updated>2018-01-01T20:50:53+00:00</updated>
<author>
<name>Alan Stern</name>
<email>stern@rowland.harvard.edu</email>
</author>
<published>2017-09-26T19:15:49+00:00</published>
<link rel='alternate' type='text/html' href='https://git.tavy.me/linux-stable.git/commit/?id=7b416b9dac6ede26d4ca0c1a88b448b543623ff3'/>
<id>7b416b9dac6ede26d4ca0c1a88b448b543623ff3</id>
<content type='text'>
commit 7dbd8f4cabd96db5a50513de9d83a8105a5ffc81 upstream.

A recent change to the synchronization in dummy-hcd was incorrect.
The issue was that dummy_udc_stop() contained no locking and therefore
could race with various gadget driver callbacks, and the fix was to
add locking and issue the callbacks with the private spinlock held.

UDC drivers aren't supposed to do this.  Gadget driver callback
routines are allowed to invoke functions in the UDC driver, and these
functions will generally try to acquire the private spinlock.  This
would deadlock the driver.

The correct solution is to drop the spinlock before issuing callbacks,
and avoid races by emulating the synchronize_irq() call that all real
UDC drivers must perform in their -&gt;udc_stop() routines after
disabling interrupts.  This involves adding a flag to dummy-hcd's
private structure to keep track of whether interrupts are supposed to
be enabled, and adding a counter to keep track of ongoing callbacks so
that dummy_udc_stop() can wait for them all to finish.

A real UDC driver won't receive disconnect, reset, suspend, resume, or
setup events once it has disabled interrupts.  dummy-hcd will receive
them but won't try to issue any gadget driver callbacks, which should
be just as good.

Signed-off-by: Alan Stern &lt;stern@rowland.harvard.edu&gt;
Fixes: f16443a034c7 ("USB: gadgetfs, dummy-hcd, net2280: fix locking for callbacks")
Signed-off-by: Felipe Balbi &lt;felipe.balbi@linux.intel.com&gt;
[bwh: Backported to 3.2: adjust filename, context]
Signed-off-by: Ben Hutchings &lt;ben@decadent.org.uk&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
commit 7dbd8f4cabd96db5a50513de9d83a8105a5ffc81 upstream.

A recent change to the synchronization in dummy-hcd was incorrect.
The issue was that dummy_udc_stop() contained no locking and therefore
could race with various gadget driver callbacks, and the fix was to
add locking and issue the callbacks with the private spinlock held.

UDC drivers aren't supposed to do this.  Gadget driver callback
routines are allowed to invoke functions in the UDC driver, and these
functions will generally try to acquire the private spinlock.  This
would deadlock the driver.

The correct solution is to drop the spinlock before issuing callbacks,
and avoid races by emulating the synchronize_irq() call that all real
UDC drivers must perform in their -&gt;udc_stop() routines after
disabling interrupts.  This involves adding a flag to dummy-hcd's
private structure to keep track of whether interrupts are supposed to
be enabled, and adding a counter to keep track of ongoing callbacks so
that dummy_udc_stop() can wait for them all to finish.

A real UDC driver won't receive disconnect, reset, suspend, resume, or
setup events once it has disabled interrupts.  dummy-hcd will receive
them but won't try to issue any gadget driver callbacks, which should
be just as good.

Signed-off-by: Alan Stern &lt;stern@rowland.harvard.edu&gt;
Fixes: f16443a034c7 ("USB: gadgetfs, dummy-hcd, net2280: fix locking for callbacks")
Signed-off-by: Felipe Balbi &lt;felipe.balbi@linux.intel.com&gt;
[bwh: Backported to 3.2: adjust filename, context]
Signed-off-by: Ben Hutchings &lt;ben@decadent.org.uk&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>USB: gadgetfs, dummy-hcd, net2280: fix locking for callbacks</title>
<updated>2018-01-01T20:50:53+00:00</updated>
<author>
<name>Alan Stern</name>
<email>stern@rowland.harvard.edu</email>
</author>
<published>2017-06-13T19:23:42+00:00</published>
<link rel='alternate' type='text/html' href='https://git.tavy.me/linux-stable.git/commit/?id=4d9648315e42c1b45ed235a70cb94df141eed81c'/>
<id>4d9648315e42c1b45ed235a70cb94df141eed81c</id>
<content type='text'>
commit f16443a034c7aa359ddf6f0f9bc40d01ca31faea upstream.

Using the syzkaller kernel fuzzer, Andrey Konovalov generated the
following error in gadgetfs:

&gt; BUG: KASAN: use-after-free in __lock_acquire+0x3069/0x3690
&gt; kernel/locking/lockdep.c:3246
&gt; Read of size 8 at addr ffff88003a2bdaf8 by task kworker/3:1/903
&gt;
&gt; CPU: 3 PID: 903 Comm: kworker/3:1 Not tainted 4.12.0-rc4+ #35
&gt; Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011
&gt; Workqueue: usb_hub_wq hub_event
&gt; Call Trace:
&gt;  __dump_stack lib/dump_stack.c:16 [inline]
&gt;  dump_stack+0x292/0x395 lib/dump_stack.c:52
&gt;  print_address_description+0x78/0x280 mm/kasan/report.c:252
&gt;  kasan_report_error mm/kasan/report.c:351 [inline]
&gt;  kasan_report+0x230/0x340 mm/kasan/report.c:408
&gt;  __asan_report_load8_noabort+0x19/0x20 mm/kasan/report.c:429
&gt;  __lock_acquire+0x3069/0x3690 kernel/locking/lockdep.c:3246
&gt;  lock_acquire+0x22d/0x560 kernel/locking/lockdep.c:3855
&gt;  __raw_spin_lock include/linux/spinlock_api_smp.h:142 [inline]
&gt;  _raw_spin_lock+0x2f/0x40 kernel/locking/spinlock.c:151
&gt;  spin_lock include/linux/spinlock.h:299 [inline]
&gt;  gadgetfs_suspend+0x89/0x130 drivers/usb/gadget/legacy/inode.c:1682
&gt;  set_link_state+0x88e/0xae0 drivers/usb/gadget/udc/dummy_hcd.c:455
&gt;  dummy_hub_control+0xd7e/0x1fb0 drivers/usb/gadget/udc/dummy_hcd.c:2074
&gt;  rh_call_control drivers/usb/core/hcd.c:689 [inline]
&gt;  rh_urb_enqueue drivers/usb/core/hcd.c:846 [inline]
&gt;  usb_hcd_submit_urb+0x92f/0x20b0 drivers/usb/core/hcd.c:1650
&gt;  usb_submit_urb+0x8b2/0x12c0 drivers/usb/core/urb.c:542
&gt;  usb_start_wait_urb+0x148/0x5b0 drivers/usb/core/message.c:56
&gt;  usb_internal_control_msg drivers/usb/core/message.c:100 [inline]
&gt;  usb_control_msg+0x341/0x4d0 drivers/usb/core/message.c:151
&gt;  usb_clear_port_feature+0x74/0xa0 drivers/usb/core/hub.c:412
&gt;  hub_port_disable+0x123/0x510 drivers/usb/core/hub.c:4177
&gt;  hub_port_init+0x1ed/0x2940 drivers/usb/core/hub.c:4648
&gt;  hub_port_connect drivers/usb/core/hub.c:4826 [inline]
&gt;  hub_port_connect_change drivers/usb/core/hub.c:4999 [inline]
&gt;  port_event drivers/usb/core/hub.c:5105 [inline]
&gt;  hub_event+0x1ae1/0x3d40 drivers/usb/core/hub.c:5185
&gt;  process_one_work+0xc08/0x1bd0 kernel/workqueue.c:2097
&gt;  process_scheduled_works kernel/workqueue.c:2157 [inline]
&gt;  worker_thread+0xb2b/0x1860 kernel/workqueue.c:2233
&gt;  kthread+0x363/0x440 kernel/kthread.c:231
&gt;  ret_from_fork+0x2a/0x40 arch/x86/entry/entry_64.S:424
&gt;
&gt; Allocated by task 9958:
&gt;  save_stack_trace+0x1b/0x20 arch/x86/kernel/stacktrace.c:59
&gt;  save_stack+0x43/0xd0 mm/kasan/kasan.c:513
&gt;  set_track mm/kasan/kasan.c:525 [inline]
&gt;  kasan_kmalloc+0xad/0xe0 mm/kasan/kasan.c:617
&gt;  kmem_cache_alloc_trace+0x87/0x280 mm/slub.c:2745
&gt;  kmalloc include/linux/slab.h:492 [inline]
&gt;  kzalloc include/linux/slab.h:665 [inline]
&gt;  dev_new drivers/usb/gadget/legacy/inode.c:170 [inline]
&gt;  gadgetfs_fill_super+0x24f/0x540 drivers/usb/gadget/legacy/inode.c:1993
&gt;  mount_single+0xf6/0x160 fs/super.c:1192
&gt;  gadgetfs_mount+0x31/0x40 drivers/usb/gadget/legacy/inode.c:2019
&gt;  mount_fs+0x9c/0x2d0 fs/super.c:1223
&gt;  vfs_kern_mount.part.25+0xcb/0x490 fs/namespace.c:976
&gt;  vfs_kern_mount fs/namespace.c:2509 [inline]
&gt;  do_new_mount fs/namespace.c:2512 [inline]
&gt;  do_mount+0x41b/0x2d90 fs/namespace.c:2834
&gt;  SYSC_mount fs/namespace.c:3050 [inline]
&gt;  SyS_mount+0xb0/0x120 fs/namespace.c:3027
&gt;  entry_SYSCALL_64_fastpath+0x1f/0xbe
&gt;
&gt; Freed by task 9960:
&gt;  save_stack_trace+0x1b/0x20 arch/x86/kernel/stacktrace.c:59
&gt;  save_stack+0x43/0xd0 mm/kasan/kasan.c:513
&gt;  set_track mm/kasan/kasan.c:525 [inline]
&gt;  kasan_slab_free+0x72/0xc0 mm/kasan/kasan.c:590
&gt;  slab_free_hook mm/slub.c:1357 [inline]
&gt;  slab_free_freelist_hook mm/slub.c:1379 [inline]
&gt;  slab_free mm/slub.c:2961 [inline]
&gt;  kfree+0xed/0x2b0 mm/slub.c:3882
&gt;  put_dev+0x124/0x160 drivers/usb/gadget/legacy/inode.c:163
&gt;  gadgetfs_kill_sb+0x33/0x60 drivers/usb/gadget/legacy/inode.c:2027
&gt;  deactivate_locked_super+0x8d/0xd0 fs/super.c:309
&gt;  deactivate_super+0x21e/0x310 fs/super.c:340
&gt;  cleanup_mnt+0xb7/0x150 fs/namespace.c:1112
&gt;  __cleanup_mnt+0x1b/0x20 fs/namespace.c:1119
&gt;  task_work_run+0x1a0/0x280 kernel/task_work.c:116
&gt;  exit_task_work include/linux/task_work.h:21 [inline]
&gt;  do_exit+0x18a8/0x2820 kernel/exit.c:878
&gt;  do_group_exit+0x14e/0x420 kernel/exit.c:982
&gt;  get_signal+0x784/0x1780 kernel/signal.c:2318
&gt;  do_signal+0xd7/0x2130 arch/x86/kernel/signal.c:808
&gt;  exit_to_usermode_loop+0x1ac/0x240 arch/x86/entry/common.c:157
&gt;  prepare_exit_to_usermode arch/x86/entry/common.c:194 [inline]
&gt;  syscall_return_slowpath+0x3ba/0x410 arch/x86/entry/common.c:263
&gt;  entry_SYSCALL_64_fastpath+0xbc/0xbe
&gt;
&gt; The buggy address belongs to the object at ffff88003a2bdae0
&gt;  which belongs to the cache kmalloc-1024 of size 1024
&gt; The buggy address is located 24 bytes inside of
&gt;  1024-byte region [ffff88003a2bdae0, ffff88003a2bdee0)
&gt; The buggy address belongs to the page:
&gt; page:ffffea0000e8ae00 count:1 mapcount:0 mapping:          (null)
&gt; index:0x0 compound_mapcount: 0
&gt; flags: 0x100000000008100(slab|head)
&gt; raw: 0100000000008100 0000000000000000 0000000000000000 0000000100170017
&gt; raw: ffffea0000ed3020 ffffea0000f5f820 ffff88003e80efc0 0000000000000000
&gt; page dumped because: kasan: bad access detected
&gt;
&gt; Memory state around the buggy address:
&gt;  ffff88003a2bd980: fb fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
&gt;  ffff88003a2bda00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
&gt; &gt;ffff88003a2bda80: fc fc fc fc fc fc fc fc fc fc fc fc fb fb fb fb
&gt;                                                                 ^
&gt;  ffff88003a2bdb00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
&gt;  ffff88003a2bdb80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
&gt; ==================================================================

What this means is that the gadgetfs_suspend() routine was trying to
access dev-&gt;lock after it had been deallocated.  The root cause is a
race in the dummy_hcd driver; the dummy_udc_stop() routine can race
with the rest of the driver because it contains no locking.  And even
when proper locking is added, it can still race with the
set_link_state() function because that function incorrectly drops the
private spinlock before invoking any gadget driver callbacks.

The result of this race, as seen above, is that set_link_state() can
invoke a callback in gadgetfs even after gadgetfs has been unbound
from dummy_hcd's UDC and its private data structures have been
deallocated.

include/linux/usb/gadget.h documents that the -&gt;reset, -&gt;disconnect,
-&gt;suspend, and -&gt;resume callbacks may be invoked in interrupt context.
In general this is necessary, to prevent races with gadget driver
removal.  This patch fixes dummy_hcd to retain the spinlock across
these calls, and it adds a spinlock acquisition to dummy_udc_stop() to
prevent the race.

The net2280 driver makes the same mistake of dropping the private
spinlock for its -&gt;disconnect and -&gt;reset callback invocations.  The
patch fixes it too.

Lastly, since gadgetfs_suspend() may be invoked in interrupt context,
it cannot assume that interrupts are enabled when it runs.  It must
use spin_lock_irqsave() instead of spin_lock_irq().  The patch fixes
that bug as well.

Signed-off-by: Alan Stern &lt;stern@rowland.harvard.edu&gt;
Reported-and-tested-by: Andrey Konovalov &lt;andreyknvl@google.com&gt;
Acked-by: Felipe Balbi &lt;felipe.balbi@linux.intel.com&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
[bwh: Backported to 3.2:
 - Drop changes in net2280
 - Adjust filenames, context]
Signed-off-by: Ben Hutchings &lt;ben@decadent.org.uk&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
commit f16443a034c7aa359ddf6f0f9bc40d01ca31faea upstream.

Using the syzkaller kernel fuzzer, Andrey Konovalov generated the
following error in gadgetfs:

&gt; BUG: KASAN: use-after-free in __lock_acquire+0x3069/0x3690
&gt; kernel/locking/lockdep.c:3246
&gt; Read of size 8 at addr ffff88003a2bdaf8 by task kworker/3:1/903
&gt;
&gt; CPU: 3 PID: 903 Comm: kworker/3:1 Not tainted 4.12.0-rc4+ #35
&gt; Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011
&gt; Workqueue: usb_hub_wq hub_event
&gt; Call Trace:
&gt;  __dump_stack lib/dump_stack.c:16 [inline]
&gt;  dump_stack+0x292/0x395 lib/dump_stack.c:52
&gt;  print_address_description+0x78/0x280 mm/kasan/report.c:252
&gt;  kasan_report_error mm/kasan/report.c:351 [inline]
&gt;  kasan_report+0x230/0x340 mm/kasan/report.c:408
&gt;  __asan_report_load8_noabort+0x19/0x20 mm/kasan/report.c:429
&gt;  __lock_acquire+0x3069/0x3690 kernel/locking/lockdep.c:3246
&gt;  lock_acquire+0x22d/0x560 kernel/locking/lockdep.c:3855
&gt;  __raw_spin_lock include/linux/spinlock_api_smp.h:142 [inline]
&gt;  _raw_spin_lock+0x2f/0x40 kernel/locking/spinlock.c:151
&gt;  spin_lock include/linux/spinlock.h:299 [inline]
&gt;  gadgetfs_suspend+0x89/0x130 drivers/usb/gadget/legacy/inode.c:1682
&gt;  set_link_state+0x88e/0xae0 drivers/usb/gadget/udc/dummy_hcd.c:455
&gt;  dummy_hub_control+0xd7e/0x1fb0 drivers/usb/gadget/udc/dummy_hcd.c:2074
&gt;  rh_call_control drivers/usb/core/hcd.c:689 [inline]
&gt;  rh_urb_enqueue drivers/usb/core/hcd.c:846 [inline]
&gt;  usb_hcd_submit_urb+0x92f/0x20b0 drivers/usb/core/hcd.c:1650
&gt;  usb_submit_urb+0x8b2/0x12c0 drivers/usb/core/urb.c:542
&gt;  usb_start_wait_urb+0x148/0x5b0 drivers/usb/core/message.c:56
&gt;  usb_internal_control_msg drivers/usb/core/message.c:100 [inline]
&gt;  usb_control_msg+0x341/0x4d0 drivers/usb/core/message.c:151
&gt;  usb_clear_port_feature+0x74/0xa0 drivers/usb/core/hub.c:412
&gt;  hub_port_disable+0x123/0x510 drivers/usb/core/hub.c:4177
&gt;  hub_port_init+0x1ed/0x2940 drivers/usb/core/hub.c:4648
&gt;  hub_port_connect drivers/usb/core/hub.c:4826 [inline]
&gt;  hub_port_connect_change drivers/usb/core/hub.c:4999 [inline]
&gt;  port_event drivers/usb/core/hub.c:5105 [inline]
&gt;  hub_event+0x1ae1/0x3d40 drivers/usb/core/hub.c:5185
&gt;  process_one_work+0xc08/0x1bd0 kernel/workqueue.c:2097
&gt;  process_scheduled_works kernel/workqueue.c:2157 [inline]
&gt;  worker_thread+0xb2b/0x1860 kernel/workqueue.c:2233
&gt;  kthread+0x363/0x440 kernel/kthread.c:231
&gt;  ret_from_fork+0x2a/0x40 arch/x86/entry/entry_64.S:424
&gt;
&gt; Allocated by task 9958:
&gt;  save_stack_trace+0x1b/0x20 arch/x86/kernel/stacktrace.c:59
&gt;  save_stack+0x43/0xd0 mm/kasan/kasan.c:513
&gt;  set_track mm/kasan/kasan.c:525 [inline]
&gt;  kasan_kmalloc+0xad/0xe0 mm/kasan/kasan.c:617
&gt;  kmem_cache_alloc_trace+0x87/0x280 mm/slub.c:2745
&gt;  kmalloc include/linux/slab.h:492 [inline]
&gt;  kzalloc include/linux/slab.h:665 [inline]
&gt;  dev_new drivers/usb/gadget/legacy/inode.c:170 [inline]
&gt;  gadgetfs_fill_super+0x24f/0x540 drivers/usb/gadget/legacy/inode.c:1993
&gt;  mount_single+0xf6/0x160 fs/super.c:1192
&gt;  gadgetfs_mount+0x31/0x40 drivers/usb/gadget/legacy/inode.c:2019
&gt;  mount_fs+0x9c/0x2d0 fs/super.c:1223
&gt;  vfs_kern_mount.part.25+0xcb/0x490 fs/namespace.c:976
&gt;  vfs_kern_mount fs/namespace.c:2509 [inline]
&gt;  do_new_mount fs/namespace.c:2512 [inline]
&gt;  do_mount+0x41b/0x2d90 fs/namespace.c:2834
&gt;  SYSC_mount fs/namespace.c:3050 [inline]
&gt;  SyS_mount+0xb0/0x120 fs/namespace.c:3027
&gt;  entry_SYSCALL_64_fastpath+0x1f/0xbe
&gt;
&gt; Freed by task 9960:
&gt;  save_stack_trace+0x1b/0x20 arch/x86/kernel/stacktrace.c:59
&gt;  save_stack+0x43/0xd0 mm/kasan/kasan.c:513
&gt;  set_track mm/kasan/kasan.c:525 [inline]
&gt;  kasan_slab_free+0x72/0xc0 mm/kasan/kasan.c:590
&gt;  slab_free_hook mm/slub.c:1357 [inline]
&gt;  slab_free_freelist_hook mm/slub.c:1379 [inline]
&gt;  slab_free mm/slub.c:2961 [inline]
&gt;  kfree+0xed/0x2b0 mm/slub.c:3882
&gt;  put_dev+0x124/0x160 drivers/usb/gadget/legacy/inode.c:163
&gt;  gadgetfs_kill_sb+0x33/0x60 drivers/usb/gadget/legacy/inode.c:2027
&gt;  deactivate_locked_super+0x8d/0xd0 fs/super.c:309
&gt;  deactivate_super+0x21e/0x310 fs/super.c:340
&gt;  cleanup_mnt+0xb7/0x150 fs/namespace.c:1112
&gt;  __cleanup_mnt+0x1b/0x20 fs/namespace.c:1119
&gt;  task_work_run+0x1a0/0x280 kernel/task_work.c:116
&gt;  exit_task_work include/linux/task_work.h:21 [inline]
&gt;  do_exit+0x18a8/0x2820 kernel/exit.c:878
&gt;  do_group_exit+0x14e/0x420 kernel/exit.c:982
&gt;  get_signal+0x784/0x1780 kernel/signal.c:2318
&gt;  do_signal+0xd7/0x2130 arch/x86/kernel/signal.c:808
&gt;  exit_to_usermode_loop+0x1ac/0x240 arch/x86/entry/common.c:157
&gt;  prepare_exit_to_usermode arch/x86/entry/common.c:194 [inline]
&gt;  syscall_return_slowpath+0x3ba/0x410 arch/x86/entry/common.c:263
&gt;  entry_SYSCALL_64_fastpath+0xbc/0xbe
&gt;
&gt; The buggy address belongs to the object at ffff88003a2bdae0
&gt;  which belongs to the cache kmalloc-1024 of size 1024
&gt; The buggy address is located 24 bytes inside of
&gt;  1024-byte region [ffff88003a2bdae0, ffff88003a2bdee0)
&gt; The buggy address belongs to the page:
&gt; page:ffffea0000e8ae00 count:1 mapcount:0 mapping:          (null)
&gt; index:0x0 compound_mapcount: 0
&gt; flags: 0x100000000008100(slab|head)
&gt; raw: 0100000000008100 0000000000000000 0000000000000000 0000000100170017
&gt; raw: ffffea0000ed3020 ffffea0000f5f820 ffff88003e80efc0 0000000000000000
&gt; page dumped because: kasan: bad access detected
&gt;
&gt; Memory state around the buggy address:
&gt;  ffff88003a2bd980: fb fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
&gt;  ffff88003a2bda00: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
&gt; &gt;ffff88003a2bda80: fc fc fc fc fc fc fc fc fc fc fc fc fb fb fb fb
&gt;                                                                 ^
&gt;  ffff88003a2bdb00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
&gt;  ffff88003a2bdb80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
&gt; ==================================================================

What this means is that the gadgetfs_suspend() routine was trying to
access dev-&gt;lock after it had been deallocated.  The root cause is a
race in the dummy_hcd driver; the dummy_udc_stop() routine can race
with the rest of the driver because it contains no locking.  And even
when proper locking is added, it can still race with the
set_link_state() function because that function incorrectly drops the
private spinlock before invoking any gadget driver callbacks.

The result of this race, as seen above, is that set_link_state() can
invoke a callback in gadgetfs even after gadgetfs has been unbound
from dummy_hcd's UDC and its private data structures have been
deallocated.

include/linux/usb/gadget.h documents that the -&gt;reset, -&gt;disconnect,
-&gt;suspend, and -&gt;resume callbacks may be invoked in interrupt context.
In general this is necessary, to prevent races with gadget driver
removal.  This patch fixes dummy_hcd to retain the spinlock across
these calls, and it adds a spinlock acquisition to dummy_udc_stop() to
prevent the race.

The net2280 driver makes the same mistake of dropping the private
spinlock for its -&gt;disconnect and -&gt;reset callback invocations.  The
patch fixes it too.

Lastly, since gadgetfs_suspend() may be invoked in interrupt context,
it cannot assume that interrupts are enabled when it runs.  It must
use spin_lock_irqsave() instead of spin_lock_irq().  The patch fixes
that bug as well.

Signed-off-by: Alan Stern &lt;stern@rowland.harvard.edu&gt;
Reported-and-tested-by: Andrey Konovalov &lt;andreyknvl@google.com&gt;
Acked-by: Felipe Balbi &lt;felipe.balbi@linux.intel.com&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
[bwh: Backported to 3.2:
 - Drop changes in net2280
 - Adjust filenames, context]
Signed-off-by: Ben Hutchings &lt;ben@decadent.org.uk&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>USB: dummy-hcd: fix infinite-loop resubmission bug</title>
<updated>2018-01-01T20:50:52+00:00</updated>
<author>
<name>Alan Stern</name>
<email>stern@rowland.harvard.edu</email>
</author>
<published>2017-09-26T19:15:40+00:00</published>
<link rel='alternate' type='text/html' href='https://git.tavy.me/linux-stable.git/commit/?id=f0318387bb5088c1f787dff79f4dd7508f25e147'/>
<id>f0318387bb5088c1f787dff79f4dd7508f25e147</id>
<content type='text'>
commit 0173a68bfb0ad1c72a6ee39cc485aa2c97540b98 upstream.

The dummy-hcd HCD/UDC emulator tries not to do too much work during
each timer interrupt.  But it doesn't try very hard; currently all
it does is limit the total amount of bulk data transferred.  Other
transfer types aren't limited, and URBs that transfer no data (because
of an error, perhaps) don't count toward the limit, even though on a
real USB bus they would consume at least a minimum overhead.

This means it's possible to get the driver stuck in an infinite loop,
for example, if the host class driver resubmits an URB every time it
completes (which is common for interrupt URBs).  Each time the URB is
resubmitted it gets added to the end of the pending-URBs list, and
dummy-hcd doesn't stop until that list is empty.  Andrey Konovalov was
able to trigger this failure mode using the syzkaller fuzzer.

This patch fixes the infinite-loop problem by restricting the URBs
handled during each timer interrupt to those that were already on the
pending list when the interrupt routine started.  Newly added URBs
won't be processed until the next timer interrupt.  The problem of
properly accounting for non-bulk bandwidth (as well as packet and
transaction overhead) is not addressed here.

Signed-off-by: Alan Stern &lt;stern@rowland.harvard.edu&gt;
Reported-by: Andrey Konovalov &lt;andreyknvl@google.com&gt;
Tested-by: Andrey Konovalov &lt;andreyknvl@google.com&gt;
Signed-off-by: Felipe Balbi &lt;felipe.balbi@linux.intel.com&gt;
[bwh: Backported to 3.2: adjust context]
Signed-off-by: Ben Hutchings &lt;ben@decadent.org.uk&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
commit 0173a68bfb0ad1c72a6ee39cc485aa2c97540b98 upstream.

The dummy-hcd HCD/UDC emulator tries not to do too much work during
each timer interrupt.  But it doesn't try very hard; currently all
it does is limit the total amount of bulk data transferred.  Other
transfer types aren't limited, and URBs that transfer no data (because
of an error, perhaps) don't count toward the limit, even though on a
real USB bus they would consume at least a minimum overhead.

This means it's possible to get the driver stuck in an infinite loop,
for example, if the host class driver resubmits an URB every time it
completes (which is common for interrupt URBs).  Each time the URB is
resubmitted it gets added to the end of the pending-URBs list, and
dummy-hcd doesn't stop until that list is empty.  Andrey Konovalov was
able to trigger this failure mode using the syzkaller fuzzer.

This patch fixes the infinite-loop problem by restricting the URBs
handled during each timer interrupt to those that were already on the
pending list when the interrupt routine started.  Newly added URBs
won't be processed until the next timer interrupt.  The problem of
properly accounting for non-bulk bandwidth (as well as packet and
transaction overhead) is not addressed here.

Signed-off-by: Alan Stern &lt;stern@rowland.harvard.edu&gt;
Reported-by: Andrey Konovalov &lt;andreyknvl@google.com&gt;
Tested-by: Andrey Konovalov &lt;andreyknvl@google.com&gt;
Signed-off-by: Felipe Balbi &lt;felipe.balbi@linux.intel.com&gt;
[bwh: Backported to 3.2: adjust context]
Signed-off-by: Ben Hutchings &lt;ben@decadent.org.uk&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>USB: gadgetfs: Fix crash caused by inadequate synchronization</title>
<updated>2018-01-01T20:50:51+00:00</updated>
<author>
<name>Alan Stern</name>
<email>stern@rowland.harvard.edu</email>
</author>
<published>2017-09-21T17:23:58+00:00</published>
<link rel='alternate' type='text/html' href='https://git.tavy.me/linux-stable.git/commit/?id=c1f10c6b89f3b32eb701395e64244d1c872ae586'/>
<id>c1f10c6b89f3b32eb701395e64244d1c872ae586</id>
<content type='text'>
commit 520b72fc64debf8a86c3853b8e486aa5982188f0 upstream.

The gadgetfs driver (drivers/usb/gadget/legacy/inode.c) was written
before the UDC and composite frameworks were adopted; it is a legacy
driver.  As such, it expects that once bound to a UDC controller, it
will not be unbound until it unregisters itself.

However, the UDC framework does unbind function drivers while they are
still registered.  When this happens, it can cause the gadgetfs driver
to misbehave or crash.  For example, userspace can cause a crash by
opening the device file and doing an ioctl call before setting up a
configuration (found by Andrey Konovalov using the syzkaller fuzzer).

This patch adds checks and synchronization to prevent these bad
behaviors.  It adds a udc_usage counter that the driver increments at
times when it is using a gadget interface without holding the private
spinlock.  The unbind routine waits for this counter to go to 0 before
returning, thereby ensuring that the UDC is no longer in use.

The patch also adds a check in the dev_ioctl() routine to make sure
the driver is bound to a UDC before dereferencing the gadget pointer,
and it makes destroy_ep_files() synchronize with the endpoint I/O
routines, to prevent the user from accessing an endpoint data
structure after it has been removed.

Signed-off-by: Alan Stern &lt;stern@rowland.harvard.edu&gt;
Reported-by: Andrey Konovalov &lt;andreyknvl@google.com&gt;
Tested-by: Andrey Konovalov &lt;andreyknvl@google.com&gt;
Acked-by: Felipe Balbi &lt;felipe.balbi@linux.intel.com&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
[bwh: Backported to 3.2:
 - Expand locked section in ep0_write() to match upstream
 - Adjust filename, context]
Signed-off-by: Ben Hutchings &lt;ben@decadent.org.uk&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
commit 520b72fc64debf8a86c3853b8e486aa5982188f0 upstream.

The gadgetfs driver (drivers/usb/gadget/legacy/inode.c) was written
before the UDC and composite frameworks were adopted; it is a legacy
driver.  As such, it expects that once bound to a UDC controller, it
will not be unbound until it unregisters itself.

However, the UDC framework does unbind function drivers while they are
still registered.  When this happens, it can cause the gadgetfs driver
to misbehave or crash.  For example, userspace can cause a crash by
opening the device file and doing an ioctl call before setting up a
configuration (found by Andrey Konovalov using the syzkaller fuzzer).

This patch adds checks and synchronization to prevent these bad
behaviors.  It adds a udc_usage counter that the driver increments at
times when it is using a gadget interface without holding the private
spinlock.  The unbind routine waits for this counter to go to 0 before
returning, thereby ensuring that the UDC is no longer in use.

The patch also adds a check in the dev_ioctl() routine to make sure
the driver is bound to a UDC before dereferencing the gadget pointer,
and it makes destroy_ep_files() synchronize with the endpoint I/O
routines, to prevent the user from accessing an endpoint data
structure after it has been removed.

Signed-off-by: Alan Stern &lt;stern@rowland.harvard.edu&gt;
Reported-by: Andrey Konovalov &lt;andreyknvl@google.com&gt;
Tested-by: Andrey Konovalov &lt;andreyknvl@google.com&gt;
Acked-by: Felipe Balbi &lt;felipe.balbi@linux.intel.com&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
[bwh: Backported to 3.2:
 - Expand locked section in ep0_write() to match upstream
 - Adjust filename, context]
Signed-off-by: Ben Hutchings &lt;ben@decadent.org.uk&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>USB: gadgetfs: fix copy_to_user while holding spinlock</title>
<updated>2018-01-01T20:50:51+00:00</updated>
<author>
<name>Alan Stern</name>
<email>stern@rowland.harvard.edu</email>
</author>
<published>2017-09-21T20:12:01+00:00</published>
<link rel='alternate' type='text/html' href='https://git.tavy.me/linux-stable.git/commit/?id=a776d55c9e394166bbb509b214f09ebbc6baf62a'/>
<id>a776d55c9e394166bbb509b214f09ebbc6baf62a</id>
<content type='text'>
commit 6e76c01e71551cb221c1f3deacb9dcd9a7346784 upstream.

The gadgetfs driver as a long-outstanding FIXME, regarding a call of
copy_to_user() made while holding a spinlock.  This patch fixes the
issue by dropping the spinlock and using the dev-&gt;udc_usage mechanism
introduced by another recent patch to guard against status changes
while the lock isn't held.

Signed-off-by: Alan Stern &lt;stern@rowland.harvard.edu&gt;
Reported-by: Andrey Konovalov &lt;andreyknvl@google.com&gt;
Acked-by: Felipe Balbi &lt;felipe.balbi@linux.intel.com&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
[bwh: Backported to 3.2: adjust filename]
Signed-off-by: Ben Hutchings &lt;ben@decadent.org.uk&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
commit 6e76c01e71551cb221c1f3deacb9dcd9a7346784 upstream.

The gadgetfs driver as a long-outstanding FIXME, regarding a call of
copy_to_user() made while holding a spinlock.  This patch fixes the
issue by dropping the spinlock and using the dev-&gt;udc_usage mechanism
introduced by another recent patch to guard against status changes
while the lock isn't held.

Signed-off-by: Alan Stern &lt;stern@rowland.harvard.edu&gt;
Reported-by: Andrey Konovalov &lt;andreyknvl@google.com&gt;
Acked-by: Felipe Balbi &lt;felipe.balbi@linux.intel.com&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
[bwh: Backported to 3.2: adjust filename]
Signed-off-by: Ben Hutchings &lt;ben@decadent.org.uk&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>usb: gadget: fix spinlock dead lock in gadgetfs</title>
<updated>2018-01-01T20:50:51+00:00</updated>
<author>
<name>Bin Liu</name>
<email>b-liu@ti.com</email>
</author>
<published>2016-05-26T16:43:45+00:00</published>
<link rel='alternate' type='text/html' href='https://git.tavy.me/linux-stable.git/commit/?id=a91864a3a37edaaad870391fa798f1d974a7a9bf'/>
<id>a91864a3a37edaaad870391fa798f1d974a7a9bf</id>
<content type='text'>
commit d246dcb2331c5783743720e6510892eb1d2801d9 upstream.

[   40.467381] =============================================
[   40.473013] [ INFO: possible recursive locking detected ]
[   40.478651] 4.6.0-08691-g7f3db9a #37 Not tainted
[   40.483466] ---------------------------------------------
[   40.489098] usb/733 is trying to acquire lock:
[   40.493734]  (&amp;(&amp;dev-&gt;lock)-&gt;rlock){-.....}, at: [&lt;bf129288&gt;] ep0_complete+0x18/0xdc [gadgetfs]
[   40.502882]
[   40.502882] but task is already holding lock:
[   40.508967]  (&amp;(&amp;dev-&gt;lock)-&gt;rlock){-.....}, at: [&lt;bf12a420&gt;] ep0_read+0x20/0x5e0 [gadgetfs]
[   40.517811]
[   40.517811] other info that might help us debug this:
[   40.524623]  Possible unsafe locking scenario:
[   40.524623]
[   40.530798]        CPU0
[   40.533346]        ----
[   40.535894]   lock(&amp;(&amp;dev-&gt;lock)-&gt;rlock);
[   40.540088]   lock(&amp;(&amp;dev-&gt;lock)-&gt;rlock);
[   40.544284]
[   40.544284]  *** DEADLOCK ***
[   40.544284]
[   40.550461]  May be due to missing lock nesting notation
[   40.550461]
[   40.557544] 2 locks held by usb/733:
[   40.561271]  #0:  (&amp;f-&gt;f_pos_lock){+.+.+.}, at: [&lt;c02a6114&gt;] __fdget_pos+0x40/0x48
[   40.569219]  #1:  (&amp;(&amp;dev-&gt;lock)-&gt;rlock){-.....}, at: [&lt;bf12a420&gt;] ep0_read+0x20/0x5e0 [gadgetfs]
[   40.578523]
[   40.578523] stack backtrace:
[   40.583075] CPU: 0 PID: 733 Comm: usb Not tainted 4.6.0-08691-g7f3db9a #37
[   40.590246] Hardware name: Generic AM33XX (Flattened Device Tree)
[   40.596625] [&lt;c010ffbc&gt;] (unwind_backtrace) from [&lt;c010c1bc&gt;] (show_stack+0x10/0x14)
[   40.604718] [&lt;c010c1bc&gt;] (show_stack) from [&lt;c04207fc&gt;] (dump_stack+0xb0/0xe4)
[   40.612267] [&lt;c04207fc&gt;] (dump_stack) from [&lt;c01886ec&gt;] (__lock_acquire+0xf68/0x1994)
[   40.620440] [&lt;c01886ec&gt;] (__lock_acquire) from [&lt;c0189528&gt;] (lock_acquire+0xd8/0x238)
[   40.628621] [&lt;c0189528&gt;] (lock_acquire) from [&lt;c06ad6b4&gt;] (_raw_spin_lock_irqsave+0x38/0x4c)
[   40.637440] [&lt;c06ad6b4&gt;] (_raw_spin_lock_irqsave) from [&lt;bf129288&gt;] (ep0_complete+0x18/0xdc [gadgetfs])
[   40.647339] [&lt;bf129288&gt;] (ep0_complete [gadgetfs]) from [&lt;bf10a728&gt;] (musb_g_giveback+0x118/0x1b0 [musb_hdrc])
[   40.657842] [&lt;bf10a728&gt;] (musb_g_giveback [musb_hdrc]) from [&lt;bf108768&gt;] (musb_g_ep0_queue+0x16c/0x188 [musb_hdrc])
[   40.668772] [&lt;bf108768&gt;] (musb_g_ep0_queue [musb_hdrc]) from [&lt;bf12a944&gt;] (ep0_read+0x544/0x5e0 [gadgetfs])
[   40.678963] [&lt;bf12a944&gt;] (ep0_read [gadgetfs]) from [&lt;c0284470&gt;] (__vfs_read+0x20/0x110)
[   40.687414] [&lt;c0284470&gt;] (__vfs_read) from [&lt;c0285324&gt;] (vfs_read+0x88/0x114)
[   40.694864] [&lt;c0285324&gt;] (vfs_read) from [&lt;c0286150&gt;] (SyS_read+0x44/0x9c)
[   40.702051] [&lt;c0286150&gt;] (SyS_read) from [&lt;c0107820&gt;] (ret_fast_syscall+0x0/0x1c)

This is caused by the spinlock bug in ep0_read().
Fix the two other deadlock sources in gadgetfs_setup() too.

Signed-off-by: Bin Liu &lt;b-liu@ti.com&gt;
Signed-off-by: Felipe Balbi &lt;felipe.balbi@linux.intel.com&gt;
Signed-off-by: Ben Hutchings &lt;ben@decadent.org.uk&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
commit d246dcb2331c5783743720e6510892eb1d2801d9 upstream.

[   40.467381] =============================================
[   40.473013] [ INFO: possible recursive locking detected ]
[   40.478651] 4.6.0-08691-g7f3db9a #37 Not tainted
[   40.483466] ---------------------------------------------
[   40.489098] usb/733 is trying to acquire lock:
[   40.493734]  (&amp;(&amp;dev-&gt;lock)-&gt;rlock){-.....}, at: [&lt;bf129288&gt;] ep0_complete+0x18/0xdc [gadgetfs]
[   40.502882]
[   40.502882] but task is already holding lock:
[   40.508967]  (&amp;(&amp;dev-&gt;lock)-&gt;rlock){-.....}, at: [&lt;bf12a420&gt;] ep0_read+0x20/0x5e0 [gadgetfs]
[   40.517811]
[   40.517811] other info that might help us debug this:
[   40.524623]  Possible unsafe locking scenario:
[   40.524623]
[   40.530798]        CPU0
[   40.533346]        ----
[   40.535894]   lock(&amp;(&amp;dev-&gt;lock)-&gt;rlock);
[   40.540088]   lock(&amp;(&amp;dev-&gt;lock)-&gt;rlock);
[   40.544284]
[   40.544284]  *** DEADLOCK ***
[   40.544284]
[   40.550461]  May be due to missing lock nesting notation
[   40.550461]
[   40.557544] 2 locks held by usb/733:
[   40.561271]  #0:  (&amp;f-&gt;f_pos_lock){+.+.+.}, at: [&lt;c02a6114&gt;] __fdget_pos+0x40/0x48
[   40.569219]  #1:  (&amp;(&amp;dev-&gt;lock)-&gt;rlock){-.....}, at: [&lt;bf12a420&gt;] ep0_read+0x20/0x5e0 [gadgetfs]
[   40.578523]
[   40.578523] stack backtrace:
[   40.583075] CPU: 0 PID: 733 Comm: usb Not tainted 4.6.0-08691-g7f3db9a #37
[   40.590246] Hardware name: Generic AM33XX (Flattened Device Tree)
[   40.596625] [&lt;c010ffbc&gt;] (unwind_backtrace) from [&lt;c010c1bc&gt;] (show_stack+0x10/0x14)
[   40.604718] [&lt;c010c1bc&gt;] (show_stack) from [&lt;c04207fc&gt;] (dump_stack+0xb0/0xe4)
[   40.612267] [&lt;c04207fc&gt;] (dump_stack) from [&lt;c01886ec&gt;] (__lock_acquire+0xf68/0x1994)
[   40.620440] [&lt;c01886ec&gt;] (__lock_acquire) from [&lt;c0189528&gt;] (lock_acquire+0xd8/0x238)
[   40.628621] [&lt;c0189528&gt;] (lock_acquire) from [&lt;c06ad6b4&gt;] (_raw_spin_lock_irqsave+0x38/0x4c)
[   40.637440] [&lt;c06ad6b4&gt;] (_raw_spin_lock_irqsave) from [&lt;bf129288&gt;] (ep0_complete+0x18/0xdc [gadgetfs])
[   40.647339] [&lt;bf129288&gt;] (ep0_complete [gadgetfs]) from [&lt;bf10a728&gt;] (musb_g_giveback+0x118/0x1b0 [musb_hdrc])
[   40.657842] [&lt;bf10a728&gt;] (musb_g_giveback [musb_hdrc]) from [&lt;bf108768&gt;] (musb_g_ep0_queue+0x16c/0x188 [musb_hdrc])
[   40.668772] [&lt;bf108768&gt;] (musb_g_ep0_queue [musb_hdrc]) from [&lt;bf12a944&gt;] (ep0_read+0x544/0x5e0 [gadgetfs])
[   40.678963] [&lt;bf12a944&gt;] (ep0_read [gadgetfs]) from [&lt;c0284470&gt;] (__vfs_read+0x20/0x110)
[   40.687414] [&lt;c0284470&gt;] (__vfs_read) from [&lt;c0285324&gt;] (vfs_read+0x88/0x114)
[   40.694864] [&lt;c0285324&gt;] (vfs_read) from [&lt;c0286150&gt;] (SyS_read+0x44/0x9c)
[   40.702051] [&lt;c0286150&gt;] (SyS_read) from [&lt;c0107820&gt;] (ret_fast_syscall+0x0/0x1c)

This is caused by the spinlock bug in ep0_read().
Fix the two other deadlock sources in gadgetfs_setup() too.

Signed-off-by: Bin Liu &lt;b-liu@ti.com&gt;
Signed-off-by: Felipe Balbi &lt;felipe.balbi@linux.intel.com&gt;
Signed-off-by: Ben Hutchings &lt;ben@decadent.org.uk&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>usb: gadget: f_mass_storage: Serialize wake and sleep execution</title>
<updated>2017-09-15T17:30:49+00:00</updated>
<author>
<name>Thinh Nguyen</name>
<email>Thinh.Nguyen@synopsys.com</email>
</author>
<published>2017-05-12T00:26:48+00:00</published>
<link rel='alternate' type='text/html' href='https://git.tavy.me/linux-stable.git/commit/?id=6cd0bb515052f9ede3846c0dc6162090a45bb6d6'/>
<id>6cd0bb515052f9ede3846c0dc6162090a45bb6d6</id>
<content type='text'>
commit dc9217b69dd6089dcfeb86ed4b3c671504326087 upstream.

f_mass_storage has a memorry barrier issue with the sleep and wake
functions that can cause a deadlock. This results in intermittent hangs
during MSC file transfer. The host will reset the device after receiving
no response to resume the transfer. This issue is seen when dwc3 is
processing 2 transfer-in-progress events at the same time, invoking
completion handlers for CSW and CBW. Also this issue occurs depending on
the system timing and latency.

To increase the chance to hit this issue, you can force dwc3 driver to
wait and process those 2 events at once by adding a small delay (~100us)
in dwc3_check_event_buf() whenever the request is for CSW and read the
event count again. Avoid debugging with printk and ftrace as extra
delays and memory barrier will mask this issue.

Scenario which can lead to failure:
-----------------------------------
1) The main thread sleeps and waits for the next command in
   get_next_command().
2) bulk_in_complete() wakes up main thread for CSW.
3) bulk_out_complete() tries to wake up the running main thread for CBW.
4) thread_wakeup_needed is not loaded with correct value in
   sleep_thread().
5) Main thread goes to sleep again.

The pattern is shown below. Note the 2 critical variables.
 * common-&gt;thread_wakeup_needed
 * bh-&gt;state

	CPU 0 (sleep_thread)		CPU 1 (wakeup_thread)
	==============================  ===============================

					bh-&gt;state = BH_STATE_FULL;
					smp_wmb();
	thread_wakeup_needed = 0;	thread_wakeup_needed = 1;
	smp_rmb();
	if (bh-&gt;state != BH_STATE_FULL)
		sleep again ...

As pointed out by Alan Stern, this is an R-pattern issue. The issue can
be seen when there are two wakeups in quick succession. The
thread_wakeup_needed can be overwritten in sleep_thread, and the read of
the bh-&gt;state maybe reordered before the write to thread_wakeup_needed.

This patch applies full memory barrier smp_mb() in both sleep_thread()
and wakeup_thread() to ensure the order which the thread_wakeup_needed
and bh-&gt;state are written and loaded.

However, a better solution in the future would be to use wait_queue
method that takes care of managing memory barrier between waker and
waiter.

Acked-by: Alan Stern &lt;stern@rowland.harvard.edu&gt;
Signed-off-by: Thinh Nguyen &lt;thinhn@synopsys.com&gt;
Signed-off-by: Felipe Balbi &lt;felipe.balbi@linux.intel.com&gt;
[bwh: Backported to 3.2: adjust filename]
Signed-off-by: Ben Hutchings &lt;ben@decadent.org.uk&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
commit dc9217b69dd6089dcfeb86ed4b3c671504326087 upstream.

f_mass_storage has a memorry barrier issue with the sleep and wake
functions that can cause a deadlock. This results in intermittent hangs
during MSC file transfer. The host will reset the device after receiving
no response to resume the transfer. This issue is seen when dwc3 is
processing 2 transfer-in-progress events at the same time, invoking
completion handlers for CSW and CBW. Also this issue occurs depending on
the system timing and latency.

To increase the chance to hit this issue, you can force dwc3 driver to
wait and process those 2 events at once by adding a small delay (~100us)
in dwc3_check_event_buf() whenever the request is for CSW and read the
event count again. Avoid debugging with printk and ftrace as extra
delays and memory barrier will mask this issue.

Scenario which can lead to failure:
-----------------------------------
1) The main thread sleeps and waits for the next command in
   get_next_command().
2) bulk_in_complete() wakes up main thread for CSW.
3) bulk_out_complete() tries to wake up the running main thread for CBW.
4) thread_wakeup_needed is not loaded with correct value in
   sleep_thread().
5) Main thread goes to sleep again.

The pattern is shown below. Note the 2 critical variables.
 * common-&gt;thread_wakeup_needed
 * bh-&gt;state

	CPU 0 (sleep_thread)		CPU 1 (wakeup_thread)
	==============================  ===============================

					bh-&gt;state = BH_STATE_FULL;
					smp_wmb();
	thread_wakeup_needed = 0;	thread_wakeup_needed = 1;
	smp_rmb();
	if (bh-&gt;state != BH_STATE_FULL)
		sleep again ...

As pointed out by Alan Stern, this is an R-pattern issue. The issue can
be seen when there are two wakeups in quick succession. The
thread_wakeup_needed can be overwritten in sleep_thread, and the read of
the bh-&gt;state maybe reordered before the write to thread_wakeup_needed.

This patch applies full memory barrier smp_mb() in both sleep_thread()
and wakeup_thread() to ensure the order which the thread_wakeup_needed
and bh-&gt;state are written and loaded.

However, a better solution in the future would be to use wait_queue
method that takes care of managing memory barrier between waker and
waiter.

Acked-by: Alan Stern &lt;stern@rowland.harvard.edu&gt;
Signed-off-by: Thinh Nguyen &lt;thinhn@synopsys.com&gt;
Signed-off-by: Felipe Balbi &lt;felipe.balbi@linux.intel.com&gt;
[bwh: Backported to 3.2: adjust filename]
Signed-off-by: Ben Hutchings &lt;ben@decadent.org.uk&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>USB: gadget: dummy_hcd: fix hub-descriptor removable fields</title>
<updated>2017-09-15T17:30:43+00:00</updated>
<author>
<name>Johan Hovold</name>
<email>johan@kernel.org</email>
</author>
<published>2017-05-10T16:18:25+00:00</published>
<link rel='alternate' type='text/html' href='https://git.tavy.me/linux-stable.git/commit/?id=bf6cca750ecdff445b9bd56c1836ef99851aaf72'/>
<id>bf6cca750ecdff445b9bd56c1836ef99851aaf72</id>
<content type='text'>
commit d81182ce30dbd497a1e7047d7fda2af040347790 upstream.

Flag the first and only port as removable while also leaving the
remaining bits (including the reserved bit zero) unset in accordance
with the specifications:

	"Within a byte, if no port exists for a given location, the bit
	field representing the port characteristics shall be 0."

Also add a comment marking the legacy PortPwrCtrlMask field.

Fixes: 1cd8fd2887e1 ("usb: gadget: dummy_hcd: add SuperSpeed support")
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: Tatyana Brokhman &lt;tlinder@codeaurora.org&gt;
Signed-off-by: Johan Hovold &lt;johan@kernel.org&gt;
Acked-by: Alan Stern &lt;stern@rowland.harvard.edu&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
[bwh: Backported to 3.2: adjust filename]
Signed-off-by: Ben Hutchings &lt;ben@decadent.org.uk&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
commit d81182ce30dbd497a1e7047d7fda2af040347790 upstream.

Flag the first and only port as removable while also leaving the
remaining bits (including the reserved bit zero) unset in accordance
with the specifications:

	"Within a byte, if no port exists for a given location, the bit
	field representing the port characteristics shall be 0."

Also add a comment marking the legacy PortPwrCtrlMask field.

Fixes: 1cd8fd2887e1 ("usb: gadget: dummy_hcd: add SuperSpeed support")
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: Tatyana Brokhman &lt;tlinder@codeaurora.org&gt;
Signed-off-by: Johan Hovold &lt;johan@kernel.org&gt;
Acked-by: Alan Stern &lt;stern@rowland.harvard.edu&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
[bwh: Backported to 3.2: adjust filename]
Signed-off-by: Ben Hutchings &lt;ben@decadent.org.uk&gt;
</pre>
</div>
</content>
</entry>
<entry>
<title>ACM gadget: fix endianness in notifications</title>
<updated>2017-07-18T17:38:35+00:00</updated>
<author>
<name>Oliver Neukum</name>
<email>oneukum@suse.com</email>
</author>
<published>2017-03-14T11:09:56+00:00</published>
<link rel='alternate' type='text/html' href='https://git.tavy.me/linux-stable.git/commit/?id=8d2311b91361f1dedfafd81778c1826b3ea3fa3a'/>
<id>8d2311b91361f1dedfafd81778c1826b3ea3fa3a</id>
<content type='text'>
commit cdd7928df0d2efaa3270d711963773a08a4cc8ab upstream.

The gadget code exports the bitfield for serial status changes
over the wire in its internal endianness. The fix is to convert
to little endian before sending it over the wire.

Signed-off-by: Oliver Neukum &lt;oneukum@suse.com&gt;
Tested-by: 家瑋 &lt;momo1208@gmail.com&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
[bwh: Backported to 3.2: adjust filename, context]
Signed-off-by: Ben Hutchings &lt;ben@decadent.org.uk&gt;
</content>
<content type='xhtml'>
<div xmlns='http://www.w3.org/1999/xhtml'>
<pre>
commit cdd7928df0d2efaa3270d711963773a08a4cc8ab upstream.

The gadget code exports the bitfield for serial status changes
over the wire in its internal endianness. The fix is to convert
to little endian before sending it over the wire.

Signed-off-by: Oliver Neukum &lt;oneukum@suse.com&gt;
Tested-by: 家瑋 &lt;momo1208@gmail.com&gt;
Signed-off-by: Greg Kroah-Hartman &lt;gregkh@linuxfoundation.org&gt;
[bwh: Backported to 3.2: adjust filename, context]
Signed-off-by: Ben Hutchings &lt;ben@decadent.org.uk&gt;
</pre>
</div>
</content>
</entry>
</feed>
