kfuncs.rst 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. =============================
  2. BPF Kernel Functions (kfuncs)
  3. =============================
  4. 1. Introduction
  5. ===============
  6. BPF Kernel Functions or more commonly known as kfuncs are functions in the Linux
  7. kernel which are exposed for use by BPF programs. Unlike normal BPF helpers,
  8. kfuncs do not have a stable interface and can change from one kernel release to
  9. another. Hence, BPF programs need to be updated in response to changes in the
  10. kernel.
  11. 2. Defining a kfunc
  12. ===================
  13. There are two ways to expose a kernel function to BPF programs, either make an
  14. existing function in the kernel visible, or add a new wrapper for BPF. In both
  15. cases, care must be taken that BPF program can only call such function in a
  16. valid context. To enforce this, visibility of a kfunc can be per program type.
  17. If you are not creating a BPF wrapper for existing kernel function, skip ahead
  18. to :ref:`BPF_kfunc_nodef`.
  19. 2.1 Creating a wrapper kfunc
  20. ----------------------------
  21. When defining a wrapper kfunc, the wrapper function should have extern linkage.
  22. This prevents the compiler from optimizing away dead code, as this wrapper kfunc
  23. is not invoked anywhere in the kernel itself. It is not necessary to provide a
  24. prototype in a header for the wrapper kfunc.
  25. An example is given below::
  26. /* Disables missing prototype warnings */
  27. __diag_push();
  28. __diag_ignore_all("-Wmissing-prototypes",
  29. "Global kfuncs as their definitions will be in BTF");
  30. struct task_struct *bpf_find_get_task_by_vpid(pid_t nr)
  31. {
  32. return find_get_task_by_vpid(nr);
  33. }
  34. __diag_pop();
  35. A wrapper kfunc is often needed when we need to annotate parameters of the
  36. kfunc. Otherwise one may directly make the kfunc visible to the BPF program by
  37. registering it with the BPF subsystem. See :ref:`BPF_kfunc_nodef`.
  38. 2.2 Annotating kfunc parameters
  39. -------------------------------
  40. Similar to BPF helpers, there is sometime need for additional context required
  41. by the verifier to make the usage of kernel functions safer and more useful.
  42. Hence, we can annotate a parameter by suffixing the name of the argument of the
  43. kfunc with a __tag, where tag may be one of the supported annotations.
  44. 2.2.1 __sz Annotation
  45. ---------------------
  46. This annotation is used to indicate a memory and size pair in the argument list.
  47. An example is given below::
  48. void bpf_memzero(void *mem, int mem__sz)
  49. {
  50. ...
  51. }
  52. Here, the verifier will treat first argument as a PTR_TO_MEM, and second
  53. argument as its size. By default, without __sz annotation, the size of the type
  54. of the pointer is used. Without __sz annotation, a kfunc cannot accept a void
  55. pointer.
  56. .. _BPF_kfunc_nodef:
  57. 2.3 Using an existing kernel function
  58. -------------------------------------
  59. When an existing function in the kernel is fit for consumption by BPF programs,
  60. it can be directly registered with the BPF subsystem. However, care must still
  61. be taken to review the context in which it will be invoked by the BPF program
  62. and whether it is safe to do so.
  63. 2.4 Annotating kfuncs
  64. ---------------------
  65. In addition to kfuncs' arguments, verifier may need more information about the
  66. type of kfunc(s) being registered with the BPF subsystem. To do so, we define
  67. flags on a set of kfuncs as follows::
  68. BTF_SET8_START(bpf_task_set)
  69. BTF_ID_FLAGS(func, bpf_get_task_pid, KF_ACQUIRE | KF_RET_NULL)
  70. BTF_ID_FLAGS(func, bpf_put_pid, KF_RELEASE)
  71. BTF_SET8_END(bpf_task_set)
  72. This set encodes the BTF ID of each kfunc listed above, and encodes the flags
  73. along with it. Ofcourse, it is also allowed to specify no flags.
  74. 2.4.1 KF_ACQUIRE flag
  75. ---------------------
  76. The KF_ACQUIRE flag is used to indicate that the kfunc returns a pointer to a
  77. refcounted object. The verifier will then ensure that the pointer to the object
  78. is eventually released using a release kfunc, or transferred to a map using a
  79. referenced kptr (by invoking bpf_kptr_xchg). If not, the verifier fails the
  80. loading of the BPF program until no lingering references remain in all possible
  81. explored states of the program.
  82. 2.4.2 KF_RET_NULL flag
  83. ----------------------
  84. The KF_RET_NULL flag is used to indicate that the pointer returned by the kfunc
  85. may be NULL. Hence, it forces the user to do a NULL check on the pointer
  86. returned from the kfunc before making use of it (dereferencing or passing to
  87. another helper). This flag is often used in pairing with KF_ACQUIRE flag, but
  88. both are orthogonal to each other.
  89. 2.4.3 KF_RELEASE flag
  90. ---------------------
  91. The KF_RELEASE flag is used to indicate that the kfunc releases the pointer
  92. passed in to it. There can be only one referenced pointer that can be passed in.
  93. All copies of the pointer being released are invalidated as a result of invoking
  94. kfunc with this flag.
  95. 2.4.4 KF_KPTR_GET flag
  96. ----------------------
  97. The KF_KPTR_GET flag is used to indicate that the kfunc takes the first argument
  98. as a pointer to kptr, safely increments the refcount of the object it points to,
  99. and returns a reference to the user. The rest of the arguments may be normal
  100. arguments of a kfunc. The KF_KPTR_GET flag should be used in conjunction with
  101. KF_ACQUIRE and KF_RET_NULL flags.
  102. 2.4.5 KF_TRUSTED_ARGS flag
  103. --------------------------
  104. The KF_TRUSTED_ARGS flag is used for kfuncs taking pointer arguments. It
  105. indicates that the all pointer arguments will always have a guaranteed lifetime,
  106. and pointers to kernel objects are always passed to helpers in their unmodified
  107. form (as obtained from acquire kfuncs).
  108. It can be used to enforce that a pointer to a refcounted object acquired from a
  109. kfunc or BPF helper is passed as an argument to this kfunc without any
  110. modifications (e.g. pointer arithmetic) such that it is trusted and points to
  111. the original object.
  112. Meanwhile, it is also allowed pass pointers to normal memory to such kfuncs,
  113. but those can have a non-zero offset.
  114. This flag is often used for kfuncs that operate (change some property, perform
  115. some operation) on an object that was obtained using an acquire kfunc. Such
  116. kfuncs need an unchanged pointer to ensure the integrity of the operation being
  117. performed on the expected object.
  118. 2.4.6 KF_SLEEPABLE flag
  119. -----------------------
  120. The KF_SLEEPABLE flag is used for kfuncs that may sleep. Such kfuncs can only
  121. be called by sleepable BPF programs (BPF_F_SLEEPABLE).
  122. 2.4.7 KF_DESTRUCTIVE flag
  123. --------------------------
  124. The KF_DESTRUCTIVE flag is used to indicate functions calling which is
  125. destructive to the system. For example such a call can result in system
  126. rebooting or panicking. Due to this additional restrictions apply to these
  127. calls. At the moment they only require CAP_SYS_BOOT capability, but more can be
  128. added later.
  129. 2.5 Registering the kfuncs
  130. --------------------------
  131. Once the kfunc is prepared for use, the final step to making it visible is
  132. registering it with the BPF subsystem. Registration is done per BPF program
  133. type. An example is shown below::
  134. BTF_SET8_START(bpf_task_set)
  135. BTF_ID_FLAGS(func, bpf_get_task_pid, KF_ACQUIRE | KF_RET_NULL)
  136. BTF_ID_FLAGS(func, bpf_put_pid, KF_RELEASE)
  137. BTF_SET8_END(bpf_task_set)
  138. static const struct btf_kfunc_id_set bpf_task_kfunc_set = {
  139. .owner = THIS_MODULE,
  140. .set = &bpf_task_set,
  141. };
  142. static int init_subsystem(void)
  143. {
  144. return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_task_kfunc_set);
  145. }
  146. late_initcall(init_subsystem);