mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Ben Hutchings <ben@decadent.org.uk>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: akpm@linux-foundation.org,
	"Douglas Anderson" <dianders@chromium.org>,
	"Alan Stern" <stern@rowland.harvard.edu>,
	"Greg Kroah-Hartman" <gregkh@linuxfoundation.org>
Subject: [PATCH 3.2 22/61] USB: core: Avoid race of async_completed() w/ usbdev_release()
Date: Wed, 22 Nov 2017 02:11:06 +0000	[thread overview]
Message-ID: <lsq.1511316666.955920608@decadent.org.uk> (raw)
In-Reply-To: <lsq.1511316665.221837797@decadent.org.uk>

3.2.96-rc1 review patch.  If anyone has any objections, please let me know.

------------------

From: Douglas Anderson <dianders@chromium.org>

commit ed62ca2f4f51c17841ea39d98c0c409cb53a3e10 upstream.

While running reboot tests w/ a specific set of USB devices (and
slub_debug enabled), I found that once every few hours my device would
be crashed with a stack that looked like this:

[   14.012445] BUG: spinlock bad magic on CPU#0, modprobe/2091
[   14.012460]  lock: 0xffffffc0cb055978, .magic: ffffffc0, .owner: cryption contexts: %lu/%lu
[   14.012460] /1025536097, .owner_cpu: 0
[   14.012466] CPU: 0 PID: 2091 Comm: modprobe Not tainted 4.4.79 #352
[   14.012468] Hardware name: Google Kevin (DT)
[   14.012471] Call trace:
[   14.012483] [<....>] dump_backtrace+0x0/0x160
[   14.012487] [<....>] show_stack+0x20/0x28
[   14.012494] [<....>] dump_stack+0xb4/0xf0
[   14.012500] [<....>] spin_dump+0x8c/0x98
[   14.012504] [<....>] spin_bug+0x30/0x3c
[   14.012508] [<....>] do_raw_spin_lock+0x40/0x164
[   14.012515] [<....>] _raw_spin_lock_irqsave+0x64/0x74
[   14.012521] [<....>] __wake_up+0x2c/0x60
[   14.012528] [<....>] async_completed+0x2d0/0x300
[   14.012534] [<....>] __usb_hcd_giveback_urb+0xc4/0x138
[   14.012538] [<....>] usb_hcd_giveback_urb+0x54/0xf0
[   14.012544] [<....>] xhci_irq+0x1314/0x1348
[   14.012548] [<....>] usb_hcd_irq+0x40/0x50
[   14.012553] [<....>] handle_irq_event_percpu+0x1b4/0x3f0
[   14.012556] [<....>] handle_irq_event+0x4c/0x7c
[   14.012561] [<....>] handle_fasteoi_irq+0x158/0x1c8
[   14.012564] [<....>] generic_handle_irq+0x30/0x44
[   14.012568] [<....>] __handle_domain_irq+0x90/0xbc
[   14.012572] [<....>] gic_handle_irq+0xcc/0x18c

Investigation using kgdb() found that the wait queue that was passed
into wake_up() had been freed (it was filled with slub_debug poison).

I analyzed and instrumented the code and reproduced.  My current
belief is that this is happening:

1. async_completed() is called (from IRQ).  Moves "as" onto the
   completed list.
2. On another CPU, proc_reapurbnonblock_compat() calls
   async_getcompleted().  Blocks on spinlock.
3. async_completed() releases the lock; keeps running; gets blocked
   midway through wake_up().
4. proc_reapurbnonblock_compat() => async_getcompleted() gets the
   lock; removes "as" from completed list and frees it.
5. usbdev_release() is called.  Frees "ps".
6. async_completed() finally continues running wake_up().  ...but
   wake_up() has a pointer to the freed "ps".

The instrumentation that led me to believe this was based on adding
some trace_printk() calls in a select few functions and then using
kdb's "ftdump" at crash time.  The trace follows (NOTE: in the trace
below I cheated a little bit and added a udelay(1000) in
async_completed() after releasing the spinlock because I wanted it to
trigger quicker):

<...>-2104   0d.h2 13759034us!: async_completed at start: as=ffffffc0cc638200
mtpd-2055    3.... 13759356us : async_getcompleted before spin_lock_irqsave
mtpd-2055    3d..1 13759362us : async_getcompleted after list_del_init: as=ffffffc0cc638200
mtpd-2055    3.... 13759371us+: proc_reapurbnonblock_compat: free_async(ffffffc0cc638200)
mtpd-2055    3.... 13759422us+: async_getcompleted before spin_lock_irqsave
mtpd-2055    3.... 13759479us : usbdev_release at start: ps=ffffffc0cc042080
mtpd-2055    3.... 13759487us : async_getcompleted before spin_lock_irqsave
mtpd-2055    3.... 13759497us!: usbdev_release after kfree(ps): ps=ffffffc0cc042080
<...>-2104   0d.h2 13760294us : async_completed before wake_up(): as=ffffffc0cc638200

To fix this problem we can just move the wake_up() under the ps->lock.
There should be no issues there that I'm aware of.

Signed-off-by: Douglas Anderson <dianders@chromium.org>
Acked-by: Alan Stern <stern@rowland.harvard.edu>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Ben Hutchings <ben@decadent.org.uk>
---
 drivers/usb/core/devio.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

