Received: by 2002:ac8:45c5:0:b0:405:464a:c27a with SMTP id e5csp1276124qto; Thu, 27 Jul 2023 09:35:44 -0700 (PDT) X-Google-Smtp-Source: APBJJlH1yfi5E5Dy6hzCxc1t53kA1sGmbhCKtpc4TG0t3Q7ehzOScKENONXFlKSLt/HoQ/KRuWBh X-Received: by 2002:a17:902:b092:b0:1bb:c5a9:6b26 with SMTP id p18-20020a170902b09200b001bbc5a96b26mr4264754plr.5.1690475743849; Thu, 27 Jul 2023 09:35:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690475743; cv=none; d=google.com; s=arc-20160816; b=rccdOwt5d6PuxM2oPoNqJbXDbsL1gVamWxT3X1wJPDM35/OKSUU7J0f5Q+NanentmY i6uGa7G96WRZzpeTyHWfhSFwDuqhwtumI82FgOxZJl55CjMc0eB7ONNKANSbbTp2TsaB bYWs+NzQaIrCWGXSHyVe6qz8d7KsJOF38LWXJDut8Ia8FYhfAus78sS5QiawDrVwwTME tF7EAIGQ9uKh/0GguzZHSwU/uVDTMIgNHdWcm7+DihZyCiAU1bz7bnqJrY6aXs+d54ni EI/nRlHogPhHSniu6GkS9Pby/1Hl+WmYjkPC9teq28KSLHzTNXwZwF6Cx+xjz9vaYTM8 a2Xw== 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=e+lkkF+CLVMT4BhNE7E5cz+wEvXfUMd8e9OamCGK4HU=; fh=ULGbqpuabFCC5QJIgUkm7fzD5xMzeoYoTPhtyDUDBns=; b=e6r+d6bNLbbIV7Z1oEcMTAbLqptlbLUPm54HL078tqGUjw9m+SvyYuIpskoY7kHlhd aOHwvOOfVCf/SWSx2NzoRsmmJb0kZw46SuNnn4ttQ1ZKTANoF3UpPaMOEr9z32z/mON4 ifAZPabpSTHdKMyMAGCWN5ZHaY5/BitQQ/PgRH1gjEgIW2RRQveS5kCv94c+zFVbQXrM cwryE3D2GmV5PbQtZPXi7yWZ4/CVBdKGQAH1hcgUXRf7IKKkcwZ8J+Mq2w6GPNe11C2D Wg085tkG87nPHUAMJKCSLKqlw8rbNlDW3nXLDt6YkM8O8l7IEJ0rxKBUAeJJcd+SVfWY Wvqw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20221208 header.b=F6j8OCqc; 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 e11-20020a170902e0cb00b001a6f0eab385si1500966pla.55.2023.07.27.09.35.29; Thu, 27 Jul 2023 09:35:43 -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=F6j8OCqc; 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 S233041AbjG0PjS (ORCPT + 99 others); Thu, 27 Jul 2023 11:39:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232997AbjG0PjR (ORCPT ); Thu, 27 Jul 2023 11:39:17 -0400 Received: from mail-lj1-x233.google.com (mail-lj1-x233.google.com [IPv6:2a00:1450:4864:20::233]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 901FF2122; Thu, 27 Jul 2023 08:39:15 -0700 (PDT) Received: by mail-lj1-x233.google.com with SMTP id 38308e7fff4ca-2b95d5ee18dso16825151fa.1; Thu, 27 Jul 2023 08:39:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1690472354; x=1691077154; 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=e+lkkF+CLVMT4BhNE7E5cz+wEvXfUMd8e9OamCGK4HU=; b=F6j8OCqcwyXUBtmVT65R8NJsFMHmPiWy6i9WOipPq1JUReuJwpp7aO5NpFmwsAR2Bj CJsCu4ELCKvHF6Buf+HIqoBv1aHEZRzFmiYnY12OVJWMAQc3pKkjRFzkCEydeheZhyt7 655vh5dN+OSj6B90HrfVerPLGyIkLsuB8bRpc0VZ5MY8eoS1SY8BMK97RksKA/3jqBB2 HvFMgnXzruA7ExqfCWlPjGThQW7hoVWSqFEyAjlpmwWQVZ63nTxXDARLt03BdH+hNbsH 0uva1GVN1nm8Q5p97DFv+VhppdMZWgwz8cu8qnpoOLrSaxRMW6Rf6LxY/aSTEelVvfHP rNCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690472354; x=1691077154; 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=e+lkkF+CLVMT4BhNE7E5cz+wEvXfUMd8e9OamCGK4HU=; b=f3rjp5b04tu9ncrFt5+SZ843ydDSIPEgxjuQrNUM7DCuK6vpyJrNKoGlKBuOlfdO88 LfGAIt8rrlM4dVpqXg5py6Nwm/AFeP00L7jJVSP532KFHYkBP2CTXk53Q07tySJjmQ1l VcYY+Hp51yibnIpYr5u5JpveyY9I9X9UHN82JKI+pIVVy5iAl2XFRVY4w2syPG4hl1+K kgTSBnx0BKtu46yOAEOY1EdyUVn9JvwStt/hFAz1yrX/NqCvOJx+/p9diV0e1CaRc31A AVN2Rnqf34YOfoNi+wIoPZ1ECGH/auOg31DkZYn5jeVJoh5vYEKT9yoXZojWNwEjWPTj YAXQ== X-Gm-Message-State: ABy/qLbq0md90o4yKFYLCl5feGLeuIHLIpPebBuEPUEYntZrXQZug8ur l1jxU3dB821RCG4kYU+y3syRKpnzmYVnlgEi+YDerJ1e X-Received: by 2002:a2e:9842:0:b0:2b6:cf31:5e81 with SMTP id e2-20020a2e9842000000b002b6cf315e81mr2080102ljj.20.1690472353501; Thu, 27 Jul 2023 08:39:13 -0700 (PDT) MIME-Version: 1.0 References: <169037639315.607919.2613476171148037242.stgit@devnote2> <169037642351.607919.10234149030120807556.stgit@devnote2> In-Reply-To: <169037642351.607919.10234149030120807556.stgit@devnote2> From: Alexei Starovoitov Date: Thu, 27 Jul 2023 08:39:02 -0700 Message-ID: Subject: Re: [PATCH v3 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_NONE,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 Wed, Jul 26, 2023 at 6:00=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(). > --- > include/linux/btf.h | 3 +++ > kernel/bpf/btf.c | 35 +++++++++++++++++++++++++++++++++++ > 2 files changed, 38 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..5258870030fc 100644 > --- a/kernel/bpf/btf.c > +++ b/kernel/bpf/btf.c > @@ -958,6 +958,41 @@ const struct btf_param *btf_get_func_param(const str= uct btf_type *func_proto, s3 > return NULL; > } > > +/* > + * 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_member *member, *ret; > + const char *name; > + int i; > + > + if (!btf_type_is_struct(type)) > + return ERR_PTR(-EINVAL); > + > + for_each_member(i, type, member) { > + if (!member->name_off) { > + /* unnamed member: dig deeper */ > + type =3D btf_type_skip_modifiers(btf, member->typ= e, NULL); > + if (type) { > + ret =3D btf_find_struct_member(btf, type, > + member_name)= ; Unbounded recursion in the kernel? Ouch. That might cause issues. Please convert it to a loop. It doesn't have to be recursive. > + if (!IS_ERR_OR_NULL(ret)) > + return ret; > + } > + } else { > + name =3D btf_name_by_offset(btf, member->name_off= ); > + if (name && !strcmp(member_name, name)) > + return member; > + } > + } > + > + return NULL; > +} > + > #define BTF_SHOW_MAX_ITER 10 > > #define BTF_KIND_BIT(kind) (1ULL << kind) >