uprobes.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627
  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. /* For now assume that the instruction need not be single-stepped */
  365. __set_bit(UPROBE_SKIP_SSTEP, &uprobe->flags);
  366. return u;
  367. }
  368. static void put_uprobe(struct uprobe *uprobe)
  369. {
  370. if (atomic_dec_and_test(&uprobe->ref))
  371. kfree(uprobe);
  372. }
  373. static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset)
  374. {
  375. struct uprobe *uprobe, *cur_uprobe;
  376. uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL);
  377. if (!uprobe)
  378. return NULL;
  379. uprobe->inode = igrab(inode);
  380. uprobe->offset = offset;
  381. init_rwsem(&uprobe->consumer_rwsem);
  382. mutex_init(&uprobe->copy_mutex);
  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. if (!uc->filter || uc->filter(uc, current))
  403. uc->handler(uc, regs);
  404. }
  405. up_read(&uprobe->consumer_rwsem);
  406. }
  407. /* Returns the previous consumer */
  408. static struct uprobe_consumer *
  409. consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc)
  410. {
  411. down_write(&uprobe->consumer_rwsem);
  412. uc->next = uprobe->consumers;
  413. uprobe->consumers = uc;
  414. up_write(&uprobe->consumer_rwsem);
  415. return uc->next;
  416. }
  417. /*
  418. * For uprobe @uprobe, delete the consumer @uc.
  419. * Return true if the @uc is deleted successfully
  420. * or return false.
  421. */
  422. static bool consumer_del(struct uprobe *uprobe, struct uprobe_consumer *uc)
  423. {
  424. struct uprobe_consumer **con;
  425. bool ret = false;
  426. down_write(&uprobe->consumer_rwsem);
  427. for (con = &uprobe->consumers; *con; con = &(*con)->next) {
  428. if (*con == uc) {
  429. *con = uc->next;
  430. ret = true;
  431. break;
  432. }
  433. }
  434. up_write(&uprobe->consumer_rwsem);
  435. return ret;
  436. }
  437. static int
  438. __copy_insn(struct address_space *mapping, struct file *filp, char *insn,
  439. unsigned long nbytes, loff_t offset)
  440. {
  441. struct page *page;
  442. void *vaddr;
  443. unsigned long off;
  444. pgoff_t idx;
  445. if (!filp)
  446. return -EINVAL;
  447. if (!mapping->a_ops->readpage)
  448. return -EIO;
  449. idx = offset >> PAGE_CACHE_SHIFT;
  450. off = offset & ~PAGE_MASK;
  451. /*
  452. * Ensure that the page that has the original instruction is
  453. * populated and in page-cache.
  454. */
  455. page = read_mapping_page(mapping, idx, filp);
  456. if (IS_ERR(page))
  457. return PTR_ERR(page);
  458. vaddr = kmap_atomic(page);
  459. memcpy(insn, vaddr + off, nbytes);
  460. kunmap_atomic(vaddr);
  461. page_cache_release(page);
  462. return 0;
  463. }
  464. static int copy_insn(struct uprobe *uprobe, struct file *filp)
  465. {
  466. struct address_space *mapping;
  467. unsigned long nbytes;
  468. int bytes;
  469. nbytes = PAGE_SIZE - (uprobe->offset & ~PAGE_MASK);
  470. mapping = uprobe->inode->i_mapping;
  471. /* Instruction at end of binary; copy only available bytes */
  472. if (uprobe->offset + MAX_UINSN_BYTES > uprobe->inode->i_size)
  473. bytes = uprobe->inode->i_size - uprobe->offset;
  474. else
  475. bytes = MAX_UINSN_BYTES;
  476. /* Instruction at the page-boundary; copy bytes in second page */
  477. if (nbytes < bytes) {
  478. int err = __copy_insn(mapping, filp, uprobe->arch.insn + nbytes,
  479. bytes - nbytes, uprobe->offset + nbytes);
  480. if (err)
  481. return err;
  482. bytes = nbytes;
  483. }
  484. return __copy_insn(mapping, filp, uprobe->arch.insn, bytes, uprobe->offset);
  485. }
  486. static int prepare_uprobe(struct uprobe *uprobe, struct file *file,
  487. struct mm_struct *mm, unsigned long vaddr)
  488. {
  489. int ret = 0;
  490. if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
  491. return ret;
  492. mutex_lock(&uprobe->copy_mutex);
  493. if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
  494. goto out;
  495. ret = copy_insn(uprobe, file);
  496. if (ret)
  497. goto out;
  498. ret = -ENOTSUPP;
  499. if (is_swbp_insn((uprobe_opcode_t *)uprobe->arch.insn))
  500. goto out;
  501. ret = arch_uprobe_analyze_insn(&uprobe->arch, mm, vaddr);
  502. if (ret)
  503. goto out;
  504. /* write_opcode() assumes we don't cross page boundary */
  505. BUG_ON((uprobe->offset & ~PAGE_MASK) +
  506. UPROBE_SWBP_INSN_SIZE > PAGE_SIZE);
  507. smp_wmb(); /* pairs with rmb() in find_active_uprobe() */
  508. set_bit(UPROBE_COPY_INSN, &uprobe->flags);
  509. out:
  510. mutex_unlock(&uprobe->copy_mutex);
  511. return ret;
  512. }
  513. static int
  514. install_breakpoint(struct uprobe *uprobe, struct mm_struct *mm,
  515. struct vm_area_struct *vma, unsigned long vaddr)
  516. {
  517. bool first_uprobe;
  518. int ret;
  519. /*
  520. * If probe is being deleted, unregister thread could be done with
  521. * the vma-rmap-walk through. Adding a probe now can be fatal since
  522. * nobody will be able to cleanup. Also we could be from fork or
  523. * mremap path, where the probe might have already been inserted.
  524. * Hence behave as if probe already existed.
  525. */
  526. if (!uprobe->consumers)
  527. return 0;
  528. ret = prepare_uprobe(uprobe, vma->vm_file, mm, vaddr);
  529. if (ret)
  530. return ret;
  531. /*
  532. * set MMF_HAS_UPROBES in advance for uprobe_pre_sstep_notifier(),
  533. * the task can hit this breakpoint right after __replace_page().
  534. */
  535. first_uprobe = !test_bit(MMF_HAS_UPROBES, &mm->flags);
  536. if (first_uprobe)
  537. set_bit(MMF_HAS_UPROBES, &mm->flags);
  538. ret = set_swbp(&uprobe->arch, mm, vaddr);
  539. if (!ret)
  540. clear_bit(MMF_RECALC_UPROBES, &mm->flags);
  541. else if (first_uprobe)
  542. clear_bit(MMF_HAS_UPROBES, &mm->flags);
  543. return ret;
  544. }
  545. static int
  546. remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, unsigned long vaddr)
  547. {
  548. /* can happen if uprobe_register() fails */
  549. if (!test_bit(MMF_HAS_UPROBES, &mm->flags))
  550. return 0;
  551. set_bit(MMF_RECALC_UPROBES, &mm->flags);
  552. return set_orig_insn(&uprobe->arch, mm, vaddr);
  553. }
  554. /*
  555. * There could be threads that have already hit the breakpoint. They
  556. * will recheck the current insn and restart if find_uprobe() fails.
  557. * See find_active_uprobe().
  558. */
  559. static void delete_uprobe(struct uprobe *uprobe)
  560. {
  561. spin_lock(&uprobes_treelock);
  562. rb_erase(&uprobe->rb_node, &uprobes_tree);
  563. spin_unlock(&uprobes_treelock);
  564. iput(uprobe->inode);
  565. put_uprobe(uprobe);
  566. atomic_dec(&uprobe_events);
  567. }
  568. struct map_info {
  569. struct map_info *next;
  570. struct mm_struct *mm;
  571. unsigned long vaddr;
  572. };
  573. static inline struct map_info *free_map_info(struct map_info *info)
  574. {
  575. struct map_info *next = info->next;
  576. kfree(info);
  577. return next;
  578. }
  579. static struct map_info *
  580. build_map_info(struct address_space *mapping, loff_t offset, bool is_register)
  581. {
  582. unsigned long pgoff = offset >> PAGE_SHIFT;
  583. struct vm_area_struct *vma;
  584. struct map_info *curr = NULL;
  585. struct map_info *prev = NULL;
  586. struct map_info *info;
  587. int more = 0;
  588. again:
  589. mutex_lock(&mapping->i_mmap_mutex);
  590. vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
  591. if (!valid_vma(vma, is_register))
  592. continue;
  593. if (!prev && !more) {
  594. /*
  595. * Needs GFP_NOWAIT to avoid i_mmap_mutex recursion through
  596. * reclaim. This is optimistic, no harm done if it fails.
  597. */
  598. prev = kmalloc(sizeof(struct map_info),
  599. GFP_NOWAIT | __GFP_NOMEMALLOC | __GFP_NOWARN);
  600. if (prev)
  601. prev->next = NULL;
  602. }
  603. if (!prev) {
  604. more++;
  605. continue;
  606. }
  607. if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
  608. continue;
  609. info = prev;
  610. prev = prev->next;
  611. info->next = curr;
  612. curr = info;
  613. info->mm = vma->vm_mm;
  614. info->vaddr = offset_to_vaddr(vma, offset);
  615. }
  616. mutex_unlock(&mapping->i_mmap_mutex);
  617. if (!more)
  618. goto out;
  619. prev = curr;
  620. while (curr) {
  621. mmput(curr->mm);
  622. curr = curr->next;
  623. }
  624. do {
  625. info = kmalloc(sizeof(struct map_info), GFP_KERNEL);
  626. if (!info) {
  627. curr = ERR_PTR(-ENOMEM);
  628. goto out;
  629. }
  630. info->next = prev;
  631. prev = info;
  632. } while (--more);
  633. goto again;
  634. out:
  635. while (prev)
  636. prev = free_map_info(prev);
  637. return curr;
  638. }
  639. static int register_for_each_vma(struct uprobe *uprobe, bool is_register)
  640. {
  641. struct map_info *info;
  642. int err = 0;
  643. percpu_down_write(&dup_mmap_sem);
  644. info = build_map_info(uprobe->inode->i_mapping,
  645. uprobe->offset, is_register);
  646. if (IS_ERR(info)) {
  647. err = PTR_ERR(info);
  648. goto out;
  649. }
  650. while (info) {
  651. struct mm_struct *mm = info->mm;
  652. struct vm_area_struct *vma;
  653. if (err && is_register)
  654. goto free;
  655. down_write(&mm->mmap_sem);
  656. vma = find_vma(mm, info->vaddr);
  657. if (!vma || !valid_vma(vma, is_register) ||
  658. vma->vm_file->f_mapping->host != uprobe->inode)
  659. goto unlock;
  660. if (vma->vm_start > info->vaddr ||
  661. vaddr_to_offset(vma, info->vaddr) != uprobe->offset)
  662. goto unlock;
  663. if (is_register)
  664. err = install_breakpoint(uprobe, mm, vma, info->vaddr);
  665. else
  666. err |= remove_breakpoint(uprobe, mm, info->vaddr);
  667. unlock:
  668. up_write(&mm->mmap_sem);
  669. free:
  670. mmput(mm);
  671. info = free_map_info(info);
  672. }
  673. out:
  674. percpu_up_write(&dup_mmap_sem);
  675. return err;
  676. }
  677. static int __uprobe_register(struct uprobe *uprobe)
  678. {
  679. return register_for_each_vma(uprobe, true);
  680. }
  681. static void __uprobe_unregister(struct uprobe *uprobe)
  682. {
  683. if (!register_for_each_vma(uprobe, false))
  684. delete_uprobe(uprobe);
  685. /* TODO : cant unregister? schedule a worker thread */
  686. }
  687. /*
  688. * uprobe_register - register a probe
  689. * @inode: the file in which the probe has to be placed.
  690. * @offset: offset from the start of the file.
  691. * @uc: information on howto handle the probe..
  692. *
  693. * Apart from the access refcount, uprobe_register() takes a creation
  694. * refcount (thro alloc_uprobe) if and only if this @uprobe is getting
  695. * inserted into the rbtree (i.e first consumer for a @inode:@offset
  696. * tuple). Creation refcount stops uprobe_unregister from freeing the
  697. * @uprobe even before the register operation is complete. Creation
  698. * refcount is released when the last @uc for the @uprobe
  699. * unregisters.
  700. *
  701. * Return errno if it cannot successully install probes
  702. * else return 0 (success)
  703. */
  704. int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
  705. {
  706. struct uprobe *uprobe;
  707. int ret;
  708. if (!inode || !uc || uc->next)
  709. return -EINVAL;
  710. if (offset > i_size_read(inode))
  711. return -EINVAL;
  712. ret = 0;
  713. mutex_lock(uprobes_hash(inode));
  714. uprobe = alloc_uprobe(inode, offset);
  715. if (!uprobe) {
  716. ret = -ENOMEM;
  717. } else if (!consumer_add(uprobe, uc)) {
  718. ret = __uprobe_register(uprobe);
  719. if (ret) {
  720. uprobe->consumers = NULL;
  721. __uprobe_unregister(uprobe);
  722. } else {
  723. set_bit(UPROBE_RUN_HANDLER, &uprobe->flags);
  724. }
  725. }
  726. mutex_unlock(uprobes_hash(inode));
  727. if (uprobe)
  728. put_uprobe(uprobe);
  729. return ret;
  730. }
  731. /*
  732. * uprobe_unregister - unregister a already registered probe.
  733. * @inode: the file in which the probe has to be removed.
  734. * @offset: offset from the start of the file.
  735. * @uc: identify which probe if multiple probes are colocated.
  736. */
  737. void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
  738. {
  739. struct uprobe *uprobe;
  740. if (!inode || !uc)
  741. return;
  742. uprobe = find_uprobe(inode, offset);
  743. if (!uprobe)
  744. return;
  745. mutex_lock(uprobes_hash(inode));
  746. if (consumer_del(uprobe, uc)) {
  747. if (!uprobe->consumers) {
  748. __uprobe_unregister(uprobe);
  749. clear_bit(UPROBE_RUN_HANDLER, &uprobe->flags);
  750. }
  751. }
  752. mutex_unlock(uprobes_hash(inode));
  753. if (uprobe)
  754. put_uprobe(uprobe);
  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 (!atomic_read(&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. list_for_each_entry_safe(uprobe, u, &tmp_list, pending_list) {
  830. if (!fatal_signal_pending(current)) {
  831. unsigned long vaddr = offset_to_vaddr(vma, uprobe->offset);
  832. install_breakpoint(uprobe, vma->vm_mm, vma, vaddr);
  833. }
  834. put_uprobe(uprobe);
  835. }
  836. mutex_unlock(uprobes_mmap_hash(inode));
  837. return 0;
  838. }
  839. static bool
  840. vma_has_uprobes(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  841. {
  842. loff_t min, max;
  843. struct inode *inode;
  844. struct rb_node *n;
  845. inode = vma->vm_file->f_mapping->host;
  846. min = vaddr_to_offset(vma, start);
  847. max = min + (end - start) - 1;
  848. spin_lock(&uprobes_treelock);
  849. n = find_node_in_range(inode, min, max);
  850. spin_unlock(&uprobes_treelock);
  851. return !!n;
  852. }
  853. /*
  854. * Called in context of a munmap of a vma.
  855. */
  856. void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  857. {
  858. if (!atomic_read(&uprobe_events) || !valid_vma(vma, false))
  859. return;
  860. if (!atomic_read(&vma->vm_mm->mm_users)) /* called by mmput() ? */
  861. return;
  862. if (!test_bit(MMF_HAS_UPROBES, &vma->vm_mm->flags) ||
  863. test_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags))
  864. return;
  865. if (vma_has_uprobes(vma, start, end))
  866. set_bit(MMF_RECALC_UPROBES, &vma->vm_mm->flags);
  867. }
  868. /* Slot allocation for XOL */
  869. static int xol_add_vma(struct xol_area *area)
  870. {
  871. struct mm_struct *mm;
  872. int ret;
  873. area->page = alloc_page(GFP_HIGHUSER);
  874. if (!area->page)
  875. return -ENOMEM;
  876. ret = -EALREADY;
  877. mm = current->mm;
  878. down_write(&mm->mmap_sem);
  879. if (mm->uprobes_state.xol_area)
  880. goto fail;
  881. ret = -ENOMEM;
  882. /* Try to map as high as possible, this is only a hint. */
  883. area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE, PAGE_SIZE, 0, 0);
  884. if (area->vaddr & ~PAGE_MASK) {
  885. ret = area->vaddr;
  886. goto fail;
  887. }
  888. ret = install_special_mapping(mm, area->vaddr, PAGE_SIZE,
  889. VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO, &area->page);
  890. if (ret)
  891. goto fail;
  892. smp_wmb(); /* pairs with get_xol_area() */
  893. mm->uprobes_state.xol_area = area;
  894. ret = 0;
  895. fail:
  896. up_write(&mm->mmap_sem);
  897. if (ret)
  898. __free_page(area->page);
  899. return ret;
  900. }
  901. static struct xol_area *get_xol_area(struct mm_struct *mm)
  902. {
  903. struct xol_area *area;
  904. area = mm->uprobes_state.xol_area;
  905. smp_read_barrier_depends(); /* pairs with wmb in xol_add_vma() */
  906. return area;
  907. }
  908. /*
  909. * xol_alloc_area - Allocate process's xol_area.
  910. * This area will be used for storing instructions for execution out of
  911. * line.
  912. *
  913. * Returns the allocated area or NULL.
  914. */
  915. static struct xol_area *xol_alloc_area(void)
  916. {
  917. struct xol_area *area;
  918. area = kzalloc(sizeof(*area), GFP_KERNEL);
  919. if (unlikely(!area))
  920. return NULL;
  921. area->bitmap = kzalloc(BITS_TO_LONGS(UINSNS_PER_PAGE) * sizeof(long), GFP_KERNEL);
  922. if (!area->bitmap)
  923. goto fail;
  924. init_waitqueue_head(&area->wq);
  925. if (!xol_add_vma(area))
  926. return area;
  927. fail:
  928. kfree(area->bitmap);
  929. kfree(area);
  930. return get_xol_area(current->mm);
  931. }
  932. /*
  933. * uprobe_clear_state - Free the area allocated for slots.
  934. */
  935. void uprobe_clear_state(struct mm_struct *mm)
  936. {
  937. struct xol_area *area = mm->uprobes_state.xol_area;
  938. if (!area)
  939. return;
  940. put_page(area->page);
  941. kfree(area->bitmap);
  942. kfree(area);
  943. }
  944. void uprobe_start_dup_mmap(void)
  945. {
  946. percpu_down_read(&dup_mmap_sem);
  947. }
  948. void uprobe_end_dup_mmap(void)
  949. {
  950. percpu_up_read(&dup_mmap_sem);
  951. }
  952. void uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm)
  953. {
  954. newmm->uprobes_state.xol_area = NULL;
  955. if (test_bit(MMF_HAS_UPROBES, &oldmm->flags)) {
  956. set_bit(MMF_HAS_UPROBES, &newmm->flags);
  957. /* unconditionally, dup_mmap() skips VM_DONTCOPY vmas */
  958. set_bit(MMF_RECALC_UPROBES, &newmm->flags);
  959. }
  960. }
  961. /*
  962. * - search for a free slot.
  963. */
  964. static unsigned long xol_take_insn_slot(struct xol_area *area)
  965. {
  966. unsigned long slot_addr;
  967. int slot_nr;
  968. do {
  969. slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE);
  970. if (slot_nr < UINSNS_PER_PAGE) {
  971. if (!test_and_set_bit(slot_nr, area->bitmap))
  972. break;
  973. slot_nr = UINSNS_PER_PAGE;
  974. continue;
  975. }
  976. wait_event(area->wq, (atomic_read(&area->slot_count) < UINSNS_PER_PAGE));
  977. } while (slot_nr >= UINSNS_PER_PAGE);
  978. slot_addr = area->vaddr + (slot_nr * UPROBE_XOL_SLOT_BYTES);
  979. atomic_inc(&area->slot_count);
  980. return slot_addr;
  981. }
  982. /*
  983. * xol_get_insn_slot - If was not allocated a slot, then
  984. * allocate a slot.
  985. * Returns the allocated slot address or 0.
  986. */
  987. static unsigned long xol_get_insn_slot(struct uprobe *uprobe, unsigned long slot_addr)
  988. {
  989. struct xol_area *area;
  990. unsigned long offset;
  991. void *vaddr;
  992. area = get_xol_area(current->mm);
  993. if (!area) {
  994. area = xol_alloc_area();
  995. if (!area)
  996. return 0;
  997. }
  998. current->utask->xol_vaddr = xol_take_insn_slot(area);
  999. /*
  1000. * Initialize the slot if xol_vaddr points to valid
  1001. * instruction slot.
  1002. */
  1003. if (unlikely(!current->utask->xol_vaddr))
  1004. return 0;
  1005. current->utask->vaddr = slot_addr;
  1006. offset = current->utask->xol_vaddr & ~PAGE_MASK;
  1007. vaddr = kmap_atomic(area->page);
  1008. memcpy(vaddr + offset, uprobe->arch.insn, MAX_UINSN_BYTES);
  1009. kunmap_atomic(vaddr);
  1010. /*
  1011. * We probably need flush_icache_user_range() but it needs vma.
  1012. * This should work on supported architectures too.
  1013. */
  1014. flush_dcache_page(area->page);
  1015. return current->utask->xol_vaddr;
  1016. }
  1017. /*
  1018. * xol_free_insn_slot - If slot was earlier allocated by
  1019. * @xol_get_insn_slot(), make the slot available for
  1020. * subsequent requests.
  1021. */
  1022. static void xol_free_insn_slot(struct task_struct *tsk)
  1023. {
  1024. struct xol_area *area;
  1025. unsigned long vma_end;
  1026. unsigned long slot_addr;
  1027. if (!tsk->mm || !tsk->mm->uprobes_state.xol_area || !tsk->utask)
  1028. return;
  1029. slot_addr = tsk->utask->xol_vaddr;
  1030. if (unlikely(!slot_addr || IS_ERR_VALUE(slot_addr)))
  1031. return;
  1032. area = tsk->mm->uprobes_state.xol_area;
  1033. vma_end = area->vaddr + PAGE_SIZE;
  1034. if (area->vaddr <= slot_addr && slot_addr < vma_end) {
  1035. unsigned long offset;
  1036. int slot_nr;
  1037. offset = slot_addr - area->vaddr;
  1038. slot_nr = offset / UPROBE_XOL_SLOT_BYTES;
  1039. if (slot_nr >= UINSNS_PER_PAGE)
  1040. return;
  1041. clear_bit(slot_nr, area->bitmap);
  1042. atomic_dec(&area->slot_count);
  1043. if (waitqueue_active(&area->wq))
  1044. wake_up(&area->wq);
  1045. tsk->utask->xol_vaddr = 0;
  1046. }
  1047. }
  1048. /**
  1049. * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
  1050. * @regs: Reflects the saved state of the task after it has hit a breakpoint
  1051. * instruction.
  1052. * Return the address of the breakpoint instruction.
  1053. */
  1054. unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs)
  1055. {
  1056. return instruction_pointer(regs) - UPROBE_SWBP_INSN_SIZE;
  1057. }
  1058. /*
  1059. * Called with no locks held.
  1060. * Called in context of a exiting or a exec-ing thread.
  1061. */
  1062. void uprobe_free_utask(struct task_struct *t)
  1063. {
  1064. struct uprobe_task *utask = t->utask;
  1065. if (!utask)
  1066. return;
  1067. if (utask->active_uprobe)
  1068. put_uprobe(utask->active_uprobe);
  1069. xol_free_insn_slot(t);
  1070. kfree(utask);
  1071. t->utask = NULL;
  1072. }
  1073. /*
  1074. * Called in context of a new clone/fork from copy_process.
  1075. */
  1076. void uprobe_copy_process(struct task_struct *t)
  1077. {
  1078. t->utask = NULL;
  1079. }
  1080. /*
  1081. * Allocate a uprobe_task object for the task.
  1082. * Called when the thread hits a breakpoint for the first time.
  1083. *
  1084. * Returns:
  1085. * - pointer to new uprobe_task on success
  1086. * - NULL otherwise
  1087. */
  1088. static struct uprobe_task *add_utask(void)
  1089. {
  1090. struct uprobe_task *utask;
  1091. utask = kzalloc(sizeof *utask, GFP_KERNEL);
  1092. if (unlikely(!utask))
  1093. return NULL;
  1094. current->utask = utask;
  1095. return utask;
  1096. }
  1097. /* Prepare to single-step probed instruction out of line. */
  1098. static int
  1099. pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long vaddr)
  1100. {
  1101. if (xol_get_insn_slot(uprobe, vaddr) && !arch_uprobe_pre_xol(&uprobe->arch, regs))
  1102. return 0;
  1103. return -EFAULT;
  1104. }
  1105. /*
  1106. * If we are singlestepping, then ensure this thread is not connected to
  1107. * non-fatal signals until completion of singlestep. When xol insn itself
  1108. * triggers the signal, restart the original insn even if the task is
  1109. * already SIGKILL'ed (since coredump should report the correct ip). This
  1110. * is even more important if the task has a handler for SIGSEGV/etc, The
  1111. * _same_ instruction should be repeated again after return from the signal
  1112. * handler, and SSTEP can never finish in this case.
  1113. */
  1114. bool uprobe_deny_signal(void)
  1115. {
  1116. struct task_struct *t = current;
  1117. struct uprobe_task *utask = t->utask;
  1118. if (likely(!utask || !utask->active_uprobe))
  1119. return false;
  1120. WARN_ON_ONCE(utask->state != UTASK_SSTEP);
  1121. if (signal_pending(t)) {
  1122. spin_lock_irq(&t->sighand->siglock);
  1123. clear_tsk_thread_flag(t, TIF_SIGPENDING);
  1124. spin_unlock_irq(&t->sighand->siglock);
  1125. if (__fatal_signal_pending(t) || arch_uprobe_xol_was_trapped(t)) {
  1126. utask->state = UTASK_SSTEP_TRAPPED;
  1127. set_tsk_thread_flag(t, TIF_UPROBE);
  1128. set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
  1129. }
  1130. }
  1131. return true;
  1132. }
  1133. /*
  1134. * Avoid singlestepping the original instruction if the original instruction
  1135. * is a NOP or can be emulated.
  1136. */
  1137. static bool can_skip_sstep(struct uprobe *uprobe, struct pt_regs *regs)
  1138. {
  1139. if (test_bit(UPROBE_SKIP_SSTEP, &uprobe->flags)) {
  1140. if (arch_uprobe_skip_sstep(&uprobe->arch, regs))
  1141. return true;
  1142. clear_bit(UPROBE_SKIP_SSTEP, &uprobe->flags);
  1143. }
  1144. return false;
  1145. }
  1146. static void mmf_recalc_uprobes(struct mm_struct *mm)
  1147. {
  1148. struct vm_area_struct *vma;
  1149. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  1150. if (!valid_vma(vma, false))
  1151. continue;
  1152. /*
  1153. * This is not strictly accurate, we can race with
  1154. * uprobe_unregister() and see the already removed
  1155. * uprobe if delete_uprobe() was not yet called.
  1156. */
  1157. if (vma_has_uprobes(vma, vma->vm_start, vma->vm_end))
  1158. return;
  1159. }
  1160. clear_bit(MMF_HAS_UPROBES, &mm->flags);
  1161. }
  1162. static int is_swbp_at_addr(struct mm_struct *mm, unsigned long vaddr)
  1163. {
  1164. struct page *page;
  1165. uprobe_opcode_t opcode;
  1166. int result;
  1167. pagefault_disable();
  1168. result = __copy_from_user_inatomic(&opcode, (void __user*)vaddr,
  1169. sizeof(opcode));
  1170. pagefault_enable();
  1171. if (likely(result == 0))
  1172. goto out;
  1173. result = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &page, NULL);
  1174. if (result < 0)
  1175. return result;
  1176. copy_opcode(page, vaddr, &opcode);
  1177. put_page(page);
  1178. out:
  1179. return is_swbp_insn(&opcode);
  1180. }
  1181. static struct uprobe *find_active_uprobe(unsigned long bp_vaddr, int *is_swbp)
  1182. {
  1183. struct mm_struct *mm = current->mm;
  1184. struct uprobe *uprobe = NULL;
  1185. struct vm_area_struct *vma;
  1186. down_read(&mm->mmap_sem);
  1187. vma = find_vma(mm, bp_vaddr);
  1188. if (vma && vma->vm_start <= bp_vaddr) {
  1189. if (valid_vma(vma, false)) {
  1190. struct inode *inode = vma->vm_file->f_mapping->host;
  1191. loff_t offset = vaddr_to_offset(vma, bp_vaddr);
  1192. uprobe = find_uprobe(inode, offset);
  1193. }
  1194. if (!uprobe)
  1195. *is_swbp = is_swbp_at_addr(mm, bp_vaddr);
  1196. } else {
  1197. *is_swbp = -EFAULT;
  1198. }
  1199. if (!uprobe && test_and_clear_bit(MMF_RECALC_UPROBES, &mm->flags))
  1200. mmf_recalc_uprobes(mm);
  1201. up_read(&mm->mmap_sem);
  1202. return uprobe;
  1203. }
  1204. /*
  1205. * Run handler and ask thread to singlestep.
  1206. * Ensure all non-fatal signals cannot interrupt thread while it singlesteps.
  1207. */
  1208. static void handle_swbp(struct pt_regs *regs)
  1209. {
  1210. struct uprobe_task *utask;
  1211. struct uprobe *uprobe;
  1212. unsigned long bp_vaddr;
  1213. int uninitialized_var(is_swbp);
  1214. bp_vaddr = uprobe_get_swbp_addr(regs);
  1215. uprobe = find_active_uprobe(bp_vaddr, &is_swbp);
  1216. if (!uprobe) {
  1217. if (is_swbp > 0) {
  1218. /* No matching uprobe; signal SIGTRAP. */
  1219. send_sig(SIGTRAP, current, 0);
  1220. } else {
  1221. /*
  1222. * Either we raced with uprobe_unregister() or we can't
  1223. * access this memory. The latter is only possible if
  1224. * another thread plays with our ->mm. In both cases
  1225. * we can simply restart. If this vma was unmapped we
  1226. * can pretend this insn was not executed yet and get
  1227. * the (correct) SIGSEGV after restart.
  1228. */
  1229. instruction_pointer_set(regs, bp_vaddr);
  1230. }
  1231. return;
  1232. }
  1233. /*
  1234. * TODO: move copy_insn/etc into _register and remove this hack.
  1235. * After we hit the bp, _unregister + _register can install the
  1236. * new and not-yet-analyzed uprobe at the same address, restart.
  1237. */
  1238. smp_rmb(); /* pairs with wmb() in install_breakpoint() */
  1239. if (unlikely(!test_bit(UPROBE_COPY_INSN, &uprobe->flags)))
  1240. goto restart;
  1241. utask = current->utask;
  1242. if (!utask) {
  1243. utask = add_utask();
  1244. /* Cannot allocate; re-execute the instruction. */
  1245. if (!utask)
  1246. goto restart;
  1247. }
  1248. handler_chain(uprobe, regs);
  1249. if (can_skip_sstep(uprobe, regs))
  1250. goto out;
  1251. if (!pre_ssout(uprobe, regs, bp_vaddr)) {
  1252. utask->active_uprobe = uprobe;
  1253. utask->state = UTASK_SSTEP;
  1254. return;
  1255. }
  1256. restart:
  1257. /*
  1258. * cannot singlestep; cannot skip instruction;
  1259. * re-execute the instruction.
  1260. */
  1261. instruction_pointer_set(regs, bp_vaddr);
  1262. out:
  1263. put_uprobe(uprobe);
  1264. }
  1265. /*
  1266. * Perform required fix-ups and disable singlestep.
  1267. * Allow pending signals to take effect.
  1268. */
  1269. static void handle_singlestep(struct uprobe_task *utask, struct pt_regs *regs)
  1270. {
  1271. struct uprobe *uprobe;
  1272. uprobe = utask->active_uprobe;
  1273. if (utask->state == UTASK_SSTEP_ACK)
  1274. arch_uprobe_post_xol(&uprobe->arch, regs);
  1275. else if (utask->state == UTASK_SSTEP_TRAPPED)
  1276. arch_uprobe_abort_xol(&uprobe->arch, regs);
  1277. else
  1278. WARN_ON_ONCE(1);
  1279. put_uprobe(uprobe);
  1280. utask->active_uprobe = NULL;
  1281. utask->state = UTASK_RUNNING;
  1282. xol_free_insn_slot(current);
  1283. spin_lock_irq(&current->sighand->siglock);
  1284. recalc_sigpending(); /* see uprobe_deny_signal() */
  1285. spin_unlock_irq(&current->sighand->siglock);
  1286. }
  1287. /*
  1288. * On breakpoint hit, breakpoint notifier sets the TIF_UPROBE flag and
  1289. * allows the thread to return from interrupt. After that handle_swbp()
  1290. * sets utask->active_uprobe.
  1291. *
  1292. * On singlestep exception, singlestep notifier sets the TIF_UPROBE flag
  1293. * and allows the thread to return from interrupt.
  1294. *
  1295. * While returning to userspace, thread notices the TIF_UPROBE flag and calls
  1296. * uprobe_notify_resume().
  1297. */
  1298. void uprobe_notify_resume(struct pt_regs *regs)
  1299. {
  1300. struct uprobe_task *utask;
  1301. clear_thread_flag(TIF_UPROBE);
  1302. utask = current->utask;
  1303. if (utask && utask->active_uprobe)
  1304. handle_singlestep(utask, regs);
  1305. else
  1306. handle_swbp(regs);
  1307. }
  1308. /*
  1309. * uprobe_pre_sstep_notifier gets called from interrupt context as part of
  1310. * notifier mechanism. Set TIF_UPROBE flag and indicate breakpoint hit.
  1311. */
  1312. int uprobe_pre_sstep_notifier(struct pt_regs *regs)
  1313. {
  1314. if (!current->mm || !test_bit(MMF_HAS_UPROBES, &current->mm->flags))
  1315. return 0;
  1316. set_thread_flag(TIF_UPROBE);
  1317. return 1;
  1318. }
  1319. /*
  1320. * uprobe_post_sstep_notifier gets called in interrupt context as part of notifier
  1321. * mechanism. Set TIF_UPROBE flag and indicate completion of singlestep.
  1322. */
  1323. int uprobe_post_sstep_notifier(struct pt_regs *regs)
  1324. {
  1325. struct uprobe_task *utask = current->utask;
  1326. if (!current->mm || !utask || !utask->active_uprobe)
  1327. /* task is currently not uprobed */
  1328. return 0;
  1329. utask->state = UTASK_SSTEP_ACK;
  1330. set_thread_flag(TIF_UPROBE);
  1331. return 1;
  1332. }
  1333. static struct notifier_block uprobe_exception_nb = {
  1334. .notifier_call = arch_uprobe_exception_notify,
  1335. .priority = INT_MAX-1, /* notified after kprobes, kgdb */
  1336. };
  1337. static int __init init_uprobes(void)
  1338. {
  1339. int i;
  1340. for (i = 0; i < UPROBES_HASH_SZ; i++) {
  1341. mutex_init(&uprobes_mutex[i]);
  1342. mutex_init(&uprobes_mmap_mutex[i]);
  1343. }
  1344. if (percpu_init_rwsem(&dup_mmap_sem))
  1345. return -ENOMEM;
  1346. return register_die_notifier(&uprobe_exception_nb);
  1347. }
  1348. module_init(init_uprobes);
  1349. static void __exit exit_uprobes(void)
  1350. {
  1351. }
  1352. module_exit(exit_uprobes);