Skip to content

Commit d002b8b

Browse files
ahunter6Peter Zijlstra
authored andcommitted
kprobes: Add symbols for kprobe insn pages
Symbols are needed for tools to describe instruction addresses. Pages allocated for kprobe's purposes need symbols to be created for them. Add such symbols to be visible via /proc/kallsyms. Note: kprobe insn pages are not used if ftrace is configured. To see the effect of this patch, the kernel must be configured with: # CONFIG_FUNCTION_TRACER is not set CONFIG_KPROBES=y and for optimised kprobes: CONFIG_OPTPROBES=y Example on x86: # perf probe __schedule Added new event: probe:__schedule (on __schedule) # cat /proc/kallsyms | grep '\[__builtin__kprobes\]' ffffffffc00d4000 t kprobe_insn_page [__builtin__kprobes] ffffffffc00d6000 t kprobe_optinsn_page [__builtin__kprobes] Note: This patch adds "__builtin__kprobes" as a module name in /proc/kallsyms for symbols for pages allocated for kprobes' purposes, even though "__builtin__kprobes" is not a module. Signed-off-by: Adrian Hunter <adrian.hunter@intel.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Acked-by: Masami Hiramatsu <mhiramat@kernel.org> Link: https://lkml.kernel.org/r/20200528080058.20230-1-adrian.hunter@intel.com
1 parent d769811 commit d002b8b

3 files changed

Lines changed: 93 additions & 4 deletions

File tree

‎include/linux/kprobes.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ struct kprobe_insn_cache {
242242
struct mutex mutex;
243243
void *(*alloc)(void); /* allocate insn page */
244244
void (*free)(void *); /* free insn page */
245+
const char *sym; /* symbol for insn pages */
245246
struct list_head pages; /* list of kprobe_insn_page */
246247
size_t insn_size; /* size of instruction slot */
247248
int nr_garbage;
@@ -272,6 +273,10 @@ static inline bool is_kprobe_##__name##_slot(unsigned long addr) \
272273
{ \
273274
return __is_insn_slot_addr(&kprobe_##__name##_slots, addr); \
274275
}
276+
#define KPROBE_INSN_PAGE_SYM "kprobe_insn_page"
277+
#define KPROBE_OPTINSN_PAGE_SYM "kprobe_optinsn_page"
278+
int kprobe_cache_get_kallsym(struct kprobe_insn_cache *c, unsigned int *symnum,
279+
unsigned long *value, char *type, char *sym);
275280
#else /* __ARCH_WANT_KPROBES_INSN_SLOT */
276281
#define DEFINE_INSN_CACHE_OPS(__name) \
277282
static inline bool is_kprobe_##__name##_slot(unsigned long addr) \
@@ -373,6 +378,11 @@ void dump_kprobe(struct kprobe *kp);
373378
void *alloc_insn_page(void);
374379
void free_insn_page(void *page);
375380

381+
int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
382+
char *sym);
383+
384+
int arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
385+
char *type, char *sym);
376386
#else /* !CONFIG_KPROBES: */
377387

378388
static inline int kprobes_built_in(void)
@@ -435,6 +445,11 @@ static inline bool within_kprobe_blacklist(unsigned long addr)
435445
{
436446
return true;
437447
}
448+
static inline int kprobe_get_kallsym(unsigned int symnum, unsigned long *value,
449+
char *type, char *sym)
450+
{
451+
return -ERANGE;
452+
}
438453
#endif /* CONFIG_KPROBES */
439454
static inline int disable_kretprobe(struct kretprobe *rp)
440455
{

‎kernel/kallsyms.c‎

Lines changed: 33 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <linux/slab.h>
2525
#include <linux/filter.h>
2626
#include <linux/ftrace.h>
27+
#include <linux/kprobes.h>
2728
#include <linux/compiler.h>
2829

2930
/*
@@ -437,6 +438,7 @@ struct kallsym_iter {
437438
loff_t pos_arch_end;
438439
loff_t pos_mod_end;
439440
loff_t pos_ftrace_mod_end;
441+
loff_t pos_bpf_end;
440442
unsigned long value;
441443
unsigned int nameoff; /* If iterating in core kernel symbols. */
442444
char type;
@@ -496,11 +498,33 @@ static int get_ksymbol_ftrace_mod(struct kallsym_iter *iter)
496498

497499
static int get_ksymbol_bpf(struct kallsym_iter *iter)
498500
{
501+
int ret;
502+
499503
strlcpy(iter->module_name, "bpf", MODULE_NAME_LEN);
500504
iter->exported = 0;
501-
return bpf_get_kallsym(iter->pos - iter->pos_ftrace_mod_end,
502-
&iter->value, &iter->type,
503-
iter->name) < 0 ? 0 : 1;
505+
ret = bpf_get_kallsym(iter->pos - iter->pos_ftrace_mod_end,
506+
&iter->value, &iter->type,
507+
iter->name);
508+
if (ret < 0) {
509+
iter->pos_bpf_end = iter->pos;
510+
return 0;
511+
}
512+
513+
return 1;
514+
}
515+
516+
/*
517+
* This uses "__builtin__kprobes" as a module name for symbols for pages
518+
* allocated for kprobes' purposes, even though "__builtin__kprobes" is not a
519+
* module.
520+
*/
521+
static int get_ksymbol_kprobe(struct kallsym_iter *iter)
522+
{
523+
strlcpy(iter->module_name, "__builtin__kprobes", MODULE_NAME_LEN);
524+
iter->exported = 0;
525+
return kprobe_get_kallsym(iter->pos - iter->pos_bpf_end,
526+
&iter->value, &iter->type,
527+
iter->name) < 0 ? 0 : 1;
504528
}
505529

506530
/* Returns space to next name. */
@@ -527,6 +551,7 @@ static void reset_iter(struct kallsym_iter *iter, loff_t new_pos)
527551
iter->pos_arch_end = 0;
528552
iter->pos_mod_end = 0;
529553
iter->pos_ftrace_mod_end = 0;
554+
iter->pos_bpf_end = 0;
530555
}
531556
}
532557

