Skip to contentSkip to conversation

Mmapable task local storage.

RFC PATCH In review Hao Luo 6 participants 17 messages Mar 24, 2022 – Apr 2, 2022

Subsystems (4)
lore
Share & tools
lore

Hao Luo

author
#0 lore raw

cover Mmapable task local storage.

Some map types support mmap operation, which allows userspace to communicate with BPF programs directly. Currently only arraymap and ringbuf have mmap implemented.

However, in some use cases, when multiple program instances can run concurrently, global mmapable memory can cause race. In that case, userspace needs to provide necessary synchronizations to coordinate the usage of mapped global data. This can be a source of bottleneck.

It would be great to have a mmapable local storage in that case. This patch adds that.

Mmap isn't BPF syscall, so unpriv users can also use it to interact with maps.

Currently the only way of allocating mmapable map area is using vmalloc() and it's only used at map allocation time. Vmalloc() may sleep, therefore it's not suitable for maps that may allocate memory in an atomic context such as local storage. Local storage uses kmalloc() with GFP_ATOMIC, which doesn't sleep. This patch uses kmalloc() with GFP_ATOMIC as well for mmapable map area.

Allocating mmapable memory has requirment on page alignment. So we have to deliberately allocate more memory than necessary to obtain an address that has sdata->data aligned at page boundary. The calculations for mmapable allocation size, and the actual allocation/deallocation are packaged in three functions:

- bpf_map_mmapable_alloc_size()
- bpf_map_mmapable_kzalloc()
- bpf_map_mmapable_kfree()

BPF local storage uses them to provide generic mmap API:

- bpf_local_storage_mmap()

And task local storage adds the mmap callback:

- task_storage_map_mmap()

When application calls mmap on a task local storage, it gets its own local storage.

Overall, mmapable local storage trades off memory with flexibility and efficiency. It brings memory fragmentation but can make programs stateless. Therefore useful in some cases.

Hao Luo (2):
bpf: Mmapable local storage. selftests/bpf: Test mmapable task local storage.

 include/linux/bpf.h                           |  4 +
 include/linux/bpf_local_storage.h             |  5 +-
 kernel/bpf/bpf_local_storage.c                | 73 +++++++++++++++++--
 kernel/bpf/bpf_task_storage.c                 | 40 ++++++++++
 kernel/bpf/syscall.c                          | 67 +++++++++++++++++
 .../bpf/prog_tests/task_local_storage.c       | 38 ++++++++++
 .../bpf/progs/task_local_storage_mmapable.c   | 38 ++++++++++
 7 files changed, 257 insertions(+), 8 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/progs/task_local_storage_mmapable.c
Signature · 1 line
2.35.1.1021.g381101b075-goog
author
#1 lore raw

RFC PATCH 1/2 bpf: Mmapable local storage.

Some map types support mmap operation, which allows userspace to communicate with BPF programs directly. Currently only arraymap and ringbuf have mmap implemented.

However, in some use cases, when multiple program instances can run concurrently, global mmapable memory can cause race. In that case, userspace needs to provide necessary synchronizations to coordinate the usage of mapped global data. This can be a source of bottleneck.

It would be great to have a mmapable local storage in that case. This patch adds that.

Mmap isn't BPF syscall, so unpriv users can also use it to interact with maps.

Currently the only way of allocating mmapable map area is using vmalloc() and it's only used at map allocation time. Vmalloc() may sleep, therefore it's not suitable for maps that may allocate memory in an atomic context such as local storage. Local storage uses kmalloc() with GFP_ATOMIC, which doesn't sleep. This patch uses kmalloc() with GFP_ATOMIC as well for mmapable map area.

Allocating mmapable memory has requirment on page alignment. So we have to deliberately allocate more memory than necessary to obtain an address that has sdata->data aligned at page boundary. The calculations for mmapable allocation size, and the actual allocation/deallocation are packaged in three functions:

- bpf_map_mmapable_alloc_size()
- bpf_map_mmapable_kzalloc()
- bpf_map_mmapable_kfree()

BPF local storage uses them to provide generic mmap API:

- bpf_local_storage_mmap()

And task local storage adds the mmap callback:

- task_storage_map_mmap()

When application calls mmap on a task local storage, it gets its own local storage.

Overall, mmapable local storage trades off memory with flexibility and efficiency. It brings memory fragmentation but can make programs stateless. Therefore useful in some cases.

---
 include/linux/bpf.h               |  4 ++
 include/linux/bpf_local_storage.h |  5 ++-
 kernel/bpf/bpf_local_storage.c    | 73 ++++++++++++++++++++++++++++---
 kernel/bpf/bpf_task_storage.c     | 40 +++++++++++++++++
 kernel/bpf/syscall.c              | 67 ++++++++++++++++++++++++++++
 5 files changed, 181 insertions(+), 8 deletions(-)
include/linux/bpf.h +4 -0
index bdb5298735ce..d76b8d6f91d2 100644
@@ -1549,6 +1549,10 @@ bpf_map_alloc_percpu(const struct bpf_map *map, size_t size, size_t align,
 	return __alloc_percpu_gfp(size, align, flags);
 }
 #endif
+size_t bpf_map_mmapable_alloc_size(size_t size, size_t offset);
+void *bpf_map_mmapable_kzalloc(const struct bpf_map *map, size_t size,
+			       size_t offset, gfp_t flags);
+void bpf_map_mmapable_kfree(void *ptr);
 
 extern int sysctl_unprivileged_bpf_disabled;
 
include/linux/bpf_local_storage.h +4 -1
index 493e63258497..36dc1102ec48 100644
@@ -74,7 +74,8 @@ struct bpf_local_storage_elem {
 	struct hlist_node snode;	/* Linked to bpf_local_storage */
 	struct bpf_local_storage __rcu *local_storage;
 	struct rcu_head rcu;
-	/* 8 bytes hole */
+	u32 map_flags;
+	/* 4 bytes hole */
 	/* The data is stored in another cacheline to minimize
 	 * the number of cachelines access during a cache hit.
 	 */
@@ -168,4 +169,6 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
 
 void bpf_local_storage_free_rcu(struct rcu_head *rcu);
 
+int bpf_local_storage_mmap(struct bpf_local_storage_map *smap, void *data,
+			   struct vm_area_struct *vma);
 #endif /* _BPF_LOCAL_STORAGE_H */
kernel/bpf/bpf_local_storage.c +66 -7

116 lines not on this page · raw message

kernel/bpf/bpf_task_storage.c +40 -0
index 6638a0ecc3d2..9552b84f96db 100644
@@ -307,6 +307,45 @@ static void task_storage_map_free(struct bpf_map *map)
 	bpf_local_storage_map_free(smap, &bpf_task_storage_busy);
 }
 
