]> asedeno.scripts.mit.edu Git - linux.git/blob - kernel/bpf/devmap.c
bpf, devmap: Pass lockdep expression to RCU lists
[linux.git] / kernel / bpf / devmap.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /* Copyright (c) 2017 Covalent IO, Inc. http://covalent.io
3  */
4
5 /* Devmaps primary use is as a backend map for XDP BPF helper call
6  * bpf_redirect_map(). Because XDP is mostly concerned with performance we
7  * spent some effort to ensure the datapath with redirect maps does not use
8  * any locking. This is a quick note on the details.
9  *
10  * We have three possible paths to get into the devmap control plane bpf
11  * syscalls, bpf programs, and driver side xmit/flush operations. A bpf syscall
12  * will invoke an update, delete, or lookup operation. To ensure updates and
13  * deletes appear atomic from the datapath side xchg() is used to modify the
14  * netdev_map array. Then because the datapath does a lookup into the netdev_map
15  * array (read-only) from an RCU critical section we use call_rcu() to wait for
16  * an rcu grace period before free'ing the old data structures. This ensures the
17  * datapath always has a valid copy. However, the datapath does a "flush"
18  * operation that pushes any pending packets in the driver outside the RCU
19  * critical section. Each bpf_dtab_netdev tracks these pending operations using
20  * a per-cpu flush list. The bpf_dtab_netdev object will not be destroyed  until
21  * this list is empty, indicating outstanding flush operations have completed.
22  *
23  * BPF syscalls may race with BPF program calls on any of the update, delete
24  * or lookup operations. As noted above the xchg() operation also keep the
25  * netdev_map consistent in this case. From the devmap side BPF programs
26  * calling into these operations are the same as multiple user space threads
27  * making system calls.
28  *
29  * Finally, any of the above may race with a netdev_unregister notifier. The
30  * unregister notifier must search for net devices in the map structure that
31  * contain a reference to the net device and remove them. This is a two step
32  * process (a) dereference the bpf_dtab_netdev object in netdev_map and (b)
33  * check to see if the ifindex is the same as the net_device being removed.
34  * When removing the dev a cmpxchg() is used to ensure the correct dev is
35  * removed, in the case of a concurrent update or delete operation it is
36  * possible that the initially referenced dev is no longer in the map. As the
37  * notifier hook walks the map we know that new dev references can not be
38  * added by the user because core infrastructure ensures dev_get_by_index()
39  * calls will fail at this point.
40  *
41  * The devmap_hash type is a map type which interprets keys as ifindexes and
42  * indexes these using a hashmap. This allows maps that use ifindex as key to be
43  * densely packed instead of having holes in the lookup array for unused
44  * ifindexes. The setup and packet enqueue/send code is shared between the two
45  * types of devmap; only the lookup and insertion is different.
46  */
47 #include <linux/bpf.h>
48 #include <net/xdp.h>
49 #include <linux/filter.h>
50 #include <trace/events/xdp.h>
51
52 #define DEV_CREATE_FLAG_MASK \
53         (BPF_F_NUMA_NODE | BPF_F_RDONLY | BPF_F_WRONLY)
54
55 #define DEV_MAP_BULK_SIZE 16
56 struct xdp_dev_bulk_queue {
57         struct xdp_frame *q[DEV_MAP_BULK_SIZE];
58         struct list_head flush_node;
59         struct net_device *dev;
60         struct net_device *dev_rx;
61         unsigned int count;
62 };
63
64 struct bpf_dtab_netdev {
65         struct net_device *dev; /* must be first member, due to tracepoint */
66         struct hlist_node index_hlist;
67         struct bpf_dtab *dtab;
68         struct rcu_head rcu;
69         unsigned int idx;
70 };
71
72 struct bpf_dtab {
73         struct bpf_map map;
74         struct bpf_dtab_netdev **netdev_map; /* DEVMAP type only */
75         struct list_head list;
76
77         /* these are only used for DEVMAP_HASH type maps */
78         struct hlist_head *dev_index_head;
79         spinlock_t index_lock;
80         unsigned int items;
81         u32 n_buckets;
82 };
83
84 static DEFINE_PER_CPU(struct list_head, dev_flush_list);
85 static DEFINE_SPINLOCK(dev_map_lock);
86 static LIST_HEAD(dev_map_list);
87
88 static struct hlist_head *dev_map_create_hash(unsigned int entries)
89 {
90         int i;
91         struct hlist_head *hash;
92
93         hash = kmalloc_array(entries, sizeof(*hash), GFP_KERNEL);
94         if (hash != NULL)
95                 for (i = 0; i < entries; i++)
96                         INIT_HLIST_HEAD(&hash[i]);
97
98         return hash;
99 }
100
101 static inline struct hlist_head *dev_map_index_hash(struct bpf_dtab *dtab,
102                                                     int idx)
103 {
104         return &dtab->dev_index_head[idx & (dtab->n_buckets - 1)];
105 }
106
107 static int dev_map_init_map(struct bpf_dtab *dtab, union bpf_attr *attr)
108 {
109         u64 cost = 0;
110         int err;
111
112         /* check sanity of attributes */
113         if (attr->max_entries == 0 || attr->key_size != 4 ||
114             attr->value_size != 4 || attr->map_flags & ~DEV_CREATE_FLAG_MASK)
115                 return -EINVAL;
116
117         /* Lookup returns a pointer straight to dev->ifindex, so make sure the
118          * verifier prevents writes from the BPF side
119          */
120         attr->map_flags |= BPF_F_RDONLY_PROG;
121
122
123         bpf_map_init_from_attr(&dtab->map, attr);
124
125         if (attr->map_type == BPF_MAP_TYPE_DEVMAP_HASH) {
126                 dtab->n_buckets = roundup_pow_of_two(dtab->map.max_entries);
127
128                 if (!dtab->n_buckets) /* Overflow check */
129                         return -EINVAL;
130                 cost += (u64) sizeof(struct hlist_head) * dtab->n_buckets;
131         } else {
132                 cost += (u64) dtab->map.max_entries * sizeof(struct bpf_dtab_netdev *);
133         }
134
135         /* if map size is larger than memlock limit, reject it */
136         err = bpf_map_charge_init(&dtab->map.memory, cost);
137         if (err)
138                 return -EINVAL;
139
140         if (attr->map_type == BPF_MAP_TYPE_DEVMAP_HASH) {
141                 dtab->dev_index_head = dev_map_create_hash(dtab->n_buckets);
142                 if (!dtab->dev_index_head)
143                         goto free_charge;
144
145                 spin_lock_init(&dtab->index_lock);
146         } else {
147                 dtab->netdev_map = bpf_map_area_alloc(dtab->map.max_entries *
148                                                       sizeof(struct bpf_dtab_netdev *),
149                                                       dtab->map.numa_node);
150                 if (!dtab->netdev_map)
151                         goto free_charge;
152         }
153
154         return 0;
155
156 free_charge:
157         bpf_map_charge_finish(&dtab->map.memory);
158         return -ENOMEM;
159 }
160
161 static struct bpf_map *dev_map_alloc(union bpf_attr *attr)
162 {
163         struct bpf_dtab *dtab;
164         int err;
165
166         if (!capable(CAP_NET_ADMIN))
167                 return ERR_PTR(-EPERM);
168
169         dtab = kzalloc(sizeof(*dtab), GFP_USER);
170         if (!dtab)
171                 return ERR_PTR(-ENOMEM);
172
173         err = dev_map_init_map(dtab, attr);
174         if (err) {
175                 kfree(dtab);
176                 return ERR_PTR(err);
177         }
178
179         spin_lock(&dev_map_lock);
180         list_add_tail_rcu(&dtab->list, &dev_map_list);
181         spin_unlock(&dev_map_lock);
182
183         return &dtab->map;
184 }
185
186 static void dev_map_free(struct bpf_map *map)
187 {
188         struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
189         int i;
190
191         /* At this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0,
192          * so the programs (can be more than one that used this map) were
193          * disconnected from events. Wait for outstanding critical sections in
194          * these programs to complete. The rcu critical section only guarantees
195          * no further reads against netdev_map. It does __not__ ensure pending
196          * flush operations (if any) are complete.
197          */
198
199         spin_lock(&dev_map_lock);
200         list_del_rcu(&dtab->list);
201         spin_unlock(&dev_map_lock);
202
203         bpf_clear_redirect_map(map);
204         synchronize_rcu();
205
206         /* Make sure prior __dev_map_entry_free() have completed. */
207         rcu_barrier();
208
209         if (dtab->map.map_type == BPF_MAP_TYPE_DEVMAP_HASH) {
210                 for (i = 0; i < dtab->n_buckets; i++) {
211                         struct bpf_dtab_netdev *dev;
212                         struct hlist_head *head;
213                         struct hlist_node *next;
214
215                         head = dev_map_index_hash(dtab, i);
216
217                         hlist_for_each_entry_safe(dev, next, head, index_hlist) {
218                                 hlist_del_rcu(&dev->index_hlist);
219                                 dev_put(dev->dev);
220                                 kfree(dev);
221                         }
222                 }
223
224                 kfree(dtab->dev_index_head);
225         } else {
226                 for (i = 0; i < dtab->map.max_entries; i++) {
227                         struct bpf_dtab_netdev *dev;
228
229                         dev = dtab->netdev_map[i];
230                         if (!dev)
231                                 continue;
232
233                         dev_put(dev->dev);
234                         kfree(dev);
235                 }
236
237                 bpf_map_area_free(dtab->netdev_map);
238         }
239
240         kfree(dtab);
241 }
242
243 static int dev_map_get_next_key(struct bpf_map *map, void *key, void *next_key)
244 {
245         struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
246         u32 index = key ? *(u32 *)key : U32_MAX;
247         u32 *next = next_key;
248
249         if (index >= dtab->map.max_entries) {
250                 *next = 0;
251                 return 0;
252         }
253
254         if (index == dtab->map.max_entries - 1)
255                 return -ENOENT;
256         *next = index + 1;
257         return 0;
258 }
259
260 struct bpf_dtab_netdev *__dev_map_hash_lookup_elem(struct bpf_map *map, u32 key)
261 {
262         struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
263         struct hlist_head *head = dev_map_index_hash(dtab, key);
264         struct bpf_dtab_netdev *dev;
265
266         hlist_for_each_entry_rcu(dev, head, index_hlist,
267                                  lockdep_is_held(&dtab->index_lock))
268                 if (dev->idx == key)
269                         return dev;
270
271         return NULL;
272 }
273
274 static int dev_map_hash_get_next_key(struct bpf_map *map, void *key,
275                                     void *next_key)
276 {
277         struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
278         u32 idx, *next = next_key;
279         struct bpf_dtab_netdev *dev, *next_dev;
280         struct hlist_head *head;
281         int i = 0;
282
283         if (!key)
284                 goto find_first;
285
286         idx = *(u32 *)key;
287
288         dev = __dev_map_hash_lookup_elem(map, idx);
289         if (!dev)
290                 goto find_first;
291
292         next_dev = hlist_entry_safe(rcu_dereference_raw(hlist_next_rcu(&dev->index_hlist)),
293                                     struct bpf_dtab_netdev, index_hlist);
294
295         if (next_dev) {
296                 *next = next_dev->idx;
297                 return 0;
298         }
299
300         i = idx & (dtab->n_buckets - 1);
301         i++;
302
303  find_first:
304         for (; i < dtab->n_buckets; i++) {
305                 head = dev_map_index_hash(dtab, i);
306
307                 next_dev = hlist_entry_safe(rcu_dereference_raw(hlist_first_rcu(head)),
308                                             struct bpf_dtab_netdev,
309                                             index_hlist);
310                 if (next_dev) {
311                         *next = next_dev->idx;
312                         return 0;
313                 }
314         }
315
316         return -ENOENT;
317 }
318
319 static int bq_xmit_all(struct xdp_dev_bulk_queue *bq, u32 flags)
320 {
321         struct net_device *dev = bq->dev;
322         int sent = 0, drops = 0, err = 0;
323         int i;
324
325         if (unlikely(!bq->count))
326                 return 0;
327
328         for (i = 0; i < bq->count; i++) {
329                 struct xdp_frame *xdpf = bq->q[i];
330
331                 prefetch(xdpf);
332         }
333
334         sent = dev->netdev_ops->ndo_xdp_xmit(dev, bq->count, bq->q, flags);
335         if (sent < 0) {
336                 err = sent;
337                 sent = 0;
338                 goto error;
339         }
340         drops = bq->count - sent;
341 out:
342         bq->count = 0;
343
344         trace_xdp_devmap_xmit(bq->dev_rx, dev, sent, drops, err);
345         bq->dev_rx = NULL;
346         __list_del_clearprev(&bq->flush_node);
347         return 0;
348 error:
349         /* If ndo_xdp_xmit fails with an errno, no frames have been
350          * xmit'ed and it's our responsibility to them free all.
351          */
352         for (i = 0; i < bq->count; i++) {
353                 struct xdp_frame *xdpf = bq->q[i];
354
355                 xdp_return_frame_rx_napi(xdpf);
356                 drops++;
357         }
358         goto out;
359 }
360
361 /* __dev_flush is called from xdp_do_flush() which _must_ be signaled
362  * from the driver before returning from its napi->poll() routine. The poll()
363  * routine is called either from busy_poll context or net_rx_action signaled
364  * from NET_RX_SOFTIRQ. Either way the poll routine must complete before the
365  * net device can be torn down. On devmap tear down we ensure the flush list
366  * is empty before completing to ensure all flush operations have completed.
367  */
368 void __dev_flush(void)
369 {
370         struct list_head *flush_list = this_cpu_ptr(&dev_flush_list);
371         struct xdp_dev_bulk_queue *bq, *tmp;
372
373         rcu_read_lock();
374         list_for_each_entry_safe(bq, tmp, flush_list, flush_node)
375                 bq_xmit_all(bq, XDP_XMIT_FLUSH);
376         rcu_read_unlock();
377 }
378
379 /* rcu_read_lock (from syscall and BPF contexts) ensures that if a delete and/or
380  * update happens in parallel here a dev_put wont happen until after reading the
381  * ifindex.
382  */
383 struct bpf_dtab_netdev *__dev_map_lookup_elem(struct bpf_map *map, u32 key)
384 {
385         struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
386         struct bpf_dtab_netdev *obj;
387
388         if (key >= map->max_entries)
389                 return NULL;
390
391         obj = READ_ONCE(dtab->netdev_map[key]);
392         return obj;
393 }
394
395 /* Runs under RCU-read-side, plus in softirq under NAPI protection.
396  * Thus, safe percpu variable access.
397  */
398 static int bq_enqueue(struct net_device *dev, struct xdp_frame *xdpf,
399                       struct net_device *dev_rx)
400 {
401         struct list_head *flush_list = this_cpu_ptr(&dev_flush_list);
402         struct xdp_dev_bulk_queue *bq = this_cpu_ptr(dev->xdp_bulkq);
403
404         if (unlikely(bq->count == DEV_MAP_BULK_SIZE))
405                 bq_xmit_all(bq, 0);
406
407         /* Ingress dev_rx will be the same for all xdp_frame's in
408          * bulk_queue, because bq stored per-CPU and must be flushed
409          * from net_device drivers NAPI func end.
410          */
411         if (!bq->dev_rx)
412                 bq->dev_rx = dev_rx;
413
414         bq->q[bq->count++] = xdpf;
415
416         if (!bq->flush_node.prev)
417                 list_add(&bq->flush_node, flush_list);
418
419         return 0;
420 }
421
422 static inline int __xdp_enqueue(struct net_device *dev, struct xdp_buff *xdp,
423                                struct net_device *dev_rx)
424 {
425         struct xdp_frame *xdpf;
426         int err;
427
428         if (!dev->netdev_ops->ndo_xdp_xmit)
429                 return -EOPNOTSUPP;
430
431         err = xdp_ok_fwd_dev(dev, xdp->data_end - xdp->data);
432         if (unlikely(err))
433                 return err;
434
435         xdpf = convert_to_xdp_frame(xdp);
436         if (unlikely(!xdpf))
437                 return -EOVERFLOW;
438
439         return bq_enqueue(dev, xdpf, dev_rx);
440 }
441
442 int dev_xdp_enqueue(struct net_device *dev, struct xdp_buff *xdp,
443                     struct net_device *dev_rx)
444 {
445         return __xdp_enqueue(dev, xdp, dev_rx);
446 }
447
448 int dev_map_enqueue(struct bpf_dtab_netdev *dst, struct xdp_buff *xdp,
449                     struct net_device *dev_rx)
450 {
451         struct net_device *dev = dst->dev;
452
453         return __xdp_enqueue(dev, xdp, dev_rx);
454 }
455
456 int dev_map_generic_redirect(struct bpf_dtab_netdev *dst, struct sk_buff *skb,
457                              struct bpf_prog *xdp_prog)
458 {
459         int err;
460
461         err = xdp_ok_fwd_dev(dst->dev, skb->len);
462         if (unlikely(err))
463                 return err;
464         skb->dev = dst->dev;
465         generic_xdp_tx(skb, xdp_prog);
466
467         return 0;
468 }
469
470 static void *dev_map_lookup_elem(struct bpf_map *map, void *key)
471 {
472         struct bpf_dtab_netdev *obj = __dev_map_lookup_elem(map, *(u32 *)key);
473         struct net_device *dev = obj ? obj->dev : NULL;
474
475         return dev ? &dev->ifindex : NULL;
476 }
477
478 static void *dev_map_hash_lookup_elem(struct bpf_map *map, void *key)
479 {
480         struct bpf_dtab_netdev *obj = __dev_map_hash_lookup_elem(map,
481                                                                 *(u32 *)key);
482         struct net_device *dev = obj ? obj->dev : NULL;
483
484         return dev ? &dev->ifindex : NULL;
485 }
486
487 static void __dev_map_entry_free(struct rcu_head *rcu)
488 {
489         struct bpf_dtab_netdev *dev;
490
491         dev = container_of(rcu, struct bpf_dtab_netdev, rcu);
492         dev_put(dev->dev);
493         kfree(dev);
494 }
495
496 static int dev_map_delete_elem(struct bpf_map *map, void *key)
497 {
498         struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
499         struct bpf_dtab_netdev *old_dev;
500         int k = *(u32 *)key;
501
502         if (k >= map->max_entries)
503                 return -EINVAL;
504
505         /* Use call_rcu() here to ensure any rcu critical sections have
506          * completed, but this does not guarantee a flush has happened
507          * yet. Because driver side rcu_read_lock/unlock only protects the
508          * running XDP program. However, for pending flush operations the
509          * dev and ctx are stored in another per cpu map. And additionally,
510          * the driver tear down ensures all soft irqs are complete before
511          * removing the net device in the case of dev_put equals zero.
512          */
513         old_dev = xchg(&dtab->netdev_map[k], NULL);
514         if (old_dev)
515                 call_rcu(&old_dev->rcu, __dev_map_entry_free);
516         return 0;
517 }
518
519 static int dev_map_hash_delete_elem(struct bpf_map *map, void *key)
520 {
521         struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
522         struct bpf_dtab_netdev *old_dev;
523         int k = *(u32 *)key;
524         unsigned long flags;
525         int ret = -ENOENT;
526
527         spin_lock_irqsave(&dtab->index_lock, flags);
528
529         old_dev = __dev_map_hash_lookup_elem(map, k);
530         if (old_dev) {
531                 dtab->items--;
532                 hlist_del_init_rcu(&old_dev->index_hlist);
533                 call_rcu(&old_dev->rcu, __dev_map_entry_free);
534                 ret = 0;
535         }
536         spin_unlock_irqrestore(&dtab->index_lock, flags);
537
538         return ret;
539 }
540
541 static struct bpf_dtab_netdev *__dev_map_alloc_node(struct net *net,
542                                                     struct bpf_dtab *dtab,
543                                                     u32 ifindex,
544                                                     unsigned int idx)
545 {
546         struct bpf_dtab_netdev *dev;
547
548         dev = kmalloc_node(sizeof(*dev), GFP_ATOMIC | __GFP_NOWARN,
549                            dtab->map.numa_node);
550         if (!dev)
551                 return ERR_PTR(-ENOMEM);
552
553         dev->dev = dev_get_by_index(net, ifindex);
554         if (!dev->dev) {
555                 kfree(dev);
556                 return ERR_PTR(-EINVAL);
557         }
558
559         dev->idx = idx;
560         dev->dtab = dtab;
561
562         return dev;
563 }
564
565 static int __dev_map_update_elem(struct net *net, struct bpf_map *map,
566                                  void *key, void *value, u64 map_flags)
567 {
568         struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
569         struct bpf_dtab_netdev *dev, *old_dev;
570         u32 ifindex = *(u32 *)value;
571         u32 i = *(u32 *)key;
572
573         if (unlikely(map_flags > BPF_EXIST))
574                 return -EINVAL;
575         if (unlikely(i >= dtab->map.max_entries))
576                 return -E2BIG;
577         if (unlikely(map_flags == BPF_NOEXIST))
578                 return -EEXIST;
579
580         if (!ifindex) {
581                 dev = NULL;
582         } else {
583                 dev = __dev_map_alloc_node(net, dtab, ifindex, i);
584                 if (IS_ERR(dev))
585                         return PTR_ERR(dev);
586         }
587
588         /* Use call_rcu() here to ensure rcu critical sections have completed
589          * Remembering the driver side flush operation will happen before the
590          * net device is removed.
591          */
592         old_dev = xchg(&dtab->netdev_map[i], dev);
593         if (old_dev)
594                 call_rcu(&old_dev->rcu, __dev_map_entry_free);
595
596         return 0;
597 }
598
599 static int dev_map_update_elem(struct bpf_map *map, void *key, void *value,
600                                u64 map_flags)
601 {
602         return __dev_map_update_elem(current->nsproxy->net_ns,
603                                      map, key, value, map_flags);
604 }
605
606 static int __dev_map_hash_update_elem(struct net *net, struct bpf_map *map,
607                                      void *key, void *value, u64 map_flags)
608 {
609         struct bpf_dtab *dtab = container_of(map, struct bpf_dtab, map);
610         struct bpf_dtab_netdev *dev, *old_dev;
611         u32 ifindex = *(u32 *)value;
612         u32 idx = *(u32 *)key;
613         unsigned long flags;
614         int err = -EEXIST;
615
616         if (unlikely(map_flags > BPF_EXIST || !ifindex))
617                 return -EINVAL;
618
619         spin_lock_irqsave(&dtab->index_lock, flags);
620
621         old_dev = __dev_map_hash_lookup_elem(map, idx);
622         if (old_dev && (map_flags & BPF_NOEXIST))
623                 goto out_err;
624
625         dev = __dev_map_alloc_node(net, dtab, ifindex, idx);
626         if (IS_ERR(dev)) {
627                 err = PTR_ERR(dev);
628                 goto out_err;
629         }
630
631         if (old_dev) {
632                 hlist_del_rcu(&old_dev->index_hlist);
633         } else {
634                 if (dtab->items >= dtab->map.max_entries) {
635                         spin_unlock_irqrestore(&dtab->index_lock, flags);
636                         call_rcu(&dev->rcu, __dev_map_entry_free);
637                         return -E2BIG;
638                 }
639                 dtab->items++;
640         }
641
642         hlist_add_head_rcu(&dev->index_hlist,
643                            dev_map_index_hash(dtab, idx));
644         spin_unlock_irqrestore(&dtab->index_lock, flags);
645
646         if (old_dev)
647                 call_rcu(&old_dev->rcu, __dev_map_entry_free);
648
649         return 0;
650
651 out_err:
652         spin_unlock_irqrestore(&dtab->index_lock, flags);
653         return err;
654 }
655
656 static int dev_map_hash_update_elem(struct bpf_map *map, void *key, void *value,
657                                    u64 map_flags)
658 {
659         return __dev_map_hash_update_elem(current->nsproxy->net_ns,
660                                          map, key, value, map_flags);
661 }
662
663 const struct bpf_map_ops dev_map_ops = {
664         .map_alloc = dev_map_alloc,
665         .map_free = dev_map_free,
666         .map_get_next_key = dev_map_get_next_key,
667         .map_lookup_elem = dev_map_lookup_elem,
668         .map_update_elem = dev_map_update_elem,
669         .map_delete_elem = dev_map_delete_elem,
670         .map_check_btf = map_check_no_btf,
671 };
672
673 const struct bpf_map_ops dev_map_hash_ops = {
674         .map_alloc = dev_map_alloc,
675         .map_free = dev_map_free,
676         .map_get_next_key = dev_map_hash_get_next_key,
677         .map_lookup_elem = dev_map_hash_lookup_elem,
678         .map_update_elem = dev_map_hash_update_elem,
679         .map_delete_elem = dev_map_hash_delete_elem,
680         .map_check_btf = map_check_no_btf,
681 };
682
683 static void dev_map_hash_remove_netdev(struct bpf_dtab *dtab,
684                                        struct net_device *netdev)
685 {
686         unsigned long flags;
687         u32 i;
688
689         spin_lock_irqsave(&dtab->index_lock, flags);
690         for (i = 0; i < dtab->n_buckets; i++) {
691                 struct bpf_dtab_netdev *dev;
692                 struct hlist_head *head;
693                 struct hlist_node *next;
694
695                 head = dev_map_index_hash(dtab, i);
696
697                 hlist_for_each_entry_safe(dev, next, head, index_hlist) {
698                         if (netdev != dev->dev)
699                                 continue;
700
701                         dtab->items--;
702                         hlist_del_rcu(&dev->index_hlist);
703                         call_rcu(&dev->rcu, __dev_map_entry_free);
704                 }
705         }
706         spin_unlock_irqrestore(&dtab->index_lock, flags);
707 }
708
709 static int dev_map_notification(struct notifier_block *notifier,
710                                 ulong event, void *ptr)
711 {
712         struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
713         struct bpf_dtab *dtab;
714         int i, cpu;
715
716         switch (event) {
717         case NETDEV_REGISTER:
718                 if (!netdev->netdev_ops->ndo_xdp_xmit || netdev->xdp_bulkq)
719                         break;
720
721                 /* will be freed in free_netdev() */
722                 netdev->xdp_bulkq =
723                         __alloc_percpu_gfp(sizeof(struct xdp_dev_bulk_queue),
724                                            sizeof(void *), GFP_ATOMIC);
725                 if (!netdev->xdp_bulkq)
726                         return NOTIFY_BAD;
727
728                 for_each_possible_cpu(cpu)
729                         per_cpu_ptr(netdev->xdp_bulkq, cpu)->dev = netdev;
730                 break;
731         case NETDEV_UNREGISTER:
732                 /* This rcu_read_lock/unlock pair is needed because
733                  * dev_map_list is an RCU list AND to ensure a delete
734                  * operation does not free a netdev_map entry while we
735                  * are comparing it against the netdev being unregistered.
736                  */
737                 rcu_read_lock();
738                 list_for_each_entry_rcu(dtab, &dev_map_list, list) {
739                         if (dtab->map.map_type == BPF_MAP_TYPE_DEVMAP_HASH) {
740                                 dev_map_hash_remove_netdev(dtab, netdev);
741                                 continue;
742                         }
743
744                         for (i = 0; i < dtab->map.max_entries; i++) {
745                                 struct bpf_dtab_netdev *dev, *odev;
746
747                                 dev = READ_ONCE(dtab->netdev_map[i]);
748                                 if (!dev || netdev != dev->dev)
749                                         continue;
750                                 odev = cmpxchg(&dtab->netdev_map[i], dev, NULL);
751                                 if (dev == odev)
752                                         call_rcu(&dev->rcu,
753                                                  __dev_map_entry_free);
754                         }
755                 }
756                 rcu_read_unlock();
757                 break;
758         default:
759                 break;
760         }
761         return NOTIFY_OK;
762 }
763
764 static struct notifier_block dev_map_notifier = {
765         .notifier_call = dev_map_notification,
766 };
767
768 static int __init dev_map_init(void)
769 {
770         int cpu;
771
772         /* Assure tracepoint shadow struct _bpf_dtab_netdev is in sync */
773         BUILD_BUG_ON(offsetof(struct bpf_dtab_netdev, dev) !=
774                      offsetof(struct _bpf_dtab_netdev, dev));
775         register_netdevice_notifier(&dev_map_notifier);
776
777         for_each_possible_cpu(cpu)
778                 INIT_LIST_HEAD(&per_cpu(dev_flush_list, cpu));
779         return 0;
780 }
781
782 subsys_initcall(dev_map_init);