Skip to content

Commit f836a56

Browse files
sinkapAlexei Starovoitov
authored and
Alexei Starovoitov
committed
bpf: Generalize bpf_sk_storage
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. This includes the changes suggested by Martin in: https://lore.kernel.org/bpf/[email protected]/ adding new map operations to support bpf_local_storage maps: * storages for different kernel objects to optionally have different memory charging strategy (map_local_storage_charge, map_local_storage_uncharge) * Functionality to extract the storage pointer from a pointer to the owning object (map_owner_storage_ptr) Co-developed-by: Martin KaFai Lau <[email protected]> Signed-off-by: Martin KaFai Lau <[email protected]> Signed-off-by: KP Singh <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 4cc9ce4 commit f836a56

File tree

5 files changed

+228
-86
lines changed

5 files changed

+228
-86
lines changed

include/linux/bpf.h

+8
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ struct btf_type;
3434
struct exception_table_entry;
3535
struct seq_operations;
3636
struct bpf_iter_aux_info;
37+
struct bpf_local_storage;
38+
struct bpf_local_storage_map;
3739

3840
extern struct idr btf_idr;
3941
extern spinlock_t btf_idr_lock;
@@ -104,6 +106,12 @@ struct bpf_map_ops {
104106
__poll_t (*map_poll)(struct bpf_map *map, struct file *filp,
105107
struct poll_table_struct *pts);
106108

109+
/* Functions called by bpf_local_storage maps */
110+
int (*map_local_storage_charge)(struct bpf_local_storage_map *smap,
111+
void *owner, u32 size);
112+
void (*map_local_storage_uncharge)(struct bpf_local_storage_map *smap,
113+
void *owner, u32 size);
114+
struct bpf_local_storage __rcu ** (*map_owner_storage_ptr)(void *owner);
107115
/* BTF name and id of struct allocated by map_alloc */
108116
const char * const map_btf_name;
109117
int *map_btf_id;

include/net/bpf_sk_storage.h

+52
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,15 @@
33
#ifndef _BPF_SK_STORAGE_H
44
#define _BPF_SK_STORAGE_H
55

6+
#include <linux/rculist.h>
7+
#include <linux/list.h>
8+
#include <linux/hash.h>
69
#include <linux/types.h>
710
#include <linux/spinlock.h>
11+
#include <linux/bpf.h>
12+
#include <net/sock.h>
13+
#include <uapi/linux/sock_diag.h>
14+
#include <uapi/linux/btf.h>
815

916
struct sock;
1017

@@ -13,6 +20,7 @@ void bpf_sk_storage_free(struct sock *sk);
1320
extern const struct bpf_func_proto bpf_sk_storage_get_proto;
1421
extern const struct bpf_func_proto bpf_sk_storage_delete_proto;
1522

23+
struct bpf_local_storage_elem;
1624
struct bpf_sk_storage_diag;
1725
struct sk_buff;
1826
struct nlattr;
@@ -34,6 +42,50 @@ u16 bpf_local_storage_cache_idx_get(struct bpf_local_storage_cache *cache);
3442
void bpf_local_storage_cache_idx_free(struct bpf_local_storage_cache *cache,
3543
u16 idx);
3644

45+
/* Helper functions for bpf_local_storage */
46+
int bpf_local_storage_map_alloc_check(union bpf_attr *attr);
47+
48+
struct bpf_local_storage_map *bpf_local_storage_map_alloc(union bpf_attr *attr);
49+
50+
struct bpf_local_storage_data *
51+
bpf_local_storage_lookup(struct bpf_local_storage *local_storage,
52+
struct bpf_local_storage_map *smap,
53+
bool cacheit_lockit);
54+
55+
void bpf_local_storage_map_free(struct bpf_local_storage_map *smap);
56+
57+
int bpf_local_storage_map_check_btf(const struct bpf_map *map,
58+
const struct btf *btf,
59+
const struct btf_type *key_type,
60+
const struct btf_type *value_type);
61+
62+
void bpf_selem_link_storage_nolock(struct bpf_local_storage *local_storage,
63+
struct bpf_local_storage_elem *selem);
64+
65+
bool bpf_selem_unlink_storage_nolock(struct bpf_local_storage *local_storage,
66+
struct bpf_local_storage_elem *selem,
67+
bool uncharge_omem);
68+
69+
void bpf_selem_unlink(struct bpf_local_storage_elem *selem);
70+
71+
void bpf_selem_link_map(struct bpf_local_storage_map *smap,
72+
struct bpf_local_storage_elem *selem);
73+
74+
void bpf_selem_unlink_map(struct bpf_local_storage_elem *selem);
75+
76+
struct bpf_local_storage_elem *
77+
bpf_selem_alloc(struct bpf_local_storage_map *smap, void *owner, void *value,
78+
bool charge_mem);
79+
80+
int
81+
bpf_local_storage_alloc(void *owner,
82+
struct bpf_local_storage_map *smap,
83+
struct bpf_local_storage_elem *first_selem);
84+
85+
struct bpf_local_storage_data *
86+
bpf_local_storage_update(void *owner, struct bpf_local_storage_map *smap,
87+
void *value, u64 map_flags);
88+
3789
#ifdef CONFIG_BPF_SYSCALL
3890
int bpf_sk_storage_clone(const struct sock *sk, struct sock *newsk);
3991
struct bpf_sk_storage_diag *

include/uapi/linux/bpf.h

+6-2
Original file line numberDiff line numberDiff line change
@@ -3765,9 +3765,13 @@ enum {
37653765
BPF_F_SYSCTL_BASE_NAME = (1ULL << 0),
37663766
};
37673767

3768-
/* BPF_FUNC_sk_storage_get flags */
3768+
/* BPF_FUNC_<kernel_obj>_storage_get flags */
37693769
enum {
3770-
BPF_SK_STORAGE_GET_F_CREATE = (1ULL << 0),
3770+
BPF_LOCAL_STORAGE_GET_F_CREATE = (1ULL << 0),
3771+
/* BPF_SK_STORAGE_GET_F_CREATE is only kept for backward compatibility
3772+
* and BPF_LOCAL_STORAGE_GET_F_CREATE must be used instead.
3773+
*/
3774+
BPF_SK_STORAGE_GET_F_CREATE = BPF_LOCAL_STORAGE_GET_F_CREATE,
37713775
};
37723776

37733777
/* BPF_FUNC_read_branch_records flags. */

0 commit comments

Comments
 (0)