On 2/9/25 10:29, Alexei Starovoitov wrote: > On Mon, Sep 1, 2025 at 8:12 AM Leon Hwang <leon.hwang@xxxxxxxxx> wrote: >> >> >> I do a PoC of adding bpf_in_interrupt() to bpf_experimental.h. > > lgtm > >> It works: >> >> extern bool CONFIG_PREEMPT_RT __kconfig __weak; >> #ifdef bpf_target_x86 > > what is bpf_target_x86 ? > bpf_target_x86 is a macro defined in bpf_tracing.h: #if defined(__TARGET_ARCH_x86) #define bpf_target_x86 ... #if defined(__x86_64__) #define bpf_target_x86 ... >> extern const int __preempt_count __ksym; >> #endif >> >> struct task_struct__preempt_rt { >> int softirq_disable_cnt; >> } __attribute__((preserve_access_index)); >> >> /* Description >> * Report whether it is in interrupt context. Only works on x86. > > arm64 shouldn't be hard to support either. > No problem. I'll add support for arm64. >> */ >> static inline int bpf_in_interrupt(void) >> { >> #ifdef bpf_target_x86 >> int pcnt; >> >> pcnt = *(int *) bpf_this_cpu_ptr(&__preempt_count); >> if (!CONFIG_PREEMPT_RT) { >> return pcnt & (NMI_MASK | HARDIRQ_MASK | SOFTIRQ_MASK); >> } else { >> struct task_struct__preempt_rt *tsk; >> >> tsk = (void *) bpf_get_current_task_btf(); >> return (pcnt & (NMI_MASK | HARDIRQ_MASK)) | >> (tsk->softirq_disable_cnt | SOFTIRQ_MASK); >> } >> #else >> return 0; >> #endif >> } >> >> However, I only test it for !PREEMPT_RT on x86. >> >> I'd like to respin the patchset by moving bpf_in_interrupt() to >> bpf_experimental.h. > > I think the approach should work for PREEMPT_RT too. > Test it and send it. Yes, it works for PREEMPT_RT too. It is able to get retval of bpf_in_interrupt() by running a simple demo. However, it's really difficult to add selftest case for PREEMPT_RT, as I'm not familiar with PREEMPT_RT. Thanks, Leon [...]