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 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 26 | static void xdp_umem_unpin_pages(struct xdp_umem *umem) |
| 27 | { |
John Hubbard | f1f6a7d | 2020-01-30 22:13:35 -0800 | [diff] [blame] | 28 | unpin_user_pages_dirty_lock(umem->pgs, umem->npgs, true); |
Björn Töpel | a49049e | 2018-05-22 09:35:02 +0200 | [diff] [blame] | 29 | |
Magnus Karlsson | a720a2a | 2021-05-21 10:33:01 +0200 | [diff] [blame] | 30 | kvfree(umem->pgs); |
Björn Töpel | a49049e | 2018-05-22 09:35:02 +0200 | [diff] [blame] | 31 | umem->pgs = NULL; |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | static void xdp_umem_unaccount_pages(struct xdp_umem *umem) |
| 35 | { |
Daniel Borkmann | c09290c | 2018-06-08 00:06:01 +0200 | [diff] [blame] | 36 | if (umem->user) { |
| 37 | atomic_long_sub(umem->npgs, &umem->user->locked_vm); |
| 38 | free_uid(umem->user); |
| 39 | } |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 40 | } |
| 41 | |
Magnus Karlsson | 7f7ffa4 | 2020-08-28 10:26:21 +0200 | [diff] [blame] | 42 | static void xdp_umem_addr_unmap(struct xdp_umem *umem) |
| 43 | { |
| 44 | vunmap(umem->addrs); |
| 45 | umem->addrs = NULL; |
| 46 | } |
| 47 | |
| 48 | static int xdp_umem_addr_map(struct xdp_umem *umem, struct page **pages, |
| 49 | u32 nr_pages) |
| 50 | { |
| 51 | umem->addrs = vmap(pages, nr_pages, VM_MAP, PAGE_KERNEL); |
| 52 | if (!umem->addrs) |
| 53 | return -ENOMEM; |
| 54 | return 0; |
| 55 | } |
| 56 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 57 | static void xdp_umem_release(struct xdp_umem *umem) |
| 58 | { |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 59 | umem->zc = false; |
Björn Töpel | 50e74c0 | 2019-01-24 19:59:38 +0100 | [diff] [blame] | 60 | ida_simple_remove(&umem_ida, umem->id); |
| 61 | |
Magnus Karlsson | 7f7ffa4 | 2020-08-28 10:26:21 +0200 | [diff] [blame] | 62 | xdp_umem_addr_unmap(umem); |
Björn Töpel | a49049e | 2018-05-22 09:35:02 +0200 | [diff] [blame] | 63 | xdp_umem_unpin_pages(umem); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 64 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 65 | xdp_umem_unaccount_pages(umem); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 66 | kfree(umem); |
| 67 | } |
| 68 | |
Magnus Karlsson | 537cf4e | 2020-11-20 12:53:39 +0100 | [diff] [blame] | 69 | static void xdp_umem_release_deferred(struct work_struct *work) |
| 70 | { |
| 71 | struct xdp_umem *umem = container_of(work, struct xdp_umem, work); |
| 72 | |
| 73 | xdp_umem_release(umem); |
| 74 | } |
| 75 | |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 76 | void xdp_get_umem(struct xdp_umem *umem) |
| 77 | { |
Björn Töpel | d3b42f1 | 2018-05-22 09:35:03 +0200 | [diff] [blame] | 78 | refcount_inc(&umem->users); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 79 | } |
| 80 | |
Magnus Karlsson | 537cf4e | 2020-11-20 12:53:39 +0100 | [diff] [blame] | 81 | void xdp_put_umem(struct xdp_umem *umem, bool defer_cleanup) |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 82 | { |
| 83 | if (!umem) |
| 84 | return; |
| 85 | |
Magnus Karlsson | 537cf4e | 2020-11-20 12:53:39 +0100 | [diff] [blame] | 86 | if (refcount_dec_and_test(&umem->users)) { |
| 87 | if (defer_cleanup) { |
| 88 | INIT_WORK(&umem->work, xdp_umem_release_deferred); |
| 89 | schedule_work(&umem->work); |
| 90 | } else { |
| 91 | xdp_umem_release(umem); |
| 92 | } |
| 93 | } |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 94 | } |
| 95 | |
Magnus Karlsson | 07bf2d9 | 2020-05-04 15:33:52 +0200 | [diff] [blame] | 96 | static int xdp_umem_pin_pages(struct xdp_umem *umem, unsigned long address) |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 97 | { |
| 98 | unsigned int gup_flags = FOLL_WRITE; |
| 99 | long npgs; |
| 100 | int err; |
| 101 | |
Magnus Karlsson | a720a2a | 2021-05-21 10:33:01 +0200 | [diff] [blame] | 102 | umem->pgs = kvcalloc(umem->npgs, sizeof(*umem->pgs), GFP_KERNEL | __GFP_NOWARN); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 103 | if (!umem->pgs) |
| 104 | return -ENOMEM; |
| 105 | |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 106 | mmap_read_lock(current->mm); |
Magnus Karlsson | 07bf2d9 | 2020-05-04 15:33:52 +0200 | [diff] [blame] | 107 | npgs = pin_user_pages(address, umem->npgs, |
Ira Weiny | 932f4a6 | 2019-05-13 17:17:03 -0700 | [diff] [blame] | 108 | gup_flags | FOLL_LONGTERM, &umem->pgs[0], NULL); |
Michel Lespinasse | d8ed45c | 2020-06-08 21:33:25 -0700 | [diff] [blame] | 109 | mmap_read_unlock(current->mm); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 110 | |
| 111 | if (npgs != umem->npgs) { |
| 112 | if (npgs >= 0) { |
| 113 | umem->npgs = npgs; |
| 114 | err = -ENOMEM; |
| 115 | goto out_pin; |
| 116 | } |
| 117 | err = npgs; |
| 118 | goto out_pgs; |
| 119 | } |
| 120 | return 0; |
| 121 | |
| 122 | out_pin: |
| 123 | xdp_umem_unpin_pages(umem); |
| 124 | out_pgs: |
Magnus Karlsson | a720a2a | 2021-05-21 10:33:01 +0200 | [diff] [blame] | 125 | kvfree(umem->pgs); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 126 | umem->pgs = NULL; |
| 127 | return err; |
| 128 | } |
| 129 | |
| 130 | static int xdp_umem_account_pages(struct xdp_umem *umem) |
| 131 | { |
| 132 | unsigned long lock_limit, new_npgs, old_npgs; |
| 133 | |
| 134 | if (capable(CAP_IPC_LOCK)) |
| 135 | return 0; |
| 136 | |
| 137 | lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 138 | umem->user = get_uid(current_user()); |
| 139 | |
| 140 | do { |
| 141 | old_npgs = atomic_long_read(&umem->user->locked_vm); |
| 142 | new_npgs = old_npgs + umem->npgs; |
| 143 | if (new_npgs > lock_limit) { |
| 144 | free_uid(umem->user); |
| 145 | umem->user = NULL; |
| 146 | return -ENOBUFS; |
| 147 | } |
| 148 | } while (atomic_long_cmpxchg(&umem->user->locked_vm, old_npgs, |
| 149 | new_npgs) != old_npgs); |
| 150 | return 0; |
| 151 | } |
| 152 | |
Björn Töpel | a49049e | 2018-05-22 09:35:02 +0200 | [diff] [blame] | 153 | 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] | 154 | { |
Björn Töpel | 2b1667e | 2020-09-10 09:56:09 +0200 | [diff] [blame] | 155 | u32 npgs_rem, chunk_size = mr->chunk_size, headroom = mr->headroom; |
Kevin Laatz | c05cd36 | 2019-08-27 02:25:22 +0000 | [diff] [blame] | 156 | bool unaligned_chunks = mr->flags & XDP_UMEM_UNALIGNED_CHUNK_FLAG; |
Björn Töpel | b16a87d | 2020-05-25 10:03:59 +0200 | [diff] [blame] | 157 | u64 npgs, addr = mr->addr, size = mr->len; |
Björn Töpel | 2b1667e | 2020-09-10 09:56:09 +0200 | [diff] [blame] | 158 | unsigned int chunks, chunks_rem; |
Magnus Karlsson | 99e3a23 | 2020-04-14 09:35:15 +0200 | [diff] [blame] | 159 | int err; |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 160 | |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 161 | 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] | 162 | /* Strictly speaking we could support this, if: |
| 163 | * - huge pages, or* |
| 164 | * - using an IOMMU, or |
| 165 | * - making sure the memory area is consecutive |
| 166 | * but for now, we simply say "computer says no". |
| 167 | */ |
| 168 | return -EINVAL; |
| 169 | } |
| 170 | |
Magnus Karlsson | c2d3d6a | 2020-08-28 10:26:19 +0200 | [diff] [blame] | 171 | if (mr->flags & ~XDP_UMEM_UNALIGNED_CHUNK_FLAG) |
Kevin Laatz | c05cd36 | 2019-08-27 02:25:22 +0000 | [diff] [blame] | 172 | return -EINVAL; |
| 173 | |
| 174 | if (!unaligned_chunks && !is_power_of_2(chunk_size)) |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 175 | return -EINVAL; |
| 176 | |
| 177 | if (!PAGE_ALIGNED(addr)) { |
| 178 | /* Memory area has to be page size aligned. For |
| 179 | * simplicity, this might change. |
| 180 | */ |
| 181 | return -EINVAL; |
| 182 | } |
| 183 | |
| 184 | if ((addr + size) < addr) |
| 185 | return -EINVAL; |
| 186 | |
Björn Töpel | 2b1667e | 2020-09-10 09:56:09 +0200 | [diff] [blame] | 187 | npgs = div_u64_rem(size, PAGE_SIZE, &npgs_rem); |
| 188 | if (npgs_rem) |
| 189 | npgs++; |
Björn Töpel | b16a87d | 2020-05-25 10:03:59 +0200 | [diff] [blame] | 190 | if (npgs > U32_MAX) |
| 191 | return -EINVAL; |
| 192 | |
Björn Töpel | 2b1667e | 2020-09-10 09:56:09 +0200 | [diff] [blame] | 193 | chunks = (unsigned int)div_u64_rem(size, chunk_size, &chunks_rem); |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 194 | if (chunks == 0) |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 195 | return -EINVAL; |
| 196 | |
Björn Töpel | 2b1667e | 2020-09-10 09:56:09 +0200 | [diff] [blame] | 197 | if (!unaligned_chunks && chunks_rem) |
| 198 | return -EINVAL; |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 199 | |
Magnus Karlsson | 99e3a23 | 2020-04-14 09:35:15 +0200 | [diff] [blame] | 200 | if (headroom >= chunk_size - XDP_PACKET_HEADROOM) |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 201 | return -EINVAL; |
| 202 | |
Magnus Karlsson | 93ee30f | 2018-08-31 13:40:02 +0200 | [diff] [blame] | 203 | umem->size = size; |
Björn Töpel | bbff2f3 | 2018-06-04 13:57:13 +0200 | [diff] [blame] | 204 | umem->headroom = headroom; |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 205 | umem->chunk_size = chunk_size; |
Magnus Karlsson | 1c1efc2 | 2020-08-28 10:26:17 +0200 | [diff] [blame] | 206 | umem->chunks = chunks; |
Björn Töpel | b16a87d | 2020-05-25 10:03:59 +0200 | [diff] [blame] | 207 | umem->npgs = (u32)npgs; |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 208 | umem->pgs = NULL; |
| 209 | umem->user = NULL; |
Kevin Laatz | c05cd36 | 2019-08-27 02:25:22 +0000 | [diff] [blame] | 210 | umem->flags = mr->flags; |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 211 | |
Magnus Karlsson | 921b686 | 2020-08-28 10:26:22 +0200 | [diff] [blame] | 212 | INIT_LIST_HEAD(&umem->xsk_dma_list); |
Björn Töpel | d3b42f1 | 2018-05-22 09:35:03 +0200 | [diff] [blame] | 213 | refcount_set(&umem->users, 1); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 214 | |
| 215 | err = xdp_umem_account_pages(umem); |
| 216 | if (err) |
Björn Töpel | 044175a | 2019-03-13 15:15:49 +0100 | [diff] [blame] | 217 | return err; |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 218 | |
Magnus Karlsson | 07bf2d9 | 2020-05-04 15:33:52 +0200 | [diff] [blame] | 219 | err = xdp_umem_pin_pages(umem, (unsigned long)addr); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 220 | if (err) |
| 221 | goto out_account; |
Björn Töpel | 8aef734 | 2018-06-04 14:05:52 +0200 | [diff] [blame] | 222 | |
Magnus Karlsson | 7f7ffa4 | 2020-08-28 10:26:21 +0200 | [diff] [blame] | 223 | err = xdp_umem_addr_map(umem, umem->pgs, umem->npgs); |
| 224 | if (err) |
| 225 | goto out_unpin; |
| 226 | |
Björn Töpel | 2b43470 | 2020-05-20 21:20:53 +0200 | [diff] [blame] | 227 | return 0; |
| 228 | |
Magnus Karlsson | 7f7ffa4 | 2020-08-28 10:26:21 +0200 | [diff] [blame] | 229 | out_unpin: |
| 230 | xdp_umem_unpin_pages(umem); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 231 | out_account: |
| 232 | xdp_umem_unaccount_pages(umem); |
Björn Töpel | c0c77d8 | 2018-05-02 13:01:23 +0200 | [diff] [blame] | 233 | return err; |
| 234 | } |
Magnus Karlsson | 965a990 | 2018-05-02 13:01:26 +0200 | [diff] [blame] | 235 | |
Björn Töpel | a49049e | 2018-05-22 09:35:02 +0200 | [diff] [blame] | 236 | struct xdp_umem *xdp_umem_create(struct xdp_umem_reg *mr) |
| 237 | { |
| 238 | struct xdp_umem *umem; |
| 239 | int err; |
| 240 | |
| 241 | umem = kzalloc(sizeof(*umem), GFP_KERNEL); |
| 242 | if (!umem) |
| 243 | return ERR_PTR(-ENOMEM); |
| 244 | |
Björn Töpel | 50e74c0 | 2019-01-24 19:59:38 +0100 | [diff] [blame] | 245 | err = ida_simple_get(&umem_ida, 0, 0, GFP_KERNEL); |
| 246 | if (err < 0) { |
| 247 | kfree(umem); |
| 248 | return ERR_PTR(err); |
| 249 | } |
| 250 | umem->id = err; |
| 251 | |
Björn Töpel | a49049e | 2018-05-22 09:35:02 +0200 | [diff] [blame] | 252 | err = xdp_umem_reg(umem, mr); |
| 253 | if (err) { |
Björn Töpel | 50e74c0 | 2019-01-24 19:59:38 +0100 | [diff] [blame] | 254 | ida_simple_remove(&umem_ida, umem->id); |
Björn Töpel | a49049e | 2018-05-22 09:35:02 +0200 | [diff] [blame] | 255 | kfree(umem); |
| 256 | return ERR_PTR(err); |
| 257 | } |
| 258 | |
| 259 | return umem; |
| 260 | } |