Skip to content

Commit 4fbd2f8

Browse files
committed
kprobes: Fix to handle forcibly unoptimized kprobes on freeing_list
Since forcibly unoptimized kprobes will be put on the freeing_list directly in the unoptimize_kprobe(), do_unoptimize_kprobes() must continue to check the freeing_list even if unoptimizing_list is empty. This bug can happen if a kprobe is put in an instruction which is in the middle of the jump-replaced instruction sequence of an optprobe, *and* the optprobe is recently unregistered and queued on unoptimizing_list. In this case, the optprobe will be unoptimized forcibly (means immediately) and put it into the freeing_list, expecting the optprobe will be handled in do_unoptimize_kprobe(). But if there is no other optprobes on the unoptimizing_list, current code returns from the do_unoptimize_kprobe() soon and does not handle the optprobe which is on the freeing_list. Then the optprobe will hit the WARN_ON_ONCE() in the do_free_cleaned_kprobes(), because it is not handled in the latter loop of the do_unoptimize_kprobe(). To solve this issue, do not return from do_unoptimize_kprobes() immediately even if unoptimizing_list is empty. Moreover, this change affects another case. kill_optimized_kprobes() expects kprobe_optimizer() will just free the optprobe on freeing_list. So I changed it to just do list_move() to freeing_list if optprobes are on unoptimizing list. And the do_unoptimize_kprobe() will skip arch_disarm_kprobe() if the probe on freeing_list has gone flag. Link: https://lore.kernel.org/all/[email protected]/ Link: https://lore.kernel.org/all/167448024501.3253718.13037333683110512967.stgit@devnote3/ Fixes: e4add24 ("kprobes: Fix optimize_kprobe()/unoptimize_kprobe() cancellation logic") Reported-by: Pengfei Xu <[email protected]> Signed-off-by: Masami Hiramatsu (Google) <[email protected]> Cc: [email protected] Acked-by: Steven Rostedt (Google) <[email protected]>
1 parent c9c3395 commit 4fbd2f8

File tree

1 file changed

+10
-13
lines changed

1 file changed

+10
-13
lines changed

kernel/kprobes.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -555,17 +555,15 @@ static void do_unoptimize_kprobes(void)
555555
/* See comment in do_optimize_kprobes() */
556556
lockdep_assert_cpus_held();
557557

558-
/* Unoptimization must be done anytime */
559-
if (list_empty(&unoptimizing_list))
560-
return;
558+
if (!list_empty(&unoptimizing_list))
559+
arch_unoptimize_kprobes(&unoptimizing_list, &freeing_list);
561560

562-
arch_unoptimize_kprobes(&unoptimizing_list, &freeing_list);
563-
/* Loop on 'freeing_list' for disarming */
561+
/* Loop on 'freeing_list' for disarming and removing from kprobe hash list */
564562
list_for_each_entry_safe(op, tmp, &freeing_list, list) {
565563
/* Switching from detour code to origin */
566564
op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
567-
/* Disarm probes if marked disabled */
568-
if (kprobe_disabled(&op->kp))
565+
/* Disarm probes if marked disabled and not gone */
566+
if (kprobe_disabled(&op->kp) && !kprobe_gone(&op->kp))
569567
arch_disarm_kprobe(&op->kp);
570568
if (kprobe_unused(&op->kp)) {
571569
/*
@@ -797,14 +795,13 @@ static void kill_optimized_kprobe(struct kprobe *p)
797795
op->kp.flags &= ~KPROBE_FLAG_OPTIMIZED;
798796

799797
if (kprobe_unused(p)) {
800-
/* Enqueue if it is unused */
801-
list_add(&op->list, &freeing_list);
802798
/*
803-
* Remove unused probes from the hash list. After waiting
804-
* for synchronization, this probe is reclaimed.
805-
* (reclaiming is done by do_free_cleaned_kprobes().)
799+
* Unused kprobe is on unoptimizing or freeing list. We move it
800+
* to freeing_list and let the kprobe_optimizer() remove it from
801+
* the kprobe hash list and free it.
806802
*/
807-
hlist_del_rcu(&op->kp.hlist);
803+
if (optprobe_queued_unopt(op))
804+
list_move(&op->list, &freeing_list);
808805
}
809806

810807
/* Don't touch the code, because it is already freed. */

0 commit comments

Comments
 (0)