kprobes.c 18 KB

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