When running BPF struct_ops tests consecutively, the second test fails because BTF object references take longer to be released while their associated module is removed from btf_modules list immediately during module unload. This creates a race condition where the second test retrieves the cached BTF object but cannot find valid module information, causing btf_try_get_module() to return NULL. Fix this by modifying MODULE_STATE_GOING handling to wait for active BTF references to be released before completing module cleanup. This ensures BTF objects are properly cleaned up from the cache before their module metadata is removed, eliminating the timing window that caused the race condition. Signed-off-by: Gregory Bell --- kernel/bpf/btf.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/kernel/bpf/btf.c b/kernel/bpf/btf.c index 09fcbb125155..81c9f46a7bb7 100644 --- a/kernel/bpf/btf.c +++ b/kernel/bpf/btf.c @@ -8382,7 +8382,12 @@ static int btf_module_notify(struct notifier_block *nb, unsigned long op, list_for_each_entry_safe(btf_mod, tmp, &btf_modules, list) { if (btf_mod->module != module) continue; - + unsigned int timeout = 1000; + while (refcount_read(&btf_mod->btf->refcnt) > 1 + && timeout > 0 ) { + msleep(1); + timeout--; + } list_del(&btf_mod->list); if (btf_mod->sysfs_attr) sysfs_remove_bin_file(btf_kobj, btf_mod->sysfs_attr); -- 2.52.0