uprobes.c 40 KB

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