uprobes.c 49 KB

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