From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-sn1nam01on0124.outbound.protection.outlook.com ([104.47.32.124]:48473 "EHLO NAM01-SN1-obe.outbound.protection.outlook.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1032640AbeCAPkI (ORCPT ); Thu, 1 Mar 2018 10:40:08 -0500 From: Sasha Levin To: "stable@vger.kernel.org" , "stable-commits@vger.kernel.org" CC: AMAN DEEP , Jeffy Chen , Greg Kroah-Hartman , Sasha Levin Subject: [added to the 4.1 stable tree] usb: ohci: Proper handling of ed_rm_list to handle race condition between usb_kill_urb() and finish_unlinks() Date: Thu, 1 Mar 2018 15:27:56 +0000 Message-ID: <20180301152116.1486-512-alexander.levin@microsoft.com> References: <20180301152116.1486-1-alexander.levin@microsoft.com> In-Reply-To: <20180301152116.1486-1-alexander.levin@microsoft.com> Content-Language: en-US Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 Sender: stable-owner@vger.kernel.org List-ID: From: AMAN DEEP This patch has been added to the 4.1 stable tree. If you have any objections, please let us know. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D [ Upstream commit 46408ea558df13b110e0866b99624384a33bdeba ] There is a race condition between finish_unlinks->finish_urb() function and usb_kill_urb() in ohci controller case. The finish_urb calls spin_unlock(&ohci->lock) before usb_hcd_giveback_urb() function call, then if during this time, usb_kill_urb is called for another endpoint, then new ed will be added to ed_rm_list at beginning for unlink, and ed_rm_list will point to newly added. When finish_urb() is completed in finish_unlinks() and ed->td_list becomes empty as in below code (in finish_unlinks() function): if (list_empty(&ed->td_list)) { *last =3D ed->ed_next; ed->ed_next =3D NULL; } else if (ohci->rh_state =3D=3D OHCI_RH_RUNNING) { *last =3D ed->ed_next; ed->ed_next =3D NULL; ed_schedule(ohci, ed); } The *last =3D ed->ed_next will make ed_rm_list to point to ed->ed_next and previously added ed by usb_kill_urb will be left unreferenced by ed_rm_list. This causes usb_kill_urb() hang forever waiting for finish_unlink to remove added ed from ed_rm_list. The main reason for hang in this race condtion is addition and removal of ed from ed_rm_list in the beginning during usb_kill_urb and later last* is modified in finish_unlinks(). As suggested by Alan Stern, the solution for proper handling of ohci->ed_rm_list is to remove ed from the ed_rm_list before finishing any URBs. Then at the end, we can add ed back to the list if necessary. This properly handle the updated ohci->ed_rm_list in usb_kill_urb(). Fixes: 977dcfdc6031 ("USB: OHCI: don't lose track of EDs when a controller = dies") Acked-by: Alan Stern CC: Signed-off-by: Aman Deep Signed-off-by: Jeffy Chen Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- drivers/usb/host/ohci-q.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/drivers/usb/host/ohci-q.c b/drivers/usb/host/ohci-q.c index 47d2c09e4f35..5cd4b286b198 100644 --- a/drivers/usb/host/ohci-q.c +++ b/drivers/usb/host/ohci-q.c @@ -1017,6 +1017,8 @@ static void finish_unlinks(struct ohci_hcd *ohci) * have modified this list. normally it's just prepending * entries (which we'd ignore), but paranoia won't hurt. */ + *last =3D ed->ed_next; + ed->ed_next =3D NULL; modified =3D 0; =20 /* unlink urbs as requested, but rescan the list after @@ -1075,21 +1077,22 @@ static void finish_unlinks(struct ohci_hcd *ohci) goto rescan_this; =20 /* - * If no TDs are queued, take ED off the ed_rm_list. + * If no TDs are queued, ED is now idle. * Otherwise, if the HC is running, reschedule. - * If not, leave it on the list for further dequeues. + * If the HC isn't running, add ED back to the + * start of the list for later processing. */ if (list_empty(&ed->td_list)) { - *last =3D ed->ed_next; - ed->ed_next =3D NULL; ed->state =3D ED_IDLE; list_del(&ed->in_use_list); } else if (ohci->rh_state =3D=3D OHCI_RH_RUNNING) { - *last =3D ed->ed_next; - ed->ed_next =3D NULL; ed_schedule(ohci, ed); } else { - last =3D &ed->ed_next; + ed->ed_next =3D ohci->ed_rm_list; + ohci->ed_rm_list =3D ed; + /* Don't loop on the same ED */ + if (last =3D=3D &ohci->ed_rm_list) + last =3D &ed->ed_next; } =20 if (modified) --=20 2.14.1