Skip to content

Commit 73c12f2

Browse files
committed
kprobes: Use dedicated kthread for kprobe optimizer
Instead of using generic workqueue, use a dedicated kthread for optimizing kprobes, because it can wait (sleep) for a long time inside the process by synchronize_rcu_task(). This means other works can be stopped until it finishes. Link: https://lore.kernel.org/all/176970170302.114949.5175231591310436910.stgit@devnote2/ Suggested-by: Steven Rostedt <rostedt@goodmis.org> Signed-off-by: Masami Hiramatsu (Google) <mhiramat@kernel.org>
1 parent b8121b9 commit 73c12f2

1 file changed

Lines changed: 86 additions & 20 deletions

File tree

kernel/kprobes.c

Lines changed: 86 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <linux/debugfs.h>
3333
#include <linux/sysctl.h>
3434
#include <linux/kdebug.h>
35+
#include <linux/kthread.h>
3536
#include <linux/memory.h>
3637
#include <linux/ftrace.h>
3738
#include <linux/cpu.h>
@@ -40,6 +41,7 @@
4041
#include <linux/perf_event.h>
4142
#include <linux/execmem.h>
4243
#include <linux/cleanup.h>
44+
#include <linux/wait.h>
4345

4446
#include <asm/sections.h>
4547
#include <asm/cacheflush.h>
@@ -514,9 +516,18 @@ static LIST_HEAD(optimizing_list);
514516
static LIST_HEAD(unoptimizing_list);
515517
static LIST_HEAD(freeing_list);
516518

517-
static void kprobe_optimizer(struct work_struct *work);
518-
static DECLARE_DELAYED_WORK(optimizing_work, kprobe_optimizer);
519519
static void optimize_kprobe(struct kprobe *p);
520+
static struct task_struct *kprobe_optimizer_task;
521+
static wait_queue_head_t kprobe_optimizer_wait;
522+
static atomic_t optimizer_state;
523+
enum {
524+
OPTIMIZER_ST_IDLE = 0,
525+
OPTIMIZER_ST_KICKED = 1,
526+
OPTIMIZER_ST_FLUSHING = 2,
527+
};
528+
529+
static DECLARE_COMPLETION(optimizer_completion);
530+
520531
#define OPTIMIZE_DELAY 5
521532

522533
/*
@@ -609,14 +620,10 @@ static void do_free_cleaned_kprobes(void)
609620
}
610621
}
611622

612-
/* Start optimizer after OPTIMIZE_DELAY passed */
613-
static void kick_kprobe_optimizer(void)
614-
{
615-
schedule_delayed_work(&optimizing_work, OPTIMIZE_DELAY);
616-
}
623+
static void kick_kprobe_optimizer(void);
617624

618625
/* Kprobe jump optimizer */
619-
static void kprobe_optimizer(struct work_struct *work)
626+
static void kprobe_optimizer(void)
620627
{
621628
guard(mutex)(&kprobe_mutex);
622629

@@ -647,23 +654,71 @@ static void kprobe_optimizer(struct work_struct *work)
647654
do_free_cleaned_kprobes();
648655
}
649656

