uprobes.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026
  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_bkpt_insn - check if instruction is breakpoint instruction.
  145. * @insn: instruction to be checked.
  146. * Default implementation of is_bkpt_insn
  147. * Returns true if @insn is a breakpoint instruction.
  148. */
  149. bool __weak is_bkpt_insn(uprobe_opcode_t *insn)
  150. {
  151. return *insn == UPROBES_BKPT_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. * @mm: the probed process address space.
  165. * @arch_uprobe: the breakpointing information.
  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 mm_struct *mm, struct arch_uprobe *auprobe,
  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_bkpt_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 + UPROBES_BKPT_INSN_SIZE > PAGE_SIZE);
  222. memcpy(vaddr_new + vaddr, &opcode, UPROBES_BKPT_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, UPROBES_BKPT_INSN_SIZE);
  262. kunmap_atomic(vaddr_new);
  263. unlock_page(page);
  264. put_page(page);
  265. return 0;
  266. }
  267. static int is_bkpt_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_bkpt_insn(&opcode))
  275. return 1;
  276. return 0;
  277. }
  278. /**
  279. * set_bkpt - store breakpoint at a given address.
  280. * @mm: the probed process address space.
  281. * @uprobe: the probepoint information.
  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_bkpt(struct mm_struct *mm, struct arch_uprobe *auprobe, unsigned long vaddr)
  288. {
  289. int result;
  290. result = is_bkpt_at_addr(mm, vaddr);
  291. if (result == 1)
  292. return -EEXIST;
  293. if (result)
  294. return result;
  295. return write_opcode(mm, auprobe, vaddr, UPROBES_BKPT_INSN);
  296. }
  297. /**
  298. * set_orig_insn - Restore the original instruction.
  299. * @mm: the probed process address space.
  300. * @uprobe: the 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 mm_struct *mm, struct arch_uprobe *auprobe, unsigned long vaddr, bool verify)
  309. {
  310. if (verify) {
  311. int result;
  312. result = is_bkpt_at_addr(mm, vaddr);
  313. if (!result)
  314. return -EINVAL;
  315. if (result != 1)
  316. return result;
  317. }
  318. return write_opcode(mm, auprobe, 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 *consumer)
  438. {
  439. down_write(&uprobe->consumer_rwsem);
  440. consumer->next = uprobe->consumers;
  441. uprobe->consumers = consumer;
  442. up_write(&uprobe->consumer_rwsem);
  443. return consumer->next;
  444. }
  445. /*
  446. * For uprobe @uprobe, delete the consumer @consumer.
  447. * Return true if the @consumer is deleted successfully
  448. * or return false.
  449. */
  450. static bool consumer_del(struct uprobe *uprobe, struct uprobe_consumer *consumer)
  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 == consumer) {
  457. *con = consumer->next;
  458. ret = true;
  459. break;
  460. }
  461. }
  462. up_write(&uprobe->consumer_rwsem);
  463. return ret;
  464. }
  465. static int __copy_insn(struct address_space *mapping,
  466. 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 copy_insn(struct uprobe *uprobe, struct vm_area_struct *vma, unsigned long addr)
  492. {
  493. struct address_space *mapping;
  494. unsigned long nbytes;
  495. int bytes;
  496. addr &= ~PAGE_MASK;
  497. nbytes = PAGE_SIZE - addr;
  498. mapping = uprobe->inode->i_mapping;
  499. /* Instruction at end of binary; copy only available bytes */
  500. if (uprobe->offset + MAX_UINSN_BYTES > uprobe->inode->i_size)
  501. bytes = uprobe->inode->i_size - uprobe->offset;
  502. else
  503. bytes = MAX_UINSN_BYTES;
  504. /* Instruction at the page-boundary; copy bytes in second page */
  505. if (nbytes < bytes) {
  506. if (__copy_insn(mapping, vma, uprobe->arch.insn + nbytes,
  507. bytes - nbytes, uprobe->offset + nbytes))
  508. return -ENOMEM;
  509. bytes = nbytes;
  510. }
  511. return __copy_insn(mapping, vma, uprobe->arch.insn, bytes, uprobe->offset);
  512. }
  513. static int install_breakpoint(struct mm_struct *mm, struct uprobe *uprobe,
  514. struct vm_area_struct *vma, loff_t vaddr)
  515. {
  516. unsigned long addr;
  517. int ret;
  518. /*
  519. * If probe is being deleted, unregister thread could be done with
  520. * the vma-rmap-walk through. Adding a probe now can be fatal since
  521. * nobody will be able to cleanup. Also we could be from fork or
  522. * mremap path, where the probe might have already been inserted.
  523. * Hence behave as if probe already existed.
  524. */
  525. if (!uprobe->consumers)
  526. return -EEXIST;
  527. addr = (unsigned long)vaddr;
  528. if (!(uprobe->flags & UPROBES_COPY_INSN)) {
  529. ret = copy_insn(uprobe, vma, addr);
  530. if (ret)
  531. return ret;
  532. if (is_bkpt_insn((uprobe_opcode_t *)uprobe->arch.insn))
  533. return -EEXIST;
  534. ret = arch_uprobes_analyze_insn(mm, &uprobe->arch);
  535. if (ret)
  536. return ret;
  537. uprobe->flags |= UPROBES_COPY_INSN;
  538. }
  539. ret = set_bkpt(mm, &uprobe->arch, addr);
  540. return ret;
  541. }
  542. static void remove_breakpoint(struct mm_struct *mm, struct uprobe *uprobe, loff_t vaddr)
  543. {
  544. set_orig_insn(mm, &uprobe->arch, (unsigned long)vaddr, true);
  545. }
  546. static void delete_uprobe(struct uprobe *uprobe)
  547. {
  548. unsigned long flags;
  549. spin_lock_irqsave(&uprobes_treelock, flags);
  550. rb_erase(&uprobe->rb_node, &uprobes_tree);
  551. spin_unlock_irqrestore(&uprobes_treelock, flags);
  552. iput(uprobe->inode);
  553. put_uprobe(uprobe);
  554. atomic_dec(&uprobe_events);
  555. }
  556. static struct vma_info *__find_next_vma_info(struct list_head *head,
  557. loff_t offset, struct address_space *mapping,
  558. struct vma_info *vi, bool is_register)
  559. {
  560. struct prio_tree_iter iter;
  561. struct vm_area_struct *vma;
  562. struct vma_info *tmpvi;
  563. unsigned long pgoff;
  564. int existing_vma;
  565. loff_t vaddr;
  566. pgoff = offset >> PAGE_SHIFT;
  567. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  568. if (!valid_vma(vma, is_register))
  569. continue;
  570. existing_vma = 0;
  571. vaddr = vma_address(vma, offset);
  572. list_for_each_entry(tmpvi, head, probe_list) {
  573. if (tmpvi->mm == vma->vm_mm && tmpvi->vaddr == vaddr) {
  574. existing_vma = 1;
  575. break;
  576. }
  577. }
  578. /*
  579. * Another vma needs a probe to be installed. However skip
  580. * installing the probe if the vma is about to be unlinked.
  581. */
  582. if (!existing_vma && atomic_inc_not_zero(&vma->vm_mm->mm_users)) {
  583. vi->mm = vma->vm_mm;
  584. vi->vaddr = vaddr;
  585. list_add(&vi->probe_list, head);
  586. return vi;
  587. }
  588. }
  589. return NULL;
  590. }
  591. /*
  592. * Iterate in the rmap prio tree and find a vma where a probe has not
  593. * yet been inserted.
  594. */
  595. static struct vma_info *
  596. find_next_vma_info(struct list_head *head, loff_t offset, struct address_space *mapping,
  597. bool is_register)
  598. {
  599. struct vma_info *vi, *retvi;
  600. vi = kzalloc(sizeof(struct vma_info), GFP_KERNEL);
  601. if (!vi)
  602. return ERR_PTR(-ENOMEM);
  603. mutex_lock(&mapping->i_mmap_mutex);
  604. retvi = __find_next_vma_info(head, offset, mapping, vi, is_register);
  605. mutex_unlock(&mapping->i_mmap_mutex);
  606. if (!retvi)
  607. kfree(vi);
  608. return retvi;
  609. }
  610. static int register_for_each_vma(struct uprobe *uprobe, bool is_register)
  611. {
  612. struct list_head try_list;
  613. struct vm_area_struct *vma;
  614. struct address_space *mapping;
  615. struct vma_info *vi, *tmpvi;
  616. struct mm_struct *mm;
  617. loff_t vaddr;
  618. int ret;
  619. mapping = uprobe->inode->i_mapping;
  620. INIT_LIST_HEAD(&try_list);
  621. ret = 0;
  622. for (;;) {
  623. vi = find_next_vma_info(&try_list, uprobe->offset, mapping, is_register);
  624. if (!vi)
  625. break;
  626. if (IS_ERR(vi)) {
  627. ret = PTR_ERR(vi);
  628. break;
  629. }
  630. mm = vi->mm;
  631. down_read(&mm->mmap_sem);
  632. vma = find_vma(mm, (unsigned long)vi->vaddr);
  633. if (!vma || !valid_vma(vma, is_register)) {
  634. list_del(&vi->probe_list);
  635. kfree(vi);
  636. up_read(&mm->mmap_sem);
  637. mmput(mm);
  638. continue;
  639. }
  640. vaddr = vma_address(vma, uprobe->offset);
  641. if (vma->vm_file->f_mapping->host != uprobe->inode ||
  642. vaddr != vi->vaddr) {
  643. list_del(&vi->probe_list);
  644. kfree(vi);
  645. up_read(&mm->mmap_sem);
  646. mmput(mm);
  647. continue;
  648. }
  649. if (is_register)
  650. ret = install_breakpoint(mm, uprobe, vma, vi->vaddr);
  651. else
  652. remove_breakpoint(mm, uprobe, vi->vaddr);
  653. up_read(&mm->mmap_sem);
  654. mmput(mm);
  655. if (is_register) {
  656. if (ret && ret == -EEXIST)
  657. ret = 0;
  658. if (ret)
  659. break;
  660. }
  661. }
  662. list_for_each_entry_safe(vi, tmpvi, &try_list, probe_list) {
  663. list_del(&vi->probe_list);
  664. kfree(vi);
  665. }
  666. return ret;
  667. }
  668. static int __uprobe_register(struct uprobe *uprobe)
  669. {
  670. return register_for_each_vma(uprobe, true);
  671. }
  672. static void __uprobe_unregister(struct uprobe *uprobe)
  673. {
  674. if (!register_for_each_vma(uprobe, false))
  675. delete_uprobe(uprobe);
  676. /* TODO : cant unregister? schedule a worker thread */
  677. }
  678. /*
  679. * uprobe_register - register a probe
  680. * @inode: the file in which the probe has to be placed.
  681. * @offset: offset from the start of the file.
  682. * @consumer: information on howto handle the probe..
  683. *
  684. * Apart from the access refcount, uprobe_register() takes a creation
  685. * refcount (thro alloc_uprobe) if and only if this @uprobe is getting
  686. * inserted into the rbtree (i.e first consumer for a @inode:@offset
  687. * tuple). Creation refcount stops uprobe_unregister from freeing the
  688. * @uprobe even before the register operation is complete. Creation
  689. * refcount is released when the last @consumer for the @uprobe
  690. * unregisters.
  691. *
  692. * Return errno if it cannot successully install probes
  693. * else return 0 (success)
  694. */
  695. int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *consumer)
  696. {
  697. struct uprobe *uprobe;
  698. int ret;
  699. if (!inode || !consumer || consumer->next)
  700. return -EINVAL;
  701. if (offset > i_size_read(inode))
  702. return -EINVAL;
  703. ret = 0;
  704. mutex_lock(uprobes_hash(inode));
  705. uprobe = alloc_uprobe(inode, offset);
  706. if (uprobe && !consumer_add(uprobe, consumer)) {
  707. ret = __uprobe_register(uprobe);
  708. if (ret) {
  709. uprobe->consumers = NULL;
  710. __uprobe_unregister(uprobe);
  711. } else {
  712. uprobe->flags |= UPROBES_RUN_HANDLER;
  713. }
  714. }
  715. mutex_unlock(uprobes_hash(inode));
  716. put_uprobe(uprobe);
  717. return ret;
  718. }
  719. /*
  720. * uprobe_unregister - unregister a already registered probe.
  721. * @inode: the file in which the probe has to be removed.
  722. * @offset: offset from the start of the file.
  723. * @consumer: identify which probe if multiple probes are colocated.
  724. */
  725. void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *consumer)
  726. {
  727. struct uprobe *uprobe;
  728. if (!inode || !consumer)
  729. return;
  730. uprobe = find_uprobe(inode, offset);
  731. if (!uprobe)
  732. return;
  733. mutex_lock(uprobes_hash(inode));
  734. if (consumer_del(uprobe, consumer)) {
  735. if (!uprobe->consumers) {
  736. __uprobe_unregister(uprobe);
  737. uprobe->flags &= ~UPROBES_RUN_HANDLER;
  738. }
  739. }
  740. mutex_unlock(uprobes_hash(inode));
  741. if (uprobe)
  742. put_uprobe(uprobe);
  743. }
  744. /*
  745. * Of all the nodes that correspond to the given inode, return the node
  746. * with the least offset.
  747. */
  748. static struct rb_node *find_least_offset_node(struct inode *inode)
  749. {
  750. struct uprobe u = { .inode = inode, .offset = 0};
  751. struct rb_node *n = uprobes_tree.rb_node;
  752. struct rb_node *close_node = NULL;
  753. struct uprobe *uprobe;
  754. int match;
  755. while (n) {
  756. uprobe = rb_entry(n, struct uprobe, rb_node);
  757. match = match_uprobe(&u, uprobe);
  758. if (uprobe->inode == inode)
  759. close_node = n;
  760. if (!match)
  761. return close_node;
  762. if (match < 0)
  763. n = n->rb_left;
  764. else
  765. n = n->rb_right;
  766. }
  767. return close_node;
  768. }
  769. /*
  770. * For a given inode, build a list of probes that need to be inserted.
  771. */
  772. static void build_probe_list(struct inode *inode, struct list_head *head)
  773. {
  774. struct uprobe *uprobe;
  775. unsigned long flags;
  776. struct rb_node *n;
  777. spin_lock_irqsave(&uprobes_treelock, flags);
  778. n = find_least_offset_node(inode);
  779. for (; n; n = rb_next(n)) {
  780. uprobe = rb_entry(n, struct uprobe, rb_node);
  781. if (uprobe->inode != inode)
  782. break;
  783. list_add(&uprobe->pending_list, head);
  784. atomic_inc(&uprobe->ref);
  785. }
  786. spin_unlock_irqrestore(&uprobes_treelock, flags);
  787. }
  788. /*
  789. * Called from mmap_region.
  790. * called with mm->mmap_sem acquired.
  791. *
  792. * Return -ve no if we fail to insert probes and we cannot
  793. * bail-out.
  794. * Return 0 otherwise. i.e:
  795. *
  796. * - successful insertion of probes
  797. * - (or) no possible probes to be inserted.
  798. * - (or) insertion of probes failed but we can bail-out.
  799. */
  800. int uprobe_mmap(struct vm_area_struct *vma)
  801. {
  802. struct list_head tmp_list;
  803. struct uprobe *uprobe, *u;
  804. struct inode *inode;
  805. int ret;
  806. if (!atomic_read(&uprobe_events) || !valid_vma(vma, true))
  807. return 0;
  808. inode = vma->vm_file->f_mapping->host;
  809. if (!inode)
  810. return 0;
  811. INIT_LIST_HEAD(&tmp_list);
  812. mutex_lock(uprobes_mmap_hash(inode));
  813. build_probe_list(inode, &tmp_list);
  814. ret = 0;
  815. list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
  816. loff_t vaddr;
  817. list_del(&uprobe->pending_list);
  818. if (!ret) {
  819. vaddr = vma_address(vma, uprobe->offset);
  820. if (vaddr >= vma->vm_start && vaddr < vma->vm_end) {
  821. ret = install_breakpoint(vma->vm_mm, uprobe, vma, vaddr);
  822. /* Ignore double add: */
  823. if (ret == -EEXIST)
  824. ret = 0;
  825. }
  826. }
  827. put_uprobe(uprobe);
  828. }
  829. mutex_unlock(uprobes_mmap_hash(inode));
  830. return ret;
  831. }
  832. static int __init init_uprobes(void)
  833. {
  834. int i;
  835. for (i = 0; i < UPROBES_HASH_SZ; i++) {
  836. mutex_init(&uprobes_mutex[i]);
  837. mutex_init(&uprobes_mmap_mutex[i]);
  838. }
  839. return 0;
  840. }
  841. static void __exit exit_uprobes(void)
  842. {
  843. }
  844. module_init(init_uprobes);
  845. module_exit(exit_uprobes);