From patchwork Fri Feb 9 04:05:48 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexei Starovoitov X-Patchwork-Id: 13550841 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 C865BC48297 for ; Fri, 9 Feb 2024 04:06:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 127B76B0072; Thu, 8 Feb 2024 23:06:17 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0B1B46B0075; Thu, 8 Feb 2024 23:06:17 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E94076B0078; Thu, 8 Feb 2024 23:06:16 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id D37CC6B0072 for ; Thu, 8 Feb 2024 23:06:16 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 5C4551601A6 for ; Fri, 9 Feb 2024 04:06:16 +0000 (UTC) X-FDA: 81770927952.06.8E1C3BC Received: from mail-pf1-f179.google.com (mail-pf1-f179.google.com [209.85.210.179]) by imf21.hostedemail.com (Postfix) with ESMTP id 916281C000A for ; Fri, 9 Feb 2024 04:06:14 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=CfjaeXlB; spf=pass (imf21.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.210.179 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=1707451574; 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=jEkkqCA203eDpu5hFhuFOhVB44CDrteH7OTjO7Ka5i0=; b=ALSuaC7KLA1B6adAq6hURcwlTXRhENUG1Itc8Rf0Kn/13xezC7d798Zcily8i/x+0T6jgH 1sGjeECo3Bhdj/zwIwSvuXkLrN/NvcyNnt0JrYC/+Llj51COwCFIMbWXg04i59/OMGGXud RomRgMs/TXbBlRHDT5+YCBpKGUrzLNQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1707451574; a=rsa-sha256; cv=none; b=awVW76sCNyN/Z3B+AQVGdjF10AoN8Wd07TnoopJ3yXhlxLW85hu6FMQDOerduO1qrTRpj2 o1+a8P9Av4sUn7Pch+M3cCsFWtZhNMEJtKZNE8b3WqDbvcDi+wU1IwnBDqqc/6GDFkBzR2 f1A8um8XxEeBWMDvP8Apy5cOw1TTl0g= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=CfjaeXlB; spf=pass (imf21.hostedemail.com: domain of alexei.starovoitov@gmail.com designates 209.85.210.179 as permitted sender) smtp.mailfrom=alexei.starovoitov@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-pf1-f179.google.com with SMTP id d2e1a72fcca58-6e0507eb60cso375738b3a.3 for ; Thu, 08 Feb 2024 20:06:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1707451573; x=1708056373; 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=jEkkqCA203eDpu5hFhuFOhVB44CDrteH7OTjO7Ka5i0=; b=CfjaeXlB3aOcluQPSgRsba90sS2w/DV43UigkMv2JS/qz2Zcnd1T2A/KNBhjHiRFXK my3bV/ddwT3x0jAhf/Tj2oVOyfjBnW/4csZ9e8QKu7ZirgAlVxFTjxd99L7lY6FuFb/o 3yJZIPagntllGqdOFALiNtMIRQ8nRJuJwHcft42o7B3K+jpPEbgyNdzcVPqtRdIeEwR2 ppmCXNUFi6iN3KGpzi0/e0AeqwuXaqXtYNF6zY8V7o0vzOL8l3L+BiS1i7OKMuLc9EkA /7yeOGKSB64wwtt+d35preMiZH8rC+JrVsCWRll9Hrr47tmuWPmYmjWcKyOZj2+UgZ+v kQCw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707451573; x=1708056373; 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=jEkkqCA203eDpu5hFhuFOhVB44CDrteH7OTjO7Ka5i0=; b=tjccHsz9rKDMyOIuKhL44NhkK52UX62vTeHIMNe5/GHvs9XbchZ//lTW1v50VLRnj9 mcq3RB4PX0nSuZbz/16nfIcWoEKY9oxPmCXLdS3nbQQzleoiuzrHpkvSggMErj1Jin0m ndeRwwqVGvqq+OB8s1JS0uiOp56Av4QZJvt3s+T08dzeg2SHPfBgPcq4mVXRHrUOGK5q PlM4qXO9h/tfVycIu+zsdBd/Qxzp9xSASM1OBOEYQqrwd8P1q0S9ukwG0PieN1Gi/qoW G9dHTTyFlK/2wxZmuIzn7bj6DMnqcxP0TsLABV4edC/hXNlAuFJ/OxhxYAYO4MpNmtCo Lb1w== X-Gm-Message-State: AOJu0YzFIgZBdg3ze7aKfHWcXlaf++xA0UuulRgFapu6Gr2MAmQmEvJH U8PIZBf3X82AhPi+U2yRejRL2ZjKobdag967xCf0AIBrkolxCv2g X-Google-Smtp-Source: AGHT+IFvcQsVrVG6+SGe1INK5B7SoWZ5sJrALhybapLaBNn1EcSUIMtEW0BDkqrCzlBMT6007/jHfw== X-Received: by 2002:a05:6a20:2d21:b0:19e:9b1d:574d with SMTP id g33-20020a056a202d2100b0019e9b1d574dmr907083pzl.18.1707451573213; Thu, 08 Feb 2024 20:06:13 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCW/ssESUSmVBd+T6j9iMIRQ9733sIBYkBIdme5EBUSmrbzmGOwLVEX6Yi0jGuAKp1O1B0KdPubWfNOqB+JEqH8kMi0ejAfW+ZBA7I4my9DHIQOhvdrS4lBL9T6s/yN8fFyD5XpuayRq8KZGC390eXqCDHcHzCTK9LUDFS674CTkPSj15XzPQGWqOT6PI3UllpORDlyF7hxSxGGWLUxtAf+HDyTd3owbb5Xe/Je1AYtExcBzNZdzN7s8fVtREsRgrSMbf6LGF5Q4UCQhThhOH6l2quBB6Pg/M3moM1PEaGKh5yQwmmjobEUBLwMG9SiPEZwFtWnef6ZnLW9QBYpzGjymFue/8jywyfpcGm8fCqHR+gc0QqIESA== Received: from macbook-pro-49.dhcp.thefacebook.com ([2620:10d:c090:400::4:a894]) by smtp.gmail.com with ESMTPSA id sm6-20020a17090b2e4600b002963e682f6fsm633878pjb.57.2024.02.08.20.06.11 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Thu, 08 Feb 2024 20:06:12 -0800 (PST) From: Alexei Starovoitov To: bpf@vger.kernel.org Cc: daniel@iogearbox.net, andrii@kernel.org, memxor@gmail.com, eddyz87@gmail.com, tj@kernel.org, brho@google.com, hannes@cmpxchg.org, lstoakes@gmail.com, akpm@linux-foundation.org, urezki@gmail.com, hch@infradead.org, linux-mm@kvack.org, kernel-team@fb.com Subject: [PATCH v2 bpf-next 00/20] bpf: Introduce BPF arena. Date: Thu, 8 Feb 2024 20:05:48 -0800 Message-Id: <20240209040608.98927-1-alexei.starovoitov@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-145) MIME-Version: 1.0 X-Rspamd-Queue-Id: 916281C000A X-Rspam-User: X-Stat-Signature: mxe3huufyufw86qqiwuuhip3inqcgszu X-Rspamd-Server: rspam03 X-HE-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TLnOhThuvWkJ8yTURLrw1pMSn6XkpvY+fnizvCqi0/3HK4HZEhclv6QgCw9ZM4XXy2djHZYcFCjyi2HQyCJudQbfLsAwOWn9PL5zsjAl19U5Tgy7pqRjx/w58Jv+henqTvVRwCL680f1UFRgYvQIBiaoAPUtKSTnpTMwGGedQNEdLE8sNQ+KNfQ+nlARG2K6WMjLSdgvbCaBeKWaHMo+Rs38gHXPWt964V+8Eyvki+V25PgkuhZUC/EJYfUJABH8RyeSxZkmbEHdJJfhqvlqIhf+npx/ZnOhTm0cpR3SBVlTVgqlWBeQpcgIqNeKOI2reBQblX+DFC81GRCIheyvB7MUCxBluAlnARxC7RUUOUHr3UtMRcQtzPaFnR4oYtCBTC6RFah1zfA4l2lzysCrgvdQAurK4LQVcQ/7PHMUVUkMAZSyA1Wb1eIw5If+hKnnSeg/rx/8J+rTbYjtq1Ii2rCUEy7J67vJhbfWBAT7k= 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 v1->v2: - Improved commit log with reasons for using vmap_pages_range() in bpf_arena. Thanks to Johannes - Added support for __arena global variables in bpf programs - Fixed race conditions spotted by Barret - Fixed wrap32 issue spotted by Barret - Fixed bpf_map_mmap_sz() the way Andrii suggested The work on bpf_arena was inspired by Barret's work: https://github.com/google/ghost-userspace/blob/main/lib/queue.bpf.h that implements queues, lists and AVL trees completely as bpf programs using giant bpf array map and integer indices instead of pointers. bpf_arena is a sparse array that allows to use normal C pointers to build such data structures. Last few patches implement page_frag allocator, link list and hash table as bpf programs. v1: 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. 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 consumes it. 3. bpf_arena is a "heap" of memory from the bpf program's point of view. The user space may mmap it, but bpf program will not convert pointers to user base at run-time to improve bpf program speed. 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 speed of bpf program is more important than ease of sharing 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 (rY) rX |= clear_lo32_bits(arena->user_vm_start); /* replace hi32 bits in rX */ 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: . 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 4: export vmap_pages_range() to be used outside of mm directory - patch 5: main patch that introduces bpf_arena map type. See commit log .. - patch 9: main verifier patch to support bpf_arena .. - patch 11-14: libbpf support for arena .. - patch 17-20: tests Alexei Starovoitov (20): bpf: Allow kfuncs return 'void *' bpf: Recognize '__map' suffix in kfunc arguments bpf: Plumb get_unmapped_area() callback into bpf_map_ops 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. libbpf: Recognize __arena global varaibles. bpf: Tell bpf programs kernel's PAGE_SIZE bpf: Add helper macro bpf_arena_cast() selftests/bpf: Add unit tests for bpf_arena_alloc/free_pages selftests/bpf: Add bpf_arena_list test. selftests/bpf: Add bpf_arena_htab test. selftests/bpf: Convert simple page_frag allocator to per-cpu. arch/x86/net/bpf_jit_comp.c | 222 ++++++- include/linux/bpf.h | 11 +- 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 | 557 ++++++++++++++++++ kernel/bpf/btf.c | 19 +- kernel/bpf/core.c | 23 +- kernel/bpf/disasm.c | 11 + kernel/bpf/log.c | 3 + kernel/bpf/syscall.c | 15 + kernel/bpf/verifier.c | 135 ++++- mm/vmalloc.c | 4 +- tools/bpf/bpftool/gen.c | 13 +- tools/include/uapi/linux/bpf.h | 12 + tools/lib/bpf/bpf_helpers.h | 6 + tools/lib/bpf/libbpf.c | 189 +++++- tools/lib/bpf/libbpf_probes.c | 7 + tools/testing/selftests/bpf/DENYLIST.aarch64 | 2 + tools/testing/selftests/bpf/DENYLIST.s390x | 2 + tools/testing/selftests/bpf/bpf_arena_alloc.h | 67 +++ .../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 | 68 +++ .../selftests/bpf/prog_tests/verifier.c | 2 + .../testing/selftests/bpf/progs/arena_htab.c | 46 ++ .../selftests/bpf/progs/arena_htab_asm.c | 5 + .../testing/selftests/bpf/progs/arena_list.c | 76 +++ .../selftests/bpf/progs/verifier_arena.c | 91 +++ tools/testing/selftests/bpf/test_loader.c | 9 +- 36 files changed, 1969 insertions(+), 43 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 create mode 100644 tools/testing/selftests/bpf/progs/verifier_arena.c