Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp3131220ybi; Thu, 18 Jul 2019 21:38:19 -0700 (PDT) X-Google-Smtp-Source: APXvYqw3Oxmzgy2ryt+9XwQpRtnsAV6GuZAXTWGizVW/0SFLiDXDCFuGVx5WOOfdA+y/avcSJH8W X-Received: by 2002:a17:90a:9f08:: with SMTP id n8mr55748749pjp.102.1563511099546; Thu, 18 Jul 2019 21:38:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563511099; cv=none; d=google.com; s=arc-20160816; b=imfxnrmKevh8TtIbhSUlHggCrg5WvB1y24kQXgomuRn+kGMd5vrnigmZhYEEuN5d7c aeWzvDeNiPy4wPPpLR7u15od2NXjvHwMzaPSVZiBcBZxIrP95wY8JpII9iCaeWybDMIZ O+FrqPQ2YGPnxDbxnwJMlBq6h/cGvCO4yX1b8KjiGqoZy0VaK5+1c4PTHUjHxB4Ph3lZ 9DB1CivLPyKtKiajKb9zK2oOAJ9noOSHUti00fHww3XUz/qwbxhHz6u7Sh/vn4VsEkSp j/ZGkSsPQabSpquNrZP1Oqy5Zel51xwwRZsx8GHfSBJ5QZGHJ2zNyEtsdd+BsBrW+Co+ 6dgg== 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=HffY4lkLeKgguKZkWHLfELpjxp+ulHiYLmQBcezkUDU=; b=ZUAn+0aIuz0DzvzRCBvb06TlSDEE+QhoGDTFu9dmM2KvC82MXFa89Ulc8BomS45rRi fIO6+7GlnamDEJGUZlTHLDvQUdt7VBafAJzzyAzBO6rVfze3c/rwyfl6A6UgUQCAD0pE ywT2/as3POeCPR8bdoISVIo+n3BKVpwRFvi2HAHA9xfGJMxMEVy8fRpVRSgnF5Yxs51+ V9i3RZolWQa92LN8/uW0cThsO/cVy69PRXo9ioJ31tbyfWmHzyzoX00+/2w6H5Xh+y5H GqyrhBTb8mqJ1v03xs7hT7FE9C/YSfcKFDYHhPjcI1JMSGuTkD+e4S5j7nVkgURdrwJ+ 2zBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=LbbLBtzo; 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 w16si178538pfi.31.2019.07.18.21.38.04; Thu, 18 Jul 2019 21:38:19 -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=LbbLBtzo; 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 S1729114AbfGSEAf (ORCPT + 99 others); Fri, 19 Jul 2019 00:00:35 -0400 Received: from mail.kernel.org ([198.145.29.99]:60278 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728181AbfGSEAZ (ORCPT ); Fri, 19 Jul 2019 00:00:25 -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 7CEE821882; Fri, 19 Jul 2019 04:00:23 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1563508824; bh=i8vDtX8H1jNWjo3F8vyc4Zbjb3Va1+eIlNAOC/ICPNc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LbbLBtzomR3VvBGY51DUcb1Oc7Pqa5GE2jrNgPhce/eodPR/4PnhXfrYiJYG42BKa RlDTfyYEP0EKCdU761cTfW9M18e+YST3m6h4YFkcN7GLr2lsDRvLTbWg/tVnsNzw3h J6CUsAp4UqfmaIxFD+P0d4J9XBb28zl+aUENBD/o= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Geert Uytterhoeven , Eugeniu Rosca , Yoshihiro Shimoda , Greg Kroah-Hartman , Sasha Levin , linux-serial@vger.kernel.org Subject: [PATCH AUTOSEL 5.2 110/171] serial: sh-sci: Fix TX DMA buffer flushing and workqueue races Date: Thu, 18 Jul 2019 23:55:41 -0400 Message-Id: <20190719035643.14300-110-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190719035643.14300-1-sashal@kernel.org> References: <20190719035643.14300-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review 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: Geert Uytterhoeven [ Upstream commit 8493eab02608b0e82f67b892aa72882e510c31d0 ] When uart_flush_buffer() is called, the .flush_buffer() callback zeroes the tx_dma_len field. This may race with the work queue function handling transmit DMA requests: 1. If the buffer is flushed before the first DMA API call, dmaengine_prep_slave_single() may be called with a zero length, causing the DMA request to never complete, leading to messages like: rcar-dmac e7300000.dma-controller: Channel Address Error happen and, with debug enabled: sh-sci e6e88000.serial: sci_dma_tx_work_fn: ffff800639b55000: 0...0, cookie 126 and DMA timeouts. 2. If the buffer is flushed after the first DMA API call, but before the second, dma_sync_single_for_device() may be called with a zero length, causing the transmit data not to be flushed to RAM, and leading to stale data being output. Fix this by: 1. Letting sci_dma_tx_work_fn() return immediately if the transmit buffer is empty, 2. Extending the critical section to cover all DMA preparational work, so tx_dma_len stays consistent for all of it, 3. Using local copies of circ_buf.head and circ_buf.tail, to make sure they match the actual operation above. Reported-by: Eugeniu Rosca Suggested-by: Yoshihiro Shimoda Signed-off-by: Geert Uytterhoeven Reviewed-by: Eugeniu Rosca Tested-by: Eugeniu Rosca Link: https://lore.kernel.org/r/20190624123540.20629-2-geert+renesas@glider.be Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- drivers/tty/serial/sh-sci.c | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/drivers/tty/serial/sh-sci.c b/drivers/tty/serial/sh-sci.c index 1d25c4e2d0d2..d18c680aa64b 100644 --- a/drivers/tty/serial/sh-sci.c +++ b/drivers/tty/serial/sh-sci.c @@ -1398,6 +1398,7 @@ static void sci_dma_tx_work_fn(struct work_struct *work) struct circ_buf *xmit = &port->state->xmit; unsigned long flags; dma_addr_t buf; + int head, tail; /* * DMA is idle now. @@ -1407,16 +1408,23 @@ static void sci_dma_tx_work_fn(struct work_struct *work) * consistent xmit buffer state. */ spin_lock_irq(&port->lock); - buf = s->tx_dma_addr + (xmit->tail & (UART_XMIT_SIZE - 1)); + head = xmit->head; + tail = xmit->tail; + buf = s->tx_dma_addr + (tail & (UART_XMIT_SIZE - 1)); s->tx_dma_len = min_t(unsigned int, - CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE), - CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE)); - spin_unlock_irq(&port->lock); + CIRC_CNT(head, tail, UART_XMIT_SIZE), + CIRC_CNT_TO_END(head, tail, UART_XMIT_SIZE)); + if (!s->tx_dma_len) { + /* Transmit buffer has been flushed */ + spin_unlock_irq(&port->lock); + return; + } desc = dmaengine_prep_slave_single(chan, buf, s->tx_dma_len, DMA_MEM_TO_DEV, DMA_PREP_INTERRUPT | DMA_CTRL_ACK); if (!desc) { + spin_unlock_irq(&port->lock); dev_warn(port->dev, "Failed preparing Tx DMA descriptor\n"); goto switch_to_pio; } @@ -1424,18 +1432,18 @@ static void sci_dma_tx_work_fn(struct work_struct *work) dma_sync_single_for_device(chan->device->dev, buf, s->tx_dma_len, DMA_TO_DEVICE); - spin_lock_irq(&port->lock); desc->callback = sci_dma_tx_complete; desc->callback_param = s; - spin_unlock_irq(&port->lock); s->cookie_tx = dmaengine_submit(desc); if (dma_submit_error(s->cookie_tx)) { + spin_unlock_irq(&port->lock); dev_warn(port->dev, "Failed submitting Tx DMA descriptor\n"); goto switch_to_pio; } + spin_unlock_irq(&port->lock); dev_dbg(port->dev, "%s: %p: %d...%d, cookie %d\n", - __func__, xmit->buf, xmit->tail, xmit->head, s->cookie_tx); + __func__, xmit->buf, tail, head, s->cookie_tx); dma_async_issue_pending(chan); return; -- 2.20.1