18ea63684SKP Singh // SPDX-License-Identifier: GPL-2.0
28ea63684SKP Singh /*
38ea63684SKP Singh * Copyright (c) 2019 Facebook
48ea63684SKP Singh * Copyright 2020 Google LLC.
58ea63684SKP Singh */
68ea63684SKP Singh
78ea63684SKP Singh #include <linux/rculist.h>
88ea63684SKP Singh #include <linux/list.h>
98ea63684SKP Singh #include <linux/hash.h>
108ea63684SKP Singh #include <linux/types.h>
118ea63684SKP Singh #include <linux/spinlock.h>
128ea63684SKP Singh #include <linux/bpf.h>
138ea63684SKP Singh #include <linux/bpf_local_storage.h>
148ea63684SKP Singh #include <net/sock.h>
158ea63684SKP Singh #include <uapi/linux/sock_diag.h>
168ea63684SKP Singh #include <uapi/linux/btf.h>
178ea63684SKP Singh #include <linux/bpf_lsm.h>
188ea63684SKP Singh #include <linux/btf_ids.h>
198ea63684SKP Singh #include <linux/fdtable.h>
200fe4b381SKP Singh #include <linux/rcupdate_trace.h>
218ea63684SKP Singh
228ea63684SKP Singh DEFINE_BPF_STORAGE_CACHE(inode_cache);
238ea63684SKP Singh
248ea63684SKP Singh static struct bpf_local_storage __rcu **
inode_storage_ptr(void * owner)258ea63684SKP Singh inode_storage_ptr(void *owner)
268ea63684SKP Singh {
278ea63684SKP Singh struct inode *inode = owner;
288ea63684SKP Singh struct bpf_storage_blob *bsb;
298ea63684SKP Singh
308ea63684SKP Singh bsb = bpf_inode(inode);
318ea63684SKP Singh if (!bsb)
328ea63684SKP Singh return NULL;
338ea63684SKP Singh return &bsb->storage;
348ea63684SKP Singh }
358ea63684SKP Singh
inode_storage_lookup(struct inode * inode,struct bpf_map * map,bool cacheit_lockit)368ea63684SKP Singh static struct bpf_local_storage_data *inode_storage_lookup(struct inode *inode,
378ea63684SKP Singh struct bpf_map *map,
388ea63684SKP Singh bool cacheit_lockit)
398ea63684SKP Singh {
408ea63684SKP Singh struct bpf_local_storage *inode_storage;
418ea63684SKP Singh struct bpf_local_storage_map *smap;
428ea63684SKP Singh struct bpf_storage_blob *bsb;
438ea63684SKP Singh
448ea63684SKP Singh bsb = bpf_inode(inode);
458ea63684SKP Singh if (!bsb)
468ea63684SKP Singh return NULL;
478ea63684SKP Singh
480fe4b381SKP Singh inode_storage =
490fe4b381SKP Singh rcu_dereference_check(bsb->storage, bpf_rcu_lock_held());
508ea63684SKP Singh if (!inode_storage)
518ea63684SKP Singh return NULL;
528ea63684SKP Singh
538ea63684SKP Singh smap = (struct bpf_local_storage_map *)map;
548ea63684SKP Singh return bpf_local_storage_lookup(inode_storage, smap, cacheit_lockit);
558ea63684SKP Singh }
568ea63684SKP Singh
bpf_inode_storage_free(struct inode * inode)578ea63684SKP Singh void bpf_inode_storage_free(struct inode *inode)
588ea63684SKP Singh {
598ea63684SKP Singh struct bpf_local_storage *local_storage;
608ea63684SKP Singh struct bpf_storage_blob *bsb;
618ea63684SKP Singh
628ea63684SKP Singh bsb = bpf_inode(inode);
638ea63684SKP Singh if (!bsb)
648ea63684SKP Singh return;
658ea63684SKP Singh
668ea63684SKP Singh rcu_read_lock();
678ea63684SKP Singh
688ea63684SKP Singh local_storage = rcu_dereference(bsb->storage);
698ea63684SKP Singh if (!local_storage) {
708ea63684SKP Singh rcu_read_unlock();
718ea63684SKP Singh return;
728ea63684SKP Singh }
738ea63684SKP Singh
742ffcb6fcSMartin KaFai Lau bpf_local_storage_destroy(local_storage);
758ea63684SKP Singh rcu_read_unlock();
768ea63684SKP Singh }
778ea63684SKP Singh
bpf_fd_inode_storage_lookup_elem(struct bpf_map * map,void * key)788ea63684SKP Singh static void *bpf_fd_inode_storage_lookup_elem(struct bpf_map *map, void *key)
798ea63684SKP Singh {
808ea63684SKP Singh struct bpf_local_storage_data *sdata;
81*1d0027dcSAl Viro struct fd f = fdget_raw(*(int *)key);
828ea63684SKP Singh
83*1d0027dcSAl Viro if (!f.file)
84769c18b2STal Lossos return ERR_PTR(-EBADF);
858ea63684SKP Singh
86*1d0027dcSAl Viro sdata = inode_storage_lookup(file_inode(f.file), map, true);
87*1d0027dcSAl Viro fdput(f);
888ea63684SKP Singh return sdata ? sdata->data : NULL;
898ea63684SKP Singh }
908ea63684SKP Singh
bpf_fd_inode_storage_update_elem(struct bpf_map * map,void * key,void * value,u64 map_flags)91d7ba4cc9SJP Kobryn static long bpf_fd_inode_storage_update_elem(struct bpf_map *map, void *key,
928ea63684SKP Singh void *value, u64 map_flags)
938ea63684SKP Singh {
948ea63684SKP Singh struct bpf_local_storage_data *sdata;
95*1d0027dcSAl Viro struct fd f = fdget_raw(*(int *)key);
968ea63684SKP Singh
97*1d0027dcSAl Viro if (!f.file)
988ea63684SKP Singh return -EBADF;
99*1d0027dcSAl Viro if (!inode_storage_ptr(file_inode(f.file))) {
100*1d0027dcSAl Viro fdput(f);
101b9557caaSPan Bian return -EBADF;
102b9557caaSPan Bian }
1038ea63684SKP Singh
104*1d0027dcSAl Viro sdata = bpf_local_storage_update(file_inode(f.file),
1058ea63684SKP Singh (struct bpf_local_storage_map *)map,
106b00fa38aSJoanne Koong value, map_flags, GFP_ATOMIC);
107*1d0027dcSAl Viro fdput(f);
1088ea63684SKP Singh return PTR_ERR_OR_ZERO(sdata);
1098ea63684SKP Singh }
1108ea63684SKP Singh
inode_storage_delete(struct inode * inode,struct bpf_map * map)1118ea63684SKP Singh static int inode_storage_delete(struct inode *inode, struct bpf_map *map)
1128ea63684SKP Singh {
1138ea63684SKP Singh struct bpf_local_storage_data *sdata;
1148ea63684SKP Singh
1158ea63684SKP Singh sdata = inode_storage_lookup(inode, map, false);
1168ea63684SKP Singh if (!sdata)
1178ea63684SKP Singh return -ENOENT;
1188ea63684SKP Singh
119a47eabf2SMartin KaFai Lau bpf_selem_unlink(SELEM(sdata), false);
1208ea63684SKP Singh
1218ea63684SKP Singh return 0;
1228ea63684SKP Singh }
1238ea63684SKP Singh
bpf_fd_inode_storage_delete_elem(struct bpf_map * map,void * key)124d7ba4cc9SJP Kobryn static long bpf_fd_inode_storage_delete_elem(struct bpf_map *map, void *key)
1258ea63684SKP Singh {
126*1d0027dcSAl Viro struct fd f = fdget_raw(*(int *)key);
127*1d0027dcSAl Viro int err;
1288ea63684SKP Singh
129*1d0027dcSAl Viro if (!f.file)
1308ea63684SKP Singh return -EBADF;
1318ea63684SKP Singh
132*1d0027dcSAl Viro err = inode_storage_delete(file_inode(f.file), map);
133*1d0027dcSAl Viro fdput(f);
1348ea63684SKP Singh return err;
1358ea63684SKP Singh }
1368ea63684SKP Singh
137b00fa38aSJoanne Koong /* *gfp_flags* is a hidden argument provided by the verifier */
BPF_CALL_5(bpf_inode_storage_get,struct bpf_map *,map,struct inode *,inode,void *,value,u64,flags,gfp_t,gfp_flags)138b00fa38aSJoanne Koong BPF_CALL_5(bpf_inode_storage_get, struct bpf_map *, map, struct inode *, inode,
139b00fa38aSJoanne Koong void *, value, u64, flags, gfp_t, gfp_flags)
1408ea63684SKP Singh {
1418ea63684SKP Singh struct bpf_local_storage_data *sdata;
1428ea63684SKP Singh
1430fe4b381SKP Singh WARN_ON_ONCE(!bpf_rcu_lock_held());
1448ea63684SKP Singh if (flags & ~(BPF_LOCAL_STORAGE_GET_F_CREATE))
1458ea63684SKP Singh return (unsigned long)NULL;
1468ea63684SKP Singh
1478ea63684SKP Singh /* explicitly check that the inode_storage_ptr is not
1488ea63684SKP Singh * NULL as inode_storage_lookup returns NULL in this case and
1498ea63684SKP Singh * bpf_local_storage_update expects the owner to have a
1508ea63684SKP Singh * valid storage pointer.
1518ea63684SKP Singh */
1521a9c72adSKP Singh if (!inode || !inode_storage_ptr(inode))
1538ea63684SKP Singh return (unsigned long)NULL;
1548ea63684SKP Singh
1558ea63684SKP Singh sdata = inode_storage_lookup(inode, map, true);
1568ea63684SKP Singh if (sdata)
1578ea63684SKP Singh return (unsigned long)sdata->data;
1588ea63684SKP Singh
15984d571d4SKP Singh /* This helper must only called from where the inode is guaranteed
1608ea63684SKP Singh * to have a refcount and cannot be freed.
1618ea63684SKP Singh */
1628ea63684SKP Singh if (flags & BPF_LOCAL_STORAGE_GET_F_CREATE) {
1638ea63684SKP Singh sdata = bpf_local_storage_update(
1648ea63684SKP Singh inode, (struct bpf_local_storage_map *)map, value,
165b00fa38aSJoanne Koong BPF_NOEXIST, gfp_flags);
1668ea63684SKP Singh return IS_ERR(sdata) ? (unsigned long)NULL :
1678ea63684SKP Singh (unsigned long)sdata->data;
1688ea63684SKP Singh }
1698ea63684SKP Singh
1708ea63684SKP Singh return (unsigned long)NULL;
1718ea63684SKP Singh }
1728ea63684SKP Singh
BPF_CALL_2(bpf_inode_storage_delete,struct bpf_map *,map,struct inode *,inode)1738ea63684SKP Singh BPF_CALL_2(bpf_inode_storage_delete,
1748ea63684SKP Singh struct bpf_map *, map, struct inode *, inode)
1758ea63684SKP Singh {
1760fe4b381SKP Singh WARN_ON_ONCE(!bpf_rcu_lock_held());
1771a9c72adSKP Singh if (!inode)
1781a9c72adSKP Singh return -EINVAL;
1791a9c72adSKP Singh
18084d571d4SKP Singh /* This helper must only called from where the inode is guaranteed
1818ea63684SKP Singh * to have a refcount and cannot be freed.
1828ea63684SKP Singh */
1838ea63684SKP Singh return inode_storage_delete(inode, map);
1848ea63684SKP Singh }
1858ea63684SKP Singh
notsupp_get_next_key(struct bpf_map * map,void * key,void * next_key)1868ea63684SKP Singh static int notsupp_get_next_key(struct bpf_map *map, void *key,
1878ea63684SKP Singh void *next_key)
1888ea63684SKP Singh {
1898ea63684SKP Singh return -ENOTSUPP;
1908ea63684SKP Singh }
1918ea63684SKP Singh
inode_storage_map_alloc(union bpf_attr * attr)1928ea63684SKP Singh static struct bpf_map *inode_storage_map_alloc(union bpf_attr *attr)
1938ea63684SKP Singh {
19408a7ce38SMartin KaFai Lau return bpf_local_storage_map_alloc(attr, &inode_cache, false);
1958ea63684SKP Singh }
1968ea63684SKP Singh
inode_storage_map_free(struct bpf_map * map)1978ea63684SKP Singh static void inode_storage_map_free(struct bpf_map *map)
1988ea63684SKP Singh {
199c83597faSYonghong Song bpf_local_storage_map_free(map, &inode_cache, NULL);
2008ea63684SKP Singh }
2018ea63684SKP Singh
2028ea63684SKP Singh const struct bpf_map_ops inode_storage_map_ops = {
203f4d05259SMartin KaFai Lau .map_meta_equal = bpf_map_meta_equal,
2048ea63684SKP Singh .map_alloc_check = bpf_local_storage_map_alloc_check,
2058ea63684SKP Singh .map_alloc = inode_storage_map_alloc,
2068ea63684SKP Singh .map_free = inode_storage_map_free,
2078ea63684SKP Singh .map_get_next_key = notsupp_get_next_key,
2088ea63684SKP Singh .map_lookup_elem = bpf_fd_inode_storage_lookup_elem,
2098ea63684SKP Singh .map_update_elem = bpf_fd_inode_storage_update_elem,
2108ea63684SKP Singh .map_delete_elem = bpf_fd_inode_storage_delete_elem,
2118ea63684SKP Singh .map_check_btf = bpf_local_storage_map_check_btf,
2127490b7f1SYafang Shao .map_mem_usage = bpf_local_storage_map_mem_usage,
2133144bfa5SYonghong Song .map_btf_id = &bpf_local_storage_map_btf_id[0],
2148ea63684SKP Singh .map_owner_storage_ptr = inode_storage_ptr,
2158ea63684SKP Singh };
2168ea63684SKP Singh
2179436ef6eSLorenz Bauer BTF_ID_LIST_SINGLE(bpf_inode_storage_btf_ids, struct, inode)
2188ea63684SKP Singh
2198ea63684SKP Singh const struct bpf_func_proto bpf_inode_storage_get_proto = {
2208ea63684SKP Singh .func = bpf_inode_storage_get,
2218ea63684SKP Singh .gpl_only = false,
2228ea63684SKP Singh .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL,
2238ea63684SKP Singh .arg1_type = ARG_CONST_MAP_PTR,
22491571a51SAlexei Starovoitov .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL,
2259436ef6eSLorenz Bauer .arg2_btf_id = &bpf_inode_storage_btf_ids[0],
2268ea63684SKP Singh .arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL,
2278ea63684SKP Singh .arg4_type = ARG_ANYTHING,
2288ea63684SKP Singh };
2298ea63684SKP Singh
2308ea63684SKP Singh const struct bpf_func_proto bpf_inode_storage_delete_proto = {
2318ea63684SKP Singh .func = bpf_inode_storage_delete,
2328ea63684SKP Singh .gpl_only = false,
2338ea63684SKP Singh .ret_type = RET_INTEGER,
2348ea63684SKP Singh .arg1_type = ARG_CONST_MAP_PTR,
23591571a51SAlexei Starovoitov .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL,
2369436ef6eSLorenz Bauer .arg2_btf_id = &bpf_inode_storage_btf_ids[0],
2378ea63684SKP Singh };
238