bpf_arena_common.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */
  2. /* Copyright (c) 2024 Meta Platforms, Inc. and affiliates. */
  3. #pragma once
  4. #ifndef WRITE_ONCE
  5. #define WRITE_ONCE(x, val) ((*(volatile typeof(x) *) &(x)) = (val))
  6. #endif
  7. #ifndef NUMA_NO_NODE
  8. #define NUMA_NO_NODE (-1)
  9. #endif
  10. #ifndef arena_container_of
  11. #define arena_container_of(ptr, type, member) \
  12. ({ \
  13. void __arena *__mptr = (void __arena *)(ptr); \
  14. ((type *)(__mptr - offsetof(type, member))); \
  15. })
  16. #endif
  17. #ifdef __BPF__ /* when compiled as bpf program */
  18. #ifndef PAGE_SIZE
  19. #define PAGE_SIZE __PAGE_SIZE
  20. /*
  21. * for older kernels try sizeof(struct genradix_node)
  22. * or flexible:
  23. * static inline long __bpf_page_size(void) {
  24. * return bpf_core_enum_value(enum page_size_enum___l, __PAGE_SIZE___l) ?: sizeof(struct genradix_node);
  25. * }
  26. * but generated code is not great.
  27. */
  28. #endif
  29. #if defined(__BPF_FEATURE_ADDR_SPACE_CAST) && !defined(BPF_ARENA_FORCE_ASM)
  30. #define __arena __attribute__((address_space(1)))
  31. #define __arena_global __attribute__((address_space(1)))
  32. #define cast_kern(ptr) /* nop for bpf prog. emitted by LLVM */
  33. #define cast_user(ptr) /* nop for bpf prog. emitted by LLVM */
  34. #else
  35. #define __arena
  36. #define __arena_global SEC(".addr_space.1")
  37. #define cast_kern(ptr) bpf_addr_space_cast(ptr, 0, 1)
  38. #define cast_user(ptr) bpf_addr_space_cast(ptr, 1, 0)
  39. #endif
  40. void __arena* bpf_arena_alloc_pages(void *map, void __arena *addr, __u32 page_cnt,
  41. int node_id, __u64 flags) __ksym __weak;
  42. void bpf_arena_free_pages(void *map, void __arena *ptr, __u32 page_cnt) __ksym __weak;
  43. #else /* when compiled as user space code */
  44. #define __arena
  45. #define __arg_arena
  46. #define cast_kern(ptr) /* nop for user space */
  47. #define cast_user(ptr) /* nop for user space */
  48. __weak char arena[1];
  49. #ifndef offsetof
  50. #define offsetof(type, member) ((unsigned long)&((type *)0)->member)
  51. #endif
  52. static inline void __arena* bpf_arena_alloc_pages(void *map, void *addr, __u32 page_cnt,
  53. int node_id, __u64 flags)
  54. {
  55. return NULL;
  56. }
  57. static inline void bpf_arena_free_pages(void *map, void __arena *ptr, __u32 page_cnt)
  58. {
  59. }
  60. #endif