Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | |
| 3 | #include <net/xsk_buff_pool.h> |
| 4 | #include <net/xdp_sock.h> |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 5 | #include <net/xdp_sock_drv.h> |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 6 | |
| 7 | #include "xsk_queue.h" |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 8 | #include "xdp_umem.h" |
| 9 | #include "xsk.h" |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 10 | |
Magnus Karlsson | a5aa8e5 | 2020-08-28 10:26:20 +0200 | [diff] [blame] | 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 | |
| 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 | |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 35 | void xp_destroy(struct xsk_buff_pool *pool) |
| 36 | { |
| 37 | if (!pool) |
| 38 | return; |
| 39 | |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 40 | kvfree(pool->heads); |
| 41 | kvfree(pool); |
| 42 | } |
| 43 | |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 44 | struct xsk_buff_pool *xp_create_and_assign_umem(struct xdp_sock *xs, |
| 45 | struct xdp_umem *umem) |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 46 | { |
Magnus Karlsson | 94033cd | 2021-09-22 09:56:06 +0200 | [diff] [blame] | 47 | bool unaligned = umem->flags & XDP_UMEM_UNALIGNED_CHUNK_FLAG; |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 48 | struct xsk_buff_pool *pool; |
| 49 | struct xdp_buff_xsk *xskb; |
Magnus Karlsson | 94033cd | 2021-09-22 09:56:06 +0200 | [diff] [blame] | 50 | u32 i, entries; |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 51 | |
Magnus Karlsson | 94033cd | 2021-09-22 09:56:06 +0200 | [diff] [blame] | 52 | entries = unaligned ? umem->chunks : 0; |
| 53 | pool = kvzalloc(struct_size(pool, free_heads, entries), GFP_KERNEL); |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 54 | if (!pool) |
| 55 | goto out; |
| 56 | |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 57 | pool->heads = kvcalloc(umem->chunks, sizeof(*pool->heads), GFP_KERNEL); |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 58 | if (!pool->heads) |
| 59 | goto out; |
| 60 | |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 61 | 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 Karlsson | 94033cd | 2021-09-22 09:56:06 +0200 | [diff] [blame] | 67 | pool->chunk_shift = ffs(umem->chunk_size) - 1; |
| 68 | pool->unaligned = unaligned; |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 69 | pool->frame_len = umem->chunk_size - umem->headroom - |
| 70 | XDP_PACKET_HEADROOM; |
Magnus Karlsson | 1742b3d | 2020-08-28 10:26:15 +0200 | [diff] [blame] | 71 | pool->umem = umem; |
Magnus Karlsson | 7f7ffa4 | 2020-08-28 10:26:21 +0200 | [diff] [blame] | 72 | pool->addrs = umem->addrs; |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 73 | INIT_LIST_HEAD(&pool->free_list); |
Magnus Karlsson | a5aa8e5 | 2020-08-28 10:26:20 +0200 | [diff] [blame] | 74 | INIT_LIST_HEAD(&pool->xsk_tx_list); |
| 75 | spin_lock_init(&pool->xsk_tx_list_lock); |
Magnus Karlsson | f09ced4 | 2020-12-18 14:45:24 +0100 | [diff] [blame] | 76 | spin_lock_init(&pool->cq_lock); |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 77 | refcount_set(&pool->users, 1); |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 78 | |
Magnus Karlsson | 7361f9c | 2020-08-28 10:26:18 +0200 | [diff] [blame] | 79 | pool->fq = xs->fq_tmp; |
| 80 | pool->cq = xs->cq_tmp; |
Magnus Karlsson | 7361f9c | 2020-08-28 10:26:18 +0200 | [diff] [blame] | 81 | |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 82 | for (i = 0; i < pool->free_heads_cnt; i++) { |
| 83 | xskb = &pool->heads[i]; |
| 84 | xskb->pool = pool; |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 85 | xskb->xdp.frame_sz = umem->chunk_size - umem->headroom; |
Ciara Loftus | 5bec7ca | 2021-12-20 15:52:50 +0000 | [diff] [blame] | 86 | INIT_LIST_HEAD(&xskb->free_list_node); |
Magnus Karlsson | 94033cd | 2021-09-22 09:56:06 +0200 | [diff] [blame] | 87 | 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öpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 91 | } |
| 92 | |
Magnus Karlsson | 7f7ffa4 | 2020-08-28 10:26:21 +0200 | [diff] [blame] | 93 | return pool; |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 94 | |
| 95 | out: |
| 96 | xp_destroy(pool); |
| 97 | return NULL; |
| 98 | } |
| 99 | |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 100 | void 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 | } |
| 107 | EXPORT_SYMBOL(xp_set_rxq_info); |
| 108 | |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 109 | static 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öpel | f0863ea | 2021-01-22 11:53:50 +0100 | [diff] [blame] | 128 | int xp_assign_dev(struct xsk_buff_pool *pool, |
| 129 | struct net_device *netdev, u16 queue_id, u16 flags) |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 130 | { |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 131 | 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 Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 143 | if (xsk_get_pool_from_qid(netdev, queue_id)) |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 144 | return -EBUSY; |
| 145 | |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 146 | pool->netdev = netdev; |
| 147 | pool->queue_id = queue_id; |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 148 | err = xsk_reg_pool_at_qid(netdev, pool, queue_id); |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 149 | if (err) |
| 150 | return err; |
| 151 | |
Björn Töpel | e392081 | 2020-11-30 19:51:59 +0100 | [diff] [blame] | 152 | if (flags & XDP_USE_NEED_WAKEUP) |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 153 | pool->uses_need_wakeup = true; |
Björn Töpel | e392081 | 2020-11-30 19:51:59 +0100 | [diff] [blame] | 154 | /* 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 Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 159 | |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 160 | dev_hold(netdev); |
| 161 | |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 162 | if (force_copy) |
| 163 | /* For copy-mode, we are done. */ |
| 164 | return 0; |
| 165 | |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 166 | if (!netdev->netdev_ops->ndo_bpf || |
| 167 | !netdev->netdev_ops->ndo_xsk_wakeup) { |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 168 | 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 Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 176 | err = netdev->netdev_ops->ndo_bpf(netdev, &bpf); |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 177 | if (err) |
| 178 | goto err_unreg_pool; |
| 179 | |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 180 | if (!pool->dma_pages) { |
| 181 | WARN(1, "Driver did not DMA map zero-copy buffers"); |
Zhang Changzhong | 12c8a8c | 2020-12-04 18:21:16 +0800 | [diff] [blame] | 182 | err = -EINVAL; |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 183 | goto err_unreg_xsk; |
| 184 | } |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 185 | pool->umem->zc = true; |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 186 | return 0; |
| 187 | |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 188 | err_unreg_xsk: |
| 189 | xp_disable_drv_zc(pool); |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 190 | err_unreg_pool: |
| 191 | if (!force_zc) |
| 192 | err = 0; /* fallback to copy mode */ |
Marek Majtyka | 17864891 | 2020-11-20 16:14:43 +0100 | [diff] [blame] | 193 | if (err) { |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 194 | xsk_clear_pool_at_qid(netdev, queue_id); |
Marek Majtyka | 17864891 | 2020-11-20 16:14:43 +0100 | [diff] [blame] | 195 | dev_put(netdev); |
| 196 | } |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 197 | return err; |
| 198 | } |
| 199 | |
Magnus Karlsson | b5aea28 | 2020-08-28 10:26:25 +0200 | [diff] [blame] | 200 | int 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öpel | f0863ea | 2021-01-22 11:53:50 +0100 | [diff] [blame] | 213 | return xp_assign_dev(pool, dev, queue_id, flags); |
Magnus Karlsson | b5aea28 | 2020-08-28 10:26:25 +0200 | [diff] [blame] | 214 | } |
| 215 | |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 216 | void xp_clear_dev(struct xsk_buff_pool *pool) |
| 217 | { |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 218 | if (!pool->netdev) |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 219 | return; |
| 220 | |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 221 | xp_disable_drv_zc(pool); |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 222 | xsk_clear_pool_at_qid(pool->netdev, pool->queue_id); |
| 223 | dev_put(pool->netdev); |
| 224 | pool->netdev = NULL; |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | static 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 Karlsson | 7361f9c | 2020-08-28 10:26:18 +0200 | [diff] [blame] | 236 | 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 Karlsson | 537cf4e | 2020-11-20 12:53:39 +0100 | [diff] [blame] | 246 | xdp_put_umem(pool->umem, false); |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 247 | xp_destroy(pool); |
| 248 | } |
| 249 | |
| 250 | void xp_get_pool(struct xsk_buff_pool *pool) |
| 251 | { |
| 252 | refcount_inc(&pool->users); |
| 253 | } |
| 254 | |
Magnus Karlsson | e5e1a4b | 2020-10-27 13:32:01 +0100 | [diff] [blame] | 255 | bool xp_put_pool(struct xsk_buff_pool *pool) |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 256 | { |
| 257 | if (!pool) |
Magnus Karlsson | e5e1a4b | 2020-10-27 13:32:01 +0100 | [diff] [blame] | 258 | return false; |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 259 | |
| 260 | if (refcount_dec_and_test(&pool->users)) { |
| 261 | INIT_WORK(&pool->work, xp_release_deferred); |
| 262 | schedule_work(&pool->work); |
Magnus Karlsson | e5e1a4b | 2020-10-27 13:32:01 +0100 | [diff] [blame] | 263 | return true; |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 264 | } |
Magnus Karlsson | e5e1a4b | 2020-10-27 13:32:01 +0100 | [diff] [blame] | 265 | |
| 266 | return false; |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 267 | } |
| 268 | |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 269 | static 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 | |
| 281 | static 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. Silva | 1d6fd78 | 2020-09-02 10:07:50 -0500 | [diff] [blame] | 291 | if (!dma_map->dma_pages) { |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 292 | 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 Karlsson | bf74a37 | 2020-09-14 16:50:36 +0200 | [diff] [blame] | 300 | refcount_set(&dma_map->users, 1); |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 301 | list_add(&dma_map->list, &umem->xsk_dma_list); |
| 302 | return dma_map; |
| 303 | } |
| 304 | |
| 305 | static 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 | |
| 312 | static void __xp_dma_unmap(struct xsk_dma_map *dma_map, unsigned long attrs) |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 313 | { |
| 314 | dma_addr_t *dma; |
| 315 | u32 i; |
| 316 | |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 317 | for (i = 0; i < dma_map->dma_pages_cnt; i++) { |
| 318 | dma = &dma_map->dma_pages[i]; |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 319 | if (*dma) { |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 320 | dma_unmap_page_attrs(dma_map->dev, *dma, PAGE_SIZE, |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 321 | DMA_BIDIRECTIONAL, attrs); |
| 322 | *dma = 0; |
| 323 | } |
| 324 | } |
| 325 | |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 326 | xp_destroy_dma_map(dma_map); |
| 327 | } |
| 328 | |
| 329 | void 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öpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 346 | kvfree(pool->dma_pages); |
| 347 | pool->dma_pages_cnt = 0; |
| 348 | pool->dev = NULL; |
| 349 | } |
| 350 | EXPORT_SYMBOL(xp_dma_unmap); |
| 351 | |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 352 | static void xp_check_dma_contiguity(struct xsk_dma_map *dma_map) |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 353 | { |
| 354 | u32 i; |
| 355 | |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 356 | 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öpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 359 | else |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 360 | dma_map->dma_pages[i] &= ~XSK_NEXT_PG_CONTIG_MASK; |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 361 | } |
| 362 | } |
| 363 | |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 364 | static 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 Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 373 | 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öpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 379 | int xp_dma_map(struct xsk_buff_pool *pool, struct device *dev, |
| 380 | unsigned long attrs, struct page **pages, u32 nr_pages) |
| 381 | { |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 382 | struct xsk_dma_map *dma_map; |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 383 | dma_addr_t dma; |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 384 | int err; |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 385 | u32 i; |
| 386 | |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 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 | |
Magnus Karlsson | bf74a37 | 2020-09-14 16:50:36 +0200 | [diff] [blame] | 393 | refcount_inc(&dma_map->users); |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 394 | 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öpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 399 | return -ENOMEM; |
| 400 | |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 401 | for (i = 0; i < dma_map->dma_pages_cnt; i++) { |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 402 | dma = dma_map_page_attrs(dev, pages[i], 0, PAGE_SIZE, |
| 403 | DMA_BIDIRECTIONAL, attrs); |
| 404 | if (dma_mapping_error(dev, dma)) { |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 405 | __xp_dma_unmap(dma_map, attrs); |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 406 | return -ENOMEM; |
| 407 | } |
Christoph Hellwig | 7e02457 | 2020-06-29 15:03:59 +0200 | [diff] [blame] | 408 | if (dma_need_sync(dev, dma)) |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 409 | dma_map->dma_need_sync = true; |
| 410 | dma_map->dma_pages[i] = dma; |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 411 | } |
| 412 | |
| 413 | if (pool->unaligned) |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 414 | xp_check_dma_contiguity(dma_map); |
Magnus Karlsson | 94033cd | 2021-09-22 09:56:06 +0200 | [diff] [blame] | 415 | 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 Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 421 | |
| 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öpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 428 | return 0; |
| 429 | } |
| 430 | EXPORT_SYMBOL(xp_dma_map); |
| 431 | |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 432 | static 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öpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 438 | static 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 | |
| 448 | static 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 | |
| 454 | static 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öpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 463 | for (;;) { |
| 464 | if (!xskq_cons_peek_addr_unchecked(pool->fq, &addr)) { |
Ciara Loftus | 8aa5a33 | 2020-07-08 07:28:33 +0000 | [diff] [blame] | 465 | pool->fq->queue_empty_descs++; |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 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 | } |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 478 | |
Magnus Karlsson | 94033cd | 2021-09-22 09:56:06 +0200 | [diff] [blame] | 479 | 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öpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 486 | } |
Magnus Karlsson | 94033cd | 2021-09-22 09:56:06 +0200 | [diff] [blame] | 487 | |
| 488 | xskq_cons_release(pool->fq); |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 489 | return xskb; |
| 490 | } |
| 491 | |
| 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); |
Magnus Karlsson | 199d983 | 2021-11-11 08:57:07 +0100 | [diff] [blame] | 504 | list_del_init(&xskb->free_list_node); |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | xskb->xdp.data = xskb->xdp.data_hard_start + XDP_PACKET_HEADROOM; |
| 508 | xskb->xdp.data_meta = xskb->xdp.data; |
| 509 | |
Christoph Hellwig | 91d5b70 | 2020-06-29 15:03:57 +0200 | [diff] [blame] | 510 | if (pool->dma_need_sync) { |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 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 | |
Magnus Karlsson | 47e4075 | 2021-09-22 09:56:02 +0200 | [diff] [blame] | 519 | static 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 Karlsson | 94033cd | 2021-09-22 09:56:06 +0200 | [diff] [blame] | 545 | 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 Karlsson | 47e4075 | 2021-09-22 09:56:02 +0200 | [diff] [blame] | 554 | *xdp = &xskb->xdp; |
Magnus Karlsson | 47e4075 | 2021-09-22 09:56:02 +0200 | [diff] [blame] | 555 | xdp++; |
| 556 | } |
| 557 | |
| 558 | xskq_cons_release_n(pool->fq, max); |
| 559 | return nb_entries; |
| 560 | } |
| 561 | |
| 562 | static 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 Karlsson | 199d983 | 2021-11-11 08:57:07 +0100 | [diff] [blame] | 572 | list_del_init(&xskb->free_list_node); |
Magnus Karlsson | 47e4075 | 2021-09-22 09:56:02 +0200 | [diff] [blame] | 573 | |
| 574 | *xdp = &xskb->xdp; |
| 575 | xdp++; |
| 576 | } |
| 577 | pool->free_list_cnt -= nb_entries; |
| 578 | |
| 579 | return nb_entries; |
| 580 | } |
| 581 | |
| 582 | u32 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 | } |
| 607 | EXPORT_SYMBOL(xp_alloc_batch); |
| 608 | |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 609 | bool 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 | } |
| 615 | EXPORT_SYMBOL(xp_can_alloc); |
| 616 | |
| 617 | void xp_free(struct xdp_buff_xsk *xskb) |
| 618 | { |
Magnus Karlsson | 199d983 | 2021-11-11 08:57:07 +0100 | [diff] [blame] | 619 | if (!list_empty(&xskb->free_list_node)) |
| 620 | return; |
| 621 | |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 622 | xskb->pool->free_list_cnt++; |
| 623 | list_add(&xskb->free_list_node, &xskb->pool->free_list); |
| 624 | } |
| 625 | EXPORT_SYMBOL(xp_free); |
| 626 | |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 627 | void *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 | } |
| 632 | EXPORT_SYMBOL(xp_raw_get_data); |
| 633 | |
| 634 | dma_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 | } |
| 641 | EXPORT_SYMBOL(xp_raw_get_dma); |
| 642 | |
Björn Töpel | 26062b1 | 2020-05-20 21:21:02 +0200 | [diff] [blame] | 643 | void xp_dma_sync_for_cpu_slow(struct xdp_buff_xsk *xskb) |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 644 | { |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 645 | dma_sync_single_range_for_cpu(xskb->pool->dev, xskb->dma, 0, |
| 646 | xskb->pool->frame_len, DMA_BIDIRECTIONAL); |
| 647 | } |
Björn Töpel | 26062b1 | 2020-05-20 21:21:02 +0200 | [diff] [blame] | 648 | EXPORT_SYMBOL(xp_dma_sync_for_cpu_slow); |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 649 | |
Björn Töpel | 26062b1 | 2020-05-20 21:21:02 +0200 | [diff] [blame] | 650 | void xp_dma_sync_for_device_slow(struct xsk_buff_pool *pool, dma_addr_t dma, |
| 651 | size_t size) |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 652 | { |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 653 | dma_sync_single_range_for_device(pool->dev, dma, 0, |
| 654 | size, DMA_BIDIRECTIONAL); |
| 655 | } |
Björn Töpel | 26062b1 | 2020-05-20 21:21:02 +0200 | [diff] [blame] | 656 | EXPORT_SYMBOL(xp_dma_sync_for_device_slow); |