| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* XDP user-space packet buffer |
| 3 |
* Copyright(c) 2018 Intel Corporation. |
| 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> |
| 14 |
#include <linux/netdevice.h> |
| 15 |
#include <linux/rtnetlink.h> |
| 16 |
#include <linux/idr.h> |
| 17 |
#include <linux/vmalloc.h> |
| 18 |
|
| 19 |
#include "xdp_umem.h" |
| 20 |
#include "xsk_queue.h" |
| 21 |
|
| 22 |
static DEFINE_IDA(umem_ida); |
| 23 |
|
| 24 |
static void xdp_umem_unpin_pages(struct xdp_umem *umem) |
| 25 |
{ |
| 26 |
unpin_user_pages_dirty_lock(umem->pgs, umem->npgs, true); |
| 27 |
|
| 28 |
kvfree(umem->pgs); |
| 29 |
umem->pgs = NULL; |
| 30 |
} |
| 31 |
|
| 32 |
static void xdp_umem_unaccount_pages(struct xdp_umem *umem) |
| 33 |
{ |
| 34 |
if (umem->user) { |
| 35 |
atomic_long_sub(umem->npgs, &umem->user->locked_vm); |
| 36 |
free_uid(umem->user); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
static void xdp_umem_addr_unmap(struct xdp_umem *umem) |
| 41 |
{ |
| 42 |
vunmap(umem->addrs); |
| 43 |
umem->addrs = NULL; |
| 44 |
} |
| 45 |
|
| 46 |
static int xdp_umem_addr_map(struct xdp_umem *umem, struct page **pages, |
| 47 |
u32 nr_pages) |
| 48 |
{ |
| 49 |
umem->addrs = vmap(pages, nr_pages, VM_MAP, PAGE_KERNEL); |
| 50 |
if (!umem->addrs) |
| 51 |
return -ENOMEM; |
| 52 |
return 0; |
| 53 |
} |
| 54 |
|
| 55 |
static void xdp_umem_release(struct xdp_umem *umem) |
| 56 |
{ |
| 57 |
umem->zc = false; |
| 58 |
ida_free(&umem_ida, umem->id); |
| 59 |
|
| 60 |
xdp_umem_addr_unmap(umem); |
| 61 |
xdp_umem_unpin_pages(umem); |
| 62 |
|
| 63 |
xdp_umem_unaccount_pages(umem); |
| 64 |
kfree(umem); |
| 65 |
} |
| 66 |
|
| 67 |
static void xdp_umem_release_deferred(struct work_struct *work) |
| 68 |
{ |
| 69 |
struct xdp_umem *umem = container_of(work, struct xdp_umem, work); |
| 70 |
|
| 71 |
xdp_umem_release(umem); |
| 72 |
} |
| 73 |
|
| 74 |
void xdp_get_umem(struct xdp_umem *umem) |
| 75 |
{ |
| 76 |
refcount_inc(&umem->users); |
| 77 |
} |
| 78 |
|
| 79 |
void xdp_put_umem(struct xdp_umem *umem, bool defer_cleanup) |
| 80 |
{ |
| 81 |
if (!umem) |
| 82 |
return; |
| 83 |
|
| 84 |
if (refcount_dec_and_test(&umem->users)) { |
| 85 |
if (defer_cleanup) { |
| 86 |
INIT_WORK(&umem->work, xdp_umem_release_deferred); |
| 87 |
schedule_work(&umem->work); |
| 88 |
} else { |
| 89 |
xdp_umem_release(umem); |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
static int xdp_umem_pin_pages(struct xdp_umem *umem, unsigned long address) |
| 95 |
{ |
| 96 |
unsigned int gup_flags = FOLL_WRITE; |
| 97 |
long npgs; |
| 98 |
int err; |
| 99 |
|
| 100 |
umem->pgs = kvzalloc_objs(*umem->pgs, umem->npgs, |
| 101 |
GFP_KERNEL | __GFP_NOWARN); |
| 102 |
if (!umem->pgs) |
| 103 |
return -ENOMEM; |
| 104 |
|
| 105 |
mmap_read_lock(current->mm); |
| 106 |
npgs = pin_user_pages(address, umem->npgs, |
| 107 |
gup_flags | FOLL_LONGTERM, &umem->pgs[0]); |
| 108 |
mmap_read_unlock(current->mm); |
| 109 |
|
| 110 |
if (npgs != umem->npgs) { |
| 111 |
if (npgs >= 0) { |
| 112 |
umem->npgs = npgs; |
| 113 |
err = -ENOMEM; |
| 114 |
goto out_pin; |
| 115 |
} |
| 116 |
err = npgs; |
| 117 |
goto out_pgs; |
| 118 |
} |
| 119 |
return 0; |
| 120 |
|
| 121 |
out_pin: |
| 122 |
xdp_umem_unpin_pages(umem); |
| 123 |
out_pgs: |
| 124 |
kvfree(umem->pgs); |
| 125 |
umem->pgs = NULL; |
| 126 |
return err; |
| 127 |
} |
| 128 |
|
| 129 |
static int xdp_umem_account_pages(struct xdp_umem *umem) |
| 130 |
{ |
| 131 |
unsigned long lock_limit, new_npgs, old_npgs; |
| 132 |
|
| 133 |
if (capable(CAP_IPC_LOCK)) |
| 134 |
return 0; |
| 135 |
|
| 136 |
lock_limit = rlimit(RLIMIT_MEMLOCK) >> PAGE_SHIFT; |
| 137 |
umem->user = get_uid(current_user()); |
| 138 |
|
| 139 |
do { |
| 140 |
old_npgs = atomic_long_read(&umem->user->locked_vm); |
| 141 |
new_npgs = old_npgs + umem->npgs; |
| 142 |
if (new_npgs > lock_limit) { |
| 143 |
free_uid(umem->user); |
| 144 |
umem->user = NULL; |
| 145 |
return -ENOBUFS; |
| 146 |
} |
| 147 |
} while (atomic_long_cmpxchg(&umem->user->locked_vm, old_npgs, |
| 148 |
new_npgs) != old_npgs); |
| 149 |
return 0; |
| 150 |
} |
| 151 |
|
| 152 |
#define XDP_UMEM_FLAGS_VALID ( \ |
| 153 |
XDP_UMEM_UNALIGNED_CHUNK_FLAG | \ |
| 154 |
XDP_UMEM_TX_SW_CSUM | \ |
| 155 |
XDP_UMEM_TX_METADATA_LEN | \ |
| 156 |
0) |
| 157 |
|
| 158 |
static int xdp_umem_reg(struct xdp_umem *umem, struct xdp_umem_reg *mr) |
| 159 |
{ |
| 160 |
bool unaligned_chunks = mr->flags & XDP_UMEM_UNALIGNED_CHUNK_FLAG; |
| 161 |
u32 chunk_size = mr->chunk_size, headroom = mr->headroom; |
| 162 |
u64 addr = mr->addr, size = mr->len; |
| 163 |
u32 chunks_rem, npgs_rem; |
| 164 |
u64 chunks, npgs; |
| 165 |
int err; |
| 166 |
|
| 167 |
if (chunk_size < XDP_UMEM_MIN_CHUNK_SIZE || chunk_size > PAGE_SIZE) { |
| 168 |
/* Strictly speaking we could support this, if: |
| 169 |
* - huge pages, or* |
| 170 |
* - using an IOMMU, or |
| 171 |
* - making sure the memory area is consecutive |
| 172 |
* but for now, we simply say "computer says no". |
| 173 |
*/ |
| 174 |
return -EINVAL; |
| 175 |
} |
| 176 |
|
| 177 |
if (mr->flags & ~XDP_UMEM_FLAGS_VALID) |
| 178 |
return -EINVAL; |
| 179 |
|
| 180 |
if (!unaligned_chunks && !is_power_of_2(chunk_size)) |
| 181 |
return -EINVAL; |
| 182 |
|
| 183 |
if (!PAGE_ALIGNED(addr)) { |
| 184 |
/* Memory area has to be page size aligned. For |
| 185 |
* simplicity, this might change. |
| 186 |
*/ |
| 187 |
return -EINVAL; |
| 188 |
} |
| 189 |
|
| 190 |
if ((addr + size) < addr) |
| 191 |
return -EINVAL; |
| 192 |
|
| 193 |
npgs = div_u64_rem(size, PAGE_SIZE, &npgs_rem); |
| 194 |
if (npgs_rem) |
| 195 |
npgs++; |
| 196 |
if (npgs > U32_MAX) |
| 197 |
return -EINVAL; |
| 198 |
|
| 199 |
chunks = div_u64_rem(size, chunk_size, &chunks_rem); |
| 200 |
if (!chunks || chunks > U32_MAX) |
| 201 |
return -EINVAL; |
| 202 |
|
| 203 |
if (!unaligned_chunks && chunks_rem) |
| 204 |
return -EINVAL; |
| 205 |
|
| 206 |
if (headroom > chunk_size - XDP_PACKET_HEADROOM - |
| 207 |
SKB_DATA_ALIGN(sizeof(struct skb_shared_info)) - 128) |
| 208 |
return -EINVAL; |
| 209 |
|
| 210 |
if (mr->flags & XDP_UMEM_TX_METADATA_LEN) { |
| 211 |
if (mr->tx_metadata_len >= 256 || mr->tx_metadata_len % 8) |
| 212 |
return -EINVAL; |
| 213 |
umem->tx_metadata_len = mr->tx_metadata_len; |
| 214 |
} |
| 215 |
|
| 216 |
umem->size = size; |
| 217 |
umem->headroom = headroom; |
| 218 |
umem->chunk_size = chunk_size; |
| 219 |
umem->chunks = chunks; |
| 220 |
umem->npgs = npgs; |
| 221 |
umem->pgs = NULL; |
| 222 |
umem->user = NULL; |
| 223 |
umem->flags = mr->flags; |
| 224 |
|
| 225 |
INIT_LIST_HEAD(&umem->xsk_dma_list); |
| 226 |
refcount_set(&umem->users, 1); |
| 227 |
|
| 228 |
err = xdp_umem_account_pages(umem); |
| 229 |
if (err) |
| 230 |
return err; |
| 231 |
|
| 232 |
err = xdp_umem_pin_pages(umem, (unsigned long)addr); |
| 233 |
if (err) |
| 234 |
goto out_account; |
| 235 |
|
| 236 |
err = xdp_umem_addr_map(umem, umem->pgs, umem->npgs); |
| 237 |
if (err) |
| 238 |
goto out_unpin; |
| 239 |
|
| 240 |
return 0; |
| 241 |
|
| 242 |
out_unpin: |
| 243 |
xdp_umem_unpin_pages(umem); |
| 244 |
out_account: |
| 245 |
xdp_umem_unaccount_pages(umem); |
| 246 |
return err; |
| 247 |
} |
| 248 |
|
| 249 |
struct xdp_umem *xdp_umem_create(struct xdp_umem_reg *mr) |
| 250 |
{ |
| 251 |
struct xdp_umem *umem; |
| 252 |
int err; |
| 253 |
|
| 254 |
umem = kzalloc_obj(*umem); |
| 255 |
if (!umem) |
| 256 |
return ERR_PTR(-ENOMEM); |
| 257 |
|
| 258 |
err = ida_alloc(&umem_ida, GFP_KERNEL); |
| 259 |
if (err < 0) { |
| 260 |
kfree(umem); |
| 261 |
return ERR_PTR(err); |
| 262 |
} |
| 263 |
umem->id = err; |
| 264 |
|
| 265 |
err = xdp_umem_reg(umem, mr); |
| 266 |
if (err) { |
| 267 |
ida_free(&umem_ida, umem->id); |
| 268 |
kfree(umem); |
| 269 |
return ERR_PTR(err); |
| 270 |
} |
| 271 |
|
| 272 |
return umem; |
| 273 |
} |