uprobes.c 39 KB

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