Received: by 10.223.185.116 with SMTP id b49csp4201300wrg; Mon, 26 Feb 2018 13:04:50 -0800 (PST) X-Google-Smtp-Source: AH8x225Wmk2hqdosGs+CJzMTcRpgLEhFjfi4WvU1zweDDxRsYQ80KnxTgtXZ21EIGJseoP8JYc9M X-Received: by 10.98.201.194 with SMTP id l63mr11805065pfk.126.1519679089913; Mon, 26 Feb 2018 13:04:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519679089; cv=none; d=google.com; s=arc-20160816; b=gjXT6m3ABHzRzenbbtpgQ9BoFpcX8gdiKHIuac4t9LJFA3cVbRRhrNQuBwKT29qZp0 MIeVd4K86BXDEcHt+C3FJl5A5j5m/oA+086OGiWT5OkSrmt+kAAX2fV+/Hyd3i+s76RB 39C+EmEeGgHFEB/XDiebg7MXvTgJtsauNYDkggn3qFzcAtWI/AqHiMw9pglBSL/pCyt1 5b0ZRz0TC0pdVAGthRB9zku0I8Q63fyej/58Y7N/9UAiAfK4G2P7TEZjWTLtNn56CDXe GkU5qK42bfwsYfmEafzWagbtmLji2+Xv9how+lc5KFre214uRMNLBz9t4c54rbLJXT6A kJmQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=if9oOJ6fXovO1Tm8YiXbFAI0SQs3x9VXGdCe6S5r86Q=; b=YkG7V008IfP6KPpO4MWK/kzai/SsszHF5MhMkn7prxMg8biWvNZzv2N2OV8mJpIZx5 LfToTbBdoXP0pklfbemDOgSlDbUp/fNbzdNdHdOzfdPsTAOHQ6pFwNdeWbEyn7243rBD 97TLktCIxgN/hThSsU5sAIG/JYwKf6HJi4V2UDXZvloc1ERbBE+VAFbhqq5V7NNANO0f oGVhPBUmhQhbCovxa6l/t/77IjJLCKC8ywIDc0jgxDO/sQglbwLszynTjV7UJtV4KGip hGnMG93EpdX4MXwM1KyjKHkVA0HEfiKl/nl976QhulhSSCE0ljHyCRYNRU+GBkrffnuY oChw== 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 a2si5999504pgd.452.2018.02.26.13.04.33; Mon, 26 Feb 2018 13:04:49 -0800 (PST) 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 S1752368AbeBZUV0 (ORCPT + 99 others); Mon, 26 Feb 2018 15:21:26 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:33200 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752141AbeBZUVY (ORCPT ); Mon, 26 Feb 2018 15:21:24 -0500 Received: from localhost (clnet-b04-243.ikbnet.co.at [83.175.124.243]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id F35C4108B; Mon, 26 Feb 2018 20:21:23 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Alan Stern , Aman Deep , Jeffy Chen Subject: [PATCH 4.9 14/39] usb: ohci: Proper handling of ed_rm_list to handle race condition between usb_kill_urb() and finish_unlinks() Date: Mon, 26 Feb 2018 21:20:35 +0100 Message-Id: <20180226201644.311208882@linuxfoundation.org> X-Mailer: git-send-email 2.16.2 In-Reply-To: <20180226201643.660109883@linuxfoundation.org> References: <20180226201643.660109883@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.9-stable 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 CC: Signed-off-by: Aman Deep Signed-off-by: Jeffy Chen Signed-off-by: Greg Kroah-Hartman --- 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 @@ -1018,6 +1018,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 @@ -1076,21 +1078,22 @@ 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; list_del(&ed->in_use_list); } 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)