uprobes.c 23 KB

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