Skip to content

Commit

Permalink
[PATCH] kprobes: fix unloading of self probed module
Browse files Browse the repository at this point in the history
When a kprobes modules is written in such a way that probes are inserted on
itself, then unload of that moudle was not possible due to reference
couning on the same module.

The below patch makes a check and incrementes the module refcount only if
it is not a self probed module.

We need to allow modules to probe themself for kprobes performance
measurements

This patch has been tested on several x86_64, ppc64 and IA64 architectures.

Signed-off-by: Anil S Keshavamurthy <anil.s.keshavamurthy>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
  • Loading branch information
anilskeshavamurthy authored and Linus Torvalds committed Jan 12, 2006
1 parent ef43bc4 commit df019b1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 10 deletions.
3 changes: 3 additions & 0 deletions include/linux/kprobes.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ struct kprobe {
/* list of kprobes for multi-handler support */
struct list_head list;

/* Indicates that the corresponding module has been ref counted */
unsigned int mod_refcounted;

/*count the number of times this probe was temporarily disarmed */
unsigned long nmissed;

Expand Down
42 changes: 32 additions & 10 deletions kernel/kprobes.c
Original file line number Diff line number Diff line change
Expand Up @@ -449,19 +449,32 @@ static int __kprobes in_kprobes_functions(unsigned long addr)
return 0;
}

int __kprobes register_kprobe(struct kprobe *p)
static int __kprobes __register_kprobe(struct kprobe *p,
unsigned long called_from)
{
int ret = 0;
struct kprobe *old_p;
struct module *mod;
struct module *probed_mod;

if ((!kernel_text_address((unsigned long) p->addr)) ||
in_kprobes_functions((unsigned long) p->addr))
return -EINVAL;

if ((mod = module_text_address((unsigned long) p->addr)) &&
(unlikely(!try_module_get(mod))))
return -EINVAL;
p->mod_refcounted = 0;
/* Check are we probing a module */
if ((probed_mod = module_text_address((unsigned long) p->addr))) {
struct module *calling_mod = module_text_address(called_from);
/* We must allow modules to probe themself and
* in this case avoid incrementing the module refcount,
* so as to allow unloading of self probing modules.
*/
if (calling_mod && (calling_mod != probed_mod)) {
if (unlikely(!try_module_get(probed_mod)))
return -EINVAL;
p->mod_refcounted = 1;
} else
probed_mod = NULL;
}

p->nmissed = 0;
down(&kprobe_mutex);
Expand All @@ -483,11 +496,17 @@ int __kprobes register_kprobe(struct kprobe *p)
out:
up(&kprobe_mutex);

if (ret && mod)
module_put(mod);
if (ret && probed_mod)
module_put(probed_mod);
return ret;
}

int __kprobes register_kprobe(struct kprobe *p)
{
return __register_kprobe(p,
(unsigned long)__builtin_return_address(0));
}

void __kprobes unregister_kprobe(struct kprobe *p)
{
struct module *mod;
Expand Down Expand Up @@ -524,7 +543,8 @@ void __kprobes unregister_kprobe(struct kprobe *p)
up(&kprobe_mutex);

synchronize_sched();
if ((mod = module_text_address((unsigned long)p->addr)))
if (p->mod_refcounted &&
(mod = module_text_address((unsigned long)p->addr)))
module_put(mod);

if (cleanup_p) {
Expand All @@ -547,7 +567,8 @@ int __kprobes register_jprobe(struct jprobe *jp)
jp->kp.pre_handler = setjmp_pre_handler;
jp->kp.break_handler = longjmp_break_handler;

return register_kprobe(&jp->kp);
return __register_kprobe(&jp->kp,
(unsigned long)__builtin_return_address(0));
}

void __kprobes unregister_jprobe(struct jprobe *jp)
Expand Down Expand Up @@ -587,7 +608,8 @@ int __kprobes register_kretprobe(struct kretprobe *rp)

rp->nmissed = 0;
/* Establish function entry probe point */
if ((ret = register_kprobe(&rp->kp)) != 0)
if ((ret = __register_kprobe(&rp->kp,
(unsigned long)__builtin_return_address(0))) != 0)
free_rp_inst(rp);
return ret;
}
Expand Down

0 comments on commit df019b1

Please sign in to comment.