Skip to content

Commit b2be84d

Browse files
Masami HiramatsuIngo Molnar
authored andcommitted
kprobes: Jump optimization sysctl interface
Add /proc/sys/debug/kprobes-optimization sysctl which enables and disables kprobes jump optimization on the fly for debugging. Changes in v7: - Remove ctl_name = CTL_UNNUMBERED for upstream compatibility. Changes in v6: - Update comments and coding style. Signed-off-by: Masami Hiramatsu <mhiramat@redhat.com> Cc: systemtap <systemtap@sources.redhat.com> Cc: DLE <dle-develop@lists.sourceforge.net> Cc: Ananth N Mavinakayanahalli <ananth@in.ibm.com> Cc: Jim Keniston <jkenisto@us.ibm.com> Cc: Srikar Dronamraju <srikar@linux.vnet.ibm.com> Cc: Christoph Hellwig <hch@infradead.org> Cc: Steven Rostedt <rostedt@goodmis.org> Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Anders Kaseorg <andersk@ksplice.com> Cc: Tim Abbott <tabbott@ksplice.com> Cc: Andi Kleen <andi@firstfloor.org> Cc: Jason Baron <jbaron@redhat.com> Cc: Mathieu Desnoyers <compudj@krystal.dyndns.org> Cc: Frederic Weisbecker <fweisbec@gmail.com> Cc: Ananth N Mavinakayanahalli <ananth@in.ibm.com> LKML-Reference: <20100225133415.6725.8274.stgit@localhost6.localdomain6> Signed-off-by: Ingo Molnar <mingo@elte.hu>
1 parent afd6625 commit b2be84d

3 files changed

Lines changed: 105 additions & 3 deletions

File tree

‎include/linux/kprobes.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,14 @@ extern int arch_within_optimized_kprobe(struct optimized_kprobe *op,
283283
unsigned long addr);
284284

285285
extern void opt_pre_handler(struct kprobe *p, struct pt_regs *regs);
286+
287+
#ifdef CONFIG_SYSCTL
288+
extern int sysctl_kprobes_optimization;
289+
extern int proc_kprobes_optimization_handler(struct ctl_table *table,
290+
int write, void __user *buffer,
291+
size_t *length, loff_t *ppos);
292+
#endif
293+
286294
#endif /* CONFIG_OPTPROBES */
287295

288296
/* Get the kprobe at this addr (if any) - called with preemption disabled */

‎kernel/kprobes.c‎

Lines changed: 85 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include <linux/freezer.h>
4343
#include <linux/seq_file.h>
4444
#include <linux/debugfs.h>
45+
#include <linux/sysctl.h>
4546
#include <linux/kdebug.h>
4647
#include <linux/memory.h>
4748
#include <linux/ftrace.h>
@@ -360,6 +361,9 @@ static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
360361
}
361362

362363
#ifdef CONFIG_OPTPROBES
364+
/* NOTE: change this value only with kprobe_mutex held */
365+
static bool kprobes_allow_optimization;
366+
363367
/*
364368
* Call all pre_handler on the list, but ignores its return value.
365369
* This must be called from arch-dep optimized caller.
@@ -428,7 +432,7 @@ static __kprobes void kprobe_optimizer(struct work_struct *work)
428432
/* Lock modules while optimizing kprobes */
429433
mutex_lock(&module_mutex);
430434
mutex_lock(&kprobe_mutex);
431-
if (kprobes_all_disarmed)
435+
if (kprobes_all_disarmed || !kprobes_allow_optimization)
432436
goto end;
433437

434438
/*
@@ -471,7 +475,7 @@ static __kprobes void optimize_kprobe(struct kprobe *p)
471475
struct optimized_kprobe *op;
472476

473477
/* Check if the kprobe is disabled or not ready for optimization. */
474-
if (!kprobe_optready(p) ||
478+
if (!kprobe_optready(p) || !kprobes_allow_optimization ||
475479
(kprobe_disabled(p) || kprobes_all_disarmed))
476480
return;
477481

@@ -588,6 +592,80 @@ static __kprobes void try_to_optimize_kprobe(struct kprobe *p)
588592
optimize_kprobe(ap);
589593
}
590594

