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]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)}(hhh]h BPF_kfunc_lifecycle_expectations}(hhhhhNhNubah}(h]h ](xrefstdstd-refeh"]h$]h&]uh1hhhubah}(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.}(hj:hhhNhNubah}(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 }(hjHhhhNhNubh)}(h:ref:`BPF_kfunc_nodef`h]h)}(hjRh]hBPF_kfunc_nodef}(hjThhhNhNubah}(h]h ](jstdstd-refeh"]h$]h&]uh1hhjPubah}(h]h ]h"]h$]h&]refdocj refdomainj^reftyperef refexplicitrefwarnjbpf_kfunc_nodefuh1hhhhKhjHubh.}(hjHhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj)hhubh)}(hhh](h)}(h2.1 Creating a wrapper kfunch]h2.1 Creating a wrapper kfunc}(hj}hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjzhhhhhKubh)}(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 hjzhhubh)}(hAn example is given below::h]hAn example is given below:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK%hjzhhubh 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'hjzhhubh)}(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]h)}(hjh]hBPF_kfunc_nodef}(hjhhhNhNubah}(h]h ](jstdstd-refeh"]h$]h&]uh1hhjubah}(h]h ]h"]h$]h&]refdocj refdomainjreftyperef refexplicitrefwarnjbpf_kfunc_nodefuh1hhhhK1hjubh.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhK1hjzhhubeh}(h]creating-a-wrapper-kfuncah ]h"]2.1 creating a wrapper kfuncah$]h&]uh1hhj)hhhhhKubh)}(hhh](h)}(h2.2 Annotating kfunc parametersh]h2.2 Annotating kfunc parameters}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhK6ubh)}(hX2Similar to BPF helpers, there is sometime need for additional context required by the verifier to make the usage of kernel functions safer and more useful. Hence, we can annotate a parameter by suffixing the name of the argument of the kfunc with a __tag, where tag may be one of the supported annotations.h]hX2Similar to BPF helpers, there is sometime need for additional context required by the verifier to make the usage of kernel functions safer and more useful. Hence, we can annotate a parameter by suffixing the name of the argument of the kfunc with a __tag, where tag may be one of the supported annotations.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK8hjhhubeh}(h]annotating-kfunc-parametersah ]h"]2.2 annotating kfunc parametersah$]h&]uh1hhj)hhhhhK6ubh)}(hhh](h)}(h2.2.1 __sz Annotationh]h2.2.1 __sz Annotation}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhK>ubh)}(hlThis annotation is used to indicate a memory and size pair in the argument list. An example is given below::h]hkThis annotation is used to indicate a memory and size pair in the argument list. An example is given below:}(hj)hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK@hjhhubj)}(h<__bpf_kfunc void bpf_memzero(void *mem, int mem__sz) { ... }h]h<__bpf_kfunc void bpf_memzero(void *mem, int mem__sz) { ... }}hj7sbah}(h]h ]h"]h$]h&]hhuh1jhhhKChjhhubh)}(hHere, the verifier will treat first argument as a PTR_TO_MEM, and second argument as its size. By default, without __sz annotation, the size of the type of the pointer is used. Without __sz annotation, a kfunc cannot accept a void pointer.h]hHere, the verifier will treat first argument as a PTR_TO_MEM, and second argument as its size. By default, without __sz annotation, the size of the type of the pointer is used. Without __sz annotation, a kfunc cannot accept a void pointer.}(hjEhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKHhjhhubeh}(h] sz-annotationah ]h"]2.2.1 __sz annotationah$]h&]uh1hhj)hhhhhK>ubh)}(hhh](h)}(h2.2.2 __k Annotationh]h2.2.2 __k Annotation}(hj^hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj[hhhhhKNubh)}(hXThis annotation is only understood for scalar arguments, where it indicates that the verifier must check the scalar argument to be a known constant, which does not indicate a size parameter, and the value of the constant is relevant to the safety of the program.h]hXThis annotation is only understood for scalar arguments, where it indicates that the verifier must check the scalar argument to be a known constant, which does not indicate a size parameter, and the value of the constant is relevant to the safety of the program.}(hjlhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKPhj[hhubh)}(hAn example is given below::h]hAn example is given below:}(hjzhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKUhj[hhubj)}(h@__bpf_kfunc void *bpf_obj_new(u32 local_type_id__k, ...) { ... }h]h@__bpf_kfunc void *bpf_obj_new(u32 local_type_id__k, ...) { ... }}hjsbah}(h]h ]h"]h$]h&]hhuh1jhhhKWhj[hhubh)}(hX#Here, bpf_obj_new uses local_type_id argument to find out the size of that type ID in program's BTF and return a sized pointer to it. Each type ID will have a distinct size, hence it is crucial to treat each such call as distinct when values don't match during verifier state pruning checks.h]hX'Here, bpf_obj_new uses local_type_id argument to find out the size of that type ID in program’s BTF and return a sized pointer to it. Each type ID will have a distinct size, hence it is crucial to treat each such call as distinct when values don’t match during verifier state pruning checks.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK\hj[hhubh)}(hHence, whenever a constant scalar argument is accepted by a kfunc which is not a size parameter, and the value of the constant matters for program safety, __k suffix should be used.h]hHence, whenever a constant scalar argument is accepted by a kfunc which is not a size parameter, and the value of the constant matters for program safety, __k suffix should be used.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKahj[hhubeh}(h] k-annotationah ]h"]2.2.2 __k annotationah$]h&]uh1hhj)hhhhhKNubh)}(hhh](h)}(h2.2.3 __uninit Annotationh]h2.2.3 __uninit Annotation}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKfubh)}(hWThis annotation is used to indicate that the argument will be treated as uninitialized.h]hWThis annotation is used to indicate that the argument will be treated as uninitialized.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhhjhhubh)}(hAn example is given below::h]hAn example is given below:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKkhjhhubj)}(hU__bpf_kfunc int bpf_dynptr_from_skb(..., struct bpf_dynptr_kern *ptr__uninit) { ... }h]hU__bpf_kfunc int bpf_dynptr_from_skb(..., struct bpf_dynptr_kern *ptr__uninit) { ... }}hjsbah}(h]h ]h"]h$]h&]hhuh1jhhhKmhjhhubh)}(hHere, the dynptr will be treated as an uninitialized dynptr. Without this annotation, the verifier will reject the program if the dynptr passed in is not initialized.h]hHere, the dynptr will be treated as an uninitialized dynptr. Without this annotation, the verifier will reject the program if the dynptr passed in is not initialized.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKrhjhhubeh}(h]uninit-annotationah ]h"]2.2.3 __uninit annotationah$]h&]uh1hhj)hhhhhKfubh)}(hhh](h)}(h2.2.4 __opt Annotationh]h2.2.4 __opt Annotation}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhKwubh)}(hX;This annotation is used to indicate that the buffer associated with an __sz or __szk argument may be null. If the function is passed a nullptr in place of the buffer, the verifier will not check that length is appropriate for the buffer. The kfunc is responsible for checking if this buffer is null before using it.h]hX;This annotation is used to indicate that the buffer associated with an __sz or __szk argument may be null. If the function is passed a nullptr in place of the buffer, the verifier will not check that length is appropriate for the buffer. The kfunc is responsible for checking if this buffer is null before using it.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKyhj hhubh)}(hAn example is given below::h]hAn example is given below:}(hj*hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK~hj hhubj)}(hS__bpf_kfunc void *bpf_dynptr_slice(..., void *buffer__opt, u32 buffer__szk) { ... }h]hS__bpf_kfunc void *bpf_dynptr_slice(..., void *buffer__opt, u32 buffer__szk) { ... }}hj8sbah}(h]h ]h"]h$]h&]hhuh1jhhhKhj hhubh)}(hX Here, the buffer may be null. If buffer is not null, it at least of size buffer_szk. Either way, the returned buffer is either NULL, or of size buffer_szk. Without this annotation, the verifier will reject the program if a null pointer is passed in with a nonzero size.h]hX Here, the buffer may be null. If buffer is not null, it at least of size buffer_szk. Either way, the returned buffer is either NULL, or of size buffer_szk. Without this annotation, the verifier will reject the program if a null pointer is passed in with a nonzero size.}(hjFhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj hhubeh}(h]opt-annotationah ]h"]2.2.4 __opt annotationah$]h&]uh1hhj)hhhhhKwubh)}(hhh](h)}(h2.2.5 __str Annotationh]h2.2.5 __str Annotation}(hj_hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj\hhhhhKubh)}(hKThis annotation is used to indicate that the argument is a constant string.h]hKThis annotation is used to indicate that the argument is a constant string.}(hjmhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj\hhubh)}(hAn example is given below::h]hAn example is given below:}(hj{hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj\hhubj)}(hG__bpf_kfunc bpf_get_file_xattr(..., const char *name__str, ...) { ... }h]hG__bpf_kfunc bpf_get_file_xattr(..., const char *name__str, ...) { ... }}hjsbah}(h]h ]h"]h$]h&]hhuh1jhhhKhj\hhubh)}(h9In this case, ``bpf_get_file_xattr()`` can be called as::h](hIn this case, }(hjhhhNhNubhliteral)}(h``bpf_get_file_xattr()``h]hbpf_get_file_xattr()}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh can be called as:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj\hhubj)}(h+bpf_get_file_xattr(..., "xattr_name", ...);h]h+bpf_get_file_xattr(..., "xattr_name", ...);}hjsbah}(h]h ]h"]h$]h&]hhuh1jhhhKhj\hhubh)}(hOr::h]hOr:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj\hhubj)}(hconst char name[] = "xattr_name"; /* This need to be global */ int BPF_PROG(...) { ... bpf_get_file_xattr(..., name, ...); ... }h]hconst char name[] = "xattr_name"; /* This need to be global */ int BPF_PROG(...) { ... bpf_get_file_xattr(..., name, ...); ... }}hjsbah}(h]h ]h"]h$]h&]hhuh1jhhhKhj\hhubh)}(h.. _BPF_kfunc_nodef:h]h}(h]h ]h"]h$]h&]hbpf-kfunc-nodefuh1hhKhj\hhhhubeh}(h]str-annotationah ]h"]2.2.5 __str annotationah$]h&]uh1hhj)hhhhhKubh)}(hhh](h)}(h%2.3 Using an existing kernel functionh]h%2.3 Using an existing kernel function}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(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&]uh1hhhhKhjhhubeh}(h](!using-an-existing-kernel-functionjeh ]h"](%2.3 using an existing kernel functionbpf_kfunc_nodefeh$]h&]uh1hhj)hhhhhKexpect_referenced_by_name}jjsexpect_referenced_by_id}jjsubh)}(hhh](h)}(h2.4 Annotating kfuncsh]h2.4 Annotating kfuncs}(hj%hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj"hhhhhKubh)}(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:}(hj3hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj"hhubj)}(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)}hjAsbah}(h]h ]h"]h$]h&]hhuh1jhhhKhj"hhubh)}(hThis set encodes the BTF ID of each kfunc listed above, and encodes the flags along with it. Ofcourse, 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. Ofcourse, it is also allowed to specify no flags.}(hjOhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhj"hhubh)}(hX0kfunc definitions should also always be annotated with the ``__bpf_kfunc`` macro. This prevents issues such as the compiler inlining the kfunc if it's a static kernel function, 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 }(hj]hhhNhNubj)}(h``__bpf_kfunc``h]h __bpf_kfunc}(hjehhhNhNubah}(h]h ]h"]h$]h&]uh1jhj]ubhX macro. This prevents issues such as the compiler inlining the kfunc if it’s a static kernel function, 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:}(hj]hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhKhj"hhubj)}(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&]hhuh1jhhhKhj"hhubeh}(h]annotating-kfuncsah ]h"]2.4 annotating kfuncsah$]h&]uh1hhj)hhhhhKubh)}(hhh](h)}(h2.4.1 KF_ACQUIRE flagh]h2.4.1 KF_ACQUIRE flag}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(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.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubeh}(h]kf-acquire-flagah ]h"]2.4.1 kf_acquire flagah$]h&]uh1hhj)hhhhhKubh)}(hhh](h)}(h2.4.2 KF_RET_NULL flagh]h2.4.2 KF_RET_NULL flag}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(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.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubeh}(h]kf-ret-null-flagah ]h"]2.4.2 kf_ret_null flagah$]h&]uh1hhj)hhhhhKubh)}(hhh](h)}(h2.4.3 KF_RELEASE flagh]h2.4.3 KF_RELEASE flag}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(hXpThe 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. KF_RELEASE kfuncs automatically receive the protection afforded by the KF_TRUSTED_ARGS flag described below.h]hXpThe 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. KF_RELEASE kfuncs automatically receive the protection afforded by the KF_TRUSTED_ARGS flag described below.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubeh}(h]kf-release-flagah ]h"]2.4.3 kf_release flagah$]h&]uh1hhj)hhhhhKubh)}(hhh](h)}(h2.4.4 KF_TRUSTED_ARGS flagh]h2.4.4 KF_TRUSTED_ARGS flag}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhKubh)}(hXKThe KF_TRUSTED_ARGS flag is used for kfuncs taking pointer arguments. It indicates that the all pointer arguments are valid, and that all pointers to BTF objects have been passed in their unmodified form (that is, at a zero offset, and without having been obtained from walking another pointer, with one exception described below).h]hXKThe KF_TRUSTED_ARGS flag is used for kfuncs taking pointer arguments. It indicates that the all pointer arguments are valid, and that all pointers to BTF objects have been passed in their unmodified form (that is, at a zero offset, and without having been obtained from walking another pointer, with one exception described below).}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(hOThere are two types of pointers to kernel objects which are considered "valid":h]hSThere are two types of pointers to kernel objects which are considered “valid”:}(hj'hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubhenumerated_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&]uh1hhhhKhj<ubah}(h]h ]h"]h$]h&]uh1j:hj7hhhhhNubj;)}(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.}(hjWhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjSubah}(h]h ]h"]h$]h&]uh1j:hj7hhhhhNubeh}(h]h ]h"]h$]h&]enumtypearabicprefixhsuffix.uh1j5hjhhhhhKubh)}(hPointers to non-BTF objects (e.g. scalar pointers) may also be passed to KF_TRUSTED_ARGS kfuncs, and may have a non-zero offset.h]hPointers to non-BTF objects (e.g. scalar pointers) may also be passed to KF_TRUSTED_ARGS kfuncs, and may have a non-zero offset.}(hjvhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKhjhhubh)}(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&]uh1hhhhKhjhhubh)}(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&]uh1hhhhKhjhhubh bullet_list)}(hhh](j;)}(h``BTF_TYPE_SAFE_TRUSTED``h]h)}(hjh]j)}(hjh]hBTF_TYPE_SAFE_TRUSTED}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1j:hjhhhhhNubj;)}(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&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1j:hjhhhhhNubj;)}(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&]uh1hhhhMhjubah}(h]h ]h"]h$]h&]uh1j:hjhhhhhNubeh}(h]h ]h"]h$]h&]bullet*uh1jhhhMhjhhubh)}(h For example,h]h For example,}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(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&]hhforcelanguagechighlight_args}uh1jhhhMhjhhubh)}(horh]hor}(hj/hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hjhhubj)}(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-}uh1jhhhMhjhhubh)}(hIn other words, you must:h]hIn other words, you must:}(hjLhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj6)}(hhh](j;)}(hsbah}(h]h ]h"]h$]h&]hhj*j+j,j-}uh1jhhhMyhjhhubh)}(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 }(hjMhhhNhNubj)}(h``struct bpf_cpumask *``h]hstruct bpf_cpumask *}(hjUhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjMubh" to be passed to a kfunc taking a }(hjMhhhNhNubj)}(h``cpumask_t *``h]h cpumask_t *}(hjghhhNhNubah}(h]h ]h"]h$]h&]uh1jhjMubh (which is a typedef of }(hjMhhhNhNubj)}(h``struct cpumask *``h]hstruct cpumask *}(hjyhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjMubh). For instance, both }(hjMhhhNhNubj)}(h``struct cpumask *``h]hstruct cpumask *}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjMubh and }(hjMhhhNhNubj)}(h``struct bpf_cpmuask *``h]hstruct bpf_cpmuask *}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjMubh) can be passed to bpf_cpumask_test_cpu().}(hjMhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(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. }(hjhhhNhNubj)}(h``struct nf_conn___init``h]hstruct nf_conn___init}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh is one such example:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj)}(h5struct nf_conn___init { struct nf_conn ct; };h]h5struct nf_conn___init { struct nf_conn ct; };}hjsbah}(h]h ]h"]h$]h&]hhj*j+j,j-}uh1jhhhMhjhhubh)}(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. }(hjhhhNhNubj)}(h``struct nf_conn___init``h]hstruct nf_conn___init}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh represents an allocated }(hjhhhNhNubj)}(h``struct nf_conn``h]hstruct nf_conn}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh object that has }(hjhhhNhNubhemphasis)}(h*not yet been initialized*h]hnot yet been initialized}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1j hjubh,, so it would therefore be unsafe to pass a }(hjhhhNhNubj)}(h``struct nf_conn___init *``h]hstruct nf_conn___init *}(hj$ hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh3 to a kfunc that’s expecting a fully initialized }(hjhhhNhNubj)}(h``struct nf_conn *``h]hstruct nf_conn *}(hj6 hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh (e.g. }(hjhhhNhNubj)}(h``bpf_ct_change_timeout()``h]hbpf_ct_change_timeout()}(hjH hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh).}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(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}(hjh hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj` ubh.}(hj` hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubh)}(h%.. _BPF_kfunc_lifecycle_expectations:h]h}(h]h ]h"]h$]h&]h bpf-kfunc-lifecycle-expectationsuh1hhMhjhhhhubeh}(h]$specifying-no-cast-aliases-with-initah ]h"]+2.6 specifying no-cast aliases with ___initah$]h&]uh1hhj)hhhhhMoubeh}(h]defining-a-kfuncah ]h"]2. defining a kfuncah$]h&]uh1hhhhhhhhKubh)}(hhh](h)}(h3. kfunc lifecycle expectationsh]h3. kfunc lifecycle expectations}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhMubh)}(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.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(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.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(h'There are several implications of this:h]h'There are several implications of this:}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubj6)}(hhh](j;)}(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.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj ubah}(h]h ]h"]h$]h&]uh1j:hj hhhhhNubj;)}(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.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj ubh)}(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.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj ubeh}(h]h ]h"]h$]h&]uh1j:hj hhhhhNubj;)}(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.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj ubah}(h]h ]h"]h$]h&]uh1j:hj hhhhhNubeh}(h]h ]h"]h$]h&]jq loweralphajshjt)uh1j5hj hhhhhMubh)}(hhh](h)}(h3.1 kfunc deprecationh]h3.1 kfunc deprecation}(hj: hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj7 hhhhhMubh)}(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.}(hjH hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj7 hhubh)}(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 }(hjV hhhNhNubh)}(h:ref:`KF_deprecated_flag`h]h)}(hj` h]hKF_deprecated_flag}(hjb hhhNhNubah}(h]h ](jstdstd-refeh"]h$]h&]uh1hhj^ ubah}(h]h ]h"]h$]h&]refdocj refdomainjl reftyperef refexplicitrefwarnjkf_deprecated_flaguh1hhhhMhjV ubh, 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:}(hjV hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj7 hhubj6)}(hhh](j;)}(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.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj ubah}(h]h ]h"]h$]h&]uh1j:hj hhhhhNubj;)}(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]h)}(hj h]habove}(hj hhhNhNubah}(h]h ](jstdstd-refeh"]h$]h&]uh1hhj ubah}(h]h ]h"]h$]h&]refdocj refdomainj reftyperef 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&]uh1j:hj hhhhhNubj;)}(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.}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj ubah}(h]h ]h"]h$]h&]uh1j:hj hhhhhNubeh}(h]h ]h"]h$]h&]jqjrjshjtjuuh1j5hj7 hhhhhMubeh}(h]kfunc-deprecationah ]h"]3.1 kfunc deprecationah$]h&]uh1hhj hhhhhMubeh}(h](kfunc-lifecycle-expectationsj eh ]h"](3. kfunc lifecycle expectations bpf_kfunc_lifecycle_expectationseh$]h&]uh1hhhhhhhhMj}j j sj }j j subh)}(hhh](h)}(h4. Core kfuncsh]h4. Core kfuncs}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj hhhhhMubh)}(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.}(hj! hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj hhubh)}(hhh](h)}(h4.1 struct task_struct * kfuncsh]h4.1 struct task_struct * kfuncs}(hj2 hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj/ hhhhhMubh)}(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 }(hj@ hhhNhNubj)}(h``struct task_struct *``h]hstruct task_struct *}(hjH hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj@ ubh objects to be used as kptrs:}(hj@ hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj/ hhubhindex)}(hhh]h}(h]h ]h"]h$]h&]entries](singlebpf_task_acquire (C function)c.bpf_task_acquirehNtauh1j` hj/ hhhNhNubhdesc)}(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}(hj~ hhhNhNubhdesc_sig_space)}(h h]h }(hj hhhNhNubah}(h]h ]wah"]h$]h&]uh1j hj~ hhhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:515: ./kernel/bpf/helpers.chMG ubhdesc_sig_keyword)}(hstructh]hstruct}(hj hhhNhNubah}(h]h ]kah"]h$]h&]uh1j hj~ hhhj hMG ubj )}(h h]h }(hj hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj~ hhhj hMG ubh)}(hhh]h desc_sig_name)}(h task_structh]h task_struct}(hj hhhNhNubah}(h]h ]nah"]h$]h&]uh1j hj ubah}(h]h ]h"]h$]h&] refdomainj,reftype identifier reftargetj modnameN classnameN c:parent_keysphinx.domains.c LookupKey)}data]j ASTIdentifier)}j bpf_task_acquiresbc.bpf_task_acquireasbuh1hhj~ hhhj hMG ubj )}(h h]h }(hj hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj~ hhhj hMG ubhdesc_sig_punctuation)}(hj h]h*}(hj hhhNhNubah}(h]h ]pah"]h$]h&]uh1j hj~ hhhj hMG ubh desc_name)}(hbpf_task_acquireh]j )}(hj h]hbpf_task_acquire}(hj hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj ubah}(h]h ](sig-namedescnameeh"]h$]h&]hhuh1j hj~ hhhj hMG ubhdesc_parameterlist)}(h(struct task_struct *p)h]hdesc_parameter)}(hstruct task_struct *ph](j )}(hj h]hstruct}(hj+ hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj' ubj )}(h h]h }(hj8 hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj' ubh)}(hhh]j )}(h task_structh]h task_struct}(hjI hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjF ubah}(h]h ]h"]h$]h&] refdomainj,reftypej reftargetjK modnameN classnameNj j )}j ]j c.bpf_task_acquireasbuh1hhj' ubj )}(h h]h }(hjg hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj' ubj )}(hj h]h*}(hju hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj' ubj )}(hj h]hp}(hj hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj' ubeh}(h]h ]h"]h$]h&]noemphhhuh1j% hj! ubah}(h]h ]h"]h$]h&]hhuh1j hj~ hhhj hMG ubeh}(h]h ]h"]h$]h&]hh add_permalinkuh1j| sphinx_line_type declaratorhjx hhhj hMG ubah}(h]jo ah ](sig sig-objecteh"]h$]h&] is_multiline _toc_parts) _toc_namehuh1jv hj hMG hjs 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().}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:515: ./kernel/bpf/helpers.chMB hj hhubah}(h]h ]h"]h$]h&]uh1j hjs hhhj hMG ubeh}(h]h ](j,functioneh"]h$]h&]domainj,objtypej desctypej noindex noindexentrynocontentsentryuh1jq hhhj/ hNhNubh container)}(h\**Parameters** ``struct task_struct *p`` The task on which a reference is being acquired.h](h)}(h**Parameters**h]hstrong)}(hj h]h Parameters}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1j hj ubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:515: ./kernel/bpf/helpers.chMF hj ubhdefinition_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)}(hj h]hstruct task_struct *p}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]uh1j hP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:515: ./kernel/bpf/helpers.chMH hj ubh definition)}(hhh]h)}(h0The task on which a reference is being acquired.h]h0The task on which a reference is being acquired.}(hj! hhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:515: ./kernel/bpf/helpers.chME hj ubah}(h]h ]h"]h$]h&]uh1j hj ubeh}(h]h ]h"]h$]h&]uh1j hj hMH hj ubah}(h]h ]h"]h$]h&]uh1j hj ubeh}(h]h ] kernelindentah"]h$]h&]uh1j hj/ hhhNhNubja )}(hhh]h}(h]h ]h"]h$]h&]entries](jm bpf_task_release (C function)c.bpf_task_releasehNtauh1j` hj/ hhhNhNubjr )}(hhh](jw )}(h9__bpf_kfunc void bpf_task_release (struct task_struct *p)h]j} )}(h8__bpf_kfunc void bpf_task_release(struct task_struct *p)h](h __bpf_kfunc}(hj^ hhhNhNubj )}(h h]h }(hjf hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj^ hhhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:515: ./kernel/bpf/helpers.chMR ubhdesc_sig_keyword_type)}(hvoidh]hvoid}(hjw hhhNhNubah}(h]h ]ktah"]h$]h&]uh1ju hj^ hhhjt hMR ubj )}(h h]h }(hj hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj^ hhhjt hMR ubj )}(hbpf_task_releaseh]j )}(hbpf_task_releaseh]hbpf_task_release}(hj hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj ubah}(h]h ](j j eh"]h$]h&]hhuh1j hj^ hhhjt hMR ubj )}(h(struct task_struct *p)h]j& )}(hstruct task_struct *ph](j )}(hj h]hstruct}(hj hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj ubj )}(h h]h }(hj hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj ubh)}(hhh]j )}(h task_structh]h task_struct}(hj hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj ubah}(h]h ]h"]h$]h&] refdomainj,reftypej reftargetj modnameN classnameNj j )}j ]j )}j j sbc.bpf_task_releaseasbuh1hhj ubj )}(h h]h }(hj hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj ubj )}(hj h]h*}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj ubj )}(hj h]hp}(hj hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj ubeh}(h]h ]h"]h$]h&]noemphhhuh1j% hj ubah}(h]h ]h"]h$]h&]hhuh1j hj^ hhhjt hMR ubeh}(h]h ]h"]h$]h&]hhj uh1j| j j hjZ hhhjt hMR ubah}(h]jU ah ](j j eh"]h$]h&]j j )j huh1jv hjt hMR hjW hhubj )}(hhh]h)}(h)Release the reference acquired on a task.h]h)Release the reference acquired on a task.}(hj6hhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:515: ./kernel/bpf/helpers.chMO hj3hhubah}(h]h ]h"]h$]h&]uh1j hjW hhhjt hMR ubeh}(h]h ](j,functioneh"]h$]h&]j j,j jNj jNj j j uh1jq hhhj/ hNhNubj )}(h\**Parameters** ``struct task_struct *p`` The task on which a reference is being released.h](h)}(h**Parameters**h]j )}(hjXh]h Parameters}(hjZhhhNhNubah}(h]h ]h"]h$]h&]uh1j hjVubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:515: ./kernel/bpf/helpers.chMS hjRubj )}(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)}(hjwh]hstruct task_struct *p}(hjyhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjuubah}(h]h ]h"]h$]h&]uh1j hP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:515: ./kernel/bpf/helpers.chMU hjqubj )}(hhh]h)}(h0The task on which a reference is being released.h]h0The task on which a reference is being released.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:515: ./kernel/bpf/helpers.chMP hjubah}(h]h ]h"]h$]h&]uh1j hjqubeh}(h]h ]h"]h$]h&]uh1j hjhMU hjnubah}(h]h ]h"]h$]h&]uh1j hjRubeh}(h]h ] kernelindentah"]h$]h&]uh1j hj/ hhhNhNubh)}(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 }(hjhhhNhNubj)}(h``struct task_struct *``h]hstruct task_struct *}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubhU that was passed as e.g. a tracepoint arg, or a struct_ops callback arg. For example:}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj/ hhubj)}(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-}uh1jhhhM hj/ hhubh)}(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&]uh1hhhhM!hj/ hhubj)}(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-}uh1jhhhM%hj/ hhubh transition)}(h----h]h}(h]h ]h"]h$]h&]uh1jhhhMEhj/ hhubh)}(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 }(hj"hhhNhNubj)}(h``struct task_struct *``h]hstruct task_struct *}(hj*hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj"ubhC object that it can acquire a reference on with bpf_task_acquire().}(hj"hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMGhj/ hhubja )}(hhh]h}(h]h ]h"]h$]h&]entries](jm bpf_task_from_pid (C function)c.bpf_task_from_pidhNtauh1j` hj/ hhhNhNubjr )}(hhh](jw )}(h<__bpf_kfunc struct task_struct * bpf_task_from_pid (s32 pid)h]j} )}(h:__bpf_kfunc struct task_struct *bpf_task_from_pid(s32 pid)h](h __bpf_kfunc}(hjWhhhNhNubj )}(h h]h }(hj_hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjWhhhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:587: ./kernel/bpf/helpers.chM ubj )}(hj h]hstruct}(hjnhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjWhhhjmhM ubj )}(h h]h }(hj{hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjWhhhjmhM ubh)}(hhh]j )}(h task_structh]h task_struct}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubah}(h]h ]h"]h$]h&] refdomainj,reftypej reftargetjmodnameN classnameNj j )}j ]j )}j bpf_task_from_pidsbc.bpf_task_from_pidasbuh1hhjWhhhjmhM ubj )}(h h]h }(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjWhhhjmhM ubj )}(hj h]h*}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjWhhhjmhM ubj )}(hbpf_task_from_pidh]j )}(hjh]hbpf_task_from_pid}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubah}(h]h ](j j eh"]h$]h&]hhuh1j hjWhhhjmhM ubj )}(h (s32 pid)h]j& )}(hs32 pidh](h)}(hhh]j )}(hs32h]hs32}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubah}(h]h ]h"]h$]h&] refdomainj,reftypej reftargetjmodnameN classnameNj j )}j ]jc.bpf_task_from_pidasbuh1hhjubj )}(h h]h }(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubj )}(hpidh]hpid}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubeh}(h]h ]h"]h$]h&]noemphhhuh1j% hjubah}(h]h ]h"]h$]h&]hhuh1j hjWhhhjmhM ubeh}(h]h ]h"]h$]h&]hhj uh1j| j j hjShhhjmhM ubah}(h]jNah ](j j eh"]h$]h&]j j )j huh1jv hjmhM hjPhhubj )}(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().}(hj@hhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:587: ./kernel/bpf/helpers.chM hj=hhubah}(h]h ]h"]h$]h&]uh1j hjPhhhjmhM ubeh}(h]h ](j,functioneh"]h$]h&]j j,j jXj jXj j j uh1jq hhhj/ hNhNubj )}(hB**Parameters** ``s32 pid`` The pid of the task being looked up.h](h)}(h**Parameters**h]j )}(hjbh]h Parameters}(hjdhhhNhNubah}(h]h ]h"]h$]h&]uh1j hj`ubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:587: ./kernel/bpf/helpers.chM hj\ubj )}(hhh]j )}(h0``s32 pid`` The pid of the task being looked up.h](j )}(h ``s32 pid``h]j)}(hjh]hs32 pid}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1j hP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:587: ./kernel/bpf/helpers.chM hj{ubj )}(hhh]h)}(h$The pid of the task being looked up.h]h$The pid of the task being looked up.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:587: ./kernel/bpf/helpers.chM hjubah}(h]h ]h"]h$]h&]uh1j hj{ubeh}(h]h ]h"]h$]h&]uh1j hjhM hjxubah}(h]h ]h"]h$]h&]uh1j hj\ubeh}(h]h ] kernelindentah"]h$]h&]uh1j hj/ hhhNhNubh)}(h$Here is an example of it being used:h]h$Here is an example of it being used:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMNhj/ hhubj)}(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-}uh1jhhhMPhj/ hhubeh}(h]struct-task-struct-kfuncsah ]h"]4.1 struct task_struct * kfuncsah$]h&]uh1hhj hhhhhMubh)}(hhh](h)}(h4.2 struct cgroup * kfuncsh]h4.2 struct cgroup * kfuncs}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMoubh)}(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&]uh1hhhhMqhjhhubja )}(hhh]h}(h]h ]h"]h$]h&]entries](jm bpf_cgroup_acquire (C function)c.bpf_cgroup_acquirehNtauh1j` hjhhhNhNubjr )}(hhh](jw )}(hD__bpf_kfunc struct cgroup * bpf_cgroup_acquire (struct cgroup *cgrp)h]j} )}(hB__bpf_kfunc struct cgroup *bpf_cgroup_acquire(struct cgroup *cgrp)h](h __bpf_kfunc}(hj)hhhNhNubj )}(h h]h }(hj1hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj)hhhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:627: ./kernel/bpf/helpers.chMd ubj )}(hj h]hstruct}(hj@hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj)hhhj?hMd ubj )}(h h]h }(hjMhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj)hhhj?hMd ubh)}(hhh]j )}(hcgrouph]hcgroup}(hj^hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj[ubah}(h]h ]h"]h$]h&] refdomainj,reftypej reftargetj`modnameN classnameNj j )}j ]j )}j bpf_cgroup_acquiresbc.bpf_cgroup_acquireasbuh1hhj)hhhj?hMd ubj )}(h h]h }(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj)hhhj?hMd ubj )}(hj h]h*}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj)hhhj?hMd ubj )}(hbpf_cgroup_acquireh]j )}(hj|h]hbpf_cgroup_acquire}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubah}(h]h ](j j eh"]h$]h&]hhuh1j hj)hhhj?hMd ubj )}(h(struct cgroup *cgrp)h]j& )}(hstruct cgroup *cgrph](j )}(hj h]hstruct}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubj )}(h h]h }(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubh)}(hhh]j )}(hcgrouph]hcgroup}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubah}(h]h ]h"]h$]h&] refdomainj,reftypej reftargetjmodnameN classnameNj j )}j ]jzc.bpf_cgroup_acquireasbuh1hhjubj )}(h h]h }(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubj )}(hj h]h*}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubj )}(hcgrph]hcgrp}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubeh}(h]h ]h"]h$]h&]noemphhhuh1j% hjubah}(h]h ]h"]h$]h&]hhuh1j hj)hhhj?hMd ubeh}(h]h ]h"]h$]h&]hhj uh1j| j j hj%hhhj?hMd ubah}(h]j ah ](j j eh"]h$]h&]j j )j huh1jv hj?hMd hj"hhubj )}(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().}(hj:hhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:627: ./kernel/bpf/helpers.chM_ hj7hhubah}(h]h ]h"]h$]h&]uh1j hj"hhhj?hMd ubeh}(h]h ](j,functioneh"]h$]h&]j j,j jRj jRj j j uh1jq hhhjhNhNubj )}(h\**Parameters** ``struct cgroup *cgrp`` The cgroup on which a reference is being acquired.h](h)}(h**Parameters**h]j )}(hj\h]h Parameters}(hj^hhhNhNubah}(h]h ]h"]h$]h&]uh1j hjZubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:627: ./kernel/bpf/helpers.chMc hjVubj )}(hhh]j )}(hJ``struct cgroup *cgrp`` The cgroup on which a reference is being acquired.h](j )}(h``struct cgroup *cgrp``h]j)}(hj{h]hstruct cgroup *cgrp}(hj}hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjyubah}(h]h ]h"]h$]h&]uh1j hP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:627: ./kernel/bpf/helpers.chMe hjuubj )}(hhh]h)}(h2The cgroup on which a reference is being acquired.h]h2The cgroup on which a reference is being acquired.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:627: ./kernel/bpf/helpers.chMb hjubah}(h]h ]h"]h$]h&]uh1j hjuubeh}(h]h ]h"]h$]h&]uh1j hjhMe hjrubah}(h]h ]h"]h$]h&]uh1j hjVubeh}(h]h ] kernelindentah"]h$]h&]uh1j hjhhhNhNubja )}(hhh]h}(h]h ]h"]h$]h&]entries](jm bpf_cgroup_release (C function)c.bpf_cgroup_releasehNtauh1j` hjhhhNhNubjr )}(hhh](jw )}(h9__bpf_kfunc void bpf_cgroup_release (struct cgroup *cgrp)h]j} )}(h8__bpf_kfunc void bpf_cgroup_release(struct cgroup *cgrp)h](h __bpf_kfunc}(hjhhhNhNubj )}(h h]h }(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjhhhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:627: ./kernel/bpf/helpers.chMp ubjv )}(hvoidh]hvoid}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1ju hjhhhjhMp ubj )}(h h]h }(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjhhhjhMp ubj )}(hbpf_cgroup_releaseh]j )}(hbpf_cgroup_releaseh]hbpf_cgroup_release}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubah}(h]h ](j j eh"]h$]h&]hhuh1j hjhhhjhMp ubj )}(h(struct cgroup *cgrp)h]j& )}(hstruct cgroup *cgrph](j )}(hj h]hstruct}(hj$hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj ubj )}(h h]h }(hj1hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj ubh)}(hhh]j )}(hcgrouph]hcgroup}(hjBhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj?ubah}(h]h ]h"]h$]h&] refdomainj,reftypej reftargetjDmodnameN classnameNj j )}j ]j )}j j sbc.bpf_cgroup_releaseasbuh1hhj ubj )}(h h]h }(hjbhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj ubj )}(hj h]h*}(hjphhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj ubj )}(hcgrph]hcgrp}(hj}hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj ubeh}(h]h ]h"]h$]h&]noemphhhuh1j% hjubah}(h]h ]h"]h$]h&]hhuh1j hjhhhjhMp ubeh}(h]h ]h"]h$]h&]hhj uh1j| j j hjhhhjhMp ubah}(h]jah ](j j eh"]h$]h&]j j )j huh1jv hjhMp hjhhubj )}(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.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:627: ./kernel/bpf/helpers.chMj hjhhubah}(h]h ]h"]h$]h&]uh1j hjhhhjhMp ubeh}(h]h ](j,functioneh"]h$]h&]j j,j jj jj j j uh1jq 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&]uh1j hjubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:627: ./kernel/bpf/helpers.chMn hjubj )}(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&]uh1j hP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:627: ./kernel/bpf/helpers.chMp 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:627: ./kernel/bpf/helpers.chMn hjubah}(h]h ]h"]h$]h&]uh1j hjubeh}(h]h ]h"]h$]h&]uh1j hjhMp hjubah}(h]h ]h"]h$]h&]uh1j hjubeh}(h]h ] kernelindentah"]h$]h&]uh1j hjhhhNhNubh)}(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.}(hj)hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMvhjhhubj)}(h----h]h}(h]h ]h"]h$]h&]uh1jhhhMyhjhhubh)}(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 }(hjAhhhNhNubj)}(h``struct cgroup *``h]hstruct cgroup *}(hjIhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjAubh 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.}(hjAhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM{hjhhubja )}(hhh]h}(h]h ]h"]h$]h&]entries](jm bpf_cgroup_ancestor (C function)c.bpf_cgroup_ancestorhNtauh1j` hjhhhNhNubjr )}(hhh](jw )}(hP__bpf_kfunc struct cgroup * bpf_cgroup_ancestor (struct cgroup *cgrp, int level)h]j} )}(hN__bpf_kfunc struct cgroup *bpf_cgroup_ancestor(struct cgroup *cgrp, int level)h](h __bpf_kfunc}(hjvhhhNhNubj )}(h h]h }(hj~hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjvhhhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:640: ./kernel/bpf/helpers.chM ubj )}(hj h]hstruct}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjvhhhjhM ubj )}(h h]h }(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjvhhhjhM ubh)}(hhh]j )}(hcgrouph]hcgroup}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubah}(h]h ]h"]h$]h&] refdomainj,reftypej reftargetjmodnameN classnameNj j )}j ]j )}j bpf_cgroup_ancestorsbc.bpf_cgroup_ancestorasbuh1hhjvhhhjhM ubj )}(h h]h }(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjvhhhjhM ubj )}(hj h]h*}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjvhhhjhM ubj )}(hbpf_cgroup_ancestorh]j )}(hjh]hbpf_cgroup_ancestor}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubah}(h]h ](j j eh"]h$]h&]hhuh1j hjvhhhjhM ubj )}(h (struct cgroup *cgrp, int level)h](j& )}(hstruct cgroup *cgrph](j )}(hj h]hstruct}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubj )}(h h]h }(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubh)}(hhh]j )}(hcgrouph]hcgroup}(hj$hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj!ubah}(h]h ]h"]h$]h&] refdomainj,reftypej reftargetj&modnameN classnameNj j )}j ]jc.bpf_cgroup_ancestorasbuh1hhjubj )}(h h]h }(hjBhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubj )}(hj h]h*}(hjPhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubj )}(hcgrph]hcgrp}(hj]hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubeh}(h]h ]h"]h$]h&]noemphhhuh1j% hjubj& )}(h int levelh](jv )}(hinth]hint}(hjvhhhNhNubah}(h]h ]j ah"]h$]h&]uh1ju hjrubj )}(h h]h }(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjrubj )}(hlevelh]hlevel}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjrubeh}(h]h ]h"]h$]h&]noemphhhuh1j% hjubeh}(h]h ]h"]h$]h&]hhuh1j hjvhhhjhM ubeh}(h]h ]h"]h$]h&]hhj uh1j| j j hjrhhhjhM ubah}(h]jmah ](j j eh"]h$]h&]j j )j huh1jv hjhM hjohhubj )}(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().}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:640: ./kernel/bpf/helpers.chM| hjhhubah}(h]h ]h"]h$]h&]uh1j hjohhhjhM ubeh}(h]h ](j,functioneh"]h$]h&]j j,j jj jj j j uh1jq 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&]uh1j hjubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:640: ./kernel/bpf/helpers.chM 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&]uh1j hP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:640: ./kernel/bpf/helpers.chM 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&]uh1hhjhM hjubah}(h]h ]h"]h$]h&]uh1j hjubeh}(h]h ]h"]h$]h&]uh1j hjhM hjubj )}(h/``int level`` The level of ancestor to look up.h](j )}(h ``int level``h]j)}(hj6h]h int level}(hj8hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj4ubah}(h]h ]h"]h$]h&]uh1j hP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:640: ./kernel/bpf/helpers.chM hj0ubj )}(hhh]h)}(h!The level of ancestor to look up.h]h!The level of ancestor to look up.}(hjOhhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:640: ./kernel/bpf/helpers.chM hjLubah}(h]h ]h"]h$]h&]uh1j hj0ubeh}(h]h ]h"]h$]h&]uh1j hjKhM hjubeh}(h]h ]h"]h$]h&]uh1j hjubeh}(h]h ] kernelindentah"]h$]h&]uh1j hjhhhNhNubja )}(hhh]h}(h]h ]h"]h$]h&]entries](jm bpf_cgroup_from_id (C function)c.bpf_cgroup_from_idhNtauh1j` hjhhhNhNubjr )}(hhh](jw )}(h9__bpf_kfunc struct cgroup * bpf_cgroup_from_id (u64 cgid)h]j} )}(h7__bpf_kfunc struct cgroup *bpf_cgroup_from_id(u64 cgid)h](h __bpf_kfunc}(hjhhhNhNubj )}(h h]h }(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjhhhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:643: ./kernel/bpf/helpers.chM ubj )}(hj h]hstruct}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjhhhjhM ubj )}(h h]h }(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjhhhjhM ubh)}(hhh]j )}(hcgrouph]hcgroup}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubah}(h]h ]h"]h$]h&] refdomainj,reftypej reftargetjmodnameN classnameNj j )}j ]j )}j bpf_cgroup_from_idsbc.bpf_cgroup_from_idasbuh1hhjhhhjhM ubj )}(h h]h }(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjhhhjhM ubj )}(hj h]h*}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjhhhjhM ubj )}(hbpf_cgroup_from_idh]j )}(hjh]hbpf_cgroup_from_id}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubah}(h]h ](j j eh"]h$]h&]hhuh1j hjhhhjhM ubj )}(h (u64 cgid)h]j& )}(hu64 cgidh](h)}(hhh]j )}(hu64h]hu64}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubah}(h]h ]h"]h$]h&] refdomainj,reftypej reftargetj!modnameN classnameNj j )}j ]jc.bpf_cgroup_from_idasbuh1hhjubj )}(h h]h }(hj=hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubj )}(hcgidh]hcgid}(hjKhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubeh}(h]h ]h"]h$]h&]noemphhhuh1j% hjubah}(h]h ]h"]h$]h&]hhuh1j hjhhhjhM ubeh}(h]h ]h"]h$]h&]hhj uh1j| j j hjhhhjhM ubah}(h]jah ](j j eh"]h$]h&]j j )j huh1jv hjhM hjhhubj )}(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().}(hjuhhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:643: ./kernel/bpf/helpers.chM hjrhhubah}(h]h ]h"]h$]h&]uh1j hjhhhjhM ubeh}(h]h ](j,functioneh"]h$]h&]j j,j jj jj j j uh1jq hhhjhNhNubj )}(h)**Parameters** ``u64 cgid`` cgroup id.h](h)}(h**Parameters**h]j )}(hjh]h Parameters}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1j hjubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:643: ./kernel/bpf/helpers.chM hjubj )}(hhh]j )}(h``u64 cgid`` cgroup id.h](j )}(h ``u64 cgid``h]j)}(hjh]hu64 cgid}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1j hP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:643: ./kernel/bpf/helpers.chM hjubj )}(hhh]h)}(h cgroup id.h]h cgroup id.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:643: ./kernel/bpf/helpers.chM hjubah}(h]h ]h"]h$]h&]uh1j hjubeh}(h]h ]h"]h$]h&]uh1j hjhM hjubah}(h]h ]h"]h$]h&]uh1j hjubeh}(h]h ] kernelindentah"]h$]h&]uh1j hjhhhNhNubh)}(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&]uh1hhhhMhjhhubj)}(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; }h]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-}uh1jhhhMhjhhubeh}(h]struct-cgroup-kfuncsah ]h"]4.2 struct cgroup * kfuncsah$]h&]uh1hhj hhhhhMoubh)}(hhh](h)}(h4.3 struct cpumask * kfuncsh]h4.3 struct cpumask * kfuncs}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjhhhhhMubh)}(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 }(hj-hhhNhNubh)}(h:ref:`cpumasks-header-label`h]h)}(hj7h]hcpumasks-header-label}(hj9hhhNhNubah}(h]h ](jstdstd-refeh"]h$]h&]uh1hhj5ubah}(h]h ]h"]h$]h&]refdocj refdomainjCreftyperef refexplicitrefwarnjcpumasks-header-labeluh1hhhhMhj-ubh for more details.}(hj-hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubeh}(h]struct-cpumask-kfuncsah ]h"]4.3 struct cpumask * kfuncsah$]h&]uh1hhj hhhhhMubeh}(h] core-kfuncsah ]h"]4. core kfuncsah$]h&]uh1hhhhhhhhMubeh}(h](bpf-kernel-functions-kfuncsheh ]h"](bpf kernel functions (kfuncs)kfuncs-header-labeleh$]h&]uh1hhhhhhhhKj}juhsj }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_handlerjerror_encodingutf-8error_encoding_error_handlerbackslashreplace language_codeenrecord_dependenciesNconfigN id_prefixhauto_id_prefixid dump_settingsNdump_internalsNdump_transformsNdump_pseudo_xmlNexpose_internalsNstrict_visitorN_disable_configN_sourceh _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]haj]jajw]jmaj ]j aunameids}(juhjtjqj&j#j j jjjjjXjUjjjjjYjVjjjjjjjjjjjjjjjjj$j!jKjHj}jzjjwjjjj j j j j j j j j jljijjjjjdjau nametypes}(jujtj&j jjjXjjjYjjjjjjjjj$jKj}jjjj j j j jljjjduh}(hhjqhj#hj j)jjzjjjUjjj[jjjVj jj\jjjjjj"jjjjjjjjj!jjHj'jzjNjwjjjj jj jj j j j j j7 jij jj/ jo jx jU jZ jNjSjjj j%jjjmjrjjjaju footnote_refs} citation_refs} autofootnotes]autofootnote_refs]symbol_footnotes]symbol_footnote_refs] footnotes] citations]autofootnote_startKsymbol_footnote_startK id_counter collectionsCounter}jKsRparse_messages]transform_messages](hsystem_message)}(hhh]h)}(hhh]h9Hyperlink target "kfuncs-header-label" is not referenced.}hj sbah}(h]h ]h"]h$]h&]uh1hhjubah}(h]h ]h"]h$]h&]levelKtypeINFOsourcehlineKuh1jubj)}(hhh]h)}(hhh]h5Hyperlink target "bpf-kfunc-nodef" is not referenced.}hj%sbah}(h]h ]h"]h$]h&]uh1hhj"ubah}(h]h ]h"]h$]h&]levelKtypejsourcehlineKuh1jubj)}(hhh]h)}(hhh]h8Hyperlink target "kf-deprecated-flag" is not referenced.}hj?sbah}(h]h ]h"]h$]h&]uh1hhj<ubah}(h]h ]h"]h$]h&]levelKtypejsourcehlineMAuh1jubj)}(hhh]h)}(hhh]hFHyperlink target "bpf-kfunc-lifecycle-expectations" is not referenced.}hjYsbah}(h]h ]h"]h$]h&]uh1hhjVubah}(h]h ]h"]h$]h&]levelKtypejsourcehlineMuh1jube transformerN include_log] decorationNhhub.