6sphinx.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 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”:}(hjhhhNhNubah}(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?hj%ubah}(h]h ]h"]h$]h&]uh1j#hj hhhhhNubj$)}(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.}(hj@hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhK@hj<ubah}(h]h ]h"]h$]h&]uh1j#hj hhhhhNubeh}(h]h ]h"]h$]h&]enumtypearabicprefixhsuffix.uh1jhjhhhhhK?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.}(hj_hhhNhNubah}(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.}(hjmhhhNhNubah}(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:}(hj{hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKHhjhhubh bullet_list)}(hhh](j$)}(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&]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&]uh1hhhhKOhjubah}(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&]uh1hhhhKPhjubah}(h]h ]h"]h$]h&]uh1j#hjhhhhhNubeh}(h]h ]h"]h$]h&]bullet*uh1jhhhKNhjhhubh)}(h For example,h]h For example,}(hjhhhNhNubah}(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&]hhforcelanguagechighlight_args}uh1jhhhKThjhhubh)}(horh]hor}(hjhhhNhNubah}(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&]hhjjjj}uh1jhhhK\hjhhubh)}(hIn other words, you must:h]hIn other words, you must:}(hj7hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhKehjhhubj)}(hhh](j$)}(hOnly arguments of specific types are implicit. Currently only }(hjhhhNhNubj)}(h``struct bpf_prog_aux *``h]hstruct bpf_prog_aux *}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubh type is supported.}(hjhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMvhjmhhubh)}(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.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMyhjmhhubh)}(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.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM}hjmhhubh)}(hExample declaration:h]hExample declaration:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjmhhubj)}(hX__bpf_kfunc int bpf_task_work_schedule_signal(struct task_struct *task, struct bpf_task_work *tw, void *map__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__map, bpf_task_work_callback_t callback, struct bpf_prog_aux *aux) { ... }}hjsbah}(h]h ]h"]h$]h&]hhjjjj}uh1jhhhMhjmhhubh)}(hExample usage in BPF program:h]hExample usage in BPF program:}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjmhhubj)}(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);}hjsbah}(h]h ]h"]h$]h&]hhjjjj}uh1jhhhMhjmhhubeh}(h]kf-implicit-args-flagah ]h"]2.5.9 kf_implicit_args flagah$]h&]uh1hhj)hhhhhMpubh)}(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 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}(hjB hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj? 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.}(hjP hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhj? 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&]uh1hhhhMhj? hhubj)}(hLstruct bpf_cpumask { cpumask_t cpumask; refcount_t usage; };h]hLstruct bpf_cpumask { cpumask_t cpumask; refcount_t usage; };}hjl sbah}(h]h ]h"]h$]h&]hhjjjj}uh1jhhhMhj? 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&]uh1hhhhMhj? 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&]uh1hhhhMhj? 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&]hhjjjj}uh1jhhhMhj? 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. }(hj hhhNhNubj)}(h``struct nf_conn___init``h]hstruct nf_conn___init}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh represents an allocated }(hj hhhNhNubj)}(h``struct nf_conn``h]hstruct nf_conn}(hj, hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh object that has }(hj hhhNhNubhemphasis)}(h*not yet been initialized*h]hnot yet been initialized}(hj@ hhhNhNubah}(h]h ]h"]h$]h&]uh1j> hj ubh,, so it would therefore be unsafe to pass a }(hj hhhNhNubj)}(h``struct nf_conn___init *``h]hstruct nf_conn___init *}(hjR hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh3 to a kfunc that’s expecting a fully initialized }(hj hhhNhNubj)}(h``struct nf_conn *``h]hstruct nf_conn *}(hjd hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh (e.g. }(hj hhhNhNubj)}(h``bpf_ct_change_timeout()``h]hbpf_ct_change_timeout()}(hjv hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubh).}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj? 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&]uh1hhhhMhj? hhubh)}(h%.. _BPF_kfunc_lifecycle_expectations:h]h}(h]h ]h"]h$]h&]h bpf-kfunc-lifecycle-expectationsuh1hhMhj? hhhhubeh}(h]$specifying-no-cast-aliases-with-initah ]h"]+2.7 specifying no-cast aliases with ___initah$]h&]uh1hhj)hhhhhMubeh}(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 hhubj)}(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.}(hj1 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.}(hjI hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjE ubah}(h]h ]h"]h$]h&]uh1j#hj hhhhhNubeh}(h]h ]h"]h$]h&]jZ loweralphaj\hj])uh1jhj hhhhhMubh)}(hhh](h)}(h3.1 kfunc deprecationh]h3.1 kfunc deprecation}(hjh hhhNhNubah}(h]h ]h"]h$]h&]uh1hhje 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.}(hjv hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM hje 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 }(hj hhhNhNubh)}(h:ref:`KF_deprecated_flag`h]h)}(hj h]hKF_deprecated_flag}(hj hhhNhNubah}(h]h ](jstdstd-refeh"]h$]h&]uh1hhj ubah}(h]h ]h"]h$]h&]refdocj refdomainj reftyperef refexplicitrefwarnjkf_deprecated_flaguh1hhhhMhj 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:}(hj hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhje hhubj)}(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_expectationsuh1hhhhM"hj 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&]uh1hhhhM"hj 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&]uh1hhhhM+hj ubah}(h]h ]h"]h$]h&]uh1j#hj hhhhhNubeh}(h]h ]h"]h$]h&]jZj[j\hj]j^uh1jhje 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@}j9 j sjB}j j subh)}(hhh](h)}(h4. Core kfuncsh]h4. Core kfuncs}(hjA hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj> hhhhhM/ubh)}(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.}(hjO hhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhM1hj> hhubh)}(hhh](h)}(h4.1 struct task_struct * kfuncsh]h4.1 struct task_struct * kfuncs}(hj` hhhNhNubah}(h]h ]h"]h$]h&]uh1hhj] hhhhhM6ubh)}(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 }(hjn hhhNhNubj)}(h``struct task_struct *``h]hstruct task_struct *}(hjv hhhNhNubah}(h]h ]h"]h$]h&]uh1jhjn ubh objects to be used as kptrs:}(hjn hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhM8hj] 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:571: ./kernel/bpf/helpers.chM ubhdesc_sig_keyword)}(hstructh]hstruct}(hj hhhNhNubah}(h]h ]kah"]h$]h&]uh1j hj hhhj hM ubj )}(h h]h }(hj hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj hhhj hM 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&] refdomainjreftype identifier reftargetj modnameN classnameN c:parent_keysphinx.domains.c LookupKey)}data]j ASTIdentifier)}j bpf_task_acquiresbc.bpf_task_acquireasbuh1hhj hhhj hM ubj )}(h h]h }(hj hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj hhhj hM ubhdesc_sig_punctuation)}(hjh]h*}(hj$ hhhNhNubah}(h]h ]pah"]h$]h&]uh1j" hj hhhj hM ubh desc_name)}(hbpf_task_acquireh]j )}(hj h]hbpf_task_acquire}(hj8 hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj4 ubah}(h]h ](sig-namedescnameeh"]h$]h&]hhuh1j2 hj hhhj hM ubhdesc_parameterlist)}(h(struct task_struct *p)h]hdesc_parameter)}(hstruct task_struct *ph](j )}(hj h]hstruct}(hjY hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjU ubj )}(h h]h }(hjf hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjU ubh)}(hhh]j )}(h task_structh]h task_struct}(hjw hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjt ubah}(h]h ]h"]h$]h&] refdomainjreftypej reftargetjy modnameN classnameNj j )}j ]j c.bpf_task_acquireasbuh1hhjU ubj )}(h h]h }(hj hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjU ubj# )}(hjh]h*}(hj hhhNhNubah}(h]h ]j. ah"]h$]h&]uh1j" hjU ubj )}(hj. h]hp}(hj hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjU ubeh}(h]h ]h"]h$]h&]noemphhhuh1jS hjO ubah}(h]h ]h"]h$]h&]hhuh1jM hj hhhj hM ubeh}(h]h ]h"]h$]h&]hh add_permalinkuh1j sphinx_line_type declaratorhj hhhj hM ubah}(h]j ah ](sig sig-objecteh"]h$]h&] is_multiline _toc_parts) _toc_namehuh1j hj hM 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().}(hj hhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:571: ./kernel/bpf/helpers.chM hj hhubah}(h]h ]h"]h$]h&]uh1j hj hhhj hM ubeh}(h]h ](jfunctioneh"]h$]h&]domainjobjtypej desctypej noindex noindexentrynocontentsentryuh1j 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}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:571: ./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)}(hj4h]hstruct task_struct *p}(hj6hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj2ubah}(h]h ]h"]h$]h&]uh1j0hP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:571: ./kernel/bpf/helpers.chM hj,ubh definition)}(hhh]h)}(h0The task on which a reference is being acquired.h]h0The task on which a reference is being acquired.}(hjOhhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:571: ./kernel/bpf/helpers.chM hjLubah}(h]h ]h"]h$]h&]uh1jJhj,ubeh}(h]h ]h"]h$]h&]uh1j*hjIhM hj'ubah}(h]h ]h"]h$]h&]uh1j%hjubeh}(h]h ] kernelindentah"]h$]h&]uh1jhj] hhhNhNubj )}(hhh]h}(h]h ]h"]h$]h&]entries](j bpf_task_release (C function)c.bpf_task_releasehNtauh1j hj] hhhNhNubj )}(hhh](j )}(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}(hjhhhNhNubj )}(h h]h }(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjhhhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:571: ./kernel/bpf/helpers.chM$ ubhdesc_sig_keyword_type)}(hvoidh]hvoid}(hjhhhNhNubah}(h]h ]ktah"]h$]h&]uh1jhjhhhjhM$ ubj )}(h h]h }(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjhhhjhM$ ubj3 )}(hbpf_task_releaseh]j )}(hbpf_task_releaseh]hbpf_task_release}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubah}(h]h ](jH jI eh"]h$]h&]hhuh1j2 hjhhhjhM$ ubjN )}(h(struct task_struct *p)h]jT )}(hstruct task_struct *ph](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 )}(h task_structh]h task_struct}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubah}(h]h ]h"]h$]h&] refdomainjreftypej reftargetjmodnameN classnameNj j )}j ]j )}j jsbc.bpf_task_releaseasbuh1hhjubj )}(h h]h }(hj hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubj# )}(hjh]h*}(hj.hhhNhNubah}(h]h ]j. ah"]h$]h&]uh1j" hjubj )}(hj. h]hp}(hj;hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubeh}(h]h ]h"]h$]h&]noemphhhuh1jS hjubah}(h]h ]h"]h$]h&]hhuh1jM hjhhhjhM$ ubeh}(h]h ]h"]h$]h&]hhj uh1j j j hjhhhjhM$ ubah}(h]jah ](j j eh"]h$]h&]j j )j huh1j hjhM$ hjhhubj )}(hhh]h)}(h)Release the reference acquired on a task.h]h)Release the reference acquired on a task.}(hjdhhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:571: ./kernel/bpf/helpers.chM$ hjahhubah}(h]h ]h"]h$]h&]uh1j hjhhhjhM$ ubeh}(h]h ](jfunctioneh"]h$]h&]j jjj|jj|jjjuh1j hhhj] hNhNubj)}(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:571: ./kernel/bpf/helpers.chM( hjubj&)}(hhh]j+)}(hJ``struct task_struct *p`` The task on which a reference is being released.h](j1)}(h``struct task_struct *p``h]j)}(hjh]hstruct task_struct *p}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1j0hP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:571: ./kernel/bpf/helpers.chM* hjubjK)}(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:571: ./kernel/bpf/helpers.chM% hjubah}(h]h ]h"]h$]h&]uh1jJhjubeh}(h]h ]h"]h$]h&]uh1j*hjhM* hjubah}(h]h ]h"]h$]h&]uh1j%hjubeh}(h]h ] kernelindentah"]h$]h&]uh1jhj] 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&]uh1hhhhM>hj] 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&]hhjjjj}uh1jhhhMBhj] 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&]uh1hhhhMYhj] 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; }}hj5sbah}(h]h ]h"]h$]h&]hhjjjj}uh1jhhhM]hj] hhubh transition)}(h----h]h}(h]h ]h"]h$]h&]uh1jDhhhM}hj] 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 }(hjPhhhNhNubj)}(h``struct task_struct *``h]hstruct task_struct *}(hjXhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjPubhC object that it can acquire a reference on with bpf_task_acquire().}(hjPhhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhj] hhubj )}(hhh]h}(h]h ]h"]h$]h&]entries](j bpf_task_from_pid (C function)c.bpf_task_from_pidhNtauh1j hj] hhhNhNubj )}(hhh](j )}(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}(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 )}(h task_structh]h task_struct}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubah}(h]h ]h"]h$]h&] refdomainjreftypej reftargetjmodnameN classnameNj j )}j ]j )}j bpf_task_from_pidsbc.bpf_task_from_pidasbuh1hhjhhhjhM ubj )}(h h]h }(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjhhhjhM ubj# )}(hjh]h*}(hjhhhNhNubah}(h]h ]j. ah"]h$]h&]uh1j" hjhhhjhM ubj3 )}(hbpf_task_from_pidh]j )}(hjh]hbpf_task_from_pid}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubah}(h]h ](jH jI eh"]h$]h&]hhuh1j2 hjhhhjhM ubjN )}(h (s32 pid)h]jT )}(hs32 pidh](h)}(hhh]j )}(hs32h]hs32}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubah}(h]h ]h"]h$]h&] refdomainjreftypej reftargetjmodnameN classnameNj j )}j ]jc.bpf_task_from_pidasbuh1hhjubj )}(h h]h }(hj6hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubj )}(hpidh]hpid}(hjDhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubeh}(h]h ]h"]h$]h&]noemphhhuh1jS hj ubah}(h]h ]h"]h$]h&]hhuh1jM hjhhhjhM ubeh}(h]h ]h"]h$]h&]hhj uh1j j j hjhhhjhM ubah}(h]j|ah ](j j eh"]h$]h&]j j )j huh1j hjhM hj~hhubj )}(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().}(hjnhhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:643: ./kernel/bpf/helpers.chM hjkhhubah}(h]h ]h"]h$]h&]uh1j hj~hhhjhM ubeh}(h]h ](jfunctioneh"]h$]h&]j jjjjjjjjuh1j hhhj] hNhNubj)}(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:643: ./kernel/bpf/helpers.chM hjubj&)}(hhh]j+)}(h0``s32 pid`` The pid of the task being looked up.h](j1)}(h ``s32 pid``h]j)}(hjh]hs32 pid}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1j0hP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:643: ./kernel/bpf/helpers.chM hjubjK)}(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:643: ./kernel/bpf/helpers.chM hjubah}(h]h ]h"]h$]h&]uh1jJhjubeh}(h]h ]h"]h$]h&]uh1j*hjhM hjubah}(h]h ]h"]h$]h&]uh1j%hjubeh}(h]h ] kernelindentah"]h$]h&]uh1jhj] 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&]uh1hhhhMhj] 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&]hhjjjj}uh1jhhhMhj] hhubeh}(h]struct-task-struct-kfuncsah ]h"]4.1 struct task_struct * kfuncsah$]h&]uh1hhj> hhhhhM6ubh)}(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 *}(hj*hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj&ubh1 objects also have acquire and release functions:}(hj&hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj )}(hhh]h}(h]h ]h"]h$]h&]entries](j bpf_cgroup_acquire (C function)c.bpf_cgroup_acquirehNtauh1j hjhhhNhNubj )}(hhh](j )}(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}(hjWhhhNhNubj )}(h h]h }(hj_hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjWhhhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:683: ./kernel/bpf/helpers.chM4 ubj )}(hj h]hstruct}(hjnhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjWhhhjmhM4 ubj )}(h h]h }(hj{hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjWhhhjmhM4 ubh)}(hhh]j )}(hcgrouph]hcgroup}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubah}(h]h ]h"]h$]h&] refdomainjreftypej reftargetjmodnameN classnameNj j )}j ]j )}j bpf_cgroup_acquiresbc.bpf_cgroup_acquireasbuh1hhjWhhhjmhM4 ubj )}(h h]h }(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjWhhhjmhM4 ubj# )}(hjh]h*}(hjhhhNhNubah}(h]h ]j. ah"]h$]h&]uh1j" hjWhhhjmhM4 ubj3 )}(hbpf_cgroup_acquireh]j )}(hjh]hbpf_cgroup_acquire}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubah}(h]h ](jH jI eh"]h$]h&]hhuh1j2 hjWhhhjmhM4 ubjN )}(h(struct cgroup *cgrp)h]jT )}(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&] refdomainjreftypej reftargetjmodnameN classnameNj j )}j ]jc.bpf_cgroup_acquireasbuh1hhjubj )}(h h]h }(hj#hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubj# )}(hjh]h*}(hj1hhhNhNubah}(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&]noemphhhuh1jS hjubah}(h]h ]h"]h$]h&]hhuh1jM hjWhhhjmhM4 ubeh}(h]h ]h"]h$]h&]hhj uh1j j j hjShhhjmhM4 ubah}(h]jNah ](j j eh"]h$]h&]j j )j huh1j hjmhM4 hjPhhubj )}(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().}(hjhhhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:683: ./kernel/bpf/helpers.chM4 hjehhubah}(h]h ]h"]h$]h&]uh1j hjPhhhjmhM4 ubeh}(h]h ](jfunctioneh"]h$]h&]j jjjjjjjjuh1j 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:683: ./kernel/bpf/helpers.chM8 hjubj&)}(hhh]j+)}(hJ``struct cgroup *cgrp`` The cgroup on which a reference is being acquired.h](j1)}(h``struct cgroup *cgrp``h]j)}(hjh]hstruct cgroup *cgrp}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1j0hP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:683: ./kernel/bpf/helpers.chM: hjubjK)}(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:683: ./kernel/bpf/helpers.chM7 hjubah}(h]h ]h"]h$]h&]uh1jJhjubeh}(h]h ]h"]h$]h&]uh1j*hjhM: hjubah}(h]h ]h"]h$]h&]uh1j%hjubeh}(h]h ] kernelindentah"]h$]h&]uh1jhjhhhNhNubj )}(hhh]h}(h]h ]h"]h$]h&]entries](j bpf_cgroup_release (C function)c.bpf_cgroup_releasehNtauh1j hjhhhNhNubj )}(hhh](j )}(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:683: ./kernel/bpf/helpers.chM? ubj)}(hvoidh]hvoid}(hjhhhNhNubah}(h]h ]jah"]h$]h&]uh1jhjhhhjhM? ubj )}(h h]h }(hj$hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjhhhjhM? ubj3 )}(hbpf_cgroup_releaseh]j )}(hbpf_cgroup_releaseh]hbpf_cgroup_release}(hj6hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj2ubah}(h]h ](jH jI eh"]h$]h&]hhuh1j2 hjhhhjhM? ubjN )}(h(struct cgroup *cgrp)h]jT )}(hstruct cgroup *cgrph](j )}(hj h]hstruct}(hjRhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjNubj )}(h h]h }(hj_hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjNubh)}(hhh]j )}(hcgrouph]hcgroup}(hjphhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjmubah}(h]h ]h"]h$]h&] refdomainjreftypej reftargetjrmodnameN classnameNj j )}j ]j )}j j8sbc.bpf_cgroup_releaseasbuh1hhjNubj )}(h h]h }(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjNubj# )}(hjh]h*}(hjhhhNhNubah}(h]h ]j. ah"]h$]h&]uh1j" hjNubj )}(hcgrph]hcgrp}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjNubeh}(h]h ]h"]h$]h&]noemphhhuh1jS hjJubah}(h]h ]h"]h$]h&]hhuh1jM hjhhhjhM? ubeh}(h]h ]h"]h$]h&]hhj uh1j j j hjhhhjhM? ubah}(h]jah ](j j eh"]h$]h&]j j )j huh1j hjhM? 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:683: ./kernel/bpf/helpers.chM? hjhhubah}(h]h ]h"]h$]h&]uh1j hjhhhjhM? ubeh}(h]h ](jfunctioneh"]h$]h&]j jjjjjjjjuh1j 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:683: ./kernel/bpf/helpers.chMC hjubj&)}(hhh]j+)}(hJ``struct cgroup *cgrp`` The cgroup on which a reference is being released.h](j1)}(h``struct cgroup *cgrp``h]j)}(hjh]hstruct cgroup *cgrp}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1j0hP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:683: ./kernel/bpf/helpers.chME hjubjK)}(hhh]h)}(h2The cgroup on which a reference is being released.h]h2The cgroup on which a reference is being released.}(hj/hhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:683: ./kernel/bpf/helpers.chMC hj,ubah}(h]h ]h"]h$]h&]uh1jJhjubeh}(h]h ]h"]h$]h&]uh1j*hj+hME hj ubah}(h]h ]h"]h$]h&]uh1j%hjubeh}(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.}(hjWhhhNhNubah}(h]h ]h"]h$]h&]uh1hhhhMhjhhubjE)}(h----h]h}(h]h ]h"]h$]h&]uh1jDhhhMhjhhubh)}(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 }(hjohhhNhNubj)}(h``struct cgroup *``h]hstruct cgroup *}(hjwhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjoubh 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.}(hjohhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjhhubj )}(hhh]h}(h]h ]h"]h$]h&]entries](j bpf_cgroup_ancestor (C function)c.bpf_cgroup_ancestorhNtauh1j hjhhhNhNubj )}(hhh](j )}(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}(hjhhhNhNubj )}(h h]h }(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjhhhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:696: ./kernel/bpf/helpers.chMQ ubj )}(hj h]hstruct}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjhhhjhMQ ubj )}(h h]h }(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjhhhjhMQ ubh)}(hhh]j )}(hcgrouph]hcgroup}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubah}(h]h ]h"]h$]h&] refdomainjreftypej reftargetjmodnameN classnameNj j )}j ]j )}j bpf_cgroup_ancestorsbc.bpf_cgroup_ancestorasbuh1hhjhhhjhMQ ubj )}(h h]h }(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjhhhjhMQ ubj# )}(hjh]h*}(hjhhhNhNubah}(h]h ]j. ah"]h$]h&]uh1j" hjhhhjhMQ ubj3 )}(hbpf_cgroup_ancestorh]j )}(hjh]hbpf_cgroup_ancestor}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubah}(h]h ](jH jI eh"]h$]h&]hhuh1j2 hjhhhjhMQ ubjN )}(h (struct cgroup *cgrp, int level)h](jT )}(hstruct cgroup *cgrph](j )}(hj h]hstruct}(hj4hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj0ubj )}(h h]h }(hjAhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj0ubh)}(hhh]j )}(hcgrouph]hcgroup}(hjRhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjOubah}(h]h ]h"]h$]h&] refdomainjreftypej reftargetjTmodnameN classnameNj j )}j ]jc.bpf_cgroup_ancestorasbuh1hhj0ubj )}(h h]h }(hjphhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj0ubj# )}(hjh]h*}(hj~hhhNhNubah}(h]h ]j. ah"]h$]h&]uh1j" hj0ubj )}(hcgrph]hcgrp}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj0ubeh}(h]h ]h"]h$]h&]noemphhhuh1jS hj,ubjT )}(h int levelh](j)}(hinth]hint}(hjhhhNhNubah}(h]h ]jah"]h$]h&]uh1jhjubj )}(h h]h }(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubj )}(hlevelh]hlevel}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubeh}(h]h ]h"]h$]h&]noemphhhuh1jS hj,ubeh}(h]h ]h"]h$]h&]hhuh1jM hjhhhjhMQ ubeh}(h]h ]h"]h$]h&]hhj uh1j j j hjhhhjhMQ ubah}(h]jah ](j j eh"]h$]h&]j j )j huh1j hjhMQ hjhhubj )}(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:696: ./kernel/bpf/helpers.chMQ hjhhubah}(h]h ]h"]h$]h&]uh1j hjhhhjhMQ ubeh}(h]h ](jfunctioneh"]h$]h&]j jjjjjjjjuh1j 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)}(hj h]h Parameters}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhj ubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:696: ./kernel/bpf/helpers.chMU hjubj&)}(hhh](j+)}(hH``struct cgroup *cgrp`` The cgroup for which we're performing a lookup. h](j1)}(h``struct cgroup *cgrp``h]j)}(hj+h]hstruct cgroup *cgrp}(hj-hhhNhNubah}(h]h ]h"]h$]h&]uh1jhj)ubah}(h]h ]h"]h$]h&]uh1j0hP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:696: ./kernel/bpf/helpers.chMT hj%ubjK)}(hhh]h)}(h/The cgroup for which we're performing a lookup.h]h1The cgroup for which we’re performing a lookup.}(hjDhhhNhNubah}(h]h ]h"]h$]h&]uh1hhj@hMT hjAubah}(h]h ]h"]h$]h&]uh1jJhj%ubeh}(h]h ]h"]h$]h&]uh1j*hj@hMT hj"ubj+)}(h/``int level`` The level of ancestor to look up.h](j1)}(h ``int level``h]j)}(hjdh]h int level}(hjfhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjbubah}(h]h ]h"]h$]h&]uh1j0hP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:696: ./kernel/bpf/helpers.chMV hj^ubjK)}(hhh]h)}(h!The level of ancestor to look up.h]h!The level of ancestor to look up.}(hj}hhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:696: ./kernel/bpf/helpers.chMU hjzubah}(h]h ]h"]h$]h&]uh1jJhj^ubeh}(h]h ]h"]h$]h&]uh1j*hjyhMV hj"ubeh}(h]h ]h"]h$]h&]uh1j%hjubeh}(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_idhNtauh1j hjhhhNhNubj )}(hhh](j )}(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:699: ./kernel/bpf/helpers.chMf ubj )}(hj h]hstruct}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjhhhjhMf ubj )}(h h]h }(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjhhhjhMf ubh)}(hhh]j )}(hcgrouph]hcgroup}(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjubah}(h]h ]h"]h$]h&] refdomainjreftypej reftargetjmodnameN classnameNj j )}j ]j )}j bpf_cgroup_from_idsbc.bpf_cgroup_from_idasbuh1hhjhhhjhMf ubj )}(h h]h }(hjhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjhhhjhMf ubj# )}(hjh]h*}(hjhhhNhNubah}(h]h ]j. ah"]h$]h&]uh1j" hjhhhjhMf ubj3 )}(hbpf_cgroup_from_idh]j )}(hj h]hbpf_cgroup_from_id}(hj/hhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hj+ubah}(h]h ](jH jI eh"]h$]h&]hhuh1j2 hjhhhjhMf ubjN )}(h (u64 cgid)h]jT )}(hu64 cgidh](h)}(hhh]j )}(hu64h]hu64}(hjMhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjJubah}(h]h ]h"]h$]h&] refdomainjreftypej reftargetjOmodnameN classnameNj j )}j ]j c.bpf_cgroup_from_idasbuh1hhjFubj )}(h h]h }(hjkhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjFubj )}(hcgidh]hcgid}(hjyhhhNhNubah}(h]h ]j ah"]h$]h&]uh1j hjFubeh}(h]h ]h"]h$]h&]noemphhhuh1jS hjBubah}(h]h ]h"]h$]h&]hhuh1jM hjhhhjhMf ubeh}(h]h ]h"]h$]h&]hhj uh1j j j hjhhhjhMf ubah}(h]jah ](j j eh"]h$]h&]j j )j huh1j hjhMf 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().}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:699: ./kernel/bpf/helpers.chMf hjhhubah}(h]h ]h"]h$]h&]uh1j hjhhhjhMf ubeh}(h]h ](jfunctioneh"]h$]h&]j jjjjjjjjuh1j hhhjhNhNubj)}(h)**Parameters** ``u64 cgid`` cgroup id.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:699: ./kernel/bpf/helpers.chMj hjubj&)}(hhh]j+)}(h``u64 cgid`` cgroup id.h](j1)}(h ``u64 cgid``h]j)}(hjh]hu64 cgid}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1jhjubah}(h]h ]h"]h$]h&]uh1j0hP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:699: ./kernel/bpf/helpers.chMl hjubjK)}(hhh]h)}(h cgroup id.h]h cgroup id.}(hjhhhNhNubah}(h]h ]h"]h$]h&]uh1hhP/var/lib/git/docbuild/linux/Documentation/bpf/kfuncs:699: ./kernel/bpf/helpers.chMi hjubah}(h]h ]h"]h$]h&]uh1jJhjubeh}(h]h ]h"]h$]h&]uh1j*hjhMl hjubah}(h]h ]h"]h$]h&]uh1j%hjubeh}(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:}(hj%hhhNhNubah}(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; }}hj3sbah}(h]h ]h"]h$]h&]hhjjjj}uh1jhhhMhjhhubeh}(h]struct-cgroup-kfuncsah ]h"]4.2 struct cgroup * kfuncsah$]h&]uh1hhj> hhhhhMubh)}(hhh](h)}(h4.3 struct cpumask * kfuncsh]h4.3 struct cpumask * kfuncs}(hjMhhhNhNubah}(h]h ]h"]h$]h&]uh1hhjJhhhhhMubh)}(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)}(hjeh]hcpumasks-header-label}(hjghhhNhNubah}(h]h ](jstdstd-refeh"]h$]h&]uh1hhjcubah}(h]h ]h"]h$]h&]refdocj refdomainjqreftyperef refexplicitrefwarnjcpumasks-header-labeluh1hhhhMhj[ubh for more details.}(hj[hhhNhNubeh}(h]h ]h"]h$]h&]uh1hhhhMhjJhhubeh}(h]struct-cpumask-kfuncsah ]h"]4.3 struct cpumask * kfuncsah$]h&]uh1hhj> hhhhhMubeh}(h] core-kfuncsah ]h"]4. core kfuncsah$]h&]uh1hhhhhhhhM/ubeh}(h](bpf-kernel-functions-kfuncsheh ]h"](bpf kernel functions (kfuncs)kfuncs-header-labeleh$]h&]uh1hhhhhhhhKj@}jhsjB}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]jaj]jaj ]j aunameids}(jhjjj&j#j j jjjjjj jRjOjjjjj}jzjjj=jj<j9jjjjjjj'j$jNjKjujrjjjj jhjjgjdj j j< j9 j j j9 j j8 j5 j0 j- jjjjjGjDjju nametypes}(jjj&j jjjjRjjj}jj=j<jjjj'jNjujjjhjgj j< j j9 j8 j0 jjjGjuh}(hhjhj#hj j)jjzjjj jjOjjjUjjjzjjjjjj9jjjDjjjjj$jjKj*jrjQjjxj jjjjdjj jmj9 j j j? j j j5 j j- je jj> jj] j j jjj|jjDjjNjSjjjjjjjjJu 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.}hj8sbah}(h]h ]h"]h$]h&]uh1hhj5ubah}(h]h ]h"]h$]h&]levelKtypeINFOsourcehlineKuh1j3ubj4)}(hhh]h)}(hhh]h5Hyperlink target "bpf-kfunc-nodef" is not referenced.}hjSsbah}(h]h ]h"]h$]h&]uh1hhjPubah}(h]h ]h"]h$]h&]levelKtypejMsourcehlineKuh1j3ubj4)}(hhh]h)}(hhh]h8Hyperlink target "kf-deprecated-flag" is not referenced.}hjmsbah}(h]h ]h"]h$]h&]uh1hhjjubah}(h]h ]h"]h$]h&]levelKtypejMsourcehlineMYuh1j3ubj4)}(hhh]h)}(hhh]hFHyperlink target "bpf-kfunc-lifecycle-expectations" is not referenced.}hjsbah}(h]h ]h"]h$]h&]uh1hhjubah}(h]h ]h"]h$]h&]levelKtypejMsourcehlineMuh1j3ube transformerN include_log] decorationNhhub.