uprobes.c 24 KB

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