kprobes.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917
  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/hash.h>
  36. #include <linux/init.h>
  37. #include <linux/slab.h>
  38. #include <linux/stddef.h>
  39. #include <linux/module.h>
  40. #include <linux/moduleloader.h>
  41. #include <linux/kallsyms.h>
  42. #include <linux/freezer.h>
  43. #include <linux/seq_file.h>
  44. #include <linux/debugfs.h>
  45. #include <linux/kdebug.h>
  46. #include <asm-generic/sections.h>
  47. #include <asm/cacheflush.h>
  48. #include <asm/errno.h>
  49. #define KPROBE_HASH_BITS 6
  50. #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
  51. /*
  52. * Some oddball architectures like 64bit powerpc have function descriptors
  53. * so this must be overridable.
  54. */
  55. #ifndef kprobe_lookup_name
  56. #define kprobe_lookup_name(name, addr) \
  57. addr = ((kprobe_opcode_t *)(kallsyms_lookup_name(name)))
  58. #endif
  59. static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
  60. static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
  61. static atomic_t kprobe_count;
  62. DEFINE_MUTEX(kprobe_mutex); /* Protects kprobe_table */
  63. DEFINE_SPINLOCK(kretprobe_lock); /* Protects kretprobe_inst_table */
  64. static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
  65. static struct notifier_block kprobe_page_fault_nb = {
  66. .notifier_call = kprobe_exceptions_notify,
  67. .priority = 0x7fffffff /* we need to notified first */
  68. };
  69. #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
  70. /*
  71. * kprobe->ainsn.insn points to the copy of the instruction to be
  72. * single-stepped. x86_64, POWER4 and above have no-exec support and
  73. * stepping on the instruction on a vmalloced/kmalloced/data page
  74. * is a recipe for disaster
  75. */
  76. #define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
  77. struct kprobe_insn_page {
  78. struct hlist_node hlist;
  79. kprobe_opcode_t *insns; /* Page of instruction slots */
  80. char slot_used[INSNS_PER_PAGE];
  81. int nused;
  82. int ngarbage;
  83. };
  84. enum kprobe_slot_state {
  85. SLOT_CLEAN = 0,
  86. SLOT_DIRTY = 1,
  87. SLOT_USED = 2,
  88. };
  89. static struct hlist_head kprobe_insn_pages;
  90. static int kprobe_garbage_slots;
  91. static int collect_garbage_slots(void);
  92. static int __kprobes check_safety(void)
  93. {
  94. int ret = 0;
  95. #if defined(CONFIG_PREEMPT) && defined(CONFIG_PM)
  96. ret = freeze_processes();
  97. if (ret == 0) {
  98. struct task_struct *p, *q;
  99. do_each_thread(p, q) {
  100. if (p != current && p->state == TASK_RUNNING &&
  101. p->pid != 0) {
  102. printk("Check failed: %s is running\n",p->comm);
  103. ret = -1;
  104. goto loop_end;
  105. }
  106. } while_each_thread(p, q);
  107. }
  108. loop_end:
  109. thaw_processes();
  110. #else
  111. synchronize_sched();
  112. #endif
  113. return ret;
  114. }
  115. /**
  116. * get_insn_slot() - Find a slot on an executable page for an instruction.
  117. * We allocate an executable page if there's no room on existing ones.
  118. */
  119. kprobe_opcode_t __kprobes *get_insn_slot(void)
  120. {
  121. struct kprobe_insn_page *kip;
  122. struct hlist_node *pos;
  123. retry:
  124. hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
  125. if (kip->nused < INSNS_PER_PAGE) {
  126. int i;
  127. for (i = 0; i < INSNS_PER_PAGE; i++) {
  128. if (kip->slot_used[i] == SLOT_CLEAN) {
  129. kip->slot_used[i] = SLOT_USED;
  130. kip->nused++;
  131. return kip->insns + (i * MAX_INSN_SIZE);
  132. }
  133. }
  134. /* Surprise! No unused slots. Fix kip->nused. */
  135. kip->nused = INSNS_PER_PAGE;
  136. }
  137. }
  138. /* If there are any garbage slots, collect it and try again. */
  139. if (kprobe_garbage_slots && collect_garbage_slots() == 0) {
  140. goto retry;
  141. }
  142. /* All out of space. Need to allocate a new page. Use slot 0. */
  143. kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
  144. if (!kip)
  145. return NULL;
  146. /*
  147. * Use module_alloc so this page is within +/- 2GB of where the
  148. * kernel image and loaded module images reside. This is required
  149. * so x86_64 can correctly handle the %rip-relative fixups.
  150. */
  151. kip->insns = module_alloc(PAGE_SIZE);
  152. if (!kip->insns) {
  153. kfree(kip);
  154. return NULL;
  155. }
  156. INIT_HLIST_NODE(&kip->hlist);
  157. hlist_add_head(&kip->hlist, &kprobe_insn_pages);
  158. memset(kip->slot_used, SLOT_CLEAN, INSNS_PER_PAGE);
  159. kip->slot_used[0] = SLOT_USED;
  160. kip->nused = 1;
  161. kip->ngarbage = 0;
  162. return kip->insns;
  163. }
  164. /* Return 1 if all garbages are collected, otherwise 0. */
  165. static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx)
  166. {
  167. kip->slot_used[idx] = SLOT_CLEAN;
  168. kip->nused--;
  169. if (kip->nused == 0) {
  170. /*
  171. * Page is no longer in use. Free it unless
  172. * it's the last one. We keep the last one
  173. * so as not to have to set it up again the
  174. * next time somebody inserts a probe.
  175. */
  176. hlist_del(&kip->hlist);
  177. if (hlist_empty(&kprobe_insn_pages)) {
  178. INIT_HLIST_NODE(&kip->hlist);
  179. hlist_add_head(&kip->hlist,
  180. &kprobe_insn_pages);
  181. } else {
  182. module_free(NULL, kip->insns);
  183. kfree(kip);
  184. }
  185. return 1;
  186. }
  187. return 0;
  188. }
  189. static int __kprobes collect_garbage_slots(void)
  190. {
  191. struct kprobe_insn_page *kip;
  192. struct hlist_node *pos, *next;
  193. /* Ensure no-one is preepmted on the garbages */
  194. if (check_safety() != 0)
  195. return -EAGAIN;
  196. hlist_for_each_entry_safe(kip, pos, next, &kprobe_insn_pages, hlist) {
  197. int i;
  198. if (kip->ngarbage == 0)
  199. continue;
  200. kip->ngarbage = 0; /* we will collect all garbages */
  201. for (i = 0; i < INSNS_PER_PAGE; i++) {
  202. if (kip->slot_used[i] == SLOT_DIRTY &&
  203. collect_one_slot(kip, i))
  204. break;
  205. }
  206. }
  207. kprobe_garbage_slots = 0;
  208. return 0;
  209. }
  210. void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty)
  211. {
  212. struct kprobe_insn_page *kip;
  213. struct hlist_node *pos;
  214. hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
  215. if (kip->insns <= slot &&
  216. slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) {
  217. int i = (slot - kip->insns) / MAX_INSN_SIZE;
  218. if (dirty) {
  219. kip->slot_used[i] = SLOT_DIRTY;
  220. kip->ngarbage++;
  221. } else {
  222. collect_one_slot(kip, i);
  223. }
  224. break;
  225. }
  226. }
  227. if (dirty && ++kprobe_garbage_slots > INSNS_PER_PAGE)
  228. collect_garbage_slots();
  229. }
  230. #endif
  231. /* We have preemption disabled.. so it is safe to use __ versions */
  232. static inline void set_kprobe_instance(struct kprobe *kp)
  233. {
  234. __get_cpu_var(kprobe_instance) = kp;
  235. }
  236. static inline void reset_kprobe_instance(void)
  237. {
  238. __get_cpu_var(kprobe_instance) = NULL;
  239. }
  240. /*
  241. * This routine is called either:
  242. * - under the kprobe_mutex - during kprobe_[un]register()
  243. * OR
  244. * - with preemption disabled - from arch/xxx/kernel/kprobes.c
  245. */
  246. struct kprobe __kprobes *get_kprobe(void *addr)
  247. {
  248. struct hlist_head *head;
  249. struct hlist_node *node;
  250. struct kprobe *p;
  251. head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
  252. hlist_for_each_entry_rcu(p, node, head, hlist) {
  253. if (p->addr == addr)
  254. return p;
  255. }
  256. return NULL;
  257. }
  258. /*
  259. * Aggregate handlers for multiple kprobes support - these handlers
  260. * take care of invoking the individual kprobe handlers on p->list
  261. */
  262. static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
  263. {
  264. struct kprobe *kp;
  265. list_for_each_entry_rcu(kp, &p->list, list) {
  266. if (kp->pre_handler) {
  267. set_kprobe_instance(kp);
  268. if (kp->pre_handler(kp, regs))
  269. return 1;
  270. }
  271. reset_kprobe_instance();
  272. }
  273. return 0;
  274. }
  275. static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
  276. unsigned long flags)
  277. {
  278. struct kprobe *kp;
  279. list_for_each_entry_rcu(kp, &p->list, list) {
  280. if (kp->post_handler) {
  281. set_kprobe_instance(kp);
  282. kp->post_handler(kp, regs, flags);
  283. reset_kprobe_instance();
  284. }
  285. }
  286. }
  287. static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
  288. int trapnr)
  289. {
  290. struct kprobe *cur = __get_cpu_var(kprobe_instance);
  291. /*
  292. * if we faulted "during" the execution of a user specified
  293. * probe handler, invoke just that probe's fault handler
  294. */
  295. if (cur && cur->fault_handler) {
  296. if (cur->fault_handler(cur, regs, trapnr))
  297. return 1;
  298. }
  299. return 0;
  300. }
  301. static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
  302. {
  303. struct kprobe *cur = __get_cpu_var(kprobe_instance);
  304. int ret = 0;
  305. if (cur && cur->break_handler) {
  306. if (cur->break_handler(cur, regs))
  307. ret = 1;
  308. }
  309. reset_kprobe_instance();
  310. return ret;
  311. }
  312. /* Walks the list and increments nmissed count for multiprobe case */
  313. void __kprobes kprobes_inc_nmissed_count(struct kprobe *p)
  314. {
  315. struct kprobe *kp;
  316. if (p->pre_handler != aggr_pre_handler) {
  317. p->nmissed++;
  318. } else {
  319. list_for_each_entry_rcu(kp, &p->list, list)
  320. kp->nmissed++;
  321. }
  322. return;
  323. }
  324. /* Called with kretprobe_lock held */
  325. void __kprobes recycle_rp_inst(struct kretprobe_instance *ri,
  326. struct hlist_head *head)
  327. {
  328. /* remove rp inst off the rprobe_inst_table */
  329. hlist_del(&ri->hlist);
  330. if (ri->rp) {
  331. /* remove rp inst off the used list */
  332. hlist_del(&ri->uflist);
  333. /* put rp inst back onto the free list */
  334. INIT_HLIST_NODE(&ri->uflist);
  335. hlist_add_head(&ri->uflist, &ri->rp->free_instances);
  336. } else
  337. /* Unregistering */
  338. hlist_add_head(&ri->hlist, head);
  339. }
  340. struct hlist_head __kprobes *kretprobe_inst_table_head(struct task_struct *tsk)
  341. {
  342. return &kretprobe_inst_table[hash_ptr(tsk, KPROBE_HASH_BITS)];
  343. }
  344. /*
  345. * This function is called from finish_task_switch when task tk becomes dead,
  346. * so that we can recycle any function-return probe instances associated
  347. * with this task. These left over instances represent probed functions
  348. * that have been called but will never return.
  349. */
  350. void __kprobes kprobe_flush_task(struct task_struct *tk)
  351. {
  352. struct kretprobe_instance *ri;
  353. struct hlist_head *head, empty_rp;
  354. struct hlist_node *node, *tmp;
  355. unsigned long flags = 0;
  356. INIT_HLIST_HEAD(&empty_rp);
  357. spin_lock_irqsave(&kretprobe_lock, flags);
  358. head = kretprobe_inst_table_head(tk);
  359. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  360. if (ri->task == tk)
  361. recycle_rp_inst(ri, &empty_rp);
  362. }
  363. spin_unlock_irqrestore(&kretprobe_lock, flags);
  364. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  365. hlist_del(&ri->hlist);
  366. kfree(ri);
  367. }
  368. }
  369. static inline void free_rp_inst(struct kretprobe *rp)
  370. {
  371. struct kretprobe_instance *ri;
  372. struct hlist_node *pos, *next;
  373. hlist_for_each_entry_safe(ri, pos, next, &rp->free_instances, uflist) {
  374. hlist_del(&ri->uflist);
  375. kfree(ri);
  376. }
  377. }
  378. /*
  379. * Keep all fields in the kprobe consistent
  380. */
  381. static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
  382. {
  383. memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
  384. memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
  385. }
  386. /*
  387. * Add the new probe to old_p->list. Fail if this is the
  388. * second jprobe at the address - two jprobes can't coexist
  389. */
  390. static int __kprobes add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
  391. {
  392. if (p->break_handler) {
  393. if (old_p->break_handler)
  394. return -EEXIST;
  395. list_add_tail_rcu(&p->list, &old_p->list);
  396. old_p->break_handler = aggr_break_handler;
  397. } else
  398. list_add_rcu(&p->list, &old_p->list);
  399. if (p->post_handler && !old_p->post_handler)
  400. old_p->post_handler = aggr_post_handler;
  401. return 0;
  402. }
  403. /*
  404. * Fill in the required fields of the "manager kprobe". Replace the
  405. * earlier kprobe in the hlist with the manager kprobe
  406. */
  407. static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
  408. {
  409. copy_kprobe(p, ap);
  410. flush_insn_slot(ap);
  411. ap->addr = p->addr;
  412. ap->pre_handler = aggr_pre_handler;
  413. ap->fault_handler = aggr_fault_handler;
  414. if (p->post_handler)
  415. ap->post_handler = aggr_post_handler;
  416. if (p->break_handler)
  417. ap->break_handler = aggr_break_handler;
  418. INIT_LIST_HEAD(&ap->list);
  419. list_add_rcu(&p->list, &ap->list);
  420. hlist_replace_rcu(&p->hlist, &ap->hlist);
  421. }
  422. /*
  423. * This is the second or subsequent kprobe at the address - handle
  424. * the intricacies
  425. */
  426. static int __kprobes register_aggr_kprobe(struct kprobe *old_p,
  427. struct kprobe *p)
  428. {
  429. int ret = 0;
  430. struct kprobe *ap;
  431. if (old_p->pre_handler == aggr_pre_handler) {
  432. copy_kprobe(old_p, p);
  433. ret = add_new_kprobe(old_p, p);
  434. } else {
  435. ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
  436. if (!ap)
  437. return -ENOMEM;
  438. add_aggr_kprobe(ap, old_p);
  439. copy_kprobe(ap, p);
  440. ret = add_new_kprobe(ap, p);
  441. }
  442. return ret;
  443. }
  444. static int __kprobes in_kprobes_functions(unsigned long addr)
  445. {
  446. if (addr >= (unsigned long)__kprobes_text_start &&
  447. addr < (unsigned long)__kprobes_text_end)
  448. return -EINVAL;
  449. return 0;
  450. }
  451. static int __kprobes __register_kprobe(struct kprobe *p,
  452. unsigned long called_from)
  453. {
  454. int ret = 0;
  455. struct kprobe *old_p;
  456. struct module *probed_mod;
  457. /*
  458. * If we have a symbol_name argument look it up,
  459. * and add it to the address. That way the addr
  460. * field can either be global or relative to a symbol.
  461. */
  462. if (p->symbol_name) {
  463. if (p->addr)
  464. return -EINVAL;
  465. kprobe_lookup_name(p->symbol_name, p->addr);
  466. }
  467. if (!p->addr)
  468. return -EINVAL;
  469. p->addr = (kprobe_opcode_t *)(((char *)p->addr)+ p->offset);
  470. if (!kernel_text_address((unsigned long) p->addr) ||
  471. in_kprobes_functions((unsigned long) p->addr))
  472. return -EINVAL;
  473. p->mod_refcounted = 0;
  474. /*
  475. * Check if are we probing a module.
  476. */
  477. probed_mod = module_text_address((unsigned long) p->addr);
  478. if (probed_mod) {
  479. struct module *calling_mod = module_text_address(called_from);
  480. /*
  481. * We must allow modules to probe themself and in this case
  482. * avoid incrementing the module refcount, so as to allow
  483. * unloading of self probing modules.
  484. */
  485. if (calling_mod && calling_mod != probed_mod) {
  486. if (unlikely(!try_module_get(probed_mod)))
  487. return -EINVAL;
  488. p->mod_refcounted = 1;
  489. } else
  490. probed_mod = NULL;
  491. }
  492. p->nmissed = 0;
  493. mutex_lock(&kprobe_mutex);
  494. old_p = get_kprobe(p->addr);
  495. if (old_p) {
  496. ret = register_aggr_kprobe(old_p, p);
  497. if (!ret)
  498. atomic_inc(&kprobe_count);
  499. goto out;
  500. }
  501. ret = arch_prepare_kprobe(p);
  502. if (ret)
  503. goto out;
  504. INIT_HLIST_NODE(&p->hlist);
  505. hlist_add_head_rcu(&p->hlist,
  506. &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
  507. if (atomic_add_return(1, &kprobe_count) == \
  508. (ARCH_INACTIVE_KPROBE_COUNT + 1))
  509. register_page_fault_notifier(&kprobe_page_fault_nb);
  510. arch_arm_kprobe(p);
  511. out:
  512. mutex_unlock(&kprobe_mutex);
  513. if (ret && probed_mod)
  514. module_put(probed_mod);
  515. return ret;
  516. }
  517. int __kprobes register_kprobe(struct kprobe *p)
  518. {
  519. return __register_kprobe(p, (unsigned long)__builtin_return_address(0));
  520. }
  521. void __kprobes unregister_kprobe(struct kprobe *p)
  522. {
  523. struct module *mod;
  524. struct kprobe *old_p, *list_p;
  525. int cleanup_p;
  526. mutex_lock(&kprobe_mutex);
  527. old_p = get_kprobe(p->addr);
  528. if (unlikely(!old_p)) {
  529. mutex_unlock(&kprobe_mutex);
  530. return;
  531. }
  532. if (p != old_p) {
  533. list_for_each_entry_rcu(list_p, &old_p->list, list)
  534. if (list_p == p)
  535. /* kprobe p is a valid probe */
  536. goto valid_p;
  537. mutex_unlock(&kprobe_mutex);
  538. return;
  539. }
  540. valid_p:
  541. if (old_p == p ||
  542. (old_p->pre_handler == aggr_pre_handler &&
  543. p->list.next == &old_p->list && p->list.prev == &old_p->list)) {
  544. /* Only probe on the hash list */
  545. arch_disarm_kprobe(p);
  546. hlist_del_rcu(&old_p->hlist);
  547. cleanup_p = 1;
  548. } else {
  549. list_del_rcu(&p->list);
  550. cleanup_p = 0;
  551. }
  552. mutex_unlock(&kprobe_mutex);
  553. synchronize_sched();
  554. if (p->mod_refcounted) {
  555. mod = module_text_address((unsigned long)p->addr);
  556. if (mod)
  557. module_put(mod);
  558. }
  559. if (cleanup_p) {
  560. if (p != old_p) {
  561. list_del_rcu(&p->list);
  562. kfree(old_p);
  563. }
  564. arch_remove_kprobe(p);
  565. } else {
  566. mutex_lock(&kprobe_mutex);
  567. if (p->break_handler)
  568. old_p->break_handler = NULL;
  569. if (p->post_handler){
  570. list_for_each_entry_rcu(list_p, &old_p->list, list){
  571. if (list_p->post_handler){
  572. cleanup_p = 2;
  573. break;
  574. }
  575. }
  576. if (cleanup_p == 0)
  577. old_p->post_handler = NULL;
  578. }
  579. mutex_unlock(&kprobe_mutex);
  580. }
  581. /* Call unregister_page_fault_notifier()
  582. * if no probes are active
  583. */
  584. mutex_lock(&kprobe_mutex);
  585. if (atomic_add_return(-1, &kprobe_count) == \
  586. ARCH_INACTIVE_KPROBE_COUNT)
  587. unregister_page_fault_notifier(&kprobe_page_fault_nb);
  588. mutex_unlock(&kprobe_mutex);
  589. return;
  590. }
  591. static struct notifier_block kprobe_exceptions_nb = {
  592. .notifier_call = kprobe_exceptions_notify,
  593. .priority = 0x7fffffff /* we need to be notified first */
  594. };
  595. int __kprobes register_jprobe(struct jprobe *jp)
  596. {
  597. /* Todo: Verify probepoint is a function entry point */
  598. jp->kp.pre_handler = setjmp_pre_handler;
  599. jp->kp.break_handler = longjmp_break_handler;
  600. return __register_kprobe(&jp->kp,
  601. (unsigned long)__builtin_return_address(0));
  602. }
  603. void __kprobes unregister_jprobe(struct jprobe *jp)
  604. {
  605. unregister_kprobe(&jp->kp);
  606. }
  607. #ifdef ARCH_SUPPORTS_KRETPROBES
  608. /*
  609. * This kprobe pre_handler is registered with every kretprobe. When probe
  610. * hits it will set up the return probe.
  611. */
  612. static int __kprobes pre_handler_kretprobe(struct kprobe *p,
  613. struct pt_regs *regs)
  614. {
  615. struct kretprobe *rp = container_of(p, struct kretprobe, kp);
  616. unsigned long flags = 0;
  617. /*TODO: consider to only swap the RA after the last pre_handler fired */
  618. spin_lock_irqsave(&kretprobe_lock, flags);
  619. if (!hlist_empty(&rp->free_instances)) {
  620. struct kretprobe_instance *ri;
  621. ri = hlist_entry(rp->free_instances.first,
  622. struct kretprobe_instance, uflist);
  623. ri->rp = rp;
  624. ri->task = current;
  625. arch_prepare_kretprobe(ri, regs);
  626. /* XXX(hch): why is there no hlist_move_head? */
  627. hlist_del(&ri->uflist);
  628. hlist_add_head(&ri->uflist, &ri->rp->used_instances);
  629. hlist_add_head(&ri->hlist, kretprobe_inst_table_head(ri->task));
  630. } else
  631. rp->nmissed++;
  632. spin_unlock_irqrestore(&kretprobe_lock, flags);
  633. return 0;
  634. }
  635. int __kprobes register_kretprobe(struct kretprobe *rp)
  636. {
  637. int ret = 0;
  638. struct kretprobe_instance *inst;
  639. int i;
  640. rp->kp.pre_handler = pre_handler_kretprobe;
  641. rp->kp.post_handler = NULL;
  642. rp->kp.fault_handler = NULL;
  643. rp->kp.break_handler = NULL;
  644. /* Pre-allocate memory for max kretprobe instances */
  645. if (rp->maxactive <= 0) {
  646. #ifdef CONFIG_PREEMPT
  647. rp->maxactive = max(10, 2 * NR_CPUS);
  648. #else
  649. rp->maxactive = NR_CPUS;
  650. #endif
  651. }
  652. INIT_HLIST_HEAD(&rp->used_instances);
  653. INIT_HLIST_HEAD(&rp->free_instances);
  654. for (i = 0; i < rp->maxactive; i++) {
  655. inst = kmalloc(sizeof(struct kretprobe_instance), GFP_KERNEL);
  656. if (inst == NULL) {
  657. free_rp_inst(rp);
  658. return -ENOMEM;
  659. }
  660. INIT_HLIST_NODE(&inst->uflist);
  661. hlist_add_head(&inst->uflist, &rp->free_instances);
  662. }
  663. rp->nmissed = 0;
  664. /* Establish function entry probe point */
  665. if ((ret = __register_kprobe(&rp->kp,
  666. (unsigned long)__builtin_return_address(0))) != 0)
  667. free_rp_inst(rp);
  668. return ret;
  669. }
  670. #else /* ARCH_SUPPORTS_KRETPROBES */
  671. int __kprobes register_kretprobe(struct kretprobe *rp)
  672. {
  673. return -ENOSYS;
  674. }
  675. static int __kprobes pre_handler_kretprobe(struct kprobe *p,
  676. struct pt_regs *regs)
  677. {
  678. return 0;
  679. }
  680. #endif /* ARCH_SUPPORTS_KRETPROBES */
  681. void __kprobes unregister_kretprobe(struct kretprobe *rp)
  682. {
  683. unsigned long flags;
  684. struct kretprobe_instance *ri;
  685. struct hlist_node *pos, *next;
  686. unregister_kprobe(&rp->kp);
  687. /* No race here */
  688. spin_lock_irqsave(&kretprobe_lock, flags);
  689. hlist_for_each_entry_safe(ri, pos, next, &rp->used_instances, uflist) {
  690. ri->rp = NULL;
  691. hlist_del(&ri->uflist);
  692. }
  693. spin_unlock_irqrestore(&kretprobe_lock, flags);
  694. free_rp_inst(rp);
  695. }
  696. static int __init init_kprobes(void)
  697. {
  698. int i, err = 0;
  699. /* FIXME allocate the probe table, currently defined statically */
  700. /* initialize all list heads */
  701. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  702. INIT_HLIST_HEAD(&kprobe_table[i]);
  703. INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
  704. }
  705. atomic_set(&kprobe_count, 0);
  706. err = arch_init_kprobes();
  707. if (!err)
  708. err = register_die_notifier(&kprobe_exceptions_nb);
  709. return err;
  710. }
  711. #ifdef CONFIG_DEBUG_FS
  712. static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p,
  713. const char *sym, int offset,char *modname)
  714. {
  715. char *kprobe_type;
  716. if (p->pre_handler == pre_handler_kretprobe)
  717. kprobe_type = "r";
  718. else if (p->pre_handler == setjmp_pre_handler)
  719. kprobe_type = "j";
  720. else
  721. kprobe_type = "k";
  722. if (sym)
  723. seq_printf(pi, "%p %s %s+0x%x %s\n", p->addr, kprobe_type,
  724. sym, offset, (modname ? modname : " "));
  725. else
  726. seq_printf(pi, "%p %s %p\n", p->addr, kprobe_type, p->addr);
  727. }
  728. static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos)
  729. {
  730. return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
  731. }
  732. static void __kprobes *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
  733. {
  734. (*pos)++;
  735. if (*pos >= KPROBE_TABLE_SIZE)
  736. return NULL;
  737. return pos;
  738. }
  739. static void __kprobes kprobe_seq_stop(struct seq_file *f, void *v)
  740. {
  741. /* Nothing to do */
  742. }
  743. static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v)
  744. {
  745. struct hlist_head *head;
  746. struct hlist_node *node;
  747. struct kprobe *p, *kp;
  748. const char *sym = NULL;
  749. unsigned int i = *(loff_t *) v;
  750. unsigned long offset = 0;
  751. char *modname, namebuf[128];
  752. head = &kprobe_table[i];
  753. preempt_disable();
  754. hlist_for_each_entry_rcu(p, node, head, hlist) {
  755. sym = kallsyms_lookup((unsigned long)p->addr, NULL,
  756. &offset, &modname, namebuf);
  757. if (p->pre_handler == aggr_pre_handler) {
  758. list_for_each_entry_rcu(kp, &p->list, list)
  759. report_probe(pi, kp, sym, offset, modname);
  760. } else
  761. report_probe(pi, p, sym, offset, modname);
  762. }
  763. preempt_enable();
  764. return 0;
  765. }
  766. static struct seq_operations kprobes_seq_ops = {
  767. .start = kprobe_seq_start,
  768. .next = kprobe_seq_next,
  769. .stop = kprobe_seq_stop,
  770. .show = show_kprobe_addr
  771. };
  772. static int __kprobes kprobes_open(struct inode *inode, struct file *filp)
  773. {
  774. return seq_open(filp, &kprobes_seq_ops);
  775. }
  776. static struct file_operations debugfs_kprobes_operations = {
  777. .open = kprobes_open,
  778. .read = seq_read,
  779. .llseek = seq_lseek,
  780. .release = seq_release,
  781. };
  782. static int __kprobes debugfs_kprobe_init(void)
  783. {
  784. struct dentry *dir, *file;
  785. dir = debugfs_create_dir("kprobes", NULL);
  786. if (!dir)
  787. return -ENOMEM;
  788. file = debugfs_create_file("list", 0444, dir, NULL,
  789. &debugfs_kprobes_operations);
  790. if (!file) {
  791. debugfs_remove(dir);
  792. return -ENOMEM;
  793. }
  794. return 0;
  795. }
  796. late_initcall(debugfs_kprobe_init);
  797. #endif /* CONFIG_DEBUG_FS */
  798. module_init(init_kprobes);
  799. EXPORT_SYMBOL_GPL(register_kprobe);
  800. EXPORT_SYMBOL_GPL(unregister_kprobe);
  801. EXPORT_SYMBOL_GPL(register_jprobe);
  802. EXPORT_SYMBOL_GPL(unregister_jprobe);
  803. EXPORT_SYMBOL_GPL(jprobe_return);
  804. EXPORT_SYMBOL_GPL(register_kretprobe);
  805. EXPORT_SYMBOL_GPL(unregister_kretprobe);