kprobes.c 18 KB

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