uprobes.c 39 KB

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