Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp801231pxa; Sat, 22 Aug 2020 00:06:52 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz8dOgx3VLTUvuz2qbEHBu4nEog+BVUiA/MeOrg42ERVYNYE/852Pu8w4xkFTUtUKX7eEd4 X-Received: by 2002:a17:906:140e:: with SMTP id p14mr6501027ejc.430.1598080012207; Sat, 22 Aug 2020 00:06:52 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598080012; cv=none; d=google.com; s=arc-20160816; b=UXI9m2E0wo0fVjG/qWSqG4MG3SwfQnYwSAtlwl/MzWxa+MryT9W2ZMMif66+RPqW5f t2MSTyKSp9pywTXMkkv+Y07QOqlMYUC4hHlMGVd7fQhxhzHy6r2FMPFv3cGHoUW9BtyH 4exFQv3G/b4p9uVJ5Ao1Of7zTB0PnZ09c/zXqyBABWlOG4fabz69ps9AMSS0wj4sa9wH zocA7lfgSkrrvmpg6sAQMAW+tKqiiYuGNjSh0pMrjeauXghi7rt6nbiXFb8Eo8Xc2oyi 8Hm8JEr913EcBSutF6HbBL1VdVVR9UCcB02TGAHuOcr28oz+ckTJG2om6lk6ZRIDSwRi BppA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=fwfunudANjOoTZXdPkqCPqeCMIZCPPl9JuhA1bl5pBw=; b=uGtJ/CI/hYw/B7BmbRe8AysFqT77UNjdls5X/PptFR4T0emiNmmaWYiIPBBvw/uUAL et0NVaG9PhdrymrjT1uT6VYijwD88AQUgI5aUdqIX5YY1VUEOLhxBmv1hPKXYCsTFGC3 Zi36g7aZaPekzp3EA5jMEtyET8B/3/R6lDZWLOK3DM/PRK4N/UFv7W2Un9Ow3WNJphhH MyDgQDpeOdiwb+hkMyV0fXbHiLe+l9J9lfEm0MD+XJEDswqOUVVki2brhwjh1B6xWBpw SHg9Zk5Lr2VKOQ4ioN1Kx0tr7FAwkYDNIZomp0DcjHsnMLG4H2EOqe0LPxdgFz3EC5X6 pP0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=AtiHmTov; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id dn20si4187808ejc.377.2020.08.22.00.06.28; Sat, 22 Aug 2020 00:06:52 -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=@google.com header.s=20161025 header.b=AtiHmTov; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727017AbgHVHEo (ORCPT + 99 others); Sat, 22 Aug 2020 03:04:44 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56540 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725917AbgHVHEl (ORCPT ); Sat, 22 Aug 2020 03:04:41 -0400 Received: from mail-ed1-x542.google.com (mail-ed1-x542.google.com [IPv6:2a00:1450:4864:20::542]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC705C061575 for ; Sat, 22 Aug 2020 00:04:40 -0700 (PDT) Received: by mail-ed1-x542.google.com with SMTP id i26so3416341edv.4 for ; Sat, 22 Aug 2020 00:04:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=fwfunudANjOoTZXdPkqCPqeCMIZCPPl9JuhA1bl5pBw=; b=AtiHmTovOFzL0yalRrqMCdj2asvK/1cxQIlCX37p5Ym1vHLaK5/uyoXDO5PV0Cynn4 dUM/RyqHAU8i3IVnpbxyAy5dQVFN3g1WwEgsnZVzWq2wHi8GnA8hZpnKmhaHNY1HPo7I NFWTu98Pw2ehMm3Mg9FVtUzRJ8xGKoxA8Cto7R84r8AZJDA4CVjbhQHT8pSBZ0sVyoEx P3xULHzA0ZOWuFJmDb5bDK2gJCPGMIn+ihEgGD4GwaiL75StGkLVdp9+E8TrJT4jSIF+ WslnbwaP1YImU7zqbBh5iEYHAe5d1z08fGQSgSaPSylBFep6M6IalNYNsDlgFB5hK0MK ncYA== 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=fwfunudANjOoTZXdPkqCPqeCMIZCPPl9JuhA1bl5pBw=; b=MbPz1iAi9K+1ZROZG/yk65GD0af1rGKxQf85wnmlVvZJ2Bvmcqe6JnQpatOnJ8t1d8 pCNimAX6GLdPRvHuWM2Bv7a/LG1HGzdkc4Xw9IPjv7VXMwnt2qEESXOsii/Vh68MmbCA Z/rSjb+cvybo8xYtIPsKenH6kQeZIGiH+6aUiMK4XK7rAj53W5f4Z+ztd0AbkXgx1hEC haLmw9I+YZe7vZpNaOotyc1dci+VFNdWSqg2aolydNgiysIDP8TLazEFkMLjzXJ4a+3f XXcdZzBfMAQUO/uTRDzw7l9rb1y3E+O1CIxY7iEeRHtdJVes+iw+wrhYr68FuSqV6+m2 iddw== X-Gm-Message-State: AOAM531ub7+EOOX1k7TBOrL9WN7uxAf1VtarWoajCIdKNlyJR0Kci2pg I4ZDdH35CSvPJOremIW8FiQ4413nF2eZlDrzUrswbw== X-Received: by 2002:a05:6402:30a5:: with SMTP id df5mr6082918edb.18.1598079878906; Sat, 22 Aug 2020 00:04:38 -0700 (PDT) MIME-Version: 1.0 References: <20200819224030.1615203-1-haoluo@google.com> <20200819224030.1615203-4-haoluo@google.com> In-Reply-To: From: Hao Luo Date: Sat, 22 Aug 2020 00:04:27 -0700 Message-ID: Subject: Re: [PATCH bpf-next v1 3/8] bpf: Introduce help function to validate ksym's type. To: Andrii Nakryiko Cc: Yonghong Song , Networking , bpf , open list , "open list:KERNEL SELFTEST FRAMEWORK" , Shuah Khan , Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Martin KaFai Lau , Song Liu , John Fastabend , KP Singh , Quentin Monnet , Steven Rostedt , Ingo Molnar , Andrey Ignatov , Jakub Sitnicki Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Ah, I see bpf_core_types_are_compat() after sync'ing my local repo. It seems the perfect fit for my use case. I only found the btf_equal_xxx() defined in btf.c when posting these patches. I can test and use bpf_core_types_are_compat() in v2. Thanks for pointing it out and explaining the public APIs. Hao On Fri, Aug 21, 2020 at 7:43 PM Andrii Nakryiko wrote: > > On Fri, Aug 21, 2020 at 5:43 PM Hao Luo wrote: > > > > On Fri, Aug 21, 2020 at 2:50 PM Andrii Nakryiko > > wrote: > > > > > > On Thu, Aug 20, 2020 at 10:22 AM Yonghong Song wrote: > > > > > > > > > > > > > > > > On 8/19/20 3:40 PM, Hao Luo wrote: > > > > > For a ksym to be safely dereferenced and accessed, its type defined in > > > > > bpf program should basically match its type defined in kernel. Implement > > > > > a help function for a quick matching, which is used by libbpf when > > > > > resolving the kernel btf_id of a ksym. > > > > > > > > > > Signed-off-by: Hao Luo > > > > > --- > > [...] > > > > > +/* > > > > > + * Match a ksym's type defined in bpf programs against its type encoded in > > > > > + * kernel btf. > > > > > + */ > > > > > +bool btf_ksym_type_match(const struct btf *ba, __u32 id_a, > > > > > + const struct btf *bb, __u32 id_b) > > > > > +{ > > > > > > [...] > > > > > > > > + } > > > > > + } > > > > > > > > I am wondering whether this is too strict and how this can co-work with > > > > CO-RE. Forcing users to write almost identical structure definition to > > > > the underlying kernel will not be user friendly and may not work cross > > > > kernel versions even if the field user cares have not changed. > > > > > > > > Maybe we can relax the constraint here. You can look at existing > > > > libbpf CO-RE code. > > > > > > Right. Hao, can you just re-use bpf_core_types_are_compat() instead? > > > See if semantics makes sense, but I think it should. BPF CO-RE has > > > been permissive in terms of struct size and few other type aspects, > > > because it handles relocations so well. This approach allows to not > > > have to exactly match all possible variations of some struct > > > definition, which is a big problem with ever-changing kernel data > > > structures. > > > > > > > I have to say I hate myself writing another type comparison instead of > > reusing the existing one. The issue is that when bpf_core_types_compat > > compares names, it uses t1->name_off == t2->name_off. It is also used > > Huh? Are we talking about the same bpf_core_types_are_compat() (there > is no bpf_core_types_compat, I think it's a typo)? > bpf_core_types_are_compat() doesn't even compare any name, so I'm not > sure what you are talking about. Some of btf_dedup functions do string > comparisons using name_off directly, but that's a special and very > careful case, it's not relevant here. > > > > in bpf_equal_common(). In my case, because these types are from two > > different BTFs, their name_off are not expected to be the same, right? > > I didn't find a good solution to refactor before posting this patch. I > > bpf_core_types_are_compat() didn't land until this week, so you must > be confusing something. Please take another look. > > > think I can adapt bpf_core_type_compat() and pay more attention to > > CO-RE. > > > > > > > > > > > + break; > > > > > + } > > > > > > [...] > > > > > > > > + > > > > > struct btf_ext_sec_setup_param { > > > > > __u32 off; > > > > > __u32 len; > > > > > diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h > > > > > index 91f0ad0e0325..5ef220e52485 100644 > > > > > --- a/tools/lib/bpf/btf.h > > > > > +++ b/tools/lib/bpf/btf.h > > > > > @@ -52,6 +52,8 @@ LIBBPF_API int btf__get_map_kv_tids(const struct btf *btf, const char *map_name, > > > > > __u32 expected_key_size, > > > > > __u32 expected_value_size, > > > > > __u32 *key_type_id, __u32 *value_type_id); > > > > > +LIBBPF_API bool btf_ksym_type_match(const struct btf *ba, __u32 id_a, > > > > > + const struct btf *bb, __u32 id_b); > > > > > > > > > > LIBBPF_API struct btf_ext *btf_ext__new(__u8 *data, __u32 size); > > > > > LIBBPF_API void btf_ext__free(struct btf_ext *btf_ext); > > > > > > > > The new API function should be added to libbpf.map. > > > > > > My question is why does this even have to be a public API? > > > > I can fix. Please pardon my ignorance, what is the difference between > > public and internal APIs? I wasn't sure, so used it improperly. > > public APIs are those that users of libbpf are supposed to use, > internal one is just for libbpf internal use. The former can't change, > the latter can be refactor as much as we need to. > > > > > Thanks, > > Hao