* Re: [PATCH] Documentation/bpf: make it clear that kfuncs should be non-static
2026-06-26 17:20 [PATCH] Documentation/bpf: make it clear that kfuncs should be non-static JP Kobryn
@ 2026-06-26 19:53 ` Randy Dunlap
2026-06-26 23:57 ` Roman Gushchin
` (2 subsequent siblings)
3 siblings, 0 replies; 6+ messages in thread
From: Randy Dunlap @ 2026-06-26 19:53 UTC (permalink / raw)
To: JP Kobryn, ast, roman.gushchin, daniel, andrii, eddyz87, memxor,
martin.lau, song, yonghong.song, jolsa, emil, corbet, skhan, bpf
Cc: linux-doc, linux-kernel
On 6/26/26 10:20 AM, JP Kobryn wrote:
> The kfunc documentation mentions how the macro __bpf_kfunc prevents
> inlining for static functions. This makes it sound like static kfuncs are
> acceptable. Although static kfuncs may happen to work, it is by chance that
> the compiler chose not to rename these functions and BTF resolution still
> succeeds.
>
> Make it clear in the documentation why kfuncs should not be declared
> static. First, remove wording that makes it sound like static is ok. Then
> point out the external naming needed for BTF resolution. Finally point out
> that sparse may warn on unreferenced kfuncs and that this warning can be
> ignored.
>
> Signed-off-by: JP Kobryn <jp.kobryn@linux.dev>
> ---
> Documentation/bpf/kfuncs.rst | 21 ++++++++++++++-------
> 1 file changed, 14 insertions(+), 7 deletions(-)
>
> diff --git a/Documentation/bpf/kfuncs.rst b/Documentation/bpf/kfuncs.rst
> index 4c814ff6061e..1dbaff8d4805 100644
> --- a/Documentation/bpf/kfuncs.rst
> +++ b/Documentation/bpf/kfuncs.rst
> @@ -276,19 +276,26 @@ This 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.
>
Not part of this patch, but "Ofcourse," should be "Of course,"
of course.
--
~Randy
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH] Documentation/bpf: make it clear that kfuncs should be non-static
2026-06-26 17:20 [PATCH] Documentation/bpf: make it clear that kfuncs should be non-static JP Kobryn
2026-06-26 19:53 ` Randy Dunlap
@ 2026-06-26 23:57 ` Roman Gushchin
2026-06-29 4:38 ` Yonghong Song
2026-07-01 22:24 ` Andrii Nakryiko
3 siblings, 0 replies; 6+ messages in thread
From: Roman Gushchin @ 2026-06-26 23:57 UTC (permalink / raw)
To: JP Kobryn
Cc: ast, daniel, andrii, eddyz87, memxor, martin.lau, song,
yonghong.song, jolsa, emil, corbet, skhan, bpf, linux-doc,
linux-kernel
JP Kobryn <jp.kobryn@linux.dev> writes:
> The kfunc documentation mentions how the macro __bpf_kfunc prevents
> inlining for static functions. This makes it sound like static kfuncs are
> acceptable. Although static kfuncs may happen to work, it is by chance that
> the compiler chose not to rename these functions and BTF resolution still
> succeeds.
>
> Make it clear in the documentation why kfuncs should not be declared
> static. First, remove wording that makes it sound like static is ok. Then
> point out the external naming needed for BTF resolution. Finally point out
> that sparse may warn on unreferenced kfuncs and that this warning can be
> ignored.
>
> Signed-off-by: JP Kobryn <jp.kobryn@linux.dev>
> ---
> Documentation/bpf/kfuncs.rst | 21 ++++++++++++++-------
> 1 file changed, 14 insertions(+), 7 deletions(-)
>
> diff --git a/Documentation/bpf/kfuncs.rst b/Documentation/bpf/kfuncs.rst
> index 4c814ff6061e..1dbaff8d4805 100644
> --- a/Documentation/bpf/kfuncs.rst
> +++ b/Documentation/bpf/kfuncs.rst
> @@ -276,19 +276,26 @@ This 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.
>
> kfunc 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::
> +macro. This prevents issues such as the compiler inlining the kfunc, or the
> +function being elided in an LTO build as it's not used in the rest of the
> +kernel. Developers should not manually add annotations to their kfunc to prevent
> +these issues. If an annotation is required to prevent such an issue with your
> +kfunc, it is a bug and should be added to the definition of the macro so that
> +other kfuncs are similarly protected. An example is given below::
>
> __bpf_kfunc struct task_struct *bpf_get_task_pid(s32 pid)
> {
> ...
> }
>
> +Note that kfuncs must not be declared ``static``. A kfunc can be called from a
> +BPF program ``*.c`` file outside the compilation unit that defines it, so its
> +externally visible name must remain available for BTF ID lookup. ``static``
> +linkage allows the compiler to rename the function, which can break this
> +BTF-based kfunc resolution. Further note that sparse may warn that an otherwise
> +unreferenced kfunc should be static. Such warnings should be ignored for kfunc
> +definitions.
> +
> 2.5.1 KF_ACQUIRE flag
> ---------------------
Acked-by: Roman Gushchin <roman.gushchin@linux.dev>
Thanks
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH] Documentation/bpf: make it clear that kfuncs should be non-static
2026-06-26 17:20 [PATCH] Documentation/bpf: make it clear that kfuncs should be non-static JP Kobryn
2026-06-26 19:53 ` Randy Dunlap
2026-06-26 23:57 ` Roman Gushchin
@ 2026-06-29 4:38 ` Yonghong Song
2026-07-01 22:24 ` Andrii Nakryiko
3 siblings, 0 replies; 6+ messages in thread
From: Yonghong Song @ 2026-06-29 4:38 UTC (permalink / raw)
To: JP Kobryn, ast, roman.gushchin, daniel, andrii, eddyz87, memxor,
martin.lau, song, jolsa, emil, corbet, skhan, bpf
Cc: linux-doc, linux-kernel
On 6/26/26 10:20 AM, JP Kobryn wrote:
> The kfunc documentation mentions how the macro __bpf_kfunc prevents
> inlining for static functions. This makes it sound like static kfuncs are
> acceptable. Although static kfuncs may happen to work, it is by chance that
> the compiler chose not to rename these functions and BTF resolution still
> succeeds.
>
> Make it clear in the documentation why kfuncs should not be declared
> static. First, remove wording that makes it sound like static is ok. Then
> point out the external naming needed for BTF resolution. Finally point out
> that sparse may warn on unreferenced kfuncs and that this warning can be
> ignored.
>
> Signed-off-by: JP Kobryn <jp.kobryn@linux.dev>
Acked-by: Yonghong Song <yonghong.song@linux.dev>
> ---
> Documentation/bpf/kfuncs.rst | 21 ++++++++++++++-------
> 1 file changed, 14 insertions(+), 7 deletions(-)
>
> diff --git a/Documentation/bpf/kfuncs.rst b/Documentation/bpf/kfuncs.rst
> index 4c814ff6061e..1dbaff8d4805 100644
> --- a/Documentation/bpf/kfuncs.rst
> +++ b/Documentation/bpf/kfuncs.rst
> @@ -276,19 +276,26 @@ This 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.
>
> kfunc 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::
> +macro. This prevents issues such as the compiler inlining the kfunc, or the
> +function being elided in an LTO build as it's not used in the rest of the
> +kernel. Developers should not manually add annotations to their kfunc to prevent
> +these issues. If an annotation is required to prevent such an issue with your
> +kfunc, it is a bug and should be added to the definition of the macro so that
> +other kfuncs are similarly protected. An example is given below::
>
> __bpf_kfunc struct task_struct *bpf_get_task_pid(s32 pid)
> {
> ...
> }
>
> +Note that kfuncs must not be declared ``static``. A kfunc can be called from a
> +BPF program ``*.c`` file outside the compilation unit that defines it, so its
> +externally visible name must remain available for BTF ID lookup. ``static``
> +linkage allows the compiler to rename the function, which can break this
> +BTF-based kfunc resolution. Further note that sparse may warn that an otherwise
> +unreferenced kfunc should be static. Such warnings should be ignored for kfunc
> +definitions.
> +
> 2.5.1 KF_ACQUIRE flag
> ---------------------
>
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH] Documentation/bpf: make it clear that kfuncs should be non-static
2026-06-26 17:20 [PATCH] Documentation/bpf: make it clear that kfuncs should be non-static JP Kobryn
` (2 preceding siblings ...)
2026-06-29 4:38 ` Yonghong Song
@ 2026-07-01 22:24 ` Andrii Nakryiko
2026-07-01 23:55 ` Alexei Starovoitov
3 siblings, 1 reply; 6+ messages in thread
From: Andrii Nakryiko @ 2026-07-01 22:24 UTC (permalink / raw)
To: JP Kobryn
Cc: ast, roman.gushchin, daniel, andrii, eddyz87, memxor, martin.lau,
song, yonghong.song, jolsa, emil, corbet, skhan, bpf, linux-doc,
linux-kernel
On Fri, Jun 26, 2026 at 10:21 AM JP Kobryn <jp.kobryn@linux.dev> wrote:
>
> The kfunc documentation mentions how the macro __bpf_kfunc prevents
> inlining for static functions. This makes it sound like static kfuncs are
> acceptable. Although static kfuncs may happen to work, it is by chance that
> the compiler chose not to rename these functions and BTF resolution still
> succeeds.
>
> Make it clear in the documentation why kfuncs should not be declared
> static. First, remove wording that makes it sound like static is ok. Then
> point out the external naming needed for BTF resolution. Finally point out
> that sparse may warn on unreferenced kfuncs and that this warning can be
> ignored.
>
> Signed-off-by: JP Kobryn <jp.kobryn@linux.dev>
> ---
> Documentation/bpf/kfuncs.rst | 21 ++++++++++++++-------
> 1 file changed, 14 insertions(+), 7 deletions(-)
>
> diff --git a/Documentation/bpf/kfuncs.rst b/Documentation/bpf/kfuncs.rst
> index 4c814ff6061e..1dbaff8d4805 100644
> --- a/Documentation/bpf/kfuncs.rst
> +++ b/Documentation/bpf/kfuncs.rst
> @@ -276,19 +276,26 @@ This 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.
>
> kfunc 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::
> +macro. This prevents issues such as the compiler inlining the kfunc, or the
> +function being elided in an LTO build as it's not used in the rest of the
> +kernel. Developers should not manually add annotations to their kfunc to prevent
> +these issues. If an annotation is required to prevent such an issue with your
> +kfunc, it is a bug and should be added to the definition of the macro so that
> +other kfuncs are similarly protected. An example is given below::
>
> __bpf_kfunc struct task_struct *bpf_get_task_pid(s32 pid)
> {
> ...
> }
>
> +Note that kfuncs must not be declared ``static``. A kfunc can be called from a
it seems like we do have static kfuncs and they work fine... And
__noclone in __bpf_kfunc is meant to prevent compiler doing
.constprop/.isra and other transformations.
> +BPF program ``*.c`` file outside the compilation unit that defines it, so its
> +externally visible name must remain available for BTF ID lookup. ``static``
> +linkage allows the compiler to rename the function, which can break this
> +BTF-based kfunc resolution. Further note that sparse may warn that an otherwise
> +unreferenced kfunc should be static. Such warnings should be ignored for kfunc
> +definitions.
> +
> 2.5.1 KF_ACQUIRE flag
> ---------------------
>
> --
> 2.54.0
>
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH] Documentation/bpf: make it clear that kfuncs should be non-static
2026-07-01 22:24 ` Andrii Nakryiko
@ 2026-07-01 23:55 ` Alexei Starovoitov
0 siblings, 0 replies; 6+ messages in thread
From: Alexei Starovoitov @ 2026-07-01 23:55 UTC (permalink / raw)
To: Andrii Nakryiko
Cc: JP Kobryn, Alexei Starovoitov, Roman Gushchin, Daniel Borkmann,
Andrii Nakryiko, Eduard, Kumar Kartikeya Dwivedi,
Martin KaFai Lau, Song Liu, Yonghong Song, Jiri Olsa,
Emil Tsalapatis, Jonathan Corbet, Shuah Khan, bpf,
open list:DOCUMENTATION, LKML
On Wed, Jul 1, 2026 at 3:24 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Fri, Jun 26, 2026 at 10:21 AM JP Kobryn <jp.kobryn@linux.dev> wrote:
> >
> > The kfunc documentation mentions how the macro __bpf_kfunc prevents
> > inlining for static functions. This makes it sound like static kfuncs are
> > acceptable. Although static kfuncs may happen to work, it is by chance that
> > the compiler chose not to rename these functions and BTF resolution still
> > succeeds.
> >
> > Make it clear in the documentation why kfuncs should not be declared
> > static. First, remove wording that makes it sound like static is ok. Then
> > point out the external naming needed for BTF resolution. Finally point out
> > that sparse may warn on unreferenced kfuncs and that this warning can be
> > ignored.
> >
> > Signed-off-by: JP Kobryn <jp.kobryn@linux.dev>
> > ---
> > Documentation/bpf/kfuncs.rst | 21 ++++++++++++++-------
> > 1 file changed, 14 insertions(+), 7 deletions(-)
> >
> > diff --git a/Documentation/bpf/kfuncs.rst b/Documentation/bpf/kfuncs.rst
> > index 4c814ff6061e..1dbaff8d4805 100644
> > --- a/Documentation/bpf/kfuncs.rst
> > +++ b/Documentation/bpf/kfuncs.rst
> > @@ -276,19 +276,26 @@ This 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.
> >
> > kfunc 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::
> > +macro. This prevents issues such as the compiler inlining the kfunc, or the
> > +function being elided in an LTO build as it's not used in the rest of the
> > +kernel. Developers should not manually add annotations to their kfunc to prevent
> > +these issues. If an annotation is required to prevent such an issue with your
> > +kfunc, it is a bug and should be added to the definition of the macro so that
> > +other kfuncs are similarly protected. An example is given below::
> >
> > __bpf_kfunc struct task_struct *bpf_get_task_pid(s32 pid)
> > {
> > ...
> > }
> >
> > +Note that kfuncs must not be declared ``static``. A kfunc can be called from a
>
> it seems like we do have static kfuncs and they work fine... And
> __noclone in __bpf_kfunc is meant to prevent compiler doing
> .constprop/.isra and other transformations.
There is no guarantee that compilers will follow that logic,
since "static" vs "__used __retain __noclone noinline" kinda conflict
with each other.
Next thing, some post optimizer that only looks at the symbol table
will decide to remove it.
So applied to bpf-next with typo fixed.
^ permalink raw reply [flat|nested] 6+ messages in thread