BPF List
 help / color / mirror / Atom feed
* [PATCH bpf 0/4] bpf: arena followups.
@ 2024-03-15  2:18 Alexei Starovoitov
  2024-03-15  2:18 ` [PATCH bpf 1/4] bpf: Clarify bpf_arena comments Alexei Starovoitov
                   ` (5 more replies)
  0 siblings, 6 replies; 10+ messages in thread
From: Alexei Starovoitov @ 2024-03-15  2:18 UTC (permalink / raw)
  To: bpf; +Cc: daniel, andrii, martin.lau, eddyz87, kernel-team

From: Alexei Starovoitov <ast@kernel.org>

A set of follow ups to clean up bpf_arena and adjust to the latest LLVM.

Alexei Starovoitov (4):
  bpf: Clarify bpf_arena comments.
  libbpf, selftests/bpf: Adjust libbpf, bpftool, selftests to match LLVM
  selftests/bpf: Remove hard coded PAGE_SIZE macro.
  selftests/bpf: Add arena test case for 4Gbyte corner case

 kernel/bpf/arena.c                            | 25 +++++--
 tools/bpf/bpftool/gen.c                       |  2 +-
 tools/lib/bpf/libbpf.c                        |  2 +-
 .../testing/selftests/bpf/bpf_arena_common.h  |  2 +-
 .../selftests/bpf/prog_tests/arena_htab.c     |  8 ++-
 .../selftests/bpf/prog_tests/arena_list.c     |  7 +-
 .../selftests/bpf/prog_tests/verifier.c       |  2 +
 .../testing/selftests/bpf/progs/arena_htab.c  |  2 +-
 .../testing/selftests/bpf/progs/arena_list.c  | 10 +--
 .../selftests/bpf/progs/verifier_arena.c      |  4 +-
 .../bpf/progs/verifier_arena_large.c          | 68 +++++++++++++++++++
 11 files changed, 109 insertions(+), 23 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/verifier_arena_large.c

-- 
2.43.0


^ permalink raw reply	[flat|nested] 10+ messages in thread

* [PATCH bpf 1/4] bpf: Clarify bpf_arena comments.
  2024-03-15  2:18 [PATCH bpf 0/4] bpf: arena followups Alexei Starovoitov
@ 2024-03-15  2:18 ` Alexei Starovoitov
  2024-03-15 17:59   ` Stanislav Fomichev
  2024-03-15  2:18 ` [PATCH bpf 2/4] libbpf, selftests/bpf: Adjust libbpf, bpftool, selftests to match LLVM Alexei Starovoitov
                   ` (4 subsequent siblings)
  5 siblings, 1 reply; 10+ messages in thread
From: Alexei Starovoitov @ 2024-03-15  2:18 UTC (permalink / raw)
  To: bpf; +Cc: daniel, andrii, martin.lau, eddyz87, kernel-team

From: Alexei Starovoitov <ast@kernel.org>

Clarify two bpf_arena comments, use existing SZ_4G #define,
improve page_cnt check.

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
 kernel/bpf/arena.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c
index 86571e760dd6..343c3456c8dd 100644
--- a/kernel/bpf/arena.c
+++ b/kernel/bpf/arena.c
@@ -38,7 +38,7 @@
 
 /* number of bytes addressable by LDX/STX insn with 16-bit 'off' field */
 #define GUARD_SZ (1ull << sizeof(((struct bpf_insn *)0)->off) * 8)
