uprobes.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979
  1. /*
  2. * User-space Probes (UProbes)
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Copyright (C) IBM Corporation, 2008-2012
  19. * Authors:
  20. * Srikar Dronamraju
  21. * Jim Keniston
  22. * Copyright (C) 2011-2012 Red Hat, Inc., Peter Zijlstra <pzijlstr@redhat.com>
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/highmem.h>
  26. #include <linux/pagemap.h> /* read_mapping_page */
  27. #include <linux/slab.h>
  28. #include <linux/sched.h>
  29. #include <linux/export.h>
  30. #include <linux/rmap.h> /* anon_vma_prepare */
  31. #include <linux/mmu_notifier.h> /* set_pte_at_notify */
  32. #include <linux/swap.h> /* try_to_free_swap */
  33. #include <linux/ptrace.h> /* user_enable_single_step */
  34. #include <linux/kdebug.h> /* notifier mechanism */
  35. #include "../../mm/internal.h" /* munlock_vma_page */
  36. #include <linux/percpu-rwsem.h>
  37. #include <linux/task_work.h>
  38. #include <linux/uprobes.h>
  39. #define UINSNS_PER_PAGE (PAGE_SIZE/UPROBE_XOL_SLOT_BYTES)
  40. #define MAX_UPROBE_XOL_SLOTS UINSNS_PER_PAGE
  41. static struct rb_root uprobes_tree = RB_ROOT;
  42. /*
  43. * allows us to skip the uprobe_mmap if there are no uprobe events active
  44. * at this time. Probably a fine grained per inode count is better?
  45. */
  46. #define no_uprobe_events() RB_EMPTY_ROOT(&uprobes_tree)
  47. static DEFINE_SPINLOCK(uprobes_treelock); /* serialize rbtree access */
  48. #define UPROBES_HASH_SZ 13
  49. /* serialize uprobe->pending_list */
  50. static struct mutex uprobes_mmap_mutex[UPROBES_HASH_SZ];
  51. #define uprobes_mmap_hash(v) (&uprobes_mmap_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
  52. static struct percpu_rw_semaphore dup_mmap_sem;
  53. /* Have a copy of original instruction */
  54. #define UPROBE_COPY_INSN 0
  55. struct uprobe {
  56. struct rb_node rb_node; /* node in the rb tree */
  57. atomic_t ref;
  58. struct rw_semaphore register_rwsem;
  59. struct rw_semaphore consumer_rwsem;
  60. struct list_head pending_list;
  61. struct uprobe_consumer *consumers;
  62. struct inode *inode; /* Also hold a ref to inode */
  63. loff_t offset;
  64. unsigned long flags;
  65. /*
  66. * The generic code assumes that it has two members of unknown type
  67. * owned by the arch-specific code:
  68. *
  69. * insn - copy_insn() saves the original instruction here for
  70. * arch_uprobe_analyze_insn().
  71. *
  72. * ixol - potentially modified instruction to execute out of
  73. * line, copied to xol_area by xol_get_insn_slot().
  74. */
  75. struct arch_uprobe arch;
  76. };
  77. struct return_instance {
  78. struct uprobe *uprobe;
  79. unsigned long func;
  80. unsigned long orig_ret_vaddr; /* original return address */
  81. bool chained; /* true, if instance is nested */
  82. struct return_instance *next; /* keep as stack */
  83. };
  84. /*
  85. * Execute out of line area: anonymous executable mapping installed
  86. * by the probed task to execute the copy of the original instruction
  87. * mangled by set_swbp().
  88. *
  89. * On a breakpoint hit, thread contests for a slot. It frees the
  90. * slot after singlestep. Currently a fixed number of slots are
  91. * allocated.
  92. */
  93. struct xol_area {
  94. wait_queue_head_t wq; /* if all slots are busy */
  95. atomic_t slot_count; /* number of in-use slots */
  96. unsigned long *bitmap; /* 0 = free slot */
  97. struct page *page;
  98. /*
  99. * We keep the vma's vm_start rather than a pointer to the vma
  100. * itself. The probed process or a naughty kernel module could make
  101. * the vma go away, and we must handle that reasonably gracefully.
  102. */
  103. unsigned long vaddr; /* Page(s) of instruction slots */
  104. };
  105. /*
  106. * valid_vma: Verify if the specified vma is an executable vma
  107. * Relax restrictions while unregistering: vm_flags might have
  108. * changed after breakpoint was inserted.
  109. * - is_register: indicates if we are in register context.
  110. * - Return 1 if the specified virtual address is in an
  111. * executable vma.
  112. */
  113. static bool valid_vma(struct vm_area_struct *vma, bool is_register)
  114. {
  115. vm_flags_t flags = VM_HUGETLB | VM_MAYEXEC | VM_MAYSHARE;
  116. if (is_register)
  117. flags |= VM_WRITE;
  118. return vma->vm_file && (vma->vm_flags & flags) == VM_MAYEXEC;
  119. }
  120. static unsigned long offset_to_vaddr(struct vm_area_struct *vma, loff_t offset)
  121. {
  122. return vma->vm_start + offset - ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
  123. }
  124. static loff_t vaddr_to_offset(struct vm_area_struct *vma, unsigned long vaddr)
  125. {
  126. return ((loff_t)vma->vm_pgoff << PAGE_SHIFT) + (vaddr - vma->vm_start);
  127. }
  128. /**
  129. * __replace_page - replace page in vma by new page.
  130. * based on replace_page in mm/ksm.c
  131. *
  132. * @vma: vma that holds the pte pointing to page
  133. * @addr: address the old @page is mapped at
  134. * @page: the cowed page we are replacing by kpage
  135. * @kpage: the modified page we replace page by
  136. *
  137. * Returns 0 on success, -EFAULT on failure.
  138. */
  139. static int __replace_page(struct vm_area_struct *vma, unsigned long addr,
  140. struct page *page, struct page *kpage)
  141. {
  142. struct mm_struct *mm = vma->vm_mm;
  143. spinlock_t *ptl;
  144. pte_t *ptep;
  145. int err;
  146. /* For mmu_notifiers */
  147. const unsigned long mmun_start = addr;
  148. const unsigned long mmun_end = addr + PAGE_SIZE;
  149. /* For try_to_free_swap() and munlock_vma_page() below */
  150. lock_page(page);
  151. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  152. err = -EAGAIN;
  153. ptep = page_check_address(page, mm, addr, &ptl, 0);
  154. if (!ptep)
  155. goto unlock;
  156. get_page(kpage);
  157. page_add_new_anon_rmap(kpage, vma, addr);
  158. if (!PageAnon(page)) {
  159. dec_mm_counter(mm, MM_FILEPAGES);
  160. inc_mm_counter(mm, MM_ANONPAGES);
  161. }
  162. flush_cache_page(vma, addr, pte_pfn(*ptep));
  163. ptep_clear_flush(vma, addr, ptep);
  164. set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot));
  165. page_remove_rmap(page);
  166. if (!page_mapped(page))
  167. try_to_free_swap(page);
  168. pte_unmap_unlock(ptep, ptl);
  169. if (vma->vm_flags & VM_LOCKED)
  170. munlock_vma_page(page);
  171. put_page(page);
  172. err = 0;
  173. unlock:
  174. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  175. unlock_page(page);
  176. return err;
  177. }
  178. /**
  179. * is_swbp_insn - check if instruction is breakpoint instruction.
  180. * @insn: instruction to be checked.
  181. * Default implementation of is_swbp_insn
  182. * Returns true if @insn is a breakpoint instruction.
  183. */
  184. bool __weak is_swbp_insn(uprobe_opcode_t *insn)
  185. {
  186. return *insn == UPROBE_SWBP_INSN;
  187. }
  188. /**
  189. * is_trap_insn - check if instruction is breakpoint instruction.
  190. * @insn: instruction to be checked.
  191. * Default implementation of is_trap_insn
  192. * Returns true if @insn is a breakpoint instruction.
  193. *
  194. * This function is needed for the case where an architecture has multiple
  195. * trap instructions (like powerpc).
  196. */
  197. bool __weak is_trap_insn(uprobe_opcode_t *insn)
  198. {
  199. return is_swbp_insn(insn);
  200. }
  201. static void copy_from_page(struct page *page, unsigned long vaddr, void *dst, int len)
  202. {
  203. void *kaddr = kmap_atomic(page);
  204. memcpy(dst, kaddr + (vaddr & ~PAGE_MASK), len);
  205. kunmap_atomic(kaddr);
  206. }
  207. static void copy_to_page(struct page *page, unsigned long vaddr, const void *src, int len)
  208. {
  209. void *kaddr = kmap_atomic(page);
  210. memcpy(kaddr + (vaddr & ~PAGE_MASK), src, len);
  211. kunmap_atomic(kaddr);
  212. }
  213. static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t *new_opcode)
  214. {
  215. uprobe_opcode_t old_opcode;
  216. bool is_swbp;
  217. /*
  218. * Note: We only check if the old_opcode is UPROBE_SWBP_INSN here.
  219. * We do not check if it is any other 'trap variant' which could
  220. * be conditional trap instruction such as the one powerpc supports.
  221. *
  222. * The logic is that we do not care if the underlying instruction
  223. * is a trap variant; uprobes always wins over any other (gdb)
  224. * breakpoint.
  225. */
  226. copy_from_page(page, vaddr, &old_opcode, UPROBE_SWBP_INSN_SIZE);
  227. is_swbp = is_swbp_insn(&old_opcode);
  228. if (is_swbp_insn(new_opcode)) {
  229. if (is_swbp) /* register: already installed? */
  230. return 0;
  231. } else {
  232. if (!is_swbp) /* unregister: was it changed by us? */
  233. return 0;
  234. }
  235. return 1;
  236. }
  237. /*
  238. * NOTE:
  239. * Expect the breakpoint instruction to be the smallest size instruction for
  240. * the architecture. If an arch has variable length instruction and the
  241. * breakpoint instruction is not of the smallest length instruction
  242. * supported by that architecture then we need to modify is_trap_at_addr and
  243. * uprobe_write_opcode accordingly. This would never be a problem for archs
  244. * that have fixed length instructions.
  245. *
  246. * uprobe_write_opcode - write the opcode at a given virtual address.
  247. * @mm: the probed process address space.
  248. * @vaddr: the virtual address to store the opcode.
  249. * @opcode: opcode to be written at @vaddr.
  250. *
  251. * Called with mm->mmap_sem held for write.
  252. * Return 0 (success) or a negative errno.
  253. */
  254. int uprobe_write_opcode(struct mm_struct *mm, unsigned long vaddr,
  255. uprobe_opcode_t opcode)
  256. {
  257. struct page *old_page, *new_page;
  258. struct vm_area_struct *vma;
  259. int ret;
  260. retry:
  261. /* Read the page with vaddr into memory */
  262. ret = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &old_page, &vma);
  263. if (ret <= 0)
  264. return ret;
  265. ret = verify_opcode(old_page, vaddr, &opcode);
  266. if (ret <= 0)
  267. goto put_old;
  268. ret = anon_vma_prepare(vma);
  269. if (ret)
  270. goto put_old;
  271. ret = -ENOMEM;
  272. new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vaddr);
  273. if (!new_page)
  274. goto put_old;
  275. if (mem_cgroup_charge_anon(new_page, mm, GFP_KERNEL))
  276. goto put_new;
  277. __SetPageUptodate(new_page);
  278. copy_highpage(new_page, old_page);
  279. copy_to_page(new_page, vaddr, &opcode, UPROBE_SWBP_INSN_SIZE);
  280. ret = __replace_page(vma, vaddr, old_page, new_page);
  281. if (ret)
  282. mem_cgroup_uncharge_page(new_page);
  283. put_new:
  284. page_cache_release(new_page);
  285. put_old:
  286. put_page(old_page);
  287. if (unlikely(ret == -EAGAIN))
  288. goto retry;
  289. return ret;
  290. }
  291. /**
  292. * set_swbp - store breakpoint at a given address.
  293. * @auprobe: arch specific probepoint information.
  294. * @mm: the probed process address space.
  295. * @vaddr: the virtual address to insert the opcode.
  296. *
  297. * For mm @mm, store the breakpoint instruction at @vaddr.
  298. * Return 0 (success) or a negative errno.
  299. */
  300. int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
  301. {
  302. return uprobe_write_opcode(mm, vaddr, UPROBE_SWBP_INSN);
  303. }
  304. /**
  305. * set_orig_insn - Restore the original instruction.
  306. * @mm: the probed process address space.
  307. * @auprobe: arch specific probepoint information.
  308. * @vaddr: the virtual address to insert the opcode.
  309. *
  310. * For mm @mm, restore the original opcode (opcode) at @vaddr.
  311. * Return 0 (success) or a negative errno.
  312. */
  313. int __weak
  314. set_orig_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
  315. {
  316. return uprobe_write_opcode(mm, vaddr, *(uprobe_opcode_t *)&auprobe->insn);
  317. }
  318. static int match_uprobe(struct uprobe *l, struct uprobe *r)
  319. {
  320. if (l->inode < r->inode)
  321. return -1;
  322. if (l->inode > r->inode)
  323. return 1;
  324. if (l->offset < r->offset)
  325. return -1;
  326. if (l->offset > r->offset)
  327. return 1;
  328. return 0;
  329. }
  330. static struct uprobe *__find_uprobe(struct inode *inode, loff_t offset)
  331. {
  332. struct uprobe u = { .inode = inode, .offset = offset };
  333. struct rb_node *n = uprobes_tree.rb_node;
  334. struct uprobe *uprobe;
  335. int match;
  336. while (n) {
  337. uprobe = rb_entry(n, struct uprobe, rb_node);
  338. match = match_uprobe(&u, uprobe);
  339. if (!match) {
  340. atomic_inc(&uprobe->ref);
  341. return uprobe;
  342. }
  343. if (match < 0)
  344. n = n->rb_left;
  345. else
  346. n = n->rb_right;
  347. }
  348. return NULL;
  349. }
  350. /*
  351. * Find a uprobe corresponding to a given inode:offset
  352. * Acquires uprobes_treelock
  353. */
  354. static struct uprobe *find_uprobe(struct inode *inode, loff_t offset)
  355. {
  356. struct uprobe *uprobe;
  357. spin_lock(&uprobes_treelock);
  358. uprobe = __find_uprobe(inode, offset);
  359. spin_unlock(&uprobes_treelock);
  360. return uprobe;
  361. }
  362. static struct uprobe *__insert_uprobe(struct uprobe *uprobe)
  363. {
  364. struct rb_node **p = &uprobes_tree.rb_node;
  365. struct rb_node *parent = NULL;
  366. struct uprobe *u;
  367. int match;
  368. while (*p) {
  369. parent = *p;
  370. u = rb_entry(parent, struct uprobe, rb_node);
  371. match = match_uprobe(uprobe, u);
  372. if (!match) {
  373. atomic_inc(&u->ref);
  374. return u;
  375. }
  376. if (match < 0)
  377. p = &parent->rb_left;
  378. else
  379. p = &parent->rb_right;
  380. }
  381. u = NULL;
  382. rb_link_node(&uprobe->rb_node, parent, p);
  383. rb_insert_color(&uprobe->rb_node, &uprobes_tree);
  384. /* get access + creation ref */
  385. atomic_set(&uprobe->ref, 2);
  386. return u;
  387. }
  388. /*
  389. * Acquire uprobes_treelock.
  390. * Matching uprobe already exists in rbtree;
  391. * increment (access refcount) and return the matching uprobe.
  392. *
  393. * No matching uprobe; insert the uprobe in rb_tree;
  394. * get a double refcount (access + creation) and return NULL.
  395. */
  396. static struct uprobe *insert_uprobe(struct uprobe *uprobe)
  397. {
  398. struct uprobe *u;
  399. spin_lock(&uprobes_treelock);
  400. u = __insert_uprobe(uprobe);
  401. spin_unlock(&uprobes_treelock);
  402. return u;
  403. }
  404. static void put_uprobe(struct uprobe *uprobe)
  405. {
  406. if (atomic_dec_and_test(&uprobe->ref))
  407. kfree(uprobe);
  408. }
  409. static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset)
  410. {
  411. struct uprobe *uprobe, *cur_uprobe;
  412. uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL);
  413. if (!uprobe)
  414. return NULL;
  415. uprobe->inode = igrab(inode);
  416. uprobe->offset = offset;
  417. init_rwsem(&uprobe->register_rwsem);
  418. init_rwsem(&uprobe->consumer_rwsem);
  419. /* add to uprobes_tree, sorted on inode:offset */
  420. cur_uprobe = insert_uprobe(uprobe);
  421. /* a uprobe exists for this inode:offset combination */
  422. if (cur_uprobe) {
  423. kfree(uprobe);
  424. uprobe = cur_uprobe;
  425. iput(inode);
  426. }
  427. return uprobe;
  428. }
  429. static void consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc)
  430. {
  431. down_write(&uprobe->consumer_rwsem);
  432. uc->next = uprobe->consumers;
  433. uprobe->consumers = uc;
  434. up_write(&uprobe->consumer_rwsem);
  435. }
  436. /*
  437. * For uprobe @uprobe, delete the consumer @uc.
  438. * Return true if the @uc is deleted successfully
  439. * or return false.
  440. */
  441. static bool consumer_del(struct uprobe *uprobe, struct uprobe_consumer *uc)
  442. {
  443. struct uprobe_consumer **con;
  444. bool ret = false;
  445. down_write(&uprobe->consumer_rwsem);
  446. for (con = &uprobe->consumers; *con; con = &(*con)->next) {
  447. if (*con == uc) {
  448. *con = uc->next;
  449. ret = true;
  450. break;
  451. }
  452. }
  453. up_write(&uprobe->consumer_rwsem);
  454. return ret;
  455. }
  456. static int __copy_insn(struct address_space *mapping, struct file *filp,
  457. void *insn, int nbytes, loff_t offset)
  458. {
  459. struct page *page;
  460. if (!mapping->a_ops->readpage)
  461. return -EIO;
  462. /*
  463. * Ensure that the page that has the original instruction is
  464. * populated and in page-cache.
  465. */
  466. page = read_mapping_page(mapping, offset >> PAGE_CACHE_SHIFT, filp);
  467. if (IS_ERR(page))
  468. return PTR_ERR(page);
  469. copy_from_page(page, offset, insn, nbytes);
  470. page_cache_release(page);
  471. return 0;
  472. }
  473. static int copy_insn(struct uprobe *uprobe, struct file *filp)
  474. {
  475. struct address_space *mapping = uprobe->inode->i_mapping;
  476. loff_t offs = uprobe->offset;
  477. void *insn = &uprobe->arch.insn;
  478. int size = sizeof(uprobe->arch.insn);
  479. int len, err = -EIO;
  480. /* Copy only available bytes, -EIO if nothing was read */
  481. do {
  482. if (offs >= i_size_read(uprobe->inode))
  483. break;
  484. len = min_t(int, size, PAGE_SIZE - (offs & ~PAGE_MASK));
  485. err = __copy_insn(mapping, filp, insn, len, offs);
  486. if (err)
  487. break;
  488. insn += len;
  489. offs += len;
  490. size -= len;
  491. } while (size);
  492. return err;
  493. }
  494. static int prepare_uprobe(struct uprobe *uprobe, struct file *file,
  495. struct mm_struct *mm, unsigned long vaddr)
  496. {
  497. int ret = 0;
  498. if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
  499. return ret;
  500. /* TODO: move this into _register, until then we abuse this sem. */
  501. down_write(&uprobe->consumer_rwsem);
  502. if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
  503. goto out;
  504. ret = copy_insn(uprobe, file);
  505. if (ret)
  506. goto out;
  507. ret = -ENOTSUPP;
  508. if (is_trap_insn((uprobe_opcode_t *)&uprobe->arch.insn))
  509. goto out;
  510. ret = arch_uprobe_analyze_insn(&uprobe->arch, mm, vaddr);
  511. if (ret)
  512. goto out;
  513. /* uprobe_write_opcode() assumes we don't cross page boundary */
  514. BUG_ON((uprobe->offset & ~PAGE_MASK) +
  515. UPROBE_SWBP_INSN_SIZE > PAGE_SIZE);
  516. smp_wmb(); /* pairs with rmb() in find_active_uprobe() */
  517. set_bit(UPROBE_COPY_INSN, &uprobe->flags);
  518. out:
  519. up_write(&uprobe->consumer_rwsem);
  520. return ret;
  521. }
  522. static inline bool consumer_filter(struct uprobe_consumer *uc,
  523. enum uprobe_filter_ctx ctx, struct mm_struct *mm)
  524. {
  525. return !uc->filter || uc->filter(uc, ctx, mm);
  526. }
  527. static bool filter_chain(struct uprobe *uprobe,
  528. enum uprobe_filter_ctx ctx, struct mm_struct *mm)
  529. {
  530. struct uprobe_consumer *uc;
  531. bool ret = false;
  532. down_read(&uprobe->consumer_rwsem);
  533. for (uc = uprobe->consumers; uc; uc = uc->next) {
  534. ret = consumer_filter(uc, ctx, mm);
  535. if (ret)
  536. break;
  537. }
  538. up_read(&uprobe->consumer_rwsem);
  539. return ret;
  540. }
  541. static int
  542. install_breakpoint(struct uprobe *uprobe, struct mm_struct *mm,
  543. struct vm_area_struct *vma, unsigned long vaddr)
  544. {
  545. bool first_uprobe;
  546. int ret;
  547. ret = prepare_uprobe(uprobe, vma->vm_file, mm, vaddr);
  548. if (ret)
  549. return ret;
  550. /*
  551. * set MMF_HAS_UPROBES in advance for uprobe_pre_sstep_notifier(),
  552. * the task can hit this breakpoint right after __replace_page().
  553. */
  554. first_uprobe = !test_bit(MMF_HAS_UPROBES, &mm->flags);
  555. if (first_uprobe)
  556. set_bit(MMF_HAS_UPROBES, &mm->flags);
  557. ret = set_swbp(&uprobe->arch, mm, vaddr);
  558. if (!ret)
  559. clear_bit(MMF_RECALC_UPROBES, &mm->flags);
  560. else if (first_uprobe)
  561. clear_bit(MMF_HAS_UPROBES, &mm->flags);
  562. return ret;
  563. }
  564. static int
  565. remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, unsigned long vaddr)
  566. {
  567. set_bit(MMF_RECALC_UPROBES, &mm->flags);
  568. return set_orig_insn(&uprobe->arch, mm, vaddr);
  569. }
  570. static inline bool uprobe_is_active(struct uprobe *uprobe)
  571. {
  572. return !RB_EMPTY_NODE(&uprobe->rb_node);
  573. }
  574. /*
  575. * There could be threads that have already hit the breakpoint. They
  576. * will recheck the current insn and restart if find_uprobe() fails.
  577. * See find_active_uprobe().
  578. */
  579. static void delete_uprobe(struct uprobe *uprobe)
  580. {
  581. if (WARN_ON(!uprobe_is_active(uprobe)))
  582. return;
  583. spin_lock(&uprobes_treelock);
  584. rb_erase(&uprobe->rb_node, &uprobes_tree);
  585. spin_unlock(&uprobes_treelock);
  586. RB_CLEAR_NODE(&uprobe->rb_node); /* for uprobe_is_active() */
  587. iput(uprobe->inode);
  588. put_uprobe(uprobe);
  589. }
  590. struct map_info {
  591. struct map_info *next;
  592. struct mm_struct *mm;
  593. unsigned long vaddr;
  594. };
  595. static inline struct map_info *free_map_info(struct map_info *info)
  596. {
  597. struct map_info *next = info->next;
  598. kfree(info);
  599. return next;
  600. }
  601. static struct map_info *
  602. build_map_info(struct address_space *mapping, loff_t offset, bool is_register)
  603. {
  604. unsigned long pgoff = offset >> PAGE_SHIFT;
  605. struct vm_area_struct *vma;
  606. struct map_info *curr = NULL;
  607. struct map_info *prev = NULL;
  608. struct map_info *info;
  609. int more = 0;
  610. again:
  611. mutex_lock(&mapping->i_mmap_mutex);
  612. vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
  613. if (!valid_vma(vma, is_register))
  614. continue;
  615. if (!prev && !more) {
  616. /*
  617. * Needs GFP_NOWAIT to avoid i_mmap_mutex recursion through
  618. * reclaim. This is optimistic, no harm done if it fails.
  619. */
  620. prev = kmalloc(sizeof(struct map_info),
  621. GFP_NOWAIT | __GFP_NOMEMALLOC | __GFP_NOWARN);
  622. if (prev)
  623. prev->next = NULL;
  624. }
  625. if (!prev) {
  626. more++;
  627. continue;
  628. }
  629. if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
  630. continue;
  631. info = prev;
  632. prev = prev->next;
  633. info->next = curr;
  634. curr = info;
  635. info->mm = vma->vm_mm;
  636. info->vaddr = offset_to_vaddr(vma, offset);
  637. }
  638. mutex_unlock(&mapping->i_mmap_mutex);
  639. if (!more)
  640. goto out;
  641. prev = curr;
  642. while (curr) {
  643. mmput(curr->mm);
  644. curr = curr->next;
  645. }
  646. do {
  647. info = kmalloc(sizeof(struct map_info), GFP_KERNEL);
  648. if (!info) {
  649. curr = ERR_PTR(-ENOMEM);
  650. goto out;
  651. }
  652. info->next = prev;
  653. prev = info;
  654. } while (--more);
  655. goto again;
  656. out:
  657. while (prev)
  658. prev = free_map_info(prev);
  659. return curr;
  660. }
  661. static int
  662. register_for_each_vma(struct uprobe *uprobe, struct uprobe_consumer *new)
  663. {
  664. bool is_register = !!new;
  665. struct map_info *info;
  666. int err = 0;
  667. percpu_down_write(&dup_mmap_sem);
  668. info = build_map_info(uprobe->inode->i_mapping,
  669. uprobe->offset, is_register);
  670. if (IS_ERR(info)) {
  671. err = PTR_ERR(info);
  672. goto out;
  673. }
  674. while (info) {
  675. struct mm_struct *mm = info->mm;
  676. struct vm_area_struct *vma;
  677. if (err && is_register)
  678. goto free;
  679. down_write(&mm->mmap_sem);
  680. vma = find_vma(mm, info->vaddr);
  681. if (!vma || !valid_vma(vma, is_register) ||
  682. file_inode(vma->vm_file) != uprobe->inode)
  683. goto unlock;
  684. if (vma->vm_start > info->vaddr ||
  685. vaddr_to_offset(vma, info->vaddr) != uprobe->offset)
  686. goto unlock;
  687. if (is_register) {
  688. /* consult only the "caller", new consumer. */
  689. if (consumer_filter(new,
  690. UPROBE_FILTER_REGISTER, mm))
  691. err = install_breakpoint(uprobe, mm, vma, info->vaddr);
  692. } else if (test_bit(MMF_HAS_UPROBES, &mm->flags)) {
  693. if (!filter_chain(uprobe,
  694. UPROBE_FILTER_UNREGISTER, mm))
  695. err |= remove_breakpoint(uprobe, mm, info->vaddr);
  696. }
  697. unlock:
  698. up_write(&mm->mmap_sem);
  699. free:
  700. mmput(mm);
  701. info = free_map_info(info);
  702. }
  703. out:
  704. percpu_up_write(&dup_mmap_sem);
  705. return err;
  706. }
  707. static int __uprobe_register(struct uprobe *uprobe, struct uprobe_consumer *uc)
  708. {
  709. consumer_add(uprobe, uc);
  710. return register_for_each_vma(uprobe, uc);
  711. }
  712. static void __uprobe_unregister(struct uprobe *uprobe, struct uprobe_consumer *uc)
  713. {
  714. int err;
  715. if (!consumer_del(uprobe, uc)) /* WARN? */
  716. return;
  717. err = register_for_each_vma(uprobe, NULL);
  718. /* TODO : cant unregister? schedule a worker thread */
  719. if (!uprobe->consumers && !err)
  720. delete_uprobe(uprobe);
  721. }
  722. /*
  723. * uprobe_register - register a probe
  724. * @inode: the file in which the probe has to be placed.
  725. * @offset: offset from the start of the file.
  726. * @uc: information on howto handle the probe..
  727. *
  728. * Apart from the access refcount, uprobe_register() takes a creation
  729. * refcount (thro alloc_uprobe) if and only if this @uprobe is getting
  730. * inserted into the rbtree (i.e first consumer for a @inode:@offset
  731. * tuple). Creation refcount stops uprobe_unregister from freeing the
  732. * @uprobe even before the register operation is complete. Creation
  733. * refcount is released when the last @uc for the @uprobe
  734. * unregisters.
  735. *
  736. * Return errno if it cannot successully install probes
  737. * else return 0 (success)
  738. */
  739. int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
  740. {
  741. struct uprobe *uprobe;
  742. int ret;
  743. /* Uprobe must have at least one set consumer */
  744. if (!uc->handler && !uc->ret_handler)
  745. return -EINVAL;
  746. /* Racy, just to catch the obvious mistakes */
  747. if (offset > i_size_read(inode))
  748. return -EINVAL;
  749. retry:
  750. uprobe = alloc_uprobe(inode, offset);
  751. if (!uprobe)
  752. return -ENOMEM;
  753. /*
  754. * We can race with uprobe_unregister()->delete_uprobe().
  755. * Check uprobe_is_active() and retry if it is false.
  756. */
  757. down_write(&uprobe->register_rwsem);
  758. ret = -EAGAIN;
  759. if (likely(uprobe_is_active(uprobe))) {
  760. ret = __uprobe_register(uprobe, uc);
  761. if (ret)
  762. __uprobe_unregister(uprobe, uc);
  763. }
  764. up_write(&uprobe->register_rwsem);
  765. put_uprobe(uprobe);
  766. if (unlikely(ret == -EAGAIN))
  767. goto retry;
  768. return ret;
  769. }
  770. EXPORT_SYMBOL_GPL(uprobe_register);
  771. /*
  772. * uprobe_apply - unregister a already registered probe.
  773. * @inode: the file in which the probe has to be removed.
  774. * @offset: offset from the start of the file.
  775. * @uc: consumer which wants to add more or remove some breakpoints
  776. * @add: add or remove the breakpoints
  777. */
  778. int uprobe_apply(struct inode *inode, loff_t offset,
  779. struct uprobe_consumer *uc, bool add)
  780. {
  781. struct uprobe *uprobe;
  782. struct uprobe_consumer *con;
  783. int ret = -ENOENT;
  784. uprobe = find_uprobe(inode, offset);
  785. if (!uprobe)
  786. return ret;
  787. down_write(&uprobe->register_rwsem);
  788. for (con = uprobe->consumers; con && con != uc ; con = con->next)
  789. ;
  790. if (con)
  791. ret = register_for_each_vma(uprobe, add ? uc : NULL);
  792. up_write(&uprobe->register_rwsem);
  793. put_uprobe(uprobe);
  794. return ret;
  795. }
  796. /*
  797. * uprobe_unregister - unregister a already registered probe.
  798. * @inode: the file in which the probe has to be removed.
  799. * @offset: offset from the start of the file.
  800. * @uc: identify which probe if multiple probes are colocated.
  801. */
  802. void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
  803. {
  804. struct uprobe *uprobe;
  805. uprobe = find_uprobe(inode, offset);
  806. if (!uprobe)
  807. return;
  808. down_write(&uprobe->register_rwsem);
  809. __uprobe_unregister(uprobe, uc);
  810. up_write(&uprobe->register_rwsem);
  811. put_uprobe(uprobe);
  812. }
  813. EXPORT_SYMBOL_GPL(uprobe_unregister);
  814. static int unapply_uprobe(struct uprobe *uprobe, struct mm_struct *mm)
  815. {
  816. struct vm_area_struct *vma;
  817. int err = 0;
  818. down_read(&mm->mmap_sem);
  819. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  820. unsigned long vaddr;
  821. loff_t offset;
  822. if (!valid_vma(vma, false) ||
  823. file_inode(vma->vm_file) != uprobe->inode)
  824. continue;
  825. offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
  826. if (uprobe->offset < offset ||
  827. uprobe->offset >= offset + vma->vm_end - vma->vm_start)
  828. continue;
  829. vaddr = offset_to_vaddr(vma, uprobe->offset);
  830. err |= remove_breakpoint(uprobe, mm, vaddr);
  831. }
  832. up_read(&mm->mmap_sem);
  833. return err;
  834. }
  835. static struct rb_node *
  836. find_node_in_range(struct inode *inode, loff_t min, loff_t max)
  837. {
  838. struct rb_node *n = uprobes_tree.rb_node;
  839. while (n) {
  840. struct uprobe *u = rb_entry(n, struct uprobe, rb_node);
  841. if (inode < u->inode) {
  842. n = n->rb_left;
  843. } else if (inode > u->inode) {
  844. n = n->rb_right;
  845. } else {
  846. if (max < u->offset)
  847. n = n->rb_left;
  848. else if (min > u->offset)
  849. n = n->rb_right;
  850. else
  851. break;
  852. }
  853. }
  854. return n;
  855. }
  856. /*
  857. * For a given range in vma, build a list of probes that need to be inserted.
  858. */
  859. static void build_probe_list(struct inode *inode,
  860. struct vm_area_struct *vma,
  861. unsigned long start, unsigned long end,
  862. struct list_head *head)
  863. {
  864. loff_t min, max;
  865. struct rb_node *n, *t;
  866. struct uprobe *u;
  867. INIT_LIST_HEAD(head);
  868. min = vaddr_to_offset(vma, start);
  869. max = min + (end - start) - 1;
  870. spin_lock(&uprobes_treelock);
  871. n = find_node_in_range(inode, min, max);
  872. if (n) {
  873. for (t = n; t; t = rb_prev(t)) {
  874. u = rb_entry(t, struct uprobe, rb_node);
  875. if (u->inode != inode || u->offset < min)
  876. break;
  877. list_add(&u->pending_list, head);
  878. atomic_inc(&u->ref);
  879. }
  880. for (t = n; (t = rb_next(t)); ) {
  881. u = rb_entry(t, struct uprobe, rb_node);
  882. if (u->inode != inode || u->offset > max)
  883. break;
  884. list_add(&u->pending_list, head);
  885. atomic_inc(&u->ref);
  886. }
  887. }
  888. spin_unlock(&uprobes_treelock);
  889. }
  890. /*
  891. * Called from mmap_region/vma_adjust with mm->mmap_sem acquired.
  892. *
  893. * Currently we ignore all errors and always return 0, the callers
  894. * can't handle the failure anyway.
  895. */
  896. int uprobe_mmap(struct vm_area_struct *vma)
  897. {
  898. struct list_head tmp_list;
  899. struct uprobe *uprobe, *u;
  900. struct inode *inode;
  901. if (no_uprobe_events() || !valid_vma(vma, true))
  902. return 0;
  903. inode = file_inode(vma->vm_file);
  904. if (!inode)
  905. return 0;
  906. mutex_lock(uprobes_mmap_hash(inode));
  907. build_probe_list(inode, vma, vma->vm_start, vma->vm_end, &tmp_list);
  908. /*
  909. * We can race with uprobe_unregister(), this uprobe can be already
  910. * removed. But in this case filter_chain() must return false, all
  911. * consumers have gone away.
  912. */
  913. list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
  914. if (!fatal_signal_pending(current) &&
  915. filter_chain(uprobe, UPROBE_FILTER_MMAP, vma->vm_mm)) {
  916. unsigned long vaddr = offset_to_vaddr(vma, uprobe->offset);
  917. install_breakpoint(uprobe, vma->vm_mm, vma, vaddr);
  918. }
  919. put_uprobe(uprobe);
  920. }
  921. mutex_unlock(uprobes_mmap_hash(inode));
  922. return 0;
  923. }
  924. static bool
  925. vma_has_uprobes(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  926. {
  927. loff_t min, max;
  928. struct inode *inode;
  929. struct rb_node *n;
  930. inode = file_inode(vma->vm_file);
  931. min = vaddr_to_offset(vma, start);
  932. max = min + (end - start) - 1;
  933. spin_lock(&uprobes_treelock);
  934. n = find_node_in_range(inode, min, max);
  935. spin_unlock(&uprobes_treelock);
  936. return !!n;
  937. }
  938. /*
  939. * Called in context of a munmap of a vma.
  940. */
  941. void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  942. {
  943. if (no_uprobe_events() || !valid_vma(vma, false))
  944. return;
  945. if (!atomic_read(&vma->vm_mm->mm_users)) /* called by mmput() ? */
  946. return;
  947. if (!test_bit(MMF_HAS_UPROBES, &vma->vm_mm->flags) ||
  948. test_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags))
  949. return;
  950. if (vma_has_uprobes(vma, start, end))
  951. set_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags);
  952. }
  953. /* Slot allocation for XOL */
  954. static int xol_add_vma(struct mm_struct *mm, struct xol_area *area)
  955. {
  956. int ret = -EALREADY;
  957. down_write(&mm->mmap_sem);
  958. if (mm->uprobes_state.xol_area)
  959. goto fail;
  960. if (!area->vaddr) {
  961. /* Try to map as high as possible, this is only a hint. */
  962. area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE,
  963. PAGE_SIZE, 0, 0);
  964. if (area->vaddr & ~PAGE_MASK) {
  965. ret = area->vaddr;
  966. goto fail;
  967. }
  968. }
  969. ret = install_special_mapping(mm, area->vaddr, PAGE_SIZE,
  970. VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO, &area->page);
  971. if (ret)
  972. goto fail;
  973. smp_wmb(); /* pairs with get_xol_area() */
  974. mm->uprobes_state.xol_area = area;
  975. fail:
  976. up_write(&mm->mmap_sem);
  977. return ret;
  978. }
  979. static struct xol_area *__create_xol_area(unsigned long vaddr)
  980. {
  981. struct mm_struct *mm = current->mm;
  982. uprobe_opcode_t insn = UPROBE_SWBP_INSN;
  983. struct xol_area *area;
  984. area = kmalloc(sizeof(*area), GFP_KERNEL);
  985. if (unlikely(!area))
  986. goto out;
  987. area->bitmap = kzalloc(BITS_TO_LONGS(UINSNS_PER_PAGE) * sizeof(long), GFP_KERNEL);
  988. if (!area->bitmap)
  989. goto free_area;
  990. area->page = alloc_page(GFP_HIGHUSER);
  991. if (!area->page)
  992. goto free_bitmap;
  993. area->vaddr = vaddr;
  994. init_waitqueue_head(&area->wq);
  995. /* Reserve the 1st slot for get_trampoline_vaddr() */
  996. set_bit(0, area->bitmap);
  997. atomic_set(&area->slot_count, 1);
  998. copy_to_page(area->page, 0, &insn, UPROBE_SWBP_INSN_SIZE);
  999. if (!xol_add_vma(mm, area))
  1000. return area;
  1001. __free_page(area->page);
  1002. free_bitmap:
  1003. kfree(area->bitmap);
  1004. free_area:
  1005. kfree(area);
  1006. out:
  1007. return NULL;
  1008. }
  1009. /*
  1010. * get_xol_area - Allocate process's xol_area if necessary.
  1011. * This area will be used for storing instructions for execution out of line.
  1012. *
  1013. * Returns the allocated area or NULL.
  1014. */
  1015. static struct xol_area *get_xol_area(void)
  1016. {
  1017. struct mm_struct *mm = current->mm;
  1018. struct xol_area *area;
  1019. if (!mm->uprobes_state.xol_area)
  1020. __create_xol_area(0);
  1021. area = mm->uprobes_state.xol_area;
  1022. smp_read_barrier_depends(); /* pairs with wmb in xol_add_vma() */
  1023. return area;
  1024. }
  1025. /*
  1026. * uprobe_clear_state - Free the area allocated for slots.
  1027. */
  1028. void uprobe_clear_state(struct mm_struct *mm)
  1029. {
  1030. struct xol_area *area = mm->uprobes_state.xol_area;
  1031. if (!area)
  1032. return;
  1033. put_page(area->page);
  1034. kfree(area->bitmap);
  1035. kfree(area);
  1036. }
  1037. void uprobe_start_dup_mmap(void)
  1038. {
  1039. percpu_down_read(&dup_mmap_sem);
  1040. }
  1041. void uprobe_end_dup_mmap(void)
  1042. {
  1043. percpu_up_read(&dup_mmap_sem);
  1044. }
  1045. void uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm)
  1046. {
  1047. newmm->uprobes_state.xol_area = NULL;
  1048. if (test_bit(MMF_HAS_UPROBES, &oldmm->flags)) {
  1049. set_bit(MMF_HAS_UPROBES, &newmm->flags);
  1050. /* unconditionally, dup_mmap() skips VM_DONTCOPY vmas */
  1051. set_bit(MMF_RECALC_UPROBES, &newmm->flags);
  1052. }
  1053. }
  1054. /*
  1055. * - search for a free slot.
  1056. */
  1057. static unsigned long xol_take_insn_slot(struct xol_area *area)
  1058. {
  1059. unsigned long slot_addr;
  1060. int slot_nr;
  1061. do {
  1062. slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE);
  1063. if (slot_nr < UINSNS_PER_PAGE) {
  1064. if (!test_and_set_bit(slot_nr, area->bitmap))
  1065. break;
  1066. slot_nr = UINSNS_PER_PAGE;
  1067. continue;
  1068. }
  1069. wait_event(area->wq, (atomic_read(&area->slot_count) < UINSNS_PER_PAGE));
  1070. } while (slot_nr >= UINSNS_PER_PAGE);
  1071. slot_addr = area->vaddr + (slot_nr * UPROBE_XOL_SLOT_BYTES);
  1072. atomic_inc(&area->slot_count);
  1073. return slot_addr;
  1074. }
  1075. /*
  1076. * xol_get_insn_slot - allocate a slot for xol.
  1077. * Returns the allocated slot address or 0.
  1078. */
  1079. static unsigned long xol_get_insn_slot(struct uprobe *uprobe)
  1080. {
  1081. struct xol_area *area;
  1082. unsigned long xol_vaddr;
  1083. area = get_xol_area();
  1084. if (!area)
  1085. return 0;
  1086. xol_vaddr = xol_take_insn_slot(area);
  1087. if (unlikely(!xol_vaddr))
  1088. return 0;
  1089. /* Initialize the slot */
  1090. copy_to_page(area->page, xol_vaddr,
  1091. &uprobe->arch.ixol, sizeof(uprobe->arch.ixol));
  1092. /*
  1093. * We probably need flush_icache_user_range() but it needs vma.
  1094. * This should work on supported architectures too.
  1095. */
  1096. flush_dcache_page(area->page);
  1097. return xol_vaddr;
  1098. }
  1099. /*
  1100. * xol_free_insn_slot - If slot was earlier allocated by
  1101. * @xol_get_insn_slot(), make the slot available for
  1102. * subsequent requests.
  1103. */
  1104. static void xol_free_insn_slot(struct task_struct *tsk)
  1105. {
  1106. struct xol_area *area;
  1107. unsigned long vma_end;
  1108. unsigned long slot_addr;
  1109. if (!tsk->mm || !tsk->mm->uprobes_state.xol_area || !tsk->utask)
  1110. return;
  1111. slot_addr = tsk->utask->xol_vaddr;
  1112. if (unlikely(!slot_addr))
  1113. return;
  1114. area = tsk->mm->uprobes_state.xol_area;
  1115. vma_end = area->vaddr + PAGE_SIZE;
  1116. if (area->vaddr <= slot_addr && slot_addr < vma_end) {
  1117. unsigned long offset;
  1118. int slot_nr;
  1119. offset = slot_addr - area->vaddr;
  1120. slot_nr = offset / UPROBE_XOL_SLOT_BYTES;
  1121. if (slot_nr >= UINSNS_PER_PAGE)
  1122. return;
  1123. clear_bit(slot_nr, area->bitmap);
  1124. atomic_dec(&area->slot_count);
  1125. if (waitqueue_active(&area->wq))
  1126. wake_up(&area->wq);
  1127. tsk->utask->xol_vaddr = 0;
  1128. }
  1129. }
  1130. /**
  1131. * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
  1132. * @regs: Reflects the saved state of the task after it has hit a breakpoint
  1133. * instruction.
  1134. * Return the address of the breakpoint instruction.
  1135. */
  1136. unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs)
  1137. {
  1138. return instruction_pointer(regs) - UPROBE_SWBP_INSN_SIZE;
  1139. }
  1140. unsigned long uprobe_get_trap_addr(struct pt_regs *regs)
  1141. {
  1142. struct uprobe_task *utask = current->utask;
  1143. if (unlikely(utask && utask->active_uprobe))
  1144. return utask->vaddr;
  1145. return instruction_pointer(regs);
  1146. }
  1147. /*
  1148. * Called with no locks held.
  1149. * Called in context of a exiting or a exec-ing thread.
  1150. */
  1151. void uprobe_free_utask(struct task_struct *t)
  1152. {
  1153. struct uprobe_task *utask = t->utask;
  1154. struct return_instance *ri, *tmp;
  1155. if (!utask)
  1156. return;
  1157. if (utask->active_uprobe)
  1158. put_uprobe(utask->active_uprobe);
  1159. ri = utask->return_instances;
  1160. while (ri) {
  1161. tmp = ri;
  1162. ri = ri->next;
  1163. put_uprobe(tmp->uprobe);
  1164. kfree(tmp);
  1165. }
  1166. xol_free_insn_slot(t);
  1167. kfree(utask);
  1168. t->utask = NULL;
  1169. }
  1170. /*
  1171. * Allocate a uprobe_task object for the task if if necessary.
  1172. * Called when the thread hits a breakpoint.
  1173. *
  1174. * Returns:
  1175. * - pointer to new uprobe_task on success
  1176. * - NULL otherwise
  1177. */
  1178. static struct uprobe_task *get_utask(void)
  1179. {
  1180. if (!current->utask)
  1181. current->utask = kzalloc(sizeof(struct uprobe_task), GFP_KERNEL);
  1182. return current->utask;
  1183. }
  1184. static int dup_utask(struct task_struct *t, struct uprobe_task *o_utask)
  1185. {
  1186. struct uprobe_task *n_utask;
  1187. struct return_instance **p, *o, *n;
  1188. n_utask = kzalloc(sizeof(struct uprobe_task), GFP_KERNEL);
  1189. if (!n_utask)
  1190. return -ENOMEM;
  1191. t->utask = n_utask;
  1192. p = &n_utask->return_instances;
  1193. for (o = o_utask->return_instances; o; o = o->next) {
  1194. n = kmalloc(sizeof(struct return_instance), GFP_KERNEL);
  1195. if (!n)
  1196. return -ENOMEM;
  1197. *n = *o;
  1198. atomic_inc(&n->uprobe->ref);
  1199. n->next = NULL;
  1200. *p = n;
  1201. p = &n->next;
  1202. n_utask->depth++;
  1203. }
  1204. return 0;
  1205. }
  1206. static void uprobe_warn(struct task_struct *t, const char *msg)
  1207. {
  1208. pr_warn("uprobe: %s:%d failed to %s\n",
  1209. current->comm, current->pid, msg);
  1210. }
  1211. static void dup_xol_work(struct callback_head *work)
  1212. {
  1213. if (current->flags & PF_EXITING)
  1214. return;
  1215. if (!__create_xol_area(current->utask->dup_xol_addr))
  1216. uprobe_warn(current, "dup xol area");
  1217. }
  1218. /*
  1219. * Called in context of a new clone/fork from copy_process.
  1220. */
  1221. void uprobe_copy_process(struct task_struct *t, unsigned long flags)
  1222. {
  1223. struct uprobe_task *utask = current->utask;
  1224. struct mm_struct *mm = current->mm;
  1225. struct xol_area *area;
  1226. t->utask = NULL;
  1227. if (!utask || !utask->return_instances)
  1228. return;
  1229. if (mm == t->mm && !(flags & CLONE_VFORK))
  1230. return;
  1231. if (dup_utask(t, utask))
  1232. return uprobe_warn(t, "dup ret instances");
  1233. /* The task can fork() after dup_xol_work() fails */
  1234. area = mm->uprobes_state.xol_area;
  1235. if (!area)
  1236. return uprobe_warn(t, "dup xol area");
  1237. if (mm == t->mm)
  1238. return;
  1239. t->utask->dup_xol_addr = area->vaddr;
  1240. init_task_work(&t->utask->dup_xol_work, dup_xol_work);
  1241. task_work_add(t, &t->utask->dup_xol_work, true);
  1242. }
  1243. /*
  1244. * Current area->vaddr notion assume the trampoline address is always
  1245. * equal area->vaddr.
  1246. *
  1247. * Returns -1 in case the xol_area is not allocated.
  1248. */
  1249. static unsigned long get_trampoline_vaddr(void)
  1250. {
  1251. struct xol_area *area;
  1252. unsigned long trampoline_vaddr = -1;
  1253. area = current->mm->uprobes_state.xol_area;
  1254. smp_read_barrier_depends();
  1255. if (area)
  1256. trampoline_vaddr = area->vaddr;
  1257. return trampoline_vaddr;
  1258. }
  1259. static void prepare_uretprobe(struct uprobe *uprobe, struct pt_regs *regs)
  1260. {
  1261. struct return_instance *ri;
  1262. struct uprobe_task *utask;
  1263. unsigned long orig_ret_vaddr, trampoline_vaddr;
  1264. bool chained = false;
  1265. if (!get_xol_area())
  1266. return;
  1267. utask = get_utask();
  1268. if (!utask)
  1269. return;
  1270. if (utask->depth >= MAX_URETPROBE_DEPTH) {
  1271. printk_ratelimited(KERN_INFO "uprobe: omit uretprobe due to"
  1272. " nestedness limit pid/tgid=%d/%d\n",
  1273. current->pid, current->tgid);
  1274. return;
  1275. }
  1276. ri = kzalloc(sizeof(struct return_instance), GFP_KERNEL);
  1277. if (!ri)
  1278. goto fail;
  1279. trampoline_vaddr = get_trampoline_vaddr();
  1280. orig_ret_vaddr = arch_uretprobe_hijack_return_addr(trampoline_vaddr, regs);
  1281. if (orig_ret_vaddr == -1)
  1282. goto fail;
  1283. /*
  1284. * We don't want to keep trampoline address in stack, rather keep the
  1285. * original return address of first caller thru all the consequent
  1286. * instances. This also makes breakpoint unwrapping easier.
  1287. */
  1288. if (orig_ret_vaddr == trampoline_vaddr) {
  1289. if (!utask->return_instances) {
  1290. /*
  1291. * This situation is not possible. Likely we have an
  1292. * attack from user-space.
  1293. */
  1294. pr_warn("uprobe: unable to set uretprobe pid/tgid=%d/%d\n",
  1295. current->pid, current->tgid);
  1296. goto fail;
  1297. }
  1298. chained = true;
  1299. orig_ret_vaddr = utask->return_instances->orig_ret_vaddr;
  1300. }
  1301. atomic_inc(&uprobe->ref);
  1302. ri->uprobe = uprobe;
  1303. ri->func = instruction_pointer(regs);
  1304. ri->orig_ret_vaddr = orig_ret_vaddr;
  1305. ri->chained = chained;
  1306. utask->depth++;
  1307. /* add instance to the stack */
  1308. ri->next = utask->return_instances;
  1309. utask->return_instances = ri;
  1310. return;
  1311. fail:
  1312. kfree(ri);
  1313. }
  1314. /* Prepare to single-step probed instruction out of line. */
  1315. static int
  1316. pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long bp_vaddr)
  1317. {
  1318. struct uprobe_task *utask;
  1319. unsigned long xol_vaddr;
  1320. int err;
  1321. utask = get_utask();
  1322. if (!utask)
  1323. return -ENOMEM;
  1324. xol_vaddr = xol_get_insn_slot(uprobe);
  1325. if (!xol_vaddr)
  1326. return -ENOMEM;
  1327. utask->xol_vaddr = xol_vaddr;
  1328. utask->vaddr = bp_vaddr;
  1329. err = arch_uprobe_pre_xol(&uprobe->arch, regs);
  1330. if (unlikely(err)) {
  1331. xol_free_insn_slot(current);
  1332. return err;
  1333. }
  1334. utask->active_uprobe = uprobe;
  1335. utask->state = UTASK_SSTEP;
  1336. return 0;
  1337. }
  1338. /*
  1339. * If we are singlestepping, then ensure this thread is not connected to
  1340. * non-fatal signals until completion of singlestep. When xol insn itself
  1341. * triggers the signal, restart the original insn even if the task is
  1342. * already SIGKILL'ed (since coredump should report the correct ip). This
  1343. * is even more important if the task has a handler for SIGSEGV/etc, The
  1344. * _same_ instruction should be repeated again after return from the signal
  1345. * handler, and SSTEP can never finish in this case.
  1346. */
  1347. bool uprobe_deny_signal(void)
  1348. {
  1349. struct task_struct *t = current;
  1350. struct uprobe_task *utask = t->utask;
  1351. if (likely(!utask || !utask->active_uprobe))
  1352. return false;
  1353. WARN_ON_ONCE(utask->state != UTASK_SSTEP);
  1354. if (signal_pending(t)) {
  1355. spin_lock_irq(&t->sighand->siglock);
  1356. clear_tsk_thread_flag(t, TIF_SIGPENDING);
  1357. spin_unlock_irq(&t->sighand->siglock);
  1358. if (__fatal_signal_pending(t) || arch_uprobe_xol_was_trapped(t)) {
  1359. utask->state = UTASK_SSTEP_TRAPPED;
  1360. set_tsk_thread_flag(t, TIF_UPROBE);
  1361. set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
  1362. }
  1363. }
  1364. return true;
  1365. }
  1366. static void mmf_recalc_uprobes(struct mm_struct *mm)
  1367. {
  1368. struct vm_area_struct *vma;
  1369. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  1370. if (!valid_vma(vma, false))
  1371. continue;
  1372. /*
  1373. * This is not strictly accurate, we can race with
  1374. * uprobe_unregister() and see the already removed
  1375. * uprobe if delete_uprobe() was not yet called.
  1376. * Or this uprobe can be filtered out.
  1377. */
  1378. if (vma_has_uprobes(vma, vma->vm_start, vma->vm_end))
  1379. return;
  1380. }
  1381. clear_bit(MMF_HAS_UPROBES, &mm->flags);
  1382. }
  1383. static int is_trap_at_addr(struct mm_struct *mm, unsigned long vaddr)
  1384. {
  1385. struct page *page;
  1386. uprobe_opcode_t opcode;
  1387. int result;
  1388. pagefault_disable();
  1389. result = __copy_from_user_inatomic(&opcode, (void __user*)vaddr,
  1390. sizeof(opcode));
  1391. pagefault_enable();
  1392. if (likely(result == 0))
  1393. goto out;
  1394. result = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &page, NULL);
  1395. if (result < 0)
  1396. return result;
  1397. copy_from_page(page, vaddr, &opcode, UPROBE_SWBP_INSN_SIZE);
  1398. put_page(page);
  1399. out:
  1400. /* This needs to return true for any variant of the trap insn */
  1401. return is_trap_insn(&opcode);
  1402. }
  1403. static struct uprobe *find_active_uprobe(unsigned long bp_vaddr, int *is_swbp)
  1404. {
  1405. struct mm_struct *mm = current->mm;
  1406. struct uprobe *uprobe = NULL;
  1407. struct vm_area_struct *vma;
  1408. down_read(&mm->mmap_sem);
  1409. vma = find_vma(mm, bp_vaddr);
  1410. if (vma && vma->vm_start <= bp_vaddr) {
  1411. if (valid_vma(vma, false)) {
  1412. struct inode *inode = file_inode(vma->vm_file);
  1413. loff_t offset = vaddr_to_offset(vma, bp_vaddr);
  1414. uprobe = find_uprobe(inode, offset);
  1415. }
  1416. if (!uprobe)
  1417. *is_swbp = is_trap_at_addr(mm, bp_vaddr);
  1418. } else {
  1419. *is_swbp = -EFAULT;
  1420. }
  1421. if (!uprobe && test_and_clear_bit(MMF_RECALC_UPROBES, &mm->flags))
  1422. mmf_recalc_uprobes(mm);
  1423. up_read(&mm->mmap_sem);
  1424. return uprobe;
  1425. }
  1426. static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
  1427. {
  1428. struct uprobe_consumer *uc;
  1429. int remove = UPROBE_HANDLER_REMOVE;
  1430. bool need_prep = false; /* prepare return uprobe, when needed */
  1431. down_read(&uprobe->register_rwsem);
  1432. for (uc = uprobe->consumers; uc; uc = uc->next) {
  1433. int rc = 0;
  1434. if (uc->handler) {
  1435. rc = uc->handler(uc, regs);
  1436. WARN(rc & ~UPROBE_HANDLER_MASK,
  1437. "bad rc=0x%x from %pf()\n", rc, uc->handler);
  1438. }
  1439. if (uc->ret_handler)
  1440. need_prep = true;
  1441. remove &= rc;
  1442. }
  1443. if (need_prep && !remove)
  1444. prepare_uretprobe(uprobe, regs); /* put bp at return */
  1445. if (remove && uprobe->consumers) {
  1446. WARN_ON(!uprobe_is_active(uprobe));
  1447. unapply_uprobe(uprobe, current->mm);
  1448. }
  1449. up_read(&uprobe->register_rwsem);
  1450. }
  1451. static void
  1452. handle_uretprobe_chain(struct return_instance *ri, struct pt_regs *regs)
  1453. {
  1454. struct uprobe *uprobe = ri->uprobe;
  1455. struct uprobe_consumer *uc;
  1456. down_read(&uprobe->register_rwsem);
  1457. for (uc = uprobe->consumers; uc; uc = uc->next) {
  1458. if (uc->ret_handler)
  1459. uc->ret_handler(uc, ri->func, regs);
  1460. }
  1461. up_read(&uprobe->register_rwsem);
  1462. }
  1463. static bool handle_trampoline(struct pt_regs *regs)
  1464. {
  1465. struct uprobe_task *utask;
  1466. struct return_instance *ri, *tmp;
  1467. bool chained;
  1468. utask = current->utask;
  1469. if (!utask)
  1470. return false;
  1471. ri = utask->return_instances;
  1472. if (!ri)
  1473. return false;
  1474. /*
  1475. * TODO: we should throw out return_instance's invalidated by
  1476. * longjmp(), currently we assume that the probed function always
  1477. * returns.
  1478. */
  1479. instruction_pointer_set(regs, ri->orig_ret_vaddr);
  1480. for (;;) {
  1481. handle_uretprobe_chain(ri, regs);
  1482. chained = ri->chained;
  1483. put_uprobe(ri->uprobe);
  1484. tmp = ri;
  1485. ri = ri->next;
  1486. kfree(tmp);
  1487. utask->depth--;
  1488. if (!chained)
  1489. break;
  1490. BUG_ON(!ri);
  1491. }
  1492. utask->return_instances = ri;
  1493. return true;
  1494. }
  1495. bool __weak arch_uprobe_ignore(struct arch_uprobe *aup, struct pt_regs *regs)
  1496. {
  1497. return false;
  1498. }
  1499. /*
  1500. * Run handler and ask thread to singlestep.
  1501. * Ensure all non-fatal signals cannot interrupt thread while it singlesteps.
  1502. */
  1503. static void handle_swbp(struct pt_regs *regs)
  1504. {
  1505. struct uprobe *uprobe;
  1506. unsigned long bp_vaddr;
  1507. int uninitialized_var(is_swbp);
  1508. bp_vaddr = uprobe_get_swbp_addr(regs);
  1509. if (bp_vaddr == get_trampoline_vaddr()) {
  1510. if (handle_trampoline(regs))
  1511. return;
  1512. pr_warn("uprobe: unable to handle uretprobe pid/tgid=%d/%d\n",
  1513. current->pid, current->tgid);
  1514. }
  1515. uprobe = find_active_uprobe(bp_vaddr, &is_swbp);
  1516. if (!uprobe) {
  1517. if (is_swbp > 0) {
  1518. /* No matching uprobe; signal SIGTRAP. */
  1519. send_sig(SIGTRAP, current, 0);
  1520. } else {
  1521. /*
  1522. * Either we raced with uprobe_unregister() or we can't
  1523. * access this memory. The latter is only possible if
  1524. * another thread plays with our ->mm. In both cases
  1525. * we can simply restart. If this vma was unmapped we
  1526. * can pretend this insn was not executed yet and get
  1527. * the (correct) SIGSEGV after restart.
  1528. */
  1529. instruction_pointer_set(regs, bp_vaddr);
  1530. }
  1531. return;
  1532. }
  1533. /* change it in advance for ->handler() and restart */
  1534. instruction_pointer_set(regs, bp_vaddr);
  1535. /*
  1536. * TODO: move copy_insn/etc into _register and remove this hack.
  1537. * After we hit the bp, _unregister + _register can install the
  1538. * new and not-yet-analyzed uprobe at the same address, restart.
  1539. */
  1540. smp_rmb(); /* pairs with wmb() in install_breakpoint() */
  1541. if (unlikely(!test_bit(UPROBE_COPY_INSN, &uprobe->flags)))
  1542. goto out;
  1543. /* Tracing handlers use ->utask to communicate with fetch methods */
  1544. if (!get_utask())
  1545. goto out;
  1546. if (arch_uprobe_ignore(&uprobe->arch, regs))
  1547. goto out;
  1548. handler_chain(uprobe, regs);
  1549. if (arch_uprobe_skip_sstep(&uprobe->arch, regs))
  1550. goto out;
  1551. if (!pre_ssout(uprobe, regs, bp_vaddr))
  1552. return;
  1553. /* arch_uprobe_skip_sstep() succeeded, or restart if can't singlestep */
  1554. out:
  1555. put_uprobe(uprobe);
  1556. }
  1557. /*
  1558. * Perform required fix-ups and disable singlestep.
  1559. * Allow pending signals to take effect.
  1560. */
  1561. static void handle_singlestep(struct uprobe_task *utask, struct pt_regs *regs)
  1562. {
  1563. struct uprobe *uprobe;
  1564. int err = 0;
  1565. uprobe = utask->active_uprobe;
  1566. if (utask->state == UTASK_SSTEP_ACK)
  1567. err = arch_uprobe_post_xol(&uprobe->arch, regs);
  1568. else if (utask->state == UTASK_SSTEP_TRAPPED)
  1569. arch_uprobe_abort_xol(&uprobe->arch, regs);
  1570. else
  1571. WARN_ON_ONCE(1);
  1572. put_uprobe(uprobe);
  1573. utask->active_uprobe = NULL;
  1574. utask->state = UTASK_RUNNING;
  1575. xol_free_insn_slot(current);
  1576. spin_lock_irq(&current->sighand->siglock);
  1577. recalc_sigpending(); /* see uprobe_deny_signal() */
  1578. spin_unlock_irq(&current->sighand->siglock);
  1579. if (unlikely(err)) {
  1580. uprobe_warn(current, "execute the probed insn, sending SIGILL.");
  1581. force_sig_info(SIGILL, SEND_SIG_FORCED, current);
  1582. }
  1583. }
  1584. /*
  1585. * On breakpoint hit, breakpoint notifier sets the TIF_UPROBE flag and
  1586. * allows the thread to return from interrupt. After that handle_swbp()
  1587. * sets utask->active_uprobe.
  1588. *
  1589. * On singlestep exception, singlestep notifier sets the TIF_UPROBE flag
  1590. * and allows the thread to return from interrupt.
  1591. *
  1592. * While returning to userspace, thread notices the TIF_UPROBE flag and calls
  1593. * uprobe_notify_resume().
  1594. */
  1595. void uprobe_notify_resume(struct pt_regs *regs)
  1596. {
  1597. struct uprobe_task *utask;
  1598. clear_thread_flag(TIF_UPROBE);
  1599. utask = current->utask;
  1600. if (utask && utask->active_uprobe)
  1601. handle_singlestep(utask, regs);
  1602. else
  1603. handle_swbp(regs);
  1604. }
  1605. /*
  1606. * uprobe_pre_sstep_notifier gets called from interrupt context as part of
  1607. * notifier mechanism. Set TIF_UPROBE flag and indicate breakpoint hit.
  1608. */
  1609. int uprobe_pre_sstep_notifier(struct pt_regs *regs)
  1610. {
  1611. if (!current->mm)
  1612. return 0;
  1613. if (!test_bit(MMF_HAS_UPROBES, &current->mm->flags) &&
  1614. (!current->utask || !current->utask->return_instances))
  1615. return 0;
  1616. set_thread_flag(TIF_UPROBE);
  1617. return 1;
  1618. }
  1619. /*
  1620. * uprobe_post_sstep_notifier gets called in interrupt context as part of notifier
  1621. * mechanism. Set TIF_UPROBE flag and indicate completion of singlestep.
  1622. */
  1623. int uprobe_post_sstep_notifier(struct pt_regs *regs)
  1624. {
  1625. struct uprobe_task *utask = current->utask;
  1626. if (!current->mm || !utask || !utask->active_uprobe)
  1627. /* task is currently not uprobed */
  1628. return 0;
  1629. utask->state = UTASK_SSTEP_ACK;
  1630. set_thread_flag(TIF_UPROBE);
  1631. return 1;
  1632. }
  1633. static struct notifier_block uprobe_exception_nb = {
  1634. .notifier_call = arch_uprobe_exception_notify,
  1635. .priority = INT_MAX-1, /* notified after kprobes, kgdb */
  1636. };
  1637. static int __init init_uprobes(void)
  1638. {
  1639. int i;
  1640. for (i = 0; i < UPROBES_HASH_SZ; i++)
  1641. mutex_init(&uprobes_mmap_mutex[i]);
  1642. if (percpu_init_rwsem(&dup_mmap_sem))
  1643. return -ENOMEM;
  1644. return register_die_notifier(&uprobe_exception_nb);
  1645. }
  1646. __initcall(init_uprobes);