Received: by 2002:a05:6358:c692:b0:131:369:b2a3 with SMTP id fe18csp5107261rwb; Mon, 31 Jul 2023 18:45:34 -0700 (PDT) X-Google-Smtp-Source: APBJJlF+tJTQA0xUBOPl0cUKFYHb+zsHo6+DXQxj0sMnNRoIHCwykXecxtfgfvQ0xFoFqEjTzIwU X-Received: by 2002:a05:6830:1be6:b0:6b7:54b1:6524 with SMTP id k6-20020a0568301be600b006b754b16524mr10851474otb.36.1690854334685; Mon, 31 Jul 2023 18:45:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690854334; cv=none; d=google.com; s=arc-20160816; b=G2vPPT6vBElPyEf/MZhctsvgKki5wogeQJ/4JHW/DSlSDnanImt5hQe1Fyl57LiI+a WfRzNJty4aEzvxSizeFieANkTEsdEdD1IUCqEkCUvUmzJP9XgX6lfKWfbWwKLSfvsStp BwfLc2h0qKS5erJu4uaal7xYOPV2VSdPikBKVRhcqqdrPwY27Jr5maeuHcOkaLyka/lH vuawaUgpNRbjxGHvOSNZGbVIEK3A16CTh7Y7GC0CeTreBQ3s41iDLrhN3n+3getyx6/a BAniBiFV6WyuWHYPhq+MdkAUDBz89Qg6APFZOu88NRbgyBGan/QAuFzAn5vL6ltcV1BF zY0g== 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=OP0ss+QFrEDr1W853Rw5boHV+kOYmqr8XMFUdINABY4=; fh=R5ese1HodKDV9PllrW0RCo0/LikU/d4Ay6Z4oUSj+B4=; b=HyHg8NH3jwZIAuLEJBvBhuQNa7hQgL+etnLTiYp3zWpSaXmq+VfbUUcrJb1aSnHoIi gneZMbXGnvG7+5NeQFqHmCLCcj6X3JBiphDwMTPIkGEu3ftADGfGpnOz7Pen8SBOHTV+ 0E+d4mAes98gNek2G0dTR0uGYbMsASKT5pLkXTXo1w/kVMmZLo88XtdWTce5wutJnYmP JvPVVvpvSioawuJJmMrg26r9sNzxDlsuAt6FqwOWbxbNtVeeuI9Iyv5aqVVDkPlwsdMM ltrl+QA3AuCwLXqsbikLhDRK3VrB2HM8mfjrNzIzMEUvFClfvO6FZl3hd0tvZzg1WAbB ZdCA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=UnHjPYlK; 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 bk13-20020a056a02028d00b0056401aef835si7549567pgb.762.2023.07.31.18.45.22; Mon, 31 Jul 2023 18:45:34 -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=UnHjPYlK; 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 S230198AbjHAAaG (ORCPT + 99 others); Mon, 31 Jul 2023 20:30:06 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41202 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229920AbjHAAaE (ORCPT ); Mon, 31 Jul 2023 20:30:04 -0400 Received: from mail-lf1-x136.google.com (mail-lf1-x136.google.com [IPv6:2a00:1450:4864:20::136]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B7DB5133; Mon, 31 Jul 2023 17:30:02 -0700 (PDT) Received: by mail-lf1-x136.google.com with SMTP id 2adb3069b0e04-4f4b2bc1565so8050404e87.2; Mon, 31 Jul 2023 17:30:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690849801; x=1691454601; 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=OP0ss+QFrEDr1W853Rw5boHV+kOYmqr8XMFUdINABY4=; b=UnHjPYlKzePkM6jaAezL/Oz1naX+R2xTssxu/YDfCgIEk9OKvo+Zr1NwgplFVh5wYh Nu8Gn9NgifSqEDUMSps1FPZU7QoNYlffON1zPM/PaeW2J1A6EwJxKFHqtBrRmnzKtFGP VHGhj85Z3vviSlkeQzoy22OkqscKCGzuk2WDkrcGHF6YNUTmV3tYvxS/EIm+stNEr5fZ Dda9dteptkuq4IiV8oQB+25qsCFdNaIpgc3m7V+uWjwyctEQ2z01HhwglI2ELhOtkz/1 7+urBeDBtqnJVN3PLJt94akoOsI/33FqMpHTFJ0rseY2qX6icgJq7/U0ZrultIDdLfh8 yuWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690849801; x=1691454601; 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=OP0ss+QFrEDr1W853Rw5boHV+kOYmqr8XMFUdINABY4=; b=j24/CTflhoIWMft3A31IX3hgeg3LmwMbmn79WM/itb5u9vxkMyhYdP2aI/QpTkdRQz kn6UsNqII/1+64/ryb9wXRBVzSJv8Jj2UKc89iTSZQain0QAE0XC6tdEhrPgTn88T1F1 fqvkyLIs7jnLqF//u9OyXLIJB906Ji7iL7o28WE8ASqwBcBTwD1Y7U6Rh1pYznsVRHoO GdPxTSeB48KvO5V3Zx5pDMwwG2kEBb6NLUYy5nNQ83gTmU5ly65+DAFVGfaYBo33tJAL dWRz6y0eerhGwspwEET6pqvAALoGmaXvtPkpL/99DXiKxa4/vsrFIdC8BU1jM7maIHVG Xr9g== X-Gm-Message-State: ABy/qLa5Trp4d/dO2ZO4fkAHyFKr2OVGClvLcFph1+FFZseblWEWje4Z yHWsfQfKRKRHmgL9NgqwCOsMzOSW8GZYVJ7mFNQ= X-Received: by 2002:a2e:80c3:0:b0:2b7:3633:2035 with SMTP id r3-20020a2e80c3000000b002b736332035mr1121930ljg.32.1690849800775; Mon, 31 Jul 2023 17:30:00 -0700 (PDT) MIME-Version: 1.0 References: <169078860386.173706.3091034523220945605.stgit@devnote2> <169078863449.173706.2322042687021909241.stgit@devnote2> <20230801085724.9bb07d2c82e5b6c6a6606848@kernel.org> In-Reply-To: <20230801085724.9bb07d2c82e5b6c6a6606848@kernel.org> From: Alexei Starovoitov Date: Mon, 31 Jul 2023 17:29:49 -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 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 4:57=E2=80=AFPM Masami Hiramatsu wrote: > > On Mon, 31 Jul 2023 14:59:47 -0700 > Alexei Starovoitov wrote: > > > 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 s= tructure > > > 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 *fu= nc_proto, > > > s32 *nr); > > > +const struct btf_member *btf_find_struct_member(struct btf *btf, > > > + const struct btf_type= *type, > > > + const char *member_na= me); > > > > > > #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= struct 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= *type, > > > + const char *member_na= me) > > > +{ > > > + 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-= >type, 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. > > Thanks, and I agree about selftest. > > > > > You probably need to drop Alan's reviewed-by, since the patch is quite > > different from the time he reviewed it. > > OK. BTW, I found a problem on this function. I guess the member->offset w= ill > be the offset from the intermediate anonymous union, it is usually 0, but > I need the offset from the given structure. Thus the interface design mus= t > be changed. Passing a 'u32 *offset' and set the correct offset in it. If > it has nested intermediate anonymous unions, that offset must also be pus= hed. With all that piling up have you considering reusing btf_struct_walk() ? It's doing the opposite off -> btf_id while you need name -> btf_id. But it will give an idea of overall complexity if you want to solve it for nested arrays and struct/union. > > > > 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 b= pf-next. > > That's a good question. I don't like splitting the whole series in 2 -nex= t > branches. So I can send this to the bpf-next. Works for me. > I need to work on another series(*) on fprobes which will not have confli= cts with > this series. (*Replacing pt_regs with ftrace_regs on fprobe, which will t= ake longer > time, and need to adjust with eBPF). ftrace_regs? Ouch. For bpf we rely on pt_regs being an argument. fprobe should be 100% compatible replacement of kprobe-at-the-func-start. If it diverges from that it's a big issue for bpf. We'd have to remove all of fprobe usage. I could be missing something, of course.