1 /* Copyright (c) 2017 Covalent IO, Inc. http://covalent.io 2 * 3 * This program is free software; you can redistribute it and/or 4 * modify it under the terms of version 2 of the GNU General Public 5 * License as published by the Free Software Foundation. 6 * 7 * This program is distributed in the hope that it will be useful, but 8 * WITHOUT ANY WARRANTY; without even the implied warranty of 9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 10 * General Public License for more details. 11 */ 12 13 /* Devmaps primary use is as a backend map for XDP BPF helper call 14 * bpf_redirect_map(). Because XDP is mostly concerned with performance we 15 * spent some effort to ensure the datapath with redirect maps does not use 16 * any locking. This is a quick note on the details. 17 * 18 * We have three possible paths to get into the devmap control plane bpf 19 * syscalls, bpf programs, and driver side xmit/flush operations. A bpf syscall 20 * will invoke an update, delete, or lookup operation. To ensure updates and 21 * deletes appear atomic from the datapath side xchg() is used to modify the 22 * netdev_map array. Then because the datapath does a lookup into the netdev_map 23 * array (read-only) from an RCU critical section we use call_rcu() to wait for 24 * an rcu grace period before free'ing the old data structures. This ensures the 25 * datapath always has a valid copy. However, the datapath does a "flush" 26 * operation that pushes any pending packets in the driver outside the RCU 27 * critical section. Each bpf_dtab_netdev tracks these pending operations using 28 * an atomic per-cpu bitmap. The bpf_dtab_netdev object will not be destroyed 29 * until all bits are cleared indicating outstanding flush operations have 30 * completed. 31 * 32 * BPF syscalls may race with BPF program calls on any of the update, delete 33 * or lookup operations. As noted above the xchg() operation also keep the 34 * netdev_map consistent in this case. From the devmap side BPF programs 35 * calling into these operations are the same as multiple user space threads 36 * making system calls. 37 * 38 * Finally, any of the above may race with a netdev_unregister notifier. The 39 * unregister notifier must search for net devices in the map structure that 40 * contain a reference to the net device and remove them. This is a two step 41 * process (a) dereference the bpf_dtab_netdev object in netdev_map and (b) 42 * check to see if the ifindex is the same as the net_device being removed. 43 * Unfortunately, the xchg() operations do not protect against this. To avoid 44 * potentially removing incorrect objects the dev_map_list_mutex protects 45 * conflicting netdev unregister and BPF syscall operations. Updates and 46 * deletes from a BPF program (done in rcu critical section) are blocked 47 * because of this mutex. 48 */ 49 #include <linux/bpf.h> 50 #include <linux/jhash.h> 51 #include <linux/filter.h> 52 #include <linux/rculist_nulls.h> 53 #include "percpu_freelist.h" 54 #include "bpf_lru_list.h" 55 #include "map_in_map.h" 56 57 struct bpf_dtab_netdev { 58 struct net_device *dev; 59 int key; 60 struct rcu_head rcu; 61 struct bpf_dtab *dtab; 62 }; 63 64 struct bpf_dtab { 65 struct bpf_map map; 66 struct bpf_dtab_netdev **netdev_map; 67 unsigned long int __percpu *flush_needed; 68 struct list_head list; 69 }; 70 71 static DEFINE_MUTEX(dev_map_list_mutex); 72 static LIST_HEAD(dev_map_list); 73 74 static struct bpf_map *dev_map_alloc(union bpf_attr *attr) 75 { 76 struct bpf_dtab *dtab; 77 u64 cost; 78 int err; 79 80 /* check sanity of attributes */ 81 if (attr->max_entries == 0 || attr->key_size != 4 || 82 attr->value_size != 4 || attr->map_flags) 83 return ERR_PTR(-EINVAL); 84 85 /* if value_size is bigger, the user space won't be able to 86 * access the elements. 87 */ 88 if (attr->value_size > KMALLOC_MAX_SIZE) 89 return ERR_PTR(-E2BIG); 90 91 dtab = kzalloc(sizeof(*dtab), GFP_USER); 92 if (!dtab) 93 return ERR_PTR(-ENOMEM); 94 95 /* mandatory map attributes */ 96 dtab->map.map_type = attr->map_type; 97 dtab->map.key_size = attr->key_size; 98 dtab->map.value_size = attr->value_size; 99 dtab->map.max_entries = attr->max_entries; 100 dtab->map.map_flags = attr->map_flags; 101 102 err = -ENOMEM; 103 104 /* make sure page count doesn't overflow */ 105 cost = (u64) dtab->map.max_entries * sizeof(struct bpf_dtab_netdev *); 106 cost += BITS_TO_LONGS(attr->max_entries) * sizeof(unsigned long); 107 if (cost >= U32_MAX - PAGE_SIZE) 108 goto free_dtab; 109 110 dtab->map.pages = round_up(cost, PAGE_SIZE) >> PAGE_SHIFT; 111 112 /* if map size is larger than memlock limit, reject it early */ 113 err = bpf_map_precharge_memlock(dtab->map.pages); 114 if (err) 115 goto free_dtab; 116 117 err = -ENOMEM; 118 /* A per cpu bitfield with a bit per possible net device */ 119 dtab->flush_needed = __alloc_percpu( 120 BITS_TO_LONGS(attr->max_entries) * 121 sizeof(unsigned long), 122 __alignof__(unsigned long)); 123 if (!dtab->flush_needed) 124 goto free_dtab; 125 126 dtab->netdev_map = bpf_map_area_alloc(dtab->map.max_entries * 127 sizeof(struct bpf_dtab_netdev *)); 128 if (!dtab->netdev_map) 129 goto free_dtab; 130 131 mutex_lock(&dev_map_list_mutex); 132 list_add_tail(&dtab->list, &dev_map_list); 133 mutex_unlock(&dev_map_list_mutex); 134 return &dtab->map; 135 136 free_dtab: 137 free_percpu(dtab->flush_needed); 138 kfree(dtab); 139 return ERR_PTR(err); 140 } 141 142 static void dev_map_free(struct bpf_map *map) 143 { 144 struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map); 145 int i, cpu; 146 147 /* At this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0, 148 * so the programs (can be more than one that used this map) were 149 * disconnected from events. Wait for outstanding critical sections in 150 * these programs to complete. The rcu critical section only guarantees 151 * no further reads against netdev_map. It does __not__ ensure pending 152 * flush operations (if any) are complete. 153 */ 154 synchronize_rcu(); 155 156 /* To ensure all pending flush operations have completed wait for flush 157 * bitmap to indicate all flush_needed bits to be zero on _all_ cpus. 158 * Because the above synchronize_rcu() ensures the map is disconnected 159 * from the program we can assume no new bits will be set. 160 */ 161 for_each_online_cpu(cpu) { 162 unsigned long *bitmap = per_cpu_ptr(dtab->flush_needed, cpu); 163 164 while (!bitmap_empty(bitmap, dtab->map.max_entries)) 165 cpu_relax(); 166 } 167 168 /* Although we should no longer have datapath or bpf syscall operations 169 * at this point we we can still race with netdev notifier, hence the 170 * lock. 171 */ 172 mutex_lock(&dev_map_list_mutex); 173 for (i = 0; i < dtab->map.max_entries; i++) { 174 struct bpf_dtab_netdev *dev; 175 176 dev = dtab->netdev_map[i]; 177 if (!dev) 178 continue; 179 180 dev_put(dev->dev); 181 kfree(dev); 182 } 183 184 /* At this point bpf program is detached and all pending operations 185 * _must_ be complete 186 */ 187 list_del(&dtab->list); 188 mutex_unlock(&dev_map_list_mutex); 189 free_percpu(dtab->flush_needed); 190 bpf_map_area_free(dtab->netdev_map); 191 kfree(dtab); 192 } 193 194 static int dev_map_get_next_key(struct bpf_map *map, void *key, void *next_key) 195 { 196 struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map); 197 u32 index = key ? *(u32 *)key : U32_MAX; 198 u32 *next = (u32 *)next_key; 199 200 if (index >= dtab->map.max_entries) { 201 *next = 0; 202 return 0; 203 } 204 205 if (index == dtab->map.max_entries - 1) 206 return -ENOENT; 207 208 *next = index + 1; 209 return 0; 210 } 211 212 void __dev_map_insert_ctx(struct bpf_map *map, u32 key) 213 { 214 struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map); 215 unsigned long *bitmap = this_cpu_ptr(dtab->flush_needed); 216 217 __set_bit(key, bitmap); 218 } 219 220 struct net_device *__dev_map_lookup_elem(struct bpf_map *map, u32 key) 221 { 222 struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map); 223 struct bpf_dtab_netdev *dev; 224 225 if (key >= map->max_entries) 226 return NULL; 227 228 dev = READ_ONCE(dtab->netdev_map[key]); 229 return dev ? dev->dev : NULL; 230 } 231 232 /* __dev_map_flush is called from xdp_do_flush_map() which _must_ be signaled 233 * from the driver before returning from its napi->poll() routine. The poll() 234 * routine is called either from busy_poll context or net_rx_action signaled 235 * from NET_RX_SOFTIRQ. Either way the poll routine must complete before the 236 * net device can be torn down. On devmap tear down we ensure the ctx bitmap 237 * is zeroed before completing to ensure all flush operations have completed. 238 */ 239 void __dev_map_flush(struct bpf_map *map) 240 { 241 struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map); 242 unsigned long *bitmap = this_cpu_ptr(dtab->flush_needed); 243 u32 bit; 244 245 for_each_set_bit(bit, bitmap, map->max_entries) { 246 struct bpf_dtab_netdev *dev = READ_ONCE(dtab->netdev_map[bit]); 247 struct net_device *netdev; 248 249 /* This is possible if the dev entry is removed by user space 250 * between xdp redirect and flush op. 251 */ 252 if (unlikely(!dev)) 253 continue; 254 255 netdev = dev->dev; 256 257 __clear_bit(bit, bitmap); 258 if (unlikely(!netdev || !netdev->netdev_ops->ndo_xdp_flush)) 259 continue; 260 261 netdev->netdev_ops->ndo_xdp_flush(netdev); 262 } 263 } 264 265 /* rcu_read_lock (from syscall and BPF contexts) ensures that if a delete and/or 266 * update happens in parallel here a dev_put wont happen until after reading the 267 * ifindex. 268 */ 269 static void *dev_map_lookup_elem(struct bpf_map *map, void *key) 270 { 271 struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map); 272 struct bpf_dtab_netdev *dev; 273 u32 i = *(u32 *)key; 274 275 if (i >= map->max_entries) 276 return NULL; 277 278 dev = READ_ONCE(dtab->netdev_map[i]); 279 return dev ? &dev->dev->ifindex : NULL; 280 } 281 282 static void dev_map_flush_old(struct bpf_dtab_netdev *old_dev) 283 { 284 if (old_dev->dev->netdev_ops->ndo_xdp_flush) { 285 struct net_device *fl = old_dev->dev; 286 unsigned long *bitmap; 287 int cpu; 288 289 for_each_online_cpu(cpu) { 290 bitmap = per_cpu_ptr(old_dev->dtab->flush_needed, cpu); 291 __clear_bit(old_dev->key, bitmap); 292 293 fl->netdev_ops->ndo_xdp_flush(old_dev->dev); 294 } 295 } 296 } 297 298 static void __dev_map_entry_free(struct rcu_head *rcu) 299 { 300 struct bpf_dtab_netdev *old_dev; 301 302 old_dev = container_of(rcu, struct bpf_dtab_netdev, rcu); 303 dev_map_flush_old(old_dev); 304 dev_put(old_dev->dev); 305 kfree(old_dev); 306 } 307 308 static int dev_map_delete_elem(struct bpf_map *map, void *key) 309 { 310 struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map); 311 struct bpf_dtab_netdev *old_dev; 312 int k = *(u32 *)key; 313 314 if (k >= map->max_entries) 315 return -EINVAL; 316 317 /* Use synchronize_rcu() here to ensure any rcu critical sections 318 * have completed, but this does not guarantee a flush has happened 319 * yet. Because driver side rcu_read_lock/unlock only protects the 320 * running XDP program. However, for pending flush operations the 321 * dev and ctx are stored in another per cpu map. And additionally, 322 * the driver tear down ensures all soft irqs are complete before 323 * removing the net device in the case of dev_put equals zero. 324 */ 325 mutex_lock(&dev_map_list_mutex); 326 old_dev = xchg(&dtab->netdev_map[k], NULL); 327 if (old_dev) 328 call_rcu(&old_dev->rcu, __dev_map_entry_free); 329 mutex_unlock(&dev_map_list_mutex); 330 return 0; 331 } 332 333 static int dev_map_update_elem(struct bpf_map *map, void *key, void *value, 334 u64 map_flags) 335 { 336 struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map); 337 struct net *net = current->nsproxy->net_ns; 338 struct bpf_dtab_netdev *dev, *old_dev; 339 u32 i = *(u32 *)key; 340 u32 ifindex = *(u32 *)value; 341 342 if (unlikely(map_flags > BPF_EXIST)) 343 return -EINVAL; 344 345 if (unlikely(i >= dtab->map.max_entries)) 346 return -E2BIG; 347 348 if (unlikely(map_flags == BPF_NOEXIST)) 349 return -EEXIST; 350 351 if (!ifindex) { 352 dev = NULL; 353 } else { 354 dev = kmalloc(sizeof(*dev), GFP_ATOMIC | __GFP_NOWARN); 355 if (!dev) 356 return -ENOMEM; 357 358 dev->dev = dev_get_by_index(net, ifindex); 359 if (!dev->dev) { 360 kfree(dev); 361 return -EINVAL; 362 } 363 364 dev->key = i; 365 dev->dtab = dtab; 366 } 367 368 /* Use call_rcu() here to ensure rcu critical sections have completed 369 * Remembering the driver side flush operation will happen before the 370 * net device is removed. 371 */ 372 mutex_lock(&dev_map_list_mutex); 373 old_dev = xchg(&dtab->netdev_map[i], dev); 374 if (old_dev) 375 call_rcu(&old_dev->rcu, __dev_map_entry_free); 376 mutex_unlock(&dev_map_list_mutex); 377 378 return 0; 379 } 380 381 const struct bpf_map_ops dev_map_ops = { 382 .map_alloc = dev_map_alloc, 383 .map_free = dev_map_free, 384 .map_get_next_key = dev_map_get_next_key, 385 .map_lookup_elem = dev_map_lookup_elem, 386 .map_update_elem = dev_map_update_elem, 387 .map_delete_elem = dev_map_delete_elem, 388 }; 389 390 static int dev_map_notification(struct notifier_block *notifier, 391 ulong event, void *ptr) 392 { 393 struct net_device *netdev = netdev_notifier_info_to_dev(ptr); 394 struct bpf_dtab *dtab; 395 int i; 396 397 switch (event) { 398 case NETDEV_UNREGISTER: 399 mutex_lock(&dev_map_list_mutex); 400 list_for_each_entry(dtab, &dev_map_list, list) { 401 for (i = 0; i < dtab->map.max_entries; i++) { 402 struct bpf_dtab_netdev *dev; 403 404 dev = dtab->netdev_map[i]; 405 if (!dev || 406 dev->dev->ifindex != netdev->ifindex) 407 continue; 408 dev = xchg(&dtab->netdev_map[i], NULL); 409 if (dev) 410 call_rcu(&dev->rcu, 411 __dev_map_entry_free); 412 } 413 } 414 mutex_unlock(&dev_map_list_mutex); 415 break; 416 default: 417 break; 418 } 419 return NOTIFY_OK; 420 } 421 422 static struct notifier_block dev_map_notifier = { 423 .notifier_call = dev_map_notification, 424 }; 425 426 static int __init dev_map_init(void) 427 { 428 register_netdevice_notifier(&dev_map_notifier); 429 return 0; 430 } 431 432 subsys_initcall(dev_map_init); 433