uprobes.c 41 KB

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