Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp4986884rwb; Mon, 31 Jul 2023 16:10:02 -0700 (PDT) X-Google-Smtp-Source: APBJJlFGMJYZW1vUPoTiJ+VSkgv+K1UlUrI83cwfN8xfAuuhkcVDPAnUYqg/x49A11m0pbXr5v90 X-Received: by 2002:a05:6402:405:b0:522:86fc:c0cc with SMTP id q5-20020a056402040500b0052286fcc0ccmr804703edv.35.1690845001768; Mon, 31 Jul 2023 16:10:01 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690845001; cv=none; d=google.com; s=arc-20160816; b=nPxmQ9Uv1HxIZ9OwoSYuD5MieT/UFFr4xAitoN9FXvUd0vi6M91lU3gpR/rypQcb5d djDBWoQee2kdPdHz1drNmvOTIj7AOMt43g94TBboh6Otd4NV0pp71W6cNnfYYwVhn/H3 P3/zctYI8+/3g1Vhpv4zZWim/+nIN0pp9gwMOFBoV3BFjcw1kS8+8v0psVm4XdGZthfg WaQIRno/4NzPU6ZB3fy/OJ1ucH4RrZH+7oWF4E15/9U1fa5aQmGPK2ZJPpD9XeTkZz9/ 5mSdJNwwfrE0KsUin/YuV4cFcjPJ5gEy072pBtnklC1RLRky11m02RiBvXplMls6SJCn O5iw== 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=va3Hr//Iy6un0Gflh3lVoQezkuRZ1y6D/A+Si7I5RQY=; fh=ULGbqpuabFCC5QJIgUkm7fzD5xMzeoYoTPhtyDUDBns=; b=ntXuiHXTutNXsE1TwL89iIQmLN6YB3xw2+o+0MLk4iHqGi7al9GZjGTyJSc7q5cZID okkW6Rcw5UadmdNtrZQ6EvTItqgTvcVOt5VFdQUIW/e53VxWVlAj8DhNxL/6+uiZWNH+ YoyaoNKQB5OiGqEEggB0CvArvXSD+Rkj41eD/4KNuOSaNuwkMhGuFbmndJPP8k3pqCmJ x+FVCyUSlsYUJ06MWISMYUjnqTuFUwJX6FAQqOd+1CAHaEvGNQrngQcnVtZgApJMWdgl WC+V2NIN7tAi0QmskL89btdqQLn5KLgrPfjoE9i6yV6U6y3Zpwd3w3mx1/x4wCTlgXJ7 XC0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=qOEWF1aX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g21-20020aa7c595000000b0052259d33818si7233472edq.33.2023.07.31.16.09.37; Mon, 31 Jul 2023 16:10:01 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-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=@gmail.com header.s=20221208 header.b=qOEWF1aX; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S231725AbjGaWAD (ORCPT + 99 others); Mon, 31 Jul 2023 18:00:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37218 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231482AbjGaWAC (ORCPT ); Mon, 31 Jul 2023 18:00:02 -0400 Received: from mail-lj1-x22f.google.com (mail-lj1-x22f.google.com [IPv6:2a00:1450:4864:20::22f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A14B8E7; Mon, 31 Jul 2023 15:00:00 -0700 (PDT) Received: by mail-lj1-x22f.google.com with SMTP id 38308e7fff4ca-2b9dc1bff38so36045531fa.1; Mon, 31 Jul 2023 15:00:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690840799; x=1691445599; 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=va3Hr//Iy6un0Gflh3lVoQezkuRZ1y6D/A+Si7I5RQY=; b=qOEWF1aXanGlasHGN4tOlxdbOZ45FjOQlnuMlfoSh6LGTJH7GMhoU6WVb5wRfZ21ER BDC55DvGgNAwHLmnxWoMbhtEWMiMsjbKjhraRxRN2u/8uH0wOjAmRow1+ZTBverzGeJ3 19MbLpL1lsLWn9h12OzowfZ3sLM1JrZoM6jqZeX5e9IUG8yLczNL7gGG2F5rQu+x48OY R8ACCnHqTCJ3/RdE7CMfXrbU0ZWRuSZdne8tHZpMxtg2rpmWRIcD2271xUHE/b+GSss4 PbxRLYGNqdK+ncwCKcmM6iOsmpCvJs4F+Ne2V4u+ulerIFySvhBaQwt3MVE1ICJkgBNV 8Kiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690840799; x=1691445599; 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=va3Hr//Iy6un0Gflh3lVoQezkuRZ1y6D/A+Si7I5RQY=; b=So4/vb0BPL25NbeYwrZAAnExFM3MWvU0jGHibCCyr+nfjFiXY59OCVFIdD6FVBTI2f 5ugmQB2VWb4Dpd6sEXvQRfXZQn6Diu1ED1lfhrRBro8ZTxW5Xh8V2Fhi1hjVEow4oXZV SHHHQhETiMTXgTeFNOp3uMJmfE9LfGtjMzfaNAzk/4XjxIJuTVPhf0q4VIL+uLhLBzdf DPdgjYIg8bBH9g5u6gZLDkkakGEaoOocPBp6WD/RN4eJYMveLHYa+SdOV+xY63sxhu8C DCgIpze/crlCOnbe82ESsMHaBBUOJeoSdCZ5/KJ2i7F+9tUOLu/WRcV1tZtQidbJiIM2 vBGQ== X-Gm-Message-State: ABy/qLblljKmhbTxeFEpcb2fndqoEd3PfgC2w1PirhWq5Wizwc664cCH t5JiKUl21d98OgRy7OdcaLh2o5CxMRpiyrBrizTK8vNP X-Received: by 2002:a2e:b011:0:b0:2b6:9fdf:d8f4 with SMTP id y17-20020a2eb011000000b002b69fdfd8f4mr833455ljk.29.1690840798696; Mon, 31 Jul 2023 14:59:58 -0700 (PDT) MIME-Version: 1.0 References: <169078860386.173706.3091034523220945605.stgit@devnote2> <169078863449.173706.2322042687021909241.stgit@devnote2> In-Reply-To: <169078863449.173706.2322042687021909241.stgit@devnote2> From: Alexei Starovoitov Date: Mon, 31 Jul 2023 14:59:47 -0700 Message-ID: Subject: Re: [PATCH v4 3/9] bpf/btf: Add a function to search a member of a struct/union To: "Masami Hiramatsu (Google)" Cc: linux-trace-kernel@vger.kernel.org, LKML , Steven Rostedt , Martin KaFai Lau , bpf , Sven Schnelle , Alexei Starovoitov Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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-kernel@vger.kernel.org On Mon, Jul 31, 2023 at 12:30=E2=80=AFAM Masami Hiramatsu (Google) wrote: > > From: Masami Hiramatsu (Google) > > Add btf_find_struct_member() API to search a member of a given data struc= ture > or union from the member's name. > > Signed-off-by: Masami Hiramatsu (Google) > Reviewed-by: Alan Maguire > --- > Changes in v3: > - Remove simple input check. > - Fix unneeded IS_ERR_OR_NULL() check for btf_type_by_id(). > - Move the code next to btf_get_func_param(). > - Use for_each_member() macro instead of for-loop. > - Use btf_type_skip_modifiers() instead of btf_type_by_id(). > Changes in v4: > - Use a stack for searching in anonymous members instead of nested call= . > --- > include/linux/btf.h | 3 +++ > kernel/bpf/btf.c | 40 ++++++++++++++++++++++++++++++++++++++++ > 2 files changed, 43 insertions(+) > > diff --git a/include/linux/btf.h b/include/linux/btf.h > index 20e3a07eef8f..4b10d57ceee0 100644 > --- a/include/linux/btf.h > +++ b/include/linux/btf.h > @@ -226,6 +226,9 @@ const struct btf_type *btf_find_func_proto(const char= *func_name, > struct btf **btf_p); > const struct btf_param *btf_get_func_param(const struct btf_type *func_p= roto, > s32 *nr); > +const struct btf_member *btf_find_struct_member(struct btf *btf, > + const struct btf_type *ty= pe, > + const char *member_name); > > #define for_each_member(i, struct_type, member) \ > for (i =3D 0, member =3D btf_type_member(struct_type); \ > diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c > index f7b25c615269..8d81a4ffa67b 100644 > --- a/kernel/bpf/btf.c > +++ b/kernel/bpf/btf.c > @@ -958,6 +958,46 @@ const struct btf_param *btf_get_func_param(const str= uct btf_type *func_proto, s3 > return NULL; > } > > +#define BTF_ANON_STACK_MAX 16 > + > +/* > + * Find a member of data structure/union by name and return it. > + * Return NULL if not found, or -EINVAL if parameter is invalid. > + */ > +const struct btf_member *btf_find_struct_member(struct btf *btf, > + const struct btf_type *ty= pe, > + const char *member_name) > +{ > + const struct btf_type *anon_stack[BTF_ANON_STACK_MAX]; > + const struct btf_member *member; > + const char *name; > + int i, top =3D 0; > + > +retry: > + if (!btf_type_is_struct(type)) > + return ERR_PTR(-EINVAL); > + > + for_each_member(i, type, member) { > + if (!member->name_off) { > + /* Anonymous union/struct: push it for later use = */ > + type =3D btf_type_skip_modifiers(btf, member->typ= e, NULL); > + if (type && top < BTF_ANON_STACK_MAX) > + anon_stack[top++] =3D type; > + } else { > + name =3D btf_name_by_offset(btf, member->name_off= ); > + if (name && !strcmp(member_name, name)) > + return member; > + } > + } > + if (top > 0) { > + /* Pop from the anonymous stack and retry */ > + type =3D anon_stack[--top]; > + goto retry; > + } Looks good, but I don't see a test case for this. The logic is a bit tricky. I'd like to have a selftest that covers it. You probably need to drop Alan's reviewed-by, since the patch is quite different from the time he reviewed it. Assuming that is addressed. How do we merge the series? The first 3 patches have serious conflicts with bpf trees. Maybe send the first 3 with extra selftest for above recursion targeting bpf-next then we can have a merge commit that Steven can pull into tracing? Or if we can have acks for patches 4-9 we can pull the whole set into bpf-n= ext.