From patchwork Thu Jul 11 05:24:26 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: 11039275 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 453646C5 for ; Thu, 11 Jul 2019 05:25:14 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 30F1A28A60 for ; Thu, 11 Jul 2019 05:25:14 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 1EDF628A62; Thu, 11 Jul 2019 05:25:14 +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 DF4B328A60 for ; Thu, 11 Jul 2019 05:25:12 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727890AbfGKFZH (ORCPT ); Thu, 11 Jul 2019 01:25:07 -0400 Received: from mail-pl1-f194.google.com ([209.85.214.194]:46432 "EHLO mail-pl1-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726290AbfGKFZH (ORCPT ); Thu, 11 Jul 2019 01:25:07 -0400 Received: by mail-pl1-f194.google.com with SMTP id c2so2371951plz.13 for ; Wed, 10 Jul 2019 22:25:06 -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=iAzKw0BoNuWYvRIzrLeTPP6dDrFST9kyO0Xv/W2qtaU=; b=E/e3PBgQ9/O1mYNDctMgvQ9JP5Ck/4I3S80VZF4toBAPiVDAgrviyTwC2sZQvNSJ+8 rqBc9CSd+ScNVhuNdBxOzkoP/GY7op2PfRpfumWFKvyNfYd+2WJZFsK7sB3z1cURxgl0 uEmaJE4HpYFWhom5pmmYwT/Fd/cHzso1rmGL9J7mv2hpFfeSGo+ncCGrCLjqDp8psUAf cuowLO5rLG0RFtKJHWOX4VAHvMNRblY7QbYQ7YtbKAHNhPg1o5zod0LM8I8oy3PixRg8 2YGfEGPhDll9EPsFbfAp2CIDgqoluJP0sp/xyDLe93LooAdg+y7uAqGK0s6oQgU+Mvjk r8nw== 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=iAzKw0BoNuWYvRIzrLeTPP6dDrFST9kyO0Xv/W2qtaU=; b=CDsJWMeRWy22GwTYuVfH0xCYDbmJAMVGRSrZzTtEYBkfSTQHd9JSJGSdn9Jiwe/Esg OZmvA6DVj20fHGHpJEpwVzxvdG6wi068TVOX1OWph7l1+8sfPmk+FqpSlIZb6Zopg+AN 2BHGmY1XrYlMSoYfb2MYlE4j9lOP6xTNjHwxsr9KDVFwVceTlhLGivhQMRiZerRRmYN0 Fj988kXFf07DV31J4W142JHHrZVxcTWcaad1gwcYkQlVqMnvZLwYnn6B6DxCNTFUIvAn 6FFIMgggu3NAc751dzgIhKSsHVSqxNZ9i56UEtPRFFDPIrBM488GZXRJdSgsZQC2S1ds HaAw== X-Gm-Message-State: APjAAAUkMRUYwyu/Bl84TCMC+kcpy/8GZ0hGKsIcRPVVV+wSOFVPQtlR ud08SqEpfb8aXOcG7APJvPWtoA== X-Google-Smtp-Source: APXvYqxGCI9u1oZOuUKhbEq8FgFw3q63JFEadaGNOb5TRxahHVBCraKfOKWLZ0+otd5GC8em7POraQ== X-Received: by 2002:a17:902:b68f:: with SMTP id c15mr2535175pls.104.1562822705862; Wed, 10 Jul 2019 22:25:05 -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 201sm5471345pfz.24.2019.07.10.22.25.02 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Wed, 10 Jul 2019 22:25:05 -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 v4 1/2] rtw88: pci: Rearrange the memory usage for skb in RX ISR Date: Thu, 11 Jul 2019 13:24:26 +0800 Message-Id: <20190711052427.5582-1-jian-hong@endlessm.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: References: 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, to 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: --- 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..c415f5e94fed 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); + /* allocate a new skb for this frame, + * discard the frame if none available + */ + 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 Thu Jul 11 05:24: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: 11039277 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 B275F138B for ; Thu, 11 Jul 2019 05:25:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A28FA28A60 for ; Thu, 11 Jul 2019 05:25:18 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 96AA928A62; Thu, 11 Jul 2019 05:25:18 +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 3A3D428A60 for ; Thu, 11 Jul 2019 05:25:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728008AbfGKFZO (ORCPT ); Thu, 11 Jul 2019 01:25:14 -0400 Received: from mail-pl1-f195.google.com ([209.85.214.195]:43863 "EHLO mail-pl1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727973AbfGKFZN (ORCPT ); Thu, 11 Jul 2019 01:25:13 -0400 Received: by mail-pl1-f195.google.com with SMTP id cl9so2377243plb.10 for ; Wed, 10 Jul 2019 22:25:13 -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=YnDj7HeMgqllbVcG4VtSZ86mS/XHDBUJrdAevBr6spA=; b=B9qYrQ9m+H4aMMhUv91/t0G9wD/DK3Li0yqRGwJJ+UpboBpDdEb/OMjnIq8d1cDGQw eC9gbnCRyABGHd0Nyj4WgZQSB+NAQEjqJS1HmVt7DNcLC73PKAwqDydhdjc2waT8rUnR CzWyxbnrGZLHtsyGntaxLTY36rX9uuL2C3WhPIQ7xygdqmgyUXt5AxzIzQSAy1YDnKw+ DCc/DFqLMS8lKtjuq/aWaXdlc+C3JugnUptgm9v4gfVdtrE1HqIiSiXyKpeyegW6MMGC 0REKQEO0uD0w3qoIARZZE4eDmPWQba0yTOZIc6ZHCw0mwNFXCETTw/Trs2xd0VUuqx1u bxyg== 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=YnDj7HeMgqllbVcG4VtSZ86mS/XHDBUJrdAevBr6spA=; b=BVrR9/cGBTH4Ko/gvKh4YcSysQ3OPvp3fVFYp67OIy8Zg+EGx2SDbVWRbXsDVFeAlA nty80GmrCMFeYSARoNFzsUqNFlCgc6UNRIa+GnRLrcSbMFKJLbByFxf7Jb8Rw01KdCB1 1XI9K/owK0AiuLxt+dOnozX3r/fkYOm3Hm7aCQNeFtfBhQFw6KQy9SVnwTuv4HPYrqyK 1ZMLCLHf9uzLE4SjQ6fUI1feBupGJ1/pYbYxrqcBHYTlbZ9FagFD/4V1maIxBhdSTdVK mBjIgHLMWWNOeOJNtxzq2cnpwbSVzVB85ITSjIPF4AmVbi9KWF245krlW5QT0NbBkRCt zN9A== X-Gm-Message-State: APjAAAXCk2Jb1Snm+BxvscnD/+3JKNGkLO9fqut9ppL0i45vSv0lXLN0 TIssZoV96g3WWh297J+kRTEC/Q== X-Google-Smtp-Source: APXvYqzKeDK8OkxK6szumbrprfqFg7I8okB/dx+QucnEs398OraeKe4q3ipZIzcp/fCFEx7KY8LVEQ== X-Received: by 2002:a17:902:6847:: with SMTP id f7mr2397704pln.311.1562822712599; Wed, 10 Jul 2019 22:25:12 -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 201sm5471345pfz.24.2019.07.10.22.25.09 (version=TLS1_3 cipher=AEAD-AES256-GCM-SHA384 bits=256/256); Wed, 10 Jul 2019 22:25:12 -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 v4 2/2] rtw88: pci: Use DMA sync instead of remapping in RX ISR Date: Thu, 11 Jul 2019 13:24:27 +0800 Message-Id: <20190711052427.5582-2-jian-hong@endlessm.com> X-Mailer: git-send-email 2.22.0 In-Reply-To: <20190711052427.5582-1-jian-hong@endlessm.com> References: <20190711052427.5582-1-jian-hong@endlessm.com> 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: --- 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 c415f5e94fed..68fae52151dd 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)