From patchwork Wed Oct 17 23:30:42 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yonghong Song X-Patchwork-Id: 985624 X-Patchwork-Delegate: bpf@iogearbox.net Return-Path: X-Original-To: patchwork-incoming-netdev@ozlabs.org Delivered-To: patchwork-incoming-netdev@ozlabs.org Authentication-Results: ozlabs.org; spf=none (mailfrom) smtp.mailfrom=vger.kernel.org (client-ip=209.132.180.67; helo=vger.kernel.org; envelope-from=netdev-owner@vger.kernel.org; receiver=) Authentication-Results: ozlabs.org; dmarc=pass (p=none dis=none) header.from=fb.com Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=fb.com header.i=@fb.com header.b="oRM+48BM"; dkim-atps=neutral Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 42b7jP62vLz9s9h for ; Thu, 18 Oct 2018 10:31:21 +1100 (AEDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727397AbeJRH3X (ORCPT ); Thu, 18 Oct 2018 03:29:23 -0400 Received: from mx0b-00082601.pphosted.com ([67.231.153.30]:44080 "EHLO mx0b-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727291AbeJRH3W (ORCPT ); Thu, 18 Oct 2018 03:29:22 -0400 Received: from pps.filterd (m0109332.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w9HNTo5e025241 for ; Wed, 17 Oct 2018 16:31:18 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-type; s=facebook; bh=9fnjwxB+NZwc/fDCXiDsQjSyqbJUM6C2JnatS0k3CE0=; b=oRM+48BMN0WPVQYYufQvMKUqIYSCIRF8OXkgLRuYa+z+NvxeJvZMf/u5d0zXzlo7fTS3 qy+qLYYnzvAYSgaYfdwycWiDonJhUYGIx+5xCHU9nFss01Gz1daZo9/gvMJhUp+SllEj C7GCi/gz1SsploVzWuNuhwFpezg7sFcoEDE= Received: from mail.thefacebook.com ([199.201.64.23]) by mx0a-00082601.pphosted.com with ESMTP id 2n6bparmev-5 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Wed, 17 Oct 2018 16:31:18 -0700 Received: from mx-out.facebook.com (2620:10d:c081:10::13) by mail.thefacebook.com (2620:10d:c081:35::128) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA) id 15.1.1531.3; Wed, 17 Oct 2018 16:30:45 -0700 Received: by devbig003.ftw2.facebook.com (Postfix, from userid 128203) id 6DC013701A4E; Wed, 17 Oct 2018 16:30:42 -0700 (PDT) Smtp-Origin-Hostprefix: devbig From: Yonghong Song Smtp-Origin-Hostname: devbig003.ftw2.facebook.com To: , , , CC: Smtp-Origin-Cluster: ftw2c04 Subject: [PATCH bpf-next v3 09/13] tools/bpf: add support to read .BTF.ext sections Date: Wed, 17 Oct 2018 16:30:42 -0700 Message-ID: <20181017233042.1072050-5-yhs@fb.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20181017233042.1072050-1-yhs@fb.com> References: <20181017233042.1072050-1-yhs@fb.com> X-FB-Internal: Safe MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2018-10-16_14:, , signatures=0 X-Proofpoint-Spam-Reason: safe X-FB-Internal: Safe Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org The .BTF section is already available to encode types. These types can be used for map pretty print. The whole .BTF will be passed to the kernel as well for which kernel can verify and return to the user space for pretty print etc. The llvm patch at https://reviews.llvm.org/D53261 will generate .BTF section and one more section .BTF.ext. The .BTF.ext section encodes function type information and line information. Note that this patch set only supports function type info. The functionality is implemented in libbpf. The .BTF section can be directly loaded into the kernel, and the .BTF.ext section cannot. The loader may need to do some relocation and merging, similar to merging multiple code sections, before loading into the kernel. Signed-off-by: Yonghong Song --- tools/lib/bpf/btf.c | 232 +++++++++++++++++++++++++++++++++++++++++ tools/lib/bpf/btf.h | 48 +++++++++ tools/lib/bpf/libbpf.c | 53 +++++++++- 3 files changed, 329 insertions(+), 4 deletions(-) diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c index 33095fc1860b..4748e0bacd2b 100644 --- a/tools/lib/bpf/btf.c +++ b/tools/lib/bpf/btf.c @@ -37,6 +37,11 @@ struct btf { int fd; }; +struct btf_ext { + void *func_info; + __u32 func_info_len; +}; + static int btf_add_type(struct btf *btf, struct btf_type *t) { if (btf->types_size - btf->nr_types < 2) { @@ -397,3 +402,230 @@ const char *btf__name_by_offset(const struct btf *btf, __u32 offset) else return NULL; } + +static int btf_ext_validate_func_info(const struct btf_sec_func_info *sinfo, + __u32 size, btf_print_fn_t err_log) +{ + int sec_hdrlen = sizeof(struct btf_sec_func_info); + __u32 record_size = sizeof(struct bpf_func_info); + __u32 size_left = size, num_records; + __u64 total_record_size; + + while (size_left) { + if (size_left < sec_hdrlen) { + elog("BTF.ext func_info header not found"); + return -EINVAL; + } + + num_records = sinfo->num_func_info; + if (num_records == 0) { + elog("incorrect BTF.ext num_func_info"); + return -EINVAL; + } + + total_record_size = sec_hdrlen + + (__u64)num_records * record_size; + if (size_left < total_record_size) { + elog("incorrect BTF.ext num_func_info"); + return -EINVAL; + } + + size_left -= total_record_size; + sinfo = (void *)sinfo + total_record_size; + } + + return 0; +} +static int btf_ext_parse_hdr(__u8 *data, __u32 data_size, + btf_print_fn_t err_log) +{ + const struct btf_ext_header *hdr = (struct btf_ext_header *)data; + const struct btf_sec_func_info *sinfo; + __u32 meta_left, last_func_info_pos; + + if (data_size < sizeof(*hdr)) { + elog("BTF.ext header not found"); + return -EINVAL; + } + + if (hdr->magic != BTF_MAGIC) { + elog("Invalid BTF.ext magic:%x\n", hdr->magic); + return -EINVAL; + } + + if (hdr->version != BTF_VERSION) { + elog("Unsupported BTF.ext version:%u\n", hdr->version); + return -ENOTSUP; + } + + if (hdr->flags) { + elog("Unsupported BTF.ext flags:%x\n", hdr->flags); + return -ENOTSUP; + } + + meta_left = data_size - sizeof(*hdr); + if (!meta_left) { + elog("BTF.ext has no data\n"); + return -EINVAL; + } + + if (meta_left < hdr->func_info_off) { + elog("Invalid BTF.ext func_info section offset:%u\n", + hdr->func_info_off); + return -EINVAL; + } + + if (hdr->func_info_off & 0x02) { + elog("BTF.ext func_info section is not aligned to 4 bytes\n"); + return -EINVAL; + } + + last_func_info_pos = sizeof(*hdr) + hdr->func_info_off + + hdr->func_info_len; + if (last_func_info_pos > data_size) { + elog("Invalid BTF.ext func_info section size:%u\n", + hdr->func_info_len); + return -EINVAL; + } + + sinfo = (const struct btf_sec_func_info *)(data + sizeof(*hdr) + + hdr->func_info_off); + return btf_ext_validate_func_info(sinfo, hdr->func_info_len, + err_log); +} + +void btf_ext__free(struct btf_ext *btf_ext) +{ + if (!btf_ext) + return; + + free(btf_ext->func_info); + free(btf_ext); +} + +struct btf_ext *btf_ext__new(__u8 *data, __u32 size, btf_print_fn_t err_log) +{ + int hdrlen = sizeof(struct btf_ext_header); + const struct btf_ext_header *hdr; + struct btf_ext *btf_ext; + void *fdata; + int err; + + err = btf_ext_parse_hdr(data, size, err_log); + if (err) + return ERR_PTR(err); + + btf_ext = calloc(1, sizeof(struct btf_ext)); + if (!btf_ext) + return ERR_PTR(-ENOMEM); + + hdr = (const struct btf_ext_header *)data; + fdata = malloc(hdr->func_info_len); + if (!fdata) + return ERR_PTR(-ENOMEM); + + memcpy(fdata, data + hdrlen + hdr->func_info_off, hdr->func_info_len); + btf_ext->func_info = fdata; + btf_ext->func_info_len = hdr->func_info_len; + + return btf_ext; +} + +int btf_ext_reloc_init(struct btf *btf, struct btf_ext *btf_ext, + const char *sec_name, __u32 *btf_fd, + void **func_info, __u32 *func_info_len) +{ + int sec_hdrlen = sizeof(struct btf_sec_func_info); + int record_len = sizeof(struct bpf_func_info); + struct btf_sec_func_info *sinfo; + int i, remain_len, records_len; + const char *info_sec_name; + void *data; + + if (!btf || !btf_ext) { + *btf_fd = 0; + *func_info = NULL; + *func_info_len = 0; + return 0; + } + + sinfo = btf_ext->func_info; + remain_len = btf_ext->func_info_len; + *btf_fd = btf__fd(btf); + + while (remain_len > 0) { + records_len = sinfo->num_func_info * record_len; + info_sec_name = btf__name_by_offset(btf, sinfo->sec_name_off); + if (strcmp(info_sec_name, sec_name)) { + remain_len -= sec_hdrlen + records_len; + sinfo = (void *)sinfo + sec_hdrlen + records_len; + continue; + } + + data = malloc(records_len); + if (!data) + return -ENOMEM; + + memcpy(data, sinfo->data, records_len); + + /* adjust the insn_offset, the data in .BTF.ext is + * the actual byte offset, and the kernel expects + * the offset in term of bpf_insn. + */ + for (i = 0; i < sinfo->num_func_info; i++) + ((struct bpf_func_info *)data)[i].insn_offset + /= sizeof(struct bpf_insn); + + *func_info = data; + *func_info_len = records_len; + break; + } + + return 0; +} + +int btf_ext_reloc(struct btf *btf, struct btf_ext *btf_ext, + const char *sec_name, __u32 insns_cnt, + void **func_info, __u32 *func_info_len) +{ + int sec_hdrlen = sizeof(struct btf_sec_func_info); + int record_len = sizeof(struct bpf_func_info); + __u32 i, remain_len, records_len; + struct btf_sec_func_info *sinfo; + struct bpf_func_info *record; + const char *info_sec_name; + __u32 existing_flen; + void *data; + + if (!*func_info) + return 0; + + sinfo = btf_ext->func_info; + remain_len = btf_ext->func_info_len; + while (remain_len > 0) { + records_len = sinfo->num_func_info * record_len; + info_sec_name = btf__name_by_offset(btf, sinfo->sec_name_off); + if (strcmp(info_sec_name, sec_name)) { + remain_len -= sec_hdrlen + records_len; + sinfo = (void *)sinfo + sec_hdrlen + records_len; + continue; + } + + existing_flen = *func_info_len; + data = realloc(*func_info, existing_flen + records_len); + if (!data) + return -ENOMEM; + + memcpy(data + existing_flen, sinfo->data, records_len); + record = data + existing_flen; + for (i = 0; i < sinfo->num_func_info; i++) + record[i].insn_offset = + record[i].insn_offset/sizeof(struct bpf_insn) + + insns_cnt; + *func_info = data; + *func_info_len = existing_flen + records_len; + break; + } + + return 0; +} diff --git a/tools/lib/bpf/btf.h b/tools/lib/bpf/btf.h index b77e7080f7e7..513e55c1a928 100644 --- a/tools/lib/bpf/btf.h +++ b/tools/lib/bpf/btf.h @@ -11,10 +11,49 @@ #endif #define BTF_ELF_SEC ".BTF" +#define BTF_EXT_ELF_SEC ".BTF.ext" struct btf; +struct btf_ext; struct btf_type; +/* + * The .BTF.ext ELF section layout defined as + * struct btf_ext_header + * func_info and line_info + * + * The func_info subsection layout: + * struct btf_sec_func_info for section #1 + * a list of bpf_func_info records for section #1 + * where struct bpf_func_info is defined in include/uapi/linux/bpf.h + * struct btf_sec_func_info for section #2 + * a list of bpf_func_info records for section #2 + * ...... + * + * NOTE: the line_info in the btf_ext_header below is included in order + * to sync with what llvm generates. line_info support is not + * in linux yet. + */ +struct btf_ext_header { + __u16 magic; + __u8 version; + __u8 flags; + __u32 hdr_len; + + /* All offsets are in bytes relative to the end of this header */ + __u32 func_info_off; + __u32 func_info_len; + __u32 line_info_off; + __u32 line_info_len; +}; + +struct btf_sec_func_info { + __u32 sec_name_off; + __u32 num_func_info; + /* followed by num_func_info number of bpf_func_info records */ + __u8 data[0]; +}; + typedef int (*btf_print_fn_t)(const char *, ...) __attribute__((format(printf, 1, 2))); @@ -29,4 +68,13 @@ LIBBPF_API int btf__resolve_type(const struct btf *btf, __u32 type_id); LIBBPF_API int btf__fd(const struct btf *btf); LIBBPF_API const char *btf__name_by_offset(const struct btf *btf, __u32 offset); +struct btf_ext *btf_ext__new(__u8 *data, __u32 size, btf_print_fn_t err_log); +void btf_ext__free(struct btf_ext *btf_ext); +int btf_ext_reloc_init(struct btf *btf, struct btf_ext *btf_ext, + const char *sec_name, __u32 *btf_fd, + void **func_info, __u32 *func_info_len); +int btf_ext_reloc(struct btf *btf, struct btf_ext *btf_ext, + const char *sec_name, __u32 insns_cnt, void **func_info, + __u32 *func_info_len); + #endif /* __LIBBPF_BTF_H */ diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index bd71efcc53be..2ea3b1467d47 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -151,6 +151,9 @@ struct bpf_program { bpf_program_clear_priv_t clear_priv; enum bpf_attach_type expected_attach_type; + __u32 btf_fd; + void *func_info; + __u32 func_info_len; }; struct bpf_map { @@ -207,6 +210,7 @@ struct bpf_object { struct list_head list; struct btf *btf; + struct btf_ext *btf_ext; void *priv; bpf_object_clear_priv_t clear_priv; @@ -783,6 +787,15 @@ static int bpf_object__elf_collect(struct bpf_object *obj, int flags) BTF_ELF_SEC, PTR_ERR(obj->btf)); obj->btf = NULL; } + } else if (strcmp(name, BTF_EXT_ELF_SEC) == 0) { + obj->btf_ext = btf_ext__new(data->d_buf, data->d_size, + __pr_debug); + if (IS_ERR(obj->btf_ext)) { + pr_warning("Error loading ELF section %s: %ld. Ignored and continue.\n", + BTF_EXT_ELF_SEC, + PTR_ERR(obj->btf_ext)); + obj->btf_ext = NULL; + } } else if (sh.sh_type == SHT_SYMTAB) { if (obj->efile.symbols) { pr_warning("bpf: multiple SYMTAB in %s\n", @@ -1166,6 +1179,7 @@ bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj, struct bpf_insn *insn, *new_insn; struct bpf_program *text; size_t new_cnt; + int err; if (relo->type != RELO_CALL) return -LIBBPF_ERRNO__RELOC; @@ -1188,6 +1202,16 @@ bpf_program__reloc_text(struct bpf_program *prog, struct bpf_object *obj, pr_warning("oom in prog realloc\n"); return -ENOMEM; } + + err = btf_ext_reloc(obj->btf, obj->btf_ext, text->section_name, + prog->insns_cnt, &prog->func_info, + &prog->func_info_len); + if (err) { + pr_warning("error in btf_ext_reloc for sec %s\n", + text->section_name); + return err; + } + memcpy(new_insn + prog->insns_cnt, text->insns, text->insns_cnt * sizeof(*insn)); prog->insns = new_insn; @@ -1207,7 +1231,19 @@ bpf_program__relocate(struct bpf_program *prog, struct bpf_object *obj) { int i, err; - if (!prog || !prog->reloc_desc) + if (!prog) + return 0; + + err = btf_ext_reloc_init(obj->btf, obj->btf_ext, prog->section_name, + &prog->btf_fd, &prog->func_info, + &prog->func_info_len); + if (err) { + pr_warning("err in btf_ext_reloc_init for sec %s\n", + prog->section_name); + return err; + } + + if (!prog->reloc_desc) return 0; for (i = 0; i < prog->nr_reloc; i++) { @@ -1297,7 +1333,8 @@ static int bpf_object__collect_reloc(struct bpf_object *obj) static int load_program(enum bpf_prog_type type, enum bpf_attach_type expected_attach_type, const char *name, struct bpf_insn *insns, int insns_cnt, - char *license, __u32 kern_version, int *pfd, int prog_ifindex) + char *license, __u32 kern_version, int *pfd, int prog_ifindex, + u32 btf_fd, struct bpf_func_info *func_info, u32 func_info_len) { struct bpf_load_program_attr load_attr; char *cp, errmsg[STRERR_BUFSIZE]; @@ -1313,6 +1350,9 @@ load_program(enum bpf_prog_type type, enum bpf_attach_type expected_attach_type, load_attr.license = license; load_attr.kern_version = kern_version; load_attr.prog_ifindex = prog_ifindex; + load_attr.prog_btf_fd = btf_fd; + load_attr.func_info = func_info; + load_attr.func_info_len = func_info_len; if (!load_attr.insns || !load_attr.insns_cnt) return -EINVAL; @@ -1396,7 +1436,9 @@ bpf_program__load(struct bpf_program *prog, err = load_program(prog->type, prog->expected_attach_type, prog->name, prog->insns, prog->insns_cnt, license, kern_version, &fd, - prog->prog_ifindex); + prog->prog_ifindex, + prog->btf_fd, prog->func_info, + prog->func_info_len); if (!err) prog->instances.fds[0] = fd; goto out; @@ -1428,7 +1470,9 @@ bpf_program__load(struct bpf_program *prog, prog->name, result.new_insn_ptr, result.new_insn_cnt, license, kern_version, &fd, - prog->prog_ifindex); + prog->prog_ifindex, + prog->btf_fd, prog->func_info, + prog->func_info_len); if (err) { pr_warning("Loading the %dth instance of program '%s' failed\n", @@ -1844,6 +1888,7 @@ void bpf_object__close(struct bpf_object *obj) bpf_object__elf_finish(obj); bpf_object__unload(obj); btf__free(obj->btf); + btf_ext__free(obj->btf_ext); for (i = 0; i < obj->nr_maps; i++) { zfree(&obj->maps[i].name);