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