uprobes.c 37 KB

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