From patchwork Thu Oct 21 01:43:54 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrii Nakryiko X-Patchwork-Id: 12573631 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 89844C433F5 for ; Thu, 21 Oct 2021 01:46:55 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 6CAEC61130 for ; Thu, 21 Oct 2021 01:46:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230286AbhJUBtJ convert rfc822-to-8bit (ORCPT ); Wed, 20 Oct 2021 21:49:09 -0400 Received: from mx0a-00082601.pphosted.com ([67.231.145.42]:8040 "EHLO mx0a-00082601.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229771AbhJUBtI (ORCPT ); Wed, 20 Oct 2021 21:49:08 -0400 Received: from pps.filterd (m0109333.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.16.1.2/8.16.1.2) with SMTP id 19KN9oPf021450 for ; Wed, 20 Oct 2021 18:46:53 -0700 Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com with ESMTP id 3btnf0vcmv-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Wed, 20 Oct 2021 18:46:53 -0700 Received: from intmgw001.05.prn6.facebook.com (2620:10d:c0a8:1b::d) by mail.thefacebook.com (2620:10d:c0a8:82::d) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.14; Wed, 20 Oct 2021 18:46:51 -0700 Received: by devbig019.vll3.facebook.com (Postfix, from userid 137359) id DF4D86E3E12C; Wed, 20 Oct 2021 18:44:08 -0700 (PDT) From: Andrii Nakryiko To: , , CC: , Subject: [PATCH v2 bpf-next 00/10] libbpf: support custom .rodata.*/.data.* sections Date: Wed, 20 Oct 2021 18:43:54 -0700 Message-ID: <20211021014404.2635234-1-andrii@kernel.org> X-Mailer: git-send-email 2.30.2 X-FB-Internal: Safe X-FB-Source: Intern X-Proofpoint-ORIG-GUID: NVQ2EfyM3oZAWsqfMOKJ2yk1FACjW1HJ X-Proofpoint-GUID: NVQ2EfyM3oZAWsqfMOKJ2yk1FACjW1HJ X-Proofpoint-UnRewURL: 0 URL was un-rewritten MIME-Version: 1.0 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.182.1,Aquarius:18.0.790,Hydra:6.0.425,FMLib:17.0.607.475 definitions=2021-10-20_06,2021-10-20_02,2020-04-07_01 X-Proofpoint-Spam-Details: rule=fb_default_notspam policy=fb_default score=0 mlxscore=0 bulkscore=0 clxscore=1034 mlxlogscore=999 priorityscore=1501 malwarescore=0 lowpriorityscore=0 suspectscore=0 impostorscore=0 spamscore=0 adultscore=0 phishscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2109230001 definitions=main-2110210008 X-FB-Internal: deliver Precedence: bulk List-ID: X-Mailing-List: bpf@vger.kernel.org X-Patchwork-Delegate: bpf@iogearbox.net This patch set refactors internals of libbpf to enable support for multiple custom .rodata.* and .data.* sections. Each such section is backed by its own BPF_MAP_TYPE_ARRAY, memory-mappable just like .rodata/.data. This is not extended to .bss because .bss is not a great name, it is generated by compiler with name that reflects completely irrelevant historical implementation details. Given that users have to annotate their variables with SEC(".data.my_sec") explicitly, standardizing on .rodata. and .data. prefixes makes more sense and keeps things simpler. Additionally, this patch set makes it simpler to work with those special internal maps by allowing to look them up by their full ELF section name. Patch #1 is a preparatory patch that deprecates one libbpf API and moves custom logic into libbpf.c, where it's used. This code is later refactored with the rest of libbpf.c logic to support multiple data section maps. See individual patches for all the details. For new custom "dot maps", their full ELF section names are used as the names that are sent into the kernel. Object name isn't prepended like for .data/.rodata/.bss. The reason is that with longer custom names, there isn't much space left for object name anyways. Also, if BTF is supported, btf_value_type_id points to DATASEC BTF type, which contains full original ELF name of the section, so tools like bpftool could use that to recover full name. This patch set doesn't add this logic yet, this is left for follow up patches. One interesting possibility that is now open by these changes is that it's possible to do: bpf_trace_printk("My fmt %s", sizeof("My fmt %s"), "blah"); and it will work as expected. I haven't updated libbpf-provided helpers in bpf_helpers.h for snprintf, seq_printf, and printk, because using `static const char ___fmt[] = fmt;` trick is still efficient and doesn't fill out the buffer at runtime (no copying). But we might consider updating them in the future, especially with the array check that Kumar proposed (see [0]). [0] https://lore.kernel.org/bpf/20211012041524.udytbr2xs5wid6x2@apollo.localdomain/ v1->v2: - don't prepend object name for new dot maps; - add __read_mostly example in selftests (Daniel). Andrii Nakryiko (10): libbpf: deprecate btf__finalize_data() and move it into libbpf.c libbpf: extract ELF processing state into separate struct libbpf: use Elf64-specific types explicitly for dealing with ELF libbpf: remove assumptions about uniqueness of .rodata/.data/.bss maps bpftool: support multiple .rodata/.data internal maps in skeleton bpftool: improve skeleton generation for data maps without DATASEC type libbpf: support multiple .rodata.* and .data.* BPF maps selftests/bpf: demonstrate use of custom .rodata/.data sections libbpf: simplify look up by name of internal maps selftests/bpf: switch to ".bss"/".rodata"/".data" lookups for internal maps tools/bpf/bpftool/gen.c | 158 ++-- tools/lib/bpf/btf.c | 93 -- tools/lib/bpf/btf.h | 1 + tools/lib/bpf/libbpf.c | 893 +++++++++++------- tools/lib/bpf/libbpf_internal.h | 8 +- tools/lib/bpf/linker.c | 1 - .../selftests/bpf/prog_tests/core_autosize.c | 2 +- .../selftests/bpf/prog_tests/core_reloc.c | 2 +- .../selftests/bpf/prog_tests/global_data.c | 11 +- .../bpf/prog_tests/global_data_init.c | 2 +- .../selftests/bpf/prog_tests/kfree_skb.c | 2 +- .../selftests/bpf/prog_tests/rdonly_maps.c | 2 +- .../selftests/bpf/prog_tests/skeleton.c | 29 + .../selftests/bpf/progs/test_skeleton.c | 18 + 14 files changed, 731 insertions(+), 491 deletions(-)