Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp1208495ybi; Fri, 2 Aug 2019 11:09:51 -0700 (PDT) X-Google-Smtp-Source: APXvYqxkG0tZTDyc96fzH4SYT44XjpoJlABsWkpXhVMjZfaK0/udsSTsnNP2bGxZ0oqy39EV1niG X-Received: by 2002:aa7:97b7:: with SMTP id d23mr63259218pfq.203.1564769391897; Fri, 02 Aug 2019 11:09:51 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564769391; cv=none; d=google.com; s=arc-20160816; b=DPwDvthGWkjaNZz8sthk9At2zZAb5i7WMwITTXdtIWFr4/Kq0P7+VZ3b8CMhzNiIqE I5meDOA43m/QCjTgIK2jO3du5L3hdXsTjfivZHZLnJluhkiRDqeBdkCWrtadsEtkORe/ MuTWJKoCoWHrWtOWgp3bhdXwrY/gDSmY7VIfmplM7DsUkg1giglJHk/EdhOWyrXR/c5Y AipY3mkm4asFLZ7lOFXO9wTp2GtWbodKwUSUrOmRj1Pwn2FY0S1QuKWNoGyZ9DvPnzsf JYK5VpjNJPyIUaBk7WjA/W67Scw6dW8z94Rj1xMF2F9X50qXdvO1WYrj03m8eeBbpMab nWMw== 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=QsdFeU49+7LrtGhRZqGbLOqNIxjuz4m5agyEhQNqMd0=; b=GrbaqcS5GJ03VAFeWjOpUIh9Mx4RpiykmjRlV3UoytAfbdQtVwUj6f/nj+8HFmIBol S4wWQBc4i73wSsMnlVSUrZT13R8sBNjb+qaoVwwgdanu05PoKXs2uX9J0aXNZwEKU10c Z3Atioqslf8/4UQU9Tw08hiQRCFv3aF3fO0ZrvvvFxhGgLspkJzkiisclX3oS+gt+3Mf vsc2O7LCZrRFytgahCw9lCaUNnD9N2fE7bJQ0ZIWTbO514c4rb1z/cvi8xl4bi16MWxe 44PtheE1jdYwO+1Fm4kzzgw+k9anGzbCp5gUliuw9k/TutoZhqs7vgDQdxQrRvtVqlHl L2sQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=mCnBOVq5; 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 y3si7066064pjv.50.2019.08.02.11.09.35; Fri, 02 Aug 2019 11:09:51 -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=mCnBOVq5; 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 S2405505AbfHBJwO (ORCPT + 99 others); Fri, 2 Aug 2019 05:52:14 -0400 Received: from mail.kernel.org ([198.145.29.99]:57696 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2392162AbfHBJwM (ORCPT ); Fri, 2 Aug 2019 05:52:12 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (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 7E28B2064A; Fri, 2 Aug 2019 09:52:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1564739531; bh=maFH+lZ97Q7cYs/7MGitG/H47gASfqsm+VozZzEakU0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=mCnBOVq5CVwcr2wKuT81u2mSbzwR0ZI6CGyyUrG9d+DnyoY42j55TUwYmqCZiNxGq Qsxxmnxv9BvicWqiAcFu1X/70nzVUC6VhIFGjJh9s5raJE4lYwPgItbzGTNzv2PwGf u+SOU/BKDeha/AwfXwhSWncMeDkicxutCzcW7gpQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Eugeniu Rosca , Yoshihiro Shimoda , Geert Uytterhoeven , Sasha Levin Subject: [PATCH 4.9 188/223] serial: sh-sci: Fix TX DMA buffer flushing and workqueue races Date: Fri, 2 Aug 2019 11:36:53 +0200 Message-Id: <20190802092249.650244804@linuxfoundation.org> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190802092238.692035242@linuxfoundation.org> References: <20190802092238.692035242@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 [ 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 8ec8b3bbaf25..ea35f5144237 100644 --- a/drivers/tty/serial/sh-sci.c +++ b/drivers/tty/serial/sh-sci.c @@ -1291,6 +1291,7 @@ static void work_fn_tx(struct work_struct *work) struct uart_port *port = &s->port; struct circ_buf *xmit = &port->state->xmit; dma_addr_t buf; + int head, tail; /* * DMA is idle now. @@ -1300,16 +1301,23 @@ static void work_fn_tx(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"); /* switch to PIO */ sci_tx_dma_release(s, true); @@ -1319,20 +1327,20 @@ static void work_fn_tx(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"); /* switch to PIO */ sci_tx_dma_release(s, true); return; } + 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); } -- 2.20.1