Received: by 2002:a05:7412:2a8c:b0:e2:908c:2ebd with SMTP id u12csp1730759rdh; Tue, 26 Sep 2023 01:31:40 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEWC/3o/vf80LJ3cPVFEFz054OdNPFzGOJCrsMnxnZVJuADG7PQEi5Bw6AX8muY1LdO4JbS X-Received: by 2002:a05:6808:17aa:b0:3a7:6ff5:c628 with SMTP id bg42-20020a05680817aa00b003a76ff5c628mr15834552oib.11.1695717099893; Tue, 26 Sep 2023 01:31:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695717099; cv=none; d=google.com; s=arc-20160816; b=WTLpLO+SuJSTYTdCk60p0NtmRh64XzakfUMlIMzp7jIWPMXykfiPkvr2fTy61+P6Bu M2HVl/hjiPzk8gPM467PyH43Iq5fDiRqGcWpiHnoFq5FXMaNLgvV5BG/5Wa87A/XXF0q zoWJucVvYd07SJ3VXvs6EaH5ZOZeVyABV2hAvdEtz+sEfdxH/Rywg6BAAVP1XSvFnjen iFmk+HExr4kaioRI25vtsNNN4pzNIOCl42e1WYxmoSnxim/n6HP8VL6yoj1K9FteZxuK Ngj26i+TUqQemEnnlWRkl0XIylDu8Zl1PLx7OfWBhD7vVc7vXrdLtJjJNL3x15EeD1b1 BEuA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=gLT6JXItUdbK8gpxFGKaP+4imC55Y5aPeSLP42utcRg=; fh=sYmWormzQGcvd0LB+SAIdTvV/E+6ys6gGvI80BSt3kE=; b=Iqm9OpEnncHwDTVsrRFtzzVlE32aeSTt2PiLJETus/UzMmTZQTEqHCU0DrJaAjP2gl MxhRSLOvkcEUROpQzeZ/sAdi2EhtClwoDh7Pn1paI3V0TO26y7HTWs6ai4bwhUC5Foxm IS+itZrkKnA90adpAM7lIrRZH+Gqsbp+N3qu8gWWusvXRGD0p0RvFKDDKvV1rYq3Qpo5 f86ktoDCmOENoAxcfB45fkWinhmqeS1WRHuTGvNGFaleOh6ukrAun2NfgMslkm5CnaWT DZvdwaLKmwaf1TCkjTTAvWv+mkuGS3grgpXjvpIUygTXY73k1h0WpaLZpcaDk61roBda KEPA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=cmWD1mGv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from morse.vger.email (morse.vger.email. [2620:137:e000::3:1]) by mx.google.com with ESMTPS id p24-20020a637418000000b00584a7863503si1403781pgc.372.2023.09.26.01.31.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 26 Sep 2023 01:31:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) client-ip=2620:137:e000::3:1; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=cmWD1mGv; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:1 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by morse.vger.email (Postfix) with ESMTP id B096E80203DB; Mon, 25 Sep 2023 13:51:11 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at morse.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232170AbjIYUvN (ORCPT + 99 others); Mon, 25 Sep 2023 16:51:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51960 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229501AbjIYUvM (ORCPT ); Mon, 25 Sep 2023 16:51:12 -0400 Received: from mail-vs1-xe2d.google.com (mail-vs1-xe2d.google.com [IPv6:2607:f8b0:4864:20::e2d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 34BDFBE; Mon, 25 Sep 2023 13:51:02 -0700 (PDT) Received: by mail-vs1-xe2d.google.com with SMTP id ada2fe7eead31-452749f6c47so3012934137.1; Mon, 25 Sep 2023 13:51:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1695675061; x=1696279861; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=gLT6JXItUdbK8gpxFGKaP+4imC55Y5aPeSLP42utcRg=; b=cmWD1mGvN7sErs2tS/1bN+B+MK571qiF4jvgVA90LliVrxm23l7qEkfvoBcyEGAuWS hSKLAk0hXpoZ8H18ZjJH2m1S9100FIg+NC6n45dYIpogL5+yJTkhtxpF3cJ50CHnJOYs BjMcDlxXMbfl09PXN3SVf4iwAk8xe23rlSH3CPdNh8VIvMK7ei+1ynHwGthgRLfuGdBC gnhbp/d1i5a8ZLDlnPcr8QBhjwGUr8shPvQP7rZRW+u5vx6pNG+h6qg8hTnfVF2KIRVl BPaEcLspSh8PYpMkVclmdVzRhI6K0Zf81tF9kfxT3XXLGy/Adcro7HUwVQfYTqAKUXpy eZZA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695675061; x=1696279861; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=gLT6JXItUdbK8gpxFGKaP+4imC55Y5aPeSLP42utcRg=; b=vktlfilba1A4t7Pt0JR1ma+0FeIcfITioYiej3ibhHinyy5TG+HuzB3LXiRaPkW5X0 ZfkHZGsu9LRw08yN4L9nI3UgoNuqGPMbK4vkfVznMt1MjmChGiUtFcIQzJ8+GezM2ca1 +Q76L86DvkplbJDGk6Uir/KzmKvUVqLTfGiUlxdj4VUQMKMQ6MTeErTNP1E8qAyXK3W9 gA7YSYUoibjyEy1b0Jt0Ap11Sgorvz0FR2QB6t08fxy+/j3ER/Smnmp61Ssotkt1I7wS xcoy9aB2zCIODDsB7LdKJrcKwMXUwUgGKleOZQ/DscCY6wB+wyQKKSg8V03E5ju3c1nl UKLg== X-Gm-Message-State: AOJu0Yya3yRBFk701yqYfudehGpeL9B8vpXP8WkWr3JoiByVwqn79ADY MFu6K9WykOuFBkQCeifccC7A9UvxNHAt+0EEFrg= X-Received: by 2002:a05:6102:2e4:b0:44d:48bf:591c with SMTP id j4-20020a05610202e400b0044d48bf591cmr5309437vsj.30.1695675061205; Mon, 25 Sep 2023 13:51:01 -0700 (PDT) MIME-Version: 1.0 References: <4a096ba9cf981a588aa87235bb91e933ee162b3d.1695542544.git.christophe.jaillet@wanadoo.fr> <65105d3a8e70e_12c73e29410@willemb.c.googlers.com.notmuch> In-Reply-To: From: Willem de Bruijn Date: Mon, 25 Sep 2023 22:50:24 +0200 Message-ID: Subject: Re: [PATCH net-next] udp_tunnel: Use flex array to simplify code To: Christophe JAILLET Cc: "David S. Miller" , David Ahern , Eric Dumazet , Jakub Kicinski , Paolo Abeni , linux-kernel@vger.kernel.org, kernel-janitors@vger.kernel.org, netdev@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on morse.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (morse.vger.email [0.0.0.0]); Mon, 25 Sep 2023 13:51:11 -0700 (PDT) On Mon, Sep 25, 2023 at 6:26=E2=80=AFPM Christophe JAILLET wrote: > > Le 24/09/2023 =C3=A0 18:00, Willem de Bruijn a =C3=A9crit : > > Christophe JAILLET wrote: > >> 'n_tables' is small, UDP_TUNNEL_NIC_MAX_TABLES =3D 4 as a maximu= m. So there > >> is no real point to allocate the 'entries' pointers array with a dedic= ate > >> 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 Reviewed-by: Willem de Bruijn > >> --- > >> 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_table= s); > >> }; > >> > >> /* We ensure all work structs are done using driver state, but not t= he 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 =3D kzalloc(sizeof(*utn), GFP_KERNEL); > >> + utn =3D kzalloc(struct_size(utn, entries, n_tables), GFP_KERNEL); > >> if (!utn) > >> return NULL; > >> utn->n_tables =3D 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.