uprobes.c 38 KB

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