uprobes.c 50 KB

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