[PATCH] Kprobes: prevent possible race conditions generic
[deliverable/linux.git] / kernel / kprobes.c
1 /*
2 * Kernel Probes (KProbes)
3 * kernel/kprobes.c
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 *
19 * Copyright (C) IBM Corporation, 2002, 2004
20 *
21 * 2002-Oct Created by Vamsi Krishna S <vamsi_krishna@in.ibm.com> Kernel
22 * Probes initial implementation (includes suggestions from
23 * Rusty Russell).
24 * 2004-Aug Updated by Prasanna S Panchamukhi <prasanna@in.ibm.com> with
25 * hlists and exceptions notifier as suggested by Andi Kleen.
26 * 2004-July Suparna Bhattacharya <suparna@in.ibm.com> added jumper probes
27 * interface to access function arguments.
28 * 2004-Sep Prasanna S Panchamukhi <prasanna@in.ibm.com> Changed Kprobes
29 * exceptions notifier to be first on the priority list.
30 * 2005-May Hien Nguyen <hien@us.ibm.com>, Jim Keniston
31 * <jkenisto@us.ibm.com> and Prasanna S Panchamukhi
32 * <prasanna@in.ibm.com> added function-return probes.
33 */
34 #include <linux/kprobes.h>
35 #include <linux/spinlock.h>
36 #include <linux/hash.h>
37 #include <linux/init.h>
38 #include <linux/module.h>
39 #include <linux/moduleloader.h>
40 #include <asm-generic/sections.h>
41 #include <asm/cacheflush.h>
42 #include <asm/errno.h>
43 #include <asm/kdebug.h>
44
45 #define KPROBE_HASH_BITS 6
46 #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
47
48 static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
49 static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
50
51 unsigned int kprobe_cpu = NR_CPUS;
52 static DEFINE_SPINLOCK(kprobe_lock);
53 static struct kprobe *curr_kprobe;
54
55 /*
56 * kprobe->ainsn.insn points to the copy of the instruction to be
57 * single-stepped. x86_64, POWER4 and above have no-exec support and
58 * stepping on the instruction on a vmalloced/kmalloced/data page
59 * is a recipe for disaster
60 */
61 #define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
62
63 struct kprobe_insn_page {
64 struct hlist_node hlist;
65 kprobe_opcode_t *insns; /* Page of instruction slots */
66 char slot_used[INSNS_PER_PAGE];
67 int nused;
68 };
69
70 static struct hlist_head kprobe_insn_pages;
71
72 /**
73 * get_insn_slot() - Find a slot on an executable page for an instruction.
74 * We allocate an executable page if there's no room on existing ones.
75 */
76 kprobe_opcode_t __kprobes *get_insn_slot(void)
77 {
78 struct kprobe_insn_page *kip;
79 struct hlist_node *pos;
80
81 hlist_for_each(pos, &kprobe_insn_pages) {
82 kip = hlist_entry(pos, struct kprobe_insn_page, hlist);
83 if (kip->nused < INSNS_PER_PAGE) {
84 int i;
85 for (i = 0; i < INSNS_PER_PAGE; i++) {
86 if (!kip->slot_used[i]) {
87 kip->slot_used[i] = 1;
88 kip->nused++;
89 return kip->insns + (i * MAX_INSN_SIZE);
90 }
91 }
92 /* Surprise! No unused slots. Fix kip->nused. */
93 kip->nused = INSNS_PER_PAGE;
94 }
95 }
96
97 /* All out of space. Need to allocate a new page. Use slot 0.*/
98 kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
99 if (!kip) {
100 return NULL;
101 }
102
103 /*
104 * Use module_alloc so this page is within +/- 2GB of where the
105 * kernel image and loaded module images reside. This is required
106 * so x86_64 can correctly handle the %rip-relative fixups.
107 */
108 kip->insns = module_alloc(PAGE_SIZE);
109 if (!kip->insns) {
110 kfree(kip);
111 return NULL;
112 }
113 INIT_HLIST_NODE(&kip->hlist);
114 hlist_add_head(&kip->hlist, &kprobe_insn_pages);
115 memset(kip->slot_used, 0, INSNS_PER_PAGE);
116 kip->slot_used[0] = 1;
117 kip->nused = 1;
118 return kip->insns;
119 }
120
121 void __kprobes free_insn_slot(kprobe_opcode_t *slot)
122 {
123 struct kprobe_insn_page *kip;
124 struct hlist_node *pos;
125
126 hlist_for_each(pos, &kprobe_insn_pages) {
127 kip = hlist_entry(pos, struct kprobe_insn_page, hlist);
128 if (kip->insns <= slot &&
129 slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) {
130 int i = (slot - kip->insns) / MAX_INSN_SIZE;
131 kip->slot_used[i] = 0;
132 kip->nused--;
133 if (kip->nused == 0) {
134 /*
135 * Page is no longer in use. Free it unless
136 * it's the last one. We keep the last one
137 * so as not to have to set it up again the
138 * next time somebody inserts a probe.
139 */
140 hlist_del(&kip->hlist);
141 if (hlist_empty(&kprobe_insn_pages)) {
142 INIT_HLIST_NODE(&kip->hlist);
143 hlist_add_head(&kip->hlist,
144 &kprobe_insn_pages);
145 } else {
146 module_free(NULL, kip->insns);
147 kfree(kip);
148 }
149 }
150 return;
151 }
152 }
153 }
154
155 /* Locks kprobe: irqs must be disabled */
156 void __kprobes lock_kprobes(void)
157 {
158 spin_lock(&kprobe_lock);
159 kprobe_cpu = smp_processor_id();
160 }
161
162 void __kprobes unlock_kprobes(void)
163 {
164 kprobe_cpu = NR_CPUS;
165 spin_unlock(&kprobe_lock);
166 }
167
168 /* You have to be holding the kprobe_lock */
169 struct kprobe __kprobes *get_kprobe(void *addr)
170 {
171 struct hlist_head *head;
172 struct hlist_node *node;
173
174 head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
175 hlist_for_each(node, head) {
176 struct kprobe *p = hlist_entry(node, struct kprobe, hlist);
177 if (p->addr == addr)
178 return p;
179 }
180 return NULL;
181 }
182
183 /*
184 * Aggregate handlers for multiple kprobes support - these handlers
185 * take care of invoking the individual kprobe handlers on p->list
186 */
187 static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
188 {
189 struct kprobe *kp;
190
191 list_for_each_entry(kp, &p->list, list) {
192 if (kp->pre_handler) {
193 curr_kprobe = kp;
194 if (kp->pre_handler(kp, regs))
195 return 1;
196 }
197 curr_kprobe = NULL;
198 }
199 return 0;
200 }
201
202 static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
203 unsigned long flags)
204 {
205 struct kprobe *kp;
206
207 list_for_each_entry(kp, &p->list, list) {
208 if (kp->post_handler) {
209 curr_kprobe = kp;
210 kp->post_handler(kp, regs, flags);
211 curr_kprobe = NULL;
212 }
213 }
214 return;
215 }
216
217 static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
218 int trapnr)
219 {
220 /*
221 * if we faulted "during" the execution of a user specified
222 * probe handler, invoke just that probe's fault handler
223 */
224 if (curr_kprobe && curr_kprobe->fault_handler) {
225 if (curr_kprobe->fault_handler(curr_kprobe, regs, trapnr))
226 return 1;
227 }
228 return 0;
229 }
230
231 static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
232 {
233 struct kprobe *kp = curr_kprobe;
234 if (curr_kprobe && kp->break_handler) {
235 if (kp->break_handler(kp, regs)) {
236 curr_kprobe = NULL;
237 return 1;
238 }
239 }
240 curr_kprobe = NULL;
241 return 0;
242 }
243
244 struct kretprobe_instance __kprobes *get_free_rp_inst(struct kretprobe *rp)
245 {
246 struct hlist_node *node;
247 struct kretprobe_instance *ri;
248 hlist_for_each_entry(ri, node, &rp->free_instances, uflist)
249 return ri;
250 return NULL;
251 }
252
253 static struct kretprobe_instance __kprobes *get_used_rp_inst(struct kretprobe
254 *rp)
255 {
256 struct hlist_node *node;
257 struct kretprobe_instance *ri;
258 hlist_for_each_entry(ri, node, &rp->used_instances, uflist)
259 return ri;
260 return NULL;
261 }
262
263 void __kprobes add_rp_inst(struct kretprobe_instance *ri)
264 {
265 /*
266 * Remove rp inst off the free list -
267 * Add it back when probed function returns
268 */
269 hlist_del(&ri->uflist);
270
271 /* Add rp inst onto table */
272 INIT_HLIST_NODE(&ri->hlist);
273 hlist_add_head(&ri->hlist,
274 &kretprobe_inst_table[hash_ptr(ri->task, KPROBE_HASH_BITS)]);
275
276 /* Also add this rp inst to the used list. */
277 INIT_HLIST_NODE(&ri->uflist);
278 hlist_add_head(&ri->uflist, &ri->rp->used_instances);
279 }
280
281 void __kprobes recycle_rp_inst(struct kretprobe_instance *ri)
282 {
283 /* remove rp inst off the rprobe_inst_table */
284 hlist_del(&ri->hlist);
285 if (ri->rp) {
286 /* remove rp inst off the used list */
287 hlist_del(&ri->uflist);
288 /* put rp inst back onto the free list */
289 INIT_HLIST_NODE(&ri->uflist);
290 hlist_add_head(&ri->uflist, &ri->rp->free_instances);
291 } else
292 /* Unregistering */
293 kfree(ri);
294 }
295
296 struct hlist_head __kprobes *kretprobe_inst_table_head(struct task_struct *tsk)
297 {
298 return &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)];
299 }
300
301 /*
302 * This function is called from exit_thread or flush_thread when task tk's
303 * stack is being recycled so that we can recycle any function-return probe
304 * instances associated with this task. These left over instances represent
305 * probed functions that have been called but will never return.
306 */
307 void __kprobes kprobe_flush_task(struct task_struct *tk)
308 {
309 struct kretprobe_instance *ri;
310 struct hlist_head *head;
311 struct hlist_node *node, *tmp;
312 unsigned long flags = 0;
313
314 spin_lock_irqsave(&kprobe_lock, flags);
315 head = kretprobe_inst_table_head(current);
316 hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
317 if (ri->task == tk)
318 recycle_rp_inst(ri);
319 }
320 spin_unlock_irqrestore(&kprobe_lock, flags);
321 }
322
323 /*
324 * This kprobe pre_handler is registered with every kretprobe. When probe
325 * hits it will set up the return probe.
326 */
327 static int __kprobes pre_handler_kretprobe(struct kprobe *p,
328 struct pt_regs *regs)
329 {
330 struct kretprobe *rp = container_of(p, struct kretprobe, kp);
331
332 /*TODO: consider to only swap the RA after the last pre_handler fired */
333 arch_prepare_kretprobe(rp, regs);
334 return 0;
335 }
336
337 static inline void free_rp_inst(struct kretprobe *rp)
338 {
339 struct kretprobe_instance *ri;
340 while ((ri = get_free_rp_inst(rp)) != NULL) {
341 hlist_del(&ri->uflist);
342 kfree(ri);
343 }
344 }
345
346 /*
347 * Keep all fields in the kprobe consistent
348 */
349 static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
350 {
351 memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
352 memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
353 }
354
355 /*
356 * Add the new probe to old_p->list. Fail if this is the
357 * second jprobe at the address - two jprobes can't coexist
358 */
359 static int __kprobes add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
360 {
361 struct kprobe *kp;
362
363 if (p->break_handler) {
364 list_for_each_entry(kp, &old_p->list, list) {
365 if (kp->break_handler)
366 return -EEXIST;
367 }
368 list_add_tail(&p->list, &old_p->list);
369 } else
370 list_add(&p->list, &old_p->list);
371 return 0;
372 }
373
374 /*
375 * Fill in the required fields of the "manager kprobe". Replace the
376 * earlier kprobe in the hlist with the manager kprobe
377 */
378 static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
379 {
380 copy_kprobe(p, ap);
381 ap->addr = p->addr;
382 ap->pre_handler = aggr_pre_handler;
383 ap->post_handler = aggr_post_handler;
384 ap->fault_handler = aggr_fault_handler;
385 ap->break_handler = aggr_break_handler;
386
387 INIT_LIST_HEAD(&ap->list);
388 list_add(&p->list, &ap->list);
389
390 INIT_HLIST_NODE(&ap->hlist);
391 hlist_del(&p->hlist);
392 hlist_add_head(&ap->hlist,
393 &kprobe_table[hash_ptr(ap->addr, KPROBE_HASH_BITS)]);
394 }
395
396 /*
397 * This is the second or subsequent kprobe at the address - handle
398 * the intricacies
399 * TODO: Move kcalloc outside the spinlock
400 */
401 static int __kprobes register_aggr_kprobe(struct kprobe *old_p,
402 struct kprobe *p)
403 {
404 int ret = 0;
405 struct kprobe *ap;
406
407 if (old_p->pre_handler == aggr_pre_handler) {
408 copy_kprobe(old_p, p);
409 ret = add_new_kprobe(old_p, p);
410 } else {
411 ap = kcalloc(1, sizeof(struct kprobe), GFP_ATOMIC);
412 if (!ap)
413 return -ENOMEM;
414 add_aggr_kprobe(ap, old_p);
415 copy_kprobe(ap, p);
416 ret = add_new_kprobe(ap, p);
417 }
418 return ret;
419 }
420
421 /* kprobe removal house-keeping routines */
422 static inline void cleanup_kprobe(struct kprobe *p, unsigned long flags)
423 {
424 arch_disarm_kprobe(p);
425 hlist_del(&p->hlist);
426 spin_unlock_irqrestore(&kprobe_lock, flags);
427 arch_remove_kprobe(p);
428 }
429
430 static inline void cleanup_aggr_kprobe(struct kprobe *old_p,
431 struct kprobe *p, unsigned long flags)
432 {
433 list_del(&p->list);
434 if (list_empty(&old_p->list)) {
435 cleanup_kprobe(old_p, flags);
436 kfree(old_p);
437 } else
438 spin_unlock_irqrestore(&kprobe_lock, flags);
439 }
440
441 static int __kprobes in_kprobes_functions(unsigned long addr)
442 {
443 if (addr >= (unsigned long)__kprobes_text_start
444 && addr < (unsigned long)__kprobes_text_end)
445 return -EINVAL;
446 return 0;
447 }
448
449 int __kprobes register_kprobe(struct kprobe *p)
450 {
451 int ret = 0;
452 unsigned long flags = 0;
453 struct kprobe *old_p;
454
455 if ((ret = in_kprobes_functions((unsigned long) p->addr)) != 0)
456 return ret;
457 if ((ret = arch_prepare_kprobe(p)) != 0)
458 goto rm_kprobe;
459
460 spin_lock_irqsave(&kprobe_lock, flags);
461 old_p = get_kprobe(p->addr);
462 p->nmissed = 0;
463 if (old_p) {
464 ret = register_aggr_kprobe(old_p, p);
465 goto out;
466 }
467
468 arch_copy_kprobe(p);
469 INIT_HLIST_NODE(&p->hlist);
470 hlist_add_head(&p->hlist,
471 &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
472
473 arch_arm_kprobe(p);
474
475 out:
476 spin_unlock_irqrestore(&kprobe_lock, flags);
477 rm_kprobe:
478 if (ret == -EEXIST)
479 arch_remove_kprobe(p);
480 return ret;
481 }
482
483 void __kprobes unregister_kprobe(struct kprobe *p)
484 {
485 unsigned long flags;
486 struct kprobe *old_p;
487
488 spin_lock_irqsave(&kprobe_lock, flags);
489 old_p = get_kprobe(p->addr);
490 if (old_p) {
491 if (old_p->pre_handler == aggr_pre_handler)
492 cleanup_aggr_kprobe(old_p, p, flags);
493 else
494 cleanup_kprobe(p, flags);
495 } else
496 spin_unlock_irqrestore(&kprobe_lock, flags);
497 }
498
499 static struct notifier_block kprobe_exceptions_nb = {
500 .notifier_call = kprobe_exceptions_notify,
501 .priority = 0x7fffffff /* we need to notified first */
502 };
503
504 int __kprobes register_jprobe(struct jprobe *jp)
505 {
506 /* Todo: Verify probepoint is a function entry point */
507 jp->kp.pre_handler = setjmp_pre_handler;
508 jp->kp.break_handler = longjmp_break_handler;
509
510 return register_kprobe(&jp->kp);
511 }
512
513 void __kprobes unregister_jprobe(struct jprobe *jp)
514 {
515 unregister_kprobe(&jp->kp);
516 }
517
518 #ifdef ARCH_SUPPORTS_KRETPROBES
519
520 int __kprobes register_kretprobe(struct kretprobe *rp)
521 {
522 int ret = 0;
523 struct kretprobe_instance *inst;
524 int i;
525
526 rp->kp.pre_handler = pre_handler_kretprobe;
527
528 /* Pre-allocate memory for max kretprobe instances */
529 if (rp->maxactive <= 0) {
530 #ifdef CONFIG_PREEMPT
531 rp->maxactive = max(10, 2 * NR_CPUS);
532 #else
533 rp->maxactive = NR_CPUS;
534 #endif
535 }
536 INIT_HLIST_HEAD(&rp->used_instances);
537 INIT_HLIST_HEAD(&rp->free_instances);
538 for (i = 0; i < rp->maxactive; i++) {
539 inst = kmalloc(sizeof(struct kretprobe_instance), GFP_KERNEL);
540 if (inst == NULL) {
541 free_rp_inst(rp);
542 return -ENOMEM;
543 }
544 INIT_HLIST_NODE(&inst->uflist);
545 hlist_add_head(&inst->uflist, &rp->free_instances);
546 }
547
548 rp->nmissed = 0;
549 /* Establish function entry probe point */
550 if ((ret = register_kprobe(&rp->kp)) != 0)
551 free_rp_inst(rp);
552 return ret;
553 }
554
555 #else /* ARCH_SUPPORTS_KRETPROBES */
556
557 int __kprobes register_kretprobe(struct kretprobe *rp)
558 {
559 return -ENOSYS;
560 }
561
562 #endif /* ARCH_SUPPORTS_KRETPROBES */
563
564 void __kprobes unregister_kretprobe(struct kretprobe *rp)
565 {
566 unsigned long flags;
567 struct kretprobe_instance *ri;
568
569 unregister_kprobe(&rp->kp);
570 /* No race here */
571 spin_lock_irqsave(&kprobe_lock, flags);
572 free_rp_inst(rp);
573 while ((ri = get_used_rp_inst(rp)) != NULL) {
574 ri->rp = NULL;
575 hlist_del(&ri->uflist);
576 }
577 spin_unlock_irqrestore(&kprobe_lock, flags);
578 }
579
580 static int __init init_kprobes(void)
581 {
582 int i, err = 0;
583
584 /* FIXME allocate the probe table, currently defined statically */
585 /* initialize all list heads */
586 for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
587 INIT_HLIST_HEAD(&kprobe_table[i]);
588 INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
589 }
590
591 err = arch_init_kprobes();
592 if (!err)
593 err = register_die_notifier(&kprobe_exceptions_nb);
594
595 return err;
596 }
597
598 __initcall(init_kprobes);
599
600 EXPORT_SYMBOL_GPL(register_kprobe);
601 EXPORT_SYMBOL_GPL(unregister_kprobe);
602 EXPORT_SYMBOL_GPL(register_jprobe);
603 EXPORT_SYMBOL_GPL(unregister_jprobe);
604 EXPORT_SYMBOL_GPL(jprobe_return);
605 EXPORT_SYMBOL_GPL(register_kretprobe);
606 EXPORT_SYMBOL_GPL(unregister_kretprobe);
607
This page took 0.043105 seconds and 5 git commands to generate.