kprobes.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409
  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. #include <asm/uaccess.h>
  50. #define KPROBE_HASH_BITS 6
  51. #define KPROBE_TABLE_SIZE (1 << KPROBE_HASH_BITS)
  52. /*
  53. * Some oddball architectures like 64bit powerpc have function descriptors
  54. * so this must be overridable.
  55. */
  56. #ifndef kprobe_lookup_name
  57. #define kprobe_lookup_name(name, addr) \
  58. addr = ((kprobe_opcode_t *)(kallsyms_lookup_name(name)))
  59. #endif
  60. static int kprobes_initialized;
  61. static struct hlist_head kprobe_table[KPROBE_TABLE_SIZE];
  62. static struct hlist_head kretprobe_inst_table[KPROBE_TABLE_SIZE];
  63. /* NOTE: change this value only with kprobe_mutex held */
  64. static bool kprobe_enabled;
  65. static DEFINE_MUTEX(kprobe_mutex); /* Protects kprobe_table */
  66. static DEFINE_PER_CPU(struct kprobe *, kprobe_instance) = NULL;
  67. static struct {
  68. spinlock_t lock ____cacheline_aligned_in_smp;
  69. } kretprobe_table_locks[KPROBE_TABLE_SIZE];
  70. static spinlock_t *kretprobe_table_lock_ptr(unsigned long hash)
  71. {
  72. return &(kretprobe_table_locks[hash].lock);
  73. }
  74. /*
  75. * Normally, functions that we'd want to prohibit kprobes in, are marked
  76. * __kprobes. But, there are cases where such functions already belong to
  77. * a different section (__sched for preempt_schedule)
  78. *
  79. * For such cases, we now have a blacklist
  80. */
  81. static struct kprobe_blackpoint kprobe_blacklist[] = {
  82. {"preempt_schedule",},
  83. {NULL} /* Terminator */
  84. };
  85. #ifdef __ARCH_WANT_KPROBES_INSN_SLOT
  86. /*
  87. * kprobe->ainsn.insn points to the copy of the instruction to be
  88. * single-stepped. x86_64, POWER4 and above have no-exec support and
  89. * stepping on the instruction on a vmalloced/kmalloced/data page
  90. * is a recipe for disaster
  91. */
  92. #define INSNS_PER_PAGE (PAGE_SIZE/(MAX_INSN_SIZE * sizeof(kprobe_opcode_t)))
  93. struct kprobe_insn_page {
  94. struct hlist_node hlist;
  95. kprobe_opcode_t *insns; /* Page of instruction slots */
  96. char slot_used[INSNS_PER_PAGE];
  97. int nused;
  98. int ngarbage;
  99. };
  100. enum kprobe_slot_state {
  101. SLOT_CLEAN = 0,
  102. SLOT_DIRTY = 1,
  103. SLOT_USED = 2,
  104. };
  105. static DEFINE_MUTEX(kprobe_insn_mutex); /* Protects kprobe_insn_pages */
  106. static struct hlist_head kprobe_insn_pages;
  107. static int kprobe_garbage_slots;
  108. static int collect_garbage_slots(void);
  109. static int __kprobes check_safety(void)
  110. {
  111. int ret = 0;
  112. #if defined(CONFIG_PREEMPT) && defined(CONFIG_PM)
  113. ret = freeze_processes();
  114. if (ret == 0) {
  115. struct task_struct *p, *q;
  116. do_each_thread(p, q) {
  117. if (p != current && p->state == TASK_RUNNING &&
  118. p->pid != 0) {
  119. printk("Check failed: %s is running\n",p->comm);
  120. ret = -1;
  121. goto loop_end;
  122. }
  123. } while_each_thread(p, q);
  124. }
  125. loop_end:
  126. thaw_processes();
  127. #else
  128. synchronize_sched();
  129. #endif
  130. return ret;
  131. }
  132. /**
  133. * __get_insn_slot() - Find a slot on an executable page for an instruction.
  134. * We allocate an executable page if there's no room on existing ones.
  135. */
  136. static kprobe_opcode_t __kprobes *__get_insn_slot(void)
  137. {
  138. struct kprobe_insn_page *kip;
  139. struct hlist_node *pos;
  140. retry:
  141. hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
  142. if (kip->nused < INSNS_PER_PAGE) {
  143. int i;
  144. for (i = 0; i < INSNS_PER_PAGE; i++) {
  145. if (kip->slot_used[i] == SLOT_CLEAN) {
  146. kip->slot_used[i] = SLOT_USED;
  147. kip->nused++;
  148. return kip->insns + (i * MAX_INSN_SIZE);
  149. }
  150. }
  151. /* Surprise! No unused slots. Fix kip->nused. */
  152. kip->nused = INSNS_PER_PAGE;
  153. }
  154. }
  155. /* If there are any garbage slots, collect it and try again. */
  156. if (kprobe_garbage_slots && collect_garbage_slots() == 0) {
  157. goto retry;
  158. }
  159. /* All out of space. Need to allocate a new page. Use slot 0. */
  160. kip = kmalloc(sizeof(struct kprobe_insn_page), GFP_KERNEL);
  161. if (!kip)
  162. return NULL;
  163. /*
  164. * Use module_alloc so this page is within +/- 2GB of where the
  165. * kernel image and loaded module images reside. This is required
  166. * so x86_64 can correctly handle the %rip-relative fixups.
  167. */
  168. kip->insns = module_alloc(PAGE_SIZE);
  169. if (!kip->insns) {
  170. kfree(kip);
  171. return NULL;
  172. }
  173. INIT_HLIST_NODE(&kip->hlist);
  174. hlist_add_head(&kip->hlist, &kprobe_insn_pages);
  175. memset(kip->slot_used, SLOT_CLEAN, INSNS_PER_PAGE);
  176. kip->slot_used[0] = SLOT_USED;
  177. kip->nused = 1;
  178. kip->ngarbage = 0;
  179. return kip->insns;
  180. }
  181. kprobe_opcode_t __kprobes *get_insn_slot(void)
  182. {
  183. kprobe_opcode_t *ret;
  184. mutex_lock(&kprobe_insn_mutex);
  185. ret = __get_insn_slot();
  186. mutex_unlock(&kprobe_insn_mutex);
  187. return ret;
  188. }
  189. /* Return 1 if all garbages are collected, otherwise 0. */
  190. static int __kprobes collect_one_slot(struct kprobe_insn_page *kip, int idx)
  191. {
  192. kip->slot_used[idx] = SLOT_CLEAN;
  193. kip->nused--;
  194. if (kip->nused == 0) {
  195. /*
  196. * Page is no longer in use. Free it unless
  197. * it's the last one. We keep the last one
  198. * so as not to have to set it up again the
  199. * next time somebody inserts a probe.
  200. */
  201. hlist_del(&kip->hlist);
  202. if (hlist_empty(&kprobe_insn_pages)) {
  203. INIT_HLIST_NODE(&kip->hlist);
  204. hlist_add_head(&kip->hlist,
  205. &kprobe_insn_pages);
  206. } else {
  207. module_free(NULL, kip->insns);
  208. kfree(kip);
  209. }
  210. return 1;
  211. }
  212. return 0;
  213. }
  214. static int __kprobes collect_garbage_slots(void)
  215. {
  216. struct kprobe_insn_page *kip;
  217. struct hlist_node *pos, *next;
  218. int safety;
  219. /* Ensure no-one is preepmted on the garbages */
  220. mutex_unlock(&kprobe_insn_mutex);
  221. safety = check_safety();
  222. mutex_lock(&kprobe_insn_mutex);
  223. if (safety != 0)
  224. return -EAGAIN;
  225. hlist_for_each_entry_safe(kip, pos, next, &kprobe_insn_pages, hlist) {
  226. int i;
  227. if (kip->ngarbage == 0)
  228. continue;
  229. kip->ngarbage = 0; /* we will collect all garbages */
  230. for (i = 0; i < INSNS_PER_PAGE; i++) {
  231. if (kip->slot_used[i] == SLOT_DIRTY &&
  232. collect_one_slot(kip, i))
  233. break;
  234. }
  235. }
  236. kprobe_garbage_slots = 0;
  237. return 0;
  238. }
  239. void __kprobes free_insn_slot(kprobe_opcode_t * slot, int dirty)
  240. {
  241. struct kprobe_insn_page *kip;
  242. struct hlist_node *pos;
  243. mutex_lock(&kprobe_insn_mutex);
  244. hlist_for_each_entry(kip, pos, &kprobe_insn_pages, hlist) {
  245. if (kip->insns <= slot &&
  246. slot < kip->insns + (INSNS_PER_PAGE * MAX_INSN_SIZE)) {
  247. int i = (slot - kip->insns) / MAX_INSN_SIZE;
  248. if (dirty) {
  249. kip->slot_used[i] = SLOT_DIRTY;
  250. kip->ngarbage++;
  251. } else {
  252. collect_one_slot(kip, i);
  253. }
  254. break;
  255. }
  256. }
  257. if (dirty && ++kprobe_garbage_slots > INSNS_PER_PAGE)
  258. collect_garbage_slots();
  259. mutex_unlock(&kprobe_insn_mutex);
  260. }
  261. #endif
  262. /* We have preemption disabled.. so it is safe to use __ versions */
  263. static inline void set_kprobe_instance(struct kprobe *kp)
  264. {
  265. __get_cpu_var(kprobe_instance) = kp;
  266. }
  267. static inline void reset_kprobe_instance(void)
  268. {
  269. __get_cpu_var(kprobe_instance) = NULL;
  270. }
  271. /*
  272. * This routine is called either:
  273. * - under the kprobe_mutex - during kprobe_[un]register()
  274. * OR
  275. * - with preemption disabled - from arch/xxx/kernel/kprobes.c
  276. */
  277. struct kprobe __kprobes *get_kprobe(void *addr)
  278. {
  279. struct hlist_head *head;
  280. struct hlist_node *node;
  281. struct kprobe *p;
  282. head = &kprobe_table[hash_ptr(addr, KPROBE_HASH_BITS)];
  283. hlist_for_each_entry_rcu(p, node, head, hlist) {
  284. if (p->addr == addr)
  285. return p;
  286. }
  287. return NULL;
  288. }
  289. /*
  290. * Aggregate handlers for multiple kprobes support - these handlers
  291. * take care of invoking the individual kprobe handlers on p->list
  292. */
  293. static int __kprobes aggr_pre_handler(struct kprobe *p, struct pt_regs *regs)
  294. {
  295. struct kprobe *kp;
  296. list_for_each_entry_rcu(kp, &p->list, list) {
  297. if (kp->pre_handler && !kprobe_gone(kp)) {
  298. set_kprobe_instance(kp);
  299. if (kp->pre_handler(kp, regs))
  300. return 1;
  301. }
  302. reset_kprobe_instance();
  303. }
  304. return 0;
  305. }
  306. static void __kprobes aggr_post_handler(struct kprobe *p, struct pt_regs *regs,
  307. unsigned long flags)
  308. {
  309. struct kprobe *kp;
  310. list_for_each_entry_rcu(kp, &p->list, list) {
  311. if (kp->post_handler && !kprobe_gone(kp)) {
  312. set_kprobe_instance(kp);
  313. kp->post_handler(kp, regs, flags);
  314. reset_kprobe_instance();
  315. }
  316. }
  317. }
  318. static int __kprobes aggr_fault_handler(struct kprobe *p, struct pt_regs *regs,
  319. int trapnr)
  320. {
  321. struct kprobe *cur = __get_cpu_var(kprobe_instance);
  322. /*
  323. * if we faulted "during" the execution of a user specified
  324. * probe handler, invoke just that probe's fault handler
  325. */
  326. if (cur && cur->fault_handler) {
  327. if (cur->fault_handler(cur, regs, trapnr))
  328. return 1;
  329. }
  330. return 0;
  331. }
  332. static int __kprobes aggr_break_handler(struct kprobe *p, struct pt_regs *regs)
  333. {
  334. struct kprobe *cur = __get_cpu_var(kprobe_instance);
  335. int ret = 0;
  336. if (cur && cur->break_handler) {
  337. if (cur->break_handler(cur, regs))
  338. ret = 1;
  339. }
  340. reset_kprobe_instance();
  341. return ret;
  342. }
  343. /* Walks the list and increments nmissed count for multiprobe case */
  344. void __kprobes kprobes_inc_nmissed_count(struct kprobe *p)
  345. {
  346. struct kprobe *kp;
  347. if (p->pre_handler != aggr_pre_handler) {
  348. p->nmissed++;
  349. } else {
  350. list_for_each_entry_rcu(kp, &p->list, list)
  351. kp->nmissed++;
  352. }
  353. return;
  354. }
  355. void __kprobes recycle_rp_inst(struct kretprobe_instance *ri,
  356. struct hlist_head *head)
  357. {
  358. struct kretprobe *rp = ri->rp;
  359. /* remove rp inst off the rprobe_inst_table */
  360. hlist_del(&ri->hlist);
  361. INIT_HLIST_NODE(&ri->hlist);
  362. if (likely(rp)) {
  363. spin_lock(&rp->lock);
  364. hlist_add_head(&ri->hlist, &rp->free_instances);
  365. spin_unlock(&rp->lock);
  366. } else
  367. /* Unregistering */
  368. hlist_add_head(&ri->hlist, head);
  369. }
  370. void __kprobes kretprobe_hash_lock(struct task_struct *tsk,
  371. struct hlist_head **head, unsigned long *flags)
  372. {
  373. unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
  374. spinlock_t *hlist_lock;
  375. *head = &kretprobe_inst_table[hash];
  376. hlist_lock = kretprobe_table_lock_ptr(hash);
  377. spin_lock_irqsave(hlist_lock, *flags);
  378. }
  379. static void __kprobes kretprobe_table_lock(unsigned long hash,
  380. unsigned long *flags)
  381. {
  382. spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
  383. spin_lock_irqsave(hlist_lock, *flags);
  384. }
  385. void __kprobes kretprobe_hash_unlock(struct task_struct *tsk,
  386. unsigned long *flags)
  387. {
  388. unsigned long hash = hash_ptr(tsk, KPROBE_HASH_BITS);
  389. spinlock_t *hlist_lock;
  390. hlist_lock = kretprobe_table_lock_ptr(hash);
  391. spin_unlock_irqrestore(hlist_lock, *flags);
  392. }
  393. void __kprobes kretprobe_table_unlock(unsigned long hash, unsigned long *flags)
  394. {
  395. spinlock_t *hlist_lock = kretprobe_table_lock_ptr(hash);
  396. spin_unlock_irqrestore(hlist_lock, *flags);
  397. }
  398. /*
  399. * This function is called from finish_task_switch when task tk becomes dead,
  400. * so that we can recycle any function-return probe instances associated
  401. * with this task. These left over instances represent probed functions
  402. * that have been called but will never return.
  403. */
  404. void __kprobes kprobe_flush_task(struct task_struct *tk)
  405. {
  406. struct kretprobe_instance *ri;
  407. struct hlist_head *head, empty_rp;
  408. struct hlist_node *node, *tmp;
  409. unsigned long hash, flags = 0;
  410. if (unlikely(!kprobes_initialized))
  411. /* Early boot. kretprobe_table_locks not yet initialized. */
  412. return;
  413. hash = hash_ptr(tk, KPROBE_HASH_BITS);
  414. head = &kretprobe_inst_table[hash];
  415. kretprobe_table_lock(hash, &flags);
  416. hlist_for_each_entry_safe(ri, node, tmp, head, hlist) {
  417. if (ri->task == tk)
  418. recycle_rp_inst(ri, &empty_rp);
  419. }
  420. kretprobe_table_unlock(hash, &flags);
  421. INIT_HLIST_HEAD(&empty_rp);
  422. hlist_for_each_entry_safe(ri, node, tmp, &empty_rp, hlist) {
  423. hlist_del(&ri->hlist);
  424. kfree(ri);
  425. }
  426. }
  427. static inline void free_rp_inst(struct kretprobe *rp)
  428. {
  429. struct kretprobe_instance *ri;
  430. struct hlist_node *pos, *next;
  431. hlist_for_each_entry_safe(ri, pos, next, &rp->free_instances, hlist) {
  432. hlist_del(&ri->hlist);
  433. kfree(ri);
  434. }
  435. }
  436. static void __kprobes cleanup_rp_inst(struct kretprobe *rp)
  437. {
  438. unsigned long flags, hash;
  439. struct kretprobe_instance *ri;
  440. struct hlist_node *pos, *next;
  441. struct hlist_head *head;
  442. /* No race here */
  443. for (hash = 0; hash < KPROBE_TABLE_SIZE; hash++) {
  444. kretprobe_table_lock(hash, &flags);
  445. head = &kretprobe_inst_table[hash];
  446. hlist_for_each_entry_safe(ri, pos, next, head, hlist) {
  447. if (ri->rp == rp)
  448. ri->rp = NULL;
  449. }
  450. kretprobe_table_unlock(hash, &flags);
  451. }
  452. free_rp_inst(rp);
  453. }
  454. /*
  455. * Keep all fields in the kprobe consistent
  456. */
  457. static inline void copy_kprobe(struct kprobe *old_p, struct kprobe *p)
  458. {
  459. memcpy(&p->opcode, &old_p->opcode, sizeof(kprobe_opcode_t));
  460. memcpy(&p->ainsn, &old_p->ainsn, sizeof(struct arch_specific_insn));
  461. }
  462. /*
  463. * Add the new probe to old_p->list. Fail if this is the
  464. * second jprobe at the address - two jprobes can't coexist
  465. */
  466. static int __kprobes add_new_kprobe(struct kprobe *old_p, struct kprobe *p)
  467. {
  468. if (p->break_handler) {
  469. if (old_p->break_handler)
  470. return -EEXIST;
  471. list_add_tail_rcu(&p->list, &old_p->list);
  472. old_p->break_handler = aggr_break_handler;
  473. } else
  474. list_add_rcu(&p->list, &old_p->list);
  475. if (p->post_handler && !old_p->post_handler)
  476. old_p->post_handler = aggr_post_handler;
  477. return 0;
  478. }
  479. /*
  480. * Fill in the required fields of the "manager kprobe". Replace the
  481. * earlier kprobe in the hlist with the manager kprobe
  482. */
  483. static inline void add_aggr_kprobe(struct kprobe *ap, struct kprobe *p)
  484. {
  485. copy_kprobe(p, ap);
  486. flush_insn_slot(ap);
  487. ap->addr = p->addr;
  488. ap->pre_handler = aggr_pre_handler;
  489. ap->fault_handler = aggr_fault_handler;
  490. /* We don't care the kprobe which has gone. */
  491. if (p->post_handler && !kprobe_gone(p))
  492. ap->post_handler = aggr_post_handler;
  493. if (p->break_handler && !kprobe_gone(p))
  494. ap->break_handler = aggr_break_handler;
  495. INIT_LIST_HEAD(&ap->list);
  496. list_add_rcu(&p->list, &ap->list);
  497. hlist_replace_rcu(&p->hlist, &ap->hlist);
  498. }
  499. /*
  500. * This is the second or subsequent kprobe at the address - handle
  501. * the intricacies
  502. */
  503. static int __kprobes register_aggr_kprobe(struct kprobe *old_p,
  504. struct kprobe *p)
  505. {
  506. int ret = 0;
  507. struct kprobe *ap;
  508. if (kprobe_gone(old_p)) {
  509. /*
  510. * Attempting to insert new probe at the same location that
  511. * had a probe in the module vaddr area which already
  512. * freed. So, the instruction slot has already been
  513. * released. We need a new slot for the new probe.
  514. */
  515. ret = arch_prepare_kprobe(old_p);
  516. if (ret)
  517. return ret;
  518. }
  519. if (old_p->pre_handler == aggr_pre_handler) {
  520. copy_kprobe(old_p, p);
  521. ret = add_new_kprobe(old_p, p);
  522. ap = old_p;
  523. } else {
  524. ap = kzalloc(sizeof(struct kprobe), GFP_KERNEL);
  525. if (!ap) {
  526. if (kprobe_gone(old_p))
  527. arch_remove_kprobe(old_p);
  528. return -ENOMEM;
  529. }
  530. add_aggr_kprobe(ap, old_p);
  531. copy_kprobe(ap, p);
  532. ret = add_new_kprobe(ap, p);
  533. }
  534. if (kprobe_gone(old_p)) {
  535. /*
  536. * If the old_p has gone, its breakpoint has been disarmed.
  537. * We have to arm it again after preparing real kprobes.
  538. */
  539. ap->flags &= ~KPROBE_FLAG_GONE;
  540. if (kprobe_enabled)
  541. arch_arm_kprobe(ap);
  542. }
  543. return ret;
  544. }
  545. static int __kprobes in_kprobes_functions(unsigned long addr)
  546. {
  547. struct kprobe_blackpoint *kb;
  548. if (addr >= (unsigned long)__kprobes_text_start &&
  549. addr < (unsigned long)__kprobes_text_end)
  550. return -EINVAL;
  551. /*
  552. * If there exists a kprobe_blacklist, verify and
  553. * fail any probe registration in the prohibited area
  554. */
  555. for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
  556. if (kb->start_addr) {
  557. if (addr >= kb->start_addr &&
  558. addr < (kb->start_addr + kb->range))
  559. return -EINVAL;
  560. }
  561. }
  562. return 0;
  563. }
  564. /*
  565. * If we have a symbol_name argument, look it up and add the offset field
  566. * to it. This way, we can specify a relative address to a symbol.
  567. */
  568. static kprobe_opcode_t __kprobes *kprobe_addr(struct kprobe *p)
  569. {
  570. kprobe_opcode_t *addr = p->addr;
  571. if (p->symbol_name) {
  572. if (addr)
  573. return NULL;
  574. kprobe_lookup_name(p->symbol_name, addr);
  575. }
  576. if (!addr)
  577. return NULL;
  578. return (kprobe_opcode_t *)(((char *)addr) + p->offset);
  579. }
  580. int __kprobes register_kprobe(struct kprobe *p)
  581. {
  582. int ret = 0;
  583. struct kprobe *old_p;
  584. struct module *probed_mod;
  585. kprobe_opcode_t *addr;
  586. addr = kprobe_addr(p);
  587. if (!addr)
  588. return -EINVAL;
  589. p->addr = addr;
  590. preempt_disable();
  591. if (!__kernel_text_address((unsigned long) p->addr) ||
  592. in_kprobes_functions((unsigned long) p->addr)) {
  593. preempt_enable();
  594. return -EINVAL;
  595. }
  596. p->flags = 0;
  597. /*
  598. * Check if are we probing a module.
  599. */
  600. probed_mod = __module_text_address((unsigned long) p->addr);
  601. if (probed_mod) {
  602. /*
  603. * We must hold a refcount of the probed module while updating
  604. * its code to prohibit unexpected unloading.
  605. */
  606. if (unlikely(!try_module_get(probed_mod))) {
  607. preempt_enable();
  608. return -EINVAL;
  609. }
  610. }
  611. preempt_enable();
  612. p->nmissed = 0;
  613. INIT_LIST_HEAD(&p->list);
  614. mutex_lock(&kprobe_mutex);
  615. old_p = get_kprobe(p->addr);
  616. if (old_p) {
  617. ret = register_aggr_kprobe(old_p, p);
  618. goto out;
  619. }
  620. ret = arch_prepare_kprobe(p);
  621. if (ret)
  622. goto out;
  623. INIT_HLIST_NODE(&p->hlist);
  624. hlist_add_head_rcu(&p->hlist,
  625. &kprobe_table[hash_ptr(p->addr, KPROBE_HASH_BITS)]);
  626. if (kprobe_enabled)
  627. arch_arm_kprobe(p);
  628. out:
  629. mutex_unlock(&kprobe_mutex);
  630. if (probed_mod)
  631. module_put(probed_mod);
  632. return ret;
  633. }
  634. /*
  635. * Unregister a kprobe without a scheduler synchronization.
  636. */
  637. static int __kprobes __unregister_kprobe_top(struct kprobe *p)
  638. {
  639. struct kprobe *old_p, *list_p;
  640. old_p = get_kprobe(p->addr);
  641. if (unlikely(!old_p))
  642. return -EINVAL;
  643. if (p != old_p) {
  644. list_for_each_entry_rcu(list_p, &old_p->list, list)
  645. if (list_p == p)
  646. /* kprobe p is a valid probe */
  647. goto valid_p;
  648. return -EINVAL;
  649. }
  650. valid_p:
  651. if (old_p == p ||
  652. (old_p->pre_handler == aggr_pre_handler &&
  653. list_is_singular(&old_p->list))) {
  654. /*
  655. * Only probe on the hash list. Disarm only if kprobes are
  656. * enabled and not gone - otherwise, the breakpoint would
  657. * already have been removed. We save on flushing icache.
  658. */
  659. if (kprobe_enabled && !kprobe_gone(old_p))
  660. arch_disarm_kprobe(p);
  661. hlist_del_rcu(&old_p->hlist);
  662. } else {
  663. if (p->break_handler && !kprobe_gone(p))
  664. old_p->break_handler = NULL;
  665. if (p->post_handler && !kprobe_gone(p)) {
  666. list_for_each_entry_rcu(list_p, &old_p->list, list) {
  667. if ((list_p != p) && (list_p->post_handler))
  668. goto noclean;
  669. }
  670. old_p->post_handler = NULL;
  671. }
  672. noclean:
  673. list_del_rcu(&p->list);
  674. }
  675. return 0;
  676. }
  677. static void __kprobes __unregister_kprobe_bottom(struct kprobe *p)
  678. {
  679. struct kprobe *old_p;
  680. if (list_empty(&p->list))
  681. arch_remove_kprobe(p);
  682. else if (list_is_singular(&p->list)) {
  683. /* "p" is the last child of an aggr_kprobe */
  684. old_p = list_entry(p->list.next, struct kprobe, list);
  685. list_del(&p->list);
  686. arch_remove_kprobe(old_p);
  687. kfree(old_p);
  688. }
  689. }
  690. int __kprobes register_kprobes(struct kprobe **kps, int num)
  691. {
  692. int i, ret = 0;
  693. if (num <= 0)
  694. return -EINVAL;
  695. for (i = 0; i < num; i++) {
  696. ret = register_kprobe(kps[i]);
  697. if (ret < 0) {
  698. if (i > 0)
  699. unregister_kprobes(kps, i);
  700. break;
  701. }
  702. }
  703. return ret;
  704. }
  705. void __kprobes unregister_kprobe(struct kprobe *p)
  706. {
  707. unregister_kprobes(&p, 1);
  708. }
  709. void __kprobes unregister_kprobes(struct kprobe **kps, int num)
  710. {
  711. int i;
  712. if (num <= 0)
  713. return;
  714. mutex_lock(&kprobe_mutex);
  715. for (i = 0; i < num; i++)
  716. if (__unregister_kprobe_top(kps[i]) < 0)
  717. kps[i]->addr = NULL;
  718. mutex_unlock(&kprobe_mutex);
  719. synchronize_sched();
  720. for (i = 0; i < num; i++)
  721. if (kps[i]->addr)
  722. __unregister_kprobe_bottom(kps[i]);
  723. }
  724. static struct notifier_block kprobe_exceptions_nb = {
  725. .notifier_call = kprobe_exceptions_notify,
  726. .priority = 0x7fffffff /* we need to be notified first */
  727. };
  728. unsigned long __weak arch_deref_entry_point(void *entry)
  729. {
  730. return (unsigned long)entry;
  731. }
  732. int __kprobes register_jprobes(struct jprobe **jps, int num)
  733. {
  734. struct jprobe *jp;
  735. int ret = 0, i;
  736. if (num <= 0)
  737. return -EINVAL;
  738. for (i = 0; i < num; i++) {
  739. unsigned long addr;
  740. jp = jps[i];
  741. addr = arch_deref_entry_point(jp->entry);
  742. if (!kernel_text_address(addr))
  743. ret = -EINVAL;
  744. else {
  745. /* Todo: Verify probepoint is a function entry point */
  746. jp->kp.pre_handler = setjmp_pre_handler;
  747. jp->kp.break_handler = longjmp_break_handler;
  748. ret = register_kprobe(&jp->kp);
  749. }
  750. if (ret < 0) {
  751. if (i > 0)
  752. unregister_jprobes(jps, i);
  753. break;
  754. }
  755. }
  756. return ret;
  757. }
  758. int __kprobes register_jprobe(struct jprobe *jp)
  759. {
  760. return register_jprobes(&jp, 1);
  761. }
  762. void __kprobes unregister_jprobe(struct jprobe *jp)
  763. {
  764. unregister_jprobes(&jp, 1);
  765. }
  766. void __kprobes unregister_jprobes(struct jprobe **jps, int num)
  767. {
  768. int i;
  769. if (num <= 0)
  770. return;
  771. mutex_lock(&kprobe_mutex);
  772. for (i = 0; i < num; i++)
  773. if (__unregister_kprobe_top(&jps[i]->kp) < 0)
  774. jps[i]->kp.addr = NULL;
  775. mutex_unlock(&kprobe_mutex);
  776. synchronize_sched();
  777. for (i = 0; i < num; i++) {
  778. if (jps[i]->kp.addr)
  779. __unregister_kprobe_bottom(&jps[i]->kp);
  780. }
  781. }
  782. #ifdef CONFIG_KRETPROBES
  783. /*
  784. * This kprobe pre_handler is registered with every kretprobe. When probe
  785. * hits it will set up the return probe.
  786. */
  787. static int __kprobes pre_handler_kretprobe(struct kprobe *p,
  788. struct pt_regs *regs)
  789. {
  790. struct kretprobe *rp = container_of(p, struct kretprobe, kp);
  791. unsigned long hash, flags = 0;
  792. struct kretprobe_instance *ri;
  793. /*TODO: consider to only swap the RA after the last pre_handler fired */
  794. hash = hash_ptr(current, KPROBE_HASH_BITS);
  795. spin_lock_irqsave(&rp->lock, flags);
  796. if (!hlist_empty(&rp->free_instances)) {
  797. ri = hlist_entry(rp->free_instances.first,
  798. struct kretprobe_instance, hlist);
  799. hlist_del(&ri->hlist);
  800. spin_unlock_irqrestore(&rp->lock, flags);
  801. ri->rp = rp;
  802. ri->task = current;
  803. if (rp->entry_handler && rp->entry_handler(ri, regs)) {
  804. spin_unlock_irqrestore(&rp->lock, flags);
  805. return 0;
  806. }
  807. arch_prepare_kretprobe(ri, regs);
  808. /* XXX(hch): why is there no hlist_move_head? */
  809. INIT_HLIST_NODE(&ri->hlist);
  810. kretprobe_table_lock(hash, &flags);
  811. hlist_add_head(&ri->hlist, &kretprobe_inst_table[hash]);
  812. kretprobe_table_unlock(hash, &flags);
  813. } else {
  814. rp->nmissed++;
  815. spin_unlock_irqrestore(&rp->lock, flags);
  816. }
  817. return 0;
  818. }
  819. int __kprobes register_kretprobe(struct kretprobe *rp)
  820. {
  821. int ret = 0;
  822. struct kretprobe_instance *inst;
  823. int i;
  824. void *addr;
  825. if (kretprobe_blacklist_size) {
  826. addr = kprobe_addr(&rp->kp);
  827. if (!addr)
  828. return -EINVAL;
  829. for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
  830. if (kretprobe_blacklist[i].addr == addr)
  831. return -EINVAL;
  832. }
  833. }
  834. rp->kp.pre_handler = pre_handler_kretprobe;
  835. rp->kp.post_handler = NULL;
  836. rp->kp.fault_handler = NULL;
  837. rp->kp.break_handler = NULL;
  838. /* Pre-allocate memory for max kretprobe instances */
  839. if (rp->maxactive <= 0) {
  840. #ifdef CONFIG_PREEMPT
  841. rp->maxactive = max(10, 2 * NR_CPUS);
  842. #else
  843. rp->maxactive = NR_CPUS;
  844. #endif
  845. }
  846. spin_lock_init(&rp->lock);
  847. INIT_HLIST_HEAD(&rp->free_instances);
  848. for (i = 0; i < rp->maxactive; i++) {
  849. inst = kmalloc(sizeof(struct kretprobe_instance) +
  850. rp->data_size, GFP_KERNEL);
  851. if (inst == NULL) {
  852. free_rp_inst(rp);
  853. return -ENOMEM;
  854. }
  855. INIT_HLIST_NODE(&inst->hlist);
  856. hlist_add_head(&inst->hlist, &rp->free_instances);
  857. }
  858. rp->nmissed = 0;
  859. /* Establish function entry probe point */
  860. ret = register_kprobe(&rp->kp);
  861. if (ret != 0)
  862. free_rp_inst(rp);
  863. return ret;
  864. }
  865. int __kprobes register_kretprobes(struct kretprobe **rps, int num)
  866. {
  867. int ret = 0, i;
  868. if (num <= 0)
  869. return -EINVAL;
  870. for (i = 0; i < num; i++) {
  871. ret = register_kretprobe(rps[i]);
  872. if (ret < 0) {
  873. if (i > 0)
  874. unregister_kretprobes(rps, i);
  875. break;
  876. }
  877. }
  878. return ret;
  879. }
  880. void __kprobes unregister_kretprobe(struct kretprobe *rp)
  881. {
  882. unregister_kretprobes(&rp, 1);
  883. }
  884. void __kprobes unregister_kretprobes(struct kretprobe **rps, int num)
  885. {
  886. int i;
  887. if (num <= 0)
  888. return;
  889. mutex_lock(&kprobe_mutex);
  890. for (i = 0; i < num; i++)
  891. if (__unregister_kprobe_top(&rps[i]->kp) < 0)
  892. rps[i]->kp.addr = NULL;
  893. mutex_unlock(&kprobe_mutex);
  894. synchronize_sched();
  895. for (i = 0; i < num; i++) {
  896. if (rps[i]->kp.addr) {
  897. __unregister_kprobe_bottom(&rps[i]->kp);
  898. cleanup_rp_inst(rps[i]);
  899. }
  900. }
  901. }
  902. #else /* CONFIG_KRETPROBES */
  903. int __kprobes register_kretprobe(struct kretprobe *rp)
  904. {
  905. return -ENOSYS;
  906. }
  907. int __kprobes register_kretprobes(struct kretprobe **rps, int num)
  908. {
  909. return -ENOSYS;
  910. }
  911. void __kprobes unregister_kretprobe(struct kretprobe *rp)
  912. {
  913. }
  914. void __kprobes unregister_kretprobes(struct kretprobe **rps, int num)
  915. {
  916. }
  917. static int __kprobes pre_handler_kretprobe(struct kprobe *p,
  918. struct pt_regs *regs)
  919. {
  920. return 0;
  921. }
  922. #endif /* CONFIG_KRETPROBES */
  923. /* Set the kprobe gone and remove its instruction buffer. */
  924. static void __kprobes kill_kprobe(struct kprobe *p)
  925. {
  926. struct kprobe *kp;
  927. p->flags |= KPROBE_FLAG_GONE;
  928. if (p->pre_handler == aggr_pre_handler) {
  929. /*
  930. * If this is an aggr_kprobe, we have to list all the
  931. * chained probes and mark them GONE.
  932. */
  933. list_for_each_entry_rcu(kp, &p->list, list)
  934. kp->flags |= KPROBE_FLAG_GONE;
  935. p->post_handler = NULL;
  936. p->break_handler = NULL;
  937. }
  938. /*
  939. * Here, we can remove insn_slot safely, because no thread calls
  940. * the original probed function (which will be freed soon) any more.
  941. */
  942. arch_remove_kprobe(p);
  943. }
  944. /* Module notifier call back, checking kprobes on the module */
  945. static int __kprobes kprobes_module_callback(struct notifier_block *nb,
  946. unsigned long val, void *data)
  947. {
  948. struct module *mod = data;
  949. struct hlist_head *head;
  950. struct hlist_node *node;
  951. struct kprobe *p;
  952. unsigned int i;
  953. if (val != MODULE_STATE_GOING)
  954. return NOTIFY_DONE;
  955. /*
  956. * module .text section will be freed. We need to
  957. * disable kprobes which have been inserted in the section.
  958. */
  959. mutex_lock(&kprobe_mutex);
  960. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  961. head = &kprobe_table[i];
  962. hlist_for_each_entry_rcu(p, node, head, hlist)
  963. if (within_module_core((unsigned long)p->addr, mod)) {
  964. /*
  965. * The vaddr this probe is installed will soon
  966. * be vfreed buy not synced to disk. Hence,
  967. * disarming the breakpoint isn't needed.
  968. */
  969. kill_kprobe(p);
  970. }
  971. }
  972. mutex_unlock(&kprobe_mutex);
  973. return NOTIFY_DONE;
  974. }
  975. static struct notifier_block kprobe_module_nb = {
  976. .notifier_call = kprobes_module_callback,
  977. .priority = 0
  978. };
  979. static int __init init_kprobes(void)
  980. {
  981. int i, err = 0;
  982. unsigned long offset = 0, size = 0;
  983. char *modname, namebuf[128];
  984. const char *symbol_name;
  985. void *addr;
  986. struct kprobe_blackpoint *kb;
  987. /* FIXME allocate the probe table, currently defined statically */
  988. /* initialize all list heads */
  989. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  990. INIT_HLIST_HEAD(&kprobe_table[i]);
  991. INIT_HLIST_HEAD(&kretprobe_inst_table[i]);
  992. spin_lock_init(&(kretprobe_table_locks[i].lock));
  993. }
  994. /*
  995. * Lookup and populate the kprobe_blacklist.
  996. *
  997. * Unlike the kretprobe blacklist, we'll need to determine
  998. * the range of addresses that belong to the said functions,
  999. * since a kprobe need not necessarily be at the beginning
  1000. * of a function.
  1001. */
  1002. for (kb = kprobe_blacklist; kb->name != NULL; kb++) {
  1003. kprobe_lookup_name(kb->name, addr);
  1004. if (!addr)
  1005. continue;
  1006. kb->start_addr = (unsigned long)addr;
  1007. symbol_name = kallsyms_lookup(kb->start_addr,
  1008. &size, &offset, &modname, namebuf);
  1009. if (!symbol_name)
  1010. kb->range = 0;
  1011. else
  1012. kb->range = size;
  1013. }
  1014. if (kretprobe_blacklist_size) {
  1015. /* lookup the function address from its name */
  1016. for (i = 0; kretprobe_blacklist[i].name != NULL; i++) {
  1017. kprobe_lookup_name(kretprobe_blacklist[i].name,
  1018. kretprobe_blacklist[i].addr);
  1019. if (!kretprobe_blacklist[i].addr)
  1020. printk("kretprobe: lookup failed: %s\n",
  1021. kretprobe_blacklist[i].name);
  1022. }
  1023. }
  1024. /* By default, kprobes are enabled */
  1025. kprobe_enabled = true;
  1026. err = arch_init_kprobes();
  1027. if (!err)
  1028. err = register_die_notifier(&kprobe_exceptions_nb);
  1029. if (!err)
  1030. err = register_module_notifier(&kprobe_module_nb);
  1031. kprobes_initialized = (err == 0);
  1032. if (!err)
  1033. init_test_probes();
  1034. return err;
  1035. }
  1036. #ifdef CONFIG_DEBUG_FS
  1037. static void __kprobes report_probe(struct seq_file *pi, struct kprobe *p,
  1038. const char *sym, int offset,char *modname)
  1039. {
  1040. char *kprobe_type;
  1041. if (p->pre_handler == pre_handler_kretprobe)
  1042. kprobe_type = "r";
  1043. else if (p->pre_handler == setjmp_pre_handler)
  1044. kprobe_type = "j";
  1045. else
  1046. kprobe_type = "k";
  1047. if (sym)
  1048. seq_printf(pi, "%p %s %s+0x%x %s %s\n", p->addr, kprobe_type,
  1049. sym, offset, (modname ? modname : " "),
  1050. (kprobe_gone(p) ? "[GONE]" : ""));
  1051. else
  1052. seq_printf(pi, "%p %s %p %s\n", p->addr, kprobe_type, p->addr,
  1053. (kprobe_gone(p) ? "[GONE]" : ""));
  1054. }
  1055. static void __kprobes *kprobe_seq_start(struct seq_file *f, loff_t *pos)
  1056. {
  1057. return (*pos < KPROBE_TABLE_SIZE) ? pos : NULL;
  1058. }
  1059. static void __kprobes *kprobe_seq_next(struct seq_file *f, void *v, loff_t *pos)
  1060. {
  1061. (*pos)++;
  1062. if (*pos >= KPROBE_TABLE_SIZE)
  1063. return NULL;
  1064. return pos;
  1065. }
  1066. static void __kprobes kprobe_seq_stop(struct seq_file *f, void *v)
  1067. {
  1068. /* Nothing to do */
  1069. }
  1070. static int __kprobes show_kprobe_addr(struct seq_file *pi, void *v)
  1071. {
  1072. struct hlist_head *head;
  1073. struct hlist_node *node;
  1074. struct kprobe *p, *kp;
  1075. const char *sym = NULL;
  1076. unsigned int i = *(loff_t *) v;
  1077. unsigned long offset = 0;
  1078. char *modname, namebuf[128];
  1079. head = &kprobe_table[i];
  1080. preempt_disable();
  1081. hlist_for_each_entry_rcu(p, node, head, hlist) {
  1082. sym = kallsyms_lookup((unsigned long)p->addr, NULL,
  1083. &offset, &modname, namebuf);
  1084. if (p->pre_handler == aggr_pre_handler) {
  1085. list_for_each_entry_rcu(kp, &p->list, list)
  1086. report_probe(pi, kp, sym, offset, modname);
  1087. } else
  1088. report_probe(pi, p, sym, offset, modname);
  1089. }
  1090. preempt_enable();
  1091. return 0;
  1092. }
  1093. static struct seq_operations kprobes_seq_ops = {
  1094. .start = kprobe_seq_start,
  1095. .next = kprobe_seq_next,
  1096. .stop = kprobe_seq_stop,
  1097. .show = show_kprobe_addr
  1098. };
  1099. static int __kprobes kprobes_open(struct inode *inode, struct file *filp)
  1100. {
  1101. return seq_open(filp, &kprobes_seq_ops);
  1102. }
  1103. static struct file_operations debugfs_kprobes_operations = {
  1104. .open = kprobes_open,
  1105. .read = seq_read,
  1106. .llseek = seq_lseek,
  1107. .release = seq_release,
  1108. };
  1109. static void __kprobes enable_all_kprobes(void)
  1110. {
  1111. struct hlist_head *head;
  1112. struct hlist_node *node;
  1113. struct kprobe *p;
  1114. unsigned int i;
  1115. mutex_lock(&kprobe_mutex);
  1116. /* If kprobes are already enabled, just return */
  1117. if (kprobe_enabled)
  1118. goto already_enabled;
  1119. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  1120. head = &kprobe_table[i];
  1121. hlist_for_each_entry_rcu(p, node, head, hlist)
  1122. if (!kprobe_gone(p))
  1123. arch_arm_kprobe(p);
  1124. }
  1125. kprobe_enabled = true;
  1126. printk(KERN_INFO "Kprobes globally enabled\n");
  1127. already_enabled:
  1128. mutex_unlock(&kprobe_mutex);
  1129. return;
  1130. }
  1131. static void __kprobes disable_all_kprobes(void)
  1132. {
  1133. struct hlist_head *head;
  1134. struct hlist_node *node;
  1135. struct kprobe *p;
  1136. unsigned int i;
  1137. mutex_lock(&kprobe_mutex);
  1138. /* If kprobes are already disabled, just return */
  1139. if (!kprobe_enabled)
  1140. goto already_disabled;
  1141. kprobe_enabled = false;
  1142. printk(KERN_INFO "Kprobes globally disabled\n");
  1143. for (i = 0; i < KPROBE_TABLE_SIZE; i++) {
  1144. head = &kprobe_table[i];
  1145. hlist_for_each_entry_rcu(p, node, head, hlist) {
  1146. if (!arch_trampoline_kprobe(p) && !kprobe_gone(p))
  1147. arch_disarm_kprobe(p);
  1148. }
  1149. }
  1150. mutex_unlock(&kprobe_mutex);
  1151. /* Allow all currently running kprobes to complete */
  1152. synchronize_sched();
  1153. return;
  1154. already_disabled:
  1155. mutex_unlock(&kprobe_mutex);
  1156. return;
  1157. }
  1158. /*
  1159. * XXX: The debugfs bool file interface doesn't allow for callbacks
  1160. * when the bool state is switched. We can reuse that facility when
  1161. * available
  1162. */
  1163. static ssize_t read_enabled_file_bool(struct file *file,
  1164. char __user *user_buf, size_t count, loff_t *ppos)
  1165. {
  1166. char buf[3];
  1167. if (kprobe_enabled)
  1168. buf[0] = '1';
  1169. else
  1170. buf[0] = '0';
  1171. buf[1] = '\n';
  1172. buf[2] = 0x00;
  1173. return simple_read_from_buffer(user_buf, count, ppos, buf, 2);
  1174. }
  1175. static ssize_t write_enabled_file_bool(struct file *file,
  1176. const char __user *user_buf, size_t count, loff_t *ppos)
  1177. {
  1178. char buf[32];
  1179. int buf_size;
  1180. buf_size = min(count, (sizeof(buf)-1));
  1181. if (copy_from_user(buf, user_buf, buf_size))
  1182. return -EFAULT;
  1183. switch (buf[0]) {
  1184. case 'y':
  1185. case 'Y':
  1186. case '1':
  1187. enable_all_kprobes();
  1188. break;
  1189. case 'n':
  1190. case 'N':
  1191. case '0':
  1192. disable_all_kprobes();
  1193. break;
  1194. }
  1195. return count;
  1196. }
  1197. static struct file_operations fops_kp = {
  1198. .read = read_enabled_file_bool,
  1199. .write = write_enabled_file_bool,
  1200. };
  1201. static int __kprobes debugfs_kprobe_init(void)
  1202. {
  1203. struct dentry *dir, *file;
  1204. unsigned int value = 1;
  1205. dir = debugfs_create_dir("kprobes", NULL);
  1206. if (!dir)
  1207. return -ENOMEM;
  1208. file = debugfs_create_file("list", 0444, dir, NULL,
  1209. &debugfs_kprobes_operations);
  1210. if (!file) {
  1211. debugfs_remove(dir);
  1212. return -ENOMEM;
  1213. }
  1214. file = debugfs_create_file("enabled", 0600, dir,
  1215. &value, &fops_kp);
  1216. if (!file) {
  1217. debugfs_remove(dir);
  1218. return -ENOMEM;
  1219. }
  1220. return 0;
  1221. }
  1222. late_initcall(debugfs_kprobe_init);
  1223. #endif /* CONFIG_DEBUG_FS */
  1224. module_init(init_kprobes);
  1225. EXPORT_SYMBOL_GPL(register_kprobe);
  1226. EXPORT_SYMBOL_GPL(unregister_kprobe);
  1227. EXPORT_SYMBOL_GPL(register_kprobes);
  1228. EXPORT_SYMBOL_GPL(unregister_kprobes);
  1229. EXPORT_SYMBOL_GPL(register_jprobe);
  1230. EXPORT_SYMBOL_GPL(unregister_jprobe);
  1231. EXPORT_SYMBOL_GPL(register_jprobes);
  1232. EXPORT_SYMBOL_GPL(unregister_jprobes);
  1233. EXPORT_SYMBOL_GPL(jprobe_return);
  1234. EXPORT_SYMBOL_GPL(register_kretprobe);
  1235. EXPORT_SYMBOL_GPL(unregister_kretprobe);
  1236. EXPORT_SYMBOL_GPL(register_kretprobes);
  1237. EXPORT_SYMBOL_GPL(unregister_kretprobes);