b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* net/core/xdp.c |
| 3 | * |
| 4 | * Copyright (c) 2017 Jesper Dangaard Brouer, Red Hat Inc. |
| 5 | */ |
| 6 | #include <linux/bpf.h> |
| 7 | #include <linux/filter.h> |
| 8 | #include <linux/types.h> |
| 9 | #include <linux/mm.h> |
| 10 | #include <linux/netdevice.h> |
| 11 | #include <linux/slab.h> |
| 12 | #include <linux/idr.h> |
| 13 | #include <linux/rhashtable.h> |
| 14 | #include <net/page_pool.h> |
| 15 | |
| 16 | #include <net/xdp.h> |
| 17 | #include <net/xdp_priv.h> /* struct xdp_mem_allocator */ |
| 18 | #include <trace/events/xdp.h> |
| 19 | |
| 20 | #define REG_STATE_NEW 0x0 |
| 21 | #define REG_STATE_REGISTERED 0x1 |
| 22 | #define REG_STATE_UNREGISTERED 0x2 |
| 23 | #define REG_STATE_UNUSED 0x3 |
| 24 | |
| 25 | static DEFINE_IDA(mem_id_pool); |
| 26 | static DEFINE_MUTEX(mem_id_lock); |
| 27 | #define MEM_ID_MAX 0xFFFE |
| 28 | #define MEM_ID_MIN 1 |
| 29 | static int mem_id_next = MEM_ID_MIN; |
| 30 | |
| 31 | static bool mem_id_init; /* false */ |
| 32 | static struct rhashtable *mem_id_ht; |
| 33 | |
| 34 | static u32 xdp_mem_id_hashfn(const void *data, u32 len, u32 seed) |
| 35 | { |
| 36 | const u32 *k = data; |
| 37 | const u32 key = *k; |
| 38 | |
| 39 | BUILD_BUG_ON(FIELD_SIZEOF(struct xdp_mem_allocator, mem.id) |
| 40 | != sizeof(u32)); |
| 41 | |
| 42 | /* Use cyclic increasing ID as direct hash key */ |
| 43 | return key; |
| 44 | } |
| 45 | |
| 46 | static int xdp_mem_id_cmp(struct rhashtable_compare_arg *arg, |
| 47 | const void *ptr) |
| 48 | { |
| 49 | const struct xdp_mem_allocator *xa = ptr; |
| 50 | u32 mem_id = *(u32 *)arg->key; |
| 51 | |
| 52 | return xa->mem.id != mem_id; |
| 53 | } |
| 54 | |
| 55 | static const struct rhashtable_params mem_id_rht_params = { |
| 56 | .nelem_hint = 64, |
| 57 | .head_offset = offsetof(struct xdp_mem_allocator, node), |
| 58 | .key_offset = offsetof(struct xdp_mem_allocator, mem.id), |
| 59 | .key_len = FIELD_SIZEOF(struct xdp_mem_allocator, mem.id), |
| 60 | .max_size = MEM_ID_MAX, |
| 61 | .min_size = 8, |
| 62 | .automatic_shrinking = true, |
| 63 | .hashfn = xdp_mem_id_hashfn, |
| 64 | .obj_cmpfn = xdp_mem_id_cmp, |
| 65 | }; |
| 66 | |
| 67 | static void __xdp_mem_allocator_rcu_free(struct rcu_head *rcu) |
| 68 | { |
| 69 | struct xdp_mem_allocator *xa; |
| 70 | |
| 71 | xa = container_of(rcu, struct xdp_mem_allocator, rcu); |
| 72 | |
| 73 | /* Allow this ID to be reused */ |
| 74 | ida_simple_remove(&mem_id_pool, xa->mem.id); |
| 75 | |
| 76 | /* Poison memory */ |
| 77 | xa->mem.id = 0xFFFF; |
| 78 | xa->mem.type = 0xF0F0; |
| 79 | xa->allocator = (void *)0xDEAD9001; |
| 80 | |
| 81 | kfree(xa); |
| 82 | } |
| 83 | |
| 84 | static void mem_xa_remove(struct xdp_mem_allocator *xa) |
| 85 | { |
| 86 | trace_mem_disconnect(xa); |
| 87 | |
| 88 | if (!rhashtable_remove_fast(mem_id_ht, &xa->node, mem_id_rht_params)) |
| 89 | call_rcu(&xa->rcu, __xdp_mem_allocator_rcu_free); |
| 90 | } |
| 91 | |
| 92 | static void mem_allocator_disconnect(void *allocator) |
| 93 | { |
| 94 | struct xdp_mem_allocator *xa; |
| 95 | struct rhashtable_iter iter; |
| 96 | |
| 97 | mutex_lock(&mem_id_lock); |
| 98 | |
| 99 | rhashtable_walk_enter(mem_id_ht, &iter); |
| 100 | do { |
| 101 | rhashtable_walk_start(&iter); |
| 102 | |
| 103 | while ((xa = rhashtable_walk_next(&iter)) && !IS_ERR(xa)) { |
| 104 | if (xa->allocator == allocator) |
| 105 | mem_xa_remove(xa); |
| 106 | } |
| 107 | |
| 108 | rhashtable_walk_stop(&iter); |
| 109 | |
| 110 | } while (xa == ERR_PTR(-EAGAIN)); |
| 111 | rhashtable_walk_exit(&iter); |
| 112 | |
| 113 | mutex_unlock(&mem_id_lock); |
| 114 | } |
| 115 | |
| 116 | static void mem_id_disconnect(int id) |
| 117 | { |
| 118 | struct xdp_mem_allocator *xa; |
| 119 | |
| 120 | mutex_lock(&mem_id_lock); |
| 121 | |
| 122 | xa = rhashtable_lookup_fast(mem_id_ht, &id, mem_id_rht_params); |
| 123 | if (!xa) { |
| 124 | mutex_unlock(&mem_id_lock); |
| 125 | WARN(1, "Request remove non-existing id(%d), driver bug?", id); |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | trace_mem_disconnect(xa); |
| 130 | |
| 131 | if (!rhashtable_remove_fast(mem_id_ht, &xa->node, mem_id_rht_params)) |
| 132 | call_rcu(&xa->rcu, __xdp_mem_allocator_rcu_free); |
| 133 | |
| 134 | mutex_unlock(&mem_id_lock); |
| 135 | } |
| 136 | |
| 137 | void xdp_rxq_info_unreg_mem_model(struct xdp_rxq_info *xdp_rxq) |
| 138 | { |
| 139 | struct xdp_mem_allocator *xa; |
| 140 | int id = xdp_rxq->mem.id; |
| 141 | |
| 142 | if (xdp_rxq->reg_state != REG_STATE_REGISTERED) { |
| 143 | WARN(1, "Missing register, driver bug"); |
| 144 | return; |
| 145 | } |
| 146 | |
| 147 | if (id == 0) |
| 148 | return; |
| 149 | |
| 150 | if (xdp_rxq->mem.type == MEM_TYPE_ZERO_COPY) |
| 151 | return mem_id_disconnect(id); |
| 152 | |
| 153 | if (xdp_rxq->mem.type == MEM_TYPE_PAGE_POOL) { |
| 154 | rcu_read_lock(); |
| 155 | xa = rhashtable_lookup(mem_id_ht, &id, mem_id_rht_params); |
| 156 | page_pool_destroy(xa->page_pool); |
| 157 | rcu_read_unlock(); |
| 158 | } |
| 159 | } |
| 160 | EXPORT_SYMBOL_GPL(xdp_rxq_info_unreg_mem_model); |
| 161 | |
| 162 | void xdp_rxq_info_unreg(struct xdp_rxq_info *xdp_rxq) |
| 163 | { |
| 164 | /* Simplify driver cleanup code paths, allow unreg "unused" */ |
| 165 | if (xdp_rxq->reg_state == REG_STATE_UNUSED) |
| 166 | return; |
| 167 | |
| 168 | WARN(!(xdp_rxq->reg_state == REG_STATE_REGISTERED), "Driver BUG"); |
| 169 | |
| 170 | xdp_rxq_info_unreg_mem_model(xdp_rxq); |
| 171 | |
| 172 | xdp_rxq->reg_state = REG_STATE_UNREGISTERED; |
| 173 | xdp_rxq->dev = NULL; |
| 174 | |
| 175 | /* Reset mem info to defaults */ |
| 176 | xdp_rxq->mem.id = 0; |
| 177 | xdp_rxq->mem.type = 0; |
| 178 | } |
| 179 | EXPORT_SYMBOL_GPL(xdp_rxq_info_unreg); |
| 180 | |
| 181 | static void xdp_rxq_info_init(struct xdp_rxq_info *xdp_rxq) |
| 182 | { |
| 183 | memset(xdp_rxq, 0, sizeof(*xdp_rxq)); |
| 184 | } |
| 185 | |
| 186 | /* Returns 0 on success, negative on failure */ |
| 187 | int xdp_rxq_info_reg(struct xdp_rxq_info *xdp_rxq, |
| 188 | struct net_device *dev, u32 queue_index) |
| 189 | { |
| 190 | if (xdp_rxq->reg_state == REG_STATE_UNUSED) { |
| 191 | WARN(1, "Driver promised not to register this"); |
| 192 | return -EINVAL; |
| 193 | } |
| 194 | |
| 195 | if (xdp_rxq->reg_state == REG_STATE_REGISTERED) { |
| 196 | WARN(1, "Missing unregister, handled but fix driver"); |
| 197 | xdp_rxq_info_unreg(xdp_rxq); |
| 198 | } |
| 199 | |
| 200 | if (!dev) { |
| 201 | WARN(1, "Missing net_device from driver"); |
| 202 | return -ENODEV; |
| 203 | } |
| 204 | |
| 205 | /* State either UNREGISTERED or NEW */ |
| 206 | xdp_rxq_info_init(xdp_rxq); |
| 207 | xdp_rxq->dev = dev; |
| 208 | xdp_rxq->queue_index = queue_index; |
| 209 | |
| 210 | xdp_rxq->reg_state = REG_STATE_REGISTERED; |
| 211 | return 0; |
| 212 | } |
| 213 | EXPORT_SYMBOL_GPL(xdp_rxq_info_reg); |
| 214 | |
| 215 | void xdp_rxq_info_unused(struct xdp_rxq_info *xdp_rxq) |
| 216 | { |
| 217 | xdp_rxq->reg_state = REG_STATE_UNUSED; |
| 218 | } |
| 219 | EXPORT_SYMBOL_GPL(xdp_rxq_info_unused); |
| 220 | |
| 221 | bool xdp_rxq_info_is_reg(struct xdp_rxq_info *xdp_rxq) |
| 222 | { |
| 223 | return (xdp_rxq->reg_state == REG_STATE_REGISTERED); |
| 224 | } |
| 225 | EXPORT_SYMBOL_GPL(xdp_rxq_info_is_reg); |
| 226 | |
| 227 | static int __mem_id_init_hash_table(void) |
| 228 | { |
| 229 | struct rhashtable *rht; |
| 230 | int ret; |
| 231 | |
| 232 | if (unlikely(mem_id_init)) |
| 233 | return 0; |
| 234 | |
| 235 | rht = kzalloc(sizeof(*rht), GFP_KERNEL); |
| 236 | if (!rht) |
| 237 | return -ENOMEM; |
| 238 | |
| 239 | ret = rhashtable_init(rht, &mem_id_rht_params); |
| 240 | if (ret < 0) { |
| 241 | kfree(rht); |
| 242 | return ret; |
| 243 | } |
| 244 | mem_id_ht = rht; |
| 245 | smp_mb(); /* mutex lock should provide enough pairing */ |
| 246 | mem_id_init = true; |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | /* Allocate a cyclic ID that maps to allocator pointer. |
| 252 | * See: https://www.kernel.org/doc/html/latest/core-api/idr.html |
| 253 | * |
| 254 | * Caller must lock mem_id_lock. |
| 255 | */ |
| 256 | static int __mem_id_cyclic_get(gfp_t gfp) |
| 257 | { |
| 258 | int retries = 1; |
| 259 | int id; |
| 260 | |
| 261 | again: |
| 262 | id = ida_simple_get(&mem_id_pool, mem_id_next, MEM_ID_MAX, gfp); |
| 263 | if (id < 0) { |
| 264 | if (id == -ENOSPC) { |
| 265 | /* Cyclic allocator, reset next id */ |
| 266 | if (retries--) { |
| 267 | mem_id_next = MEM_ID_MIN; |
| 268 | goto again; |
| 269 | } |
| 270 | } |
| 271 | return id; /* errno */ |
| 272 | } |
| 273 | mem_id_next = id + 1; |
| 274 | |
| 275 | return id; |
| 276 | } |
| 277 | |
| 278 | static bool __is_supported_mem_type(enum xdp_mem_type type) |
| 279 | { |
| 280 | if (type == MEM_TYPE_PAGE_POOL) |
| 281 | return is_page_pool_compiled_in(); |
| 282 | |
| 283 | if (type >= MEM_TYPE_MAX) |
| 284 | return false; |
| 285 | |
| 286 | return true; |
| 287 | } |
| 288 | |
| 289 | int xdp_rxq_info_reg_mem_model(struct xdp_rxq_info *xdp_rxq, |
| 290 | enum xdp_mem_type type, void *allocator) |
| 291 | { |
| 292 | struct xdp_mem_allocator *xdp_alloc; |
| 293 | gfp_t gfp = GFP_KERNEL; |
| 294 | int id, errno, ret; |
| 295 | void *ptr; |
| 296 | |
| 297 | if (xdp_rxq->reg_state != REG_STATE_REGISTERED) { |
| 298 | WARN(1, "Missing register, driver bug"); |
| 299 | return -EFAULT; |
| 300 | } |
| 301 | |
| 302 | if (!__is_supported_mem_type(type)) |
| 303 | return -EOPNOTSUPP; |
| 304 | |
| 305 | xdp_rxq->mem.type = type; |
| 306 | |
| 307 | if (!allocator) { |
| 308 | if (type == MEM_TYPE_PAGE_POOL || type == MEM_TYPE_ZERO_COPY) |
| 309 | return -EINVAL; /* Setup time check page_pool req */ |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | /* Delay init of rhashtable to save memory if feature isn't used */ |
| 314 | if (!mem_id_init) { |
| 315 | mutex_lock(&mem_id_lock); |
| 316 | ret = __mem_id_init_hash_table(); |
| 317 | mutex_unlock(&mem_id_lock); |
| 318 | if (ret < 0) { |
| 319 | WARN_ON(1); |
| 320 | return ret; |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | xdp_alloc = kzalloc(sizeof(*xdp_alloc), gfp); |
| 325 | if (!xdp_alloc) |
| 326 | return -ENOMEM; |
| 327 | |
| 328 | mutex_lock(&mem_id_lock); |
| 329 | id = __mem_id_cyclic_get(gfp); |
| 330 | if (id < 0) { |
| 331 | errno = id; |
| 332 | goto err; |
| 333 | } |
| 334 | xdp_rxq->mem.id = id; |
| 335 | xdp_alloc->mem = xdp_rxq->mem; |
| 336 | xdp_alloc->allocator = allocator; |
| 337 | |
| 338 | /* Insert allocator into ID lookup table */ |
| 339 | ptr = rhashtable_insert_slow(mem_id_ht, &id, &xdp_alloc->node); |
| 340 | if (IS_ERR(ptr)) { |
| 341 | ida_simple_remove(&mem_id_pool, xdp_rxq->mem.id); |
| 342 | xdp_rxq->mem.id = 0; |
| 343 | errno = PTR_ERR(ptr); |
| 344 | goto err; |
| 345 | } |
| 346 | |
| 347 | if (type == MEM_TYPE_PAGE_POOL) |
| 348 | page_pool_use_xdp_mem(allocator, mem_allocator_disconnect); |
| 349 | |
| 350 | mutex_unlock(&mem_id_lock); |
| 351 | |
| 352 | trace_mem_connect(xdp_alloc, xdp_rxq); |
| 353 | return 0; |
| 354 | err: |
| 355 | mutex_unlock(&mem_id_lock); |
| 356 | kfree(xdp_alloc); |
| 357 | return errno; |
| 358 | } |
| 359 | EXPORT_SYMBOL_GPL(xdp_rxq_info_reg_mem_model); |
| 360 | |
| 361 | /* XDP RX runs under NAPI protection, and in different delivery error |
| 362 | * scenarios (e.g. queue full), it is possible to return the xdp_frame |
| 363 | * while still leveraging this protection. The @napi_direct boolian |
| 364 | * is used for those calls sites. Thus, allowing for faster recycling |
| 365 | * of xdp_frames/pages in those cases. |
| 366 | */ |
| 367 | static void __xdp_return(void *data, struct xdp_mem_info *mem, bool napi_direct, |
| 368 | unsigned long handle) |
| 369 | { |
| 370 | struct xdp_mem_allocator *xa; |
| 371 | struct page *page; |
| 372 | |
| 373 | switch (mem->type) { |
| 374 | case MEM_TYPE_PAGE_POOL: |
| 375 | rcu_read_lock(); |
| 376 | /* mem->id is valid, checked in xdp_rxq_info_reg_mem_model() */ |
| 377 | xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params); |
| 378 | page = virt_to_head_page(data); |
| 379 | napi_direct &= !xdp_return_frame_no_direct(); |
| 380 | page_pool_put_page(xa->page_pool, page, napi_direct); |
| 381 | rcu_read_unlock(); |
| 382 | break; |
| 383 | case MEM_TYPE_PAGE_SHARED: |
| 384 | page_frag_free(data); |
| 385 | break; |
| 386 | case MEM_TYPE_PAGE_ORDER0: |
| 387 | page = virt_to_page(data); /* Assumes order0 page*/ |
| 388 | put_page(page); |
| 389 | break; |
| 390 | case MEM_TYPE_ZERO_COPY: |
| 391 | /* NB! Only valid from an xdp_buff! */ |
| 392 | rcu_read_lock(); |
| 393 | /* mem->id is valid, checked in xdp_rxq_info_reg_mem_model() */ |
| 394 | xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params); |
| 395 | xa->zc_alloc->free(xa->zc_alloc, handle); |
| 396 | rcu_read_unlock(); |
| 397 | default: |
| 398 | /* Not possible, checked in xdp_rxq_info_reg_mem_model() */ |
| 399 | break; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | void xdp_return_frame(struct xdp_frame *xdpf) |
| 404 | { |
| 405 | __xdp_return(xdpf->data, &xdpf->mem, false, 0); |
| 406 | } |
| 407 | EXPORT_SYMBOL_GPL(xdp_return_frame); |
| 408 | |
| 409 | void xdp_return_frame_rx_napi(struct xdp_frame *xdpf) |
| 410 | { |
| 411 | __xdp_return(xdpf->data, &xdpf->mem, true, 0); |
| 412 | } |
| 413 | EXPORT_SYMBOL_GPL(xdp_return_frame_rx_napi); |
| 414 | |
| 415 | void xdp_return_buff(struct xdp_buff *xdp) |
| 416 | { |
| 417 | __xdp_return(xdp->data, &xdp->rxq->mem, true, xdp->handle); |
| 418 | } |
| 419 | EXPORT_SYMBOL_GPL(xdp_return_buff); |
| 420 | |
| 421 | /* Only called for MEM_TYPE_PAGE_POOL see xdp.h */ |
| 422 | void __xdp_release_frame(void *data, struct xdp_mem_info *mem) |
| 423 | { |
| 424 | struct xdp_mem_allocator *xa; |
| 425 | struct page *page; |
| 426 | |
| 427 | rcu_read_lock(); |
| 428 | xa = rhashtable_lookup(mem_id_ht, &mem->id, mem_id_rht_params); |
| 429 | page = virt_to_head_page(data); |
| 430 | if (xa) |
| 431 | page_pool_release_page(xa->page_pool, page); |
| 432 | rcu_read_unlock(); |
| 433 | } |
| 434 | EXPORT_SYMBOL_GPL(__xdp_release_frame); |
| 435 | |
| 436 | int xdp_attachment_query(struct xdp_attachment_info *info, |
| 437 | struct netdev_bpf *bpf) |
| 438 | { |
| 439 | bpf->prog_id = info->prog ? info->prog->aux->id : 0; |
| 440 | bpf->prog_flags = info->prog ? info->flags : 0; |
| 441 | return 0; |
| 442 | } |
| 443 | EXPORT_SYMBOL_GPL(xdp_attachment_query); |
| 444 | |
| 445 | bool xdp_attachment_flags_ok(struct xdp_attachment_info *info, |
| 446 | struct netdev_bpf *bpf) |
| 447 | { |
| 448 | if (info->prog && (bpf->flags ^ info->flags) & XDP_FLAGS_MODES) { |
| 449 | NL_SET_ERR_MSG(bpf->extack, |
| 450 | "program loaded with different flags"); |
| 451 | return false; |
| 452 | } |
| 453 | return true; |
| 454 | } |
| 455 | EXPORT_SYMBOL_GPL(xdp_attachment_flags_ok); |
| 456 | |
| 457 | void xdp_attachment_setup(struct xdp_attachment_info *info, |
| 458 | struct netdev_bpf *bpf) |
| 459 | { |
| 460 | if (info->prog) |
| 461 | bpf_prog_put(info->prog); |
| 462 | info->prog = bpf->prog; |
| 463 | info->flags = bpf->flags; |
| 464 | } |
| 465 | EXPORT_SYMBOL_GPL(xdp_attachment_setup); |
| 466 | |
| 467 | struct xdp_frame *xdp_convert_zc_to_xdp_frame(struct xdp_buff *xdp) |
| 468 | { |
| 469 | unsigned int metasize, totsize; |
| 470 | void *addr, *data_to_copy; |
| 471 | struct xdp_frame *xdpf; |
| 472 | struct page *page; |
| 473 | |
| 474 | /* Clone into a MEM_TYPE_PAGE_ORDER0 xdp_frame. */ |
| 475 | metasize = xdp_data_meta_unsupported(xdp) ? 0 : |
| 476 | xdp->data - xdp->data_meta; |
| 477 | totsize = xdp->data_end - xdp->data + metasize; |
| 478 | |
| 479 | if (sizeof(*xdpf) + totsize > PAGE_SIZE) |
| 480 | return NULL; |
| 481 | |
| 482 | page = dev_alloc_page(); |
| 483 | if (!page) |
| 484 | return NULL; |
| 485 | |
| 486 | addr = page_to_virt(page); |
| 487 | xdpf = addr; |
| 488 | memset(xdpf, 0, sizeof(*xdpf)); |
| 489 | |
| 490 | addr += sizeof(*xdpf); |
| 491 | data_to_copy = metasize ? xdp->data_meta : xdp->data; |
| 492 | memcpy(addr, data_to_copy, totsize); |
| 493 | |
| 494 | xdpf->data = addr + metasize; |
| 495 | xdpf->len = totsize - metasize; |
| 496 | xdpf->headroom = 0; |
| 497 | xdpf->metasize = metasize; |
| 498 | xdpf->mem.type = MEM_TYPE_PAGE_ORDER0; |
| 499 | |
| 500 | xdp_return_buff(xdp); |
| 501 | return xdpf; |
| 502 | } |
| 503 | EXPORT_SYMBOL_GPL(xdp_convert_zc_to_xdp_frame); |