• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include <net/xsk_buff_pool.h>
4 #include <net/xdp_sock.h>
5 #include <net/xdp_sock_drv.h>
6 
7 #include "xsk_queue.h"
8 #include "xdp_umem.h"
9 #include "xsk.h"
10 
xp_add_xsk(struct xsk_buff_pool * pool,struct xdp_sock * xs)11 void xp_add_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs)
12 {
13 	unsigned long flags;
14 
15 	if (!xs->tx)
16 		return;
17 
18 	spin_lock_irqsave(&pool->xsk_tx_list_lock, flags);
19 	list_add_rcu(&xs->tx_list, &pool->xsk_tx_list);
20 	spin_unlock_irqrestore(&pool->xsk_tx_list_lock, flags);
21 }
22 
xp_del_xsk(struct xsk_buff_pool * pool,struct xdp_sock * xs)23 void xp_del_xsk(struct xsk_buff_pool *pool, struct xdp_sock *xs)
24 {
25 	unsigned long flags;
26 
27 	if (!xs->tx)
28 		return;
29 
30 	spin_lock_irqsave(&pool->xsk_tx_list_lock, flags);
31 	list_del_rcu(&xs->tx_list);
32 	spin_unlock_irqrestore(&pool->xsk_tx_list_lock, flags);
33 }
34 
xp_destroy(struct xsk_buff_pool * pool)35 void xp_destroy(struct xsk_buff_pool *pool)
36 {
37 	if (!pool)
38 		return;
39 
40 	kvfree(pool->tx_descs);
41 	kvfree(pool->heads);
42 	kvfree(pool);
43 }
44 
xp_alloc_tx_descs(struct xsk_buff_pool * pool,struct xdp_sock * xs)45 int xp_alloc_tx_descs(struct xsk_buff_pool *pool, struct xdp_sock *xs)
46 {
47 	pool->tx_descs = kvcalloc(xs->tx->nentries, sizeof(*pool->tx_descs),
48 				  GFP_KERNEL);
49 	if (!pool->tx_descs)
50 		return -ENOMEM;
51 
52 	return 0;
53 }
54 
xp_create_and_assign_umem(struct xdp_sock * xs,struct xdp_umem * umem)55 struct xsk_buff_pool *xp_create_and_assign_umem(struct xdp_sock *xs,
56 						struct xdp_umem *umem)
57 {
58 	struct xsk_buff_pool *pool;
59 	struct xdp_buff_xsk *xskb;
60 	u32 i;
61 
62 	pool = kvzalloc(struct_size(pool, free_heads, umem->chunks),
63 			GFP_KERNEL);
64 	if (!pool)
65 		goto out;
66 
67 	pool->heads = kvcalloc(umem->chunks, sizeof(*pool->heads), GFP_KERNEL);
68 	if (!pool->heads)
69 		goto out;
70 
71 	if (xs->tx)
72 		if (xp_alloc_tx_descs(pool, xs))
73 			goto out;
74 
75 	pool->chunk_mask = ~((u64)umem->chunk_size - 1);
76 	pool->addrs_cnt = umem->size;
77 	pool->heads_cnt = umem->chunks;
78 	pool->free_heads_cnt = umem->chunks;
79 	pool->headroom = umem->headroom;
80 	pool->chunk_size = umem->chunk_size;
81 	pool->unaligned = umem->flags & XDP_UMEM_UNALIGNED_CHUNK_FLAG;
82 	pool->frame_len = umem->chunk_size - umem->headroom -
83 		XDP_PACKET_HEADROOM;
84 	pool->umem = umem;
85 	pool->addrs = umem->addrs;
86 	INIT_LIST_HEAD(&pool->free_list);
87 	INIT_LIST_HEAD(&pool->xsk_tx_list);
88 	spin_lock_init(&pool->xsk_tx_list_lock);
89 	spin_lock_init(&pool->cq_lock);
90 	refcount_set(&pool->users, 1);
91 
92 	pool->fq = xs->fq_tmp;
93 	pool->cq = xs->cq_tmp;
94 
95 	for (i = 0; i < pool->free_heads_cnt; i++) {
96 		xskb = &pool->heads[i];
97 		xskb->pool = pool;
98 		xskb->xdp.frame_sz = umem->chunk_size - umem->headroom;
99 		pool->free_heads[i] = xskb;
100 	}
101 
102 	return pool;
103 
104 out:
105 	xp_destroy(pool);
106 	return NULL;
107 }
108 
xp_set_rxq_info(struct xsk_buff_pool * pool,struct xdp_rxq_info * rxq)109 void xp_set_rxq_info(struct xsk_buff_pool *pool, struct xdp_rxq_info *rxq)
110 {
111 	u32 i;
112 
113 	for (i = 0; i < pool->heads_cnt; i++)
114 		pool->heads[i].xdp.rxq = rxq;
115 }
116 EXPORT_SYMBOL(xp_set_rxq_info);
117 
xp_disable_drv_zc(struct xsk_buff_pool * pool)118 static void xp_disable_drv_zc(struct xsk_buff_pool *pool)
119 {
120 	struct netdev_bpf bpf;
121 	int err;
122 
123 	ASSERT_RTNL();
124 
125 	if (pool->umem->zc) {
126 		bpf.command = XDP_SETUP_XSK_POOL;
127 		bpf.xsk.pool = NULL;
128 		bpf.xsk.queue_id = pool->queue_id;
129 
130 		err = pool->netdev->netdev_ops->ndo_bpf(pool->netdev, &bpf);
131 
132 		if (err)
133 			WARN(1, "Failed to disable zero-copy!\n");
134 	}
135 }
136 
xp_assign_dev(struct xsk_buff_pool * pool,struct net_device * netdev,u16 queue_id,u16 flags)137 int xp_assign_dev(struct xsk_buff_pool *pool,
138 		  struct net_device *netdev, u16 queue_id, u16 flags)
139 {
140 	bool force_zc, force_copy;
141 	struct netdev_bpf bpf;
142 	int err = 0;
143 
144 	ASSERT_RTNL();
145 
146 	force_zc = flags & XDP_ZEROCOPY;
147 	force_copy = flags & XDP_COPY;
148 
149 	if (force_zc && force_copy)
150 		return -EINVAL;
151 
152 	if (xsk_get_pool_from_qid(netdev, queue_id))
153 		return -EBUSY;
154 
155 	pool->netdev = netdev;
156 	pool->queue_id = queue_id;
157 	err = xsk_reg_pool_at_qid(netdev, pool, queue_id);
158 	if (err)
159 		return err;
160 
161 	if (flags & XDP_USE_NEED_WAKEUP)
162 		pool->uses_need_wakeup = true;
163 	/* Tx needs to be explicitly woken up the first time.  Also
164 	 * for supporting drivers that do not implement this
165 	 * feature. They will always have to call sendto() or poll().
166 	 */
167 	pool->cached_need_wakeup = XDP_WAKEUP_TX;
168 
169 	dev_hold(netdev);
170 
171 	if (force_copy)
172 		/* For copy-mode, we are done. */
173 		return 0;
174 
175 	if (!netdev->netdev_ops->ndo_bpf ||
176 	    !netdev->netdev_ops->ndo_xsk_wakeup) {
177 		err = -EOPNOTSUPP;
178 		goto err_unreg_pool;
179 	}
180 
181 	bpf.command = XDP_SETUP_XSK_POOL;
182 	bpf.xsk.pool = pool;
183 	bpf.xsk.queue_id = queue_id;
184 
185 	err = netdev->netdev_ops->ndo_bpf(netdev, &bpf);
186 	if (err)
187 		goto err_unreg_pool;
188 
189 	if (!pool->dma_pages) {
190 		WARN(1, "Driver did not DMA map zero-copy buffers");
191 		err = -EINVAL;
192 		goto err_unreg_xsk;
193 	}
194 	pool->umem->zc = true;
195 	return 0;
196 
197 err_unreg_xsk:
198 	xp_disable_drv_zc(pool);
199 err_unreg_pool:
200 	if (!force_zc)
201 		err = 0; /* fallback to copy mode */
202 	if (err) {
203 		xsk_clear_pool_at_qid(netdev, queue_id);
204 		dev_put(netdev);
205 	}
206 	return err;
207 }
208 
xp_assign_dev_shared(struct xsk_buff_pool * pool,struct xdp_sock * umem_xs,struct net_device * dev,u16 queue_id)209 int xp_assign_dev_shared(struct xsk_buff_pool *pool, struct xdp_sock *umem_xs,
210 			 struct net_device *dev, u16 queue_id)
211 {
212 	u16 flags;
213 	struct xdp_umem *umem = umem_xs->umem;
214 
215 	/* One fill and completion ring required for each queue id. */
216 	if (!pool->fq || !pool->cq)
217 		return -EINVAL;
218 
219 	flags = umem->zc ? XDP_ZEROCOPY : XDP_COPY;
220 	if (umem_xs->pool->uses_need_wakeup)
221 		flags |= XDP_USE_NEED_WAKEUP;
222 
223 	return xp_assign_dev(pool, dev, queue_id, flags);
224 }
225 
xp_clear_dev(struct xsk_buff_pool * pool)226 void xp_clear_dev(struct xsk_buff_pool *pool)
227 {
228 	if (!pool->netdev)
229 		return;
230 
231 	xp_disable_drv_zc(pool);
232 	xsk_clear_pool_at_qid(pool->netdev, pool->queue_id);
233 	dev_put(pool->netdev);
234 	pool->netdev = NULL;
235 }
236 
xp_release_deferred(struct work_struct * work)237 static void xp_release_deferred(struct work_struct *work)
238 {
239 	struct xsk_buff_pool *pool = container_of(work, struct xsk_buff_pool,
240 						  work);
241 
242 	rtnl_lock();
243 	xp_clear_dev(pool);
244 	rtnl_unlock();
245 
246 	if (pool->fq) {
247 		xskq_destroy(pool->fq);
248 		pool->fq = NULL;
249 	}
250 
251 	if (pool->cq) {
252 		xskq_destroy(pool->cq);
253 		pool->cq = NULL;
254 	}
255 
256 	xdp_put_umem(pool->umem, false);
257 	xp_destroy(pool);
258 }
259 
xp_get_pool(struct xsk_buff_pool * pool)260 void xp_get_pool(struct xsk_buff_pool *pool)
261 {
262 	refcount_inc(&pool->users);
263 }
264 
xp_put_pool(struct xsk_buff_pool * pool)265 bool xp_put_pool(struct xsk_buff_pool *pool)
266 {
267 	if (!pool)
268 		return false;
269 
270 	if (refcount_dec_and_test(&pool->users)) {
271 		INIT_WORK(&pool->work, xp_release_deferred);
272 		schedule_work(&pool->work);
273 		return true;
274 	}
275 
276 	return false;
277 }
278 
xp_find_dma_map(struct xsk_buff_pool * pool)279 static struct xsk_dma_map *xp_find_dma_map(struct xsk_buff_pool *pool)
280 {
281 	struct xsk_dma_map *dma_map;
282 
283 	list_for_each_entry(dma_map, &pool->umem->xsk_dma_list, list) {
284 		if (dma_map->netdev == pool->netdev)
285 			return dma_map;
286 	}
287 
288 	return NULL;
289 }
290 
xp_create_dma_map(struct device * dev,struct net_device * netdev,u32 nr_pages,struct xdp_umem * umem)291 static struct xsk_dma_map *xp_create_dma_map(struct device *dev, struct net_device *netdev,
292 					     u32 nr_pages, struct xdp_umem *umem)
293 {
294 	struct xsk_dma_map *dma_map;
295 
296 	dma_map = kzalloc(sizeof(*dma_map), GFP_KERNEL);
297 	if (!dma_map)
298 		return NULL;
299 
300 	dma_map->dma_pages = kvcalloc(nr_pages, sizeof(*dma_map->dma_pages), GFP_KERNEL);
301 	if (!dma_map->dma_pages) {
302 		kfree(dma_map);
303 		return NULL;
304 	}
305 
306 	dma_map->netdev = netdev;
307 	dma_map->dev = dev;
308 	dma_map->dma_need_sync = false;
309 	dma_map->dma_pages_cnt = nr_pages;
310 	refcount_set(&dma_map->users, 1);
311 	list_add(&dma_map->list, &umem->xsk_dma_list);
312 	return dma_map;
313 }
314 
xp_destroy_dma_map(struct xsk_dma_map * dma_map)315 static void xp_destroy_dma_map(struct xsk_dma_map *dma_map)
316 {
317 	list_del(&dma_map->list);
318 	kvfree(dma_map->dma_pages);
319 	kfree(dma_map);
320 }
321 
__xp_dma_unmap(struct xsk_dma_map * dma_map,unsigned long attrs)322 static void __xp_dma_unmap(struct xsk_dma_map *dma_map, unsigned long attrs)
323 {
324 	dma_addr_t *dma;
325 	u32 i;
326 
327 	for (i = 0; i < dma_map->dma_pages_cnt; i++) {
328 		dma = &dma_map->dma_pages[i];
329 		if (*dma) {
330 			*dma &= ~XSK_NEXT_PG_CONTIG_MASK;
331 			dma_unmap_page_attrs(dma_map->dev, *dma, PAGE_SIZE,
332 					     DMA_BIDIRECTIONAL, attrs);
333 			*dma = 0;
334 		}
335 	}
336 
337 	xp_destroy_dma_map(dma_map);
338 }
339 
xp_dma_unmap(struct xsk_buff_pool * pool,unsigned long attrs)340 void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs)
341 {
342 	struct xsk_dma_map *dma_map;
343 
344 	if (pool->dma_pages_cnt == 0)
345 		return;
346 
347 	dma_map = xp_find_dma_map(pool);
348 	if (!dma_map) {
349 		WARN(1, "Could not find dma_map for device");
350 		return;
351 	}
352 
353 	if (!refcount_dec_and_test(&dma_map->users))
354 		return;
355 
356 	__xp_dma_unmap(dma_map, attrs);
357 	kvfree(pool->dma_pages);
358 	pool->dma_pages_cnt = 0;
359 	pool->dev = NULL;
360 }
361 EXPORT_SYMBOL(xp_dma_unmap);
362 
xp_check_dma_contiguity(struct xsk_dma_map * dma_map)363 static void xp_check_dma_contiguity(struct xsk_dma_map *dma_map)
364 {
365 	u32 i;
366 
367 	for (i = 0; i < dma_map->dma_pages_cnt - 1; i++) {
368 		if (dma_map->dma_pages[i] + PAGE_SIZE == dma_map->dma_pages[i + 1])
369 			dma_map->dma_pages[i] |= XSK_NEXT_PG_CONTIG_MASK;
370 		else
371 			dma_map->dma_pages[i] &= ~XSK_NEXT_PG_CONTIG_MASK;
372 	}
373 }
374 
xp_init_dma_info(struct xsk_buff_pool * pool,struct xsk_dma_map * dma_map)375 static int xp_init_dma_info(struct xsk_buff_pool *pool, struct xsk_dma_map *dma_map)
376 {
377 	pool->dma_pages = kvcalloc(dma_map->dma_pages_cnt, sizeof(*pool->dma_pages), GFP_KERNEL);
378 	if (!pool->dma_pages)
379 		return -ENOMEM;
380 
381 	pool->dev = dma_map->dev;
382 	pool->dma_pages_cnt = dma_map->dma_pages_cnt;
383 	pool->dma_need_sync = dma_map->dma_need_sync;
384 	memcpy(pool->dma_pages, dma_map->dma_pages,
385 	       pool->dma_pages_cnt * sizeof(*pool->dma_pages));
386 
387 	return 0;
388 }
389 
xp_dma_map(struct xsk_buff_pool * pool,struct device * dev,unsigned long attrs,struct page ** pages,u32 nr_pages)390 int xp_dma_map(struct xsk_buff_pool *pool, struct device *dev,
391 	       unsigned long attrs, struct page **pages, u32 nr_pages)
392 {
393 	struct xsk_dma_map *dma_map;
394 	dma_addr_t dma;
395 	int err;
396 	u32 i;
397 
398 	dma_map = xp_find_dma_map(pool);
399 	if (dma_map) {
400 		err = xp_init_dma_info(pool, dma_map);
401 		if (err)
402 			return err;
403 
404 		refcount_inc(&dma_map->users);
405 		return 0;
406 	}
407 
408 	dma_map = xp_create_dma_map(dev, pool->netdev, nr_pages, pool->umem);
409 	if (!dma_map)
410 		return -ENOMEM;
411 
412 	for (i = 0; i < dma_map->dma_pages_cnt; i++) {
413 		dma = dma_map_page_attrs(dev, pages[i], 0, PAGE_SIZE,
414 					 DMA_BIDIRECTIONAL, attrs);
415 		if (dma_mapping_error(dev, dma)) {
416 			__xp_dma_unmap(dma_map, attrs);
417 			return -ENOMEM;
418 		}
419 		if (dma_need_sync(dev, dma))
420 			dma_map->dma_need_sync = true;
421 		dma_map->dma_pages[i] = dma;
422 	}
423 
424 	if (pool->unaligned)
425 		xp_check_dma_contiguity(dma_map);
426 
427 	err = xp_init_dma_info(pool, dma_map);
428 	if (err) {
429 		__xp_dma_unmap(dma_map, attrs);
430 		return err;
431 	}
432 
433 	return 0;
434 }
435 EXPORT_SYMBOL(xp_dma_map);
436 
xp_addr_crosses_non_contig_pg(struct xsk_buff_pool * pool,u64 addr)437 static bool xp_addr_crosses_non_contig_pg(struct xsk_buff_pool *pool,
438 					  u64 addr)
439 {
440 	return xp_desc_crosses_non_contig_pg(pool, addr, pool->chunk_size);
441 }
442 
xp_check_unaligned(struct xsk_buff_pool * pool,u64 * addr)443 static bool xp_check_unaligned(struct xsk_buff_pool *pool, u64 *addr)
444 {
445 	*addr = xp_unaligned_extract_addr(*addr);
446 	if (*addr >= pool->addrs_cnt ||
447 	    *addr + pool->chunk_size > pool->addrs_cnt ||
448 	    xp_addr_crosses_non_contig_pg(pool, *addr))
449 		return false;
450 	return true;
451 }
452 
xp_check_aligned(struct xsk_buff_pool * pool,u64 * addr)453 static bool xp_check_aligned(struct xsk_buff_pool *pool, u64 *addr)
454 {
455 	*addr = xp_aligned_extract_addr(pool, *addr);
456 	return *addr < pool->addrs_cnt;
457 }
458 
__xp_alloc(struct xsk_buff_pool * pool)459 static struct xdp_buff_xsk *__xp_alloc(struct xsk_buff_pool *pool)
460 {
461 	struct xdp_buff_xsk *xskb;
462 	u64 addr;
463 	bool ok;
464 
465 	if (pool->free_heads_cnt == 0)
466 		return NULL;
467 
468 	xskb = pool->free_heads[--pool->free_heads_cnt];
469 
470 	for (;;) {
471 		if (!xskq_cons_peek_addr_unchecked(pool->fq, &addr)) {
472 			pool->fq->queue_empty_descs++;
473 			xp_release(xskb);
474 			return NULL;
475 		}
476 
477 		ok = pool->unaligned ? xp_check_unaligned(pool, &addr) :
478 		     xp_check_aligned(pool, &addr);
479 		if (!ok) {
480 			pool->fq->invalid_descs++;
481 			xskq_cons_release(pool->fq);
482 			continue;
483 		}
484 		break;
485 	}
486 	xskq_cons_release(pool->fq);
487 
488 	xskb->orig_addr = addr;
489 	xskb->xdp.data_hard_start = pool->addrs + addr + pool->headroom;
490 	if (pool->dma_pages_cnt) {
491 		xskb->frame_dma = (pool->dma_pages[addr >> PAGE_SHIFT] &
492 				   ~XSK_NEXT_PG_CONTIG_MASK) +
493 				  (addr & ~PAGE_MASK);
494 		xskb->dma = xskb->frame_dma + pool->headroom +
495 			    XDP_PACKET_HEADROOM;
496 	}
497 	return xskb;
498 }
499 
xp_alloc(struct xsk_buff_pool * pool)500 struct xdp_buff *xp_alloc(struct xsk_buff_pool *pool)
501 {
502 	struct xdp_buff_xsk *xskb;
503 
504 	if (!pool->free_list_cnt) {
505 		xskb = __xp_alloc(pool);
506 		if (!xskb)
507 			return NULL;
508 	} else {
509 		pool->free_list_cnt--;
510 		xskb = list_first_entry(&pool->free_list, struct xdp_buff_xsk,
511 					free_list_node);
512 		list_del(&xskb->free_list_node);
513 	}
514 
515 	xskb->xdp.data = xskb->xdp.data_hard_start + XDP_PACKET_HEADROOM;
516 	xskb->xdp.data_meta = xskb->xdp.data;
517 
518 	if (pool->dma_need_sync) {
519 		dma_sync_single_range_for_device(pool->dev, xskb->dma, 0,
520 						 pool->frame_len,
521 						 DMA_BIDIRECTIONAL);
522 	}
523 	return &xskb->xdp;
524 }
525 EXPORT_SYMBOL(xp_alloc);
526 
xp_can_alloc(struct xsk_buff_pool * pool,u32 count)527 bool xp_can_alloc(struct xsk_buff_pool *pool, u32 count)
528 {
529 	if (pool->free_list_cnt >= count)
530 		return true;
531 	return xskq_cons_has_entries(pool->fq, count - pool->free_list_cnt);
532 }
533 EXPORT_SYMBOL(xp_can_alloc);
534 
xp_free(struct xdp_buff_xsk * xskb)535 void xp_free(struct xdp_buff_xsk *xskb)
536 {
537 	xskb->pool->free_list_cnt++;
538 	list_add(&xskb->free_list_node, &xskb->pool->free_list);
539 }
540 EXPORT_SYMBOL(xp_free);
541 
xp_raw_get_data(struct xsk_buff_pool * pool,u64 addr)542 void *xp_raw_get_data(struct xsk_buff_pool *pool, u64 addr)
543 {
544 	addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr;
545 	return pool->addrs + addr;
546 }
547 EXPORT_SYMBOL(xp_raw_get_data);
548 
xp_raw_get_dma(struct xsk_buff_pool * pool,u64 addr)549 dma_addr_t xp_raw_get_dma(struct xsk_buff_pool *pool, u64 addr)
550 {
551 	addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr;
552 	return (pool->dma_pages[addr >> PAGE_SHIFT] &
553 		~XSK_NEXT_PG_CONTIG_MASK) +
554 		(addr & ~PAGE_MASK);
555 }
556 EXPORT_SYMBOL(xp_raw_get_dma);
557 
xp_dma_sync_for_cpu_slow(struct xdp_buff_xsk * xskb)558 void xp_dma_sync_for_cpu_slow(struct xdp_buff_xsk *xskb)
559 {
560 	dma_sync_single_range_for_cpu(xskb->pool->dev, xskb->dma, 0,
561 				      xskb->pool->frame_len, DMA_BIDIRECTIONAL);
562 }
563 EXPORT_SYMBOL(xp_dma_sync_for_cpu_slow);
564 
xp_dma_sync_for_device_slow(struct xsk_buff_pool * pool,dma_addr_t dma,size_t size)565 void xp_dma_sync_for_device_slow(struct xsk_buff_pool *pool, dma_addr_t dma,
566 				 size_t size)
567 {
568 	dma_sync_single_range_for_device(pool->dev, dma, 0,
569 					 size, DMA_BIDIRECTIONAL);
570 }
571 EXPORT_SYMBOL(xp_dma_sync_for_device_slow);
572