From patchwork Mon Dec 26 08:01:10 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "\(Exiting\) Baolin Wang" X-Patchwork-Id: 89017 Delivered-To: patch@linaro.org Received: by 10.140.20.101 with SMTP id 92csp4407061qgi; Mon, 26 Dec 2016 00:01:48 -0800 (PST) X-Received: by 10.99.244.17 with SMTP id g17mr47310330pgi.80.1482739308162; Mon, 26 Dec 2016 00:01:48 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r12si42292192pgn.241.2016.12.26.00.01.47; Mon, 26 Dec 2016 00:01:48 -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; dkim=pass header.i=@linaro.org; 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 dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1755264AbcLZIBj (ORCPT + 25 others); Mon, 26 Dec 2016 03:01:39 -0500 Received: from mail-pg0-f54.google.com ([74.125.83.54]:36059 "EHLO mail-pg0-f54.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752016AbcLZIBh (ORCPT ); Mon, 26 Dec 2016 03:01:37 -0500 Received: by mail-pg0-f54.google.com with SMTP id f188so132641265pgc.3 for ; Mon, 26 Dec 2016 00:01:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id; bh=ot9Mc3cTln+0lCxUBVWjJLBPYmiSZxBAlVdD43E3Zm4=; b=WnmtW/SKogi6Z9TyiEPTJ4U98hZmuapMb6Vt5g85IZC/s6JQNsj0ASLUkwH6XA0cMg CElOT1ONn4nYZY8we0RSmEvEzWFHQabNe2SUeod4xdiVkwKeOXUjFTPwfLIbnEgYxPr3 yBaplYpRE3RkfT+fqM+c1BX+RI7twHEG3xSnc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=ot9Mc3cTln+0lCxUBVWjJLBPYmiSZxBAlVdD43E3Zm4=; b=RmOpxRnb3QviHbd/snp9TBTvpGrbbNq9vznxRqQi0kcHGfjMFkYuPcNi/FLRVp5Lql sICVyggd+m+6vqnVwU0dB8hjlqzqs3eI+W9MgscwEjBzHwajl+E8OULndOm0UAnevmfz 9htV3lX52te72DOPmrkCpmsJp1ulPTbkyExp+5mDgrYnzFv6PYk6jbsUhKqH4Ky3SKFG EQ0EkizG8znSi+yoivBSmnTaH8Rskis/3dK+wlmmMSCUWPMIX2xrfhHkK9N3ymUSFFOU apUvNZ85ukWy9rF/IHqYMrdnmZ0fEblsGxYKbA14Tw8d2F7mrxlTFoKAGN2vrz4D/MVp Gx1w== X-Gm-Message-State: AIkVDXKPYhzhP9ILXeKCeU4oCaWfoq6/uh/riNHEH0xguXdTCHmSC1M9ej7ts8E4Mtfx5PMy X-Received: by 10.98.21.148 with SMTP id 142mr23722276pfv.38.1482739296300; Mon, 26 Dec 2016 00:01:36 -0800 (PST) Received: from baolinwangubtpc.spreadtrum.com ([175.111.195.49]) by smtp.gmail.com with ESMTPSA id y62sm15867187pfk.52.2016.12.26.00.01.33 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Mon, 26 Dec 2016 00:01:35 -0800 (PST) From: Baolin Wang To: balbi@kernel.org, gregkh@linuxfoundation.org Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, linaro-kernel@lists.linaro.org, broonie@kernel.org, baolin.wang@linaro.org Subject: [PATCH] usb: dwc3: gadget: Avoid race between dwc3 interrupt handler and irq thread handler Date: Mon, 26 Dec 2016 16:01:10 +0800 Message-Id: <0d79eb1f34e409749a136173b68f365b545c4789.1482738764.git.baolin.wang@linaro.org> X-Mailer: git-send-email 1.7.9.5 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On some platfroms(like x86 platform), when one core is running the USB gadget irq thread handler by dwc3_thread_interrupt(), meanwhile another core also can respond other interrupts from dwc3 controller and modify the event buffer by dwc3_interrupt() function, that will cause getting the wrong event count in irq thread handler to make the USB function abnormal. We should add spin_lock/unlock() in dwc3_check_event_buf() to avoid this race. Signed-off-by: Baolin Wang --- drivers/usb/dwc3/gadget.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) -- 1.7.9.5 diff --git a/drivers/usb/dwc3/gadget.c b/drivers/usb/dwc3/gadget.c index 6785595..1a1e1f4 100644 --- a/drivers/usb/dwc3/gadget.c +++ b/drivers/usb/dwc3/gadget.c @@ -2894,10 +2894,13 @@ static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt) return IRQ_HANDLED; } + spin_lock(&dwc->lock); count = dwc3_readl(dwc->regs, DWC3_GEVNTCOUNT(0)); count &= DWC3_GEVNTCOUNT_MASK; - if (!count) + if (!count) { + spin_unlock(&dwc->lock); return IRQ_NONE; + } evt->count = count; evt->flags |= DWC3_EVENT_PENDING; @@ -2914,6 +2917,7 @@ static irqreturn_t dwc3_check_event_buf(struct dwc3_event_buffer *evt) memcpy(evt->cache, evt->buf, count - amount); dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(0), count); + spin_unlock(&dwc->lock); return IRQ_WAKE_THREAD; }