Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp1397822pxa; Thu, 20 Aug 2020 10:12:13 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxqz71OzbagEUOKoMP2WQFtems8wtR6HZy6SQ9BhEVZJmGRkPpKiCfnm2TlmEGVp51jitSD X-Received: by 2002:a17:906:9717:: with SMTP id k23mr4333134ejx.493.1597943533524; Thu, 20 Aug 2020 10:12:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597943533; cv=none; d=google.com; s=arc-20160816; b=pXL+R82RI+pP2M7EIXL36odIa5koDB3lWO6BzNv6vm6+j1gyazTaXUFzxz0kPLYBtx mzQmSx4ltBBcz2/CBGaSKry26/CfchQzefLfxYVJ/XNYbseDJVSC3nbONrFsk2MIyBHN oFKG74PjZTQWzL11cvy7hP8+w69EmlFRlBLe4pbbCY5sQIXKsSdtinez3tJE8NgEQSmY mItx7veJwxYbh50fxGwsUTT86+b4Uxnz1lJNEUk9Cm+slahr8b6xCLRWVaEYpD3+gkZM 23U6GaynNJOA2BtgHl1x82kSNMs+uTOqdNXb2Km2//lOx9GxvlqUvt4IXfZDgGCswQNm xa2A== 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=Io5tdLMfOwzBnOTgWMWIIWlg9YYSmolrrRZ0RRvwKR8=; b=oHTDat8vfyQfaCIA5j8DKHe4uiuVdJJvAeUmRh+4VVr00fhkmJe9mjY7Bxla5ySFe6 CXu6GaaTweJd0zwcjUqMzQfoKxN63cesJJwYQDfpZLA31ntyx/83FhQnnGgo/sgNr78c /KhEZ5pCoZni7H6N26gVy4eqX7bi8rfxLdJJvaF6j/Rq0STSb+QV0HIKHf+pKgDqa0jh a2lCq5T+zzQHmJzUgkQun5eMgtj2CqxuPf7f+RG9Vvh5nG/K2wA+WReYa2L7u9FeDCHb Qjr9Xf+I2A0egBT3l7MDnrV13T+mxPD03GzRsaJtRk8IO/edpDxD3BDVtk6RDirZIjfk XGwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=kh+7Oi77; 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 rl7si1535039ejb.264.2020.08.20.10.11.49; Thu, 20 Aug 2020 10:12: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=@google.com header.s=20161025 header.b=kh+7Oi77; 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 S1730441AbgHTRID (ORCPT + 99 others); Thu, 20 Aug 2020 13:08:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42738 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730380AbgHTRFQ (ORCPT ); Thu, 20 Aug 2020 13:05:16 -0400 Received: from mail-ej1-x643.google.com (mail-ej1-x643.google.com [IPv6:2a00:1450:4864:20::643]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 27531C061388 for ; Thu, 20 Aug 2020 10:05:06 -0700 (PDT) Received: by mail-ej1-x643.google.com with SMTP id d6so3385154ejr.5 for ; Thu, 20 Aug 2020 10:05:06 -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=Io5tdLMfOwzBnOTgWMWIIWlg9YYSmolrrRZ0RRvwKR8=; b=kh+7Oi77zCYs0Sv7wjusq4Fc5vU9fpUjsb6r/DE/kUtxx0CQITSCQqoDaZMzjaZZ0n b1VJBkGhEFH8feITupWHQFvEFbhGDN845BnvjWUre/C1K2N+lPbdhqaIylz5UM+JGll+ GAVJaLbBjUVw3gF32YxoWxhiSe7a6YeLphd03FE7liKLJ5lz6XlIf+0N3dLp2wHQZ2P2 53QhRJ8b2V+Ssvr4++sy0/W/vhM+NV6FRxAvOqnSw4Nm2O2HY9sBxZRz1vMCOGY4jAbH UjkJOxnjy+aZJDIrRyJTTdd0XsUtqpmNCMnzV1KqEUvUC7x54C+Gl0x7HdYdkHKrC7ng JLZQ== 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=Io5tdLMfOwzBnOTgWMWIIWlg9YYSmolrrRZ0RRvwKR8=; b=lkUtkmPmp8mo23m+kYI4+QK+G7hKixNi4sOcswZdWTyatlg/RLBJFa68KVJ0AKfYdF IXGHAXh9OiMhm74t/WJ2s3LdStr5SicAB2yUxUgHfOL6N6u/sqHjJUHUb7YVdfSUEpmn lF9UTrtsh49KBaT6lvDTsc9Q4t1yuovvBF6JINbHYKdBz0oa1ghpmAma1fG1YQdjGccJ cDairqcxx9a4xevttoIGHlNXha1lGM+fG/vuvP53iRyROKo5/Ulply6PCkh2k/fmYkgH G88luCn9eQgkwfrtG0MreRAkcs5RRMxEtuTtO9gEH0aWZ5gOm51WwuPl48ZZJ/nOmy5D 8eGQ== X-Gm-Message-State: AOAM531/AWKwU8t4oEoHCInIN2XbaATzG6xf6GOIhPCREvo4x2Qkjsgh up01S4GM8NwIV9mArOLgwTiXI7LP/fbO/cXGr5whuw== X-Received: by 2002:a17:906:54d3:: with SMTP id c19mr4419389ejp.408.1597943104367; Thu, 20 Aug 2020 10:05:04 -0700 (PDT) MIME-Version: 1.0 References: <20200819224030.1615203-1-haoluo@google.com> <20200819224030.1615203-2-haoluo@google.com> <35519fec-754c-0a17-4f01-9d6e39a8a7e8@fb.com> In-Reply-To: <35519fec-754c-0a17-4f01-9d6e39a8a7e8@fb.com> From: Hao Luo Date: Thu, 20 Aug 2020 10:04:53 -0700 Message-ID: Subject: Re: [PATCH bpf-next v1 1/8] bpf: Introduce pseudo_btf_id To: Yonghong Song Cc: 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 , Arnaldo Carvalho de Melo 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 Yonghong, Thank you for taking a look. Explicitly cc'ing Arnaldo to see if he has any immediate insights. In the meantime, I'll dedicate time to investigate this issue you found. Thanks, Hao On Thu, Aug 20, 2020 at 8:23 AM Yonghong Song wrote: > > > > On 8/19/20 3:40 PM, Hao Luo wrote: > > Pseudo_btf_id is a type of ld_imm insn that associates a btf_id to a > > ksym so that further dereferences on the ksym can use the BTF info > > to validate accesses. Internally, when seeing a pseudo_btf_id ld insn, > > the verifier reads the btf_id stored in the insn[0]'s imm field and > > marks the dst_reg as PTR_TO_BTF_ID. The btf_id points to a VAR_KIND, > > which is encoded in btf_vminux by pahole. If the VAR is not of a struct > > type, the dst reg will be marked as PTR_TO_MEM instead of PTR_TO_BTF_ID > > and the mem_size is resolved to the size of the VAR's type. > > > > From the VAR btf_id, the verifier can also read the address of the > > ksym's corresponding kernel var from kallsyms and use that to fill > > dst_reg. > > > > Therefore, the proper functionality of pseudo_btf_id depends on (1) > > kallsyms and (2) the encoding of kernel global VARs in pahole, which > > should be available since pahole v1.18. > > I tried your patch with latest pahole but it did not generate > expected BTF_TYPE_VARs. My pahole head is: > f3d9054ba8ff btf_encoder: Teach pahole to store percpu variables in > vmlinux BTF. > > First I made the following changes to facilitate debugging: > diff --git a/btf_encoder.c b/btf_encoder.c > index 982f59d..f94c3a6 100644 > --- a/btf_encoder.c > +++ b/btf_encoder.c > @@ -334,6 +334,9 @@ int cu__encode_btf(struct cu *cu, int verbose, bool > force) > /* percpu variables are allocated in global space */ > if (variable__scope(var) != VSCOPE_GLOBAL) > continue; > + /* type 0 is void, probably an internal error */ > + if (var->ip.tag.type == 0) > + continue; > has_global_var = true; > head = &hash_addr[hashaddr__fn(var->ip.addr)]; > hlist_add_head(&var->tool_hnode, head); > @@ -399,8 +402,8 @@ int cu__encode_btf(struct cu *cu, int verbose, bool > force) > } > > if (verbose) > - printf("symbol '%s' of address 0x%lx encoded\n", > - sym_name, addr); > + printf("symbol '%s' of address 0x%lx encoded, > type %u\n", > + sym_name, addr, type); > > /* add a BTF_KIND_VAR in btfe->types */ > linkage = var->external ? BTF_VAR_GLOBAL_ALLOCATED : > BTF_VAR_STATIC; > diff --git a/libbtf.c b/libbtf.c > index 7a01ded..3a0d8d7 100644 > --- a/libbtf.c > +++ b/libbtf.c > @@ -304,6 +304,8 @@ static const char * const btf_kind_str[NR_BTF_KINDS] = { > [BTF_KIND_RESTRICT] = "RESTRICT", > [BTF_KIND_FUNC] = "FUNC", > [BTF_KIND_FUNC_PROTO] = "FUNC_PROTO", > + [BTF_KIND_VAR] = "VAR", > + [BTF_KIND_DATASEC] = "DATASEC", > }; > > static const char *btf_elf__name_in_gobuf(const struct btf_elf *btfe, > uint32_t offset) > @@ -671,7 +673,7 @@ int32_t btf_elf__add_var_type(struct btf_elf *btfe, > uint32_t type, uint32_t name > return -1; > } > > - btf_elf__log_type(btfe, &t.type, false, false, "type=%u name=%s", > + btf_elf__log_type(btfe, &t.type, false, false, "type=%u name=%s\n", > t.type.type, btf_elf__name_in_gobuf(btfe, > t.type.name_off)); > > return btfe->type_index; > > It would be good if you can add some of the above changes to > pahole for easier `pahole -JV` dump. > > With the above change, I only got static per cpu variables. > For example, > static DEFINE_PER_CPU(unsigned int , mirred_rec_level); > in net/sched/act_mirred.c. > > [10] INT 'unsigned int' size=4 bits_offset=0 nr_bits=32 encoding=(none) > [74536] VAR 'mirred_rec_level' type_id=10, linkage=static > > The dwarf debug_info entry for `mirred_rec_level`: > 0x0001d8d6: DW_TAG_variable > DW_AT_name ("mirred_rec_level") > DW_AT_decl_file > ("/data/users/yhs/work/net-next/net/sched/act_mirred.c") > DW_AT_decl_line (31) > DW_AT_decl_column (0x08) > DW_AT_type (0x00000063 "unsigned int") > DW_AT_location (DW_OP_addr 0x0) > It is not a declaration and it contains type. > > All global per cpu variables do not have BTF_KIND_VAR generated. > I did a brief investigation and found this mostly like to be a > pahole issue. For example, for global per cpu variable > bpf_prog_active, > include/linux/bpf.h > DECLARE_PER_CPU(int , bpf_prog_active); > kernel/bpf/syscall.c > DEFINE_PER_CPU(int , bpf_prog_active); > it is declared in the header include/linux/bpf.h and > defined in kernel/bpf/syscall.c. > > In many cu's, you will see: > 0x0003592a: DW_TAG_variable > DW_AT_name ("bpf_prog_active") > DW_AT_decl_file > ("/data/users/yhs/work/net-next/include/linux/bpf.h") > DW_AT_decl_line (1074) > DW_AT_decl_column (0x01) > DW_AT_type (0x0001fa7e "int") > DW_AT_external (true) > DW_AT_declaration (true) > > In kernel/bpf/syscall.c, I see > the following dwarf entry for real definition: > 0x00013534: DW_TAG_variable > DW_AT_name ("bpf_prog_active") > DW_AT_decl_file > ("/data/users/yhs/work/net-next/include/linux/bpf.h") > DW_AT_decl_line (1074) > DW_AT_decl_column (0x01) > DW_AT_type (0x000000d6 "int") > DW_AT_external (true) > DW_AT_declaration (true) > > 0x00021a25: DW_TAG_variable > DW_AT_specification (0x00013534 "bpf_prog_active") > DW_AT_decl_file > ("/data/users/yhs/work/net-next/kernel/bpf/syscall.c") > DW_AT_decl_line (43) > DW_AT_location (DW_OP_addr 0x0) > > Note that for the second entry DW_AT_specification points to the > declaration. I am not 100% sure whether pahole handle this properly or > not. It generates a type id 0 (void) for bpf_prog_active variable. > > Could you investigate this a little more? > > I am using gcc 8.2.1. Using kernel default dwarf (dwarf 2) exposed > the above issue. Tries to use dwarf 4 and the problem still exists. > > > > > > Signed-off-by: Hao Luo > > --- > > include/linux/btf.h | 15 +++++++++ > > include/uapi/linux/bpf.h | 38 ++++++++++++++++------ > > kernel/bpf/btf.c | 15 --------- > > kernel/bpf/verifier.c | 68 ++++++++++++++++++++++++++++++++++++++++ > > 4 files changed, 112 insertions(+), 24 deletions(-) > > > [...]