Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp568012pxa; Fri, 21 Aug 2020 14:51:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzXaRt13hRQOjTfvVIODxYB9cj5hYPAQXW4IC9onoh9QU1uJVIBx9OmTEvHy2tPRugDmiRB X-Received: by 2002:a17:906:3a81:: with SMTP id y1mr4689849ejd.464.1598046673678; Fri, 21 Aug 2020 14:51:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598046673; cv=none; d=google.com; s=arc-20160816; b=c60GLIAlXREQEFn/no0a13K3dUqAisoOcUX4GjnkgDcTT9nCqV+ovTbll9nSYlda7z BRMdnKrlNlIuQiMbqj5Mbe4Gk9vpAGPv7HnbEaMkeJ8FGATIe4DOYvejzVVrSz6HVa4/ /EYjQk0+N0Ttioh6CSCopjRr34LuTyMFHT8C50Cpjbfwx5fu9GSwR/AIaK8VXwXWd2F4 hMpCDv1uYW/sX86O5VSDlK/5JwLkVcVTH1Nqjn9Bwfxui2g1wzLijElOV6Iabe2Kz6qE Em8ecVsbTchRXEHw+fdj+5RmFB3fcORZKyOwCR988uLrXBLyWmfUPupzLepVS64/45Eh oiUw== 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=LCtDZbEXzC+Kn+f0fxyMM1jOnFnep3y18ErpLmPyWt4=; b=UosTpRMUsnuOqQeCOBxNME4SGmcQBzCQntbiDLkiu95MHr6qN6FdHKednezo/PfGh1 vXnb1dLJYBvTpgZySTyGd40ZGifhXjcFYR5ZKo3DS+Pd9cjxiUjupkF908j0xMqGBx90 9HNQ6NZnT2+gBNL6PD51HDXySswxcjAo8+zxE61n6M3LiVdl4w7+sm0T0XOjfyezGkND 01/P+xObcaOGDCRakzWP3EkAYTVQUiLHqk2Q8EMu2GHAdt0EwFgABi21EGciLwB7rvMW +P1VuEvKp1SvPF5woaHt8ZrtklDFBQWsiehIuuz//OXjl3wKYFz25YR1Qkso/nfhOOIE 84CQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=T8qkn1gP; 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 v24si2188528eds.68.2020.08.21.14.50.50; Fri, 21 Aug 2020 14:51:13 -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=T8qkn1gP; 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 S1726830AbgHUVuR (ORCPT + 99 others); Fri, 21 Aug 2020 17:50:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56356 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726187AbgHUVuQ (ORCPT ); Fri, 21 Aug 2020 17:50:16 -0400 Received: from mail-yb1-xb41.google.com (mail-yb1-xb41.google.com [IPv6:2607:f8b0:4864:20::b41]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 47A3CC061573; Fri, 21 Aug 2020 14:50:16 -0700 (PDT) Received: by mail-yb1-xb41.google.com with SMTP id i10so1781223ybt.11; Fri, 21 Aug 2020 14:50:16 -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=LCtDZbEXzC+Kn+f0fxyMM1jOnFnep3y18ErpLmPyWt4=; b=T8qkn1gP+Rp85/WBW+E10QZI55mtY/lvclpr3P7fxqBrHSA7suC3XiloMsmuuWf/+m HmDmMB04s32ENwcls/J6AbATRIcvsmO+7lr+2ePM9UbcWDZC0tR1yvN1ch++35SfNpqN qV1Wx+XrikqB2RucXlQQro36UTEy8/13alnvL09oSLB1mp1Bnei89N9W7DWiwD6SQF5z NKD0AA5Cb/2D2oaZ1K7mIIiGD4z4kPLKHMzWUxBdW9+vr+buKs1ySwQVfukV11vWRVv5 RrukKViMe3OkIWkbygbUP4X3ffqQBygwF0bZYkYHYoYESW/pmE0sAnHnnnaAXM0nHfNF 8t/g== 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=LCtDZbEXzC+Kn+f0fxyMM1jOnFnep3y18ErpLmPyWt4=; b=VEmldfgKf4tdpxUyOhCwrQGBSmSX+sQ6g20MczEUldZci+vSlXQ6SNJshd61IFjyif 6YiaSTyX8wH9+Kd4SsnOtjfUpbETXtT61Hn5bDwtjB7amNQFb/G/BxFdBJtF0R/ssC/2 KNVJin++sohPxuwZBWTWwj8EdKzAIzzFypVCaf+UyjAZddQqItvN96xfXFfmpZg3emqk 1Thu0yDpc0h/MG9QaofCnXNt19dPdiYFFCy7GzoHaO7F4evSAQiuZi8n4iq3PTLTx87k ykTItNkTuIWwKkvZxxEUBtBHljY4/7tAAWohUZRTwkNs4/PiMgGRs8ad0rdLpjJPf3y0 lP3Q== X-Gm-Message-State: AOAM531klb6ZjzL00vPj5jPubAIXYcnqPKcGmtT8aYm7bumghWBNiTek HVCESD8WuE/vAV4sd73Tu9i4pL22bSYlSqenAr8= X-Received: by 2002:a25:2ad3:: with SMTP id q202mr6235577ybq.27.1598046615439; Fri, 21 Aug 2020 14:50:15 -0700 (PDT) MIME-Version: 1.0 References: <20200819224030.1615203-1-haoluo@google.com> <20200819224030.1615203-4-haoluo@google.com> In-Reply-To: From: Andrii Nakryiko Date: Fri, 21 Aug 2020 14:50:04 -0700 Message-ID: Subject: Re: [PATCH bpf-next v1 3/8] bpf: Introduce help function to validate ksym's type. To: Yonghong Song Cc: Hao Luo , 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 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 > > --- > > tools/lib/bpf/btf.c | 171 ++++++++++++++++++++++++++++++++++++++++++++ > > tools/lib/bpf/btf.h | 2 + > > 2 files changed, 173 insertions(+) > > > > diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c > > index a3d259e614b0..2ff31f244d7a 100644 > > --- a/tools/lib/bpf/btf.c > > +++ b/tools/lib/bpf/btf.c > > @@ -1005,6 +1005,177 @@ int btf__get_map_kv_tids(const struct btf *btf, const char *map_name, > > return 0; > > } > > > > +/* > > + * Basic type check for ksym support. Only checks type kind and resolved size. > > + */ > > +static inline > > +bool btf_ksym_equal_type(const struct btf *ba, __u32 type_a, > > + const struct btf *bb, __u32 type_b) > > "ba" and "bb" is not descriptive. Maybe "btf_a" or "btf_b"? > or even "btf1" or "btf2" since the number does not carry > extra meaning compared to letters. > > The same for below, may be t1, t2? > > > +{ > > + const struct btf_type *ta, *tb; > > + > > + ta = btf__type_by_id(ba, type_a); > > + tb = btf__type_by_id(bb, type_b); > > + > > + /* compare type kind */ > > + if (btf_kind(ta) != btf_kind(tb)) > > + return false; > > + > > + /* compare resolved type size */ > > + return btf__resolve_size(ba, type_a) == btf__resolve_size(bb, type_b); > > +} > > + > > +/* > > + * 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. > > > + 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?