Generalizing bpf_local_storage
Subsystems (10)
v6 cover Generalizing bpf_local_storage
From: KP Singh <[email protected]>
# v5 -> v6
- Fixed a build warning.
- Rebase.
# v4 -> v5
- Split non-functional changes into separate commits.
- Updated the cache macros to be simpler.
- Fixed some bugs noticed by Martin.
- Updated the userspace map functions to use an fd for lookups, updates and deletes.
- Rebase.
# v3 -> v4
- Fixed a missing include to bpf_sk_storage.h in bpf_sk_storage.c
- Fixed some functions that were not marked as static which led to W=1 compilation warnings.
# v2 -> v3
* Restructured the code as per Martin's suggestions:
- Common functionality in bpf_local_storage.c
- bpf_sk_storage functionality remains in net/bpf_sk_storage.
- bpf_inode_storage is kept separate as it is enabled only with CONFIG_BPF_LSM.
* A separate cache for inode and sk storage with macros to define it.
* Use the ops style approach as suggested by Martin instead of the enum + switch style.
* Added the inode map to bpftool bash completion and docs.
* Rebase and indentation fixes.
# v1 -> v2
* Use the security blob pointer instead of dedicated member in struct inode.
* Better code re-use as suggested by Alexei.
* Dropped the inode count arithmetic as pointed out by Alexei.
* Minor bug fixes and rebase.
bpf_sk_storage can already be used by some BPF program types to annotate socket objects. These annotations are managed with the life-cycle of the object (i.e. freed when the object is freed) which makes BPF programs much simpler and less prone to errors and leaks.
This patch series:
* Generalizes the bpf_sk_storage infrastructure to allow easy implementation of local storage for other objects
* Implements local storage for inodes
* Makes both bpf_{sk, inode}_storage available to LSM programs.
Local storage is safe to use in LSM programs as the attachment sites are limited and the owning object won't be freed, however, this is not the case for tracing. Usage in tracing is expected to follow a white-list based approach similar to the d_path helper
(https://lore.kernel.org/bpf/[email protected]).
Access to local storage would allow LSM programs to implement stateful detections like detecting the unlink of a running executable from the examples shared as a part of the KRSI series
https://lore.kernel.org/bpf/[email protected]/
and
https://github.com/sinkap/linux-krsi/blob/patch/v1/examples/samples/bpf/lsm_detect_exec_unlink.c
KP Singh (7):
bpf: Renames to prepare for generalizing sk_storage.
bpf: Generalize caching for sk_storage.
bpf: Generalize bpf_sk_storage
bpf: Split bpf_local_storage to bpf_sk_storage
bpf: Implement bpf_local_storage for inodes
bpf: Allow local storage to be used from LSM programs
bpf: Add selftests for local_storage
include/linux/bpf.h | 13 + include/linux/bpf_local_storage.h | 175 ++++ include/linux/bpf_lsm.h | 21 + include/linux/bpf_types.h | 3 + include/net/bpf_sk_storage.h | 12 + include/net/sock.h | 4 +- include/uapi/linux/bpf.h | 54 +- kernel/bpf/Makefile | 2 + kernel/bpf/bpf_inode_storage.c | 353 ++++++++ kernel/bpf/bpf_local_storage.c | 519 ++++++++++++ kernel/bpf/bpf_lsm.c | 21 +- kernel/bpf/syscall.c | 3 +- kernel/bpf/verifier.c | 10 + net/core/bpf_sk_storage.c | 759 ++++-------------- security/bpf/hooks.c | 7 + .../bpf/bpftool/Documentation/bpftool-map.rst | 2 +- tools/bpf/bpftool/bash-completion/bpftool | 3 +- tools/bpf/bpftool/map.c | 3 +- tools/include/uapi/linux/bpf.h | 54 +- tools/lib/bpf/libbpf_probes.c | 5 +- .../bpf/prog_tests/test_local_storage.c | 60 ++ .../selftests/bpf/progs/local_storage.c | 136 ++++ 22 files changed, 1593 insertions(+), 626 deletions(-) create mode 100644 include/linux/bpf_local_storage.h create mode 100644 kernel/bpf/bpf_inode_storage.c create mode 100644 kernel/bpf/bpf_local_storage.c create mode 100644 tools/testing/selftests/bpf/prog_tests/test_local_storage.c create mode 100644 tools/testing/selftests/bpf/progs/local_storage.c
Signature · 1 line
2.28.0.rc0.105.gf9edc3c819-goog
PATCH v6 1/7 bpf: Renames to prepare for generalizing sk_storage.
From: KP Singh <[email protected]>
A purely mechanical change to split the renaming from the actual generalization.
Flags/consts:
SK_STORAGE_CREATE_FLAG_MASK BPF_LOCAL_STORAGE_CREATE_FLAG_MASK
BPF_SK_STORAGE_CACHE_SIZE BPF_LOCAL_STORAGE_CACHE_SIZE
MAX_VALUE_SIZE BPF_LOCAL_STORAGE_MAX_VALUE_SIZE
Structs:
bucket bpf_local_storage_map_bucket
bpf_sk_storage_map bpf_local_storage_map
bpf_sk_storage_data bpf_local_storage_data
bpf_sk_storage_elem bpf_local_storage_elem
bpf_sk_storage bpf_local_storage
selem_linked_to_sk selem_linked_to_storage
selem_alloc bpf_selem_alloc
The "sk" member in bpf_local_storage is also updated to "owner" in preparation for changing the type to void * in a subsequent patch.
Functions:
__selem_unlink_sk bpf_selem_unlink_storage
__selem_link_sk bpf_selem_link_storage
selem_unlink_sk __bpf_selem_unlink_storage
sk_storage_update bpf_local_storage_update
__sk_storage_lookup bpf_local_storage_lookup
bpf_sk_storage_map_free bpf_local_storage_map_free
bpf_sk_storage_map_alloc bpf_local_storage_map_alloc
bpf_sk_storage_map_alloc_check bpf_local_storage_map_alloc_check
bpf_sk_storage_map_check_btf bpf_local_storage_map_check_btf
--- include/net/sock.h | 4 +- net/core/bpf_sk_storage.c | 405 +++++++++++++++++++------------------- 2 files changed, 208 insertions(+), 201 deletions(-)
include/net/sock.h +2 -2
@@ -245,7 +245,7 @@ struct sock_common {/* public: */ }; -struct bpf_sk_storage ; +struct bpf_local_storage ;/** * struct sock - network layer representation of sockets
@@ -516,7 +516,7 @@ struct sock {void (*sk_destruct)(struct sock *sk);struct sock_reuseport __rcu *sk_reuseport_cb;#ifdef CONFIG_BPF_SYSCALL -struct bpf_sk_storage __rcu *sk_bpf_storage; +struct bpf_local_storage __rcu *sk_bpf_storage;#endif struct rcu_head sk_rcu; };
net/core/bpf_sk_storage.c +206 -199
846 lines not on this page · raw message
Signature · 1 line
2.28.0.rc0.105.gf9edc3c819-goog
Re: [PATCH bpf-next v6 1/7] bpf: Renames to prepare for generalizing sk_storage.
Quoted 26 lines · KP Singh
From: KP Singh <[email protected]>
A purely mechanical change to split the renaming from the actual generalization.
Flags/consts:
SK_STORAGE_CREATE_FLAG_MASKBPF_LOCAL_STORAGE_CREATE_FLAG_MASK
BPF_SK_STORAGE_CACHE_SIZEBPF_LOCAL_STORAGE_CACHE_SIZE
MAX_VALUE_SIZEBPF_LOCAL_STORAGE_MAX_VALUE_SIZEStructs:
bucket
bpf_local_storage_map_bucket
bpf_sk_storage_mapbpf_local_storage_map
bpf_sk_storage_databpf_local_storage_data
bpf_sk_storage_elembpf_local_storage_elem
bpf_sk_storagebpf_local_storage
selem_linked_to_skselem_linked_to_storage
selem_allocbpf_selem_allocThe "sk" member in
bpf_local_storageis also updated to "owner" in preparation for changing the type to void * in a subsequent patch.Functions:
Quoted 9 lines
__selem_unlink_skbpf_selem_unlink_storage
__selem_link_skbpf_selem_link_storage
selem_unlink_sk__bpf_selem_unlink_storage
sk_storage_updatebpf_local_storage_update
__sk_storage_lookupbpf_local_storage_lookup
bpf_sk_storage_map_freebpf_local_storage_map_free
bpf_sk_storage_map_allocbpf_local_storage_map_alloc
bpf_sk_storage_map_alloc_checkbpf_local_storage_map_alloc_check
bpf_sk_storage_map_check_btfbpf_local_storage_map_check_btf
Thanks for separating this mechanical name change in a separate patch. It is much easier to follow. This patch looks good.
A minor thought is, when I look at unlink_map() and unlink_storage(), it keeps me looking back for the lock situation. I think the main reason is the bpf_selem_unlink_map() is locked but bpf_selem_unlink_storage() is unlocked now. May be:
bpf_selem_unlink_map() => bpf_selem_unlink_map_locked()bpf_selem_link_map() => bpf_selem_link_map_locked()__bpf_selem_unlink_storage() => bpf_selem_unlink_storage_locked()bpf_link_storage() means unlockedbpf_unlink_storage() means unlocked.
I think it could be one follow-up patch later instead of interrupting multiple patches in this set for this minor thing. For now, lets continue with this and remember default is nolock for storage.
I will continue tomorrow.
Re: [PATCH bpf-next v6 1/7] bpf: Renames to prepare for generalizing sk_storage.
Quoted 50 lines · Martin KaFai Lau
Quoted 26 lines · KP Singh
From: KP Singh <[email protected]>
A purely mechanical change to split the renaming from the actual generalization.
Flags/consts:
SK_STORAGE_CREATE_FLAG_MASKBPF_LOCAL_STORAGE_CREATE_FLAG_MASK
BPF_SK_STORAGE_CACHE_SIZEBPF_LOCAL_STORAGE_CACHE_SIZE
MAX_VALUE_SIZEBPF_LOCAL_STORAGE_MAX_VALUE_SIZEStructs:
bucket
bpf_local_storage_map_bucket
bpf_sk_storage_mapbpf_local_storage_map
bpf_sk_storage_databpf_local_storage_data
bpf_sk_storage_elembpf_local_storage_elem
bpf_sk_storagebpf_local_storage
selem_linked_to_skselem_linked_to_storage
selem_allocbpf_selem_allocThe "sk" member in
bpf_local_storageis also updated to "owner" in preparation for changing the type to void * in a subsequent patch.Functions:
Quoted 9 lines
__selem_unlink_skbpf_selem_unlink_storage
__selem_link_skbpf_selem_link_storage
selem_unlink_sk__bpf_selem_unlink_storage
sk_storage_updatebpf_local_storage_update
__sk_storage_lookupbpf_local_storage_lookup
bpf_sk_storage_map_freebpf_local_storage_map_free
bpf_sk_storage_map_allocbpf_local_storage_map_alloc
bpf_sk_storage_map_alloc_checkbpf_local_storage_map_alloc_check
bpf_sk_storage_map_check_btfbpf_local_storage_map_check_btfThanks for separating this mechanical name change in a separate patch. It is much easier to follow. This patch looks good.
A minor thought is, when I look at
unlink_map()andunlink_storage(), it keeps me looking back for the lock situation. I think the main reason is thebpf_selem_unlink_map()is locked butbpf_selem_unlink_storage()is unlocked now. May be:
bpf_selem_unlink_map()=>bpf_selem_unlink_map_locked()bpf_selem_link_map()=>bpf_selem_link_map_locked()__bpf_selem_unlink_storage()=>bpf_selem_unlink_storage_locked()bpf_link_storage()means unlockedbpf_unlink_storage()means unlocked.
Quoted 4 lines
I think it could be one follow-up patch later instead of interrupting multiple patches in this set for this minor thing. For now, lets continue with this and remember default is nolock for storage.
Makes sense. I can update these in a separate patch if there are no major changes needed in this one.
Quoted 1 line
I will continue tomorrow.
Awesome! Thanks :)
- KP
Quoted 1 line
PATCH v6 2/7 bpf: Generalize caching for sk_storage.
In reply to KP SinghFrom: KP Singh <[email protected]>
Provide the a ability to define local storage caches on a per-object type basis. The caches and caching indices for different objects should not be inter-mixed as suggested in:
https://lore.kernel.org/bpf/[email protected]/
"Caching a sk-storage at idx=0 of a sk should not stop an inode-storage to be cached at the same idx of a inode."
--- include/net/bpf_sk_storage.h | 19 +++++++++++++++++++ net/core/bpf_sk_storage.c | 31 +++++++++++++++---------------- 2 files changed, 34 insertions(+), 16 deletions(-)
include/net/bpf_sk_storage.h +19 -0
@@ -3,6 +3,9 @@#ifndef _BPF_SK_STORAGE_H#define _BPF_SK_STORAGE_H +#include <linux/types.h> +#include <linux/spinlock.h> +struct sock ;void bpf_sk_storage_free (struct sock *sk);
@@ -15,6 +18,22 @@ struct sk_buff;struct nlattr ;struct sock ; +#define BPF_LOCAL_STORAGE_CACHE_SIZE 16 + +struct bpf_local_storage_cache { +spinlock_t idx_lock; +u64 idx_usage_counts[BPF_LOCAL_STORAGE_CACHE_SIZE ]; +}; + +#define DEFINE_BPF_STORAGE_CACHE (name) \ +static struct bpf_local_storage_cache name = { \ + .idx_lock = __SPIN_LOCK_UNLOCKED(name.idx_lock), \ +} + +u16 bpf_local_storage_cache_idx_get (struct bpf_local_storage_cache *cache); +void bpf_local_storage_cache_idx_free (struct bpf_local_storage_cache *cache, +u16 idx); +#ifdef CONFIG_BPF_SYSCALL int bpf_sk_storage_clone (const struct sock *sk,struct sock *newsk); struct bpf_sk_storage_diag *
net/core/bpf_sk_storage.c +15 -16
@@ -13,6 +13,8 @@#define BPF_LOCAL_STORAGE_CREATE_FLAG_MASK (BPF_F_NO_PREALLOC | BPF_F_CLONE) +DEFINE_BPF_STORAGE_CACHE (sk_cache); +struct bpf_local_storage_map_bucket {struct hlist_head list;raw_spinlock_t lock;
@@ -77,10 +79,6 @@ struct bpf_local_storage_elem {#define SELEM (_SDATA) \ container_of((_SDATA), struct bpf_local_storage_elem, sdata)#define SDATA (_SELEM) (&(_SELEM)->sdata) -#define BPF_LOCAL_STORAGE_CACHE_SIZE 16 - -static DEFINE_SPINLOCK (cache_idx_lock); -static u64 cache_idx_usage_counts[BPF_LOCAL_STORAGE_CACHE_SIZE ];struct bpf_local_storage {struct bpf_local_storage_data __rcu *cache[BPF_LOCAL_STORAGE_CACHE_SIZE ];
@@ -516,16 +514,16 @@ static int sk_storage_delete(struct sock *sk, struct bpf_map *map)return 0 ; } -static u16 cache_idx_get (void ) +u16 bpf_local_storage_cache_idx_get (struct bpf_local_storage_cache *cache) {u64 min_usage =U64_MAX ;u16 i, res =0 ; -spin_lock (&cache_idx_lock); +spin_lock (&cache->idx_lock);for (i =0 ; i <BPF_LOCAL_STORAGE_CACHE_SIZE ; i++) { -if (cache_idx_usage_counts[i] < min_usage) { - min_usage = cache_idx_usage_counts[i]; +if (cache->idx_usage_counts[i] < min_usage) { + min_usage = cache->idx_usage_counts[i]; res = i;/* Found a free cache_idx */
@@ -533,18 +531,19 @@ static u16 cache_idx_get(void)break ; } } - cache_idx_usage_counts[res]++; + cache->idx_usage_counts[res]++; -spin_unlock (&cache_idx_lock); +spin_unlock (&cache->idx_lock);return res; } -static void cache_idx_free (u16 idx) +void bpf_local_storage_cache_idx_free (struct bpf_local_storage_cache *cache, +u16 idx) { -spin_lock (&cache_idx_lock); - cache_idx_usage_counts[idx]--; -spin_unlock (&cache_idx_lock); +spin_lock (&cache->idx_lock); + cache->idx_usage_counts[idx]--; +spin_unlock (&cache->idx_lock); }/* Called by __sk_destruct() & bpf_sk_storage_clone() */
@@ -596,7 +595,7 @@ static void bpf_local_storage_map_free(struct bpf_map *map) smap = (struct bpf_local_storage_map *)map; -cache_idx_free (smap->cache_idx); +bpf_local_storage_cache_idx_free (&sk_cache, smap->cache_idx);/* Note that this map might be concurrently cloned from * bpf_sk_storage_clone. Wait for any existing bpf_sk_storage_clone
@@ -712,7 +711,7 @@ static struct bpf_map *bpf_local_storage_map_alloc(union bpf_attr *attr) } smap->elem_size =sizeof (struct bpf_local_storage_elem ) + attr->value_size; - smap->cache_idx =cache_idx_get (); + smap->cache_idx =bpf_local_storage_cache_idx_get (&sk_cache);return &smap->map; }
Signature · 1 line
2.28.0.rc0.105.gf9edc3c819-goog
PATCH v6 3/7 bpf: Generalize bpf_sk_storage
In reply to KP SinghFrom: KP Singh <[email protected]>
Refactor the functionality in bpf_sk_storage.c so that concept of storage linked to kernel objects can be extended to other objects like inode, task_struct etc.
Each new local storage will still be a separate map and provide its own set of helpers. This allows for future object specific extensions and still share a lot of the underlying implementation.
--- include/linux/bpf.h | 13 ++ include/net/bpf_sk_storage.h | 55 +++++ include/uapi/linux/bpf.h | 8 +- net/core/bpf_sk_storage.c | 387 ++++++++++++++++++++------------- tools/include/uapi/linux/bpf.h | 8 +- 5 files changed, 320 insertions(+), 151 deletions(-)
include/linux/bpf.h +13 -0
@@ -33,6 +33,9 @@ struct btf;struct btf_type ;struct exception_table_entry ;struct seq_operations ; +struct bpf_local_storage ; +struct bpf_local_storage_map ; +struct bpf_local_storage_elem ;extern struct idr btf_idr;extern spinlock_t btf_idr_lock;
@@ -93,6 +96,16 @@ struct bpf_map_ops {__poll_t (*map_poll)(struct bpf_map *map,struct file *filp,struct poll_table_struct *pts); +/* Functions called by bpf_local_storage maps */ +bool (*map_local_storage_unlink)(struct bpf_local_storage *local_storage, +struct bpf_local_storage_elem *selem, +bool uncharge_omem); +struct bpf_local_storage_elem *(*map_selem_alloc)( +struct bpf_local_storage_map *smap,void *owner,void *value, +bool charge_omem); +struct bpf_local_storage_data *(*map_local_storage_update)( +void *owner,struct bpf_map *map,void *value,u64 flags); +/* BTF name and id of struct allocated by map_alloc */ const char *const map_btf_name;int *map_btf_id;
include/net/bpf_sk_storage.h +55 -0
69 lines not on this page · raw message
include/uapi/linux/bpf.h +6 -2
@@ -3630,9 +3630,13 @@ enum { BPF_F_SYSCTL_BASE_NAME = (1ULL << 0), }; -/* BPF_FUNC_sk_storage_get flags */ +/* BPF_FUNC_<local>_storage_get flags */ enum { -BPF_SK_STORAGE_GET_F_CREATE = (1ULL <<0 ), +BPF_LOCAL_STORAGE_GET_F_CREATE = (1ULL <<0 ), +/* BPF_SK_STORAGE_GET_F_CREATE is only kept for backward compatibility +* and BPF_LOCAL_STORAGE_GET_F_CREATE must be used instead. +*/ +BPF_SK_STORAGE_GET_F_CREATE =BPF_LOCAL_STORAGE_GET_F_CREATE , };/* BPF_FUNC_read_branch_records flags. */
net/core/bpf_sk_storage.c +240 -147
621 lines not on this page · raw message
tools/include/uapi/linux/bpf.h +6 -2
@@ -3630,9 +3630,13 @@ enum { BPF_F_SYSCTL_BASE_NAME = (1ULL << 0), }; -/* BPF_FUNC_sk_storage_get flags */ +/* BPF_FUNC_<local>_storage_get flags */ enum { -BPF_SK_STORAGE_GET_F_CREATE = (1ULL <<0 ), +BPF_LOCAL_STORAGE_GET_F_CREATE = (1ULL <<0 ), +/* BPF_SK_STORAGE_GET_F_CREATE is only kept for backward compatibility +* and BPF_LOCAL_STORAGE_GET_F_CREATE must be used instead. +*/ +BPF_SK_STORAGE_GET_F_CREATE =BPF_LOCAL_STORAGE_GET_F_CREATE , };/* BPF_FUNC_read_branch_records flags. */
Signature · 1 line
2.28.0.rc0.105.gf9edc3c819-goog
Re: [PATCH bpf-next v6 3/7] bpf: Generalize bpf_sk_storage
Quoted 10 lines · KP Singh
From: KP Singh <[email protected]>
Refactor the functionality in
bpf_sk_storage.cso that concept of storage linked to kernel objects can be extended to other objects like inode,task_structetc.Each new local storage will still be a separate map and provide its own set of helpers. This allows for future object specific extensions and still share a lot of the underlying implementation.
[ ... ]
Quoted 18 lines
(quoted diff) +3
-4@@ -386,54 +407,28 @@ static int sk_storage_alloc(struct sock *sk, * Otherwise, it will become a leak (and other memory issues * during map destruction). */ -static struct bpf_local_storage_data * -bpf_local_storage_update(struct sock *sk, struct bpf_map *map, void *value, +struct bpf_local_storage_data * +bpf_local_storage_update(void *owner, struct bpf_map *map, + struct bpf_local_storage *local_storage, void *value, u64 map_flags) { struct bpf_local_storage_data *old_sdata = NULL; struct bpf_local_storage_elem *selem; - struct bpf_local_storage *local_storage; struct bpf_local_storage_map *smap; int err; - /* BPF_EXIST and BPF_NOEXIST cannot be both set */
Review static int sk_storage_alloc(struct sock *sk, · line 409
Quoted 10 lines
(quoted diff) +0
-9- if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST) || - /* BPF_F_LOCK can only be used in a value with spin_lock */ - unlikely((map_flags & BPF_F_LOCK) && !map_value_has_spin_lock(map))) - return ERR_PTR(-EINVAL); - smap = (struct bpf_local_storage_map *)map; - local_storage = rcu_dereference(sk->sk_bpf_storage); - if (!local_storage || hlist_empty(&local_storage->list)) { - /* Very first elem for this object */ - err = check_flags(NULL, map_flags);
This check_flags here is missing in the later sk_storage_update().
Quoted 32 lines
(quoted diff) +2
-18- if (err) - return ERR_PTR(err); - - selem = bpf_selem_alloc(smap, sk, value, true); - if (!selem) - return ERR_PTR(-ENOMEM); - - err = sk_storage_alloc(sk, smap, selem); - if (err) { - kfree(selem); - atomic_sub(smap->elem_size, &sk->sk_omem_alloc); - return ERR_PTR(err); - } - - return SDATA(selem); - } if ((map_flags & BPF_F_LOCK) && !(map_flags & BPF_NOEXIST)) { /* Hoping to find an old_sdata to do inline update * such that it can avoid taking the local_storage->lock * and changing the lists. */ - old_sdata = - bpf_local_storage_lookup(local_storage, smap, false); + old_sdata = bpf_local_storage_lookup(local_storage, smap, false); err = check_flags(old_sdata, map_flags); if (err) return ERR_PTR(err); + if (old_sdata && selem_linked_to_storage(SELEM(old_sdata))) { copy_map_value_locked(map, old_sdata->data, value, false);
[ ... ]
Quoted 9 lines
(quoted diff) +9
-0+static struct bpf_local_storage_data * +sk_storage_update(void *owner, struct bpf_map *map, void *value, u64 map_flags) +{ + struct bpf_local_storage_data *old_sdata = NULL; + struct bpf_local_storage_elem *selem; + struct bpf_local_storage *local_storage; + struct bpf_local_storage_map *smap; + struct sock *sk; + int err;
Review
Quoted 10 lines
(quoted diff) +10
-0+ + err = bpf_local_storage_check_update_flags(map, map_flags); + if (err) + return ERR_PTR(err); + + sk = owner; + local_storage = rcu_dereference(sk->sk_bpf_storage); + smap = (struct bpf_local_storage_map *)map; + + if (!local_storage || hlist_empty(&local_storage->list)) {
"check_flags(NULL, map_flags);" is gone in this refactoring.
This part of code is copied into the inode_storage_update() in the latter patch which then has the same issue.
Quoted 14 lines
(quoted diff) +7
-3+ /* Very first elem */ + selem = map->ops->map_selem_alloc(smap, owner, value, !old_sdata); + if (!selem) + return ERR_PTR(-ENOMEM); static int sk_storage_map_btf_id; const struct bpf_map_ops sk_storage_map_ops = { - .map_alloc_check = bpf_sk_storage_map_alloc_check, - .map_alloc = bpf_local_storage_map_alloc, - .map_free = bpf_local_storage_map_free, + .map_alloc_check = bpf_local_storage_map_alloc_check, + .map_alloc = sk_storage_map_alloc, + .map_free = sk_storage_map_free, .map_get_next_key = notsupp_get_next_key,
Review
Quoted 10 lines
(quoted diff) +4
-1.map_lookup_elem = bpf_fd_sk_storage_lookup_elem, .map_update_elem = bpf_fd_sk_storage_update_elem, .map_delete_elem = bpf_fd_sk_storage_delete_elem, - .map_check_btf = bpf_sk_storage_map_check_btf, + .map_check_btf = bpf_local_storage_map_check_btf, .map_btf_name = "bpf_local_storage_map", .map_btf_id = &sk_storage_map_btf_id, + .map_selem_alloc = sk_selem_alloc, + .map_local_storage_update = sk_storage_update, + .map_local_storage_unlink = unlink_sk_storage,
I think refactoring codes as map_selem_alloc, map_local_storage_update, and map_local_storage_unlink is not the best option. The sk and inode version of the above map_ops are mostly the same. Fixing the issue like the one mentioned above need to fix both sk, inode, and the future kernel-object code.
The only difference is sk charge omem and inode does not.
I have played around a little. I think adding the following three ops (pasted at the end) is better and should be enough for both sk and inode. The inode does not even have to implement the (un)charge ops at all.
That should remove the duplication for the followings:
- (sk|inode)_selem_alloc
- (sk|inode)_storage_update
- unlink_(sk|inode)_storage
- (sk|inode)_storage_alloc
Another bonus is the new bpf_local_storage_check_update_flags() and bpf_local_storage_publish() will be no longer needed too.
I have hacked up this patch 3 change to compiler-test out this idea. I will post in another email. Let me know wdyt.
include/linux/bpf.h +10 -0
@@ -33,6 +33,9 @@ struct btf;struct btf_type ;struct exception_table_entry ;struct seq_operations ; +struct bpf_local_storage ; +struct bpf_local_storage_map ; +struct bpf_local_storage_elem ;extern struct idr btf_idr;extern spinlock_t btf_idr_lock;
@@ -93,6 +96,13 @@ struct bpf_map_ops {__poll_t (*map_poll)(struct bpf_map *map,struct file *filp,struct poll_table_struct *pts); +/* Functions called by bpf_local_storage maps */ +int (*map_local_storage_charge)(struct bpf_local_storage_map *smap, +void *owner,u32 size); +void (*map_local_storage_uncharge)(struct bpf_local_storage_map *smap, +void *owner,u32 size); +struct bpf_local_storage __rcu ** (*map_owner_storage_ptr)(struct bpf_local_storage_map *smap, +void *owner);/* BTF name and id of struct allocated by map_alloc */ const char *const map_btf_name;int *map_btf_id;
Re: [PATCH bpf-next v6 3/7] bpf: Generalize bpf_sk_storage
Quoted 139 lines · Martin KaFai Lau
Quoted 10 lines · KP Singh
From: KP Singh <[email protected]>
Refactor the functionality in
bpf_sk_storage.cso that concept of storage linked to kernel objects can be extended to other objects like inode,task_structetc.Each new local storage will still be a separate map and provide its own set of helpers. This allows for future object specific extensions and still share a lot of the underlying implementation.
[ ... ]
Quoted 18 lines
(quoted diff) +3
-4@@ -386,54 +407,28 @@ static int sk_storage_alloc(struct sock *sk, * Otherwise, it will become a leak (and other memory issues * during map destruction). */ -static struct bpf_local_storage_data * -bpf_local_storage_update(struct sock *sk, struct bpf_map *map, void *value, +struct bpf_local_storage_data * +bpf_local_storage_update(void *owner, struct bpf_map *map, + struct bpf_local_storage *local_storage, void *value, u64 map_flags) { struct bpf_local_storage_data *old_sdata = NULL; struct bpf_local_storage_elem *selem; - struct bpf_local_storage *local_storage; struct bpf_local_storage_map *smap; int err; - /* BPF_EXIST and BPF_NOEXIST cannot be both set */Review static int sk_storage_alloc(struct sock *sk, · line 409
Quoted 10 lines
(quoted diff) +0
-9- if (unlikely((map_flags & ~BPF_F_LOCK) > BPF_EXIST) || - /* BPF_F_LOCK can only be used in a value with spin_lock */ - unlikely((map_flags & BPF_F_LOCK) && !map_value_has_spin_lock(map))) - return ERR_PTR(-EINVAL); - smap = (struct bpf_local_storage_map *)map; - local_storage = rcu_dereference(sk->sk_bpf_storage); - if (!local_storage || hlist_empty(&local_storage->list)) { - /* Very first elem for this object */ - err = check_flags(NULL, map_flags);This
check_flagshere is missing in the latersk_storage_update().Quoted 32 lines
(quoted diff) +2
-18- if (err) - return ERR_PTR(err); - - selem = bpf_selem_alloc(smap, sk, value, true); - if (!selem) - return ERR_PTR(-ENOMEM); - - err = sk_storage_alloc(sk, smap, selem); - if (err) { - kfree(selem); - atomic_sub(smap->elem_size, &sk->sk_omem_alloc); - return ERR_PTR(err); - } - - return SDATA(selem); - } if ((map_flags & BPF_F_LOCK) && !(map_flags & BPF_NOEXIST)) { /* Hoping to find an old_sdata to do inline update * such that it can avoid taking the local_storage->lock * and changing the lists. */ - old_sdata = - bpf_local_storage_lookup(local_storage, smap, false); + old_sdata = bpf_local_storage_lookup(local_storage, smap, false); err = check_flags(old_sdata, map_flags); if (err) return ERR_PTR(err); + if (old_sdata && selem_linked_to_storage(SELEM(old_sdata))) { copy_map_value_locked(map, old_sdata->data, value, false);[ ... ]
Quoted 9 lines
(quoted diff) +9
-0+static struct bpf_local_storage_data * +sk_storage_update(void *owner, struct bpf_map *map, void *value, u64 map_flags) +{ + struct bpf_local_storage_data *old_sdata = NULL; + struct bpf_local_storage_elem *selem; + struct bpf_local_storage *local_storage; + struct bpf_local_storage_map *smap; + struct sock *sk; + int err;Review
Quoted 10 lines
(quoted diff) +10
-0+ + err = bpf_local_storage_check_update_flags(map, map_flags); + if (err) + return ERR_PTR(err); + + sk = owner; + local_storage = rcu_dereference(sk->sk_bpf_storage); + smap = (struct bpf_local_storage_map *)map; + + if (!local_storage || hlist_empty(&local_storage->list)) {"
check_flags(NULL, map_flags);" is gone in this refactoring.This part of code is copied into the
inode_storage_update()in the latter patch which then has the same issue.Quoted 14 lines
(quoted diff) +7
-3+ /* Very first elem */ + selem = map->ops->map_selem_alloc(smap, owner, value, !old_sdata); + if (!selem) + return ERR_PTR(-ENOMEM); static int sk_storage_map_btf_id; const struct bpf_map_ops sk_storage_map_ops = { - .map_alloc_check = bpf_sk_storage_map_alloc_check, - .map_alloc = bpf_local_storage_map_alloc, - .map_free = bpf_local_storage_map_free, + .map_alloc_check = bpf_local_storage_map_alloc_check, + .map_alloc = sk_storage_map_alloc, + .map_free = sk_storage_map_free, .map_get_next_key = notsupp_get_next_key,Review
Quoted 10 lines
(quoted diff) +4
-1.map_lookup_elem = bpf_fd_sk_storage_lookup_elem, .map_update_elem = bpf_fd_sk_storage_update_elem, .map_delete_elem = bpf_fd_sk_storage_delete_elem, - .map_check_btf = bpf_sk_storage_map_check_btf, + .map_check_btf = bpf_local_storage_map_check_btf, .map_btf_name = "bpf_local_storage_map", .map_btf_id = &sk_storage_map_btf_id, + .map_selem_alloc = sk_selem_alloc, + .map_local_storage_update = sk_storage_update, + .map_local_storage_unlink = unlink_sk_storage,I think refactoring codes as
map_selem_alloc,map_local_storage_update, andmap_local_storage_unlinkis not the best option. The sk and inode version of the abovemap_opsare mostly the same. Fixing the issue like the one mentioned above need to fix both sk, inode, and the future kernel-object code.The only difference is sk charge omem and inode does not.
I have played around a little. I think adding the following three ops (pasted at the end) is better and should be enough for both sk and inode. The inode does not even have to implement the (un)charge ops at all.
Quoted 8 lines
That should remove the duplication for the followings:
- (sk|inode)_selem_alloc
- (sk|inode)_storage_update
- unlink_(sk|inode)_storage
- (sk|inode)_storage_allocAnother bonus is the new
bpf_local_storage_check_update_flags()andbpf_local_storage_publish()will be no longer needed too.
I really like this approach. Thank you so much!
Quoted 30 lines
I have hacked up this patch 3 change to compiler-test out this idea. I will post in another email. Let me know wdy>
include/linux/bpf.h +10
-0@@ -33,6 +33,9 @@ struct btf;struct btf_type ;struct exception_table_entry ;struct seq_operations ; +struct bpf_local_storage ; +struct bpf_local_storage_map ; +struct bpf_local_storage_elem ;extern struct idr btf_idr;extern spinlock_t btf_idr_lock;@@ -93,6 +96,13 @@ struct bpf_map_ops {__poll_t (*map_poll)(struct bpf_map *map,struct file *filp,struct poll_table_struct *pts); +/* Functions called by bpf_local_storage maps */ +int (*map_local_storage_charge)(struct bpf_local_storage_map *smap, +void *owner,u32 size); +void (*map_local_storage_uncharge)(struct bpf_local_storage_map *smap, +void *owner,u32 size); +struct bpf_local_storage __rcu ** (*map_owner_storage_ptr)(struct bpf_local_storage_map *smap, +void *owner);/* BTF name and id of struct allocated by map_alloc */ const char *const map_btf_name;int *map_btf_id;
RFC PATCH 1/1 bpf: POC on local_storage charge and uncharge map_ops
In reply to KP SinghIt is a direct replacement of the patch 3 in discussion [1] and to test out the idea on adding map_local_storage_charge, map_local_storage_uncharge, and map_owner_storage_ptr.
It is only compiler tested to demo the idea.
[1]: https://patchwork.ozlabs.org/project/netdev/patch/[email protected]/
--- include/linux/bpf.h | 10 ++ include/net/bpf_sk_storage.h | 51 +++++++ include/uapi/linux/bpf.h | 8 +- net/core/bpf_sk_storage.c | 250 +++++++++++++++++++++------------ tools/include/uapi/linux/bpf.h | 8 +- 5 files changed, 236 insertions(+), 91 deletions(-)
include/linux/bpf.h +10 -0
@@ -33,6 +33,9 @@ struct btf;struct btf_type ;struct exception_table_entry ;struct seq_operations ; +struct bpf_local_storage ; +struct bpf_local_storage_map ; +struct bpf_local_storage_elem ;extern struct idr btf_idr;extern spinlock_t btf_idr_lock;
@@ -93,6 +96,13 @@ struct bpf_map_ops {__poll_t (*map_poll)(struct bpf_map *map,struct file *filp,struct poll_table_struct *pts); +/* Functions called by bpf_local_storage maps */ +int (*map_local_storage_charge)(struct bpf_local_storage_map *smap, +void *owner,u32 size); +void (*map_local_storage_uncharge)(struct bpf_local_storage_map *smap, +void *owner,u32 size); +struct bpf_local_storage __rcu ** (*map_owner_storage_ptr)(struct bpf_local_storage_map *smap, +void *owner);/* BTF name and id of struct allocated by map_alloc */ const char *const map_btf_name;int *map_btf_id;
include/net/bpf_sk_storage.h +51 -0
65 lines not on this page · raw message
include/uapi/linux/bpf.h +6 -2
@@ -3630,9 +3630,13 @@ enum { BPF_F_SYSCTL_BASE_NAME = (1ULL << 0), }; -/* BPF_FUNC_sk_storage_get flags */ +/* BPF_FUNC_<local>_storage_get flags */ enum { -BPF_SK_STORAGE_GET_F_CREATE = (1ULL <<0 ), +BPF_LOCAL_STORAGE_GET_F_CREATE = (1ULL <<0 ), +/* BPF_SK_STORAGE_GET_F_CREATE is only kept for backward compatibility +* and BPF_LOCAL_STORAGE_GET_F_CREATE must be used instead. +*/ +BPF_SK_STORAGE_GET_F_CREATE =BPF_LOCAL_STORAGE_GET_F_CREATE , };/* BPF_FUNC_read_branch_records flags. */
net/core/bpf_sk_storage.c +163 -87
480 lines not on this page · raw message
tools/include/uapi/linux/bpf.h +6 -2
@@ -3630,9 +3630,13 @@ enum { BPF_F_SYSCTL_BASE_NAME = (1ULL << 0), }; -/* BPF_FUNC_sk_storage_get flags */ +/* BPF_FUNC_<local>_storage_get flags */ enum { -BPF_SK_STORAGE_GET_F_CREATE = (1ULL <<0 ), +BPF_LOCAL_STORAGE_GET_F_CREATE = (1ULL <<0 ), +/* BPF_SK_STORAGE_GET_F_CREATE is only kept for backward compatibility +* and BPF_LOCAL_STORAGE_GET_F_CREATE must be used instead. +*/ +BPF_SK_STORAGE_GET_F_CREATE =BPF_LOCAL_STORAGE_GET_F_CREATE , };/* BPF_FUNC_read_branch_records flags. */
Signature · 1 line
2.24.1
Re: [RFC PATCH bpf-next] bpf: POC on local_storage charge and uncharge map_ops
Thanks for this, I was able to update the series with this patch and it works. One minor comment though.
I was wondering how should I send it as a part of the series. I will keep the original commit description + mention this thread and add your Co-Developed-by:
tag and then you can add your Signed-off-by: as well. I am not sure of the canonical way here and am open to suggestions :)
- KP
Quoted 14 lines · Martin KaFai Lau
It is a direct replacement of the patch 3 in discussion [1] and to test out the idea on adding
map_local_storage_charge,map_local_storage_uncharge, andmap_owner_storage_ptr.It is only compiler tested to demo the idea.
[1]: https://patchwork.ozlabs.org/project/netdev/patch/[email protected]/
Signed-off-by Martin KaFai Lau <[email protected]>--- include/linux/bpf.h | 10 ++ include/net/bpf_sk_storage.h | 51 +++++++ include/uapi/linux/bpf.h | 8 +-
[...]
net/core/bpf_sk_storage.c BPF_CALL_2(bpf_sk_storage_delete, struct bpf_map *, map, struct sock *, sk) · line 994
Quoted 11 lines
net/core/bpf_sk_storage.c +11
-0+ +static void sk_storage_uncharge (struct bpf_local_storage_map *smap, +void *owner,u32 size) +{ +struct sock *sk = owner; + +atomic_sub (size, &sk->sk_omem_alloc); +} + +static struct bpf_local_storage __rcu ** +sk_storage_ptr (struct bpf_local_storage_map *smap,void *owner)
Do we need an smap pointer here? It's not being used and is also not used for inode as well.
- KP
Quoted 6 lines
net/core/bpf_sk_storage.c +6
-0+{ +struct sock *sk = owner; + +return &sk->sk_bpf_storage; +} +
[...]
Quoted 13 lines
include/uapi/linux/bpf.h +6
-2-/* BPF_FUNC_sk_storage_get flags */ +/* BPF_FUNC_<local>_storage_get flags */ enum { -BPF_SK_STORAGE_GET_F_CREATE = (1ULL <<0 ), +BPF_LOCAL_STORAGE_GET_F_CREATE = (1ULL <<0 ), +/* BPF_SK_STORAGE_GET_F_CREATE is only kept for backward compatibility +* and BPF_LOCAL_STORAGE_GET_F_CREATE must be used instead. +*/ +BPF_SK_STORAGE_GET_F_CREATE =BPF_LOCAL_STORAGE_GET_F_CREATE , };/* BPF_FUNC_read_branch_records flags. */
Re: [RFC PATCH bpf-next] bpf: POC on local_storage charge and uncharge map_ops
Quoted 6 lines · KP Singh
Thanks for this, I was able to update the series with this patch and it works. One minor comment though.
I was wondering how should I send it as a part of the series. I will keep the original commit description + mention this thread and add your Co-Developed-by:
tag and then you can add your Signed-off-by: as well.
Sounds good to me.
Thanks for verifying the idea. Feel free to make changes or clean up on this RFC.
Quoted 23 lines
I am not sure of the
canonical way here and am open to suggestions :)- KP
Quoted 14 lines · Martin KaFai Lau
It is a direct replacement of the patch 3 in discussion [1] and to test out the idea on adding
map_local_storage_charge,map_local_storage_uncharge, andmap_owner_storage_ptr.It is only compiler tested to demo the idea.
Signed-off-by Martin KaFai Lau <[email protected]>--- include/linux/bpf.h | 10 ++ include/net/bpf_sk_storage.h | 51 +++++++ include/uapi/linux/bpf.h | 8 +-[...]
Quoted 14 lines
net/core/bpf_sk_storage.c BPF_CALL_2(bpf_sk_storage_delete, struct bpf_map *, map, struct sock *, sk) · line 994
Quoted 11 lines
net/core/bpf_sk_storage.c +11
-0+ +static void sk_storage_uncharge (struct bpf_local_storage_map *smap, +void *owner,u32 size) +{ +struct sock *sk = owner; + +atomic_sub (size, &sk->sk_omem_alloc); +} + +static struct bpf_local_storage __rcu ** +sk_storage_ptr (struct bpf_local_storage_map *smap,void *owner)Do we need an smap pointer here? It's not being used and is also not used for inode as well.
You are correct. No, it is not needed.
I threw in there merely because it is a map_ops. It is unused and can be removed.
Quoted 6 lines
Quoted 6 lines
net/core/bpf_sk_storage.c +6
-0+{ +struct sock *sk = owner; + +return &sk->sk_bpf_storage; +} +
PATCH v6 4/7 bpf: Split bpf_local_storage to bpf_sk_storage
In reply to KP SinghFrom: KP Singh <[email protected]>
A purely mechanical change:
bpf_sk_storage.c = bpf_sk_storage.c + bpf_local_storage.c bpf_sk_storage.h = bpf_sk_storage.h + bpf_local_storage.h
--- include/linux/bpf_local_storage.h | 165 +++++++++ include/net/bpf_sk_storage.h | 64 ---- kernel/bpf/Makefile | 1 + kernel/bpf/bpf_local_storage.c | 519 ++++++++++++++++++++++++++ net/core/bpf_sk_storage.c | 587 +----------------------------- 5 files changed, 686 insertions(+), 650 deletions(-) create mode 100644 include/linux/bpf_local_storage.h create mode 100644 kernel/bpf/bpf_local_storage.c
include/linux/bpf_local_storage.h new file +165 -0
@@ -0,0 +1,165 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* Copyright (c) 2019 Facebook +* Copyright 2020 Google LLC. +*/ + +#ifndef _BPF_LOCAL_STORAGE_H +#define _BPF_LOCAL_STORAGE_H + +#include <linux/bpf.h> +#include <linux/rculist.h> +#include <linux/list.h> +#include <linux/hash.h> +#include <linux/types.h> +#include <uapi/linux/btf.h> + +#define BPF_LOCAL_STORAGE_CACHE_SIZE 16 + +struct bpf_local_storage_map_bucket { +struct hlist_head list; +raw_spinlock_t lock; +}; + +/* Thp map is not the primary owner of a bpf_local_storage_elem. +* Instead, the container object (eg. sk->sk_bpf_storage) is. +* +* The map (bpf_local_storage_map) is for two purposes +* 1. Define the size of the "local storage". It is +* the map's value_size. +* +* 2. Maintain a list to keep track of all elems such +* that they can be cleaned up during the map destruction. +* +* When a bpf local storage is being looked up for a +* particular object, the "bpf_map" pointer is actually used +* as the "key" to search in the list of elem in +* the respective bpf_local_storage owned by the object. +* +* e.g. sk->sk_bpf_storage is the mini-map with the "bpf_map" pointer +* as the searching key. +*/ +struct bpf_local_storage_map { +struct bpf_map map; +/* Lookup elem does not require accessing the map. +* +* Updating/Deleting requires a bucket lock to +* link/unlink the elem from the map. Having +* multiple buckets to improve contention. +*/ +struct bpf_local_storage_map_bucket *buckets; +u32 bucket_log; +u16 elem_size; +u16 cache_idx; +}; + +struct bpf_local_storage_data { +/* smap is used as the searching key when looking up +* from the object's bpf_local_storage. +* +* Put it in the same cacheline as the data to minimize +* the number of cachelines access during the cache hit case. +*/ +struct bpf_local_storage_map __rcu *smap; +u8 data[]__aligned(8) ; +}; + +/* Linked to bpf_local_storage and bpf_local_storage_map */ +struct bpf_local_storage_elem { +struct hlist_node map_node;/* Linked to bpf_local_storage_map */ +struct hlist_node snode;/* Linked to bpf_local_storage */ +struct bpf_local_storage __rcu *local_storage; +struct rcu_head rcu; +/* 8 bytes hole */ +/* The data is stored in aother cacheline to minimize +* the number of cachelines access during a cache hit. +*/ +struct bpf_local_storage_data sdata____cacheline_aligned ; +}; + +struct bpf_local_storage { +struct bpf_local_storage_data __rcu *cache[BPF_LOCAL_STORAGE_CACHE_SIZE ]; +struct hlist_head list;/* List of bpf_local_storage_elem */ +void *owner;/* The object that owns the the above "list" of +* bpf_local_storage_elem. +*/ +struct rcu_head rcu; +raw_spinlock_t lock;/* Protect adding/removing from the "list" */ +}; + +struct bpf_local_storage_cache { +spinlock_t idx_lock; +u64 idx_usage_counts[BPF_LOCAL_STORAGE_CACHE_SIZE ]; +}; + +#define DEFINE_BPF_STORAGE_CACHE (name) \ +static struct bpf_local_storage_cache name = { \ + .idx_lock = __SPIN_LOCK_UNLOCKED(name.idx_lock), \ +} + +u16 bpf_local_storage_cache_idx_get (struct bpf_local_storage_cache *cache); +void bpf_local_storage_cache_idx_free (struct bpf_local_storage_cache *cache, +u16 idx); + +/* U16_MAX is much more than enough for sk local storage +* considering a tcp_sock is ~2k. +*/ +#define BPF_LOCAL_STORAGE_MAX_VALUE_SIZE \ + min_t(u32, \ + (KMALLOC_MAX_SIZE - MAX_BPF_STACK - \ + sizeof(struct bpf_local_storage_elem)), \ + (U16_MAX - sizeof(struct bpf_local_storage_elem))) + +#define SELEM (_SDATA) \ + container_of((_SDATA), struct bpf_local_storage_elem, sdata) +#define SDATA (_SELEM) (&(_SELEM)->sdata) + +/* Helper functions for bpf_local_storage */ +int bpf_local_storage_map_alloc_check (union bpf_attr *attr); + +struct bpf_local_storage_map *bpf_local_storage_map_alloc (union bpf_attr *attr); + +struct bpf_local_storage_data * +bpf_local_storage_lookup (struct bpf_local_storage *local_storage, +struct bpf_local_storage_map *smap, +bool cacheit_lockit); + +void bpf_local_storage_map_free (struct bpf_local_storage_map *smap); + +int bpf_local_storage_map_check_btf (const struct bpf_map *map, +const struct btf *btf, +const struct btf_type *key_type, +const struct btf_type *value_type); + +void bpf_selem_link_storage (struct bpf_local_storage *local_storage, +struct bpf_local_storage_elem *selem); + +bool bpf_selem_unlink_storage (struct bpf_local_storage *local_storage, +struct bpf_local_storage_elem *selem, +bool uncharge_omem); + +void bpf_selem_unlink (struct bpf_local_storage_elem *selem); + +void bpf_selem_link_map (struct bpf_local_storage_map *smap, +struct bpf_local_storage_elem *selem); + +void bpf_selem_unlink_map (struct bpf_local_storage_elem *selem); + +struct bpf_local_storage_elem * +bpf_selem_alloc (struct bpf_local_storage_map *smap,void *value); + +struct bpf_local_storage * +bpf_local_storage_alloc (struct bpf_local_storage_map *smap); + +int bpf_local_storage_publish (struct bpf_local_storage_elem *first_selem, +struct bpf_local_storage **addr, +struct bpf_local_storage *curr); + +int bpf_local_storage_check_update_flags (struct bpf_map *map,u64 map_flags); + +struct bpf_local_storage_data * +bpf_local_storage_update (void *owner,struct bpf_map *map, +struct bpf_local_storage *local_storage,void *value, +u64 map_flags); + +#endif /* _BPF_LOCAL_STORAGE_H */
include/net/bpf_sk_storage.h +0 -64
70 lines not on this page · raw message
kernel/bpf/Makefile +1 -0
@@ -12,6 +12,7 @@ obj-$(CONFIG_BPF_JIT) += dispatcher.o ifeq ($(CONFIG_NET),y) obj-$(CONFIG_BPF_SYSCALL) += devmap.o obj-$(CONFIG_BPF_SYSCALL) += cpumap.o +obj-$(CONFIG_BPF_SYSCALL) += bpf_local_storage.o obj-$(CONFIG_BPF_SYSCALL) += offload.o obj-$(CONFIG_BPF_SYSCALL) += net_namespace.o endif
kernel/bpf/bpf_local_storage.c new file +519 -0
519 lines not on this page · raw message
net/core/bpf_sk_storage.c +1 -586
653 lines not on this page · raw message
Signature · 1 line
2.28.0.rc0.105.gf9edc3c819-goog
PATCH v6 5/7 bpf: Implement bpf_local_storage for inodes
In reply to KP SinghFrom: KP Singh <[email protected]>
Similar to bpf_local_storage for sockets, add local storage for inodes. The life-cycle of storage is managed with the life-cycle of the inode. i.e. the storage is destroyed along with the owning inode.
The BPF LSM allocates an __rcu pointer to the bpf_local_storage in the security blob which are now stackable and can co-exist with other LSMs.
--- include/linux/bpf_local_storage.h | 10 + include/linux/bpf_lsm.h | 21 ++ include/linux/bpf_types.h | 3 + include/uapi/linux/bpf.h | 38 ++ kernel/bpf/Makefile | 1 + kernel/bpf/bpf_inode_storage.c | 353 ++++++++++++++++++ kernel/bpf/syscall.c | 3 +- kernel/bpf/verifier.c | 10 + security/bpf/hooks.c | 7 + .../bpf/bpftool/Documentation/bpftool-map.rst | 2 +- tools/bpf/bpftool/bash-completion/bpftool | 3 +- tools/bpf/bpftool/map.c | 3 +- tools/include/uapi/linux/bpf.h | 38 ++ tools/lib/bpf/libbpf_probes.c | 5 +- 14 files changed, 491 insertions(+), 6 deletions(-) create mode 100644 kernel/bpf/bpf_inode_storage.c
include/linux/bpf_local_storage.h +10 -0
@@ -162,4 +162,14 @@ bpf_local_storage_update(void *owner, struct bpf_map *map, struct bpf_local_storage *local_storage, void *value, u64 map_flags); +#ifdef CONFIG_BPF_LSM +extern const struct bpf_func_proto bpf_inode_storage_get_proto; +extern const struct bpf_func_proto bpf_inode_storage_delete_proto; +void bpf_inode_storage_free (struct inode *inode); +#else +static inline void bpf_inode_storage_free (struct inode *inode) +{ +} +#endif /* CONFIG_BPF_LSM */ +#endif /* _BPF_LOCAL_STORAGE_H */
include/linux/bpf_lsm.h +21 -0
@@ -17,9 +17,24 @@#include <linux/lsm_hook_defs.h> #undef LSM_HOOK +struct bpf_storage_blob { +struct bpf_local_storage __rcu *storage; +}; + +extern struct lsm_blob_sizes bpf_lsm_blob_sizes; +int bpf_lsm_verify_prog (struct bpf_verifier_log *vlog,const struct bpf_prog *prog); +static inline struct bpf_storage_blob *bpf_inode ( +const struct inode *inode) +{ +if (unlikely (!inode->i_security)) +return NULL ; + +return inode->i_security + bpf_lsm_blob_sizes.lbs_inode; +} +#else /* !CONFIG_BPF_LSM */ static inline int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog,
@@ -28,6 +43,12 @@ static inline int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog,return -EOPNOTSUPP ; } +static inline struct bpf_storage_blob *bpf_inode ( +const struct inode *inode) +{ +return NULL ; +} +#endif /* CONFIG_BPF_LSM */ #endif /* _LINUX_BPF_LSM_H */
include/linux/bpf_types.h +3 -0
@@ -107,6 +107,9 @@ BPF_MAP_TYPE(BPF_MAP_TYPE_SK_STORAGE, sk_storage_map_ops) BPF_MAP_TYPE(BPF_MAP_TYPE_SOCKMAP, sock_map_ops) BPF_MAP_TYPE(BPF_MAP_TYPE_SOCKHASH, sock_hash_ops)#endif +#ifdef CONFIG_BPF_LSM +BPF_MAP_TYPE(BPF_MAP_TYPE_INODE_STORAGE, inode_storage_map_ops) +#endif BPF_MAP_TYPE(BPF_MAP_TYPE_CPUMAP, cpu_map_ops)#if defined(CONFIG_XDP_SOCKETS )BPF_MAP_TYPE (BPF_MAP_TYPE_XSKMAP , xsk_map_ops)
include/uapi/linux/bpf.h +38 -0
@@ -148,6 +148,7 @@ enum bpf_map_type {BPF_MAP_TYPE_DEVMAP_HASH ,BPF_MAP_TYPE_STRUCT_OPS ,BPF_MAP_TYPE_RINGBUF , + BPF_MAP_TYPE_INODE_STORAGE, };/* Note that tracing related programs such as
@@ -3377,6 +3378,41 @@ union bpf_attr {* A non-negative value equal to or less than *size* on success, * or a negative error in case of failure. * +* void *bpf_inode_storage_get(struct bpf_map *map, void *inode, void *value, u64 flags) +* Description +* Get a bpf_local_storage from an *inode*. +* +* Logically, it could be thought of as getting the value from +* a *map* with *inode* as the **key**. From this +* perspective, the usage is not much different from +* **bpf_map_lookup_elem**\ (*map*, **&**\ *inode*) except this +* helper enforces the key must be an inode and the map must also +* be a **BPF_MAP_TYPE_INODE_STORAGE**. +* +* Underneath, the value is stored locally at *inode* instead of +* the *map*. The *map* is used as the bpf-local-storage +* "type". The bpf-local-storage "type" (i.e. the *map*) is +* searched against all bpf_local_storage residing at *inode*. +* +* An optional *flags* (**BPF_LOCAL_STORAGE_GET_F_CREATE**) can be +* used such that a new bpf_local_storage will be +* created if one does not exist. *value* can be used +* together with **BPF_LOCAL_STORAGE_GET_F_CREATE** to specify +* the initial value of a bpf_local_storage. If *value* is +* **NULL**, the new bpf_local_storage will be zero initialized. +* Return +* A bpf_local_storage pointer is returned on success. +* +* **NULL** if not found or there was an error in adding +* a new bpf_local_storage. +* +* int bpf_inode_storage_delete(struct bpf_map *map, void *inode) +* Description +* Delete a bpf_local_storage from an *inode*. +* Return +* 0 on success. +* +* **-ENOENT** if the bpf_local_storage cannot be found. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -3521,6 +3557,8 @@ union bpf_attr {FN (skc_to_tcp_request_sock), \FN (skc_to_udp6_sock), \FN (get_task_stack), \ +FN (inode_storage_get), \ +FN (inode_storage_delete), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
kernel/bpf/Makefile +1 -0
@@ -5,6 +5,7 @@ CFLAGS_core.o += $(call cc-disable-warning, override-init) obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o bpf_iter.o map_iter.o task_iter.o prog_iter.o obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o +obj-${CONFIG_BPF_LSM} += bpf_inode_storage.o obj-$(CONFIG_BPF_SYSCALL) += disasm.o obj-$(CONFIG_BPF_JIT) += trampoline.o obj-$(CONFIG_BPF_SYSCALL) += btf.o
kernel/bpf/bpf_inode_storage.c new file +353 -0
353 lines not on this page · raw message
kernel/bpf/syscall.c +2 -1
@@ -768,7 +768,8 @@ static int map_check_btf(struct bpf_map *map, const struct btf *btf,if (map->map_type !=BPF_MAP_TYPE_HASH && map->map_type !=BPF_MAP_TYPE_ARRAY && map->map_type !=BPF_MAP_TYPE_CGROUP_STORAGE && - map->map_type !=BPF_MAP_TYPE_SK_STORAGE ) + map->map_type !=BPF_MAP_TYPE_SK_STORAGE && + map->map_type !=BPF_MAP_TYPE_INODE_STORAGE )return -ENOTSUPP ;if (map->spin_lock_off +sizeof (struct bpf_spin_lock ) > map->value_size) {
kernel/bpf/verifier.c +10 -0
@@ -4167,6 +4167,11 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env, func_id != BPF_FUNC_sk_storage_delete) goto error;break ; +case BPF_MAP_TYPE_INODE_STORAGE : +if (func_id != BPF_FUNC_inode_storage_get && + func_id != BPF_FUNC_inode_storage_delete) + goto error; +break ;default :break ; }
@@ -4240,6 +4245,11 @@ static int check_map_func_compatibility(struct bpf_verifier_env *env,if (map->map_type !=BPF_MAP_TYPE_SK_STORAGE ) goto error;break ; +case BPF_FUNC_inode_storage_get: +case BPF_FUNC_inode_storage_delete: +if (map->map_type !=BPF_MAP_TYPE_INODE_STORAGE ) + goto error; +break ;default :break ; }
security/bpf/hooks.c +7 -0
@@ -3,6 +3,7 @@/* * Copyright (C) 2020 Google LLC. */ +#include <linux/bpf_local_storage.h> #include <linux/lsm_hooks.h> #include <linux/bpf_lsm.h>
@@ -11,6 +12,7 @@ static struct security_hook_list bpf_lsm_hooks[] __lsm_ro_after_init = { LSM_HOOK_INIT(NAME, bpf_lsm_##NAME),#include <linux/lsm_hook_defs.h> #undef LSM_HOOK + LSM_HOOK_INIT(inode_free_security, bpf_inode_storage_free), };static int __init bpf_lsm_init (void )
@@ -20,7 +22,12 @@ static int __init bpf_lsm_init(void)return 0 ; } +struct lsm_blob_sizes bpf_lsm_blob_sizes __lsm_ro_after_init = { + .lbs_inode =sizeof (struct bpf_storage_blob ), +}; +DEFINE_LSM (bpf) = { .name ="bpf" , .init = bpf_lsm_init, + .blobs = &bpf_lsm_blob_sizes };
tools/bpf/bpftool/Documentation/bpftool-map.rst +1 -1
@@ -49,7 +49,7 @@ MAP COMMANDS | | **lru_percpu_hash** | **lpm_trie** | **array_of_maps** | **hash_of_maps** | | **devmap** | **devmap_hash** | **sockmap** | **cpumap** | **xskmap** | **sockhash** | | **cgroup_storage** | **reuseport_sockarray** | **percpu_cgroup_storage** -| | **queue** | **stack** | **sk_storage** | **struct_ops** | **ringbuf** } +| | **queue** | **stack** | **sk_storage** | **struct_ops** | **ringbuf** | **inode_storage** } DESCRIPTION ===========
tools/bpf/bpftool/bash-completion/bpftool +2 -1
@@ -688,7 +688,8 @@ _bpftool() lru_percpu_hash lpm_trie array_of_maps \ hash_of_maps devmap devmap_hash sockmap cpumap \ xskmap sockhash cgroup_storage reuseport_sockarray \ - percpu_cgroup_storage queue stack' -- \ + percpu_cgroup_storage queue stack sk_storage \ + struct_ops inode_storage' -- \ "$cur" ) ) return 0 ;;
tools/bpf/bpftool/map.c +2 -1
@@ -50,6 +50,7 @@ const char * const map_type_name[] = { [BPF_MAP_TYPE_SK_STORAGE] = "sk_storage", [BPF_MAP_TYPE_STRUCT_OPS] = "struct_ops", [BPF_MAP_TYPE_RINGBUF] = "ringbuf", + [BPF_MAP_TYPE_INODE_STORAGE] = "inode_storage", };const size_t map_type_name_size =ARRAY_SIZE (map_type_name);
@@ -1442,7 +1443,7 @@ static int do_help(int argc, char **argv)" lru_percpu_hash | lpm_trie | array_of_maps | hash_of_maps |\n" " devmap | devmap_hash | sockmap | cpumap | xskmap | sockhash |\n" " cgroup_storage | reuseport_sockarray | percpu_cgroup_storage |\n" -" queue | stack | sk_storage | struct_ops | ringbuf }\n" +" queue | stack | sk_storage | struct_ops | ringbuf | inode_storage }\n" " " HELP_SPEC_OPTIONS "\n" "" , bin_name, argv[-2]);
tools/include/uapi/linux/bpf.h +38 -0
@@ -148,6 +148,7 @@ enum bpf_map_type {BPF_MAP_TYPE_DEVMAP_HASH ,BPF_MAP_TYPE_STRUCT_OPS ,BPF_MAP_TYPE_RINGBUF , + BPF_MAP_TYPE_INODE_STORAGE, };/* Note that tracing related programs such as
@@ -3377,6 +3378,41 @@ union bpf_attr {* A non-negative value equal to or less than *size* on success, * or a negative error in case of failure. * +* void *bpf_inode_storage_get(struct bpf_map *map, void *inode, void *value, u64 flags) +* Description +* Get a bpf_local_storage from an *inode*. +* +* Logically, it could be thought of as getting the value from +* a *map* with *inode* as the **key**. From this +* perspective, the usage is not much different from +* **bpf_map_lookup_elem**\ (*map*, **&**\ *inode*) except this +* helper enforces the key must be an inode and the map must also +* be a **BPF_MAP_TYPE_INODE_STORAGE**. +* +* Underneath, the value is stored locally at *inode* instead of +* the *map*. The *map* is used as the bpf-local-storage +* "type". The bpf-local-storage "type" (i.e. the *map*) is +* searched against all bpf_local_storage residing at *inode*. +* +* An optional *flags* (**BPF_LOCAL_STORAGE_GET_F_CREATE**) can be +* used such that a new bpf_local_storage will be +* created if one does not exist. *value* can be used +* together with **BPF_LOCAL_STORAGE_GET_F_CREATE** to specify +* the initial value of a bpf_local_storage. If *value* is +* **NULL**, the new bpf_local_storage will be zero initialized. +* Return +* A bpf_local_storage pointer is returned on success. +* +* **NULL** if not found or there was an error in adding +* a new bpf_local_storage. +* +* int bpf_inode_storage_delete(struct bpf_map *map, void *inode) +* Description +* Delete a bpf_local_storage from an *inode*. +* Return +* 0 on success. +* +* **-ENOENT** if the bpf_local_storage cannot be found. */ #define __BPF_FUNC_MAPPER (FN ) \ FN(unspec), \
@@ -3521,6 +3557,8 @@ union bpf_attr {FN (skc_to_tcp_request_sock), \FN (skc_to_udp6_sock), \FN (get_task_stack), \ +FN (inode_storage_get), \ +FN (inode_storage_delete), \/* */ /* integer value in 'imm' field of BPF_CALL instruction selects which helper
tools/lib/bpf/libbpf_probes.c +3 -2
@@ -173,7 +173,7 @@ int libbpf__load_raw_btf(const char *raw_types, size_t types_len,return btf_fd; } -static int load_sk_storage_btf (void ) +static int load_local_storage_btf (void ) {const char strs[] ="\0bpf_spin_lock\0val\0cnt\0l" ;/* struct bpf_spin_lock {
@@ -232,12 +232,13 @@ bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex) key_size =0 ;break ;case BPF_MAP_TYPE_SK_STORAGE : +case BPF_MAP_TYPE_INODE_STORAGE : btf_key_type_id =1 ; btf_value_type_id =3 ; value_size =8 ; max_entries =0 ; map_flags =BPF_F_NO_PREALLOC ; - btf_fd =load_sk_storage_btf (); + btf_fd =load_local_storage_btf ();if (btf_fd <0 )return false;break ;
Signature · 1 line
2.28.0.rc0.105.gf9edc3c819-goog
PATCH v6 6/7 bpf: Allow local storage to be used from LSM programs
In reply to KP SinghFrom: KP Singh <[email protected]>
Adds support for both bpf_{sk, inode}_storage_{get, delete} to be used in LSM programs. These helpers are not used for tracing programs (currently) as their usage is tied to the life-cycle of the object and should only be used where the owning object won't be freed (when the owning object is passed as an argument to the LSM hook). Thus, they are safer to use in LSM hooks than tracing. Usage of local storage in tracing programs will probably follow a per function based whitelist approach.
Since the UAPI helper signature for bpf_sk_storage expect a bpf_sock, it, leads to a compilation warning for LSM programs, it's also updated to accept a void * pointer instead.
--- include/net/bpf_sk_storage.h | 2 ++ include/uapi/linux/bpf.h | 8 ++++++-- kernel/bpf/bpf_lsm.c | 21 ++++++++++++++++++++- net/core/bpf_sk_storage.c | 27 +++++++++++++++++++++++++++ tools/include/uapi/linux/bpf.h | 8 ++++++-- 5 files changed, 61 insertions(+), 5 deletions(-)
include/net/bpf_sk_storage.h +2 -0
@@ -19,6 +19,8 @@ void bpf_sk_storage_free(struct sock *sk);extern const struct bpf_func_proto bpf_sk_storage_get_proto;extern const struct bpf_func_proto bpf_sk_storage_delete_proto; +extern const struct bpf_func_proto sk_storage_get_btf_proto; +extern const struct bpf_func_proto sk_storage_delete_btf_proto;struct bpf_sk_storage_diag ;struct sk_buff ;
include/uapi/linux/bpf.h +6 -2
@@ -2790,7 +2790,7 @@ union bpf_attr {* * **-ERANGE** if resulting value was out of range. * -* void *bpf_sk_storage_get(struct bpf_map *map, struct bpf_sock *sk, void *value, u64 flags) +* void *bpf_sk_storage_get(struct bpf_map *map, void *sk, void *value, u64 flags) * Description * Get a bpf-local-storage from a *sk*. *
@@ -2806,6 +2806,10 @@ union bpf_attr {* "type". The bpf-local-storage "type" (i.e. the *map*) is * searched against all bpf-local-storages residing at *sk*. * +* For socket programs, *sk* should be a **struct bpf_sock** pointer +* and an **ARG_PTR_TO_BTF_ID** of type **struct sock** for LSM +* programs. +* * An optional *flags* (**BPF_SK_STORAGE_GET_F_CREATE**) can be * used such that a new bpf-local-storage will be * created if one does not exist. *value* can be used
@@ -2818,7 +2822,7 @@ union bpf_attr {* **NULL** if not found or there was an error in adding * a new bpf-local-storage. * -* long bpf_sk_storage_delete(struct bpf_map *map, struct bpf_sock *sk) +* long bpf_sk_storage_delete(struct bpf_map *map, void *sk) * Description * Delete a bpf-local-storage from a *sk*. * Return
kernel/bpf/bpf_lsm.c +20 -1
@@ -11,6 +11,8 @@#include <linux/bpf_lsm.h> #include <linux/kallsyms.h> #include <linux/bpf_verifier.h> +#include <net/bpf_sk_storage.h> +#include <linux/bpf_local_storage.h> /* For every LSM hook that allows attachment of BPF programs, declare a nop * function where a BPF program can be attached.
@@ -45,10 +47,27 @@ int bpf_lsm_verify_prog(struct bpf_verifier_log *vlog,return 0 ; } +static const struct bpf_func_proto * +bpf_lsm_func_proto (enum bpf_func_id func_id,const struct bpf_prog *prog) +{ +switch (func_id) { +case BPF_FUNC_inode_storage_get: +return &bpf_inode_storage_get_proto; +case BPF_FUNC_inode_storage_delete: +return &bpf_inode_storage_delete_proto; +case BPF_FUNC_sk_storage_get: +return &sk_storage_get_btf_proto; +case BPF_FUNC_sk_storage_delete: +return &sk_storage_delete_btf_proto; +default : +return tracing_prog_func_proto (func_id, prog); + } +} +const struct bpf_prog_ops lsm_prog_ops = { };const struct bpf_verifier_ops lsm_verifier_ops = { - .get_func_proto = tracing_prog_func_proto, + .get_func_proto = bpf_lsm_func_proto, .is_valid_access = btf_ctx_access, };
net/core/bpf_sk_storage.c +27 -0
@@ -11,6 +11,7 @@#include <net/sock.h> #include <uapi/linux/sock_diag.h> #include <uapi/linux/btf.h> +#include <linux/btf_ids.h> DEFINE_BPF_STORAGE_CACHE (sk_cache);
@@ -465,6 +466,32 @@ const struct bpf_func_proto bpf_sk_storage_delete_proto = { .arg2_type = ARG_PTR_TO_SOCKET, }; +BTF_ID_LIST (sk_storage_get_btf_ids) +BTF_ID(struct, sock) + +const struct bpf_func_proto sk_storage_get_btf_proto = { + .func = bpf_sk_storage_get, + .gpl_only = false, + .ret_type =RET_PTR_TO_MAP_VALUE_OR_NULL , + .arg1_type =ARG_CONST_MAP_PTR , + .arg2_type =ARG_PTR_TO_BTF_ID , + .arg3_type =ARG_PTR_TO_MAP_VALUE_OR_NULL , + .arg4_type =ARG_ANYTHING , + .btf_id = sk_storage_get_btf_ids, +}; + +BTF_ID_LIST (sk_storage_delete_btf_ids) +BTF_ID(struct, sock) + +const struct bpf_func_proto sk_storage_delete_btf_proto = { + .func = bpf_sk_storage_delete, + .gpl_only = false, + .ret_type =RET_INTEGER , + .arg1_type =ARG_CONST_MAP_PTR , + .arg2_type =ARG_PTR_TO_BTF_ID , + .btf_id = sk_storage_delete_btf_ids, +}; +struct bpf_sk_storage_diag {u32 nr_maps;struct bpf_map *maps[];
tools/include/uapi/linux/bpf.h +6 -2
@@ -2790,7 +2790,7 @@ union bpf_attr {* * **-ERANGE** if resulting value was out of range. * -* void *bpf_sk_storage_get(struct bpf_map *map, struct bpf_sock *sk, void *value, u64 flags) +* void *bpf_sk_storage_get(struct bpf_map *map, void *sk, void *value, u64 flags) * Description * Get a bpf-local-storage from a *sk*. *
@@ -2806,6 +2806,10 @@ union bpf_attr {* "type". The bpf-local-storage "type" (i.e. the *map*) is * searched against all bpf-local-storages residing at *sk*. * +* For socket programs, *sk* should be a **struct bpf_sock** pointer +* and an **ARG_PTR_TO_BTF_ID** of type **struct sock** for LSM +* programs. +* * An optional *flags* (**BPF_SK_STORAGE_GET_F_CREATE**) can be * used such that a new bpf-local-storage will be * created if one does not exist. *value* can be used
@@ -2818,7 +2822,7 @@ union bpf_attr {* **NULL** if not found or there was an error in adding * a new bpf-local-storage. * -* long bpf_sk_storage_delete(struct bpf_map *map, struct bpf_sock *sk) +* long bpf_sk_storage_delete(struct bpf_map *map, void *sk) * Description * Delete a bpf-local-storage from a *sk*. * Return
Signature · 1 line
2.28.0.rc0.105.gf9edc3c819-goog
PATCH v6 7/7 bpf: Add selftests for local_storage
In reply to KP SinghFrom: KP Singh <[email protected]>
inode_local_storage:
* Hook to the file_open and inode_unlink LSM hooks.
* Create and unlink a temporary file.
* Store some information in the inode's bpf_local_storage during file_open.
* Verify that this information exists when the file is unlinked.
sk_local_storage:
* Hook to the socket_post_create and socket_bind LSM hooks.
* Open and bind a socket and set the sk_storage in the socket_post_create hook using the start_server helper.
* Verify if the information is set in the socket_bind hook.
--- .../bpf/prog_tests/test_local_storage.c | 60 ++++++++ .../selftests/bpf/progs/local_storage.c | 136 ++++++++++++++++++ 2 files changed, 196 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_local_storage.c create mode 100644 tools/testing/selftests/bpf/progs/local_storage.c
tools/testing/selftests/bpf/prog_tests/test_local_storage.c new file +60 -0
@@ -0,0 +1,60 @@ +// SPDX-License-Identifier: GPL-2.0 + +/* +* Copyright (C) 2020 Google LLC. +*/ + +#include <test_progs.h> +#include <linux/limits.h> + +#include "local_storage.skel.h" +#include "network_helpers.h" + +int create_and_unlink_file (void ) +{ +char fname[PATH_MAX ] ="/tmp/fileXXXXXX" ; +int fd; + + fd =mkstemp (fname); +if (fd <0 ) +return fd; + +close (fd); +unlink (fname); +return 0 ; +} + +void test_test_local_storage (void ) +{ +struct local_storage *skel =NULL ; +int err, duration =0 , serv_sk =-1 ; + + skel =local_storage__open_and_load (); +if (CHECK (!skel,"skel_load" ,"lsm skeleton failed\n" )) + goto close_prog; + + err =local_storage__attach (skel); +if (CHECK (err,"attach" ,"lsm attach failed: %d\n" , err)) + goto close_prog; + + skel->bss->monitored_pid =getpid (); + + err =create_and_unlink_file (); +if (CHECK (err <0 ,"exec_cmd" ,"err %d errno %d\n" , err, errno)) + goto close_prog; + +CHECK (!skel->bss->inode_storage_result,"inode_storage_result" , +"inode_local_storage not set" ); + + serv_sk =start_server (AF_INET6 ,SOCK_STREAM ,NULL ,0 ,0 ); +if (CHECK (serv_sk <0 ,"start_server" ,"failed to start server\n" )) + goto close_prog; + +CHECK (!skel->bss->sk_storage_result,"sk_storage_result" , +"sk_local_storage not set" ); + +close (serv_sk); + +close_prog: +local_storage__destroy (skel); +}
tools/testing/selftests/bpf/progs/local_storage.c new file +136 -0
136 lines not on this page · raw message
Signature · 1 line
2.28.0.rc0.105.gf9edc3c819-goog