uprobes.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645
  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/ptrace.h> /* user_enable_single_step */
  33. #include <linux/kdebug.h> /* notifier mechanism */
  34. #include "../../mm/internal.h" /* munlock_vma_page */
  35. #include <linux/percpu-rwsem.h>
  36. #include <linux/uprobes.h>
  37. #define UINSNS_PER_PAGE (PAGE_SIZE/UPROBE_XOL_SLOT_BYTES)
  38. #define MAX_UPROBE_XOL_SLOTS UINSNS_PER_PAGE
  39. static struct rb_root uprobes_tree = RB_ROOT;
  40. /*
  41. * allows us to skip the uprobe_mmap if there are no uprobe events active
  42. * at this time. Probably a fine grained per inode count is better?
  43. */
  44. #define no_uprobe_events() RB_EMPTY_ROOT(&uprobes_tree)
  45. static DEFINE_SPINLOCK(uprobes_treelock); /* serialize rbtree access */
  46. #define UPROBES_HASH_SZ 13
  47. /* serialize uprobe->pending_list */
  48. static struct mutex uprobes_mmap_mutex[UPROBES_HASH_SZ];
  49. #define uprobes_mmap_hash(v) (&uprobes_mmap_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
  50. static struct percpu_rw_semaphore dup_mmap_sem;
  51. /* Have a copy of original instruction */
  52. #define UPROBE_COPY_INSN 0
  53. /* Can skip singlestep */
  54. #define UPROBE_SKIP_SSTEP 1
  55. struct uprobe {
  56. struct rb_node rb_node; /* node in the rb tree */
  57. atomic_t ref;
  58. struct rw_semaphore register_rwsem;
  59. struct rw_semaphore consumer_rwsem;
  60. struct list_head pending_list;
  61. struct uprobe_consumer *consumers;
  62. struct inode *inode; /* Also hold a ref to inode */
  63. loff_t offset;
  64. unsigned long flags;
  65. struct arch_uprobe arch;
  66. };
  67. /*
  68. * valid_vma: Verify if the specified vma is an executable vma
  69. * Relax restrictions while unregistering: vm_flags might have
  70. * changed after breakpoint was inserted.
  71. * - is_register: indicates if we are in register context.
  72. * - Return 1 if the specified virtual address is in an
  73. * executable vma.
  74. */
  75. static bool valid_vma(struct vm_area_struct *vma, bool is_register)
  76. {
  77. vm_flags_t flags = VM_HUGETLB | VM_MAYEXEC | VM_SHARED;
  78. if (is_register)
  79. flags |= VM_WRITE;
  80. return vma->vm_file && (vma->vm_flags & flags) == VM_MAYEXEC;
  81. }
  82. static unsigned long offset_to_vaddr(struct vm_area_struct *vma, loff_t offset)
  83. {
  84. return vma->vm_start + offset - ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
  85. }
  86. static loff_t vaddr_to_offset(struct vm_area_struct *vma, unsigned long vaddr)
  87. {
  88. return ((loff_t)vma->vm_pgoff << PAGE_SHIFT) + (vaddr - vma->vm_start);
  89. }
  90. /**
  91. * __replace_page - replace page in vma by new page.
  92. * based on replace_page in mm/ksm.c
  93. *
  94. * @vma: vma that holds the pte pointing to page
  95. * @addr: address the old @page is mapped at
  96. * @page: the cowed page we are replacing by kpage
  97. * @kpage: the modified page we replace page by
  98. *
  99. * Returns 0 on success, -EFAULT on failure.
  100. */
  101. static int __replace_page(struct vm_area_struct *vma, unsigned long addr,
  102. struct page *page, struct page *kpage)
  103. {
  104. struct mm_struct *mm = vma->vm_mm;
  105. spinlock_t *ptl;
  106. pte_t *ptep;
  107. int err;
  108. /* For mmu_notifiers */
  109. const unsigned long mmun_start = addr;
  110. const unsigned long mmun_end = addr + PAGE_SIZE;
  111. /* For try_to_free_swap() and munlock_vma_page() below */
  112. lock_page(page);
  113. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  114. err = -EAGAIN;
  115. ptep = page_check_address(page, mm, addr, &ptl, 0);
  116. if (!ptep)
  117. goto unlock;
  118. get_page(kpage);
  119. page_add_new_anon_rmap(kpage, vma, addr);
  120. if (!PageAnon(page)) {
  121. dec_mm_counter(mm, MM_FILEPAGES);
  122. inc_mm_counter(mm, MM_ANONPAGES);
  123. }
  124. flush_cache_page(vma, addr, pte_pfn(*ptep));
  125. ptep_clear_flush(vma, addr, ptep);
  126. set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot));
  127. page_remove_rmap(page);
  128. if (!page_mapped(page))
  129. try_to_free_swap(page);
  130. pte_unmap_unlock(ptep, ptl);
  131. if (vma->vm_flags & VM_LOCKED)
  132. munlock_vma_page(page);
  133. put_page(page);
  134. err = 0;
  135. unlock:
  136. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  137. unlock_page(page);
  138. return err;
  139. }
  140. /**
  141. * is_swbp_insn - check if instruction is breakpoint instruction.
  142. * @insn: instruction to be checked.
  143. * Default implementation of is_swbp_insn
  144. * Returns true if @insn is a breakpoint instruction.
  145. */
  146. bool __weak is_swbp_insn(uprobe_opcode_t *insn)
  147. {
  148. return *insn == UPROBE_SWBP_INSN;
  149. }
  150. static void copy_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t *opcode)
  151. {
  152. void *kaddr = kmap_atomic(page);
  153. memcpy(opcode, kaddr + (vaddr & ~PAGE_MASK), UPROBE_SWBP_INSN_SIZE);
  154. kunmap_atomic(kaddr);
  155. }
  156. static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t *new_opcode)
  157. {
  158. uprobe_opcode_t old_opcode;
  159. bool is_swbp;
  160. copy_opcode(page, vaddr, &old_opcode);
  161. is_swbp = is_swbp_insn(&old_opcode);
  162. if (is_swbp_insn(new_opcode)) {
  163. if (is_swbp) /* register: already installed? */
  164. return 0;
  165. } else {
  166. if (!is_swbp) /* unregister: was it changed by us? */
  167. return 0;
  168. }
  169. return 1;
  170. }
  171. /*
  172. * NOTE:
  173. * Expect the breakpoint instruction to be the smallest size instruction for
  174. * the architecture. If an arch has variable length instruction and the
  175. * breakpoint instruction is not of the smallest length instruction
  176. * supported by that architecture then we need to modify is_swbp_at_addr and
  177. * write_opcode accordingly. This would never be a problem for archs that
  178. * have fixed length instructions.
  179. */
  180. /*
  181. * write_opcode - write the opcode at a given virtual address.
  182. * @mm: the probed process address space.
  183. * @vaddr: the virtual address to store the opcode.
  184. * @opcode: opcode to be written at @vaddr.
  185. *
  186. * Called with mm->mmap_sem held (for read and with a reference to
  187. * mm).
  188. *
  189. * For mm @mm, write the opcode at @vaddr.
  190. * Return 0 (success) or a negative errno.
  191. */
  192. static int write_opcode(struct mm_struct *mm, unsigned long vaddr,
  193. uprobe_opcode_t opcode)
  194. {
  195. struct page *old_page, *new_page;
  196. void *vaddr_old, *vaddr_new;
  197. struct vm_area_struct *vma;
  198. int ret;
  199. retry:
  200. /* Read the page with vaddr into memory */
  201. ret = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &old_page, &vma);
  202. if (ret <= 0)
  203. return ret;
  204. ret = verify_opcode(old_page, vaddr, &opcode);
  205. if (ret <= 0)
  206. goto put_old;
  207. ret = -ENOMEM;
  208. new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vaddr);
  209. if (!new_page)
  210. goto put_old;
  211. __SetPageUptodate(new_page);
  212. /* copy the page now that we've got it stable */
  213. vaddr_old = kmap_atomic(old_page);
  214. vaddr_new = kmap_atomic(new_page);
  215. memcpy(vaddr_new, vaddr_old, PAGE_SIZE);
  216. memcpy(vaddr_new + (vaddr & ~PAGE_MASK), &opcode, UPROBE_SWBP_INSN_SIZE);
  217. kunmap_atomic(vaddr_new);
  218. kunmap_atomic(vaddr_old);
  219. ret = anon_vma_prepare(vma);
  220. if (ret)
  221. goto put_new;
  222. ret = __replace_page(vma, vaddr, old_page, new_page);
  223. put_new:
  224. page_cache_release(new_page);
  225. put_old:
  226. put_page(old_page);
  227. if (unlikely(ret == -EAGAIN))
  228. goto retry;
  229. return ret;
  230. }
  231. /**
  232. * set_swbp - store breakpoint at a given address.
  233. * @auprobe: arch specific probepoint information.
  234. * @mm: the probed process address space.
  235. * @vaddr: the virtual address to insert the opcode.
  236. *
  237. * For mm @mm, store the breakpoint instruction at @vaddr.
  238. * Return 0 (success) or a negative errno.
  239. */
  240. int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
  241. {
  242. return write_opcode(mm, vaddr, UPROBE_SWBP_INSN);
  243. }
  244. /**
  245. * set_orig_insn - Restore the original instruction.
  246. * @mm: the probed process address space.
  247. * @auprobe: arch specific probepoint information.
  248. * @vaddr: the virtual address to insert the opcode.
  249. *
  250. * For mm @mm, restore the original opcode (opcode) at @vaddr.
  251. * Return 0 (success) or a negative errno.
  252. */
  253. int __weak
  254. set_orig_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
  255. {
  256. return write_opcode(mm, vaddr, *(uprobe_opcode_t *)auprobe->insn);
  257. }
  258. static int match_uprobe(struct uprobe *l, struct uprobe *r)
  259. {
  260. if (l->inode < r->inode)
  261. return -1;
  262. if (l->inode > r->inode)
  263. return 1;
  264. if (l->offset < r->offset)
  265. return -1;
  266. if (l->offset > r->offset)
  267. return 1;
  268. return 0;
  269. }
  270. static struct uprobe *__find_uprobe(struct inode *inode, loff_t offset)
  271. {
  272. struct uprobe u = { .inode = inode, .offset = offset };
  273. struct rb_node *n = uprobes_tree.rb_node;
  274. struct uprobe *uprobe;
  275. int match;
  276. while (n) {
  277. uprobe = rb_entry(n, struct uprobe, rb_node);
  278. match = match_uprobe(&u, uprobe);
  279. if (!match) {
  280. atomic_inc(&uprobe->ref);
  281. return uprobe;
  282. }
  283. if (match < 0)
  284. n = n->rb_left;
  285. else
  286. n = n->rb_right;
  287. }
  288. return NULL;
  289. }
  290. /*
  291. * Find a uprobe corresponding to a given inode:offset
  292. * Acquires uprobes_treelock
  293. */
  294. static struct uprobe *find_uprobe(struct inode *inode, loff_t offset)
  295. {
  296. struct uprobe *uprobe;
  297. spin_lock(&uprobes_treelock);
  298. uprobe = __find_uprobe(inode, offset);
  299. spin_unlock(&uprobes_treelock);
  300. return uprobe;
  301. }
  302. static struct uprobe *__insert_uprobe(struct uprobe *uprobe)
  303. {
  304. struct rb_node **p = &uprobes_tree.rb_node;
  305. struct rb_node *parent = NULL;
  306. struct uprobe *u;
  307. int match;
  308. while (*p) {
  309. parent = *p;
  310. u = rb_entry(parent, struct uprobe, rb_node);
  311. match = match_uprobe(uprobe, u);
  312. if (!match) {
  313. atomic_inc(&u->ref);
  314. return u;
  315. }
  316. if (match < 0)
  317. p = &parent->rb_left;
  318. else
  319. p = &parent->rb_right;
  320. }
  321. u = NULL;
  322. rb_link_node(&uprobe->rb_node, parent, p);
  323. rb_insert_color(&uprobe->rb_node, &uprobes_tree);
  324. /* get access + creation ref */
  325. atomic_set(&uprobe->ref, 2);
  326. return u;
  327. }
  328. /*
  329. * Acquire uprobes_treelock.
  330. * Matching uprobe already exists in rbtree;
  331. * increment (access refcount) and return the matching uprobe.
  332. *
  333. * No matching uprobe; insert the uprobe in rb_tree;
  334. * get a double refcount (access + creation) and return NULL.
  335. */
  336. static struct uprobe *insert_uprobe(struct uprobe *uprobe)
  337. {
  338. struct uprobe *u;
  339. spin_lock(&uprobes_treelock);
  340. u = __insert_uprobe(uprobe);
  341. spin_unlock(&uprobes_treelock);
  342. return u;
  343. }
  344. static void put_uprobe(struct uprobe *uprobe)
  345. {
  346. if (atomic_dec_and_test(&uprobe->ref))
  347. kfree(uprobe);
  348. }
  349. static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset)
  350. {
  351. struct uprobe *uprobe, *cur_uprobe;
  352. uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL);
  353. if (!uprobe)
  354. return NULL;
  355. uprobe->inode = igrab(inode);
  356. uprobe->offset = offset;
  357. init_rwsem(&uprobe->register_rwsem);
  358. init_rwsem(&uprobe->consumer_rwsem);
  359. /* For now assume that the instruction need not be single-stepped */
  360. __set_bit(UPROBE_SKIP_SSTEP, &uprobe->flags);
  361. /* add to uprobes_tree, sorted on inode:offset */
  362. cur_uprobe = insert_uprobe(uprobe);
  363. /* a uprobe exists for this inode:offset combination */
  364. if (cur_uprobe) {
  365. kfree(uprobe);
  366. uprobe = cur_uprobe;
  367. iput(inode);
  368. }
  369. return uprobe;
  370. }
  371. static void consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc)
  372. {
  373. down_write(&uprobe->consumer_rwsem);
  374. uc->next = uprobe->consumers;
  375. uprobe->consumers = uc;
  376. up_write(&uprobe->consumer_rwsem);
  377. }
  378. /*
  379. * For uprobe @uprobe, delete the consumer @uc.
  380. * Return true if the @uc is deleted successfully
  381. * or return false.
  382. */
  383. static bool consumer_del(struct uprobe *uprobe, struct uprobe_consumer *uc)
  384. {
  385. struct uprobe_consumer **con;
  386. bool ret = false;
  387. down_write(&uprobe->consumer_rwsem);
  388. for (con = &uprobe->consumers; *con; con = &(*con)->next) {
  389. if (*con == uc) {
  390. *con = uc->next;
  391. ret = true;
  392. break;
  393. }
  394. }
  395. up_write(&uprobe->consumer_rwsem);
  396. return ret;
  397. }
  398. static int
  399. __copy_insn(struct address_space *mapping, struct file *filp, char *insn,
  400. unsigned long nbytes, loff_t offset)
  401. {
  402. struct page *page;
  403. void *vaddr;
  404. unsigned long off;
  405. pgoff_t idx;
  406. if (!filp)
  407. return -EINVAL;
  408. if (!mapping->a_ops->readpage)
  409. return -EIO;
  410. idx = offset >> PAGE_CACHE_SHIFT;
  411. off = offset & ~PAGE_MASK;
  412. /*
  413. * Ensure that the page that has the original instruction is
  414. * populated and in page-cache.
  415. */
  416. page = read_mapping_page(mapping, idx, filp);
  417. if (IS_ERR(page))
  418. return PTR_ERR(page);
  419. vaddr = kmap_atomic(page);
  420. memcpy(insn, vaddr + off, nbytes);
  421. kunmap_atomic(vaddr);
  422. page_cache_release(page);
  423. return 0;
  424. }
  425. static int copy_insn(struct uprobe *uprobe, struct file *filp)
  426. {
  427. struct address_space *mapping;
  428. unsigned long nbytes;
  429. int bytes;
  430. nbytes = PAGE_SIZE - (uprobe->offset & ~PAGE_MASK);
  431. mapping = uprobe->inode->i_mapping;
  432. /* Instruction at end of binary; copy only available bytes */
  433. if (uprobe->offset + MAX_UINSN_BYTES > uprobe->inode->i_size)
  434. bytes = uprobe->inode->i_size - uprobe->offset;
  435. else
  436. bytes = MAX_UINSN_BYTES;
  437. /* Instruction at the page-boundary; copy bytes in second page */
  438. if (nbytes < bytes) {
  439. int err = __copy_insn(mapping, filp, uprobe->arch.insn + nbytes,
  440. bytes - nbytes, uprobe->offset + nbytes);
  441. if (err)
  442. return err;
  443. bytes = nbytes;
  444. }
  445. return __copy_insn(mapping, filp, uprobe->arch.insn, bytes, uprobe->offset);
  446. }
  447. static int prepare_uprobe(struct uprobe *uprobe, struct file *file,
  448. struct mm_struct *mm, unsigned long vaddr)
  449. {
  450. int ret = 0;
  451. if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
  452. return ret;
  453. /* TODO: move this into _register, until then we abuse this sem. */
  454. down_write(&uprobe->consumer_rwsem);
  455. if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
  456. goto out;
  457. ret = copy_insn(uprobe, file);
  458. if (ret)
  459. goto out;
  460. ret = -ENOTSUPP;
  461. if (is_swbp_insn((uprobe_opcode_t *)uprobe->arch.insn))
  462. goto out;
  463. ret = arch_uprobe_analyze_insn(&uprobe->arch, mm, vaddr);
  464. if (ret)
  465. goto out;
  466. /* write_opcode() assumes we don't cross page boundary */
  467. BUG_ON((uprobe->offset & ~PAGE_MASK) +
  468. UPROBE_SWBP_INSN_SIZE > PAGE_SIZE);
  469. smp_wmb(); /* pairs with rmb() in find_active_uprobe() */
  470. set_bit(UPROBE_COPY_INSN, &uprobe->flags);
  471. out:
  472. up_write(&uprobe->consumer_rwsem);
  473. return ret;
  474. }
  475. static inline bool consumer_filter(struct uprobe_consumer *uc,
  476. enum uprobe_filter_ctx ctx, struct mm_struct *mm)
  477. {
  478. return !uc->filter || uc->filter(uc, ctx, mm);
  479. }
  480. static bool filter_chain(struct uprobe *uprobe,
  481. enum uprobe_filter_ctx ctx, struct mm_struct *mm)
  482. {
  483. struct uprobe_consumer *uc;
  484. bool ret = false;
  485. down_read(&uprobe->consumer_rwsem);
  486. for (uc = uprobe->consumers; uc; uc = uc->next) {
  487. ret = consumer_filter(uc, ctx, mm);
  488. if (ret)
  489. break;
  490. }
  491. up_read(&uprobe->consumer_rwsem);
  492. return ret;
  493. }
  494. static int
  495. install_breakpoint(struct uprobe *uprobe, struct mm_struct *mm,
  496. struct vm_area_struct *vma, unsigned long vaddr)
  497. {
  498. bool first_uprobe;
  499. int ret;
  500. ret = prepare_uprobe(uprobe, vma->vm_file, mm, vaddr);
  501. if (ret)
  502. return ret;
  503. /*
  504. * set MMF_HAS_UPROBES in advance for uprobe_pre_sstep_notifier(),
  505. * the task can hit this breakpoint right after __replace_page().
  506. */
  507. first_uprobe = !test_bit(MMF_HAS_UPROBES, &mm->flags);
  508. if (first_uprobe)
  509. set_bit(MMF_HAS_UPROBES, &mm->flags);
  510. ret = set_swbp(&uprobe->arch, mm, vaddr);
  511. if (!ret)
  512. clear_bit(MMF_RECALC_UPROBES, &mm->flags);
  513. else if (first_uprobe)
  514. clear_bit(MMF_HAS_UPROBES, &mm->flags);
  515. return ret;
  516. }
  517. static int
  518. remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, unsigned long vaddr)
  519. {
  520. set_bit(MMF_RECALC_UPROBES, &mm->flags);
  521. return set_orig_insn(&uprobe->arch, mm, vaddr);
  522. }
  523. static inline bool uprobe_is_active(struct uprobe *uprobe)
  524. {
  525. return !RB_EMPTY_NODE(&uprobe->rb_node);
  526. }
  527. /*
  528. * There could be threads that have already hit the breakpoint. They
  529. * will recheck the current insn and restart if find_uprobe() fails.
  530. * See find_active_uprobe().
  531. */
  532. static void delete_uprobe(struct uprobe *uprobe)
  533. {
  534. if (WARN_ON(!uprobe_is_active(uprobe)))
  535. return;
  536. spin_lock(&uprobes_treelock);
  537. rb_erase(&uprobe->rb_node, &uprobes_tree);
  538. spin_unlock(&uprobes_treelock);
  539. RB_CLEAR_NODE(&uprobe->rb_node); /* for uprobe_is_active() */
  540. iput(uprobe->inode);
  541. put_uprobe(uprobe);
  542. }
  543. struct map_info {
  544. struct map_info *next;
  545. struct mm_struct *mm;
  546. unsigned long vaddr;
  547. };
  548. static inline struct map_info *free_map_info(struct map_info *info)
  549. {
  550. struct map_info *next = info->next;
  551. kfree(info);
  552. return next;
  553. }
  554. static struct map_info *
  555. build_map_info(struct address_space *mapping, loff_t offset, bool is_register)
  556. {
  557. unsigned long pgoff = offset >> PAGE_SHIFT;
  558. struct vm_area_struct *vma;
  559. struct map_info *curr = NULL;
  560. struct map_info *prev = NULL;
  561. struct map_info *info;
  562. int more = 0;
  563. again:
  564. mutex_lock(&mapping->i_mmap_mutex);
  565. vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
  566. if (!valid_vma(vma, is_register))
  567. continue;
  568. if (!prev && !more) {
  569. /*
  570. * Needs GFP_NOWAIT to avoid i_mmap_mutex recursion through
  571. * reclaim. This is optimistic, no harm done if it fails.
  572. */
  573. prev = kmalloc(sizeof(struct map_info),
  574. GFP_NOWAIT | __GFP_NOMEMALLOC | __GFP_NOWARN);
  575. if (prev)
  576. prev->next = NULL;
  577. }
  578. if (!prev) {
  579. more++;
  580. continue;
  581. }
  582. if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
  583. continue;
  584. info = prev;
  585. prev = prev->next;
  586. info->next = curr;
  587. curr = info;
  588. info->mm = vma->vm_mm;
  589. info->vaddr = offset_to_vaddr(vma, offset);
  590. }
  591. mutex_unlock(&mapping->i_mmap_mutex);
  592. if (!more)
  593. goto out;
  594. prev = curr;
  595. while (curr) {
  596. mmput(curr->mm);
  597. curr = curr->next;
  598. }
  599. do {
  600. info = kmalloc(sizeof(struct map_info), GFP_KERNEL);
  601. if (!info) {
  602. curr = ERR_PTR(-ENOMEM);
  603. goto out;
  604. }
  605. info->next = prev;
  606. prev = info;
  607. } while (--more);
  608. goto again;
  609. out:
  610. while (prev)
  611. prev = free_map_info(prev);
  612. return curr;
  613. }
  614. static int register_for_each_vma(struct uprobe *uprobe, bool is_register)
  615. {
  616. struct map_info *info;
  617. int err = 0;
  618. percpu_down_write(&dup_mmap_sem);
  619. info = build_map_info(uprobe->inode->i_mapping,
  620. uprobe->offset, is_register);
  621. if (IS_ERR(info)) {
  622. err = PTR_ERR(info);
  623. goto out;
  624. }
  625. while (info) {
  626. struct mm_struct *mm = info->mm;
  627. struct vm_area_struct *vma;
  628. if (err && is_register)
  629. goto free;
  630. down_write(&mm->mmap_sem);
  631. vma = find_vma(mm, info->vaddr);
  632. if (!vma || !valid_vma(vma, is_register) ||
  633. vma->vm_file->f_mapping->host != uprobe->inode)
  634. goto unlock;
  635. if (vma->vm_start > info->vaddr ||
  636. vaddr_to_offset(vma, info->vaddr) != uprobe->offset)
  637. goto unlock;
  638. if (is_register) {
  639. /* consult only the "caller", new consumer. */
  640. if (consumer_filter(uprobe->consumers,
  641. UPROBE_FILTER_REGISTER, mm))
  642. err = install_breakpoint(uprobe, mm, vma, info->vaddr);
  643. } else if (test_bit(MMF_HAS_UPROBES, &mm->flags)) {
  644. if (!filter_chain(uprobe,
  645. UPROBE_FILTER_UNREGISTER, mm))
  646. err |= remove_breakpoint(uprobe, mm, info->vaddr);
  647. }
  648. unlock:
  649. up_write(&mm->mmap_sem);
  650. free:
  651. mmput(mm);
  652. info = free_map_info(info);
  653. }
  654. out:
  655. percpu_up_write(&dup_mmap_sem);
  656. return err;
  657. }
  658. static int __uprobe_register(struct uprobe *uprobe, struct uprobe_consumer *uc)
  659. {
  660. consumer_add(uprobe, uc);
  661. return register_for_each_vma(uprobe, true);
  662. }
  663. static void __uprobe_unregister(struct uprobe *uprobe, struct uprobe_consumer *uc)
  664. {
  665. int err;
  666. if (!consumer_del(uprobe, uc)) /* WARN? */
  667. return;
  668. err = register_for_each_vma(uprobe, false);
  669. /* TODO : cant unregister? schedule a worker thread */
  670. if (!uprobe->consumers && !err)
  671. delete_uprobe(uprobe);
  672. }
  673. /*
  674. * uprobe_register - register a probe
  675. * @inode: the file in which the probe has to be placed.
  676. * @offset: offset from the start of the file.
  677. * @uc: information on howto handle the probe..
  678. *
  679. * Apart from the access refcount, uprobe_register() takes a creation
  680. * refcount (thro alloc_uprobe) if and only if this @uprobe is getting
  681. * inserted into the rbtree (i.e first consumer for a @inode:@offset
  682. * tuple). Creation refcount stops uprobe_unregister from freeing the
  683. * @uprobe even before the register operation is complete. Creation
  684. * refcount is released when the last @uc for the @uprobe
  685. * unregisters.
  686. *
  687. * Return errno if it cannot successully install probes
  688. * else return 0 (success)
  689. */
  690. int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
  691. {
  692. struct uprobe *uprobe;
  693. int ret;
  694. /* Racy, just to catch the obvious mistakes */
  695. if (offset > i_size_read(inode))
  696. return -EINVAL;
  697. retry:
  698. uprobe = alloc_uprobe(inode, offset);
  699. if (!uprobe)
  700. return -ENOMEM;
  701. /*
  702. * We can race with uprobe_unregister()->delete_uprobe().
  703. * Check uprobe_is_active() and retry if it is false.
  704. */
  705. down_write(&uprobe->register_rwsem);
  706. ret = -EAGAIN;
  707. if (likely(uprobe_is_active(uprobe))) {
  708. ret = __uprobe_register(uprobe, uc);
  709. if (ret)
  710. __uprobe_unregister(uprobe, uc);
  711. }
  712. up_write(&uprobe->register_rwsem);
  713. put_uprobe(uprobe);
  714. if (unlikely(ret == -EAGAIN))
  715. goto retry;
  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. * @uc: identify which probe if multiple probes are colocated.
  723. */
  724. void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
  725. {
  726. struct uprobe *uprobe;
  727. uprobe = find_uprobe(inode, offset);
  728. if (!uprobe)
  729. return;
  730. down_write(&uprobe->register_rwsem);
  731. __uprobe_unregister(uprobe, uc);
  732. up_write(&uprobe->register_rwsem);
  733. put_uprobe(uprobe);
  734. }
  735. static int unapply_uprobe(struct uprobe *uprobe, struct mm_struct *mm)
  736. {
  737. struct vm_area_struct *vma;
  738. int err = 0;
  739. down_read(&mm->mmap_sem);
  740. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  741. unsigned long vaddr;
  742. loff_t offset;
  743. if (!valid_vma(vma, false) ||
  744. vma->vm_file->f_mapping->host != uprobe->inode)
  745. continue;
  746. offset = (loff_t)vma->vm_pgoff << PAGE_SHIFT;
  747. if (uprobe->offset < offset ||
  748. uprobe->offset >= offset + vma->vm_end - vma->vm_start)
  749. continue;
  750. vaddr = offset_to_vaddr(vma, uprobe->offset);
  751. err |= remove_breakpoint(uprobe, mm, vaddr);
  752. }
  753. up_read(&mm->mmap_sem);
  754. return err;
  755. }
  756. static struct rb_node *
  757. find_node_in_range(struct inode *inode, loff_t min, loff_t max)
  758. {
  759. struct rb_node *n = uprobes_tree.rb_node;
  760. while (n) {
  761. struct uprobe *u = rb_entry(n, struct uprobe, rb_node);
  762. if (inode < u->inode) {
  763. n = n->rb_left;
  764. } else if (inode > u->inode) {
  765. n = n->rb_right;
  766. } else {
  767. if (max < u->offset)
  768. n = n->rb_left;
  769. else if (min > u->offset)
  770. n = n->rb_right;
  771. else
  772. break;
  773. }
  774. }
  775. return n;
  776. }
  777. /*
  778. * For a given range in vma, build a list of probes that need to be inserted.
  779. */
  780. static void build_probe_list(struct inode *inode,
  781. struct vm_area_struct *vma,
  782. unsigned long start, unsigned long end,
  783. struct list_head *head)
  784. {
  785. loff_t min, max;
  786. struct rb_node *n, *t;
  787. struct uprobe *u;
  788. INIT_LIST_HEAD(head);
  789. min = vaddr_to_offset(vma, start);
  790. max = min + (end - start) - 1;
  791. spin_lock(&uprobes_treelock);
  792. n = find_node_in_range(inode, min, max);
  793. if (n) {
  794. for (t = n; t; t = rb_prev(t)) {
  795. u = rb_entry(t, struct uprobe, rb_node);
  796. if (u->inode != inode || u->offset < min)
  797. break;
  798. list_add(&u->pending_list, head);
  799. atomic_inc(&u->ref);
  800. }
  801. for (t = n; (t = rb_next(t)); ) {
  802. u = rb_entry(t, struct uprobe, rb_node);
  803. if (u->inode != inode || u->offset > max)
  804. break;
  805. list_add(&u->pending_list, head);
  806. atomic_inc(&u->ref);
  807. }
  808. }
  809. spin_unlock(&uprobes_treelock);
  810. }
  811. /*
  812. * Called from mmap_region/vma_adjust with mm->mmap_sem acquired.
  813. *
  814. * Currently we ignore all errors and always return 0, the callers
  815. * can't handle the failure anyway.
  816. */
  817. int uprobe_mmap(struct vm_area_struct *vma)
  818. {
  819. struct list_head tmp_list;
  820. struct uprobe *uprobe, *u;
  821. struct inode *inode;
  822. if (no_uprobe_events() || !valid_vma(vma, true))
  823. return 0;
  824. inode = vma->vm_file->f_mapping->host;
  825. if (!inode)
  826. return 0;
  827. mutex_lock(uprobes_mmap_hash(inode));
  828. build_probe_list(inode, vma, vma->vm_start, vma->vm_end, &tmp_list);
  829. /*
  830. * We can race with uprobe_unregister(), this uprobe can be already
  831. * removed. But in this case filter_chain() must return false, all
  832. * consumers have gone away.
  833. */
  834. list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
  835. if (!fatal_signal_pending(current) &&
  836. filter_chain(uprobe, UPROBE_FILTER_MMAP, vma->vm_mm)) {
  837. unsigned long vaddr = offset_to_vaddr(vma, uprobe->offset);
  838. install_breakpoint(uprobe, vma->vm_mm, vma, vaddr);
  839. }
  840. put_uprobe(uprobe);
  841. }
  842. mutex_unlock(uprobes_mmap_hash(inode));
  843. return 0;
  844. }
  845. static bool
  846. vma_has_uprobes(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  847. {
  848. loff_t min, max;
  849. struct inode *inode;
  850. struct rb_node *n;
  851. inode = vma->vm_file->f_mapping->host;
  852. min = vaddr_to_offset(vma, start);
  853. max = min + (end - start) - 1;
  854. spin_lock(&uprobes_treelock);
  855. n = find_node_in_range(inode, min, max);
  856. spin_unlock(&uprobes_treelock);
  857. return !!n;
  858. }
  859. /*
  860. * Called in context of a munmap of a vma.
  861. */
  862. void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  863. {
  864. if (no_uprobe_events() || !valid_vma(vma, false))
  865. return;
  866. if (!atomic_read(&vma->vm_mm->mm_users)) /* called by mmput() ? */
  867. return;
  868. if (!test_bit(MMF_HAS_UPROBES, &vma->vm_mm->flags) ||
  869. test_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags))
  870. return;
  871. if (vma_has_uprobes(vma, start, end))
  872. set_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags);
  873. }
  874. /* Slot allocation for XOL */
  875. static int xol_add_vma(struct xol_area *area)
  876. {
  877. struct mm_struct *mm;
  878. int ret;
  879. area->page = alloc_page(GFP_HIGHUSER);
  880. if (!area->page)
  881. return -ENOMEM;
  882. ret = -EALREADY;
  883. mm = current->mm;
  884. down_write(&mm->mmap_sem);
  885. if (mm->uprobes_state.xol_area)
  886. goto fail;
  887. ret = -ENOMEM;
  888. /* Try to map as high as possible, this is only a hint. */
  889. area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE, PAGE_SIZE, 0, 0);
  890. if (area->vaddr & ~PAGE_MASK) {
  891. ret = area->vaddr;
  892. goto fail;
  893. }
  894. ret = install_special_mapping(mm, area->vaddr, PAGE_SIZE,
  895. VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO, &area->page);
  896. if (ret)
  897. goto fail;
  898. smp_wmb(); /* pairs with get_xol_area() */
  899. mm->uprobes_state.xol_area = area;
  900. ret = 0;
  901. fail:
  902. up_write(&mm->mmap_sem);
  903. if (ret)
  904. __free_page(area->page);
  905. return ret;
  906. }
  907. static struct xol_area *get_xol_area(struct mm_struct *mm)
  908. {
  909. struct xol_area *area;
  910. area = mm->uprobes_state.xol_area;
  911. smp_read_barrier_depends(); /* pairs with wmb in xol_add_vma() */
  912. return area;
  913. }
  914. /*
  915. * xol_alloc_area - Allocate process's xol_area.
  916. * This area will be used for storing instructions for execution out of
  917. * line.
  918. *
  919. * Returns the allocated area or NULL.
  920. */
  921. static struct xol_area *xol_alloc_area(void)
  922. {
  923. struct xol_area *area;
  924. area = kzalloc(sizeof(*area), GFP_KERNEL);
  925. if (unlikely(!area))
  926. return NULL;
  927. area->bitmap = kzalloc(BITS_TO_LONGS(UINSNS_PER_PAGE) * sizeof(long), GFP_KERNEL);
  928. if (!area->bitmap)
  929. goto fail;
  930. init_waitqueue_head(&area->wq);
  931. if (!xol_add_vma(area))
  932. return area;
  933. fail:
  934. kfree(area->bitmap);
  935. kfree(area);
  936. return get_xol_area(current->mm);
  937. }
  938. /*
  939. * uprobe_clear_state - Free the area allocated for slots.
  940. */
  941. void uprobe_clear_state(struct mm_struct *mm)
  942. {
  943. struct xol_area *area = mm->uprobes_state.xol_area;
  944. if (!area)
  945. return;
  946. put_page(area->page);
  947. kfree(area->bitmap);
  948. kfree(area);
  949. }
  950. void uprobe_start_dup_mmap(void)
  951. {
  952. percpu_down_read(&dup_mmap_sem);
  953. }
  954. void uprobe_end_dup_mmap(void)
  955. {
  956. percpu_up_read(&dup_mmap_sem);
  957. }
  958. void uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm)
  959. {
  960. newmm->uprobes_state.xol_area = NULL;
  961. if (test_bit(MMF_HAS_UPROBES, &oldmm->flags)) {
  962. set_bit(MMF_HAS_UPROBES, &newmm->flags);
  963. /* unconditionally, dup_mmap() skips VM_DONTCOPY vmas */
  964. set_bit(MMF_RECALC_UPROBES, &newmm->flags);
  965. }
  966. }
  967. /*
  968. * - search for a free slot.
  969. */
  970. static unsigned long xol_take_insn_slot(struct xol_area *area)
  971. {
  972. unsigned long slot_addr;
  973. int slot_nr;
  974. do {
  975. slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE);
  976. if (slot_nr < UINSNS_PER_PAGE) {
  977. if (!test_and_set_bit(slot_nr, area->bitmap))
  978. break;
  979. slot_nr = UINSNS_PER_PAGE;
  980. continue;
  981. }
  982. wait_event(area->wq, (atomic_read(&area->slot_count) < UINSNS_PER_PAGE));
  983. } while (slot_nr >= UINSNS_PER_PAGE);
  984. slot_addr = area->vaddr + (slot_nr * UPROBE_XOL_SLOT_BYTES);
  985. atomic_inc(&area->slot_count);
  986. return slot_addr;
  987. }
  988. /*
  989. * xol_get_insn_slot - If was not allocated a slot, then
  990. * allocate a slot.
  991. * Returns the allocated slot address or 0.
  992. */
  993. static unsigned long xol_get_insn_slot(struct uprobe *uprobe, unsigned long slot_addr)
  994. {
  995. struct xol_area *area;
  996. unsigned long offset;
  997. void *vaddr;
  998. area = get_xol_area(current->mm);
  999. if (!area) {
  1000. area = xol_alloc_area();
  1001. if (!area)
  1002. return 0;
  1003. }
  1004. current->utask->xol_vaddr = xol_take_insn_slot(area);
  1005. /*
  1006. * Initialize the slot if xol_vaddr points to valid
  1007. * instruction slot.
  1008. */
  1009. if (unlikely(!current->utask->xol_vaddr))
  1010. return 0;
  1011. current->utask->vaddr = slot_addr;
  1012. offset = current->utask->xol_vaddr & ~PAGE_MASK;
  1013. vaddr = kmap_atomic(area->page);
  1014. memcpy(vaddr + offset, uprobe->arch.insn, MAX_UINSN_BYTES);
  1015. kunmap_atomic(vaddr);
  1016. /*
  1017. * We probably need flush_icache_user_range() but it needs vma.
  1018. * This should work on supported architectures too.
  1019. */
  1020. flush_dcache_page(area->page);
  1021. return current->utask->xol_vaddr;
  1022. }
  1023. /*
  1024. * xol_free_insn_slot - If slot was earlier allocated by
  1025. * @xol_get_insn_slot(), make the slot available for
  1026. * subsequent requests.
  1027. */
  1028. static void xol_free_insn_slot(struct task_struct *tsk)
  1029. {
  1030. struct xol_area *area;
  1031. unsigned long vma_end;
  1032. unsigned long slot_addr;
  1033. if (!tsk->mm || !tsk->mm->uprobes_state.xol_area || !tsk->utask)
  1034. return;
  1035. slot_addr = tsk->utask->xol_vaddr;
  1036. if (unlikely(!slot_addr || IS_ERR_VALUE(slot_addr)))
  1037. return;
  1038. area = tsk->mm->uprobes_state.xol_area;
  1039. vma_end = area->vaddr + PAGE_SIZE;
  1040. if (area->vaddr <= slot_addr && slot_addr < vma_end) {
  1041. unsigned long offset;
  1042. int slot_nr;
  1043. offset = slot_addr - area->vaddr;
  1044. slot_nr = offset / UPROBE_XOL_SLOT_BYTES;
  1045. if (slot_nr >= UINSNS_PER_PAGE)
  1046. return;
  1047. clear_bit(slot_nr, area->bitmap);
  1048. atomic_dec(&area->slot_count);
  1049. if (waitqueue_active(&area->wq))
  1050. wake_up(&area->wq);
  1051. tsk->utask->xol_vaddr = 0;
  1052. }
  1053. }
  1054. /**
  1055. * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
  1056. * @regs: Reflects the saved state of the task after it has hit a breakpoint
  1057. * instruction.
  1058. * Return the address of the breakpoint instruction.
  1059. */
  1060. unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs)
  1061. {
  1062. return instruction_pointer(regs) - UPROBE_SWBP_INSN_SIZE;
  1063. }
  1064. /*
  1065. * Called with no locks held.
  1066. * Called in context of a exiting or a exec-ing thread.
  1067. */
  1068. void uprobe_free_utask(struct task_struct *t)
  1069. {
  1070. struct uprobe_task *utask = t->utask;
  1071. if (!utask)
  1072. return;
  1073. if (utask->active_uprobe)
  1074. put_uprobe(utask->active_uprobe);
  1075. xol_free_insn_slot(t);
  1076. kfree(utask);
  1077. t->utask = NULL;
  1078. }
  1079. /*
  1080. * Called in context of a new clone/fork from copy_process.
  1081. */
  1082. void uprobe_copy_process(struct task_struct *t)
  1083. {
  1084. t->utask = NULL;
  1085. }
  1086. /*
  1087. * Allocate a uprobe_task object for the task.
  1088. * Called when the thread hits a breakpoint for the first time.
  1089. *
  1090. * Returns:
  1091. * - pointer to new uprobe_task on success
  1092. * - NULL otherwise
  1093. */
  1094. static struct uprobe_task *add_utask(void)
  1095. {
  1096. struct uprobe_task *utask;
  1097. utask = kzalloc(sizeof *utask, GFP_KERNEL);
  1098. if (unlikely(!utask))
  1099. return NULL;
  1100. current->utask = utask;
  1101. return utask;
  1102. }
  1103. /* Prepare to single-step probed instruction out of line. */
  1104. static int
  1105. pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long vaddr)
  1106. {
  1107. if (xol_get_insn_slot(uprobe, vaddr) && !arch_uprobe_pre_xol(&uprobe->arch, regs))
  1108. return 0;
  1109. return -EFAULT;
  1110. }
  1111. /*
  1112. * If we are singlestepping, then ensure this thread is not connected to
  1113. * non-fatal signals until completion of singlestep. When xol insn itself
  1114. * triggers the signal, restart the original insn even if the task is
  1115. * already SIGKILL'ed (since coredump should report the correct ip). This
  1116. * is even more important if the task has a handler for SIGSEGV/etc, The
  1117. * _same_ instruction should be repeated again after return from the signal
  1118. * handler, and SSTEP can never finish in this case.
  1119. */
  1120. bool uprobe_deny_signal(void)
  1121. {
  1122. struct task_struct *t = current;
  1123. struct uprobe_task *utask = t->utask;
  1124. if (likely(!utask || !utask->active_uprobe))
  1125. return false;
  1126. WARN_ON_ONCE(utask->state != UTASK_SSTEP);
  1127. if (signal_pending(t)) {
  1128. spin_lock_irq(&t->sighand->siglock);
  1129. clear_tsk_thread_flag(t, TIF_SIGPENDING);
  1130. spin_unlock_irq(&t->sighand->siglock);
  1131. if (__fatal_signal_pending(t) || arch_uprobe_xol_was_trapped(t)) {
  1132. utask->state = UTASK_SSTEP_TRAPPED;
  1133. set_tsk_thread_flag(t, TIF_UPROBE);
  1134. set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
  1135. }
  1136. }
  1137. return true;
  1138. }
  1139. /*
  1140. * Avoid singlestepping the original instruction if the original instruction
  1141. * is a NOP or can be emulated.
  1142. */
  1143. static bool can_skip_sstep(struct uprobe *uprobe, struct pt_regs *regs)
  1144. {
  1145. if (test_bit(UPROBE_SKIP_SSTEP, &uprobe->flags)) {
  1146. if (arch_uprobe_skip_sstep(&uprobe->arch, regs))
  1147. return true;
  1148. clear_bit(UPROBE_SKIP_SSTEP, &uprobe->flags);
  1149. }
  1150. return false;
  1151. }
  1152. static void mmf_recalc_uprobes(struct mm_struct *mm)
  1153. {
  1154. struct vm_area_struct *vma;
  1155. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  1156. if (!valid_vma(vma, false))
  1157. continue;
  1158. /*
  1159. * This is not strictly accurate, we can race with
  1160. * uprobe_unregister() and see the already removed
  1161. * uprobe if delete_uprobe() was not yet called.
  1162. * Or this uprobe can be filtered out.
  1163. */
  1164. if (vma_has_uprobes(vma, vma->vm_start, vma->vm_end))
  1165. return;
  1166. }
  1167. clear_bit(MMF_HAS_UPROBES, &mm->flags);
  1168. }
  1169. static int is_swbp_at_addr(struct mm_struct *mm, unsigned long vaddr)
  1170. {
  1171. struct page *page;
  1172. uprobe_opcode_t opcode;
  1173. int result;
  1174. pagefault_disable();
  1175. result = __copy_from_user_inatomic(&opcode, (void __user*)vaddr,
  1176. sizeof(opcode));
  1177. pagefault_enable();
  1178. if (likely(result == 0))
  1179. goto out;
  1180. result = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &page, NULL);
  1181. if (result < 0)
  1182. return result;
  1183. copy_opcode(page, vaddr, &opcode);
  1184. put_page(page);
  1185. out:
  1186. return is_swbp_insn(&opcode);
  1187. }
  1188. static struct uprobe *find_active_uprobe(unsigned long bp_vaddr, int *is_swbp)
  1189. {
  1190. struct mm_struct *mm = current->mm;
  1191. struct uprobe *uprobe = NULL;
  1192. struct vm_area_struct *vma;
  1193. down_read(&mm->mmap_sem);
  1194. vma = find_vma(mm, bp_vaddr);
  1195. if (vma && vma->vm_start <= bp_vaddr) {
  1196. if (valid_vma(vma, false)) {
  1197. struct inode *inode = vma->vm_file->f_mapping->host;
  1198. loff_t offset = vaddr_to_offset(vma, bp_vaddr);
  1199. uprobe = find_uprobe(inode, offset);
  1200. }
  1201. if (!uprobe)
  1202. *is_swbp = is_swbp_at_addr(mm, bp_vaddr);
  1203. } else {
  1204. *is_swbp = -EFAULT;
  1205. }
  1206. if (!uprobe && test_and_clear_bit(MMF_RECALC_UPROBES, &mm->flags))
  1207. mmf_recalc_uprobes(mm);
  1208. up_read(&mm->mmap_sem);
  1209. return uprobe;
  1210. }
  1211. static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
  1212. {
  1213. struct uprobe_consumer *uc;
  1214. int remove = UPROBE_HANDLER_REMOVE;
  1215. down_read(&uprobe->register_rwsem);
  1216. for (uc = uprobe->consumers; uc; uc = uc->next) {
  1217. int rc = uc->handler(uc, regs);
  1218. WARN(rc & ~UPROBE_HANDLER_MASK,
  1219. "bad rc=0x%x from %pf()\n", rc, uc->handler);
  1220. remove &= rc;
  1221. }
  1222. if (remove && uprobe->consumers) {
  1223. WARN_ON(!uprobe_is_active(uprobe));
  1224. unapply_uprobe(uprobe, current->mm);
  1225. }
  1226. up_read(&uprobe->register_rwsem);
  1227. }
  1228. /*
  1229. * Run handler and ask thread to singlestep.
  1230. * Ensure all non-fatal signals cannot interrupt thread while it singlesteps.
  1231. */
  1232. static void handle_swbp(struct pt_regs *regs)
  1233. {
  1234. struct uprobe_task *utask;
  1235. struct uprobe *uprobe;
  1236. unsigned long bp_vaddr;
  1237. int uninitialized_var(is_swbp);
  1238. bp_vaddr = uprobe_get_swbp_addr(regs);
  1239. uprobe = find_active_uprobe(bp_vaddr, &is_swbp);
  1240. if (!uprobe) {
  1241. if (is_swbp > 0) {
  1242. /* No matching uprobe; signal SIGTRAP. */
  1243. send_sig(SIGTRAP, current, 0);
  1244. } else {
  1245. /*
  1246. * Either we raced with uprobe_unregister() or we can't
  1247. * access this memory. The latter is only possible if
  1248. * another thread plays with our ->mm. In both cases
  1249. * we can simply restart. If this vma was unmapped we
  1250. * can pretend this insn was not executed yet and get
  1251. * the (correct) SIGSEGV after restart.
  1252. */
  1253. instruction_pointer_set(regs, bp_vaddr);
  1254. }
  1255. return;
  1256. }
  1257. /* change it in advance for ->handler() and restart */
  1258. instruction_pointer_set(regs, bp_vaddr);
  1259. /*
  1260. * TODO: move copy_insn/etc into _register and remove this hack.
  1261. * After we hit the bp, _unregister + _register can install the
  1262. * new and not-yet-analyzed uprobe at the same address, restart.
  1263. */
  1264. smp_rmb(); /* pairs with wmb() in install_breakpoint() */
  1265. if (unlikely(!test_bit(UPROBE_COPY_INSN, &uprobe->flags)))
  1266. goto out;
  1267. utask = current->utask;
  1268. if (!utask) {
  1269. utask = add_utask();
  1270. /* Cannot allocate; re-execute the instruction. */
  1271. if (!utask)
  1272. goto out;
  1273. }
  1274. handler_chain(uprobe, regs);
  1275. if (can_skip_sstep(uprobe, regs))
  1276. goto out;
  1277. if (!pre_ssout(uprobe, regs, bp_vaddr)) {
  1278. utask->active_uprobe = uprobe;
  1279. utask->state = UTASK_SSTEP;
  1280. return;
  1281. }
  1282. /* can_skip_sstep() succeeded, or restart if can't singlestep */
  1283. out:
  1284. put_uprobe(uprobe);
  1285. }
  1286. /*
  1287. * Perform required fix-ups and disable singlestep.
  1288. * Allow pending signals to take effect.
  1289. */
  1290. static void handle_singlestep(struct uprobe_task *utask, struct pt_regs *regs)
  1291. {
  1292. struct uprobe *uprobe;
  1293. uprobe = utask->active_uprobe;
  1294. if (utask->state == UTASK_SSTEP_ACK)
  1295. arch_uprobe_post_xol(&uprobe->arch, regs);
  1296. else if (utask->state == UTASK_SSTEP_TRAPPED)
  1297. arch_uprobe_abort_xol(&uprobe->arch, regs);
  1298. else
  1299. WARN_ON_ONCE(1);
  1300. put_uprobe(uprobe);
  1301. utask->active_uprobe = NULL;
  1302. utask->state = UTASK_RUNNING;
  1303. xol_free_insn_slot(current);
  1304. spin_lock_irq(&current->sighand->siglock);
  1305. recalc_sigpending(); /* see uprobe_deny_signal() */
  1306. spin_unlock_irq(&current->sighand->siglock);
  1307. }
  1308. /*
  1309. * On breakpoint hit, breakpoint notifier sets the TIF_UPROBE flag and
  1310. * allows the thread to return from interrupt. After that handle_swbp()
  1311. * sets utask->active_uprobe.
  1312. *
  1313. * On singlestep exception, singlestep notifier sets the TIF_UPROBE flag
  1314. * and allows the thread to return from interrupt.
  1315. *
  1316. * While returning to userspace, thread notices the TIF_UPROBE flag and calls
  1317. * uprobe_notify_resume().
  1318. */
  1319. void uprobe_notify_resume(struct pt_regs *regs)
  1320. {
  1321. struct uprobe_task *utask;
  1322. clear_thread_flag(TIF_UPROBE);
  1323. utask = current->utask;
  1324. if (utask && utask->active_uprobe)
  1325. handle_singlestep(utask, regs);
  1326. else
  1327. handle_swbp(regs);
  1328. }
  1329. /*
  1330. * uprobe_pre_sstep_notifier gets called from interrupt context as part of
  1331. * notifier mechanism. Set TIF_UPROBE flag and indicate breakpoint hit.
  1332. */
  1333. int uprobe_pre_sstep_notifier(struct pt_regs *regs)
  1334. {
  1335. if (!current->mm || !test_bit(MMF_HAS_UPROBES, &current->mm->flags))
  1336. return 0;
  1337. set_thread_flag(TIF_UPROBE);
  1338. return 1;
  1339. }
  1340. /*
  1341. * uprobe_post_sstep_notifier gets called in interrupt context as part of notifier
  1342. * mechanism. Set TIF_UPROBE flag and indicate completion of singlestep.
  1343. */
  1344. int uprobe_post_sstep_notifier(struct pt_regs *regs)
  1345. {
  1346. struct uprobe_task *utask = current->utask;
  1347. if (!current->mm || !utask || !utask->active_uprobe)
  1348. /* task is currently not uprobed */
  1349. return 0;
  1350. utask->state = UTASK_SSTEP_ACK;
  1351. set_thread_flag(TIF_UPROBE);
  1352. return 1;
  1353. }
  1354. static struct notifier_block uprobe_exception_nb = {
  1355. .notifier_call = arch_uprobe_exception_notify,
  1356. .priority = INT_MAX-1, /* notified after kprobes, kgdb */
  1357. };
  1358. static int __init init_uprobes(void)
  1359. {
  1360. int i;
  1361. for (i = 0; i < UPROBES_HASH_SZ; i++)
  1362. mutex_init(&uprobes_mmap_mutex[i]);
  1363. if (percpu_init_rwsem(&dup_mmap_sem))
  1364. return -ENOMEM;
  1365. return register_die_notifier(&uprobe_exception_nb);
  1366. }
  1367. module_init(init_uprobes);
  1368. static void __exit exit_uprobes(void)
  1369. {
  1370. }
  1371. module_exit(exit_uprobes);