1 From 46408ea558df13b110e0866b99624384a33bdeba Mon Sep 17 00:00:00 2001
2 From: AMAN DEEP <aman.deep@samsung.com>
3 Date: Thu, 8 Feb 2018 11:55:01 +0800
4 Subject: usb: ohci: Proper handling of ed_rm_list to handle race condition between usb_kill_urb() and finish_unlinks()
6 From: AMAN DEEP <aman.deep@samsung.com>
8 commit 46408ea558df13b110e0866b99624384a33bdeba upstream.
10 There is a race condition between finish_unlinks->finish_urb() function
11 and usb_kill_urb() in ohci controller case. The finish_urb calls
12 spin_unlock(&ohci->lock) before usb_hcd_giveback_urb() function call,
13 then if during this time, usb_kill_urb is called for another endpoint,
14 then new ed will be added to ed_rm_list at beginning for unlink, and
15 ed_rm_list will point to newly added.
17 When finish_urb() is completed in finish_unlinks() and ed->td_list
18 becomes empty as in below code (in finish_unlinks() function):
20 if (list_empty(&ed->td_list)) {
23 } else if (ohci->rh_state == OHCI_RH_RUNNING) {
26 ed_schedule(ohci, ed);
29 The *last = ed->ed_next will make ed_rm_list to point to ed->ed_next
30 and previously added ed by usb_kill_urb will be left unreferenced by
31 ed_rm_list. This causes usb_kill_urb() hang forever waiting for
32 finish_unlink to remove added ed from ed_rm_list.
34 The main reason for hang in this race condtion is addition and removal
35 of ed from ed_rm_list in the beginning during usb_kill_urb and later
36 last* is modified in finish_unlinks().
38 As suggested by Alan Stern, the solution for proper handling of
39 ohci->ed_rm_list is to remove ed from the ed_rm_list before finishing
40 any URBs. Then at the end, we can add ed back to the list if necessary.
42 This properly handle the updated ohci->ed_rm_list in usb_kill_urb().
44 Fixes: 977dcfdc6031 ("USB: OHCI: don't lose track of EDs when a controller dies")
45 Acked-by: Alan Stern <stern@rowland.harvard.edu>
46 CC: <stable@vger.kernel.org>
47 Signed-off-by: Aman Deep <aman.deep@samsung.com>
48 Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com>
49 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
51 diff --git a/drivers/usb/host/ohci-q.c b/drivers/usb/host/ohci-q.c
52 index b2ec8c399363..4ccb85a67bb3 100644
53 --- a/drivers/usb/host/ohci-q.c
54 +++ b/drivers/usb/host/ohci-q.c
55 @@ -1019,6 +1019,8 @@ static void finish_unlinks(struct ohci_hcd *ohci)
56 * have modified this list. normally it's just prepending
57 * entries (which we'd ignore), but paranoia won't hurt.
59 + *last = ed->ed_next;
63 /* unlink urbs as requested, but rescan the list after
64 @@ -1077,21 +1079,22 @@ static void finish_unlinks(struct ohci_hcd *ohci)
68 - * If no TDs are queued, take ED off the ed_rm_list.
69 + * If no TDs are queued, ED is now idle.
70 * Otherwise, if the HC is running, reschedule.
71 - * If not, leave it on the list for further dequeues.
72 + * If the HC isn't running, add ED back to the
73 + * start of the list for later processing.
75 if (list_empty(&ed->td_list)) {
76 - *last = ed->ed_next;
79 list_del(&ed->in_use_list);
80 } else if (ohci->rh_state == OHCI_RH_RUNNING) {
81 - *last = ed->ed_next;
83 ed_schedule(ohci, ed);
85 - last = &ed->ed_next;
86 + ed->ed_next = ohci->ed_rm_list;
87 + ohci->ed_rm_list = ed;
88 + /* Don't loop on the same ED */
89 + if (last == &ohci->ed_rm_list)
90 + last = &ed->ed_next;