compat.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. /*
  3. * Copyright (c) 2024 Meta Platforms, Inc. and affiliates.
  4. * Copyright (c) 2024 Tejun Heo <tj@kernel.org>
  5. * Copyright (c) 2024 David Vernet <dvernet@meta.com>
  6. */
  7. #ifndef __SCX_COMPAT_H
  8. #define __SCX_COMPAT_H
  9. #include <bpf/btf.h>
  10. #include <fcntl.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. struct btf *__COMPAT_vmlinux_btf __attribute__((weak));
  14. static inline void __COMPAT_load_vmlinux_btf(void)
  15. {
  16. if (!__COMPAT_vmlinux_btf) {
  17. __COMPAT_vmlinux_btf = btf__load_vmlinux_btf();
  18. SCX_BUG_ON(!__COMPAT_vmlinux_btf, "btf__load_vmlinux_btf()");
  19. }
  20. }
  21. static inline bool __COMPAT_read_enum(const char *type, const char *name, u64 *v)
  22. {
  23. const struct btf_type *t;
  24. const char *n;
  25. s32 tid;
  26. int i;
  27. __COMPAT_load_vmlinux_btf();
  28. tid = btf__find_by_name(__COMPAT_vmlinux_btf, type);
  29. if (tid < 0)
  30. return false;
  31. t = btf__type_by_id(__COMPAT_vmlinux_btf, tid);
  32. SCX_BUG_ON(!t, "btf__type_by_id(%d)", tid);
  33. if (btf_is_enum(t)) {
  34. struct btf_enum *e = btf_enum(t);
  35. for (i = 0; i < BTF_INFO_VLEN(t->info); i++) {
  36. n = btf__name_by_offset(__COMPAT_vmlinux_btf, e[i].name_off);
  37. SCX_BUG_ON(!n, "btf__name_by_offset()");
  38. if (!strcmp(n, name)) {
  39. *v = e[i].val;
  40. return true;
  41. }
  42. }
  43. } else if (btf_is_enum64(t)) {
  44. struct btf_enum64 *e = btf_enum64(t);
  45. for (i = 0; i < BTF_INFO_VLEN(t->info); i++) {
  46. n = btf__name_by_offset(__COMPAT_vmlinux_btf, e[i].name_off);
  47. SCX_BUG_ON(!n, "btf__name_by_offset()");
  48. if (!strcmp(n, name)) {
  49. *v = btf_enum64_value(&e[i]);
  50. return true;
  51. }
  52. }
  53. }
  54. return false;
  55. }
  56. #define __COMPAT_ENUM_OR_ZERO(__type, __ent) \
  57. ({ \
  58. u64 __val = 0; \
  59. __COMPAT_read_enum(__type, __ent, &__val); \
  60. __val; \
  61. })
  62. static inline bool __COMPAT_has_ksym(const char *ksym)
  63. {
  64. __COMPAT_load_vmlinux_btf();
  65. return btf__find_by_name(__COMPAT_vmlinux_btf, ksym) >= 0;
  66. }
  67. static inline bool __COMPAT_struct_has_field(const char *type, const char *field)
  68. {
  69. const struct btf_type *t;
  70. const struct btf_member *m;
  71. const char *n;
  72. s32 tid;
  73. int i;
  74. __COMPAT_load_vmlinux_btf();
  75. tid = btf__find_by_name_kind(__COMPAT_vmlinux_btf, type, BTF_KIND_STRUCT);
  76. if (tid < 0)
  77. return false;
  78. t = btf__type_by_id(__COMPAT_vmlinux_btf, tid);
  79. SCX_BUG_ON(!t, "btf__type_by_id(%d)", tid);
  80. m = btf_members(t);
  81. for (i = 0; i < BTF_INFO_VLEN(t->info); i++) {
  82. n = btf__name_by_offset(__COMPAT_vmlinux_btf, m[i].name_off);
  83. SCX_BUG_ON(!n, "btf__name_by_offset()");
  84. if (!strcmp(n, field))
  85. return true;
  86. }
  87. return false;
  88. }
  89. #define SCX_OPS_SWITCH_PARTIAL \
  90. __COMPAT_ENUM_OR_ZERO("scx_ops_flags", "SCX_OPS_SWITCH_PARTIAL")
  91. static inline long scx_hotplug_seq(void)
  92. {
  93. int fd;
  94. char buf[32];
  95. ssize_t len;
  96. long val;
  97. fd = open("/sys/kernel/sched_ext/hotplug_seq", O_RDONLY);
  98. if (fd < 0)
  99. return -ENOENT;
  100. len = read(fd, buf, sizeof(buf) - 1);
  101. SCX_BUG_ON(len <= 0, "read failed (%ld)", len);
  102. buf[len] = 0;
  103. close(fd);
  104. val = strtoul(buf, NULL, 10);
  105. SCX_BUG_ON(val < 0, "invalid num hotplug events: %lu", val);
  106. return val;
  107. }
  108. /*
  109. * struct sched_ext_ops can change over time. If compat.bpf.h::SCX_OPS_DEFINE()
  110. * is used to define ops and compat.h::SCX_OPS_LOAD/ATTACH() are used to load
  111. * and attach it, backward compatibility is automatically maintained where
  112. * reasonable.
  113. *
  114. * ec7e3b0463e1 ("implement-ops") in https://github.com/sched-ext/sched_ext is
  115. * the current minimum required kernel version.
  116. */
  117. #define SCX_OPS_OPEN(__ops_name, __scx_name) ({ \
  118. struct __scx_name *__skel; \
  119. \
  120. SCX_BUG_ON(!__COMPAT_struct_has_field("sched_ext_ops", "dump"), \
  121. "sched_ext_ops.dump() missing, kernel too old?"); \
  122. \
  123. __skel = __scx_name##__open(); \
  124. SCX_BUG_ON(!__skel, "Could not open " #__scx_name); \
  125. __skel->struct_ops.__ops_name->hotplug_seq = scx_hotplug_seq(); \
  126. __skel; \
  127. })
  128. #define SCX_OPS_LOAD(__skel, __ops_name, __scx_name, __uei_name) ({ \
  129. UEI_SET_SIZE(__skel, __ops_name, __uei_name); \
  130. SCX_BUG_ON(__scx_name##__load((__skel)), "Failed to load skel"); \
  131. })
  132. /*
  133. * New versions of bpftool now emit additional link placeholders for BPF maps,
  134. * and set up BPF skeleton in such a way that libbpf will auto-attach BPF maps
  135. * automatically, assumming libbpf is recent enough (v1.5+). Old libbpf will do
  136. * nothing with those links and won't attempt to auto-attach maps.
  137. *
  138. * To maintain compatibility with older libbpf while avoiding trying to attach
  139. * twice, disable the autoattach feature on newer libbpf.
  140. */
  141. #if LIBBPF_MAJOR_VERSION > 1 || \
  142. (LIBBPF_MAJOR_VERSION == 1 && LIBBPF_MINOR_VERSION >= 5)
  143. #define __SCX_OPS_DISABLE_AUTOATTACH(__skel, __ops_name) \
  144. bpf_map__set_autoattach((__skel)->maps.__ops_name, false)
  145. #else
  146. #define __SCX_OPS_DISABLE_AUTOATTACH(__skel, __ops_name) do {} while (0)
  147. #endif
  148. #define SCX_OPS_ATTACH(__skel, __ops_name, __scx_name) ({ \
  149. struct bpf_link *__link; \
  150. __SCX_OPS_DISABLE_AUTOATTACH(__skel, __ops_name); \
  151. SCX_BUG_ON(__scx_name##__attach((__skel)), "Failed to attach skel"); \
  152. __link = bpf_map__attach_struct_ops((__skel)->maps.__ops_name); \
  153. SCX_BUG_ON(!__link, "Failed to attach struct_ops"); \
  154. __link; \
  155. })
  156. #endif /* __SCX_COMPAT_H */