uprobes.c 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569
  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/rmap.h> /* anon_vma_prepare */
  30. #include <linux/mmu_notifier.h> /* set_pte_at_notify */
  31. #include <linux/swap.h> /* try_to_free_swap */
  32. #include <linux/ptrace.h> /* user_enable_single_step */
  33. #include <linux/kdebug.h> /* notifier mechanism */
  34. #include "../../mm/internal.h" /* munlock_vma_page */
  35. #include <linux/uprobes.h>
  36. #define UINSNS_PER_PAGE (PAGE_SIZE/UPROBE_XOL_SLOT_BYTES)
  37. #define MAX_UPROBE_XOL_SLOTS UINSNS_PER_PAGE
  38. static struct rb_root uprobes_tree = RB_ROOT;
  39. static DEFINE_SPINLOCK(uprobes_treelock); /* serialize rbtree access */
  40. #define UPROBES_HASH_SZ 13
  41. /*
  42. * We need separate register/unregister and mmap/munmap lock hashes because
  43. * of mmap_sem nesting.
  44. *
  45. * uprobe_register() needs to install probes on (potentially) all processes
  46. * and thus needs to acquire multiple mmap_sems (consequtively, not
  47. * concurrently), whereas uprobe_mmap() is called while holding mmap_sem
  48. * for the particular process doing the mmap.
  49. *
  50. * uprobe_register()->register_for_each_vma() needs to drop/acquire mmap_sem
  51. * because of lock order against i_mmap_mutex. This means there's a hole in
  52. * the register vma iteration where a mmap() can happen.
  53. *
  54. * Thus uprobe_register() can race with uprobe_mmap() and we can try and
  55. * install a probe where one is already installed.
  56. */
  57. /* serialize (un)register */
  58. static struct mutex uprobes_mutex[UPROBES_HASH_SZ];
  59. #define uprobes_hash(v) (&uprobes_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
  60. /* serialize uprobe->pending_list */
  61. static struct mutex uprobes_mmap_mutex[UPROBES_HASH_SZ];
  62. #define uprobes_mmap_hash(v) (&uprobes_mmap_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
  63. /*
  64. * uprobe_events allows us to skip the uprobe_mmap if there are no uprobe
  65. * events active at this time. Probably a fine grained per inode count is
  66. * better?
  67. */
  68. static atomic_t uprobe_events = ATOMIC_INIT(0);
  69. struct uprobe {
  70. struct rb_node rb_node; /* node in the rb tree */
  71. atomic_t ref;
  72. struct rw_semaphore consumer_rwsem;
  73. struct list_head pending_list;
  74. struct uprobe_consumer *consumers;
  75. struct inode *inode; /* Also hold a ref to inode */
  76. loff_t offset;
  77. int flags;
  78. struct arch_uprobe arch;
  79. };
  80. /*
  81. * valid_vma: Verify if the specified vma is an executable vma
  82. * Relax restrictions while unregistering: vm_flags might have
  83. * changed after breakpoint was inserted.
  84. * - is_register: indicates if we are in register context.
  85. * - Return 1 if the specified virtual address is in an
  86. * executable vma.
  87. */
  88. static bool valid_vma(struct vm_area_struct *vma, bool is_register)
  89. {
  90. vm_flags_t flags = VM_HUGETLB | VM_MAYEXEC | VM_SHARED;
  91. if (is_register)
  92. flags |= VM_WRITE;
  93. return vma->vm_file && (vma->vm_flags & flags) == VM_MAYEXEC;
  94. }
  95. static unsigned long offset_to_vaddr(struct vm_area_struct *vma, loff_t offset)
  96. {
  97. return vma->vm_start + offset - ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
  98. }
  99. static loff_t vaddr_to_offset(struct vm_area_struct *vma, unsigned long vaddr)
  100. {
  101. return ((loff_t)vma->vm_pgoff << PAGE_SHIFT) + (vaddr - vma->vm_start);
  102. }
  103. /**
  104. * __replace_page - replace page in vma by new page.
  105. * based on replace_page in mm/ksm.c
  106. *
  107. * @vma: vma that holds the pte pointing to page
  108. * @addr: address the old @page is mapped at
  109. * @page: the cowed page we are replacing by kpage
  110. * @kpage: the modified page we replace page by
  111. *
  112. * Returns 0 on success, -EFAULT on failure.
  113. */
  114. static int __replace_page(struct vm_area_struct *vma, unsigned long addr,
  115. struct page *page, struct page *kpage)
  116. {
  117. struct mm_struct *mm = vma->vm_mm;
  118. spinlock_t *ptl;
  119. pte_t *ptep;
  120. int err;
  121. /* For try_to_free_swap() and munlock_vma_page() below */
  122. lock_page(page);
  123. err = -EAGAIN;
  124. ptep = page_check_address(page, mm, addr, &ptl, 0);
  125. if (!ptep)
  126. goto unlock;
  127. get_page(kpage);
  128. page_add_new_anon_rmap(kpage, vma, addr);
  129. if (!PageAnon(page)) {
  130. dec_mm_counter(mm, MM_FILEPAGES);
  131. inc_mm_counter(mm, MM_ANONPAGES);
  132. }
  133. flush_cache_page(vma, addr, pte_pfn(*ptep));
  134. ptep_clear_flush(vma, addr, ptep);
  135. set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot));
  136. page_remove_rmap(page);
  137. if (!page_mapped(page))
  138. try_to_free_swap(page);
  139. pte_unmap_unlock(ptep, ptl);
  140. if (vma->vm_flags & VM_LOCKED)
  141. munlock_vma_page(page);
  142. put_page(page);
  143. err = 0;
  144. unlock:
  145. unlock_page(page);
  146. return err;
  147. }
  148. /**
  149. * is_swbp_insn - check if instruction is breakpoint instruction.
  150. * @insn: instruction to be checked.
  151. * Default implementation of is_swbp_insn
  152. * Returns true if @insn is a breakpoint instruction.
  153. */
  154. bool __weak is_swbp_insn(uprobe_opcode_t *insn)
  155. {
  156. return *insn == UPROBE_SWBP_INSN;
  157. }
  158. static void copy_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t *opcode)
  159. {
  160. void *kaddr = kmap_atomic(page);
  161. memcpy(opcode, kaddr + (vaddr & ~PAGE_MASK), UPROBE_SWBP_INSN_SIZE);
  162. kunmap_atomic(kaddr);
  163. }
  164. static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t *new_opcode)
  165. {
  166. uprobe_opcode_t old_opcode;
  167. bool is_swbp;
  168. copy_opcode(page, vaddr, &old_opcode);
  169. is_swbp = is_swbp_insn(&old_opcode);
  170. if (is_swbp_insn(new_opcode)) {
  171. if (is_swbp) /* register: already installed? */
  172. return 0;
  173. } else {
  174. if (!is_swbp) /* unregister: was it changed by us? */
  175. return -EINVAL;
  176. }
  177. return 1;
  178. }
  179. /*
  180. * NOTE:
  181. * Expect the breakpoint instruction to be the smallest size instruction for
  182. * the architecture. If an arch has variable length instruction and the
  183. * breakpoint instruction is not of the smallest length instruction
  184. * supported by that architecture then we need to modify is_swbp_at_addr and
  185. * write_opcode accordingly. This would never be a problem for archs that
  186. * have fixed length instructions.
  187. */
  188. /*
  189. * write_opcode - write the opcode at a given virtual address.
  190. * @mm: the probed process address space.
  191. * @vaddr: the virtual address to store the opcode.
  192. * @opcode: opcode to be written at @vaddr.
  193. *
  194. * Called with mm->mmap_sem held (for read and with a reference to
  195. * mm).
  196. *
  197. * For mm @mm, write the opcode at @vaddr.
  198. * Return 0 (success) or a negative errno.
  199. */
  200. static int write_opcode(struct mm_struct *mm, unsigned long vaddr,
  201. uprobe_opcode_t opcode)
  202. {
  203. struct page *old_page, *new_page;
  204. void *vaddr_old, *vaddr_new;
  205. struct vm_area_struct *vma;
  206. int ret;
  207. retry:
  208. /* Read the page with vaddr into memory */
  209. ret = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &old_page, &vma);
  210. if (ret <= 0)
  211. return ret;
  212. ret = verify_opcode(old_page, vaddr, &opcode);
  213. if (ret <= 0)
  214. goto put_old;
  215. ret = -ENOMEM;
  216. new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vaddr);
  217. if (!new_page)
  218. goto put_old;
  219. __SetPageUptodate(new_page);
  220. /* copy the page now that we've got it stable */
  221. vaddr_old = kmap_atomic(old_page);
  222. vaddr_new = kmap_atomic(new_page);
  223. memcpy(vaddr_new, vaddr_old, PAGE_SIZE);
  224. memcpy(vaddr_new + (vaddr & ~PAGE_MASK), &opcode, UPROBE_SWBP_INSN_SIZE);
  225. kunmap_atomic(vaddr_new);
  226. kunmap_atomic(vaddr_old);
  227. ret = anon_vma_prepare(vma);
  228. if (ret)
  229. goto put_new;
  230. ret = __replace_page(vma, vaddr, old_page, new_page);
  231. put_new:
  232. page_cache_release(new_page);
  233. put_old:
  234. put_page(old_page);
  235. if (unlikely(ret == -EAGAIN))
  236. goto retry;
  237. return ret;
  238. }
  239. /**
  240. * set_swbp - store breakpoint at a given address.
  241. * @auprobe: arch specific probepoint information.
  242. * @mm: the probed process address space.
  243. * @vaddr: the virtual address to insert the opcode.
  244. *
  245. * For mm @mm, store the breakpoint instruction at @vaddr.
  246. * Return 0 (success) or a negative errno.
  247. */
  248. int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
  249. {
  250. return write_opcode(mm, vaddr, UPROBE_SWBP_INSN);
  251. }
  252. /**
  253. * set_orig_insn - Restore the original instruction.
  254. * @mm: the probed process address space.
  255. * @auprobe: arch specific probepoint information.
  256. * @vaddr: the virtual address to insert the opcode.
  257. *
  258. * For mm @mm, restore the original opcode (opcode) at @vaddr.
  259. * Return 0 (success) or a negative errno.
  260. */
  261. int __weak
  262. set_orig_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
  263. {
  264. return write_opcode(mm, vaddr, *(uprobe_opcode_t *)auprobe->insn);
  265. }
  266. static int match_uprobe(struct uprobe *l, struct uprobe *r)
  267. {
  268. if (l->inode < r->inode)
  269. return -1;
  270. if (l->inode > r->inode)
  271. return 1;
  272. if (l->offset < r->offset)
  273. return -1;
  274. if (l->offset > r->offset)
  275. return 1;
  276. return 0;
  277. }
  278. static struct uprobe *__find_uprobe(struct inode *inode, loff_t offset)
  279. {
  280. struct uprobe u = { .inode = inode, .offset = offset };
  281. struct rb_node *n = uprobes_tree.rb_node;
  282. struct uprobe *uprobe;
  283. int match;
  284. while (n) {
  285. uprobe = rb_entry(n, struct uprobe, rb_node);
  286. match = match_uprobe(&u, uprobe);
  287. if (!match) {
  288. atomic_inc(&uprobe->ref);
  289. return uprobe;
  290. }
  291. if (match < 0)
  292. n = n->rb_left;
  293. else
  294. n = n->rb_right;
  295. }
  296. return NULL;
  297. }
  298. /*
  299. * Find a uprobe corresponding to a given inode:offset
  300. * Acquires uprobes_treelock
  301. */
  302. static struct uprobe *find_uprobe(struct inode *inode, loff_t offset)
  303. {
  304. struct uprobe *uprobe;
  305. spin_lock(&uprobes_treelock);
  306. uprobe = __find_uprobe(inode, offset);
  307. spin_unlock(&uprobes_treelock);
  308. return uprobe;
  309. }
  310. static struct uprobe *__insert_uprobe(struct uprobe *uprobe)
  311. {
  312. struct rb_node **p = &uprobes_tree.rb_node;
  313. struct rb_node *parent = NULL;
  314. struct uprobe *u;
  315. int match;
  316. while (*p) {
  317. parent = *p;
  318. u = rb_entry(parent, struct uprobe, rb_node);
  319. match = match_uprobe(uprobe, u);
  320. if (!match) {
  321. atomic_inc(&u->ref);
  322. return u;
  323. }
  324. if (match < 0)
  325. p = &parent->rb_left;
  326. else
  327. p = &parent->rb_right;
  328. }
  329. u = NULL;
  330. rb_link_node(&uprobe->rb_node, parent, p);
  331. rb_insert_color(&uprobe->rb_node, &uprobes_tree);
  332. /* get access + creation ref */
  333. atomic_set(&uprobe->ref, 2);
  334. return u;
  335. }
  336. /*
  337. * Acquire uprobes_treelock.
  338. * Matching uprobe already exists in rbtree;
  339. * increment (access refcount) and return the matching uprobe.
  340. *
  341. * No matching uprobe; insert the uprobe in rb_tree;
  342. * get a double refcount (access + creation) and return NULL.
  343. */
  344. static struct uprobe *insert_uprobe(struct uprobe *uprobe)
  345. {
  346. struct uprobe *u;
  347. spin_lock(&uprobes_treelock);
  348. u = __insert_uprobe(uprobe);
  349. spin_unlock(&uprobes_treelock);
  350. /* For now assume that the instruction need not be single-stepped */
  351. uprobe->flags |= UPROBE_SKIP_SSTEP;
  352. return u;
  353. }
  354. static void put_uprobe(struct uprobe *uprobe)
  355. {
  356. if (atomic_dec_and_test(&uprobe->ref))
  357. kfree(uprobe);
  358. }
  359. static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset)
  360. {
  361. struct uprobe *uprobe, *cur_uprobe;
  362. uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL);
  363. if (!uprobe)
  364. return NULL;
  365. uprobe->inode = igrab(inode);
  366. uprobe->offset = offset;
  367. init_rwsem(&uprobe->consumer_rwsem);
  368. /* add to uprobes_tree, sorted on inode:offset */
  369. cur_uprobe = insert_uprobe(uprobe);
  370. /* a uprobe exists for this inode:offset combination */
  371. if (cur_uprobe) {
  372. kfree(uprobe);
  373. uprobe = cur_uprobe;
  374. iput(inode);
  375. } else {
  376. atomic_inc(&uprobe_events);
  377. }
  378. return uprobe;
  379. }
  380. static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
  381. {
  382. struct uprobe_consumer *uc;
  383. if (!(uprobe->flags & UPROBE_RUN_HANDLER))
  384. return;
  385. down_read(&uprobe->consumer_rwsem);
  386. for (uc = uprobe->consumers; uc; uc = uc->next) {
  387. if (!uc->filter || uc->filter(uc, current))
  388. uc->handler(uc, regs);
  389. }
  390. up_read(&uprobe->consumer_rwsem);
  391. }
  392. /* Returns the previous consumer */
  393. static struct uprobe_consumer *
  394. consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc)
  395. {
  396. down_write(&uprobe->consumer_rwsem);
  397. uc->next = uprobe->consumers;
  398. uprobe->consumers = uc;
  399. up_write(&uprobe->consumer_rwsem);
  400. return uc->next;
  401. }
  402. /*
  403. * For uprobe @uprobe, delete the consumer @uc.
  404. * Return true if the @uc is deleted successfully
  405. * or return false.
  406. */
  407. static bool consumer_del(struct uprobe *uprobe, struct uprobe_consumer *uc)
  408. {
  409. struct uprobe_consumer **con;
  410. bool ret = false;
  411. down_write(&uprobe->consumer_rwsem);
  412. for (con = &uprobe->consumers; *con; con = &(*con)->next) {
  413. if (*con == uc) {
  414. *con = uc->next;
  415. ret = true;
  416. break;
  417. }
  418. }
  419. up_write(&uprobe->consumer_rwsem);
  420. return ret;
  421. }
  422. static int
  423. __copy_insn(struct address_space *mapping, struct file *filp, char *insn,
  424. unsigned long nbytes, loff_t offset)
  425. {
  426. struct page *page;
  427. void *vaddr;
  428. unsigned long off;
  429. pgoff_t idx;
  430. if (!filp)
  431. return -EINVAL;
  432. if (!mapping->a_ops->readpage)
  433. return -EIO;
  434. idx = offset >> PAGE_CACHE_SHIFT;
  435. off = offset & ~PAGE_MASK;
  436. /*
  437. * Ensure that the page that has the original instruction is
  438. * populated and in page-cache.
  439. */
  440. page = read_mapping_page(mapping, idx, filp);
  441. if (IS_ERR(page))
  442. return PTR_ERR(page);
  443. vaddr = kmap_atomic(page);
  444. memcpy(insn, vaddr + off, nbytes);
  445. kunmap_atomic(vaddr);
  446. page_cache_release(page);
  447. return 0;
  448. }
  449. static int copy_insn(struct uprobe *uprobe, struct file *filp)
  450. {
  451. struct address_space *mapping;
  452. unsigned long nbytes;
  453. int bytes;
  454. nbytes = PAGE_SIZE - (uprobe->offset & ~PAGE_MASK);
  455. mapping = uprobe->inode->i_mapping;
  456. /* Instruction at end of binary; copy only available bytes */
  457. if (uprobe->offset + MAX_UINSN_BYTES > uprobe->inode->i_size)
  458. bytes = uprobe->inode->i_size - uprobe->offset;
  459. else
  460. bytes = MAX_UINSN_BYTES;
  461. /* Instruction at the page-boundary; copy bytes in second page */
  462. if (nbytes < bytes) {
  463. int err = __copy_insn(mapping, filp, uprobe->arch.insn + nbytes,
  464. bytes - nbytes, uprobe->offset + nbytes);
  465. if (err)
  466. return err;
  467. bytes = nbytes;
  468. }
  469. return __copy_insn(mapping, filp, uprobe->arch.insn, bytes, uprobe->offset);
  470. }
  471. static int
  472. install_breakpoint(struct uprobe *uprobe, struct mm_struct *mm,
  473. struct vm_area_struct *vma, unsigned long vaddr)
  474. {
  475. bool first_uprobe;
  476. int ret;
  477. /*
  478. * If probe is being deleted, unregister thread could be done with
  479. * the vma-rmap-walk through. Adding a probe now can be fatal since
  480. * nobody will be able to cleanup. Also we could be from fork or
  481. * mremap path, where the probe might have already been inserted.
  482. * Hence behave as if probe already existed.
  483. */
  484. if (!uprobe->consumers)
  485. return 0;
  486. if (!(uprobe->flags & UPROBE_COPY_INSN)) {
  487. ret = copy_insn(uprobe, vma->vm_file);
  488. if (ret)
  489. return ret;
  490. if (is_swbp_insn((uprobe_opcode_t *)uprobe->arch.insn))
  491. return -ENOTSUPP;
  492. ret = arch_uprobe_analyze_insn(&uprobe->arch, mm, vaddr);
  493. if (ret)
  494. return ret;
  495. /* write_opcode() assumes we don't cross page boundary */
  496. BUG_ON((uprobe->offset & ~PAGE_MASK) +
  497. UPROBE_SWBP_INSN_SIZE > PAGE_SIZE);
  498. uprobe->flags |= UPROBE_COPY_INSN;
  499. }
  500. /*
  501. * set MMF_HAS_UPROBES in advance for uprobe_pre_sstep_notifier(),
  502. * the task can hit this breakpoint right after __replace_page().
  503. */
  504. first_uprobe = !test_bit(MMF_HAS_UPROBES, &mm->flags);
  505. if (first_uprobe)
  506. set_bit(MMF_HAS_UPROBES, &mm->flags);
  507. ret = set_swbp(&uprobe->arch, mm, vaddr);
  508. if (!ret)
  509. clear_bit(MMF_RECALC_UPROBES, &mm->flags);
  510. else if (first_uprobe)
  511. clear_bit(MMF_HAS_UPROBES, &mm->flags);
  512. return ret;
  513. }
  514. static void
  515. remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, unsigned long vaddr)
  516. {
  517. /* can happen if uprobe_register() fails */
  518. if (!test_bit(MMF_HAS_UPROBES, &mm->flags))
  519. return;
  520. set_bit(MMF_RECALC_UPROBES, &mm->flags);
  521. set_orig_insn(&uprobe->arch, mm, vaddr);
  522. }
  523. /*
  524. * There could be threads that have already hit the breakpoint. They
  525. * will recheck the current insn and restart if find_uprobe() fails.
  526. * See find_active_uprobe().
  527. */
  528. static void delete_uprobe(struct uprobe *uprobe)
  529. {
  530. spin_lock(&uprobes_treelock);
  531. rb_erase(&uprobe->rb_node, &uprobes_tree);
  532. spin_unlock(&uprobes_treelock);
  533. iput(uprobe->inode);
  534. put_uprobe(uprobe);
  535. atomic_dec(&uprobe_events);
  536. }
  537. struct map_info {
  538. struct map_info *next;
  539. struct mm_struct *mm;
  540. unsigned long vaddr;
  541. };
  542. static inline struct map_info *free_map_info(struct map_info *info)
  543. {
  544. struct map_info *next = info->next;
  545. kfree(info);
  546. return next;
  547. }
  548. static struct map_info *
  549. build_map_info(struct address_space *mapping, loff_t offset, bool is_register)
  550. {
  551. unsigned long pgoff = offset >> PAGE_SHIFT;
  552. struct prio_tree_iter iter;
  553. struct vm_area_struct *vma;
  554. struct map_info *curr = NULL;
  555. struct map_info *prev = NULL;
  556. struct map_info *info;
  557. int more = 0;
  558. again:
  559. mutex_lock(&mapping->i_mmap_mutex);
  560. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  561. if (!valid_vma(vma, is_register))
  562. continue;
  563. if (!prev && !more) {
  564. /*
  565. * Needs GFP_NOWAIT to avoid i_mmap_mutex recursion through
  566. * reclaim. This is optimistic, no harm done if it fails.
  567. */
  568. prev = kmalloc(sizeof(struct map_info),
  569. GFP_NOWAIT | __GFP_NOMEMALLOC | __GFP_NOWARN);
  570. if (prev)
  571. prev->next = NULL;
  572. }
  573. if (!prev) {
  574. more++;
  575. continue;
  576. }
  577. if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
  578. continue;
  579. info = prev;
  580. prev = prev->next;
  581. info->next = curr;
  582. curr = info;
  583. info->mm = vma->vm_mm;
  584. info->vaddr = offset_to_vaddr(vma, offset);
  585. }
  586. mutex_unlock(&mapping->i_mmap_mutex);
  587. if (!more)
  588. goto out;
  589. prev = curr;
  590. while (curr) {
  591. mmput(curr->mm);
  592. curr = curr->next;
  593. }
  594. do {
  595. info = kmalloc(sizeof(struct map_info), GFP_KERNEL);
  596. if (!info) {
  597. curr = ERR_PTR(-ENOMEM);
  598. goto out;
  599. }
  600. info->next = prev;
  601. prev = info;
  602. } while (--more);
  603. goto again;
  604. out:
  605. while (prev)
  606. prev = free_map_info(prev);
  607. return curr;
  608. }
  609. static int register_for_each_vma(struct uprobe *uprobe, bool is_register)
  610. {
  611. struct map_info *info;
  612. int err = 0;
  613. info = build_map_info(uprobe->inode->i_mapping,
  614. uprobe->offset, is_register);
  615. if (IS_ERR(info))
  616. return PTR_ERR(info);
  617. while (info) {
  618. struct mm_struct *mm = info->mm;
  619. struct vm_area_struct *vma;
  620. if (err)
  621. goto free;
  622. down_write(&mm->mmap_sem);
  623. vma = find_vma(mm, info->vaddr);
  624. if (!vma || !valid_vma(vma, is_register) ||
  625. vma->vm_file->f_mapping->host != uprobe->inode)
  626. goto unlock;
  627. if (vma->vm_start > info->vaddr ||
  628. vaddr_to_offset(vma, info->vaddr) != uprobe->offset)
  629. goto unlock;
  630. if (is_register)
  631. err = install_breakpoint(uprobe, mm, vma, info->vaddr);
  632. else
  633. remove_breakpoint(uprobe, mm, info->vaddr);
  634. unlock:
  635. up_write(&mm->mmap_sem);
  636. free:
  637. mmput(mm);
  638. info = free_map_info(info);
  639. }
  640. return err;
  641. }
  642. static int __uprobe_register(struct uprobe *uprobe)
  643. {
  644. return register_for_each_vma(uprobe, true);
  645. }
  646. static void __uprobe_unregister(struct uprobe *uprobe)
  647. {
  648. if (!register_for_each_vma(uprobe, false))
  649. delete_uprobe(uprobe);
  650. /* TODO : cant unregister? schedule a worker thread */
  651. }
  652. /*
  653. * uprobe_register - register a probe
  654. * @inode: the file in which the probe has to be placed.
  655. * @offset: offset from the start of the file.
  656. * @uc: information on howto handle the probe..
  657. *
  658. * Apart from the access refcount, uprobe_register() takes a creation
  659. * refcount (thro alloc_uprobe) if and only if this @uprobe is getting
  660. * inserted into the rbtree (i.e first consumer for a @inode:@offset
  661. * tuple). Creation refcount stops uprobe_unregister from freeing the
  662. * @uprobe even before the register operation is complete. Creation
  663. * refcount is released when the last @uc for the @uprobe
  664. * unregisters.
  665. *
  666. * Return errno if it cannot successully install probes
  667. * else return 0 (success)
  668. */
  669. int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
  670. {
  671. struct uprobe *uprobe;
  672. int ret;
  673. if (!inode || !uc || uc->next)
  674. return -EINVAL;
  675. if (offset > i_size_read(inode))
  676. return -EINVAL;
  677. ret = 0;
  678. mutex_lock(uprobes_hash(inode));
  679. uprobe = alloc_uprobe(inode, offset);
  680. if (uprobe && !consumer_add(uprobe, uc)) {
  681. ret = __uprobe_register(uprobe);
  682. if (ret) {
  683. uprobe->consumers = NULL;
  684. __uprobe_unregister(uprobe);
  685. } else {
  686. uprobe->flags |= UPROBE_RUN_HANDLER;
  687. }
  688. }
  689. mutex_unlock(uprobes_hash(inode));
  690. if (uprobe)
  691. put_uprobe(uprobe);
  692. return ret;
  693. }
  694. /*
  695. * uprobe_unregister - unregister a already registered probe.
  696. * @inode: the file in which the probe has to be removed.
  697. * @offset: offset from the start of the file.
  698. * @uc: identify which probe if multiple probes are colocated.
  699. */
  700. void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
  701. {
  702. struct uprobe *uprobe;
  703. if (!inode || !uc)
  704. return;
  705. uprobe = find_uprobe(inode, offset);
  706. if (!uprobe)
  707. return;
  708. mutex_lock(uprobes_hash(inode));
  709. if (consumer_del(uprobe, uc)) {
  710. if (!uprobe->consumers) {
  711. __uprobe_unregister(uprobe);
  712. uprobe->flags &= ~UPROBE_RUN_HANDLER;
  713. }
  714. }
  715. mutex_unlock(uprobes_hash(inode));
  716. if (uprobe)
  717. put_uprobe(uprobe);
  718. }
  719. static struct rb_node *
  720. find_node_in_range(struct inode *inode, loff_t min, loff_t max)
  721. {
  722. struct rb_node *n = uprobes_tree.rb_node;
  723. while (n) {
  724. struct uprobe *u = rb_entry(n, struct uprobe, rb_node);
  725. if (inode < u->inode) {
  726. n = n->rb_left;
  727. } else if (inode > u->inode) {
  728. n = n->rb_right;
  729. } else {
  730. if (max < u->offset)
  731. n = n->rb_left;
  732. else if (min > u->offset)
  733. n = n->rb_right;
  734. else
  735. break;
  736. }
  737. }
  738. return n;
  739. }
  740. /*
  741. * For a given range in vma, build a list of probes that need to be inserted.
  742. */
  743. static void build_probe_list(struct inode *inode,
  744. struct vm_area_struct *vma,
  745. unsigned long start, unsigned long end,
  746. struct list_head *head)
  747. {
  748. loff_t min, max;
  749. struct rb_node *n, *t;
  750. struct uprobe *u;
  751. INIT_LIST_HEAD(head);
  752. min = vaddr_to_offset(vma, start);
  753. max = min + (end - start) - 1;
  754. spin_lock(&uprobes_treelock);
  755. n = find_node_in_range(inode, min, max);
  756. if (n) {
  757. for (t = n; t; t = rb_prev(t)) {
  758. u = rb_entry(t, struct uprobe, rb_node);
  759. if (u->inode != inode || u->offset < min)
  760. break;
  761. list_add(&u->pending_list, head);
  762. atomic_inc(&u->ref);
  763. }
  764. for (t = n; (t = rb_next(t)); ) {
  765. u = rb_entry(t, struct uprobe, rb_node);
  766. if (u->inode != inode || u->offset > max)
  767. break;
  768. list_add(&u->pending_list, head);
  769. atomic_inc(&u->ref);
  770. }
  771. }
  772. spin_unlock(&uprobes_treelock);
  773. }
  774. /*
  775. * Called from mmap_region/vma_adjust with mm->mmap_sem acquired.
  776. *
  777. * Currently we ignore all errors and always return 0, the callers
  778. * can't handle the failure anyway.
  779. */
  780. int uprobe_mmap(struct vm_area_struct *vma)
  781. {
  782. struct list_head tmp_list;
  783. struct uprobe *uprobe, *u;
  784. struct inode *inode;
  785. if (!atomic_read(&uprobe_events) || !valid_vma(vma, true))
  786. return 0;
  787. inode = vma->vm_file->f_mapping->host;
  788. if (!inode)
  789. return 0;
  790. mutex_lock(uprobes_mmap_hash(inode));
  791. build_probe_list(inode, vma, vma->vm_start, vma->vm_end, &tmp_list);
  792. list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
  793. if (!fatal_signal_pending(current)) {
  794. unsigned long vaddr = offset_to_vaddr(vma, uprobe->offset);
  795. install_breakpoint(uprobe, vma->vm_mm, vma, vaddr);
  796. }
  797. put_uprobe(uprobe);
  798. }
  799. mutex_unlock(uprobes_mmap_hash(inode));
  800. return 0;
  801. }
  802. static bool
  803. vma_has_uprobes(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  804. {
  805. loff_t min, max;
  806. struct inode *inode;
  807. struct rb_node *n;
  808. inode = vma->vm_file->f_mapping->host;
  809. min = vaddr_to_offset(vma, start);
  810. max = min + (end - start) - 1;
  811. spin_lock(&uprobes_treelock);
  812. n = find_node_in_range(inode, min, max);
  813. spin_unlock(&uprobes_treelock);
  814. return !!n;
  815. }
  816. /*
  817. * Called in context of a munmap of a vma.
  818. */
  819. void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  820. {
  821. if (!atomic_read(&uprobe_events) || !valid_vma(vma, false))
  822. return;
  823. if (!atomic_read(&vma->vm_mm->mm_users)) /* called by mmput() ? */
  824. return;
  825. if (!test_bit(MMF_HAS_UPROBES, &vma->vm_mm->flags) ||
  826. test_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags))
  827. return;
  828. if (vma_has_uprobes(vma, start, end))
  829. set_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags);
  830. }
  831. /* Slot allocation for XOL */
  832. static int xol_add_vma(struct xol_area *area)
  833. {
  834. struct mm_struct *mm;
  835. int ret;
  836. area->page = alloc_page(GFP_HIGHUSER);
  837. if (!area->page)
  838. return -ENOMEM;
  839. ret = -EALREADY;
  840. mm = current->mm;
  841. down_write(&mm->mmap_sem);
  842. if (mm->uprobes_state.xol_area)
  843. goto fail;
  844. ret = -ENOMEM;
  845. /* Try to map as high as possible, this is only a hint. */
  846. area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE, PAGE_SIZE, 0, 0);
  847. if (area->vaddr & ~PAGE_MASK) {
  848. ret = area->vaddr;
  849. goto fail;
  850. }
  851. ret = install_special_mapping(mm, area->vaddr, PAGE_SIZE,
  852. VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO, &area->page);
  853. if (ret)
  854. goto fail;
  855. smp_wmb(); /* pairs with get_xol_area() */
  856. mm->uprobes_state.xol_area = area;
  857. ret = 0;
  858. fail:
  859. up_write(&mm->mmap_sem);
  860. if (ret)
  861. __free_page(area->page);
  862. return ret;
  863. }
  864. static struct xol_area *get_xol_area(struct mm_struct *mm)
  865. {
  866. struct xol_area *area;
  867. area = mm->uprobes_state.xol_area;
  868. smp_read_barrier_depends(); /* pairs with wmb in xol_add_vma() */
  869. return area;
  870. }
  871. /*
  872. * xol_alloc_area - Allocate process's xol_area.
  873. * This area will be used for storing instructions for execution out of
  874. * line.
  875. *
  876. * Returns the allocated area or NULL.
  877. */
  878. static struct xol_area *xol_alloc_area(void)
  879. {
  880. struct xol_area *area;
  881. area = kzalloc(sizeof(*area), GFP_KERNEL);
  882. if (unlikely(!area))
  883. return NULL;
  884. area->bitmap = kzalloc(BITS_TO_LONGS(UINSNS_PER_PAGE) * sizeof(long), GFP_KERNEL);
  885. if (!area->bitmap)
  886. goto fail;
  887. init_waitqueue_head(&area->wq);
  888. if (!xol_add_vma(area))
  889. return area;
  890. fail:
  891. kfree(area->bitmap);
  892. kfree(area);
  893. return get_xol_area(current->mm);
  894. }
  895. /*
  896. * uprobe_clear_state - Free the area allocated for slots.
  897. */
  898. void uprobe_clear_state(struct mm_struct *mm)
  899. {
  900. struct xol_area *area = mm->uprobes_state.xol_area;
  901. if (!area)
  902. return;
  903. put_page(area->page);
  904. kfree(area->bitmap);
  905. kfree(area);
  906. }
  907. void uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm)
  908. {
  909. newmm->uprobes_state.xol_area = NULL;
  910. if (test_bit(MMF_HAS_UPROBES, &oldmm->flags)) {
  911. set_bit(MMF_HAS_UPROBES, &newmm->flags);
  912. /* unconditionally, dup_mmap() skips VM_DONTCOPY vmas */
  913. set_bit(MMF_RECALC_UPROBES, &newmm->flags);
  914. }
  915. }
  916. /*
  917. * - search for a free slot.
  918. */
  919. static unsigned long xol_take_insn_slot(struct xol_area *area)
  920. {
  921. unsigned long slot_addr;
  922. int slot_nr;
  923. do {
  924. slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE);
  925. if (slot_nr < UINSNS_PER_PAGE) {
  926. if (!test_and_set_bit(slot_nr, area->bitmap))
  927. break;
  928. slot_nr = UINSNS_PER_PAGE;
  929. continue;
  930. }
  931. wait_event(area->wq, (atomic_read(&area->slot_count) < UINSNS_PER_PAGE));
  932. } while (slot_nr >= UINSNS_PER_PAGE);
  933. slot_addr = area->vaddr + (slot_nr * UPROBE_XOL_SLOT_BYTES);
  934. atomic_inc(&area->slot_count);
  935. return slot_addr;
  936. }
  937. /*
  938. * xol_get_insn_slot - If was not allocated a slot, then
  939. * allocate a slot.
  940. * Returns the allocated slot address or 0.
  941. */
  942. static unsigned long xol_get_insn_slot(struct uprobe *uprobe, unsigned long slot_addr)
  943. {
  944. struct xol_area *area;
  945. unsigned long offset;
  946. void *vaddr;
  947. area = get_xol_area(current->mm);
  948. if (!area) {
  949. area = xol_alloc_area();
  950. if (!area)
  951. return 0;
  952. }
  953. current->utask->xol_vaddr = xol_take_insn_slot(area);
  954. /*
  955. * Initialize the slot if xol_vaddr points to valid
  956. * instruction slot.
  957. */
  958. if (unlikely(!current->utask->xol_vaddr))
  959. return 0;
  960. current->utask->vaddr = slot_addr;
  961. offset = current->utask->xol_vaddr & ~PAGE_MASK;
  962. vaddr = kmap_atomic(area->page);
  963. memcpy(vaddr + offset, uprobe->arch.insn, MAX_UINSN_BYTES);
  964. kunmap_atomic(vaddr);
  965. return current->utask->xol_vaddr;
  966. }
  967. /*
  968. * xol_free_insn_slot - If slot was earlier allocated by
  969. * @xol_get_insn_slot(), make the slot available for
  970. * subsequent requests.
  971. */
  972. static void xol_free_insn_slot(struct task_struct *tsk)
  973. {
  974. struct xol_area *area;
  975. unsigned long vma_end;
  976. unsigned long slot_addr;
  977. if (!tsk->mm || !tsk->mm->uprobes_state.xol_area || !tsk->utask)
  978. return;
  979. slot_addr = tsk->utask->xol_vaddr;
  980. if (unlikely(!slot_addr || IS_ERR_VALUE(slot_addr)))
  981. return;
  982. area = tsk->mm->uprobes_state.xol_area;
  983. vma_end = area->vaddr + PAGE_SIZE;
  984. if (area->vaddr <= slot_addr && slot_addr < vma_end) {
  985. unsigned long offset;
  986. int slot_nr;
  987. offset = slot_addr - area->vaddr;
  988. slot_nr = offset / UPROBE_XOL_SLOT_BYTES;
  989. if (slot_nr >= UINSNS_PER_PAGE)
  990. return;
  991. clear_bit(slot_nr, area->bitmap);
  992. atomic_dec(&area->slot_count);
  993. if (waitqueue_active(&area->wq))
  994. wake_up(&area->wq);
  995. tsk->utask->xol_vaddr = 0;
  996. }
  997. }
  998. /**
  999. * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
  1000. * @regs: Reflects the saved state of the task after it has hit a breakpoint
  1001. * instruction.
  1002. * Return the address of the breakpoint instruction.
  1003. */
  1004. unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs)
  1005. {
  1006. return instruction_pointer(regs) - UPROBE_SWBP_INSN_SIZE;
  1007. }
  1008. /*
  1009. * Called with no locks held.
  1010. * Called in context of a exiting or a exec-ing thread.
  1011. */
  1012. void uprobe_free_utask(struct task_struct *t)
  1013. {
  1014. struct uprobe_task *utask = t->utask;
  1015. if (!utask)
  1016. return;
  1017. if (utask->active_uprobe)
  1018. put_uprobe(utask->active_uprobe);
  1019. xol_free_insn_slot(t);
  1020. kfree(utask);
  1021. t->utask = NULL;
  1022. }
  1023. /*
  1024. * Called in context of a new clone/fork from copy_process.
  1025. */
  1026. void uprobe_copy_process(struct task_struct *t)
  1027. {
  1028. t->utask = NULL;
  1029. }
  1030. /*
  1031. * Allocate a uprobe_task object for the task.
  1032. * Called when the thread hits a breakpoint for the first time.
  1033. *
  1034. * Returns:
  1035. * - pointer to new uprobe_task on success
  1036. * - NULL otherwise
  1037. */
  1038. static struct uprobe_task *add_utask(void)
  1039. {
  1040. struct uprobe_task *utask;
  1041. utask = kzalloc(sizeof *utask, GFP_KERNEL);
  1042. if (unlikely(!utask))
  1043. return NULL;
  1044. current->utask = utask;
  1045. return utask;
  1046. }
  1047. /* Prepare to single-step probed instruction out of line. */
  1048. static int
  1049. pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long vaddr)
  1050. {
  1051. if (xol_get_insn_slot(uprobe, vaddr) && !arch_uprobe_pre_xol(&uprobe->arch, regs))
  1052. return 0;
  1053. return -EFAULT;
  1054. }
  1055. /*
  1056. * If we are singlestepping, then ensure this thread is not connected to
  1057. * non-fatal signals until completion of singlestep. When xol insn itself
  1058. * triggers the signal, restart the original insn even if the task is
  1059. * already SIGKILL'ed (since coredump should report the correct ip). This
  1060. * is even more important if the task has a handler for SIGSEGV/etc, The
  1061. * _same_ instruction should be repeated again after return from the signal
  1062. * handler, and SSTEP can never finish in this case.
  1063. */
  1064. bool uprobe_deny_signal(void)
  1065. {
  1066. struct task_struct *t = current;
  1067. struct uprobe_task *utask = t->utask;
  1068. if (likely(!utask || !utask->active_uprobe))
  1069. return false;
  1070. WARN_ON_ONCE(utask->state != UTASK_SSTEP);
  1071. if (signal_pending(t)) {
  1072. spin_lock_irq(&t->sighand->siglock);
  1073. clear_tsk_thread_flag(t, TIF_SIGPENDING);
  1074. spin_unlock_irq(&t->sighand->siglock);
  1075. if (__fatal_signal_pending(t) || arch_uprobe_xol_was_trapped(t)) {
  1076. utask->state = UTASK_SSTEP_TRAPPED;
  1077. set_tsk_thread_flag(t, TIF_UPROBE);
  1078. set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
  1079. }
  1080. }
  1081. return true;
  1082. }
  1083. /*
  1084. * Avoid singlestepping the original instruction if the original instruction
  1085. * is a NOP or can be emulated.
  1086. */
  1087. static bool can_skip_sstep(struct uprobe *uprobe, struct pt_regs *regs)
  1088. {
  1089. if (uprobe->flags & UPROBE_SKIP_SSTEP) {
  1090. if (arch_uprobe_skip_sstep(&uprobe->arch, regs))
  1091. return true;
  1092. uprobe->flags &= ~UPROBE_SKIP_SSTEP;
  1093. }
  1094. return false;
  1095. }
  1096. static void mmf_recalc_uprobes(struct mm_struct *mm)
  1097. {
  1098. struct vm_area_struct *vma;
  1099. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  1100. if (!valid_vma(vma, false))
  1101. continue;
  1102. /*
  1103. * This is not strictly accurate, we can race with
  1104. * uprobe_unregister() and see the already removed
  1105. * uprobe if delete_uprobe() was not yet called.
  1106. */
  1107. if (vma_has_uprobes(vma, vma->vm_start, vma->vm_end))
  1108. return;
  1109. }
  1110. clear_bit(MMF_HAS_UPROBES, &mm->flags);
  1111. }
  1112. static int is_swbp_at_addr(struct mm_struct *mm, unsigned long vaddr)
  1113. {
  1114. struct page *page;
  1115. uprobe_opcode_t opcode;
  1116. int result;
  1117. pagefault_disable();
  1118. result = __copy_from_user_inatomic(&opcode, (void __user*)vaddr,
  1119. sizeof(opcode));
  1120. pagefault_enable();
  1121. if (likely(result == 0))
  1122. goto out;
  1123. result = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &page, NULL);
  1124. if (result < 0)
  1125. return result;
  1126. copy_opcode(page, vaddr, &opcode);
  1127. put_page(page);
  1128. out:
  1129. return is_swbp_insn(&opcode);
  1130. }
  1131. static struct uprobe *find_active_uprobe(unsigned long bp_vaddr, int *is_swbp)
  1132. {
  1133. struct mm_struct *mm = current->mm;
  1134. struct uprobe *uprobe = NULL;
  1135. struct vm_area_struct *vma;
  1136. down_read(&mm->mmap_sem);
  1137. vma = find_vma(mm, bp_vaddr);
  1138. if (vma && vma->vm_start <= bp_vaddr) {
  1139. if (valid_vma(vma, false)) {
  1140. struct inode *inode = vma->vm_file->f_mapping->host;
  1141. loff_t offset = vaddr_to_offset(vma, bp_vaddr);
  1142. uprobe = find_uprobe(inode, offset);
  1143. }
  1144. if (!uprobe)
  1145. *is_swbp = is_swbp_at_addr(mm, bp_vaddr);
  1146. } else {
  1147. *is_swbp = -EFAULT;
  1148. }
  1149. if (!uprobe && test_and_clear_bit(MMF_RECALC_UPROBES, &mm->flags))
  1150. mmf_recalc_uprobes(mm);
  1151. up_read(&mm->mmap_sem);
  1152. return uprobe;
  1153. }
  1154. void __weak arch_uprobe_enable_step(struct arch_uprobe *arch)
  1155. {
  1156. user_enable_single_step(current);
  1157. }
  1158. void __weak arch_uprobe_disable_step(struct arch_uprobe *arch)
  1159. {
  1160. user_disable_single_step(current);
  1161. }
  1162. /*
  1163. * Run handler and ask thread to singlestep.
  1164. * Ensure all non-fatal signals cannot interrupt thread while it singlesteps.
  1165. */
  1166. static void handle_swbp(struct pt_regs *regs)
  1167. {
  1168. struct uprobe_task *utask;
  1169. struct uprobe *uprobe;
  1170. unsigned long bp_vaddr;
  1171. int uninitialized_var(is_swbp);
  1172. bp_vaddr = uprobe_get_swbp_addr(regs);
  1173. uprobe = find_active_uprobe(bp_vaddr, &is_swbp);
  1174. if (!uprobe) {
  1175. if (is_swbp > 0) {
  1176. /* No matching uprobe; signal SIGTRAP. */
  1177. send_sig(SIGTRAP, current, 0);
  1178. } else {
  1179. /*
  1180. * Either we raced with uprobe_unregister() or we can't
  1181. * access this memory. The latter is only possible if
  1182. * another thread plays with our ->mm. In both cases
  1183. * we can simply restart. If this vma was unmapped we
  1184. * can pretend this insn was not executed yet and get
  1185. * the (correct) SIGSEGV after restart.
  1186. */
  1187. instruction_pointer_set(regs, bp_vaddr);
  1188. }
  1189. return;
  1190. }
  1191. utask = current->utask;
  1192. if (!utask) {
  1193. utask = add_utask();
  1194. /* Cannot allocate; re-execute the instruction. */
  1195. if (!utask)
  1196. goto restart;
  1197. }
  1198. handler_chain(uprobe, regs);
  1199. if (can_skip_sstep(uprobe, regs))
  1200. goto out;
  1201. if (!pre_ssout(uprobe, regs, bp_vaddr)) {
  1202. arch_uprobe_enable_step(&uprobe->arch);
  1203. utask->active_uprobe = uprobe;
  1204. utask->state = UTASK_SSTEP;
  1205. return;
  1206. }
  1207. restart:
  1208. /*
  1209. * cannot singlestep; cannot skip instruction;
  1210. * re-execute the instruction.
  1211. */
  1212. instruction_pointer_set(regs, bp_vaddr);
  1213. out:
  1214. put_uprobe(uprobe);
  1215. }
  1216. /*
  1217. * Perform required fix-ups and disable singlestep.
  1218. * Allow pending signals to take effect.
  1219. */
  1220. static void handle_singlestep(struct uprobe_task *utask, struct pt_regs *regs)
  1221. {
  1222. struct uprobe *uprobe;
  1223. uprobe = utask->active_uprobe;
  1224. if (utask->state == UTASK_SSTEP_ACK)
  1225. arch_uprobe_post_xol(&uprobe->arch, regs);
  1226. else if (utask->state == UTASK_SSTEP_TRAPPED)
  1227. arch_uprobe_abort_xol(&uprobe->arch, regs);
  1228. else
  1229. WARN_ON_ONCE(1);
  1230. arch_uprobe_disable_step(&uprobe->arch);
  1231. put_uprobe(uprobe);
  1232. utask->active_uprobe = NULL;
  1233. utask->state = UTASK_RUNNING;
  1234. xol_free_insn_slot(current);
  1235. spin_lock_irq(&current->sighand->siglock);
  1236. recalc_sigpending(); /* see uprobe_deny_signal() */
  1237. spin_unlock_irq(&current->sighand->siglock);
  1238. }
  1239. /*
  1240. * On breakpoint hit, breakpoint notifier sets the TIF_UPROBE flag and
  1241. * allows the thread to return from interrupt. After that handle_swbp()
  1242. * sets utask->active_uprobe.
  1243. *
  1244. * On singlestep exception, singlestep notifier sets the TIF_UPROBE flag
  1245. * and allows the thread to return from interrupt.
  1246. *
  1247. * While returning to userspace, thread notices the TIF_UPROBE flag and calls
  1248. * uprobe_notify_resume().
  1249. */
  1250. void uprobe_notify_resume(struct pt_regs *regs)
  1251. {
  1252. struct uprobe_task *utask;
  1253. clear_thread_flag(TIF_UPROBE);
  1254. utask = current->utask;
  1255. if (utask && utask->active_uprobe)
  1256. handle_singlestep(utask, regs);
  1257. else
  1258. handle_swbp(regs);
  1259. }
  1260. /*
  1261. * uprobe_pre_sstep_notifier gets called from interrupt context as part of
  1262. * notifier mechanism. Set TIF_UPROBE flag and indicate breakpoint hit.
  1263. */
  1264. int uprobe_pre_sstep_notifier(struct pt_regs *regs)
  1265. {
  1266. if (!current->mm || !test_bit(MMF_HAS_UPROBES, &current->mm->flags))
  1267. return 0;
  1268. set_thread_flag(TIF_UPROBE);
  1269. return 1;
  1270. }
  1271. /*
  1272. * uprobe_post_sstep_notifier gets called in interrupt context as part of notifier
  1273. * mechanism. Set TIF_UPROBE flag and indicate completion of singlestep.
  1274. */
  1275. int uprobe_post_sstep_notifier(struct pt_regs *regs)
  1276. {
  1277. struct uprobe_task *utask = current->utask;
  1278. if (!current->mm || !utask || !utask->active_uprobe)
  1279. /* task is currently not uprobed */
  1280. return 0;
  1281. utask->state = UTASK_SSTEP_ACK;
  1282. set_thread_flag(TIF_UPROBE);
  1283. return 1;
  1284. }
  1285. static struct notifier_block uprobe_exception_nb = {
  1286. .notifier_call = arch_uprobe_exception_notify,
  1287. .priority = INT_MAX-1, /* notified after kprobes, kgdb */
  1288. };
  1289. static int __init init_uprobes(void)
  1290. {
  1291. int i;
  1292. for (i = 0; i < UPROBES_HASH_SZ; i++) {
  1293. mutex_init(&uprobes_mutex[i]);
  1294. mutex_init(&uprobes_mmap_mutex[i]);
  1295. }
  1296. return register_die_notifier(&uprobe_exception_nb);
  1297. }
  1298. module_init(init_uprobes);
  1299. static void __exit exit_uprobes(void)
  1300. {
  1301. }
  1302. module_exit(exit_uprobes);