595+
#ifdef CONFIG_SYSCTL
596+
static void __kprobes optimize_all_kprobes(void)
597+
{
598+
struct hlist_head *head;
599+
struct hlist_node *node;
600+
struct kprobe *p;
601+
unsigned int i;
602+
603+
/* If optimization is already allowed, just return */
604+
if (kprobes_allow_optimization)
605+
return;
606+
607+
kprobes_allow_optimization = true;
608+
mutex_lock(&text_mutex);
609+
for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
610+
head = &kprobe_table[i];
611+
hlist_for_each_entry_rcu(p, node, head, hlist)
612+
if (!kprobe_disabled(p))
613+
optimize_kprobe(p);
614+
}
615+
mutex_unlock(&text_mutex);
616+
printk(KERN_INFO "Kprobes globally optimized\n");
617+
}
618+
619+
static void __kprobes unoptimize_all_kprobes(void)
620+
{
621+
struct hlist_head *head;
622+
struct hlist_node *node;
623+
struct kprobe *p;
624+
unsigned int i;
625+
626+
/* If optimization is already prohibited, just return */
627+
if (!kprobes_allow_optimization)
628+
return;
629+
630+
kprobes_allow_optimization = false;
631+
printk(KERN_INFO "Kprobes globally unoptimized\n");
632+
get_online_cpus(); /* For avoiding text_mutex deadlock */
633+
mutex_lock(&text_mutex);
634+
for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
635+
head = &kprobe_table[i];
636+
hlist_for_each_entry_rcu(p, node, head, hlist) {
637+
if (!kprobe_disabled(p))
638+
unoptimize_kprobe(p);
639+
}
640+
}
641+
642+
mutex_unlock(&text_mutex);
643+
put_online_cpus();
644+
/* Allow all currently running kprobes to complete */
645+
synchronize_sched();
646+
}
647+
648+
int sysctl_kprobes_optimization;
649+
int proc_kprobes_optimization_handler(struct ctl_table *table, int write,
650+
void __user *buffer, size_t *length,
651+
loff_t *ppos)
652+
{
653+
int ret;
654+
655+
mutex_lock(&kprobe_mutex);
656+
sysctl_kprobes_optimization = kprobes_allow_optimization ? 1 : 0;
657+
ret = proc_dointvec_minmax(table, write, buffer, length, ppos);
658+
659+
if (sysctl_kprobes_optimization)
660+
optimize_all_kprobes();
661+
else
662+
unoptimize_all_kprobes();
663+
mutex_unlock(&kprobe_mutex);
664+
665+
return ret;
666+
}
667+
#endif /* CONFIG_SYSCTL */
668+
591669
static void __kprobes __arm_kprobe(struct kprobe *p)
592670
{
593671
struct kprobe *old_p;
@@ -1610,10 +1688,14 @@ static int __init init_kprobes(void)
16101688
}
16111689
}
16121690

1613-
#if defined(CONFIG_OPTPROBES) && defined(__ARCH_WANT_KPROBES_INSN_SLOT)
1691+
#if defined(CONFIG_OPTPROBES)
1692+
#if defined(__ARCH_WANT_KPROBES_INSN_SLOT)
16141693
/* Init kprobe_optinsn_slots */
16151694
kprobe_optinsn_slots.insn_size = MAX_OPTINSN_SIZE;
16161695
#endif
1696+
/* By default, kprobes can be optimized */
1697+
kprobes_allow_optimization = true;
1698+
#endif
16171699

16181700
/* By default, kprobes are armed */
16191701
kprobes_all_disarmed = false;

‎kernel/sysctl.c‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include <linux/ftrace.h>
5151
#include <linux/slow-work.h>
5252
#include <linux/perf_event.h>
53+
#include <linux/kprobes.h>
5354

5455
#include <asm/uaccess.h>
5556
#include <asm/processor.h>
@@ -1449,6 +1450,17 @@ static struct ctl_table debug_table[] = {
14491450
.mode = 0644,
14501451
.proc_handler = proc_dointvec
14511452
},
1453+
#endif
1454+
#if defined(CONFIG_OPTPROBES)
1455+
{
1456+
.procname = "kprobes-optimization",
1457+
.data = &sysctl_kprobes_optimization,
1458+
.maxlen = sizeof(int),
1459+
.mode = 0644,
1460+
.proc_handler = proc_kprobes_optimization_handler,
1461+
.extra1 = &zero,
1462+
.extra2 = &one,
1463+
},
14521464
#endif
14531465
{ }
14541466
};

0 commit comments

Comments
 (0)