-#define KERN_VM_SZ ((1ull << 32) + GUARD_SZ)
+#define KERN_VM_SZ (SZ_4G + GUARD_SZ)
 
 struct bpf_arena {
 	struct bpf_map map;
@@ -110,7 +110,7 @@ static struct bpf_map *arena_map_alloc(union bpf_attr *attr)
 		return ERR_PTR(-EINVAL);
 
 	vm_range = (u64)attr->max_entries * PAGE_SIZE;
-	if (vm_range > (1ull << 32))
+	if (vm_range > SZ_4G)
 		return ERR_PTR(-E2BIG);
 
 	if ((attr->map_extra >> 32) != ((attr->map_extra + vm_range - 1) >> 32))
@@ -301,7 +301,7 @@ static unsigned long arena_get_unmapped_area(struct file *filp, unsigned long ad
 
 	if (pgoff)
 		return -EINVAL;
-	if (len > (1ull << 32))
+	if (len > SZ_4G)
 		return -E2BIG;
 
 	/* if user_vm_start was specified at arena creation time */
@@ -322,7 +322,7 @@ static unsigned long arena_get_unmapped_area(struct file *filp, unsigned long ad
 	if (WARN_ON_ONCE(arena->user_vm_start))
 		/* checks at map creation time should prevent this */
 		return -EFAULT;
-	return round_up(ret, 1ull << 32);
+	return round_up(ret, SZ_4G);
 }
 
 static int arena_map_mmap(struct bpf_map *map, struct vm_area_struct *vma)
@@ -346,7 +346,7 @@ static int arena_map_mmap(struct bpf_map *map, struct vm_area_struct *vma)
 		return -EBUSY;
 
 	/* Earlier checks should prevent this */
-	if (WARN_ON_ONCE(vma->vm_end - vma->vm_start > (1ull << 32) || vma->vm_pgoff))
+	if (WARN_ON_ONCE(vma->vm_end - vma->vm_start > SZ_4G || vma->vm_pgoff))
 		return -EFAULT;
 
 	if (remember_vma(arena, vma))
@@ -420,7 +420,7 @@ static long arena_alloc_pages(struct bpf_arena *arena, long uaddr, long page_cnt
 		if (uaddr & ~PAGE_MASK)
 			return 0;
 		pgoff = compute_pgoff(arena, uaddr);
-		if (pgoff + page_cnt > page_cnt_max)
+		if (pgoff > page_cnt_max - page_cnt)
 			/* requested address will be outside of user VMA */
 			return 0;
 	}
@@ -447,7 +447,13 @@ static long arena_alloc_pages(struct bpf_arena *arena, long uaddr, long page_cnt
 		goto out;
 
 	uaddr32 = (u32)(arena->user_vm_start + pgoff * PAGE_SIZE);
-	/* Earlier checks make sure that uaddr32 + page_cnt * PAGE_SIZE will not overflow 32-bit */
+	/* Earlier checks made sure that uaddr32 + page_cnt * PAGE_SIZE - 1
+	 * will not overflow 32-bit. Lower 32-bit need to represent
+	 * contiguous user address range.
+	 * Map these pages at kern_vm_start base.
+	 * kern_vm_start + uaddr32 + page_cnt * PAGE_SIZE - 1 can overflow
+	 * lower 32-bit and it's ok.
+	 */
 	ret = vm_area_map_pages(arena->kern_vm, kern_vm_start + uaddr32,
 				kern_vm_start + uaddr32 + page_cnt * PAGE_SIZE, pages);
 	if (ret) {
@@ -510,6 +516,11 @@ static void arena_free_pages(struct bpf_arena *arena, long uaddr, long page_cnt)
 		if (!page)
 			continue;
 		if (page_cnt == 1 && page_mapped(page)) /* mapped by some user process */
+			/* Optimization for the common case of page_cnt==1:
+			 * If page wasn't mapped into some user vma there
+			 * is no need to call zap_pages which is slow. When
+			 * page_cnt is big it's faster to do the batched zap.
+			 */
 			zap_pages(arena, full_uaddr, 1);
 		vm_area_unmap_pages(arena->kern_vm, kaddr, kaddr + PAGE_SIZE);
 		__free_page(page);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH bpf 2/4] libbpf, selftests/bpf: Adjust libbpf, bpftool, selftests to match LLVM
  2024-03-15  2:18 [PATCH bpf 0/4] bpf: arena followups Alexei Starovoitov
  2024-03-15  2:18 ` [PATCH bpf 1/4] bpf: Clarify bpf_arena comments Alexei Starovoitov
@ 2024-03-15  2:18 ` Alexei Starovoitov
  2024-03-15  3:57   ` Andrii Nakryiko
  2024-03-15  2:18 ` [PATCH bpf 3/4] selftests/bpf: Remove hard coded PAGE_SIZE macro Alexei Starovoitov
                   ` (3 subsequent siblings)
  5 siblings, 1 reply; 10+ messages in thread
From: Alexei Starovoitov @ 2024-03-15  2:18 UTC (permalink / raw)
  To: bpf; +Cc: daniel, andrii, martin.lau, eddyz87, kernel-team

From: Alexei Starovoitov <ast@kernel.org>

The selftests use
to tell LLVM about special pointers. For LLVM there is nothing "arena"
about them. They are simply pointers in a different address space.
Hence LLVM diff https://github.com/llvm/llvm-project/pull/85161 renamed:
. macro __BPF_FEATURE_ARENA_CAST -> __BPF_FEATURE_ADDR_SPACE_CAST
. global variables in __attribute__((address_space(N))) are now
  placed in section named ".address_space.N" instead of ".arena.N".

Adjust libbpf, bpftool, and selftests to match LLVM.

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
 tools/bpf/bpftool/gen.c                            |  2 +-
 tools/lib/bpf/libbpf.c                             |  2 +-
 tools/testing/selftests/bpf/bpf_arena_common.h     |  2 +-
 tools/testing/selftests/bpf/progs/arena_htab.c     |  2 +-
 tools/testing/selftests/bpf/progs/arena_list.c     | 10 +++++-----
 tools/testing/selftests/bpf/progs/verifier_arena.c |  4 ++--
 6 files changed, 11 insertions(+), 11 deletions(-)

diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c
index 4fa4ade1ce74..540c0f2c4fda 100644
--- a/tools/bpf/bpftool/gen.c
+++ b/tools/bpf/bpftool/gen.c
@@ -121,7 +121,7 @@ static bool get_datasec_ident(const char *sec_name, char *buf, size_t buf_sz)
 	int i, n;
 
 	/* recognize hard coded LLVM section name */
-	if (strcmp(sec_name, ".arena.1") == 0) {
+	if (strcmp(sec_name, ".addr_space.1") == 0) {
 		/* this is the name to use in skeleton */
 		snprintf(buf, buf_sz, "arena");
 		return true;
diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
index efab29b8935b..36e26f4f5997 100644
--- a/tools/lib/bpf/libbpf.c
+++ b/tools/lib/bpf/libbpf.c
@@ -498,7 +498,7 @@ struct bpf_struct_ops {
 #define KSYMS_SEC ".ksyms"
 #define STRUCT_OPS_SEC ".struct_ops"
 #define STRUCT_OPS_LINK_SEC ".struct_ops.link"
-#define ARENA_SEC ".arena.1"
+#define ARENA_SEC ".addr_space.1"
 
 enum libbpf_map_type {
 	LIBBPF_MAP_UNSPEC,
diff --git a/tools/testing/selftests/bpf/bpf_arena_common.h b/tools/testing/selftests/bpf/bpf_arena_common.h
index bcf195c64a45..567491f3e1b5 100644
--- a/tools/testing/selftests/bpf/bpf_arena_common.h
+++ b/tools/testing/selftests/bpf/bpf_arena_common.h
@@ -32,7 +32,7 @@
  */
 #endif
 
-#if defined(__BPF_FEATURE_ARENA_CAST) && !defined(BPF_ARENA_FORCE_ASM)
+#if defined(__BPF_FEATURE_ADDR_SPACE_CAST) && !defined(BPF_ARENA_FORCE_ASM)
 #define __arena __attribute__((address_space(1)))
 #define cast_kern(ptr) /* nop for bpf prog. emitted by LLVM */
 #define cast_user(ptr) /* nop for bpf prog. emitted by LLVM */
diff --git a/tools/testing/selftests/bpf/progs/arena_htab.c b/tools/testing/selftests/bpf/progs/arena_htab.c
index b7bb712cacfd..1e6ac187a6a0 100644
--- a/tools/testing/selftests/bpf/progs/arena_htab.c
+++ b/tools/testing/selftests/bpf/progs/arena_htab.c
@@ -22,7 +22,7 @@ int zero = 0;
 SEC("syscall")
 int arena_htab_llvm(void *ctx)
 {
-#if defined(__BPF_FEATURE_ARENA_CAST) || defined(BPF_ARENA_FORCE_ASM)
+#if defined(__BPF_FEATURE_ADDR_SPACE_CAST) || defined(BPF_ARENA_FORCE_ASM)
 	struct htab __arena *htab;
 	__u64 i;
 
diff --git a/tools/testing/selftests/bpf/progs/arena_list.c b/tools/testing/selftests/bpf/progs/arena_list.c
index cd35b8448435..c0422c58cee2 100644
--- a/tools/testing/selftests/bpf/progs/arena_list.c
+++ b/tools/testing/selftests/bpf/progs/arena_list.c
@@ -30,13 +30,13 @@ int list_sum;
 int cnt;
 bool skip = false;
 
-#ifdef __BPF_FEATURE_ARENA_CAST
+#ifdef __BPF_FEATURE_ADDR_SPACE_CAST
 long __arena arena_sum;
 int __arena test_val = 1;
 struct arena_list_head __arena global_head;
 #else
-long arena_sum SEC(".arena.1");
-int test_val SEC(".arena.1");
+long arena_sum SEC(".addr_space.1");
+int test_val SEC(".addr_space.1");
 #endif
 
 int zero;
@@ -44,7 +44,7 @@ int zero;
 SEC("syscall")
 int arena_list_add(void *ctx)
 {
-#ifdef __BPF_FEATURE_ARENA_CAST
+#ifdef __BPF_FEATURE_ADDR_SPACE_CAST
 	__u64 i;
 
 	list_head = &global_head;
@@ -66,7 +66,7 @@ int arena_list_add(void *ctx)
 SEC("syscall")
 int arena_list_del(void *ctx)
 {
-#ifdef __BPF_FEATURE_ARENA_CAST
+#ifdef __BPF_FEATURE_ADDR_SPACE_CAST
 	struct elem __arena *n;
 	int sum = 0;
 
diff --git a/tools/testing/selftests/bpf/progs/verifier_arena.c b/tools/testing/selftests/bpf/progs/verifier_arena.c
index 5540b05ff9ee..969bc091060b 100644
--- a/tools/testing/selftests/bpf/progs/verifier_arena.c
+++ b/tools/testing/selftests/bpf/progs/verifier_arena.c
@@ -19,7 +19,7 @@ SEC("syscall")
 __success __retval(0)
 int basic_alloc1(void *ctx)
 {
-#if defined(__BPF_FEATURE_ARENA_CAST)
+#if defined(__BPF_FEATURE_ADDR_SPACE_CAST)
 	volatile int __arena *page1, *page2, *no_page, *page3;
 
 	page1 = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
@@ -58,7 +58,7 @@ SEC("syscall")
 __success __retval(0)
 int basic_alloc2(void *ctx)
 {
-#if defined(__BPF_FEATURE_ARENA_CAST)
+#if defined(__BPF_FEATURE_ADDR_SPACE_CAST)
 	volatile char __arena *page1, *page2, *page3, *page4;
 
 	page1 = bpf_arena_alloc_pages(&arena, NULL, 2, NUMA_NO_NODE, 0);
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH bpf 3/4] selftests/bpf: Remove hard coded PAGE_SIZE macro.
  2024-03-15  2:18 [PATCH bpf 0/4] bpf: arena followups Alexei Starovoitov
  2024-03-15  2:18 ` [PATCH bpf 1/4] bpf: Clarify bpf_arena comments Alexei Starovoitov
  2024-03-15  2:18 ` [PATCH bpf 2/4] libbpf, selftests/bpf: Adjust libbpf, bpftool, selftests to match LLVM Alexei Starovoitov
@ 2024-03-15  2:18 ` Alexei Starovoitov
  2024-03-15  2:18 ` [PATCH bpf 4/4] selftests/bpf: Add arena test case for 4Gbyte corner case Alexei Starovoitov
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 10+ messages in thread
From: Alexei Starovoitov @ 2024-03-15  2:18 UTC (permalink / raw)
  To: bpf; +Cc: daniel, andrii, martin.lau, eddyz87, kernel-team

From: Alexei Starovoitov <ast@kernel.org>

Remove hard coded PAGE_SIZE.
Add #include <sys/user.h> instead (that works on x86-64 and s390)
and fallback to slow getpagesize() for aarch64.

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
 tools/testing/selftests/bpf/prog_tests/arena_htab.c | 8 +++++---
 tools/testing/selftests/bpf/prog_tests/arena_list.c | 7 +++++--
 2 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/tools/testing/selftests/bpf/prog_tests/arena_htab.c b/tools/testing/selftests/bpf/prog_tests/arena_htab.c
index 0766702de846..d69fd2465f53 100644
--- a/tools/testing/selftests/bpf/prog_tests/arena_htab.c
+++ b/tools/testing/selftests/bpf/prog_tests/arena_htab.c
@@ -3,12 +3,14 @@
 #include <test_progs.h>
 #include <sys/mman.h>
 #include <network_helpers.h>
-
+#include <sys/user.h>
+#ifndef PAGE_SIZE /* on some archs it comes in sys/user.h */
+#include <unistd.h>
+#define PAGE_SIZE getpagesize()
+#endif
 #include "arena_htab_asm.skel.h"
 #include "arena_htab.skel.h"
 
-#define PAGE_SIZE 4096
-
 #include "bpf_arena_htab.h"
 
 static void test_arena_htab_common(struct htab *htab)
diff --git a/tools/testing/selftests/bpf/prog_tests/arena_list.c b/tools/testing/selftests/bpf/prog_tests/arena_list.c
index e61886debab1..d15867cddde0 100644
--- a/tools/testing/selftests/bpf/prog_tests/arena_list.c
+++ b/tools/testing/selftests/bpf/prog_tests/arena_list.c
@@ -3,8 +3,11 @@
 #include <test_progs.h>
 #include <sys/mman.h>
 #include <network_helpers.h>
-
-#define PAGE_SIZE 4096
+#include <sys/user.h>
+#ifndef PAGE_SIZE /* on some archs it comes in sys/user.h */
+#include <unistd.h>
+#define PAGE_SIZE getpagesize()
+#endif
 
 #include "bpf_arena_list.h"
 #include "arena_list.skel.h"
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [PATCH bpf 4/4] selftests/bpf: Add arena test case for 4Gbyte corner case
  2024-03-15  2:18 [PATCH bpf 0/4] bpf: arena followups Alexei Starovoitov
                   ` (2 preceding siblings ...)
  2024-03-15  2:18 ` [PATCH bpf 3/4] selftests/bpf: Remove hard coded PAGE_SIZE macro Alexei Starovoitov
@ 2024-03-15  2:18 ` Alexei Starovoitov
  2024-03-15 18:00 ` [PATCH bpf 0/4] bpf: arena followups Stanislav Fomichev
  2024-03-15 21:30 ` patchwork-bot+netdevbpf
  5 siblings, 0 replies; 10+ messages in thread
From: Alexei Starovoitov @ 2024-03-15  2:18 UTC (permalink / raw)
  To: bpf; +Cc: daniel, andrii, martin.lau, eddyz87, kernel-team

From: Alexei Starovoitov <ast@kernel.org>

Check that 4Gbyte arena can be allocated and overflow/underflow access in
the first and the last page behaves as expected.

Signed-off-by: Alexei Starovoitov <ast@kernel.org>
---
 .../selftests/bpf/prog_tests/verifier.c       |  2 +
 .../bpf/progs/verifier_arena_large.c          | 68 +++++++++++++++++++
 2 files changed, 70 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/verifier_arena_large.c

diff --git a/tools/testing/selftests/bpf/prog_tests/verifier.c b/tools/testing/selftests/bpf/prog_tests/verifier.c
index 985273832f89..c4f9f306646e 100644
--- a/tools/testing/selftests/bpf/prog_tests/verifier.c
+++ b/tools/testing/selftests/bpf/prog_tests/verifier.c
@@ -5,6 +5,7 @@
 #include "cap_helpers.h"
 #include "verifier_and.skel.h"
 #include "verifier_arena.skel.h"
+#include "verifier_arena_large.skel.h"
 #include "verifier_array_access.skel.h"
 #include "verifier_basic_stack.skel.h"
 #include "verifier_bitfield_write.skel.h"
@@ -120,6 +121,7 @@ static void run_tests_aux(const char *skel_name,
 
 void test_verifier_and(void)                  { RUN(verifier_and); }
 void test_verifier_arena(void)                { RUN(verifier_arena); }
+void test_verifier_arena_large(void)          { RUN(verifier_arena_large); }
 void test_verifier_basic_stack(void)          { RUN(verifier_basic_stack); }
 void test_verifier_bitfield_write(void)       { RUN(verifier_bitfield_write); }
 void test_verifier_bounds(void)               { RUN(verifier_bounds); }
diff --git a/tools/testing/selftests/bpf/progs/verifier_arena_large.c b/tools/testing/selftests/bpf/progs/verifier_arena_large.c
new file mode 100644
index 000000000000..ef66ea460264
--- /dev/null
+++ b/tools/testing/selftests/bpf/progs/verifier_arena_large.c
@@ -0,0 +1,68 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
+
+#include <vmlinux.h>
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+#include "bpf_misc.h"
+#include "bpf_experimental.h"
+#include "bpf_arena_common.h"
+
+#define ARENA_SIZE (1ull << 32)
+
+struct {
+	__uint(type, BPF_MAP_TYPE_ARENA);
+	__uint(map_flags, BPF_F_MMAPABLE);
+	__uint(max_entries, ARENA_SIZE / PAGE_SIZE);
+} arena SEC(".maps");
+
+SEC("syscall")
+__success __retval(0)
+int big_alloc1(void *ctx)
+{
+#if defined(__BPF_FEATURE_ADDR_SPACE_CAST)
+	volatile char __arena *page1, *page2, *no_page, *page3;
+	void __arena *base;
+
+	page1 = base = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
+	if (!page1)
+		return 1;
+	*page1 = 1;
+	page2 = bpf_arena_alloc_pages(&arena, base + ARENA_SIZE - PAGE_SIZE,
+				      1, NUMA_NO_NODE, 0);
+	if (!page2)
+		return 2;
+	*page2 = 2;
+	no_page = bpf_arena_alloc_pages(&arena, base + ARENA_SIZE,
+					1, NUMA_NO_NODE, 0);
+	if (no_page)
+		return 3;
+	if (*page1 != 1)
+		return 4;
+	if (*page2 != 2)
+		return 5;
+	bpf_arena_free_pages(&arena, (void __arena *)page1, 1);
+	if (*page2 != 2)
+		return 6;
+	if (*page1 != 0) /* use-after-free should return 0 */
+		return 7;
+	page3 = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
+	if (!page3)
+		return 8;
+	*page3 = 3;
+	if (page1 != page3)
+		return 9;
+	if (*page2 != 2)
+		return 10;
+	if (*(page1 + PAGE_SIZE) != 0)
+		return 11;
+	if (*(page1 - PAGE_SIZE) != 0)
+		return 12;
+	if (*(page2 + PAGE_SIZE) != 0)
+		return 13;
+	if (*(page2 - PAGE_SIZE) != 0)
+		return 14;
+#endif
+	return 0;
+}
+char _license[] SEC("license") = "GPL";
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [PATCH bpf 2/4] libbpf, selftests/bpf: Adjust libbpf, bpftool, selftests to match LLVM
  2024-03-15  2:18 ` [PATCH bpf 2/4] libbpf, selftests/bpf: Adjust libbpf, bpftool, selftests to match LLVM Alexei Starovoitov
@ 2024-03-15  3:57   ` Andrii Nakryiko
  2024-03-15  4:44     ` Alexei Starovoitov
  0 siblings, 1 reply; 10+ messages in thread
From: Andrii Nakryiko @ 2024-03-15  3:57 UTC (permalink / raw)
  To: Alexei Starovoitov; +Cc: bpf, daniel, andrii, martin.lau, eddyz87, kernel-team

On Thu, Mar 14, 2024 at 7:18 PM Alexei Starovoitov
<alexei.starovoitov@gmail.com> wrote:
>
> From: Alexei Starovoitov <ast@kernel.org>
>
> The selftests use
> to tell LLVM about special pointers. For LLVM there is nothing "arena"
> about them. They are simply pointers in a different address space.
> Hence LLVM diff https://github.com/llvm/llvm-project/pull/85161 renamed:
> . macro __BPF_FEATURE_ARENA_CAST -> __BPF_FEATURE_ADDR_SPACE_CAST
> . global variables in __attribute__((address_space(N))) are now
>   placed in section named ".address_space.N" instead of ".arena.N".

.addr_space.N, according to code below

>
> Adjust libbpf, bpftool, and selftests to match LLVM.
>
> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
> ---
>  tools/bpf/bpftool/gen.c                            |  2 +-
>  tools/lib/bpf/libbpf.c                             |  2 +-
>  tools/testing/selftests/bpf/bpf_arena_common.h     |  2 +-
>  tools/testing/selftests/bpf/progs/arena_htab.c     |  2 +-
>  tools/testing/selftests/bpf/progs/arena_list.c     | 10 +++++-----
>  tools/testing/selftests/bpf/progs/verifier_arena.c |  4 ++--
>  6 files changed, 11 insertions(+), 11 deletions(-)
>
> diff --git a/tools/bpf/bpftool/gen.c b/tools/bpf/bpftool/gen.c
> index 4fa4ade1ce74..540c0f2c4fda 100644
> --- a/tools/bpf/bpftool/gen.c
> +++ b/tools/bpf/bpftool/gen.c
> @@ -121,7 +121,7 @@ static bool get_datasec_ident(const char *sec_name, char *buf, size_t buf_sz)
>         int i, n;
>
>         /* recognize hard coded LLVM section name */
> -       if (strcmp(sec_name, ".arena.1") == 0) {
> +       if (strcmp(sec_name, ".addr_space.1") == 0) {
>                 /* this is the name to use in skeleton */
>                 snprintf(buf, buf_sz, "arena");
>                 return true;
> diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c
> index efab29b8935b..36e26f4f5997 100644
> --- a/tools/lib/bpf/libbpf.c
> +++ b/tools/lib/bpf/libbpf.c
> @@ -498,7 +498,7 @@ struct bpf_struct_ops {
>  #define KSYMS_SEC ".ksyms"
>  #define STRUCT_OPS_SEC ".struct_ops"
>  #define STRUCT_OPS_LINK_SEC ".struct_ops.link"
> -#define ARENA_SEC ".arena.1"
> +#define ARENA_SEC ".addr_space.1"
>
>  enum libbpf_map_type {
>         LIBBPF_MAP_UNSPEC,
> diff --git a/tools/testing/selftests/bpf/bpf_arena_common.h b/tools/testing/selftests/bpf/bpf_arena_common.h
> index bcf195c64a45..567491f3e1b5 100644
> --- a/tools/testing/selftests/bpf/bpf_arena_common.h
> +++ b/tools/testing/selftests/bpf/bpf_arena_common.h
> @@ -32,7 +32,7 @@
>   */
>  #endif
>
> -#if defined(__BPF_FEATURE_ARENA_CAST) && !defined(BPF_ARENA_FORCE_ASM)
> +#if defined(__BPF_FEATURE_ADDR_SPACE_CAST) && !defined(BPF_ARENA_FORCE_ASM)
>  #define __arena __attribute__((address_space(1)))
>  #define cast_kern(ptr) /* nop for bpf prog. emitted by LLVM */
>  #define cast_user(ptr) /* nop for bpf prog. emitted by LLVM */
> diff --git a/tools/testing/selftests/bpf/progs/arena_htab.c b/tools/testing/selftests/bpf/progs/arena_htab.c
> index b7bb712cacfd..1e6ac187a6a0 100644
> --- a/tools/testing/selftests/bpf/progs/arena_htab.c
> +++ b/tools/testing/selftests/bpf/progs/arena_htab.c
> @@ -22,7 +22,7 @@ int zero = 0;
>  SEC("syscall")
>  int arena_htab_llvm(void *ctx)
>  {
> -#if defined(__BPF_FEATURE_ARENA_CAST) || defined(BPF_ARENA_FORCE_ASM)
> +#if defined(__BPF_FEATURE_ADDR_SPACE_CAST) || defined(BPF_ARENA_FORCE_ASM)
>         struct htab __arena *htab;
>         __u64 i;
>
> diff --git a/tools/testing/selftests/bpf/progs/arena_list.c b/tools/testing/selftests/bpf/progs/arena_list.c
> index cd35b8448435..c0422c58cee2 100644
> --- a/tools/testing/selftests/bpf/progs/arena_list.c
> +++ b/tools/testing/selftests/bpf/progs/arena_list.c
> @@ -30,13 +30,13 @@ int list_sum;
>  int cnt;
>  bool skip = false;
>
> -#ifdef __BPF_FEATURE_ARENA_CAST
> +#ifdef __BPF_FEATURE_ADDR_SPACE_CAST
>  long __arena arena_sum;
>  int __arena test_val = 1;
>  struct arena_list_head __arena global_head;
>  #else
> -long arena_sum SEC(".arena.1");
> -int test_val SEC(".arena.1");
> +long arena_sum SEC(".addr_space.1");
> +int test_val SEC(".addr_space.1");
>  #endif
>
>  int zero;
> @@ -44,7 +44,7 @@ int zero;
>  SEC("syscall")
>  int arena_list_add(void *ctx)
>  {
> -#ifdef __BPF_FEATURE_ARENA_CAST
> +#ifdef __BPF_FEATURE_ADDR_SPACE_CAST
>         __u64 i;
>
>         list_head = &global_head;
> @@ -66,7 +66,7 @@ int arena_list_add(void *ctx)
>  SEC("syscall")
>  int arena_list_del(void *ctx)
>  {
> -#ifdef __BPF_FEATURE_ARENA_CAST
> +#ifdef __BPF_FEATURE_ADDR_SPACE_CAST
>         struct elem __arena *n;
>         int sum = 0;
>
> diff --git a/tools/testing/selftests/bpf/progs/verifier_arena.c b/tools/testing/selftests/bpf/progs/verifier_arena.c
> index 5540b05ff9ee..969bc091060b 100644
> --- a/tools/testing/selftests/bpf/progs/verifier_arena.c
> +++ b/tools/testing/selftests/bpf/progs/verifier_arena.c
> @@ -19,7 +19,7 @@ SEC("syscall")
>  __success __retval(0)
>  int basic_alloc1(void *ctx)
>  {
> -#if defined(__BPF_FEATURE_ARENA_CAST)
> +#if defined(__BPF_FEATURE_ADDR_SPACE_CAST)
>         volatile int __arena *page1, *page2, *no_page, *page3;
>
>         page1 = bpf_arena_alloc_pages(&arena, NULL, 1, NUMA_NO_NODE, 0);
> @@ -58,7 +58,7 @@ SEC("syscall")
>  __success __retval(0)
>  int basic_alloc2(void *ctx)
>  {
> -#if defined(__BPF_FEATURE_ARENA_CAST)
> +#if defined(__BPF_FEATURE_ADDR_SPACE_CAST)
>         volatile char __arena *page1, *page2, *page3, *page4;
>
>         page1 = bpf_arena_alloc_pages(&arena, NULL, 2, NUMA_NO_NODE, 0);
> --
> 2.43.0
>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH bpf 2/4] libbpf, selftests/bpf: Adjust libbpf, bpftool, selftests to match LLVM
  2024-03-15  3:57   ` Andrii Nakryiko
@ 2024-03-15  4:44     ` Alexei Starovoitov
  0 siblings, 0 replies; 10+ messages in thread
From: Alexei Starovoitov @ 2024-03-15  4:44 UTC (permalink / raw)
  To: Andrii Nakryiko
  Cc: bpf, Daniel Borkmann, Andrii Nakryiko, Martin KaFai Lau, Eddy Z,
	Kernel Team

On Thu, Mar 14, 2024 at 8:57 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Thu, Mar 14, 2024 at 7:18 PM Alexei Starovoitov
> <alexei.starovoitov@gmail.com> wrote:
> >
> > From: Alexei Starovoitov <ast@kernel.org>
> >
> > The selftests use
> > to tell LLVM about special pointers. For LLVM there is nothing "arena"
> > about them. They are simply pointers in a different address space.
> > Hence LLVM diff https://github.com/llvm/llvm-project/pull/85161 renamed:
> > . macro __BPF_FEATURE_ARENA_CAST -> __BPF_FEATURE_ADDR_SPACE_CAST
> > . global variables in __attribute__((address_space(N))) are now
> >   placed in section named ".address_space.N" instead of ".arena.N".
>
> .addr_space.N, according to code below

yes. subject typo.

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH bpf 1/4] bpf: Clarify bpf_arena comments.
  2024-03-15  2:18 ` [PATCH bpf 1/4] bpf: Clarify bpf_arena comments Alexei Starovoitov
@ 2024-03-15 17:59   ` Stanislav Fomichev
  0 siblings, 0 replies; 10+ messages in thread
From: Stanislav Fomichev @ 2024-03-15 17:59 UTC (permalink / raw)
  To: Alexei Starovoitov; +Cc: bpf, daniel, andrii, martin.lau, eddyz87, kernel-team

On 03/14, Alexei Starovoitov wrote:
> From: Alexei Starovoitov <ast@kernel.org>
> 
> Clarify two bpf_arena comments, use existing SZ_4G #define,
> improve page_cnt check.
> 
> Signed-off-by: Alexei Starovoitov <ast@kernel.org>
> ---
>  kernel/bpf/arena.c | 25 ++++++++++++++++++-------
>  1 file changed, 18 insertions(+), 7 deletions(-)
> 
> diff --git a/kernel/bpf/arena.c b/kernel/bpf/arena.c
> index 86571e760dd6..343c3456c8dd 100644
> --- a/kernel/bpf/arena.c
> +++ b/kernel/bpf/arena.c
> @@ -38,7 +38,7 @@
>  
>  /* number of bytes addressable by LDX/STX insn with 16-bit 'off' field */
>  #define GUARD_SZ (1ull << sizeof(((struct bpf_insn *)0)->off) * 8)

Unrelated nit, maybe worth doing the following as well:

#define GUARD_SZ (1ull << sizeof_field(struct bpf_insn, off) * 8)

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH bpf 0/4] bpf: arena followups.
  2024-03-15  2:18 [PATCH bpf 0/4] bpf: arena followups Alexei Starovoitov
                   ` (3 preceding siblings ...)
  2024-03-15  2:18 ` [PATCH bpf 4/4] selftests/bpf: Add arena test case for 4Gbyte corner case Alexei Starovoitov
@ 2024-03-15 18:00 ` Stanislav Fomichev
  2024-03-15 21:30 ` patchwork-bot+netdevbpf
  5 siblings, 0 replies; 10+ messages in thread
From: Stanislav Fomichev @ 2024-03-15 18:00 UTC (permalink / raw)
  To: Alexei Starovoitov; +Cc: bpf, daniel, andrii, martin.lau, eddyz87, kernel-team

On 03/14, Alexei Starovoitov wrote:
> From: Alexei Starovoitov <ast@kernel.org>
> 
> A set of follow ups to clean up bpf_arena and adjust to the latest LLVM.
> 
> Alexei Starovoitov (4):
>   bpf: Clarify bpf_arena comments.
>   libbpf, selftests/bpf: Adjust libbpf, bpftool, selftests to match LLVM
>   selftests/bpf: Remove hard coded PAGE_SIZE macro.
>   selftests/bpf: Add arena test case for 4Gbyte corner case
> 
>  kernel/bpf/arena.c                            | 25 +++++--
>  tools/bpf/bpftool/gen.c                       |  2 +-
>  tools/lib/bpf/libbpf.c                        |  2 +-
>  .../testing/selftests/bpf/bpf_arena_common.h  |  2 +-
>  .../selftests/bpf/prog_tests/arena_htab.c     |  8 ++-
>  .../selftests/bpf/prog_tests/arena_list.c     |  7 +-
>  .../selftests/bpf/prog_tests/verifier.c       |  2 +
>  .../testing/selftests/bpf/progs/arena_htab.c  |  2 +-
>  .../testing/selftests/bpf/progs/arena_list.c  | 10 +--
>  .../selftests/bpf/progs/verifier_arena.c      |  4 +-
>  .../bpf/progs/verifier_arena_large.c          | 68 +++++++++++++++++++
>  11 files changed, 109 insertions(+), 23 deletions(-)
>  create mode 100644 tools/testing/selftests/bpf/progs/verifier_arena_large.c
> 
> -- 
> 2.43.0
> 

Acked-by: Stanislav Fomichev <sdf@google.com>

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [PATCH bpf 0/4] bpf: arena followups.
  2024-03-15  2:18 [PATCH bpf 0/4] bpf: arena followups Alexei Starovoitov
                   ` (4 preceding siblings ...)
  2024-03-15 18:00 ` [PATCH bpf 0/4] bpf: arena followups Stanislav Fomichev
@ 2024-03-15 21:30 ` patchwork-bot+netdevbpf
  5 siblings, 0 replies; 10+ messages in thread
From: patchwork-bot+netdevbpf @ 2024-03-15 21:30 UTC (permalink / raw)
  To: Alexei Starovoitov; +Cc: bpf, daniel, andrii, martin.lau, eddyz87, kernel-team

Hello:

This series was applied to bpf/bpf.git (master)
by Andrii Nakryiko <andrii@kernel.org>:

On Thu, 14 Mar 2024 19:18:30 -0700 you wrote:
> From: Alexei Starovoitov <ast@kernel.org>
> 
> A set of follow ups to clean up bpf_arena and adjust to the latest LLVM.
> 
> Alexei Starovoitov (4):
>   bpf: Clarify bpf_arena comments.
>   libbpf, selftests/bpf: Adjust libbpf, bpftool, selftests to match LLVM
>   selftests/bpf: Remove hard coded PAGE_SIZE macro.
>   selftests/bpf: Add arena test case for 4Gbyte corner case
> 
> [...]

Here is the summary with links:
  - [bpf,1/4] bpf: Clarify bpf_arena comments.
    https://git.kernel.org/bpf/bpf/c/ee498a38f317
  - [bpf,2/4] libbpf, selftests/bpf: Adjust libbpf, bpftool, selftests to match LLVM
    https://git.kernel.org/bpf/bpf/c/10ebe835c937
  - [bpf,3/4] selftests/bpf: Remove hard coded PAGE_SIZE macro.
    https://git.kernel.org/bpf/bpf/c/9a2d5a966b47
  - [bpf,4/4] selftests/bpf: Add arena test case for 4Gbyte corner case
    https://git.kernel.org/bpf/bpf/c/a90c5845db95

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2024-03-15 21:30 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-03-15  2:18 [PATCH bpf 0/4] bpf: arena followups Alexei Starovoitov
2024-03-15  2:18 ` [PATCH bpf 1/4] bpf: Clarify bpf_arena comments Alexei Starovoitov
2024-03-15 17:59   ` Stanislav Fomichev
2024-03-15  2:18 ` [PATCH bpf 2/4] libbpf, selftests/bpf: Adjust libbpf, bpftool, selftests to match LLVM Alexei Starovoitov
2024-03-15  3:57   ` Andrii Nakryiko
2024-03-15  4:44     ` Alexei Starovoitov
2024-03-15  2:18 ` [PATCH bpf 3/4] selftests/bpf: Remove hard coded PAGE_SIZE macro Alexei Starovoitov
2024-03-15  2:18 ` [PATCH bpf 4/4] selftests/bpf: Add arena test case for 4Gbyte corner case Alexei Starovoitov
2024-03-15 18:00 ` [PATCH bpf 0/4] bpf: arena followups Stanislav Fomichev
2024-03-15 21:30 ` patchwork-bot+netdevbpf

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox