kprobes.c 35 KB

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