From patchwork Sun Feb 4 18:40:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Daniel Xu X-Patchwork-Id: 13544766 X-Patchwork-Delegate: bpf@iogearbox.net Received: from wout2-smtp.messagingengine.com (wout2-smtp.messagingengine.com [64.147.123.25]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AEB2F24A1D for ; Sun, 4 Feb 2024 18:40:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=64.147.123.25 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707072058; cv=none; b=hyV7CQCOcHLkCViz5t/f3caLICEO1VOHAwsUDzrr+4yLVMjBEWXrOfGtCbq8T3ob3qbjnSc5UN4ippSG+fl4xWlf/c+LhbCmGSENEXXB2XgYn1AKzcf/3tEup97v3ZxMUMcUogx4GsruxpCdOXLz8YwSPiJdC9ZYPkNZMH9Qgjw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707072058; c=relaxed/simple; bh=kG6Dn+nZzRRhkgimS5kmRK0pcGdibqrQ9V6Wq9aR1Rg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=WLIQrdDThUQzx+VxvX3mD9VP7d2nEtzl840rRTD4xOvbKAmivQ5vN+o2tXdDNscDAdDGbkFZHLfqch01GCZalj64pn4m8JJAXt9uQr8u1E2/JVP4gpnGo0CHCymDn73OLckyeE1CqqWL7TYqujLbESLon1I0NroSDU+bKhcNMoE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=dxuuu.xyz; spf=pass smtp.mailfrom=dxuuu.xyz; dkim=pass (2048-bit key) header.d=dxuuu.xyz header.i=@dxuuu.xyz header.b=l0vPqKjT; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=bl1wPH5m; arc=none smtp.client-ip=64.147.123.25 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=dxuuu.xyz Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=dxuuu.xyz Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=dxuuu.xyz header.i=@dxuuu.xyz header.b="l0vPqKjT"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="bl1wPH5m" Received: from compute7.internal (compute7.nyi.internal [10.202.2.48]) by mailout.west.internal (Postfix) with ESMTP id 24B903200AAA; Sun, 4 Feb 2024 13:40:55 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute7.internal (MEProxy); Sun, 04 Feb 2024 13:40:55 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dxuuu.xyz; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm2; t=1707072054; x= 1707158454; bh=q4KU42Tn49M//Kxu0V2yh1PP1789rWkbBWIKAi+MM+U=; b=l 0vPqKjTvgJA093SNhRD8xpsh1/bTPh0pbW8b71N3QVo4P619Vucc3mLFLCHO3rkV ADuU5acsa2JoW/aJhvM2ZljxEbMlX2FVAviFxAQIUSVlPgkRy7JfYeu8vXXERgHg +8vlALgaPNprHJ8gMIC6QrNLzlB+FeUSJ+1l9AzOLO/rEkvEbyM+HUVrs0WvRE4z UhEcM2W39M2/4PfF138tkK38P6YQ5iu0vNNJbV0wjv5jSllsn1FOUWXtSdB/ADWR zRKBZdbAxUZcrvepVa9stulCHrzfFsNMBjYvxVziIDmdezqK0I0hIjFHaZ0L3HwT /02oj+nJOa0qaWHegJZGA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm3; t=1707072054; x= 1707158454; bh=q4KU42Tn49M//Kxu0V2yh1PP1789rWkbBWIKAi+MM+U=; b=b l1wPH5m+CEl8WlhzsXytSzCuSN4FtoHX+JbB0Qf6jth74D71LAGtwQr8N1W7UrJF /DDR6k8yUQoMO6IlZ2PCPuizR1y8HCHcdmQHjHQ5yoM5wfjLPETPCGYGEK/wKkPa b41GLZCp2+zIVuo+g/8Sn4nmgW/xt2TThdyn3ZNfo1dHhT3dWDJoz94gwiR1rgOK MQsYGlVZVAITXJIVUqlLBA1jsSrR1AfcALPmN9qobU1GW9pGrnEKAQ7NVPhbgODG xZUg29xuR2+oVFFfhE1W0NqlWQtZAZtJM53bPjx0NaELRdjueM+Lo1Sn8XqMeCIc k9XpTQA7jj929DnceWxbQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvkedrfedukedgudduhecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecufghrlhcuvffnffculdejtddmnecujfgurhephf fvvefufffkofgjfhgggfestdekredtredttdenucfhrhhomhepffgrnhhivghlucgiuhcu oegugihusegugihuuhhurdighiiiqeenucggtffrrghtthgvrhhnpefgfefggeejhfduie ekvdeuteffleeifeeuvdfhheejleejjeekgfffgefhtddtteenucevlhhushhtvghrufhi iigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegugihusegugihuuhhurdighiii X-ME-Proxy: Feedback-ID: i6a694271:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 4 Feb 2024 13:40:53 -0500 (EST) From: Daniel Xu To: acme@kernel.org, jolsa@kernel.org, quentin@isovalent.com, alan.maguire@oracle.com Cc: andrii.nakryiko@gmail.com, ast@kernel.org, daniel@iogearbox.net, bpf@vger.kernel.org Subject: [PATCH dwarves v4 2/2] pahole: Inject kfunc decl tags into BTF Date: Sun, 4 Feb 2024 11:40:38 -0700 Message-ID: <28e81ccf28d6dd33f6db50af6526dc1770502b8d.1707071969.git.dxu@dxuuu.xyz> X-Mailer: git-send-email 2.42.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: bpf@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 This commit teaches pahole to parse symbols in .BTF_ids section in vmlinux and discover exported kfuncs. Pahole then takes the list of kfuncs and injects a BTF_KIND_DECL_TAG for each kfunc. Example of encoding: $ bpftool btf dump file .tmp_vmlinux.btf | rg "DECL_TAG 'bpf_kfunc'" | wc -l 121 $ bpftool btf dump file .tmp_vmlinux.btf | rg 56337 [56337] FUNC 'bpf_ct_change_timeout' type_id=56336 linkage=static [127861] DECL_TAG 'bpf_kfunc' type_id=56337 component_idx=-1 This enables downstream users and tools to dynamically discover which kfuncs are available on a system by parsing vmlinux or module BTF, both available in /sys/kernel/btf. Signed-off-by: Daniel Xu Tested-by: Eduard Zingerman --- btf_encoder.c | 359 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 359 insertions(+) diff --git a/btf_encoder.c b/btf_encoder.c index e325f66..d6a561c 100644 --- a/btf_encoder.c +++ b/btf_encoder.c @@ -34,6 +34,21 @@ #include #define BTF_ENCODER_MAX_PROTO 512 +#define BTF_IDS_SECTION ".BTF_ids" +#define BTF_ID_FUNC_PFX "__BTF_ID__func__" +#define BTF_ID_SET8_PFX "__BTF_ID__set8__" +#define BTF_SET8_KFUNCS (1 << 0) +#define BTF_KFUNC_TYPE_TAG "bpf_kfunc" + +/* Adapted from include/linux/btf_ids.h */ +struct btf_id_set8 { + uint32_t cnt; + uint32_t flags; + struct { + uint32_t id; + uint32_t flags; + } pairs[]; +}; /* state used to do later encoding of saved functions */ struct btf_encoder_state { @@ -95,6 +110,17 @@ struct btf_encoder { } functions; }; +struct btf_func { + const char *name; + int type_id; +}; + +/* Half open interval representing range of addresses containing kfuncs */ +struct btf_kfunc_set_range { + size_t start; + size_t end; +}; + static LIST_HEAD(encoders); static pthread_mutex_t encoders__lock = PTHREAD_MUTEX_INITIALIZER; @@ -1353,6 +1379,331 @@ out: return err; } +/* Returns if `sym` points to a kfunc set */ +static int is_sym_kfunc_set(GElf_Sym *sym, const char *name, Elf_Data *idlist, size_t idlist_addr) +{ + void *ptr = idlist->d_buf; + struct btf_id_set8 *set; + bool is_set8; + int off; + + /* kfuncs are only found in BTF_SET8's */ + is_set8 = !strncmp(name, BTF_ID_SET8_PFX, sizeof(BTF_ID_SET8_PFX) - 1); + if (!is_set8) + return false; + + off = sym->st_value - idlist_addr; + if (off >= idlist->d_size) { + fprintf(stderr, "%s: symbol '%s' out of bounds\n", __func__, name); + return false; + } + + /* Check the set8 flags to see if it was marked as kfunc */ + set = ptr + off; + return set->flags & BTF_SET8_KFUNCS; +} + +/* + * Parse BTF_ID symbol and return the func name. + * + * Returns: + * Caller-owned string containing func name if successful. + * NULL if !func or on error. + */ +static char *get_func_name(const char *sym) +{ + char *func, *end; + + if (strncmp(sym, BTF_ID_FUNC_PFX, sizeof(BTF_ID_FUNC_PFX) - 1)) + return NULL; + + /* Strip prefix */ + func = strdup(sym + sizeof(BTF_ID_FUNC_PFX) - 1); + + /* Strip suffix */ + end = strrchr(func, '_'); + if (!end || *(end - 1) != '_') { + free(func); + return NULL; + } + *(end - 1) = '\0'; + + return func; +} + +static int btf_func_cmp(const void *_a, const void *_b) +{ + const struct btf_func *a = _a; + const struct btf_func *b = _b; + + return strcmp(a->name, b->name); +} + +/* + * Collects all functions described in BTF. + * Returns non-zero on error. + */ +static int btf_encoder__collect_btf_funcs(struct btf_encoder *encoder, struct gobuffer *funcs) +{ + struct btf *btf = encoder->btf; + int nr_types, type_id; + int err = -1; + + /* First collect all the func entries into an array */ + nr_types = btf__type_cnt(btf); + for (type_id = 1; type_id < nr_types; type_id++) { + const struct btf_type *type; + struct btf_func func = {}; + const char *name; + + type = btf__type_by_id(btf, type_id); + if (!type) { + fprintf(stderr, "%s: malformed BTF, can't resolve type for ID %d\n", + __func__, type_id); + err = -EINVAL; + goto out; + } + + if (!btf_is_func(type)) + continue; + + name = btf__name_by_offset(btf, type->name_off); + if (!name) { + fprintf(stderr, "%s: malformed BTF, can't resolve name for ID %d\n", + __func__, type_id); + err = -EINVAL; + goto out; + } + + func.name = name; + func.type_id = type_id; + err = gobuffer__add(funcs, &func, sizeof(func)); + if (err < 0) + goto out; + } + + /* Now that we've collected funcs, sort them by name */ + qsort((void *)gobuffer__entries(funcs), gobuffer__nr_entries(funcs), + sizeof(struct btf_func), btf_func_cmp); + + err = 0; +out: + return err; +} + +static int btf_encoder__tag_kfunc(struct btf_encoder *encoder, struct gobuffer *funcs, const char *kfunc) +{ + struct btf_func key = { .name = kfunc }; + struct btf *btf = encoder->btf; + struct btf_func *target; + const void *base; + unsigned int cnt; + int err = -1; + + base = gobuffer__entries(funcs); + cnt = gobuffer__nr_entries(funcs); + target = bsearch(&key, base, cnt, sizeof(key), btf_func_cmp); + if (!target) { + fprintf(stderr, "%s: failed to find kfunc '%s' in BTF\n", __func__, kfunc); + goto out; + } + + /* Note we are unconditionally adding the btf_decl_tag even + * though vmlinux may already contain btf_decl_tags for kfuncs. + * We are ok to do this b/c we will later btf__dedup() to remove + * any duplicates. + */ + err = btf__add_decl_tag(btf, BTF_KFUNC_TYPE_TAG, target->type_id, -1); + if (err < 0) { + fprintf(stderr, "%s: failed to insert kfunc decl tag for '%s': %d\n", + __func__, kfunc, err); + goto out; + } + + err = 0; +out: + return err; +} + +static int btf_encoder__tag_kfuncs(struct btf_encoder *encoder) +{ + const char *filename = encoder->filename; + struct gobuffer btf_kfunc_ranges = {}; + struct gobuffer btf_funcs = {}; + Elf_Scn *symscn = NULL; + int symbols_shndx = -1; + int fd = -1, err = -1; + int idlist_shndx = -1; + Elf_Scn *scn = NULL; + size_t idlist_addr; + Elf_Data *symbols; + Elf_Data *idlist; + size_t strtabidx; + Elf *elf = NULL; + GElf_Shdr shdr; + size_t strndx; + char *secname; + int nr_syms; + int i = 0; + + fd = open(filename, O_RDONLY); + if (fd < 0) { + fprintf(stderr, "Cannot open %s\n", filename); + goto out; + } + + if (elf_version(EV_CURRENT) == EV_NONE) { + elf_error("Cannot set libelf version"); + goto out; + } + + elf = elf_begin(fd, ELF_C_READ, NULL); + if (elf == NULL) { + elf_error("Cannot update ELF file"); + goto out; + } + + /* Location symbol table and .BTF_ids sections */ + elf_getshdrstrndx(elf, &strndx); + while ((scn = elf_nextscn(elf, scn)) != NULL) { + Elf_Data *data; + + i++; + if (!gelf_getshdr(scn, &shdr)) { + elf_error("Failed to get ELF section(%d) hdr", i); + goto out; + } + + secname = elf_strptr(elf, strndx, shdr.sh_name); + if (!secname) { + elf_error("Failed to get ELF section(%d) hdr name", i); + goto out; + } + + data = elf_getdata(scn, 0); + if (!data) { + elf_error("Failed to get ELF section(%d) data", i); + goto out; + } + + if (shdr.sh_type == SHT_SYMTAB) { + symbols_shndx = i; + symscn = scn; + symbols = data; + strtabidx = shdr.sh_link; + } else if (!strcmp(secname, BTF_IDS_SECTION)) { + idlist_shndx = i; + idlist_addr = shdr.sh_addr; + idlist = data; + } + } + + /* Cannot resolve symbol or .BTF_ids sections. Nothing to do. */ + if (symbols_shndx == -1 || idlist_shndx == -1) { + err = 0; + goto out; + } + + if (!gelf_getshdr(symscn, &shdr)) { + elf_error("Failed to get ELF symbol table header"); + goto out; + } + nr_syms = shdr.sh_size / shdr.sh_entsize; + + err = btf_encoder__collect_btf_funcs(encoder, &btf_funcs); + if (err) { + fprintf(stderr, "%s: failed to collect BTF funcs\n", __func__); + goto out; + } + + /* First collect all kfunc set ranges. + * + * Note we choose not to sort these ranges and accept a linear + * search when doing lookups. Reasoning is that the number of + * sets is ~O(100) and not worth the additional code to optimize. + */ + for (i = 0; i < nr_syms; i++) { + struct btf_kfunc_set_range range = {}; + const char *name; + GElf_Sym sym; + + if (!gelf_getsym(symbols, i, &sym)) { + elf_error("Failed to get ELF symbol(%d)", i); + goto out; + } + + if (sym.st_shndx != idlist_shndx) + continue; + + name = elf_strptr(elf, strtabidx, sym.st_name); + if (!is_sym_kfunc_set(&sym, name, idlist, idlist_addr)) + continue; + + range.start = sym.st_value; + range.end = sym.st_value + sym.st_size; + gobuffer__add(&btf_kfunc_ranges, &range, sizeof(range)); + } + + /* Now inject BTF with kfunc decl tag for detected kfuncs */ + for (i = 0; i < nr_syms; i++) { + const struct btf_kfunc_set_range *ranges; + unsigned int ranges_cnt; + char *func, *name; + GElf_Sym sym; + bool found; + int err; + int j; + + if (!gelf_getsym(symbols, i, &sym)) { + elf_error("Failed to get ELF symbol(%d)", i); + goto out; + } + + if (sym.st_shndx != idlist_shndx) + continue; + + name = elf_strptr(elf, strtabidx, sym.st_name); + func = get_func_name(name); + if (!func) + continue; + + /* Check if function belongs to a kfunc set */ + ranges = gobuffer__entries(&btf_kfunc_ranges); + ranges_cnt = gobuffer__nr_entries(&btf_kfunc_ranges); + found = false; + for (j = 0; j < ranges_cnt; j++) { + size_t addr = sym.st_value; + + if (ranges[j].start <= addr && addr < ranges[j].end) { + found = true; + break; + } + } + if (!found) { + free(func); + continue; + } + + err = btf_encoder__tag_kfunc(encoder, &btf_funcs, func); + if (err) { + fprintf(stderr, "%s: failed to tag kfunc '%s'\n", __func__, func); + free(func); + goto out; + } + free(func); + } + + err = 0; +out: + __gobuffer__delete(&btf_funcs); + __gobuffer__delete(&btf_kfunc_ranges); + if (elf) + elf_end(elf); + if (fd != -1) + close(fd); + return err; +} + int btf_encoder__encode(struct btf_encoder *encoder) { int err; @@ -1367,6 +1718,14 @@ int btf_encoder__encode(struct btf_encoder *encoder) if (btf__type_cnt(encoder->btf) == 1) return 0; + /* Note vmlinux may already contain btf_decl_tag's for kfuncs. So + * take care to call this before btf_dedup(). + */ + if (encoder->tag_kfuncs && btf_encoder__tag_kfuncs(encoder)) { + fprintf(stderr, "%s: failed to tag kfuncs!\n", __func__); + return -1; + } + if (btf__dedup(encoder->btf, NULL)) { fprintf(stderr, "%s: btf__dedup failed!\n", __func__); return -1;