bpf: Embed local storage values in the storage object
Subsystems (7)
v2 cover bpf: Embed local storage values in the storage object
A lookup in a local storage map reads the storage object, a cache slot and then the selem that holds the value. When the owner is cold, each of these reads can be a cache miss. Programs that do a few lookups for each packet pay this cost on each packet.
This series adds the map flag BPF_F_INLINE_VALUE for sk, task, inode and cgroup storage. The value of such a map is in the storage object, at a fixed offset, so a lookup has one dependent load less.
Design
------
- Each owner type has one inline area at the end of struct bpf_local_storage. A storage object is at most 1 KB.
- Map create claims a slot (an 8 B tag and the value) in the area, first fit. A table of the live inline maps holds the layout, with at most 16 maps for each owner type. Map create fails with -ENOSPC if the area has no room now, and with -E2BIG if the value never fits.
- A storage object that is made while inline maps exist gets the area. If an object has no room for a slot, the value goes into a normal selem. Programs do not see the difference.
- The tag comes from a claim number. A lookup compares the tag with an acquire load, so a new map does not see the values of a freed map at the same offset.
- Programs can keep pointers to inline values, and these values have no selem. Thus an object with an inline area lives until its owner dies, and it is freed after an RCU tasks trace grace period.
- A lookup of a normal map tests one more field of the map, also on a host with no inline map.
- The sk storage map iterator is refused for inline maps, and sk diag shows inline values. A delete in NMI fails with -EOPNOTSUPP, as for normal maps. The flag needs a 64-bit kernel.
- libbpf does not retry a local storage map create without BTF any more. That retry always failed with -EINVAL and hid the real error.
Semantics and costs
-------------------
- A syscall update copies the value in place, under the storage lock. A reader that takes no lock can see a torn value, as with array maps. Without the flag, an update swaps in a new selem, so readers see the whole old or new value. BPF_F_LOCK is not available, because an inline value cannot have a spin lock.
- A pointer that a program keeps after an update or a delete points to the same slot. It sees, and can change, the next value of the same map. Without the flag, it sees the old value.
- The tag is predictable, and claim numbers wrap after 2^32 claims. A user that can create inline maps can make another map see bytes that it did not write, in objects made before that map.
- The area and the 16-map limit are shared by the whole host, also with BPF token users.
- While an inline map exists, new objects of its owner type are bigger and live until owner death. This is also true for owners that use only normal maps. A delete or a map unload does not give this memory back.
Numbers
-------
Host: AMD EPYC (Zen 5), pinned 24-vCPU KVM guest. Workload: two TC egress programs look up two sk storage values for each packet (124 B and 2 B), 9 sk storage maps for each socket, 131k connections with request/response traffic. Paired A/B runs against the base kernel.
- Whole request, programs on, both values inline: -200 +- 108 ns (-2.9%).
- Sampled cost of the two lookups: 169 -> 58 ns and 90 -> 11 ns.
- bench_local_storage, hot loop with all data in L1, ns per lookup:
normal maps: +0.39 to +0.76 (1 to 16 maps; 3 more instructions and 1 more taken branch); inline hit: +0.32 to +0.67. The gain comes from cold owners, not from a hot loop.
- Memory: an object grows from 192 B to 256 B with inline values of up to 64 B, and to 1 KB with a 512 B inline value. Owners that use only normal maps pay the same while an inline map exists. In the workload above, unreclaimable slab grows by 629 B for each connection.
- Socket churn: no throughput cost. At about 230k closes each second, about 18 MB more objects wait for the tasks trace grace period.
Patches
-------
Patch 1 fixes an existing bug: a failed create in NMI freed storage that was never published with kfree_rcu(). It can go in alone. It needs commit bd897919e4dc, which is in bpf-next only. Patches 2-3 prepare the code. Patches 4-10 add the inline area, its lifetime, lookup and update, the iterator refusal, sk diag and the UAPI flag. Patch 11 changes libbpf, patch 12 adds a selftest and patch 13 adds the docs.
Testing
-------
- The selftest passes on normal and KASAN kernels, with no kmemleak report.
- Out of tree: races of create, update, delete, destroy and map free, also from sleepable and NMI programs; sk diag, clone, inner maps and owner death; fault injection
that forces the storage lock to fail in destroy, with no leak and no KASAN report. KCSAN ran on an earlier revision with no report.
---
Changes in v2:
- Removed RFC tag, simplified selftests.
- Link to v1: https://patch.msgid.link/[email protected]
---
Mykyta Yatsenko (13):
bpf: Free unpublished local storage with kfree_nolock()
bpf: Add obj_size to bpf_local_storage
bpf: Add bpf_local_storage_delete()
bpf: Add an inline slot allocator to local storage caches
bpf: Size new local storage objects for the inline area
bpf: Keep local storage objects with an inline area until owner death
bpf: Keep values of BPF_F_INLINE_VALUE maps in the storage object
bpf: Refuse the sk storage map iterator for BPF_F_INLINE_VALUE maps
bpf: Show inline values in sk diag
bpf: Allow BPF_F_INLINE_VALUE for local storage maps
libbpf: Do not retry local storage map create without BTF selftests/bpf: Test BPF_F_INLINE_VALUE local storage maps bpf, docs: Document BPF_F_INLINE_VALUE for local storage maps
Documentation/bpf/map_cgrp_storage.rst | 3 + Documentation/bpf/map_sk_storage.rst | 103 +++- include/linux/bpf_local_storage.h | 105 +++- include/uapi/linux/bpf.h | 7 + kernel/bpf/bpf_cgrp_storage.c | 8 +- kernel/bpf/bpf_inode_storage.c | 8 +- kernel/bpf/bpf_local_storage.c | 420 +++++++++++++-- kernel/bpf/bpf_task_storage.c | 8 +- net/core/bpf_sk_storage.c | 71 ++- tools/include/uapi/linux/bpf.h | 7 + tools/lib/bpf/libbpf.c | 17 +- .../bpf/prog_tests/local_storage_inline.c | 583 +++++++++++++++++++++ .../selftests/bpf/progs/local_storage_inline.c | 123 +++++ 13 files changed, 1388 insertions(+), 75 deletions(-)
---
base-commit: 4425c2a376e11caa4f01ea4ff2fbab61d197d67b
change-id: 20261113-sls_f1-abf16304e3ef
Best regards,
Signature · 1 line
Mykyta Yatsenko <[email protected]>
PATCH v2 1/13 bpf: Free unpublished local storage with kfree_nolock()
From: Mykyta Yatsenko <[email protected]>
A tracing program can create local storage in NMI. When the create fails, the new storage and selem are freed with kfree_rcu(), which is not safe in NMI. Nobody has seen them yet, so free them at once with kfree_nolock(), which works in any context for kmalloc_nolock() memory.
This fix needs commit bd897919e4dc ("bpf: Link the first element after its storage is published"). Before that commit, an iterator can find the first selem before the storage is published, so an immediate free is not safe.
--- kernel/bpf/bpf_local_storage.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-)
kernel/bpf/bpf_local_storage.c +16 -3
@@ -158,6 +158,18 @@ void bpf_selem_free(struct bpf_local_storage_elem *selem,call_rcu_tasks_trace (&selem->rcu, bpf_selem_free_trace_rcu); } +/* +* selem was never published: no reader can see it, and this works in NMI. +* Only the syscall update puts special fields (uptrs) in a new selem, and it +* never runs in NMI, so freeing them here is safe too. +*/ +static void bpf_selem_free_unpublished (struct bpf_local_storage_elem *selem, +struct bpf_local_storage_map *smap) +{ +bpf_obj_free_fields (smap->map.record,SDATA (selem)->data); +kfree_nolock (selem); +} +static void bpf_selem_free_list (struct hlist_head *list,bool reuse_now) {struct bpf_local_storage_elem *selem;
@@ -562,7 +574,8 @@ int bpf_local_storage_alloc(void *owner,return 0 ; uncharge: -bpf_local_storage_free (storage, true); +/* storage was never published: no reader can see it */ +kfree_nolock (storage);mem_uncharge (smap, owner,sizeof (*storage));return err; }
@@ -605,7 +618,7 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap, err =bpf_local_storage_alloc (owner, smap, selem);if (err) { -bpf_selem_free (selem, true); +bpf_selem_free_unpublished (selem, smap);mem_uncharge (smap, owner, smap->elem_size);return ERR_PTR (err); }
@@ -692,7 +705,7 @@ bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,bpf_selem_free_list (&old_selem_free_list, false);if (alloc_selem) {mem_uncharge (smap, owner, smap->elem_size); -bpf_selem_free (alloc_selem, true); +bpf_selem_free_unpublished (alloc_selem, smap); }return err ?ERR_PTR (err) :SDATA (selem); }
Signature · 1 line
2.53.1-Meta
PATCH v2 2/13 bpf: Add obj_size to bpf_local_storage
In reply to Mykyta YatsenkoFrom: Mykyta Yatsenko <[email protected]>
A later patch makes storage objects that are bigger than the struct. Keep the size in the object, and charge and uncharge that size.
--- include/linux/bpf_local_storage.h | 1 + kernel/bpf/bpf_local_storage.c | 14 ++++++++------ 2 files changed, 9 insertions(+), 6 deletions(-)
include/linux/bpf_local_storage.h +1 -0
@@ -100,6 +100,7 @@ struct bpf_local_storage {*/ struct rcu_head rcu;rqspinlock_t lock;/* Protect adding/removing from the "list" */ +u32 obj_size;/* Size of this object. It does not change. */ u64 mem_charge;/* Copy of mem charged to owner. Protected by "lock" */ refcount_t owner_refcnt;/* Used to pin owner when map_free is uncharging */ };
kernel/bpf/bpf_local_storage.c +8 -6
@@ -199,7 +199,7 @@ static void bpf_selem_unlink_storage_nolock_misc(struct bpf_local_storage_elem *if (pin_owner && !refcount_inc_not_zero (&local_storage->owner_refcnt))return ; - uncharge += free_local_storage ?sizeof (*local_storage) :0 ; + uncharge += free_local_storage ? local_storage->obj_size :0 ;mem_uncharge (smap, local_storage->owner, uncharge); local_storage->mem_charge -= uncharge;
@@ -517,15 +517,16 @@ int bpf_local_storage_alloc(void *owner,struct bpf_local_storage *prev_storage, *storage;struct bpf_local_storage **owner_storage_ptr;struct bpf_local_storage_map_bucket *b; +u32 size =sizeof (*storage);unsigned long flags;int err; - err =mem_charge (smap, owner,sizeof (*storage)); + err =mem_charge (smap, owner, size);if (err)return err; - storage =bpf_map_kmalloc_nolock (&smap->map,sizeof (*storage), - __GFP_ZERO,NUMA_NO_NODE ); + storage =bpf_map_kmalloc_nolock (&smap->map, size, __GFP_ZERO, +NUMA_NO_NODE );if (!storage) { err = -ENOMEM ; goto uncharge;
@@ -534,7 +535,8 @@ int bpf_local_storage_alloc(void *owner,INIT_HLIST_HEAD (&storage->list);raw_res_spin_lock_init (&storage->lock); storage->owner = owner; - storage->mem_charge =sizeof (*storage); + storage->obj_size = size; + storage->mem_charge = size;refcount_set (&storage->owner_refcnt,1 );bpf_selem_link_storage_nolock (storage, first_selem);
@@ -576,7 +578,7 @@ int bpf_local_storage_alloc(void *owner, uncharge:/* storage was never published: no reader can see it */ kfree_nolock (storage); -mem_uncharge (smap, owner,sizeof (*storage)); +mem_uncharge (smap, owner, size);return err; }
Signature · 1 line
2.53.1-Meta
PATCH v2 3/13 bpf: Add bpf_local_storage_delete()
In reply to Mykyta YatsenkoFrom: Mykyta Yatsenko <[email protected]>
The four owner types have the same delete code. A later patch changes how a value is deleted. One function keeps that change in one place.
No functional change intended.
--- include/linux/bpf_local_storage.h | 2 ++ kernel/bpf/bpf_cgrp_storage.c | 8 +------- kernel/bpf/bpf_inode_storage.c | 8 +------- kernel/bpf/bpf_local_storage.c | 15 +++++++++++++++ kernel/bpf/bpf_task_storage.c | 8 +------- net/core/bpf_sk_storage.c | 8 +------- 6 files changed, 21 insertions(+), 28 deletions(-)
include/linux/bpf_local_storage.h +2 -0
@@ -196,6 +196,8 @@ void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage,int bpf_selem_unlink (struct bpf_local_storage_elem *selem); +int bpf_local_storage_delete (void *owner,struct bpf_local_storage_map *smap); +int bpf_selem_link_map (struct bpf_local_storage_map *smap,struct bpf_local_storage *local_storage,struct bpf_local_storage_elem *selem);
kernel/bpf/bpf_cgrp_storage.c +1 -7
@@ -83,13 +83,7 @@ static long bpf_cgrp_storage_update_elem(struct bpf_map *map, void *key,static int cgroup_storage_delete (struct cgroup *cgroup,struct bpf_map *map) { -struct bpf_local_storage_data *sdata; - - sdata =cgroup_storage_lookup (cgroup, map, false); -if (!sdata) -return -ENOENT ; - -return bpf_selem_unlink (SELEM (sdata)); +return bpf_local_storage_delete (cgroup, (struct bpf_local_storage_map *)map); }static long bpf_cgrp_storage_delete_elem (struct bpf_map *map,void *key)
kernel/bpf/bpf_inode_storage.c +1 -7
@@ -104,13 +104,7 @@ static long bpf_fd_inode_storage_update_elem(struct bpf_map *map, void *key,static int inode_storage_delete (struct inode *inode,struct bpf_map *map) { -struct bpf_local_storage_data *sdata; - - sdata =inode_storage_lookup (inode, map, false); -if (!sdata) -return -ENOENT ; - -return bpf_selem_unlink (SELEM (sdata)); +return bpf_local_storage_delete (inode, (struct bpf_local_storage_map *)map); }static long bpf_fd_inode_storage_delete_elem (struct bpf_map *map,void *key)
kernel/bpf/bpf_local_storage.c +15 -0
@@ -356,6 +356,21 @@ int bpf_selem_unlink(struct bpf_local_storage_elem *selem)return err; } +int bpf_local_storage_delete (void *owner,struct bpf_local_storage_map *smap) +{ +struct bpf_local_storage __rcu **owner_ptr =owner_storage (smap, owner); +struct bpf_local_storage *local_storage; +struct bpf_local_storage_data *sdata; + +/* An inode can have no LSM blob, so no storage pointer */ + local_storage = owner_ptr ?rcu_dereference_check (*owner_ptr,bpf_rcu_lock_held ()) :NULL ; + sdata = local_storage ?bpf_local_storage_lookup (local_storage, smap, false) :NULL ; +if (!sdata) +return -ENOENT ; + +return bpf_selem_unlink (SELEM (sdata)); +} +/* * Unlink an selem from map and local storage with lockless fallback if callers * are racing or rqspinlock returns error. It should only be called by
kernel/bpf/bpf_task_storage.c +1 -7
@@ -128,13 +128,7 @@ static long bpf_pid_task_storage_update_elem(struct bpf_map *map, void *key,static int task_storage_delete (struct task_struct *task,struct bpf_map *map) { -struct bpf_local_storage_data *sdata; - - sdata =task_storage_lookup (task, map, false); -if (!sdata) -return -ENOENT ; - -return bpf_selem_unlink (SELEM (sdata)); +return bpf_local_storage_delete (task, (struct bpf_local_storage_map *)map); }static long bpf_pid_task_storage_delete_elem (struct bpf_map *map,void *key)
net/core/bpf_sk_storage.c +1 -7
@@ -34,13 +34,7 @@ bpf_sk_storage_lookup(struct sock *sk, struct bpf_map *map, bool cacheit_lockit)static int bpf_sk_storage_del (struct sock *sk,struct bpf_map *map) { -struct bpf_local_storage_data *sdata; - - sdata =bpf_sk_storage_lookup (sk, map, false); -if (!sdata) -return -ENOENT ; - -return bpf_selem_unlink (SELEM (sdata)); +return bpf_local_storage_delete (sk, (struct bpf_local_storage_map *)map); }/* Called by __sk_destruct() & bpf_sk_storage_clone() */
Signature · 1 line
2.53.1-Meta
PATCH v2 4/13 bpf: Add an inline slot allocator to local storage caches
In reply to Mykyta YatsenkoFrom: Mykyta Yatsenko <[email protected]>
BPF_F_INLINE_VALUE maps will keep their values in the storage object, so that a lookup needs no selem. Each owner type has one layout of slots for all its objects. A table of the live inline maps holds the layout. Fail map create with -ENOSPC if the area has no room, or if the owner type has 16 inline maps: a small table keeps the scans cheap.
The claim is in map_alloc(), with the other resources of the map, so map_free() gives the slot back on every path. Thus -ENOSPC can come before -EINVAL for special fields or bad BTF, and before the LSM hook. A create that fails later holds a slot for a short time.
Old objects keep the bytes of a freed map. Each claim gets a new number. A later patch makes the slot tag from it, so that a new map rarely sees these bytes as its values.
The flag is not in the UAPI and not allowed yet: a later patch does that.
--- include/linux/bpf_local_storage.h | 31 ++++++++++ kernel/bpf/bpf_local_storage.c | 119 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+)
include/linux/bpf_local_storage.h +31 -0
@@ -51,9 +51,17 @@ struct bpf_local_storage_map {* multiple buckets to improve contention. */ struct bpf_local_storage_map_bucket *buckets; +struct bpf_local_storage_cache *cache;u32 bucket_log;u16 elem_size;u16 cache_idx; +/* +* Byte offset of the inline slot of a BPF_F_INLINE_VALUE map from the +* start of the storage object, and the end of the slot. 0: no slot. +*/ +u16 inline_off; +u16 inline_end; +u32 inline_claim;/* Claim number of the slot, the source of its tag */ };struct bpf_local_storage_data {
@@ -133,9 +141,32 @@ bpf_local_storage_unpin_owner(struct bpf_local_storage *local_storage)#define BPF_LOCAL_STORAGE_CACHE_SIZE 16 +/* +* The inline area of a storage object goes from the end of the struct to +* at most 1 KB. A slot is an 8 byte tag, then the value. +*/ +#define BPF_LOCAL_STORAGE_INLINE_MAX 1024 +#define BPF_LOCAL_STORAGE_INLINE_START ALIGN(sizeof(struct bpf_local_storage), 8) +/* +* Inline maps per owner type. The same as the cache size on purpose: more +* maps than that share cache[] entries, so they gain less from a slot. +*/ +#define BPF_LOCAL_STORAGE_INLINE_MAPS 16 + +/* A later patch adds this flag to the UAPI and allows it */ +#define BPF_F_INLINE_VALUE BIT(20) +struct bpf_local_storage_cache {spinlock_t idx_lock;u64 idx_usage_counts[BPF_LOCAL_STORAGE_CACHE_SIZE ]; +/* +* The live inline maps, NULL if free. Few entries keep the scans in +* map create and sk diag cheap. Written under idx_lock. +*/ +struct bpf_local_storage_map __rcu *inline_maps[BPF_LOCAL_STORAGE_INLINE_MAPS ]; +/* Largest slot end in inline_maps[], so new objects read no map */ +u32 inline_end; +u32 inline_claims;/* Number of claims, the source of slot tags */ };#define DEFINE_BPF_STORAGE_CACHE (name) \
kernel/bpf/bpf_local_storage.c +119 -0
137 lines not on this page · raw message
Signature · 1 line
2.53.1-Meta
PATCH v2 5/13 bpf: Size new local storage objects for the inline area
In reply to Mykyta YatsenkoFrom: Mykyta Yatsenko <[email protected]>
A storage object must have room for the slots of the inline maps of its owner type. Make a new object as big as the end of the last used slot. The size of an object does not change after that, so the slots that an object holds stay at fixed places.
No map can have a slot yet, so all objects keep the size of the struct.
--- kernel/bpf/bpf_local_storage.c | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-)
kernel/bpf/bpf_local_storage.c +12 -1
@@ -525,6 +525,17 @@ static int check_flags(const struct bpf_local_storage_data *old_sdata,return 0 ; } +/* +* Once an inline map exists, a new object gets an inline area up to the end +* of the last slot, also for other maps. Its size does not change. +*/ +static u32 bpf_local_storage_obj_size (struct bpf_local_storage_map *smap) +{ +u32 end =max (READ_ONCE (smap->cache->inline_end), smap->inline_end); + +return end ?kmalloc_size_roundup (end) :sizeof (struct bpf_local_storage ); +} +int bpf_local_storage_alloc (void *owner,struct bpf_local_storage_map *smap,struct bpf_local_storage_elem *first_selem)
@@ -532,7 +543,7 @@ int bpf_local_storage_alloc(void *owner,struct bpf_local_storage *prev_storage, *storage;struct bpf_local_storage **owner_storage_ptr;struct bpf_local_storage_map_bucket *b; -u32 size =sizeof (*storage); +u32 size =bpf_local_storage_obj_size (smap);unsigned long flags;int err;
Signature · 1 line
2.53.1-Meta
PATCH v2 6/13 bpf: Keep local storage objects with an inline area until owner death
In reply to Mykyta YatsenkoFrom: Mykyta Yatsenko <[email protected]>
A later patch lets programs get pointers to values in the inline area. Such a value has no selem, so an empty list does not mean that nobody uses the object. Free an object with an inline area only after destroy(), and always after an RCU tasks trace grace period, so that sleepable programs cannot see freed memory.
Updaters can no longer use an empty list to see that the object goes away, so destroy() marks it as dying under the storage lock. If it cannot take that lock, it tries again after a grace period, so the mark adds no leak. A failed unlink of an selem leaks as before.
While an inline map exists, new objects of its owner type are bigger and live until owner death, also when the owner uses only normal maps.
--- include/linux/bpf_local_storage.h | 11 +++++ kernel/bpf/bpf_local_storage.c | 96 +++++++++++++++++++++++++++++++++++---- 2 files changed, 99 insertions(+), 8 deletions(-)
include/linux/bpf_local_storage.h +11 -0
@@ -111,8 +111,19 @@ struct bpf_local_storage {u32 obj_size;/* Size of this object. It does not change. */ u64 mem_charge;/* Copy of mem charged to owner. Protected by "lock" */ refcount_t owner_refcnt;/* Used to pin owner when map_free is uncharging */ +bool dying;/* destroy() has run. Only with an inline area */ }; +/* +* A program can hold a pointer into the inline area without an selem. So an +* object with an inline area lives until its owner goes away. +*/ +static inline bool +bpf_local_storage_has_inline (const struct bpf_local_storage *local_storage) +{ +return local_storage->obj_size >sizeof (*local_storage); +} +/* * Drop a reference got by refcount_inc_not_zero(&local_storage->owner_refcnt). * bpf_local_storage_destroy() may have dropped the owner's reference meanwhile
kernel/bpf/bpf_local_storage.c +88 -8
150 lines not on this page · raw message
Signature · 1 line
2.53.1-Meta
PATCH v2 7/13 bpf: Keep values of BPF_F_INLINE_VALUE maps in the storage object
In reply to Mykyta YatsenkoFrom: Mykyta Yatsenko <[email protected]>
A lookup of a normal map reads the storage object, a cache slot and then the selem that holds the value. When the owner is cold, each read can miss. A map with a slot finds its value at a fixed offset in the object, with one tag compare, so a lookup has one dependent load less.
The tag is 8 bytes and comes from the claim number of the map, so old value bytes rarely match it. It is written with a release store after the value and read with an acquire load. 32-bit has no 8 byte acquire and release, so slots are 64-bit only.
An update copies the value in place under the storage lock. Lockless readers (programs, the syscall lookup, sk diag) can see a torn value, as in array maps. KCSAN can report this race; it is intended.
A normal map pays one more test of a map field. There is no hint on that test, so that inline maps do not pay for normal maps.
--- include/linux/bpf_local_storage.h | 63 ++++++++++++++++++- kernel/bpf/bpf_local_storage.c | 129 ++++++++++++++++++++++++++++++++++---- net/core/bpf_sk_storage.c | 9 ++- 3 files changed, 180 insertions(+), 21 deletions(-)
include/linux/bpf_local_storage.h +60 -3
@@ -70,8 +70,16 @@ struct bpf_local_storage_data {* * Put it in the same cacheline as the data to minimize * the number of cachelines accessed during the cache hit case. +* +* An inline slot has the same layout, with a tag in place of smap: +* the tag of the map whose value is in the slot. Any other tag (0 +* after a delete) means no value. A slot is not in an selem: never +* apply SELEM() to it. */ -struct bpf_local_storage_map __rcu *smap; +union { +struct bpf_local_storage_map __rcu *smap; +u64 inline_tag; + };u8 data[]__aligned(8) ; };
@@ -195,6 +203,45 @@ bpf_local_storage_map_alloc(union bpf_attr *attr,void __bpf_local_storage_insert_cache (struct bpf_local_storage *local_storage,struct bpf_local_storage_map *smap,struct bpf_local_storage_elem *selem); + +/* +* smap has an inline slot. The tag needs an 8 byte acquire and release, so +* slots are 64-bit only: on 32-bit, false lets the compiler drop the slot +* code. There is no hint on the inline_off test, so that inline maps do not +* pay for normal maps. +*/ +static __always_inline bool +bpf_local_storage_map_inline (const struct bpf_local_storage_map *smap) +{ +return IS_ENABLED (CONFIG_64BIT ) && smap->inline_off; +} + +/* The inline slot of smap, or NULL if local_storage does not hold it */ +static __always_inline struct bpf_local_storage_data * +bpf_local_storage_inline (struct bpf_local_storage *local_storage, +struct bpf_local_storage_map *smap) +{ +if (!bpf_local_storage_map_inline (smap) || + smap->inline_end > local_storage->obj_size) +return NULL ; +return (void *)local_storage + smap->inline_off; +} + +static inline u64 bpf_local_storage_inline_tag (const struct bpf_local_storage_map *smap) +{ +return (u64 )~smap->inline_claim <<32 | smap->inline_claim; +} + +/* The slot holds a value of smap */ +static __always_inline bool +bpf_local_storage_inline_valid (struct bpf_local_storage_data *sdata, +const struct bpf_local_storage_map *smap) +{ +/* Pairs with the tag release in an update: see the value */ +return smp_load_acquire (&sdata->inline_tag) == +bpf_local_storage_inline_tag (smap); +} +/* If cacheit_lockit is false, this lookup function is lockless */ static inline struct bpf_local_storage_data * bpf_local_storage_lookup(struct bpf_local_storage *local_storage,
@@ -204,6 +251,15 @@ bpf_local_storage_lookup(struct bpf_local_storage *local_storage,struct bpf_local_storage_data *sdata;struct bpf_local_storage_elem *selem; +/* +* An object that holds the slot of smap never has an selem of smap: +* the object size does not change, and a create puts the value in +* the slot. +*/ + sdata =bpf_local_storage_inline (local_storage, smap); +if (sdata) +return bpf_local_storage_inline_valid (sdata, smap) ? sdata :NULL ; +/* Fast path (cache hit) */ sdata =rcu_dereference_check (local_storage->cache[smap->cache_idx],bpf_rcu_lock_held ());
@@ -251,10 +307,11 @@ bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner, void *value,void bpf_selem_free (struct bpf_local_storage_elem *selem,bool reuse_now); -int +struct bpf_local_storage *bpf_local_storage_alloc (void *owner,struct bpf_local_storage_map *smap, -struct bpf_local_storage_elem *first_selem); +struct bpf_local_storage_elem *first_selem, +void *value);struct bpf_local_storage_data * bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
kernel/bpf/bpf_local_storage.c +116 -13
202 lines not on this page · raw message
net/core/bpf_sk_storage.c +4 -5
@@ -201,17 +201,16 @@ int bpf_sk_storage_clone(const struct sock *sk, struct sock *newsk) }bpf_selem_link_storage_nolock (new_sk_storage, copy_selem); }else { - ret =bpf_local_storage_alloc (newsk, smap, copy_selem); -if (ret) { + new_sk_storage =bpf_local_storage_alloc (newsk, smap, + copy_selem,NULL ); +if (IS_ERR (new_sk_storage)) { + ret =PTR_ERR (new_sk_storage);bpf_selem_free (copy_selem, true);atomic_sub (smap->elem_size, &newsk->sk_omem_alloc);bpf_map_put (map); goto out; } - - new_sk_storage = -rcu_dereference (copy_selem->local_storage); }bpf_map_put (map); }
Signature · 1 line
2.53.1-Meta
PATCH v2 8/13 bpf: Refuse the sk storage map iterator for BPF_F_INLINE_VALUE maps
In reply to Mykyta YatsenkoFrom: Mykyta Yatsenko <[email protected]>
Inline values are not linked to the map, so the iterator would miss them.
--- net/core/bpf_sk_storage.c | 6 ++++++ 1 file changed, 6 insertions(+)
net/core/bpf_sk_storage.c +6 -0
@@ -938,6 +938,12 @@ static int bpf_iter_attach_map(struct bpf_prog *prog,if (map->map_type !=BPF_MAP_TYPE_SK_STORAGE ) goto put_map; +/* The iterator walks the selems of the map: it cannot see inline values */ +if (map->map_flags &BPF_F_INLINE_VALUE ) { + err = -EOPNOTSUPP ; + goto put_map; + } + aux->map = map; err =bpf_iter_map_check_prog (prog, aux);if (err)
Signature · 1 line
2.53.1-Meta
PATCH v2 9/13 bpf: Show inline values in sk diag
In reply to Mykyta YatsenkoFrom: Mykyta Yatsenko <[email protected]>
Inline values have no selem, and the list of an object can be empty when all its values are inline. A request that names maps finds them with the lookup, so it must not stop at an empty list. A dump of all maps walks the list only: let it also walk the table of live inline maps. Both walks skip a map that goes away (id 0): user space cannot name it.
--- net/core/bpf_sk_storage.c | 48 ++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-)
net/core/bpf_sk_storage.c +41 -7
@@ -529,7 +529,17 @@ bpf_sk_storage_diag_alloc(const struct nlattr *nla_stgs) }EXPORT_SYMBOL_GPL (bpf_sk_storage_diag_alloc); -static int diag_get (struct bpf_local_storage_map *smap, +/* +* Map free can set the map id to 0 at any time, so read it once. User space +* cannot name a map with id 0: skip it. +*/ +static u32 diag_map_id (struct bpf_local_storage_map *smap) +{ +return smap ?READ_ONCE (smap->map.id) :0 ; +} + +/* id is the map id that the caller read once */ +static int diag_get (struct bpf_local_storage_map *smap,u32 id,struct bpf_local_storage_data *sdata,struct sk_buff *skb) {struct nlattr *nla_stg, *nla_value;
@@ -541,7 +551,7 @@ static int diag_get(struct bpf_local_storage_map *smap,if (!nla_stg)return -EMSGSIZE ; -if (nla_put_u32 (skb,SK_DIAG_BPF_STORAGE_MAP_ID , smap->map.id)) +if (nla_put_u32 (skb,SK_DIAG_BPF_STORAGE_MAP_ID , id)) goto errout; nla_value = nla_reserve_64bit(skb, SK_DIAG_BPF_STORAGE_MAP_VALUE,
@@ -572,16 +582,19 @@ static int bpf_sk_storage_diag_put_all(struct sock *sk, struct sk_buff *skb,/* stg_array_type (e.g. INET_DIAG_BPF_SK_STORAGES) */ unsigned int diag_size =nla_total_size (0 );struct bpf_local_storage *sk_storage; +struct bpf_local_storage_data *sdata;struct bpf_local_storage_elem *selem;struct bpf_local_storage_map *smap;struct nlattr *nla_stgs;unsigned int saved_len; +u32 i, id, nr;int err =0 ;rcu_read_lock (); sk_storage =rcu_dereference (sk->sk_bpf_storage); -if (!sk_storage ||hlist_empty (&sk_storage->list)) { +/* The list can be empty when the values are inline */ +if (!sk_storage) {rcu_read_unlock ();return 0 ; }
@@ -594,15 +607,34 @@ static int bpf_sk_storage_diag_put_all(struct sock *sk, struct sk_buff *skb, saved_len = skb->len;hlist_for_each_entry_rcu (selem, &sk_storage->list, snode) { smap =rcu_dereference (SDATA (selem)->smap); -if (!smap) + id =diag_map_id (smap); +if (!id)continue ; diag_size +=nla_value_size (smap->map.value_size); -if (nla_stgs &&diag_get (smap,SDATA (selem), skb)) +if (nla_stgs &&diag_get (smap, id,SDATA (selem), skb))/* Continue to learn diag_size */ err = -EMSGSIZE ; } +/* +* Inline values have no selem: find them through the inline maps. A +* plain object holds no slot. +*/ + nr =bpf_local_storage_has_inline (sk_storage) ? +BPF_LOCAL_STORAGE_INLINE_MAPS :0 ; +for (i =0 ; i < nr; i++) { + smap =rcu_dereference (sk_cache.inline_maps[i]); + id =diag_map_id (smap); + sdata = id ?bpf_local_storage_inline (sk_storage, smap) :NULL ; +if (!sdata || !bpf_local_storage_inline_valid (sdata, smap)) +continue ; + diag_size +=nla_value_size (smap->map.value_size); + +if (nla_stgs &&diag_get (smap, id, sdata, skb)) + err = -EMSGSIZE ; + } +rcu_read_unlock ();if (nla_stgs) {
@@ -644,7 +676,8 @@ int bpf_sk_storage_diag_put(struct bpf_sk_storage_diag *diag,rcu_read_lock (); sk_storage =rcu_dereference (sk->sk_bpf_storage); -if (!sk_storage ||hlist_empty (&sk_storage->list)) { +/* The list can be empty when the values are inline */ +if (!sk_storage) {rcu_read_unlock ();return 0 ; }
@@ -665,7 +698,8 @@ int bpf_sk_storage_diag_put(struct bpf_sk_storage_diag *diag, diag_size +=nla_value_size (diag->maps[i]->value_size); -if (nla_stgs &&diag_get ((struct bpf_local_storage_map *)diag->maps[i], sdata, skb)) +if (nla_stgs &&diag_get ((struct bpf_local_storage_map *)diag->maps[i], + diag->maps[i]->id, sdata, skb))/* Continue to learn diag_size */ err = -EMSGSIZE ; }
Signature · 1 line
2.53.1-Meta
PATCH v2 10/13 bpf: Allow BPF_F_INLINE_VALUE for local storage maps
In reply to Mykyta YatsenkoFrom: Mykyta Yatsenko <[email protected]>
A lookup of a local storage map reads the selem that holds the value. When the owner is cold, this read can miss on each packet. Let sk, task, inode and cgroup storage maps keep their values in the storage object instead, so that a lookup has one dependent load less.
Map create fails with -E2BIG if the value never fits in the inline area, with -ENOSPC if the area has no room now or the owner type has 16 inline maps, with -EINVAL with BPF_F_CLONE or special fields, and with -EOPNOTSUPP on 32-bit. Documentation/bpf/map_sk_storage.rst gives the rules and costs.
--- include/linux/bpf_local_storage.h | 3 --- include/uapi/linux/bpf.h | 7 +++++++ kernel/bpf/bpf_local_storage.c | 19 ++++++++++++++++++- tools/include/uapi/linux/bpf.h | 7 +++++++ 4 files changed, 32 insertions(+), 4 deletions(-)
include/linux/bpf_local_storage.h +0 -3
@@ -172,9 +172,6 @@ bpf_local_storage_unpin_owner(struct bpf_local_storage *local_storage)*/ #define BPF_LOCAL_STORAGE_INLINE_MAPS 16 -/* A later patch adds this flag to the UAPI and allows it */ -#define BPF_F_INLINE_VALUE BIT(20) -struct bpf_local_storage_cache {spinlock_t idx_lock;u64 idx_usage_counts[BPF_LOCAL_STORAGE_CACHE_SIZE ];
include/uapi/linux/bpf.h +7 -0
@@ -1513,6 +1513,13 @@ enum {/* Enable BPF ringbuf overwrite mode */ BPF_F_RB_OVERWRITE = (1U <<19 ), + +/* +* Local storage maps: keep the value in the storage object of the owner. +* Readers take no lock, so they can see torn values. 64-bit only. The rules +* and errors are in Documentation/bpf/map_sk_storage.rst. +*/ + BPF_F_INLINE_VALUE = (1U << 20), };/* Flags for BPF_PROG_QUERY. */
kernel/bpf/bpf_local_storage.c +18 -1
@@ -15,7 +15,8 @@#include <linux/rcupdate_trace.h> #include <linux/rcupdate_wait.h> -#define BPF_LOCAL_STORAGE_CREATE_FLAG_MASK (BPF_F_NO_PREALLOC | BPF_F_CLONE) +#define BPF_LOCAL_STORAGE_CREATE_FLAG_MASK \ + (BPF_F_NO_PREALLOC | BPF_F_CLONE | BPF_F_INLINE_VALUE)static struct bpf_local_storage_map_bucket * select_bucket(struct bpf_local_storage_map *smap,
@@ -1010,15 +1011,27 @@ int bpf_local_storage_map_alloc_check(union bpf_attr *attr) {if (attr->map_flags & ~BPF_LOCAL_STORAGE_CREATE_FLAG_MASK || !(attr->map_flags &BPF_F_NO_PREALLOC ) || + ((attr->map_flags &BPF_F_CLONE ) && + (attr->map_flags &BPF_F_INLINE_VALUE )) || attr->max_entries || attr->key_size !=sizeof (int ) || !attr->value_size ||/* Enforce BTF for userspace sk dumping */ !attr->btf_key_type_id || !attr->btf_value_type_id)return -EINVAL ; +/* The slot tag needs an 8 byte acquire and release */ +if (!IS_ENABLED (CONFIG_64BIT ) && (attr->map_flags &BPF_F_INLINE_VALUE )) +return -EOPNOTSUPP ; +if (attr->value_size >BPF_LOCAL_STORAGE_MAX_VALUE_SIZE )return -E2BIG ; +/* Never fits, also in an empty area: -ENOSPC is only for no room now */ +if ((attr->map_flags &BPF_F_INLINE_VALUE ) && +bpf_local_storage_slot_size (attr->value_size) > +BPF_LOCAL_STORAGE_INLINE_MAX -BPF_LOCAL_STORAGE_INLINE_START ) +return -E2BIG ; +return 0 ; }
@@ -1030,6 +1043,10 @@ int bpf_local_storage_map_check_btf(struct bpf_map *map,if (!btf_type_is_i32 (key_type))return -EINVAL ; +/* An inline value is copied in place and is not freed with a delete */ +if ((map->map_flags &BPF_F_INLINE_VALUE ) && !IS_ERR_OR_NULL (map->record)) +return -EINVAL ; +return 0 ; }
tools/include/uapi/linux/bpf.h +7 -0
@@ -1513,6 +1513,13 @@ enum {/* Enable BPF ringbuf overwrite mode */ BPF_F_RB_OVERWRITE = (1U <<19 ), + +/* +* Local storage maps: keep the value in the storage object of the owner. +* Readers take no lock, so they can see torn values. 64-bit only. The rules +* and errors are in Documentation/bpf/map_sk_storage.rst. +*/ + BPF_F_INLINE_VALUE = (1U << 20), };/* Flags for BPF_PROG_QUERY. */
Signature · 1 line
2.53.1-Meta
PATCH v2 11/13 libbpf: Do not retry local storage map create without BTF
In reply to Mykyta YatsenkoFrom: Mykyta Yatsenko <[email protected]>
Local storage maps need BTF, so the retry without BTF always fails with -EINVAL. That hides the real error, for example -ENOSPC, -E2BIG or -EOPNOTSUPP for a BPF_F_INLINE_VALUE map, and the application cannot see when to create the map without the flag. Return the first error.
--- tools/lib/bpf/libbpf.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-)
tools/lib/bpf/libbpf.c +16 -1
@@ -5531,6 +5531,20 @@ bpf_object__populate_internal_map(struct bpf_object *obj, struct bpf_map *map)static void bpf_map__destroy (struct bpf_map *map); +/* The kernel refuses these map types without BTF */ +static bool map_type_needs_btf (enum bpf_map_type type) +{ +switch (type) { +case BPF_MAP_TYPE_SK_STORAGE : +case BPF_MAP_TYPE_INODE_STORAGE : +case BPF_MAP_TYPE_TASK_STORAGE : +case BPF_MAP_TYPE_CGRP_STORAGE : +return true; +default : +return false; + } +} +static int bpf_object__create_map (struct bpf_object *obj,struct bpf_map *map,bool is_inner) {LIBBPF_OPTS (bpf_map_create_opts, create_attr);
@@ -5631,7 +5645,8 @@ static int bpf_object__create_map(struct bpf_object *obj, struct bpf_map *map, b def->key_size, def->value_size, def->max_entries, &create_attr); } -if (map_fd <0 && (create_attr.btf_key_type_id || create_attr.btf_value_type_id)) { +if (map_fd <0 && (create_attr.btf_key_type_id || create_attr.btf_value_type_id) && + !map_type_needs_btf (def->type)) { err = -errno;pr_warn ("Error in bpf_create_map_xattr(%s): %s. Retrying without BTF.\n" , map->name,errstr (err));
Signature · 1 line
2.53.1-Meta
PATCH v2 12/13 selftests/bpf: Test BPF_F_INLINE_VALUE local storage maps
In reply to Mykyta YatsenkoFrom: Mykyta Yatsenko <[email protected]>
Inline maps keep values in a slot of the storage object and have new limits. Make sure that they behave as normal maps and that the limits hold.
Test that values in the slot are the same through the syscall and a program for sk storage, and through a program for task storage, also after a delete in a program. The program records where each value is, so the test proves that the slot path runs. Test the map limits and errors:-E2BIG for a value that never fits, also through libbpf, -ENOSPC for a full area and after 16 maps, and the undo of a slot claim. Test the fallback for an object with no room for the slot. Test that a new map at the offset of a freed map sees no old value.
--- .../bpf/prog_tests/local_storage_inline.c | 583 +++++++++++++++++++++ .../selftests/bpf/progs/local_storage_inline.c | 123 +++++ 2 files changed, 706 insertions(+)
tools/testing/selftests/bpf/prog_tests/local_storage_inline.c new file +583 -0
@@ -0,0 +1,583 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */ + +#include <netinet/in.h> +#include <pthread.h> +#include <sys/socket.h> +#include <test_progs.h> +#include <bpf/btf.h> +#include "task_local_storage_helpers.h" + +/* The same as in progs/local_storage_inline.c */ +#define LS_VAL_WORDS 4 +#define NR_MAPS 4 +/* The sk maps come first. The syscall needs a key: the test has a socket fd */ +#define NR_SK_MAPS 2 + +struct ls_val { +__u64 w[LS_VAL_WORDS ]; +}; + +#include "local_storage_inline.skel.h" + +#define INLINE_FLAGS (BPF_F_NO_PREALLOC | BPF_F_INLINE_VALUE) +/* All inline maps of one owner type share an area that ends at 1 KB */ +#define AREA_END 1024 +#define MAX_INLINE_MAPS 16 +#define NR_SOCKS 2 +/* +* Map free is deferred: the slot of a closed map can stay taken for a time. +* All waits share one time budget, so that the test fails fast after the +* first timeout. +*/ +#define WAIT_BUDGET_NS (30ULL * 1000 * 1000 * 1000) +#define WAIT_US 10000 + +static __u64 wait_end; + +/* Sleep a little and return true, or return false if the budget is spent */ +static bool wait_more (void ) +{ +if (get_time_ns () >= wait_end) +return false; +usleep (WAIT_US ); +return true; +} + +/* A create that must fail with err. Close the map if it worked */ +static void assert_err (int fd,int err,const char *name) +{ +if (!ASSERT_EQ (fd, err, name) && fd >=0 ) +close (fd); +} + +/* A create that must work. The test needs only the result */ +static void assert_fd_close (int fd,const char *name) +{ +if (ASSERT_OK_FD (fd, name)) +close (fd); +} + +static void close_fds (const int *fds,int n) +{ +int i; + +for (i =0 ; i < n; i++) +if (fds[i] >=0 ) +close (fds[i]); +} + +struct socks { +int fd[NR_SOCKS ]; +bool ok;/* All sockets are open */ +}; + +static struct socks open_socks (void ) +{ +struct socks s = { .ok = true }; +int i; + +for (i =0 ; i <NR_SOCKS ; i++) { + s.fd[i] =socket (AF_INET ,SOCK_STREAM ,0 ); +if (!ASSERT_OK_FD (s.fd[i],"socket" )) + s.ok = false; + } +return s; +} + +/* BTF with an int key (id 1) and, as the last type, a value of value_size bytes */ +static struct btf *load_btf (__u32 value_size) +{ +struct btf *btf; + + btf =btf__new_empty (); +if (!btf) +return NULL ; +btf__add_int (btf,"int" ,4 ,BTF_INT_SIGNED ); +btf__add_int (btf,"char" ,1 ,0 ); +btf__add_array (btf,1 ,2 , value_size); +if (btf__load_into_kernel (btf)) { +btf__free (btf); +return NULL ; + } +return btf; +} + +static int map_create_btf (struct btf *btf,enum bpf_map_type type,__u32 flags, +__u32 value_size) +{ + LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = flags, + .btf_fd = btf__fd(btf), .btf_key_type_id = 1, + .btf_value_type_id = btf__type_cnt(btf) - 1); + +return bpf_map_create (type,"ls_inline" ,sizeof (int ), value_size,0 , &opts); +} + +/* +* Make a local storage map with an int key. With retry, wait while the slots +* of closed maps are not free. A create that must fail does not retry. +*/ +static int create_map (enum bpf_map_type type,__u32 flags,__u32 value_size, +bool retry) +{ +struct btf *btf =load_btf (value_size); +int fd; + +if (!ASSERT_OK_PTR (btf,"load_btf" )) +return -ENOMEM ; + +do { + fd =map_create_btf (btf, type, flags, value_size); + }while (retry && fd == -ENOSPC &&wait_more ()); +btf__free (btf); +return fd; +} + +/* +* The largest inline value. The area starts after struct bpf_local_storage, +* aligned to 8 B. A slot is an 8 B tag and the value, aligned to 8 B. +*/ +static int read_value_max (void ) +{ +const struct btf_type *t; +struct btf *btf; +int id, max =-1 ; + + btf =btf__load_vmlinux_btf (); +if (!ASSERT_OK_PTR (btf,"btf__load_vmlinux_btf" )) +return -1 ; + id =btf__find_by_name_kind (btf,"bpf_local_storage" ,BTF_KIND_STRUCT ); +if (id >0 ) { + t =btf__type_by_id (btf, id); + max =AREA_END - (t->size +7 ) /8 *8 -8 ; + } +btf__free (btf); +return max; +} + +static void fill_val (struct ls_val *v,__u64 first) +{ +int i; + +for (i =0 ; i <LS_VAL_WORDS ; i++) + v->w[i] = first + i; +} + +/* Run the program on sk and on the current task. Return the number of NULL gets */ +static int run_prog (struct local_storage_inline *skel,int sk,__u64 get_flags) +{ +socklen_t len =sizeof (int ); +int ttl; + + skel->bss->get_flags = get_flags; + skel->bss->nr_null =0 ; +ASSERT_OK (getsockopt (sk,SOL_IP ,IP_TTL , &ttl, &len),"getsockopt" ); +return skel->bss->nr_null; +} + +/* The value that get i saw is in the storage object of its owner: a slot */ +static bool in_obj (struct local_storage_inline *skel,int i) +{ +__u64 off = skel->bss->val_addr[i] - skel->bss->obj_addr[i]; + +return skel->bss->obj_addr[i] && off < skel->bss->obj_size[i]; +} + +/* The program and the syscall must see the same value in the sk maps */ +static void check_values (struct local_storage_inline *skel,const int *fds,int sk, +const struct ls_val *v) +{ +struct ls_val out; +int i; + +for (i =0 ; i <NR_SK_MAPS ; i++) { +ASSERT_MEMEQ (&skel->bss->seen_val[i], v,sizeof (*v),"seen" ); +ASSERT_OK (bpf_map_lookup_elem (fds[i], &sk, &out),"lookup" ); +ASSERT_MEMEQ (&out, v,sizeof (*v),"lookup value" ); + } +} + +/* The program must see v in the task maps */ +static void check_task_values (struct local_storage_inline *skel,const struct ls_val *v) +{ +int i; + +for (i =NR_SK_MAPS ; i <NR_MAPS ; i++) +ASSERT_MEMEQ (&skel->bss->seen_val[i], v,sizeof (*v),"task seen" ); +} + +enum {OP_UPDATE ,OP_DELETE ,OP_LOOKUP }; + +/* All maps get the same ops: an inline map must give the codes of a normal map */ +static void check_ops (int fd,int key) +{ +static const struct { +int op; +__u64 flags; +int ret; + } ops[] = { + {OP_DELETE ,0 ,0 }, + {OP_DELETE ,0 , -ENOENT }, + {OP_LOOKUP ,0 , -ENOENT }, + {OP_UPDATE ,BPF_EXIST , -ENOENT }, + {OP_UPDATE ,BPF_NOEXIST ,0 }, + {OP_UPDATE ,BPF_NOEXIST , -EEXIST }, + {OP_UPDATE ,BPF_F_LOCK , -EINVAL }, + {OP_UPDATE ,BPF_ANY ,0 }, + {OP_DELETE ,0 ,0 }, + {OP_UPDATE ,BPF_ANY ,0 }, + {OP_UPDATE ,BPF_EXIST ,0 }, + {OP_LOOKUP ,0 ,0 }, + }; +struct ls_val v, out; +char name[16 ]; +int i, ret; + +for (i =0 ; i <ARRAY_SIZE (ops); i++) { +fill_val (&v,0x4000 + i *0x10 ); +if (ops[i].op ==OP_UPDATE ) + ret =bpf_map_update_elem (fd, &key, &v, ops[i].flags); +else if (ops[i].op ==OP_DELETE ) + ret =bpf_map_delete_elem (fd, &key); +else + ret =bpf_map_lookup_elem (fd, &key, &out); +snprintf (name,sizeof (name),"op %d" , i); +ASSERT_EQ (ret, ops[i].ret, name); + } +/* The last op reads the value of the update before it */ +fill_val (&v,0x4000 + (i -2 ) *0x10 ); +ASSERT_MEMEQ (&out, &v,sizeof (v),"last value" ); +} + +/* Runs in a new thread: the task is the owner of the task values */ +static void test_values (int max) +{ +struct local_storage_inline *skel; +int fds[NR_SK_MAPS ], i, j, sk =-1 ; +struct ls_val v, zero = {}; + + skel =local_storage_inline__open (); +if (!ASSERT_OK_PTR (skel,"skel open" )) +return ; +if (!ASSERT_OK (bpf_map__set_map_flags (skel->maps.sk_inline,INLINE_FLAGS ),"flags" ) || + !ASSERT_OK (bpf_map__set_map_flags (skel->maps.task_inline,INLINE_FLAGS ),"flags" ) || + !ASSERT_OK (local_storage_inline__load (skel),"skel load" ) || + !ASSERT_OK (local_storage_inline__attach (skel),"attach" )) + goto out; + sk =socket (AF_INET ,SOCK_STREAM ,0 ); +if (!ASSERT_OK_FD (sk,"socket" )) + goto out; + +/* The order of the maps in the program */ + fds[0 ] =bpf_map__fd (skel->maps.sk_inline); + fds[1 ] =bpf_map__fd (skel->maps.sk_normal); + skel->bss->target_tid =sys_gettid (); + +fill_val (&skel->bss->init_val,0x1000 ); + skel->bss->use_init = true; +ASSERT_EQ (run_prog (skel, sk,BPF_LOCAL_STORAGE_GET_F_CREATE ),0 , +"create with init" ); +check_values (skel, fds, sk, &skel->bss->init_val); +check_task_values (skel, &skel->bss->init_val); +/* The program finds inline values in a slot, normal values elsewhere */ +for (i =0 ; i <NR_MAPS ; i++) +ASSERT_EQ (in_obj (skel, i), !(i &1 ),"value in object" ); + +/* The program sees a syscall update, and the syscall a program write */ +fill_val (&v,0x2000 ); +for (i =0 ; i <NR_SK_MAPS ; i++) +ASSERT_OK (bpf_map_update_elem (fds[i], &sk, &v,BPF_EXIST ),"update" ); + skel->bss->val_add =0x100 ; +ASSERT_EQ (run_prog (skel, sk,0 ),0 ,"get" ); +fill_val (&v,0x2100 ); +check_values (skel, fds, sk, &v); +fill_val (&v,0x1100 ); +check_task_values (skel, &v); + +/* +* A delete in the program must give the codes of a normal map. A slot +* keeps its bytes after a delete, but they must not show. +*/ + skel->bss->do_delete = true; +for (i =0 ; i <2 ; i++) { +ASSERT_EQ (run_prog (skel, sk,0 ), i ?NR_MAPS :0 ,"get before delete" ); +for (j =0 ; j <NR_MAPS ; j++) +ASSERT_EQ (skel->bss->del_ret[j], i ? -ENOENT :0 ,"prog delete" ); + } + skel->bss->do_delete = false; + skel->bss->use_init = false; + skel->bss->val_add =0 ; +ASSERT_EQ (run_prog (skel, sk,BPF_LOCAL_STORAGE_GET_F_CREATE ),0 ,"create" ); +check_values (skel, fds, sk, &zero); +check_task_values (skel, &zero); + + skel->bss->target_tid =0 ; +for (i =0 ; i <NR_SK_MAPS ; i++) +check_ops (fds[i], sk); +out: +if (sk >=0 ) +close (sk); +local_storage_inline__destroy (skel); +} + +/* +* A value that never fits gets -E2BIG at once, with no retry. The biggest +* value takes the full area: retry until the maps of earlier subtests are +* freed. +*/ +static void check_biggest (int max) +{ +int big; + +assert_err (create_map (BPF_MAP_TYPE_TASK_STORAGE ,INLINE_FLAGS , max +1 , false), + -E2BIG ,"value too big" ); + big =create_map (BPF_MAP_TYPE_TASK_STORAGE ,INLINE_FLAGS , max, true); +if (!ASSERT_OK_FD (big,"biggest value" )) +return ; +/* No room now: the biggest value takes the full area */ +assert_err (create_map (BPF_MAP_TYPE_TASK_STORAGE ,INLINE_FLAGS ,8 , false), + -ENOSPC ,"no room now" ); +close (big); +assert_fd_close (create_map (BPF_MAP_TYPE_TASK_STORAGE ,INLINE_FLAGS ,8 , true), +"room after free" ); +} + +static const char excl_hash[32 ]; + +/* A create that fails after the slot claim gives the slot back */ +static void check_claim_undo (void ) +{ + LIBBPF_OPTS(bpf_map_create_opts, opts, .map_flags = INLINE_FLAGS, + .btf_key_type_id = 1, .excl_prog_hash = excl_hash, + .excl_prog_hash_size = 1); +struct btf *btf =load_btf (8 ); +int i, fd = -EINVAL ; + +if (!ASSERT_OK_PTR (btf,"load_btf" )) +return ; + opts.btf_fd =btf__fd (btf); + opts.btf_value_type_id =btf__type_cnt (btf) -1 ; +/* More failures than table entries: each must give its entry back */ +for (i =0 ; i <=MAX_INLINE_MAPS && fd == -EINVAL ; i++) + fd =bpf_map_create (BPF_MAP_TYPE_TASK_STORAGE ,"ls_inline" , +sizeof (int ),8 ,0 , &opts); +assert_err (fd, -EINVAL ,"bad excl_prog_hash_size" ); +btf__free (btf); +} + +/* libbpf gives the error of the kernel, not -EINVAL of a retry without BTF */ +static void check_load_err (int max) +{ +struct local_storage_inline *skel; + + skel =local_storage_inline__open (); +if (!ASSERT_OK_PTR (skel,"skel open" )) +return ; +if (ASSERT_OK (bpf_map__set_map_flags (skel->maps.task_inline,INLINE_FLAGS ),"flags" ) && +ASSERT_OK (bpf_map__set_value_size (skel->maps.task_inline, max +1 ),"value size" )) +ASSERT_EQ (local_storage_inline__load (skel), -E2BIG ,"skeleton load" ); +local_storage_inline__destroy (skel); +} + +static void test_limits (int max) +{ +int fds[MAX_INLINE_MAPS ], i; + +check_biggest (max); +check_load_err (max); + +for (i =0 ; i <MAX_INLINE_MAPS ; i++) { + fds[i] =create_map (BPF_MAP_TYPE_TASK_STORAGE ,INLINE_FLAGS ,8 , true); +if (!ASSERT_OK_FD (fds[i],"inline map" )) +break ; +/* The first map proves room: the failed creates need no retry */ +if (!i) +check_claim_undo (); + } +if (i ==MAX_INLINE_MAPS ) { +/* The area has room, but the owner type has 16 inline maps */ +assert_err (create_map (BPF_MAP_TYPE_TASK_STORAGE ,INLINE_FLAGS ,8 , false), + -ENOSPC ,"too many maps" ); +/* Each owner type has its own limit */ +assert_fd_close (create_map (BPF_MAP_TYPE_SK_STORAGE ,INLINE_FLAGS ,8 , true), +"other owner type" ); + } +close_fds (fds, i); +} + +/* Load the program with map_fd in place of sk_reuse, and attach it */ +static struct local_storage_inline *load_reuse (int map_fd) +{ +struct local_storage_inline *skel; + + skel =local_storage_inline__open (); +if (!ASSERT_OK_PTR (skel,"skel open" )) +return NULL ; +if (!ASSERT_OK (bpf_map__reuse_fd (skel->maps.sk_reuse, map_fd),"reuse_fd" ) || + !ASSERT_OK (local_storage_inline__load (skel),"skel load" ) || + !ASSERT_OK (local_storage_inline__attach (skel),"attach" )) { +local_storage_inline__destroy (skel); +return NULL ; + } + skel->bss->target_tid =sys_gettid (); + skel->bss->reuse_test = true; +return skel; +} + +static void test_fallback (int max) +{ +struct socks s =open_socks (); +struct local_storage_inline *skel; +int normal, map =-1 , i; +char val[AREA_END ], out[AREA_END ]; + + normal =create_map (BPF_MAP_TYPE_SK_STORAGE ,BPF_F_NO_PREALLOC ,4 , false); +if (!s.ok || !ASSERT_OK_FD (normal,"normal map" )) + goto out; + +/* sk 0 gets its storage before the inline map exists, sk 1 after */ +ASSERT_OK (bpf_map_update_elem (normal, &s.fd[0 ], &s.fd[0 ],0 ),"normal update" ); +/* The slot takes the full area, so the storage of sk 0 has no room */ + map =create_map (BPF_MAP_TYPE_SK_STORAGE ,INLINE_FLAGS , max, true); +if (!ASSERT_OK_FD (map,"inline map" )) + goto out; + +for (i =0 ; i <2 ; i++) { +memset (val,0x5a + i, max); +ASSERT_OK (bpf_map_update_elem (map, &s.fd[i], val,BPF_NOEXIST ),"update" ); + } +/* sk 0 keeps the value in a normal element, sk 1 in the slot */ + skel =load_reuse (map); +for (i =0 ; skel && i <2 ; i++) { +ASSERT_EQ (run_prog (skel, s.fd[i],0 ),0 ,"prog get" ); +ASSERT_EQ (in_obj (skel,0 ), i,"value in object" ); + } +local_storage_inline__destroy (skel); +for (i =0 ; i <2 ; i++) { +memset (val,0x5a + i, max); +ASSERT_OK (bpf_map_lookup_elem (map, &s.fd[i], out),"lookup" ); +ASSERT_MEMEQ (out, val, max,"lookup value" ); +ASSERT_OK (bpf_map_delete_elem (map, &s.fd[i]),"delete" ); + } +ASSERT_OK (bpf_map_lookup_elem (normal, &s.fd[0 ], out),"normal lookup" ); +ASSERT_EQ (*(int *)out, s.fd[0 ],"normal value" ); +out: +close_fds (s.fd,NR_SOCKS ); +if (normal >=0 ) +close (normal); +if (map >=0 ) +close (map); +} + +/* +* Map B gets the slot of the freed map A in a live object. The old tag and +* value bytes of A must not show as a value of B. +*/ +static void test_reuse (int max) +{ +struct local_storage_inline *skel =NULL ; +struct socks s =open_socks (); +int a, b =-1 , i; +char val[AREA_END ], out[AREA_END ]; +struct ls_val zero = {}; + +if (!s.ok) + goto out; +/* The biggest value: the slot of A and of B starts at the area start */ + a =create_map (BPF_MAP_TYPE_SK_STORAGE ,INLINE_FLAGS , max, true); +if (!ASSERT_OK_FD (a,"map A" )) + goto out; +memset (val,0x5a , max); +for (i =0 ; i <2 ; i++) +ASSERT_OK (bpf_map_update_elem (a, &s.fd[i], val,BPF_NOEXIST ),"update A" ); +close (a); + +/* B gets room only after the deferred free of A, with a new tag */ + b =create_map (BPF_MAP_TYPE_SK_STORAGE ,INLINE_FLAGS , max, true); +if (!ASSERT_OK_FD (b,"map B" )) + goto out; +for (i =0 ; i <2 ; i++) +ASSERT_EQ (bpf_map_lookup_elem (b, &s.fd[i], out), -ENOENT ,"lookup B" ); + +memset (val,0xa5 , max); +ASSERT_OK (bpf_map_update_elem (b, &s.fd[0 ], val,BPF_NOEXIST ),"syscall create" ); +ASSERT_OK (bpf_map_lookup_elem (b, &s.fd[0 ], out),"lookup syscall value" ); +ASSERT_MEMEQ (out, val, max,"syscall value" ); + + skel =load_reuse (b); +if (!skel) + goto out; +ASSERT_EQ (run_prog (skel, s.fd[1 ],BPF_LOCAL_STORAGE_GET_F_CREATE ),0 , +"prog create" ); +ASSERT_MEMEQ (&skel->bss->seen_val[0 ], &zero,sizeof (zero),"prog value" ); +memset (val,0 , max); +ASSERT_OK (bpf_map_lookup_elem (b, &s.fd[1 ], out),"lookup prog value" ); +ASSERT_MEMEQ (out, val, max,"prog value zero" ); +out: +local_storage_inline__destroy (skel); +close_fds (s.fd,NR_SOCKS ); +if (b >=0 ) +close (b); +} + +struct subtest { +const char *name; +void (*fn)(int max); +bool thread;/* Task storage: a new task has a new storage object */ +}; + +struct thread_arg { +const struct subtest *t; +int max; +}; + +static void *subtest_thread (void *arg) +{ +struct thread_arg *a = arg; + + a->t->fn (a->max); +return NULL ; +} + +static void run_subtest (const struct subtest *t,int max) +{ +struct thread_arg a = { t, max }; +pthread_t thread; + +if (!t->thread) { + t->fn (max); +return ; + } +if (ASSERT_OK (pthread_create (&thread,NULL , subtest_thread, &a),"pthread_create" )) +pthread_join (thread,NULL ); +} + +/* Serial: all inline maps of an owner type share one area and one limit */ +void serial_test_local_storage_inline (void ) +{ +static const struct subtest tests[] = { + {"values" , test_values, true }, + {"limits" , test_limits }, + {"fallback" , test_fallback }, + {"reuse" , test_reuse }, + }; +int fd, i, max; + + wait_end =get_time_ns () +WAIT_BUDGET_NS ; +/* 32-bit kernels refuse the flag */ + fd =create_map (BPF_MAP_TYPE_SK_STORAGE ,INLINE_FLAGS ,8 , true); +if (fd == -EOPNOTSUPP ) { +test__skip (); +return ; + } +assert_fd_close (fd,"inline map" ); + max =read_value_max (); +if (!ASSERT_GT (max,0 ,"inline value max" )) +return ; + +for (i =0 ; i <ARRAY_SIZE (tests); i++) { +if (!test__start_subtest (tests[i].name)) +continue ; +run_subtest (&tests[i], max); + } +}
tools/testing/selftests/bpf/progs/local_storage_inline.c new file +123 -0
123 lines not on this page · raw message
Signature · 1 line
2.53.1-Meta
PATCH v2 13/13 bpf, docs: Document BPF_F_INLINE_VALUE for local storage maps
In reply to Mykyta YatsenkoFrom: Mykyta Yatsenko <[email protected]>
Users need the rules of the new flag, its costs and the -ENOSPC fallback. Task and inode storage have no docs, so the sk storage doc gives the rules for all owner types.
--- Documentation/bpf/map_cgrp_storage.rst | 3 + Documentation/bpf/map_sk_storage.rst | 103 ++++++++++++++++++++++++++++++++- 2 files changed, 105 insertions(+), 1 deletion(-)
Documentation/bpf/map_cgrp_storage.rst +3 -0
@@ -31,6 +31,9 @@ The local storage can be removed with ``bpf_cgrp_storage_delete``:: The map is available to all program types. +A map created with ``BPF_F_INLINE_VALUE`` keeps its value in the storage object +of the cgroup. Documentation/bpf/map_sk_storage.rst gives the rules. + Examples ========
Documentation/bpf/map_sk_storage.rst +102 -1
115 lines not on this page · raw message
Signature · 1 line
2.53.1-Meta