Skip to content

Commit b54a0d4

Browse files
committed
Daniel Borkmann says: ==================== bpf-next 2022-11-02 We've added 70 non-merge commits during the last 14 day(s) which contain a total of 96 files changed, 3203 insertions(+), 640 deletions(-). The main changes are: 1) Make cgroup local storage available to non-cgroup attached BPF programs such as tc BPF ones, from Yonghong Song. 2) Avoid unnecessary deadlock detection and failures wrt BPF task storage helpers, from Martin KaFai Lau. 3) Add LLVM disassembler as default library for dumping JITed code in bpftool, from Quentin Monnet. 4) Various kprobe_multi_link fixes related to kernel modules, from Jiri Olsa. 5) Optimize x86-64 JIT with emitting BMI2-based shift instructions, from Jie Meng. 6) Improve BPF verifier's memory type compatibility for map key/value arguments, from Dave Marchevsky. 7) Only create mmap-able data section maps in libbpf when data is exposed via skeletons, from Andrii Nakryiko. 8) Add an autoattach option for bpftool to load all object assets, from Wang Yufen. 9) Various memory handling fixes for libbpf and BPF selftests, from Xu Kuohai. 10) Initial support for BPF selftest's vmtest.sh on arm64, from Manu Bretelle. 11) Improve libbpf's BTF handling to dedup identical structs, from Alan Maguire. 12) Add BPF CI and denylist documentation for BPF selftests, from Daniel Müller. 13) Check BPF cpumap max_entries before doing allocation work, from Florian Lehner. * tag 'for-netdev' of https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf-next: (70 commits) samples/bpf: Fix typo in README bpf: Remove the obsolte u64_stats_fetch_*_irq() users. bpf: check max_entries before allocating memory bpf: Fix a typo in comment for DFS algorithm bpftool: Fix spelling mistake "disasembler" -> "disassembler" selftests/bpf: Fix bpftool synctypes checking failure selftests/bpf: Panic on hard/soft lockup docs/bpf: Add documentation for new cgroup local storage selftests/bpf: Add test cgrp_local_storage to DENYLIST.s390x selftests/bpf: Add selftests for new cgroup local storage selftests/bpf: Fix test test_libbpf_str/bpf_map_type_str bpftool: Support new cgroup local storage libbpf: Support new cgroup local storage bpf: Implement cgroup storage available to non-cgroup-attached bpf progs bpf: Refactor some inode/task/sk storage functions for reuse bpf: Make struct cgroup btf id global selftests/bpf: Tracing prog can still do lookup under busy lock selftests/bpf: Ensure no task storage failure for bpf_lsm.s prog due to deadlock detection bpf: Add new bpf_task_storage_delete proto with no deadlock detection bpf: bpf_task_storage_delete_recur does lookup first before the deadlock check ... ==================== Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jakub Kicinski <[email protected]>
2 parents ef2dd61 + 3a07dcf commit b54a0d4

File tree

96 files changed

+3203
-640
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

96 files changed

