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>
20 #include <linux/rcupdate_trace.h>
22 DEFINE_BPF_STORAGE_CACHE(inode_cache);
24 static struct bpf_local_storage __rcu **
25 inode_storage_ptr(void *owner)
27 struct inode *inode = owner;
28 struct bpf_storage_blob *bsb;
30 bsb = bpf_inode(inode);
36 static struct bpf_local_storage_data *inode_storage_lookup(struct inode *inode,
40 struct bpf_local_storage *inode_storage;
41 struct bpf_local_storage_map *smap;
42 struct bpf_storage_blob *bsb;
44 bsb = bpf_inode(inode);
49 rcu_dereference_check(bsb->storage, bpf_rcu_lock_held());
53 smap = (struct bpf_local_storage_map *)map;
54 return bpf_local_storage_lookup(inode_storage, smap, cacheit_lockit);
57 void bpf_inode_storage_free(struct inode *inode)
59 struct bpf_local_storage *local_storage;
60 bool free_inode_storage = false;
61 struct bpf_storage_blob *bsb;
63 bsb = bpf_inode(inode);
69 local_storage = rcu_dereference(bsb->storage);
75 raw_spin_lock_bh(&local_storage->lock);
76 free_inode_storage = bpf_local_storage_unlink_nolock(local_storage);
77 raw_spin_unlock_bh(&local_storage->lock);
80 if (free_inode_storage)
81 kfree_rcu(local_storage, rcu);
84 static void *bpf_fd_inode_storage_lookup_elem(struct bpf_map *map, void *key)
86 struct bpf_local_storage_data *sdata;
93 return ERR_PTR(-EBADF);
95 sdata = inode_storage_lookup(f->f_inode, map, true);
97 return sdata ? sdata->data : NULL;
100 static int bpf_fd_inode_storage_update_elem(struct bpf_map *map, void *key,
101 void *value, u64 map_flags)
103 struct bpf_local_storage_data *sdata;
111 if (!inode_storage_ptr(f->f_inode)) {
116 sdata = bpf_local_storage_update(f->f_inode,
117 (struct bpf_local_storage_map *)map,
118 value, map_flags, GFP_ATOMIC);
120 return PTR_ERR_OR_ZERO(sdata);
123 static int inode_storage_delete(struct inode *inode, struct bpf_map *map)
125 struct bpf_local_storage_data *sdata;
127 sdata = inode_storage_lookup(inode, map, false);
131 bpf_selem_unlink(SELEM(sdata), true);
136 static int bpf_fd_inode_storage_delete_elem(struct bpf_map *map, void *key)
146 err = inode_storage_delete(f->f_inode, map);
151 /* *gfp_flags* is a hidden argument provided by the verifier */
152 BPF_CALL_5(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, inode,
153 void *, value, u64, flags, gfp_t, gfp_flags)
155 struct bpf_local_storage_data *sdata;
157 WARN_ON_ONCE(!bpf_rcu_lock_held());
158 if (flags & ~(BPF_LOCAL_STORAGE_GET_F_CREATE))
159 return (unsigned long)NULL;
161 /* explicitly check that the inode_storage_ptr is not
162 * NULL as inode_storage_lookup returns NULL in this case and
163 * bpf_local_storage_update expects the owner to have a
164 * valid storage pointer.
166 if (!inode || !inode_storage_ptr(inode))
167 return (unsigned long)NULL;
169 sdata = inode_storage_lookup(inode, map, true);
171 return (unsigned long)sdata->data;
173 /* This helper must only called from where the inode is guaranteed
174 * to have a refcount and cannot be freed.
176 if (flags & BPF_LOCAL_STORAGE_GET_F_CREATE) {
177 sdata = bpf_local_storage_update(
178 inode, (struct bpf_local_storage_map *)map, value,
179 BPF_NOEXIST, gfp_flags);
180 return IS_ERR(sdata) ? (unsigned long)NULL :
181 (unsigned long)sdata->data;
184 return (unsigned long)NULL;
187 BPF_CALL_2(bpf_inode_storage_delete,
188 struct bpf_map *, map, struct inode *, inode)
190 WARN_ON_ONCE(!bpf_rcu_lock_held());
194 /* This helper must only called from where the inode is guaranteed
195 * to have a refcount and cannot be freed.
197 return inode_storage_delete(inode, map);
200 static int notsupp_get_next_key(struct bpf_map *map, void *key,
206 static struct bpf_map *inode_storage_map_alloc(union bpf_attr *attr)
208 return bpf_local_storage_map_alloc(attr, &inode_cache);
211 static void inode_storage_map_free(struct bpf_map *map)
213 bpf_local_storage_map_free(map, &inode_cache, NULL);
216 const struct bpf_map_ops inode_storage_map_ops = {
217 .map_meta_equal = bpf_map_meta_equal,
218 .map_alloc_check = bpf_local_storage_map_alloc_check,
219 .map_alloc = inode_storage_map_alloc,
220 .map_free = inode_storage_map_free,
221 .map_get_next_key = notsupp_get_next_key,
222 .map_lookup_elem = bpf_fd_inode_storage_lookup_elem,
223 .map_update_elem = bpf_fd_inode_storage_update_elem,
224 .map_delete_elem = bpf_fd_inode_storage_delete_elem,
225 .map_check_btf = bpf_local_storage_map_check_btf,
226 .map_btf_id = &bpf_local_storage_map_btf_id[0],
227 .map_owner_storage_ptr = inode_storage_ptr,
230 BTF_ID_LIST_SINGLE(bpf_inode_storage_btf_ids, struct, inode)
232 const struct bpf_func_proto bpf_inode_storage_get_proto = {
233 .func = bpf_inode_storage_get,
235 .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
236 .arg1_type = ARG_CONST_MAP_PTR,
237 .arg2_type = ARG_PTR_TO_BTF_ID,
238 .arg2_btf_id = &bpf_inode_storage_btf_ids[0],
239 .arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL,
240 .arg4_type = ARG_ANYTHING,
243 const struct bpf_func_proto bpf_inode_storage_delete_proto = {
244 .func = bpf_inode_storage_delete,
246 .ret_type = RET_INTEGER,
247 .arg1_type = ARG_CONST_MAP_PTR,
248 .arg2_type = ARG_PTR_TO_BTF_ID,
249 .arg2_btf_id = &bpf_inode_storage_btf_ids[0],