@@ -551,7 +576,11 @@ static int update_iter_mod(struct kallsym_iter *iter, loff_t pos)
551576
get_ksymbol_ftrace_mod(iter))
552577
return 1;
553578

554-
return get_ksymbol_bpf(iter);
579+
if ((!iter->pos_bpf_end || iter->pos_bpf_end > pos) &&
580+
get_ksymbol_bpf(iter))
581+
return 1;
582+
583+
return get_ksymbol_kprobe(iter);
555584
}
556585

557586
/* Returns false if pos at or past end of file. */

‎kernel/kprobes.c‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ struct kprobe_insn_cache kprobe_insn_slots = {
118118
.mutex = __MUTEX_INITIALIZER(kprobe_insn_slots.mutex),
119119
.alloc = alloc_insn_page,
120120
.free = free_insn_page,
121+
.sym = KPROBE_INSN_PAGE_SYM,
121122
.pages = LIST_HEAD_INIT(kprobe_insn_slots.pages),
122123
.insn_size = MAX_INSN_SIZE,
123124
.nr_garbage = 0,
@@ -290,12 +291,34 @@ bool __is_insn_slot_addr(struct kprobe_insn_cache *c, unsigned long addr)
290291
return ret;
291292
}
292293

294+
int kprobe_cache_get_kallsym(struct kprobe_insn_cache *c, unsigned int *symnum,
295+
unsigned long *value, char *type, char *sym)
296+
{
297+
struct kprobe_insn_page *kip;
298+
int ret = -ERANGE;
299+
300+
rcu_read_lock();
301+
list_for_each_entry_rcu(kip, &c->pages, list) {
302+
if ((*symnum)--)
303+
continue;
304+
strlcpy(sym, c->sym, KSYM_NAME_LEN);
305+
*type = 't';
306+
*value = (unsigned long)kip->insns;
307+
ret = 0;
308+
break;
309+
}
310+
rcu_read_unlock();
311+
312+
return ret;
313+
}
314+
293315
#ifdef CONFIG_OPTPROBES
294316
/* For optimized_kprobe buffer */
295317
struct kprobe_insn_cache kprobe_optinsn_slots = {
296318
.mutex = __MUTEX_INITIALIZER(kprobe_optinsn_slots.mutex),
297319
.alloc = alloc_insn_page,
298320
.free = free_insn_page,
321+
.sym = KPROBE_OPTINSN_PAGE_SYM,
299322
.pages = LIST_HEAD_INIT(kprobe_optinsn_slots.pages),
300323
/* .insn_size is initialized later */
301324
.nr_garbage = 0,
@@ -2197,6 +2220,28 @@ static void kprobe_remove_ksym_blacklist(unsigned long entry)
21972220
kprobe_remove_area_blacklist(entry, entry + 1);
21982221
}
21992222

2223+
int __weak arch_kprobe_get_kallsym(unsigned int *symnum, unsigned long *value,
2224+
char *type, char *sym)
2225+
{
2226+
return -ERANGE;
2227+
}
2228+
2229+
int kprobe_get_kallsym(unsigned int symnum, unsigned long *value, char *type,
2230+
char *sym)
2231+
{
2232+
#ifdef __ARCH_WANT_KPROBES_INSN_SLOT
2233+
if (!kprobe_cache_get_kallsym(&kprobe_insn_slots, &symnum, value, type, sym))
2234+
return 0;
2235+
#ifdef CONFIG_OPTPROBES
2236+
if (!kprobe_cache_get_kallsym(&kprobe_optinsn_slots, &symnum, value, type, sym))
2237+
return 0;
2238+
#endif
2239+
#endif
2240+
if (!arch_kprobe_get_kallsym(&symnum, value, type, sym))
2241+
return 0;
2242+
return -ERANGE;
2243+
}
2244+
22002245
int __init __weak arch_populate_kprobe_blacklist(void)
22012246
{
22022247
return 0;

0 commit comments

Comments
 (0)