bpf: Fix race between btf_put and btf_idr walk.
[platform/kernel/linux-starfive.git] / kernel / bpf / btf.c
index a83a3f2..a8838a3 100644 (file)
@@ -572,8 +572,8 @@ static s32 bpf_find_btf_id(const char *name, u32 kind, struct btf **btf_p)
                        *btf_p = btf;
                        return ret;
                }
-               spin_lock_bh(&btf_idr_lock);
                btf_put(btf);
+               spin_lock_bh(&btf_idr_lock);
        }
        spin_unlock_bh(&btf_idr_lock);
        return ret;
@@ -7957,12 +7957,10 @@ check_modules:
                btf_get(mod_btf);
                spin_unlock_bh(&btf_idr_lock);
                cands = bpf_core_add_cands(cands, mod_btf, btf_nr_types(main_btf));
-               if (IS_ERR(cands)) {
-                       btf_put(mod_btf);
+               btf_put(mod_btf);
+               if (IS_ERR(cands))
                        return ERR_CAST(cands);
-               }
                spin_lock_bh(&btf_idr_lock);
-               btf_put(mod_btf);
        }
        spin_unlock_bh(&btf_idr_lock);
        /* cands is a pointer to kmalloced memory here if cands->cnt > 0