uprobes.c 40 KB

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