uprobes.c 39 KB

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