Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3515960imu; Mon, 7 Jan 2019 04:57:21 -0800 (PST) X-Google-Smtp-Source: ALg8bN5mevFwhcxEZK9B7gu236RgvdIqr9zmnQco6jo5yPE8TmRmDLF97/lFQpmLBaPqg5qOKLnQ X-Received: by 2002:a63:5455:: with SMTP id e21mr10912202pgm.316.1546865841452; Mon, 07 Jan 2019 04:57:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546865841; cv=none; d=google.com; s=arc-20160816; b=epgH7V4feBdHO6B7AgKm1MHt77XGIY0Pt35CAZaA3mX+Xlig2H0ijjbQGLmjN2zAIc dug2fCKhKLroHN+Kqqb09k5PgOFGyfgnZSz9wtItMFexJXJf4kM8W6i9aZ2uqbxqcu0N Q8rPzJu+EW4q7xjDoTwOob9p3Mx0N+yElSt485OxlJV669nJzywKWUIDtvxgOadxKAfS cEdobmWuLpKsaxAbeUd5OS1Kwf6aGbxlylg/dgP+i3q0vyoTBHstD2ihvCka9rwM8kuK PnWuWNqB2rJVOZZB5hdx6cwMsmglhV9r/QNKmT640MXq/xRVWoXPDdB7eCXVDqR2aYyA x82w== 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=4GRnk0EaeCjMflyrLmDISCYm51so0qAbQFNYvE+t/E0=; b=uCAOQRwjk9bOWAWMPWdjTvtVy2O9UszU6P79VyuLPoDP3xlLKwXbTdAvuSBNWZUOVo kWckFN6YmJOs1oOHLKSV6UrXOU3UE3vsM8elAlWdgwGPrFwgLnMl6fVh0SAzTbrP2sOZ 394r5oSXYmuIXQKDGO0Z/Az5fzimDVxseV4OkDoIvZmfzKtGuu1qmIu5Tb5fAW/GdNIO +SDbMSHEDFueycmqTMOr7YTb69qsNpu3VuMD6t6lfqMfFXEsA1+7H7COF/VgTZt3Hlh7 B8XDfs5LmTeNJx5r0qO5HcQ2WqNZ2TJ1CQ68ruCY/RedxTPr6JOkbLTxuTFHiSQAZ6f7 T5Kw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=tQbQbeWB; 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 w189si3203829pfb.151.2019.01.07.04.57.06; Mon, 07 Jan 2019 04:57:21 -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=@kernel.org header.s=default header.b=tQbQbeWB; 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 S1727841AbfAGMzL (ORCPT + 99 others); Mon, 7 Jan 2019 07:55:11 -0500 Received: from mail.kernel.org ([198.145.29.99]:43634 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729264AbfAGMzI (ORCPT ); Mon, 7 Jan 2019 07:55:08 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.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 37F4820651; Mon, 7 Jan 2019 12:55:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1546865707; bh=GWy0NywB4iJqKTuch/LHvwsty9YnnPPQMPAYlwT43GA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tQbQbeWBUaY3JhfdoMQdU4pZqU3rNecRusD3XI233sUU9/LhJxKSu9mcBelBeiSv+ 6CJufZUYrr0iStVPk+8hiVlHELeYyv22+9kSmwmhQAlrdlBjENqIkFaGXtRI5Sgrv5 99geAtIFyFwYD6hTIJMPk921Uix5ooD3MvpajfD8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Lukas Wunner , Mathias Duckeck , Frank Pavlic , Martin Sperl , =?UTF-8?q?Noralf=20Tr=C3=B8nnes?= , Mark Brown Subject: [PATCH 4.19 115/170] spi: bcm2835: Fix race on DMA termination Date: Mon, 7 Jan 2019 13:32:22 +0100 Message-Id: <20190107104506.513422580@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190107104452.953560660@linuxfoundation.org> References: <20190107104452.953560660@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore 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 4.19-stable review patch. If anyone has any objections, please let me know. ------------------ From: Lukas Wunner commit e82b0b3828451c1cd331d9f304c6078fcd43b62e upstream. If a DMA transfer finishes orderly right when spi_transfer_one_message() determines that it has timed out, the callbacks bcm2835_spi_dma_done() and bcm2835_spi_handle_err() race to call dmaengine_terminate_all(), potentially leading to double termination. Prevent by atomically changing the dma_pending flag before calling dmaengine_terminate_all(). Signed-off-by: Lukas Wunner Fixes: 3ecd37edaa2a ("spi: bcm2835: enable dma modes for transfers meeting certain conditions") Cc: stable@vger.kernel.org # v4.2+ Cc: Mathias Duckeck Cc: Frank Pavlic Cc: Martin Sperl Cc: Noralf Trønnes Signed-off-by: Mark Brown Signed-off-by: Greg Kroah-Hartman --- drivers/spi/spi-bcm2835.c | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) --- a/drivers/spi/spi-bcm2835.c +++ b/drivers/spi/spi-bcm2835.c @@ -233,10 +233,9 @@ static void bcm2835_spi_dma_done(void *d * is called the tx-dma must have finished - can't get to this * situation otherwise... */ - dmaengine_terminate_all(master->dma_tx); - - /* mark as no longer pending */ - bs->dma_pending = 0; + if (cmpxchg(&bs->dma_pending, true, false)) { + dmaengine_terminate_all(master->dma_tx); + } /* and mark as completed */; complete(&master->xfer_completion); @@ -617,10 +616,9 @@ static void bcm2835_spi_handle_err(struc struct bcm2835_spi *bs = spi_master_get_devdata(master); /* if an error occurred and we have an active dma, then terminate */ - if (bs->dma_pending) { + if (cmpxchg(&bs->dma_pending, true, false)) { dmaengine_terminate_all(master->dma_tx); dmaengine_terminate_all(master->dma_rx); - bs->dma_pending = 0; } /* and reset */ bcm2835_spi_reset_hw(master);