]> git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob
83ff4d10ad4f11de33778b361735cc30fb5e7872
[thirdparty/kernel/stable-queue.git] /
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()
5
6 From: AMAN DEEP <aman.deep@samsung.com>
7
8 commit 46408ea558df13b110e0866b99624384a33bdeba upstream.
9
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.
16
17 When finish_urb() is completed in finish_unlinks() and ed->td_list
18 becomes empty as in below code (in finish_unlinks() function):
19
20 if (list_empty(&ed->td_list)) {
21 *last = ed->ed_next;
22 ed->ed_next = NULL;
23 } else if (ohci->rh_state == OHCI_RH_RUNNING) {
24 *last = ed->ed_next;
25 ed->ed_next = NULL;
26 ed_schedule(ohci, ed);
27 }
28
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.
33
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().
37
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.
41
42 This properly handle the updated ohci->ed_rm_list in usb_kill_urb().
43
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>
50
51 ---
52 drivers/usb/host/ohci-q.c | 17 ++++++++++-------
53 1 file changed, 10 insertions(+), 7 deletions(-)
54
55 --- a/drivers/usb/host/ohci-q.c
56 +++ b/drivers/usb/host/ohci-q.c
57 @@ -1018,6 +1018,8 @@ skip_ed:
58 * have modified this list. normally it's just prepending
59 * entries (which we'd ignore), but paranoia won't hurt.
60 */
61 + *last = ed->ed_next;
62 + ed->ed_next = NULL;
63 modified = 0;
64
65 /* unlink urbs as requested, but rescan the list after
66 @@ -1076,21 +1078,22 @@ rescan_this:
67 goto rescan_this;
68
69 /*
70 - * If no TDs are queued, take ED off the ed_rm_list.
71 + * If no TDs are queued, ED is now idle.
72 * Otherwise, if the HC is running, reschedule.
73 - * If not, leave it on the list for further dequeues.
74 + * If the HC isn't running, add ED back to the
75 + * start of the list for later processing.
76 */
77 if (list_empty(&ed->td_list)) {
78 - *last = ed->ed_next;
79 - ed->ed_next = NULL;
80 ed->state = ED_IDLE;
81 list_del(&ed->in_use_list);
82 } else if (ohci->rh_state == OHCI_RH_RUNNING) {
83 - *last = ed->ed_next;
84 - ed->ed_next = NULL;
85 ed_schedule(ohci, ed);
86 } else {
87 - last = &ed->ed_next;
88 + ed->ed_next = ohci->ed_rm_list;
89 + ohci->ed_rm_list = ed;
90 + /* Don't loop on the same ED */
91 + if (last == &ohci->ed_rm_list)
92 + last = &ed->ed_next;
93 }
94
95 if (modified)