Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2249678imm; Thu, 7 Jun 2018 07:42:04 -0700 (PDT) X-Google-Smtp-Source: ADUXVKLUpDYiJPqin7o9k/6tEueWXvSkHxRi/m+nF7/npQ0MqbN+tHr7hKos2MJ2eKqNhuGXxyj7 X-Received: by 2002:a63:bd01:: with SMTP id a1-v6mr1808587pgf.319.1528382523990; Thu, 07 Jun 2018 07:42:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528382523; cv=none; d=google.com; s=arc-20160816; b=qQYcb83dJX2pHZeaE0ZhrIB326mSedzAz2chNBwNZdbG/DBXE26jpSDDlFPX/yc9gJ 6hpzyd27AUFNiesTxWDGL0awZSw033nc6Sx/Rf+PGxZ98VsFs4r1LR8qxFZfshO9IUJl AT8SIQHome1h75sAtZfYhKmIGsJAAKhgjdheHNy95Mnifd8dlKC0n76f7bktT0gNRXFC WJcaFlJ4j1OOCiK3p2F8j6ytTM5UK0tabxQAM9BRILCzT3i2YoGo3w4Cb8ufiXQSUZGB Wtf0viucJIvbKwjbdKpPlPg3Y2g4/VCWVV8DfoJJZ2M2AQFP28j3Hap+YqfyV+WUZ0qi LU8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:subject:message-id:date:cc:to :from:mime-version:content-transfer-encoding:content-disposition :arc-authentication-results; bh=ycDjRopu482L8c0v25uieyzwB1cT4suFnrXpveBsNmk=; b=DqWTsTlzK8dMgmDfbF2TL4fFUqtwJ85SneJfVAEib9/nC+eqKXHwPtzVZxU//svH47 Zy8Qg9P+TNZ1CIiF1qLOWCibXTJr3yhY7/xm0GE7V7eL2Z52DzJOVdUW/CwVnlrKbdeI qJ7z+G0Ob/PesfaOJtT/57o4uas6elg/HQnNieSYRftDzSkISREnEVdttJPXrtytHKm9 t0ZOR1ocDqI524jLH+VXW37XW10f+UXfcxA2Su+GWf8jxz+R27GDJdny4ZVfrV25UixC +1hvrQRABDW/JYB1lz6qhgoND3F0Ez0tu2YsmFo4UK4lmTy9wRhk49ozeWzIUGCndgBt 87NQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ba5-v6si16790807plb.184.2018.06.07.07.41.49; Thu, 07 Jun 2018 07:42:03 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934342AbeFGOju (ORCPT + 99 others); Thu, 7 Jun 2018 10:39:50 -0400 Received: from shadbolt.e.decadent.org.uk ([88.96.1.126]:40514 "EHLO shadbolt.e.decadent.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933481AbeFGOjq (ORCPT ); Thu, 7 Jun 2018 10:39:46 -0400 Received: from [148.252.241.226] (helo=deadeye) by shadbolt.decadent.org.uk with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1fQvbd-0005Zo-Ja; Thu, 07 Jun 2018 15:09:37 +0100 Received: from ben by deadeye with local (Exim 4.91) (envelope-from ) id 1fQvb7-000347-Rv; Thu, 07 Jun 2018 15:09:05 +0100 Content-Type: text/plain; charset="UTF-8" Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 From: Ben Hutchings To: linux-kernel@vger.kernel.org, stable@vger.kernel.org CC: akpm@linux-foundation.org, "Alan Stern" , "Greg Kroah-Hartman" , "Jeffy Chen" , "AMAN DEEP" Date: Thu, 07 Jun 2018 15:05:21 +0100 Message-ID: X-Mailer: LinuxStableQueue (scripts by bwh) Subject: [PATCH 3.16 243/410] usb: ohci: Proper handling of ed_rm_list to handle race condition between usb_kill_urb() and finish_unlinks() In-Reply-To: X-SA-Exim-Connect-IP: 148.252.241.226 X-SA-Exim-Mail-From: ben@decadent.org.uk X-SA-Exim-Scanned: No (on shadbolt.decadent.org.uk); SAEximRunCond expanded to false Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 3.16.57-rc1 review patch. If anyone has any objections, please let me know. ------------------ From: AMAN DEEP commit 46408ea558df13b110e0866b99624384a33bdeba upstream. 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 = ed->ed_next; ed->ed_next = NULL; } else if (ohci->rh_state == OHCI_RH_RUNNING) { *last = ed->ed_next; ed->ed_next = NULL; ed_schedule(ohci, ed); } The *last = 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 Signed-off-by: Aman Deep Signed-off-by: Jeffy Chen Signed-off-by: Greg Kroah-Hartman [bwh: Backported to 3.16: adjust context] Signed-off-by: Ben Hutchings --- drivers/usb/host/ohci-q.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) --- a/drivers/usb/host/ohci-q.c +++ b/drivers/usb/host/ohci-q.c @@ -966,6 +966,8 @@ skip_ed: * have modified this list. normally it's just prepending * entries (which we'd ignore), but paranoia won't hurt. */ + *last = ed->ed_next; + ed->ed_next = NULL; modified = 0; /* unlink urbs as requested, but rescan the list after @@ -1024,20 +1026,21 @@ rescan_this: goto rescan_this; /* - * 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 = ed->ed_next; - ed->ed_next = NULL; ed->state = ED_IDLE; } else if (ohci->rh_state == OHCI_RH_RUNNING) { - *last = ed->ed_next; - ed->ed_next = NULL; ed_schedule(ohci, ed); } else { - last = &ed->ed_next; + ed->ed_next = ohci->ed_rm_list; + ohci->ed_rm_list = ed; + /* Don't loop on the same ED */ + if (last == &ohci->ed_rm_list) + last = &ed->ed_next; } if (modified)