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