uprobes.c 48 KB

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