[PATCH bpf-next] selftests/bpf: Return -1 from get_preempt_count() for unsupported architecture

Saket Kumar Bhaskar posted 1 patch 1 week, 2 days ago
tools/testing/selftests/bpf/bpf_experimental.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH bpf-next] selftests/bpf: Return -1 from get_preempt_count() for unsupported architecture
Posted by Saket Kumar Bhaskar 1 week, 2 days ago
Make get_preempt_count() to return -1 for unsupported
architectures instead of 0, so callers can distinguish not supported
from a valid preempt_count of 0.

Signed-off-by: Saket Kumar Bhaskar <skb99@linux.ibm.com>
---
 tools/testing/selftests/bpf/bpf_experimental.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/tools/testing/selftests/bpf/bpf_experimental.h b/tools/testing/selftests/bpf/bpf_experimental.h
index 2cd9165c7348..6759d2ae6151 100644
--- a/tools/testing/selftests/bpf/bpf_experimental.h
+++ b/tools/testing/selftests/bpf/bpf_experimental.h
@@ -631,7 +631,7 @@ static inline int get_preempt_count(void)
 #elif defined(bpf_target_arm64)
 	return bpf_get_current_task_btf()->thread_info.preempt.count;
 #endif
-	return 0;
+	return -1;
 }
 
 /* Description
-- 
2.52.0
Re: [PATCH bpf-next] selftests/bpf: Return -1 from get_preempt_count() for unsupported architecture
Posted by Yonghong Song 1 week, 1 day ago

On 1/28/26 9:09 PM, Saket Kumar Bhaskar wrote:
> Make get_preempt_count() to return -1 for unsupported
> architectures instead of 0, so callers can distinguish not supported
> from a valid preempt_count of 0.
>
> Signed-off-by: Saket Kumar Bhaskar <skb99@linux.ibm.com>

LGTM.

Acked-by: Yonghong Song <yonghong.song@linux.dev>
Re: [PATCH bpf-next] selftests/bpf: Return -1 from get_preempt_count() for unsupported architecture
Posted by Yonghong Song 1 week, 1 day ago

On 1/29/26 8:03 AM, Yonghong Song wrote:
>
>
> On 1/28/26 9:09 PM, Saket Kumar Bhaskar wrote:
>> Make get_preempt_count() to return -1 for unsupported
>> architectures instead of 0, so callers can distinguish not supported
>> from a valid preempt_count of 0.
>>
>> Signed-off-by: Saket Kumar Bhaskar <skb99@linux.ibm.com>
>
> LGTM.
>
> Acked-by: Yonghong Song <yonghong.song@linux.dev>

Dig through further. My above Ack is invalid due the following usage
e.g.
static inline int bpf_in_nmi(void)
{
         return get_preempt_count() & NMI_MASK;
}
in bpf_experimental.h. In such cases, returning -1 for get_preempt_count()
may cause bpf_in_nmi() to be true, but this is incorrect.
Re: [PATCH bpf-next] selftests/bpf: Return -1 from get_preempt_count() for unsupported architecture
Posted by bot+bpf-ci@kernel.org 1 week, 2 days ago
> diff --git a/tools/testing/selftests/bpf/bpf_experimental.h b/tools/testing/selftests/bpf/bpf_experimental.h
> index a39576c8ba..87706a213a 100644
> --- a/tools/testing/selftests/bpf/bpf_experimental.h
> +++ b/tools/testing/selftests/bpf/bpf_experimental.h
> @@ -628,7 +628,7 @@ static inline int get_preempt_count(void)
>  #elif defined(bpf_target_arm64)
>  	return bpf_get_current_task_btf()->thread_info.preempt.count;
>  #endif
> -	return 0;
> +	return -1;
>  }

Returning -1 here causes incorrect behavior in all the bpf_in_* helper
functions that call get_preempt_count(). Since -1 is 0xFFFFFFFF in
twos complement, the bitmask operations in the callers produce non-zero
results:

    bpf_in_nmi():     -1 & NMI_MASK     = 0x00F00000 (non-zero)
    bpf_in_hardirq(): -1 & HARDIRQ_MASK = 0x000F0000 (non-zero)
    bpf_in_interrupt(): returns non-zero (falsely indicates interrupt ctx)
    bpf_in_serving_softirq(): returns non-zero
    bpf_in_task(): returns 0 (falsely indicates NOT in task context)

Previously with return 0, these functions would return 0 on unsupported
architectures. Now they return false positives indicating the system is
in NMI/hardirq/softirq context when it may not be.

Should the callers be updated to check for -1 before performing the
bitmask operations? For example:

    static inline int bpf_in_nmi(void)
    {
        int pcnt = get_preempt_count();
        if (pcnt < 0)
            return -1;  /* or 0? */
        return pcnt & NMI_MASK;
    }


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/21466700415

AI-authorship-score: low
AI-authorship-explanation: Simple one-line change with clear human-authored commit message following standard kernel conventions.
issues-found: 1
issue-severity-score: low
issue-severity-explanation: Incomplete API change causes bpf_in_* functions to return false positives on unsupported architectures, but this only affects selftest code on non-x86/arm64 systems.