xref: /openbmc/linux/net/xdp/xsk.c (revision a6c76bb0)
1 // SPDX-License-Identifier: GPL-2.0
2 /* XDP sockets
3  *
4  * AF_XDP sockets allows a channel between XDP programs and userspace
5  * applications.
6  * Copyright(c) 2018 Intel Corporation.
7  *
8  * Author(s): Björn Töpel <bjorn.topel@intel.com>
9  *	      Magnus Karlsson <magnus.karlsson@intel.com>
10  */
11 
12 #define pr_fmt(fmt) "AF_XDP: %s: " fmt, __func__
13 
14 #include <linux/if_xdp.h>
15 #include <linux/init.h>
16 #include <linux/sched/mm.h>
17 #include <linux/sched/signal.h>
18 #include <linux/sched/task.h>
19 #include <linux/socket.h>
20 #include <linux/file.h>
21 #include <linux/uaccess.h>
22 #include <linux/net.h>
23 #include <linux/netdevice.h>
24 #include <linux/rculist.h>
25 #include <net/xdp_sock_drv.h>
26 #include <net/busy_poll.h>
27 #include <net/xdp.h>
28 
29 #include "xsk_queue.h"
30 #include "xdp_umem.h"
31 #include "xsk.h"
32 
33 #define TX_BATCH_SIZE 16
34 
35 static DEFINE_PER_CPU(struct list_head, xskmap_flush_list);
36 
37 void xsk_set_rx_need_wakeup(struct xsk_buff_pool *pool)
38 {
39 	if (pool->cached_need_wakeup & XDP_WAKEUP_RX)
40 		return;
41 
42 	pool->fq->ring->flags |= XDP_RING_NEED_WAKEUP;
43 	pool->cached_need_wakeup |= XDP_WAKEUP_RX;
44 }
45 EXPORT_SYMBOL(xsk_set_rx_need_wakeup);
46 
47 void xsk_set_tx_need_wakeup(struct xsk_buff_pool *pool)
48 {
49 	struct xdp_sock *xs;
50 
51 	if (pool->cached_need_wakeup & XDP_WAKEUP_TX)
52 		return;
53 
54 	rcu_read_lock();
55 	list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
56 		xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP;
57 	}
58 	rcu_read_unlock();
59 
60 	pool->cached_need_wakeup |= XDP_WAKEUP_TX;
61 }
62 EXPORT_SYMBOL(xsk_set_tx_need_wakeup);
63 
64 void xsk_clear_rx_need_wakeup(struct xsk_buff_pool *pool)
65 {
66 	if (!(pool->cached_need_wakeup & XDP_WAKEUP_RX))
67 		return;
68 
69 	pool->fq->ring->flags &= ~XDP_RING_NEED_WAKEUP;
70 	pool->cached_need_wakeup &= ~XDP_WAKEUP_RX;
71 }
72 EXPORT_SYMBOL(xsk_clear_rx_need_wakeup);
73 
74 void xsk_clear_tx_need_wakeup(struct xsk_buff_pool *pool)
75 {
76 	struct xdp_sock *xs;
77 
78 	if (!(pool->cached_need_wakeup & XDP_WAKEUP_TX))
79 		return;
80 
81 	rcu_read_lock();
82 	list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
83 		xs->tx->ring->flags &= ~XDP_RING_NEED_WAKEUP;
84 	}
85 	rcu_read_unlock();
86 
87 	pool->cached_need_wakeup &= ~XDP_WAKEUP_TX;
88 }
89 EXPORT_SYMBOL(xsk_clear_tx_need_wakeup);
90 
91 bool xsk_uses_need_wakeup(struct xsk_buff_pool *pool)
92 {
93 	return pool->uses_need_wakeup;
94 }
95 EXPORT_SYMBOL(xsk_uses_need_wakeup);
96 
97 struct xsk_buff_pool *xsk_get_pool_from_qid(struct net_device *dev,
98 					    u16 queue_id)
99 {
100 	if (queue_id < dev->real_num_rx_queues)
101 		return dev->_rx[queue_id].pool;
102 	if (queue_id < dev->real_num_tx_queues)
103 		return dev->_tx[queue_id].pool;
104 
105 	return NULL;
106 }
107 EXPORT_SYMBOL(xsk_get_pool_from_qid);
108 
109 void xsk_clear_pool_at_qid(struct net_device *dev, u16 queue_id)
110 {
111 	if (queue_id < dev->num_rx_queues)
112 		dev->_rx[queue_id].pool = NULL;
113 	if (queue_id < dev->num_tx_queues)
114 		dev->_tx[queue_id].pool = NULL;
115 }
116 
117 /* The buffer pool is stored both in the _rx struct and the _tx struct as we do
118  * not know if the device has more tx queues than rx, or the opposite.
119  * This might also change during run time.
120  */
121 int xsk_reg_pool_at_qid(struct net_device *dev, struct xsk_buff_pool *pool,
122 			u16 queue_id)
123 {
124 	if (queue_id >= max_t(unsigned int,
125 			      dev->real_num_rx_queues,
126 			      dev->real_num_tx_queues))
127 		return -EINVAL;
128 
129 	if (queue_id < dev->real_num_rx_queues)
130 		dev->_rx[queue_id].pool = pool;
131 	if (queue_id < dev->real_num_tx_queues)
132 		dev->_tx[queue_id].pool = pool;
133 
134 	return 0;
135 }
136 
137 void xp_release(struct xdp_buff_xsk *xskb)
138 {
139 	xskb->pool->free_heads[xskb->pool->free_heads_cnt++] = xskb;
140 }
141 
142 static u64 xp_get_handle(struct xdp_buff_xsk *xskb)
143 {
144 	u64 offset = xskb->xdp.data - xskb->xdp.data_hard_start;
145 
146 	offset += xskb->pool->headroom;
147 	if (!xskb->pool->unaligned)
148 		return xskb->orig_addr + offset;
149 	return xskb->orig_addr + (offset << XSK_UNALIGNED_BUF_OFFSET_SHIFT);
150 }
151 
152 static int __xsk_rcv_zc(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len)
153 {
154 	struct xdp_buff_xsk *xskb = container_of(xdp, struct xdp_buff_xsk, xdp);
155 	u64 addr;
156 	int err;
157 
158 	addr = xp_get_handle(xskb);
159 	err = xskq_prod_reserve_desc(xs->rx, addr, len);
160 	if (err) {
161 		xs->rx_queue_full++;
162 		return err;
163 	}
164 
165 	xp_release(xskb);
166 	return 0;
167 }
168 
169 static void xsk_copy_xdp(struct xdp_buff *to, struct xdp_buff *from, u32 len)
170 {
171 	void *from_buf, *to_buf;
172 	u32 metalen;
173 
174 	if (unlikely(xdp_data_meta_unsupported(from))) {
175 		from_buf = from->data;
176 		to_buf = to->data;
177 		metalen = 0;
178 	} else {
179 		from_buf = from->data_meta;
180 		metalen = from->data - from->data_meta;
181 		to_buf = to->data - metalen;
182 	}
183 
184 	memcpy(to_buf, from_buf, len + metalen);
185 }
186 
187 static int __xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp, u32 len,
188 		     bool explicit_free)
189 {
190 	struct xdp_buff *xsk_xdp;
191 	int err;
192 
193 	if (len > xsk_pool_get_rx_frame_size(xs->pool)) {
194 		xs->rx_dropped++;
195 		return -ENOSPC;
196 	}
197 
198 	xsk_xdp = xsk_buff_alloc(xs->pool);
199 	if (!xsk_xdp) {
200 		xs->rx_dropped++;
201 		return -ENOSPC;
202 	}
203 
204 	xsk_copy_xdp(xsk_xdp, xdp, len);
205 	err = __xsk_rcv_zc(xs, xsk_xdp, len);
206 	if (err) {
207 		xsk_buff_free(xsk_xdp);
208 		return err;
209 	}
210 	if (explicit_free)
211 		xdp_return_buff(xdp);
212 	return 0;
213 }
214 
215 static bool xsk_tx_writeable(struct xdp_sock *xs)
216 {
217 	if (xskq_cons_present_entries(xs->tx) > xs->tx->nentries / 2)
218 		return false;
219 
220 	return true;
221 }
222 
223 static bool xsk_is_bound(struct xdp_sock *xs)
224 {
225 	if (READ_ONCE(xs->state) == XSK_BOUND) {
226 		/* Matches smp_wmb() in bind(). */
227 		smp_rmb();
228 		return true;
229 	}
230 	return false;
231 }
232 
233 static int xsk_rcv(struct xdp_sock *xs, struct xdp_buff *xdp,
234 		   bool explicit_free)
235 {
236 	u32 len;
237 
238 	if (!xsk_is_bound(xs))
239 		return -EINVAL;
240 
241 	if (xs->dev != xdp->rxq->dev || xs->queue_id != xdp->rxq->queue_index)
242 		return -EINVAL;
243 
244 	sk_mark_napi_id_once_xdp(&xs->sk, xdp);
245 	len = xdp->data_end - xdp->data;
246 
247 	return xdp->rxq->mem.type == MEM_TYPE_XSK_BUFF_POOL ?
248 		__xsk_rcv_zc(xs, xdp, len) :
249 		__xsk_rcv(xs, xdp, len, explicit_free);
250 }
251 
252 static void xsk_flush(struct xdp_sock *xs)
253 {
254 	xskq_prod_submit(xs->rx);
255 	__xskq_cons_release(xs->pool->fq);
256 	sock_def_readable(&xs->sk);
257 }
258 
259 int xsk_generic_rcv(struct xdp_sock *xs, struct xdp_buff *xdp)
260 {
261 	int err;
262 
263 	spin_lock_bh(&xs->rx_lock);
264 	err = xsk_rcv(xs, xdp, false);
265 	xsk_flush(xs);
266 	spin_unlock_bh(&xs->rx_lock);
267 	return err;
268 }
269 
270 int __xsk_map_redirect(struct xdp_sock *xs, struct xdp_buff *xdp)
271 {
272 	struct list_head *flush_list = this_cpu_ptr(&xskmap_flush_list);
273 	int err;
274 
275 	err = xsk_rcv(xs, xdp, true);
276 	if (err)
277 		return err;
278 
279 	if (!xs->flush_node.prev)
280 		list_add(&xs->flush_node, flush_list);
281 
282 	return 0;
283 }
284 
285 void __xsk_map_flush(void)
286 {
287 	struct list_head *flush_list = this_cpu_ptr(&xskmap_flush_list);
288 	struct xdp_sock *xs, *tmp;
289 
290 	list_for_each_entry_safe(xs, tmp, flush_list, flush_node) {
291 		xsk_flush(xs);
292 		__list_del_clearprev(&xs->flush_node);
293 	}
294 }
295 
296 void xsk_tx_completed(struct xsk_buff_pool *pool, u32 nb_entries)
297 {
298 	xskq_prod_submit_n(pool->cq, nb_entries);
299 }
300 EXPORT_SYMBOL(xsk_tx_completed);
301 
302 void xsk_tx_release(struct xsk_buff_pool *pool)
303 {
304 	struct xdp_sock *xs;
305 
306 	rcu_read_lock();
307 	list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
308 		__xskq_cons_release(xs->tx);
309 		if (xsk_tx_writeable(xs))
310 			xs->sk.sk_write_space(&xs->sk);
311 	}
312 	rcu_read_unlock();
313 }
314 EXPORT_SYMBOL(xsk_tx_release);
315 
316 bool xsk_tx_peek_desc(struct xsk_buff_pool *pool, struct xdp_desc *desc)
317 {
318 	struct xdp_sock *xs;
319 
320 	rcu_read_lock();
321 	list_for_each_entry_rcu(xs, &pool->xsk_tx_list, tx_list) {
322 		if (!xskq_cons_peek_desc(xs->tx, desc, pool)) {
323 			xs->tx->queue_empty_descs++;
324 			continue;
325 		}
326 
327 		/* This is the backpressure mechanism for the Tx path.
328 		 * Reserve space in the completion queue and only proceed
329 		 * if there is space in it. This avoids having to implement
330 		 * any buffering in the Tx path.
331 		 */
332 		if (xskq_prod_reserve_addr(pool->cq, desc->addr))
333 			goto out;
334 
335 		xskq_cons_release(xs->tx);
336 		rcu_read_unlock();
337 		return true;
338 	}
339 
340 out:
341 	rcu_read_unlock();
342 	return false;
343 }
344 EXPORT_SYMBOL(xsk_tx_peek_desc);
345 
346 static u32 xsk_tx_peek_release_fallback(struct xsk_buff_pool *pool, struct xdp_desc *descs,
347 					u32 max_entries)
348 {
349 	u32 nb_pkts = 0;
350 
351 	while (nb_pkts < max_entries && xsk_tx_peek_desc(pool, &descs[nb_pkts]))
352 		nb_pkts++;
353 
354 	xsk_tx_release(pool);
355 	return nb_pkts;
356 }
357 
358 u32 xsk_tx_peek_release_desc_batch(struct xsk_buff_pool *pool, struct xdp_desc *descs,
359 				   u32 max_entries)
360 {
361 	struct xdp_sock *xs;
362 	u32 nb_pkts;
363 
364 	rcu_read_lock();
365 	if (!list_is_singular(&pool->xsk_tx_list)) {
366 		/* Fallback to the non-batched version */
367 		rcu_read_unlock();
368 		return xsk_tx_peek_release_fallback(pool, descs, max_entries);
369 	}
370 
371 	xs = list_first_or_null_rcu(&pool->xsk_tx_list, struct xdp_sock, tx_list);
372 	if (!xs) {
373 		nb_pkts = 0;
374 		goto out;
375 	}
376 
377 	nb_pkts = xskq_cons_peek_desc_batch(xs->tx, descs, pool, max_entries);
378 	if (!nb_pkts) {
379 		xs->tx->queue_empty_descs++;
380 		goto out;
381 	}
382 
383 	/* This is the backpressure mechanism for the Tx path. Try to
384 	 * reserve space in the completion queue for all packets, but
385 	 * if there are fewer slots available, just process that many
386 	 * packets. This avoids having to implement any buffering in
387 	 * the Tx path.
388 	 */
389 	nb_pkts = xskq_prod_reserve_addr_batch(pool->cq, descs, nb_pkts);
390 	if (!nb_pkts)
391 		goto out;
392 
393 	xskq_cons_release_n(xs->tx, nb_pkts);
394 	__xskq_cons_release(xs->tx);
395 	xs->sk.sk_write_space(&xs->sk);
396 
397 out:
398 	rcu_read_unlock();
399 	return nb_pkts;
400 }
401 EXPORT_SYMBOL(xsk_tx_peek_release_desc_batch);
402 
403 static int xsk_wakeup(struct xdp_sock *xs, u8 flags)
404 {
405 	struct net_device *dev = xs->dev;
406 	int err;
407 
408 	rcu_read_lock();
409 	err = dev->netdev_ops->ndo_xsk_wakeup(dev, xs->queue_id, flags);
410 	rcu_read_unlock();
411 
412 	return err;
413 }
414 
415 static int xsk_zc_xmit(struct xdp_sock *xs)
416 {
417 	return xsk_wakeup(xs, XDP_WAKEUP_TX);
418 }
419 
420 static void xsk_destruct_skb(struct sk_buff *skb)
421 {
422 	u64 addr = (u64)(long)skb_shinfo(skb)->destructor_arg;
423 	struct xdp_sock *xs = xdp_sk(skb->sk);
424 	unsigned long flags;
425 
426 	spin_lock_irqsave(&xs->pool->cq_lock, flags);
427 	xskq_prod_submit_addr(xs->pool->cq, addr);
428 	spin_unlock_irqrestore(&xs->pool->cq_lock, flags);
429 
430 	sock_wfree(skb);
431 }
432 
433 static int xsk_generic_xmit(struct sock *sk)
434 {
435 	struct xdp_sock *xs = xdp_sk(sk);
436 	u32 max_batch = TX_BATCH_SIZE;
437 	bool sent_frame = false;
438 	struct xdp_desc desc;
439 	struct sk_buff *skb;
440 	unsigned long flags;
441 	int err = 0;
442 
443 	mutex_lock(&xs->mutex);
444 
445 	if (xs->queue_id >= xs->dev->real_num_tx_queues)
446 		goto out;
447 
448 	while (xskq_cons_peek_desc(xs->tx, &desc, xs->pool)) {
449 		char *buffer;
450 		u64 addr;
451 		u32 len;
452 
453 		if (max_batch-- == 0) {
454 			err = -EAGAIN;
455 			goto out;
456 		}
457 
458 		len = desc.len;
459 		skb = sock_alloc_send_skb(sk, len, 1, &err);
460 		if (unlikely(!skb))
461 			goto out;
462 
463 		skb_put(skb, len);
464 		addr = desc.addr;
465 		buffer = xsk_buff_raw_get_data(xs->pool, addr);
466 		err = skb_store_bits(skb, 0, buffer, len);
467 		/* This is the backpressure mechanism for the Tx path.
468 		 * Reserve space in the completion queue and only proceed
469 		 * if there is space in it. This avoids having to implement
470 		 * any buffering in the Tx path.
471 		 */
472 		spin_lock_irqsave(&xs->pool->cq_lock, flags);
473 		if (unlikely(err) || xskq_prod_reserve(xs->pool->cq)) {
474 			spin_unlock_irqrestore(&xs->pool->cq_lock, flags);
475 			kfree_skb(skb);
476 			goto out;
477 		}
478 		spin_unlock_irqrestore(&xs->pool->cq_lock, flags);
479 
480 		skb->dev = xs->dev;
481 		skb->priority = sk->sk_priority;
482 		skb->mark = sk->sk_mark;
483 		skb_shinfo(skb)->destructor_arg = (void *)(long)desc.addr;
484 		skb->destructor = xsk_destruct_skb;
485 
486 		err = __dev_direct_xmit(skb, xs->queue_id);
487 		if  (err == NETDEV_TX_BUSY) {
488 			/* Tell user-space to retry the send */
489 			skb->destructor = sock_wfree;
490 			spin_lock_irqsave(&xs->pool->cq_lock, flags);
491 			xskq_prod_cancel(xs->pool->cq);
492 			spin_unlock_irqrestore(&xs->pool->cq_lock, flags);
493 			/* Free skb without triggering the perf drop trace */
494 			consume_skb(skb);
495 			err = -EAGAIN;
496 			goto out;
497 		}
498 
499 		xskq_cons_release(xs->tx);
500 		/* Ignore NET_XMIT_CN as packet might have been sent */
501 		if (err == NET_XMIT_DROP) {
502 			/* SKB completed but not sent */
503 			err = -EBUSY;
504 			goto out;
505 		}
506 
507 		sent_frame = true;
508 	}
509 
510 	xs->tx->queue_empty_descs++;
511 
512 out:
513 	if (sent_frame)
514 		if (xsk_tx_writeable(xs))
515 			sk->sk_write_space(sk);
516 
517 	mutex_unlock(&xs->mutex);
518 	return err;
519 }
520 
521 static int __xsk_sendmsg(struct sock *sk)
522 {
523 	struct xdp_sock *xs = xdp_sk(sk);
524 
525 	if (unlikely(!(xs->dev->flags & IFF_UP)))
526 		return -ENETDOWN;
527 	if (unlikely(!xs->tx))
528 		return -ENOBUFS;
529 
530 	return xs->zc ? xsk_zc_xmit(xs) : xsk_generic_xmit(sk);
531 }
532 
533 static bool xsk_no_wakeup(struct sock *sk)
534 {
535 #ifdef CONFIG_NET_RX_BUSY_POLL
536 	/* Prefer busy-polling, skip the wakeup. */
537 	return READ_ONCE(sk->sk_prefer_busy_poll) && READ_ONCE(sk->sk_ll_usec) &&
538 		READ_ONCE(sk->sk_napi_id) >= MIN_NAPI_ID;
539 #else
540 	return false;
541 #endif
542 }
543 
544 static int xsk_sendmsg(struct socket *sock, struct msghdr *m, size_t total_len)
545 {
546 	bool need_wait = !(m->msg_flags & MSG_DONTWAIT);
547 	struct sock *sk = sock->sk;
548 	struct xdp_sock *xs = xdp_sk(sk);
549 	struct xsk_buff_pool *pool;
550 
551 	if (unlikely(!xsk_is_bound(xs)))
552 		return -ENXIO;
553 	if (unlikely(need_wait))
554 		return -EOPNOTSUPP;
555 
556 	if (sk_can_busy_loop(sk))
557 		sk_busy_loop(sk, 1); /* only support non-blocking sockets */
558 
559 	if (xsk_no_wakeup(sk))
560 		return 0;
561 
562 	pool = xs->pool;
563 	if (pool->cached_need_wakeup & XDP_WAKEUP_TX)
564 		return __xsk_sendmsg(sk);
565 	return 0;
566 }
567 
568 static int xsk_recvmsg(struct socket *sock, struct msghdr *m, size_t len, int flags)
569 {
570 	bool need_wait = !(flags & MSG_DONTWAIT);
571 	struct sock *sk = sock->sk;
572 	struct xdp_sock *xs = xdp_sk(sk);
573 
574 	if (unlikely(!xsk_is_bound(xs)))
575 		return -ENXIO;
576 	if (unlikely(!(xs->dev->flags & IFF_UP)))
577 		return -ENETDOWN;
578 	if (unlikely(!xs->rx))
579 		return -ENOBUFS;
580 	if (unlikely(need_wait))
581 		return -EOPNOTSUPP;
582 
583 	if (sk_can_busy_loop(sk))
584 		sk_busy_loop(sk, 1); /* only support non-blocking sockets */
585 
586 	if (xsk_no_wakeup(sk))
587 		return 0;
588 
589 	if (xs->pool->cached_need_wakeup & XDP_WAKEUP_RX && xs->zc)
590 		return xsk_wakeup(xs, XDP_WAKEUP_RX);
591 	return 0;
592 }
593 
594 static __poll_t xsk_poll(struct file *file, struct socket *sock,
595 			     struct poll_table_struct *wait)
596 {
597 	__poll_t mask = 0;
598 	struct sock *sk = sock->sk;
599 	struct xdp_sock *xs = xdp_sk(sk);
600 	struct xsk_buff_pool *pool;
601 
602 	sock_poll_wait(file, sock, wait);
603 
604 	if (unlikely(!xsk_is_bound(xs)))
605 		return mask;
606 
607 	pool = xs->pool;
608 
609 	if (pool->cached_need_wakeup) {
610 		if (xs->zc)
611 			xsk_wakeup(xs, pool->cached_need_wakeup);
612 		else
613 			/* Poll needs to drive Tx also in copy mode */
614 			__xsk_sendmsg(sk);
615 	}
616 
617 	if (xs->rx && !xskq_prod_is_empty(xs->rx))
618 		mask |= EPOLLIN | EPOLLRDNORM;
619 	if (xs->tx && xsk_tx_writeable(xs))
620 		mask |= EPOLLOUT | EPOLLWRNORM;
621 
622 	return mask;
623 }
624 
625 static int xsk_init_queue(u32 entries, struct xsk_queue **queue,
626 			  bool umem_queue)
627 {
628 	struct xsk_queue *q;
629 
630 	if (entries == 0 || *queue || !is_power_of_2(entries))
631 		return -EINVAL;
632 
633 	q = xskq_create(entries, umem_queue);
634 	if (!q)
635 		return -ENOMEM;
636 
637 	/* Make sure queue is ready before it can be seen by others */
638 	smp_wmb();
639 	WRITE_ONCE(*queue, q);
640 	return 0;
641 }
642 
643 static void xsk_unbind_dev(struct xdp_sock *xs)
644 {
645 	struct net_device *dev = xs->dev;
646 
647 	if (xs->state != XSK_BOUND)
648 		return;
649 	WRITE_ONCE(xs->state, XSK_UNBOUND);
650 
651 	/* Wait for driver to stop using the xdp socket. */
652 	xp_del_xsk(xs->pool, xs);
653 	xs->dev = NULL;
654 	synchronize_net();
655 	dev_put(dev);
656 }
657 
658 static struct xsk_map *xsk_get_map_list_entry(struct xdp_sock *xs,
659 					      struct xdp_sock ***map_entry)
660 {
661 	struct xsk_map *map = NULL;
662 	struct xsk_map_node *node;
663 
664 	*map_entry = NULL;
665 
666 	spin_lock_bh(&xs->map_list_lock);
667 	node = list_first_entry_or_null(&xs->map_list, struct xsk_map_node,
668 					node);
669 	if (node) {
670 		bpf_map_inc(&node->map->map);
671 		map = node->map;
672 		*map_entry = node->map_entry;
673 	}
674 	spin_unlock_bh(&xs->map_list_lock);
675 	return map;
676 }
677 
678 static void xsk_delete_from_maps(struct xdp_sock *xs)
679 {
680 	/* This function removes the current XDP socket from all the
681 	 * maps it resides in. We need to take extra care here, due to
682 	 * the two locks involved. Each map has a lock synchronizing
683 	 * updates to the entries, and each socket has a lock that
684 	 * synchronizes access to the list of maps (map_list). For
685 	 * deadlock avoidance the locks need to be taken in the order
686 	 * "map lock"->"socket map list lock". We start off by
687 	 * accessing the socket map list, and take a reference to the
688 	 * map to guarantee existence between the
689 	 * xsk_get_map_list_entry() and xsk_map_try_sock_delete()
690 	 * calls. Then we ask the map to remove the socket, which
691 	 * tries to remove the socket from the map. Note that there
692 	 * might be updates to the map between
693 	 * xsk_get_map_list_entry() and xsk_map_try_sock_delete().
694 	 */
695 	struct xdp_sock **map_entry = NULL;
696 	struct xsk_map *map;
697 
698 	while ((map = xsk_get_map_list_entry(xs, &map_entry))) {
699 		xsk_map_try_sock_delete(map, xs, map_entry);
700 		bpf_map_put(&map->map);
701 	}
702 }
703 
704 static int xsk_release(struct socket *sock)
705 {
706 	struct sock *sk = sock->sk;
707 	struct xdp_sock *xs = xdp_sk(sk);
708 	struct net *net;
709 
710 	if (!sk)
711 		return 0;
712 
713 	net = sock_net(sk);
714 
715 	mutex_lock(&net->xdp.lock);
716 	sk_del_node_init_rcu(sk);
717 	mutex_unlock(&net->xdp.lock);
718 
719 	local_bh_disable();
720 	sock_prot_inuse_add(net, sk->sk_prot, -1);
721 	local_bh_enable();
722 
723 	xsk_delete_from_maps(xs);
724 	mutex_lock(&xs->mutex);
725 	xsk_unbind_dev(xs);
726 	mutex_unlock(&xs->mutex);
727 
728 	xskq_destroy(xs->rx);
729 	xskq_destroy(xs->tx);
730 	xskq_destroy(xs->fq_tmp);
731 	xskq_destroy(xs->cq_tmp);
732 
733 	sock_orphan(sk);
734 	sock->sk = NULL;
735 
736 	sk_refcnt_debug_release(sk);
737 	sock_put(sk);
738 
739 	return 0;
740 }
741 
742 static struct socket *xsk_lookup_xsk_from_fd(int fd)
743 {
744 	struct socket *sock;
745 	int err;
746 
747 	sock = sockfd_lookup(fd, &err);
748 	if (!sock)
749 		return ERR_PTR(-ENOTSOCK);
750 
751 	if (sock->sk->sk_family != PF_XDP) {
752 		sockfd_put(sock);
753 		return ERR_PTR(-ENOPROTOOPT);
754 	}
755 
756 	return sock;
757 }
758 
759 static bool xsk_validate_queues(struct xdp_sock *xs)
760 {
761 	return xs->fq_tmp && xs->cq_tmp;
762 }
763 
764 static int xsk_bind(struct socket *sock, struct sockaddr *addr, int addr_len)
765 {
766 	struct sockaddr_xdp *sxdp = (struct sockaddr_xdp *)addr;
767 	struct sock *sk = sock->sk;
768 	struct xdp_sock *xs = xdp_sk(sk);
769 	struct net_device *dev;
770 	u32 flags, qid;
771 	int err = 0;
772 
773 	if (addr_len < sizeof(struct sockaddr_xdp))
774 		return -EINVAL;
775 	if (sxdp->sxdp_family != AF_XDP)
776 		return -EINVAL;
777 
778 	flags = sxdp->sxdp_flags;
779 	if (flags & ~(XDP_SHARED_UMEM | XDP_COPY | XDP_ZEROCOPY |
780 		      XDP_USE_NEED_WAKEUP))
781 		return -EINVAL;
782 
783 	rtnl_lock();
784 	mutex_lock(&xs->mutex);
785 	if (xs->state != XSK_READY) {
786 		err = -EBUSY;
787 		goto out_release;
788 	}
789 
790 	dev = dev_get_by_index(sock_net(sk), sxdp->sxdp_ifindex);
791 	if (!dev) {
792 		err = -ENODEV;
793 		goto out_release;
794 	}
795 
796 	if (!xs->rx && !xs->tx) {
797 		err = -EINVAL;
798 		goto out_unlock;
799 	}
800 
801 	qid = sxdp->sxdp_queue_id;
802 
803 	if (flags & XDP_SHARED_UMEM) {
804 		struct xdp_sock *umem_xs;
805 		struct socket *sock;
806 
807 		if ((flags & XDP_COPY) || (flags & XDP_ZEROCOPY) ||
808 		    (flags & XDP_USE_NEED_WAKEUP)) {
809 			/* Cannot specify flags for shared sockets. */
810 			err = -EINVAL;
811 			goto out_unlock;
812 		}
813 
814 		if (xs->umem) {
815 			/* We have already our own. */
816 			err = -EINVAL;
817 			goto out_unlock;
818 		}
819 
820 		sock = xsk_lookup_xsk_from_fd(sxdp->sxdp_shared_umem_fd);
821 		if (IS_ERR(sock)) {
822 			err = PTR_ERR(sock);
823 			goto out_unlock;
824 		}
825 
826 		umem_xs = xdp_sk(sock->sk);
827 		if (!xsk_is_bound(umem_xs)) {
828 			err = -EBADF;
829 			sockfd_put(sock);
830 			goto out_unlock;
831 		}
832 
833 		if (umem_xs->queue_id != qid || umem_xs->dev != dev) {
834 			/* Share the umem with another socket on another qid
835 			 * and/or device.
836 			 */
837 			xs->pool = xp_create_and_assign_umem(xs,
838 							     umem_xs->umem);
839 			if (!xs->pool) {
840 				err = -ENOMEM;
841 				sockfd_put(sock);
842 				goto out_unlock;
843 			}
844 
845 			err = xp_assign_dev_shared(xs->pool, umem_xs->umem,
846 						   dev, qid);
847 			if (err) {
848 				xp_destroy(xs->pool);
849 				xs->pool = NULL;
850 				sockfd_put(sock);
851 				goto out_unlock;
852 			}
853 		} else {
854 			/* Share the buffer pool with the other socket. */
855 			if (xs->fq_tmp || xs->cq_tmp) {
856 				/* Do not allow setting your own fq or cq. */
857 				err = -EINVAL;
858 				sockfd_put(sock);
859 				goto out_unlock;
860 			}
861 
862 			xp_get_pool(umem_xs->pool);
863 			xs->pool = umem_xs->pool;
864 		}
865 
866 		xdp_get_umem(umem_xs->umem);
867 		WRITE_ONCE(xs->umem, umem_xs->umem);
868 		sockfd_put(sock);
869 	} else if (!xs->umem || !xsk_validate_queues(xs)) {
870 		err = -EINVAL;
871 		goto out_unlock;
872 	} else {
873 		/* This xsk has its own umem. */
874 		xs->pool = xp_create_and_assign_umem(xs, xs->umem);
875 		if (!xs->pool) {
876 			err = -ENOMEM;
877 			goto out_unlock;
878 		}
879 
880 		err = xp_assign_dev(xs->pool, dev, qid, flags);
881 		if (err) {
882 			xp_destroy(xs->pool);
883 			xs->pool = NULL;
884 			goto out_unlock;
885 		}
886 	}
887 
888 	/* FQ and CQ are now owned by the buffer pool and cleaned up with it. */
889 	xs->fq_tmp = NULL;
890 	xs->cq_tmp = NULL;
891 
892 	xs->dev = dev;
893 	xs->zc = xs->umem->zc;
894 	xs->queue_id = qid;
895 	xp_add_xsk(xs->pool, xs);
896 
897 out_unlock:
898 	if (err) {
899 		dev_put(dev);
900 	} else {
901 		/* Matches smp_rmb() in bind() for shared umem
902 		 * sockets, and xsk_is_bound().
903 		 */
904 		smp_wmb();
905 		WRITE_ONCE(xs->state, XSK_BOUND);
906 	}
907 out_release:
908 	mutex_unlock(&xs->mutex);
909 	rtnl_unlock();
910 	return err;
911 }
912 
913 struct xdp_umem_reg_v1 {
914 	__u64 addr; /* Start of packet data area */
915 	__u64 len; /* Length of packet data area */
916 	__u32 chunk_size;
917 	__u32 headroom;
918 };
919 
920 static int xsk_setsockopt(struct socket *sock, int level, int optname,
921 			  sockptr_t optval, unsigned int optlen)
922 {
923 	struct sock *sk = sock->sk;
924 	struct xdp_sock *xs = xdp_sk(sk);
925 	int err;
926 
927 	if (level != SOL_XDP)
928 		return -ENOPROTOOPT;
929 
930 	switch (optname) {
931 	case XDP_RX_RING:
932 	case XDP_TX_RING:
933 	{
934 		struct xsk_queue **q;
935 		int entries;
936 
937 		if (optlen < sizeof(entries))
938 			return -EINVAL;
939 		if (copy_from_sockptr(&entries, optval, sizeof(entries)))
940 			return -EFAULT;
941 
942 		mutex_lock(&xs->mutex);
943 		if (xs->state != XSK_READY) {
944 			mutex_unlock(&xs->mutex);
945 			return -EBUSY;
946 		}
947 		q = (optname == XDP_TX_RING) ? &xs->tx : &xs->rx;
948 		err = xsk_init_queue(entries, q, false);
949 		if (!err && optname == XDP_TX_RING)
950 			/* Tx needs to be explicitly woken up the first time */
951 			xs->tx->ring->flags |= XDP_RING_NEED_WAKEUP;
952 		mutex_unlock(&xs->mutex);
953 		return err;
954 	}
955 	case XDP_UMEM_REG:
956 	{
957 		size_t mr_size = sizeof(struct xdp_umem_reg);
958 		struct xdp_umem_reg mr = {};
959 		struct xdp_umem *umem;
960 
961 		if (optlen < sizeof(struct xdp_umem_reg_v1))
962 			return -EINVAL;
963 		else if (optlen < sizeof(mr))
964 			mr_size = sizeof(struct xdp_umem_reg_v1);
965 
966 		if (copy_from_sockptr(&mr, optval, mr_size))
967 			return -EFAULT;
968 
969 		mutex_lock(&xs->mutex);
970 		if (xs->state != XSK_READY || xs->umem) {
971 			mutex_unlock(&xs->mutex);
972 			return -EBUSY;
973 		}
974 
975 		umem = xdp_umem_create(&mr);
976 		if (IS_ERR(umem)) {
977 			mutex_unlock(&xs->mutex);
978 			return PTR_ERR(umem);
979 		}
980 
981 		/* Make sure umem is ready before it can be seen by others */
982 		smp_wmb();
983 		WRITE_ONCE(xs->umem, umem);
984 		mutex_unlock(&xs->mutex);
985 		return 0;
986 	}
987 	case XDP_UMEM_FILL_RING:
988 	case XDP_UMEM_COMPLETION_RING:
989 	{
990 		struct xsk_queue **q;
991 		int entries;
992 
993 		if (copy_from_sockptr(&entries, optval, sizeof(entries)))
994 			return -EFAULT;
995 
996 		mutex_lock(&xs->mutex);
997 		if (xs->state != XSK_READY) {
998 			mutex_unlock(&xs->mutex);
999 			return -EBUSY;
1000 		}
1001 
1002 		q = (optname == XDP_UMEM_FILL_RING) ? &xs->fq_tmp :
1003 			&xs->cq_tmp;
1004 		err = xsk_init_queue(entries, q, true);
1005 		mutex_unlock(&xs->mutex);
1006 		return err;
1007 	}
1008 	default:
1009 		break;
1010 	}
1011 
1012 	return -ENOPROTOOPT;
1013 }
1014 
1015 static void xsk_enter_rxtx_offsets(struct xdp_ring_offset_v1 *ring)
1016 {
1017 	ring->producer = offsetof(struct xdp_rxtx_ring, ptrs.producer);
1018 	ring->consumer = offsetof(struct xdp_rxtx_ring, ptrs.consumer);
1019 	ring->desc = offsetof(struct xdp_rxtx_ring, desc);
1020 }
1021 
1022 static void xsk_enter_umem_offsets(struct xdp_ring_offset_v1 *ring)
1023 {
1024 	ring->producer = offsetof(struct xdp_umem_ring, ptrs.producer);
1025 	ring->consumer = offsetof(struct xdp_umem_ring, ptrs.consumer);
1026 	ring->desc = offsetof(struct xdp_umem_ring, desc);
1027 }
1028 
1029 struct xdp_statistics_v1 {
1030 	__u64 rx_dropped;
1031 	__u64 rx_invalid_descs;
1032 	__u64 tx_invalid_descs;
1033 };
1034 
1035 static int xsk_getsockopt(struct socket *sock, int level, int optname,
1036 			  char __user *optval, int __user *optlen)
1037 {
1038 	struct sock *sk = sock->sk;
1039 	struct xdp_sock *xs = xdp_sk(sk);
1040 	int len;
1041 
1042 	if (level != SOL_XDP)
1043 		return -ENOPROTOOPT;
1044 
1045 	if (get_user(len, optlen))
1046 		return -EFAULT;
1047 	if (len < 0)
1048 		return -EINVAL;
1049 
1050 	switch (optname) {
1051 	case XDP_STATISTICS:
1052 	{
1053 		struct xdp_statistics stats = {};
1054 		bool extra_stats = true;
1055 		size_t stats_size;
1056 
1057 		if (len < sizeof(struct xdp_statistics_v1)) {
1058 			return -EINVAL;
1059 		} else if (len < sizeof(stats)) {
1060 			extra_stats = false;
1061 			stats_size = sizeof(struct xdp_statistics_v1);
1062 		} else {
1063 			stats_size = sizeof(stats);
1064 		}
1065 
1066 		mutex_lock(&xs->mutex);
1067 		stats.rx_dropped = xs->rx_dropped;
1068 		if (extra_stats) {
1069 			stats.rx_ring_full = xs->rx_queue_full;
1070 			stats.rx_fill_ring_empty_descs =
1071 				xs->pool ? xskq_nb_queue_empty_descs(xs->pool->fq) : 0;
1072 			stats.tx_ring_empty_descs = xskq_nb_queue_empty_descs(xs->tx);
1073 		} else {
1074 			stats.rx_dropped += xs->rx_queue_full;
1075 		}
1076 		stats.rx_invalid_descs = xskq_nb_invalid_descs(xs->rx);
1077 		stats.tx_invalid_descs = xskq_nb_invalid_descs(xs->tx);
1078 		mutex_unlock(&xs->mutex);
1079 
1080 		if (copy_to_user(optval, &stats, stats_size))
1081 			return -EFAULT;
1082 		if (put_user(stats_size, optlen))
1083 			return -EFAULT;
1084 
1085 		return 0;
1086 	}
1087 	case XDP_MMAP_OFFSETS:
1088 	{
1089 		struct xdp_mmap_offsets off;
1090 		struct xdp_mmap_offsets_v1 off_v1;
1091 		bool flags_supported = true;
1092 		void *to_copy;
1093 
1094 		if (len < sizeof(off_v1))
1095 			return -EINVAL;
1096 		else if (len < sizeof(off))
1097 			flags_supported = false;
1098 
1099 		if (flags_supported) {
1100 			/* xdp_ring_offset is identical to xdp_ring_offset_v1
1101 			 * except for the flags field added to the end.
1102 			 */
1103 			xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
1104 					       &off.rx);
1105 			xsk_enter_rxtx_offsets((struct xdp_ring_offset_v1 *)
1106 					       &off.tx);
1107 			xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
1108 					       &off.fr);
1109 			xsk_enter_umem_offsets((struct xdp_ring_offset_v1 *)
1110 					       &off.cr);
1111 			off.rx.flags = offsetof(struct xdp_rxtx_ring,
1112 						ptrs.flags);
1113 			off.tx.flags = offsetof(struct xdp_rxtx_ring,
1114 						ptrs.flags);
1115 			off.fr.flags = offsetof(struct xdp_umem_ring,
1116 						ptrs.flags);
1117 			off.cr.flags = offsetof(struct xdp_umem_ring,
1118 						ptrs.flags);
1119 
1120 			len = sizeof(off);
1121 			to_copy = &off;
1122 		} else {
1123 			xsk_enter_rxtx_offsets(&off_v1.rx);
1124 			xsk_enter_rxtx_offsets(&off_v1.tx);
1125 			xsk_enter_umem_offsets(&off_v1.fr);
1126 			xsk_enter_umem_offsets(&off_v1.cr);
1127 
1128 			len = sizeof(off_v1);
1129 			to_copy = &off_v1;
1130 		}
1131 
1132 		if (copy_to_user(optval, to_copy, len))
1133 			return -EFAULT;
1134 		if (put_user(len, optlen))
1135 			return -EFAULT;
1136 
1137 		return 0;
1138 	}
1139 	case XDP_OPTIONS:
1140 	{
1141 		struct xdp_options opts = {};
1142 
1143 		if (len < sizeof(opts))
1144 			return -EINVAL;
1145 
1146 		mutex_lock(&xs->mutex);
1147 		if (xs->zc)
1148 			opts.flags |= XDP_OPTIONS_ZEROCOPY;
1149 		mutex_unlock(&xs->mutex);
1150 
1151 		len = sizeof(opts);
1152 		if (copy_to_user(optval, &opts, len))
1153 			return -EFAULT;
1154 		if (put_user(len, optlen))
1155 			return -EFAULT;
1156 
1157 		return 0;
1158 	}
1159 	default:
1160 		break;
1161 	}
1162 
1163 	return -EOPNOTSUPP;
1164 }
1165 
1166 static int xsk_mmap(struct file *file, struct socket *sock,
1167 		    struct vm_area_struct *vma)
1168 {
1169 	loff_t offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
1170 	unsigned long size = vma->vm_end - vma->vm_start;
1171 	struct xdp_sock *xs = xdp_sk(sock->sk);
1172 	struct xsk_queue *q = NULL;
1173 	unsigned long pfn;
1174 	struct page *qpg;
1175 
1176 	if (READ_ONCE(xs->state) != XSK_READY)
1177 		return -EBUSY;
1178 
1179 	if (offset == XDP_PGOFF_RX_RING) {
1180 		q = READ_ONCE(xs->rx);
1181 	} else if (offset == XDP_PGOFF_TX_RING) {
1182 		q = READ_ONCE(xs->tx);
1183 	} else {
1184 		/* Matches the smp_wmb() in XDP_UMEM_REG */
1185 		smp_rmb();
1186 		if (offset == XDP_UMEM_PGOFF_FILL_RING)
1187 			q = READ_ONCE(xs->fq_tmp);
1188 		else if (offset == XDP_UMEM_PGOFF_COMPLETION_RING)
1189 			q = READ_ONCE(xs->cq_tmp);
1190 	}
1191 
1192 	if (!q)
1193 		return -EINVAL;
1194 
1195 	/* Matches the smp_wmb() in xsk_init_queue */
1196 	smp_rmb();
1197 	qpg = virt_to_head_page(q->ring);
1198 	if (size > page_size(qpg))
1199 		return -EINVAL;
1200 
1201 	pfn = virt_to_phys(q->ring) >> PAGE_SHIFT;
1202 	return remap_pfn_range(vma, vma->vm_start, pfn,
1203 			       size, vma->vm_page_prot);
1204 }
1205 
1206 static int xsk_notifier(struct notifier_block *this,
1207 			unsigned long msg, void *ptr)
1208 {
1209 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1210 	struct net *net = dev_net(dev);
1211 	struct sock *sk;
1212 
1213 	switch (msg) {
1214 	case NETDEV_UNREGISTER:
1215 		mutex_lock(&net->xdp.lock);
1216 		sk_for_each(sk, &net->xdp.list) {
1217 			struct xdp_sock *xs = xdp_sk(sk);
1218 
1219 			mutex_lock(&xs->mutex);
1220 			if (xs->dev == dev) {
1221 				sk->sk_err = ENETDOWN;
1222 				if (!sock_flag(sk, SOCK_DEAD))
1223 					sk->sk_error_report(sk);
1224 
1225 				xsk_unbind_dev(xs);
1226 
1227 				/* Clear device references. */
1228 				xp_clear_dev(xs->pool);
1229 			}
1230 			mutex_unlock(&xs->mutex);
1231 		}
1232 		mutex_unlock(&net->xdp.lock);
1233 		break;
1234 	}
1235 	return NOTIFY_DONE;
1236 }
1237 
1238 static struct proto xsk_proto = {
1239 	.name =		"XDP",
1240 	.owner =	THIS_MODULE,
1241 	.obj_size =	sizeof(struct xdp_sock),
1242 };
1243 
1244 static const struct proto_ops xsk_proto_ops = {
1245 	.family		= PF_XDP,
1246 	.owner		= THIS_MODULE,
1247 	.release	= xsk_release,
1248 	.bind		= xsk_bind,
1249 	.connect	= sock_no_connect,
1250 	.socketpair	= sock_no_socketpair,
1251 	.accept		= sock_no_accept,
1252 	.getname	= sock_no_getname,
1253 	.poll		= xsk_poll,
1254 	.ioctl		= sock_no_ioctl,
1255 	.listen		= sock_no_listen,
1256 	.shutdown	= sock_no_shutdown,
1257 	.setsockopt	= xsk_setsockopt,
1258 	.getsockopt	= xsk_getsockopt,
1259 	.sendmsg	= xsk_sendmsg,
1260 	.recvmsg	= xsk_recvmsg,
1261 	.mmap		= xsk_mmap,
1262 	.sendpage	= sock_no_sendpage,
1263 };
1264 
1265 static void xsk_destruct(struct sock *sk)
1266 {
1267 	struct xdp_sock *xs = xdp_sk(sk);
1268 
1269 	if (!sock_flag(sk, SOCK_DEAD))
1270 		return;
1271 
1272 	if (!xp_put_pool(xs->pool))
1273 		xdp_put_umem(xs->umem, !xs->pool);
1274 
1275 	sk_refcnt_debug_dec(sk);
1276 }
1277 
1278 static int xsk_create(struct net *net, struct socket *sock, int protocol,
1279 		      int kern)
1280 {
1281 	struct xdp_sock *xs;
1282 	struct sock *sk;
1283 
1284 	if (!ns_capable(net->user_ns, CAP_NET_RAW))
1285 		return -EPERM;
1286 	if (sock->type != SOCK_RAW)
1287 		return -ESOCKTNOSUPPORT;
1288 
1289 	if (protocol)
1290 		return -EPROTONOSUPPORT;
1291 
1292 	sock->state = SS_UNCONNECTED;
1293 
1294 	sk = sk_alloc(net, PF_XDP, GFP_KERNEL, &xsk_proto, kern);
1295 	if (!sk)
1296 		return -ENOBUFS;
1297 
1298 	sock->ops = &xsk_proto_ops;
1299 
1300 	sock_init_data(sock, sk);
1301 
1302 	sk->sk_family = PF_XDP;
1303 
1304 	sk->sk_destruct = xsk_destruct;
1305 	sk_refcnt_debug_inc(sk);
1306 
1307 	sock_set_flag(sk, SOCK_RCU_FREE);
1308 
1309 	xs = xdp_sk(sk);
1310 	xs->state = XSK_READY;
1311 	mutex_init(&xs->mutex);
1312 	spin_lock_init(&xs->rx_lock);
1313 
1314 	INIT_LIST_HEAD(&xs->map_list);
1315 	spin_lock_init(&xs->map_list_lock);
1316 
1317 	mutex_lock(&net->xdp.lock);
1318 	sk_add_node_rcu(sk, &net->xdp.list);
1319 	mutex_unlock(&net->xdp.lock);
1320 
1321 	local_bh_disable();
1322 	sock_prot_inuse_add(net, &xsk_proto, 1);
1323 	local_bh_enable();
1324 
1325 	return 0;
1326 }
1327 
1328 static const struct net_proto_family xsk_family_ops = {
1329 	.family = PF_XDP,
1330 	.create = xsk_create,
1331 	.owner	= THIS_MODULE,
1332 };
1333 
1334 static struct notifier_block xsk_netdev_notifier = {
1335 	.notifier_call	= xsk_notifier,
1336 };
1337 
1338 static int __net_init xsk_net_init(struct net *net)
1339 {
1340 	mutex_init(&net->xdp.lock);
1341 	INIT_HLIST_HEAD(&net->xdp.list);
1342 	return 0;
1343 }
1344 
1345 static void __net_exit xsk_net_exit(struct net *net)
1346 {
1347 	WARN_ON_ONCE(!hlist_empty(&net->xdp.list));
1348 }
1349 
1350 static struct pernet_operations xsk_net_ops = {
1351 	.init = xsk_net_init,
1352 	.exit = xsk_net_exit,
1353 };
1354 
1355 static int __init xsk_init(void)
1356 {
1357 	int err, cpu;
1358 
1359 	err = proto_register(&xsk_proto, 0 /* no slab */);
1360 	if (err)
1361 		goto out;
1362 
1363 	err = sock_register(&xsk_family_ops);
1364 	if (err)
1365 		goto out_proto;
1366 
1367 	err = register_pernet_subsys(&xsk_net_ops);
1368 	if (err)
1369 		goto out_sk;
1370 
1371 	err = register_netdevice_notifier(&xsk_netdev_notifier);
1372 	if (err)
1373 		goto out_pernet;
1374 
1375 	for_each_possible_cpu(cpu)
1376 		INIT_LIST_HEAD(&per_cpu(xskmap_flush_list, cpu));
1377 	return 0;
1378 
1379 out_pernet:
1380 	unregister_pernet_subsys(&xsk_net_ops);
1381 out_sk:
1382 	sock_unregister(PF_XDP);
1383 out_proto:
1384 	proto_unregister(&xsk_proto);
1385 out:
1386 	return err;
1387 }
1388 
1389 fs_initcall(xsk_init);
1390