uprobes.c 39 KB

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