1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Copyright (c) 2022 Meta Platforms, Inc. and affiliates. 4 */ 5 6 #include <linux/types.h> 7 #include <linux/bpf.h> 8 #include <linux/bpf_local_storage.h> 9 #include <uapi/linux/btf.h> 10 #include <linux/btf_ids.h> 11 12 DEFINE_BPF_STORAGE_CACHE(cgroup_cache); 13 14 static struct bpf_local_storage __rcu **cgroup_storage_ptr(void *owner) 15 { 16 struct cgroup *cg = owner; 17 18 return &cg->bpf_cgrp_storage; 19 } 20 21 void bpf_cgrp_storage_free(struct cgroup *cgroup) 22 { 23 struct bpf_local_storage *local_storage; 24 25 rcu_read_lock(); 26 local_storage = rcu_dereference(cgroup->bpf_cgrp_storage); 27 if (!local_storage) 28 goto out; 29 30 bpf_local_storage_destroy(local_storage); 31 out: 32 rcu_read_unlock(); 33 } 34 35 static struct bpf_local_storage_data * 36 cgroup_storage_lookup(struct cgroup *cgroup, struct bpf_map *map, bool cacheit_lockit) 37 { 38 struct bpf_local_storage *cgroup_storage; 39 struct bpf_local_storage_map *smap; 40 41 cgroup_storage = rcu_dereference_check(cgroup->bpf_cgrp_storage, 42 bpf_rcu_lock_held()); 43 if (!cgroup_storage) 44 return NULL; 45 46 smap = (struct bpf_local_storage_map *)map; 47 return bpf_local_storage_lookup(cgroup_storage, smap, cacheit_lockit); 48 } 49 50 static void *bpf_cgrp_storage_lookup_elem(struct bpf_map *map, void *key) 51 { 52 struct bpf_local_storage_data *sdata; 53 struct cgroup *cgroup; 54 int fd; 55 56 fd = *(int *)key; 57 cgroup = cgroup_v1v2_get_from_fd(fd); 58 if (IS_ERR(cgroup)) 59 return ERR_CAST(cgroup); 60 61 sdata = cgroup_storage_lookup(cgroup, map, true); 62 cgroup_put(cgroup); 63 return sdata ? sdata->data : NULL; 64 } 65 66 static long bpf_cgrp_storage_update_elem(struct bpf_map *map, void *key, 67 void *value, u64 map_flags) 68 { 69 struct bpf_local_storage_data *sdata; 70 struct cgroup *cgroup; 71 int fd; 72 73 fd = *(int *)key; 74 cgroup = cgroup_v1v2_get_from_fd(fd); 75 if (IS_ERR(cgroup)) 76 return PTR_ERR(cgroup); 77 78 sdata = bpf_local_storage_update(cgroup, (struct bpf_local_storage_map *)map, 79 value, map_flags, false, GFP_ATOMIC); 80 cgroup_put(cgroup); 81 return PTR_ERR_OR_ZERO(sdata); 82 } 83 84 static int cgroup_storage_delete(struct cgroup *cgroup, struct bpf_map *map) 85 { 86 struct bpf_local_storage_data *sdata; 87 88 sdata = cgroup_storage_lookup(cgroup, map, false); 89 if (!sdata) 90 return -ENOENT; 91 92 return bpf_selem_unlink(SELEM(sdata)); 93 } 94 95 static long bpf_cgrp_storage_delete_elem(struct bpf_map *map, void *key) 96 { 97 struct cgroup *cgroup; 98 int err, fd; 99 100 fd = *(int *)key; 101 cgroup = cgroup_v1v2_get_from_fd(fd); 102 if (IS_ERR(cgroup)) 103 return PTR_ERR(cgroup); 104 105 err = cgroup_storage_delete(cgroup, map); 106 cgroup_put(cgroup); 107 return err; 108 } 109 110 static int notsupp_get_next_key(struct bpf_map *map, void *key, void *next_key) 111 { 112 return -ENOTSUPP; 113 } 114 115 static struct bpf_map *cgroup_storage_map_alloc(union bpf_attr *attr) 116 { 117 return bpf_local_storage_map_alloc(attr, &cgroup_cache, true); 118 } 119 120 static void cgroup_storage_map_free(struct bpf_map *map) 121 { 122 bpf_local_storage_map_free(map, &cgroup_cache); 123 } 124 125 /* *gfp_flags* is a hidden argument provided by the verifier */ 126 BPF_CALL_5(bpf_cgrp_storage_get, struct bpf_map *, map, struct cgroup *, cgroup, 127 void *, value, u64, flags, gfp_t, gfp_flags) 128 { 129 struct bpf_local_storage_data *sdata; 130 131 WARN_ON_ONCE(!bpf_rcu_lock_held()); 132 if (flags & ~(BPF_LOCAL_STORAGE_GET_F_CREATE)) 133 return (unsigned long)NULL; 134 135 if (!cgroup) 136 return (unsigned long)NULL; 137 138 sdata = cgroup_storage_lookup(cgroup, map, true); 139 if (sdata) 140 goto out; 141 142 /* only allocate new storage, when the cgroup is refcounted */ 143 if (!percpu_ref_is_dying(&cgroup->self.refcnt) && 144 (flags & BPF_LOCAL_STORAGE_GET_F_CREATE)) 145 sdata = bpf_local_storage_update(cgroup, (struct bpf_local_storage_map *)map, 146 value, BPF_NOEXIST, false, gfp_flags); 147 148 out: 149 return IS_ERR_OR_NULL(sdata) ? (unsigned long)NULL : (unsigned long)sdata->data; 150 } 151 152 BPF_CALL_2(bpf_cgrp_storage_delete, struct bpf_map *, map, struct cgroup *, cgroup) 153 { 154 WARN_ON_ONCE(!bpf_rcu_lock_held()); 155 if (!cgroup) 156 return -EINVAL; 157 158 return cgroup_storage_delete(cgroup, map); 159 } 160 161 const struct bpf_map_ops cgrp_storage_map_ops = { 162 .map_meta_equal = bpf_map_meta_equal, 163 .map_alloc_check = bpf_local_storage_map_alloc_check, 164 .map_alloc = cgroup_storage_map_alloc, 165 .map_free = cgroup_storage_map_free, 166 .map_get_next_key = notsupp_get_next_key, 167 .map_lookup_elem = bpf_cgrp_storage_lookup_elem, 168 .map_update_elem = bpf_cgrp_storage_update_elem, 169 .map_delete_elem = bpf_cgrp_storage_delete_elem, 170 .map_check_btf = bpf_local_storage_map_check_btf, 171 .map_mem_usage = bpf_local_storage_map_mem_usage, 172 .map_btf_id = &bpf_local_storage_map_btf_id[0], 173 .map_owner_storage_ptr = cgroup_storage_ptr, 174 }; 175 176 const struct bpf_func_proto bpf_cgrp_storage_get_proto = { 177 .func = bpf_cgrp_storage_get, 178 .gpl_only = false, 179 .ret_type = RET_PTR_TO_MAP_VALUE_OR_NULL, 180 .arg1_type = ARG_CONST_MAP_PTR, 181 .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL, 182 .arg2_btf_id = &bpf_cgroup_btf_id[0], 183 .arg3_type = ARG_PTR_TO_MAP_VALUE_OR_NULL, 184 .arg4_type = ARG_ANYTHING, 185 }; 186 187 const struct bpf_func_proto bpf_cgrp_storage_delete_proto = { 188 .func = bpf_cgrp_storage_delete, 189 .gpl_only = false, 190 .ret_type = RET_INTEGER, 191 .arg1_type = ARG_CONST_MAP_PTR, 192 .arg2_type = ARG_PTR_TO_BTF_ID_OR_NULL, 193 .arg2_btf_id = &bpf_cgroup_btf_id[0], 194 }; 195