Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp2020497pxk; Tue, 1 Sep 2020 13:36:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw1+O3sWOvjV50O3OX1IlgVA2soqq6VmAcfEM/fd3Id+FZ/mjsAvmhGXjKCfv9xTHu3R70b X-Received: by 2002:a17:906:f8d2:: with SMTP id lh18mr3105750ejb.363.1598992606861; Tue, 01 Sep 2020 13:36:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598992606; cv=none; d=google.com; s=arc-20160816; b=cFvwd4XHxyDde6clYQB3s97VSUVcR8Tuf/bK5nVo4+B58uS2PF6XPYZ+PlgjyLELmq LAH/0qpeeF/6tUk50//aiCRoUDXe+HWpMz8e7I3lozkzdoW10fOPHIXN2KLfadRj90W1 Rt9ZRO+eTI5EYT15ZG8CIeWJuWNRnZMdVhpIk0Qr+vUIU2cZFh4Mi0UBPigP2JnIgoJE pcurLForl2lTSIVPtV1DeX9bQ8ncVoSuiGfqgIT93QmSEJgIMQBBLmLUPJBHEPYelL3d ipiwGJylz/CwsapIrYoFqjklsEY1MXlPeKOmtaIpKb0oO1jJG5jGNpk9OTJy0XWNLIg3 QcqQ== 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=/fv4Q0UAT/ZAtqAn51EeCeb1F3QIrxMcdfWrDVrWkTI=; b=GqyTlkZC7mvPxNPntEjF1rTkNKUXZYFOjG4ddH2KBF8KT8y4nZ1nOJkDcGJQ1O18Em aTNXjDiSyjprGXMKIkmE1Qv3ij/DmysgX5h6/IIdwAFVLLZ2UUZtQta/MTkBR/r1zSEC 2olqf1v3UILky7r5Pb+ZSeAg5zYVmFCLmtvT0XZ2O1dD+iUA/bkv3DZSrWMB8Amks5Au rkJV3VGt+eQpa46qW96sNjnHRq5fN2rpVjstqP2XtRQN8Y24Er1clcjwtzNggY1JC4sn MtdH+fWLPBSyV6yefy088P+WjSA7U/3qzhFd6m6XulbyJqjNZWwPVRT+Ki7Z4e+3GfUr TOcQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=qlvheLOW; 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 v26si1262842ejr.748.2020.09.01.13.36.22; Tue, 01 Sep 2020 13:36:46 -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=qlvheLOW; 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 S1728548AbgIAUfg (ORCPT + 99 others); Tue, 1 Sep 2020 16:35:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45000 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726091AbgIAUfb (ORCPT ); Tue, 1 Sep 2020 16:35:31 -0400 Received: from mail-ed1-x544.google.com (mail-ed1-x544.google.com [IPv6:2a00:1450:4864:20::544]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D0D66C061244 for ; Tue, 1 Sep 2020 13:35:30 -0700 (PDT) Received: by mail-ed1-x544.google.com with SMTP id b12so2746077edz.11 for ; Tue, 01 Sep 2020 13:35:30 -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=/fv4Q0UAT/ZAtqAn51EeCeb1F3QIrxMcdfWrDVrWkTI=; b=qlvheLOWupwRUGhcpiy3DSwi9x1+3f0W1j/G0kKSEcQdmdG0+23jvoRyMQ/5itywI8 ExcEU4skx3fk4uWZ0rMs5M7BuFqJ6B0pbTq/82e/319zzYdABpmOoP2eagNvkCBtLFIO j2dfJv8s6J08LvqwWmH8wDdPlnosZpULcoGwUdk52kEM1h7MjkPgnwLv4w8uz6zQbggz +S+jn/P0vnqmThm/bwoJSzdqmkr09zNBfodIQbAtyK6IbffiGiJRg5grGCtXbyxaSgLX gx0dVsgZTc41bsPpI1rkr3fI2cfhN8Iz+ecq27XG9Gen+KL6cgMlJc/jKNSz6DlLb0wk 4Vzw== 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=/fv4Q0UAT/ZAtqAn51EeCeb1F3QIrxMcdfWrDVrWkTI=; b=iq1/smSvbZE0fIFPG8o364r6YIAkhYOGXjvCTD740NjN7Y7KEIV7tt6nG2ck5lx2yW mGTNWFpV+LHlVLRWZ+o4WBrqUw0qC755eNBWx5WScMS0Y1mlmPEOEme+s4FCPh//9QYL duLc7J6T/QtZmOBeDMxGuspfPX25yZzRmTGQc/Vk3EBvDZ7OlfkO0SVWfdnIfxeee5ZV BSjtkCOEmGZ8IrK6mKFp4/smxWeuT1VObshXNkB0hhgW2Wbl1EuVUBnWyNo+ZwvHsbKi fvo2U1uX5P7xR7zXYEB9RA/IsBXDFtQEf9vf+l0Kn1CdmTI/taHd6pysf2XYjOVASBCa N+Vg== X-Gm-Message-State: AOAM530aX9luPmCNAicuPpFkDPSQywZpdLgWqfJ/16wWk6APgSIDi25E XnK/3ASSEKkSMZEwbQtrlzRMlGo6CJb9SdXGYkXeWw== X-Received: by 2002:aa7:dd01:: with SMTP id i1mr3522834edv.121.1598992529151; Tue, 01 Sep 2020 13:35:29 -0700 (PDT) MIME-Version: 1.0 References: <20200819224030.1615203-1-haoluo@google.com> <20200819224030.1615203-5-haoluo@google.com> In-Reply-To: From: Hao Luo Date: Tue, 1 Sep 2020 13:35:17 -0700 Message-ID: Subject: Re: [PATCH bpf-next v1 4/8] bpf/libbpf: BTF support for typed ksyms To: Andrii Nakryiko Cc: Networking , bpf , open list , "open list:KERNEL SELFTEST FRAMEWORK" , Shuah Khan , Alexei Starovoitov , Andrii Nakryiko , Daniel Borkmann , Martin KaFai Lau , Song Liu , Yonghong Song , 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 Tue, Sep 1, 2020 at 11:11 AM Andrii Nakryiko wrote: > > On Thu, Aug 27, 2020 at 3:29 PM Hao Luo wrote: > > > > On Fri, Aug 21, 2020 at 3:37 PM Andrii Nakryiko > > wrote: > > > > > > On Wed, Aug 19, 2020 at 3:42 PM Hao Luo wrote: > > > > > > > > If a ksym is defined with a type, libbpf will try to find the ksym's btf > > > > information from kernel btf. If a valid btf entry for the ksym is found, > > > > libbpf can pass in the found btf id to the verifier, which validates the > > > > ksym's type and value. > > > > > > > > Typeless ksyms (i.e. those defined as 'void') will not have such btf_id, > > > > but it has the symbol's address (read from kallsyms) and its value is > > > > treated as a raw pointer. > > > > > > > > Signed-off-by: Hao Luo > > > > --- > > > > tools/lib/bpf/libbpf.c | 130 ++++++++++++++++++++++++++++++++++++----- > > > > 1 file changed, 114 insertions(+), 16 deletions(-) > > > > > > > > diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c > > > > index 4a81c6b2d21b..94eff612c7c2 100644 > > > > --- a/tools/lib/bpf/libbpf.c > > > > +++ b/tools/lib/bpf/libbpf.c > > > > @@ -357,7 +357,16 @@ struct extern_desc { > > > > bool is_signed; > > > > } kcfg; > > > > struct { > > > > - unsigned long long addr; > > > > + /* > > > > + * 1. If ksym is typeless, the field 'addr' is valid. > > > > + * 2. If ksym is typed, the field 'vmlinux_btf_id' is > > > > + * valid. > > > > + */ > > > > + bool is_typeless; > > > > + union { > > > > + unsigned long long addr; > > > > + int vmlinux_btf_id; > > > > + }; > > > > > > ksym is 16 bytes anyways, union doesn't help to save space. I propose > > > to encode all this with just two fields: vmlinux_btf_id and addr. If > > > btf_id == 0, then extern is typeless. > > > > Ack on expanding the union. But I slightly preferred keeping > > is_typeless. IIUC, btf_id points a VAR_KIND, we need the following > > pointer chasing every time > > > > t = btf__type_by_id(obj->btf, ext->btf_id); > > t->type; > > > > which I felt is worse than keeping a is_typeless flag. > > Sorry, I'm not following. In all places where you would check > sym->is_typeless, you'd now just do: > > if (ext->ksym.vmlinux_btf_id) { > /* typed, use ext->ksym.vmlinux_btf_id */ > } else { > /* typeless */ > } > My apologies, I should be more specific. 'vmlinux_btf_id' gets its value in bpf_object__resolve_ksyms_btf_id(). Before we call this function, there are three places that need to tell whether a ksym is typed, currently in v1. Specifically, - in bpf_object__collect_externs(), typeless ksyms are rewritten as 'int', in contrast, typed ones are left untouched (though this may change in v2). - bpf_object__load_vmlinux_btf() now is called before bpf_object__resolve_ksyms_btf_id(). In v1's design, if there is no typed ksym, we could skip loading vmlinux_btf potentially. - even bpf_object__resolve_ksyms_btf_id() itself is conditionally called, depending on whether there is any typed ksym. At the time when these places are called, vmlinux_btf_id is unavailable and we can't use it for the purpose of telling whether a ksym is typed. However, rather than vmlinux_btf_id, there may be an alternative. We can record the ksym extern's type's btf_id and use that as 'is_typeless' flag. This also solves the problem below. [...] > > > > } else { > > > > pr_warn("unrecognized extern section '%s'\n", sec_name); > > > > return -ENOTSUP; > > > > @@ -2992,9 +3006,9 @@ static int bpf_object__collect_externs(struct bpf_object *obj) > > > > /* sort externs by type, for kcfg ones also by (align, size, name) */ > > > > qsort(obj->externs, obj->nr_extern, sizeof(*ext), cmp_externs); > > > > > > > > - /* for .ksyms section, we need to turn all externs into allocated > > > > - * variables in BTF to pass kernel verification; we do this by > > > > - * pretending that each extern is a 8-byte variable > > > > + /* for .ksyms section, we need to turn all typeless externs into > > > > + * allocated variables in BTF to pass kernel verification; we do > > > > + * this by pretending that each typeless extern is a 8-byte variable > > > > */ > > > > if (ksym_sec) { > > > > /* find existing 4-byte integer type in BTF to use for fake > > > > @@ -3012,7 +3026,7 @@ static int bpf_object__collect_externs(struct bpf_object *obj) > > > > > > > > sec = ksym_sec; > > > > n = btf_vlen(sec); > > > > - for (i = 0, off = 0; i < n; i++, off += sizeof(int)) { > > > > + for (i = 0, off = 0; i < n; i++) { > > > > struct btf_var_secinfo *vs = btf_var_secinfos(sec) + i; > > > > struct btf_type *vt; > > > > > > > > @@ -3025,9 +3039,14 @@ static int bpf_object__collect_externs(struct bpf_object *obj) > > > > return -ESRCH; > > > > } > > > > btf_var(vt)->linkage = BTF_VAR_GLOBAL_ALLOCATED; > > > > - vt->type = int_btf_id; > > > > + if (ext->ksym.is_typeless) { > > > > + vt->type = int_btf_id; > > > > + vs->size = sizeof(int); > > > > + } > > > > vs->offset = off; > > > > - vs->size = sizeof(int); > > > > + off += vs->size; > > > > + pr_debug("ksym var_secinfo: var '%s', type #%d, size %d, offset %d\n", > > > > + ext->name, vt->type, vs->size, vs->offset); > > > > > > It's a bit of a waste that we still allocate memory for those typed > > > ksym externs, as they don't really need space. But modifying BTF is a > > > pain right now, so I think we'll have to do it, until we have a better > > > BTF API. But let's make them integers for now to take a fixed and > > > small amount of space. > > > > > > > Do you mean making typed ksym externs of type integer? If so, we can't > > do that, I think. After collect_externs, we later need to compare the > > declared extern's type against the type defined in kernel. Better not > > rewrite their types in BTf. > > Then maybe we need to make btf_id to point to the actual type of the > variable, not BTF_KIND_VAR? Or just additionally record type's btf_id, > not sure which one makes more sense at the moment. > > > > > I am generally against modifying BTF. I initially didn't notice that > > all the ksym externs' types are modified to 'int' and the type > > comparison I mentioned above always failed. I dumped the btf in > > vmlinux and the btf in object file, checked the kernel variable's > > source code, printed out everything I could. The experience was very > > bad. > > > > It might be confusing, I agree, but the alternative is just a waste of > memory just to match the BTF definition of a DATASEC, which describes > externs. It seems sloppy to allocate a bunch of unused memory just to > match the kernel's variable size, while in reality we either use 8 > bytes used (for typeless externs, storing ksym address) or none (for > typed externs). > > Another alternative is to not specify BTF ID for .ksyms map, but it's > not great for typeless externs case, as we are losing all type info > completely. Trade-offs... > I see. It looks like rewriting all ksym externs' type to integers is the most straightforward solution here, though I felt a bit hacky. I can record the btf_id of the var's type before rewriting, so bpf_core_type_are_compat() can find the true type for comparison. One good thing about recording the type's btf_id is that it can be used to tell whether the ksym extern is typed or not, before vmlinux_btf_id gets its value. I will think about this and try the alternatives a bit more and follow up if I come up with a better solution. Thanks! [...]