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 | { |
| 47 | struct xsk_buff_pool *pool; |
| 48 | struct xdp_buff_xsk *xskb; |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 49 | u32 i; |
| 50 | |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 51 | pool = kvzalloc(struct_size(pool, free_heads, umem->chunks), |
| 52 | GFP_KERNEL); |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 53 | if (!pool) |
| 54 | goto out; |
| 55 | |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 56 | pool->heads = kvcalloc(umem->chunks, sizeof(*pool->heads), GFP_KERNEL); |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 57 | if (!pool->heads) |
| 58 | goto out; |
| 59 | |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 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; |
Magnus Karlsson | 1742b3d | 2020-08-28 10:26:15 +0200 | [diff] [blame] | 69 | pool->umem = umem; |
Magnus Karlsson | 7f7ffa4 | 2020-08-28 10:26:21 +0200 | [diff] [blame] | 70 | pool->addrs = umem->addrs; |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 71 | INIT_LIST_HEAD(&pool->free_list); |
Magnus Karlsson | a5aa8e5 | 2020-08-28 10:26:20 +0200 | [diff] [blame] | 72 | INIT_LIST_HEAD(&pool->xsk_tx_list); |
| 73 | spin_lock_init(&pool->xsk_tx_list_lock); |
Magnus Karlsson | f09ced4 | 2020-12-18 14:45:24 +0100 | [diff] [blame] | 74 | spin_lock_init(&pool->cq_lock); |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 75 | refcount_set(&pool->users, 1); |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 76 | |
Magnus Karlsson | 7361f9c | 2020-08-28 10:26:18 +0200 | [diff] [blame] | 77 | pool->fq = xs->fq_tmp; |
| 78 | pool->cq = xs->cq_tmp; |
Magnus Karlsson | 7361f9c | 2020-08-28 10:26:18 +0200 | [diff] [blame] | 79 | |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 80 | for (i = 0; i < pool->free_heads_cnt; i++) { |
| 81 | xskb = &pool->heads[i]; |
| 82 | xskb->pool = pool; |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 83 | xskb->xdp.frame_sz = umem->chunk_size - umem->headroom; |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 84 | pool->free_heads[i] = xskb; |
| 85 | } |
| 86 | |
Magnus Karlsson | 7f7ffa4 | 2020-08-28 10:26:21 +0200 | [diff] [blame] | 87 | return pool; |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 88 | |
| 89 | out: |
| 90 | xp_destroy(pool); |
| 91 | return NULL; |
| 92 | } |
| 93 | |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 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 | |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 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 | |
Björn Töpel | f0863ea | 2021-01-22 11:53:50 +0100 | [diff] [blame] | 122 | int xp_assign_dev(struct xsk_buff_pool *pool, |
| 123 | struct net_device *netdev, u16 queue_id, u16 flags) |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 124 | { |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 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 | |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 137 | if (xsk_get_pool_from_qid(netdev, queue_id)) |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 138 | return -EBUSY; |
| 139 | |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 140 | pool->netdev = netdev; |
| 141 | pool->queue_id = queue_id; |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 142 | err = xsk_reg_pool_at_qid(netdev, pool, queue_id); |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 143 | if (err) |
| 144 | return err; |
| 145 | |
Björn Töpel | e392081 | 2020-11-30 19:51:59 +0100 | [diff] [blame] | 146 | if (flags & XDP_USE_NEED_WAKEUP) |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 147 | pool->uses_need_wakeup = true; |
Björn Töpel | e392081 | 2020-11-30 19:51:59 +0100 | [diff] [blame] | 148 | /* Tx needs to be explicitly woken up the first time. Also |
| 149 | * for supporting drivers that do not implement this |
| 150 | * feature. They will always have to call sendto() or poll(). |
| 151 | */ |
| 152 | pool->cached_need_wakeup = XDP_WAKEUP_TX; |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 153 | |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 154 | dev_hold(netdev); |
| 155 | |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 156 | if (force_copy) |
| 157 | /* For copy-mode, we are done. */ |
| 158 | return 0; |
| 159 | |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 160 | if (!netdev->netdev_ops->ndo_bpf || |
| 161 | !netdev->netdev_ops->ndo_xsk_wakeup) { |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 162 | err = -EOPNOTSUPP; |
| 163 | goto err_unreg_pool; |
| 164 | } |
| 165 | |
| 166 | bpf.command = XDP_SETUP_XSK_POOL; |
| 167 | bpf.xsk.pool = pool; |
| 168 | bpf.xsk.queue_id = queue_id; |
| 169 | |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 170 | err = netdev->netdev_ops->ndo_bpf(netdev, &bpf); |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 171 | if (err) |
| 172 | goto err_unreg_pool; |
| 173 | |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 174 | if (!pool->dma_pages) { |
| 175 | WARN(1, "Driver did not DMA map zero-copy buffers"); |
Zhang Changzhong | 12c8a8c | 2020-12-04 18:21:16 +0800 | [diff] [blame] | 176 | err = -EINVAL; |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 177 | goto err_unreg_xsk; |
| 178 | } |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 179 | pool->umem->zc = true; |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 180 | return 0; |
| 181 | |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 182 | err_unreg_xsk: |
| 183 | xp_disable_drv_zc(pool); |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 184 | err_unreg_pool: |
| 185 | if (!force_zc) |
| 186 | err = 0; /* fallback to copy mode */ |
Marek Majtyka | 17864891 | 2020-11-20 16:14:43 +0100 | [diff] [blame] | 187 | if (err) { |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 188 | xsk_clear_pool_at_qid(netdev, queue_id); |
Marek Majtyka | 17864891 | 2020-11-20 16:14:43 +0100 | [diff] [blame] | 189 | dev_put(netdev); |
| 190 | } |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 191 | return err; |
| 192 | } |
| 193 | |
Magnus Karlsson | b5aea28 | 2020-08-28 10:26:25 +0200 | [diff] [blame] | 194 | int xp_assign_dev_shared(struct xsk_buff_pool *pool, struct xdp_umem *umem, |
| 195 | struct net_device *dev, u16 queue_id) |
| 196 | { |
| 197 | u16 flags; |
| 198 | |
| 199 | /* One fill and completion ring required for each queue id. */ |
| 200 | if (!pool->fq || !pool->cq) |
| 201 | return -EINVAL; |
| 202 | |
| 203 | flags = umem->zc ? XDP_ZEROCOPY : XDP_COPY; |
| 204 | if (pool->uses_need_wakeup) |
| 205 | flags |= XDP_USE_NEED_WAKEUP; |
| 206 | |
Björn Töpel | f0863ea | 2021-01-22 11:53:50 +0100 | [diff] [blame] | 207 | return xp_assign_dev(pool, dev, queue_id, flags); |
Magnus Karlsson | b5aea28 | 2020-08-28 10:26:25 +0200 | [diff] [blame] | 208 | } |
| 209 | |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 210 | void xp_clear_dev(struct xsk_buff_pool *pool) |
| 211 | { |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 212 | if (!pool->netdev) |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 213 | return; |
| 214 | |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 215 | xp_disable_drv_zc(pool); |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 216 | xsk_clear_pool_at_qid(pool->netdev, pool->queue_id); |
| 217 | dev_put(pool->netdev); |
| 218 | pool->netdev = NULL; |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | static void xp_release_deferred(struct work_struct *work) |
| 222 | { |
| 223 | struct xsk_buff_pool *pool = container_of(work, struct xsk_buff_pool, |
| 224 | work); |
| 225 | |
| 226 | rtnl_lock(); |
| 227 | xp_clear_dev(pool); |
| 228 | rtnl_unlock(); |
| 229 | |
Magnus Karlsson | 7361f9c | 2020-08-28 10:26:18 +0200 | [diff] [blame] | 230 | if (pool->fq) { |
| 231 | xskq_destroy(pool->fq); |
| 232 | pool->fq = NULL; |
| 233 | } |
| 234 | |
| 235 | if (pool->cq) { |
| 236 | xskq_destroy(pool->cq); |
| 237 | pool->cq = NULL; |
| 238 | } |
| 239 | |
Magnus Karlsson | 537cf4e | 2020-11-20 12:53:39 +0100 | [diff] [blame] | 240 | xdp_put_umem(pool->umem, false); |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 241 | xp_destroy(pool); |
| 242 | } |
| 243 | |
| 244 | void xp_get_pool(struct xsk_buff_pool *pool) |
| 245 | { |
| 246 | refcount_inc(&pool->users); |
| 247 | } |
| 248 | |
Magnus Karlsson | e5e1a4b | 2020-10-27 13:32:01 +0100 | [diff] [blame] | 249 | bool xp_put_pool(struct xsk_buff_pool *pool) |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 250 | { |
| 251 | if (!pool) |
Magnus Karlsson | e5e1a4b | 2020-10-27 13:32:01 +0100 | [diff] [blame] | 252 | return false; |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 253 | |
| 254 | if (refcount_dec_and_test(&pool->users)) { |
| 255 | INIT_WORK(&pool->work, xp_release_deferred); |
| 256 | schedule_work(&pool->work); |
Magnus Karlsson | e5e1a4b | 2020-10-27 13:32:01 +0100 | [diff] [blame] | 257 | return true; |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 258 | } |
Magnus Karlsson | e5e1a4b | 2020-10-27 13:32:01 +0100 | [diff] [blame] | 259 | |
| 260 | return false; |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 261 | } |
| 262 | |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 263 | static struct xsk_dma_map *xp_find_dma_map(struct xsk_buff_pool *pool) |
| 264 | { |
| 265 | struct xsk_dma_map *dma_map; |
| 266 | |
| 267 | list_for_each_entry(dma_map, &pool->umem->xsk_dma_list, list) { |
| 268 | if (dma_map->netdev == pool->netdev) |
| 269 | return dma_map; |
| 270 | } |
| 271 | |
| 272 | return NULL; |
| 273 | } |
| 274 | |
| 275 | static struct xsk_dma_map *xp_create_dma_map(struct device *dev, struct net_device *netdev, |
| 276 | u32 nr_pages, struct xdp_umem *umem) |
| 277 | { |
| 278 | struct xsk_dma_map *dma_map; |
| 279 | |
| 280 | dma_map = kzalloc(sizeof(*dma_map), GFP_KERNEL); |
| 281 | if (!dma_map) |
| 282 | return NULL; |
| 283 | |
| 284 | 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] | 285 | if (!dma_map->dma_pages) { |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 286 | kfree(dma_map); |
| 287 | return NULL; |
| 288 | } |
| 289 | |
| 290 | dma_map->netdev = netdev; |
| 291 | dma_map->dev = dev; |
| 292 | dma_map->dma_need_sync = false; |
| 293 | dma_map->dma_pages_cnt = nr_pages; |
Magnus Karlsson | bf74a37 | 2020-09-14 16:50:36 +0200 | [diff] [blame] | 294 | refcount_set(&dma_map->users, 1); |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 295 | list_add(&dma_map->list, &umem->xsk_dma_list); |
| 296 | return dma_map; |
| 297 | } |
| 298 | |
| 299 | static void xp_destroy_dma_map(struct xsk_dma_map *dma_map) |
| 300 | { |
| 301 | list_del(&dma_map->list); |
| 302 | kvfree(dma_map->dma_pages); |
| 303 | kfree(dma_map); |
| 304 | } |
| 305 | |
| 306 | 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] | 307 | { |
| 308 | dma_addr_t *dma; |
| 309 | u32 i; |
| 310 | |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 311 | for (i = 0; i < dma_map->dma_pages_cnt; i++) { |
| 312 | dma = &dma_map->dma_pages[i]; |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 313 | if (*dma) { |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 314 | dma_unmap_page_attrs(dma_map->dev, *dma, PAGE_SIZE, |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 315 | DMA_BIDIRECTIONAL, attrs); |
| 316 | *dma = 0; |
| 317 | } |
| 318 | } |
| 319 | |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 320 | xp_destroy_dma_map(dma_map); |
| 321 | } |
| 322 | |
| 323 | void xp_dma_unmap(struct xsk_buff_pool *pool, unsigned long attrs) |
| 324 | { |
| 325 | struct xsk_dma_map *dma_map; |
| 326 | |
| 327 | if (pool->dma_pages_cnt == 0) |
| 328 | return; |
| 329 | |
| 330 | dma_map = xp_find_dma_map(pool); |
| 331 | if (!dma_map) { |
| 332 | WARN(1, "Could not find dma_map for device"); |
| 333 | return; |
| 334 | } |
| 335 | |
| 336 | if (!refcount_dec_and_test(&dma_map->users)) |
| 337 | return; |
| 338 | |
| 339 | __xp_dma_unmap(dma_map, attrs); |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 340 | kvfree(pool->dma_pages); |
| 341 | pool->dma_pages_cnt = 0; |
| 342 | pool->dev = NULL; |
| 343 | } |
| 344 | EXPORT_SYMBOL(xp_dma_unmap); |
| 345 | |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 346 | 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] | 347 | { |
| 348 | u32 i; |
| 349 | |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 350 | for (i = 0; i < dma_map->dma_pages_cnt - 1; i++) { |
| 351 | if (dma_map->dma_pages[i] + PAGE_SIZE == dma_map->dma_pages[i + 1]) |
| 352 | dma_map->dma_pages[i] |= XSK_NEXT_PG_CONTIG_MASK; |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 353 | else |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 354 | dma_map->dma_pages[i] &= ~XSK_NEXT_PG_CONTIG_MASK; |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 355 | } |
| 356 | } |
| 357 | |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 358 | static int xp_init_dma_info(struct xsk_buff_pool *pool, struct xsk_dma_map *dma_map) |
| 359 | { |
| 360 | pool->dma_pages = kvcalloc(dma_map->dma_pages_cnt, sizeof(*pool->dma_pages), GFP_KERNEL); |
| 361 | if (!pool->dma_pages) |
| 362 | return -ENOMEM; |
| 363 | |
| 364 | pool->dev = dma_map->dev; |
| 365 | pool->dma_pages_cnt = dma_map->dma_pages_cnt; |
| 366 | pool->dma_need_sync = dma_map->dma_need_sync; |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 367 | memcpy(pool->dma_pages, dma_map->dma_pages, |
| 368 | pool->dma_pages_cnt * sizeof(*pool->dma_pages)); |
| 369 | |
| 370 | return 0; |
| 371 | } |
| 372 | |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 373 | int xp_dma_map(struct xsk_buff_pool *pool, struct device *dev, |
| 374 | unsigned long attrs, struct page **pages, u32 nr_pages) |
| 375 | { |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 376 | struct xsk_dma_map *dma_map; |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 377 | dma_addr_t dma; |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 378 | int err; |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 379 | u32 i; |
| 380 | |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 381 | dma_map = xp_find_dma_map(pool); |
| 382 | if (dma_map) { |
| 383 | err = xp_init_dma_info(pool, dma_map); |
| 384 | if (err) |
| 385 | return err; |
| 386 | |
Magnus Karlsson | bf74a37 | 2020-09-14 16:50:36 +0200 | [diff] [blame] | 387 | refcount_inc(&dma_map->users); |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 388 | return 0; |
| 389 | } |
| 390 | |
| 391 | dma_map = xp_create_dma_map(dev, pool->netdev, nr_pages, pool->umem); |
| 392 | if (!dma_map) |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 393 | return -ENOMEM; |
| 394 | |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 395 | for (i = 0; i < dma_map->dma_pages_cnt; i++) { |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 396 | dma = dma_map_page_attrs(dev, pages[i], 0, PAGE_SIZE, |
| 397 | DMA_BIDIRECTIONAL, attrs); |
| 398 | if (dma_mapping_error(dev, dma)) { |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 399 | __xp_dma_unmap(dma_map, attrs); |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 400 | return -ENOMEM; |
| 401 | } |
Christoph Hellwig | 7e02457 | 2020-06-29 15:03:59 +0200 | [diff] [blame] | 402 | if (dma_need_sync(dev, dma)) |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 403 | dma_map->dma_need_sync = true; |
| 404 | dma_map->dma_pages[i] = dma; |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 405 | } |
| 406 | |
| 407 | if (pool->unaligned) |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 408 | xp_check_dma_contiguity(dma_map); |
| 409 | |
| 410 | err = xp_init_dma_info(pool, dma_map); |
| 411 | if (err) { |
| 412 | __xp_dma_unmap(dma_map, attrs); |
| 413 | return err; |
| 414 | } |
| 415 | |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 416 | return 0; |
| 417 | } |
| 418 | EXPORT_SYMBOL(xp_dma_map); |
| 419 | |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 420 | static bool xp_addr_crosses_non_contig_pg(struct xsk_buff_pool *pool, |
| 421 | u64 addr) |
| 422 | { |
| 423 | return xp_desc_crosses_non_contig_pg(pool, addr, pool->chunk_size); |
| 424 | } |
| 425 | |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 426 | static bool xp_check_unaligned(struct xsk_buff_pool *pool, u64 *addr) |
| 427 | { |
| 428 | *addr = xp_unaligned_extract_addr(*addr); |
| 429 | if (*addr >= pool->addrs_cnt || |
| 430 | *addr + pool->chunk_size > pool->addrs_cnt || |
| 431 | xp_addr_crosses_non_contig_pg(pool, *addr)) |
| 432 | return false; |
| 433 | return true; |
| 434 | } |
| 435 | |
| 436 | static bool xp_check_aligned(struct xsk_buff_pool *pool, u64 *addr) |
| 437 | { |
| 438 | *addr = xp_aligned_extract_addr(pool, *addr); |
| 439 | return *addr < pool->addrs_cnt; |
| 440 | } |
| 441 | |
| 442 | static struct xdp_buff_xsk *__xp_alloc(struct xsk_buff_pool *pool) |
| 443 | { |
| 444 | struct xdp_buff_xsk *xskb; |
| 445 | u64 addr; |
| 446 | bool ok; |
| 447 | |
| 448 | if (pool->free_heads_cnt == 0) |
| 449 | return NULL; |
| 450 | |
| 451 | xskb = pool->free_heads[--pool->free_heads_cnt]; |
| 452 | |
| 453 | for (;;) { |
| 454 | if (!xskq_cons_peek_addr_unchecked(pool->fq, &addr)) { |
Ciara Loftus | 8aa5a33 | 2020-07-08 07:28:33 +0000 | [diff] [blame] | 455 | pool->fq->queue_empty_descs++; |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 456 | xp_release(xskb); |
| 457 | return NULL; |
| 458 | } |
| 459 | |
| 460 | ok = pool->unaligned ? xp_check_unaligned(pool, &addr) : |
| 461 | xp_check_aligned(pool, &addr); |
| 462 | if (!ok) { |
| 463 | pool->fq->invalid_descs++; |
| 464 | xskq_cons_release(pool->fq); |
| 465 | continue; |
| 466 | } |
| 467 | break; |
| 468 | } |
| 469 | xskq_cons_release(pool->fq); |
| 470 | |
| 471 | xskb->orig_addr = addr; |
| 472 | xskb->xdp.data_hard_start = pool->addrs + addr + pool->headroom; |
| 473 | if (pool->dma_pages_cnt) { |
| 474 | xskb->frame_dma = (pool->dma_pages[addr >> PAGE_SHIFT] & |
| 475 | ~XSK_NEXT_PG_CONTIG_MASK) + |
| 476 | (addr & ~PAGE_MASK); |
| 477 | xskb->dma = xskb->frame_dma + pool->headroom + |
| 478 | XDP_PACKET_HEADROOM; |
| 479 | } |
| 480 | return xskb; |
| 481 | } |
| 482 | |
| 483 | struct xdp_buff *xp_alloc(struct xsk_buff_pool *pool) |
| 484 | { |
| 485 | struct xdp_buff_xsk *xskb; |
| 486 | |
| 487 | if (!pool->free_list_cnt) { |
| 488 | xskb = __xp_alloc(pool); |
| 489 | if (!xskb) |
| 490 | return NULL; |
| 491 | } else { |
| 492 | pool->free_list_cnt--; |
| 493 | xskb = list_first_entry(&pool->free_list, struct xdp_buff_xsk, |
| 494 | free_list_node); |
| 495 | list_del(&xskb->free_list_node); |
| 496 | } |
| 497 | |
| 498 | xskb->xdp.data = xskb->xdp.data_hard_start + XDP_PACKET_HEADROOM; |
| 499 | xskb->xdp.data_meta = xskb->xdp.data; |
| 500 | |
Christoph Hellwig | 91d5b70 | 2020-06-29 15:03:57 +0200 | [diff] [blame] | 501 | if (pool->dma_need_sync) { |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 502 | dma_sync_single_range_for_device(pool->dev, xskb->dma, 0, |
| 503 | pool->frame_len, |
| 504 | DMA_BIDIRECTIONAL); |
| 505 | } |
| 506 | return &xskb->xdp; |
| 507 | } |
| 508 | EXPORT_SYMBOL(xp_alloc); |
| 509 | |
Magnus Karlsson | 47e4075 | 2021-09-22 09:56:02 +0200 | [diff] [blame^] | 510 | static u32 xp_alloc_new_from_fq(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max) |
| 511 | { |
| 512 | u32 i, cached_cons, nb_entries; |
| 513 | |
| 514 | if (max > pool->free_heads_cnt) |
| 515 | max = pool->free_heads_cnt; |
| 516 | max = xskq_cons_nb_entries(pool->fq, max); |
| 517 | |
| 518 | cached_cons = pool->fq->cached_cons; |
| 519 | nb_entries = max; |
| 520 | i = max; |
| 521 | while (i--) { |
| 522 | struct xdp_buff_xsk *xskb; |
| 523 | u64 addr; |
| 524 | bool ok; |
| 525 | |
| 526 | __xskq_cons_read_addr_unchecked(pool->fq, cached_cons++, &addr); |
| 527 | |
| 528 | ok = pool->unaligned ? xp_check_unaligned(pool, &addr) : |
| 529 | xp_check_aligned(pool, &addr); |
| 530 | if (unlikely(!ok)) { |
| 531 | pool->fq->invalid_descs++; |
| 532 | nb_entries--; |
| 533 | continue; |
| 534 | } |
| 535 | |
| 536 | xskb = pool->free_heads[--pool->free_heads_cnt]; |
| 537 | *xdp = &xskb->xdp; |
| 538 | xskb->orig_addr = addr; |
| 539 | xskb->xdp.data_hard_start = pool->addrs + addr + pool->headroom; |
| 540 | xskb->frame_dma = (pool->dma_pages[addr >> PAGE_SHIFT] & |
| 541 | ~XSK_NEXT_PG_CONTIG_MASK) + (addr & ~PAGE_MASK); |
| 542 | xskb->dma = xskb->frame_dma + pool->headroom + XDP_PACKET_HEADROOM; |
| 543 | xdp++; |
| 544 | } |
| 545 | |
| 546 | xskq_cons_release_n(pool->fq, max); |
| 547 | return nb_entries; |
| 548 | } |
| 549 | |
| 550 | static u32 xp_alloc_reused(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 nb_entries) |
| 551 | { |
| 552 | struct xdp_buff_xsk *xskb; |
| 553 | u32 i; |
| 554 | |
| 555 | nb_entries = min_t(u32, nb_entries, pool->free_list_cnt); |
| 556 | |
| 557 | i = nb_entries; |
| 558 | while (i--) { |
| 559 | xskb = list_first_entry(&pool->free_list, struct xdp_buff_xsk, free_list_node); |
| 560 | list_del(&xskb->free_list_node); |
| 561 | |
| 562 | *xdp = &xskb->xdp; |
| 563 | xdp++; |
| 564 | } |
| 565 | pool->free_list_cnt -= nb_entries; |
| 566 | |
| 567 | return nb_entries; |
| 568 | } |
| 569 | |
| 570 | u32 xp_alloc_batch(struct xsk_buff_pool *pool, struct xdp_buff **xdp, u32 max) |
| 571 | { |
| 572 | u32 nb_entries1 = 0, nb_entries2; |
| 573 | |
| 574 | if (unlikely(pool->dma_need_sync)) { |
| 575 | /* Slow path */ |
| 576 | *xdp = xp_alloc(pool); |
| 577 | return !!*xdp; |
| 578 | } |
| 579 | |
| 580 | if (unlikely(pool->free_list_cnt)) { |
| 581 | nb_entries1 = xp_alloc_reused(pool, xdp, max); |
| 582 | if (nb_entries1 == max) |
| 583 | return nb_entries1; |
| 584 | |
| 585 | max -= nb_entries1; |
| 586 | xdp += nb_entries1; |
| 587 | } |
| 588 | |
| 589 | nb_entries2 = xp_alloc_new_from_fq(pool, xdp, max); |
| 590 | if (!nb_entries2) |
| 591 | pool->fq->queue_empty_descs++; |
| 592 | |
| 593 | return nb_entries1 + nb_entries2; |
| 594 | } |
| 595 | EXPORT_SYMBOL(xp_alloc_batch); |
| 596 | |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 597 | bool xp_can_alloc(struct xsk_buff_pool *pool, u32 count) |
| 598 | { |
| 599 | if (pool->free_list_cnt >= count) |
| 600 | return true; |
| 601 | return xskq_cons_has_entries(pool->fq, count - pool->free_list_cnt); |
| 602 | } |
| 603 | EXPORT_SYMBOL(xp_can_alloc); |
| 604 | |
| 605 | void xp_free(struct xdp_buff_xsk *xskb) |
| 606 | { |
| 607 | xskb->pool->free_list_cnt++; |
| 608 | list_add(&xskb->free_list_node, &xskb->pool->free_list); |
| 609 | } |
| 610 | EXPORT_SYMBOL(xp_free); |
| 611 | |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 612 | void *xp_raw_get_data(struct xsk_buff_pool *pool, u64 addr) |
| 613 | { |
| 614 | addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr; |
| 615 | return pool->addrs + addr; |
| 616 | } |
| 617 | EXPORT_SYMBOL(xp_raw_get_data); |
| 618 | |
| 619 | dma_addr_t xp_raw_get_dma(struct xsk_buff_pool *pool, u64 addr) |
| 620 | { |
| 621 | addr = pool->unaligned ? xp_unaligned_add_offset_to_addr(addr) : addr; |
| 622 | return (pool->dma_pages[addr >> PAGE_SHIFT] & |
| 623 | ~XSK_NEXT_PG_CONTIG_MASK) + |
| 624 | (addr & ~PAGE_MASK); |
| 625 | } |
| 626 | EXPORT_SYMBOL(xp_raw_get_dma); |
| 627 | |
Björn Töpel | 26062b1 | 2020-05-20 21:21:02 +0200 | [diff] [blame] | 628 | 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] | 629 | { |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 630 | dma_sync_single_range_for_cpu(xskb->pool->dev, xskb->dma, 0, |
| 631 | xskb->pool->frame_len, DMA_BIDIRECTIONAL); |
| 632 | } |
Björn Töpel | 26062b1 | 2020-05-20 21:21:02 +0200 | [diff] [blame] | 633 | EXPORT_SYMBOL(xp_dma_sync_for_cpu_slow); |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 634 | |
Björn Töpel | 26062b1 | 2020-05-20 21:21:02 +0200 | [diff] [blame] | 635 | void xp_dma_sync_for_device_slow(struct xsk_buff_pool *pool, dma_addr_t dma, |
| 636 | size_t size) |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 637 | { |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 638 | dma_sync_single_range_for_device(pool->dev, dma, 0, |
| 639 | size, DMA_BIDIRECTIONAL); |
| 640 | } |
Björn Töpel | 26062b1 | 2020-05-20 21:21:02 +0200 | [diff] [blame] | 641 | EXPORT_SYMBOL(xp_dma_sync_for_device_slow); |