From mboxrd@z Thu Jan 1 00:00:00 1970 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4A6C4C43219 for ; Wed, 2 Nov 2022 09:15:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231537AbiKBJPg (ORCPT ); Wed, 2 Nov 2022 05:15:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43616 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231251AbiKBJOr (ORCPT ); Wed, 2 Nov 2022 05:14:47 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2B56427FD2; Wed, 2 Nov 2022 02:13:27 -0700 (PDT) Received: from canpemm500010.china.huawei.com (unknown [172.30.72.55]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4N2LlQ5bZ2zHvbR; Wed, 2 Nov 2022 17:13:06 +0800 (CST) Received: from [10.174.179.191] (10.174.179.191) by canpemm500010.china.huawei.com (7.192.105.118) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Wed, 2 Nov 2022 17:13:25 +0800 Message-ID: <8ab6c29a-4080-e321-b762-5e4e0cc4665e@huawei.com> Date: Wed, 2 Nov 2022 17:13:24 +0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Thunderbird/102.2.0 Subject: Re: [PATCH net] net: tun: Fix memory leaks of napi_get_frags To: , CC: , , , , References: <1667381439-4419-1-git-send-email-wangyufen@huawei.com> From: wangyufen In-Reply-To: <1667381439-4419-1-git-send-email-wangyufen@huawei.com> Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 8bit X-Originating-IP: [10.174.179.191] X-ClientProxiedBy: dggems704-chm.china.huawei.com (10.3.19.181) To canpemm500010.china.huawei.com (7.192.105.118) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org Ignore, will resend later 在 2022/11/2 17:30, Wang Yufen 写道: > kmemleak reports after running test_progs: > > unreferenced object 0xffff8881b1672dc0 (size 232): > comm "test_progs", pid 394388, jiffies 4354712116 (age 841.975s) > hex dump (first 32 bytes): > e0 84 d7 a8 81 88 ff ff 80 2c 67 b1 81 88 ff ff .........,g..... > 00 40 c5 9b 81 88 ff ff 00 00 00 00 00 00 00 00 .@.............. > backtrace: > [<00000000c8f01748>] napi_skb_cache_get+0xd4/0x150 > [<0000000041c7fc09>] __napi_build_skb+0x15/0x50 > [<00000000431c7079>] __napi_alloc_skb+0x26e/0x540 > [<000000003ecfa30e>] napi_get_frags+0x59/0x140 > [<0000000099b2199e>] tun_get_user+0x183d/0x3bb0 [tun] > [<000000008a5adef0>] tun_chr_write_iter+0xc0/0x1b1 [tun] > [<0000000049993ff4>] do_iter_readv_writev+0x19f/0x320 > [<000000008f338ea2>] do_iter_write+0x135/0x630 > [<000000008a3377a4>] vfs_writev+0x12e/0x440 > [<00000000a6b5639a>] do_writev+0x104/0x280 > [<00000000ccf065d8>] do_syscall_64+0x3b/0x90 > [<00000000d776e329>] entry_SYSCALL_64_after_hwframe+0x63/0xcd > > The issue occurs in the following scenarios: > tun_get_user() > napi_gro_frags() > napi_frags_finish() > case GRO_NORMAL: > gro_normal_one() > list_add_tail(&skb->list, &napi->rx_list); > <-- While napi->rx_count < READ_ONCE(gro_normal_batch), > <-- gro_normal_list() is not called, napi->rx_list is not empty > <-- not ask to complete the gro work, will cause memory leaks in > <-- following tun_napi_del() > ... > tun_napi_del() > netif_napi_del() > __netif_napi_del() > <-- &napi->rx_list is not empty, which caused memory leaks > > To fix, add napi_complete() after napi_gro_frags(). > > Fixes: 90e33d459407 ("tun: enable napi_gro_frags() for TUN/TAP driver") > Signed-off-by: Wang Yufen > --- > drivers/net/tun.c | 15 +++++---------- > 1 file changed, 5 insertions(+), 10 deletions(-) > > diff --git a/drivers/net/tun.c b/drivers/net/tun.c > index 27c6d23..910990d 100644 > --- a/drivers/net/tun.c > +++ b/drivers/net/tun.c > @@ -1864,13 +1864,8 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, > > if (virtio_net_hdr_to_skb(skb, &gso, tun_is_little_endian(tun))) { > atomic_long_inc(&tun->rx_frame_errors); > - kfree_skb(skb); > - if (frags) { > - tfile->napi.skb = NULL; > - mutex_unlock(&tfile->napi_mutex); > - } > - > - return -EINVAL; > + err = -EINVAL; > + goto drop; > } > > switch (tun->flags & TUN_TYPE_MASK) { > @@ -1886,9 +1881,8 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, > pi.proto = htons(ETH_P_IPV6); > break; > default: > - dev_core_stats_rx_dropped_inc(tun->dev); > - kfree_skb(skb); > - return -EINVAL; > + err = -EINVAL; > + goto drop; > } > } > > @@ -1976,6 +1970,7 @@ static ssize_t tun_get_user(struct tun_struct *tun, struct tun_file *tfile, > > local_bh_disable(); > napi_gro_frags(&tfile->napi); > + napi_complete(&tfile->napi); > local_bh_enable(); > mutex_unlock(&tfile->napi_mutex); > } else if (tfile->napi_enabled) {