1 /* 1 /* 2 * Copyright (C) 2017-2018 Netronome Systems, 2 * Copyright (C) 2017-2018 Netronome Systems, Inc. 3 * 3 * 4 * This software is licensed under the GNU Gen 4 * This software is licensed under the GNU General License Version 2, 5 * June 1991 as shown in the file COPYING in t 5 * June 1991 as shown in the file COPYING in the top-level directory of this 6 * source tree. 6 * source tree. 7 * 7 * 8 * THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 8 * THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" 9 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRES 9 * WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, 10 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 10 * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 11 * FOR A PARTICULAR PURPOSE. THE ENTIRE RISK A 11 * FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE 12 * OF THE PROGRAM IS WITH YOU. SHOULD THE PROG 12 * OF THE PROGRAM IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME 13 * THE COST OF ALL NECESSARY SERVICING, REPAIR 13 * THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. 14 */ 14 */ 15 15 16 #include <linux/bpf.h> 16 #include <linux/bpf.h> 17 #include <linux/bpf_verifier.h> 17 #include <linux/bpf_verifier.h> 18 #include <linux/bug.h> 18 #include <linux/bug.h> 19 #include <linux/kdev_t.h> 19 #include <linux/kdev_t.h> 20 #include <linux/list.h> 20 #include <linux/list.h> 21 #include <linux/lockdep.h> 21 #include <linux/lockdep.h> 22 #include <linux/netdevice.h> 22 #include <linux/netdevice.h> 23 #include <linux/printk.h> 23 #include <linux/printk.h> 24 #include <linux/proc_ns.h> 24 #include <linux/proc_ns.h> 25 #include <linux/rhashtable.h> 25 #include <linux/rhashtable.h> 26 #include <linux/rtnetlink.h> 26 #include <linux/rtnetlink.h> 27 #include <linux/rwsem.h> 27 #include <linux/rwsem.h> 28 #include <net/xdp.h> 28 #include <net/xdp.h> 29 29 30 /* Protects offdevs, members of bpf_offload_ne 30 /* Protects offdevs, members of bpf_offload_netdev and offload members 31 * of all progs. 31 * of all progs. 32 * RTNL lock cannot be taken when holding this 32 * RTNL lock cannot be taken when holding this lock. 33 */ 33 */ 34 static DECLARE_RWSEM(bpf_devs_lock); 34 static DECLARE_RWSEM(bpf_devs_lock); 35 35 36 struct bpf_offload_dev { 36 struct bpf_offload_dev { 37 const struct bpf_prog_offload_ops *ops 37 const struct bpf_prog_offload_ops *ops; 38 struct list_head netdevs; 38 struct list_head netdevs; 39 void *priv; 39 void *priv; 40 }; 40 }; 41 41 42 struct bpf_offload_netdev { 42 struct bpf_offload_netdev { 43 struct rhash_head l; 43 struct rhash_head l; 44 struct net_device *netdev; 44 struct net_device *netdev; 45 struct bpf_offload_dev *offdev; /* NUL 45 struct bpf_offload_dev *offdev; /* NULL when bound-only */ 46 struct list_head progs; 46 struct list_head progs; 47 struct list_head maps; 47 struct list_head maps; 48 struct list_head offdev_netdevs; 48 struct list_head offdev_netdevs; 49 }; 49 }; 50 50 51 static const struct rhashtable_params offdevs_ 51 static const struct rhashtable_params offdevs_params = { 52 .nelem_hint = 4, 52 .nelem_hint = 4, 53 .key_len = sizeof(struc 53 .key_len = sizeof(struct net_device *), 54 .key_offset = offsetof(str 54 .key_offset = offsetof(struct bpf_offload_netdev, netdev), 55 .head_offset = offsetof(str 55 .head_offset = offsetof(struct bpf_offload_netdev, l), 56 .automatic_shrinking = true, 56 .automatic_shrinking = true, 57 }; 57 }; 58 58 59 static struct rhashtable offdevs; 59 static struct rhashtable offdevs; 60 60 61 static int bpf_dev_offload_check(struct net_de 61 static int bpf_dev_offload_check(struct net_device *netdev) 62 { 62 { 63 if (!netdev) 63 if (!netdev) 64 return -EINVAL; 64 return -EINVAL; 65 if (!netdev->netdev_ops->ndo_bpf) 65 if (!netdev->netdev_ops->ndo_bpf) 66 return -EOPNOTSUPP; 66 return -EOPNOTSUPP; 67 return 0; 67 return 0; 68 } 68 } 69 69 70 static struct bpf_offload_netdev * 70 static struct bpf_offload_netdev * 71 bpf_offload_find_netdev(struct net_device *net 71 bpf_offload_find_netdev(struct net_device *netdev) 72 { 72 { 73 lockdep_assert_held(&bpf_devs_lock); 73 lockdep_assert_held(&bpf_devs_lock); 74 74 75 return rhashtable_lookup_fast(&offdevs 75 return rhashtable_lookup_fast(&offdevs, &netdev, offdevs_params); 76 } 76 } 77 77 78 static int __bpf_offload_dev_netdev_register(s 78 static int __bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev, 79 s 79 struct net_device *netdev) 80 { 80 { 81 struct bpf_offload_netdev *ondev; 81 struct bpf_offload_netdev *ondev; 82 int err; 82 int err; 83 83 84 ondev = kzalloc(sizeof(*ondev), GFP_KE 84 ondev = kzalloc(sizeof(*ondev), GFP_KERNEL); 85 if (!ondev) 85 if (!ondev) 86 return -ENOMEM; 86 return -ENOMEM; 87 87 88 ondev->netdev = netdev; 88 ondev->netdev = netdev; 89 ondev->offdev = offdev; 89 ondev->offdev = offdev; 90 INIT_LIST_HEAD(&ondev->progs); 90 INIT_LIST_HEAD(&ondev->progs); 91 INIT_LIST_HEAD(&ondev->maps); 91 INIT_LIST_HEAD(&ondev->maps); 92 92 93 err = rhashtable_insert_fast(&offdevs, 93 err = rhashtable_insert_fast(&offdevs, &ondev->l, offdevs_params); 94 if (err) { 94 if (err) { 95 netdev_warn(netdev, "failed to 95 netdev_warn(netdev, "failed to register for BPF offload\n"); 96 goto err_free; 96 goto err_free; 97 } 97 } 98 98 99 if (offdev) 99 if (offdev) 100 list_add(&ondev->offdev_netdev 100 list_add(&ondev->offdev_netdevs, &offdev->netdevs); 101 return 0; 101 return 0; 102 102 103 err_free: 103 err_free: 104 kfree(ondev); 104 kfree(ondev); 105 return err; 105 return err; 106 } 106 } 107 107 108 static void __bpf_prog_offload_destroy(struct 108 static void __bpf_prog_offload_destroy(struct bpf_prog *prog) 109 { 109 { 110 struct bpf_prog_offload *offload = pro 110 struct bpf_prog_offload *offload = prog->aux->offload; 111 111 112 if (offload->dev_state) 112 if (offload->dev_state) 113 offload->offdev->ops->destroy( 113 offload->offdev->ops->destroy(prog); 114 114 115 list_del_init(&offload->offloads); 115 list_del_init(&offload->offloads); 116 kfree(offload); 116 kfree(offload); 117 prog->aux->offload = NULL; 117 prog->aux->offload = NULL; 118 } 118 } 119 119 120 static int bpf_map_offload_ndo(struct bpf_offl 120 static int bpf_map_offload_ndo(struct bpf_offloaded_map *offmap, 121 enum bpf_netdev 121 enum bpf_netdev_command cmd) 122 { 122 { 123 struct netdev_bpf data = {}; 123 struct netdev_bpf data = {}; 124 struct net_device *netdev; 124 struct net_device *netdev; 125 125 126 ASSERT_RTNL(); 126 ASSERT_RTNL(); 127 127 128 data.command = cmd; 128 data.command = cmd; 129 data.offmap = offmap; 129 data.offmap = offmap; 130 /* Caller must make sure netdev is val 130 /* Caller must make sure netdev is valid */ 131 netdev = offmap->netdev; 131 netdev = offmap->netdev; 132 132 133 return netdev->netdev_ops->ndo_bpf(net 133 return netdev->netdev_ops->ndo_bpf(netdev, &data); 134 } 134 } 135 135 136 static void __bpf_map_offload_destroy(struct b 136 static void __bpf_map_offload_destroy(struct bpf_offloaded_map *offmap) 137 { 137 { 138 WARN_ON(bpf_map_offload_ndo(offmap, BP 138 WARN_ON(bpf_map_offload_ndo(offmap, BPF_OFFLOAD_MAP_FREE)); 139 /* Make sure BPF_MAP_GET_NEXT_ID can't 139 /* Make sure BPF_MAP_GET_NEXT_ID can't find this dead map */ 140 bpf_map_free_id(&offmap->map); 140 bpf_map_free_id(&offmap->map); 141 list_del_init(&offmap->offloads); 141 list_del_init(&offmap->offloads); 142 offmap->netdev = NULL; 142 offmap->netdev = NULL; 143 } 143 } 144 144 145 static void __bpf_offload_dev_netdev_unregiste 145 static void __bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev, 146 146 struct net_device *netdev) 147 { 147 { 148 struct bpf_offload_netdev *ondev, *alt 148 struct bpf_offload_netdev *ondev, *altdev = NULL; 149 struct bpf_offloaded_map *offmap, *mtm 149 struct bpf_offloaded_map *offmap, *mtmp; 150 struct bpf_prog_offload *offload, *ptm 150 struct bpf_prog_offload *offload, *ptmp; 151 151 152 ASSERT_RTNL(); 152 ASSERT_RTNL(); 153 153 154 ondev = rhashtable_lookup_fast(&offdev 154 ondev = rhashtable_lookup_fast(&offdevs, &netdev, offdevs_params); 155 if (WARN_ON(!ondev)) 155 if (WARN_ON(!ondev)) 156 return; 156 return; 157 157 158 WARN_ON(rhashtable_remove_fast(&offdev 158 WARN_ON(rhashtable_remove_fast(&offdevs, &ondev->l, offdevs_params)); 159 159 160 /* Try to move the objects to another 160 /* Try to move the objects to another netdev of the device */ 161 if (offdev) { 161 if (offdev) { 162 list_del(&ondev->offdev_netdev 162 list_del(&ondev->offdev_netdevs); 163 altdev = list_first_entry_or_n 163 altdev = list_first_entry_or_null(&offdev->netdevs, 164 164 struct bpf_offload_netdev, 165 165 offdev_netdevs); 166 } 166 } 167 167 168 if (altdev) { 168 if (altdev) { 169 list_for_each_entry(offload, & 169 list_for_each_entry(offload, &ondev->progs, offloads) 170 offload->netdev = altd 170 offload->netdev = altdev->netdev; 171 list_splice_init(&ondev->progs 171 list_splice_init(&ondev->progs, &altdev->progs); 172 172 173 list_for_each_entry(offmap, &o 173 list_for_each_entry(offmap, &ondev->maps, offloads) 174 offmap->netdev = altde 174 offmap->netdev = altdev->netdev; 175 list_splice_init(&ondev->maps, 175 list_splice_init(&ondev->maps, &altdev->maps); 176 } else { 176 } else { 177 list_for_each_entry_safe(offlo 177 list_for_each_entry_safe(offload, ptmp, &ondev->progs, offloads) 178 __bpf_prog_offload_des 178 __bpf_prog_offload_destroy(offload->prog); 179 list_for_each_entry_safe(offma 179 list_for_each_entry_safe(offmap, mtmp, &ondev->maps, offloads) 180 __bpf_map_offload_dest 180 __bpf_map_offload_destroy(offmap); 181 } 181 } 182 182 183 WARN_ON(!list_empty(&ondev->progs)); 183 WARN_ON(!list_empty(&ondev->progs)); 184 WARN_ON(!list_empty(&ondev->maps)); 184 WARN_ON(!list_empty(&ondev->maps)); 185 kfree(ondev); 185 kfree(ondev); 186 } 186 } 187 187 188 static int __bpf_prog_dev_bound_init(struct bp 188 static int __bpf_prog_dev_bound_init(struct bpf_prog *prog, struct net_device *netdev) 189 { 189 { 190 struct bpf_offload_netdev *ondev; 190 struct bpf_offload_netdev *ondev; 191 struct bpf_prog_offload *offload; 191 struct bpf_prog_offload *offload; 192 int err; 192 int err; 193 193 194 offload = kzalloc(sizeof(*offload), GF 194 offload = kzalloc(sizeof(*offload), GFP_USER); 195 if (!offload) 195 if (!offload) 196 return -ENOMEM; 196 return -ENOMEM; 197 197 198 offload->prog = prog; 198 offload->prog = prog; 199 offload->netdev = netdev; 199 offload->netdev = netdev; 200 200 201 ondev = bpf_offload_find_netdev(offloa 201 ondev = bpf_offload_find_netdev(offload->netdev); 202 /* When program is offloaded require p 202 /* When program is offloaded require presence of "true" 203 * bpf_offload_netdev, avoid the one c 203 * bpf_offload_netdev, avoid the one created for !ondev case below. 204 */ 204 */ 205 if (bpf_prog_is_offloaded(prog->aux) & 205 if (bpf_prog_is_offloaded(prog->aux) && (!ondev || !ondev->offdev)) { 206 err = -EINVAL; 206 err = -EINVAL; 207 goto err_free; 207 goto err_free; 208 } 208 } 209 if (!ondev) { 209 if (!ondev) { 210 /* When only binding to the de 210 /* When only binding to the device, explicitly 211 * create an entry in the hash 211 * create an entry in the hashtable. 212 */ 212 */ 213 err = __bpf_offload_dev_netdev 213 err = __bpf_offload_dev_netdev_register(NULL, offload->netdev); 214 if (err) 214 if (err) 215 goto err_free; 215 goto err_free; 216 ondev = bpf_offload_find_netde 216 ondev = bpf_offload_find_netdev(offload->netdev); 217 } 217 } 218 offload->offdev = ondev->offdev; 218 offload->offdev = ondev->offdev; 219 prog->aux->offload = offload; 219 prog->aux->offload = offload; 220 list_add_tail(&offload->offloads, &ond 220 list_add_tail(&offload->offloads, &ondev->progs); 221 221 222 return 0; 222 return 0; 223 err_free: 223 err_free: 224 kfree(offload); 224 kfree(offload); 225 return err; 225 return err; 226 } 226 } 227 227 228 int bpf_prog_dev_bound_init(struct bpf_prog *p 228 int bpf_prog_dev_bound_init(struct bpf_prog *prog, union bpf_attr *attr) 229 { 229 { 230 struct net_device *netdev; 230 struct net_device *netdev; 231 int err; 231 int err; 232 232 233 if (attr->prog_type != BPF_PROG_TYPE_S 233 if (attr->prog_type != BPF_PROG_TYPE_SCHED_CLS && 234 attr->prog_type != BPF_PROG_TYPE_X 234 attr->prog_type != BPF_PROG_TYPE_XDP) 235 return -EINVAL; 235 return -EINVAL; 236 236 237 if (attr->prog_flags & ~(BPF_F_XDP_DEV 237 if (attr->prog_flags & ~(BPF_F_XDP_DEV_BOUND_ONLY | BPF_F_XDP_HAS_FRAGS)) 238 return -EINVAL; 238 return -EINVAL; 239 239 240 /* Frags are allowed only if program i 240 /* Frags are allowed only if program is dev-bound-only, but not 241 * if it is requesting bpf offload. 241 * if it is requesting bpf offload. 242 */ 242 */ 243 if (attr->prog_flags & BPF_F_XDP_HAS_F 243 if (attr->prog_flags & BPF_F_XDP_HAS_FRAGS && 244 !(attr->prog_flags & BPF_F_XDP_DEV 244 !(attr->prog_flags & BPF_F_XDP_DEV_BOUND_ONLY)) 245 return -EINVAL; 245 return -EINVAL; 246 246 247 if (attr->prog_type == BPF_PROG_TYPE_S 247 if (attr->prog_type == BPF_PROG_TYPE_SCHED_CLS && 248 attr->prog_flags & BPF_F_XDP_DEV_B 248 attr->prog_flags & BPF_F_XDP_DEV_BOUND_ONLY) 249 return -EINVAL; 249 return -EINVAL; 250 250 251 netdev = dev_get_by_index(current->nsp 251 netdev = dev_get_by_index(current->nsproxy->net_ns, attr->prog_ifindex); 252 if (!netdev) 252 if (!netdev) 253 return -EINVAL; 253 return -EINVAL; 254 254 255 err = bpf_dev_offload_check(netdev); 255 err = bpf_dev_offload_check(netdev); 256 if (err) 256 if (err) 257 goto out; 257 goto out; 258 258 259 prog->aux->offload_requested = !(attr- 259 prog->aux->offload_requested = !(attr->prog_flags & BPF_F_XDP_DEV_BOUND_ONLY); 260 260 261 down_write(&bpf_devs_lock); 261 down_write(&bpf_devs_lock); 262 err = __bpf_prog_dev_bound_init(prog, 262 err = __bpf_prog_dev_bound_init(prog, netdev); 263 up_write(&bpf_devs_lock); 263 up_write(&bpf_devs_lock); 264 264 265 out: 265 out: 266 dev_put(netdev); 266 dev_put(netdev); 267 return err; 267 return err; 268 } 268 } 269 269 270 int bpf_prog_dev_bound_inherit(struct bpf_prog 270 int bpf_prog_dev_bound_inherit(struct bpf_prog *new_prog, struct bpf_prog *old_prog) 271 { 271 { 272 int err; 272 int err; 273 273 274 if (!bpf_prog_is_dev_bound(old_prog->a 274 if (!bpf_prog_is_dev_bound(old_prog->aux)) 275 return 0; 275 return 0; 276 276 277 if (bpf_prog_is_offloaded(old_prog->au 277 if (bpf_prog_is_offloaded(old_prog->aux)) 278 return -EINVAL; 278 return -EINVAL; 279 279 280 new_prog->aux->dev_bound = old_prog->a 280 new_prog->aux->dev_bound = old_prog->aux->dev_bound; 281 new_prog->aux->offload_requested = old 281 new_prog->aux->offload_requested = old_prog->aux->offload_requested; 282 282 283 down_write(&bpf_devs_lock); 283 down_write(&bpf_devs_lock); 284 if (!old_prog->aux->offload) { 284 if (!old_prog->aux->offload) { 285 err = -EINVAL; 285 err = -EINVAL; 286 goto out; 286 goto out; 287 } 287 } 288 288 289 err = __bpf_prog_dev_bound_init(new_pr 289 err = __bpf_prog_dev_bound_init(new_prog, old_prog->aux->offload->netdev); 290 290 291 out: 291 out: 292 up_write(&bpf_devs_lock); 292 up_write(&bpf_devs_lock); 293 return err; 293 return err; 294 } 294 } 295 295 296 int bpf_prog_offload_verifier_prep(struct bpf_ 296 int bpf_prog_offload_verifier_prep(struct bpf_prog *prog) 297 { 297 { 298 struct bpf_prog_offload *offload; 298 struct bpf_prog_offload *offload; 299 int ret = -ENODEV; 299 int ret = -ENODEV; 300 300 301 down_read(&bpf_devs_lock); 301 down_read(&bpf_devs_lock); 302 offload = prog->aux->offload; 302 offload = prog->aux->offload; 303 if (offload) { 303 if (offload) { 304 ret = offload->offdev->ops->pr 304 ret = offload->offdev->ops->prepare(prog); 305 offload->dev_state = !ret; 305 offload->dev_state = !ret; 306 } 306 } 307 up_read(&bpf_devs_lock); 307 up_read(&bpf_devs_lock); 308 308 309 return ret; 309 return ret; 310 } 310 } 311 311 312 int bpf_prog_offload_verify_insn(struct bpf_ve 312 int bpf_prog_offload_verify_insn(struct bpf_verifier_env *env, 313 int insn_idx, 313 int insn_idx, int prev_insn_idx) 314 { 314 { 315 struct bpf_prog_offload *offload; 315 struct bpf_prog_offload *offload; 316 int ret = -ENODEV; 316 int ret = -ENODEV; 317 317 318 down_read(&bpf_devs_lock); 318 down_read(&bpf_devs_lock); 319 offload = env->prog->aux->offload; 319 offload = env->prog->aux->offload; 320 if (offload) 320 if (offload) 321 ret = offload->offdev->ops->in 321 ret = offload->offdev->ops->insn_hook(env, insn_idx, 322 322 prev_insn_idx); 323 up_read(&bpf_devs_lock); 323 up_read(&bpf_devs_lock); 324 324 325 return ret; 325 return ret; 326 } 326 } 327 327 328 int bpf_prog_offload_finalize(struct bpf_verif 328 int bpf_prog_offload_finalize(struct bpf_verifier_env *env) 329 { 329 { 330 struct bpf_prog_offload *offload; 330 struct bpf_prog_offload *offload; 331 int ret = -ENODEV; 331 int ret = -ENODEV; 332 332 333 down_read(&bpf_devs_lock); 333 down_read(&bpf_devs_lock); 334 offload = env->prog->aux->offload; 334 offload = env->prog->aux->offload; 335 if (offload) { 335 if (offload) { 336 if (offload->offdev->ops->fina 336 if (offload->offdev->ops->finalize) 337 ret = offload->offdev- 337 ret = offload->offdev->ops->finalize(env); 338 else 338 else 339 ret = 0; 339 ret = 0; 340 } 340 } 341 up_read(&bpf_devs_lock); 341 up_read(&bpf_devs_lock); 342 342 343 return ret; 343 return ret; 344 } 344 } 345 345 346 void 346 void 347 bpf_prog_offload_replace_insn(struct bpf_verif 347 bpf_prog_offload_replace_insn(struct bpf_verifier_env *env, u32 off, 348 struct bpf_insn 348 struct bpf_insn *insn) 349 { 349 { 350 const struct bpf_prog_offload_ops *ops 350 const struct bpf_prog_offload_ops *ops; 351 struct bpf_prog_offload *offload; 351 struct bpf_prog_offload *offload; 352 int ret = -EOPNOTSUPP; 352 int ret = -EOPNOTSUPP; 353 353 354 down_read(&bpf_devs_lock); 354 down_read(&bpf_devs_lock); 355 offload = env->prog->aux->offload; 355 offload = env->prog->aux->offload; 356 if (offload) { 356 if (offload) { 357 ops = offload->offdev->ops; 357 ops = offload->offdev->ops; 358 if (!offload->opt_failed && op 358 if (!offload->opt_failed && ops->replace_insn) 359 ret = ops->replace_ins 359 ret = ops->replace_insn(env, off, insn); 360 offload->opt_failed |= ret; 360 offload->opt_failed |= ret; 361 } 361 } 362 up_read(&bpf_devs_lock); 362 up_read(&bpf_devs_lock); 363 } 363 } 364 364 365 void 365 void 366 bpf_prog_offload_remove_insns(struct bpf_verif 366 bpf_prog_offload_remove_insns(struct bpf_verifier_env *env, u32 off, u32 cnt) 367 { 367 { 368 struct bpf_prog_offload *offload; 368 struct bpf_prog_offload *offload; 369 int ret = -EOPNOTSUPP; 369 int ret = -EOPNOTSUPP; 370 370 371 down_read(&bpf_devs_lock); 371 down_read(&bpf_devs_lock); 372 offload = env->prog->aux->offload; 372 offload = env->prog->aux->offload; 373 if (offload) { 373 if (offload) { 374 if (!offload->opt_failed && of 374 if (!offload->opt_failed && offload->offdev->ops->remove_insns) 375 ret = offload->offdev- 375 ret = offload->offdev->ops->remove_insns(env, off, cnt); 376 offload->opt_failed |= ret; 376 offload->opt_failed |= ret; 377 } 377 } 378 up_read(&bpf_devs_lock); 378 up_read(&bpf_devs_lock); 379 } 379 } 380 380 381 void bpf_prog_dev_bound_destroy(struct bpf_pro 381 void bpf_prog_dev_bound_destroy(struct bpf_prog *prog) 382 { 382 { 383 struct bpf_offload_netdev *ondev; 383 struct bpf_offload_netdev *ondev; 384 struct net_device *netdev; 384 struct net_device *netdev; 385 385 386 rtnl_lock(); 386 rtnl_lock(); 387 down_write(&bpf_devs_lock); 387 down_write(&bpf_devs_lock); 388 if (prog->aux->offload) { 388 if (prog->aux->offload) { 389 list_del_init(&prog->aux->offl 389 list_del_init(&prog->aux->offload->offloads); 390 390 391 netdev = prog->aux->offload->n 391 netdev = prog->aux->offload->netdev; 392 __bpf_prog_offload_destroy(pro 392 __bpf_prog_offload_destroy(prog); 393 393 394 ondev = bpf_offload_find_netde 394 ondev = bpf_offload_find_netdev(netdev); 395 if (!ondev->offdev && list_emp 395 if (!ondev->offdev && list_empty(&ondev->progs)) 396 __bpf_offload_dev_netd 396 __bpf_offload_dev_netdev_unregister(NULL, netdev); 397 } 397 } 398 up_write(&bpf_devs_lock); 398 up_write(&bpf_devs_lock); 399 rtnl_unlock(); 399 rtnl_unlock(); 400 } 400 } 401 401 402 static int bpf_prog_offload_translate(struct b 402 static int bpf_prog_offload_translate(struct bpf_prog *prog) 403 { 403 { 404 struct bpf_prog_offload *offload; 404 struct bpf_prog_offload *offload; 405 int ret = -ENODEV; 405 int ret = -ENODEV; 406 406 407 down_read(&bpf_devs_lock); 407 down_read(&bpf_devs_lock); 408 offload = prog->aux->offload; 408 offload = prog->aux->offload; 409 if (offload) 409 if (offload) 410 ret = offload->offdev->ops->tr 410 ret = offload->offdev->ops->translate(prog); 411 up_read(&bpf_devs_lock); 411 up_read(&bpf_devs_lock); 412 412 413 return ret; 413 return ret; 414 } 414 } 415 415 416 static unsigned int bpf_prog_warn_on_exec(cons 416 static unsigned int bpf_prog_warn_on_exec(const void *ctx, 417 cons 417 const struct bpf_insn *insn) 418 { 418 { 419 WARN(1, "attempt to execute device eBP 419 WARN(1, "attempt to execute device eBPF program on the host!"); 420 return 0; 420 return 0; 421 } 421 } 422 422 423 int bpf_prog_offload_compile(struct bpf_prog * 423 int bpf_prog_offload_compile(struct bpf_prog *prog) 424 { 424 { 425 prog->bpf_func = bpf_prog_warn_on_exec 425 prog->bpf_func = bpf_prog_warn_on_exec; 426 426 427 return bpf_prog_offload_translate(prog 427 return bpf_prog_offload_translate(prog); 428 } 428 } 429 429 430 struct ns_get_path_bpf_prog_args { 430 struct ns_get_path_bpf_prog_args { 431 struct bpf_prog *prog; 431 struct bpf_prog *prog; 432 struct bpf_prog_info *info; 432 struct bpf_prog_info *info; 433 }; 433 }; 434 434 435 static struct ns_common *bpf_prog_offload_info 435 static struct ns_common *bpf_prog_offload_info_fill_ns(void *private_data) 436 { 436 { 437 struct ns_get_path_bpf_prog_args *args 437 struct ns_get_path_bpf_prog_args *args = private_data; 438 struct bpf_prog_aux *aux = args->prog- 438 struct bpf_prog_aux *aux = args->prog->aux; 439 struct ns_common *ns; 439 struct ns_common *ns; 440 struct net *net; 440 struct net *net; 441 441 442 rtnl_lock(); 442 rtnl_lock(); 443 down_read(&bpf_devs_lock); 443 down_read(&bpf_devs_lock); 444 444 445 if (aux->offload) { 445 if (aux->offload) { 446 args->info->ifindex = aux->off 446 args->info->ifindex = aux->offload->netdev->ifindex; 447 net = dev_net(aux->offload->ne 447 net = dev_net(aux->offload->netdev); 448 get_net(net); 448 get_net(net); 449 ns = &net->ns; 449 ns = &net->ns; 450 } else { 450 } else { 451 args->info->ifindex = 0; 451 args->info->ifindex = 0; 452 ns = NULL; 452 ns = NULL; 453 } 453 } 454 454 455 up_read(&bpf_devs_lock); 455 up_read(&bpf_devs_lock); 456 rtnl_unlock(); 456 rtnl_unlock(); 457 457 458 return ns; 458 return ns; 459 } 459 } 460 460 461 int bpf_prog_offload_info_fill(struct bpf_prog 461 int bpf_prog_offload_info_fill(struct bpf_prog_info *info, 462 struct bpf_prog 462 struct bpf_prog *prog) 463 { 463 { 464 struct ns_get_path_bpf_prog_args args 464 struct ns_get_path_bpf_prog_args args = { 465 .prog = prog, 465 .prog = prog, 466 .info = info, 466 .info = info, 467 }; 467 }; 468 struct bpf_prog_aux *aux = prog->aux; 468 struct bpf_prog_aux *aux = prog->aux; 469 struct inode *ns_inode; 469 struct inode *ns_inode; 470 struct path ns_path; 470 struct path ns_path; 471 char __user *uinsns; 471 char __user *uinsns; 472 int res; 472 int res; 473 u32 ulen; 473 u32 ulen; 474 474 475 res = ns_get_path_cb(&ns_path, bpf_pro 475 res = ns_get_path_cb(&ns_path, bpf_prog_offload_info_fill_ns, &args); 476 if (res) { 476 if (res) { 477 if (!info->ifindex) 477 if (!info->ifindex) 478 return -ENODEV; 478 return -ENODEV; 479 return res; 479 return res; 480 } 480 } 481 481 482 down_read(&bpf_devs_lock); 482 down_read(&bpf_devs_lock); 483 483 484 if (!aux->offload) { 484 if (!aux->offload) { 485 up_read(&bpf_devs_lock); 485 up_read(&bpf_devs_lock); 486 return -ENODEV; 486 return -ENODEV; 487 } 487 } 488 488 489 ulen = info->jited_prog_len; 489 ulen = info->jited_prog_len; 490 info->jited_prog_len = aux->offload->j 490 info->jited_prog_len = aux->offload->jited_len; 491 if (info->jited_prog_len && ulen) { 491 if (info->jited_prog_len && ulen) { 492 uinsns = u64_to_user_ptr(info- 492 uinsns = u64_to_user_ptr(info->jited_prog_insns); 493 ulen = min_t(u32, info->jited_ 493 ulen = min_t(u32, info->jited_prog_len, ulen); 494 if (copy_to_user(uinsns, aux-> 494 if (copy_to_user(uinsns, aux->offload->jited_image, ulen)) { 495 up_read(&bpf_devs_lock 495 up_read(&bpf_devs_lock); 496 return -EFAULT; 496 return -EFAULT; 497 } 497 } 498 } 498 } 499 499 500 up_read(&bpf_devs_lock); 500 up_read(&bpf_devs_lock); 501 501 502 ns_inode = ns_path.dentry->d_inode; 502 ns_inode = ns_path.dentry->d_inode; 503 info->netns_dev = new_encode_dev(ns_in 503 info->netns_dev = new_encode_dev(ns_inode->i_sb->s_dev); 504 info->netns_ino = ns_inode->i_ino; 504 info->netns_ino = ns_inode->i_ino; 505 path_put(&ns_path); 505 path_put(&ns_path); 506 506 507 return 0; 507 return 0; 508 } 508 } 509 509 510 const struct bpf_prog_ops bpf_offload_prog_ops 510 const struct bpf_prog_ops bpf_offload_prog_ops = { 511 }; 511 }; 512 512 513 struct bpf_map *bpf_map_offload_map_alloc(unio 513 struct bpf_map *bpf_map_offload_map_alloc(union bpf_attr *attr) 514 { 514 { 515 struct net *net = current->nsproxy->ne 515 struct net *net = current->nsproxy->net_ns; 516 struct bpf_offload_netdev *ondev; 516 struct bpf_offload_netdev *ondev; 517 struct bpf_offloaded_map *offmap; 517 struct bpf_offloaded_map *offmap; 518 int err; 518 int err; 519 519 520 if (!capable(CAP_SYS_ADMIN)) 520 if (!capable(CAP_SYS_ADMIN)) 521 return ERR_PTR(-EPERM); 521 return ERR_PTR(-EPERM); 522 if (attr->map_type != BPF_MAP_TYPE_ARR 522 if (attr->map_type != BPF_MAP_TYPE_ARRAY && 523 attr->map_type != BPF_MAP_TYPE_HAS 523 attr->map_type != BPF_MAP_TYPE_HASH) 524 return ERR_PTR(-EINVAL); 524 return ERR_PTR(-EINVAL); 525 525 526 offmap = bpf_map_area_alloc(sizeof(*of 526 offmap = bpf_map_area_alloc(sizeof(*offmap), NUMA_NO_NODE); 527 if (!offmap) 527 if (!offmap) 528 return ERR_PTR(-ENOMEM); 528 return ERR_PTR(-ENOMEM); 529 529 530 bpf_map_init_from_attr(&offmap->map, a 530 bpf_map_init_from_attr(&offmap->map, attr); 531 531 532 rtnl_lock(); 532 rtnl_lock(); 533 down_write(&bpf_devs_lock); 533 down_write(&bpf_devs_lock); 534 offmap->netdev = __dev_get_by_index(ne 534 offmap->netdev = __dev_get_by_index(net, attr->map_ifindex); 535 err = bpf_dev_offload_check(offmap->ne 535 err = bpf_dev_offload_check(offmap->netdev); 536 if (err) 536 if (err) 537 goto err_unlock; 537 goto err_unlock; 538 538 539 ondev = bpf_offload_find_netdev(offmap 539 ondev = bpf_offload_find_netdev(offmap->netdev); 540 if (!ondev) { 540 if (!ondev) { 541 err = -EINVAL; 541 err = -EINVAL; 542 goto err_unlock; 542 goto err_unlock; 543 } 543 } 544 544 545 err = bpf_map_offload_ndo(offmap, BPF_ 545 err = bpf_map_offload_ndo(offmap, BPF_OFFLOAD_MAP_ALLOC); 546 if (err) 546 if (err) 547 goto err_unlock; 547 goto err_unlock; 548 548 549 list_add_tail(&offmap->offloads, &onde 549 list_add_tail(&offmap->offloads, &ondev->maps); 550 up_write(&bpf_devs_lock); 550 up_write(&bpf_devs_lock); 551 rtnl_unlock(); 551 rtnl_unlock(); 552 552 553 return &offmap->map; 553 return &offmap->map; 554 554 555 err_unlock: 555 err_unlock: 556 up_write(&bpf_devs_lock); 556 up_write(&bpf_devs_lock); 557 rtnl_unlock(); 557 rtnl_unlock(); 558 bpf_map_area_free(offmap); 558 bpf_map_area_free(offmap); 559 return ERR_PTR(err); 559 return ERR_PTR(err); 560 } 560 } 561 561 562 void bpf_map_offload_map_free(struct bpf_map * 562 void bpf_map_offload_map_free(struct bpf_map *map) 563 { 563 { 564 struct bpf_offloaded_map *offmap = map 564 struct bpf_offloaded_map *offmap = map_to_offmap(map); 565 565 566 rtnl_lock(); 566 rtnl_lock(); 567 down_write(&bpf_devs_lock); 567 down_write(&bpf_devs_lock); 568 if (offmap->netdev) 568 if (offmap->netdev) 569 __bpf_map_offload_destroy(offm 569 __bpf_map_offload_destroy(offmap); 570 up_write(&bpf_devs_lock); 570 up_write(&bpf_devs_lock); 571 rtnl_unlock(); 571 rtnl_unlock(); 572 572 573 bpf_map_area_free(offmap); 573 bpf_map_area_free(offmap); 574 } 574 } 575 575 576 u64 bpf_map_offload_map_mem_usage(const struct 576 u64 bpf_map_offload_map_mem_usage(const struct bpf_map *map) 577 { 577 { 578 /* The memory dynamically allocated in 578 /* The memory dynamically allocated in netdev dev_ops is not counted */ 579 return sizeof(struct bpf_offloaded_map 579 return sizeof(struct bpf_offloaded_map); 580 } 580 } 581 581 582 int bpf_map_offload_lookup_elem(struct bpf_map 582 int bpf_map_offload_lookup_elem(struct bpf_map *map, void *key, void *value) 583 { 583 { 584 struct bpf_offloaded_map *offmap = map 584 struct bpf_offloaded_map *offmap = map_to_offmap(map); 585 int ret = -ENODEV; 585 int ret = -ENODEV; 586 586 587 down_read(&bpf_devs_lock); 587 down_read(&bpf_devs_lock); 588 if (offmap->netdev) 588 if (offmap->netdev) 589 ret = offmap->dev_ops->map_loo 589 ret = offmap->dev_ops->map_lookup_elem(offmap, key, value); 590 up_read(&bpf_devs_lock); 590 up_read(&bpf_devs_lock); 591 591 592 return ret; 592 return ret; 593 } 593 } 594 594 595 int bpf_map_offload_update_elem(struct bpf_map 595 int bpf_map_offload_update_elem(struct bpf_map *map, 596 void *key, voi 596 void *key, void *value, u64 flags) 597 { 597 { 598 struct bpf_offloaded_map *offmap = map 598 struct bpf_offloaded_map *offmap = map_to_offmap(map); 599 int ret = -ENODEV; 599 int ret = -ENODEV; 600 600 601 if (unlikely(flags > BPF_EXIST)) 601 if (unlikely(flags > BPF_EXIST)) 602 return -EINVAL; 602 return -EINVAL; 603 603 604 down_read(&bpf_devs_lock); 604 down_read(&bpf_devs_lock); 605 if (offmap->netdev) 605 if (offmap->netdev) 606 ret = offmap->dev_ops->map_upd 606 ret = offmap->dev_ops->map_update_elem(offmap, key, value, 607 607 flags); 608 up_read(&bpf_devs_lock); 608 up_read(&bpf_devs_lock); 609 609 610 return ret; 610 return ret; 611 } 611 } 612 612 613 int bpf_map_offload_delete_elem(struct bpf_map 613 int bpf_map_offload_delete_elem(struct bpf_map *map, void *key) 614 { 614 { 615 struct bpf_offloaded_map *offmap = map 615 struct bpf_offloaded_map *offmap = map_to_offmap(map); 616 int ret = -ENODEV; 616 int ret = -ENODEV; 617 617 618 down_read(&bpf_devs_lock); 618 down_read(&bpf_devs_lock); 619 if (offmap->netdev) 619 if (offmap->netdev) 620 ret = offmap->dev_ops->map_del 620 ret = offmap->dev_ops->map_delete_elem(offmap, key); 621 up_read(&bpf_devs_lock); 621 up_read(&bpf_devs_lock); 622 622 623 return ret; 623 return ret; 624 } 624 } 625 625 626 int bpf_map_offload_get_next_key(struct bpf_ma 626 int bpf_map_offload_get_next_key(struct bpf_map *map, void *key, void *next_key) 627 { 627 { 628 struct bpf_offloaded_map *offmap = map 628 struct bpf_offloaded_map *offmap = map_to_offmap(map); 629 int ret = -ENODEV; 629 int ret = -ENODEV; 630 630 631 down_read(&bpf_devs_lock); 631 down_read(&bpf_devs_lock); 632 if (offmap->netdev) 632 if (offmap->netdev) 633 ret = offmap->dev_ops->map_get 633 ret = offmap->dev_ops->map_get_next_key(offmap, key, next_key); 634 up_read(&bpf_devs_lock); 634 up_read(&bpf_devs_lock); 635 635 636 return ret; 636 return ret; 637 } 637 } 638 638 639 struct ns_get_path_bpf_map_args { 639 struct ns_get_path_bpf_map_args { 640 struct bpf_offloaded_map *offmap; 640 struct bpf_offloaded_map *offmap; 641 struct bpf_map_info *info; 641 struct bpf_map_info *info; 642 }; 642 }; 643 643 644 static struct ns_common *bpf_map_offload_info_ 644 static struct ns_common *bpf_map_offload_info_fill_ns(void *private_data) 645 { 645 { 646 struct ns_get_path_bpf_map_args *args 646 struct ns_get_path_bpf_map_args *args = private_data; 647 struct ns_common *ns; 647 struct ns_common *ns; 648 struct net *net; 648 struct net *net; 649 649 650 rtnl_lock(); 650 rtnl_lock(); 651 down_read(&bpf_devs_lock); 651 down_read(&bpf_devs_lock); 652 652 653 if (args->offmap->netdev) { 653 if (args->offmap->netdev) { 654 args->info->ifindex = args->of 654 args->info->ifindex = args->offmap->netdev->ifindex; 655 net = dev_net(args->offmap->ne 655 net = dev_net(args->offmap->netdev); 656 get_net(net); 656 get_net(net); 657 ns = &net->ns; 657 ns = &net->ns; 658 } else { 658 } else { 659 args->info->ifindex = 0; 659 args->info->ifindex = 0; 660 ns = NULL; 660 ns = NULL; 661 } 661 } 662 662 663 up_read(&bpf_devs_lock); 663 up_read(&bpf_devs_lock); 664 rtnl_unlock(); 664 rtnl_unlock(); 665 665 666 return ns; 666 return ns; 667 } 667 } 668 668 669 int bpf_map_offload_info_fill(struct bpf_map_i 669 int bpf_map_offload_info_fill(struct bpf_map_info *info, struct bpf_map *map) 670 { 670 { 671 struct ns_get_path_bpf_map_args args = 671 struct ns_get_path_bpf_map_args args = { 672 .offmap = map_to_offmap(map), 672 .offmap = map_to_offmap(map), 673 .info = info, 673 .info = info, 674 }; 674 }; 675 struct inode *ns_inode; 675 struct inode *ns_inode; 676 struct path ns_path; 676 struct path ns_path; 677 int res; 677 int res; 678 678 679 res = ns_get_path_cb(&ns_path, bpf_map 679 res = ns_get_path_cb(&ns_path, bpf_map_offload_info_fill_ns, &args); 680 if (res) { 680 if (res) { 681 if (!info->ifindex) 681 if (!info->ifindex) 682 return -ENODEV; 682 return -ENODEV; 683 return res; 683 return res; 684 } 684 } 685 685 686 ns_inode = ns_path.dentry->d_inode; 686 ns_inode = ns_path.dentry->d_inode; 687 info->netns_dev = new_encode_dev(ns_in 687 info->netns_dev = new_encode_dev(ns_inode->i_sb->s_dev); 688 info->netns_ino = ns_inode->i_ino; 688 info->netns_ino = ns_inode->i_ino; 689 path_put(&ns_path); 689 path_put(&ns_path); 690 690 691 return 0; 691 return 0; 692 } 692 } 693 693 694 static bool __bpf_offload_dev_match(struct bpf 694 static bool __bpf_offload_dev_match(struct bpf_prog *prog, 695 struct net 695 struct net_device *netdev) 696 { 696 { 697 struct bpf_offload_netdev *ondev1, *on 697 struct bpf_offload_netdev *ondev1, *ondev2; 698 struct bpf_prog_offload *offload; 698 struct bpf_prog_offload *offload; 699 699 700 if (!bpf_prog_is_dev_bound(prog->aux)) 700 if (!bpf_prog_is_dev_bound(prog->aux)) 701 return false; 701 return false; 702 702 703 offload = prog->aux->offload; 703 offload = prog->aux->offload; 704 if (!offload) 704 if (!offload) 705 return false; 705 return false; 706 if (offload->netdev == netdev) 706 if (offload->netdev == netdev) 707 return true; 707 return true; 708 708 709 ondev1 = bpf_offload_find_netdev(offlo 709 ondev1 = bpf_offload_find_netdev(offload->netdev); 710 ondev2 = bpf_offload_find_netdev(netde 710 ondev2 = bpf_offload_find_netdev(netdev); 711 711 712 return ondev1 && ondev2 && ondev1->off 712 return ondev1 && ondev2 && ondev1->offdev == ondev2->offdev; 713 } 713 } 714 714 715 bool bpf_offload_dev_match(struct bpf_prog *pr 715 bool bpf_offload_dev_match(struct bpf_prog *prog, struct net_device *netdev) 716 { 716 { 717 bool ret; 717 bool ret; 718 718 719 down_read(&bpf_devs_lock); 719 down_read(&bpf_devs_lock); 720 ret = __bpf_offload_dev_match(prog, ne 720 ret = __bpf_offload_dev_match(prog, netdev); 721 up_read(&bpf_devs_lock); 721 up_read(&bpf_devs_lock); 722 722 723 return ret; 723 return ret; 724 } 724 } 725 EXPORT_SYMBOL_GPL(bpf_offload_dev_match); 725 EXPORT_SYMBOL_GPL(bpf_offload_dev_match); 726 726 727 bool bpf_prog_dev_bound_match(const struct bpf 727 bool bpf_prog_dev_bound_match(const struct bpf_prog *lhs, const struct bpf_prog *rhs) 728 { 728 { 729 bool ret; 729 bool ret; 730 730 731 if (bpf_prog_is_offloaded(lhs->aux) != 731 if (bpf_prog_is_offloaded(lhs->aux) != bpf_prog_is_offloaded(rhs->aux)) 732 return false; 732 return false; 733 733 734 down_read(&bpf_devs_lock); 734 down_read(&bpf_devs_lock); 735 ret = lhs->aux->offload && rhs->aux->o 735 ret = lhs->aux->offload && rhs->aux->offload && 736 lhs->aux->offload->netdev && 736 lhs->aux->offload->netdev && 737 lhs->aux->offload->netdev == rhs 737 lhs->aux->offload->netdev == rhs->aux->offload->netdev; 738 up_read(&bpf_devs_lock); 738 up_read(&bpf_devs_lock); 739 739 740 return ret; 740 return ret; 741 } 741 } 742 742 743 bool bpf_offload_prog_map_match(struct bpf_pro 743 bool bpf_offload_prog_map_match(struct bpf_prog *prog, struct bpf_map *map) 744 { 744 { 745 struct bpf_offloaded_map *offmap; 745 struct bpf_offloaded_map *offmap; 746 bool ret; 746 bool ret; 747 747 748 if (!bpf_map_is_offloaded(map)) 748 if (!bpf_map_is_offloaded(map)) 749 return bpf_map_offload_neutral 749 return bpf_map_offload_neutral(map); 750 offmap = map_to_offmap(map); 750 offmap = map_to_offmap(map); 751 751 752 down_read(&bpf_devs_lock); 752 down_read(&bpf_devs_lock); 753 ret = __bpf_offload_dev_match(prog, of 753 ret = __bpf_offload_dev_match(prog, offmap->netdev); 754 up_read(&bpf_devs_lock); 754 up_read(&bpf_devs_lock); 755 755 756 return ret; 756 return ret; 757 } 757 } 758 758 759 int bpf_offload_dev_netdev_register(struct bpf 759 int bpf_offload_dev_netdev_register(struct bpf_offload_dev *offdev, 760 struct net 760 struct net_device *netdev) 761 { 761 { 762 int err; 762 int err; 763 763 764 down_write(&bpf_devs_lock); 764 down_write(&bpf_devs_lock); 765 err = __bpf_offload_dev_netdev_registe 765 err = __bpf_offload_dev_netdev_register(offdev, netdev); 766 up_write(&bpf_devs_lock); 766 up_write(&bpf_devs_lock); 767 return err; 767 return err; 768 } 768 } 769 EXPORT_SYMBOL_GPL(bpf_offload_dev_netdev_regis 769 EXPORT_SYMBOL_GPL(bpf_offload_dev_netdev_register); 770 770 771 void bpf_offload_dev_netdev_unregister(struct 771 void bpf_offload_dev_netdev_unregister(struct bpf_offload_dev *offdev, 772 struct 772 struct net_device *netdev) 773 { 773 { 774 down_write(&bpf_devs_lock); 774 down_write(&bpf_devs_lock); 775 __bpf_offload_dev_netdev_unregister(of 775 __bpf_offload_dev_netdev_unregister(offdev, netdev); 776 up_write(&bpf_devs_lock); 776 up_write(&bpf_devs_lock); 777 } 777 } 778 EXPORT_SYMBOL_GPL(bpf_offload_dev_netdev_unreg 778 EXPORT_SYMBOL_GPL(bpf_offload_dev_netdev_unregister); 779 779 780 struct bpf_offload_dev * 780 struct bpf_offload_dev * 781 bpf_offload_dev_create(const struct bpf_prog_o 781 bpf_offload_dev_create(const struct bpf_prog_offload_ops *ops, void *priv) 782 { 782 { 783 struct bpf_offload_dev *offdev; 783 struct bpf_offload_dev *offdev; 784 784 785 offdev = kzalloc(sizeof(*offdev), GFP_ 785 offdev = kzalloc(sizeof(*offdev), GFP_KERNEL); 786 if (!offdev) 786 if (!offdev) 787 return ERR_PTR(-ENOMEM); 787 return ERR_PTR(-ENOMEM); 788 788 789 offdev->ops = ops; 789 offdev->ops = ops; 790 offdev->priv = priv; 790 offdev->priv = priv; 791 INIT_LIST_HEAD(&offdev->netdevs); 791 INIT_LIST_HEAD(&offdev->netdevs); 792 792 793 return offdev; 793 return offdev; 794 } 794 } 795 EXPORT_SYMBOL_GPL(bpf_offload_dev_create); 795 EXPORT_SYMBOL_GPL(bpf_offload_dev_create); 796 796 797 void bpf_offload_dev_destroy(struct bpf_offloa 797 void bpf_offload_dev_destroy(struct bpf_offload_dev *offdev) 798 { 798 { 799 WARN_ON(!list_empty(&offdev->netdevs)) 799 WARN_ON(!list_empty(&offdev->netdevs)); 800 kfree(offdev); 800 kfree(offdev); 801 } 801 } 802 EXPORT_SYMBOL_GPL(bpf_offload_dev_destroy); 802 EXPORT_SYMBOL_GPL(bpf_offload_dev_destroy); 803 803 804 void *bpf_offload_dev_priv(struct bpf_offload_ 804 void *bpf_offload_dev_priv(struct bpf_offload_dev *offdev) 805 { 805 { 806 return offdev->priv; 806 return offdev->priv; 807 } 807 } 808 EXPORT_SYMBOL_GPL(bpf_offload_dev_priv); 808 EXPORT_SYMBOL_GPL(bpf_offload_dev_priv); 809 809 810 void bpf_dev_bound_netdev_unregister(struct ne 810 void bpf_dev_bound_netdev_unregister(struct net_device *dev) 811 { 811 { 812 struct bpf_offload_netdev *ondev; 812 struct bpf_offload_netdev *ondev; 813 813 814 ASSERT_RTNL(); 814 ASSERT_RTNL(); 815 815 816 down_write(&bpf_devs_lock); 816 down_write(&bpf_devs_lock); 817 ondev = bpf_offload_find_netdev(dev); 817 ondev = bpf_offload_find_netdev(dev); 818 if (ondev && !ondev->offdev) 818 if (ondev && !ondev->offdev) 819 __bpf_offload_dev_netdev_unreg 819 __bpf_offload_dev_netdev_unregister(NULL, ondev->netdev); 820 up_write(&bpf_devs_lock); 820 up_write(&bpf_devs_lock); 821 } 821 } 822 822 823 int bpf_dev_bound_kfunc_check(struct bpf_verif 823 int bpf_dev_bound_kfunc_check(struct bpf_verifier_log *log, 824 struct bpf_prog_ 824 struct bpf_prog_aux *prog_aux) 825 { 825 { 826 if (!bpf_prog_is_dev_bound(prog_aux)) 826 if (!bpf_prog_is_dev_bound(prog_aux)) { 827 bpf_log(log, "metadata kfuncs 827 bpf_log(log, "metadata kfuncs require device-bound program\n"); 828 return -EINVAL; 828 return -EINVAL; 829 } 829 } 830 830 831 if (bpf_prog_is_offloaded(prog_aux)) { 831 if (bpf_prog_is_offloaded(prog_aux)) { 832 bpf_log(log, "metadata kfuncs 832 bpf_log(log, "metadata kfuncs can't be offloaded\n"); 833 return -EINVAL; 833 return -EINVAL; 834 } 834 } 835 835 836 return 0; 836 return 0; 837 } 837 } 838 838 839 void *bpf_dev_bound_resolve_kfunc(struct bpf_p 839 void *bpf_dev_bound_resolve_kfunc(struct bpf_prog *prog, u32 func_id) 840 { 840 { 841 const struct xdp_metadata_ops *ops; 841 const struct xdp_metadata_ops *ops; 842 void *p = NULL; 842 void *p = NULL; 843 843 844 /* We don't hold bpf_devs_lock while r 844 /* We don't hold bpf_devs_lock while resolving several 845 * kfuncs and can race with the unregi 845 * kfuncs and can race with the unregister_netdevice(). 846 * We rely on bpf_dev_bound_match() ch 846 * We rely on bpf_dev_bound_match() check at attach 847 * to render this program unusable. 847 * to render this program unusable. 848 */ 848 */ 849 down_read(&bpf_devs_lock); 849 down_read(&bpf_devs_lock); 850 if (!prog->aux->offload) 850 if (!prog->aux->offload) 851 goto out; 851 goto out; 852 852 853 ops = prog->aux->offload->netdev->xdp_ 853 ops = prog->aux->offload->netdev->xdp_metadata_ops; 854 if (!ops) 854 if (!ops) 855 goto out; 855 goto out; 856 856 857 #define XDP_METADATA_KFUNC(name, _, __, xmo) \ 857 #define XDP_METADATA_KFUNC(name, _, __, xmo) \ 858 if (func_id == bpf_xdp_metadata_kfunc_ 858 if (func_id == bpf_xdp_metadata_kfunc_id(name)) p = ops->xmo; 859 XDP_METADATA_KFUNC_xxx 859 XDP_METADATA_KFUNC_xxx 860 #undef XDP_METADATA_KFUNC 860 #undef XDP_METADATA_KFUNC 861 861 862 out: 862 out: 863 up_read(&bpf_devs_lock); 863 up_read(&bpf_devs_lock); 864 864 865 return p; 865 return p; 866 } 866 } 867 867 868 static int __init bpf_offload_init(void) 868 static int __init bpf_offload_init(void) 869 { 869 { 870 return rhashtable_init(&offdevs, &offd 870 return rhashtable_init(&offdevs, &offdevs_params); 871 } 871 } 872 872 873 core_initcall(bpf_offload_init); 873 core_initcall(bpf_offload_init); 874 874
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.