Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp6552667ybi; Mon, 8 Jul 2019 04:57:06 -0700 (PDT) X-Google-Smtp-Source: APXvYqyDeRNadn9vo/zK4Wc1JypSy7ipsjmfOaEiGLi3CZUzm7jHdUr8DCgQCDDsVQcMLVkcSTLB X-Received: by 2002:a17:90a:ad86:: with SMTP id s6mr25032445pjq.42.1562587025976; Mon, 08 Jul 2019 04:57:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562587025; cv=none; d=google.com; s=arc-20160816; b=GJQ5+6JgmIkuzwAU10tSdENgzkSkIPnSbNT2Qt6J6e9Pe1up1vbLx1vc3So0Xnnp6+ RpZM+EPi3rLBWM0VIy5jf4b1gTQdxTg0qp/ZCEEpiV+clWwAfjc7cxQaSN7+WsMCC3rp D6mpVx6DP3d3sT9/0uEsuSsbUS287jcQoZh9fgQDUrt8qqkl7kWN6ktvf+Nqfc0PnXO3 /iKt2ixkQoDKk+3ZJ9P0i3OcJLc63cN1y6qWTZDp/JrxT+LMdrjYZa4veG6ULDDU9chP tbRCHRp5+aHUOhQ+eU1pExOhNVJNKGPR1vDBGBFToE+K+b1FijbyWynSKTqECgs1Cj45 XKnw== 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 :content-language:accept-language:in-reply-to:references:message-id :date:thread-index:thread-topic:subject:cc:to:from; bh=1WC2xELW3WMRfQtoQzg79gs/GgpZjFAjAOPf7wU4lwI=; b=akKYS318OV/vrev5irCzd3NBLFq8M5lu/ZFj6ygJmFnIN5wNB/TQI47UmMwM6IZGes DVRst6lugvh/NBamErdrYRyAkNzLHJIeHYXztWx9MlVANyMdUTydPbqZduzpeDQlhnaj 6ohqj/lJLJqrb1Uege0ky5+TixtXeKy3AyhVs4cl1NEMKOj2sEPmT8TtzlPmvMcKBxBl Zny1npVOaBjyFMYUlmOO+6p8YnKKwF8pbBxuqLLe/L+ZR6+xHTdry95uBkK+ViSJTpD6 bLlHqJjZ7gOGmA94wZaeipS2a8pIvgv004QWzlbbdAGyATJH1+/FWI13iVtkhbn2ef5O UBfA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-wireless-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-wireless-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 65si18798937ple.240.2019.07.08.04.56.37; Mon, 08 Jul 2019 04:57:05 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-wireless-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-wireless-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729454AbfGHIg6 convert rfc822-to-8bit (ORCPT + 99 others); Mon, 8 Jul 2019 04:36:58 -0400 Received: from eu-smtp-delivery-151.mimecast.com ([207.82.80.151]:38254 "EHLO eu-smtp-delivery-151.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729445AbfGHIg6 (ORCPT ); Mon, 8 Jul 2019 04:36:58 -0400 Received: from AcuMS.aculab.com (156.67.243.126 [156.67.243.126]) (Using TLS) by relay.mimecast.com with ESMTP id uk-mta-58-GxZAHFndPNeufvGAW7tiGg-1; Mon, 08 Jul 2019 09:36:53 +0100 Received: from AcuMS.Aculab.com (fd9f:af1c:a25b:0:43c:695e:880f:8750) by AcuMS.aculab.com (fd9f:af1c:a25b:0:43c:695e:880f:8750) with Microsoft SMTP Server (TLS) id 15.0.1347.2; Mon, 8 Jul 2019 09:36:52 +0100 Received: from AcuMS.Aculab.com ([fe80::43c:695e:880f:8750]) by AcuMS.aculab.com ([fe80::43c:695e:880f:8750%12]) with mapi id 15.00.1347.000; Mon, 8 Jul 2019 09:36:52 +0100 From: David Laight To: 'Jian-Hong Pan' , Yan-Hsuan Chuang , Kalle Valo , "David S . Miller" CC: "linux-wireless@vger.kernel.org" , "netdev@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "linux@endlessm.com" , Daniel Drake , "stable@vger.kernel.org" Subject: RE: [PATCH] rtw88/pci: Rearrange the memory usage for skb in RX ISR Thread-Topic: [PATCH] rtw88/pci: Rearrange the memory usage for skb in RX ISR Thread-Index: AQHVNVeEF2VH1aWcW0SX/aCCs+qgc6bAY9qw Date: Mon, 8 Jul 2019 08:36:52 +0000 Message-ID: References: <20190708063252.4756-1-jian-hong@endlessm.com> In-Reply-To: <20190708063252.4756-1-jian-hong@endlessm.com> Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-ms-exchange-transport-fromentityheader: Hosted x-originating-ip: [10.202.205.107] MIME-Version: 1.0 X-MC-Unique: GxZAHFndPNeufvGAW7tiGg-1 X-Mimecast-Spam-Score: 0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8BIT Sender: linux-wireless-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org From: Jian-Hong Pan > Sent: 08 July 2019 07:33 > To: Yan-Hsuan Chuang; Kalle Valo; David S . Miller > > 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 skb first in RX ISR. If we don't have memory > available, we discard the current frame, allowing the existing skb to be > reused in the ring. Otherwise, we simplify the code flow and just hand > over the RX-populated skb over to mac80211. > > In addition, to fixing the kernel crash, the RX routine should now > generally behave better under low memory conditions. Under low memory conditions it may be preferable to limit the amount of memory assigned to the receive ring. I also thought it was preferable (DM may correct me here) to do the skb allocates from the 'bh' of the driver rather than from the hardware interrupt. It is also almost certainly preferable (especially on IOMMU systems) to copy small frames into a new skb (of the right size) and then reuse the skb (with its dma-mapped buffer) for a later frame. Allocating a new skb before ay px processing just seems wrong... David > Buglink: https://bugzilla.kernel.org/show_bug.cgi?id=204053 > Signed-off-by: Jian-Hong Pan > Reviewed-by: Daniel Drake > Cc: > --- > drivers/net/wireless/realtek/rtw88/pci.c | 28 +++++++++++------------- > 1 file changed, 13 insertions(+), 15 deletions(-) > > diff --git a/drivers/net/wireless/realtek/rtw88/pci.c b/drivers/net/wireless/realtek/rtw88/pci.c > index cfe05ba7280d..1bfc99ae6b84 100644 > --- a/drivers/net/wireless/realtek/rtw88/pci.c > +++ b/drivers/net/wireless/realtek/rtw88/pci.c > @@ -786,6 +786,15 @@ static void rtw_pci_rx_isr(struct rtw_dev *rtwdev, struct rtw_pci *rtwpci, > rx_desc = skb->data; > chip->ops->query_rx_desc(rtwdev, rx_desc, &pkt_stat, &rx_status); > > + /* discard current skb if the new skb cannot be allocated as a > + * new one in rx ring later > + * */ > + new = dev_alloc_skb(RTK_PCI_RX_BUF_SIZE); > + if (WARN(!new, "rx routine starvation\n")) { > + new = skb; > + goto next_rp; > + } > + > /* offset from rx_desc to payload */ > pkt_offset = pkt_desc_sz + pkt_stat.drv_info_sz + > pkt_stat.shift; > @@ -803,25 +812,14 @@ static void rtw_pci_rx_isr(struct rtw_dev *rtwdev, struct rtw_pci *rtwpci, > 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); > - memcpy(new->cb, &rx_status, sizeof(rx_status)); > - ieee80211_rx_irqsafe(rtwdev->hw, new); > + memcpy(skb->cb, &rx_status, sizeof(rx_status)); > + ieee80211_rx_irqsafe(rtwdev->hw, skb); > } > > - /* 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; > - > +next_rp: > + /* skb delivered to mac80211, attach the new one into rx ring */ > ring->buf[cur_rp] = new; > rtw_pci_reset_rx_desc(rtwdev, new, ring, cur_rp, buf_desc_sz); > > -- > 2.22.0 - Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)