+3203
-640
lines changed
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
.. SPDX-License-Identifier: GPL-2.0-only
2+
.. Copyright (C) 2022 Meta Platforms, Inc. and affiliates.
3+
4+
=========================
5+
BPF_MAP_TYPE_CGRP_STORAGE
6+
=========================
7+
8+
The ``BPF_MAP_TYPE_CGRP_STORAGE`` map type represents a local fix-sized
9+
storage for cgroups. It is only available with ``CONFIG_CGROUPS``.
10+
The programs are made available by the same Kconfig. The
11+
data for a particular cgroup can be retrieved by looking up the map
12+
with that cgroup.
13+
14+
This document describes the usage and semantics of the
15+
``BPF_MAP_TYPE_CGRP_STORAGE`` map type.
16+
17+
Usage
18+
=====
19+
20+
The map key must be ``sizeof(int)`` representing a cgroup fd.
21+
To access the storage in a program, use ``bpf_cgrp_storage_get``::
22+
23+
void *bpf_cgrp_storage_get(struct bpf_map *map, struct cgroup *cgroup, void *value, u64 flags)
24+
25+
``flags`` could be 0 or ``BPF_LOCAL_STORAGE_GET_F_CREATE`` which indicates that
26+
a new local storage will be created if one does not exist.
27+
28+
The local storage can be removed with ``bpf_cgrp_storage_delete``::
29+
30+
long bpf_cgrp_storage_delete(struct bpf_map *map, struct cgroup *cgroup)
31+
32+
The map is available to all program types.
33+
34+
Examples
35+
========
36+
37+
A BPF program example with BPF_MAP_TYPE_CGRP_STORAGE::
38+
39+
#include <vmlinux.h>
40+
#include <bpf/bpf_helpers.h>
41+
#include <bpf/bpf_tracing.h>
42+
43+
struct {
44+
__uint(type, BPF_MAP_TYPE_CGRP_STORAGE);
45+
__uint(map_flags, BPF_F_NO_PREALLOC);
46+
__type(key, int);
47+
__type(value, long);
48+
} cgrp_storage SEC(".maps");
49+
50+
SEC("tp_btf/sys_enter")
51+
int BPF_PROG(on_enter, struct pt_regs *regs, long id)
52+
{
53+
struct task_struct *task = bpf_get_current_task_btf();
54+
long *ptr;
55+
56+
ptr = bpf_cgrp_storage_get(&cgrp_storage, task->cgroups->dfl_cgrp, 0,
57+
BPF_LOCAL_STORAGE_GET_F_CREATE);
58+
if (ptr)
59+
__sync_fetch_and_add(ptr, 1);
60+
61+
return 0;
62+
}
63+
64+
Userspace accessing map declared above::
65+
66+
#include <linux/bpf.h>
67+
#include <linux/libbpf.h>
68+
69+
__u32 map_lookup(struct bpf_map *map, int cgrp_fd)
70+
{
71+
__u32 *value;
72+
value = bpf_map_lookup_elem(bpf_map__fd(map), &cgrp_fd);
73+
if (value)
74+
return *value;
75+
return 0;
76+
}
77+
78+
Difference Between BPF_MAP_TYPE_CGRP_STORAGE and BPF_MAP_TYPE_CGROUP_STORAGE
79+
============================================================================
80+
81+
The old cgroup storage map ``BPF_MAP_TYPE_CGROUP_STORAGE`` has been marked as
82+
deprecated (renamed to ``BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED``). The new
83+
``BPF_MAP_TYPE_CGRP_STORAGE`` map should be used instead. The following
84+
illusates the main difference between ``BPF_MAP_TYPE_CGRP_STORAGE`` and
85+
``BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED``.
86+
87+
(1). ``BPF_MAP_TYPE_CGRP_STORAGE`` can be used by all program types while
88+
``BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED`` is available only to cgroup program types
89+
like BPF_CGROUP_INET_INGRESS or BPF_CGROUP_SOCK_OPS, etc.
90+
91+
(2). ``BPF_MAP_TYPE_CGRP_STORAGE`` supports local storage for more than one
92+
cgroup while ``BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED`` only supports one cgroup
93+
which is attached by a BPF program.
94+
95+
(3). ``BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED`` allocates local storage at attach time so
96+
``bpf_get_local_storage()`` always returns non-NULL local storage.
97+
``BPF_MAP_TYPE_CGRP_STORAGE`` allocates local storage at runtime so
98+
it is possible that ``bpf_cgrp_storage_get()`` may return null local storage.
99+
To avoid such null local storage issue, user space can do
100+
``bpf_map_update_elem()`` to pre-allocate local storage before a BPF program
101+
is attached.
102+
103+
(4). ``BPF_MAP_TYPE_CGRP_STORAGE`` supports deleting local storage by a BPF program
104+
while ``BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED`` only deletes storage during
105+
prog detach time.
106+
107+
So overall, ``BPF_MAP_TYPE_CGRP_STORAGE`` supports all ``BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED``
108+
functionality and beyond. It is recommended to use ``BPF_MAP_TYPE_CGRP_STORAGE``
109+
instead of ``BPF_MAP_TYPE_CGROUP_STORAGE_DEPRECATED``.

Documentation/bpf/maps.rst

Lines changed: 65 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,81 @@
11

2-
=========
3-
eBPF maps
2+
========
3+
BPF maps
4+
========
5+
6+
BPF 'maps' provide generic storage of different types for sharing data between
7+
kernel and user space. There are several storage types available, including
8+
hash, array, bloom filter and radix-tree. Several of the map types exist to
9+
support specific BPF helpers that perform actions based on the map contents. The
10+
maps are accessed from BPF programs via BPF helpers which are documented in the
11+
`man-pages`_ for `bpf-helpers(7)`_.
12+
13+
BPF maps are accessed from user space via the ``bpf`` syscall, which provides
14+
commands to create maps, lookup elements, update elements and delete
15+
elements. More details of the BPF syscall are available in
16+
:doc:`/userspace-api/ebpf/syscall` and in the `man-pages`_ for `bpf(2)`_.
17+
18+
Map Types
419
=========
520

6-
'maps' is a generic storage of different types for sharing data between kernel
7-
and userspace.
21+
.. toctree::
22+
:maxdepth: 1
23+
:glob:
824

9-
The maps are accessed from user space via BPF syscall, which has commands:
25+
map_*
1026

11-
- create a map with given type and attributes
12-
``map_fd = bpf(BPF_MAP_CREATE, union bpf_attr *attr, u32 size)``
13-
using attr->map_type, attr->key_size, attr->value_size, attr->max_entries
14-
returns process-local file descriptor or negative error
27+
Usage Notes
28+
===========
1529