650-
/* Step 5: Kick optimizer again if needed */
657+
/* Step 5: Kick optimizer again if needed. But if there is a flush requested, */
658+
if (completion_done(&optimizer_completion))
659+
complete(&optimizer_completion);
660+
651661
if (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list))
652-
kick_kprobe_optimizer();
662+
kick_kprobe_optimizer(); /*normal kick*/
663+
}
664+
665+
static int kprobe_optimizer_thread(void *data)
666+
{
667+
while (!kthread_should_stop()) {
668+
/* To avoid hung_task, wait in interruptible state. */
669+
wait_event_interruptible(kprobe_optimizer_wait,
670+
atomic_read(&optimizer_state) != OPTIMIZER_ST_IDLE ||
671+
kthread_should_stop());
672+
673+
if (kthread_should_stop())
674+
break;
675+
676+
/*
677+
* If it was a normal kick, wait for OPTIMIZE_DELAY.
678+
* This wait can be interrupted by a flush request.
679+
*/
680+
if (atomic_read(&optimizer_state) == 1)
681+
wait_event_interruptible_timeout(
682+
kprobe_optimizer_wait,
683+
atomic_read(&optimizer_state) == OPTIMIZER_ST_FLUSHING ||
684+
kthread_should_stop(),
685+
OPTIMIZE_DELAY);
686+
687+
if (kthread_should_stop())
688+
break;
689+
690+
atomic_set(&optimizer_state, OPTIMIZER_ST_IDLE);
691+
692+
kprobe_optimizer();
693+
}
694+
return 0;
695+
}
696+
697+
/* Start optimizer after OPTIMIZE_DELAY passed */
698+
static void kick_kprobe_optimizer(void)
699+
{
700+
lockdep_assert_held(&kprobe_mutex);
701+
if (atomic_cmpxchg(&optimizer_state,
702+
OPTIMIZER_ST_IDLE, OPTIMIZER_ST_KICKED) == OPTIMIZER_ST_IDLE)
703+
wake_up(&kprobe_optimizer_wait);
653704
}
654705

655706
static void wait_for_kprobe_optimizer_locked(void)
656707
{
657708
lockdep_assert_held(&kprobe_mutex);
658709

659710
while (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list)) {
660-
mutex_unlock(&kprobe_mutex);
661-
662-
/* This will also make 'optimizing_work' execute immmediately */
663-
flush_delayed_work(&optimizing_work);
664-
/* 'optimizing_work' might not have been queued yet, relax */
665-
cpu_relax();
711+
init_completion(&optimizer_completion);
712+
/*
713+
* Set state to OPTIMIZER_ST_FLUSHING and wake up the thread if it's
714+
* idle. If it's already kicked, it will see the state change.
715+
*/
716+
if (atomic_xchg_acquire(&optimizer_state,
717+
OPTIMIZER_ST_FLUSHING) != OPTIMIZER_ST_FLUSHING)
718+
wake_up(&kprobe_optimizer_wait);
666719

720+
mutex_unlock(&kprobe_mutex);
721+
wait_for_completion(&optimizer_completion);
667722
mutex_lock(&kprobe_mutex);
668723
}
669724
}
@@ -1016,8 +1071,21 @@ static void __disarm_kprobe(struct kprobe *p, bool reopt)
10161071
}
10171072
}
10181073

1074+
static void __init init_optprobe(void)
1075+
{
1076+
#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
1077+
/* Init 'kprobe_optinsn_slots' for allocation */
1078+
kprobe_optinsn_slots.insn_size = MAX_OPTINSN_SIZE;
1079+
#endif
1080+
1081+
init_waitqueue_head(&kprobe_optimizer_wait);
1082+
atomic_set(&optimizer_state, OPTIMIZER_ST_IDLE);
1083+
kprobe_optimizer_task = kthread_run(kprobe_optimizer_thread, NULL,
1084+
"kprobe-optimizer");
1085+
}
10191086
#else /* !CONFIG_OPTPROBES */
10201087

1088+
#define init_optprobe() do {} while (0)
10211089
#define optimize_kprobe(p) do {} while (0)
10221090
#define unoptimize_kprobe(p, f) do {} while (0)
10231091
#define kill_optimized_kprobe(p) do {} while (0)
@@ -2700,10 +2768,8 @@ static int __init init_kprobes(void)
27002768
/* By default, kprobes are armed */
27012769
kprobes_all_disarmed = false;
27022770

2703-
#if defined(CONFIG_OPTPROBES) && defined(__ARCH_WANT_KPROBES_INSN_SLOT)
2704-
/* Init 'kprobe_optinsn_slots' for allocation */
2705-
kprobe_optinsn_slots.insn_size = MAX_OPTINSN_SIZE;
2706-
#endif
2771+
/* Initialize the optimization infrastructure */
2772+
init_optprobe();
27072773

27082774
err = arch_init_kprobes();
27092775
if (!err)

0 commit comments

Comments
 (0)