Received: by 2002:a05:6358:f14:b0:e5:3b68:ec04 with SMTP id b20csp5434102rwj; Wed, 21 Dec 2022 02:35:57 -0800 (PST) X-Google-Smtp-Source: AMrXdXu5t/50DFov+0ein9rIToSPafLWh6R9ttFEY6FJv/9kI8J61KNyfbuAmah1NwFxUO63iqNH X-Received: by 2002:a17:906:6693:b0:7c1:8ba7:3182 with SMTP id z19-20020a170906669300b007c18ba73182mr1135239ejo.69.1671618957341; Wed, 21 Dec 2022 02:35:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1671618957; cv=none; d=google.com; s=arc-20160816; b=vg5AdJZ6dp4+E6Ii53JGk7E9qk51LRFFtSNizH4lW9F1hrpVYOr3A6jVmdZTjTms+1 uEgKwsk09LzsUySe7hm2Qz9vYAUUDqEM5t+JEu9flhkVcV+z4CZsIOO2NBDwDeWk/IgR b7o1Oc1180SZm3e19Y+/nnaioROiXPtQePelJEXFaU75gcxfQdjY5/WNf3EnIIXNq2pv 0y0oKUE2zhjRZuRtbEQqIG/eNkTYgspZhrnqn8byXFys513Z/0+rvBgilSfeeNV5zRzv w1ILX1pynvN54buFiqf5wrYO9YQrvfMV0YR5oTus3vkiKCRiEsJw54DVGnSpDN2yeNS9 gQbg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=gZ/m2nv2wFuD8e/xXaSO7CKmp+3A0bAq1k75FNHJdTU=; b=ey/OIuFVgfKZdxKmri9L9WCuIjR+50Le0AXOvcy3NoXbTkj8bpuGtmBsY9hqmZ99sw qVodhXoxCBSrG/XjU0AZkDCiSXwXEczIzO4Pbz3ooGv7ZTTERkzfQM6awz8O0Pwrf2xy U0Yu/zYx6yKKMwK+r6dwIT7DqydJTSchYeJleF3iRItH8TNCPrkajqA+BTwx4eCsFuCS QP4CWhJ1HCwWZJcloIcg3BNcxjYkrFOTryXcGj42wpkl2CTqIzmWRMghOY8V9i58KtVt 3PcMq/G6IDvoAZIp85jB7+vhDkrbp79JBehwAkcDyf49pvICASoCUCb73EmMH+m9Ow1y NrfQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Jf6cVpE5; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id sg43-20020a170907a42b00b0080de94d6280si2323260ejc.311.2022.12.21.02.35.34; Wed, 21 Dec 2022 02:35:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=Jf6cVpE5; spf=pass (google.com: domain of linux-wireless-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-wireless-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234586AbiLUKaw (ORCPT + 66 others); Wed, 21 Dec 2022 05:30:52 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48526 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234505AbiLUK3p (ORCPT ); Wed, 21 Dec 2022 05:29:45 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5969E21884 for ; Wed, 21 Dec 2022 02:26:26 -0800 (PST) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id CA78561758 for ; Wed, 21 Dec 2022 10:26:25 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A62BEC433D2; Wed, 21 Dec 2022 10:26:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1671618385; bh=vX5YCsGjJRB4RCj7GNynp2IUhgssJobCUhxfzQJjB+g=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Jf6cVpE5cjFigkl95FcmSS2mVuEvejcwjXRb9wNQojkN9ee6otVvbpSz6ioCBfmSX TsYUWENSp0eNODdZs4uuyAo0h0X1kLTyWrOQwKPjZ6Fbj2uJcYDkfm49CEwvRfHyHm QSCVfQ41LuUpP+9jzZJXqeh0bsl2jSg1uwcgxlATB/menVwcz/Zj5IBsaYEOcOAlgJ VgwYjhm569FLwyTZB7EVTuXC21Z0JpTdA7XuDMa7kedW0qYxu3HNK6pLmMxeQbG6BN XZH172uOuklJxORO6GSZ8Pjd2bo6AxCF04/fwNdffuv8lzyfTbuszlrJaGW4VxWVGk Xy/wY4qr9Wt6g== Date: Wed, 21 Dec 2022 11:26:21 +0100 From: "lorenzo@kernel.org" To: Sujuan Chen =?utf-8?B?KOmZiOe0oOWonyk=?= Cc: Bo Jiao =?utf-8?B?KOeEpuazoik=?= , "linux-wireless@vger.kernel.org" , Shayne Chen =?utf-8?B?KOmZs+i7kuS4nik=?= , "nbd@nbd.name" , Evelyn Tsai =?utf-8?B?KOiUoeePiumIuik=?= , Ryder Lee , "linux-mediatek@lists.infradead.org" Subject: Re: [PATCH v2] wifi: mt76: fix potential memory leakage Message-ID: References: <0a218783098a6b1b90ff120c7182acee93dc8feb.1671423784.git.Bo.Jiao@mediatek.com> <9d8aa92e3650128c94af91435a222c00ff2acb1c.camel@mediatek.com> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="SyFKwzvfz74vbiYO" Content-Disposition: inline In-Reply-To: <9d8aa92e3650128c94af91435a222c00ff2acb1c.camel@mediatek.com> X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-wireless@vger.kernel.org --SyFKwzvfz74vbiYO Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable [...] > Hi Lore, Hi Sujuan, >=20 > we love your patch, but we have another patch to avoid memory > fragmentation by duplicating the rx skb after mt76_dma_dequeue(). it > requires mt76_get_rxwi() be placed before page_frag_alloc(). >=20 > the below patch(need rebase) will be sent after the current patch is > accepted. >=20 > --- a/drivers/net/wireless/mediatek/mt76/dma.c > +++ b/drivers/net/wireless/mediatek/mt76/dma.c > @@ -386,9 +386,11 @@ mt76_dma_get_buf(struct mt76_dev *dev, struct > mt76_queue *q, int idx, > SKB_WITH_OVERHEAD(q->buf_size), > DMA_FROM_DEVICE); >=20 > - buf =3D t->ptr; > + buf =3D page_frag_alloc(&q->rx_page, q->buf_size, > GFP_ATOMIC); > + if (!buf) > + return NULL; > + memcpy(buf, t->ptr, SKB_WITH_OVERHEAD(q->buf_size)); We this approach we still need to allocate the buffer (in mt76_dma_get_buf() instead of mt76_dma_rx_fill()) but we need even to copy the full buffer that can be pretty expensive instead of relying on the DMA, so I would avoid this approach. Regards, Lorenzo > t->dma_addr =3D 0; > - t->ptr =3D NULL; >=20 > mt76_put_rxwi(dev, t); >=20 > @@ -569,6 +571,7 @@ mt76_dma_rx_fill(struct mt76_dev *dev, struct > mt76_queue *q) > while (q->queued < q->ndesc - 1) { > struct mt76_txwi_cache *t =3D NULL; > struct mt76_queue_buf qbuf; > + bool skip_alloc =3D false; > void *buf =3D NULL; >=20 > if ((q->flags & MT_QFLAG_WED) && > @@ -576,11 +579,19 @@ mt76_dma_rx_fill(struct mt76_dev *dev, struct > mt76_queue *q) > t =3D mt76_get_rxwi(dev); > if (!t) > break; > + > + if (t->ptr) { > + skip_alloc =3D true; > + buf =3D t->ptr; > + } > } >=20 > - buf =3D page_frag_alloc(&q->rx_page, q->buf_size, > GFP_ATOMIC); > - if (!buf) > - break; > + if (!skip_alloc) { > + buf =3D page_frag_alloc(&q->rx_page, q->buf_size, > + GFP_ATOMIC); > + if (!buf) > + break; > + } >=20 > addr =3D dma_map_single(dev->dma_dev, buf, len, > DMA_FROM_DEVICE); > if (unlikely(dma_mapping_error(dev->dma_dev, addr))) { > -- > 2.18.0 >=20 > > > --=20 > > > 2.18.0 > > >=20 --SyFKwzvfz74vbiYO Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iHUEABYKAB0WIQTquNwa3Txd3rGGn7Y6cBh0uS2trAUCY6LfTQAKCRA6cBh0uS2t rBF/AP0ce3mIwZ/Uk5Zj1Y0rWsZg6hECTlHMNDdzISxhS4OuiwD/RSy8M9Sf3rVK R4S5GCzFMqSqiU0ogdBVoRnk0nsccg0= =wWbO -----END PGP SIGNATURE----- --SyFKwzvfz74vbiYO--