uprobes.c 24 KB

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