--- a/drivers/usb/core/devio.c
+++ b/drivers/usb/core/devio.c
@@ -423,6 +423,8 @@ static void async_completed(struct urb *
 	if (as->status < 0 && as->bulk_addr && as->status != -ECONNRESET &&
 			as->status != -ENOENT)
 		cancel_bulk_urbs(ps, as->bulk_addr);
+
+	wake_up(&ps->wait);
 	spin_unlock(&ps->lock);
 
 	if (signr) {
@@ -430,8 +432,6 @@ static void async_completed(struct urb *
 		put_pid(pid);
 		put_cred(cred);
 	}
-
-	wake_up(&ps->wait);
 }
 
 static void destroy_async(struct dev_state *ps, struct list_head *list)

  parent reply	other threads:[~2017-11-22  2:55 UTC|newest]

Thread overview: 63+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-11-22  2:11 [PATCH 3.2 00/61] 3.2.96-rc1 review Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 03/61] fcntl: Don't use ambiguous SIG_POLL si_codes Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 07/61] x86/fsgsbase/64: Report FSBASE and GSBASE correctly in core dumps Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 01/61] IB/core: Fix the validations of a multicast LID in attach or detach operations Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 06/61] dlm: avoid double-free on error path in dlm_device_{register,unregister} Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 08/61] scsi: zfcp: fix queuecommand for scsi_eh commands when DIX enabled Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 05/61] PCI: shpchp: Enable bridge bus mastering if MSI is enabled Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 04/61] powerpc/mm: Fix check of multiple 16G pages from device tree Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 02/61] signal: move the "sig < SIGRTMIN" check into siginmask(sig) Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 47/61] Input: xpad - validate USB endpoint type during probe Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 44/61] Input: xpad - add a few new VID/PID combinations Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 58/61] Input: gtco - fix potential out-of-bound access Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 19/61] media: uvcvideo: Prevent heap overflow when accessing mapped controls Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 39/61] MIPS: BCM63XX: allow NULL clock for clk_get_rate Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 12/61] scsi: zfcp: fix missing trace records for early returns in TMF eh handlers Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 54/61] sctp: do not peel off an assoc from one netns to another one Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 13/61] scsi: zfcp: fix payload with full FCP_RSP IU in SCSI trace records Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 35/61] xfs: fix incorrect log_flushed on fsync Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 46/61] Input: xpad - don't depend on endpoint order Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 18/61] block: Relax a check in blk_start_queue() Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 33/61] driver core: bus: Fix a potential double free Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 42/61] ipv6: fix memory leak with multiple tables during netns destruction Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 45/61] Input: xpad - add support for Xbox One controllers Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 09/61] scsi: zfcp: add handling for FCP_RESID_OVER to the fcp ingress path Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 25/61] IB/{qib, hfi1}: Avoid flow control testing for RDMA write operation Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 31/61] powerpc/44x: Fix mask and shift to zero bug Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 14/61] scsi: zfcp: trace HBA FSF response by default on dismiss or timedout late response Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 17/61] drm/ttm: Fix accounting error when fail to get pages for pool Ben Hutchings
2017-11-22  2:11 ` Ben Hutchings [this message]
2017-11-22  2:11 ` [PATCH 3.2 34/61] ftrace: Fix selftest goto location on error Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 40/61] mm/vmstat.c: fix wrong comment Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 38/61] MIPS: AR7: allow NULL clock for clk_get_rate Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 41/61] genirq: Make sparse_irq_lock protect what it should protect Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 49/61] KVM: SVM: Add a missing 'break' statement Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 52/61] ext4: validate s_first_meta_bg at mount time Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 59/61] net: cdc_ether: fix divide by 0 on bad descriptors Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 32/61] powerpc: Correct instruction code for xxlor instruction Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 28/61] [SCSI] qla2xxx: Corrections to returned sysfs error codes Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 26/61] net/mlx4_core: Make explicit conversion to 64bit value Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 30/61] scsi: qla2xxx: Fix an integer overflow in sysfs code Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 57/61] media: imon: Fix null-ptr-deref in imon_probe Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 27/61] scsi: aacraid: Fix command send race condition Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 37/61] l2tp: pass tunnel pointer to ->session_create() Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 50/61] KVM: async_pf: Fix #DF due to inject "Page not Present" and "Page Ready" exceptions simultaneously Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 36/61] l2tp: prevent creation of sessions on terminated tunnels Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 24/61] usb: Add device quirk for Logitech HD Pro Webcam C920-C Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 55/61] USB: serial: console: fix use-after-free after failed setup Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 43/61] ipv6: fix typo in fib6_net_exit() Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 11/61] scsi: zfcp: fix passing fsf_req to SCSI trace on TMF to correlate with HBA Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 53/61] ext4: fix fencepost in s_first_meta_bg validation Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 60/61] mac80211: don't compare TKIP TX MIC key in reinstall prevention Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 56/61] [media] cx231xx-cards: fix NULL-deref on missing association descriptor Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 10/61] scsi: zfcp: fix capping of unsuccessful GPN_FT SAN response trace records Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 23/61] usb: quirks: add delay init quirk for Corsair Strafe RGB keyboard Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 15/61] scsi: mac_esp: Fix PIO transfers for MESSAGE IN phase Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 61/61] mac80211: Fix null dereference in ieee80211_key_link() Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 48/61] smsc95xx: Configure pause time to 0xffff when tx flow control enabled Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 51/61] Input: i8042 - add Gigabyte P57 to the keyboard reset table Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 20/61] media: lirc_zilog: driver only sends LIRCCODE Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 21/61] media: em28xx: calculate left volume level correctly Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 29/61] [SCSI] qla2xxx: Add mutex around optrom calls to serialize accesses Ben Hutchings
2017-11-22  2:11 ` [PATCH 3.2 16/61] cs5536: add support for IDE controller variant Ben Hutchings
2017-11-22 14:59 ` [PATCH 3.2 00/61] 3.2.96-rc1 review Guenter Roeck

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=lsq.1511316666.955920608@decadent.org.uk \
    --to=ben@decadent.org.uk \
    --cc=akpm@linux-foundation.org \
    --cc=dianders@chromium.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=stable@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

all inboxes | Powered by JetHome®