+static int task_storage_map_mmap(struct bpf_map *map, struct vm_area_struct *vma)
+{
+	struct bpf_local_storage_map *smap;
+	struct bpf_local_storage_data *sdata;
+	int err;
+
+	if (!(map->map_flags & BPF_F_MMAPABLE))
+		return -EINVAL;
+
+	rcu_read_lock();
+	if (!bpf_task_storage_trylock()) {
+		rcu_read_unlock();
+		return -EBUSY;
+	}
+
+	smap = (struct bpf_local_storage_map *)map;
+	sdata = task_storage_lookup(current, map, true);
+	if (sdata) {
+		err = bpf_local_storage_mmap(smap, sdata->data, vma);
+		goto unlock;
+	}
+
+	/* only allocate new storage, when the task is refcounted */
+	if (refcount_read(&current->usage)) {
+		sdata = bpf_local_storage_update(current, smap, NULL,
+						 BPF_NOEXIST, GFP_ATOMIC);
+		if (IS_ERR(sdata)) {
+			err = PTR_ERR(sdata);
+			goto unlock;
+		}
+	}
+
+	err = bpf_local_storage_mmap(smap, sdata->data, vma);
+unlock:
+	bpf_task_storage_unlock();
+	rcu_read_unlock();
+	return err;
+}
+
 static int task_storage_map_btf_id;
 const struct bpf_map_ops task_storage_map_ops = {
 	.map_meta_equal = bpf_map_meta_equal,
@@ -321,6 +360,7 @@ const struct bpf_map_ops task_storage_map_ops = {
 	.map_btf_name = "bpf_local_storage_map",
 	.map_btf_id = &task_storage_map_btf_id,
 	.map_owner_storage_ptr = task_storage_ptr,
+	.map_mmap = &task_storage_map_mmap,
 };
 
 const struct bpf_func_proto bpf_task_storage_get_proto = {
kernel/bpf/syscall.c +67 -0

73 lines not on this page · raw message

Signature · 1 line
2.35.1.1021.g381101b075-goog
author
#2 lore raw

RFC PATCH 2/2 selftests/bpf: Test mmapable task local storage.

In reply to Hao Luo

Tests mmapable task local storage.

---
 .../bpf/prog_tests/task_local_storage.c       | 38 +++++++++++++++++++
 .../bpf/progs/task_local_storage_mmapable.c   | 38 +++++++++++++++++++
 2 files changed, 76 insertions(+)
 create mode 100644 tools/testing/selftests/bpf/progs/task_local_storage_mmapable.c
tools/testing/selftests/bpf/prog_tests/task_local_storage.c +38 -0
index 035c263aab1b..24e6edd32a78 100644
@@ -6,8 +6,10 @@
 #include <sys/syscall.h>   /* For SYS_xxx definitions */
 #include <sys/types.h>
 #include <test_progs.h>
+#include <sys/mman.h>
 #include "task_local_storage.skel.h"
 #include "task_local_storage_exit_creds.skel.h"
+#include "task_local_storage_mmapable.skel.h"
 #include "task_ls_recursion.skel.h"
 
 static void test_sys_enter_exit(void)
@@ -81,6 +83,40 @@ static void test_recursion(void)
 	task_ls_recursion__destroy(skel);
 }
 
+#define MAGIC_VALUE 0xabcd1234
+
+static void test_mmapable(void)
+{
+	struct task_local_storage_mmapable *skel;
+	const long page_size = sysconf(_SC_PAGE_SIZE);
+	int fd, err;
+	void *ptr;
+
+	skel = task_local_storage_mmapable__open_and_load();
+	if (!ASSERT_OK_PTR(skel, "skel_open_and_load"))
+		return;
+
+	fd = bpf_map__fd(skel->maps.mmapable_map);
+	ptr = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
+	if (!ASSERT_NEQ(ptr, MAP_FAILED, "mmap"))
+		goto out;
+
+	skel->bss->target_pid = syscall(SYS_gettid);
+
+	err = task_local_storage_mmapable__attach(skel);
+	if (!ASSERT_OK(err, "skel_attach"))
+		goto unmap;
+
+	syscall(SYS_gettid);
+
+	ASSERT_EQ(*(u64 *)ptr, MAGIC_VALUE, "value");
+
+unmap:
+	munmap(ptr, page_size);
+out:
+	task_local_storage_mmapable__destroy(skel);
+}
+
 void test_task_local_storage(void)
 {
 	if (test__start_subtest("sys_enter_exit"))
@@ -89,4 +125,6 @@ void test_task_local_storage(void)
 		test_exit_creds();
 	if (test__start_subtest("recursion"))
 		test_recursion();
+	if (test__start_subtest("mmapable"))
+		test_mmapable();
 }
tools/testing/selftests/bpf/progs/task_local_storage_mmapable.c new file +38 -0
new file mode 100644
index 000000000000..8cd82bb7336a
@@ -0,0 +1,38 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Copyright (c) 2022 Google */
+
+#include "vmlinux.h"
+#include <bpf/bpf_helpers.h>
+#include <bpf/bpf_tracing.h>
+
+char _license[] SEC("license") = "GPL";
+
+struct {
+	__uint(type, BPF_MAP_TYPE_TASK_STORAGE);
+	__uint(map_flags, BPF_F_NO_PREALLOC | BPF_F_MMAPABLE);
+	__type(key, int);
+	__type(value, long);
+} mmapable_map SEC(".maps");
+
+#define MAGIC_VALUE 0xabcd1234
+
+pid_t target_pid = 0;
+
+SEC("tp_btf/sys_enter")
+int BPF_PROG(on_enter, struct pt_regs *regs, long id)
+{
+	struct task_struct *task;
+	long *ptr;
+
+	task = bpf_get_current_task_btf();
+	if (task->pid != target_pid)
+		return 0;
+
+	ptr = bpf_task_storage_get(&mmapable_map, task, 0,
+				   BPF_LOCAL_STORAGE_GET_F_CREATE);
+	if (!ptr)
+		return 0;
+
+	*ptr = MAGIC_VALUE;
+	return 0;
+}
Signature · 1 line
2.35.1.1021.g381101b075-goog
#3 lore raw
In reply to Hao Luo
Quoted 9 lines · Hao Luo

Some map types support mmap operation, which allows userspace to communicate with BPF programs directly. Currently only arraymap and ringbuf have mmap implemented.

However, in some use cases, when multiple program instances can run concurrently, global mmapable memory can cause race. In that case, userspace needs to provide necessary synchronizations to coordinate the usage of mapped global data. This can be a source of bottleneck.

I can see your use case here. Each calling process can get the corresponding bpf program task local storage data through mmap interface. As you mentioned, there is a tradeoff between more memory vs. non-global synchronization.

I am thinking that another bpf_iter approach can retrieve the similar result. We could implement a bpf_iter for task local storage map, optionally it can provide a tid to retrieve the data for that particular tid. This way, user space needs an explicit syscall, but does not need to allocate more memory than necessary.

WDYT?

Quoted 53 lines

It would be great to have a mmapable local storage in that case. This patch adds that.

Mmap isn't BPF syscall, so unpriv users can also use it to interact with maps.

Currently the only way of allocating mmapable map area is using vmalloc() and it's only used at map allocation time. Vmalloc() may sleep, therefore it's not suitable for maps that may allocate memory in an atomic context such as local storage. Local storage uses kmalloc() with GFP_ATOMIC, which doesn't sleep. This patch uses kmalloc() with GFP_ATOMIC as well for mmapable map area.

Allocating mmapable memory has requirment on page alignment. So we have to deliberately allocate more memory than necessary to obtain an address that has sdata->data aligned at page boundary. The calculations for mmapable allocation size, and the actual allocation/deallocation are packaged in three functions:

- bpf_map_mmapable_alloc_size()
- bpf_map_mmapable_kzalloc()
- bpf_map_mmapable_kfree()

BPF local storage uses them to provide generic mmap API:

- bpf_local_storage_mmap()

And task local storage adds the mmap callback:

- task_storage_map_mmap()

When application calls mmap on a task local storage, it gets its own local storage.

Overall, mmapable local storage trades off memory with flexibility and efficiency. It brings memory fragmentation but can make programs stateless. Therefore useful in some cases.

Hao Luo (2):
bpf: Mmapable local storage. selftests/bpf: Test mmapable task local storage.

  include/linux/bpf.h                           |  4 +
  include/linux/bpf_local_storage.h             |  5 +-
  kernel/bpf/bpf_local_storage.c                | 73 +++++++++++++++++--
  kernel/bpf/bpf_task_storage.c                 | 40 ++++++++++
  kernel/bpf/syscall.c                          | 67 +++++++++++++++++
  .../bpf/prog_tests/task_local_storage.c       | 38 ++++++++++
  .../bpf/progs/task_local_storage_mmapable.c   | 38 ++++++++++
  7 files changed, 257 insertions(+), 8 deletions(-)

create mode 100644 tools/testing/selftests/bpf/progs/task_local_storage_mmapable.c

author
#4 lore raw

Hi Yonghong,

Quoted 17 lines · Yonghong Song
Quoted 9 lines · Hao Luo

Some map types support mmap operation, which allows userspace to communicate with BPF programs directly. Currently only arraymap and ringbuf have mmap implemented.

However, in some use cases, when multiple program instances can run concurrently, global mmapable memory can cause race. In that case, userspace needs to provide necessary synchronizations to coordinate the usage of mapped global data. This can be a source of bottleneck.

I can see your use case here. Each calling process can get the corresponding bpf program task local storage data through mmap interface. As you mentioned, there is a tradeoff between more memory vs. non-global synchronization.

Quoted 9 lines

I am thinking that another bpf_iter approach can retrieve the similar result. We could implement a bpf_iter for task local storage map, optionally it can provide a tid to retrieve the data for that particular tid. This way, user space needs an explicit syscall, but does not need to allocate more memory than necessary.

WDYT?

Thanks for the suggestion. I have two thoughts about bpf_iter + tid and mmap:

- mmap prevents the calling task from reading other task's value. Using bpf_iter, one can pass other task's tid to get their values. I assume there are two potential ways of passing tid to bpf_iter: one is to use global data in bpf prog, the other is adding tid parameterized iter_link. For the first, it's not easy for unpriv tasks to use. For the second, we need to create one iter_link object for each interested tid. It may not be easy to use either.

- Regarding adding an explicit syscall. I thought about adding write/read syscalls for task local storage maps, just like reading values from iter_link. Writing or reading task local storage map updates/reads the current task's value. I think this could achieve the same effect as mmap.

Hao

Quoted 53 lines
Quoted 53 lines

It would be great to have a mmapable local storage in that case. This patch adds that.

Mmap isn't BPF syscall, so unpriv users can also use it to interact with maps.

Currently the only way of allocating mmapable map area is using vmalloc() and it's only used at map allocation time. Vmalloc() may sleep, therefore it's not suitable for maps that may allocate memory in an atomic context such as local storage. Local storage uses kmalloc() with GFP_ATOMIC, which doesn't sleep. This patch uses kmalloc() with GFP_ATOMIC as well for mmapable map area.

Allocating mmapable memory has requirment on page alignment. So we have to deliberately allocate more memory than necessary to obtain an address that has sdata->data aligned at page boundary. The calculations for mmapable allocation size, and the actual allocation/deallocation are packaged in three functions:

- bpf_map_mmapable_alloc_size()
- bpf_map_mmapable_kzalloc()
- bpf_map_mmapable_kfree()

BPF local storage uses them to provide generic mmap API:

- bpf_local_storage_mmap()

And task local storage adds the mmap callback:

- task_storage_map_mmap()

When application calls mmap on a task local storage, it gets its own local storage.

Overall, mmapable local storage trades off memory with flexibility and efficiency. It brings memory fragmentation but can make programs stateless. Therefore useful in some cases.

Hao Luo (2):
bpf: Mmapable local storage. selftests/bpf: Test mmapable task local storage.

  include/linux/bpf.h                           |  4 +
  include/linux/bpf_local_storage.h             |  5 +-
  kernel/bpf/bpf_local_storage.c                | 73 +++++++++++++++++--
  kernel/bpf/bpf_task_storage.c                 | 40 ++++++++++
  kernel/bpf/syscall.c                          | 67 +++++++++++++++++
  .../bpf/prog_tests/task_local_storage.c       | 38 ++++++++++
  .../bpf/progs/task_local_storage_mmapable.c   | 38 ++++++++++
  7 files changed, 257 insertions(+), 8 deletions(-)

create mode 100644 tools/testing/selftests/bpf/progs/task_local_storage_mmapable.c

author
#5 lore raw
Quoted 41 lines · Hao Luo

Hi Yonghong,

Quoted 17 lines · Yonghong Song
Quoted 9 lines · Hao Luo

Some map types support mmap operation, which allows userspace to communicate with BPF programs directly. Currently only arraymap and ringbuf have mmap implemented.

However, in some use cases, when multiple program instances can run concurrently, global mmapable memory can cause race. In that case, userspace needs to provide necessary synchronizations to coordinate the usage of mapped global data. This can be a source of bottleneck.

I can see your use case here. Each calling process can get the corresponding bpf program task local storage data through mmap interface. As you mentioned, there is a tradeoff between more memory vs. non-global synchronization.

Quoted 9 lines

I am thinking that another bpf_iter approach can retrieve the similar result. We could implement a bpf_iter for task local storage map, optionally it can provide a tid to retrieve the data for that particular tid. This way, user space needs an explicit syscall, but does not need to allocate more memory than necessary.

WDYT?

Thanks for the suggestion. I have two thoughts about bpf_iter + tid and mmap:

- mmap prevents the calling task from reading other task's value. Using bpf_iter, one can pass other task's tid to get their values. I assume there are two potential ways of passing tid to bpf_iter: one is to use global data in bpf prog, the other is adding tid parameterized iter_link. For the first, it's not easy for unpriv tasks to use. For the second, we need to create one iter_link object for each interested tid. It may not be easy to use either.

Quoted 6 lines

- Regarding adding an explicit syscall. I thought about adding write/read syscalls for task local storage maps, just like reading values from iter_link. Writing or reading task local storage map updates/reads the current task's value. I think this could achieve the same effect as mmap.

Actually, my use case of using mmap on task local storage is to allow userspace to pass FDs into bpf prog. Some of the helpers I want to add need to take an FD as parameter and the bpf progs can run concurrently, thus using global data is racy. Mmapable task local storage is the best solution I can find for this purpose.

Song also mentioned to me offline, that mmapable task local storage may be useful for his use case.

I am actually open to other proposals.

Quoted 53 lines
Quoted 53 lines
Quoted 53 lines

It would be great to have a mmapable local storage in that case. This patch adds that.

Mmap isn't BPF syscall, so unpriv users can also use it to interact with maps.

Currently the only way of allocating mmapable map area is using vmalloc() and it's only used at map allocation time. Vmalloc() may sleep, therefore it's not suitable for maps that may allocate memory in an atomic context such as local storage. Local storage uses kmalloc() with GFP_ATOMIC, which doesn't sleep. This patch uses kmalloc() with GFP_ATOMIC as well for mmapable map area.

Allocating mmapable memory has requirment on page alignment. So we have to deliberately allocate more memory than necessary to obtain an address that has sdata->data aligned at page boundary. The calculations for mmapable allocation size, and the actual allocation/deallocation are packaged in three functions:

- bpf_map_mmapable_alloc_size()
- bpf_map_mmapable_kzalloc()
- bpf_map_mmapable_kfree()

BPF local storage uses them to provide generic mmap API:

- bpf_local_storage_mmap()

And task local storage adds the mmap callback:

- task_storage_map_mmap()

When application calls mmap on a task local storage, it gets its own local storage.

Overall, mmapable local storage trades off memory with flexibility and efficiency. It brings memory fragmentation but can make programs stateless. Therefore useful in some cases.

Hao Luo (2):
bpf: Mmapable local storage. selftests/bpf: Test mmapable task local storage.

  include/linux/bpf.h                           |  4 +
  include/linux/bpf_local_storage.h             |  5 +-
  kernel/bpf/bpf_local_storage.c                | 73 +++++++++++++++++--
  kernel/bpf/bpf_task_storage.c                 | 40 ++++++++++
  kernel/bpf/syscall.c                          | 67 +++++++++++++++++
  .../bpf/prog_tests/task_local_storage.c       | 38 ++++++++++
  .../bpf/progs/task_local_storage_mmapable.c   | 38 ++++++++++
  7 files changed, 257 insertions(+), 8 deletions(-)

create mode 100644 tools/testing/selftests/bpf/progs/task_local_storage_mmapable.c

#6 lore raw
Quoted 55 lines · Hao Luo
Quoted 41 lines · Hao Luo

Hi Yonghong,

Quoted 17 lines · Yonghong Song
Quoted 9 lines · Hao Luo

Some map types support mmap operation, which allows userspace to communicate with BPF programs directly. Currently only arraymap and ringbuf have mmap implemented.

However, in some use cases, when multiple program instances can run concurrently, global mmapable memory can cause race. In that case, userspace needs to provide necessary synchronizations to coordinate the usage of mapped global data. This can be a source of bottleneck.

I can see your use case here. Each calling process can get the corresponding bpf program task local storage data through mmap interface. As you mentioned, there is a tradeoff between more memory vs. non-global synchronization.

Quoted 9 lines

I am thinking that another bpf_iter approach can retrieve the similar result. We could implement a bpf_iter for task local storage map, optionally it can provide a tid to retrieve the data for that particular tid. This way, user space needs an explicit syscall, but does not need to allocate more memory than necessary.

WDYT?

Thanks for the suggestion. I have two thoughts about bpf_iter + tid and mmap:

- mmap prevents the calling task from reading other task's value. Using bpf_iter, one can pass other task's tid to get their values. I assume there are two potential ways of passing tid to bpf_iter: one is to use global data in bpf prog, the other is adding tid parameterized iter_link. For the first, it's not easy for unpriv tasks to use. For the second, we need to create one iter_link object for each interested tid. It may not be easy to use either.

Quoted 6 lines

- Regarding adding an explicit syscall. I thought about adding write/read syscalls for task local storage maps, just like reading values from iter_link. Writing or reading task local storage map updates/reads the current task's value. I think this could achieve the same effect as mmap.

Actually, my use case of using mmap on task local storage is to allow userspace to pass FDs into bpf prog. Some of the helpers I want to add need to take an FD as parameter and the bpf progs can run concurrently, thus using global data is racy. Mmapable task local storage is the best solution I can find for this purpose.

Quoted 5 lines

Song also mentioned to me offline, that mmapable task local storage may be useful for his use case.

I am actually open to other proposals.

You could also use a syscall prog, and use bpf_prog_test_run to update local storage for current. Data can be passed for that specific prog invocation using ctx. You might have to enable bpf_task_storage helpers in it though, since they are not allowed to be called right now.

Quoted 43 lines
Quoted 43 lines
Quoted 43 lines
Quoted 43 lines

It would be great to have a mmapable local storage in that case. This patch adds that.

Mmap isn't BPF syscall, so unpriv users can also use it to interact with maps.

Currently the only way of allocating mmapable map area is using vmalloc() and it's only used at map allocation time. Vmalloc() may sleep, therefore it's not suitable for maps that may allocate memory in an atomic context such as local storage. Local storage uses kmalloc() with GFP_ATOMIC, which doesn't sleep. This patch uses kmalloc() with GFP_ATOMIC as well for mmapable map area.

Allocating mmapable memory has requirment on page alignment. So we have to deliberately allocate more memory than necessary to obtain an address that has sdata->data aligned at page boundary. The calculations for mmapable allocation size, and the actual allocation/deallocation are packaged in three functions:

- bpf_map_mmapable_alloc_size()
- bpf_map_mmapable_kzalloc()
- bpf_map_mmapable_kfree()

BPF local storage uses them to provide generic mmap API:

- bpf_local_storage_mmap()

And task local storage adds the mmap callback:

- task_storage_map_mmap()

When application calls mmap on a task local storage, it gets its own local storage.

Overall, mmapable local storage trades off memory with flexibility and efficiency. It brings memory fragmentation but can make programs stateless. Therefore useful in some cases.

Hao Luo (2):
bpf: Mmapable local storage. selftests/bpf: Test mmapable task local storage.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
  include/linux/bpf.h                           |  4 +
  include/linux/bpf_local_storage.h             |  5 +-
  kernel/bpf/bpf_local_storage.c                | 73 +++++++++++++++++--
  kernel/bpf/bpf_task_storage.c                 | 40 ++++++++++
  kernel/bpf/syscall.c                          | 67 +++++++++++++++++
  .../bpf/prog_tests/task_local_storage.c       | 38 ++++++++++
  .../bpf/progs/task_local_storage_mmapable.c   | 38 ++++++++++
  7 files changed, 257 insertions(+), 8 deletions(-)

create mode 100644 tools/testing/selftests/bpf/progs/task_local_storage_mmapable.c

--
Kartikeya

author
#7 lore raw
Quoted 60 lines · Kumar Kartikeya Dwivedi
Quoted 58 lines · Hao Luo
Quoted 41 lines · Hao Luo

Hi Yonghong,

Quoted 17 lines · Yonghong Song
Quoted 9 lines · Hao Luo

Some map types support mmap operation, which allows userspace to communicate with BPF programs directly. Currently only arraymap and ringbuf have mmap implemented.

However, in some use cases, when multiple program instances can run concurrently, global mmapable memory can cause race. In that case, userspace needs to provide necessary synchronizations to coordinate the usage of mapped global data. This can be a source of bottleneck.

I can see your use case here. Each calling process can get the corresponding bpf program task local storage data through mmap interface. As you mentioned, there is a tradeoff between more memory vs. non-global synchronization.

Quoted 9 lines

I am thinking that another bpf_iter approach can retrieve the similar result. We could implement a bpf_iter for task local storage map, optionally it can provide a tid to retrieve the data for that particular tid. This way, user space needs an explicit syscall, but does not need to allocate more memory than necessary.

WDYT?

Thanks for the suggestion. I have two thoughts about bpf_iter + tid and mmap:

- mmap prevents the calling task from reading other task's value. Using bpf_iter, one can pass other task's tid to get their values. I assume there are two potential ways of passing tid to bpf_iter: one is to use global data in bpf prog, the other is adding tid parameterized iter_link. For the first, it's not easy for unpriv tasks to use. For the second, we need to create one iter_link object for each interested tid. It may not be easy to use either.

Quoted 6 lines

- Regarding adding an explicit syscall. I thought about adding write/read syscalls for task local storage maps, just like reading values from iter_link. Writing or reading task local storage map updates/reads the current task's value. I think this could achieve the same effect as mmap.

Actually, my use case of using mmap on task local storage is to allow userspace to pass FDs into bpf prog. Some of the helpers I want to add need to take an FD as parameter and the bpf progs can run concurrently, thus using global data is racy. Mmapable task local storage is the best solution I can find for this purpose.

Song also mentioned to me offline, that mmapable task local storage may be useful for his use case.

Quoted 8 lines
Quoted 2 lines

I am actually open to other proposals.

You could also use a syscall prog, and use bpf_prog_test_run to update local storage for current. Data can be passed for that specific prog invocation using ctx. You might have to enable bpf_task_storage helpers in it though, since they are not allowed to be called right now.

The loading process needs CAP_BPF to load bpf_prog_test_run. I'm thinking of allowing any thread including unpriv ones to be able to pass data to the prog and update their own storage.

Quoted 56 lines
Quoted 43 lines
Quoted 43 lines
Quoted 43 lines
Quoted 43 lines

It would be great to have a mmapable local storage in that case. This patch adds that.

Mmap isn't BPF syscall, so unpriv users can also use it to interact with maps.

Currently the only way of allocating mmapable map area is using vmalloc() and it's only used at map allocation time. Vmalloc() may sleep, therefore it's not suitable for maps that may allocate memory in an atomic context such as local storage. Local storage uses kmalloc() with GFP_ATOMIC, which doesn't sleep. This patch uses kmalloc() with GFP_ATOMIC as well for mmapable map area.

Allocating mmapable memory has requirment on page alignment. So we have to deliberately allocate more memory than necessary to obtain an address that has sdata->data aligned at page boundary. The calculations for mmapable allocation size, and the actual allocation/deallocation are packaged in three functions:

- bpf_map_mmapable_alloc_size()
- bpf_map_mmapable_kzalloc()
- bpf_map_mmapable_kfree()

BPF local storage uses them to provide generic mmap API:

- bpf_local_storage_mmap()

And task local storage adds the mmap callback:

- task_storage_map_mmap()

When application calls mmap on a task local storage, it gets its own local storage.

Overall, mmapable local storage trades off memory with flexibility and efficiency. It brings memory fragmentation but can make programs stateless. Therefore useful in some cases.

Hao Luo (2):
bpf: Mmapable local storage. selftests/bpf: Test mmapable task local storage.

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
  include/linux/bpf.h                           |  4 +
  include/linux/bpf_local_storage.h             |  5 +-
  kernel/bpf/bpf_local_storage.c                | 73 +++++++++++++++++--
  kernel/bpf/bpf_task_storage.c                 | 40 ++++++++++
  kernel/bpf/syscall.c                          | 67 +++++++++++++++++
  .../bpf/prog_tests/task_local_storage.c       | 38 ++++++++++
  .../bpf/progs/task_local_storage_mmapable.c   | 38 ++++++++++
  7 files changed, 257 insertions(+), 8 deletions(-)

create mode 100644 tools/testing/selftests/bpf/progs/task_local_storage_mmapable.c

--
Kartikeya

#8 lore raw
Quoted 53 lines · Hao Luo
Quoted 51 lines · Kumar Kartikeya Dwivedi
Quoted 49 lines · Hao Luo
Quoted 47 lines · Hao Luo

Hi Yonghong,

Quoted 17 lines · Yonghong Song
Quoted 9 lines · Hao Luo

Some map types support mmap operation, which allows userspace to communicate with BPF programs directly. Currently only arraymap and ringbuf have mmap implemented.

However, in some use cases, when multiple program instances can run concurrently, global mmapable memory can cause race. In that case, userspace needs to provide necessary synchronizations to coordinate the usage of mapped global data. This can be a source of bottleneck.

I can see your use case here. Each calling process can get the corresponding bpf program task local storage data through mmap interface. As you mentioned, there is a tradeoff between more memory vs. non-global synchronization.

Quoted 9 lines

I am thinking that another bpf_iter approach can retrieve the similar result. We could implement a bpf_iter for task local storage map, optionally it can provide a tid to retrieve the data for that particular tid. This way, user space needs an explicit syscall, but does not need to allocate more memory than necessary.

WDYT?

Thanks for the suggestion. I have two thoughts about bpf_iter + tid and mmap:

- mmap prevents the calling task from reading other task's value. Using bpf_iter, one can pass other task's tid to get their values. I assume there are two potential ways of passing tid to bpf_iter: one is to use global data in bpf prog, the other is adding tid parameterized iter_link. For the first, it's not easy for unpriv tasks to use. For the second, we need to create one iter_link object for each interested tid. It may not be easy to use either.

- Regarding adding an explicit syscall. I thought about adding write/read syscalls for task local storage maps, just like reading values from iter_link. Writing or reading task local storage map updates/reads the current task's value. I think this could achieve the same effect as mmap.

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Actually, my use case of using mmap on task local storage is to allow userspace to pass FDs into bpf prog. Some of the helpers I want to add need to take an FD as parameter and the bpf progs can run concurrently, thus using global data is racy. Mmapable task local storage is the best solution I can find for this purpose.

Some more details is needed about the use case. As long as there is storage local to an individual task, racing within this one task's specific storage is a non issue?

The patch 2 example is doable with the current api and is pretty far from the above use case description. The existing bpf_map_update_elem() and bpf_map_lookup_elem() can not solve your use case?

or the current bpf_map_{update,lookup}_elem() works but prefer a direct data read/write interface?

btw, how delete is going to look like ?

and do you see the mmap could be used with sk and inode storage instead of the 'current' task?

Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Song also mentioned to me offline, that mmapable task local storage may be useful for his use case.

I am actually open to other proposals.

If the arraymap is local to an individual task, does it solve
your use case? Have you thought about storing an arraymap (which is mmap-able) in the local storage? That could then be used to store ringbuf map and other bpf maps in local storage. It is logically similar to map-in-map. The outer map is the pseudo local storage map here.

Quoted 11 lines
Quoted 7 lines
Quoted 1 line

You could also use a syscall prog, and use bpf_prog_test_run to update local storage for current. Data can be passed for that specific prog invocation using ctx. You might have to enable bpf_task_storage helpers in it though, since they are not allowed to be called right now.

The loading process needs CAP_BPF to load bpf_prog_test_run. I'm thinking of allowing any thread including unpriv ones to be able to pass data to the prog and update their own storage.

author
#9 lore raw
Quoted 55 lines · Martin KaFai Lau
Quoted 53 lines · Hao Luo
Quoted 51 lines · Kumar Kartikeya Dwivedi
Quoted 49 lines · Hao Luo
Quoted 47 lines · Hao Luo

Hi Yonghong,

Quoted 17 lines · Yonghong Song
Quoted 9 lines · Hao Luo

Some map types support mmap operation, which allows userspace to communicate with BPF programs directly. Currently only arraymap and ringbuf have mmap implemented.

However, in some use cases, when multiple program instances can run concurrently, global mmapable memory can cause race. In that case, userspace needs to provide necessary synchronizations to coordinate the usage of mapped global data. This can be a source of bottleneck.

I can see your use case here. Each calling process can get the corresponding bpf program task local storage data through mmap interface. As you mentioned, there is a tradeoff between more memory vs. non-global synchronization.

Quoted 9 lines

I am thinking that another bpf_iter approach can retrieve the similar result. We could implement a bpf_iter for task local storage map, optionally it can provide a tid to retrieve the data for that particular tid. This way, user space needs an explicit syscall, but does not need to allocate more memory than necessary.

WDYT?

Thanks for the suggestion. I have two thoughts about bpf_iter + tid and mmap:

- mmap prevents the calling task from reading other task's value. Using bpf_iter, one can pass other task's tid to get their values. I assume there are two potential ways of passing tid to bpf_iter: one is to use global data in bpf prog, the other is adding tid parameterized iter_link. For the first, it's not easy for unpriv tasks to use. For the second, we need to create one iter_link object for each interested tid. It may not be easy to use either.

- Regarding adding an explicit syscall. I thought about adding write/read syscalls for task local storage maps, just like reading values from iter_link. Writing or reading task local storage map updates/reads the current task's value. I think this could achieve the same effect as mmap.

Quoted 9 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Actually, my use case of using mmap on task local storage is to allow userspace to pass FDs into bpf prog. Some of the helpers I want to add need to take an FD as parameter and the bpf progs can run concurrently, thus using global data is racy. Mmapable task local storage is the best solution I can find for this purpose.

Some more details is needed about the use case. As long as there is storage local to an individual task, racing within this one task's specific storage is a non issue?

Race is still possible. In the use case I was thinking, the workflow is:

1. Current task mmaps a local storage map, writes a value to its local storage.
2. Current task then makes a syscall, which triggers a bpf prog.
3. The bpf prog reads the current task's local storage and fetches the value stored by the task.

The steps above are sequential, therefore no race between task and bpf prog. If a task accesses other task's local storage, there is still race.

Quoted 4 lines

The patch 2 example is doable with the current api and is pretty far from the above use case description. The existing bpf_map_update_elem() and bpf_map_lookup_elem() can not solve your use case?

With sysctl_unprivileged_bpf_disabled, tasks without CAP_BPF are not able to make use of __sys_bpf(). bpf_map_update_elem() and bpf_map_lookup_elem() call __sys_bpf underneath IIRC. So unpriv tasks can't use these two syscalls.

Quoted 5 lines

or the current bpf_map_{update,lookup}_elem() works but prefer a direct data read/write interface?

btw, how delete is going to look like ?

Good question. Deletion is not done from the userspace. It's done at bpf prog side. The task mmaps its storage (which creates the storage), writes a value. The bpf prog reads the value and deletes the storage.

Quoted 3 lines

and do you see the mmap could be used with sk and inode storage instead of the 'current' task?

Yes. I think this patch can certainly extend to sk or inode or cgroup local storage. But I don't have a use case yet.

Quoted 11 lines
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines

Song also mentioned to me offline, that mmapable task local storage may be useful for his use case.

I am actually open to other proposals.

If the arraymap is local to an individual task, does it solve
your use case? Have you thought about storing an arraymap (which is mmap-able) in the local storage? That could then be used to store ringbuf map and other bpf maps in local storage. It is logically similar to map-in-map. The outer map is the pseudo local storage map here.

I haven't thought about this. IMHO, I feel it might be complicated to properly set up the set of maps.

Quoted 11 lines
Quoted 11 lines
Quoted 7 lines
Quoted 1 line

You could also use a syscall prog, and use bpf_prog_test_run to update local storage for current. Data can be passed for that specific prog invocation using ctx. You might have to enable bpf_task_storage helpers in it though, since they are not allowed to be called right now.

The loading process needs CAP_BPF to load bpf_prog_test_run. I'm thinking of allowing any thread including unpriv ones to be able to pass data to the prog and update their own storage.

#10 lore raw
In reply to Hao Luo
Quoted 71 lines · Hao Luo
Quoted 60 lines · Kumar Kartikeya Dwivedi
Quoted 58 lines · Hao Luo
Quoted 41 lines · Hao Luo

Hi Yonghong,

Quoted 17 lines · Yonghong Song
Quoted 9 lines · Hao Luo

Some map types support mmap operation, which allows userspace to communicate with BPF programs directly. Currently only arraymap and ringbuf have mmap implemented.

However, in some use cases, when multiple program instances can run concurrently, global mmapable memory can cause race. In that case, userspace needs to provide necessary synchronizations to coordinate the usage of mapped global data. This can be a source of bottleneck.

I can see your use case here. Each calling process can get the corresponding bpf program task local storage data through mmap interface. As you mentioned, there is a tradeoff between more memory vs. non-global synchronization.

Quoted 9 lines

I am thinking that another bpf_iter approach can retrieve the similar result. We could implement a bpf_iter for task local storage map, optionally it can provide a tid to retrieve the data for that particular tid. This way, user space needs an explicit syscall, but does not need to allocate more memory than necessary.

WDYT?

Thanks for the suggestion. I have two thoughts about bpf_iter + tid and mmap:

- mmap prevents the calling task from reading other task's value. Using bpf_iter, one can pass other task's tid to get their values. I assume there are two potential ways of passing tid to bpf_iter: one is to use global data in bpf prog, the other is adding tid parameterized iter_link. For the first, it's not easy for unpriv tasks to use. For the second, we need to create one iter_link object for each interested tid. It may not be easy to use either.

Quoted 6 lines

- Regarding adding an explicit syscall. I thought about adding write/read syscalls for task local storage maps, just like reading values from iter_link. Writing or reading task local storage map updates/reads the current task's value. I think this could achieve the same effect as mmap.

Actually, my use case of using mmap on task local storage is to allow userspace to pass FDs into bpf prog. Some of the helpers I want to add need to take an FD as parameter and the bpf progs can run concurrently, thus using global data is racy. Mmapable task local storage is the best solution I can find for this purpose.

Song also mentioned to me offline, that mmapable task local storage may be useful for his use case.

Quoted 8 lines
Quoted 2 lines

I am actually open to other proposals.

You could also use a syscall prog, and use bpf_prog_test_run to update local storage for current. Data can be passed for that specific prog invocation using ctx. You might have to enable bpf_task_storage helpers in it though, since they are not allowed to be called right now.

Quoted 3 lines

The loading process needs CAP_BPF to load bpf_prog_test_run. I'm thinking of allowing any thread including unpriv ones to be able to pass data to the prog and update their own storage.

If I understand the use case correctly all of this mmap-ing is only to allow unpriv userspace to access a priv map via unpriv mmap() syscall. But the map can be accessed as unpriv already.
Pin it with the world read creds and do map_lookup sys_bpf cmd on it.

author
#11 lore raw
Quoted 73 lines · Alexei Starovoitov
Quoted 71 lines · Hao Luo
Quoted 68 lines · Kumar Kartikeya Dwivedi
Quoted 55 lines · Hao Luo
Quoted 41 lines · Hao Luo

Hi Yonghong,

Quoted 17 lines · Yonghong Song
Quoted 9 lines · Hao Luo

Some map types support mmap operation, which allows userspace to communicate with BPF programs directly. Currently only arraymap and ringbuf have mmap implemented.

However, in some use cases, when multiple program instances can run concurrently, global mmapable memory can cause race. In that case, userspace needs to provide necessary synchronizations to coordinate the usage of mapped global data. This can be a source of bottleneck.

I can see your use case here. Each calling process can get the corresponding bpf program task local storage data through mmap interface. As you mentioned, there is a tradeoff between more memory vs. non-global synchronization.

Quoted 9 lines

I am thinking that another bpf_iter approach can retrieve the similar result. We could implement a bpf_iter for task local storage map, optionally it can provide a tid to retrieve the data for that particular tid. This way, user space needs an explicit syscall, but does not need to allocate more memory than necessary.

WDYT?

Thanks for the suggestion. I have two thoughts about bpf_iter + tid and mmap:

- mmap prevents the calling task from reading other task's value. Using bpf_iter, one can pass other task's tid to get their values. I assume there are two potential ways of passing tid to bpf_iter: one is to use global data in bpf prog, the other is adding tid parameterized iter_link. For the first, it's not easy for unpriv tasks to use. For the second, we need to create one iter_link object for each interested tid. It may not be easy to use either.

Quoted 6 lines

- Regarding adding an explicit syscall. I thought about adding write/read syscalls for task local storage maps, just like reading values from iter_link. Writing or reading task local storage map updates/reads the current task's value. I think this could achieve the same effect as mmap.

Actually, my use case of using mmap on task local storage is to allow userspace to pass FDs into bpf prog. Some of the helpers I want to add need to take an FD as parameter and the bpf progs can run concurrently, thus using global data is racy. Mmapable task local storage is the best solution I can find for this purpose.

Quoted 5 lines

Song also mentioned to me offline, that mmapable task local storage may be useful for his use case.

I am actually open to other proposals.

You could also use a syscall prog, and use bpf_prog_test_run to update local storage for current. Data can be passed for that specific prog invocation using ctx. You might have to enable bpf_task_storage helpers in it though, since they are not allowed to be called right now.

Quoted 8 lines
Quoted 3 lines

The loading process needs CAP_BPF to load bpf_prog_test_run. I'm thinking of allowing any thread including unpriv ones to be able to pass data to the prog and update their own storage.

If I understand the use case correctly all of this mmap-ing is only to allow unpriv userspace to access a priv map via unpriv mmap() syscall. But the map can be accessed as unpriv already.
Pin it with the world read creds and do map_lookup sys_bpf cmd on it.

Right, but, if I understand correctly, with sysctl_unprivileged_bpf_disabled, unpriv tasks are not able to make use of __sys_bpf(). Is there anything I missed?

#12 lore raw
Quoted 80 lines · Hao Luo
Quoted 77 lines · Alexei Starovoitov
Quoted 74 lines · Hao Luo
Quoted 60 lines · Kumar Kartikeya Dwivedi
Quoted 58 lines · Hao Luo
Quoted 41 lines · Hao Luo

Hi Yonghong,

Quoted 17 lines · Yonghong Song
Quoted 9 lines · Hao Luo

Some map types support mmap operation, which allows userspace to communicate with BPF programs directly. Currently only arraymap and ringbuf have mmap implemented.

However, in some use cases, when multiple program instances can run concurrently, global mmapable memory can cause race. In that case, userspace needs to provide necessary synchronizations to coordinate the usage of mapped global data. This can be a source of bottleneck.

I can see your use case here. Each calling process can get the corresponding bpf program task local storage data through mmap interface. As you mentioned, there is a tradeoff between more memory vs. non-global synchronization.

Quoted 9 lines

I am thinking that another bpf_iter approach can retrieve the similar result. We could implement a bpf_iter for task local storage map, optionally it can provide a tid to retrieve the data for that particular tid. This way, user space needs an explicit syscall, but does not need to allocate more memory than necessary.

WDYT?

Thanks for the suggestion. I have two thoughts about bpf_iter + tid and mmap:

- mmap prevents the calling task from reading other task's value. Using bpf_iter, one can pass other task's tid to get their values. I assume there are two potential ways of passing tid to bpf_iter: one is to use global data in bpf prog, the other is adding tid parameterized iter_link. For the first, it's not easy for unpriv tasks to use. For the second, we need to create one iter_link object for each interested tid. It may not be easy to use either.

Quoted 6 lines

- Regarding adding an explicit syscall. I thought about adding write/read syscalls for task local storage maps, just like reading values from iter_link. Writing or reading task local storage map updates/reads the current task's value. I think this could achieve the same effect as mmap.

Actually, my use case of using mmap on task local storage is to allow userspace to pass FDs into bpf prog. Some of the helpers I want to add need to take an FD as parameter and the bpf progs can run concurrently, thus using global data is racy. Mmapable task local storage is the best solution I can find for this purpose.

Song also mentioned to me offline, that mmapable task local storage may be useful for his use case.

Quoted 8 lines
Quoted 2 lines

I am actually open to other proposals.

You could also use a syscall prog, and use bpf_prog_test_run to update local storage for current. Data can be passed for that specific prog invocation using ctx. You might have to enable bpf_task_storage helpers in it though, since they are not allowed to be called right now.

The loading process needs CAP_BPF to load bpf_prog_test_run. I'm thinking of allowing any thread including unpriv ones to be able to pass data to the prog and update their own storage.

Quoted 8 lines
Quoted 4 lines

If I understand the use case correctly all of this mmap-ing is only to allow unpriv userspace to access a priv map via unpriv mmap() syscall. But the map can be accessed as unpriv already.
Pin it with the world read creds and do map_lookup sys_bpf cmd on it.

Right, but, if I understand correctly, with sysctl_unprivileged_bpf_disabled, unpriv tasks are not able to make use of __sys_bpf(). Is there anything I missed?

That sysctl is a heavy hammer. Let's fix it instead.
map lookup/update/delete can be allowed for unpriv for certain map types. There are permissions checks in corresponding lookup/update calls already.

author
#13 lore raw
Quoted 87 lines · Alexei Starovoitov
Quoted 85 lines · Hao Luo
Quoted 81 lines · Alexei Starovoitov
Quoted 71 lines · Hao Luo
Quoted 60 lines · Kumar Kartikeya Dwivedi
Quoted 58 lines · Hao Luo
Quoted 41 lines · Hao Luo

Hi Yonghong,

Quoted 17 lines · Yonghong Song
Quoted 9 lines · Hao Luo

Some map types support mmap operation, which allows userspace to communicate with BPF programs directly. Currently only arraymap and ringbuf have mmap implemented.

However, in some use cases, when multiple program instances can run concurrently, global mmapable memory can cause race. In that case, userspace needs to provide necessary synchronizations to coordinate the usage of mapped global data. This can be a source of bottleneck.

I can see your use case here. Each calling process can get the corresponding bpf program task local storage data through mmap interface. As you mentioned, there is a tradeoff between more memory vs. non-global synchronization.

Quoted 9 lines

I am thinking that another bpf_iter approach can retrieve the similar result. We could implement a bpf_iter for task local storage map, optionally it can provide a tid to retrieve the data for that particular tid. This way, user space needs an explicit syscall, but does not need to allocate more memory than necessary.

WDYT?

Thanks for the suggestion. I have two thoughts about bpf_iter + tid and mmap:

- mmap prevents the calling task from reading other task's value. Using bpf_iter, one can pass other task's tid to get their values. I assume there are two potential ways of passing tid to bpf_iter: one is to use global data in bpf prog, the other is adding tid parameterized iter_link. For the first, it's not easy for unpriv tasks to use. For the second, we need to create one iter_link object for each interested tid. It may not be easy to use either.

Quoted 6 lines

- Regarding adding an explicit syscall. I thought about adding write/read syscalls for task local storage maps, just like reading values from iter_link. Writing or reading task local storage map updates/reads the current task's value. I think this could achieve the same effect as mmap.

Actually, my use case of using mmap on task local storage is to allow userspace to pass FDs into bpf prog. Some of the helpers I want to add need to take an FD as parameter and the bpf progs can run concurrently, thus using global data is racy. Mmapable task local storage is the best solution I can find for this purpose.

Song also mentioned to me offline, that mmapable task local storage may be useful for his use case.

Quoted 8 lines
Quoted 2 lines

I am actually open to other proposals.

You could also use a syscall prog, and use bpf_prog_test_run to update local storage for current. Data can be passed for that specific prog invocation using ctx. You might have to enable bpf_task_storage helpers in it though, since they are not allowed to be called right now.

Quoted 3 lines

The loading process needs CAP_BPF to load bpf_prog_test_run. I'm thinking of allowing any thread including unpriv ones to be able to pass data to the prog and update their own storage.

If I understand the use case correctly all of this mmap-ing is only to allow unpriv userspace to access a priv map via unpriv mmap() syscall. But the map can be accessed as unpriv already.
Pin it with the world read creds and do map_lookup sys_bpf cmd on it.

Quoted 7 lines
Quoted 3 lines

Right, but, if I understand correctly, with sysctl_unprivileged_bpf_disabled, unpriv tasks are not able to make use of __sys_bpf(). Is there anything I missed?

That sysctl is a heavy hammer. Let's fix it instead.
map lookup/update/delete can be allowed for unpriv for certain map types. There are permissions checks in corresponding lookup/update calls already.

This sounds great. If we can allow basic map operations for some map types, it will change many use cases I'm looking at. Let me take a look and report back.

#14 lore raw
Quoted 90 lines · Hao Luo
Quoted 87 lines · Alexei Starovoitov
Quoted 85 lines · Hao Luo
Quoted 81 lines · Alexei Starovoitov
Quoted 71 lines · Hao Luo
Quoted 60 lines · Kumar Kartikeya Dwivedi
Quoted 58 lines · Hao Luo
Quoted 41 lines · Hao Luo

Hi Yonghong,

Quoted 17 lines · Yonghong Song
Quoted 9 lines · Hao Luo

Some map types support mmap operation, which allows userspace to communicate with BPF programs directly. Currently only arraymap and ringbuf have mmap implemented.

However, in some use cases, when multiple program instances can run concurrently, global mmapable memory can cause race. In that case, userspace needs to provide necessary synchronizations to coordinate the usage of mapped global data. This can be a source of bottleneck.

I can see your use case here. Each calling process can get the corresponding bpf program task local storage data through mmap interface. As you mentioned, there is a tradeoff between more memory vs. non-global synchronization.

Quoted 9 lines

I am thinking that another bpf_iter approach can retrieve the similar result. We could implement a bpf_iter for task local storage map, optionally it can provide a tid to retrieve the data for that particular tid. This way, user space needs an explicit syscall, but does not need to allocate more memory than necessary.

WDYT?

Thanks for the suggestion. I have two thoughts about bpf_iter + tid and mmap:

- mmap prevents the calling task from reading other task's value. Using bpf_iter, one can pass other task's tid to get their values. I assume there are two potential ways of passing tid to bpf_iter: one is to use global data in bpf prog, the other is adding tid parameterized iter_link. For the first, it's not easy for unpriv tasks to use. For the second, we need to create one iter_link object for each interested tid. It may not be easy to use either.

Quoted 6 lines

- Regarding adding an explicit syscall. I thought about adding write/read syscalls for task local storage maps, just like reading values from iter_link. Writing or reading task local storage map updates/reads the current task's value. I think this could achieve the same effect as mmap.

Actually, my use case of using mmap on task local storage is to allow userspace to pass FDs into bpf prog. Some of the helpers I want to add need to take an FD as parameter and the bpf progs can run concurrently, thus using global data is racy. Mmapable task local storage is the best solution I can find for this purpose.

Song also mentioned to me offline, that mmapable task local storage may be useful for his use case.

Quoted 8 lines
Quoted 2 lines

I am actually open to other proposals.

You could also use a syscall prog, and use bpf_prog_test_run to update local storage for current. Data can be passed for that specific prog invocation using ctx. You might have to enable bpf_task_storage helpers in it though, since they are not allowed to be called right now.

Quoted 3 lines

The loading process needs CAP_BPF to load bpf_prog_test_run. I'm thinking of allowing any thread including unpriv ones to be able to pass data to the prog and update their own storage.

If I understand the use case correctly all of this mmap-ing is only to allow unpriv userspace to access a priv map via unpriv mmap() syscall. But the map can be accessed as unpriv already.
Pin it with the world read creds and do map_lookup sys_bpf cmd on it.

Quoted 8 lines
Quoted 7 lines
Quoted 3 lines

Right, but, if I understand correctly, with sysctl_unprivileged_bpf_disabled, unpriv tasks are not able to make use of __sys_bpf(). Is there anything I missed?

That sysctl is a heavy hammer. Let's fix it instead.
map lookup/update/delete can be allowed for unpriv for certain map types. There are permissions checks in corresponding lookup/update calls already.

(Adding Jann)

I wonder if we can tag a map as BPF_F_UNPRIVILEGED and allow the writes to only maps that are explicitly marked as writable by unprivileged processes.

We will have task local storage in LSM programs that we won't like unprivileged processes to write to as well.

struct {
        __uint(type, BPF_MAP_TYPE_TASK_STORAGE);
        __uint(map_flags, BPF_F_NO_PREALLOC | BPF_F_UNPRIVILEGED);
        __type(key, int);
        __type(value, struct fd_storage);
} task_fd_storage_map SEC(".maps");

- KP

Quoted 3 lines

This sounds great. If we can allow basic map operations for some map types, it will change many use cases I'm looking at. Let me take a look and report back.

#15 lore raw
Quoted 101 lines · KP Singh
Quoted 90 lines · Hao Luo
Quoted 87 lines · Alexei Starovoitov
Quoted 85 lines · Hao Luo
Quoted 81 lines · Alexei Starovoitov
Quoted 71 lines · Hao Luo
Quoted 60 lines · Kumar Kartikeya Dwivedi
Quoted 58 lines · Hao Luo
Quoted 41 lines · Hao Luo

Hi Yonghong,

Quoted 17 lines · Yonghong Song
Quoted 9 lines · Hao Luo

Some map types support mmap operation, which allows userspace to communicate with BPF programs directly. Currently only arraymap and ringbuf have mmap implemented.

However, in some use cases, when multiple program instances can run concurrently, global mmapable memory can cause race. In that case, userspace needs to provide necessary synchronizations to coordinate the usage of mapped global data. This can be a source of bottleneck.

I can see your use case here. Each calling process can get the corresponding bpf program task local storage data through mmap interface. As you mentioned, there is a tradeoff between more memory vs. non-global synchronization.

Quoted 9 lines

I am thinking that another bpf_iter approach can retrieve the similar result. We could implement a bpf_iter for task local storage map, optionally it can provide a tid to retrieve the data for that particular tid. This way, user space needs an explicit syscall, but does not need to allocate more memory than necessary.

WDYT?

Thanks for the suggestion. I have two thoughts about bpf_iter + tid and mmap:

- mmap prevents the calling task from reading other task's value. Using bpf_iter, one can pass other task's tid to get their values. I assume there are two potential ways of passing tid to bpf_iter: one is to use global data in bpf prog, the other is adding tid parameterized iter_link. For the first, it's not easy for unpriv tasks to use. For the second, we need to create one iter_link object for each interested tid. It may not be easy to use either.

Quoted 6 lines

- Regarding adding an explicit syscall. I thought about adding write/read syscalls for task local storage maps, just like reading values from iter_link. Writing or reading task local storage map updates/reads the current task's value. I think this could achieve the same effect as mmap.

Actually, my use case of using mmap on task local storage is to allow userspace to pass FDs into bpf prog. Some of the helpers I want to add need to take an FD as parameter and the bpf progs can run concurrently, thus using global data is racy. Mmapable task local storage is the best solution I can find for this purpose.

Song also mentioned to me offline, that mmapable task local storage may be useful for his use case.

Quoted 8 lines
Quoted 2 lines

I am actually open to other proposals.

You could also use a syscall prog, and use bpf_prog_test_run to update local storage for current. Data can be passed for that specific prog invocation using ctx. You might have to enable bpf_task_storage helpers in it though, since they are not allowed to be called right now.

Quoted 3 lines

The loading process needs CAP_BPF to load bpf_prog_test_run. I'm thinking of allowing any thread including unpriv ones to be able to pass data to the prog and update their own storage.

If I understand the use case correctly all of this mmap-ing is only to allow unpriv userspace to access a priv map via unpriv mmap() syscall. But the map can be accessed as unpriv already.
Pin it with the world read creds and do map_lookup sys_bpf cmd on it.

Quoted 8 lines
Quoted 7 lines
Quoted 3 lines

Right, but, if I understand correctly, with sysctl_unprivileged_bpf_disabled, unpriv tasks are not able to make use of __sys_bpf(). Is there anything I missed?

That sysctl is a heavy hammer. Let's fix it instead.
map lookup/update/delete can be allowed for unpriv for certain map types. There are permissions checks in corresponding lookup/update calls already.

Quoted 4 lines

(Adding Jann)

I wonder if we can tag a map as BPF_F_UNPRIVILEGED and allow the writes to only maps that are explicitly marked as writable by unprivileged processes.

I think it's overkill for existing unpriv maps like hash and array. These maps by themself don't pose a security threat.
The sysctl was/is in the wrong place.

Quoted 9 lines

We will have task local storage in LSM programs that we won't like unprivileged processes to write to as well.

struct {
        __uint(type, BPF_MAP_TYPE_TASK_STORAGE);
        __uint(map_flags, BPF_F_NO_PREALLOC | BPF_F_UNPRIVILEGED);
        __type(key, int);
        __type(value, struct fd_storage);
} task_fd_storage_map SEC(".maps");

local storage map was not exposed to unpriv before. This would be a different consideration.
But even in such a case the extra flag looks unnecessary.

#16 lore raw
Quoted 122 lines · Alexei Starovoitov
Quoted 101 lines · KP Singh
Quoted 90 lines · Hao Luo
Quoted 87 lines · Alexei Starovoitov
Quoted 85 lines · Hao Luo
Quoted 81 lines · Alexei Starovoitov
Quoted 71 lines · Hao Luo
Quoted 60 lines · Kumar Kartikeya Dwivedi
Quoted 58 lines · Hao Luo
Quoted 41 lines · Hao Luo

Hi Yonghong,

Quoted 17 lines · Yonghong Song
Quoted 9 lines · Hao Luo

Some map types support mmap operation, which allows userspace to communicate with BPF programs directly. Currently only arraymap and ringbuf have mmap implemented.

However, in some use cases, when multiple program instances can run concurrently, global mmapable memory can cause race. In that case, userspace needs to provide necessary synchronizations to coordinate the usage of mapped global data. This can be a source of bottleneck.

I can see your use case here. Each calling process can get the corresponding bpf program task local storage data through mmap interface. As you mentioned, there is a tradeoff between more memory vs. non-global synchronization.

Quoted 9 lines

I am thinking that another bpf_iter approach can retrieve the similar result. We could implement a bpf_iter for task local storage map, optionally it can provide a tid to retrieve the data for that particular tid. This way, user space needs an explicit syscall, but does not need to allocate more memory than necessary.

WDYT?

Thanks for the suggestion. I have two thoughts about bpf_iter + tid and mmap:

- mmap prevents the calling task from reading other task's value. Using bpf_iter, one can pass other task's tid to get their values. I assume there are two potential ways of passing tid to bpf_iter: one is to use global data in bpf prog, the other is adding tid parameterized iter_link. For the first, it's not easy for unpriv tasks to use. For the second, we need to create one iter_link object for each interested tid. It may not be easy to use either.

Quoted 6 lines

- Regarding adding an explicit syscall. I thought about adding write/read syscalls for task local storage maps, just like reading values from iter_link. Writing or reading task local storage map updates/reads the current task's value. I think this could achieve the same effect as mmap.

Actually, my use case of using mmap on task local storage is to allow userspace to pass FDs into bpf prog. Some of the helpers I want to add need to take an FD as parameter and the bpf progs can run concurrently, thus using global data is racy. Mmapable task local storage is the best solution I can find for this purpose.

Song also mentioned to me offline, that mmapable task local storage may be useful for his use case.

Quoted 8 lines
Quoted 2 lines

I am actually open to other proposals.

You could also use a syscall prog, and use bpf_prog_test_run to update local storage for current. Data can be passed for that specific prog invocation using ctx. You might have to enable bpf_task_storage helpers in it though, since they are not allowed to be called right now.

Quoted 3 lines

The loading process needs CAP_BPF to load bpf_prog_test_run. I'm thinking of allowing any thread including unpriv ones to be able to pass data to the prog and update their own storage.

If I understand the use case correctly all of this mmap-ing is only to allow unpriv userspace to access a priv map via unpriv mmap() syscall. But the map can be accessed as unpriv already.
Pin it with the world read creds and do map_lookup sys_bpf cmd on it.

Quoted 8 lines
Quoted 7 lines
Quoted 3 lines

Right, but, if I understand correctly, with sysctl_unprivileged_bpf_disabled, unpriv tasks are not able to make use of __sys_bpf(). Is there anything I missed?

That sysctl is a heavy hammer. Let's fix it instead.
map lookup/update/delete can be allowed for unpriv for certain map types. There are permissions checks in corresponding lookup/update calls already.

Quoted 4 lines

(Adding Jann)

I wonder if we can tag a map as BPF_F_UNPRIVILEGED and allow the writes to only maps that are explicitly marked as writable by unprivileged processes.

I think it's overkill for existing unpriv maps like hash and array. These maps by themself don't pose a security threat.
The sysctl was/is in the wrong place.

Quoted 9 lines

We will have task local storage in LSM programs that we won't like unprivileged processes to write to as well.

struct {
        __uint(type, BPF_MAP_TYPE_TASK_STORAGE);
        __uint(map_flags, BPF_F_NO_PREALLOC | BPF_F_UNPRIVILEGED);
        __type(key, int);
        __type(value, struct fd_storage);
} task_fd_storage_map SEC(".maps");
Quoted 3 lines

local storage map was not exposed to unpriv before. This would be a different consideration.
But even in such a case the extra flag looks unnecessary.

I took a look at the code and it makes sense to allow maps like hash and array which are already mmapable. These should not really need the BPF_F_UNPRIVILEGED.

Hao, I think you can send a patch that removes these map operations from the scope of
the sysctl. Regarding the local storages, let's do them separately since it would be a newer
access surface.