Message ID | 4a096ba9cf981a588aa87235bb91e933ee162b3d.1695542544.git.christophe.jaillet@wanadoo.fr |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:cae8:0:b0:403:3b70:6f57 with SMTP id r8csp552570vqu; Sun, 24 Sep 2023 01:05:01 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEnmO3Sj2LZJWJmI0W0hZXfffeBYHwroe3x4h45fibWRTVc/mSZQapnbk+YdSndJLblSU8U X-Received: by 2002:a05:6358:52c4:b0:13a:a85b:ce00 with SMTP id z4-20020a05635852c400b0013aa85bce00mr5588700rwz.31.1695542701389; Sun, 24 Sep 2023 01:05:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695542701; cv=none; d=google.com; s=arc-20160816; b=P2oxe5xfTjFbNcMMDfNAVWa1spjPSjGxSsG8tCK3CK7hSirdtnAvDmsIHSXln+N/Zh 38Etr+LAgZTNbEDAa/Exu8JV3lo5dXxXen/EpdrdlUO7kH2DnhR7Kf+JU5gv6umqCT/9 0WR5h0MW3sFi8eTLQtwgeN8MxTuvdepE9POcOhO1oDWphQOiUf1RHsjQALyQc5s8EC3I gpiRvWxyaT0qfOkH+bLREAlL61FuykDEIJpXe5qYqr+B+Z0GfO8duwbPT+gbJSqi5w0N oSE4E83Z2m7EOoHAd7MXpE4hA/pyLwVPQpOW5aDQOl6aru5FvsjLoMEGQ/B6dcE/XYEX OOZw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=u45atJv1BtkgA8BuqIIa8D3M4GDg5T72SV54ytqq6ds=; fh=1YTKGmU8OumZgfli4ZHpESNIL53MczToJ3viPjUmTuw=; b=lUAnmZo2DxRBvLt1aaO2vDajhQo4/4xanXJXrKXkTgWvRuS2WGgDe0Do8xM83dpixP W8dDsPjdo+Jf4qi+n3nf2hm/K+CGj+CFEUIz4wpOowv6WUuQvJEJmLDunPJsLkHLRGk7 bxU77qhFIsjzcpSuEIFJMiXJLTCWzRyUrGkaOrLNV5wwR2/g9RJMgYxf4TUjoWFyO/wb Iybt28q0prZnOjZd9e4BmitvOX0HbKDhaY1iypWMRrHIfZ92EKPBM+ibh5K2j4hI9HT3 U9ue8LYqNFDqbzPYInFFjhFpzd9T1IsZdWYBcsVifxdCEzAGjzssa22nPGrhBVkRmeHL 8+8A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=dQ3XcACW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from howler.vger.email (howler.vger.email. [2620:137:e000::3:4]) by mx.google.com with ESMTPS id v20-20020a056a00149400b0068c01fba736si8072947pfu.301.2023.09.24.01.05.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Sep 2023 01:05:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) client-ip=2620:137:e000::3:4; Authentication-Results: mx.google.com; dkim=pass header.i=@wanadoo.fr header.s=t20230301 header.b=dQ3XcACW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:4 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=wanadoo.fr Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by howler.vger.email (Postfix) with ESMTP id 512A38334888; Sun, 24 Sep 2023 01:03:26 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at howler.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229949AbjIXIDV (ORCPT <rfc822;pusanteemu@gmail.com> + 30 others); Sun, 24 Sep 2023 04:03:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58400 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229737AbjIXIDT (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Sun, 24 Sep 2023 04:03:19 -0400 Received: from smtp.smtpout.orange.fr (smtp-27.smtpout.orange.fr [80.12.242.27]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B38C110A for <linux-kernel@vger.kernel.org>; Sun, 24 Sep 2023 01:03:11 -0700 (PDT) Received: from pop-os.home ([86.243.2.178]) by smtp.orange.fr with ESMTPA id kK56qpUBLqQHikK57qsa0Q; Sun, 24 Sep 2023 10:03:10 +0200 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=wanadoo.fr; s=t20230301; t=1695542590; bh=u45atJv1BtkgA8BuqIIa8D3M4GDg5T72SV54ytqq6ds=; h=From:To:Cc:Subject:Date; b=dQ3XcACW9ta7AnZyfPgpVGsN/g48nPB37c5JGi9GvpVYsFrpFjdO3OTZN4CzJ1nzA 1WMkzjKVtrJvAGKEDVaq1JhOpmVdcfnBvbFkjNAJfftslP/W2DaK5q1inEuYNQC6J7 EcMvq5v392XkUSM5/xqcpcDoeZeoY7YKsuk3sv/+VRG89QXWLYJKBC5QRGPJXS302P 8nNQP4YCqnkVrPsLeeKPmJf190vFld8wMUYk1uIppsZZkW9+wg2aRD9JcUxuQwR8in X62S8P5I9mcWHDADcalieY0iQwPweHBJmoplO47U3BZMJYZV7ldR+wUKNxZ3lAwu4q 8kqZYZHGGqi7A== X-ME-Helo: pop-os.home X-ME-Auth: Y2hyaXN0b3BoZS5qYWlsbGV0QHdhbmFkb28uZnI= X-ME-Date: Sun, 24 Sep 2023 10:03:10 +0200 X-ME-IP: 86.243.2.178 From: Christophe JAILLET <christophe.jaillet@wanadoo.fr> To: "David S. Miller" <davem@davemloft.net>, David Ahern <dsahern@kernel.org>, Eric Dumazet <edumazet@google.com>, Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com> Cc: linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, Christophe JAILLET <christophe.jaillet@wanadoo.fr>, netdev@vger.kernel.org Subject: [PATCH net-next] udp_tunnel: Use flex array to simplify code Date: Sun, 24 Sep 2023 10:03:07 +0200 Message-Id: <4a096ba9cf981a588aa87235bb91e933ee162b3d.1695542544.git.christophe.jaillet@wanadoo.fr> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_PASS,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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (howler.vger.email [0.0.0.0]); Sun, 24 Sep 2023 01:03:26 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777905383821578020 X-GMAIL-MSGID: 1777905383821578020 |
Series |
[net-next] udp_tunnel: Use flex array to simplify code
|
|
Commit Message
Christophe JAILLET
Sept. 24, 2023, 8:03 a.m. UTC
'n_tables' is small, UDP_TUNNEL_NIC_MAX_TABLES = 4 as a maximum. So there
is no real point to allocate the 'entries' pointers array with a dedicate
memory allocation.
Using a flexible array for struct udp_tunnel_nic->entries avoids the
overhead of an additional memory allocation.
This also saves an indirection when the array is accessed.
Finally, __counted_by() can be used for run-time bounds checking if
configured and supported by the compiler.
Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr>
---
net/ipv4/udp_tunnel_nic.c | 11 ++---------
1 file changed, 2 insertions(+), 9 deletions(-)
Comments
Christophe JAILLET wrote: > 'n_tables' is small, UDP_TUNNEL_NIC_MAX_TABLES = 4 as a maximum. So there > is no real point to allocate the 'entries' pointers array with a dedicate > memory allocation. > > Using a flexible array for struct udp_tunnel_nic->entries avoids the > overhead of an additional memory allocation. > > This also saves an indirection when the array is accessed. > > Finally, __counted_by() can be used for run-time bounds checking if > configured and supported by the compiler. > > Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> > --- > net/ipv4/udp_tunnel_nic.c | 11 ++--------- > 1 file changed, 2 insertions(+), 9 deletions(-) > > diff --git a/net/ipv4/udp_tunnel_nic.c b/net/ipv4/udp_tunnel_nic.c > index 029219749785..b6d2d16189c0 100644 > --- a/net/ipv4/udp_tunnel_nic.c > +++ b/net/ipv4/udp_tunnel_nic.c > @@ -47,7 +47,7 @@ struct udp_tunnel_nic { > > unsigned int n_tables; > unsigned long missed; > - struct udp_tunnel_nic_table_entry **entries; > + struct udp_tunnel_nic_table_entry *entries[] __counted_by(n_tables); > }; > > /* We ensure all work structs are done using driver state, but not the code. > @@ -725,16 +725,12 @@ udp_tunnel_nic_alloc(const struct udp_tunnel_nic_info *info, > struct udp_tunnel_nic *utn; > unsigned int i; > > - utn = kzalloc(sizeof(*utn), GFP_KERNEL); > + utn = kzalloc(struct_size(utn, entries, n_tables), GFP_KERNEL); > if (!utn) > return NULL; > utn->n_tables = n_tables; Should utn->n_tables be initialized before first use of struct_size(utn, entries, n_tables)?
Le 24/09/2023 à 18:00, Willem de Bruijn a écrit : > Christophe JAILLET wrote: >> 'n_tables' is small, UDP_TUNNEL_NIC_MAX_TABLES = 4 as a maximum. So there >> is no real point to allocate the 'entries' pointers array with a dedicate >> memory allocation. >> >> Using a flexible array for struct udp_tunnel_nic->entries avoids the >> overhead of an additional memory allocation. >> >> This also saves an indirection when the array is accessed. >> >> Finally, __counted_by() can be used for run-time bounds checking if >> configured and supported by the compiler. >> >> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> >> --- >> net/ipv4/udp_tunnel_nic.c | 11 ++--------- >> 1 file changed, 2 insertions(+), 9 deletions(-) >> >> diff --git a/net/ipv4/udp_tunnel_nic.c b/net/ipv4/udp_tunnel_nic.c >> index 029219749785..b6d2d16189c0 100644 >> --- a/net/ipv4/udp_tunnel_nic.c >> +++ b/net/ipv4/udp_tunnel_nic.c >> @@ -47,7 +47,7 @@ struct udp_tunnel_nic { >> >> unsigned int n_tables; >> unsigned long missed; >> - struct udp_tunnel_nic_table_entry **entries; >> + struct udp_tunnel_nic_table_entry *entries[] __counted_by(n_tables); >> }; >> >> /* We ensure all work structs are done using driver state, but not the code. >> @@ -725,16 +725,12 @@ udp_tunnel_nic_alloc(const struct udp_tunnel_nic_info *info, >> struct udp_tunnel_nic *utn; >> unsigned int i; >> >> - utn = kzalloc(sizeof(*utn), GFP_KERNEL); >> + utn = kzalloc(struct_size(utn, entries, n_tables), GFP_KERNEL); >> if (!utn) >> return NULL; >> utn->n_tables = n_tables; > > Should utn->n_tables be initialized before first use of > struct_size(utn, entries, n_tables)? > It can't be. struct_size() is used to compute the memory size to allocate. Before the kzalloc() call, utn does not exist, so we can't write anything to utn->n_tables. It is undefined at this point. It is initialized the line just after, after the allocation, but before any use. CJ
On Mon, Sep 25, 2023 at 6:26 PM Christophe JAILLET <christophe.jaillet@wanadoo.fr> wrote: > > Le 24/09/2023 à 18:00, Willem de Bruijn a écrit : > > Christophe JAILLET wrote: > >> 'n_tables' is small, UDP_TUNNEL_NIC_MAX_TABLES = 4 as a maximum. So there > >> is no real point to allocate the 'entries' pointers array with a dedicate > >> memory allocation. > >> > >> Using a flexible array for struct udp_tunnel_nic->entries avoids the > >> overhead of an additional memory allocation. > >> > >> This also saves an indirection when the array is accessed. > >> > >> Finally, __counted_by() can be used for run-time bounds checking if > >> configured and supported by the compiler. > >> > >> Signed-off-by: Christophe JAILLET <christophe.jaillet@wanadoo.fr> Reviewed-by: Willem de Bruijn <willemb@google.com> > >> --- > >> net/ipv4/udp_tunnel_nic.c | 11 ++--------- > >> 1 file changed, 2 insertions(+), 9 deletions(-) > >> > >> diff --git a/net/ipv4/udp_tunnel_nic.c b/net/ipv4/udp_tunnel_nic.c > >> index 029219749785..b6d2d16189c0 100644 > >> --- a/net/ipv4/udp_tunnel_nic.c > >> +++ b/net/ipv4/udp_tunnel_nic.c > >> @@ -47,7 +47,7 @@ struct udp_tunnel_nic { > >> > >> unsigned int n_tables; > >> unsigned long missed; > >> - struct udp_tunnel_nic_table_entry **entries; > >> + struct udp_tunnel_nic_table_entry *entries[] __counted_by(n_tables); > >> }; > >> > >> /* We ensure all work structs are done using driver state, but not the code. > >> @@ -725,16 +725,12 @@ udp_tunnel_nic_alloc(const struct udp_tunnel_nic_info *info, > >> struct udp_tunnel_nic *utn; > >> unsigned int i; > >> > >> - utn = kzalloc(sizeof(*utn), GFP_KERNEL); > >> + utn = kzalloc(struct_size(utn, entries, n_tables), GFP_KERNEL); > >> if (!utn) > >> return NULL; > >> utn->n_tables = n_tables; > > > > Should utn->n_tables be initialized before first use of > > struct_size(utn, entries, n_tables)? > > > > It can't be. > struct_size() is used to compute the memory size to allocate. > > Before the kzalloc() call, utn does not exist, so we can't write > anything to utn->n_tables. It is undefined at this point. > > It is initialized the line just after, after the allocation, but before > any use. Of course, sorry. I confused __counted_by's reference to a field member in the struct, with the normal argument passing of struct_size and flex_array_size.
Hello: This patch was applied to netdev/net-next.git (main) by Paolo Abeni <pabeni@redhat.com>: On Sun, 24 Sep 2023 10:03:07 +0200 you wrote: > 'n_tables' is small, UDP_TUNNEL_NIC_MAX_TABLES = 4 as a maximum. So there > is no real point to allocate the 'entries' pointers array with a dedicate > memory allocation. > > Using a flexible array for struct udp_tunnel_nic->entries avoids the > overhead of an additional memory allocation. > > [...] Here is the summary with links: - [net-next] udp_tunnel: Use flex array to simplify code https://git.kernel.org/netdev/net-next/c/ef35bed6fad6 You are awesome, thank you!
diff --git a/net/ipv4/udp_tunnel_nic.c b/net/ipv4/udp_tunnel_nic.c index 029219749785..b6d2d16189c0 100644 --- a/net/ipv4/udp_tunnel_nic.c +++ b/net/ipv4/udp_tunnel_nic.c @@ -47,7 +47,7 @@ struct udp_tunnel_nic { unsigned int n_tables; unsigned long missed; - struct udp_tunnel_nic_table_entry **entries; + struct udp_tunnel_nic_table_entry *entries[] __counted_by(n_tables); }; /* We ensure all work structs are done using driver state, but not the code. @@ -725,16 +725,12 @@ udp_tunnel_nic_alloc(const struct udp_tunnel_nic_info *info, struct udp_tunnel_nic *utn; unsigned int i; - utn = kzalloc(sizeof(*utn), GFP_KERNEL); + utn = kzalloc(struct_size(utn, entries, n_tables), GFP_KERNEL); if (!utn) return NULL; utn->n_tables = n_tables; INIT_WORK(&utn->work, udp_tunnel_nic_device_sync_work); - utn->entries = kmalloc_array(n_tables, sizeof(void *), GFP_KERNEL); - if (!utn->entries) - goto err_free_utn; - for (i = 0; i < n_tables; i++) { utn->entries[i] = kcalloc(info->tables[i].n_entries, sizeof(*utn->entries[i]), GFP_KERNEL); @@ -747,8 +743,6 @@ udp_tunnel_nic_alloc(const struct udp_tunnel_nic_info *info, err_free_prev_entries: while (i--) kfree(utn->entries[i]); - kfree(utn->entries); -err_free_utn: kfree(utn); return NULL; } @@ -759,7 +753,6 @@ static void udp_tunnel_nic_free(struct udp_tunnel_nic *utn) for (i = 0; i < utn->n_tables; i++) kfree(utn->entries[i]); - kfree(utn->entries); kfree(utn); }