Skip to content

Commit 2d10a48

Browse files
committed
Merge tag 'probes-v7.0' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace
Pull kprobes updates from Masami Hiramatsu: - Use a dedicated kernel thread to optimize the kprobes instead of using workqueue thread. Since the kprobe optimizer waits a long time for synchronize_rcu_task(), it can block other workers in the same queue if it uses a workqueue. - kprobe-events: return immediately if no new probe events are specified on the kernel command line at boot time. This shortens the kernel boot time. - When a kprobe is fully removed from the kernel code, retry optimizing another kprobe which is blocked by that kprobe. * tag 'probes-v7.0' of git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace: kprobes: Use dedicated kthread for kprobe optimizer tracing: kprobe-event: Return directly when trace kprobes is empty kprobes: retry blocked optprobe in do_free_cleaned_kprobes
2 parents 0f2acd3 + 73c12f2 commit 2d10a48

2 files changed

Lines changed: 102 additions & 26 deletions

File tree

kernel/kprobes.c

Lines changed: 98 additions & 26 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,8 +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);
519+
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+
519531
#define OPTIMIZE_DELAY 5
520532

521533
/*
@@ -593,18 +605,25 @@ static void do_free_cleaned_kprobes(void)
593605
*/
594606
continue;
595607
}
608+
609+
/*
610+
* The aggregator was holding back another probe while it sat on the
611+
* unoptimizing/freeing lists. Now that the aggregator has been fully
612+
* reverted we can safely retry the optimization of that sibling.
613+
*/
614+
615+
struct kprobe *_p = get_optimized_kprobe(op->kp.addr);
616+
if (unlikely(_p))
617+
optimize_kprobe(_p);
618+
596619
free_aggr_kprobe(&op->kp);
597620
}
598621
}
599622

600-
/* Start optimizer after OPTIMIZE_DELAY passed */
601-
static void kick_kprobe_optimizer(void)
602-
{
603-
schedule_delayed_work(&optimizing_work, OPTIMIZE_DELAY);
604-
}
623+
static void kick_kprobe_optimizer(void);
605624

606625
/* Kprobe jump optimizer */
607-
static void kprobe_optimizer(struct work_struct *work)
626+
static void kprobe_optimizer(void)
608627
{
609628
guard(mutex)(&kprobe_mutex);
610629

@@ -635,23 +654,71 @@ static void kprobe_optimizer(struct work_struct *work)
635654
do_free_cleaned_kprobes();
636655
}
637656

638-
/* 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+
639661
if (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list))
640-
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);
641704
}
642705

643706
static void wait_for_kprobe_optimizer_locked(void)
644707
{
645708
lockdep_assert_held(&kprobe_mutex);
646709

647710
while (!list_empty(&optimizing_list) || !list_empty(&unoptimizing_list)) {
648-
mutex_unlock(&kprobe_mutex);
649-
650-
/* This will also make 'optimizing_work' execute immmediately */
651-
flush_delayed_work(&optimizing_work);
652-
/* 'optimizing_work' might not have been queued yet, relax */
653-
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);
654719

720+
mutex_unlock(&kprobe_mutex);
721+
wait_for_completion(&optimizer_completion);
655722
mutex_lock(&kprobe_mutex);
656723
}
657724
}
@@ -1002,16 +1069,23 @@ static void __disarm_kprobe(struct kprobe *p, bool reopt)
10021069
if (unlikely(_p) && reopt)
10031070
optimize_kprobe(_p);
10041071
}
1005-
/*
1006-
* TODO: Since unoptimization and real disarming will be done by
1007-
* the worker thread, we can not check whether another probe are
1008-
* unoptimized because of this probe here. It should be re-optimized
1009-
* by the worker thread.
1010-
*/
10111072
}
10121073

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+
}
10131086
#else /* !CONFIG_OPTPROBES */
10141087

1088+
#define init_optprobe() do {} while (0)
10151089
#define optimize_kprobe(p) do {} while (0)
10161090
#define unoptimize_kprobe(p, f) do {} while (0)
10171091
#define kill_optimized_kprobe(p) do {} while (0)
@@ -2694,10 +2768,8 @@ static int __init init_kprobes(void)
26942768
/* By default, kprobes are armed */
26952769
kprobes_all_disarmed = false;
26962770

2697-
#if defined(CONFIG_OPTPROBES) && defined(__ARCH_WANT_KPROBES_INSN_SLOT)
2698-
/* Init 'kprobe_optinsn_slots' for allocation */
2699-
kprobe_optinsn_slots.insn_size = MAX_OPTINSN_SIZE;
2700-
#endif
2771+
/* Initialize the optimization infrastructure */
2772+
init_optprobe();
27012773

27022774
err = arch_init_kprobes();
27032775
if (!err)

kernel/trace/trace_kprobe.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ static struct trace_kprobe *to_trace_kprobe(struct dyn_event *ev)
8282
#define for_each_trace_kprobe(pos, dpos) \
8383
for_each_dyn_event(dpos) \
8484
if (is_trace_kprobe(dpos) && (pos = to_trace_kprobe(dpos)))
85+
#define trace_kprobe_list_empty() list_empty(&dyn_event_list)
8586

8687
static nokprobe_inline bool trace_kprobe_is_return(struct trace_kprobe *tk)
8788
{
@@ -1982,6 +1983,9 @@ static __init void enable_boot_kprobe_events(void)
19821983
struct trace_kprobe *tk;
19831984
struct dyn_event *pos;
19841985

1986+
if (trace_kprobe_list_empty())
1987+
return;
1988+
19851989
guard(mutex)(&event_mutex);
19861990
for_each_trace_kprobe(tk, pos) {
19871991
list_for_each_entry(file, &tr->events, list)

0 commit comments

Comments
 (0)