Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp4520576pxf; Tue, 16 Mar 2021 16:08:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxNCGU+gtrNffqtjddR4KTCajonXTljanLoVCzU/NxtFLwR8LwA6uZKwVeJ3izT5TCPhMsy X-Received: by 2002:aa7:d296:: with SMTP id w22mr40279782edq.150.1615936135394; Tue, 16 Mar 2021 16:08:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1615936135; cv=none; d=google.com; s=arc-20160816; b=HJsNvD94qY2zAWWLbMrVS39t3KarcEjuaY2znyxR3gtq9R3koumXkPlivh7xY+vX4N WyvHbWxKllAByuOMCesn0+bVzMT7mULpjsYf2dzImswR/SipXzRUwK1aAmbbKZ9xbgzK Qq6qO1FnmGzosPNY1NmhcKYU2SREUXGy3OexgjN63vddBkv2/dfStWq3tW5JjIdfropo aqdtsk0rRpLiNi66zlEeAMc0OAGp32H2wuclrEpWFs+RwAwo7iGwNFP2Hi3SO18wdKGK UBzEd+FZYi9x/VWH0Zokhteb0cOdD137wLEIhDEjI9UpDzpLxYo35cD8ZKBOzJLlo1Op X53w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=j2cst11jcwFb+EzWFHn4bvkF3XRi4pyH2UKgp3CnPWw=; b=zZYWKPYk5yAnIFb160yBsu0sxTSKYaW14PTrCdFXlUWIEiAFiacSVUX0mBx1KZjLjX yv+UQe8MRq1V80dzbn021Y2/qU7P2vml02TAfpdB4erTdj1t45FevCpfpm9CsWQf1Zqc lO9vzi6HogzJj+e88JFe84b4tXr8ScFnR7xsvjVuJDPklWZ+0X/CbRGAUfphp9OiOcFO fbAD0cm76ZICHz/dXHd0OnpXQgzSTYIii6eVeLF4u0qG8DFyAZN5nDBaZipkvHUkPV9p TvTlFFZojXyllCqiZ2o3+Wl364ntYq7YsowiSQbNQMn9nNwtQ3h86WXB7NPj6rqgeKqh QzKQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=INlc0bGF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id x26si14759175ejj.525.2021.03.16.16.08.32; Tue, 16 Mar 2021 16:08:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=INlc0bGF; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229536AbhCPXGq (ORCPT + 99 others); Tue, 16 Mar 2021 19:06:46 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51106 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229524AbhCPXGg (ORCPT ); Tue, 16 Mar 2021 19:06:36 -0400 Received: from mail-yb1-xb34.google.com (mail-yb1-xb34.google.com [IPv6:2607:f8b0:4864:20::b34]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6EEDEC06174A; Tue, 16 Mar 2021 16:06:36 -0700 (PDT) Received: by mail-yb1-xb34.google.com with SMTP id p186so38620958ybg.2; Tue, 16 Mar 2021 16:06:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=j2cst11jcwFb+EzWFHn4bvkF3XRi4pyH2UKgp3CnPWw=; b=INlc0bGFKkJ3O5UE+B2tZi2kDdTek1CiGVWGX4doIUWZNydBoMCiGZpqpsjfZwqq35 wCS+ujEtx7rI4f92P44MgvF+lPze1OfFcvZNz0y4dExOFslpQ5Xz6rw1AzcqNaJswJiA b6CrGHzmT0oKRR7MTBmtM/lacLPouGvblwElaMiyvSHqJPVw21arJvsiIKil40GQlfAZ CQPnJdqOpa+J7GbaHL4cCHd35DABuJr3i9kwwWTfS8whd598Gs874lcb0kNvCesYwh0g MKwR7VXwDsh8Bl6xsjmsFvYUXNTKTUf5bZm42eOGg6U9ahfxYJDvvLT1xQxwgyAmQaIc 1cAg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=j2cst11jcwFb+EzWFHn4bvkF3XRi4pyH2UKgp3CnPWw=; b=F8VBbgDr33UMl8uuEKUijdaBNq0gu2EofY+TiMt02DzcmJQRvBa5tquqplnsp6KivI pOIB5iwJA0B4s+Ajzxzh/OU3BoO7c95Y94sLU/SaH1qJp26jXLPrZkZJrQtf/lEenwJ2 YR1nqKnmqiSlOyUJkcZBKLA0BFZwUiOPFaYOjpTBLL5GAu4bHjLEQ1HaFT2A5eGS58XP nMjw3j7I80cRltQcwG0WqBrXLPdsGGbcawpml558DDv2UDcUSQ5K4bj7i4RHj3Gl5dHZ rXMkWRxSQ5VHAkySXiEq8YHQv7hldwPYCrYY2Lg5+8Eh5j6f8xj4fw/6yMZQOk7JLXsx jERw== X-Gm-Message-State: AOAM532R+CNmitiLlLvZ/F6atsZu1s9TdiSc4DKt/+Pzvzr32buSiK2p 16nB8BPvOXJdg3vVC86483CooLBjrxy7FMNxLL8= X-Received: by 2002:a25:874c:: with SMTP id e12mr1409360ybn.403.1615935995690; Tue, 16 Mar 2021 16:06:35 -0700 (PDT) MIME-Version: 1.0 References: <20210310220211.1454516-1-revest@chromium.org> <20210310220211.1454516-4-revest@chromium.org> In-Reply-To: From: Andrii Nakryiko Date: Tue, 16 Mar 2021 16:06:24 -0700 Message-ID: Subject: Re: [PATCH bpf-next 3/5] libbpf: Initialize the bpf_seq_printf parameters array field by field To: Florent Revest Cc: bpf , Alexei Starovoitov , Daniel Borkmann , Andrii Nakryiko , Yonghong Song , KP Singh , Brendan Jackman , open list Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Mar 16, 2021 at 3:43 PM Florent Revest wrote: > > On Tue, Mar 16, 2021 at 5:36 AM Andrii Nakryiko > wrote: > > On Wed, Mar 10, 2021 at 2:02 PM Florent Revest wrote: > > > +#define ___bpf_build_param0(narg, x) > > > +#define ___bpf_build_param1(narg, x) ___param[narg - 1] = x > > > +#define ___bpf_build_param2(narg, x, args...) ___param[narg - 2] = x; \ > > > + ___bpf_build_param1(narg, args) > > > +#define ___bpf_build_param3(narg, x, args...) ___param[narg - 3] = x; \ > > > + ___bpf_build_param2(narg, args) > > > +#define ___bpf_build_param4(narg, x, args...) ___param[narg - 4] = x; \ > > > + ___bpf_build_param3(narg, args) > > > +#define ___bpf_build_param5(narg, x, args...) ___param[narg - 5] = x; \ > > > + ___bpf_build_param4(narg, args) > > > +#define ___bpf_build_param6(narg, x, args...) ___param[narg - 6] = x; \ > > > + ___bpf_build_param5(narg, args) > > > +#define ___bpf_build_param7(narg, x, args...) ___param[narg - 7] = x; \ > > > + ___bpf_build_param6(narg, args) > > > +#define ___bpf_build_param8(narg, x, args...) ___param[narg - 8] = x; \ > > > + ___bpf_build_param7(narg, args) > > > +#define ___bpf_build_param9(narg, x, args...) ___param[narg - 9] = x; \ > > > + ___bpf_build_param8(narg, args) > > > +#define ___bpf_build_param10(narg, x, args...) ___param[narg - 10] = x; \ > > > + ___bpf_build_param9(narg, args) > > > +#define ___bpf_build_param11(narg, x, args...) ___param[narg - 11] = x; \ > > > + ___bpf_build_param10(narg, args) > > > +#define ___bpf_build_param12(narg, x, args...) ___param[narg - 12] = x; \ > > > + ___bpf_build_param11(narg, args) > > > > took me some time to get why the [narg - 12] :) it makes sense, but > > then I started wondering why not > > > > #define ___bpf_build_param12(narg, x, args...) > > ___bpf_build_param11(narg, args); ___param[11] = x > > > > ? seems more straightforward, no? > > Unless I'm misunderstanding something, I don't think this would work. > The awkward "narg - 12" comes from the fact that these variadic macros > work by taking the first argument out of the variadic arguments (x > followed by args) and calling another macro with what's left (args). You are right, of course, silly me. > > So if you do __bpf_build_param(arg1, arg2) you will have > __bpf_build_param2() called with arg1 and __bpf_build_param1() called > with arg2. And if you do __bpf_build_param(arg1, arg2, arg3) you will > have __bpf_build_param3() called with arg1, __bpf_build_param2() > called with arg2, and __bpf_build_param1() called with arg3. > Basically, things are inverted, the position at which you need to > insert in ___param evolves in the opposite direction of the X after > ___bpf_build_param which is the number of arguments left. > > No matter in which order __bpf_build_paramX calls > __bpf_build_param(X-1) (before or after setting ___param[n]) you will > be unable to know just from the macro name at which cell in __param > you need to write the argument. (except for __bpf_build_param12 which > is an exception, because the max number of arg is 12, if this macro > gets called, then we know that narg=12 and we will always write at > __param[0]) > > That being said, I share your concern that this code is hard to read. > So instead of giving narg to each macro, I tried to give a pos > argument which indicates in which cell the macro should write. pos is > basically a counter that goes from 0 to narg as macros go from narg to > 0. > > #define ___bpf_fill0(array, pos, x) > #define ___bpf_fill1(array, pos, x) array[pos] = x > #define ___bpf_fill2(array, pos, x, args...) array[pos] = x; > ___bpf_fill1(array, pos + 1, args) > #define ___bpf_fill3(array, pos, x, args...) array[pos] = x; > ___bpf_fill2(array, pos + 1, args) > #define ___bpf_fill4(array, pos, x, args...) array[pos] = x; > ___bpf_fill3(array, pos + 1, args) > #define ___bpf_fill5(array, pos, x, args...) array[pos] = x; > ___bpf_fill4(array, pos + 1, args) > #define ___bpf_fill6(array, pos, x, args...) array[pos] = x; > ___bpf_fill5(array, pos + 1, args) > #define ___bpf_fill7(array, pos, x, args...) array[pos] = x; > ___bpf_fill6(array, pos + 1, args) > #define ___bpf_fill8(array, pos, x, args...) array[pos] = x; > ___bpf_fill7(array, pos + 1, args) > #define ___bpf_fill9(array, pos, x, args...) array[pos] = x; > ___bpf_fill8(array, pos + 1, args) > #define ___bpf_fill10(array, pos, x, args...) array[pos] = x; > ___bpf_fill9(array, pos + 1, args) > #define ___bpf_fill11(array, pos, x, args...) array[pos] = x; > ___bpf_fill10(array, pos + 1, args) > #define ___bpf_fill12(array, pos, x, args...) array[pos] = x; > ___bpf_fill11(array, pos + 1, args) > #define ___bpf_fill(array, args...) \ > ___bpf_apply(___bpf_fill, ___bpf_narg(args))(array, 0, args) Yeah, this is still more straightforward, I think. Please use shorter names to keep it a bit more succinct: arr and p seems clear enough. > > I hope this makes things a bit clearer ? (I often joke that BPF is > written in preprocessor... :p) Definitely true for BPF_CORE_READ macros :)