#sphinx.addnodesdocument)}( rawsourcechildren]( translations LanguagesNode)}(hhh](h pending_xref)}(hhh]docutils.nodesTextChinese (Simplified)}parenthsba attributes}(ids]classes]names]dupnames]backrefs] refdomainstdreftypedoc reftarget/translations/zh_CN/bpf/kfuncsmodnameN classnameN refexplicitutagnamehhh ubh)}(hhh]hChinese (Traditional)}hh2sbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget/translations/zh_TW/bpf/kfuncsmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hItalian}hhFsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget/translations/it_IT/bpf/kfuncsmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hJapanese}hhZsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget/translations/ja_JP/bpf/kfuncsmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hKorean}hhnsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget/translations/ko_KR/bpf/kfuncsmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hPortuguese (Brazilian)}hhsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget/translations/pt_BR/bpf/kfuncsmodnameN classnameN refexplicituh1hhh ubh)}(hhh]hSpanish}hhsbah}(h]h ]h"]h$]h&] refdomainh)reftypeh+ reftarget/translations/sp_SP/bpf/kfuncsmodnameN classnameN refexplicituh1hhh ubeh}(h]h ]h"]h$]h&]current_languageEnglishuh1h hh _documenthsourceNlineNubhcomment)}(h SPDX-License-Identifier: GPL-2.0h]h SPDX-License-Identifier: GPL-2.0}hhsbah}(h]h ]h"]h$]h&] xml:spacepreserveuh1hhhhhh8/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs.rsthKubhtarget)}(h.. _kfuncs-header-label:h]h}(h]h ]h"]h$]h&]refidkfuncs-header-labeluh1hhKhhhhhhubhsection)}(hhh](htitle)}(hBPF Kernel Functions (kfuncs)h]hBPF Kernel Functions (kfuncs)}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(h1. Introductionh]h1. Introduction}(hhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhhhhhK ubh paragraph)}(hXBPF Kernel Functions or more commonly known as kfuncs are functions in the Linux kernel which are exposed for use by BPF programs. Unlike normal BPF helpers, kfuncs do not have a stable interface and can change from one kernel release to another. Hence, BPF programs need to be updated in response to changes in the kernel. See :ref:`BPF_kfunc_lifecycle_expectations` for more information.h](hXHBPF Kernel Functions or more commonly known as kfuncs are functions in the Linux kernel which are exposed for use by BPF programs. Unlike normal BPF helpers, kfuncs do not have a stable interface and can change from one kernel release to another. Hence, BPF programs need to be updated in response to changes in the kernel. See }(hhhhhNhNubh)}(h':ref:`BPF_kfunc_lifecycle_expectations`h]hinline)}(hjh]h BPF_kfunc_lifecycle_expectations}(hj hhhNhNubah}(h]h ](xrefstdstd-refeh"]h$]h&]uh1j hjubah}(h]h ]h"]h$]h&]refdoc bpf/kfuncs refdomainjreftyperef refexplicitrefwarn reftarget bpf_kfunc_lifecycle_expectationsuh1hhhhK hhubh for more information.}(hhhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK hhhhubeh}(h] introductionah ]h"]1. introductionah$]h&]uh1hhhhhhhhK ubh)}(hhh](h)}(h2. Defining a kfunch]h2. Defining a kfunc}(hj@hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj=hhhhhKubh)}(hX8There are two ways to expose a kernel function to BPF programs, either make an existing function in the kernel visible, or add a new wrapper for BPF. In both cases, care must be taken that BPF program can only call such function in a valid context. To enforce this, visibility of a kfunc can be per program type.h]hX8There are two ways to expose a kernel function to BPF programs, either make an existing function in the kernel visible, or add a new wrapper for BPF. In both cases, care must be taken that BPF program can only call such function in a valid context. To enforce this, visibility of a kfunc can be per program type.}(hjNhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj=hhubh)}(hiIf you are not creating a BPF wrapper for existing kernel function, skip ahead to :ref:`BPF_kfunc_nodef`.h](hRIf you are not creating a BPF wrapper for existing kernel function, skip ahead to }(hj\hhhNhNubh)}(h:ref:`BPF_kfunc_nodef`h]j )}(hjfh]hBPF_kfunc_nodef}(hjhhhhNhNubah}(h]h ](jstdstd-refeh"]h$]h&]uh1j hjdubah}(h]h ]h"]h$]h&]refdocj# refdomainjrreftyperef refexplicitrefwarnj)bpf_kfunc_nodefuh1hhhhKhj\ubh.}(hj\hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj=hhubh)}(hhh](h)}(h2.1 Creating a wrapper kfunch]h2.1 Creating a wrapper kfunc}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(hXWhen defining a wrapper kfunc, the wrapper function should have extern linkage. This prevents the compiler from optimizing away dead code, as this wrapper kfunc is not invoked anywhere in the kernel itself. It is not necessary to provide a prototype in a header for the wrapper kfunc.h]hXWhen defining a wrapper kfunc, the wrapper function should have extern linkage. This prevents the compiler from optimizing away dead code, as this wrapper kfunc is not invoked anywhere in the kernel itself. It is not necessary to provide a prototype in a header for the wrapper kfunc.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK hjhhubh)}(hAn example is given below::h]hAn example is given below:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK%hjhhubh literal_block)}(h/* Disables missing prototype warnings */ __bpf_kfunc_start_defs(); __bpf_kfunc struct task_struct *bpf_find_get_task_by_vpid(pid_t nr) { return find_get_task_by_vpid(nr); } __bpf_kfunc_end_defs();h]h/* Disables missing prototype warnings */ __bpf_kfunc_start_defs(); __bpf_kfunc struct task_struct *bpf_find_get_task_by_vpid(pid_t nr) { return find_get_task_by_vpid(nr); } __bpf_kfunc_end_defs();}hjsbah}(h]h ]h"]h$]h&]hhuh1jhhhK'hjhhubh)}(hA wrapper kfunc is often needed when we need to annotate parameters of the kfunc. Otherwise one may directly make the kfunc visible to the BPF program by registering it with the BPF subsystem. See :ref:`BPF_kfunc_nodef`.h](hA wrapper kfunc is often needed when we need to annotate parameters of the kfunc. Otherwise one may directly make the kfunc visible to the BPF program by registering it with the BPF subsystem. See }(hjhhhNhNubh)}(h:ref:`BPF_kfunc_nodef`h]j )}(hjh]hBPF_kfunc_nodef}(hjhhhNhNubah}(h]h ](jstdstd-refeh"]h$]h&]uh1j hjubah}(h]h ]h"]h$]h&]refdocj# refdomainjreftyperef refexplicitrefwarnj)bpf_kfunc_nodefuh1hhhhK1hjubh.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK1hjhhubeh}(h]creating-a-wrapper-kfuncah ]h"]2.1 creating a wrapper kfuncah$]h&]uh1hhj=hhhhhKubh)}(hhh](h)}(h2.2 kfunc Parametersh]h2.2 kfunc Parameters}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhK6ubh)}(hX%All kfuncs now require trusted arguments by default. This means that all pointer arguments must be valid, and all pointers to BTF objects must be passed in their unmodified form (at a zero offset, and without having been obtained from walking another pointer, with exceptions described below).h]hX%All kfuncs now require trusted arguments by default. This means that all pointer arguments must be valid, and all pointers to BTF objects must be passed in their unmodified form (at a zero offset, and without having been obtained from walking another pointer, with exceptions described below).}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK8hjhhubh)}(hQThere are two types of pointers to kernel objects which are considered "trusted":h]hUThere are two types of pointers to kernel objects which are considered “trusted”:}(hj$hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK=hjhhubhenumerated_list)}(hhh](h list_item)}(hIPointers which are passed as tracepoint or struct_ops callback arguments.h]h)}(hj;h]hIPointers which are passed as tracepoint or struct_ops callback arguments.}(hj=hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK?hj9ubah}(h]h ]h"]h$]h&]uh1j7hj4hhhhhNubj8)}(h6Pointers which were returned from a KF_ACQUIRE kfunc. h]h)}(h5Pointers which were returned from a KF_ACQUIRE kfunc.h]h5Pointers which were returned from a KF_ACQUIRE kfunc.}(hjThhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK@hjPubah}(h]h ]h"]h$]h&]uh1j7hj4hhhhhNubeh}(h]h ]h"]h$]h&]enumtypearabicprefixhsuffix.uh1j2hjhhhhhK?ubh)}(hpPointers to non-BTF objects (e.g. scalar pointers) may also be passed to kfuncs, and may have a non-zero offset.h]hpPointers to non-BTF objects (e.g. scalar pointers) may also be passed to kfuncs, and may have a non-zero offset.}(hjshhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKBhjhhubh)}(htThe definition of "valid" pointers is subject to change at any time, and has absolutely no ABI stability guarantees.h]hxThe definition of “valid” pointers is subject to change at any time, and has absolutely no ABI stability guarantees.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKEhjhhubh)}(hXMAs mentioned above, a nested pointer obtained from walking a trusted pointer is no longer trusted, with one exception. If a struct type has a field that is guaranteed to be valid (trusted or rcu, as in KF_RCU description below) as long as its parent pointer is valid, the following macros can be used to express that to the verifier:h]hXMAs mentioned above, a nested pointer obtained from walking a trusted pointer is no longer trusted, with one exception. If a struct type has a field that is guaranteed to be valid (trusted or rcu, as in KF_RCU description below) as long as its parent pointer is valid, the following macros can be used to express that to the verifier:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKHhjhhubh bullet_list)}(hhh](j8)}(h``BTF_TYPE_SAFE_TRUSTED``h]h)}(hjh]hliteral)}(hjh]hBTF_TYPE_SAFE_TRUSTED}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1hhhhKNhjubah}(h]h ]h"]h$]h&]uh1j7hjhhhhhNubj8)}(h``BTF_TYPE_SAFE_RCU``h]h)}(hjh]j)}(hjh]hBTF_TYPE_SAFE_RCU}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1hhhhKOhjubah}(h]h ]h"]h$]h&]uh1j7hjhhhhhNubj8)}(h``BTF_TYPE_SAFE_RCU_OR_NULL`` h]h)}(h``BTF_TYPE_SAFE_RCU_OR_NULL``h]j)}(hjh]hBTF_TYPE_SAFE_RCU_OR_NULL}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1hhhhKPhjubah}(h]h ]h"]h$]h&]uh1j7hjhhhhhNubeh}(h]h ]h"]h$]h&]bullet*uh1jhhhKNhjhhubh)}(h For example,h]h For example,}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKRhjhhubj)}(hBBTF_TYPE_SAFE_TRUSTED(struct socket) { struct sock *sk; };h]hBBTF_TYPE_SAFE_TRUSTED(struct socket) { struct sock *sk; };}hjsbah}(h]h ]h"]h$]h&]hhƌforcelanguagechighlight_args}uh1jhhhKThjhhubh)}(horh]hor}(hj.hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKZhjhhubj)}(hBTF_TYPE_SAFE_RCU(struct task_struct) { const cpumask_t *cpus_ptr; struct css_set __rcu *cgroups; struct task_struct __rcu *real_parent; struct task_struct *group_leader; };h]hBTF_TYPE_SAFE_RCU(struct task_struct) { const cpumask_t *cpus_ptr; struct css_set __rcu *cgroups; struct task_struct __rcu *real_parent; struct task_struct *group_leader; };}hj<sbah}(h]h ]h"]h$]h&]hhj)j*j+j,}uh1jhhhK\hjhhubh)}(hIn other words, you must:h]hIn other words, you must:}(hjKhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKehjhhubj3)}(hhh](j8)}(hhjhhhhubeh}(h]$arena-and-arena-nullable-annotationsah ]h"]/2.3.8 __arena and __arena__nullable annotationsah$]h&]uh1hhj=hhhhhMubh)}(hhh](h)}(h%2.4 Using an existing kernel functionh]h%2.4 Using an existing kernel function}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj}hhhhhMAubh)}(hX When an existing function in the kernel is fit for consumption by BPF programs, it can be directly registered with the BPF subsystem. However, care must still be taken to review the context in which it will be invoked by the BPF program and whether it is safe to do so.h]hX When an existing function in the kernel is fit for consumption by BPF programs, it can be directly registered with the BPF subsystem. However, care must still be taken to review the context in which it will be invoked by the BPF program and whether it is safe to do so.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMChj}hhubeh}(h](!using-an-existing-kernel-functionjteh ]h"](%2.4 using an existing kernel functionbpf_kfunc_nodefeh$]h&]uh1hhj=hhhhhMAexpect_referenced_by_name}jjjsexpect_referenced_by_id}jtjjsubh)}(hhh](h)}(h2.5 Annotating kfuncsh]h2.5 Annotating kfuncs}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMIubh)}(hIn addition to kfuncs' arguments, verifier may need more information about the type of kfunc(s) being registered with the BPF subsystem. To do so, we define flags on a set of kfuncs as follows::h]hIn addition to kfuncs’ arguments, verifier may need more information about the type of kfunc(s) being registered with the BPF subsystem. To do so, we define flags on a set of kfuncs as follows:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMKhjhhubj)}(hBTF_KFUNCS_START(bpf_task_set) BTF_ID_FLAGS(func, bpf_get_task_pid, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_put_pid, KF_RELEASE) BTF_KFUNCS_END(bpf_task_set)h]hBTF_KFUNCS_START(bpf_task_set) BTF_ID_FLAGS(func, bpf_get_task_pid, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_put_pid, KF_RELEASE) BTF_KFUNCS_END(bpf_task_set)}hjsbah}(h]h ]h"]h$]h&]hhuh1jhhhMOhjhhubh)}(hThis set encodes the BTF ID of each kfunc listed above, and encodes the flags along with it. It is also allowed to specify no flags.h]hThis set encodes the BTF ID of each kfunc listed above, and encodes the flags along with it. It is also allowed to specify no flags.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMThjhhubh)}(hXkfunc definitions should also always be annotated with the ``__bpf_kfunc`` macro. This prevents issues such as the compiler inlining the kfunc, or the function being elided in an LTO build as it's not used in the rest of the kernel. Developers should not manually add annotations to their kfunc to prevent these issues. If an annotation is required to prevent such an issue with your kfunc, it is a bug and should be added to the definition of the macro so that other kfuncs are similarly protected. An example is given below::h](h;kfunc definitions should also always be annotated with the }(hjhhhNhNubj)}(h``__bpf_kfunc``h]h __bpf_kfunc}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhX macro. This prevents issues such as the compiler inlining the kfunc, or the function being elided in an LTO build as it’s not used in the rest of the kernel. Developers should not manually add annotations to their kfunc to prevent these issues. If an annotation is required to prevent such an issue with your kfunc, it is a bug and should be added to the definition of the macro so that other kfuncs are similarly protected. An example is given below:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMWhjhhubj)}(hA__bpf_kfunc struct task_struct *bpf_get_task_pid(s32 pid) { ... }h]hA__bpf_kfunc struct task_struct *bpf_get_task_pid(s32 pid) { ... }}hj sbah}(h]h ]h"]h$]h&]hhuh1jhhhM_hjhhubh)}(hXNote that kfuncs must not be declared ``static``. A kfunc can be called from a BPF program ``*.c`` file outside the compilation unit that defines it, so its externally visible name must remain available for BTF ID lookup. ``static`` linkage allows the compiler to rename the function, which can break this BTF-based kfunc resolution. Further note that sparse may warn that an otherwise unreferenced kfunc should be static. Such warnings should be ignored for kfunc definitions.h](h&Note that kfuncs must not be declared }(hj hhhNhNubj)}(h ``static``h]hstatic}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh+. A kfunc can be called from a BPF program }(hj hhhNhNubj)}(h``*.c``h]h*.c}(hj, hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh| file outside the compilation unit that defines it, so its externally visible name must remain available for BTF ID lookup. }(hj hhhNhNubj)}(h ``static``h]hstatic}(hj> hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh linkage allows the compiler to rename the function, which can break this BTF-based kfunc resolution. Further note that sparse may warn that an otherwise unreferenced kfunc should be static. Such warnings should be ignored for kfunc definitions.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMdhjhhubeh}(h]annotating-kfuncsah ]h"]2.5 annotating kfuncsah$]h&]uh1hhj=hhhhhMIubh)}(hhh](h)}(h2.5.1 KF_ACQUIRE flagh]h2.5.1 KF_ACQUIRE flag}(hja hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj^ hhhhhMmubh)}(hXThe KF_ACQUIRE flag is used to indicate that the kfunc returns a pointer to a refcounted object. The verifier will then ensure that the pointer to the object is eventually released using a release kfunc, or transferred to a map using a referenced kptr (by invoking bpf_kptr_xchg). If not, the verifier fails the loading of the BPF program until no lingering references remain in all possible explored states of the program.h]hXThe KF_ACQUIRE flag is used to indicate that the kfunc returns a pointer to a refcounted object. The verifier will then ensure that the pointer to the object is eventually released using a release kfunc, or transferred to a map using a referenced kptr (by invoking bpf_kptr_xchg). If not, the verifier fails the loading of the BPF program until no lingering references remain in all possible explored states of the program.}(hjo hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMohj^ hhubeh}(h]kf-acquire-flagah ]h"]2.5.1 kf_acquire flagah$]h&]uh1hhj=hhhhhMmubh)}(hhh](h)}(h2.5.2 KF_RET_NULL flagh]h2.5.2 KF_RET_NULL flag}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhMwubh)}(hXVThe KF_RET_NULL flag is used to indicate that the pointer returned by the kfunc may be NULL. Hence, it forces the user to do a NULL check on the pointer returned from the kfunc before making use of it (dereferencing or passing to another helper). This flag is often used in pairing with KF_ACQUIRE flag, but both are orthogonal to each other.h]hXVThe KF_RET_NULL flag is used to indicate that the pointer returned by the kfunc may be NULL. Hence, it forces the user to do a NULL check on the pointer returned from the kfunc before making use of it (dereferencing or passing to another helper). This flag is often used in pairing with KF_ACQUIRE flag, but both are orthogonal to each other.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMyhj hhubeh}(h]kf-ret-null-flagah ]h"]2.5.2 kf_ret_null flagah$]h&]uh1hhj=hhhhhMwubh)}(hhh](h)}(h2.5.3 KF_RELEASE flagh]h2.5.3 KF_RELEASE flag}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhMubh)}(hXThe KF_RELEASE flag is used to indicate that the kfunc releases the pointer passed in to it. There can be only one referenced pointer that can be passed in. All copies of the pointer being released are invalidated as a result of invoking kfunc with this flag.h]hXThe KF_RELEASE flag is used to indicate that the kfunc releases the pointer passed in to it. There can be only one referenced pointer that can be passed in. All copies of the pointer being released are invalidated as a result of invoking kfunc with this flag.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubeh}(h]kf-release-flagah ]h"]2.5.3 kf_release flagah$]h&]uh1hhj=hhhhhMubh)}(hhh](h)}(h2.5.4 KF_SLEEPABLE flagh]h2.5.4 KF_SLEEPABLE flag}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhMubh)}(hThe KF_SLEEPABLE flag is used for kfuncs that may sleep. Such kfuncs can only be called by sleepable BPF programs (BPF_F_SLEEPABLE).h]hThe KF_SLEEPABLE flag is used for kfuncs that may sleep. Such kfuncs can only be called by sleepable BPF programs (BPF_F_SLEEPABLE).}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubeh}(h]kf-sleepable-flagah ]h"]2.5.4 kf_sleepable flagah$]h&]uh1hhj=hhhhhMubh)}(hhh](h)}(h2.5.5 KF_DESTRUCTIVE flagh]h2.5.5 KF_DESTRUCTIVE flag}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhMubh)}(hX6The KF_DESTRUCTIVE flag is used to indicate functions calling which is destructive to the system. For example such a call can result in system rebooting or panicking. Due to this additional restrictions apply to these calls. At the moment they only require CAP_SYS_BOOT capability, but more can be added later.h]hX6The KF_DESTRUCTIVE flag is used to indicate functions calling which is destructive to the system. For example such a call can result in system rebooting or panicking. Due to this additional restrictions apply to these calls. At the moment they only require CAP_SYS_BOOT capability, but more can be added later.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubeh}(h]kf-destructive-flagah ]h"]2.5.5 kf_destructive flagah$]h&]uh1hhj=hhhhhMubh)}(hhh](h)}(h2.5.6 KF_RCU flagh]h2.5.6 KF_RCU flag}(hj$ hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj! hhhhhMubh)}(hX0The KF_RCU flag allows kfuncs to opt out of the default trusted args requirement and accept RCU pointers with weaker guarantees. The kfuncs marked with KF_RCU expect either PTR_TRUSTED or MEM_RCU arguments. The verifier guarantees that the objects are valid and there is no use-after-free. The pointers are not NULL, but the object's refcount could have reached zero. The kfuncs need to consider doing refcnt != 0 check, especially when returning a KF_ACQUIRE pointer. Note as well that a KF_ACQUIRE kfunc that is KF_RCU should very likely also be KF_RET_NULL.h]hX2The KF_RCU flag allows kfuncs to opt out of the default trusted args requirement and accept RCU pointers with weaker guarantees. The kfuncs marked with KF_RCU expect either PTR_TRUSTED or MEM_RCU arguments. The verifier guarantees that the objects are valid and there is no use-after-free. The pointers are not NULL, but the object’s refcount could have reached zero. The kfuncs need to consider doing refcnt != 0 check, especially when returning a KF_ACQUIRE pointer. Note as well that a KF_ACQUIRE kfunc that is KF_RCU should very likely also be KF_RET_NULL.}(hj2 hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj! hhubeh}(h] kf-rcu-flagah ]h"]2.5.6 kf_rcu flagah$]h&]uh1hhj=hhhhhMubh)}(hhh](h)}(h2.5.7 KF_RCU_PROTECTED flagh]h2.5.7 KF_RCU_PROTECTED flag}(hjK hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjH hhhhhMubh)}(hThe KF_RCU_PROTECTED flag is used to indicate that the kfunc must be invoked in an RCU critical section. This is assumed by default in non-sleepable programs, and must be explicitly ensured by calling ``bpf_rcu_read_lock`` for sleepable ones.h](hThe KF_RCU_PROTECTED flag is used to indicate that the kfunc must be invoked in an RCU critical section. This is assumed by default in non-sleepable programs, and must be explicitly ensured by calling }(hjY hhhNhNubj)}(h``bpf_rcu_read_lock``h]hbpf_rcu_read_lock}(hja hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjY ubh for sleepable ones.}(hjY hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjH hhubh)}(hIf the kfunc returns a pointer value, this flag also enforces that the returned pointer is RCU protected, and can only be used while the RCU critical section is active.h]hIf the kfunc returns a pointer value, this flag also enforces that the returned pointer is RCU protected, and can only be used while the RCU critical section is active.}(hjy hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjH hhubh)}(hX#The flag is distinct from the ``KF_RCU`` flag, which only ensures that its arguments are at least RCU protected pointers. This may transitively imply that RCU protection is ensured, but it does not work in cases of kfuncs which require RCU protection but do not take RCU protected arguments.h](hThe flag is distinct from the }(hj hhhNhNubj)}(h ``KF_RCU``h]hKF_RCU}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh flag, which only ensures that its arguments are at least RCU protected pointers. This may transitively imply that RCU protection is ensured, but it does not work in cases of kfuncs which require RCU protection but do not take RCU protected arguments.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjH hhubh)}(h.. _KF_deprecated_flag:h]h}(h]h ]h"]h$]h&]hԌkf-deprecated-flaguh1hhMhjH hhhhubeh}(h]kf-rcu-protected-flagah ]h"]2.5.7 kf_rcu_protected flagah$]h&]uh1hhj=hhhhhMubh)}(hhh](h)}(h2.5.8 KF_DEPRECATED flagh]h2.5.8 KF_DEPRECATED flag}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhMubh)}(hXThe KF_DEPRECATED flag is used for kfuncs which are scheduled to be changed or removed in a subsequent kernel release. A kfunc that is marked with KF_DEPRECATED should also have any relevant information captured in its kernel doc. Such information typically includes the kfunc's expected remaining lifespan, a recommendation for new functionality that can replace it if any is available, and possibly a rationale for why it is being removed.h]hXThe KF_DEPRECATED flag is used for kfuncs which are scheduled to be changed or removed in a subsequent kernel release. A kfunc that is marked with KF_DEPRECATED should also have any relevant information captured in its kernel doc. Such information typically includes the kfunc’s expected remaining lifespan, a recommendation for new functionality that can replace it if any is available, and possibly a rationale for why it is being removed.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(hXGNote that while on some occasions, a KF_DEPRECATED kfunc may continue to be supported and have its KF_DEPRECATED flag removed, it is likely to be far more difficult to remove a KF_DEPRECATED flag after it's been added than it is to prevent it from being added in the first place. As described in :ref:`BPF_kfunc_lifecycle_expectations`, users that rely on specific kfuncs are encouraged to make their use-cases known as early as possible, and participate in upstream discussions regarding whether to keep, change, deprecate, or remove those kfuncs if and when such discussions occur.h](hX*Note that while on some occasions, a KF_DEPRECATED kfunc may continue to be supported and have its KF_DEPRECATED flag removed, it is likely to be far more difficult to remove a KF_DEPRECATED flag after it’s been added than it is to prevent it from being added in the first place. As described in }(hj hhhNhNubh)}(h':ref:`BPF_kfunc_lifecycle_expectations`h]j )}(hj h]h BPF_kfunc_lifecycle_expectations}(hj hhhNhNubah}(h]h ](jstdstd-refeh"]h$]h&]uh1j hj ubah}(h]h ]h"]h$]h&]refdocj# refdomainj reftyperef refexplicitrefwarnj) bpf_kfunc_lifecycle_expectationsuh1hhhhMhj ubh, users that rely on specific kfuncs are encouraged to make their use-cases known as early as possible, and participate in upstream discussions regarding whether to keep, change, deprecate, or remove those kfuncs if and when such discussions occur.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj hhubeh}(h](j id1eh ]h"](2.5.8 kf_deprecated flagkf_deprecated_flageh$]h&]uh1hhj=hhhhhMj}j j sj}j j subh)}(hhh](h)}(h2.5.9 KF_IMPLICIT_ARGS flagh]h2.5.9 KF_IMPLICIT_ARGS flag}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhMubh)}(hThe KF_IMPLICIT_ARGS flag is used to indicate that the BPF signature of the kfunc is different from it's kernel signature, and the values for implicit arguments are provided at load time by the verifier.h]hThe KF_IMPLICIT_ARGS flag is used to indicate that the BPF signature of the kfunc is different from it’s kernel signature, and the values for implicit arguments are provided at load time by the verifier.}(hj' hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(hjOnly arguments of specific types are implicit. Currently only ``struct bpf_prog_aux *`` type is supported.h](h>Only arguments of specific types are implicit. Currently only }(hj5 hhhNhNubj)}(h``struct bpf_prog_aux *``h]hstruct bpf_prog_aux *}(hj= hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj5 ubh type is supported.}(hj5 hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(hA kfunc with KF_IMPLICIT_ARGS flag therefore has two types in BTF: one function matching the kernel declaration (with _impl suffix in the name by convention), and another matching the intended BPF API.h]hA kfunc with KF_IMPLICIT_ARGS flag therefore has two types in BTF: one function matching the kernel declaration (with _impl suffix in the name by convention), and another matching the intended BPF API.}(hjU hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(huVerifier only allows calls to the non-_impl version of a kfunc, that uses a signature without the implicit arguments.h]huVerifier only allows calls to the non-_impl version of a kfunc, that uses a signature without the implicit arguments.}(hjc hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(hExample declaration:h]hExample declaration:}(hjq hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubj)}(hX__bpf_kfunc int bpf_task_work_schedule_signal(struct task_struct *task, struct bpf_task_work *tw, void *map__const_map, bpf_task_work_callback_t callback, struct bpf_prog_aux *aux) { ... }h]hX__bpf_kfunc int bpf_task_work_schedule_signal(struct task_struct *task, struct bpf_task_work *tw, void *map__const_map, bpf_task_work_callback_t callback, struct bpf_prog_aux *aux) { ... }}hj sbah}(h]h ]h"]h$]h&]hhj)j*j+j,}uh1jhhhMhj hhubh)}(hExample usage in BPF program:h]hExample usage in BPF program:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubj)}(hy/* note that the last argument is omitted */ bpf_task_work_schedule_signal(task, &work->tw, &arrmap, task_work_callback);h]hy/* note that the last argument is omitted */ bpf_task_work_schedule_signal(task, &work->tw, &arrmap, task_work_callback);}hj sbah}(h]h ]h"]h$]h&]hhj)j*j+j,}uh1jhhhMhj hhubeh}(h]kf-implicit-args-flagah ]h"]2.5.9 kf_implicit_args flagah$]h&]uh1hhj=hhhhhMubh)}(hhh](h)}(h2.6 Registering the kfuncsh]h2.6 Registering the kfuncs}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhMubh)}(hOnce the kfunc is prepared for use, the final step to making it visible is registering it with the BPF subsystem. Registration is done per BPF program type. An example is shown below::h]hOnce the kfunc is prepared for use, the final step to making it visible is registering it with the BPF subsystem. Registration is done per BPF program type. An example is shown below:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubj)}(hXBTF_KFUNCS_START(bpf_task_set) BTF_ID_FLAGS(func, bpf_get_task_pid, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_put_pid, KF_RELEASE) BTF_KFUNCS_END(bpf_task_set) static const struct btf_kfunc_id_set bpf_task_kfunc_set = { .owner = THIS_MODULE, .set = &bpf_task_set, }; static int init_subsystem(void) { return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_task_kfunc_set); } late_initcall(init_subsystem);h]hXBTF_KFUNCS_START(bpf_task_set) BTF_ID_FLAGS(func, bpf_get_task_pid, KF_ACQUIRE | KF_RET_NULL) BTF_ID_FLAGS(func, bpf_put_pid, KF_RELEASE) BTF_KFUNCS_END(bpf_task_set) static const struct btf_kfunc_id_set bpf_task_kfunc_set = { .owner = THIS_MODULE, .set = &bpf_task_set, }; static int init_subsystem(void) { return register_btf_kfunc_id_set(BPF_PROG_TYPE_TRACING, &bpf_task_kfunc_set); } late_initcall(init_subsystem);}hj sbah}(h]h ]h"]h$]h&]hhuh1jhhhMhj hhubh)}(hXAt kernel build time the ``resolve_btfids`` tool finds all kfuncs declared with ``BTF_KFUNCS_START()`` and emits their BTF annotations into the kernel's BTF. For each kfunc it emits a ``bpf_kfunc`` BTF decl tag, a ``bpf_fastcall`` decl tag when the kfunc is flagged ``KF_FASTCALL``, and the ``address_space(1)`` type attribute on the return value and/or arguments that use arena pointers (see sections 2.3.8 and 2.8).h](hAt kernel build time the }(hj hhhNhNubj)}(h``resolve_btfids``h]hresolve_btfids}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh% tool finds all kfuncs declared with }(hj hhhNhNubj)}(h``BTF_KFUNCS_START()``h]hBTF_KFUNCS_START()}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubhT and emits their BTF annotations into the kernel’s BTF. For each kfunc it emits a }(hj hhhNhNubj)}(h ``bpf_kfunc``h]h bpf_kfunc}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh BTF decl tag, a }(hj hhhNhNubj)}(h``bpf_fastcall``h]h bpf_fastcall}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh$ decl tag when the kfunc is flagged }(hj hhhNhNubj)}(h``KF_FASTCALL``h]h KF_FASTCALL}(hj0 hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh , and the }(hj hhhNhNubj)}(h``address_space(1)``h]haddress_space(1)}(hjB hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubhj type attribute on the return value and/or arguments that use arena pointers (see sections 2.3.8 and 2.8).}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj hhubeh}(h]registering-the-kfuncsah ]h"]2.6 registering the kfuncsah$]h&]uh1hhj=hhhhhMubh)}(hhh](h)}(h,2.7 Specifying no-cast aliases with ___inith]h,2.7 Specifying no-cast aliases with ___init}(hje hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjb hhhhhMubh)}(hX=The verifier will always enforce that the BTF type of a pointer passed to a kfunc by a BPF program, matches the type of pointer specified in the kfunc definition. The verifier, does, however, allow types that are equivalent according to the C standard to be passed to the same kfunc arg, even if their BTF_IDs differ.h]hX=The verifier will always enforce that the BTF type of a pointer passed to a kfunc by a BPF program, matches the type of pointer specified in the kfunc definition. The verifier, does, however, allow types that are equivalent according to the C standard to be passed to the same kfunc arg, even if their BTF_IDs differ.}(hjs hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjb hhubh)}(h/For example, for the following type definition:h]h/For example, for the following type definition:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjb hhubj)}(hLstruct bpf_cpumask { cpumask_t cpumask; refcount_t usage; };h]hLstruct bpf_cpumask { cpumask_t cpumask; refcount_t usage; };}hj sbah}(h]h ]h"]h$]h&]hhj)j*j+j,}uh1jhhhMhjb hhubh)}(hXThe verifier would allow a ``struct bpf_cpumask *`` to be passed to a kfunc taking a ``cpumask_t *`` (which is a typedef of ``struct cpumask *``). For instance, both ``struct cpumask *`` and ``struct bpf_cpmuask *`` can be passed to bpf_cpumask_test_cpu().h](hThe verifier would allow a }(hj hhhNhNubj)}(h``struct bpf_cpumask *``h]hstruct bpf_cpumask *}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh" to be passed to a kfunc taking a }(hj hhhNhNubj)}(h``cpumask_t *``h]h cpumask_t *}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh (which is a typedef of }(hj hhhNhNubj)}(h``struct cpumask *``h]hstruct cpumask *}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh). For instance, both }(hj hhhNhNubj)}(h``struct cpumask *``h]hstruct cpumask *}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh and }(hj hhhNhNubj)}(h``struct bpf_cpmuask *``h]hstruct bpf_cpmuask *}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh) can be passed to bpf_cpumask_test_cpu().}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjb hhubh)}(hiIn some cases, this type-aliasing behavior is not desired. ``struct nf_conn___init`` is one such example:h](h;In some cases, this type-aliasing behavior is not desired. }(hj hhhNhNubj)}(h``struct nf_conn___init``h]hstruct nf_conn___init}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh is one such example:}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjb hhubj)}(h5struct nf_conn___init { struct nf_conn ct; };h]h5struct nf_conn___init { struct nf_conn ct; };}hj& sbah}(h]h ]h"]h$]h&]hhj)j*j+j,}uh1jhhhM!hjb hhubh)}(hXThe C standard would consider these types to be equivalent, but it would not always be safe to pass either type to a trusted kfunc. ``struct nf_conn___init`` represents an allocated ``struct nf_conn`` object that has *not yet been initialized*, so it would therefore be unsafe to pass a ``struct nf_conn___init *`` to a kfunc that's expecting a fully initialized ``struct nf_conn *`` (e.g. ``bpf_ct_change_timeout()``).h](hThe C standard would consider these types to be equivalent, but it would not always be safe to pass either type to a trusted kfunc. }(hj5 hhhNhNubj)}(h``struct nf_conn___init``h]hstruct nf_conn___init}(hj= hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj5 ubh represents an allocated }(hj5 hhhNhNubj)}(h``struct nf_conn``h]hstruct nf_conn}(hjO hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj5 ubh object that has }(hj5 hhhNhNubhemphasis)}(h*not yet been initialized*h]hnot yet been initialized}(hjc hhhNhNubah}(h]h ]h"]h$]h&]uh1ja hj5 ubh,, so it would therefore be unsafe to pass a }(hj5 hhhNhNubj)}(h``struct nf_conn___init *``h]hstruct nf_conn___init *}(hju hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj5 ubh3 to a kfunc that’s expecting a fully initialized }(hj5 hhhNhNubj)}(h``struct nf_conn *``h]hstruct nf_conn *}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj5 ubh (e.g. }(hj5 hhhNhNubj)}(h``bpf_ct_change_timeout()``h]hbpf_ct_change_timeout()}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj5 ubh).}(hj5 hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM'hjb hhubh)}(hIn order to accommodate such requirements, the verifier will enforce strict PTR_TO_BTF_ID type matching if two types have the exact same name, with one being suffixed with ``___init``.h](hIn order to accommodate such requirements, the verifier will enforce strict PTR_TO_BTF_ID type matching if two types have the exact same name, with one being suffixed with }(hj hhhNhNubj)}(h ``___init``h]h___init}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh.}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM.hjb hhubh)}(h.. _BPF_kfunc_arena_access:h]h}(h]h ]h"]h$]h&]hԌbpf-kfunc-arena-accessuh1hhM2hjb hhhhubeh}(h]$specifying-no-cast-aliases-with-initah ]h"]+2.7 specifying no-cast aliases with ___initah$]h&]uh1hhj=hhhhhMubh)}(hhh](h)}(h22.8 Accessing arena memory through kfunc argumentsh]h22.8 Accessing arena memory through kfunc arguments}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhM5ubh)}(hXA read or write at any address inside an arena does not oops the kernel. Unallocated arena pages are lazily backed by a scratch page and the access is reported through the program's BPF stream as an error. Only the BPF program's correctness is affected; the kernel itself remains intact.h]hX#A read or write at any address inside an arena does not oops the kernel. Unallocated arena pages are lazily backed by a scratch page and the access is reported through the program’s BPF stream as an error. Only the BPF program’s correctness is affected; the kernel itself remains intact.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM7hj hhubh)}(hXThe arena is followed by a ``GUARD_SZ / 2`` (32 KiB) guard region that is also covered by this recovery. A kfunc handed an arena pointer may therefore access up to ``GUARD_SZ / 2`` past it without bounds-checking against the arena. Larger accesses must verify the range explicitly.h](hThe arena is followed by a }(hjhhhNhNubj)}(h``GUARD_SZ / 2``h]h GUARD_SZ / 2}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhy (32 KiB) guard region that is also covered by this recovery. A kfunc handed an arena pointer may therefore access up to }(hjhhhNhNubj)}(h``GUARD_SZ / 2``h]h GUARD_SZ / 2}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhe past it without bounds-checking against the arena. Larger accesses must verify the range explicitly.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM=hj hhubh)}(h%.. _BPF_kfunc_lifecycle_expectations:h]h}(h]h ]h"]h$]h&]hԌ bpf-kfunc-lifecycle-expectationsuh1hhMBhj hhhhubeh}(h](.accessing-arena-memory-through-kfunc-argumentsj eh ]h"](22.8 accessing arena memory through kfunc argumentsbpf_kfunc_arena_accesseh$]h&]uh1hhj=hhhhhM5j}jFj sj}j j subeh}(h]defining-a-kfuncah ]h"]2. defining a kfuncah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(h3. kfunc lifecycle expectationsh]h3. kfunc lifecycle expectations}(hjVhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjShhhhhMEubh)}(hXMkfuncs provide a kernel <-> kernel API, and thus are not bound by any of the strict stability restrictions associated with kernel <-> user UAPIs. This means they can be thought of as similar to EXPORT_SYMBOL_GPL, and can therefore be modified or removed by a maintainer of the subsystem they're defined in when it's deemed necessary.h]hXQkfuncs provide a kernel <-> kernel API, and thus are not bound by any of the strict stability restrictions associated with kernel <-> user UAPIs. This means they can be thought of as similar to EXPORT_SYMBOL_GPL, and can therefore be modified or removed by a maintainer of the subsystem they’re defined in when it’s deemed necessary.}(hjdhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMGhjShhubh)}(hXLike any other change to the kernel, maintainers will not change or remove a kfunc without having a reasonable justification. Whether or not they'll choose to change a kfunc will ultimately depend on a variety of factors, such as how widely used the kfunc is, how long the kfunc has been in the kernel, whether an alternative kfunc exists, what the norm is in terms of stability for the subsystem in question, and of course what the technical cost is of continuing to support the kfunc.h]hXLike any other change to the kernel, maintainers will not change or remove a kfunc without having a reasonable justification. Whether or not they’ll choose to change a kfunc will ultimately depend on a variety of factors, such as how widely used the kfunc is, how long the kfunc has been in the kernel, whether an alternative kfunc exists, what the norm is in terms of stability for the subsystem in question, and of course what the technical cost is of continuing to support the kfunc.}(hjrhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMMhjShhubh)}(h'There are several implications of this:h]h'There are several implications of this:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMUhjShhubj3)}(hhh](j8)}(hX<kfuncs that are widely used or have been in the kernel for a long time will be more difficult to justify being changed or removed by a maintainer. In other words, kfuncs that are known to have a lot of users and provide significant value provide stronger incentives for maintainers to invest the time and complexity in supporting them. It is therefore important for developers that are using kfuncs in their BPF programs to communicate and explain how and why those kfuncs are being used, and to participate in discussions regarding those kfuncs when they occur upstream. h]h)}(hX;kfuncs that are widely used or have been in the kernel for a long time will be more difficult to justify being changed or removed by a maintainer. In other words, kfuncs that are known to have a lot of users and provide significant value provide stronger incentives for maintainers to invest the time and complexity in supporting them. It is therefore important for developers that are using kfuncs in their BPF programs to communicate and explain how and why those kfuncs are being used, and to participate in discussions regarding those kfuncs when they occur upstream.h]hX;kfuncs that are widely used or have been in the kernel for a long time will be more difficult to justify being changed or removed by a maintainer. In other words, kfuncs that are known to have a lot of users and provide significant value provide stronger incentives for maintainers to invest the time and complexity in supporting them. It is therefore important for developers that are using kfuncs in their BPF programs to communicate and explain how and why those kfuncs are being used, and to participate in discussions regarding those kfuncs when they occur upstream.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMWhjubah}(h]h ]h"]h$]h&]uh1j7hjhhhhhNubj8)}(hXUnlike regular kernel symbols marked with EXPORT_SYMBOL_GPL, BPF programs that call kfuncs are generally not part of the kernel tree. This means that refactoring cannot typically change callers in-place when a kfunc changes, as is done for e.g. an upstreamed driver being updated in place when a kernel symbol is changed. Unlike with regular kernel symbols, this is expected behavior for BPF symbols, and out-of-tree BPF programs that use kfuncs should be considered relevant to discussions and decisions around modifying and removing those kfuncs. The BPF community will take an active role in participating in upstream discussions when necessary to ensure that the perspectives of such users are taken into account. h](h)}(hXAUnlike regular kernel symbols marked with EXPORT_SYMBOL_GPL, BPF programs that call kfuncs are generally not part of the kernel tree. This means that refactoring cannot typically change callers in-place when a kfunc changes, as is done for e.g. an upstreamed driver being updated in place when a kernel symbol is changed.h]hXAUnlike regular kernel symbols marked with EXPORT_SYMBOL_GPL, BPF programs that call kfuncs are generally not part of the kernel tree. This means that refactoring cannot typically change callers in-place when a kfunc changes, as is done for e.g. an upstreamed driver being updated in place when a kernel symbol is changed.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM`hjubh)}(hXUnlike with regular kernel symbols, this is expected behavior for BPF symbols, and out-of-tree BPF programs that use kfuncs should be considered relevant to discussions and decisions around modifying and removing those kfuncs. The BPF community will take an active role in participating in upstream discussions when necessary to ensure that the perspectives of such users are taken into account.h]hXUnlike with regular kernel symbols, this is expected behavior for BPF symbols, and out-of-tree BPF programs that use kfuncs should be considered relevant to discussions and decisions around modifying and removing those kfuncs. The BPF community will take an active role in participating in upstream discussions when necessary to ensure that the perspectives of such users are taken into account.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMfhjubeh}(h]h ]h"]h$]h&]uh1j7hjhhhhhNubj8)}(hXA kfunc will never have any hard stability guarantees. BPF APIs cannot and will not ever hard-block a change in the kernel purely for stability reasons. That being said, kfuncs are features that are meant to solve problems and provide value to users. The decision of whether to change or remove a kfunc is a multivariate technical decision that is made on a case-by-case basis, and which is informed by data points such as those mentioned above. It is expected that a kfunc being removed or changed with no warning will not be a common occurrence or take place without sound justification, but it is a possibility that must be accepted if one is to use kfuncs. h]h)}(hXA kfunc will never have any hard stability guarantees. BPF APIs cannot and will not ever hard-block a change in the kernel purely for stability reasons. That being said, kfuncs are features that are meant to solve problems and provide value to users. The decision of whether to change or remove a kfunc is a multivariate technical decision that is made on a case-by-case basis, and which is informed by data points such as those mentioned above. It is expected that a kfunc being removed or changed with no warning will not be a common occurrence or take place without sound justification, but it is a possibility that must be accepted if one is to use kfuncs.h]hXA kfunc will never have any hard stability guarantees. BPF APIs cannot and will not ever hard-block a change in the kernel purely for stability reasons. That being said, kfuncs are features that are meant to solve problems and provide value to users. The decision of whether to change or remove a kfunc is a multivariate technical decision that is made on a case-by-case basis, and which is informed by data points such as those mentioned above. It is expected that a kfunc being removed or changed with no warning will not be a common occurrence or take place without sound justification, but it is a possibility that must be accepted if one is to use kfuncs.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMmhjubah}(h]h ]h"]h$]h&]uh1j7hjhhhhhNubeh}(h]h ]h"]h$]h&]jn loweralphajphjq)uh1j2hjShhhhhMWubh)}(hhh](h)}(h3.1 kfunc deprecationh]h3.1 kfunc deprecation}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMyubh)}(hXAs described above, while sometimes a maintainer may find that a kfunc must be changed or removed immediately to accommodate some changes in their subsystem, usually kfuncs will be able to accommodate a longer and more measured deprecation process. For example, if a new kfunc comes along which provides superior functionality to an existing kfunc, the existing kfunc may be deprecated for some period of time to allow users to migrate their BPF programs to use the new one. Or, if a kfunc has no known users, a decision may be made to remove the kfunc (without providing an alternative API) after some deprecation period so as to provide users with a window to notify the kfunc maintainer if it turns out that the kfunc is actually being used.h]hXAs described above, while sometimes a maintainer may find that a kfunc must be changed or removed immediately to accommodate some changes in their subsystem, usually kfuncs will be able to accommodate a longer and more measured deprecation process. For example, if a new kfunc comes along which provides superior functionality to an existing kfunc, the existing kfunc may be deprecated for some period of time to allow users to migrate their BPF programs to use the new one. Or, if a kfunc has no known users, a decision may be made to remove the kfunc (without providing an alternative API) after some deprecation period so as to provide users with a window to notify the kfunc maintainer if it turns out that the kfunc is actually being used.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM{hjhhubh)}(hXIt's expected that the common case will be that kfuncs will go through a deprecation period rather than being changed or removed without warning. As described in :ref:`KF_deprecated_flag`, the kfunc framework provides the KF_DEPRECATED flag to kfunc developers to signal to users that a kfunc has been deprecated. Once a kfunc has been marked with KF_DEPRECATED, the following procedure is followed for removal:h](hIt’s expected that the common case will be that kfuncs will go through a deprecation period rather than being changed or removed without warning. As described in }(hjhhhNhNubh)}(h:ref:`KF_deprecated_flag`h]j )}(hjh]hKF_deprecated_flag}(hjhhhNhNubah}(h]h ](jstdstd-refeh"]h$]h&]uh1j hjubah}(h]h ]h"]h$]h&]refdocj# refdomainj$reftyperef refexplicitrefwarnj)kf_deprecated_flaguh1hhhhMhjubh, the kfunc framework provides the KF_DEPRECATED flag to kfunc developers to signal to users that a kfunc has been deprecated. Once a kfunc has been marked with KF_DEPRECATED, the following procedure is followed for removal:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj3)}(hhh](j8)}(hXHAny relevant information for deprecated kfuncs is documented in the kfunc's kernel docs. This documentation will typically include the kfunc's expected remaining lifespan, a recommendation for new functionality that can replace the usage of the deprecated function (or an explanation as to why no such replacement exists), etc. h]h)}(hXGAny relevant information for deprecated kfuncs is documented in the kfunc's kernel docs. This documentation will typically include the kfunc's expected remaining lifespan, a recommendation for new functionality that can replace the usage of the deprecated function (or an explanation as to why no such replacement exists), etc.h]hXKAny relevant information for deprecated kfuncs is documented in the kfunc’s kernel docs. This documentation will typically include the kfunc’s expected remaining lifespan, a recommendation for new functionality that can replace the usage of the deprecated function (or an explanation as to why no such replacement exists), etc.}(hjGhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjCubah}(h]h ]h"]h$]h&]uh1j7hj@hhhhhNubj8)}(hX!The deprecated kfunc is kept in the kernel for some period of time after it was first marked as deprecated. This time period will be chosen on a case-by-case basis, and will typically depend on how widespread the use of the kfunc is, how long it has been in the kernel, and how hard it is to move to alternatives. This deprecation time period is "best effort", and as described :ref:`above`, circumstances may sometimes dictate that the kfunc be removed before the full intended deprecation period has elapsed. h]h)}(hX The deprecated kfunc is kept in the kernel for some period of time after it was first marked as deprecated. This time period will be chosen on a case-by-case basis, and will typically depend on how widespread the use of the kfunc is, how long it has been in the kernel, and how hard it is to move to alternatives. This deprecation time period is "best effort", and as described :ref:`above`, circumstances may sometimes dictate that the kfunc be removed before the full intended deprecation period has elapsed.h](hX~The deprecated kfunc is kept in the kernel for some period of time after it was first marked as deprecated. This time period will be chosen on a case-by-case basis, and will typically depend on how widespread the use of the kfunc is, how long it has been in the kernel, and how hard it is to move to alternatives. This deprecation time period is “best effort”, and as described }(hj_hhhNhNubh)}(h.:ref:`above`h]j )}(hjih]habove}(hjkhhhNhNubah}(h]h ](jstdstd-refeh"]h$]h&]uh1j hjgubah}(h]h ]h"]h$]h&]refdocj# refdomainjureftyperef refexplicitrefwarnj) bpf_kfunc_lifecycle_expectationsuh1hhhhMhj_ubhx, circumstances may sometimes dictate that the kfunc be removed before the full intended deprecation period has elapsed.}(hj_hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj[ubah}(h]h ]h"]h$]h&]uh1j7hj@hhhhhNubj8)}(hAfter the deprecation period the kfunc will be removed. At this point, BPF programs calling the kfunc will be rejected by the verifier. h]h)}(hAfter the deprecation period the kfunc will be removed. At this point, BPF programs calling the kfunc will be rejected by the verifier.h]hAfter the deprecation period the kfunc will be removed. At this point, BPF programs calling the kfunc will be rejected by the verifier.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1j7hj@hhhhhNubeh}(h]h ]h"]h$]h&]jnjojphjqjruh1j2hjhhhhhMubeh}(h]kfunc-deprecationah ]h"]3.1 kfunc deprecationah$]h&]uh1hhjShhhhhMyubeh}(h](kfunc-lifecycle-expectationsj?eh ]h"](3. kfunc lifecycle expectations bpf_kfunc_lifecycle_expectationseh$]h&]uh1hhhhhhhhMEj}jj5sj}j?j5subh)}(hhh](h)}(h4. Core kfuncsh]h4. Core kfuncs}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(hThe BPF subsystem provides a number of "core" kfuncs that are potentially applicable to a wide variety of different possible use cases and programs. Those kfuncs are documented here.h]hThe BPF subsystem provides a number of “core” kfuncs that are potentially applicable to a wide variety of different possible use cases and programs. Those kfuncs are documented here.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(hhh](h)}(h4.1 struct task_struct * kfuncsh]h4.1 struct task_struct * kfuncs}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(h]There are a number of kfuncs that allow ``struct task_struct *`` objects to be used as kptrs:h](h(There are a number of kfuncs that allow }(hjhhhNhNubj)}(h``struct task_struct *``h]hstruct task_struct *}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh objects to be used as kptrs:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubhindex)}(hhh]h}(h]h ]h"]h$]h&]entries](singlebpf_task_acquire (C function)c.bpf_task_acquirehNtauh1jhjhhhNhNubhdesc)}(hhh](hdesc_signature)}(hI__bpf_kfunc struct task_struct * bpf_task_acquire (struct task_struct *p)h]hdesc_signature_line)}(hG__bpf_kfunc struct task_struct *bpf_task_acquire(struct task_struct *p)h](h __bpf_kfunc}(hj6hhhNhNubhdesc_sig_space)}(h h]h }(hj@hhhNhNubah}(h]h ]wah"]h$]h&]uh1j>hj6hhhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:684: ./kernel/bpf/helpers.chM ubhdesc_sig_keyword)}(hstructh]hstruct}(hjRhhhNhNubah}(h]h ]kah"]h$]h&]uh1jPhj6hhhjOhM ubj?)}(h h]h }(hjahhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hj6hhhjOhM ubh)}(hhh]h desc_sig_name)}(h task_structh]h task_struct}(hjthhhNhNubah}(h]h ]nah"]h$]h&]uh1jrhjoubah}(h]h ]h"]h$]h&] refdomainj+reftype identifier reftargetjvmodnameN classnameN c:parent_keysphinx.domains.c LookupKey)}data]j ASTIdentifier)}jbpf_task_acquiresbc.bpf_task_acquireasbuh1hhj6hhhjOhM ubj?)}(h h]h }(hjhhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hj6hhhjOhM ubhdesc_sig_punctuation)}(hj h]h*}(hjhhhNhNubah}(h]h ]pah"]h$]h&]uh1jhj6hhhjOhM ubh desc_name)}(hbpf_task_acquireh]js)}(hjh]hbpf_task_acquire}(hjhhhNhNubah}(h]h ]jah"]h$]h&]uh1jrhjubah}(h]h ](sig-namedescnameeh"]h$]h&]hhuh1jhj6hhhjOhM ubhdesc_parameterlist)}(h(struct task_struct *p)h]hdesc_parameter)}(hstruct task_struct *ph](jQ)}(hjTh]hstruct}(hjhhhNhNubah}(h]h ]j]ah"]h$]h&]uh1jPhjubj?)}(h h]h }(hjhhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hjubh)}(hhh]js)}(h task_structh]h task_struct}(hjhhhNhNubah}(h]h ]jah"]h$]h&]uh1jrhjubah}(h]h ]h"]h$]h&] refdomainj+reftypej reftargetjmodnameN classnameNjj)}j]jc.bpf_task_acquireasbuh1hhjubj?)}(h h]h }(hjhhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hjubj)}(hj h]h*}(hj-hhhNhNubah}(h]h ]jah"]h$]h&]uh1jhjubjs)}(hjh]hp}(hj:hhhNhNubah}(h]h ]jah"]h$]h&]uh1jrhjubeh}(h]h ]h"]h$]h&]noemphhhuh1jhjubah}(h]h ]h"]h$]h&]hhuh1jhj6hhhjOhM ubeh}(h]h ]h"]h$]h&]hhƌ add_permalinkuh1j4sphinx_line_type declaratorhj0hhhjOhM ubah}(h]j'ah ](sig sig-objecteh"]h$]h&] is_multiline _toc_parts) _toc_namehuh1j.hjOhM hj+hhubh desc_content)}(hhh]h)}(hAcquire a reference to a task. A task acquired by this kfunc which is not stored in a map as a kptr, must be released by calling bpf_task_release().h]hAcquire a reference to a task. A task acquired by this kfunc which is not stored in a map as a kptr, must be released by calling bpf_task_release().}(hjmhhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:684: ./kernel/bpf/helpers.chM hjjhhubah}(h]h ]h"]h$]h&]uh1jhhj+hhhjOhM ubeh}(h]h ](j+functioneh"]h$]h&]domainj+objtypejdesctypejnoindex noindexentrynocontentsentryuh1j)hhhjhNhNubh container)}(h\**Parameters** ``struct task_struct *p`` The task on which a reference is being acquired.h](h)}(h**Parameters**h]hstrong)}(hjh]h Parameters}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:684: ./kernel/bpf/helpers.chM hjubhdefinition_list)}(hhh]hdefinition_list_item)}(hJ``struct task_struct *p`` The task on which a reference is being acquired.h](hterm)}(h``struct task_struct *p``h]j)}(hjh]hstruct task_struct *p}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1jhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:684: ./kernel/bpf/helpers.chM hjubh definition)}(hhh]h)}(h0The task on which a reference is being acquired.h]h0The task on which a reference is being acquired.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:684: ./kernel/bpf/helpers.chM hjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhjhM hjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ] kernelindentah"]h$]h&]uh1jhjhhhNhNubj)}(hhh]h}(h]h ]h"]h$]h&]entries](j%bpf_task_release (C function)c.bpf_task_releasehNtauh1jhjhhhNhNubj*)}(hhh](j/)}(h9__bpf_kfunc void bpf_task_release (struct task_struct *p)h]j5)}(h8__bpf_kfunc void bpf_task_release(struct task_struct *p)h](h __bpf_kfunc}(hjhhhNhNubj?)}(h h]h }(hjhhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hjhhhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:684: ./kernel/bpf/helpers.chM ubhdesc_sig_keyword_type)}(hvoidh]hvoid}(hj/hhhNhNubah}(h]h ]ktah"]h$]h&]uh1j-hjhhhj,hM ubj?)}(h h]h }(hj>hhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hjhhhj,hM ubj)}(hbpf_task_releaseh]js)}(hbpf_task_releaseh]hbpf_task_release}(hjPhhhNhNubah}(h]h ]jah"]h$]h&]uh1jrhjLubah}(h]h ](jjeh"]h$]h&]hhuh1jhjhhhj,hM ubj)}(h(struct task_struct *p)h]j)}(hstruct task_struct *ph](jQ)}(hjTh]hstruct}(hjlhhhNhNubah}(h]h ]j]ah"]h$]h&]uh1jPhjhubj?)}(h h]h }(hjyhhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hjhubh)}(hhh]js)}(h task_structh]h task_struct}(hjhhhNhNubah}(h]h ]jah"]h$]h&]uh1jrhjubah}(h]h ]h"]h$]h&] refdomainj+reftypej reftargetjmodnameN classnameNjj)}j]j)}jjRsbc.bpf_task_releaseasbuh1hhjhubj?)}(h h]h }(hjhhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hjhubj)}(hj h]h*}(hjhhhNhNubah}(h]h ]jah"]h$]h&]uh1jhjhubjs)}(hjh]hp}(hjhhhNhNubah}(h]h ]jah"]h$]h&]uh1jrhjhubeh}(h]h ]h"]h$]h&]noemphhhuh1jhjdubah}(h]h ]h"]h$]h&]hhuh1jhjhhhj,hM ubeh}(h]h ]h"]h$]h&]hhjZuh1j4j[j\hjhhhj,hM ubah}(h]j ah ](j`jaeh"]h$]h&]jejf)jghuh1j.hj,hM hjhhubji)}(hhh]h)}(h)Release the reference acquired on a task.h]h)Release the reference acquired on a task.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:684: ./kernel/bpf/helpers.chM hjhhubah}(h]h ]h"]h$]h&]uh1jhhjhhhj,hM ubeh}(h]h ](j+functioneh"]h$]h&]jj+jjjjjjjuh1j)hhhjhNhNubj)}(h\**Parameters** ``struct task_struct *p`` The task on which a reference is being released.h](h)}(h**Parameters**h]j)}(hjh]h Parameters}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:684: ./kernel/bpf/helpers.chM hj ubj)}(hhh]j)}(hJ``struct task_struct *p`` The task on which a reference is being released.h](j)}(h``struct task_struct *p``h]j)}(hj/h]hstruct task_struct *p}(hj1hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj-ubah}(h]h ]h"]h$]h&]uh1jhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:684: ./kernel/bpf/helpers.chM hj)ubj)}(hhh]h)}(h0The task on which a reference is being released.h]h0The task on which a reference is being released.}(hjHhhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:684: ./kernel/bpf/helpers.chM hjEubah}(h]h ]h"]h$]h&]uh1jhj)ubeh}(h]h ]h"]h$]h&]uh1jhjDhM hj&ubah}(h]h ]h"]h$]h&]uh1jhj ubeh}(h]h ] kernelindentah"]h$]h&]uh1jhjhhhNhNubh)}(hThese kfuncs are useful when you want to acquire or release a reference to a ``struct task_struct *`` that was passed as e.g. a tracepoint arg, or a struct_ops callback arg. For example:h](hMThese kfuncs are useful when you want to acquire or release a reference to a }(hjphhhNhNubj)}(h``struct task_struct *``h]hstruct task_struct *}(hjxhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjpubhU that was passed as e.g. a tracepoint arg, or a struct_ops callback arg. For example:}(hjphhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(hX/** * A trivial example tracepoint program that shows how to * acquire and release a struct task_struct * pointer. */ SEC("tp_btf/task_newtask") int BPF_PROG(task_acquire_release_example, struct task_struct *task, u64 clone_flags) { struct task_struct *acquired; acquired = bpf_task_acquire(task); if (acquired) /* * In a typical program you'd do something like store * the task in a map, and the map will automatically * release it later. Here, we release it manually. */ bpf_task_release(acquired); return 0; }h]hX/** * A trivial example tracepoint program that shows how to * acquire and release a struct task_struct * pointer. */ SEC("tp_btf/task_newtask") int BPF_PROG(task_acquire_release_example, struct task_struct *task, u64 clone_flags) { struct task_struct *acquired; acquired = bpf_task_acquire(task); if (acquired) /* * In a typical program you'd do something like store * the task in a map, and the map will automatically * release it later. Here, we release it manually. */ bpf_task_release(acquired); return 0; }}hjsbah}(h]h ]h"]h$]h&]hhj)j*j+j,}uh1jhhhMhjhhubh)}(hReferences acquired on ``struct task_struct *`` objects are RCU protected. Therefore, when in an RCU read region, you can obtain a pointer to a task embedded in a map value without having to acquire a reference:h](hReferences acquired on }(hjhhhNhNubj)}(h``struct task_struct *``h]hstruct task_struct *}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh objects are RCU protected. Therefore, when in an RCU read region, you can obtain a pointer to a task embedded in a map value without having to acquire a reference:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(hX#define private(name) SEC(".data." #name) __hidden __attribute__((aligned(8))) private(TASK) static struct task_struct *global; /** * A trivial example showing how to access a task stored * in a map using RCU. */ SEC("tp_btf/task_newtask") int BPF_PROG(task_rcu_read_example, struct task_struct *task, u64 clone_flags) { struct task_struct *local_copy; bpf_rcu_read_lock(); local_copy = global; if (local_copy) /* * We could also pass local_copy to kfuncs or helper functions here, * as we're guaranteed that local_copy will be valid until we exit * the RCU read region below. */ bpf_printk("Global task %s is valid", local_copy->comm); else bpf_printk("No global task found"); bpf_rcu_read_unlock(); /* At this point we can no longer reference local_copy. */ return 0; }h]hX#define private(name) SEC(".data." #name) __hidden __attribute__((aligned(8))) private(TASK) static struct task_struct *global; /** * A trivial example showing how to access a task stored * in a map using RCU. */ SEC("tp_btf/task_newtask") int BPF_PROG(task_rcu_read_example, struct task_struct *task, u64 clone_flags) { struct task_struct *local_copy; bpf_rcu_read_lock(); local_copy = global; if (local_copy) /* * We could also pass local_copy to kfuncs or helper functions here, * as we're guaranteed that local_copy will be valid until we exit * the RCU read region below. */ bpf_printk("Global task %s is valid", local_copy->comm); else bpf_printk("No global task found"); bpf_rcu_read_unlock(); /* At this point we can no longer reference local_copy. */ return 0; }}hjsbah}(h]h ]h"]h$]h&]hhj)j*j+j,}uh1jhhhMhjhhubh transition)}(h----h]h}(h]h ]h"]h$]h&]uh1jhhhMhjhhubh)}(hA BPF program can also look up a task from a pid. This can be useful if the caller doesn't have a trusted pointer to a ``struct task_struct *`` object that it can acquire a reference on with bpf_task_acquire().h](hyA BPF program can also look up a task from a pid. This can be useful if the caller doesn’t have a trusted pointer to a }(hjhhhNhNubj)}(h``struct task_struct *``h]hstruct task_struct *}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhC object that it can acquire a reference on with bpf_task_acquire().}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(hhh]h}(h]h ]h"]h$]h&]entries](j%bpf_task_from_pid (C function)c.bpf_task_from_pidhNtauh1jhjhhhNhNubj*)}(hhh](j/)}(h<__bpf_kfunc struct task_struct * bpf_task_from_pid (s32 pid)h]j5)}(h:__bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid)h](h __bpf_kfunc}(hjhhhNhNubj?)}(h h]h }(hjhhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hjhhhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:756: ./kernel/bpf/helpers.chM ubjQ)}(hjTh]hstruct}(hj&hhhNhNubah}(h]h ]j]ah"]h$]h&]uh1jPhjhhhj%hM ubj?)}(h h]h }(hj3hhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hjhhhj%hM ubh)}(hhh]js)}(h task_structh]h task_struct}(hjDhhhNhNubah}(h]h ]jah"]h$]h&]uh1jrhjAubah}(h]h ]h"]h$]h&] refdomainj+reftypej reftargetjFmodnameN classnameNjj)}j]j)}jbpf_task_from_pidsbc.bpf_task_from_pidasbuh1hhjhhhj%hM ubj?)}(h h]h }(hjehhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hjhhhj%hM ubj)}(hj h]h*}(hjshhhNhNubah}(h]h ]jah"]h$]h&]uh1jhjhhhj%hM ubj)}(hbpf_task_from_pidh]js)}(hjbh]hbpf_task_from_pid}(hjhhhNhNubah}(h]h ]jah"]h$]h&]uh1jrhjubah}(h]h ](jjeh"]h$]h&]hhuh1jhjhhhj%hM ubj)}(h (s32 pid)h]j)}(hs32 pidh](h)}(hhh]js)}(hs32h]hs32}(hjhhhNhNubah}(h]h ]jah"]h$]h&]uh1jrhjubah}(h]h ]h"]h$]h&] refdomainj+reftypej reftargetjmodnameN classnameNjj)}j]j`c.bpf_task_from_pidasbuh1hhjubj?)}(h h]h }(hjhhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hjubjs)}(hpidh]hpid}(hjhhhNhNubah}(h]h ]jah"]h$]h&]uh1jrhjubeh}(h]h ]h"]h$]h&]noemphhhuh1jhjubah}(h]h ]h"]h$]h&]hhuh1jhjhhhj%hM ubeh}(h]h ]h"]h$]h&]hhjZuh1j4j[j\hj hhhj%hM ubah}(h]jah ](j`jaeh"]h$]h&]jejf)jghuh1j.hj%hM hjhhubji)}(hhh]h)}(hFind a struct task_struct from its pid by looking it up in the root pid namespace idr. If a task is returned, it must either be stored in a map, or released with bpf_task_release().h]hFind a struct task_struct from its pid by looking it up in the root pid namespace idr. If a task is returned, it must either be stored in a map, or released with bpf_task_release().}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:756: ./kernel/bpf/helpers.chM hjhhubah}(h]h ]h"]h$]h&]uh1jhhjhhhj%hM ubeh}(h]h ](j+functioneh"]h$]h&]jj+jjjjjjjuh1j)hhhjhNhNubj)}(hB**Parameters** ``s32 pid`` The pid of the task being looked up.h](h)}(h**Parameters**h]j)}(hjh]h Parameters}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:756: ./kernel/bpf/helpers.chM hjubj)}(hhh]j)}(h0``s32 pid`` The pid of the task being looked up.h](j)}(h ``s32 pid``h]j)}(hj9h]hs32 pid}(hj;hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj7ubah}(h]h ]h"]h$]h&]uh1jhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:756: ./kernel/bpf/helpers.chM hj3ubj)}(hhh]h)}(h$The pid of the task being looked up.h]h$The pid of the task being looked up.}(hjRhhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:756: ./kernel/bpf/helpers.chM hjOubah}(h]h ]h"]h$]h&]uh1jhj3ubeh}(h]h ]h"]h$]h&]uh1jhjNhM hj0ubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ] kernelindentah"]h$]h&]uh1jhjhhhNhNubh)}(h$Here is an example of it being used:h]h$Here is an example of it being used:}(hjzhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(hXSEC("tp_btf/task_newtask") int BPF_PROG(task_get_pid_example, struct task_struct *task, u64 clone_flags) { struct task_struct *lookup; lookup = bpf_task_from_pid(task->pid); if (!lookup) /* A task should always be found, as %task is a tracepoint arg. */ return -ENOENT; if (lookup->pid != task->pid) { /* bpf_task_from_pid() looks up the task via its * globally-unique pid from the init_pid_ns. Thus, * the pid of the lookup task should always be the * same as the input task. */ bpf_task_release(lookup); return -EINVAL; } /* bpf_task_from_pid() returns an acquired reference, * so it must be dropped before returning from the * tracepoint handler. */ bpf_task_release(lookup); return 0; }h]hXSEC("tp_btf/task_newtask") int BPF_PROG(task_get_pid_example, struct task_struct *task, u64 clone_flags) { struct task_struct *lookup; lookup = bpf_task_from_pid(task->pid); if (!lookup) /* A task should always be found, as %task is a tracepoint arg. */ return -ENOENT; if (lookup->pid != task->pid) { /* bpf_task_from_pid() looks up the task via its * globally-unique pid from the init_pid_ns. Thus, * the pid of the lookup task should always be the * same as the input task. */ bpf_task_release(lookup); return -EINVAL; } /* bpf_task_from_pid() returns an acquired reference, * so it must be dropped before returning from the * tracepoint handler. */ bpf_task_release(lookup); return 0; }}hjsbah}(h]h ]h"]h$]h&]hhj)j*j+j,}uh1jhhhMhjhhubeh}(h]struct-task-struct-kfuncsah ]h"]4.1 struct task_struct * kfuncsah$]h&]uh1hhjhhhhhMubh)}(hhh](h)}(h4.2 struct cgroup * kfuncsh]h4.2 struct cgroup * kfuncs}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(hD``struct cgroup *`` objects also have acquire and release functions:h](j)}(h``struct cgroup *``h]hstruct cgroup *}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh1 objects also have acquire and release functions:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(hhh]h}(h]h ]h"]h$]h&]entries](j%bpf_cgroup_acquire (C function)c.bpf_cgroup_acquirehNtauh1jhjhhhNhNubj*)}(hhh](j/)}(hD__bpf_kfunc struct cgroup * bpf_cgroup_acquire (struct cgroup *cgrp)h]j5)}(hB__bpf_kfunc struct cgroup *bpf_cgroup_acquire(struct cgroup *cgrp)h](h __bpf_kfunc}(hjhhhNhNubj?)}(h h]h }(hjhhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hjhhhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:796: ./kernel/bpf/helpers.chM& ubjQ)}(hjTh]hstruct}(hjhhhNhNubah}(h]h ]j]ah"]h$]h&]uh1jPhjhhhjhM& ubj?)}(h h]h }(hjhhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hjhhhjhM& ubh)}(hhh]js)}(hcgrouph]hcgroup}(hjhhhNhNubah}(h]h ]jah"]h$]h&]uh1jrhjubah}(h]h ]h"]h$]h&] refdomainj+reftypej reftargetjmodnameN classnameNjj)}j]j)}jbpf_cgroup_acquiresbc.bpf_cgroup_acquireasbuh1hhjhhhjhM& ubj?)}(h h]h }(hj7hhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hjhhhjhM& ubj)}(hj h]h*}(hjEhhhNhNubah}(h]h ]jah"]h$]h&]uh1jhjhhhjhM& ubj)}(hbpf_cgroup_acquireh]js)}(hj4h]hbpf_cgroup_acquire}(hjVhhhNhNubah}(h]h ]jah"]h$]h&]uh1jrhjRubah}(h]h ](jjeh"]h$]h&]hhuh1jhjhhhjhM& ubj)}(h(struct cgroup *cgrp)h]j)}(hstruct cgroup *cgrph](jQ)}(hjTh]hstruct}(hjqhhhNhNubah}(h]h ]j]ah"]h$]h&]uh1jPhjmubj?)}(h h]h }(hj~hhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hjmubh)}(hhh]js)}(hcgrouph]hcgroup}(hjhhhNhNubah}(h]h ]jah"]h$]h&]uh1jrhjubah}(h]h ]h"]h$]h&] refdomainj+reftypej reftargetjmodnameN classnameNjj)}j]j2c.bpf_cgroup_acquireasbuh1hhjmubj?)}(h h]h }(hjhhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hjmubj)}(hj h]h*}(hjhhhNhNubah}(h]h ]jah"]h$]h&]uh1jhjmubjs)}(hcgrph]hcgrp}(hjhhhNhNubah}(h]h ]jah"]h$]h&]uh1jrhjmubeh}(h]h ]h"]h$]h&]noemphhhuh1jhjiubah}(h]h ]h"]h$]h&]hhuh1jhjhhhjhM& ubeh}(h]h ]h"]h$]h&]hhjZuh1j4j[j\hjhhhjhM& ubah}(h]jah ](j`jaeh"]h$]h&]jejf)jghuh1j.hjhM& hjhhubji)}(hhh]h)}(hAcquire a reference to a cgroup. A cgroup acquired by this kfunc which is not stored in a map as a kptr, must be released by calling bpf_cgroup_release().h]hAcquire a reference to a cgroup. A cgroup acquired by this kfunc which is not stored in a map as a kptr, must be released by calling bpf_cgroup_release().}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:796: ./kernel/bpf/helpers.chM& hjhhubah}(h]h ]h"]h$]h&]uh1jhhjhhhjhM& ubeh}(h]h ](j+functioneh"]h$]h&]jj+jj jj jjjuh1j)hhhjhNhNubj)}(h\**Parameters** ``struct cgroup *cgrp`` The cgroup on which a reference is being acquired.h](h)}(h**Parameters**h]j)}(hjh]h Parameters}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:796: ./kernel/bpf/helpers.chM* hjubj)}(hhh]j)}(hJ``struct cgroup *cgrp`` The cgroup on which a reference is being acquired.h](j)}(h``struct cgroup *cgrp``h]j)}(hj3h]hstruct cgroup *cgrp}(hj5hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj1ubah}(h]h ]h"]h$]h&]uh1jhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:796: ./kernel/bpf/helpers.chM, hj-ubj)}(hhh]h)}(h2The cgroup on which a reference is being acquired.h]h2The cgroup on which a reference is being acquired.}(hjLhhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:796: ./kernel/bpf/helpers.chM) hjIubah}(h]h ]h"]h$]h&]uh1jhj-ubeh}(h]h ]h"]h$]h&]uh1jhjHhM, hj*ubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ] kernelindentah"]h$]h&]uh1jhjhhhNhNubj)}(hhh]h}(h]h ]h"]h$]h&]entries](j%bpf_cgroup_release (C function)c.bpf_cgroup_releasehNtauh1jhjhhhNhNubj*)}(hhh](j/)}(h9__bpf_kfunc void bpf_cgroup_release (struct cgroup *cgrp)h]j5)}(h8__bpf_kfunc void bpf_cgroup_release(struct cgroup *cgrp)h](h __bpf_kfunc}(hjhhhNhNubj?)}(h h]h }(hjhhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hjhhhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:796: ./kernel/bpf/helpers.chM1 ubj.)}(hvoidh]hvoid}(hjhhhNhNubah}(h]h ]j:ah"]h$]h&]uh1j-hjhhhjhM1 ubj?)}(h h]h }(hjhhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hjhhhjhM1 ubj)}(hbpf_cgroup_releaseh]js)}(hbpf_cgroup_releaseh]hbpf_cgroup_release}(hjhhhNhNubah}(h]h ]jah"]h$]h&]uh1jrhjubah}(h]h ](jjeh"]h$]h&]hhuh1jhjhhhjhM1 ubj)}(h(struct cgroup *cgrp)h]j)}(hstruct cgroup *cgrph](jQ)}(hjTh]hstruct}(hjhhhNhNubah}(h]h ]j]ah"]h$]h&]uh1jPhjubj?)}(h h]h }(hjhhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hjubh)}(hhh]js)}(hcgrouph]hcgroup}(hjhhhNhNubah}(h]h ]jah"]h$]h&]uh1jrhjubah}(h]h ]h"]h$]h&] refdomainj+reftypej reftargetjmodnameN classnameNjj)}j]j)}jjsbc.bpf_cgroup_releaseasbuh1hhjubj?)}(h h]h }(hjhhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hjubj)}(hj h]h*}(hj(hhhNhNubah}(h]h ]jah"]h$]h&]uh1jhjubjs)}(hcgrph]hcgrp}(hj5hhhNhNubah}(h]h ]jah"]h$]h&]uh1jrhjubeh}(h]h ]h"]h$]h&]noemphhhuh1jhjubah}(h]h ]h"]h$]h&]hhuh1jhjhhhjhM1 ubeh}(h]h ]h"]h$]h&]hhjZuh1j4j[j\hjhhhjhM1 ubah}(h]jah ](j`jaeh"]h$]h&]jejf)jghuh1j.hjhM1 hjhhubji)}(hhh]h)}(hRelease the reference acquired on a cgroup. If this kfunc is invoked in an RCU read region, the cgroup is guaranteed to not be freed until the current grace period has ended, even if its refcount drops to 0.h]hRelease the reference acquired on a cgroup. If this kfunc is invoked in an RCU read region, the cgroup is guaranteed to not be freed until the current grace period has ended, even if its refcount drops to 0.}(hj_hhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:796: ./kernel/bpf/helpers.chM1 hj\hhubah}(h]h ]h"]h$]h&]uh1jhhjhhhjhM1 ubeh}(h]h ](j+functioneh"]h$]h&]jj+jjwjjwjjjuh1j)hhhjhNhNubj)}(h\**Parameters** ``struct cgroup *cgrp`` The cgroup on which a reference is being released.h](h)}(h**Parameters**h]j)}(hjh]h Parameters}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:796: ./kernel/bpf/helpers.chM5 hj{ubj)}(hhh]j)}(hJ``struct cgroup *cgrp`` The cgroup on which a reference is being released.h](j)}(h``struct cgroup *cgrp``h]j)}(hjh]hstruct cgroup *cgrp}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1jhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:796: ./kernel/bpf/helpers.chM7 hjubj)}(hhh]h)}(h2The cgroup on which a reference is being released.h]h2The cgroup on which a reference is being released.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:796: ./kernel/bpf/helpers.chM5 hjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhjhM7 hjubah}(h]h ]h"]h$]h&]uh1jhj{ubeh}(h]h ] kernelindentah"]h$]h&]uh1jhjhhhNhNubh)}(hThese kfuncs are used in exactly the same manner as bpf_task_acquire() and bpf_task_release() respectively, so we won't provide examples for them.h]hThese kfuncs are used in exactly the same manner as bpf_task_acquire() and bpf_task_release() respectively, so we won’t provide examples for them.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(h----h]h}(h]h ]h"]h$]h&]uh1jhhhM"hjhhubh)}(hOther kfuncs available for interacting with ``struct cgroup *`` objects are bpf_cgroup_ancestor() and bpf_cgroup_from_id(), allowing callers to access the ancestor of a cgroup and find a cgroup by its ID, respectively. Both return a cgroup kptr.h](h,Other kfuncs available for interacting with }(hjhhhNhNubj)}(h``struct cgroup *``h]hstruct cgroup *}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh objects are bpf_cgroup_ancestor() and bpf_cgroup_from_id(), allowing callers to access the ancestor of a cgroup and find a cgroup by its ID, respectively. Both return a cgroup kptr.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM$hjhhubj)}(hhh]h}(h]h ]h"]h$]h&]entries](j% bpf_cgroup_ancestor (C function)c.bpf_cgroup_ancestorhNtauh1jhjhhhNhNubj*)}(hhh](j/)}(hP__bpf_kfunc struct cgroup * bpf_cgroup_ancestor (struct cgroup *cgrp, int level)h]j5)}(hN__bpf_kfunc struct cgroup *bpf_cgroup_ancestor(struct cgroup *cgrp, int level)h](h __bpf_kfunc}(hj.hhhNhNubj?)}(h h]h }(hj6hhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hj.hhhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:809: ./kernel/bpf/helpers.chMC ubjQ)}(hjTh]hstruct}(hjEhhhNhNubah}(h]h ]j]ah"]h$]h&]uh1jPhj.hhhjDhMC ubj?)}(h h]h }(hjRhhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hj.hhhjDhMC ubh)}(hhh]js)}(hcgrouph]hcgroup}(hjchhhNhNubah}(h]h ]jah"]h$]h&]uh1jrhj`ubah}(h]h ]h"]h$]h&] refdomainj+reftypej reftargetjemodnameN classnameNjj)}j]j)}jbpf_cgroup_ancestorsbc.bpf_cgroup_ancestorasbuh1hhj.hhhjDhMC ubj?)}(h h]h }(hjhhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hj.hhhjDhMC ubj)}(hj h]h*}(hjhhhNhNubah}(h]h ]jah"]h$]h&]uh1jhj.hhhjDhMC ubj)}(hbpf_cgroup_ancestorh]js)}(hjh]hbpf_cgroup_ancestor}(hjhhhNhNubah}(h]h ]jah"]h$]h&]uh1jrhjubah}(h]h ](jjeh"]h$]h&]hhuh1jhj.hhhjDhMC ubj)}(h (struct cgroup *cgrp, int level)h](j)}(hstruct cgroup *cgrph](jQ)}(hjTh]hstruct}(hjhhhNhNubah}(h]h ]j]ah"]h$]h&]uh1jPhjubj?)}(h h]h }(hjhhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hjubh)}(hhh]js)}(hcgrouph]hcgroup}(hjhhhNhNubah}(h]h ]jah"]h$]h&]uh1jrhjubah}(h]h ]h"]h$]h&] refdomainj+reftypej reftargetjmodnameN classnameNjj)}j]jc.bpf_cgroup_ancestorasbuh1hhjubj?)}(h h]h }(hjhhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hjubj)}(hj h]h*}(hjhhhNhNubah}(h]h ]jah"]h$]h&]uh1jhjubjs)}(hcgrph]hcgrp}(hjhhhNhNubah}(h]h ]jah"]h$]h&]uh1jrhjubeh}(h]h ]h"]h$]h&]noemphhhuh1jhjubj)}(h int levelh](j.)}(hinth]hint}(hj.hhhNhNubah}(h]h ]j:ah"]h$]h&]uh1j-hj*ubj?)}(h h]h }(hj<hhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hj*ubjs)}(hlevelh]hlevel}(hjJhhhNhNubah}(h]h ]jah"]h$]h&]uh1jrhj*ubeh}(h]h ]h"]h$]h&]noemphhhuh1jhjubeh}(h]h ]h"]h$]h&]hhuh1jhj.hhhjDhMC ubeh}(h]h ]h"]h$]h&]hhjZuh1j4j[j\hj*hhhjDhMC ubah}(h]j%ah ](j`jaeh"]h$]h&]jejf)jghuh1j.hjDhMC hj'hhubji)}(hhh]h)}(hPerform a lookup on an entry in a cgroup's ancestor array. A cgroup returned by this kfunc which is not subsequently stored in a map, must be released by calling bpf_cgroup_release().h]hPerform a lookup on an entry in a cgroup’s ancestor array. A cgroup returned by this kfunc which is not subsequently stored in a map, must be released by calling bpf_cgroup_release().}(hjthhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:809: ./kernel/bpf/helpers.chMC hjqhhubah}(h]h ]h"]h$]h&]uh1jhhj'hhhjDhMC ubeh}(h]h ](j+functioneh"]h$]h&]jj+jjjjjjjuh1j)hhhjhNhNubj)}(h**Parameters** ``struct cgroup *cgrp`` The cgroup for which we're performing a lookup. ``int level`` The level of ancestor to look up.h](h)}(h**Parameters**h]j)}(hjh]h Parameters}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:809: ./kernel/bpf/helpers.chMG hjubj)}(hhh](j)}(hH``struct cgroup *cgrp`` The cgroup for which we're performing a lookup. h](j)}(h``struct cgroup *cgrp``h]j)}(hjh]hstruct cgroup *cgrp}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1jhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:809: ./kernel/bpf/helpers.chMF hjubj)}(hhh]h)}(h/The cgroup for which we're performing a lookup.h]h1The cgroup for which we’re performing a lookup.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhMF hjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhjhMF hjubj)}(h/``int level`` The level of ancestor to look up.h](j)}(h ``int level``h]j)}(hjh]h int level}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1jhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:809: ./kernel/bpf/helpers.chMH hjubj)}(hhh]h)}(h!The level of ancestor to look up.h]h!The level of ancestor to look up.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:809: ./kernel/bpf/helpers.chMG hjubah}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ]h"]h$]h&]uh1jhjhMH hjubeh}(h]h ]h"]h$]h&]uh1jhjubeh}(h]h ] kernelindentah"]h$]h&]uh1jhjhhhNhNubj)}(hhh]h}(h]h ]h"]h$]h&]entries](j%bpf_cgroup_from_id (C function)c.bpf_cgroup_from_idhNtauh1jhjhhhNhNubj*)}(hhh](j/)}(h9__bpf_kfunc struct cgroup * bpf_cgroup_from_id (u64 cgid)h]j5)}(h7__bpf_kfunc struct cgroup *bpf_cgroup_from_id(u64 cgid)h](h __bpf_kfunc}(hjDhhhNhNubj?)}(h h]h }(hjLhhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hjDhhhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:812: ./kernel/bpf/helpers.chMX ubjQ)}(hjTh]hstruct}(hj[hhhNhNubah}(h]h ]j]ah"]h$]h&]uh1jPhjDhhhjZhMX ubj?)}(h h]h }(hjhhhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hjDhhhjZhMX ubh)}(hhh]js)}(hcgrouph]hcgroup}(hjyhhhNhNubah}(h]h ]jah"]h$]h&]uh1jrhjvubah}(h]h ]h"]h$]h&] refdomainj+reftypej reftargetj{modnameN classnameNjj)}j]j)}jbpf_cgroup_from_idsbc.bpf_cgroup_from_idasbuh1hhjDhhhjZhMX ubj?)}(h h]h }(hjhhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hjDhhhjZhMX ubj)}(hj h]h*}(hjhhhNhNubah}(h]h ]jah"]h$]h&]uh1jhjDhhhjZhMX ubj)}(hbpf_cgroup_from_idh]js)}(hjh]hbpf_cgroup_from_id}(hjhhhNhNubah}(h]h ]jah"]h$]h&]uh1jrhjubah}(h]h ](jjeh"]h$]h&]hhuh1jhjDhhhjZhMX ubj)}(h (u64 cgid)h]j)}(hu64 cgidh](h)}(hhh]js)}(hu64h]hu64}(hjhhhNhNubah}(h]h ]jah"]h$]h&]uh1jrhjubah}(h]h ]h"]h$]h&] refdomainj+reftypej reftargetjmodnameN classnameNjj)}j]jc.bpf_cgroup_from_idasbuh1hhjubj?)}(h h]h }(hjhhhNhNubah}(h]h ]jKah"]h$]h&]uh1j>hjubjs)}(hcgidh]hcgid}(hjhhhNhNubah}(h]h ]jah"]h$]h&]uh1jrhjubeh}(h]h ]h"]h$]h&]noemphhhuh1jhjubah}(h]h ]h"]h$]h&]hhuh1jhjDhhhjZhMX ubeh}(h]h ]h"]h$]h&]hhjZuh1j4j[j\hj@hhhjZhMX ubah}(h]j;ah ](j`jaeh"]h$]h&]jejf)jghuh1j.hjZhMX hj=hhubji)}(hhh]h)}(hFind a cgroup from its ID. A cgroup returned by this kfunc which is not subsequently stored in a map, must be released by calling bpf_cgroup_release().h]hFind a cgroup from its ID. A cgroup returned by this kfunc which is not subsequently stored in a map, must be released by calling bpf_cgroup_release().}(hj-hhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:812: ./kernel/bpf/helpers.chMX hj*hhubah}(h]h ]h"]h$]h&]uh1jhhj=hhhjZhMX ubeh}(h]h ](j+functioneh"]h$]h&]jj+jjEjjEjjjuh1j)hhhjhNhNubj)}(h)**Parameters** ``u64 cgid`` cgroup id.h](h)}(h**Parameters**h]j)}(hjOh]h Parameters}(hjQhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjMubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:812: ./kernel/bpf/helpers.chM\ hjIubj)}(hhh]j)}(h``u64 cgid`` cgroup id.h](j)}(h ``u64 cgid``h]j)}(hjnh]hu64 cgid}(hjphhhNhNubah}(h]h ]h"]h$]h&]uh1jhjlubah}(h]h ]h"]h$]h&]uh1jhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:812: ./kernel/bpf/helpers.chM^ hjhubj)}(hhh]h)}(h cgroup id.h]h cgroup id.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:812: ./kernel/bpf/helpers.chM[ hjubah}(h]h ]h"]h$]h&]uh1jhjhubeh}(h]h ]h"]h$]h&]uh1jhjhM^ hjeubah}(h]h ]h"]h$]h&]uh1jhjIubeh}(h]h ] kernelindentah"]h$]h&]uh1jhjhhhNhNubh)}(hEventually, BPF should be updated to allow this to happen with a normal memory load in the program itself. This is currently not possible without more work in the verifier. bpf_cgroup_ancestor() can be used as follows:h]hEventually, BPF should be updated to allow this to happen with a normal memory load in the program itself. This is currently not possible without more work in the verifier. bpf_cgroup_ancestor() can be used as follows:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM/hjhhubj)}(hX/** * Simple tracepoint example that illustrates how a cgroup's * ancestor can be accessed using bpf_cgroup_ancestor(). */ SEC("tp_btf/cgroup_mkdir") int BPF_PROG(cgrp_ancestor_example, struct cgroup *cgrp, const char *path) { struct cgroup *parent; /* The parent cgroup resides at the level before the current cgroup's level. */ parent = bpf_cgroup_ancestor(cgrp, cgrp->level - 1); if (!parent) return -ENOENT; bpf_printk("Parent id is %d", parent->self.id); /* Return the parent cgroup that was acquired above. */ bpf_cgroup_release(parent); return 0; }3h]hX/** * Simple tracepoint example that illustrates how a cgroup's * ancestor can be accessed using bpf_cgroup_ancestor(). */ SEC("tp_btf/cgroup_mkdir") int BPF_PROG(cgrp_ancestor_example, struct cgroup *cgrp, const char *path) { struct cgroup *parent; /* The parent cgroup resides at the level before the current cgroup's level. */ parent = bpf_cgroup_ancestor(cgrp, cgrp->level - 1); if (!parent) return -ENOENT; bpf_printk("Parent id is %d", parent->self.id); /* Return the parent cgroup that was acquired above. */ bpf_cgroup_release(parent); return 0; }}hjsbah}(h]h ]h"]h$]h&]hhj)j*j+j,}uh1jhhhM3hjhhubeh}(h]struct-cgroup-kfuncsah ]h"]4.2 struct cgroup * kfuncsah$]h&]uh1hhjhhhhhMubh)}(hhh](h)}(h4.3 struct cpumask * kfuncsh]h4.3 struct cpumask * kfuncs}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMKubh)}(hBPF provides a set of kfuncs that can be used to query, allocate, mutate, and destroy struct cpumask * objects. Please refer to :ref:`cpumasks-header-label` for more details.h](hBPF provides a set of kfuncs that can be used to query, allocate, mutate, and destroy struct cpumask * objects. Please refer to }(hjhhhNhNubh)}(h:ref:`cpumasks-header-label`h]j )}(hjh]hcpumasks-header-label}(hjhhhNhNubah}(h]h ](jstdstd-refeh"]h$]h&]uh1j hjubah}(h]h ]h"]h$]h&]refdocj# refdomainjreftyperef refexplicitrefwarnj)cpumasks-header-labeluh1hhhhMMhjubh for more details.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMMhjhhubeh}(h]struct-cpumask-kfuncsah ]h"]4.3 struct cpumask * kfuncsah$]h&]uh1hhjhhhhhMKubeh}(h] core-kfuncsah ]h"]4. core kfuncsah$]h&]uh1hhhhhhhhMubeh}(h](bpf-kernel-functions-kfuncsheh ]h"](bpf kernel functions (kfuncs)kfuncs-header-labeleh$]h&]uh1hhhhhhhhKj}j-hsj}hhsubeh}(h]h ]h"]h$]h&]sourcehuh1hcurrent_sourceN current_lineNsettingsdocutils.frontendValues)}(hN generatorN datestampN source_linkN source_urlN toc_backlinksentryfootnote_backlinksK sectnum_xformKstrip_commentsNstrip_elements_with_classesN strip_classesN report_levelK halt_levelKexit_status_levelKdebugNwarning_streamN tracebackinput_encoding utf-8-siginput_encoding_error_handlerstrictoutput_encodingutf-8output_encoding_error_handlerjUerror_encodingutf-8error_encoding_error_handlerbackslashreplace language_codeenrecord_dependenciesNconfigN id_prefixhauto_id_prefixid dump_settingsNdump_internalsNdump_transformsNdump_pseudo_xmlNexpose_internalsNstrict_visitorN_disable_configN_sourcehnj _destinationN _config_files]7/var/lib/git/docbuild/linux/Documentation/docutils.confafile_insertion_enabled raw_enabledKline_length_limitM'pep_referencesN pep_base_urlhttps://peps.python.org/pep_file_url_templatepep-%04drfc_referencesN rfc_base_url&https://datatracker.ietf.org/doc/html/ tab_widthKtrim_footnote_reference_spacesyntax_highlightlong smart_quotessmartquotes_locales]character_level_inline_markupdoctitle_xform docinfo_xformKsectsubtitle_xform image_loadinglinkembed_stylesheetcloak_email_addressessection_self_linkenvNubreporterNindirect_targets]substitution_defs}substitution_names}refnames}refids}(h]hajt]jjaj ]j aj ]j aj?]j5aunameids}(j-hj,j)j:j7jPjMjjjjj#j jfjcjjjjjjjjjjjjjzjwjjtjjj[ jX j j j j j j j j j j jE jB j j j j j j j j j_ j\ j j jFj jEjBjj?jjjjj$j!jjjjjju nametypes}(j-j,j:jPjjj#jfjjjjjjjzjjj[ j j j j j jE j j j j j_ j jFjEjjjj$jjjuh}(hhj)hj7hjMj=jjjjj jjcj&jjijjjjjjjjjjjwjjtj}jj}jX jj j^ j j j j j j j j jB j! j jH j j j j j j j\ j j jb j j jBj j?jSjjSjjj!jjjj'j0j jjj jjjjjjj%j*j;j@jju footnote_refs} citation_refs} autofootnotes]autofootnote_refs]symbol_footnotes]symbol_footnote_refs] footnotes] citations]autofootnote_startKsymbol_footnote_startK id_counter collectionsCounter}jcKsRparse_messages]transform_messages](hsystem_message)}(hhh]h)}(hhh]h9Hyperlink target "kfuncs-header-label" is not referenced.}hjsbah}(h]h ]h"]h$]h&]uh1hhjubah}(h]h ]h"]h$]h&]levelKtypeINFOsourcehnjlineKuh1jubj)}(hhh]h)}(hhh]h5Hyperlink target "bpf-kfunc-nodef" is not referenced.}hjsbah}(h]h ]h"]h$]h&]uh1hhjubah}(h]h ]h"]h$]h&]levelKtypejsourcehnjlineM>uh1jubj)}(hhh]h)}(hhh]h8Hyperlink target "kf-deprecated-flag" is not referenced.}hjsbah}(h]h ]h"]h$]h&]uh1hhjubah}(h]h ]h"]h$]h&]levelKtypejsourcehnjlineMuh1jubj)}(hhh]h)}(hhh]h