From: Mykyta Yatsenko 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. Signed-off-by: Mykyta Yatsenko --- 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(-) diff --git a/include/linux/bpf_local_storage.h b/include/linux/bpf_local_storage.h index d482ff6a1448..4d9ae3a11b94 100644 --- a/include/linux/bpf_local_storage.h +++ b/include/linux/bpf_local_storage.h @@ -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]; diff --git a/include/uapi/linux/bpf.h b/include/uapi/linux/bpf.h index 62de0919db72..9eac0930af95 100644 --- a/include/uapi/linux/bpf.h +++ b/include/uapi/linux/bpf.h @@ -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. */ diff --git a/kernel/bpf/bpf_local_storage.c b/kernel/bpf/bpf_local_storage.c index 31f62a2be62b..aaeaafe60e6d 100644 --- a/kernel/bpf/bpf_local_storage.c +++ b/kernel/bpf/bpf_local_storage.c @@ -15,7 +15,8 @@ #include #include -#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; } diff --git a/tools/include/uapi/linux/bpf.h b/tools/include/uapi/linux/bpf.h index 62de0919db72..9eac0930af95 100644 --- a/tools/include/uapi/linux/bpf.h +++ b/tools/include/uapi/linux/bpf.h @@ -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. */ -- 2.53.1-Meta