Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* XDP user-space packet buffer |
| 3 | * Copyright(c) 2018 Intel Corporation. |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <linux/init.h> |
| 7 | #include <linux/sched/mm.h> |
| 8 | #include <linux/sched/signal.h> |
| 9 | #include <linux/sched/task.h> |
| 10 | #include <linux/uaccess.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/bpf.h> |
| 13 | #include <linux/mm.h> |
Jakub Kicinski | 84c6b86 | 2018-07-30 20:43:53 -0700 | [diff] [blame] | 14 | #include <linux/netdevice.h> |
| 15 | #include <linux/rtnetlink.h> |
Björn Töpel | 50e74c0 | 2019-01-24 19:59:38 +0100 | [diff] [blame] | 16 | #include <linux/idr.h> |
Ivan Khoronzhuk | 624676e | 2019-08-15 15:13:55 +0300 | [diff] [blame] | 17 | #include <linux/vmalloc.h> |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 18 | |
| 19 | #include "xdp_umem.h" |
Björn Töpel | e61e62b9 | 2018-06-04 14:05:51 +0200 | [diff] [blame] | 20 | #include "xsk_queue.h" |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 21 | |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 22 | #define XDP_UMEM_MIN_CHUNK_SIZE 2048 |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 23 | |
Björn Töpel | 50e74c0 | 2019-01-24 19:59:38 +0100 | [diff] [blame] | 24 | static DEFINE_IDA(umem_ida); |
| 25 | |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 26 | void xdp_add_sk_umem(struct xdp_umem *umem, struct xdp_sock *xs) |
| 27 | { |
| 28 | unsigned long flags; |
| 29 | |
| 30 | spin_lock_irqsave(&umem->xsk_list_lock, flags); |
| 31 | list_add_rcu(&xs->list, &umem->xsk_list); |
| 32 | spin_unlock_irqrestore(&umem->xsk_list_lock, flags); |
| 33 | } |
| 34 | |
| 35 | void xdp_del_sk_umem(struct xdp_umem *umem, struct xdp_sock *xs) |
| 36 | { |
| 37 | unsigned long flags; |
| 38 | |
Björn Töpel | 541d7fd | 2018-10-05 13:25:15 +0200 | [diff] [blame] | 39 | spin_lock_irqsave(&umem->xsk_list_lock, flags); |
| 40 | list_del_rcu(&xs->list); |
| 41 | spin_unlock_irqrestore(&umem->xsk_list_lock, flags); |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 42 | } |
| 43 | |
Magnus Karlsson | c9b47cc | 2018-10-01 14:51:34 +0200 | [diff] [blame] | 44 | /* The umem is stored both in the _rx struct and the _tx struct as we do |
| 45 | * not know if the device has more tx queues than rx, or the opposite. |
| 46 | * This might also change during run time. |
| 47 | */ |
Krzysztof Kazimierczak | cc5b5d35 | 2019-01-10 20:29:02 +0100 | [diff] [blame] | 48 | static int xdp_reg_umem_at_qid(struct net_device *dev, struct xdp_umem *umem, |
| 49 | u16 queue_id) |
Jakub Kicinski | 84c6b86 | 2018-07-30 20:43:53 -0700 | [diff] [blame] | 50 | { |
Krzysztof Kazimierczak | cc5b5d35 | 2019-01-10 20:29:02 +0100 | [diff] [blame] | 51 | if (queue_id >= max_t(unsigned int, |
| 52 | dev->real_num_rx_queues, |
| 53 | dev->real_num_tx_queues)) |
| 54 | return -EINVAL; |
| 55 | |
Magnus Karlsson | c9b47cc | 2018-10-01 14:51:34 +0200 | [diff] [blame] | 56 | if (queue_id < dev->real_num_rx_queues) |
| 57 | dev->_rx[queue_id].umem = umem; |
| 58 | if (queue_id < dev->real_num_tx_queues) |
| 59 | dev->_tx[queue_id].umem = umem; |
Krzysztof Kazimierczak | cc5b5d35 | 2019-01-10 20:29:02 +0100 | [diff] [blame] | 60 | |
| 61 | return 0; |
Magnus Karlsson | c9b47cc | 2018-10-01 14:51:34 +0200 | [diff] [blame] | 62 | } |
Jakub Kicinski | 84c6b86 | 2018-07-30 20:43:53 -0700 | [diff] [blame] | 63 | |
Jakub Kicinski | 1661d34 | 2018-10-01 14:51:36 +0200 | [diff] [blame] | 64 | struct xdp_umem *xdp_get_umem_from_qid(struct net_device *dev, |
| 65 | u16 queue_id) |
Magnus Karlsson | c9b47cc | 2018-10-01 14:51:34 +0200 | [diff] [blame] | 66 | { |
| 67 | if (queue_id < dev->real_num_rx_queues) |
| 68 | return dev->_rx[queue_id].umem; |
| 69 | if (queue_id < dev->real_num_tx_queues) |
| 70 | return dev->_tx[queue_id].umem; |
Jakub Kicinski | 84c6b86 | 2018-07-30 20:43:53 -0700 | [diff] [blame] | 71 | |
Magnus Karlsson | c9b47cc | 2018-10-01 14:51:34 +0200 | [diff] [blame] | 72 | return NULL; |
| 73 | } |
Jan Sokolowski | 5f4f3b2 | 2018-12-18 13:45:13 +0000 | [diff] [blame] | 74 | EXPORT_SYMBOL(xdp_get_umem_from_qid); |
Jakub Kicinski | 84c6b86 | 2018-07-30 20:43:53 -0700 | [diff] [blame] | 75 | |
Magnus Karlsson | c9b47cc | 2018-10-01 14:51:34 +0200 | [diff] [blame] | 76 | static void xdp_clear_umem_at_qid(struct net_device *dev, u16 queue_id) |
| 77 | { |
Magnus Karlsson | a41b4f3 | 2018-10-01 14:51:37 +0200 | [diff] [blame] | 78 | if (queue_id < dev->real_num_rx_queues) |
Magnus Karlsson | c9b47cc | 2018-10-01 14:51:34 +0200 | [diff] [blame] | 79 | dev->_rx[queue_id].umem = NULL; |
Magnus Karlsson | a41b4f3 | 2018-10-01 14:51:37 +0200 | [diff] [blame] | 80 | if (queue_id < dev->real_num_tx_queues) |
Magnus Karlsson | c9b47cc | 2018-10-01 14:51:34 +0200 | [diff] [blame] | 81 | dev->_tx[queue_id].umem = NULL; |
Jakub Kicinski | 84c6b86 | 2018-07-30 20:43:53 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 84 | int xdp_umem_assign_dev(struct xdp_umem *umem, struct net_device *dev, |
Magnus Karlsson | c9b47cc | 2018-10-01 14:51:34 +0200 | [diff] [blame] | 85 | u16 queue_id, u16 flags) |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 86 | { |
| 87 | bool force_zc, force_copy; |
| 88 | struct netdev_bpf bpf; |
Magnus Karlsson | c9b47cc | 2018-10-01 14:51:34 +0200 | [diff] [blame] | 89 | int err = 0; |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 90 | |
Ilya Maximets | 5464c3a | 2019-07-08 14:03:44 +0300 | [diff] [blame] | 91 | ASSERT_RTNL(); |
| 92 | |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 93 | force_zc = flags & XDP_ZEROCOPY; |
| 94 | force_copy = flags & XDP_COPY; |
| 95 | |
| 96 | if (force_zc && force_copy) |
| 97 | return -EINVAL; |
| 98 | |
Ilya Maximets | 5464c3a | 2019-07-08 14:03:44 +0300 | [diff] [blame] | 99 | if (xdp_get_umem_from_qid(dev, queue_id)) |
| 100 | return -EBUSY; |
Magnus Karlsson | c9b47cc | 2018-10-01 14:51:34 +0200 | [diff] [blame] | 101 | |
Krzysztof Kazimierczak | cc5b5d35 | 2019-01-10 20:29:02 +0100 | [diff] [blame] | 102 | err = xdp_reg_umem_at_qid(dev, umem, queue_id); |
| 103 | if (err) |
Ilya Maximets | 5464c3a | 2019-07-08 14:03:44 +0300 | [diff] [blame] | 104 | return err; |
Krzysztof Kazimierczak | cc5b5d35 | 2019-01-10 20:29:02 +0100 | [diff] [blame] | 105 | |
Magnus Karlsson | c9b47cc | 2018-10-01 14:51:34 +0200 | [diff] [blame] | 106 | umem->dev = dev; |
| 107 | umem->queue_id = queue_id; |
Ilya Maximets | 162c820 | 2019-06-28 11:04:06 +0300 | [diff] [blame] | 108 | |
Magnus Karlsson | 77cd0d7 | 2019-08-14 09:27:17 +0200 | [diff] [blame] | 109 | if (flags & XDP_USE_NEED_WAKEUP) { |
| 110 | umem->flags |= XDP_UMEM_USES_NEED_WAKEUP; |
| 111 | /* Tx needs to be explicitly woken up the first time. |
| 112 | * Also for supporting drivers that do not implement this |
| 113 | * feature. They will always have to call sendto(). |
| 114 | */ |
| 115 | xsk_set_tx_need_wakeup(umem); |
| 116 | } |
| 117 | |
Ilya Maximets | 162c820 | 2019-06-28 11:04:06 +0300 | [diff] [blame] | 118 | dev_hold(dev); |
| 119 | |
Magnus Karlsson | c9b47cc | 2018-10-01 14:51:34 +0200 | [diff] [blame] | 120 | if (force_copy) |
| 121 | /* For copy-mode, we are done. */ |
Ilya Maximets | 5464c3a | 2019-07-08 14:03:44 +0300 | [diff] [blame] | 122 | return 0; |
Magnus Karlsson | c9b47cc | 2018-10-01 14:51:34 +0200 | [diff] [blame] | 123 | |
Magnus Karlsson | 9116e5e | 2019-08-14 09:27:16 +0200 | [diff] [blame] | 124 | if (!dev->netdev_ops->ndo_bpf || !dev->netdev_ops->ndo_xsk_wakeup) { |
Magnus Karlsson | c9b47cc | 2018-10-01 14:51:34 +0200 | [diff] [blame] | 125 | err = -EOPNOTSUPP; |
| 126 | goto err_unreg_umem; |
Jakub Kicinski | 84c6b86 | 2018-07-30 20:43:53 -0700 | [diff] [blame] | 127 | } |
Jakub Kicinski | f734607 | 2018-07-30 20:43:52 -0700 | [diff] [blame] | 128 | |
| 129 | bpf.command = XDP_SETUP_XSK_UMEM; |
| 130 | bpf.xsk.umem = umem; |
| 131 | bpf.xsk.queue_id = queue_id; |
| 132 | |
Jakub Kicinski | f734607 | 2018-07-30 20:43:52 -0700 | [diff] [blame] | 133 | err = dev->netdev_ops->ndo_bpf(dev, &bpf); |
Jakub Kicinski | f734607 | 2018-07-30 20:43:52 -0700 | [diff] [blame] | 134 | if (err) |
Magnus Karlsson | c9b47cc | 2018-10-01 14:51:34 +0200 | [diff] [blame] | 135 | goto err_unreg_umem; |
Jakub Kicinski | f734607 | 2018-07-30 20:43:52 -0700 | [diff] [blame] | 136 | |
Jakub Kicinski | f734607 | 2018-07-30 20:43:52 -0700 | [diff] [blame] | 137 | umem->zc = true; |
| 138 | return 0; |
Jakub Kicinski | 84c6b86 | 2018-07-30 20:43:53 -0700 | [diff] [blame] | 139 | |
Magnus Karlsson | c9b47cc | 2018-10-01 14:51:34 +0200 | [diff] [blame] | 140 | err_unreg_umem: |
Magnus Karlsson | c9b47cc | 2018-10-01 14:51:34 +0200 | [diff] [blame] | 141 | if (!force_zc) |
| 142 | err = 0; /* fallback to copy mode */ |
Björn Töpel | 1e405c1 | 2019-02-12 08:51:14 +0100 | [diff] [blame] | 143 | if (err) |
| 144 | xdp_clear_umem_at_qid(dev, queue_id); |
Magnus Karlsson | c9b47cc | 2018-10-01 14:51:34 +0200 | [diff] [blame] | 145 | return err; |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 146 | } |
| 147 | |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 148 | void xdp_umem_clear_dev(struct xdp_umem *umem) |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 149 | { |
| 150 | struct netdev_bpf bpf; |
| 151 | int err; |
| 152 | |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 153 | ASSERT_RTNL(); |
| 154 | |
Ilya Maximets | 01d76b5 | 2019-06-07 20:27:32 +0300 | [diff] [blame] | 155 | if (!umem->dev) |
| 156 | return; |
| 157 | |
Magnus Karlsson | c9b47cc | 2018-10-01 14:51:34 +0200 | [diff] [blame] | 158 | if (umem->zc) { |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 159 | bpf.command = XDP_SETUP_XSK_UMEM; |
| 160 | bpf.xsk.umem = NULL; |
| 161 | bpf.xsk.queue_id = umem->queue_id; |
| 162 | |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 163 | err = umem->dev->netdev_ops->ndo_bpf(umem->dev, &bpf); |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 164 | |
| 165 | if (err) |
| 166 | WARN(1, "failed to disable umem!\n"); |
Magnus Karlsson | c9b47cc | 2018-10-01 14:51:34 +0200 | [diff] [blame] | 167 | } |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 168 | |
Ilya Maximets | 01d76b5 | 2019-06-07 20:27:32 +0300 | [diff] [blame] | 169 | xdp_clear_umem_at_qid(umem->dev, umem->queue_id); |
Magnus Karlsson | c9b47cc | 2018-10-01 14:51:34 +0200 | [diff] [blame] | 170 | |
Ilya Maximets | 162c820 | 2019-06-28 11:04:06 +0300 | [diff] [blame] | 171 | dev_put(umem->dev); |
| 172 | umem->dev = NULL; |
| 173 | umem->zc = false; |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 174 | } |
| 175 | |
Ivan Khoronzhuk | d9973cec | 2019-08-08 12:38:03 +0300 | [diff] [blame] | 176 | static void xdp_umem_unmap_pages(struct xdp_umem *umem) |
| 177 | { |
| 178 | unsigned int i; |
| 179 | |
| 180 | for (i = 0; i < umem->npgs; i++) |
Ivan Khoronzhuk | 624676e | 2019-08-15 15:13:55 +0300 | [diff] [blame] | 181 | if (PageHighMem(umem->pgs[i])) |
| 182 | vunmap(umem->pages[i].addr); |
| 183 | } |
| 184 | |
| 185 | static int xdp_umem_map_pages(struct xdp_umem *umem) |
| 186 | { |
| 187 | unsigned int i; |
| 188 | void *addr; |
| 189 | |
| 190 | for (i = 0; i < umem->npgs; i++) { |
| 191 | if (PageHighMem(umem->pgs[i])) |
| 192 | addr = vmap(&umem->pgs[i], 1, VM_MAP, PAGE_KERNEL); |
| 193 | else |
| 194 | addr = page_address(umem->pgs[i]); |
| 195 | |
| 196 | if (!addr) { |
| 197 | xdp_umem_unmap_pages(umem); |
| 198 | return -ENOMEM; |
| 199 | } |
| 200 | |
| 201 | umem->pages[i].addr = addr; |
| 202 | } |
| 203 | |
| 204 | return 0; |
Ivan Khoronzhuk | d9973cec | 2019-08-08 12:38:03 +0300 | [diff] [blame] | 205 | } |
| 206 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 207 | static void xdp_umem_unpin_pages(struct xdp_umem *umem) |
| 208 | { |
| 209 | unsigned int i; |
| 210 | |
Björn Töpel | a49049e | 2018-05-22 09:35:02 +0200 | [diff] [blame] | 211 | for (i = 0; i < umem->npgs; i++) { |
| 212 | struct page *page = umem->pgs[i]; |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 213 | |
Björn Töpel | a49049e | 2018-05-22 09:35:02 +0200 | [diff] [blame] | 214 | set_page_dirty_lock(page); |
| 215 | put_page(page); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 216 | } |
Björn Töpel | a49049e | 2018-05-22 09:35:02 +0200 | [diff] [blame] | 217 | |
| 218 | kfree(umem->pgs); |
| 219 | umem->pgs = NULL; |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static void xdp_umem_unaccount_pages(struct xdp_umem *umem) |
| 223 | { |
Daniel Borkmann | c09290c | 2018-06-08 00:06:01 +0200 | [diff] [blame] | 224 | if (umem->user) { |
| 225 | atomic_long_sub(umem->npgs, &umem->user->locked_vm); |
| 226 | free_uid(umem->user); |
| 227 | } |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | static void xdp_umem_release(struct xdp_umem *umem) |
| 231 | { |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 232 | rtnl_lock(); |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 233 | xdp_umem_clear_dev(umem); |
Ilya Maximets | 455302d | 2019-06-28 11:04:07 +0300 | [diff] [blame] | 234 | rtnl_unlock(); |
Björn Töpel | 173d3ad | 2018-06-04 14:05:55 +0200 | [diff] [blame] | 235 | |
Björn Töpel | 50e74c0 | 2019-01-24 19:59:38 +0100 | [diff] [blame] | 236 | ida_simple_remove(&umem_ida, umem->id); |
| 237 | |
Magnus Karlsson | 423f383 | 2018-05-02 13:01:24 +0200 | [diff] [blame] | 238 | if (umem->fq) { |
| 239 | xskq_destroy(umem->fq); |
| 240 | umem->fq = NULL; |
| 241 | } |
| 242 | |
Magnus Karlsson | fe23083 | 2018-05-02 13:01:31 +0200 | [diff] [blame] | 243 | if (umem->cq) { |
| 244 | xskq_destroy(umem->cq); |
| 245 | umem->cq = NULL; |
| 246 | } |
| 247 | |
Jakub Kicinski | f5bd913 | 2018-09-07 10:18:46 +0200 | [diff] [blame] | 248 | xsk_reuseq_destroy(umem); |
| 249 | |
Ivan Khoronzhuk | d9973cec | 2019-08-08 12:38:03 +0300 | [diff] [blame] | 250 | xdp_umem_unmap_pages(umem); |
Björn Töpel | a49049e | 2018-05-22 09:35:02 +0200 | [diff] [blame] | 251 | xdp_umem_unpin_pages(umem); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 252 | |
Björn Töpel | 8aef734 | 2018-06-04 14:05:52 +0200 | [diff] [blame] | 253 | kfree(umem->pages); |
| 254 | umem->pages = NULL; |
| 255 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 256 | xdp_umem_unaccount_pages(umem); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 257 | kfree(umem); |
| 258 | } |
| 259 | |
| 260 | static void xdp_umem_release_deferred(struct work_struct *work) |
| 261 | { |
| 262 | struct xdp_umem *umem = container_of(work, struct xdp_umem, work); |
| 263 | |
| 264 | xdp_umem_release(umem); |
| 265 | } |
| 266 | |
| 267 | void xdp_get_umem(struct xdp_umem *umem) |
| 268 | { |
Björn Töpel | d3b42f1 | 2018-05-22 09:35:03 +0200 | [diff] [blame] | 269 | refcount_inc(&umem->users); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | void xdp_put_umem(struct xdp_umem *umem) |
| 273 | { |
| 274 | if (!umem) |
| 275 | return; |
| 276 | |
Björn Töpel | d3b42f1 | 2018-05-22 09:35:03 +0200 | [diff] [blame] | 277 | if (refcount_dec_and_test(&umem->users)) { |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 278 | INIT_WORK(&umem->work, xdp_umem_release_deferred); |
| 279 | schedule_work(&umem->work); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | static int xdp_umem_pin_pages(struct xdp_umem *umem) |
| 284 | { |
| 285 | unsigned int gup_flags = FOLL_WRITE; |
| 286 | long npgs; |
| 287 | int err; |
| 288 | |
Björn Töpel | a343993 | 2018-06-11 13:57:12 +0200 | [diff] [blame] | 289 | umem->pgs = kcalloc(umem->npgs, sizeof(*umem->pgs), |
| 290 | GFP_KERNEL | __GFP_NOWARN); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 291 | if (!umem->pgs) |
| 292 | return -ENOMEM; |
| 293 | |
Davidlohr Bueso | e451eb5 | 2019-02-11 08:15:29 -0800 | [diff] [blame] | 294 | down_read(¤t->mm->mmap_sem); |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 295 | npgs = get_user_pages(umem->address, umem->npgs, |
| 296 | gup_flags | FOLL_LONGTERM, &umem->pgs[0], NULL); |
Davidlohr Bueso | e451eb5 | 2019-02-11 08:15:29 -0800 | [diff] [blame] | 297 | up_read(¤t->mm->mmap_sem); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 298 | |
| 299 | if (npgs != umem->npgs) { |
| 300 | if (npgs >= 0) { |
| 301 | umem->npgs = npgs; |
| 302 | err = -ENOMEM; |
| 303 | goto out_pin; |
| 304 | } |
| 305 | err = npgs; |
| 306 | goto out_pgs; |
| 307 | } |
| 308 | return 0; |
| 309 | |
| 310 | out_pin: |
| 311 | xdp_umem_unpin_pages(umem); |
| 312 | out_pgs: |
| 313 | kfree(umem->pgs); |
| 314 | umem->pgs = NULL; |
| 315 | return err; |
| 316 | } |
| 317 | |
| 318 | static int xdp_umem_account_pages(struct xdp_umem *umem) |
| 319 | { |
| 320 | unsigned long lock_limit, new_npgs, old_npgs; |
| 321 | |
| 322 | if (capable(CAP_IPC_LOCK)) |
| 323 | return 0; |
| 324 | |
| 325 | lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 326 | umem->user = get_uid(current_user()); |
| 327 | |
| 328 | do { |
| 329 | old_npgs = atomic_long_read(&umem->user->locked_vm); |
| 330 | new_npgs = old_npgs + umem->npgs; |
| 331 | if (new_npgs > lock_limit) { |
| 332 | free_uid(umem->user); |
| 333 | umem->user = NULL; |
| 334 | return -ENOBUFS; |
| 335 | } |
| 336 | } while (atomic_long_cmpxchg(&umem->user->locked_vm, old_npgs, |
| 337 | new_npgs) != old_npgs); |
| 338 | return 0; |
| 339 | } |
| 340 | |
Björn Töpel | a49049e | 2018-05-22 09:35:02 +0200 | [diff] [blame] | 341 | static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr) |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 342 | { |
Kevin Laatz | c05cd36 | 2019-08-27 02:25:22 +0000 | [diff] [blame^] | 343 | bool unaligned_chunks = mr->flags & XDP_UMEM_UNALIGNED_CHUNK_FLAG; |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 344 | u32 chunk_size = mr->chunk_size, headroom = mr->headroom; |
| 345 | unsigned int chunks, chunks_per_page; |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 346 | u64 addr = mr->addr, size = mr->len; |
Ivan Khoronzhuk | 624676e | 2019-08-15 15:13:55 +0300 | [diff] [blame] | 347 | int size_chk, err; |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 348 | |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 349 | if (chunk_size < XDP_UMEM_MIN_CHUNK_SIZE || chunk_size > PAGE_SIZE) { |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 350 | /* Strictly speaking we could support this, if: |
| 351 | * - huge pages, or* |
| 352 | * - using an IOMMU, or |
| 353 | * - making sure the memory area is consecutive |
| 354 | * but for now, we simply say "computer says no". |
| 355 | */ |
| 356 | return -EINVAL; |
| 357 | } |
| 358 | |
Kevin Laatz | c05cd36 | 2019-08-27 02:25:22 +0000 | [diff] [blame^] | 359 | if (mr->flags & ~(XDP_UMEM_UNALIGNED_CHUNK_FLAG | |
| 360 | XDP_UMEM_USES_NEED_WAKEUP)) |
| 361 | return -EINVAL; |
| 362 | |
| 363 | if (!unaligned_chunks && !is_power_of_2(chunk_size)) |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 364 | return -EINVAL; |
| 365 | |
| 366 | if (!PAGE_ALIGNED(addr)) { |
| 367 | /* Memory area has to be page size aligned. For |
| 368 | * simplicity, this might change. |
| 369 | */ |
| 370 | return -EINVAL; |
| 371 | } |
| 372 | |
| 373 | if ((addr + size) < addr) |
| 374 | return -EINVAL; |
| 375 | |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 376 | chunks = (unsigned int)div_u64(size, chunk_size); |
| 377 | if (chunks == 0) |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 378 | return -EINVAL; |
| 379 | |
Kevin Laatz | c05cd36 | 2019-08-27 02:25:22 +0000 | [diff] [blame^] | 380 | if (!unaligned_chunks) { |
| 381 | chunks_per_page = PAGE_SIZE / chunk_size; |
| 382 | if (chunks < chunks_per_page || chunks % chunks_per_page) |
| 383 | return -EINVAL; |
| 384 | } |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 385 | |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 386 | headroom = ALIGN(headroom, 64); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 387 | |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 388 | size_chk = chunk_size - headroom - XDP_PACKET_HEADROOM; |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 389 | if (size_chk < 0) |
| 390 | return -EINVAL; |
| 391 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 392 | umem->address = (unsigned long)addr; |
Kevin Laatz | c05cd36 | 2019-08-27 02:25:22 +0000 | [diff] [blame^] | 393 | umem->chunk_mask = unaligned_chunks ? XSK_UNALIGNED_BUF_ADDR_MASK |
| 394 | : ~((u64)chunk_size - 1); |
Magnus Karlsson | 93ee30f | 2018-08-31 13:40:02 +0200 | [diff] [blame] | 395 | umem->size = size; |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 396 | umem->headroom = headroom; |
| 397 | umem->chunk_size_nohr = chunk_size - headroom; |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 398 | umem->npgs = size / PAGE_SIZE; |
| 399 | umem->pgs = NULL; |
| 400 | umem->user = NULL; |
Kevin Laatz | c05cd36 | 2019-08-27 02:25:22 +0000 | [diff] [blame^] | 401 | umem->flags = mr->flags; |
Magnus Karlsson | ac98d8a | 2018-06-04 14:05:57 +0200 | [diff] [blame] | 402 | INIT_LIST_HEAD(&umem->xsk_list); |
| 403 | spin_lock_init(&umem->xsk_list_lock); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 404 | |
Björn Töpel | d3b42f1 | 2018-05-22 09:35:03 +0200 | [diff] [blame] | 405 | refcount_set(&umem->users, 1); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 406 | |
| 407 | err = xdp_umem_account_pages(umem); |
| 408 | if (err) |
Björn Töpel | 044175a | 2019-03-13 15:15:49 +0100 | [diff] [blame] | 409 | return err; |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 410 | |
| 411 | err = xdp_umem_pin_pages(umem); |
| 412 | if (err) |
| 413 | goto out_account; |
Björn Töpel | 8aef734 | 2018-06-04 14:05:52 +0200 | [diff] [blame] | 414 | |
| 415 | umem->pages = kcalloc(umem->npgs, sizeof(*umem->pages), GFP_KERNEL); |
| 416 | if (!umem->pages) { |
| 417 | err = -ENOMEM; |
| 418 | goto out_account; |
| 419 | } |
| 420 | |
Ivan Khoronzhuk | 624676e | 2019-08-15 15:13:55 +0300 | [diff] [blame] | 421 | err = xdp_umem_map_pages(umem); |
| 422 | if (!err) |
| 423 | return 0; |
Björn Töpel | 8aef734 | 2018-06-04 14:05:52 +0200 | [diff] [blame] | 424 | |
Ivan Khoronzhuk | 624676e | 2019-08-15 15:13:55 +0300 | [diff] [blame] | 425 | kfree(umem->pages); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 426 | |
| 427 | out_account: |
| 428 | xdp_umem_unaccount_pages(umem); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 429 | return err; |
| 430 | } |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 431 | |
Björn Töpel | a49049e | 2018-05-22 09:35:02 +0200 | [diff] [blame] | 432 | struct xdp_umem *xdp_umem_create(struct xdp_umem_reg *mr) |
| 433 | { |
| 434 | struct xdp_umem *umem; |
| 435 | int err; |
| 436 | |
| 437 | umem = kzalloc(sizeof(*umem), GFP_KERNEL); |
| 438 | if (!umem) |
| 439 | return ERR_PTR(-ENOMEM); |
| 440 | |
Björn Töpel | 50e74c0 | 2019-01-24 19:59:38 +0100 | [diff] [blame] | 441 | err = ida_simple_get(&umem_ida, 0, 0, GFP_KERNEL); |
| 442 | if (err < 0) { |
| 443 | kfree(umem); |
| 444 | return ERR_PTR(err); |
| 445 | } |
| 446 | umem->id = err; |
| 447 | |
Björn Töpel | a49049e | 2018-05-22 09:35:02 +0200 | [diff] [blame] | 448 | err = xdp_umem_reg(umem, mr); |
| 449 | if (err) { |
Björn Töpel | 50e74c0 | 2019-01-24 19:59:38 +0100 | [diff] [blame] | 450 | ida_simple_remove(&umem_ida, umem->id); |
Björn Töpel | a49049e | 2018-05-22 09:35:02 +0200 | [diff] [blame] | 451 | kfree(umem); |
| 452 | return ERR_PTR(err); |
| 453 | } |
| 454 | |
| 455 | return umem; |
| 456 | } |
| 457 | |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 458 | bool xdp_umem_validate_queues(struct xdp_umem *umem) |
| 459 | { |
Björn Töpel | da60cf0 | 2018-05-18 14:00:23 +0200 | [diff] [blame] | 460 | return umem->fq && umem->cq; |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 461 | } |