uprobes.c 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044
  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 = 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. }
  434. return uprobe;
  435. }
  436. static void consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc)
  437. {
  438. down_write(&uprobe->consumer_rwsem);
  439. uc->next = uprobe->consumers;
  440. uprobe->consumers = uc;
  441. up_write(&uprobe->consumer_rwsem);
  442. }
  443. /*
  444. * For uprobe @uprobe, delete the consumer @uc.
  445. * Return true if the @uc is deleted successfully
  446. * or return false.
  447. */
  448. static bool consumer_del(struct uprobe *uprobe, struct uprobe_consumer *uc)
  449. {
  450. struct uprobe_consumer **con;
  451. bool ret = false;
  452. down_write(&uprobe->consumer_rwsem);
  453. for (con = &uprobe->consumers; *con; con = &(*con)->next) {
  454. if (*con == uc) {
  455. *con = uc->next;
  456. ret = true;
  457. break;
  458. }
  459. }
  460. up_write(&uprobe->consumer_rwsem);
  461. return ret;
  462. }
  463. static int __copy_insn(struct address_space *mapping, struct file *filp,
  464. void *insn, int nbytes, loff_t offset)
  465. {
  466. struct page *page;
  467. /*
  468. * Ensure that the page that has the original instruction is populated
  469. * and in page-cache. If ->readpage == NULL it must be shmem_mapping(),
  470. * see uprobe_register().
  471. */
  472. if (mapping->a_ops->readpage)
  473. page = read_mapping_page(mapping, offset >> PAGE_SHIFT, filp);
  474. else
  475. page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
  476. if (IS_ERR(page))
  477. return PTR_ERR(page);
  478. copy_from_page(page, offset, insn, nbytes);
  479. put_page(page);
  480. return 0;
  481. }
  482. static int copy_insn(struct uprobe *uprobe, struct file *filp)
  483. {
  484. struct address_space *mapping = uprobe->inode->i_mapping;
  485. loff_t offs = uprobe->offset;
  486. void *insn = &uprobe->arch.insn;
  487. int size = sizeof(uprobe->arch.insn);
  488. int len, err = -EIO;
  489. /* Copy only available bytes, -EIO if nothing was read */
  490. do {
  491. if (offs >= i_size_read(uprobe->inode))
  492. break;
  493. len = min_t(int, size, PAGE_SIZE - (offs & ~PAGE_MASK));
  494. err = __copy_insn(mapping, filp, insn, len, offs);
  495. if (err)
  496. break;
  497. insn += len;
  498. offs += len;
  499. size -= len;
  500. } while (size);
  501. return err;
  502. }
  503. static int prepare_uprobe(struct uprobe *uprobe, struct file *file,
  504. struct mm_struct *mm, unsigned long vaddr)
  505. {
  506. int ret = 0;
  507. if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
  508. return ret;
  509. /* TODO: move this into _register, until then we abuse this sem. */
  510. down_write(&uprobe->consumer_rwsem);
  511. if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
  512. goto out;
  513. ret = copy_insn(uprobe, file);
  514. if (ret)
  515. goto out;
  516. ret = -ENOTSUPP;
  517. if (is_trap_insn((uprobe_opcode_t *)&uprobe->arch.insn))
  518. goto out;
  519. ret = arch_uprobe_analyze_insn(&uprobe->arch, mm, vaddr);
  520. if (ret)
  521. goto out;
  522. /* uprobe_write_opcode() assumes we don't cross page boundary */
  523. BUG_ON((uprobe->offset & ~PAGE_MASK) +
  524. UPROBE_SWBP_INSN_SIZE > PAGE_SIZE);
  525. smp_wmb(); /* pairs with rmb() in find_active_uprobe() */
  526. set_bit(UPROBE_COPY_INSN, &uprobe->flags);
  527. out:
  528. up_write(&uprobe->consumer_rwsem);
  529. return ret;
  530. }
  531. static inline bool consumer_filter(struct uprobe_consumer *uc,
  532. enum uprobe_filter_ctx ctx, struct mm_struct *mm)
  533. {
  534. return !uc->filter || uc->filter(uc, ctx, mm);
  535. }
  536. static bool filter_chain(struct uprobe *uprobe,
  537. enum uprobe_filter_ctx ctx, struct mm_struct *mm)
  538. {
  539. struct uprobe_consumer *uc;
  540. bool ret = false;
  541. down_read(&uprobe->consumer_rwsem);
  542. for (uc = uprobe->consumers; uc; uc = uc->next) {
  543. ret = consumer_filter(uc, ctx, mm);
  544. if (ret)
  545. break;
  546. }
  547. up_read(&uprobe->consumer_rwsem);
  548. return ret;
  549. }
  550. static int
  551. install_breakpoint(struct uprobe *uprobe, struct mm_struct *mm,
  552. struct vm_area_struct *vma, unsigned long vaddr)
  553. {
  554. bool first_uprobe;
  555. int ret;
  556. ret = prepare_uprobe(uprobe, vma->vm_file, mm, vaddr);
  557. if (ret)
  558. return ret;
  559. /*
  560. * set MMF_HAS_UPROBES in advance for uprobe_pre_sstep_notifier(),
  561. * the task can hit this breakpoint right after __replace_page().
  562. */
  563. first_uprobe = !test_bit(MMF_HAS_UPROBES, &mm->flags);
  564. if (first_uprobe)
  565. set_bit(MMF_HAS_UPROBES, &mm->flags);
  566. ret = set_swbp(&uprobe->arch, mm, vaddr);
  567. if (!ret)
  568. clear_bit(MMF_RECALC_UPROBES, &mm->flags);
  569. else if (first_uprobe)
  570. clear_bit(MMF_HAS_UPROBES, &mm->flags);
  571. return ret;
  572. }
  573. static int
  574. remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, unsigned long vaddr)
  575. {
  576. set_bit(MMF_RECALC_UPROBES, &mm->flags);
  577. return set_orig_insn(&uprobe->arch, mm, vaddr);
  578. }
  579. static inline bool uprobe_is_active(struct uprobe *uprobe)
  580. {
  581. return !RB_EMPTY_NODE(&uprobe->rb_node);
  582. }
  583. /*
  584. * There could be threads that have already hit the breakpoint. They
  585. * will recheck the current insn and restart if find_uprobe() fails.
  586. * See find_active_uprobe().
  587. */
  588. static void delete_uprobe(struct uprobe *uprobe)
  589. {
  590. if (WARN_ON(!uprobe_is_active(uprobe)))
  591. return;
  592. spin_lock(&uprobes_treelock);
  593. rb_erase(&uprobe->rb_node, &uprobes_tree);
  594. spin_unlock(&uprobes_treelock);
  595. RB_CLEAR_NODE(&uprobe->rb_node); /* for uprobe_is_active() */
  596. put_uprobe(uprobe);
  597. }
  598. struct map_info {
  599. struct map_info *next;
  600. struct mm_struct *mm;
  601. unsigned long vaddr;
  602. };
  603. static inline struct map_info *free_map_info(struct map_info *info)
  604. {
  605. struct map_info *next = info->next;
  606. kfree(info);
  607. return next;
  608. }
  609. static struct map_info *
  610. build_map_info(struct address_space *mapping, loff_t offset, bool is_register)
  611. {
  612. unsigned long pgoff = offset >> PAGE_SHIFT;
  613. struct vm_area_struct *vma;
  614. struct map_info *curr = NULL;
  615. struct map_info *prev = NULL;
  616. struct map_info *info;
  617. int more = 0;
  618. again:
  619. i_mmap_lock_read(mapping);
  620. vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
  621. if (!valid_vma(vma, is_register))
  622. continue;
  623. if (!prev && !more) {
  624. /*
  625. * Needs GFP_NOWAIT to avoid i_mmap_rwsem recursion through
  626. * reclaim. This is optimistic, no harm done if it fails.
  627. */
  628. prev = kmalloc(sizeof(struct map_info),
  629. GFP_NOWAIT | __GFP_NOMEMALLOC | __GFP_NOWARN);
  630. if (prev)
  631. prev->next = NULL;
  632. }
  633. if (!prev) {
  634. more++;
  635. continue;
  636. }
  637. if (!mmget_not_zero(vma->vm_mm))
  638. continue;
  639. info = prev;
  640. prev = prev->next;
  641. info->next = curr;
  642. curr = info;
  643. info->mm = vma->vm_mm;
  644. info->vaddr = offset_to_vaddr(vma, offset);
  645. }
  646. i_mmap_unlock_read(mapping);
  647. if (!more)
  648. goto out;
  649. prev = curr;
  650. while (curr) {
  651. mmput(curr->mm);
  652. curr = curr->next;
  653. }
  654. do {
  655. info = kmalloc(sizeof(struct map_info), GFP_KERNEL);
  656. if (!info) {
  657. curr = ERR_PTR(-ENOMEM);
  658. goto out;
  659. }
  660. info->next = prev;
  661. prev = info;
  662. } while (--more);
  663. goto again;
  664. out:
  665. while (prev)
  666. prev = free_map_info(prev);
  667. return curr;
  668. }
  669. static int
  670. register_for_each_vma(struct uprobe *uprobe, struct uprobe_consumer *new)
  671. {
  672. bool is_register = !!new;
  673. struct map_info *info;
  674. int err = 0;
  675. percpu_down_write(&dup_mmap_sem);
  676. info = build_map_info(uprobe->inode->i_mapping,
  677. uprobe->offset, is_register);
  678. if (IS_ERR(info)) {
  679. err = PTR_ERR(info);
  680. goto out;
  681. }
  682. while (info) {
  683. struct mm_struct *mm = info->mm;
  684. struct vm_area_struct *vma;
  685. if (err && is_register)
  686. goto free;
  687. down_write(&mm->mmap_sem);
  688. vma = find_vma(mm, info->vaddr);
  689. if (!vma || !valid_vma(vma, is_register) ||
  690. file_inode(vma->vm_file) != uprobe->inode)
  691. goto unlock;
  692. if (vma->vm_start > info->vaddr ||
  693. vaddr_to_offset(vma, info->vaddr) != uprobe->offset)
  694. goto unlock;
  695. if (is_register) {
  696. /* consult only the "caller", new consumer. */
  697. if (consumer_filter(new,
  698. UPROBE_FILTER_REGISTER, mm))
  699. err = install_breakpoint(uprobe, mm, vma, info->vaddr);
  700. } else if (test_bit(MMF_HAS_UPROBES, &mm->flags)) {
  701. if (!filter_chain(uprobe,
  702. UPROBE_FILTER_UNREGISTER, mm))
  703. err |= remove_breakpoint(uprobe, mm, info->vaddr);
  704. }
  705. unlock:
  706. up_write(&mm->mmap_sem);
  707. free:
  708. mmput(mm);
  709. info = free_map_info(info);
  710. }
  711. out:
  712. percpu_up_write(&dup_mmap_sem);
  713. return err;
  714. }
  715. static int __uprobe_register(struct uprobe *uprobe, struct uprobe_consumer *uc)
  716. {
  717. consumer_add(uprobe, uc);
  718. return register_for_each_vma(uprobe, uc);
  719. }
  720. static void __uprobe_unregister(struct uprobe *uprobe, struct uprobe_consumer *uc)
  721. {
  722. int err;
  723. if (WARN_ON(!consumer_del(uprobe, uc)))
  724. return;
  725. err = register_for_each_vma(uprobe, NULL);
  726. /* TODO : cant unregister? schedule a worker thread */
  727. if (!uprobe->consumers && !err)
  728. delete_uprobe(uprobe);
  729. }
  730. /*
  731. * uprobe_register - register a probe
  732. * @inode: the file in which the probe has to be placed.
  733. * @offset: offset from the start of the file.
  734. * @uc: information on howto handle the probe..
  735. *
  736. * Apart from the access refcount, uprobe_register() takes a creation
  737. * refcount (thro alloc_uprobe) if and only if this @uprobe is getting
  738. * inserted into the rbtree (i.e first consumer for a @inode:@offset
  739. * tuple). Creation refcount stops uprobe_unregister from freeing the
  740. * @uprobe even before the register operation is complete. Creation
  741. * refcount is released when the last @uc for the @uprobe
  742. * unregisters. Caller of uprobe_register() is required to keep @inode
  743. * (and the containing mount) referenced.
  744. *
  745. * Return errno if it cannot successully install probes
  746. * else return 0 (success)
  747. */
  748. int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
  749. {
  750. struct uprobe *uprobe;
  751. int ret;
  752. /* Uprobe must have at least one set consumer */
  753. if (!uc->handler && !uc->ret_handler)
  754. return -EINVAL;
  755. /* copy_insn() uses read_mapping_page() or shmem_read_mapping_page() */
  756. if (!inode->i_mapping->a_ops->readpage && !shmem_mapping(inode->i_mapping))
  757. return -EIO;
  758. /* Racy, just to catch the obvious mistakes */
  759. if (offset > i_size_read(inode))
  760. return -EINVAL;
  761. retry:
  762. uprobe = alloc_uprobe(inode, offset);
  763. if (!uprobe)
  764. return -ENOMEM;
  765. /*
  766. * We can race with uprobe_unregister()->delete_uprobe().
  767. * Check uprobe_is_active() and retry if it is false.
  768. */
  769. down_write(&uprobe->register_rwsem);
  770. ret = -EAGAIN;
  771. if (likely(uprobe_is_active(uprobe))) {
  772. ret = __uprobe_register(uprobe, uc);
  773. if (ret)
  774. __uprobe_unregister(uprobe, uc);
  775. }
  776. up_write(&uprobe->register_rwsem);
  777. put_uprobe(uprobe);
  778. if (unlikely(ret == -EAGAIN))
  779. goto retry;
  780. return ret;
  781. }
  782. EXPORT_SYMBOL_GPL(uprobe_register);
  783. /*
  784. * uprobe_apply - unregister a already registered probe.
  785. * @inode: the file in which the probe has to be removed.
  786. * @offset: offset from the start of the file.
  787. * @uc: consumer which wants to add more or remove some breakpoints
  788. * @add: add or remove the breakpoints
  789. */
  790. int uprobe_apply(struct inode *inode, loff_t offset,
  791. struct uprobe_consumer *uc, bool add)
  792. {
  793. struct uprobe *uprobe;
  794. struct uprobe_consumer *con;
  795. int ret = -ENOENT;
  796. uprobe = find_uprobe(inode, offset);
  797. if (WARN_ON(!uprobe))
  798. return ret;
  799. down_write(&uprobe->register_rwsem);
  800. for (con = uprobe->consumers; con && con != uc ; con = con->next)
  801. ;
  802. if (con)
  803. ret = register_for_each_vma(uprobe, add ? uc : NULL);
  804. up_write(&uprobe->register_rwsem);
  805. put_uprobe(uprobe);
  806. return ret;
  807. }
  808. /*
  809. * uprobe_unregister - unregister a already registered probe.
  810. * @inode: the file in which the probe has to be removed.
  811. * @offset: offset from the start of the file.
  812. * @uc: identify which probe if multiple probes are colocated.
  813. */
  814. void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
  815. {
  816. struct uprobe *uprobe;
  817. uprobe = find_uprobe(inode, offset);
  818. if (WARN_ON(!uprobe))
  819. return;
  820. down_write(&uprobe->register_rwsem);
  821. __uprobe_unregister(uprobe, uc);
  822. up_write(&uprobe->register_rwsem);
  823. put_uprobe(uprobe);
  824. }
  825. EXPORT_SYMBOL_GPL(uprobe_unregister);
  826. static int unapply_uprobe(struct uprobe *uprobe, struct mm_struct *mm)
  827. {
  828. struct vm_area_struct *vma;
  829. int err = 0;
  830. down_read(&mm->mmap_sem);
  831. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  832. unsigned long vaddr;
  833. loff_t offset;
  834. if (!valid_vma(vma, false) ||
  835. file_inode(vma->vm_file) != uprobe->inode)
  836. continue;
  837. offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
  838. if (uprobe->offset < offset ||
  839. uprobe->offset >= offset + vma->vm_end - vma->vm_start)
  840. continue;
  841. vaddr = offset_to_vaddr(vma, uprobe->offset);
  842. err |= remove_breakpoint(uprobe, mm, vaddr);
  843. }
  844. up_read(&mm->mmap_sem);
  845. return err;
  846. }
  847. static struct rb_node *
  848. find_node_in_range(struct inode *inode, loff_t min, loff_t max)
  849. {
  850. struct rb_node *n = uprobes_tree.rb_node;
  851. while (n) {
  852. struct uprobe *u = rb_entry(n, struct uprobe, rb_node);
  853. if (inode < u->inode) {
  854. n = n->rb_left;
  855. } else if (inode > u->inode) {
  856. n = n->rb_right;
  857. } else {
  858. if (max < u->offset)
  859. n = n->rb_left;
  860. else if (min > u->offset)
  861. n = n->rb_right;
  862. else
  863. break;
  864. }
  865. }
  866. return n;
  867. }
  868. /*
  869. * For a given range in vma, build a list of probes that need to be inserted.
  870. */
  871. static void build_probe_list(struct inode *inode,
  872. struct vm_area_struct *vma,
  873. unsigned long start, unsigned long end,
  874. struct list_head *head)
  875. {
  876. loff_t min, max;
  877. struct rb_node *n, *t;
  878. struct uprobe *u;
  879. INIT_LIST_HEAD(head);
  880. min = vaddr_to_offset(vma, start);
  881. max = min + (end - start) - 1;
  882. spin_lock(&uprobes_treelock);
  883. n = find_node_in_range(inode, min, max);
  884. if (n) {
  885. for (t = n; t; t = rb_prev(t)) {
  886. u = rb_entry(t, struct uprobe, rb_node);
  887. if (u->inode != inode || u->offset < min)
  888. break;
  889. list_add(&u->pending_list, head);
  890. get_uprobe(u);
  891. }
  892. for (t = n; (t = rb_next(t)); ) {
  893. u = rb_entry(t, struct uprobe, rb_node);
  894. if (u->inode != inode || u->offset > max)
  895. break;
  896. list_add(&u->pending_list, head);
  897. get_uprobe(u);
  898. }
  899. }
  900. spin_unlock(&uprobes_treelock);
  901. }
  902. /*
  903. * Called from mmap_region/vma_adjust with mm->mmap_sem acquired.
  904. *
  905. * Currently we ignore all errors and always return 0, the callers
  906. * can't handle the failure anyway.
  907. */
  908. int uprobe_mmap(struct vm_area_struct *vma)
  909. {
  910. struct list_head tmp_list;
  911. struct uprobe *uprobe, *u;
  912. struct inode *inode;
  913. if (no_uprobe_events() || !valid_vma(vma, true))
  914. return 0;
  915. inode = file_inode(vma->vm_file);
  916. if (!inode)
  917. return 0;
  918. mutex_lock(uprobes_mmap_hash(inode));
  919. build_probe_list(inode, vma, vma->vm_start, vma->vm_end, &tmp_list);
  920. /*
  921. * We can race with uprobe_unregister(), this uprobe can be already
  922. * removed. But in this case filter_chain() must return false, all
  923. * consumers have gone away.
  924. */
  925. list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
  926. if (!fatal_signal_pending(current) &&
  927. filter_chain(uprobe, UPROBE_FILTER_MMAP, vma->vm_mm)) {
  928. unsigned long vaddr = offset_to_vaddr(vma, uprobe->offset);
  929. install_breakpoint(uprobe, vma->vm_mm, vma, vaddr);
  930. }
  931. put_uprobe(uprobe);
  932. }
  933. mutex_unlock(uprobes_mmap_hash(inode));
  934. return 0;
  935. }
  936. static bool
  937. vma_has_uprobes(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  938. {
  939. loff_t min, max;
  940. struct inode *inode;
  941. struct rb_node *n;
  942. inode = file_inode(vma->vm_file);
  943. min = vaddr_to_offset(vma, start);
  944. max = min + (end - start) - 1;
  945. spin_lock(&uprobes_treelock);
  946. n = find_node_in_range(inode, min, max);
  947. spin_unlock(&uprobes_treelock);
  948. return !!n;
  949. }
  950. /*
  951. * Called in context of a munmap of a vma.
  952. */
  953. void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  954. {
  955. if (no_uprobe_events() || !valid_vma(vma, false))
  956. return;
  957. if (!atomic_read(&vma->vm_mm->mm_users)) /* called by mmput() ? */
  958. return;
  959. if (!test_bit(MMF_HAS_UPROBES, &vma->vm_mm->flags) ||
  960. test_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags))
  961. return;
  962. if (vma_has_uprobes(vma, start, end))
  963. set_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags);
  964. }
  965. /* Slot allocation for XOL */
  966. static int xol_add_vma(struct mm_struct *mm, struct xol_area *area)
  967. {
  968. struct vm_area_struct *vma;
  969. int ret;
  970. if (down_write_killable(&mm->mmap_sem))
  971. return -EINTR;
  972. if (mm->uprobes_state.xol_area) {
  973. ret = -EALREADY;
  974. goto fail;
  975. }
  976. if (!area->vaddr) {
  977. /* Try to map as high as possible, this is only a hint. */
  978. area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE,
  979. PAGE_SIZE, 0, 0);
  980. if (area->vaddr & ~PAGE_MASK) {
  981. ret = area->vaddr;
  982. goto fail;
  983. }
  984. }
  985. vma = _install_special_mapping(mm, area->vaddr, PAGE_SIZE,
  986. VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO,
  987. &area->xol_mapping);
  988. if (IS_ERR(vma)) {
  989. ret = PTR_ERR(vma);
  990. goto fail;
  991. }
  992. ret = 0;
  993. /* pairs with get_xol_area() */
  994. smp_store_release(&mm->uprobes_state.xol_area, area); /* ^^^ */
  995. fail:
  996. up_write(&mm->mmap_sem);
  997. return ret;
  998. }
  999. static struct xol_area *__create_xol_area(unsigned long vaddr)
  1000. {
  1001. struct mm_struct *mm = current->mm;
  1002. uprobe_opcode_t insn = UPROBE_SWBP_INSN;
  1003. struct xol_area *area;
  1004. area = kmalloc(sizeof(*area), GFP_KERNEL);
  1005. if (unlikely(!area))
  1006. goto out;
  1007. area->bitmap = kcalloc(BITS_TO_LONGS(UINSNS_PER_PAGE), sizeof(long),
  1008. GFP_KERNEL);
  1009. if (!area->bitmap)
  1010. goto free_area;
  1011. area->xol_mapping.name = "[uprobes]";
  1012. area->xol_mapping.fault = NULL;
  1013. area->xol_mapping.pages = area->pages;
  1014. area->pages[0] = alloc_page(GFP_HIGHUSER);
  1015. if (!area->pages[0])
  1016. goto free_bitmap;
  1017. area->pages[1] = NULL;
  1018. area->vaddr = vaddr;
  1019. init_waitqueue_head(&area->wq);
  1020. /* Reserve the 1st slot for get_trampoline_vaddr() */
  1021. set_bit(0, area->bitmap);
  1022. atomic_set(&area->slot_count, 1);
  1023. arch_uprobe_copy_ixol(area->pages[0], 0, &insn, UPROBE_SWBP_INSN_SIZE);
  1024. if (!xol_add_vma(mm, area))
  1025. return area;
  1026. __free_page(area->pages[0]);
  1027. free_bitmap:
  1028. kfree(area->bitmap);
  1029. free_area:
  1030. kfree(area);
  1031. out:
  1032. return NULL;
  1033. }
  1034. /*
  1035. * get_xol_area - Allocate process's xol_area if necessary.
  1036. * This area will be used for storing instructions for execution out of line.
  1037. *
  1038. * Returns the allocated area or NULL.
  1039. */
  1040. static struct xol_area *get_xol_area(void)
  1041. {
  1042. struct mm_struct *mm = current->mm;
  1043. struct xol_area *area;
  1044. if (!mm->uprobes_state.xol_area)
  1045. __create_xol_area(0);
  1046. /* Pairs with xol_add_vma() smp_store_release() */
  1047. area = READ_ONCE(mm->uprobes_state.xol_area); /* ^^^ */
  1048. return area;
  1049. }
  1050. /*
  1051. * uprobe_clear_state - Free the area allocated for slots.
  1052. */
  1053. void uprobe_clear_state(struct mm_struct *mm)
  1054. {
  1055. struct xol_area *area = mm->uprobes_state.xol_area;
  1056. if (!area)
  1057. return;
  1058. put_page(area->pages[0]);
  1059. kfree(area->bitmap);
  1060. kfree(area);
  1061. }
  1062. void uprobe_start_dup_mmap(void)
  1063. {
  1064. percpu_down_read(&dup_mmap_sem);
  1065. }
  1066. void uprobe_end_dup_mmap(void)
  1067. {
  1068. percpu_up_read(&dup_mmap_sem);
  1069. }
  1070. void uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm)
  1071. {
  1072. if (test_bit(MMF_HAS_UPROBES, &oldmm->flags)) {
  1073. set_bit(MMF_HAS_UPROBES, &newmm->flags);
  1074. /* unconditionally, dup_mmap() skips VM_DONTCOPY vmas */
  1075. set_bit(MMF_RECALC_UPROBES, &newmm->flags);
  1076. }
  1077. }
  1078. /*
  1079. * - search for a free slot.
  1080. */
  1081. static unsigned long xol_take_insn_slot(struct xol_area *area)
  1082. {
  1083. unsigned long slot_addr;
  1084. int slot_nr;
  1085. do {
  1086. slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE);
  1087. if (slot_nr < UINSNS_PER_PAGE) {
  1088. if (!test_and_set_bit(slot_nr, area->bitmap))
  1089. break;
  1090. slot_nr = UINSNS_PER_PAGE;
  1091. continue;
  1092. }
  1093. wait_event(area->wq, (atomic_read(&area->slot_count) < UINSNS_PER_PAGE));
  1094. } while (slot_nr >= UINSNS_PER_PAGE);
  1095. slot_addr = area->vaddr + (slot_nr * UPROBE_XOL_SLOT_BYTES);
  1096. atomic_inc(&area->slot_count);
  1097. return slot_addr;
  1098. }
  1099. /*
  1100. * xol_get_insn_slot - allocate a slot for xol.
  1101. * Returns the allocated slot address or 0.
  1102. */
  1103. static unsigned long xol_get_insn_slot(struct uprobe *uprobe)
  1104. {
  1105. struct xol_area *area;
  1106. unsigned long xol_vaddr;
  1107. area = get_xol_area();
  1108. if (!area)
  1109. return 0;
  1110. xol_vaddr = xol_take_insn_slot(area);
  1111. if (unlikely(!xol_vaddr))
  1112. return 0;
  1113. arch_uprobe_copy_ixol(area->pages[0], xol_vaddr,
  1114. &uprobe->arch.ixol, sizeof(uprobe->arch.ixol));
  1115. return xol_vaddr;
  1116. }
  1117. /*
  1118. * xol_free_insn_slot - If slot was earlier allocated by
  1119. * @xol_get_insn_slot(), make the slot available for
  1120. * subsequent requests.
  1121. */
  1122. static void xol_free_insn_slot(struct task_struct *tsk)
  1123. {
  1124. struct xol_area *area;
  1125. unsigned long vma_end;
  1126. unsigned long slot_addr;
  1127. if (!tsk->mm || !tsk->mm->uprobes_state.xol_area || !tsk->utask)
  1128. return;
  1129. slot_addr = tsk->utask->xol_vaddr;
  1130. if (unlikely(!slot_addr))
  1131. return;
  1132. area = tsk->mm->uprobes_state.xol_area;
  1133. vma_end = area->vaddr + PAGE_SIZE;
  1134. if (area->vaddr <= slot_addr && slot_addr < vma_end) {
  1135. unsigned long offset;
  1136. int slot_nr;
  1137. offset = slot_addr - area->vaddr;
  1138. slot_nr = offset / UPROBE_XOL_SLOT_BYTES;
  1139. if (slot_nr >= UINSNS_PER_PAGE)
  1140. return;
  1141. clear_bit(slot_nr, area->bitmap);
  1142. atomic_dec(&area->slot_count);
  1143. smp_mb__after_atomic(); /* pairs with prepare_to_wait() */
  1144. if (waitqueue_active(&area->wq))
  1145. wake_up(&area->wq);
  1146. tsk->utask->xol_vaddr = 0;
  1147. }
  1148. }
  1149. void __weak arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr,
  1150. void *src, unsigned long len)
  1151. {
  1152. /* Initialize the slot */
  1153. copy_to_page(page, vaddr, src, len);
  1154. /*
  1155. * We probably need flush_icache_user_range() but it needs vma.
  1156. * This should work on most of architectures by default. If
  1157. * architecture needs to do something different it can define
  1158. * its own version of the function.
  1159. */
  1160. flush_dcache_page(page);
  1161. }
  1162. /**
  1163. * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
  1164. * @regs: Reflects the saved state of the task after it has hit a breakpoint
  1165. * instruction.
  1166. * Return the address of the breakpoint instruction.
  1167. */
  1168. unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs)
  1169. {
  1170. return instruction_pointer(regs) - UPROBE_SWBP_INSN_SIZE;
  1171. }
  1172. unsigned long uprobe_get_trap_addr(struct pt_regs *regs)
  1173. {
  1174. struct uprobe_task *utask = current->utask;
  1175. if (unlikely(utask && utask->active_uprobe))
  1176. return utask->vaddr;
  1177. return instruction_pointer(regs);
  1178. }
  1179. static struct return_instance *free_ret_instance(struct return_instance *ri)
  1180. {
  1181. struct return_instance *next = ri->next;
  1182. put_uprobe(ri->uprobe);
  1183. kfree(ri);
  1184. return next;
  1185. }
  1186. /*
  1187. * Called with no locks held.
  1188. * Called in context of a exiting or a exec-ing thread.
  1189. */
  1190. void uprobe_free_utask(struct task_struct *t)
  1191. {
  1192. struct uprobe_task *utask = t->utask;
  1193. struct return_instance *ri;
  1194. if (!utask)
  1195. return;
  1196. if (utask->active_uprobe)
  1197. put_uprobe(utask->active_uprobe);
  1198. ri = utask->return_instances;
  1199. while (ri)
  1200. ri = free_ret_instance(ri);
  1201. xol_free_insn_slot(t);
  1202. kfree(utask);
  1203. t->utask = NULL;
  1204. }
  1205. /*
  1206. * Allocate a uprobe_task object for the task if if necessary.
  1207. * Called when the thread hits a breakpoint.
  1208. *
  1209. * Returns:
  1210. * - pointer to new uprobe_task on success
  1211. * - NULL otherwise
  1212. */
  1213. static struct uprobe_task *get_utask(void)
  1214. {
  1215. if (!current->utask)
  1216. current->utask = kzalloc(sizeof(struct uprobe_task), GFP_KERNEL);
  1217. return current->utask;
  1218. }
  1219. static int dup_utask(struct task_struct *t, struct uprobe_task *o_utask)
  1220. {
  1221. struct uprobe_task *n_utask;
  1222. struct return_instance **p, *o, *n;
  1223. n_utask = kzalloc(sizeof(struct uprobe_task), GFP_KERNEL);
  1224. if (!n_utask)
  1225. return -ENOMEM;
  1226. t->utask = n_utask;
  1227. p = &n_utask->return_instances;
  1228. for (o = o_utask->return_instances; o; o = o->next) {
  1229. n = kmalloc(sizeof(struct return_instance), GFP_KERNEL);
  1230. if (!n)
  1231. return -ENOMEM;
  1232. *n = *o;
  1233. get_uprobe(n->uprobe);
  1234. n->next = NULL;
  1235. *p = n;
  1236. p = &n->next;
  1237. n_utask->depth++;
  1238. }
  1239. return 0;
  1240. }
  1241. static void uprobe_warn(struct task_struct *t, const char *msg)
  1242. {
  1243. pr_warn("uprobe: %s:%d failed to %s\n",
  1244. current->comm, current->pid, msg);
  1245. }
  1246. static void dup_xol_work(struct callback_head *work)
  1247. {
  1248. if (current->flags & PF_EXITING)
  1249. return;
  1250. if (!__create_xol_area(current->utask->dup_xol_addr) &&
  1251. !fatal_signal_pending(current))
  1252. uprobe_warn(current, "dup xol area");
  1253. }
  1254. /*
  1255. * Called in context of a new clone/fork from copy_process.
  1256. */
  1257. void uprobe_copy_process(struct task_struct *t, unsigned long flags)
  1258. {
  1259. struct uprobe_task *utask = current->utask;
  1260. struct mm_struct *mm = current->mm;
  1261. struct xol_area *area;
  1262. t->utask = NULL;
  1263. if (!utask || !utask->return_instances)
  1264. return;
  1265. if (mm == t->mm && !(flags & CLONE_VFORK))
  1266. return;
  1267. if (dup_utask(t, utask))
  1268. return uprobe_warn(t, "dup ret instances");
  1269. /* The task can fork() after dup_xol_work() fails */
  1270. area = mm->uprobes_state.xol_area;
  1271. if (!area)
  1272. return uprobe_warn(t, "dup xol area");
  1273. if (mm == t->mm)
  1274. return;
  1275. t->utask->dup_xol_addr = area->vaddr;
  1276. init_task_work(&t->utask->dup_xol_work, dup_xol_work);
  1277. task_work_add(t, &t->utask->dup_xol_work, true);
  1278. }
  1279. /*
  1280. * Current area->vaddr notion assume the trampoline address is always
  1281. * equal area->vaddr.
  1282. *
  1283. * Returns -1 in case the xol_area is not allocated.
  1284. */
  1285. static unsigned long get_trampoline_vaddr(void)
  1286. {
  1287. struct xol_area *area;
  1288. unsigned long trampoline_vaddr = -1;
  1289. /* Pairs with xol_add_vma() smp_store_release() */
  1290. area = READ_ONCE(current->mm->uprobes_state.xol_area); /* ^^^ */
  1291. if (area)
  1292. trampoline_vaddr = area->vaddr;
  1293. return trampoline_vaddr;
  1294. }
  1295. static void cleanup_return_instances(struct uprobe_task *utask, bool chained,
  1296. struct pt_regs *regs)
  1297. {
  1298. struct return_instance *ri = utask->return_instances;
  1299. enum rp_check ctx = chained ? RP_CHECK_CHAIN_CALL : RP_CHECK_CALL;
  1300. while (ri && !arch_uretprobe_is_alive(ri, ctx, regs)) {
  1301. ri = free_ret_instance(ri);
  1302. utask->depth--;
  1303. }
  1304. utask->return_instances = ri;
  1305. }
  1306. static void prepare_uretprobe(struct uprobe *uprobe, struct pt_regs *regs)
  1307. {
  1308. struct return_instance *ri;
  1309. struct uprobe_task *utask;
  1310. unsigned long orig_ret_vaddr, trampoline_vaddr;
  1311. bool chained;
  1312. if (!get_xol_area())
  1313. return;
  1314. utask = get_utask();
  1315. if (!utask)
  1316. return;
  1317. if (utask->depth >= MAX_URETPROBE_DEPTH) {
  1318. printk_ratelimited(KERN_INFO "uprobe: omit uretprobe due to"
  1319. " nestedness limit pid/tgid=%d/%d\n",
  1320. current->pid, current->tgid);
  1321. return;
  1322. }
  1323. ri = kmalloc(sizeof(struct return_instance), GFP_KERNEL);
  1324. if (!ri)
  1325. return;
  1326. trampoline_vaddr = get_trampoline_vaddr();
  1327. orig_ret_vaddr = arch_uretprobe_hijack_return_addr(trampoline_vaddr, regs);
  1328. if (orig_ret_vaddr == -1)
  1329. goto fail;
  1330. /* drop the entries invalidated by longjmp() */
  1331. chained = (orig_ret_vaddr == trampoline_vaddr);
  1332. cleanup_return_instances(utask, chained, regs);
  1333. /*
  1334. * We don't want to keep trampoline address in stack, rather keep the
  1335. * original return address of first caller thru all the consequent
  1336. * instances. This also makes breakpoint unwrapping easier.
  1337. */
  1338. if (chained) {
  1339. if (!utask->return_instances) {
  1340. /*
  1341. * This situation is not possible. Likely we have an
  1342. * attack from user-space.
  1343. */
  1344. uprobe_warn(current, "handle tail call");
  1345. goto fail;
  1346. }
  1347. orig_ret_vaddr = utask->return_instances->orig_ret_vaddr;
  1348. }
  1349. ri->uprobe = get_uprobe(uprobe);
  1350. ri->func = instruction_pointer(regs);
  1351. ri->stack = user_stack_pointer(regs);
  1352. ri->orig_ret_vaddr = orig_ret_vaddr;
  1353. ri->chained = chained;
  1354. utask->depth++;
  1355. ri->next = utask->return_instances;
  1356. utask->return_instances = ri;
  1357. return;
  1358. fail:
  1359. kfree(ri);
  1360. }
  1361. /* Prepare to single-step probed instruction out of line. */
  1362. static int
  1363. pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long bp_vaddr)
  1364. {
  1365. struct uprobe_task *utask;
  1366. unsigned long xol_vaddr;
  1367. int err;
  1368. utask = get_utask();
  1369. if (!utask)
  1370. return -ENOMEM;
  1371. xol_vaddr = xol_get_insn_slot(uprobe);
  1372. if (!xol_vaddr)
  1373. return -ENOMEM;
  1374. utask->xol_vaddr = xol_vaddr;
  1375. utask->vaddr = bp_vaddr;
  1376. err = arch_uprobe_pre_xol(&uprobe->arch, regs);
  1377. if (unlikely(err)) {
  1378. xol_free_insn_slot(current);
  1379. return err;
  1380. }
  1381. utask->active_uprobe = uprobe;
  1382. utask->state = UTASK_SSTEP;
  1383. return 0;
  1384. }
  1385. /*
  1386. * If we are singlestepping, then ensure this thread is not connected to
  1387. * non-fatal signals until completion of singlestep. When xol insn itself
  1388. * triggers the signal, restart the original insn even if the task is
  1389. * already SIGKILL'ed (since coredump should report the correct ip). This
  1390. * is even more important if the task has a handler for SIGSEGV/etc, The
  1391. * _same_ instruction should be repeated again after return from the signal
  1392. * handler, and SSTEP can never finish in this case.
  1393. */
  1394. bool uprobe_deny_signal(void)
  1395. {
  1396. struct task_struct *t = current;
  1397. struct uprobe_task *utask = t->utask;
  1398. if (likely(!utask || !utask->active_uprobe))
  1399. return false;
  1400. WARN_ON_ONCE(utask->state != UTASK_SSTEP);
  1401. if (signal_pending(t)) {
  1402. spin_lock_irq(&t->sighand->siglock);
  1403. clear_tsk_thread_flag(t, TIF_SIGPENDING);
  1404. spin_unlock_irq(&t->sighand->siglock);
  1405. if (__fatal_signal_pending(t) || arch_uprobe_xol_was_trapped(t)) {
  1406. utask->state = UTASK_SSTEP_TRAPPED;
  1407. set_tsk_thread_flag(t, TIF_UPROBE);
  1408. }
  1409. }
  1410. return true;
  1411. }
  1412. static void mmf_recalc_uprobes(struct mm_struct *mm)
  1413. {
  1414. struct vm_area_struct *vma;
  1415. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  1416. if (!valid_vma(vma, false))
  1417. continue;
  1418. /*
  1419. * This is not strictly accurate, we can race with
  1420. * uprobe_unregister() and see the already removed
  1421. * uprobe if delete_uprobe() was not yet called.
  1422. * Or this uprobe can be filtered out.
  1423. */
  1424. if (vma_has_uprobes(vma, vma->vm_start, vma->vm_end))
  1425. return;
  1426. }
  1427. clear_bit(MMF_HAS_UPROBES, &mm->flags);
  1428. }
  1429. static int is_trap_at_addr(struct mm_struct *mm, unsigned long vaddr)
  1430. {
  1431. struct page *page;
  1432. uprobe_opcode_t opcode;
  1433. int result;
  1434. pagefault_disable();
  1435. result = __get_user(opcode, (uprobe_opcode_t __user *)vaddr);
  1436. pagefault_enable();
  1437. if (likely(result == 0))
  1438. goto out;
  1439. /*
  1440. * The NULL 'tsk' here ensures that any faults that occur here
  1441. * will not be accounted to the task. 'mm' *is* current->mm,
  1442. * but we treat this as a 'remote' access since it is
  1443. * essentially a kernel access to the memory.
  1444. */
  1445. result = get_user_pages_remote(NULL, mm, vaddr, 1, FOLL_FORCE, &page,
  1446. NULL, NULL);
  1447. if (result < 0)
  1448. return result;
  1449. copy_from_page(page, vaddr, &opcode, UPROBE_SWBP_INSN_SIZE);
  1450. put_page(page);
  1451. out:
  1452. /* This needs to return true for any variant of the trap insn */
  1453. return is_trap_insn(&opcode);
  1454. }
  1455. static struct uprobe *find_active_uprobe(unsigned long bp_vaddr, int *is_swbp)
  1456. {
  1457. struct mm_struct *mm = current->mm;
  1458. struct uprobe *uprobe = NULL;
  1459. struct vm_area_struct *vma;
  1460. down_read(&mm->mmap_sem);
  1461. vma = find_vma(mm, bp_vaddr);
  1462. if (vma && vma->vm_start <= bp_vaddr) {
  1463. if (valid_vma(vma, false)) {
  1464. struct inode *inode = file_inode(vma->vm_file);
  1465. loff_t offset = vaddr_to_offset(vma, bp_vaddr);
  1466. uprobe = find_uprobe(inode, offset);
  1467. }
  1468. if (!uprobe)
  1469. *is_swbp = is_trap_at_addr(mm, bp_vaddr);
  1470. } else {
  1471. *is_swbp = -EFAULT;
  1472. }
  1473. if (!uprobe && test_and_clear_bit(MMF_RECALC_UPROBES, &mm->flags))
  1474. mmf_recalc_uprobes(mm);
  1475. up_read(&mm->mmap_sem);
  1476. return uprobe;
  1477. }
  1478. static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
  1479. {
  1480. struct uprobe_consumer *uc;
  1481. int remove = UPROBE_HANDLER_REMOVE;
  1482. bool need_prep = false; /* prepare return uprobe, when needed */
  1483. down_read(&uprobe->register_rwsem);
  1484. for (uc = uprobe->consumers; uc; uc = uc->next) {
  1485. int rc = 0;
  1486. if (uc->handler) {
  1487. rc = uc->handler(uc, regs);
  1488. WARN(rc & ~UPROBE_HANDLER_MASK,
  1489. "bad rc=0x%x from %pf()\n", rc, uc->handler);
  1490. }
  1491. if (uc->ret_handler)
  1492. need_prep = true;
  1493. remove &= rc;
  1494. }
  1495. if (need_prep && !remove)
  1496. prepare_uretprobe(uprobe, regs); /* put bp at return */
  1497. if (remove && uprobe->consumers) {
  1498. WARN_ON(!uprobe_is_active(uprobe));
  1499. unapply_uprobe(uprobe, current->mm);
  1500. }
  1501. up_read(&uprobe->register_rwsem);
  1502. }
  1503. static void
  1504. handle_uretprobe_chain(struct return_instance *ri, struct pt_regs *regs)
  1505. {
  1506. struct uprobe *uprobe = ri->uprobe;
  1507. struct uprobe_consumer *uc;
  1508. down_read(&uprobe->register_rwsem);
  1509. for (uc = uprobe->consumers; uc; uc = uc->next) {
  1510. if (uc->ret_handler)
  1511. uc->ret_handler(uc, ri->func, regs);
  1512. }
  1513. up_read(&uprobe->register_rwsem);
  1514. }
  1515. static struct return_instance *find_next_ret_chain(struct return_instance *ri)
  1516. {
  1517. bool chained;
  1518. do {
  1519. chained = ri->chained;
  1520. ri = ri->next; /* can't be NULL if chained */
  1521. } while (chained);
  1522. return ri;
  1523. }
  1524. static void handle_trampoline(struct pt_regs *regs)
  1525. {
  1526. struct uprobe_task *utask;
  1527. struct return_instance *ri, *next;
  1528. bool valid;
  1529. utask = current->utask;
  1530. if (!utask)
  1531. goto sigill;
  1532. ri = utask->return_instances;
  1533. if (!ri)
  1534. goto sigill;
  1535. do {
  1536. /*
  1537. * We should throw out the frames invalidated by longjmp().
  1538. * If this chain is valid, then the next one should be alive
  1539. * or NULL; the latter case means that nobody but ri->func
  1540. * could hit this trampoline on return. TODO: sigaltstack().
  1541. */
  1542. next = find_next_ret_chain(ri);
  1543. valid = !next || arch_uretprobe_is_alive(next, RP_CHECK_RET, regs);
  1544. instruction_pointer_set(regs, ri->orig_ret_vaddr);
  1545. do {
  1546. if (valid)
  1547. handle_uretprobe_chain(ri, regs);
  1548. ri = free_ret_instance(ri);
  1549. utask->depth--;
  1550. } while (ri != next);
  1551. } while (!valid);
  1552. utask->return_instances = ri;
  1553. return;
  1554. sigill:
  1555. uprobe_warn(current, "handle uretprobe, sending SIGILL.");
  1556. force_sig_info(SIGILL, SEND_SIG_FORCED, current);
  1557. }
  1558. bool __weak arch_uprobe_ignore(struct arch_uprobe *aup, struct pt_regs *regs)
  1559. {
  1560. return false;
  1561. }
  1562. bool __weak arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx,
  1563. struct pt_regs *regs)
  1564. {
  1565. return true;
  1566. }
  1567. /*
  1568. * Run handler and ask thread to singlestep.
  1569. * Ensure all non-fatal signals cannot interrupt thread while it singlesteps.
  1570. */
  1571. static void handle_swbp(struct pt_regs *regs)
  1572. {
  1573. struct uprobe *uprobe;
  1574. unsigned long bp_vaddr;
  1575. int uninitialized_var(is_swbp);
  1576. bp_vaddr = uprobe_get_swbp_addr(regs);
  1577. if (bp_vaddr == get_trampoline_vaddr())
  1578. return handle_trampoline(regs);
  1579. uprobe = find_active_uprobe(bp_vaddr, &is_swbp);
  1580. if (!uprobe) {
  1581. if (is_swbp > 0) {
  1582. /* No matching uprobe; signal SIGTRAP. */
  1583. send_sig(SIGTRAP, current, 0);
  1584. } else {
  1585. /*
  1586. * Either we raced with uprobe_unregister() or we can't
  1587. * access this memory. The latter is only possible if
  1588. * another thread plays with our ->mm. In both cases
  1589. * we can simply restart. If this vma was unmapped we
  1590. * can pretend this insn was not executed yet and get
  1591. * the (correct) SIGSEGV after restart.
  1592. */
  1593. instruction_pointer_set(regs, bp_vaddr);
  1594. }
  1595. return;
  1596. }
  1597. /* change it in advance for ->handler() and restart */
  1598. instruction_pointer_set(regs, bp_vaddr);
  1599. /*
  1600. * TODO: move copy_insn/etc into _register and remove this hack.
  1601. * After we hit the bp, _unregister + _register can install the
  1602. * new and not-yet-analyzed uprobe at the same address, restart.
  1603. */
  1604. smp_rmb(); /* pairs with wmb() in install_breakpoint() */
  1605. if (unlikely(!test_bit(UPROBE_COPY_INSN, &uprobe->flags)))
  1606. goto out;
  1607. /* Tracing handlers use ->utask to communicate with fetch methods */
  1608. if (!get_utask())
  1609. goto out;
  1610. if (arch_uprobe_ignore(&uprobe->arch, regs))
  1611. goto out;
  1612. handler_chain(uprobe, regs);
  1613. if (arch_uprobe_skip_sstep(&uprobe->arch, regs))
  1614. goto out;
  1615. if (!pre_ssout(uprobe, regs, bp_vaddr))
  1616. return;
  1617. /* arch_uprobe_skip_sstep() succeeded, or restart if can't singlestep */
  1618. out:
  1619. put_uprobe(uprobe);
  1620. }
  1621. /*
  1622. * Perform required fix-ups and disable singlestep.
  1623. * Allow pending signals to take effect.
  1624. */
  1625. static void handle_singlestep(struct uprobe_task *utask, struct pt_regs *regs)
  1626. {
  1627. struct uprobe *uprobe;
  1628. int err = 0;
  1629. uprobe = utask->active_uprobe;
  1630. if (utask->state == UTASK_SSTEP_ACK)
  1631. err = arch_uprobe_post_xol(&uprobe->arch, regs);
  1632. else if (utask->state == UTASK_SSTEP_TRAPPED)
  1633. arch_uprobe_abort_xol(&uprobe->arch, regs);
  1634. else
  1635. WARN_ON_ONCE(1);
  1636. put_uprobe(uprobe);
  1637. utask->active_uprobe = NULL;
  1638. utask->state = UTASK_RUNNING;
  1639. xol_free_insn_slot(current);
  1640. spin_lock_irq(&current->sighand->siglock);
  1641. recalc_sigpending(); /* see uprobe_deny_signal() */
  1642. spin_unlock_irq(&current->sighand->siglock);
  1643. if (unlikely(err)) {
  1644. uprobe_warn(current, "execute the probed insn, sending SIGILL.");
  1645. force_sig_info(SIGILL, SEND_SIG_FORCED, current);
  1646. }
  1647. }
  1648. /*
  1649. * On breakpoint hit, breakpoint notifier sets the TIF_UPROBE flag and
  1650. * allows the thread to return from interrupt. After that handle_swbp()
  1651. * sets utask->active_uprobe.
  1652. *
  1653. * On singlestep exception, singlestep notifier sets the TIF_UPROBE flag
  1654. * and allows the thread to return from interrupt.
  1655. *
  1656. * While returning to userspace, thread notices the TIF_UPROBE flag and calls
  1657. * uprobe_notify_resume().
  1658. */
  1659. void uprobe_notify_resume(struct pt_regs *regs)
  1660. {
  1661. struct uprobe_task *utask;
  1662. clear_thread_flag(TIF_UPROBE);
  1663. utask = current->utask;
  1664. if (utask && utask->active_uprobe)
  1665. handle_singlestep(utask, regs);
  1666. else
  1667. handle_swbp(regs);
  1668. }
  1669. /*
  1670. * uprobe_pre_sstep_notifier gets called from interrupt context as part of
  1671. * notifier mechanism. Set TIF_UPROBE flag and indicate breakpoint hit.
  1672. */
  1673. int uprobe_pre_sstep_notifier(struct pt_regs *regs)
  1674. {
  1675. if (!current->mm)
  1676. return 0;
  1677. if (!test_bit(MMF_HAS_UPROBES, &current->mm->flags) &&
  1678. (!current->utask || !current->utask->return_instances))
  1679. return 0;
  1680. set_thread_flag(TIF_UPROBE);
  1681. return 1;
  1682. }
  1683. /*
  1684. * uprobe_post_sstep_notifier gets called in interrupt context as part of notifier
  1685. * mechanism. Set TIF_UPROBE flag and indicate completion of singlestep.
  1686. */
  1687. int uprobe_post_sstep_notifier(struct pt_regs *regs)
  1688. {
  1689. struct uprobe_task *utask = current->utask;
  1690. if (!current->mm || !utask || !utask->active_uprobe)
  1691. /* task is currently not uprobed */
  1692. return 0;
  1693. utask->state = UTASK_SSTEP_ACK;
  1694. set_thread_flag(TIF_UPROBE);
  1695. return 1;
  1696. }
  1697. static struct notifier_block uprobe_exception_nb = {
  1698. .notifier_call = arch_uprobe_exception_notify,
  1699. .priority = INT_MAX-1, /* notified after kprobes, kgdb */
  1700. };
  1701. static int __init init_uprobes(void)
  1702. {
  1703. int i;
  1704. for (i = 0; i < UPROBES_HASH_SZ; i++)
  1705. mutex_init(&uprobes_mmap_mutex[i]);
  1706. if (percpu_init_rwsem(&dup_mmap_sem))
  1707. return -ENOMEM;
  1708. return register_die_notifier(&uprobe_exception_nb);
  1709. }
  1710. __initcall(init_uprobes);