All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH RFC bpf-next 0/2] Mmapable task local storage.
@ 2022-03-24 23:41 Hao Luo
  2022-03-24 23:41 ` [PATCH RFC bpf-next 1/2] bpf: Mmapable " Hao Luo
                   ` (2 more replies)
  0 siblings, 3 replies; 20+ messages in thread
From: Hao Luo @ 2022-03-24 23:41 UTC (permalink / raw)
  To: Alexei Starovoitov, Andrii Nakryiko, Daniel Borkmann
  Cc: yhs, KP Singh, Martin KaFai Lau, Song Liu, bpf, linux-kernel, 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.

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

-- 
2.35.1.1021.g381101b075-goog


^ permalink raw reply	[flat|nested] 20+ messages in thread
* Re: [PATCH RFC bpf-next 1/2] bpf: Mmapable local storage.
  2022-03-24 23:41 ` [PATCH RFC bpf-next 1/2] bpf: Mmapable " Hao Luo
@ 2022-03-25 12:40 ` Dan Carpenter
  -1 siblings, 0 replies; 20+ messages in thread
From: kernel test robot @ 2022-03-25  7:53 UTC (permalink / raw)
  To: kbuild

[-- Attachment #1: Type: text/plain, Size: 3750 bytes --]

CC: kbuild-all(a)lists.01.org
BCC: lkp(a)intel.com
In-Reply-To: <20220324234123.1608337-2-haoluo@google.com>
References: <20220324234123.1608337-2-haoluo@google.com>
TO: Hao Luo <haoluo@google.com>

Hi Hao,

[FYI, it's a private test report for your RFC patch.]
[auto build test WARNING on bpf-next/master]

url:    https://github.com/0day-ci/linux/commits/Hao-Luo/Mmapable-task-local-storage/20220325-074313
base:   https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next.git master
:::::: branch date: 8 hours ago
:::::: commit date: 8 hours ago
config: i386-randconfig-m021 (https://download.01.org/0day-ci/archive/20220325/202203251506.EuMlgJWs-lkp(a)intel.com/config)
compiler: gcc-9 (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0

If you fix the issue, kindly add following tag as appropriate
Reported-by: kernel test robot <lkp@intel.com>
Reported-by: Dan Carpenter <dan.carpenter@oracle.com>

smatch warnings:
kernel/bpf/bpf_task_storage.c:342 task_storage_map_mmap() error: we previously assumed 'sdata' could be null (see line 327)

vim +/sdata +342 kernel/bpf/bpf_task_storage.c

4cf1bc1f104520 KP Singh 2020-11-06  309  
e8fe1745ccea53 Hao Luo  2022-03-24  310  static int task_storage_map_mmap(struct bpf_map *map, struct vm_area_struct *vma)
e8fe1745ccea53 Hao Luo  2022-03-24  311  {
e8fe1745ccea53 Hao Luo  2022-03-24  312  	struct bpf_local_storage_map *smap;
e8fe1745ccea53 Hao Luo  2022-03-24  313  	struct bpf_local_storage_data *sdata;
e8fe1745ccea53 Hao Luo  2022-03-24  314  	int err;
e8fe1745ccea53 Hao Luo  2022-03-24  315  
e8fe1745ccea53 Hao Luo  2022-03-24  316  	if (!(map->map_flags & BPF_F_MMAPABLE))
e8fe1745ccea53 Hao Luo  2022-03-24  317  		return -EINVAL;
e8fe1745ccea53 Hao Luo  2022-03-24  318  
e8fe1745ccea53 Hao Luo  2022-03-24  319  	rcu_read_lock();
e8fe1745ccea53 Hao Luo  2022-03-24  320  	if (!bpf_task_storage_trylock()) {
e8fe1745ccea53 Hao Luo  2022-03-24  321  		rcu_read_unlock();
e8fe1745ccea53 Hao Luo  2022-03-24  322  		return -EBUSY;
e8fe1745ccea53 Hao Luo  2022-03-24  323  	}
e8fe1745ccea53 Hao Luo  2022-03-24  324  
e8fe1745ccea53 Hao Luo  2022-03-24  325  	smap = (struct bpf_local_storage_map *)map;
e8fe1745ccea53 Hao Luo  2022-03-24  326  	sdata = task_storage_lookup(current, map, true);
e8fe1745ccea53 Hao Luo  2022-03-24 @327  	if (sdata) {
e8fe1745ccea53 Hao Luo  2022-03-24  328  		err = bpf_local_storage_mmap(smap, sdata->data, vma);
e8fe1745ccea53 Hao Luo  2022-03-24  329  		goto unlock;
e8fe1745ccea53 Hao Luo  2022-03-24  330  	}
e8fe1745ccea53 Hao Luo  2022-03-24  331  
e8fe1745ccea53 Hao Luo  2022-03-24  332  	/* only allocate new storage, when the task is refcounted */
e8fe1745ccea53 Hao Luo  2022-03-24  333  	if (refcount_read(&current->usage)) {
e8fe1745ccea53 Hao Luo  2022-03-24  334  		sdata = bpf_local_storage_update(current, smap, NULL,
e8fe1745ccea53 Hao Luo  2022-03-24  335  						 BPF_NOEXIST, GFP_ATOMIC);
e8fe1745ccea53 Hao Luo  2022-03-24  336  		if (IS_ERR(sdata)) {
e8fe1745ccea53 Hao Luo  2022-03-24  337  			err = PTR_ERR(sdata);
e8fe1745ccea53 Hao Luo  2022-03-24  338  			goto unlock;
e8fe1745ccea53 Hao Luo  2022-03-24  339  		}
e8fe1745ccea53 Hao Luo  2022-03-24  340  	}
e8fe1745ccea53 Hao Luo  2022-03-24  341  
e8fe1745ccea53 Hao Luo  2022-03-24 @342  	err = bpf_local_storage_mmap(smap, sdata->data, vma);
e8fe1745ccea53 Hao Luo  2022-03-24  343  unlock:
e8fe1745ccea53 Hao Luo  2022-03-24  344  	bpf_task_storage_unlock();
e8fe1745ccea53 Hao Luo  2022-03-24  345  	rcu_read_unlock();
e8fe1745ccea53 Hao Luo  2022-03-24  346  	return err;
e8fe1745ccea53 Hao Luo  2022-03-24  347  }
e8fe1745ccea53 Hao Luo  2022-03-24  348  

-- 
0-DAY CI Kernel Test Service
https://01.org/lkp

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

end of thread, other threads:[~2022-04-02  0:48 UTC | newest]

Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-24 23:41 [PATCH RFC bpf-next 0/2] Mmapable task local storage Hao Luo
2022-03-24 23:41 ` [PATCH RFC bpf-next 1/2] bpf: Mmapable " Hao Luo
2022-03-24 23:41 ` [PATCH RFC bpf-next 2/2] selftests/bpf: Test mmapable task " Hao Luo
2022-03-25 19:16 ` [PATCH RFC bpf-next 0/2] Mmapable " Yonghong Song
2022-03-28 17:39   ` Hao Luo
2022-03-28 17:46     ` Hao Luo
2022-03-29  9:37       ` Kumar Kartikeya Dwivedi
2022-03-29 17:43         ` Hao Luo
2022-03-29 21:45           ` Martin KaFai Lau
2022-03-30 18:05             ` Hao Luo
2022-03-29 23:29           ` Alexei Starovoitov
2022-03-30 18:06             ` Hao Luo
2022-03-30 18:16               ` Alexei Starovoitov
2022-03-30 18:26                 ` Hao Luo
2022-03-31 22:32                   ` KP Singh
2022-03-31 23:06                     ` Alexei Starovoitov
2022-04-02  0:48                       ` KP Singh
2022-03-25  7:53 [PATCH RFC bpf-next 1/2] bpf: Mmapable " kernel test robot
2022-03-25 12:40 ` [kbuild] " Dan Carpenter
2022-03-25 17:36 ` Hao Luo

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.