Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp654360pxa; Fri, 21 Aug 2020 17:47:18 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxY6T259FaFzk4bL6ytZad9Qek34cB9bJMSmbIpNM+SbM95DF9Iq+fJnkLn+1zoHYivGfqN X-Received: by 2002:a17:906:6cd:: with SMTP id v13mr5271241ejb.307.1598057238592; Fri, 21 Aug 2020 17:47:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598057238; cv=none; d=google.com; s=arc-20160816; b=Pk28qSXzRRoSJGzfSc3MCmlXJcdPTbXCWVBVM6qcurtOhv8qv4/sdcuk07M2LVY8Zo k5RtDxcaS5guc8J8sv+ciklH2bsTYJyNIOZ1iTzAwLkSMsrhn2zNk+Rom8wUqyySMnGa DKXy2/cO3R8MWDT9rmimgSwNldt9H6kCsy3tH0EuCt65+WP3JgdTLg/pQFtZB/QXBbQj Px6Y0+Q0sDLwWUa9byinXX0xJuInsabV4+54zGbCgZqr+jPR17X+WTRKr/+QW1/cOjnm QmgmvirXXct80k4x3HXu9MYNqV9yf9nRojFVZSePygXu+IeBV//IBm/dljURJW8oHoMW MUmA== 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=ZiWSg+hYBkj5lzGxjv3JQWa7cVF6R88j6OVrhK2uYBA=; b=PnmQGQ7pZStE59eqkk7WQYAdHdJ+M5ImvufqyYKvMvFan2Xf6TfIVq0H6d24h7uZVo vC8if03CoVCGYELtafL1avRKPo51Xxv1qkNyI8IXFfLwogxTA6EwwixLSARBF51aFjNb O4VMnojJUEF/tq9SL1CxZQVSkZAeoaJs0af76uPpsM/dNiUR4HXdzFO4oMXkUoThC4Cq 0OjQbxhvxth1LU1wN+qOy3htanfoR3DG1X/mDPcAAwbj5Elkt9K7lBt2JvE3vpNTnMfA 5gykxfkOYYzz/R/hqnC0Cg5iBlZahCfqpZtW9vxx1YLN/O4ddtiDevPPMTZ1qytQXtjO SIBA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=KxL3pP6y; 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 q23si2973876edb.548.2020.08.21.17.46.54; Fri, 21 Aug 2020 17:47:18 -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=KxL3pP6y; 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 S1726909AbgHVAoC (ORCPT + 99 others); Fri, 21 Aug 2020 20:44:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54722 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726856AbgHVAoB (ORCPT ); Fri, 21 Aug 2020 20:44:01 -0400 Received: from mail-ej1-x644.google.com (mail-ej1-x644.google.com [IPv6:2a00:1450:4864:20::644]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B08A8C061575 for ; Fri, 21 Aug 2020 17:44:00 -0700 (PDT) Received: by mail-ej1-x644.google.com with SMTP id si26so4567542ejb.12 for ; Fri, 21 Aug 2020 17:43:59 -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=ZiWSg+hYBkj5lzGxjv3JQWa7cVF6R88j6OVrhK2uYBA=; b=KxL3pP6yAfeMPoV7fDm3WekUknERTlN3sAOsvzQLQkyjAPp1Ai0394nLI+3h8BstI4 cru5zfAnacjkVdbkWZKptmu3nH5JmOdHq/mZXKeEkjrRDTiAPESIhswWFmdGY3J3Wa0P UWl9N6os5BIvy5xbJnvxPZmeQbI3R7rd6k1q7pNY7Tn0FRXChLTOaTf9z26toa3XC+rX B+M6JjoDPmc77B3lGYxP5YOv95QMIWagwMZI0jCVHBVP4f4UgBhs3qNZKNUBKW6x5Du6 ETkB8bZ2VZcINjgFqM8Zrqv7t7K19MHiNn0g0GmZ4j+YXBirIjRiZ8Ob8CZkBUtAISYa /hCA== 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=ZiWSg+hYBkj5lzGxjv3JQWa7cVF6R88j6OVrhK2uYBA=; b=MCkRsQ8IO/Oh7Rjue25Z2w0TxAYJ0TknbO0EskNGwe+XCw+5CtTqhaqNDDq2FYX52R sNToyN/ll8N7JqgGJczH2QzLPIOg3lYSVife9RhGzWUlBazww1U+iLjZPawkRAXUsmCb ShUmNpplN2MaxHfrFzEKXaHzvRiDwXFexb5XDhT1Anv8RQ3c8C2gvi5uc1v55bgPnNwB ghsQaOGfM3k22v7LEYKABfTnN/ITBXW22gROnjPgnujW3qf1VUzJ/tV4TSJnYBLPmVLw eMB78Xs2xHizstV99wwDuN6tWRSvR8h/I1izH7dF3JD/1UwFGtf5canP72EvoTIL3oyw XfBw== X-Gm-Message-State: AOAM532QnPHTzl726EFFgD50lC2tU9mza3Z7sNuauWEcBt9hNk5iDt48 8kSCrq5lQr+GhhqFS+La+DuFndXAmzVarSbduLSbJw== X-Received: by 2002:a17:906:54d3:: with SMTP id c19mr5711543ejp.408.1598057038351; Fri, 21 Aug 2020 17:43:58 -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: Fri, 21 Aug 2020 17:43:47 -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 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 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 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. Thanks, Hao