From patchwork Mon Oct 21 06:10:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Furong Xu <0x1207@gmail.com> X-Patchwork-Id: 13843600 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id ED066D3C93F for ; Mon, 21 Oct 2024 06:13:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:Message-Id:Date:Subject:Cc:To:From:Reply-To:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:In-Reply-To:References:List-Owner; bh=iQESqJ9aYfd46Wdo01VIZ5qOywZL6bQHldy6wnuTUU8=; b=P/jC6GRTzVtMjyyTrwvIW4DyX5 RsHkT76+y+WpRqrYAhKUdXqe+b7FnQOCHsKETJ2RsfDwgYz56HA6r8Af1+XrM67qPBkzz34cq7+vh jWCgUZcfCMkhCRW8T6Em/NCegOOj+f9WPNW1L3VM04knePejZKgOrGTM0JQQBfG6lq4/fKycsPv1G 98ZfHBxVnnfJ2XpVcpeZwtuq2EMRnYwK449nw4U/g7ByZWIhvF0yWH4rYs2IK608Q02IKikFhYJsm SoLIDc09dm2LOAnkTdRuyqww6F0W1FlzQielC20GWQzH5Aas8RJjoMiUenjVX8XtX2j8fgBBhu3j/ loZC6CdQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98 #2 (Red Hat Linux)) id 1t2lfO-00000006BNb-1pWj; Mon, 21 Oct 2024 06:13:22 +0000 Received: from mail-pf1-x432.google.com ([2607:f8b0:4864:20::432]) by bombadil.infradead.org with esmtps (Exim 4.98 #2 (Red Hat Linux)) id 1t2lcr-00000006B02-0XU0 for linux-arm-kernel@lists.infradead.org; Mon, 21 Oct 2024 06:10:49 +0000 Received: by mail-pf1-x432.google.com with SMTP id d2e1a72fcca58-71e61b47c6cso3199836b3a.2 for ; Sun, 20 Oct 2024 23:10:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1729491044; x=1730095844; darn=lists.infradead.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=iQESqJ9aYfd46Wdo01VIZ5qOywZL6bQHldy6wnuTUU8=; b=ZMuAHckws9OM8OnHhJnLesuBhfe/lWfovglRyDKQtV8u2Rk225GLbdUPP7C2BFyygf zZf6uIFrGLVbfLdw4gKthYXfCAkV8vvbOKGzaKHYq7ZNwsGYJqjbtuICcA/84tkuOhuc kXjbCQwDQ++wbx8kxUcwWYiClOsK3KFbwcQMChc1kkzFrdlDI5aOscXFQ4VvofSEJvEg tJcAPRQiJaGwlHiwurE9dXEU0KCcDZ+gAA7IGpFR1udl9pKU2oGb0YCCF35h4Z23r2Is h3FVvXloZQ7GM01n8nQZUosbYJUTN0biorR5CYhfiFu299K2y+xiwgIPpA/MEMeasb66 FZSA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1729491044; x=1730095844; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=iQESqJ9aYfd46Wdo01VIZ5qOywZL6bQHldy6wnuTUU8=; b=WQ5hMUVIBEtIZif4mzdOAhVyVI3S4wt+qS9706e74fmO6CREHdoSu2/RUh/pGtLr6W n1IdYWFc9EoxzF/u3fa95C0DrzPXXwQ/YgplnV15CM0PQUC3NnUKYrVR/PxRV/qgJHK9 1rPsTwwsvzCuC9LHTIFEWvZfN+g2zygpLbqbfP2Qti45cYkX3XflvzyfE4uTCQYo1nWX TVQV5l0wmU46JVWAVmQz9mI1mHYnCwVppqXbuCNE/hpoqCyJwm8Y2BYp4y8m3wvo0OqK yQ2yNNA/dMwe/77v1ST/mN3Tt9olYtjvr/iaeig5DB2GXMwmD20PLoT8T92gV+Fvv1Ve kDNw== X-Forwarded-Encrypted: i=1; AJvYcCW6sHVq3kjpzTaqhc5DxRtJLHSbHZnAZ+Qun3oTEYneIB8Wo/6z7ITKeBc7sxI+wd0TM+GC9NPJVIwvZhCpGoHd@lists.infradead.org X-Gm-Message-State: AOJu0YyBG9gGtNN3O/5iY6LVXfhqZs0/X7VBkQd/4k4kagFzHcEN1r3l XJ63PmlhWp7S6TixzoiQ0G9f5RXnY+rEbW4q3jo/rm5BeTGxtMEo X-Google-Smtp-Source: AGHT+IFfcM3mmxhkbfvMBoyB5bFdlWr6KbbE9WM+AEcGbqCxvjfTNgq2Enq1ESlcFgxrfwc/MvRFcQ== X-Received: by 2002:a05:6a00:3d15:b0:71e:79a9:ec47 with SMTP id d2e1a72fcca58-71ea3118addmr16050357b3a.6.1729491043923; Sun, 20 Oct 2024 23:10:43 -0700 (PDT) Received: from localhost.localdomain ([129.146.253.192]) by smtp.googlemail.com with ESMTPSA id d2e1a72fcca58-71ec13d7422sm2145735b3a.97.2024.10.20.23.10.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 20 Oct 2024 23:10:43 -0700 (PDT) From: Furong Xu <0x1207@gmail.com> To: netdev@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Cc: Alexandre Torgue , Jose Abreu , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Maxime Coquelin , xfr@outlook.com, Furong Xu <0x1207@gmail.com>, Suraj Jaiswal Subject: [PATCH net v1] net: stmmac: TSO: Fix unbalanced DMA map/unmap for non-paged SKB data Date: Mon, 21 Oct 2024 14:10:23 +0800 Message-Id: <20241021061023.2162701-1-0x1207@gmail.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20241020_231048_404995_1D6385A2 X-CRM114-Status: GOOD ( 22.89 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org In case the non-paged data of a SKB carries protocol header and protocol payload to be transmitted on a certain platform that the DMA AXI address width is configured to 40-bit/48-bit, or the size of the non-paged data is bigger than TSO_MAX_BUFF_SIZE on a certain platform that the DMA AXI address width is configured to 32-bit, then this SKB requires at least two DMA transmit descriptors to serve it. For example, three descriptors are allocated to split one DMA buffer mapped from one piece of non-paged data: dma_desc[N + 0], dma_desc[N + 1], dma_desc[N + 2]. Then three elements of tx_q->tx_skbuff_dma[] will be allocated to hold extra information to be reused in stmmac_tx_clean(): tx_q->tx_skbuff_dma[N + 0], tx_q->tx_skbuff_dma[N + 1], tx_q->tx_skbuff_dma[N + 2]. Now we focus on tx_q->tx_skbuff_dma[entry].buf, which is the DMA buffer address returned by DMA mapping call. stmmac_tx_clean() will try to unmap the DMA buffer _ONLY_IF_ tx_q->tx_skbuff_dma[entry].buf is a valid buffer address. The expected behavior that saves DMA buffer address of this non-paged data to tx_q->tx_skbuff_dma[entry].buf is: tx_q->tx_skbuff_dma[N + 0].buf = NULL; tx_q->tx_skbuff_dma[N + 1].buf = NULL; tx_q->tx_skbuff_dma[N + 2].buf = dma_map_single(); Unfortunately, the current code misbehaves like this: tx_q->tx_skbuff_dma[N + 0].buf = dma_map_single(); tx_q->tx_skbuff_dma[N + 1].buf = NULL; tx_q->tx_skbuff_dma[N + 2].buf = NULL; On the stmmac_tx_clean() side, when dma_desc[N + 0] is closed by the DMA engine, tx_q->tx_skbuff_dma[N + 0].buf is a valid buffer address obviously, then the DMA buffer will be unmapped immediately. There may be a rare case that the DMA engine does not finish the pending dma_desc[N + 1], dma_desc[N + 2] yet. Now things will go horribly wrong, DMA is going to access a unmapped/unreferenced memory region, corrupted data will be transmited or iommu fault will be triggered :( In contrast, the for-loop that maps SKB fragments behaves perfectly as expected, and that is how the driver should do for both non-paged data and paged frags actually. This patch corrects DMA map/unmap sequences by fixing the array index for tx_q->tx_skbuff_dma[entry].buf when assigning DMA buffer address. Tested and verified on DWXGMAC CORE 3.20a Reported-by: Suraj Jaiswal Fixes: f748be531d70 ("stmmac: support new GMAC4") Signed-off-by: Furong Xu <0x1207@gmail.com> Reviewed-by: Hariprasad Kelam Reviewed-by: Simon Horman --- .../net/ethernet/stmicro/stmmac/stmmac_main.c | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c index d3895d7eecfc..208dbc68aaf9 100644 --- a/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c +++ b/drivers/net/ethernet/stmicro/stmmac/stmmac_main.c @@ -4304,11 +4304,6 @@ static netdev_tx_t stmmac_tso_xmit(struct sk_buff *skb, struct net_device *dev) if (dma_mapping_error(priv->device, des)) goto dma_map_err; - tx_q->tx_skbuff_dma[first_entry].buf = des; - tx_q->tx_skbuff_dma[first_entry].len = skb_headlen(skb); - tx_q->tx_skbuff_dma[first_entry].map_as_page = false; - tx_q->tx_skbuff_dma[first_entry].buf_type = STMMAC_TXBUF_T_SKB; - if (priv->dma_cap.addr64 <= 32) { first->des0 = cpu_to_le32(des); @@ -4327,6 +4322,23 @@ static netdev_tx_t stmmac_tso_xmit(struct sk_buff *skb, struct net_device *dev) stmmac_tso_allocator(priv, des, tmp_pay_len, (nfrags == 0), queue); + /* In case two or more DMA transmit descriptors are allocated for this + * non-paged SKB data, the DMA buffer address should be saved to + * tx_q->tx_skbuff_dma[].buf corresponding to the last descriptor, + * and leave the other tx_q->tx_skbuff_dma[].buf as NULL to guarantee + * that stmmac_tx_clean() does not unmap the entire DMA buffer too early + * since the tail areas of the DMA buffer can be accessed by DMA engine + * sooner or later. + * By saving the DMA buffer address to tx_q->tx_skbuff_dma[].buf + * corresponding to the last descriptor, stmmac_tx_clean() will unmap + * this DMA buffer right after the DMA engine completely finishes the + * full buffer transmission. + */ + tx_q->tx_skbuff_dma[tx_q->cur_tx].buf = des; + tx_q->tx_skbuff_dma[tx_q->cur_tx].len = skb_headlen(skb); + tx_q->tx_skbuff_dma[tx_q->cur_tx].map_as_page = false; + tx_q->tx_skbuff_dma[tx_q->cur_tx].buf_type = STMMAC_TXBUF_T_SKB; + /* Prepare fragments */ for (i = 0; i < nfrags; i++) { const skb_frag_t *frag = &skb_shinfo(skb)->frags[i];