16-
- lookup key in a given map
17-
``err = bpf(BPF_MAP_LOOKUP_ELEM, union bpf_attr *attr, u32 size)``
18-
using attr->map_fd, attr->key, attr->value
19-
returns zero and stores found elem into value or negative error
30+
.. c:function::
31+
int bpf(int command, union bpf_attr *attr, u32 size)
2032
21-
- create or update key/value pair in a given map
22-
``err = bpf(BPF_MAP_UPDATE_ELEM, union bpf_attr *attr, u32 size)``
23-
using attr->map_fd, attr->key, attr->value
24-
returns zero or negative error
33+
Use the ``bpf()`` system call to perform the operation specified by
34+
``command``. The operation takes parameters provided in ``attr``. The ``size``
35+
argument is the size of the ``union bpf_attr`` in ``attr``.
2536

26-
- find and delete element by key in a given map
27-
``err = bpf(BPF_MAP_DELETE_ELEM, union bpf_attr *attr, u32 size)``
28-
using attr->map_fd, attr->key
37+
**BPF_MAP_CREATE**
2938

30-
- to delete map: close(fd)
31-
Exiting process will delete maps automatically
39+
Create a map with the desired type and attributes in ``attr``:
3240

33-
userspace programs use this syscall to create/access maps that eBPF programs
34-
are concurrently updating.
41+
.. code-block:: c
3542
36-
maps can have different types: hash, array, bloom filter, radix-tree, etc.
43+
int fd;
44+
union bpf_attr attr = {
45+
.map_type = BPF_MAP_TYPE_ARRAY; /* mandatory */
46+
.key_size = sizeof(__u32); /* mandatory */
47+
.value_size = sizeof(__u32); /* mandatory */
48+
.max_entries = 256; /* mandatory */
49+
.map_flags = BPF_F_MMAPABLE;
50+
.map_name = "example_array";
51+
};
3752
38-
The map is defined by:
53+
fd = bpf(BPF_MAP_CREATE, &attr, sizeof(attr));
3954
40-
- type
41-
- max number of elements
42-
- key size in bytes
43-
- value size in bytes
55+
Returns a process-local file descriptor on success, or negative error in case of
56+
failure. The map can be deleted by calling ``close(fd)``. Maps held by open
57+
file descriptors will be deleted automatically when a process exits.
4458

45-
Map Types
46-
=========
59+
.. note:: Valid characters for ``map_name`` are ``A-Z``, ``a-z``, ``0-9``,
60+
``'_'`` and ``'.'``.
4761

48-
.. toctree::
49-
:maxdepth: 1
50-
:glob:
62+
**BPF_MAP_LOOKUP_ELEM**
63+
64+
Lookup key in a given map using ``attr->map_fd``, ``attr->key``,
65+
``attr->value``. Returns zero and stores found elem into ``attr->value`` on
66+
success, or negative error on failure.
67+
68+
**BPF_MAP_UPDATE_ELEM**
69+
70+
Create or update key/value pair in a given map using ``attr->map_fd``, ``attr->key``,
71+
``attr->value``. Returns zero on success or negative error on failure.
72+
73+
**BPF_MAP_DELETE_ELEM**
74+
75+
Find and delete element by key in a given map using ``attr->map_fd``,
76+
``attr->key``. Returns zero on success or negative error on failure.
5177

52-
map_*
78+
.. Links:
79+
.. _man-pages: https://www.kernel.org/doc/man-pages/
80+
.. _bpf(2): https://man7.org/linux/man-pages/man2/bpf.2.html
81+
.. _bpf-helpers(7): https://man7.org/linux/man-pages/man7/bpf-helpers.7.html

arch/arm64/net/bpf_jit_comp.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,13 +1649,8 @@ static void invoke_bpf_prog(struct jit_ctx *ctx, struct bpf_tramp_link *l,
16491649
struct bpf_prog *p = l->link.prog;
16501650
int cookie_off = offsetof(struct bpf_tramp_run_ctx, bpf_cookie);
16511651

1652-
if (p->aux->sleepable) {
1653-
enter_prog = (u64)__bpf_prog_enter_sleepable;
1654-
exit_prog = (u64)__bpf_prog_exit_sleepable;
1655-
} else {
1656-
enter_prog = (u64)__bpf_prog_enter;
1657-
exit_prog = (u64)__bpf_prog_exit;
1658-
}
1652+
enter_prog = (u64)bpf_trampoline_enter(p);
1653+
exit_prog = (u64)bpf_trampoline_exit(p);
16591654

16601655
if (l->cookie == 0) {
16611656
/* if cookie is zero, one instruction is enough to store it */

0 commit comments

Comments
 (0)