uprobes.c 50 KB

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