From patchwork Tue Feb 6 22:04:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13547835 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E4195C48297 for ; Tue, 6 Feb 2024 22:04:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 089FE6B007E; Tue, 6 Feb 2024 17:04:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0130D6B0080; Tue, 6 Feb 2024 17:04:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DFF2E6B0081; Tue, 6 Feb 2024 17:04:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id C7FFA6B007E for ; Tue, 6 Feb 2024 17:04:48 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 7FC20A0313 for ; Tue, 6 Feb 2024 22:04:48 +0000 (UTC) X-FDA: 81762759456.14.795923B Received: from mail-io1-f45.google.com (mail-io1-f45.google.com [209.85.166.45]) by imf23.hostedemail.com (Postfix) with ESMTP id 9C69A140024 for ; Tue, 6 Feb 2024 22:04:46 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Y5FvXTiJ; spf=pass (imf23.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.166.45 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707257086; a=rsa-sha256; cv=none; b=sIBpf72MT1cLbnobtMSPCooScvuEq1YP9d8tBFyX/HEVYrP2Vi5J/wMTVDAPiuKB8HEL+u 9xxPGxdhwBoBphxiUpARbhWUjOFmHwLHjPYKwU8yDfzmpvtI8tBAH5ClmBFqL/NeSPuXyQ zIh3eCEcKGR4I2K0DnutkorB7yrLgpk= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=Y5FvXTiJ; spf=pass (imf23.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.166.45 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1707257086; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=4wgrJzzaBt4YVHJ/ppmhbd4uaNfq8OCIuwnfxSX7DKU=; b=Hc7Dqxs/9w2mMhU4BdOu/EqIw+4HaZCdCmyvP7duezbd/h4FO0mhqYmh6d02hOmKJojEPA ANeH1ycfglsm2WJPAGDoCTawJTfP+88WZNu7Wnb3eendYOUlG3NCbpRwTYc5KNOXS4poRj nOVmpZuh0Qz4oFgZ4wm1cdKaLDig87Y= Received: by mail-io1-f45.google.com with SMTP id ca18e2360f4ac-7bed9fb159fso271093139f.1 for ; Tue, 06 Feb 2024 14:04:46 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707257086; x=1707861886; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=4wgrJzzaBt4YVHJ/ppmhbd4uaNfq8OCIuwnfxSX7DKU=; b=Y5FvXTiJkm3rCZsIGc1hJe7stSQG7DJv2Y5a4494rAEPz4GjEIeGDKl4zWY6joGBec VPTgG5M9cK3kAaQZrY9+pAtLFy1/0muskxeC3TahHY+JarSGsjQoKco04cXaMvITGL3V V0KR3QBP73bQlPxHKCml9EON+8XLI6TkViR3gBF3UPltsmMxMwGCEem4ExOEHLG8Jd55 jebHUKF3P6oH40JEUwshz7FsEToEldpuV17tcjQfE/bcKuaUEJEjuDQyqi6lz9CWIyM8 2q8OVHCqugCGzQcXWk6uz/pyzin5a6kE1eMriJz7lyiIAiG1SKSG8hc0qd39DcXN4hFv QYdA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707257086; x=1707861886; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=4wgrJzzaBt4YVHJ/ppmhbd4uaNfq8OCIuwnfxSX7DKU=; b=U6XkWJT1xFfACiQQZLX8iDwzGyCIj6dtjqZ609PX62nk54bohA3cfrHTmaZpikSkkD Tlsw8/KN8am9XMZyX03hAGFoElpazSzgNvRVLF55SnT9Uow0ReAfU7nwaynpFXn3ngAH QFbPLzXfQBQCg12jmQtGr/uWg/SrLL4bpRn28JE5w65+BExOP0oy6C1bSkOcIwjpkYiV VrIUHu6h5JjDhHSJba2fnOb1Na/9akOhzEpnlbpEhddyK74gcQbfw/bSjLCmAkhpMtde hMOhwfuBZLRIjpxSTtLNdx2oH+roewvVzfn+9InmQEFRDq7gWCUwVIwLx3IwIO4dvMoZ DEwg== X-Gm-Message-State: AOJu0YyKh+sR9iXVBXHIVKDpDbTC2T82YUhtbSg7V5iC71e30/r7/CHB X5C0vMOIXGcWFxPww9OsGzcsy6kAkjUkHNCkJM03Q660MEXAsOad X-Google-Smtp-Source: AGHT+IH8zcUGY4rUvmQTuorDIPqrba0mMoNRSrSR8ogIMZKligjeO+xAUhIwfsg8DCbc2/gg0XIEyw== X-Received: by 2002:a6b:4e16:0:b0:7c3:f0db:585e with SMTP id c22-20020a6b4e16000000b007c3f0db585emr3392204iob.9.1707257085454; Tue, 06 Feb 2024 14:04:45 -0800 (PST) X-Forwarded-Encrypted: i=0; AJvYcCUYWJNGrYJDntZ1Lva2ck2OXgpMu3lmdpxaBFjppjMxMg4BqZZI9FmCeGLe7AHfySa+3bi5Ayo/yKpa/36Fu7WTCEDjfJxIKab2SdEvncx08dUwzlq8k4o4PqANWhS7t6zo594jzvlUHCaCO0rlw/DALVUDQD0/j3zdgjU16RpqJXXIXYFJzaM1oEkYNABiy+K8QUoIucWCW447c6DsNejknGyPMwSOiHOngswZfcsNU/EpqA7qG6vScbgTpGPvmT8vTeVcpdBHBlu6WI+ot/QfyazebDs8B8j3 Received: from localhost.localdomain ([2620:10d:c090:400::4:27bf]) by smtp.gmail.com with ESMTPSA id 13-20020a63020d000000b005d64d09acffsm2707484pgc.72.2024.02.06.14.04.43 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Tue, 06 Feb 2024 14:04:44 -0800 (PST) From: Alexei Starovoitov To: bpf@vger.kernel.org Cc: daniel@iogearbox.net, andrii@kernel.org, martin.lau@kernel.org, memxor@gmail.com, eddyz87@gmail.com, tj@kernel.org, brho@google.com, hannes@cmpxchg.org, linux-mm@kvack.org, kernel-team@fb.com Subject: [PATCH bpf-next 00/16] bpf: Introduce BPF arena. Date: Tue, 6 Feb 2024 14:04:25 -0800 Message-Id: <20240206220441.38311-1-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-145) MIME-Version: 1.0 X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 9C69A140024 X-Stat-Signature: nx56eg6mcq3jo3eip79uesfaa5fwmf65 X-Rspam-User: X-HE-Tag: 1707257086-884944 X-HE-Meta: U2FsdGVkX1/XH7l7nOXSwYvNayH/PrlhGkWwZ4ZaC/z8ntksVUHiYSC5ef4xgbiNCceBlZsvnv+UrjG214jBA0LOI6seodajFNqNn7l9Zmqh+xPLXJxe743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X1MhdJ/ALH0wL+QK96qlF+TQYlMy+QTWLykdxaDmI5+o3rtdgZwASkt1CxKSLtgeAqfIy5KCFVq45HRwq2fS9ifJnio3mTM3NkOnXekHiAr5O0lEUm1ScV5NpuQoKnDY9mCcOq77BgxJRCQzJEkgBhK3zBu/6HaOJf9M8nk73H4nc2hD3AFGzxRafjjQEwdN51EhAAqLIDxT5CzHaReCnIpjKQBGgMbMP6LyfOVpQpxsVTCbsMrhdyIcmXk8T4n3zws57Ke3igOJefsTS8WAv5MULizYpyAaduhVu1M8iVGOCnQuD6JxcQYIkcD1h/FfjE9pFL3BwsAR/4wh5H9gaiXCjQVNwhdWUO3lGBQHcxzd/ID8cSVnBHgvXI= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: Alexei Starovoitov bpf programs have multiple options to communicate with user space: - Various ring buffers (perf, ftrace, bpf): The data is streamed unidirectionally from bpf to user space. - Hash map: The bpf program populates elements, and user space consumes them via bpf syscall. - mmap()-ed array map: Libbpf creates an array map that is directly accessed by the bpf program and mmap-ed to user space. It's the fastest way. Its disadvantage is that memory for the whole array is reserved at the start. These patches introduce bpf_arena, which is a sparse shared memory region between the bpf program and user space. Use cases: 1. User space mmap-s bpf_arena and uses it as a traditional mmap-ed anonymous region, like memcached or any key/value storage. The bpf program implements an in-kernel accelerator. XDP prog can search for a key in bpf_arena and return a value without going to user space. 2. The bpf program builds arbitrary data structures in bpf_arena (hash tables, rb-trees, sparse arrays), while user space occasionally consumes it. 3. bpf_arena is a "heap" of memory from the bpf program's point of view. It is not shared with user space. Initially, the kernel vm_area and user vma are not populated. User space can fault in pages within the range. While servicing a page fault, bpf_arena logic will insert a new page into the kernel and user vmas. The bpf program can allocate pages from that region via bpf_arena_alloc_pages(). This kernel function will insert pages into the kernel vm_area. The subsequent fault-in from user space will populate that page into the user vma. The BPF_F_SEGV_ON_FAULT flag at arena creation time can be used to prevent fault-in from user space. In such a case, if a page is not allocated by the bpf program and not present in the kernel vm_area, the user process will segfault. This is useful for use cases 2 and 3 above. bpf_arena_alloc_pages() is similar to user space mmap(). It allocates pages either at a specific address within the arena or allocates a range with the maple tree. bpf_arena_free_pages() is analogous to munmap(), which frees pages and removes the range from the kernel vm_area and from user process vmas. bpf_arena can be used as a bpf program "heap" of up to 4GB. The memory is not shared with user space. This is use case 3. In such a case, the BPF_F_NO_USER_CONV flag is recommended. It will tell the verifier to treat the rX = bpf_arena_cast_user(rY) instruction as a 32-bit move wX = wY, which will improve bpf prog performance. Otherwise, bpf_arena_cast_user is translated by JIT to conditionally add the upper 32 bits of user vm_start (if the pointer is not NULL) to arena pointers before they are stored into memory. This way, user space sees them as valid 64-bit pointers. Diff https://github.com/llvm/llvm-project/pull/79902 taught LLVM BPF backend to generate the bpf_cast_kern() instruction before dereference of the arena pointer and the bpf_cast_user() instruction when the arena pointer is formed. In a typical bpf program there will be very few bpf_cast_user(). From LLVM's point of view, arena pointers are tagged as __attribute__((address_space(1))). Hence, clang provides helpful diagnostics when pointers cross address space. Libbpf and the kernel support only address_space == 1. All other address space identifiers are reserved. rX = bpf_cast_kern(rY, addr_space) tells the verifier that rX->type = PTR_TO_ARENA. Any further operations on PTR_TO_ARENA register have to be in the 32-bit domain. The verifier will mark load/store through PTR_TO_ARENA with PROBE_MEM32. JIT will generate them as kern_vm_start + 32bit_addr memory accesses. The behavior is similar to copy_from_kernel_nofault() except that no address checks are necessary. The address is guaranteed to be in the 4GB range. If the page is not present, the destination register is zeroed on read, and the operation is ignored on write. rX = bpf_cast_user(rY, addr_space) tells the verifier that rX->type = unknown scalar. If arena->map_flags has BPF_F_NO_USER_CONV set, then the verifier converts cast_user to mov32. Otherwise, JIT will emit native code equivalent to: rX = (u32)rY; if (rX) rX |= arena->user_vm_start & ~(u64)~0U; After such conversion, the pointer becomes a valid user pointer within bpf_arena range. The user process can access data structures created in bpf_arena without any additional computations. For example, a linked list built by a bpf program can be walked natively by user space. The last two patches demonstrate how algorithms in the C language can be compiled as a bpf program and as native code. Followup patches are planned: . selftests in asm . support arena variables in global data. Example: void __arena * ptr; // works int __arena var; // supported by llvm, but not by kernel and libbpf yet . support bpf_spin_lock in arena bpf programs running on different CPUs can synchronize access to the arena via existing bpf_spin_lock mechanisms (spin_locks in bpf_array or in bpf hash map). It will be more convenient to allow spin_locks inside the arena too. Patch set overview: - patch 1,2: minor verifier enhancements to enable bpf_arena kfuncs - patch 3: export vmap_pages_range() to be used out side of mm directory - patch 4: main patch that introduces bpf_arena map type. See commit log - patch 6: probe_mem32 support in x86 JIT - patch 7: bpf_cast_user support in x86 JIT - patch 8: main verifier patch to support bpf_arena - patch 9: __arg_arena to tag arena pointers in bpf globla functions - patch 11: libbpf support for arena - patch 12: __ulong() macro to pass 64-bit constants in BTF - patch 13: export PAGE_SIZE constant into vmlinux BTF to be used from bpf programs - patch 14: bpf_arena_cast instruction as inline asm for setups with old LLVM - patch 15,16: testcases in C Alexei Starovoitov (16): bpf: Allow kfuncs return 'void *' bpf: Recognize '__map' suffix in kfunc arguments mm: Expose vmap_pages_range() to the rest of the kernel. bpf: Introduce bpf_arena. bpf: Disasm support for cast_kern/user instructions. bpf: Add x86-64 JIT support for PROBE_MEM32 pseudo instructions. bpf: Add x86-64 JIT support for bpf_cast_user instruction. bpf: Recognize cast_kern/user instructions in the verifier. bpf: Recognize btf_decl_tag("arg:arena") as PTR_TO_ARENA. libbpf: Add __arg_arena to bpf_helpers.h libbpf: Add support for bpf_arena. libbpf: Allow specifying 64-bit integers in map BTF. bpf: Tell bpf programs kernel's PAGE_SIZE bpf: Add helper macro bpf_arena_cast() selftests/bpf: Add bpf_arena_list test. selftests/bpf: Add bpf_arena_htab test. arch/x86/net/bpf_jit_comp.c | 222 +++++++- include/linux/bpf.h | 8 +- include/linux/bpf_types.h | 1 + include/linux/bpf_verifier.h | 1 + include/linux/filter.h | 4 + include/linux/vmalloc.h | 2 + include/uapi/linux/bpf.h | 12 + kernel/bpf/Makefile | 3 + kernel/bpf/arena.c | 518 ++++++++++++++++++ kernel/bpf/btf.c | 19 +- kernel/bpf/core.c | 23 +- kernel/bpf/disasm.c | 11 + kernel/bpf/log.c | 3 + kernel/bpf/syscall.c | 3 + kernel/bpf/verifier.c | 127 ++++- mm/vmalloc.c | 4 +- tools/include/uapi/linux/bpf.h | 12 + tools/lib/bpf/bpf_helpers.h | 2 + tools/lib/bpf/libbpf.c | 62 ++- tools/lib/bpf/libbpf_probes.c | 6 + tools/testing/selftests/bpf/DENYLIST.aarch64 | 1 + tools/testing/selftests/bpf/DENYLIST.s390x | 1 + tools/testing/selftests/bpf/bpf_arena_alloc.h | 58 ++ .../testing/selftests/bpf/bpf_arena_common.h | 70 +++ tools/testing/selftests/bpf/bpf_arena_htab.h | 100 ++++ tools/testing/selftests/bpf/bpf_arena_list.h | 95 ++++ .../testing/selftests/bpf/bpf_experimental.h | 41 ++ .../selftests/bpf/prog_tests/arena_htab.c | 88 +++ .../selftests/bpf/prog_tests/arena_list.c | 65 +++ .../testing/selftests/bpf/progs/arena_htab.c | 48 ++ .../selftests/bpf/progs/arena_htab_asm.c | 5 + .../testing/selftests/bpf/progs/arena_list.c | 75 +++ 32 files changed, 1669 insertions(+), 21 deletions(-) create mode 100644 kernel/bpf/arena.c create mode 100644 tools/testing/selftests/bpf/bpf_arena_alloc.h create mode 100644 tools/testing/selftests/bpf/bpf_arena_common.h create mode 100644 tools/testing/selftests/bpf/bpf_arena_htab.h create mode 100644 tools/testing/selftests/bpf/bpf_arena_list.h create mode 100644 tools/testing/selftests/bpf/prog_tests/arena_htab.c create mode 100644 tools/testing/selftests/bpf/prog_tests/arena_list.c create mode 100644 tools/testing/selftests/bpf/progs/arena_htab.c create mode 100644 tools/testing/selftests/bpf/progs/arena_htab_asm.c create mode 100644 tools/testing/selftests/bpf/progs/arena_list.c