uprobes.c 39 KB

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