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