uprobes.c 40 KB

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