Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

kprobes: fix kill kprobe which has been marked as gone

If a kprobe is marked as gone, we should not kill it again. Otherwise, we
can disarm the kprobe more than once. In that case, the statistics of
kprobe_ftrace_enabled can unbalance which can lead to that kprobe do not
work.

Fixes: e8386a0cb22f ("kprobes: support probing module __exit function")
Co-developed-by: Chengming Zhou <zhouchengming@bytedance.com>
Signed-off-by: Muchun Song <songmuchun@bytedance.com>
Signed-off-by: Chengming Zhou <zhouchengming@bytedance.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Acked-by: Masami Hiramatsu <mhiramat@kernel.org>
Cc: "Naveen N . Rao" <naveen.n.rao@linux.ibm.com>
Cc: Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
Cc: David S. Miller <davem@davemloft.net>
Cc: Song Liu <songliubraving@fb.com>
Cc: Steven Rostedt <rostedt@goodmis.org>
Cc: <stable@vger.kernel.org>
Link: https://lkml.kernel.org/r/20200822030055.32383-1-songmuchun@bytedance.com
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Muchun Song and committed by
Linus Torvalds
b0399092 bb3e96d6

+8 -1
+8 -1
kernel/kprobes.c
··· 2140 2140 2141 2141 lockdep_assert_held(&kprobe_mutex); 2142 2142 2143 + if (WARN_ON_ONCE(kprobe_gone(p))) 2144 + return; 2145 + 2143 2146 p->flags |= KPROBE_FLAG_GONE; 2144 2147 if (kprobe_aggrprobe(p)) { 2145 2148 /* ··· 2422 2419 mutex_lock(&kprobe_mutex); 2423 2420 for (i = 0; i < KPROBE_TABLE_SIZE; i++) { 2424 2421 head = &kprobe_table[i]; 2425 - hlist_for_each_entry(p, head, hlist) 2422 + hlist_for_each_entry(p, head, hlist) { 2423 + if (kprobe_gone(p)) 2424 + continue; 2425 + 2426 2426 if (within_module_init((unsigned long)p->addr, mod) || 2427 2427 (checkcore && 2428 2428 within_module_core((unsigned long)p->addr, mod))) { ··· 2442 2436 */ 2443 2437 kill_kprobe(p); 2444 2438 } 2439 + } 2445 2440 } 2446 2441 if (val == MODULE_STATE_GOING) 2447 2442 remove_module_kprobe_blacklist(mod);