Received: by 2002:ac0:bc90:0:0:0:0:0 with SMTP id a16csp5596978img; Wed, 27 Mar 2019 11:23:07 -0700 (PDT) X-Google-Smtp-Source: APXvYqymUPpCvSC/ydslPLNppWmx7tHCPwxX2B6ZDNfpyBeSDCJOp2ak+3zfMskHyAFM/mC3Oq6X X-Received: by 2002:a65:6559:: with SMTP id a25mr32609786pgw.99.1553710987344; Wed, 27 Mar 2019 11:23:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1553710987; cv=none; d=google.com; s=arc-20160816; b=A0y4YkRhemIEBG3nxRhfopMk7EevGbQhJK4hkvl+AsPIrT2j8SLGEB+9EeLXAL2KwU hKff4t5eYDtBV8VINwXu5p7BRMJKNnTzkUGZRoG4p2iY720Lqy4w8+3M9sbAqUc/vm4P COCbHTLMD4zJOvQ2uiUV1txyv5Y+Tm7pEC00EeRfzffxjEGXo2jcRHHErLtFVSsouwZF tPNbkjqnO0hA9ScTIiJ1aivtWbA5aTHCU1GSm3XzbjR5DkP1qRoE598IZu7QNgbu4P4R B70uUNT/Df7zMEN9Ya8N88d6dLTQ8cD/nKpzE0GK4a7/oHHUIbDQlC/M7vpqZ0gT02jO cDsg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=+haTus112aXu/91Nj/IXVY0AUlWDxH1wsZwOWulkhgo=; b=Xa4wAXQd1DDTg8ZTf2E3Xgndkk5Ia5o8uQQ1r5n3bFP0xcmHukLdsdquivEm0N1nNe BCSPTZe329yPENkxh01tm8woyvVL7yy2ChwvKQB/h7PHs3LJtn6VDmanLg3pc2wXineC T9HzJEjoAgWmtsQ9g2VVxsXxTXdJBLXKROPfVtZ9YJTudY8EHK6ZMEfU9V0pqIrFIPpJ 3a6acQ3piumuU1vFk1iHa0yqV9vnNbBtqZXI6HFSaP5MNGLWee13i/OUYq/8QjvBdqi9 6gZ3U+R5tiBIl/j+y6Q2tQ2hanDl+ZmVYUqQieB8vZ54czpzGvgSzqUp+ez2IWPpu7kF mL1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=EK3FUcyK; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id h2si18810245pfn.77.2019.03.27.11.22.52; Wed, 27 Mar 2019 11:23:07 -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; dkim=pass header.i=@kernel.org header.s=default header.b=EK3FUcyK; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2403835AbfC0SWQ (ORCPT + 99 others); Wed, 27 Mar 2019 14:22:16 -0400 Received: from mail.kernel.org ([198.145.29.99]:40266 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391063AbfC0SWP (ORCPT ); Wed, 27 Mar 2019 14:22:15 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 2A70620449; Wed, 27 Mar 2019 18:22:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1553710933; bh=R9ZHvQ8Du4A/E/C7p2SHCKRdCRP0ARMNbWyMpobFLgw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=EK3FUcyKtuwlP7M2bO7CN1lSsDXLt7WTlo65XQwDyjxectiYj0LrkvY+kUkVt2bHM viGTmtLTpAyMGLTiumwBTyRlWE/xzWizWeBfqgxPgSdbEKqQM0PyQZGYIhcTe24j1b C8SeOsK4fV8X70P3gHH9ICUyPiibeyQPEWWBk5NI= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: John Stultz , Alan Stern , Felipe Balbi , Zeng Tao , Jack Pham , Thinh Nguyen , Chen Yu , Jerry Zhang , Lars-Peter Clausen , Vincent Pelletier , Andrzej Pietrasiewicz , Greg Kroah-Hartman , Linux USB List , Felipe Balbi , Sasha Levin Subject: [PATCH AUTOSEL 4.9 51/87] usb: f_fs: Avoid crash due to out-of-scope stack ptr access Date: Wed, 27 Mar 2019 14:20:04 -0400 Message-Id: <20190327182040.17444-51-sashal@kernel.org> X-Mailer: git-send-email 2.19.1 In-Reply-To: <20190327182040.17444-1-sashal@kernel.org> References: <20190327182040.17444-1-sashal@kernel.org> MIME-Version: 1.0 X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: John Stultz [ Upstream commit 54f64d5c983f939901dacc8cfc0983727c5c742e ] Since the 5.0 merge window opened, I've been seeing frequent crashes on suspend and reboot with the trace: [ 36.911170] Unable to handle kernel paging request at virtual address ffffff801153d660 [ 36.912769] Unable to handle kernel paging request at virtual address ffffff800004b564 ... [ 36.950666] Call trace: [ 36.950670] queued_spin_lock_slowpath+0x1cc/0x2c8 [ 36.950681] _raw_spin_lock_irqsave+0x64/0x78 [ 36.950692] complete+0x28/0x70 [ 36.950703] ffs_epfile_io_complete+0x3c/0x50 [ 36.950713] usb_gadget_giveback_request+0x34/0x108 [ 36.950721] dwc3_gadget_giveback+0x50/0x68 [ 36.950723] dwc3_thread_interrupt+0x358/0x1488 [ 36.950731] irq_thread_fn+0x30/0x88 [ 36.950734] irq_thread+0x114/0x1b0 [ 36.950739] kthread+0x104/0x130 [ 36.950747] ret_from_fork+0x10/0x1c I isolated this down to in ffs_epfile_io(): https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/usb/gadget/function/f_fs.c#n1065 Where the completion done is setup on the stack: DECLARE_COMPLETION_ONSTACK(done); Then later we setup a request and queue it, and wait for it: if (unlikely(wait_for_completion_interruptible(&done))) { /* * To avoid race condition with ffs_epfile_io_complete, * dequeue the request first then check * status. usb_ep_dequeue API should guarantee no race * condition with req->complete callback. */ usb_ep_dequeue(ep->ep, req); interrupted = ep->status < 0; } The problem is, that we end up being interrupted, dequeue the request, and exit. But then the irq triggers and we try calling complete() on the context pointer which points to now random stack space, which results in the panic. Alan Stern pointed out there is a bug here, in that the snippet above "assumes that usb_ep_dequeue() waits until the request has been completed." And that: wait_for_completion(&done); Is needed right after the usb_ep_dequeue(). Thus this patch implements that change. With it I no longer see the crashes on suspend or reboot. This issue seems to have been uncovered by behavioral changes in the dwc3 driver in commit fec9095bdef4e ("usb: dwc3: gadget: remove wait_end_transfer"). Cc: Alan Stern Cc: Felipe Balbi Cc: Zeng Tao Cc: Jack Pham Cc: Thinh Nguyen Cc: Chen Yu Cc: Jerry Zhang Cc: Lars-Peter Clausen Cc: Vincent Pelletier Cc: Andrzej Pietrasiewicz Cc: Greg Kroah-Hartman Cc: Linux USB List Suggested-by: Alan Stern Signed-off-by: John Stultz Signed-off-by: Felipe Balbi Signed-off-by: Sasha Levin --- drivers/usb/gadget/function/f_fs.c | 1 + 1 file changed, 1 insertion(+) diff --git a/drivers/usb/gadget/function/f_fs.c b/drivers/usb/gadget/function/f_fs.c index 04eb64381d92..927ac0ee09b7 100644 --- a/drivers/usb/gadget/function/f_fs.c +++ b/drivers/usb/gadget/function/f_fs.c @@ -1008,6 +1008,7 @@ static ssize_t ffs_epfile_io(struct file *file, struct ffs_io_data *io_data) * condition with req->complete callback. */ usb_ep_dequeue(ep->ep, req); + wait_for_completion(&done); interrupted = ep->status < 0; } -- 2.19.1