Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp1905013pxk; Sun, 13 Sep 2020 21:58:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwVVqki4ViORdZlwFwL1NldvJI1ZaehFlM8BK/cP0X2rqSNdfGxyahtKalRqhjl+DDvkl2W X-Received: by 2002:a17:906:3495:: with SMTP id g21mr12595803ejb.121.1600059492136; Sun, 13 Sep 2020 21:58:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600059492; cv=none; d=google.com; s=arc-20160816; b=iTr5LMw99RJr/j7+FqYnyx9z9JCX8sOeN7cduXo2/kn0mAxCFvKPKmst/uNZlNmlnO Wz2GkND7hdFVheGatAUO9nbUM2AO+MVrKBSGd0NenF98RdThm6UQoWj+tkg8VtnarmG0 vjmnJRJGUhcLkaAcx+j7DkhJAIr3xEK2TMj+w+SQ41zaZXiky6gQ+NpgebK1U795BN6w Gj6FutKA/M941C4BE5XcPucXAPFIKYlAwoDExnFsQ9XrlFZOF0IFfQIHaz1dB9HI9HxR P1lM/8YCQL/4pYtcS9XaM65YJ491cqxIsLQRJ+YADOZAlRHg5QnZsU/7BfniYwCKxPBE tgEA== 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=evGZg+iA2HTquYUzwgwC0BxjcVLbRURc4vJR44PQwEM=; b=sQmaGYHlgwZg3Weom81DiSWqAMmi/beI5fpFylc3qf19sK9B9frJ0NtfJcV1yI8uDG VrfaGLEe7dy9XZOfK//JilhXHhOWS/IhdUaUCSYy45dcw2HTYntq5QVC6WeR2n5Ux/Tb Mj/5XT2P75TbQp3APSBwmlM8s5pLbN/sTVPhp46CnUmwXJygpH5rH23lZT4+8PN82s1I BSsuKzsNbxK+Vg6FNf+rinLYMgghgf1+h6G3ivQiee0fnXgJ42BrFyMyhuHmo3NzB/7D /ry+r0TFcLKoIZkxA4aNFboSx6ljxXHutv8fLGmXFY3mO4MuR7tFlZdkxRBpZ+eqI8O5 m8qA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Akt6vxLk; 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 w20si6172855ejb.217.2020.09.13.21.57.49; Sun, 13 Sep 2020 21:58:12 -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=Akt6vxLk; 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 S1726039AbgINE5L (ORCPT + 99 others); Mon, 14 Sep 2020 00:57:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:34170 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725991AbgINE5F (ORCPT ); Mon, 14 Sep 2020 00:57:05 -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 B46F6C061788 for ; Sun, 13 Sep 2020 21:57:03 -0700 (PDT) Received: by mail-ej1-x644.google.com with SMTP id p9so21271439ejf.6 for ; Sun, 13 Sep 2020 21:57:03 -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=evGZg+iA2HTquYUzwgwC0BxjcVLbRURc4vJR44PQwEM=; b=Akt6vxLkCgqM48vOMLd1Gltfm0RnwaycnqFoeNCAOQU7+ijShRIOJZOvJG/nVQ1Xt6 shuHX/QSWB3+dgMKyDCoNRHsPEy+ob2iE6JecXi0LUMq42u8dyV4ru6Oblx8OArD6aD2 gF63DVWiuljt3d94ilfE168QIdcDhCSSCwCOmW4nTT2dSJ7gGkGmBVtvzeKVCpFRwy9t koIKkFkihH29FUbOkJ65c4R7SQFny7637R1DVvRjfoe0qkGa7BWunmCueypAP9mFs64B gWGLCyk39i0LRC0leNtRFCp2SjB9TRT+rYj0thOv9Gzn6lEDPGcS+CPBLJY8zPGvE1Hc 55oA== 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=evGZg+iA2HTquYUzwgwC0BxjcVLbRURc4vJR44PQwEM=; b=hHFFVhKxKXS4QHWtcie/Q9ErI9CHvXnfqewfgHXwMufEHFr5RAAymjRf/AvY1FIze7 TCtDOQr6V8/udZ/imWla1y/lxxOLr+LG+uSqRh449Qg2IB/UwszEYb0LePzAWLiv7fYa kqLuQuCNleubH2RoC0oiHi+MQ5sJgYqxHoKGpIY+UaGgs9CULkRxLUdKelTCqu4G8QiM +4ZLyOxlebU7KJM2IPVQQmzvL+3mNl8DAAVwM9lhRfKy45KKfHd9YlSQvFp8TLJRredc r11qkRxa23zT26LvAmuAY5oOoX4+akSIz3zULBcsh9gzAJ2GyId7yCCs+r/pL433sYsx F2QA== X-Gm-Message-State: AOAM533fhqYZP6Axuql6tUfWoTpEkegt/dsZZJz/VfWMKo+6+DFdmbPE /hY3iKi7RbfO3LcR/+XugAiN29wkzAjClVjrezywZA== X-Received: by 2002:a17:907:110f:: with SMTP id qu15mr13388357ejb.359.1600059422196; Sun, 13 Sep 2020 21:57:02 -0700 (PDT) MIME-Version: 1.0 References: <20200903223332.881541-1-haoluo@google.com> <20200903223332.881541-3-haoluo@google.com> In-Reply-To: From: Hao Luo Date: Sun, 13 Sep 2020 21:56:51 -0700 Message-ID: Subject: Re: [PATCH bpf-next v2 2/6] 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 Will follow the libbpf logging convention. Thanks for the suggestions. On Fri, Sep 4, 2020 at 12:34 PM Andrii Nakryiko wrote: > > On Thu, Sep 3, 2020 at 3:34 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 > > --- > > Logic looks correct, but I have complaints about libbpf logging > consistency, please see suggestions below. > > > tools/lib/bpf/libbpf.c | 116 ++++++++++++++++++++++++++++++++++++----- > > 1 file changed, 102 insertions(+), 14 deletions(-) > > > > [...] > > > @@ -3119,6 +3130,8 @@ static int bpf_object__collect_externs(struct bpf_object *obj) > > vt->type = int_btf_id; > > vs->offset = off; > > vs->size = sizeof(int); > > + pr_debug("ksym var_secinfo: var '%s', type #%d, size %d, offset %d\n", > > + ext->name, vt->type, vs->size, vs->offset); > > debug leftover? > I was thinking we should leave a debug message when some entries in BTF are modified. It's probably unnecessary, as I'm thinking of it right now. I will remove this in v3. > > } > > sec->size = off; > > } > > @@ -5724,8 +5737,13 @@ bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj) > > insn[0].imm = obj->maps[obj->kconfig_map_idx].fd; > > insn[1].imm = ext->kcfg.data_off; > > } else /* EXT_KSYM */ { > > - insn[0].imm = (__u32)ext->ksym.addr; > > - insn[1].imm = ext->ksym.addr >> 32; > > + if (ext->ksym.type_id) { /* typed ksyms */ > > + insn[0].src_reg = BPF_PSEUDO_BTF_ID; > > + insn[0].imm = ext->ksym.vmlinux_btf_id; > > + } else { /* typeless ksyms */ > > + insn[0].imm = (__u32)ext->ksym.addr; > > + insn[1].imm = ext->ksym.addr >> 32; > > + } > > } > > break; > > case RELO_CALL: > > @@ -6462,10 +6480,72 @@ static int bpf_object__read_kallsyms_file(struct bpf_object *obj) > > return err; > > } > > > > +static int bpf_object__resolve_ksyms_btf_id(struct bpf_object *obj) > > +{ > > + struct extern_desc *ext; > > + int i, id; > > + > > + if (!obj->btf_vmlinux) { > > + pr_warn("support of typed ksyms needs kernel btf.\n"); > > + return -ENOENT; > > + } > > This check shouldn't be needed, you'd either successfully load > btf_vmlinux by now or will fail earlier, because BTF is required but > not found. > > > + > > + for (i = 0; i < obj->nr_extern; i++) { > > + const struct btf_type *targ_var, *targ_type; > > + __u32 targ_type_id, local_type_id; > > + int ret; > > + > > + ext = &obj->externs[i]; > > + if (ext->type != EXT_KSYM || !ext->ksym.type_id) > > + continue; > > + > > + id = btf__find_by_name_kind(obj->btf_vmlinux, ext->name, > > + BTF_KIND_VAR); > > + if (id <= 0) { > > + pr_warn("no btf entry for ksym '%s' in vmlinux.\n", > > + ext->name); > > please try to stick to consistent style of comments: > > "extern (ksym) '%s': failed to find BTF ID in vmlinux BTF" or > something like that > > > > + return -ESRCH; > > + } > > + > > + /* find target type_id */ > > + targ_var = btf__type_by_id(obj->btf_vmlinux, id); > > + targ_type = skip_mods_and_typedefs(obj->btf_vmlinux, > > + targ_var->type, > > + &targ_type_id); > > + > > + /* find local type_id */ > > + local_type_id = ext->ksym.type_id; > > + > > + ret = bpf_core_types_are_compat(obj->btf_vmlinux, targ_type_id, > > + obj->btf, local_type_id); > > you reversed the order, it's always local btf/id, then target btf/id. > > > + if (ret <= 0) { > > + const struct btf_type *local_type; > > + const char *targ_name, *local_name; > > + > > + local_type = btf__type_by_id(obj->btf, local_type_id); > > + targ_name = btf__name_by_offset(obj->btf_vmlinux, > > + targ_type->name_off); > > + local_name = btf__name_by_offset(obj->btf, > > + local_type->name_off); > > it's a bit unfortunate that we get the name of an already resolved > type, because if you have a typedef to anon struct, this will give you > an empty string. I don't know how much of a problem that would be, so > I think it's fine to leave it as is, and fix it if it's a problem in > practice. > > > + > > + pr_warn("ksym '%s' expects type '%s' (vmlinux_btf_id: #%d), " > > + "but got '%s' (btf_id: #%d)\n", ext->name, > > + targ_name, targ_type_id, local_name, local_type_id); > > same thing, please stay consistent in logging format. Check > bpf_core_dump_spec() for how BTF type info is usually emitted > throughout libbpf: > > "extern (ksym): incompatible types, expected [%d] %s %s, but kernel > has [%d] %s %s\n" > > there is a btf_kind_str() helper to resolve kind to a string representation. > > > > + return -EINVAL; > > + } > > + > > + ext->is_set = true; > > + ext->ksym.vmlinux_btf_id = id; > > + pr_debug("extern (ksym) %s=vmlinux_btf_id(#%d)\n", ext->name, id); > > "extern (ksym) '%s': resolved to [%d] %s %s\n", similar to above > suggestion. This "[%d]" format is very consistently used for BTF IDs > throughout, so it will be familiar and recognizable for people that > had to deal with this in libbpf logs. > > > + } > > + return 0; > > +} > > + > > static int bpf_object__resolve_externs(struct bpf_object *obj, > > const char *extra_kconfig) > > { > > - bool need_config = false, need_kallsyms = false; > > + bool need_kallsyms = false, need_vmlinux_btf = false; > > + bool need_config = false; > > nit: doesn't make sense to change the existing source code line at > all. Just add `bool need_vmlinux_btf = false;` on a new line? Or we > can split all these bools into 3 separate lines, if you prefer. > > > struct extern_desc *ext; > > void *kcfg_data = NULL; > > int err, i; > > [...]