Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp1702145ybi; Wed, 17 Jul 2019 20:24:25 -0700 (PDT) X-Google-Smtp-Source: APXvYqy2iCO0RohPhiDM+goHb7rxHAY5zz/zpbe4+knbW/gxQ7Ygg3bBGeP4mT7VBj+5IqJbRW9f X-Received: by 2002:a17:902:8207:: with SMTP id x7mr47511781pln.63.1563420265459; Wed, 17 Jul 2019 20:24:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563420265; cv=none; d=google.com; s=arc-20160816; b=nu1vM21HDNXLAtG3vyqEGSiUYvzxCwC1CZxwgsCD0Mo6VmHIQf9EDfZ4Wt+05uGfSN pO9pdFL0keF0BU6qOVxb4zWgeUIUrzuL3IB9YXd6XfG3YnAUabp4m0d7AwzbpSCl8enm fywdFK7XKh8/qUwIVfRHrHeB+JxUCZOz2i80Z1ma8irAM7PjvULe730IPWZUu1ICv1C6 rNY3xkNloIZxNVDyXAq/AePL5H/InZKnUg3VR1a1y6/yDmHSRlDTwExyAcjVIpVJgd9m yCmekZHYA/BBILwBj58FAj2LXFk6VwdcCKAA9Udn7KCc3YVtAEFvQotm+gX1ljLCKsXL FvOg== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=VXclKcJjezOBU8MvHVSm7O2dZoVqIPhpCSMS4HzPwVg=; b=sLQYMlK5bT6McMJCmO+LpX1ye0pp5immMNXP/D02+U17H5k4StneVgXQb5qXzko7bM O8rPcuHpTZQA/sHHqReTlwzxUFPtAyzLECWPX1VLZxrnfp2ZgGvfc0dtCGBU204DwSy0 d7PgQKmT8r+fOVseXu2WvLjjnMPqmXCTasqOMNxHpZbAMhp6UvNLHwGAeuMkF+Ry3M1Z 6HoMifcX+c5xE6HHs7I7R2in1uiul9SG/wUisfWbKlROQYs0B6KHP34yqoC/7tmOV3PW uvILSjvnYj3EUX9swkeFnxSB8IpNZqvK/gTcDI7rPvWZ6y8U+jDi7u/NPl623oSVmo+A Lz8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=TVd1OEYB; 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 f12si27557618pfq.187.2019.07.17.20.24.09; Wed, 17 Jul 2019 20:24:25 -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=TVd1OEYB; 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 S2391683AbfGRDXF (ORCPT + 99 others); Wed, 17 Jul 2019 23:23:05 -0400 Received: from mail.kernel.org ([198.145.29.99]:60290 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727601AbfGRDXE (ORCPT ); Wed, 17 Jul 2019 23:23:04 -0400 Received: from localhost (115.42.148.210.bf.2iij.net [210.148.42.115]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id C856B20818; Thu, 18 Jul 2019 03:13:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1563419618; bh=tMIkPAUrJX4cVcb2pgdpRWKSEyW7lM/2TE9CNA6vFO8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=TVd1OEYBeUrOcFN8oQLn+I0Apsj4Q2XIMyoTeB/OLMhcy3V80VFZ24BWHVuj2KuIX NmND+GqHS6XJW248AoFAyN+PS/v3Gs6XZYKUqX+G1aD7ZDCcXnqusDsXEPsKQXSGkx 5BYrN3+6Ppyk4fx6egkLVarDgRMcrrkZWtxpCe2Q= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Yoshihiro Shimoda , Felipe Balbi Subject: [PATCH 4.9 34/54] usb: renesas_usbhs: add a workaround for a race condition of workqueue Date: Thu, 18 Jul 2019 12:02:04 +0900 Message-Id: <20190718030052.084474453@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190718030048.392549994@linuxfoundation.org> References: <20190718030048.392549994@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Yoshihiro Shimoda commit b2357839c56ab7d06bcd4e866ebc2d0e2b7997f3 upstream. The old commit 6e4b74e4690d ("usb: renesas: fix scheduling in atomic context bug") fixed an atomic issue by using workqueue for the shdmac dmaengine driver. However, this has a potential race condition issue between the work pending and usbhsg_ep_free_request() in gadget mode. When usbhsg_ep_free_request() is called while pending the queue, since the work_struct will be freed and then the work handler is called, kernel panic happens on process_one_work(). To fix the issue, if we could call cancel_work_sync() at somewhere before the free request, it could be easy. However, the usbhsg_ep_free_request() is called on atomic (e.g. f_ncm driver calls free request via gether_disconnect()). For now, almost all users are having "USB-DMAC" and the DMAengine driver can be used on atomic. So, this patch adds a workaround for a race condition to call the DMAengine APIs without the workqueue. This means we still have TODO on shdmac environment (SH7724), but since it doesn't have SMP, the race condition might not happen. Fixes: ab330cf3888d ("usb: renesas_usbhs: add support for USB-DMAC") Cc: # v4.1+ Signed-off-by: Yoshihiro Shimoda Signed-off-by: Felipe Balbi Signed-off-by: Greg Kroah-Hartman --- drivers/usb/renesas_usbhs/fifo.c | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) --- a/drivers/usb/renesas_usbhs/fifo.c +++ b/drivers/usb/renesas_usbhs/fifo.c @@ -821,9 +821,8 @@ static int __usbhsf_dma_map_ctrl(struct } static void usbhsf_dma_complete(void *arg); -static void xfer_work(struct work_struct *work) +static void usbhsf_dma_xfer_preparing(struct usbhs_pkt *pkt) { - struct usbhs_pkt *pkt = container_of(work, struct usbhs_pkt, work); struct usbhs_pipe *pipe = pkt->pipe; struct usbhs_fifo *fifo; struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); @@ -831,12 +830,10 @@ static void xfer_work(struct work_struct struct dma_chan *chan; struct device *dev = usbhs_priv_to_dev(priv); enum dma_transfer_direction dir; - unsigned long flags; - usbhs_lock(priv, flags); fifo = usbhs_pipe_to_fifo(pipe); if (!fifo) - goto xfer_work_end; + return; chan = usbhsf_dma_chan_get(fifo, pkt); dir = usbhs_pipe_is_dir_in(pipe) ? DMA_DEV_TO_MEM : DMA_MEM_TO_DEV; @@ -845,7 +842,7 @@ static void xfer_work(struct work_struct pkt->trans, dir, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); if (!desc) - goto xfer_work_end; + return; desc->callback = usbhsf_dma_complete; desc->callback_param = pipe; @@ -853,7 +850,7 @@ static void xfer_work(struct work_struct pkt->cookie = dmaengine_submit(desc); if (pkt->cookie < 0) { dev_err(dev, "Failed to submit dma descriptor\n"); - goto xfer_work_end; + return; } dev_dbg(dev, " %s %d (%d/ %d)\n", @@ -864,8 +861,17 @@ static void xfer_work(struct work_struct dma_async_issue_pending(chan); usbhsf_dma_start(pipe, fifo); usbhs_pipe_enable(pipe); +} + +static void xfer_work(struct work_struct *work) +{ + struct usbhs_pkt *pkt = container_of(work, struct usbhs_pkt, work); + struct usbhs_pipe *pipe = pkt->pipe; + struct usbhs_priv *priv = usbhs_pipe_to_priv(pipe); + unsigned long flags; -xfer_work_end: + usbhs_lock(priv, flags); + usbhsf_dma_xfer_preparing(pkt); usbhs_unlock(priv, flags); } @@ -918,8 +924,13 @@ static int usbhsf_dma_prepare_push(struc pkt->trans = len; usbhsf_tx_irq_ctrl(pipe, 0); - INIT_WORK(&pkt->work, xfer_work); - schedule_work(&pkt->work); + /* FIXME: Workaound for usb dmac that driver can be used in atomic */ + if (usbhs_get_dparam(priv, has_usb_dmac)) { + usbhsf_dma_xfer_preparing(pkt); + } else { + INIT_WORK(&pkt->work, xfer_work); + schedule_work(&pkt->work); + } return 0; @@ -1025,8 +1036,7 @@ static int usbhsf_dma_prepare_pop_with_u pkt->trans = pkt->length; - INIT_WORK(&pkt->work, xfer_work); - schedule_work(&pkt->work); + usbhsf_dma_xfer_preparing(pkt); return 0;