mirror of
https://github.com/netdata/libbpf.git
synced 2026-04-06 00:29:07 +08:00
bpf: Implement bpf_local_storage for inodes
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. Signed-off-by: KP Singh <kpsingh@google.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20200825182919.1118197-6-kpsingh@chromium.org
This commit is contained in:
committed by
Andrii Nakryiko
parent
2bd0d158d4
commit
e565f2bfe9
@@ -155,6 +155,7 @@ enum bpf_map_type {
|
|||||||
BPF_MAP_TYPE_DEVMAP_HASH,
|
BPF_MAP_TYPE_DEVMAP_HASH,
|
||||||
BPF_MAP_TYPE_STRUCT_OPS,
|
BPF_MAP_TYPE_STRUCT_OPS,
|
||||||
BPF_MAP_TYPE_RINGBUF,
|
BPF_MAP_TYPE_RINGBUF,
|
||||||
|
BPF_MAP_TYPE_INODE_STORAGE,
|
||||||
};
|
};
|
||||||
|
|
||||||
/* Note that tracing related programs such as
|
/* Note that tracing related programs such as
|
||||||
@@ -3509,6 +3510,41 @@ union bpf_attr {
|
|||||||
*
|
*
|
||||||
* **-EPERM** This helper cannot be used under the
|
* **-EPERM** This helper cannot be used under the
|
||||||
* current sock_ops->op.
|
* current sock_ops->op.
|
||||||
|
* 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) \
|
#define __BPF_FUNC_MAPPER(FN) \
|
||||||
FN(unspec), \
|
FN(unspec), \
|
||||||
@@ -3655,7 +3691,9 @@ union bpf_attr {
|
|||||||
FN(get_task_stack), \
|
FN(get_task_stack), \
|
||||||
FN(load_hdr_opt), \
|
FN(load_hdr_opt), \
|
||||||
FN(store_hdr_opt), \
|
FN(store_hdr_opt), \
|
||||||
FN(reserve_hdr_opt),
|
FN(reserve_hdr_opt), \
|
||||||
|
FN(inode_storage_get), \
|
||||||
|
FN(inode_storage_delete), \
|
||||||
/* */
|
/* */
|
||||||
|
|
||||||
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
|
/* integer value in 'imm' field of BPF_CALL instruction selects which helper
|
||||||
|
|||||||
@@ -170,7 +170,7 @@ int libbpf__load_raw_btf(const char *raw_types, size_t types_len,
|
|||||||
return btf_fd;
|
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";
|
const char strs[] = "\0bpf_spin_lock\0val\0cnt\0l";
|
||||||
/* struct bpf_spin_lock {
|
/* struct bpf_spin_lock {
|
||||||
@@ -229,12 +229,13 @@ bool bpf_probe_map_type(enum bpf_map_type map_type, __u32 ifindex)
|
|||||||
key_size = 0;
|
key_size = 0;
|
||||||
break;
|
break;
|
||||||
case BPF_MAP_TYPE_SK_STORAGE:
|
case BPF_MAP_TYPE_SK_STORAGE:
|
||||||
|
case BPF_MAP_TYPE_INODE_STORAGE:
|
||||||
btf_key_type_id = 1;
|
btf_key_type_id = 1;
|
||||||
btf_value_type_id = 3;
|
btf_value_type_id = 3;
|
||||||
value_size = 8;
|
value_size = 8;
|
||||||
max_entries = 0;
|
max_entries = 0;
|
||||||
map_flags = BPF_F_NO_PREALLOC;
|
map_flags = BPF_F_NO_PREALLOC;
|
||||||
btf_fd = load_sk_storage_btf();
|
btf_fd = load_local_storage_btf();
|
||||||
if (btf_fd < 0)
|
if (btf_fd < 0)
|
||||||
return false;
|
return false;
|
||||||
break;
|
break;
|
||||||
|
|||||||
Reference in New Issue
Block a user