blob: fd39bb660ebcda3ca61152ef34d2e4de40b9cf02 [file] [log] [blame]
Björn Töpel2b434702020-05-20 21:20:53 +02001// SPDX-License-Identifier: GPL-2.0
2
3#include <net/xsk_buff_pool.h>
4#include <net/xdp_sock.h>
Magnus Karlsson1c1efc22020-08-28 10:26:17 +02005#include <net/xdp_sock_drv.h>
Björn Töpel2b434702020-05-20 21:20:53 +02006
7#include "xsk_queue.h"
Magnus Karlsson1c1efc22020-08-28 10:26:17 +02008#include "xdp_umem.h"
9#include "xsk.h"
Björn Töpel2b434702020-05-20 21:20:53 +020010
Magnus Karlssona5aa8e52020-08-28 10:26:20 +020011void 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
23void 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
Björn Töpel2b434702020-05-20 21:20:53 +020035void xp_destroy(struct xsk_buff_pool *pool)
36{
37 if (!pool)
38 return;
39
Björn Töpel2b434702020-05-20 21:20:53 +020040 kvfree(pool->heads);
41 kvfree(pool);
42}
43
Magnus Karlsson1c1efc22020-08-28 10:26:17 +020044struct xsk_buff_pool *xp_create_and_assign_umem(struct xdp_sock *xs,
45 struct xdp_umem *umem)
Björn Töpel2b434702020-05-20 21:20:53 +020046{
Magnus Karlsson94033cd2021-09-22 09:56:06 +020047 bool unaligned = umem->flags & XDP_UMEM_UNALIGNED_CHUNK_FLAG;
Björn Töpel2b434702020-05-20 21:20:53 +020048 struct xsk_buff_pool *pool;
49 struct xdp_buff_xsk *xskb;
Magnus Karlsson94033cd2021-09-22 09:56:06 +020050 u32 i, entries;
Björn Töpel2b434702020-05-20 21:20:53 +020051
Magnus Karlsson94033cd2021-09-22 09:56:06 +020052 entries = unaligned ? umem->chunks : 0;
53 pool = kvzalloc(struct_size(pool, free_heads, entries), GFP_KERNEL);
Björn Töpel2b434702020-05-20 21:20:53 +020054 if (!pool)
55 goto out;
56
Magnus Karlsson1c1efc22020-08-28 10:26:17 +020057 pool->heads = kvcalloc(umem->chunks, sizeof(*pool->heads), GFP_KERNEL);
Björn Töpel2b434702020-05-20 21:20:53 +020058 if (!pool->heads)
59 goto out;
60
Magnus Karlsson1c1efc22020-08-28 10:26:17 +020061 pool->chunk_mask = ~((u64)umem->chunk_size - 1);
62 pool->addrs_cnt = umem->size;
63 pool->heads_cnt = umem->chunks;
64 pool->free_heads_cnt = umem->chunks;
65 pool->headroom = umem->headroom;
66 pool->chunk_size = umem->chunk_size;
Magnus Karlsson94033cd2021-09-22 09:56:06 +020067 pool->chunk_shift = ffs(umem->chunk_size) - 1;
68 pool->unaligned = unaligned;
Magnus Karlsson1c1efc22020-08-28 10:26:17 +020069 pool->frame_len = umem->chunk_size - umem->headroom -
70 XDP_PACKET_HEADROOM;
Magnus Karlsson1742b3d2020-08-28 10:26:15 +020071 pool->umem = umem;
Magnus Karlsson7f7ffa42020-08-28 10:26:21 +020072 pool->addrs = umem->addrs;
Björn Töpel2b434702020-05-20 21:20:53 +020073 INIT_LIST_HEAD(&pool->free_list);
Magnus Karlssona5aa8e52020-08-28 10:26:20 +020074 INIT_LIST_HEAD(&pool->xsk_tx_list);
75 spin_lock_init(&pool->xsk_tx_list_lock);
Magnus Karlssonf09ced42020-12-18 14:45:24 +010076 spin_lock_init(&pool->cq_lock);
Magnus Karlsson1c1efc22020-08-28 10:26:17 +020077 refcount_set(&pool->users, 1);
Björn Töpel2b434702020-05-20 21:20:53 +020078
Magnus Karlsson7361f9c2020-08-28 10:26:18 +020079 pool->fq = xs->fq_tmp;
80 pool->cq = xs->cq_tmp;
Magnus Karlsson7361f9c2020-08-28 10:26:18 +020081
Björn Töpel2b434702020-05-20 21:20:53 +020082 for (i = 0; i < pool->free_heads_cnt; i++) {
83 xskb = &pool->heads[i];
84 xskb->pool = pool;
Magnus Karlsson1c1efc22020-08-28 10:26:17 +020085 xskb->xdp.frame_sz = umem->chunk_size - umem->headroom;
Ciara Loftus5bec7ca2021-12-20 15:52:50 +000086 INIT_LIST_HEAD(&xskb->free_list_node);
Magnus Karlsson94033cd2021-09-22 09:56:06 +020087 if (pool->unaligned)
88 pool->free_heads[i] = xskb;
89 else
90 xp_init_xskb_addr(xskb, pool, i * pool->chunk_size);
Björn Töpel2b434702020-05-20 21:20:53 +020091 }
92
Magnus Karlsson7f7ffa42020-08-28 10:26:21 +020093 return pool;
Björn Töpel2b434702020-05-20 21:20:53 +020094
95out:
96 xp_destroy(pool);
97 return NULL;
98}
99
Björn Töpel2b434702020-05-20 21:20:53 +0200100void xp_set_rxq_info(struct xsk_buff_pool *pool, struct xdp_rxq_info *rxq)
101{
102 u32 i;
103
104 for (i = 0; i < pool->heads_cnt; i++)
105 pool->heads[i].xdp.rxq = rxq;
106}
107EXPORT_SYMBOL(xp_set_rxq_info);
108
Magnus Karlsson921b6862020-08-28 10:26:22 +0200109static void xp_disable_drv_zc(struct xsk_buff_pool *pool)
110{
111 struct netdev_bpf bpf;
112 int err;
113
114 ASSERT_RTNL();
115
116 if (pool->umem->zc) {
117 bpf.command = XDP_SETUP_XSK_POOL;
118 bpf.xsk.pool = NULL;
119 bpf.xsk.queue_id = pool->queue_id;
120
121 err = pool->netdev->netdev_ops->ndo_bpf(pool->netdev, &bpf);
122
123 if (err)
124 WARN(1, "Failed to disable zero-copy!\n");
125 }
126}
127
Björn Töpelf0863ea2021-01-22 11:53:50 +0100128int xp_assign_dev(struct xsk_buff_pool *pool,
129 struct net_device *netdev, u16 queue_id, u16 flags)
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200130{
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200131 bool force_zc, force_copy;
132 struct netdev_bpf bpf;
133 int err = 0;
134
135 ASSERT_RTNL();
136
137 force_zc = flags & XDP_ZEROCOPY;
138 force_copy = flags & XDP_COPY;
139
140 if (force_zc && force_copy)
141 return -EINVAL;
142
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +0200143 if (xsk_get_pool_from_qid(netdev, queue_id))
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200144 return -EBUSY;
145
Magnus Karlsson921b6862020-08-28 10:26:22 +0200146 pool->netdev = netdev;
147 pool->queue_id = queue_id;
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +0200148 err = xsk_reg_pool_at_qid(netdev, pool, queue_id);
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200149 if (err)
150 return err;
151
Björn Töpele3920812020-11-30 19:51:59 +0100152 if (flags & XDP_USE_NEED_WAKEUP)
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +0200153 pool->uses_need_wakeup = true;
Björn Töpele3920812020-11-30 19:51:59 +0100154 /* Tx needs to be explicitly woken up the first time. Also
155 * for supporting drivers that do not implement this
156 * feature. They will always have to call sendto() or poll().
157 */
158 pool->cached_need_wakeup = XDP_WAKEUP_TX;
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200159
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +0200160 dev_hold(netdev);
161
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200162 if (force_copy)
163 /* For copy-mode, we are done. */
164 return 0;
165
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +0200166 if (!netdev->netdev_ops->ndo_bpf ||
167 !netdev->netdev_ops->ndo_xsk_wakeup) {
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200168 err = -EOPNOTSUPP;
169 goto err_unreg_pool;
170 }
171
172 bpf.command = XDP_SETUP_XSK_POOL;
173 bpf.xsk.pool = pool;
174 bpf.xsk.queue_id = queue_id;
175
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +0200176 err = netdev->netdev_ops->ndo_bpf(netdev, &bpf);
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200177 if (err)
178 goto err_unreg_pool;
179
Magnus Karlsson921b6862020-08-28 10:26:22 +0200180 if (!pool->dma_pages) {
181 WARN(1, "Driver did not DMA map zero-copy buffers");
Zhang Changzhong12c8a8c2020-12-04 18:21:16 +0800182 err = -EINVAL;
Magnus Karlsson921b6862020-08-28 10:26:22 +0200183 goto err_unreg_xsk;
184 }
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +0200185 pool->umem->zc = true;
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200186 return 0;
187
Magnus Karlsson921b6862020-08-28 10:26:22 +0200188err_unreg_xsk:
189 xp_disable_drv_zc(pool);
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200190err_unreg_pool:
191 if (!force_zc)
192 err = 0; /* fallback to copy mode */
Marek Majtyka178648912020-11-20 16:14:43 +0100193 if (err) {
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +0200194 xsk_clear_pool_at_qid(netdev, queue_id);
Marek Majtyka178648912020-11-20 16:14:43 +0100195 dev_put(netdev);
196 }
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200197 return err;
198}
199
Magnus Karlssonb5aea282020-08-28 10:26:25 +0200200int xp_assign_dev_shared(struct xsk_buff_pool *pool, struct xdp_umem *umem,
201 struct net_device *dev, u16 queue_id)
202{
203 u16 flags;
204
205 /* One fill and completion ring required for each queue id. */
206 if (!pool->fq || !pool->cq)
207 return -EINVAL;
208
209 flags = umem->zc ? XDP_ZEROCOPY : XDP_COPY;
210 if (pool->uses_need_wakeup)
211 flags |= XDP_USE_NEED_WAKEUP;
212
Björn Töpelf0863ea2021-01-22 11:53:50 +0100213 return xp_assign_dev(pool, dev, queue_id, flags);
Magnus Karlssonb5aea282020-08-28 10:26:25 +0200214}
215
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200216void xp_clear_dev(struct xsk_buff_pool *pool)
217{
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +0200218 if (!pool->netdev)
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200219 return;
220
Magnus Karlsson921b6862020-08-28 10:26:22 +0200221 xp_disable_drv_zc(pool);
Magnus Karlssonc2d3d6a2020-08-28 10:26:19 +0200222 xsk_clear_pool_at_qid(pool->netdev, pool->queue_id);
223 dev_put(pool->netdev);
224 pool->netdev = NULL;
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200225}
226
227static void xp_release_deferred(struct work_struct *work)
228{
229 struct xsk_buff_pool *pool = container_of(work, struct xsk_buff_pool,
230 work);
231
232 rtnl_lock();
233 xp_clear_dev(pool);
234 rtnl_unlock();
235
Magnus Karlsson7361f9c2020-08-28 10:26:18 +0200236 if (pool->fq) {
237 xskq_destroy(pool->fq);
238 pool->fq = NULL;
239 }
240
241 if (pool->cq) {
242 xskq_destroy(pool->cq);
243 pool->cq = NULL;
244 }
245
Magnus Karlsson537cf4e2020-11-20 12:53:39 +0100246 xdp_put_umem(pool->umem, false);
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200247 xp_destroy(pool);
248}
249
250void xp_get_pool(struct xsk_buff_pool *pool)
251{
252 refcount_inc(&pool->users);
253}
254
Magnus Karlssone5e1a4b2020-10-27 13:32:01 +0100255bool xp_put_pool(struct xsk_buff_pool *pool)
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200256{
257 if (!pool)
Magnus Karlssone5e1a4b2020-10-27 13:32:01 +0100258 return false;
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200259
260 if (refcount_dec_and_test(&pool->users)) {
261 INIT_WORK(&pool->work, xp_release_deferred);
262 schedule_work(&pool->work);
Magnus Karlssone5e1a4b2020-10-27 13:32:01 +0100263 return true;
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200264 }
Magnus Karlssone5e1a4b2020-10-27 13:32:01 +0100265
266 return false;
Magnus Karlsson1c1efc22020-08-28 10:26:17 +0200267}
268
Magnus Karlsson921b6862020-08-28 10:26:22 +0200269static struct xsk_dma_map *xp_find_dma_map(struct xsk_buff_pool *pool)
270{
271 struct xsk_dma_map *dma_map;
272
273 list_for_each_entry(dma_map, &pool->umem->xsk_dma_list, list) {
274 if (dma_map->netdev == pool->netdev)
275 return dma_map;
276 }
277
278 return NULL;
279}
280
281static struct xsk_dma_map *xp_create_dma_map(struct device *dev, struct net_device *netdev,
282 u32 nr_pages, struct xdp_umem *umem)
283{
284 struct xsk_dma_map *dma_map;
285
286 dma_map = kzalloc(sizeof(*dma_map), GFP_KERNEL);
287 if (!dma_map)
288 return NULL;
289
290 dma_map->dma_pages = kvcalloc(nr_pages, sizeof(*dma_map->dma_pages), GFP_KERNEL);
Gustavo A. R. Silva1d6fd782020-09-02 10:07:50 -0500291 if (!dma_map->dma_pages) {
Magnus Karlsson921b6862020-08-28 10:26:22 +0200292 kfree(dma_map);
293 return NULL;
294 }
295
296 dma_map->netdev = netdev;
297 dma_map->dev = dev;
298 dma_map->dma_need_sync = false;
299 dma_map->dma_pages_cnt = nr_pages;
Magnus Karlssonbf74a372020-09-14 16:50:36 +0200300 refcount_set(&dma_map->users, 1);
Magnus Karlsson921b6862020-08-28 10:26:22 +0200301 list_add(&dma_map->list, &umem->xsk_dma_list);
302 return dma_map;
303}
304
305static void xp_destroy_dma_map(struct xsk_dma_map *dma_map)
306{
307 list_del(&dma_map->list);
308 kvfree(dma_map->dma_pages);
309 kfree(dma_map);
310}
311
312static void __xp_dma_unmap(struct xsk_dma_map *dma_map, unsigned long attrs)
Björn Töpel2b434702020-05-20 21:20:53 +0200313{
314 dma_addr_t *dma;
315 u32 i;
316
Magnus Karlsson921b6862020-08-28 10:26:22 +0200317 for (i = 0; i < dma_map->dma_pages_cnt; i++) {
318 dma = &dma_map->dma_pages[i];
Björn Töpel2b434702020-05-20 21:20:53 +0200319 if (*dma) {
Magnus Karlsson921b6862020-08-28 10:26:22 +0200320 dma_unmap_page_attrs(dma_map->dev, *dma, PAGE_SIZE,
Björn Töpel2b434702020-05-20 21:20:53 +0200321 DMA_BIDIRECTIONAL, attrs);
322 *dma = 0;
323 }
324 }
325
Magnus Karlsson921b6862020-08-28 10:26:22 +0200326 xp_destroy_dma_map(dma_map);
327}
328
329void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs)
330{
331 struct xsk_dma_map *dma_map;
332
333 if (pool->dma_pages_cnt == 0)
334 return;
335
336 dma_map = xp_find_dma_map(pool);
337 if (!dma_map) {
338 WARN(1, "Could not find dma_map for device");
339 return;
340 }
341
342 if (!refcount_dec_and_test(&dma_map->users))
343 return;
344
345 __xp_dma_unmap(dma_map, attrs);
Björn Töpel2b434702020-05-20 21:20:53 +0200346 kvfree(pool->dma_pages);
347 pool->dma_pages_cnt = 0;
348 pool->dev = NULL;
349}
350EXPORT_SYMBOL(xp_dma_unmap);
351
Magnus Karlsson921b6862020-08-28 10:26:22 +0200352static void xp_check_dma_contiguity(struct xsk_dma_map *dma_map)
Björn Töpel2b434702020-05-20 21:20:53 +0200353{
354 u32 i;
355
Magnus Karlsson921b6862020-08-28 10:26:22 +0200356 for (i = 0; i < dma_map->dma_pages_cnt - 1; i++) {
357 if (dma_map->dma_pages[i] + PAGE_SIZE == dma_map->dma_pages[i + 1])
358 dma_map->dma_pages[i] |= XSK_NEXT_PG_CONTIG_MASK;
Björn Töpel2b434702020-05-20 21:20:53 +0200359 else
Magnus Karlsson921b6862020-08-28 10:26:22 +0200360 dma_map->dma_pages[i] &= ~XSK_NEXT_PG_CONTIG_MASK;
Björn Töpel2b434702020-05-20 21:20:53 +0200361 }
362}
363
Magnus Karlsson921b6862020-08-28 10:26:22 +0200364static int xp_init_dma_info(struct xsk_buff_pool *pool, struct xsk_dma_map *dma_map)
365{
366 pool->dma_pages = kvcalloc(dma_map->dma_pages_cnt, sizeof(*pool->dma_pages), GFP_KERNEL);
367 if (!pool->dma_pages)
368 return -ENOMEM;
369
370 pool->dev = dma_map->dev;
371 pool->dma_pages_cnt = dma_map->dma_pages_cnt;
372 pool->dma_need_sync = dma_map->dma_need_sync;
Magnus Karlsson921b6862020-08-28 10:26:22 +0200373 memcpy(pool->dma_pages, dma_map->dma_pages,
374 pool->dma_pages_cnt * sizeof(*pool->dma_pages));
375
376 return 0;
377}
378
Björn Töpel2b434702020-05-20 21:20:53 +0200379int xp_dma_map(struct xsk_buff_pool *pool, struct device *dev,
380 unsigned long attrs, struct page **pages, u32 nr_pages)
381{
Magnus Karlsson921b6862020-08-28 10:26:22 +0200382 struct xsk_dma_map *dma_map;
Björn Töpel2b434702020-05-20 21:20:53 +0200383 dma_addr_t dma;
Magnus Karlsson921b6862020-08-28 10:26:22 +0200384 int err;
Björn Töpel2b434702020-05-20 21:20:53 +0200385 u32 i;
386
Magnus Karlsson921b6862020-08-28 10:26:22 +0200387 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
Magnus Karlssonbf74a372020-09-14 16:50:36 +0200393 refcount_inc(&dma_map->users);
Magnus Karlsson921b6862020-08-28 10:26:22 +0200394 return 0;
395 }
396
397 dma_map = xp_create_dma_map(dev, pool->netdev, nr_pages, pool->umem);
398 if (!dma_map)
Björn Töpel2b434702020-05-20 21:20:53 +0200399 return -ENOMEM;
400
Magnus Karlsson921b6862020-08-28 10:26:22 +0200401 for (i = 0; i < dma_map->dma_pages_cnt; i++) {
Björn Töpel2b434702020-05-20 21:20:53 +0200402 dma = dma_map_page_attrs(dev, pages[i], 0, PAGE_SIZE,
403 DMA_BIDIRECTIONAL, attrs);
404 if (dma_mapping_error(dev, dma)) {
Magnus Karlsson921b6862020-08-28 10:26:22 +0200405 __xp_dma_unmap(dma_map, attrs);
Björn Töpel2b434702020-05-20 21:20:53 +0200406 return -ENOMEM;
407 }
Christoph Hellwig7e024572020-06-29 15:03:59 +0200408 if (dma_need_sync(dev, dma))
Magnus Karlsson921b6862020-08-28 10:26:22 +0200409 dma_map->dma_need_sync = true;
410 dma_map->dma_pages[i] = dma;
Björn Töpel2b434702020-05-20 21:20:53 +0200411 }
412
413 if (pool->unaligned)
Magnus Karlsson921b6862020-08-28 10:26:22 +0200414 xp_check_dma_contiguity(dma_map);
Magnus Karlsson94033cd2021-09-22 09:56:06 +0200415 else
416 for (i = 0; i < pool->heads_cnt; i++) {
417 struct xdp_buff_xsk *xskb = &pool->heads[i];
418
419 xp_init_xskb_dma(xskb, pool, dma_map->dma_pages, xskb->orig_addr);
420 }
Magnus Karlsson921b6862020-08-28 10:26:22 +0200421
422 err = xp_init_dma_info(pool, dma_map);
423 if (err) {
424 __xp_dma_unmap(dma_map, attrs);
425 return err;
426 }
427
Björn Töpel2b434702020-05-20 21:20:53 +0200428 return 0;
429}
430EXPORT_SYMBOL(xp_dma_map);
431
Björn Töpel2b434702020-05-20 21:20:53 +0200432static bool xp_addr_crosses_non_contig_pg(struct xsk_buff_pool *pool,
433 u64 addr)
434{
435 return xp_desc_crosses_non_contig_pg(pool, addr, pool->chunk_size);
436}
437
Björn Töpel2b434702020-05-20 21:20:53 +0200438static bool xp_check_unaligned(struct xsk_buff_pool *pool, u64 *addr)
439{
440 *addr = xp_unaligned_extract_addr(*addr);
441 if (*addr >= pool->addrs_cnt ||
442 *addr + pool->chunk_size > pool->addrs_cnt ||
443 xp_addr_crosses_non_contig_pg(pool, *addr))
444 return false;
445 return true;
446}
447
448static bool xp_check_aligned(struct xsk_buff_pool *pool, u64 *addr)
449{
450 *addr = xp_aligned_extract_addr(pool, *addr);
451 return *addr < pool->addrs_cnt;
452}
453
454static struct xdp_buff_xsk *__xp_alloc(struct xsk_buff_pool *pool)
455{
456 struct xdp_buff_xsk *xskb;
457 u64 addr;
458 bool ok;
459
460 if (pool->free_heads_cnt == 0)
461 return NULL;
462
Björn Töpel2b434702020-05-20 21:20:53 +0200463 for (;;) {
464 if (!xskq_cons_peek_addr_unchecked(pool->fq, &addr)) {
Ciara Loftus8aa5a332020-07-08 07:28:33 +0000465 pool->fq->queue_empty_descs++;
Björn Töpel2b434702020-05-20 21:20:53 +0200466 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 }
Björn Töpel2b434702020-05-20 21:20:53 +0200478
Magnus Karlsson94033cd2021-09-22 09:56:06 +0200479 if (pool->unaligned) {
480 xskb = pool->free_heads[--pool->free_heads_cnt];
481 xp_init_xskb_addr(xskb, pool, addr);
482 if (pool->dma_pages_cnt)
483 xp_init_xskb_dma(xskb, pool, pool->dma_pages, addr);
484 } else {
485 xskb = &pool->heads[xp_aligned_extract_idx(pool, addr)];
Björn Töpel2b434702020-05-20 21:20:53 +0200486 }
Magnus Karlsson94033cd2021-09-22 09:56:06 +0200487
488 xskq_cons_release(pool->fq);
Björn Töpel2b434702020-05-20 21:20:53 +0200489 return xskb;
490}
491
492struct 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);
Magnus Karlsson199d9832021-11-11 08:57:07 +0100504 list_del_init(&xskb->free_list_node);
Björn Töpel2b434702020-05-20 21:20:53 +0200505 }
506
507 xskb->xdp.data = xskb->xdp.data_hard_start + XDP_PACKET_HEADROOM;
508 xskb->xdp.data_meta = xskb->xdp.data;
509
Christoph Hellwig91d5b702020-06-29 15:03:57 +0200510 if (pool->dma_need_sync) {
Björn Töpel2b434702020-05-20 21:20:53 +0200511 dma_sync_single_range_for_device(pool->dev, xskb->dma, 0,
512 pool->frame_len,
513 DMA_BIDIRECTIONAL);
514 }
515 return &xskb->xdp;
516}
517EXPORT_SYMBOL(xp_alloc);
518
Magnus Karlsson47e40752021-09-22 09:56:02 +0200519static u32 xp_alloc_new_from_fq(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
520{
521 u32 i, cached_cons, nb_entries;
522
523 if (max > pool->free_heads_cnt)
524 max = pool->free_heads_cnt;
525 max = xskq_cons_nb_entries(pool->fq, max);
526
527 cached_cons = pool->fq->cached_cons;
528 nb_entries = max;
529 i = max;
530 while (i--) {
531 struct xdp_buff_xsk *xskb;
532 u64 addr;
533 bool ok;
534
535 __xskq_cons_read_addr_unchecked(pool->fq, cached_cons++, &addr);
536
537 ok = pool->unaligned ? xp_check_unaligned(pool, &addr) :
538 xp_check_aligned(pool, &addr);
539 if (unlikely(!ok)) {
540 pool->fq->invalid_descs++;
541 nb_entries--;
542 continue;
543 }
544
Magnus Karlsson94033cd2021-09-22 09:56:06 +0200545 if (pool->unaligned) {
546 xskb = pool->free_heads[--pool->free_heads_cnt];
547 xp_init_xskb_addr(xskb, pool, addr);
548 if (pool->dma_pages_cnt)
549 xp_init_xskb_dma(xskb, pool, pool->dma_pages, addr);
550 } else {
551 xskb = &pool->heads[xp_aligned_extract_idx(pool, addr)];
552 }
553
Magnus Karlsson47e40752021-09-22 09:56:02 +0200554 *xdp = &xskb->xdp;
Magnus Karlsson47e40752021-09-22 09:56:02 +0200555 xdp++;
556 }
557
558 xskq_cons_release_n(pool->fq, max);
559 return nb_entries;
560}
561
562static u32 xp_alloc_reused(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 nb_entries)
563{
564 struct xdp_buff_xsk *xskb;
565 u32 i;
566
567 nb_entries = min_t(u32, nb_entries, pool->free_list_cnt);
568
569 i = nb_entries;
570 while (i--) {
571 xskb = list_first_entry(&pool->free_list, struct xdp_buff_xsk, free_list_node);
Magnus Karlsson199d9832021-11-11 08:57:07 +0100572 list_del_init(&xskb->free_list_node);
Magnus Karlsson47e40752021-09-22 09:56:02 +0200573
574 *xdp = &xskb->xdp;
575 xdp++;
576 }
577 pool->free_list_cnt -= nb_entries;
578
579 return nb_entries;
580}
581
582u32 xp_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max)
583{
584 u32 nb_entries1 = 0, nb_entries2;
585
586 if (unlikely(pool->dma_need_sync)) {
587 /* Slow path */
588 *xdp = xp_alloc(pool);
589 return !!*xdp;
590 }
591
592 if (unlikely(pool->free_list_cnt)) {
593 nb_entries1 = xp_alloc_reused(pool, xdp, max);
594 if (nb_entries1 == max)
595 return nb_entries1;
596
597 max -= nb_entries1;
598 xdp += nb_entries1;
599 }
600
601 nb_entries2 = xp_alloc_new_from_fq(pool, xdp, max);
602 if (!nb_entries2)
603 pool->fq->queue_empty_descs++;
604
605 return nb_entries1 + nb_entries2;
606}
607EXPORT_SYMBOL(xp_alloc_batch);
608
Björn Töpel2b434702020-05-20 21:20:53 +0200609bool xp_can_alloc(struct xsk_buff_pool *pool, u32 count)
610{
611 if (pool->free_list_cnt >= count)
612 return true;
613 return xskq_cons_has_entries(pool->fq, count - pool->free_list_cnt);
614}
615EXPORT_SYMBOL(xp_can_alloc);
616
617void xp_free(struct xdp_buff_xsk *xskb)
618{
Magnus Karlsson199d9832021-11-11 08:57:07 +0100619 if (!list_empty(&xskb->free_list_node))
620 return;
621
Björn Töpel2b434702020-05-20 21:20:53 +0200622 xskb->pool->free_list_cnt++;
623 list_add(&xskb->free_list_node, &xskb->pool->free_list);
624}
625EXPORT_SYMBOL(xp_free);
626
Björn Töpel2b434702020-05-20 21:20:53 +0200627void *xp_raw_get_data(struct xsk_buff_pool *pool, u64 addr)
628{
629 addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr;
630 return pool->addrs + addr;
631}
632EXPORT_SYMBOL(xp_raw_get_data);
633
634dma_addr_t xp_raw_get_dma(struct xsk_buff_pool *pool, u64 addr)
635{
636 addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr;
637 return (pool->dma_pages[addr >> PAGE_SHIFT] &
638 ~XSK_NEXT_PG_CONTIG_MASK) +
639 (addr & ~PAGE_MASK);
640}
641EXPORT_SYMBOL(xp_raw_get_dma);
642
Björn Töpel26062b12020-05-20 21:21:02 +0200643void xp_dma_sync_for_cpu_slow(struct xdp_buff_xsk *xskb)
Björn Töpel2b434702020-05-20 21:20:53 +0200644{
Björn Töpel2b434702020-05-20 21:20:53 +0200645 dma_sync_single_range_for_cpu(xskb->pool->dev, xskb->dma, 0,
646 xskb->pool->frame_len, DMA_BIDIRECTIONAL);
647}
Björn Töpel26062b12020-05-20 21:21:02 +0200648EXPORT_SYMBOL(xp_dma_sync_for_cpu_slow);
Björn Töpel2b434702020-05-20 21:20:53 +0200649
Björn Töpel26062b12020-05-20 21:21:02 +0200650void xp_dma_sync_for_device_slow(struct xsk_buff_pool *pool, dma_addr_t dma,
651 size_t size)
Björn Töpel2b434702020-05-20 21:20:53 +0200652{
Björn Töpel2b434702020-05-20 21:20:53 +0200653 dma_sync_single_range_for_device(pool->dev, dma, 0,
654 size, DMA_BIDIRECTIONAL);
655}
Björn Töpel26062b12020-05-20 21:21:02 +0200656EXPORT_SYMBOL(xp_dma_sync_for_device_slow);