uprobes.c 50 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048
  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 arch_uprobe *auprobe, struct mm_struct *mm,
  266. unsigned long vaddr, 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(auprobe, 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(auprobe, mm, vaddr,
  324. *(uprobe_opcode_t *)&auprobe->insn);
  325. }
  326. static struct uprobe *get_uprobe(struct uprobe *uprobe)
  327. {
  328. atomic_inc(&uprobe->ref);
  329. return uprobe;
  330. }
  331. static void put_uprobe(struct uprobe *uprobe)
  332. {
  333. if (atomic_dec_and_test(&uprobe->ref))
  334. kfree(uprobe);
  335. }
  336. static int match_uprobe(struct uprobe *l, struct uprobe *r)
  337. {
  338. if (l->inode < r->inode)
  339. return -1;
  340. if (l->inode > r->inode)
  341. return 1;
  342. if (l->offset < r->offset)
  343. return -1;
  344. if (l->offset > r->offset)
  345. return 1;
  346. return 0;
  347. }
  348. static struct uprobe *__find_uprobe(struct inode *inode, loff_t offset)
  349. {
  350. struct uprobe u = { .inode = inode, .offset = offset };
  351. struct rb_node *n = uprobes_tree.rb_node;
  352. struct uprobe *uprobe;
  353. int match;
  354. while (n) {
  355. uprobe = rb_entry(n, struct uprobe, rb_node);
  356. match = match_uprobe(&u, uprobe);
  357. if (!match)
  358. return get_uprobe(uprobe);
  359. if (match < 0)
  360. n = n->rb_left;
  361. else
  362. n = n->rb_right;
  363. }
  364. return NULL;
  365. }
  366. /*
  367. * Find a uprobe corresponding to a given inode:offset
  368. * Acquires uprobes_treelock
  369. */
  370. static struct uprobe *find_uprobe(struct inode *inode, loff_t offset)
  371. {
  372. struct uprobe *uprobe;
  373. spin_lock(&uprobes_treelock);
  374. uprobe = __find_uprobe(inode, offset);
  375. spin_unlock(&uprobes_treelock);
  376. return uprobe;
  377. }
  378. static struct uprobe *__insert_uprobe(struct uprobe *uprobe)
  379. {
  380. struct rb_node **p = &uprobes_tree.rb_node;
  381. struct rb_node *parent = NULL;
  382. struct uprobe *u;
  383. int match;
  384. while (*p) {
  385. parent = *p;
  386. u = rb_entry(parent, struct uprobe, rb_node);
  387. match = match_uprobe(uprobe, u);
  388. if (!match)
  389. return get_uprobe(u);
  390. if (match < 0)
  391. p = &parent->rb_left;
  392. else
  393. p = &parent->rb_right;
  394. }
  395. u = NULL;
  396. rb_link_node(&uprobe->rb_node, parent, p);
  397. rb_insert_color(&uprobe->rb_node, &uprobes_tree);
  398. /* get access + creation ref */
  399. atomic_set(&uprobe->ref, 2);
  400. return u;
  401. }
  402. /*
  403. * Acquire uprobes_treelock.
  404. * Matching uprobe already exists in rbtree;
  405. * increment (access refcount) and return the matching uprobe.
  406. *
  407. * No matching uprobe; insert the uprobe in rb_tree;
  408. * get a double refcount (access + creation) and return NULL.
  409. */
  410. static struct uprobe *insert_uprobe(struct uprobe *uprobe)
  411. {
  412. struct uprobe *u;
  413. spin_lock(&uprobes_treelock);
  414. u = __insert_uprobe(uprobe);
  415. spin_unlock(&uprobes_treelock);
  416. return u;
  417. }
  418. static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset)
  419. {
  420. struct uprobe *uprobe, *cur_uprobe;
  421. uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL);
  422. if (!uprobe)
  423. return NULL;
  424. uprobe->inode = inode;
  425. uprobe->offset = offset;
  426. init_rwsem(&uprobe->register_rwsem);
  427. init_rwsem(&uprobe->consumer_rwsem);
  428. /* add to uprobes_tree, sorted on inode:offset */
  429. cur_uprobe = insert_uprobe(uprobe);
  430. /* a uprobe exists for this inode:offset combination */
  431. if (cur_uprobe) {
  432. kfree(uprobe);
  433. uprobe = cur_uprobe;
  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. /* uprobe_write_opcode() assumes we don't cross page boundary */
  524. BUG_ON((uprobe->offset & ~PAGE_MASK) +
  525. UPROBE_SWBP_INSN_SIZE > PAGE_SIZE);
  526. smp_wmb(); /* pairs with rmb() in find_active_uprobe() */
  527. set_bit(UPROBE_COPY_INSN, &uprobe->flags);
  528. out:
  529. up_write(&uprobe->consumer_rwsem);
  530. return ret;
  531. }
  532. static inline bool consumer_filter(struct uprobe_consumer *uc,
  533. enum uprobe_filter_ctx ctx, struct mm_struct *mm)
  534. {
  535. return !uc->filter || uc->filter(uc, ctx, mm);
  536. }
  537. static bool filter_chain(struct uprobe *uprobe,
  538. enum uprobe_filter_ctx ctx, struct mm_struct *mm)
  539. {
  540. struct uprobe_consumer *uc;
  541. bool ret = false;
  542. down_read(&uprobe->consumer_rwsem);
  543. for (uc = uprobe->consumers; uc; uc = uc->next) {
  544. ret = consumer_filter(uc, ctx, mm);
  545. if (ret)
  546. break;
  547. }
  548. up_read(&uprobe->consumer_rwsem);
  549. return ret;
  550. }
  551. static int
  552. install_breakpoint(struct uprobe *uprobe, struct mm_struct *mm,
  553. struct vm_area_struct *vma, unsigned long vaddr)
  554. {
  555. bool first_uprobe;
  556. int ret;
  557. ret = prepare_uprobe(uprobe, vma->vm_file, mm, vaddr);
  558. if (ret)
  559. return ret;
  560. /*
  561. * set MMF_HAS_UPROBES in advance for uprobe_pre_sstep_notifier(),
  562. * the task can hit this breakpoint right after __replace_page().
  563. */
  564. first_uprobe = !test_bit(MMF_HAS_UPROBES, &mm->flags);
  565. if (first_uprobe)
  566. set_bit(MMF_HAS_UPROBES, &mm->flags);
  567. ret = set_swbp(&uprobe->arch, mm, vaddr);
  568. if (!ret)
  569. clear_bit(MMF_RECALC_UPROBES, &mm->flags);
  570. else if (first_uprobe)
  571. clear_bit(MMF_HAS_UPROBES, &mm->flags);
  572. return ret;
  573. }
  574. static int
  575. remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, unsigned long vaddr)
  576. {
  577. set_bit(MMF_RECALC_UPROBES, &mm->flags);
  578. return set_orig_insn(&uprobe->arch, mm, vaddr);
  579. }
  580. static inline bool uprobe_is_active(struct uprobe *uprobe)
  581. {
  582. return !RB_EMPTY_NODE(&uprobe->rb_node);
  583. }
  584. /*
  585. * There could be threads that have already hit the breakpoint. They
  586. * will recheck the current insn and restart if find_uprobe() fails.
  587. * See find_active_uprobe().
  588. */
  589. static void delete_uprobe(struct uprobe *uprobe)
  590. {
  591. if (WARN_ON(!uprobe_is_active(uprobe)))
  592. return;
  593. spin_lock(&uprobes_treelock);
  594. rb_erase(&uprobe->rb_node, &uprobes_tree);
  595. spin_unlock(&uprobes_treelock);
  596. RB_CLEAR_NODE(&uprobe->rb_node); /* for uprobe_is_active() */
  597. put_uprobe(uprobe);
  598. }
  599. struct map_info {
  600. struct map_info *next;
  601. struct mm_struct *mm;
  602. unsigned long vaddr;
  603. };
  604. static inline struct map_info *free_map_info(struct map_info *info)
  605. {
  606. struct map_info *next = info->next;
  607. kfree(info);
  608. return next;
  609. }
  610. static struct map_info *
  611. build_map_info(struct address_space *mapping, loff_t offset, bool is_register)
  612. {
  613. unsigned long pgoff = offset >> PAGE_SHIFT;
  614. struct vm_area_struct *vma;
  615. struct map_info *curr = NULL;
  616. struct map_info *prev = NULL;
  617. struct map_info *info;
  618. int more = 0;
  619. again:
  620. i_mmap_lock_read(mapping);
  621. vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
  622. if (!valid_vma(vma, is_register))
  623. continue;
  624. if (!prev && !more) {
  625. /*
  626. * Needs GFP_NOWAIT to avoid i_mmap_rwsem recursion through
  627. * reclaim. This is optimistic, no harm done if it fails.
  628. */
  629. prev = kmalloc(sizeof(struct map_info),
  630. GFP_NOWAIT | __GFP_NOMEMALLOC | __GFP_NOWARN);
  631. if (prev)
  632. prev->next = NULL;
  633. }
  634. if (!prev) {
  635. more++;
  636. continue;
  637. }
  638. if (!mmget_not_zero(vma->vm_mm))
  639. continue;
  640. info = prev;
  641. prev = prev->next;
  642. info->next = curr;
  643. curr = info;
  644. info->mm = vma->vm_mm;
  645. info->vaddr = offset_to_vaddr(vma, offset);
  646. }
  647. i_mmap_unlock_read(mapping);
  648. if (!more)
  649. goto out;
  650. prev = curr;
  651. while (curr) {
  652. mmput(curr->mm);
  653. curr = curr->next;
  654. }
  655. do {
  656. info = kmalloc(sizeof(struct map_info), GFP_KERNEL);
  657. if (!info) {
  658. curr = ERR_PTR(-ENOMEM);
  659. goto out;
  660. }
  661. info->next = prev;
  662. prev = info;
  663. } while (--more);
  664. goto again;
  665. out:
  666. while (prev)
  667. prev = free_map_info(prev);
  668. return curr;
  669. }
  670. static int
  671. register_for_each_vma(struct uprobe *uprobe, struct uprobe_consumer *new)
  672. {
  673. bool is_register = !!new;
  674. struct map_info *info;
  675. int err = 0;
  676. percpu_down_write(&dup_mmap_sem);
  677. info = build_map_info(uprobe->inode->i_mapping,
  678. uprobe->offset, is_register);
  679. if (IS_ERR(info)) {
  680. err = PTR_ERR(info);
  681. goto out;
  682. }
  683. while (info) {
  684. struct mm_struct *mm = info->mm;
  685. struct vm_area_struct *vma;
  686. if (err && is_register)
  687. goto free;
  688. down_write(&mm->mmap_sem);
  689. vma = find_vma(mm, info->vaddr);
  690. if (!vma || !valid_vma(vma, is_register) ||
  691. file_inode(vma->vm_file) != uprobe->inode)
  692. goto unlock;
  693. if (vma->vm_start > info->vaddr ||
  694. vaddr_to_offset(vma, info->vaddr) != uprobe->offset)
  695. goto unlock;
  696. if (is_register) {
  697. /* consult only the "caller", new consumer. */
  698. if (consumer_filter(new,
  699. UPROBE_FILTER_REGISTER, mm))
  700. err = install_breakpoint(uprobe, mm, vma, info->vaddr);
  701. } else if (test_bit(MMF_HAS_UPROBES, &mm->flags)) {
  702. if (!filter_chain(uprobe,
  703. UPROBE_FILTER_UNREGISTER, mm))
  704. err |= remove_breakpoint(uprobe, mm, info->vaddr);
  705. }
  706. unlock:
  707. up_write(&mm->mmap_sem);
  708. free:
  709. mmput(mm);
  710. info = free_map_info(info);
  711. }
  712. out:
  713. percpu_up_write(&dup_mmap_sem);
  714. return err;
  715. }
  716. static void
  717. __uprobe_unregister(struct uprobe *uprobe, struct uprobe_consumer *uc)
  718. {
  719. int err;
  720. if (WARN_ON(!consumer_del(uprobe, uc)))
  721. return;
  722. err = register_for_each_vma(uprobe, NULL);
  723. /* TODO : cant unregister? schedule a worker thread */
  724. if (!uprobe->consumers && !err)
  725. delete_uprobe(uprobe);
  726. }
  727. /*
  728. * uprobe_unregister - unregister an already registered probe.
  729. * @inode: the file in which the probe has to be removed.
  730. * @offset: offset from the start of the file.
  731. * @uc: identify which probe if multiple probes are colocated.
  732. */
  733. void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
  734. {
  735. struct uprobe *uprobe;
  736. uprobe = find_uprobe(inode, offset);
  737. if (WARN_ON(!uprobe))
  738. return;
  739. down_write(&uprobe->register_rwsem);
  740. __uprobe_unregister(uprobe, uc);
  741. up_write(&uprobe->register_rwsem);
  742. put_uprobe(uprobe);
  743. }
  744. EXPORT_SYMBOL_GPL(uprobe_unregister);
  745. /*
  746. * __uprobe_register - register a probe
  747. * @inode: the file in which the probe has to be placed.
  748. * @offset: offset from the start of the file.
  749. * @uc: information on howto handle the probe..
  750. *
  751. * Apart from the access refcount, __uprobe_register() takes a creation
  752. * refcount (thro alloc_uprobe) if and only if this @uprobe is getting
  753. * inserted into the rbtree (i.e first consumer for a @inode:@offset
  754. * tuple). Creation refcount stops uprobe_unregister from freeing the
  755. * @uprobe even before the register operation is complete. Creation
  756. * refcount is released when the last @uc for the @uprobe
  757. * unregisters. Caller of __uprobe_register() is required to keep @inode
  758. * (and the containing mount) referenced.
  759. *
  760. * Return errno if it cannot successully install probes
  761. * else return 0 (success)
  762. */
  763. static int __uprobe_register(struct inode *inode, loff_t offset,
  764. struct uprobe_consumer *uc)
  765. {
  766. struct uprobe *uprobe;
  767. int ret;
  768. /* Uprobe must have at least one set consumer */
  769. if (!uc->handler && !uc->ret_handler)
  770. return -EINVAL;
  771. /* copy_insn() uses read_mapping_page() or shmem_read_mapping_page() */
  772. if (!inode->i_mapping->a_ops->readpage && !shmem_mapping(inode->i_mapping))
  773. return -EIO;
  774. /* Racy, just to catch the obvious mistakes */
  775. if (offset > i_size_read(inode))
  776. return -EINVAL;
  777. retry:
  778. uprobe = alloc_uprobe(inode, offset);
  779. if (!uprobe)
  780. return -ENOMEM;
  781. /*
  782. * We can race with uprobe_unregister()->delete_uprobe().
  783. * Check uprobe_is_active() and retry if it is false.
  784. */
  785. down_write(&uprobe->register_rwsem);
  786. ret = -EAGAIN;
  787. if (likely(uprobe_is_active(uprobe))) {
  788. consumer_add(uprobe, uc);
  789. ret = register_for_each_vma(uprobe, uc);
  790. if (ret)
  791. __uprobe_unregister(uprobe, uc);
  792. }
  793. up_write(&uprobe->register_rwsem);
  794. put_uprobe(uprobe);
  795. if (unlikely(ret == -EAGAIN))
  796. goto retry;
  797. return ret;
  798. }
  799. int uprobe_register(struct inode *inode, loff_t offset,
  800. struct uprobe_consumer *uc)
  801. {
  802. return __uprobe_register(inode, offset, uc);
  803. }
  804. EXPORT_SYMBOL_GPL(uprobe_register);
  805. /*
  806. * uprobe_apply - unregister an already registered probe.
  807. * @inode: the file in which the probe has to be removed.
  808. * @offset: offset from the start of the file.
  809. * @uc: consumer which wants to add more or remove some breakpoints
  810. * @add: add or remove the breakpoints
  811. */
  812. int uprobe_apply(struct inode *inode, loff_t offset,
  813. struct uprobe_consumer *uc, bool add)
  814. {
  815. struct uprobe *uprobe;
  816. struct uprobe_consumer *con;
  817. int ret = -ENOENT;
  818. uprobe = find_uprobe(inode, offset);
  819. if (WARN_ON(!uprobe))
  820. return ret;
  821. down_write(&uprobe->register_rwsem);
  822. for (con = uprobe->consumers; con && con != uc ; con = con->next)
  823. ;
  824. if (con)
  825. ret = register_for_each_vma(uprobe, add ? uc : NULL);
  826. up_write(&uprobe->register_rwsem);
  827. put_uprobe(uprobe);
  828. return ret;
  829. }
  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. /* pairs with get_xol_area() */
  998. smp_store_release(&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 = kcalloc(BITS_TO_LONGS(UINSNS_PER_PAGE), sizeof(long),
  1012. GFP_KERNEL);
  1013. if (!area->bitmap)
  1014. goto free_area;
  1015. area->xol_mapping.name = "[uprobes]";
  1016. area->xol_mapping.fault = NULL;
  1017. area->xol_mapping.pages = area->pages;
  1018. area->pages[0] = alloc_page(GFP_HIGHUSER);
  1019. if (!area->pages[0])
  1020. goto free_bitmap;
  1021. area->pages[1] = NULL;
  1022. area->vaddr = vaddr;
  1023. init_waitqueue_head(&area->wq);
  1024. /* Reserve the 1st slot for get_trampoline_vaddr() */
  1025. set_bit(0, area->bitmap);
  1026. atomic_set(&area->slot_count, 1);
  1027. arch_uprobe_copy_ixol(area->pages[0], 0, &insn, UPROBE_SWBP_INSN_SIZE);
  1028. if (!xol_add_vma(mm, area))
  1029. return area;
  1030. __free_page(area->pages[0]);
  1031. free_bitmap:
  1032. kfree(area->bitmap);
  1033. free_area:
  1034. kfree(area);
  1035. out:
  1036. return NULL;
  1037. }
  1038. /*
  1039. * get_xol_area - Allocate process's xol_area if necessary.
  1040. * This area will be used for storing instructions for execution out of line.
  1041. *
  1042. * Returns the allocated area or NULL.
  1043. */
  1044. static struct xol_area *get_xol_area(void)
  1045. {
  1046. struct mm_struct *mm = current->mm;
  1047. struct xol_area *area;
  1048. if (!mm->uprobes_state.xol_area)
  1049. __create_xol_area(0);
  1050. /* Pairs with xol_add_vma() smp_store_release() */
  1051. area = READ_ONCE(mm->uprobes_state.xol_area); /* ^^^ */
  1052. return area;
  1053. }
  1054. /*
  1055. * uprobe_clear_state - Free the area allocated for slots.
  1056. */
  1057. void uprobe_clear_state(struct mm_struct *mm)
  1058. {
  1059. struct xol_area *area = mm->uprobes_state.xol_area;
  1060. if (!area)
  1061. return;
  1062. put_page(area->pages[0]);
  1063. kfree(area->bitmap);
  1064. kfree(area);
  1065. }
  1066. void uprobe_start_dup_mmap(void)
  1067. {
  1068. percpu_down_read(&dup_mmap_sem);
  1069. }
  1070. void uprobe_end_dup_mmap(void)
  1071. {
  1072. percpu_up_read(&dup_mmap_sem);
  1073. }
  1074. void uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm)
  1075. {
  1076. if (test_bit(MMF_HAS_UPROBES, &oldmm->flags)) {
  1077. set_bit(MMF_HAS_UPROBES, &newmm->flags);
  1078. /* unconditionally, dup_mmap() skips VM_DONTCOPY vmas */
  1079. set_bit(MMF_RECALC_UPROBES, &newmm->flags);
  1080. }
  1081. }
  1082. /*
  1083. * - search for a free slot.
  1084. */
  1085. static unsigned long xol_take_insn_slot(struct xol_area *area)
  1086. {
  1087. unsigned long slot_addr;
  1088. int slot_nr;
  1089. do {
  1090. slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE);
  1091. if (slot_nr < UINSNS_PER_PAGE) {
  1092. if (!test_and_set_bit(slot_nr, area->bitmap))
  1093. break;
  1094. slot_nr = UINSNS_PER_PAGE;
  1095. continue;
  1096. }
  1097. wait_event(area->wq, (atomic_read(&area->slot_count) < UINSNS_PER_PAGE));
  1098. } while (slot_nr >= UINSNS_PER_PAGE);
  1099. slot_addr = area->vaddr + (slot_nr * UPROBE_XOL_SLOT_BYTES);
  1100. atomic_inc(&area->slot_count);
  1101. return slot_addr;
  1102. }
  1103. /*
  1104. * xol_get_insn_slot - allocate a slot for xol.
  1105. * Returns the allocated slot address or 0.
  1106. */
  1107. static unsigned long xol_get_insn_slot(struct uprobe *uprobe)
  1108. {
  1109. struct xol_area *area;
  1110. unsigned long xol_vaddr;
  1111. area = get_xol_area();
  1112. if (!area)
  1113. return 0;
  1114. xol_vaddr = xol_take_insn_slot(area);
  1115. if (unlikely(!xol_vaddr))
  1116. return 0;
  1117. arch_uprobe_copy_ixol(area->pages[0], xol_vaddr,
  1118. &uprobe->arch.ixol, sizeof(uprobe->arch.ixol));
  1119. return xol_vaddr;
  1120. }
  1121. /*
  1122. * xol_free_insn_slot - If slot was earlier allocated by
  1123. * @xol_get_insn_slot(), make the slot available for
  1124. * subsequent requests.
  1125. */
  1126. static void xol_free_insn_slot(struct task_struct *tsk)
  1127. {
  1128. struct xol_area *area;
  1129. unsigned long vma_end;
  1130. unsigned long slot_addr;
  1131. if (!tsk->mm || !tsk->mm->uprobes_state.xol_area || !tsk->utask)
  1132. return;
  1133. slot_addr = tsk->utask->xol_vaddr;
  1134. if (unlikely(!slot_addr))
  1135. return;
  1136. area = tsk->mm->uprobes_state.xol_area;
  1137. vma_end = area->vaddr + PAGE_SIZE;
  1138. if (area->vaddr <= slot_addr && slot_addr < vma_end) {
  1139. unsigned long offset;
  1140. int slot_nr;
  1141. offset = slot_addr - area->vaddr;
  1142. slot_nr = offset / UPROBE_XOL_SLOT_BYTES;
  1143. if (slot_nr >= UINSNS_PER_PAGE)
  1144. return;
  1145. clear_bit(slot_nr, area->bitmap);
  1146. atomic_dec(&area->slot_count);
  1147. smp_mb__after_atomic(); /* pairs with prepare_to_wait() */
  1148. if (waitqueue_active(&area->wq))
  1149. wake_up(&area->wq);
  1150. tsk->utask->xol_vaddr = 0;
  1151. }
  1152. }
  1153. void __weak arch_uprobe_copy_ixol(struct page *page, unsigned long vaddr,
  1154. void *src, unsigned long len)
  1155. {
  1156. /* Initialize the slot */
  1157. copy_to_page(page, vaddr, src, len);
  1158. /*
  1159. * We probably need flush_icache_user_range() but it needs vma.
  1160. * This should work on most of architectures by default. If
  1161. * architecture needs to do something different it can define
  1162. * its own version of the function.
  1163. */
  1164. flush_dcache_page(page);
  1165. }
  1166. /**
  1167. * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
  1168. * @regs: Reflects the saved state of the task after it has hit a breakpoint
  1169. * instruction.
  1170. * Return the address of the breakpoint instruction.
  1171. */
  1172. unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs)
  1173. {
  1174. return instruction_pointer(regs) - UPROBE_SWBP_INSN_SIZE;
  1175. }
  1176. unsigned long uprobe_get_trap_addr(struct pt_regs *regs)
  1177. {
  1178. struct uprobe_task *utask = current->utask;
  1179. if (unlikely(utask && utask->active_uprobe))
  1180. return utask->vaddr;
  1181. return instruction_pointer(regs);
  1182. }
  1183. static struct return_instance *free_ret_instance(struct return_instance *ri)
  1184. {
  1185. struct return_instance *next = ri->next;
  1186. put_uprobe(ri->uprobe);
  1187. kfree(ri);
  1188. return next;
  1189. }
  1190. /*
  1191. * Called with no locks held.
  1192. * Called in context of an exiting or an exec-ing thread.
  1193. */
  1194. void uprobe_free_utask(struct task_struct *t)
  1195. {
  1196. struct uprobe_task *utask = t->utask;
  1197. struct return_instance *ri;
  1198. if (!utask)
  1199. return;
  1200. if (utask->active_uprobe)
  1201. put_uprobe(utask->active_uprobe);
  1202. ri = utask->return_instances;
  1203. while (ri)
  1204. ri = free_ret_instance(ri);
  1205. xol_free_insn_slot(t);
  1206. kfree(utask);
  1207. t->utask = NULL;
  1208. }
  1209. /*
  1210. * Allocate a uprobe_task object for the task if if necessary.
  1211. * Called when the thread hits a breakpoint.
  1212. *
  1213. * Returns:
  1214. * - pointer to new uprobe_task on success
  1215. * - NULL otherwise
  1216. */
  1217. static struct uprobe_task *get_utask(void)
  1218. {
  1219. if (!current->utask)
  1220. current->utask = kzalloc(sizeof(struct uprobe_task), GFP_KERNEL);
  1221. return current->utask;
  1222. }
  1223. static int dup_utask(struct task_struct *t, struct uprobe_task *o_utask)
  1224. {
  1225. struct uprobe_task *n_utask;
  1226. struct return_instance **p, *o, *n;
  1227. n_utask = kzalloc(sizeof(struct uprobe_task), GFP_KERNEL);
  1228. if (!n_utask)
  1229. return -ENOMEM;
  1230. t->utask = n_utask;
  1231. p = &n_utask->return_instances;
  1232. for (o = o_utask->return_instances; o; o = o->next) {
  1233. n = kmalloc(sizeof(struct return_instance), GFP_KERNEL);
  1234. if (!n)
  1235. return -ENOMEM;
  1236. *n = *o;
  1237. get_uprobe(n->uprobe);
  1238. n->next = NULL;
  1239. *p = n;
  1240. p = &n->next;
  1241. n_utask->depth++;
  1242. }
  1243. return 0;
  1244. }
  1245. static void uprobe_warn(struct task_struct *t, const char *msg)
  1246. {
  1247. pr_warn("uprobe: %s:%d failed to %s\n",
  1248. current->comm, current->pid, msg);
  1249. }
  1250. static void dup_xol_work(struct callback_head *work)
  1251. {
  1252. if (current->flags & PF_EXITING)
  1253. return;
  1254. if (!__create_xol_area(current->utask->dup_xol_addr) &&
  1255. !fatal_signal_pending(current))
  1256. uprobe_warn(current, "dup xol area");
  1257. }
  1258. /*
  1259. * Called in context of a new clone/fork from copy_process.
  1260. */
  1261. void uprobe_copy_process(struct task_struct *t, unsigned long flags)
  1262. {
  1263. struct uprobe_task *utask = current->utask;
  1264. struct mm_struct *mm = current->mm;
  1265. struct xol_area *area;
  1266. t->utask = NULL;
  1267. if (!utask || !utask->return_instances)
  1268. return;
  1269. if (mm == t->mm && !(flags & CLONE_VFORK))
  1270. return;
  1271. if (dup_utask(t, utask))
  1272. return uprobe_warn(t, "dup ret instances");
  1273. /* The task can fork() after dup_xol_work() fails */
  1274. area = mm->uprobes_state.xol_area;
  1275. if (!area)
  1276. return uprobe_warn(t, "dup xol area");
  1277. if (mm == t->mm)
  1278. return;
  1279. t->utask->dup_xol_addr = area->vaddr;
  1280. init_task_work(&t->utask->dup_xol_work, dup_xol_work);
  1281. task_work_add(t, &t->utask->dup_xol_work, true);
  1282. }
  1283. /*
  1284. * Current area->vaddr notion assume the trampoline address is always
  1285. * equal area->vaddr.
  1286. *
  1287. * Returns -1 in case the xol_area is not allocated.
  1288. */
  1289. static unsigned long get_trampoline_vaddr(void)
  1290. {
  1291. struct xol_area *area;
  1292. unsigned long trampoline_vaddr = -1;
  1293. /* Pairs with xol_add_vma() smp_store_release() */
  1294. area = READ_ONCE(current->mm->uprobes_state.xol_area); /* ^^^ */
  1295. if (area)
  1296. trampoline_vaddr = area->vaddr;
  1297. return trampoline_vaddr;
  1298. }
  1299. static void cleanup_return_instances(struct uprobe_task *utask, bool chained,
  1300. struct pt_regs *regs)
  1301. {
  1302. struct return_instance *ri = utask->return_instances;
  1303. enum rp_check ctx = chained ? RP_CHECK_CHAIN_CALL : RP_CHECK_CALL;
  1304. while (ri && !arch_uretprobe_is_alive(ri, ctx, regs)) {
  1305. ri = free_ret_instance(ri);
  1306. utask->depth--;
  1307. }
  1308. utask->return_instances = ri;
  1309. }
  1310. static void prepare_uretprobe(struct uprobe *uprobe, struct pt_regs *regs)
  1311. {
  1312. struct return_instance *ri;
  1313. struct uprobe_task *utask;
  1314. unsigned long orig_ret_vaddr, trampoline_vaddr;
  1315. bool chained;
  1316. if (!get_xol_area())
  1317. return;
  1318. utask = get_utask();
  1319. if (!utask)
  1320. return;
  1321. if (utask->depth >= MAX_URETPROBE_DEPTH) {
  1322. printk_ratelimited(KERN_INFO "uprobe: omit uretprobe due to"
  1323. " nestedness limit pid/tgid=%d/%d\n",
  1324. current->pid, current->tgid);
  1325. return;
  1326. }
  1327. ri = kmalloc(sizeof(struct return_instance), GFP_KERNEL);
  1328. if (!ri)
  1329. return;
  1330. trampoline_vaddr = get_trampoline_vaddr();
  1331. orig_ret_vaddr = arch_uretprobe_hijack_return_addr(trampoline_vaddr, regs);
  1332. if (orig_ret_vaddr == -1)
  1333. goto fail;
  1334. /* drop the entries invalidated by longjmp() */
  1335. chained = (orig_ret_vaddr == trampoline_vaddr);
  1336. cleanup_return_instances(utask, chained, regs);
  1337. /*
  1338. * We don't want to keep trampoline address in stack, rather keep the
  1339. * original return address of first caller thru all the consequent
  1340. * instances. This also makes breakpoint unwrapping easier.
  1341. */
  1342. if (chained) {
  1343. if (!utask->return_instances) {
  1344. /*
  1345. * This situation is not possible. Likely we have an
  1346. * attack from user-space.
  1347. */
  1348. uprobe_warn(current, "handle tail call");
  1349. goto fail;
  1350. }
  1351. orig_ret_vaddr = utask->return_instances->orig_ret_vaddr;
  1352. }
  1353. ri->uprobe = get_uprobe(uprobe);
  1354. ri->func = instruction_pointer(regs);
  1355. ri->stack = user_stack_pointer(regs);
  1356. ri->orig_ret_vaddr = orig_ret_vaddr;
  1357. ri->chained = chained;
  1358. utask->depth++;
  1359. ri->next = utask->return_instances;
  1360. utask->return_instances = ri;
  1361. return;
  1362. fail:
  1363. kfree(ri);
  1364. }
  1365. /* Prepare to single-step probed instruction out of line. */
  1366. static int
  1367. pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long bp_vaddr)
  1368. {
  1369. struct uprobe_task *utask;
  1370. unsigned long xol_vaddr;
  1371. int err;
  1372. utask = get_utask();
  1373. if (!utask)
  1374. return -ENOMEM;
  1375. xol_vaddr = xol_get_insn_slot(uprobe);
  1376. if (!xol_vaddr)
  1377. return -ENOMEM;
  1378. utask->xol_vaddr = xol_vaddr;
  1379. utask->vaddr = bp_vaddr;
  1380. err = arch_uprobe_pre_xol(&uprobe->arch, regs);
  1381. if (unlikely(err)) {
  1382. xol_free_insn_slot(current);
  1383. return err;
  1384. }
  1385. utask->active_uprobe = uprobe;
  1386. utask->state = UTASK_SSTEP;
  1387. return 0;
  1388. }
  1389. /*
  1390. * If we are singlestepping, then ensure this thread is not connected to
  1391. * non-fatal signals until completion of singlestep. When xol insn itself
  1392. * triggers the signal, restart the original insn even if the task is
  1393. * already SIGKILL'ed (since coredump should report the correct ip). This
  1394. * is even more important if the task has a handler for SIGSEGV/etc, The
  1395. * _same_ instruction should be repeated again after return from the signal
  1396. * handler, and SSTEP can never finish in this case.
  1397. */
  1398. bool uprobe_deny_signal(void)
  1399. {
  1400. struct task_struct *t = current;
  1401. struct uprobe_task *utask = t->utask;
  1402. if (likely(!utask || !utask->active_uprobe))
  1403. return false;
  1404. WARN_ON_ONCE(utask->state != UTASK_SSTEP);
  1405. if (signal_pending(t)) {
  1406. spin_lock_irq(&t->sighand->siglock);
  1407. clear_tsk_thread_flag(t, TIF_SIGPENDING);
  1408. spin_unlock_irq(&t->sighand->siglock);
  1409. if (__fatal_signal_pending(t) || arch_uprobe_xol_was_trapped(t)) {
  1410. utask->state = UTASK_SSTEP_TRAPPED;
  1411. set_tsk_thread_flag(t, TIF_UPROBE);
  1412. }
  1413. }
  1414. return true;
  1415. }
  1416. static void mmf_recalc_uprobes(struct mm_struct *mm)
  1417. {
  1418. struct vm_area_struct *vma;
  1419. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  1420. if (!valid_vma(vma, false))
  1421. continue;
  1422. /*
  1423. * This is not strictly accurate, we can race with
  1424. * uprobe_unregister() and see the already removed
  1425. * uprobe if delete_uprobe() was not yet called.
  1426. * Or this uprobe can be filtered out.
  1427. */
  1428. if (vma_has_uprobes(vma, vma->vm_start, vma->vm_end))
  1429. return;
  1430. }
  1431. clear_bit(MMF_HAS_UPROBES, &mm->flags);
  1432. }
  1433. static int is_trap_at_addr(struct mm_struct *mm, unsigned long vaddr)
  1434. {
  1435. struct page *page;
  1436. uprobe_opcode_t opcode;
  1437. int result;
  1438. pagefault_disable();
  1439. result = __get_user(opcode, (uprobe_opcode_t __user *)vaddr);
  1440. pagefault_enable();
  1441. if (likely(result == 0))
  1442. goto out;
  1443. /*
  1444. * The NULL 'tsk' here ensures that any faults that occur here
  1445. * will not be accounted to the task. 'mm' *is* current->mm,
  1446. * but we treat this as a 'remote' access since it is
  1447. * essentially a kernel access to the memory.
  1448. */
  1449. result = get_user_pages_remote(NULL, mm, vaddr, 1, FOLL_FORCE, &page,
  1450. NULL, NULL);
  1451. if (result < 0)
  1452. return result;
  1453. copy_from_page(page, vaddr, &opcode, UPROBE_SWBP_INSN_SIZE);
  1454. put_page(page);
  1455. out:
  1456. /* This needs to return true for any variant of the trap insn */
  1457. return is_trap_insn(&opcode);
  1458. }
  1459. static struct uprobe *find_active_uprobe(unsigned long bp_vaddr, int *is_swbp)
  1460. {
  1461. struct mm_struct *mm = current->mm;
  1462. struct uprobe *uprobe = NULL;
  1463. struct vm_area_struct *vma;
  1464. down_read(&mm->mmap_sem);
  1465. vma = find_vma(mm, bp_vaddr);
  1466. if (vma && vma->vm_start <= bp_vaddr) {
  1467. if (valid_vma(vma, false)) {
  1468. struct inode *inode = file_inode(vma->vm_file);
  1469. loff_t offset = vaddr_to_offset(vma, bp_vaddr);
  1470. uprobe = find_uprobe(inode, offset);
  1471. }
  1472. if (!uprobe)
  1473. *is_swbp = is_trap_at_addr(mm, bp_vaddr);
  1474. } else {
  1475. *is_swbp = -EFAULT;
  1476. }
  1477. if (!uprobe && test_and_clear_bit(MMF_RECALC_UPROBES, &mm->flags))
  1478. mmf_recalc_uprobes(mm);
  1479. up_read(&mm->mmap_sem);
  1480. return uprobe;
  1481. }
  1482. static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
  1483. {
  1484. struct uprobe_consumer *uc;
  1485. int remove = UPROBE_HANDLER_REMOVE;
  1486. bool need_prep = false; /* prepare return uprobe, when needed */
  1487. down_read(&uprobe->register_rwsem);
  1488. for (uc = uprobe->consumers; uc; uc = uc->next) {
  1489. int rc = 0;
  1490. if (uc->handler) {
  1491. rc = uc->handler(uc, regs);
  1492. WARN(rc & ~UPROBE_HANDLER_MASK,
  1493. "bad rc=0x%x from %pf()\n", rc, uc->handler);
  1494. }
  1495. if (uc->ret_handler)
  1496. need_prep = true;
  1497. remove &= rc;
  1498. }
  1499. if (need_prep && !remove)
  1500. prepare_uretprobe(uprobe, regs); /* put bp at return */
  1501. if (remove && uprobe->consumers) {
  1502. WARN_ON(!uprobe_is_active(uprobe));
  1503. unapply_uprobe(uprobe, current->mm);
  1504. }
  1505. up_read(&uprobe->register_rwsem);
  1506. }
  1507. static void
  1508. handle_uretprobe_chain(struct return_instance *ri, struct pt_regs *regs)
  1509. {
  1510. struct uprobe *uprobe = ri->uprobe;
  1511. struct uprobe_consumer *uc;
  1512. down_read(&uprobe->register_rwsem);
  1513. for (uc = uprobe->consumers; uc; uc = uc->next) {
  1514. if (uc->ret_handler)
  1515. uc->ret_handler(uc, ri->func, regs);
  1516. }
  1517. up_read(&uprobe->register_rwsem);
  1518. }
  1519. static struct return_instance *find_next_ret_chain(struct return_instance *ri)
  1520. {
  1521. bool chained;
  1522. do {
  1523. chained = ri->chained;
  1524. ri = ri->next; /* can't be NULL if chained */
  1525. } while (chained);
  1526. return ri;
  1527. }
  1528. static void handle_trampoline(struct pt_regs *regs)
  1529. {
  1530. struct uprobe_task *utask;
  1531. struct return_instance *ri, *next;
  1532. bool valid;
  1533. utask = current->utask;
  1534. if (!utask)
  1535. goto sigill;
  1536. ri = utask->return_instances;
  1537. if (!ri)
  1538. goto sigill;
  1539. do {
  1540. /*
  1541. * We should throw out the frames invalidated by longjmp().
  1542. * If this chain is valid, then the next one should be alive
  1543. * or NULL; the latter case means that nobody but ri->func
  1544. * could hit this trampoline on return. TODO: sigaltstack().
  1545. */
  1546. next = find_next_ret_chain(ri);
  1547. valid = !next || arch_uretprobe_is_alive(next, RP_CHECK_RET, regs);
  1548. instruction_pointer_set(regs, ri->orig_ret_vaddr);
  1549. do {
  1550. if (valid)
  1551. handle_uretprobe_chain(ri, regs);
  1552. ri = free_ret_instance(ri);
  1553. utask->depth--;
  1554. } while (ri != next);
  1555. } while (!valid);
  1556. utask->return_instances = ri;
  1557. return;
  1558. sigill:
  1559. uprobe_warn(current, "handle uretprobe, sending SIGILL.");
  1560. force_sig_info(SIGILL, SEND_SIG_FORCED, current);
  1561. }
  1562. bool __weak arch_uprobe_ignore(struct arch_uprobe *aup, struct pt_regs *regs)
  1563. {
  1564. return false;
  1565. }
  1566. bool __weak arch_uretprobe_is_alive(struct return_instance *ret, enum rp_check ctx,
  1567. struct pt_regs *regs)
  1568. {
  1569. return true;
  1570. }
  1571. /*
  1572. * Run handler and ask thread to singlestep.
  1573. * Ensure all non-fatal signals cannot interrupt thread while it singlesteps.
  1574. */
  1575. static void handle_swbp(struct pt_regs *regs)
  1576. {
  1577. struct uprobe *uprobe;
  1578. unsigned long bp_vaddr;
  1579. int uninitialized_var(is_swbp);
  1580. bp_vaddr = uprobe_get_swbp_addr(regs);
  1581. if (bp_vaddr == get_trampoline_vaddr())
  1582. return handle_trampoline(regs);
  1583. uprobe = find_active_uprobe(bp_vaddr, &is_swbp);
  1584. if (!uprobe) {
  1585. if (is_swbp > 0) {
  1586. /* No matching uprobe; signal SIGTRAP. */
  1587. send_sig(SIGTRAP, current, 0);
  1588. } else {
  1589. /*
  1590. * Either we raced with uprobe_unregister() or we can't
  1591. * access this memory. The latter is only possible if
  1592. * another thread plays with our ->mm. In both cases
  1593. * we can simply restart. If this vma was unmapped we
  1594. * can pretend this insn was not executed yet and get
  1595. * the (correct) SIGSEGV after restart.
  1596. */
  1597. instruction_pointer_set(regs, bp_vaddr);
  1598. }
  1599. return;
  1600. }
  1601. /* change it in advance for ->handler() and restart */
  1602. instruction_pointer_set(regs, bp_vaddr);
  1603. /*
  1604. * TODO: move copy_insn/etc into _register and remove this hack.
  1605. * After we hit the bp, _unregister + _register can install the
  1606. * new and not-yet-analyzed uprobe at the same address, restart.
  1607. */
  1608. smp_rmb(); /* pairs with wmb() in install_breakpoint() */
  1609. if (unlikely(!test_bit(UPROBE_COPY_INSN, &uprobe->flags)))
  1610. goto out;
  1611. /* Tracing handlers use ->utask to communicate with fetch methods */
  1612. if (!get_utask())
  1613. goto out;
  1614. if (arch_uprobe_ignore(&uprobe->arch, regs))
  1615. goto out;
  1616. handler_chain(uprobe, regs);
  1617. if (arch_uprobe_skip_sstep(&uprobe->arch, regs))
  1618. goto out;
  1619. if (!pre_ssout(uprobe, regs, bp_vaddr))
  1620. return;
  1621. /* arch_uprobe_skip_sstep() succeeded, or restart if can't singlestep */
  1622. out:
  1623. put_uprobe(uprobe);
  1624. }
  1625. /*
  1626. * Perform required fix-ups and disable singlestep.
  1627. * Allow pending signals to take effect.
  1628. */
  1629. static void handle_singlestep(struct uprobe_task *utask, struct pt_regs *regs)
  1630. {
  1631. struct uprobe *uprobe;
  1632. int err = 0;
  1633. uprobe = utask->active_uprobe;
  1634. if (utask->state == UTASK_SSTEP_ACK)
  1635. err = arch_uprobe_post_xol(&uprobe->arch, regs);
  1636. else if (utask->state == UTASK_SSTEP_TRAPPED)
  1637. arch_uprobe_abort_xol(&uprobe->arch, regs);
  1638. else
  1639. WARN_ON_ONCE(1);
  1640. put_uprobe(uprobe);
  1641. utask->active_uprobe = NULL;
  1642. utask->state = UTASK_RUNNING;
  1643. xol_free_insn_slot(current);
  1644. spin_lock_irq(&current->sighand->siglock);
  1645. recalc_sigpending(); /* see uprobe_deny_signal() */
  1646. spin_unlock_irq(&current->sighand->siglock);
  1647. if (unlikely(err)) {
  1648. uprobe_warn(current, "execute the probed insn, sending SIGILL.");
  1649. force_sig_info(SIGILL, SEND_SIG_FORCED, current);
  1650. }
  1651. }
  1652. /*
  1653. * On breakpoint hit, breakpoint notifier sets the TIF_UPROBE flag and
  1654. * allows the thread to return from interrupt. After that handle_swbp()
  1655. * sets utask->active_uprobe.
  1656. *
  1657. * On singlestep exception, singlestep notifier sets the TIF_UPROBE flag
  1658. * and allows the thread to return from interrupt.
  1659. *
  1660. * While returning to userspace, thread notices the TIF_UPROBE flag and calls
  1661. * uprobe_notify_resume().
  1662. */
  1663. void uprobe_notify_resume(struct pt_regs *regs)
  1664. {
  1665. struct uprobe_task *utask;
  1666. clear_thread_flag(TIF_UPROBE);
  1667. utask = current->utask;
  1668. if (utask && utask->active_uprobe)
  1669. handle_singlestep(utask, regs);
  1670. else
  1671. handle_swbp(regs);
  1672. }
  1673. /*
  1674. * uprobe_pre_sstep_notifier gets called from interrupt context as part of
  1675. * notifier mechanism. Set TIF_UPROBE flag and indicate breakpoint hit.
  1676. */
  1677. int uprobe_pre_sstep_notifier(struct pt_regs *regs)
  1678. {
  1679. if (!current->mm)
  1680. return 0;
  1681. if (!test_bit(MMF_HAS_UPROBES, &current->mm->flags) &&
  1682. (!current->utask || !current->utask->return_instances))
  1683. return 0;
  1684. set_thread_flag(TIF_UPROBE);
  1685. return 1;
  1686. }
  1687. /*
  1688. * uprobe_post_sstep_notifier gets called in interrupt context as part of notifier
  1689. * mechanism. Set TIF_UPROBE flag and indicate completion of singlestep.
  1690. */
  1691. int uprobe_post_sstep_notifier(struct pt_regs *regs)
  1692. {
  1693. struct uprobe_task *utask = current->utask;
  1694. if (!current->mm || !utask || !utask->active_uprobe)
  1695. /* task is currently not uprobed */
  1696. return 0;
  1697. utask->state = UTASK_SSTEP_ACK;
  1698. set_thread_flag(TIF_UPROBE);
  1699. return 1;
  1700. }
  1701. static struct notifier_block uprobe_exception_nb = {
  1702. .notifier_call = arch_uprobe_exception_notify,
  1703. .priority = INT_MAX-1, /* notified after kprobes, kgdb */
  1704. };
  1705. static int __init init_uprobes(void)
  1706. {
  1707. int i;
  1708. for (i = 0; i < UPROBES_HASH_SZ; i++)
  1709. mutex_init(&uprobes_mmap_mutex[i]);
  1710. if (percpu_init_rwsem(&dup_mmap_sem))
  1711. return -ENOMEM;
  1712. return register_die_notifier(&uprobe_exception_nb);
  1713. }
  1714. __initcall(init_uprobes);