uprobes.c 39 KB

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