Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4708941pxj; Wed, 12 May 2021 11:23:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyR0X2U0/mlG/ue2kF7lWKUQAxj7SVY/CId1HwxeM91CBQgkEe00LRdRiJTSLlYZuLq2Uem X-Received: by 2002:a17:906:3c47:: with SMTP id i7mr1274332ejg.3.1620843797499; Wed, 12 May 2021 11:23:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620843797; cv=none; d=google.com; s=arc-20160816; b=DimRczYgNyj/S7UExytHmn1ZU+yH6IBIzvgHZeOO7xpejFsVO/C3/SLRgvCuEi/oAH qFfRXPaWG/A/XpOobGxiVPr94zoKen1IjTZp3kQ79hW1iNKfVOg1Bcoph5X+gup1edQw vgnaFsOeCT3uOQhnuPpFRcpoFiSvTBZ93+U3SV3DACx9/7zcQtGTWGSaPCbeFSNT9+s7 WSXROtCmtXAnAgsJVYcbf00goBwi+LVIBFUty/V+kDWKkweN84QF+yPS6zr2epppWL38 ao44b8xOomqbsbVEQpRbWgiuA8Wkzzry3YYkHzk/pD9OPEEQLFAq+qTKHwROCelprccy KEiQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=HvKO4ewfXjWeXzjMLWOxIwpEQ3iB7K2sJpD/MEFkWq0=; b=Ocgso+UuTQfBBR8iOYlef2/wtf50JJOKgNqiUXkxLc3g3uZNe4s3lxn0s4y+3CTU3x 4px1BWm4vwtmJuboaivdvEPPrroqPDMyDp8A3j0K3JFAuSMdgZyqTszm3kbS0y2ywnBL BZR1hz9XpNrtwAtXzO1ND+c7jnsXeTtDtkFhyLtWvOYXwLZmQw0As9CDh/GvDNrzJIKh kcZt8RlYv88yx7qkcFYWA0xM8FY9a32hErCWQs+7EEKxMORZe35AowC68MEh24fl0CwE S+n9CXe7dpQfa2GodptU/5bDLZJsPMmydMkTGbeEOi+6DN/V4Ru1caFE91Q0FP3mSwWI aPSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=f+Cb1fD2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t21si413963edi.443.2021.05.12.11.22.53; Wed, 12 May 2021 11:23:17 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=f+Cb1fD2; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1354733AbhELSSc (ORCPT + 99 others); Wed, 12 May 2021 14:18:32 -0400 Received: from mail.kernel.org ([198.145.29.99]:42878 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234087AbhELQ2D (ORCPT ); Wed, 12 May 2021 12:28:03 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id DB79961936; Wed, 12 May 2021 15:56:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620834974; bh=s6ooNlwQdXo7YqgkRh3gcTgX+pdykZEMcqpxkxJUl/I=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=f+Cb1fD2dkVWkfF62toMfYtpx+JrJCgnJLsEvvbhJw9HCAQEnIjiqqPpc2iasN1pb ailhTC43TujLX9XuFnx6/E8CoynWUU2biQbaW31xwUCxrPRvIsCCQ81u0pgFmEjOyp XXGPzfxEP1Bu4doqE0zixw5ObhIBQ7wbsS80ZSuM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Erwan Le Ray , Sasha Levin Subject: [PATCH 5.12 159/677] serial: stm32: fix probe and remove order for dma Date: Wed, 12 May 2021 16:43:25 +0200 Message-Id: <20210512144842.531917183@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210512144837.204217980@linuxfoundation.org> References: <20210512144837.204217980@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Erwan Le Ray [ Upstream commit 87fd0741d6dcf63ebdb14050c2b921ae14c7f307 ] The probe and remove orders are wrong as the uart_port is registered before saving device data in the probe, and unregistered after DMA resource deallocation in the remove. uart_port registering should be done at the end of probe and unregistering should be done at the begin of remove to avoid resource allocation issues. Fix probe and remove orders. This enforce resource allocation occur at proper time. Terminate both DMA rx and tx transfers before removing device. Move pm_runtime after uart_remove_one_port() call in remove() to keep the probe error path. Fixes: 3489187204eb ("serial: stm32: adding dma support") Signed-off-by: Erwan Le Ray Link: https://lore.kernel.org/r/20210304162308.8984-2-erwan.leray@foss.st.com Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- drivers/tty/serial/stm32-usart.c | 57 ++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 13 deletions(-) diff --git a/drivers/tty/serial/stm32-usart.c b/drivers/tty/serial/stm32-usart.c index b3675cf25a69..3d58824ac2af 100644 --- a/drivers/tty/serial/stm32-usart.c +++ b/drivers/tty/serial/stm32-usart.c @@ -1252,10 +1252,6 @@ static int stm32_usart_serial_probe(struct platform_device *pdev) device_set_wakeup_enable(&pdev->dev, false); } - ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port); - if (ret) - goto err_wirq; - ret = stm32_usart_of_dma_rx_probe(stm32port, pdev); if (ret) dev_info(&pdev->dev, "interrupt mode used for rx (no dma)\n"); @@ -1269,11 +1265,40 @@ static int stm32_usart_serial_probe(struct platform_device *pdev) pm_runtime_get_noresume(&pdev->dev); pm_runtime_set_active(&pdev->dev); pm_runtime_enable(&pdev->dev); + + ret = uart_add_one_port(&stm32_usart_driver, &stm32port->port); + if (ret) + goto err_port; + pm_runtime_put_sync(&pdev->dev); return 0; -err_wirq: +err_port: + pm_runtime_disable(&pdev->dev); + pm_runtime_set_suspended(&pdev->dev); + pm_runtime_put_noidle(&pdev->dev); + + if (stm32port->rx_ch) { + dmaengine_terminate_async(stm32port->rx_ch); + dma_release_channel(stm32port->rx_ch); + } + + if (stm32port->rx_dma_buf) + dma_free_coherent(&pdev->dev, + RX_BUF_L, stm32port->rx_buf, + stm32port->rx_dma_buf); + + if (stm32port->tx_ch) { + dmaengine_terminate_async(stm32port->tx_ch); + dma_release_channel(stm32port->tx_ch); + } + + if (stm32port->tx_dma_buf) + dma_free_coherent(&pdev->dev, + TX_BUF_L, stm32port->tx_buf, + stm32port->tx_dma_buf); + if (stm32port->wakeirq > 0) dev_pm_clear_wake_irq(&pdev->dev); @@ -1295,11 +1320,20 @@ static int stm32_usart_serial_remove(struct platform_device *pdev) int err; pm_runtime_get_sync(&pdev->dev); + err = uart_remove_one_port(&stm32_usart_driver, port); + if (err) + return(err); + + pm_runtime_disable(&pdev->dev); + pm_runtime_set_suspended(&pdev->dev); + pm_runtime_put_noidle(&pdev->dev); stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAR); - if (stm32_port->rx_ch) + if (stm32_port->rx_ch) { + dmaengine_terminate_async(stm32_port->rx_ch); dma_release_channel(stm32_port->rx_ch); + } if (stm32_port->rx_dma_buf) dma_free_coherent(&pdev->dev, @@ -1308,8 +1342,10 @@ static int stm32_usart_serial_remove(struct platform_device *pdev) stm32_usart_clr_bits(port, ofs->cr3, USART_CR3_DMAT); - if (stm32_port->tx_ch) + if (stm32_port->tx_ch) { + dmaengine_terminate_async(stm32_port->tx_ch); dma_release_channel(stm32_port->tx_ch); + } if (stm32_port->tx_dma_buf) dma_free_coherent(&pdev->dev, @@ -1323,12 +1359,7 @@ static int stm32_usart_serial_remove(struct platform_device *pdev) stm32_usart_deinit_port(stm32_port); - err = uart_remove_one_port(&stm32_usart_driver, port); - - pm_runtime_disable(&pdev->dev); - pm_runtime_put_noidle(&pdev->dev); - - return err; + return 0; } #ifdef CONFIG_SERIAL_STM32_CONSOLE -- 2.30.2