kprobes.c 21 KB

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