uprobes.c 50 KB

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