uprobes.c 41 KB

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