From patchwork Wed Jul 10 08:38:25 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jian-Hong Pan X-Patchwork-Id: 11038327 X-Patchwork-Delegate: kvalo@adurom.com Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A768F14DB for ; Wed, 10 Jul 2019 08:43:50 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 966B328874 for ; Wed, 10 Jul 2019 08:43:50 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 87C97288E6; Wed, 10 Jul 2019 08:43:50 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E54AB28874 for ; Wed, 10 Jul 2019 08:43:49 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727573AbfGJIno (ORCPT ); Wed, 10 Jul 2019 04:43:44 -0400 Received: from mail-pl1-f196.google.com ([209.85.214.196]:35638 "EHLO mail-pl1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727382AbfGJIno (ORCPT ); Wed, 10 Jul 2019 04:43:44 -0400 Received: by mail-pl1-f196.google.com with SMTP id w24so881231plp.2 for ; Wed, 10 Jul 2019 01:43:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=endlessm-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=nkpqbWFq6iVL9u3b/aoSDyMFKVVWjq2ho0SU87Ve9EA=; b=niCiiEw89B6+M1qSVHe0m9LiMfF3eX8mzaVfbsqSLV9SzUHmROxyvUU6zcgi1CqOS6 7yJAHOZI+j+MCieTg2qcgsd/gn7Bok4JlEB6msHfAnbKDf4IYU/h30wpSMGpfykU9yrP pn+UElSBRFX6MdyZa/AGBKyvwEHYO7lENKSVV3URDlGB/HPR4581snqAqNTqaMKblyAq MvD2RvzQL4iEcK/pPB2wU+pS4KiwR/25VmlwvR6lNcqYLuSk7Cmgq7h5PKJvI8e7vvTi 8wpu37jzAij1POC44xa0O2ruCNYqMiJNNtV5FTgOYYJMxdsvUy5VrODWTMXV7yoIqK1r Ofuw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=nkpqbWFq6iVL9u3b/aoSDyMFKVVWjq2ho0SU87Ve9EA=; b=YMb4dKh9MVw8qFFUrQnT+cB1zbXWXBlrRQ8NcIX9OgWuncNCxh25elZTjCT9K5oj0D w9gbKWuxI4JoRNnQZPeg6whqCNXUnAwgZfktjXQxtPdXs2C3jlnwixlG5q+T94MosW0J tvWiqg08HPR7HFususfcLEwYsvPNtiV2eFeNguwuhL5RAA1sqMCqAx1E/f1uU9T/k9NC PjVy3DlydAzehaizAlsxyxBIiX7VHdSYzWdN+ZHJmRV5UEELTSMcenopw46Jw8Ba5fFD uQ3i0g12N2pubNyUI1e1WM/9pJ1QRpc/WzW/ZBfmS8+CBz8rAXZce2bUBZwSvle0cF/Y Zanw== X-Gm-Message-State: APjAAAXFkIhXBUro7XRHIB3vtcMLaZDknftuyA4IQq5gWnuVZ/duSHis 267XJqM+BP08Rhahn421pjVVYg== X-Google-Smtp-Source: APXvYqyGtjVkFZ27qMNV9S8bC9+HxMoJIumIUViBQDClbzBgmvLYf57pAXcmR80wQre9lSTKM/e0Bw== X-Received: by 2002:a17:902:fa2:: with SMTP id 31mr38219781plz.38.1562748223109; Wed, 10 Jul 2019 01:43:43 -0700 (PDT) Received: from localhost.localdomain (123-204-46-122.static.seed.net.tw. [123.204.46.122]) by smtp.gmail.com with ESMTPSA id v5sm1378133pgq.66.2019.07.10.01.43.39 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Wed, 10 Jul 2019 01:43:42 -0700 (PDT) From: Jian-Hong Pan To: Yan-Hsuan Chuang , Kalle Valo , "David S . Miller" , Larry Finger , David Laight , Christoph Hellwig Cc: linux-wireless@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux@endlessm.com, Daniel Drake , Jian-Hong Pan , stable@vger.kernel.org Subject: [PATCH v3 1/2] rtw88: pci: Rearrange the memory usage for skb in RX ISR Date: Wed, 10 Jul 2019 16:38:25 +0800 Message-Id: <20190710083825.7115-1-jian-hong@endlessm.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190709161550.GA8703@infradead.org> References: <20190709161550.GA8703@infradead.org> MIME-Version: 1.0 Sender: linux-wireless-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Testing with RTL8822BE hardware, when available memory is low, we frequently see a kernel panic and system freeze. First, rtw_pci_rx_isr encounters a memory allocation failure (trimmed): rx routine starvation WARNING: CPU: 7 PID: 9871 at drivers/net/wireless/realtek/rtw88/pci.c:822 rtw_pci_rx_isr.constprop.25+0x35a/0x370 [rtwpci] [ 2356.580313] RIP: 0010:rtw_pci_rx_isr.constprop.25+0x35a/0x370 [rtwpci] Then we see a variety of different error conditions and kernel panics, such as this one (trimmed): rtw_pci 0000:02:00.0: pci bus timeout, check dma status skbuff: skb_over_panic: text:00000000091b6e66 len:415 put:415 head:00000000d2880c6f data:000000007a02b1ea tail:0x1df end:0xc0 dev: ------------[ cut here ]------------ kernel BUG at net/core/skbuff.c:105! invalid opcode: 0000 [#1] SMP NOPTI RIP: 0010:skb_panic+0x43/0x45 When skb allocation fails and the "rx routine starvation" is hit, the function returns immediately without updating the RX ring. At this point, the RX ring may continue referencing an old skb which was already handed off to ieee80211_rx_irqsafe(). When it comes to be used again, bad things happen. This patch allocates a new, data-sized skb first in RX ISR. After copying the data in, we pass it to the upper layers. However, if skb allocation fails, we effectively drop the frame. In both cases, the original, full size ring skb is reused. In addition, by fixing the kernel crash, the RX routine should now generally behave better under low memory conditions. Buglink: https://bugzilla.kernel.org/show_bug.cgi?id=204053 Signed-off-by: Jian-Hong Pan Cc: --- v2: - Allocate new data-sized skb and put data into it, then pass it to mac80211. Reuse the original skb in RX ring by DMA sync. - Modify the commit message. - Introduce following [PATCH v3 2/2] rtw88: pci: Use DMA sync instead of remapping in RX ISR. v3: - Same as v2. drivers/net/wireless/realtek/rtw88/pci.c | 49 +++++++++++------------- 1 file changed, 22 insertions(+), 27 deletions(-) diff --git a/drivers/net/wireless/realtek/rtw88/pci.c b/drivers/net/wireless/realtek/rtw88/pci.c index cfe05ba7280d..e9fe3ad896c8 100644 --- a/drivers/net/wireless/realtek/rtw88/pci.c +++ b/drivers/net/wireless/realtek/rtw88/pci.c @@ -763,6 +763,7 @@ static void rtw_pci_rx_isr(struct rtw_dev *rtwdev, struct rtw_pci *rtwpci, u32 pkt_offset; u32 pkt_desc_sz = chip->rx_pkt_desc_sz; u32 buf_desc_sz = chip->rx_buf_desc_sz; + u32 new_len; u8 *rx_desc; dma_addr_t dma; @@ -790,40 +791,34 @@ static void rtw_pci_rx_isr(struct rtw_dev *rtwdev, struct rtw_pci *rtwpci, pkt_offset = pkt_desc_sz + pkt_stat.drv_info_sz + pkt_stat.shift; - if (pkt_stat.is_c2h) { - /* keep rx_desc, halmac needs it */ - skb_put(skb, pkt_stat.pkt_len + pkt_offset); + /* discard current skb if the new skb cannot be allocated as a + * new one in rx ring later + */ + new_len = pkt_stat.pkt_len + pkt_offset; + new = dev_alloc_skb(new_len); + if (WARN_ONCE(!new, "rx routine starvation\n")) + goto next_rp; + + /* put the DMA data including rx_desc from phy to new skb */ + skb_put_data(new, skb->data, new_len); - /* pass offset for further operation */ - *((u32 *)skb->cb) = pkt_offset; - skb_queue_tail(&rtwdev->c2h_queue, skb); + if (pkt_stat.is_c2h) { + /* pass rx_desc & offset for further operation */ + *((u32 *)new->cb) = pkt_offset; + skb_queue_tail(&rtwdev->c2h_queue, new); ieee80211_queue_work(rtwdev->hw, &rtwdev->c2h_work); } else { - /* remove rx_desc, maybe use skb_pull? */ - skb_put(skb, pkt_stat.pkt_len); - skb_reserve(skb, pkt_offset); - - /* alloc a smaller skb to mac80211 */ - new = dev_alloc_skb(pkt_stat.pkt_len); - if (!new) { - new = skb; - } else { - skb_put_data(new, skb->data, skb->len); - dev_kfree_skb_any(skb); - } - /* TODO: merge into rx.c */ - rtw_rx_stats(rtwdev, pkt_stat.vif, skb); + /* remove rx_desc */ + skb_pull(new, pkt_offset); + + rtw_rx_stats(rtwdev, pkt_stat.vif, new); memcpy(new->cb, &rx_status, sizeof(rx_status)); ieee80211_rx_irqsafe(rtwdev->hw, new); } - /* skb delivered to mac80211, alloc a new one in rx ring */ - new = dev_alloc_skb(RTK_PCI_RX_BUF_SIZE); - if (WARN(!new, "rx routine starvation\n")) - return; - - ring->buf[cur_rp] = new; - rtw_pci_reset_rx_desc(rtwdev, new, ring, cur_rp, buf_desc_sz); +next_rp: + /* new skb delivered to mac80211, re-enable original skb DMA */ + rtw_pci_reset_rx_desc(rtwdev, skb, ring, cur_rp, buf_desc_sz); /* host read next element in ring */ if (++cur_rp >= ring->r.len) From patchwork Wed Jul 10 08:38:27 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jian-Hong Pan X-Patchwork-Id: 11038329 X-Patchwork-Delegate: kvalo@adurom.com Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id C9EF014DB for ; Wed, 10 Jul 2019 08:44:20 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B9E1F28874 for ; Wed, 10 Jul 2019 08:44:20 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id ACFF5288E6; Wed, 10 Jul 2019 08:44:20 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,MAILING_LIST_MULTI,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2FDCC28874 for ; Wed, 10 Jul 2019 08:44:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727591AbfGJIoP (ORCPT ); Wed, 10 Jul 2019 04:44:15 -0400 Received: from mail-pf1-f195.google.com ([209.85.210.195]:44492 "EHLO mail-pf1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726580AbfGJIoP (ORCPT ); Wed, 10 Jul 2019 04:44:15 -0400 Received: by mail-pf1-f195.google.com with SMTP id t16so755237pfe.11 for ; Wed, 10 Jul 2019 01:44:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=endlessm-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=ZxB6K90WEOCOsXFUiDbe1sddkLrl4+FMawAXBSkTyfs=; b=Yv02FiX2qw7jZOKTrsoiIMkDDSP78zsxI2V+pkdLGpN8sQ2V9lYpCBTujbjLOd4+XX cDHNZGrU/OY6+FRbsNHKPd9Pi/5iY4BUy8RRb4v/YuXTqTo96k3IM3BvQBPijRhv5ykC 3M8lrau2R5HxxkFPUp0vn7kIiIuFkU0VmWVtqF8NQ8810MvhOKhoHVeynE63w6hpwNtX zqpv0eFQplEjHM793bOPiK6GZZjpPSpLCWoz9X38kBQ8V6Pt15H4jijSHphDEN+Qtimw aurOy9tnyBXRUbgisvNaPCNVSUb0EOgeJPbdyErfH7PHrnO69wdgHiS3AqX/zHyZLQcm S23w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=ZxB6K90WEOCOsXFUiDbe1sddkLrl4+FMawAXBSkTyfs=; b=We30GlltNYb1gfe2fj+MCaOwr/7wLhuK4lIaLwRDnGmpH2SkwCCLk41+i9oOctAFey Ar0rjThJoFy/5rqQjBBbHDFRKW8FIUp4VKm9VCoCLL9YnZZCr/oeb9QD2GLcfMCx2+9C aTN/eQFmDkwbi26sEx7uSy13CLBYxCLPpDBiDVqF9xANl5DiDE9hqLB+I9F36G0zTfHW Rp/qbaMNiUw8jkc7z3q9UrkZ4HyQ64qR0VaEs3or91wuKStAdwK0c9kIvzMSIuaSioi3 hqoZftyEHRowLgaue6KLQdcRpASoFzHWOc7iPKAMJB/2suJwkuq9pEZYfdrNyr6z4zF2 2VoQ== X-Gm-Message-State: APjAAAUUhIWEpMhuJdB9ZT13EGXmAauTMchZRHQWm8MJ1y+WSpYaXb3v 19d/cfirX1zshYuW2kLAcg0EyQ== X-Google-Smtp-Source: APXvYqx6r3nK/XJfM+4IfR4WIlijQtiFKtASr2UXeXiYiziTXmlsNyQAr1EFL/V112AK8JqshhpfCw== X-Received: by 2002:a63:7d05:: with SMTP id y5mr32971869pgc.425.1562748254525; Wed, 10 Jul 2019 01:44:14 -0700 (PDT) Received: from localhost.localdomain (123-204-46-122.static.seed.net.tw. [123.204.46.122]) by smtp.gmail.com with ESMTPSA id v5sm1378133pgq.66.2019.07.10.01.44.10 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Wed, 10 Jul 2019 01:44:14 -0700 (PDT) From: Jian-Hong Pan To: Yan-Hsuan Chuang , Kalle Valo , "David S . Miller" , Larry Finger , David Laight , Christoph Hellwig Cc: linux-wireless@vger.kernel.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, linux@endlessm.com, Daniel Drake , Jian-Hong Pan , stable@vger.kernel.org Subject: [PATCH v3 2/2] rtw88: pci: Use DMA sync instead of remapping in RX ISR Date: Wed, 10 Jul 2019 16:38:27 +0800 Message-Id: <20190710083825.7115-2-jian-hong@endlessm.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190709161550.GA8703@infradead.org> References: <20190709161550.GA8703@infradead.org> MIME-Version: 1.0 Sender: linux-wireless-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Since each skb in RX ring is reused instead of new allocation, we can treat the DMA in a more efficient way by DMA synchronization. Signed-off-by: Jian-Hong Pan Cc: --- v2: - New patch by following [PATCH v3 1/2] rtw88: pci: Rearrange the memory usage for skb in RX ISR. v3: - Remove rtw_pci_sync_rx_desc_cpu and call dma_sync_single_for_cpu in rtw_pci_rx_isr directly. - Remove the return value of rtw_pci_sync_rx_desc_device. - Use DMA_FROM_DEVICE instead of PCI_DMA_FROMDEVICE. drivers/net/wireless/realtek/rtw88/pci.c | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/drivers/net/wireless/realtek/rtw88/pci.c b/drivers/net/wireless/realtek/rtw88/pci.c index e9fe3ad896c8..485d30c06935 100644 --- a/drivers/net/wireless/realtek/rtw88/pci.c +++ b/drivers/net/wireless/realtek/rtw88/pci.c @@ -206,6 +206,23 @@ static int rtw_pci_reset_rx_desc(struct rtw_dev *rtwdev, struct sk_buff *skb, return 0; } +static void rtw_pci_sync_rx_desc_device(struct rtw_dev *rtwdev, dma_addr_t dma, + struct rtw_pci_rx_ring *rx_ring, + u32 idx, u32 desc_sz) +{ + struct device *dev = rtwdev->dev; + struct rtw_pci_rx_buffer_desc *buf_desc; + int buf_sz = RTK_PCI_RX_BUF_SIZE; + + dma_sync_single_for_device(dev, dma, buf_sz, DMA_FROM_DEVICE); + + buf_desc = (struct rtw_pci_rx_buffer_desc *)(rx_ring->r.head + + idx * desc_sz); + memset(buf_desc, 0, sizeof(*buf_desc)); + buf_desc->buf_size = cpu_to_le16(RTK_PCI_RX_BUF_SIZE); + buf_desc->dma = cpu_to_le32(dma); +} + static int rtw_pci_init_rx_ring(struct rtw_dev *rtwdev, struct rtw_pci_rx_ring *rx_ring, u8 desc_size, u32 len) @@ -782,8 +799,8 @@ static void rtw_pci_rx_isr(struct rtw_dev *rtwdev, struct rtw_pci *rtwpci, rtw_pci_dma_check(rtwdev, ring, cur_rp); skb = ring->buf[cur_rp]; dma = *((dma_addr_t *)skb->cb); - pci_unmap_single(rtwpci->pdev, dma, RTK_PCI_RX_BUF_SIZE, - PCI_DMA_FROMDEVICE); + dma_sync_single_for_cpu(rtwdev->dev, dma, RTK_PCI_RX_BUF_SIZE, + DMA_FROM_DEVICE); rx_desc = skb->data; chip->ops->query_rx_desc(rtwdev, rx_desc, &pkt_stat, &rx_status); @@ -818,7 +835,8 @@ static void rtw_pci_rx_isr(struct rtw_dev *rtwdev, struct rtw_pci *rtwpci, next_rp: /* new skb delivered to mac80211, re-enable original skb DMA */ - rtw_pci_reset_rx_desc(rtwdev, skb, ring, cur_rp, buf_desc_sz); + rtw_pci_sync_rx_desc_device(rtwdev, dma, ring, cur_rp, + buf_desc_sz); /* host read next element in ring */ if (++cur_rp >= ring->r.len)