uprobes.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976
  1. /*
  2. * Userspace 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-2011
  19. * Authors:
  20. * Srikar Dronamraju
  21. * Jim Keniston
  22. */
  23. #include <linux/kernel.h>
  24. #include <linux/highmem.h>
  25. #include <linux/pagemap.h> /* read_mapping_page */
  26. #include <linux/slab.h>
  27. #include <linux/sched.h>
  28. #include <linux/rmap.h> /* anon_vma_prepare */
  29. #include <linux/mmu_notifier.h> /* set_pte_at_notify */
  30. #include <linux/swap.h> /* try_to_free_swap */
  31. #include <linux/uprobes.h>
  32. static struct rb_root uprobes_tree = RB_ROOT;
  33. static DEFINE_SPINLOCK(uprobes_treelock); /* serialize rbtree access */
  34. #define UPROBES_HASH_SZ 13
  35. /* serialize (un)register */
  36. static struct mutex uprobes_mutex[UPROBES_HASH_SZ];
  37. #define uprobes_hash(v) (&uprobes_mutex[((unsigned long)(v)) %\
  38. UPROBES_HASH_SZ])
  39. /* serialize uprobe->pending_list */
  40. static struct mutex uprobes_mmap_mutex[UPROBES_HASH_SZ];
  41. #define uprobes_mmap_hash(v) (&uprobes_mmap_mutex[((unsigned long)(v)) %\
  42. UPROBES_HASH_SZ])
  43. /*
  44. * uprobe_events allows us to skip the mmap_uprobe if there are no uprobe
  45. * events active at this time. Probably a fine grained per inode count is
  46. * better?
  47. */
  48. static atomic_t uprobe_events = ATOMIC_INIT(0);
  49. /*
  50. * Maintain a temporary per vma info that can be used to search if a vma
  51. * has already been handled. This structure is introduced since extending
  52. * vm_area_struct wasnt recommended.
  53. */
  54. struct vma_info {
  55. struct list_head probe_list;
  56. struct mm_struct *mm;
  57. loff_t vaddr;
  58. };
  59. /*
  60. * valid_vma: Verify if the specified vma is an executable vma
  61. * Relax restrictions while unregistering: vm_flags might have
  62. * changed after breakpoint was inserted.
  63. * - is_register: indicates if we are in register context.
  64. * - Return 1 if the specified virtual address is in an
  65. * executable vma.
  66. */
  67. static bool valid_vma(struct vm_area_struct *vma, bool is_register)
  68. {
  69. if (!vma->vm_file)
  70. return false;
  71. if (!is_register)
  72. return true;
  73. if ((vma->vm_flags & (VM_READ|VM_WRITE|VM_EXEC|VM_SHARED)) ==
  74. (VM_READ|VM_EXEC))
  75. return true;
  76. return false;
  77. }
  78. static loff_t vma_address(struct vm_area_struct *vma, loff_t offset)
  79. {
  80. loff_t vaddr;
  81. vaddr = vma->vm_start + offset;
  82. vaddr -= vma->vm_pgoff << PAGE_SHIFT;
  83. return vaddr;
  84. }
  85. /**
  86. * __replace_page - replace page in vma by new page.
  87. * based on replace_page in mm/ksm.c
  88. *
  89. * @vma: vma that holds the pte pointing to page
  90. * @page: the cowed page we are replacing by kpage
  91. * @kpage: the modified page we replace page by
  92. *
  93. * Returns 0 on success, -EFAULT on failure.
  94. */
  95. static int __replace_page(struct vm_area_struct *vma, struct page *page,
  96. struct page *kpage)
  97. {
  98. struct mm_struct *mm = vma->vm_mm;
  99. pgd_t *pgd;
  100. pud_t *pud;
  101. pmd_t *pmd;
  102. pte_t *ptep;
  103. spinlock_t *ptl;
  104. unsigned long addr;
  105. int err = -EFAULT;
  106. addr = page_address_in_vma(page, vma);
  107. if (addr == -EFAULT)
  108. goto out;
  109. pgd = pgd_offset(mm, addr);
  110. if (!pgd_present(*pgd))
  111. goto out;
  112. pud = pud_offset(pgd, addr);
  113. if (!pud_present(*pud))
  114. goto out;
  115. pmd = pmd_offset(pud, addr);
  116. if (!pmd_present(*pmd))
  117. goto out;
  118. ptep = pte_offset_map_lock(mm, pmd, addr, &ptl);
  119. if (!ptep)
  120. goto out;
  121. get_page(kpage);
  122. page_add_new_anon_rmap(kpage, vma, addr);
  123. flush_cache_page(vma, addr, pte_pfn(*ptep));
  124. ptep_clear_flush(vma, addr, ptep);
  125. set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot));
  126. page_remove_rmap(page);
  127. if (!page_mapped(page))
  128. try_to_free_swap(page);
  129. put_page(page);
  130. pte_unmap_unlock(ptep, ptl);
  131. err = 0;
  132. out:
  133. return err;
  134. }
  135. /**
  136. * is_bkpt_insn - check if instruction is breakpoint instruction.
  137. * @insn: instruction to be checked.
  138. * Default implementation of is_bkpt_insn
  139. * Returns true if @insn is a breakpoint instruction.
  140. */
  141. bool __weak is_bkpt_insn(uprobe_opcode_t *insn)
  142. {
  143. return (*insn == UPROBES_BKPT_INSN);
  144. }
  145. /*
  146. * NOTE:
  147. * Expect the breakpoint instruction to be the smallest size instruction for
  148. * the architecture. If an arch has variable length instruction and the
  149. * breakpoint instruction is not of the smallest length instruction
  150. * supported by that architecture then we need to modify read_opcode /
  151. * write_opcode accordingly. This would never be a problem for archs that
  152. * have fixed length instructions.
  153. */
  154. /*
  155. * write_opcode - write the opcode at a given virtual address.
  156. * @mm: the probed process address space.
  157. * @uprobe: the breakpointing information.
  158. * @vaddr: the virtual address to store the opcode.
  159. * @opcode: opcode to be written at @vaddr.
  160. *
  161. * Called with mm->mmap_sem held (for read and with a reference to
  162. * mm).
  163. *
  164. * For mm @mm, write the opcode at @vaddr.
  165. * Return 0 (success) or a negative errno.
  166. */
  167. static int write_opcode(struct mm_struct *mm, struct uprobe *uprobe,
  168. unsigned long vaddr, uprobe_opcode_t opcode)
  169. {
  170. struct page *old_page, *new_page;
  171. struct address_space *mapping;
  172. void *vaddr_old, *vaddr_new;
  173. struct vm_area_struct *vma;
  174. loff_t addr;
  175. int ret;
  176. /* Read the page with vaddr into memory */
  177. ret = get_user_pages(NULL, mm, vaddr, 1, 0, 0, &old_page, &vma);
  178. if (ret <= 0)
  179. return ret;
  180. ret = -EINVAL;
  181. /*
  182. * We are interested in text pages only. Our pages of interest
  183. * should be mapped for read and execute only. We desist from
  184. * adding probes in write mapped pages since the breakpoints
  185. * might end up in the file copy.
  186. */
  187. if (!valid_vma(vma, is_bkpt_insn(&opcode)))
  188. goto put_out;
  189. mapping = uprobe->inode->i_mapping;
  190. if (mapping != vma->vm_file->f_mapping)
  191. goto put_out;
  192. addr = vma_address(vma, uprobe->offset);
  193. if (vaddr != (unsigned long)addr)
  194. goto put_out;
  195. ret = -ENOMEM;
  196. new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vaddr);
  197. if (!new_page)
  198. goto put_out;
  199. __SetPageUptodate(new_page);
  200. /*
  201. * lock page will serialize against do_wp_page()'s
  202. * PageAnon() handling
  203. */
  204. lock_page(old_page);
  205. /* copy the page now that we've got it stable */
  206. vaddr_old = kmap_atomic(old_page);
  207. vaddr_new = kmap_atomic(new_page);
  208. memcpy(vaddr_new, vaddr_old, PAGE_SIZE);
  209. /* poke the new insn in, ASSUMES we don't cross page boundary */
  210. vaddr &= ~PAGE_MASK;
  211. BUG_ON(vaddr + uprobe_opcode_sz > PAGE_SIZE);
  212. memcpy(vaddr_new + vaddr, &opcode, uprobe_opcode_sz);
  213. kunmap_atomic(vaddr_new);
  214. kunmap_atomic(vaddr_old);
  215. ret = anon_vma_prepare(vma);
  216. if (ret)
  217. goto unlock_out;
  218. lock_page(new_page);
  219. ret = __replace_page(vma, old_page, new_page);
  220. unlock_page(new_page);
  221. unlock_out:
  222. unlock_page(old_page);
  223. page_cache_release(new_page);
  224. put_out:
  225. put_page(old_page); /* we did a get_page in the beginning */
  226. return ret;
  227. }
  228. /**
  229. * read_opcode - read the opcode at a given virtual address.
  230. * @mm: the probed process address space.
  231. * @vaddr: the virtual address to read the opcode.
  232. * @opcode: location to store the read opcode.
  233. *
  234. * Called with mm->mmap_sem held (for read and with a reference to
  235. * mm.
  236. *
  237. * For mm @mm, read the opcode at @vaddr and store it in @opcode.
  238. * Return 0 (success) or a negative errno.
  239. */
  240. static int read_opcode(struct mm_struct *mm, unsigned long vaddr,
  241. uprobe_opcode_t *opcode)
  242. {
  243. struct page *page;
  244. void *vaddr_new;
  245. int ret;
  246. ret = get_user_pages(NULL, mm, vaddr, 1, 0, 0, &page, NULL);
  247. if (ret <= 0)
  248. return ret;
  249. lock_page(page);
  250. vaddr_new = kmap_atomic(page);
  251. vaddr &= ~PAGE_MASK;
  252. memcpy(opcode, vaddr_new + vaddr, uprobe_opcode_sz);
  253. kunmap_atomic(vaddr_new);
  254. unlock_page(page);
  255. put_page(page); /* we did a get_user_pages in the beginning */
  256. return 0;
  257. }
  258. static int is_bkpt_at_addr(struct mm_struct *mm, unsigned long vaddr)
  259. {
  260. uprobe_opcode_t opcode;
  261. int result = read_opcode(mm, vaddr, &opcode);
  262. if (result)
  263. return result;
  264. if (is_bkpt_insn(&opcode))
  265. return 1;
  266. return 0;
  267. }
  268. /**
  269. * set_bkpt - store breakpoint at a given address.
  270. * @mm: the probed process address space.
  271. * @uprobe: the probepoint information.
  272. * @vaddr: the virtual address to insert the opcode.
  273. *
  274. * For mm @mm, store the breakpoint instruction at @vaddr.
  275. * Return 0 (success) or a negative errno.
  276. */
  277. int __weak set_bkpt(struct mm_struct *mm, struct uprobe *uprobe,
  278. unsigned long vaddr)
  279. {
  280. int result = is_bkpt_at_addr(mm, vaddr);
  281. if (result == 1)
  282. return -EEXIST;
  283. if (result)
  284. return result;
  285. return write_opcode(mm, uprobe, vaddr, UPROBES_BKPT_INSN);
  286. }
  287. /**
  288. * set_orig_insn - Restore the original instruction.
  289. * @mm: the probed process address space.
  290. * @uprobe: the probepoint information.
  291. * @vaddr: the virtual address to insert the opcode.
  292. * @verify: if true, verify existance of breakpoint instruction.
  293. *
  294. * For mm @mm, restore the original opcode (opcode) at @vaddr.
  295. * Return 0 (success) or a negative errno.
  296. */
  297. int __weak set_orig_insn(struct mm_struct *mm, struct uprobe *uprobe,
  298. unsigned long vaddr, bool verify)
  299. {
  300. if (verify) {
  301. int result = is_bkpt_at_addr(mm, vaddr);
  302. if (!result)
  303. return -EINVAL;
  304. if (result != 1)
  305. return result;
  306. }
  307. return write_opcode(mm, uprobe, vaddr,
  308. *(uprobe_opcode_t *)uprobe->insn);
  309. }
  310. static int match_uprobe(struct uprobe *l, struct uprobe *r)
  311. {
  312. if (l->inode < r->inode)
  313. return -1;
  314. if (l->inode > r->inode)
  315. return 1;
  316. else {
  317. if (l->offset < r->offset)
  318. return -1;
  319. if (l->offset > r->offset)
  320. return 1;
  321. }
  322. return 0;
  323. }
  324. static struct uprobe *__find_uprobe(struct inode *inode, loff_t offset)
  325. {
  326. struct uprobe u = { .inode = inode, .offset = offset };
  327. struct rb_node *n = uprobes_tree.rb_node;
  328. struct uprobe *uprobe;
  329. int match;
  330. while (n) {
  331. uprobe = rb_entry(n, struct uprobe, rb_node);
  332. match = match_uprobe(&u, uprobe);
  333. if (!match) {
  334. atomic_inc(&uprobe->ref);
  335. return uprobe;
  336. }
  337. if (match < 0)
  338. n = n->rb_left;
  339. else
  340. n = n->rb_right;
  341. }
  342. return NULL;
  343. }
  344. /*
  345. * Find a uprobe corresponding to a given inode:offset
  346. * Acquires uprobes_treelock
  347. */
  348. static struct uprobe *find_uprobe(struct inode *inode, loff_t offset)
  349. {
  350. struct uprobe *uprobe;
  351. unsigned long flags;
  352. spin_lock_irqsave(&uprobes_treelock, flags);
  353. uprobe = __find_uprobe(inode, offset);
  354. spin_unlock_irqrestore(&uprobes_treelock, flags);
  355. return uprobe;
  356. }
  357. static struct uprobe *__insert_uprobe(struct uprobe *uprobe)
  358. {
  359. struct rb_node **p = &uprobes_tree.rb_node;
  360. struct rb_node *parent = NULL;
  361. struct uprobe *u;
  362. int match;
  363. while (*p) {
  364. parent = *p;
  365. u = rb_entry(parent, struct uprobe, rb_node);
  366. match = match_uprobe(uprobe, u);
  367. if (!match) {
  368. atomic_inc(&u->ref);
  369. return u;
  370. }
  371. if (match < 0)
  372. p = &parent->rb_left;
  373. else
  374. p = &parent->rb_right;
  375. }
  376. u = NULL;
  377. rb_link_node(&uprobe->rb_node, parent, p);
  378. rb_insert_color(&uprobe->rb_node, &uprobes_tree);
  379. /* get access + creation ref */
  380. atomic_set(&uprobe->ref, 2);
  381. return u;
  382. }
  383. /*
  384. * Acquires uprobes_treelock.
  385. * Matching uprobe already exists in rbtree;
  386. * increment (access refcount) and return the matching uprobe.
  387. *
  388. * No matching uprobe; insert the uprobe in rb_tree;
  389. * get a double refcount (access + creation) and return NULL.
  390. */
  391. static struct uprobe *insert_uprobe(struct uprobe *uprobe)
  392. {
  393. unsigned long flags;
  394. struct uprobe *u;
  395. spin_lock_irqsave(&uprobes_treelock, flags);
  396. u = __insert_uprobe(uprobe);
  397. spin_unlock_irqrestore(&uprobes_treelock, flags);
  398. return u;
  399. }
  400. static void put_uprobe(struct uprobe *uprobe)
  401. {
  402. if (atomic_dec_and_test(&uprobe->ref))
  403. kfree(uprobe);
  404. }
  405. static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset)
  406. {
  407. struct uprobe *uprobe, *cur_uprobe;
  408. uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL);
  409. if (!uprobe)
  410. return NULL;
  411. uprobe->inode = igrab(inode);
  412. uprobe->offset = offset;
  413. init_rwsem(&uprobe->consumer_rwsem);
  414. INIT_LIST_HEAD(&uprobe->pending_list);
  415. /* add to uprobes_tree, sorted on inode:offset */
  416. cur_uprobe = insert_uprobe(uprobe);
  417. /* a uprobe exists for this inode:offset combination */
  418. if (cur_uprobe) {
  419. kfree(uprobe);
  420. uprobe = cur_uprobe;
  421. iput(inode);
  422. } else
  423. atomic_inc(&uprobe_events);
  424. return uprobe;
  425. }
  426. /* Returns the previous consumer */
  427. static struct uprobe_consumer *add_consumer(struct uprobe *uprobe,
  428. struct uprobe_consumer *consumer)
  429. {
  430. down_write(&uprobe->consumer_rwsem);
  431. consumer->next = uprobe->consumers;
  432. uprobe->consumers = consumer;
  433. up_write(&uprobe->consumer_rwsem);
  434. return consumer->next;
  435. }
  436. /*
  437. * For uprobe @uprobe, delete the consumer @consumer.
  438. * Return true if the @consumer is deleted successfully
  439. * or return false.
  440. */
  441. static bool del_consumer(struct uprobe *uprobe,
  442. struct uprobe_consumer *consumer)
  443. {
  444. struct uprobe_consumer **con;
  445. bool ret = false;
  446. down_write(&uprobe->consumer_rwsem);
  447. for (con = &uprobe->consumers; *con; con = &(*con)->next) {
  448. if (*con == consumer) {
  449. *con = consumer->next;
  450. ret = true;
  451. break;
  452. }
  453. }
  454. up_write(&uprobe->consumer_rwsem);
  455. return ret;
  456. }
  457. static int __copy_insn(struct address_space *mapping,
  458. struct vm_area_struct *vma, char *insn,
  459. unsigned long nbytes, unsigned long offset)
  460. {
  461. struct file *filp = vma->vm_file;
  462. struct page *page;
  463. void *vaddr;
  464. unsigned long off1;
  465. unsigned long idx;
  466. if (!filp)
  467. return -EINVAL;
  468. idx = (unsigned long)(offset >> PAGE_CACHE_SHIFT);
  469. off1 = offset &= ~PAGE_MASK;
  470. /*
  471. * Ensure that the page that has the original instruction is
  472. * populated and in page-cache.
  473. */
  474. page = read_mapping_page(mapping, idx, filp);
  475. if (IS_ERR(page))
  476. return PTR_ERR(page);
  477. vaddr = kmap_atomic(page);
  478. memcpy(insn, vaddr + off1, nbytes);
  479. kunmap_atomic(vaddr);
  480. page_cache_release(page);
  481. return 0;
  482. }
  483. static int copy_insn(struct uprobe *uprobe, struct vm_area_struct *vma,
  484. unsigned long addr)
  485. {
  486. struct address_space *mapping;
  487. int bytes;
  488. unsigned long nbytes;
  489. addr &= ~PAGE_MASK;
  490. nbytes = PAGE_SIZE - addr;
  491. mapping = uprobe->inode->i_mapping;
  492. /* Instruction at end of binary; copy only available bytes */
  493. if (uprobe->offset + MAX_UINSN_BYTES > uprobe->inode->i_size)
  494. bytes = uprobe->inode->i_size - uprobe->offset;
  495. else
  496. bytes = MAX_UINSN_BYTES;
  497. /* Instruction at the page-boundary; copy bytes in second page */
  498. if (nbytes < bytes) {
  499. if (__copy_insn(mapping, vma, uprobe->insn + nbytes,
  500. bytes - nbytes, uprobe->offset + nbytes))
  501. return -ENOMEM;
  502. bytes = nbytes;
  503. }
  504. return __copy_insn(mapping, vma, uprobe->insn, bytes, uprobe->offset);
  505. }
  506. static int install_breakpoint(struct mm_struct *mm, struct uprobe *uprobe,
  507. struct vm_area_struct *vma, loff_t vaddr)
  508. {
  509. unsigned long addr;
  510. int ret;
  511. /*
  512. * If probe is being deleted, unregister thread could be done with
  513. * the vma-rmap-walk through. Adding a probe now can be fatal since
  514. * nobody will be able to cleanup. Also we could be from fork or
  515. * mremap path, where the probe might have already been inserted.
  516. * Hence behave as if probe already existed.
  517. */
  518. if (!uprobe->consumers)
  519. return -EEXIST;
  520. addr = (unsigned long)vaddr;
  521. if (!(uprobe->flags & UPROBES_COPY_INSN)) {
  522. ret = copy_insn(uprobe, vma, addr);
  523. if (ret)
  524. return ret;
  525. if (is_bkpt_insn((uprobe_opcode_t *)uprobe->insn))
  526. return -EEXIST;
  527. ret = analyze_insn(mm, uprobe);
  528. if (ret)
  529. return ret;
  530. uprobe->flags |= UPROBES_COPY_INSN;
  531. }
  532. ret = set_bkpt(mm, uprobe, addr);
  533. return ret;
  534. }
  535. static void remove_breakpoint(struct mm_struct *mm, struct uprobe *uprobe,
  536. loff_t vaddr)
  537. {
  538. set_orig_insn(mm, uprobe, (unsigned long)vaddr, true);
  539. }
  540. static void delete_uprobe(struct uprobe *uprobe)
  541. {
  542. unsigned long flags;
  543. spin_lock_irqsave(&uprobes_treelock, flags);
  544. rb_erase(&uprobe->rb_node, &uprobes_tree);
  545. spin_unlock_irqrestore(&uprobes_treelock, flags);
  546. iput(uprobe->inode);
  547. put_uprobe(uprobe);
  548. atomic_dec(&uprobe_events);
  549. }
  550. static struct vma_info *__find_next_vma_info(struct list_head *head,
  551. loff_t offset, struct address_space *mapping,
  552. struct vma_info *vi, bool is_register)
  553. {
  554. struct prio_tree_iter iter;
  555. struct vm_area_struct *vma;
  556. struct vma_info *tmpvi;
  557. loff_t vaddr;
  558. unsigned long pgoff = offset >> PAGE_SHIFT;
  559. int existing_vma;
  560. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  561. if (!valid_vma(vma, is_register))
  562. continue;
  563. existing_vma = 0;
  564. vaddr = vma_address(vma, offset);
  565. list_for_each_entry(tmpvi, head, probe_list) {
  566. if (tmpvi->mm == vma->vm_mm && tmpvi->vaddr == vaddr) {
  567. existing_vma = 1;
  568. break;
  569. }
  570. }
  571. /*
  572. * Another vma needs a probe to be installed. However skip
  573. * installing the probe if the vma is about to be unlinked.
  574. */
  575. if (!existing_vma &&
  576. atomic_inc_not_zero(&vma->vm_mm->mm_users)) {
  577. vi->mm = vma->vm_mm;
  578. vi->vaddr = vaddr;
  579. list_add(&vi->probe_list, head);
  580. return vi;
  581. }
  582. }
  583. return NULL;
  584. }
  585. /*
  586. * Iterate in the rmap prio tree and find a vma where a probe has not
  587. * yet been inserted.
  588. */
  589. static struct vma_info *find_next_vma_info(struct list_head *head,
  590. loff_t offset, struct address_space *mapping,
  591. bool is_register)
  592. {
  593. struct vma_info *vi, *retvi;
  594. vi = kzalloc(sizeof(struct vma_info), GFP_KERNEL);
  595. if (!vi)
  596. return ERR_PTR(-ENOMEM);
  597. mutex_lock(&mapping->i_mmap_mutex);
  598. retvi = __find_next_vma_info(head, offset, mapping, vi, is_register);
  599. mutex_unlock(&mapping->i_mmap_mutex);
  600. if (!retvi)
  601. kfree(vi);
  602. return retvi;
  603. }
  604. static int register_for_each_vma(struct uprobe *uprobe, bool is_register)
  605. {
  606. struct list_head try_list;
  607. struct vm_area_struct *vma;
  608. struct address_space *mapping;
  609. struct vma_info *vi, *tmpvi;
  610. struct mm_struct *mm;
  611. loff_t vaddr;
  612. int ret = 0;
  613. mapping = uprobe->inode->i_mapping;
  614. INIT_LIST_HEAD(&try_list);
  615. while ((vi = find_next_vma_info(&try_list, uprobe->offset,
  616. mapping, is_register)) != NULL) {
  617. if (IS_ERR(vi)) {
  618. ret = PTR_ERR(vi);
  619. break;
  620. }
  621. mm = vi->mm;
  622. down_read(&mm->mmap_sem);
  623. vma = find_vma(mm, (unsigned long)vi->vaddr);
  624. if (!vma || !valid_vma(vma, is_register)) {
  625. list_del(&vi->probe_list);
  626. kfree(vi);
  627. up_read(&mm->mmap_sem);
  628. mmput(mm);
  629. continue;
  630. }
  631. vaddr = vma_address(vma, uprobe->offset);
  632. if (vma->vm_file->f_mapping->host != uprobe->inode ||
  633. vaddr != vi->vaddr) {
  634. list_del(&vi->probe_list);
  635. kfree(vi);
  636. up_read(&mm->mmap_sem);
  637. mmput(mm);
  638. continue;
  639. }
  640. if (is_register)
  641. ret = install_breakpoint(mm, uprobe, vma, vi->vaddr);
  642. else
  643. remove_breakpoint(mm, uprobe, vi->vaddr);
  644. up_read(&mm->mmap_sem);
  645. mmput(mm);
  646. if (is_register) {
  647. if (ret && ret == -EEXIST)
  648. ret = 0;
  649. if (ret)
  650. break;
  651. }
  652. }
  653. list_for_each_entry_safe(vi, tmpvi, &try_list, probe_list) {
  654. list_del(&vi->probe_list);
  655. kfree(vi);
  656. }
  657. return ret;
  658. }
  659. static int __register_uprobe(struct uprobe *uprobe)
  660. {
  661. return register_for_each_vma(uprobe, true);
  662. }
  663. static void __unregister_uprobe(struct uprobe *uprobe)
  664. {
  665. if (!register_for_each_vma(uprobe, false))
  666. delete_uprobe(uprobe);
  667. /* TODO : cant unregister? schedule a worker thread */
  668. }
  669. /*
  670. * register_uprobe - register a probe
  671. * @inode: the file in which the probe has to be placed.
  672. * @offset: offset from the start of the file.
  673. * @consumer: information on howto handle the probe..
  674. *
  675. * Apart from the access refcount, register_uprobe() takes a creation
  676. * refcount (thro alloc_uprobe) if and only if this @uprobe is getting
  677. * inserted into the rbtree (i.e first consumer for a @inode:@offset
  678. * tuple). Creation refcount stops unregister_uprobe from freeing the
  679. * @uprobe even before the register operation is complete. Creation
  680. * refcount is released when the last @consumer for the @uprobe
  681. * unregisters.
  682. *
  683. * Return errno if it cannot successully install probes
  684. * else return 0 (success)
  685. */
  686. int register_uprobe(struct inode *inode, loff_t offset,
  687. struct uprobe_consumer *consumer)
  688. {
  689. struct uprobe *uprobe;
  690. int ret = -EINVAL;
  691. if (!inode || !consumer || consumer->next)
  692. return ret;
  693. if (offset > i_size_read(inode))
  694. return ret;
  695. ret = 0;
  696. mutex_lock(uprobes_hash(inode));
  697. uprobe = alloc_uprobe(inode, offset);
  698. if (uprobe && !add_consumer(uprobe, consumer)) {
  699. ret = __register_uprobe(uprobe);
  700. if (ret) {
  701. uprobe->consumers = NULL;
  702. __unregister_uprobe(uprobe);
  703. } else
  704. uprobe->flags |= UPROBES_RUN_HANDLER;
  705. }
  706. mutex_unlock(uprobes_hash(inode));
  707. put_uprobe(uprobe);
  708. return ret;
  709. }
  710. /*
  711. * unregister_uprobe - unregister a already registered probe.
  712. * @inode: the file in which the probe has to be removed.
  713. * @offset: offset from the start of the file.
  714. * @consumer: identify which probe if multiple probes are colocated.
  715. */
  716. void unregister_uprobe(struct inode *inode, loff_t offset,
  717. struct uprobe_consumer *consumer)
  718. {
  719. struct uprobe *uprobe = NULL;
  720. if (!inode || !consumer)
  721. return;
  722. uprobe = find_uprobe(inode, offset);
  723. if (!uprobe)
  724. return;
  725. mutex_lock(uprobes_hash(inode));
  726. if (!del_consumer(uprobe, consumer))
  727. goto unreg_out;
  728. if (!uprobe->consumers) {
  729. __unregister_uprobe(uprobe);
  730. uprobe->flags &= ~UPROBES_RUN_HANDLER;
  731. }
  732. unreg_out:
  733. mutex_unlock(uprobes_hash(inode));
  734. if (uprobe)
  735. put_uprobe(uprobe);
  736. }
  737. /*
  738. * Of all the nodes that correspond to the given inode, return the node
  739. * with the least offset.
  740. */
  741. static struct rb_node *find_least_offset_node(struct inode *inode)
  742. {
  743. struct uprobe u = { .inode = inode, .offset = 0};
  744. struct rb_node *n = uprobes_tree.rb_node;
  745. struct rb_node *close_node = NULL;
  746. struct uprobe *uprobe;
  747. int match;
  748. while (n) {
  749. uprobe = rb_entry(n, struct uprobe, rb_node);
  750. match = match_uprobe(&u, uprobe);
  751. if (uprobe->inode == inode)
  752. close_node = n;
  753. if (!match)
  754. return close_node;
  755. if (match < 0)
  756. n = n->rb_left;
  757. else
  758. n = n->rb_right;
  759. }
  760. return close_node;
  761. }
  762. /*
  763. * For a given inode, build a list of probes that need to be inserted.
  764. */
  765. static void build_probe_list(struct inode *inode, struct list_head *head)
  766. {
  767. struct uprobe *uprobe;
  768. struct rb_node *n;
  769. unsigned long flags;
  770. spin_lock_irqsave(&uprobes_treelock, flags);
  771. n = find_least_offset_node(inode);
  772. for (; n; n = rb_next(n)) {
  773. uprobe = rb_entry(n, struct uprobe, rb_node);
  774. if (uprobe->inode != inode)
  775. break;
  776. list_add(&uprobe->pending_list, head);
  777. atomic_inc(&uprobe->ref);
  778. }
  779. spin_unlock_irqrestore(&uprobes_treelock, flags);
  780. }
  781. /*
  782. * Called from mmap_region.
  783. * called with mm->mmap_sem acquired.
  784. *
  785. * Return -ve no if we fail to insert probes and we cannot
  786. * bail-out.
  787. * Return 0 otherwise. i.e :
  788. * - successful insertion of probes
  789. * - (or) no possible probes to be inserted.
  790. * - (or) insertion of probes failed but we can bail-out.
  791. */
  792. int mmap_uprobe(struct vm_area_struct *vma)
  793. {
  794. struct list_head tmp_list;
  795. struct uprobe *uprobe, *u;
  796. struct inode *inode;
  797. int ret = 0;
  798. if (!atomic_read(&uprobe_events) || !valid_vma(vma, true))
  799. return ret; /* Bail-out */
  800. inode = vma->vm_file->f_mapping->host;
  801. if (!inode)
  802. return ret;
  803. INIT_LIST_HEAD(&tmp_list);
  804. mutex_lock(uprobes_mmap_hash(inode));
  805. build_probe_list(inode, &tmp_list);
  806. list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
  807. loff_t vaddr;
  808. list_del(&uprobe->pending_list);
  809. if (!ret) {
  810. vaddr = vma_address(vma, uprobe->offset);
  811. if (vaddr < vma->vm_start || vaddr >= vma->vm_end) {
  812. put_uprobe(uprobe);
  813. continue;
  814. }
  815. ret = install_breakpoint(vma->vm_mm, uprobe, vma,
  816. vaddr);
  817. if (ret == -EEXIST)
  818. ret = 0;
  819. }
  820. put_uprobe(uprobe);
  821. }
  822. mutex_unlock(uprobes_mmap_hash(inode));
  823. return ret;
  824. }
  825. static int __init init_uprobes(void)
  826. {
  827. int i;
  828. for (i = 0; i < UPROBES_HASH_SZ; i++) {
  829. mutex_init(&uprobes_mutex[i]);
  830. mutex_init(&uprobes_mmap_mutex[i]);
  831. }
  832. return 0;
  833. }
  834. static void __exit exit_uprobes(void)
  835. {
  836. }
  837. module_init(init_uprobes);
  838. module_exit(exit_uprobes);