1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2019 Facebook
4 * Copyright 2020 Google LLC.
7 #include <linux/rculist.h>
8 #include <linux/list.h>
9 #include <linux/hash.h>
10 #include <linux/types.h>
11 #include <linux/spinlock.h>
12 #include <linux/bpf.h>
13 #include <linux/bpf_local_storage.h>
15 #include <uapi/linux/sock_diag.h>
16 #include <uapi/linux/btf.h>
17 #include <linux/bpf_lsm.h>
18 #include <linux/btf_ids.h>
19 #include <linux/fdtable.h>
21 DEFINE_BPF_STORAGE_CACHE(inode_cache);
23 static struct bpf_local_storage __rcu **
24 inode_storage_ptr(void *owner)
26 struct inode *inode = owner;
27 struct bpf_storage_blob *bsb;
29 bsb = bpf_inode(inode);
35 static struct bpf_local_storage_data *inode_storage_lookup(struct inode *inode,
39 struct bpf_local_storage *inode_storage;
40 struct bpf_local_storage_map *smap;
41 struct bpf_storage_blob *bsb;
43 bsb = bpf_inode(inode);
47 inode_storage = rcu_dereference(bsb->storage);
51 smap = (struct bpf_local_storage_map *)map;
52 return bpf_local_storage_lookup(inode_storage, smap, cacheit_lockit);
55 void bpf_inode_storage_free(struct inode *inode)
57 struct bpf_local_storage_elem *selem;
58 struct bpf_local_storage *local_storage;
59 bool free_inode_storage = false;
60 struct bpf_storage_blob *bsb;
63 bsb = bpf_inode(inode);
69 local_storage = rcu_dereference(bsb->storage);
75 /* Netiher the bpf_prog nor the bpf-map's syscall
76 * could be modifying the local_storage->list now.
77 * Thus, no elem can be added-to or deleted-from the
78 * local_storage->list by the bpf_prog or by the bpf-map's syscall.
80 * It is racing with bpf_local_storage_map_free() alone
81 * when unlinking elem from the local_storage->list and
82 * the map's bucket->list.
84 raw_spin_lock_bh(&local_storage->lock);
85 hlist_for_each_entry_safe(selem, n, &local_storage->list, snode) {
86 /* Always unlink from map before unlinking from
89 bpf_selem_unlink_map(selem);
90 free_inode_storage = bpf_selem_unlink_storage_nolock(
91 local_storage, selem, false);
93 raw_spin_unlock_bh(&local_storage->lock);
96 /* free_inoode_storage should always be true as long as
97 * local_storage->list was non-empty.
99 if (free_inode_storage)
100 kfree_rcu(local_storage, rcu);
103 static void *bpf_fd_inode_storage_lookup_elem(struct bpf_map *map, void *key)
105 struct bpf_local_storage_data *sdata;
112 return ERR_PTR(-EBADF);
114 sdata = inode_storage_lookup(f->f_inode, map, true);
116 return sdata ? sdata->data : NULL;
119 static int bpf_fd_inode_storage_update_elem(struct bpf_map *map, void *key,
120 void *value, u64 map_flags)
122 struct bpf_local_storage_data *sdata;
130 if (!inode_storage_ptr(f->f_inode)) {
135 sdata = bpf_local_storage_update(f->f_inode,
136 (struct bpf_local_storage_map *)map,
139 return PTR_ERR_OR_ZERO(sdata);
142 static int inode_storage_delete(struct inode *inode, struct bpf_map *map)
144 struct bpf_local_storage_data *sdata;
146 sdata = inode_storage_lookup(inode, map, false);
150 bpf_selem_unlink(SELEM(sdata));
155 static int bpf_fd_inode_storage_delete_elem(struct bpf_map *map, void *key)
165 err = inode_storage_delete(f->f_inode, map);
170 BPF_CALL_4(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, inode,
171 void *, value, u64, flags)
173 struct bpf_local_storage_data *sdata;
175 if (flags & ~(BPF_LOCAL_STORAGE_GET_F_CREATE))
176 return (unsigned long)NULL;
178 /* explicitly check that the inode_storage_ptr is not
179 * NULL as inode_storage_lookup returns NULL in this case and
180 * bpf_local_storage_update expects the owner to have a
181 * valid storage pointer.
183 if (!inode || !inode_storage_ptr(inode))
184 return (unsigned long)NULL;
186 sdata = inode_storage_lookup(inode, map, true);
188 return (unsigned long)sdata->data;
190 /* This helper must only called from where the inode is guaranteed
191 * to have a refcount and cannot be freed.
193 if (flags & BPF_LOCAL_STORAGE_GET_F_CREATE) {
194 sdata = bpf_local_storage_update(
195 inode, (struct bpf_local_storage_map *)map, value,
197 return IS_ERR(sdata) ? (unsigned long)NULL :
198 (unsigned long)sdata->data;
201 return (unsigned long)NULL;
204 BPF_CALL_2(bpf_inode_storage_delete,
205 struct bpf_map *, map, struct inode *, inode)
210 /* This helper must only called from where the inode is guaranteed
211 * to have a refcount and cannot be freed.
213 return inode_storage_delete(inode, map);
216 static int notsupp_get_next_key(struct bpf_map *map, void *key,
222 static struct bpf_map *inode_storage_map_alloc(union bpf_attr *attr)
224 struct bpf_local_storage_map *smap;
226 smap = bpf_local_storage_map_alloc(attr);
228 return ERR_CAST(smap);
230 smap->cache_idx = bpf_local_storage_cache_idx_get(&inode_cache);
234 static void inode_storage_map_free(struct bpf_map *map)
236 struct bpf_local_storage_map *smap;
238 smap = (struct bpf_local_storage_map *)map;
239 bpf_local_storage_cache_idx_free(&inode_cache, smap->cache_idx);
240 bpf_local_storage_map_free(smap, NULL);
243 static int inode_storage_map_btf_id;
244 const struct bpf_map_ops inode_storage_map_ops = {
245 .map_meta_equal = bpf_map_meta_equal,
246 .map_alloc_check = bpf_local_storage_map_alloc_check,
247 .map_alloc = inode_storage_map_alloc,
248 .map_free = inode_storage_map_free,
249 .map_get_next_key = notsupp_get_next_key,
250 .map_lookup_elem = bpf_fd_inode_storage_lookup_elem,
251 .map_update_elem = bpf_fd_inode_storage_update_elem,
252 .map_delete_elem = bpf_fd_inode_storage_delete_elem,
253 .map_check_btf = bpf_local_storage_map_check_btf,
254 .map_btf_name = "bpf_local_storage_map",
255 .map_btf_id = &inode_storage_map_btf_id,
256 .map_owner_storage_ptr = inode_storage_ptr,
259 BTF_ID_LIST_SINGLE(bpf_inode_storage_btf_ids, struct, inode)
261 const struct bpf_func_proto bpf_inode_storage_get_proto = {
262 .func = bpf_inode_storage_get,
264 .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
265 .arg1_type = ARG_CONST_MAP_PTR,
266 .arg2_type = ARG_PTR_TO_BTF_ID,
267 .arg2_btf_id = &bpf_inode_storage_btf_ids[0],
268 .arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL,
269 .arg4_type = ARG_ANYTHING,
272 const struct bpf_func_proto bpf_inode_storage_delete_proto = {
273 .func = bpf_inode_storage_delete,
275 .ret_type = RET_INTEGER,
276 .arg1_type = ARG_CONST_MAP_PTR,
277 .arg2_type = ARG_PTR_TO_BTF_ID,
278 .arg2_btf_id = &bpf_inode_storage_btf_ids[0],