uprobes.c 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944
  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/task_work.h>
  38. #include <linux/uprobes.h>
  39. #define UINSNS_PER_PAGE (PAGE_SIZE/UPROBE_XOL_SLOT_BYTES)
  40. #define MAX_UPROBE_XOL_SLOTS UINSNS_PER_PAGE
  41. static struct rb_root uprobes_tree = RB_ROOT;
  42. /*
  43. * allows us to skip the uprobe_mmap if there are no uprobe events active
  44. * at this time. Probably a fine grained per inode count is better?
  45. */
  46. #define no_uprobe_events() RB_EMPTY_ROOT(&uprobes_tree)
  47. static DEFINE_SPINLOCK(uprobes_treelock); /* serialize rbtree access */
  48. #define UPROBES_HASH_SZ 13
  49. /* serialize uprobe->pending_list */
  50. static struct mutex uprobes_mmap_mutex[UPROBES_HASH_SZ];
  51. #define uprobes_mmap_hash(v) (&uprobes_mmap_mutex[((unsigned long)(v)) % UPROBES_HASH_SZ])
  52. static struct percpu_rw_semaphore dup_mmap_sem;
  53. /* Have a copy of original instruction */
  54. #define UPROBE_COPY_INSN 0
  55. /* Can skip singlestep */
  56. #define UPROBE_SKIP_SSTEP 1
  57. struct uprobe {
  58. struct rb_node rb_node; /* node in the rb tree */
  59. atomic_t ref;
  60. struct rw_semaphore register_rwsem;
  61. struct rw_semaphore consumer_rwsem;
  62. struct list_head pending_list;
  63. struct uprobe_consumer *consumers;
  64. struct inode *inode; /* Also hold a ref to inode */
  65. loff_t offset;
  66. unsigned long flags;
  67. struct arch_uprobe arch;
  68. };
  69. struct return_instance {
  70. struct uprobe *uprobe;
  71. unsigned long func;
  72. unsigned long orig_ret_vaddr; /* original return address */
  73. bool chained; /* true, if instance is nested */
  74. struct return_instance *next; /* keep as stack */
  75. };
  76. /*
  77. * valid_vma: Verify if the specified vma is an executable vma
  78. * Relax restrictions while unregistering: vm_flags might have
  79. * changed after breakpoint was inserted.
  80. * - is_register: indicates if we are in register context.
  81. * - Return 1 if the specified virtual address is in an
  82. * executable vma.
  83. */
  84. static bool valid_vma(struct vm_area_struct *vma, bool is_register)
  85. {
  86. vm_flags_t flags = VM_HUGETLB | VM_MAYEXEC | VM_SHARED;
  87. if (is_register)
  88. flags |= VM_WRITE;
  89. return vma->vm_file && (vma->vm_flags & flags) == VM_MAYEXEC;
  90. }
  91. static unsigned long offset_to_vaddr(struct vm_area_struct *vma, loff_t offset)
  92. {
  93. return vma->vm_start + offset - ((loff_t)vma->vm_pgoff << PAGE_SHIFT);
  94. }
  95. static loff_t vaddr_to_offset(struct vm_area_struct *vma, unsigned long vaddr)
  96. {
  97. return ((loff_t)vma->vm_pgoff << PAGE_SHIFT) + (vaddr - vma->vm_start);
  98. }
  99. /**
  100. * __replace_page - replace page in vma by new page.
  101. * based on replace_page in mm/ksm.c
  102. *
  103. * @vma: vma that holds the pte pointing to page
  104. * @addr: address the old @page is mapped at
  105. * @page: the cowed page we are replacing by kpage
  106. * @kpage: the modified page we replace page by
  107. *
  108. * Returns 0 on success, -EFAULT on failure.
  109. */
  110. static int __replace_page(struct vm_area_struct *vma, unsigned long addr,
  111. struct page *page, struct page *kpage)
  112. {
  113. struct mm_struct *mm = vma->vm_mm;
  114. spinlock_t *ptl;
  115. pte_t *ptep;
  116. int err;
  117. /* For mmu_notifiers */
  118. const unsigned long mmun_start = addr;
  119. const unsigned long mmun_end = addr + PAGE_SIZE;
  120. /* For try_to_free_swap() and munlock_vma_page() below */
  121. lock_page(page);
  122. mmu_notifier_invalidate_range_start(mm, mmun_start, mmun_end);
  123. err = -EAGAIN;
  124. ptep = page_check_address(page, mm, addr, &ptl, 0);
  125. if (!ptep)
  126. goto unlock;
  127. get_page(kpage);
  128. page_add_new_anon_rmap(kpage, vma, addr);
  129. if (!PageAnon(page)) {
  130. dec_mm_counter(mm, MM_FILEPAGES);
  131. inc_mm_counter(mm, MM_ANONPAGES);
  132. }
  133. flush_cache_page(vma, addr, pte_pfn(*ptep));
  134. ptep_clear_flush(vma, addr, ptep);
  135. set_pte_at_notify(mm, addr, ptep, mk_pte(kpage, vma->vm_page_prot));
  136. page_remove_rmap(page);
  137. if (!page_mapped(page))
  138. try_to_free_swap(page);
  139. pte_unmap_unlock(ptep, ptl);
  140. if (vma->vm_flags & VM_LOCKED)
  141. munlock_vma_page(page);
  142. put_page(page);
  143. err = 0;
  144. unlock:
  145. mmu_notifier_invalidate_range_end(mm, mmun_start, mmun_end);
  146. unlock_page(page);
  147. return err;
  148. }
  149. /**
  150. * is_swbp_insn - check if instruction is breakpoint instruction.
  151. * @insn: instruction to be checked.
  152. * Default implementation of is_swbp_insn
  153. * Returns true if @insn is a breakpoint instruction.
  154. */
  155. bool __weak is_swbp_insn(uprobe_opcode_t *insn)
  156. {
  157. return *insn == UPROBE_SWBP_INSN;
  158. }
  159. /**
  160. * is_trap_insn - check if instruction is breakpoint instruction.
  161. * @insn: instruction to be checked.
  162. * Default implementation of is_trap_insn
  163. * Returns true if @insn is a breakpoint instruction.
  164. *
  165. * This function is needed for the case where an architecture has multiple
  166. * trap instructions (like powerpc).
  167. */
  168. bool __weak is_trap_insn(uprobe_opcode_t *insn)
  169. {
  170. return is_swbp_insn(insn);
  171. }
  172. static void copy_from_page(struct page *page, unsigned long vaddr, void *dst, int len)
  173. {
  174. void *kaddr = kmap_atomic(page);
  175. memcpy(dst, kaddr + (vaddr & ~PAGE_MASK), len);
  176. kunmap_atomic(kaddr);
  177. }
  178. static void copy_to_page(struct page *page, unsigned long vaddr, const void *src, int len)
  179. {
  180. void *kaddr = kmap_atomic(page);
  181. memcpy(kaddr + (vaddr & ~PAGE_MASK), src, len);
  182. kunmap_atomic(kaddr);
  183. }
  184. static int verify_opcode(struct page *page, unsigned long vaddr, uprobe_opcode_t *new_opcode)
  185. {
  186. uprobe_opcode_t old_opcode;
  187. bool is_swbp;
  188. /*
  189. * Note: We only check if the old_opcode is UPROBE_SWBP_INSN here.
  190. * We do not check if it is any other 'trap variant' which could
  191. * be conditional trap instruction such as the one powerpc supports.
  192. *
  193. * The logic is that we do not care if the underlying instruction
  194. * is a trap variant; uprobes always wins over any other (gdb)
  195. * breakpoint.
  196. */
  197. copy_from_page(page, vaddr, &old_opcode, UPROBE_SWBP_INSN_SIZE);
  198. is_swbp = is_swbp_insn(&old_opcode);
  199. if (is_swbp_insn(new_opcode)) {
  200. if (is_swbp) /* register: already installed? */
  201. return 0;
  202. } else {
  203. if (!is_swbp) /* unregister: was it changed by us? */
  204. return 0;
  205. }
  206. return 1;
  207. }
  208. /*
  209. * NOTE:
  210. * Expect the breakpoint instruction to be the smallest size instruction for
  211. * the architecture. If an arch has variable length instruction and the
  212. * breakpoint instruction is not of the smallest length instruction
  213. * supported by that architecture then we need to modify is_trap_at_addr and
  214. * uprobe_write_opcode accordingly. This would never be a problem for archs
  215. * that have fixed length instructions.
  216. */
  217. /*
  218. * uprobe_write_opcode - write the opcode at a given virtual address.
  219. * @mm: the probed process address space.
  220. * @vaddr: the virtual address to store the opcode.
  221. * @opcode: opcode to be written at @vaddr.
  222. *
  223. * Called with mm->mmap_sem held (for read and with a reference to
  224. * mm).
  225. *
  226. * For mm @mm, write the opcode at @vaddr.
  227. * Return 0 (success) or a negative errno.
  228. */
  229. int uprobe_write_opcode(struct mm_struct *mm, unsigned long vaddr,
  230. uprobe_opcode_t opcode)
  231. {
  232. struct page *old_page, *new_page;
  233. struct vm_area_struct *vma;
  234. int ret;
  235. retry:
  236. /* Read the page with vaddr into memory */
  237. ret = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &old_page, &vma);
  238. if (ret <= 0)
  239. return ret;
  240. ret = verify_opcode(old_page, vaddr, &opcode);
  241. if (ret <= 0)
  242. goto put_old;
  243. ret = -ENOMEM;
  244. new_page = alloc_page_vma(GFP_HIGHUSER_MOVABLE, vma, vaddr);
  245. if (!new_page)
  246. goto put_old;
  247. __SetPageUptodate(new_page);
  248. copy_highpage(new_page, old_page);
  249. copy_to_page(new_page, vaddr, &opcode, UPROBE_SWBP_INSN_SIZE);
  250. ret = anon_vma_prepare(vma);
  251. if (ret)
  252. goto put_new;
  253. ret = __replace_page(vma, vaddr, old_page, new_page);
  254. put_new:
  255. page_cache_release(new_page);
  256. put_old:
  257. put_page(old_page);
  258. if (unlikely(ret == -EAGAIN))
  259. goto retry;
  260. return ret;
  261. }
  262. /**
  263. * set_swbp - store breakpoint at a given address.
  264. * @auprobe: arch specific probepoint information.
  265. * @mm: the probed process address space.
  266. * @vaddr: the virtual address to insert the opcode.
  267. *
  268. * For mm @mm, store the breakpoint instruction at @vaddr.
  269. * Return 0 (success) or a negative errno.
  270. */
  271. int __weak set_swbp(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
  272. {
  273. return uprobe_write_opcode(mm, vaddr, UPROBE_SWBP_INSN);
  274. }
  275. /**
  276. * set_orig_insn - Restore the original instruction.
  277. * @mm: the probed process address space.
  278. * @auprobe: arch specific probepoint information.
  279. * @vaddr: the virtual address to insert the opcode.
  280. *
  281. * For mm @mm, restore the original opcode (opcode) at @vaddr.
  282. * Return 0 (success) or a negative errno.
  283. */
  284. int __weak
  285. set_orig_insn(struct arch_uprobe *auprobe, struct mm_struct *mm, unsigned long vaddr)
  286. {
  287. return uprobe_write_opcode(mm, vaddr, *(uprobe_opcode_t *)auprobe->insn);
  288. }
  289. static int match_uprobe(struct uprobe *l, struct uprobe *r)
  290. {
  291. if (l->inode < r->inode)
  292. return -1;
  293. if (l->inode > r->inode)
  294. return 1;
  295. if (l->offset < r->offset)
  296. return -1;
  297. if (l->offset > r->offset)
  298. return 1;
  299. return 0;
  300. }
  301. static struct uprobe *__find_uprobe(struct inode *inode, loff_t offset)
  302. {
  303. struct uprobe u = { .inode = inode, .offset = offset };
  304. struct rb_node *n = uprobes_tree.rb_node;
  305. struct uprobe *uprobe;
  306. int match;
  307. while (n) {
  308. uprobe = rb_entry(n, struct uprobe, rb_node);
  309. match = match_uprobe(&u, uprobe);
  310. if (!match) {
  311. atomic_inc(&uprobe->ref);
  312. return uprobe;
  313. }
  314. if (match < 0)
  315. n = n->rb_left;
  316. else
  317. n = n->rb_right;
  318. }
  319. return NULL;
  320. }
  321. /*
  322. * Find a uprobe corresponding to a given inode:offset
  323. * Acquires uprobes_treelock
  324. */
  325. static struct uprobe *find_uprobe(struct inode *inode, loff_t offset)
  326. {
  327. struct uprobe *uprobe;
  328. spin_lock(&uprobes_treelock);
  329. uprobe = __find_uprobe(inode, offset);
  330. spin_unlock(&uprobes_treelock);
  331. return uprobe;
  332. }
  333. static struct uprobe *__insert_uprobe(struct uprobe *uprobe)
  334. {
  335. struct rb_node **p = &uprobes_tree.rb_node;
  336. struct rb_node *parent = NULL;
  337. struct uprobe *u;
  338. int match;
  339. while (*p) {
  340. parent = *p;
  341. u = rb_entry(parent, struct uprobe, rb_node);
  342. match = match_uprobe(uprobe, u);
  343. if (!match) {
  344. atomic_inc(&u->ref);
  345. return u;
  346. }
  347. if (match < 0)
  348. p = &parent->rb_left;
  349. else
  350. p = &parent->rb_right;
  351. }
  352. u = NULL;
  353. rb_link_node(&uprobe->rb_node, parent, p);
  354. rb_insert_color(&uprobe->rb_node, &uprobes_tree);
  355. /* get access + creation ref */
  356. atomic_set(&uprobe->ref, 2);
  357. return u;
  358. }
  359. /*
  360. * Acquire uprobes_treelock.
  361. * Matching uprobe already exists in rbtree;
  362. * increment (access refcount) and return the matching uprobe.
  363. *
  364. * No matching uprobe; insert the uprobe in rb_tree;
  365. * get a double refcount (access + creation) and return NULL.
  366. */
  367. static struct uprobe *insert_uprobe(struct uprobe *uprobe)
  368. {
  369. struct uprobe *u;
  370. spin_lock(&uprobes_treelock);
  371. u = __insert_uprobe(uprobe);
  372. spin_unlock(&uprobes_treelock);
  373. return u;
  374. }
  375. static void put_uprobe(struct uprobe *uprobe)
  376. {
  377. if (atomic_dec_and_test(&uprobe->ref))
  378. kfree(uprobe);
  379. }
  380. static struct uprobe *alloc_uprobe(struct inode *inode, loff_t offset)
  381. {
  382. struct uprobe *uprobe, *cur_uprobe;
  383. uprobe = kzalloc(sizeof(struct uprobe), GFP_KERNEL);
  384. if (!uprobe)
  385. return NULL;
  386. uprobe->inode = igrab(inode);
  387. uprobe->offset = offset;
  388. init_rwsem(&uprobe->register_rwsem);
  389. init_rwsem(&uprobe->consumer_rwsem);
  390. /* For now assume that the instruction need not be single-stepped */
  391. __set_bit(UPROBE_SKIP_SSTEP, &uprobe->flags);
  392. /* add to uprobes_tree, sorted on inode:offset */
  393. cur_uprobe = insert_uprobe(uprobe);
  394. /* a uprobe exists for this inode:offset combination */
  395. if (cur_uprobe) {
  396. kfree(uprobe);
  397. uprobe = cur_uprobe;
  398. iput(inode);
  399. }
  400. return uprobe;
  401. }
  402. static void consumer_add(struct uprobe *uprobe, struct uprobe_consumer *uc)
  403. {
  404. down_write(&uprobe->consumer_rwsem);
  405. uc->next = uprobe->consumers;
  406. uprobe->consumers = uc;
  407. up_write(&uprobe->consumer_rwsem);
  408. }
  409. /*
  410. * For uprobe @uprobe, delete the consumer @uc.
  411. * Return true if the @uc is deleted successfully
  412. * or return false.
  413. */
  414. static bool consumer_del(struct uprobe *uprobe, struct uprobe_consumer *uc)
  415. {
  416. struct uprobe_consumer **con;
  417. bool ret = false;
  418. down_write(&uprobe->consumer_rwsem);
  419. for (con = &uprobe->consumers; *con; con = &(*con)->next) {
  420. if (*con == uc) {
  421. *con = uc->next;
  422. ret = true;
  423. break;
  424. }
  425. }
  426. up_write(&uprobe->consumer_rwsem);
  427. return ret;
  428. }
  429. static int __copy_insn(struct address_space *mapping, struct file *filp,
  430. void *insn, int nbytes, loff_t offset)
  431. {
  432. struct page *page;
  433. if (!mapping->a_ops->readpage)
  434. return -EIO;
  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, offset >> PAGE_CACHE_SHIFT, filp);
  440. if (IS_ERR(page))
  441. return PTR_ERR(page);
  442. copy_from_page(page, offset, insn, nbytes);
  443. page_cache_release(page);
  444. return 0;
  445. }
  446. static int copy_insn(struct uprobe *uprobe, struct file *filp)
  447. {
  448. struct address_space *mapping = uprobe->inode->i_mapping;
  449. loff_t offs = uprobe->offset;
  450. void *insn = uprobe->arch.insn;
  451. int size = MAX_UINSN_BYTES;
  452. int len, err = -EIO;
  453. /* Copy only available bytes, -EIO if nothing was read */
  454. do {
  455. if (offs >= i_size_read(uprobe->inode))
  456. break;
  457. len = min_t(int, size, PAGE_SIZE - (offs & ~PAGE_MASK));
  458. err = __copy_insn(mapping, filp, insn, len, offs);
  459. if (err)
  460. break;
  461. insn += len;
  462. offs += len;
  463. size -= len;
  464. } while (size);
  465. return err;
  466. }
  467. static int prepare_uprobe(struct uprobe *uprobe, struct file *file,
  468. struct mm_struct *mm, unsigned long vaddr)
  469. {
  470. int ret = 0;
  471. if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
  472. return ret;
  473. /* TODO: move this into _register, until then we abuse this sem. */
  474. down_write(&uprobe->consumer_rwsem);
  475. if (test_bit(UPROBE_COPY_INSN, &uprobe->flags))
  476. goto out;
  477. ret = copy_insn(uprobe, file);
  478. if (ret)
  479. goto out;
  480. ret = -ENOTSUPP;
  481. if (is_trap_insn((uprobe_opcode_t *)uprobe->arch.insn))
  482. goto out;
  483. ret = arch_uprobe_analyze_insn(&uprobe->arch, mm, vaddr);
  484. if (ret)
  485. goto out;
  486. /* uprobe_write_opcode() assumes we don't cross page boundary */
  487. BUG_ON((uprobe->offset & ~PAGE_MASK) +
  488. UPROBE_SWBP_INSN_SIZE > PAGE_SIZE);
  489. smp_wmb(); /* pairs with rmb() in find_active_uprobe() */
  490. set_bit(UPROBE_COPY_INSN, &uprobe->flags);
  491. out:
  492. up_write(&uprobe->consumer_rwsem);
  493. return ret;
  494. }
  495. static inline bool consumer_filter(struct uprobe_consumer *uc,
  496. enum uprobe_filter_ctx ctx, struct mm_struct *mm)
  497. {
  498. return !uc->filter || uc->filter(uc, ctx, mm);
  499. }
  500. static bool filter_chain(struct uprobe *uprobe,
  501. enum uprobe_filter_ctx ctx, struct mm_struct *mm)
  502. {
  503. struct uprobe_consumer *uc;
  504. bool ret = false;
  505. down_read(&uprobe->consumer_rwsem);
  506. for (uc = uprobe->consumers; uc; uc = uc->next) {
  507. ret = consumer_filter(uc, ctx, mm);
  508. if (ret)
  509. break;
  510. }
  511. up_read(&uprobe->consumer_rwsem);
  512. return ret;
  513. }
  514. static int
  515. install_breakpoint(struct uprobe *uprobe, struct mm_struct *mm,
  516. struct vm_area_struct *vma, unsigned long vaddr)
  517. {
  518. bool first_uprobe;
  519. int ret;
  520. ret = prepare_uprobe(uprobe, vma->vm_file, mm, vaddr);
  521. if (ret)
  522. return ret;
  523. /*
  524. * set MMF_HAS_UPROBES in advance for uprobe_pre_sstep_notifier(),
  525. * the task can hit this breakpoint right after __replace_page().
  526. */
  527. first_uprobe = !test_bit(MMF_HAS_UPROBES, &mm->flags);
  528. if (first_uprobe)
  529. set_bit(MMF_HAS_UPROBES, &mm->flags);
  530. ret = set_swbp(&uprobe->arch, mm, vaddr);
  531. if (!ret)
  532. clear_bit(MMF_RECALC_UPROBES, &mm->flags);
  533. else if (first_uprobe)
  534. clear_bit(MMF_HAS_UPROBES, &mm->flags);
  535. return ret;
  536. }
  537. static int
  538. remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, unsigned long vaddr)
  539. {
  540. set_bit(MMF_RECALC_UPROBES, &mm->flags);
  541. return set_orig_insn(&uprobe->arch, mm, vaddr);
  542. }
  543. static inline bool uprobe_is_active(struct uprobe *uprobe)
  544. {
  545. return !RB_EMPTY_NODE(&uprobe->rb_node);
  546. }
  547. /*
  548. * There could be threads that have already hit the breakpoint. They
  549. * will recheck the current insn and restart if find_uprobe() fails.
  550. * See find_active_uprobe().
  551. */
  552. static void delete_uprobe(struct uprobe *uprobe)
  553. {
  554. if (WARN_ON(!uprobe_is_active(uprobe)))
  555. return;
  556. spin_lock(&uprobes_treelock);
  557. rb_erase(&uprobe->rb_node, &uprobes_tree);
  558. spin_unlock(&uprobes_treelock);
  559. RB_CLEAR_NODE(&uprobe->rb_node); /* for uprobe_is_active() */
  560. iput(uprobe->inode);
  561. put_uprobe(uprobe);
  562. }
  563. struct map_info {
  564. struct map_info *next;
  565. struct mm_struct *mm;
  566. unsigned long vaddr;
  567. };
  568. static inline struct map_info *free_map_info(struct map_info *info)
  569. {
  570. struct map_info *next = info->next;
  571. kfree(info);
  572. return next;
  573. }
  574. static struct map_info *
  575. build_map_info(struct address_space *mapping, loff_t offset, bool is_register)
  576. {
  577. unsigned long pgoff = offset >> PAGE_SHIFT;
  578. struct vm_area_struct *vma;
  579. struct map_info *curr = NULL;
  580. struct map_info *prev = NULL;
  581. struct map_info *info;
  582. int more = 0;
  583. again:
  584. mutex_lock(&mapping->i_mmap_mutex);
  585. vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) {
  586. if (!valid_vma(vma, is_register))
  587. continue;
  588. if (!prev && !more) {
  589. /*
  590. * Needs GFP_NOWAIT to avoid i_mmap_mutex recursion through
  591. * reclaim. This is optimistic, no harm done if it fails.
  592. */
  593. prev = kmalloc(sizeof(struct map_info),
  594. GFP_NOWAIT | __GFP_NOMEMALLOC | __GFP_NOWARN);
  595. if (prev)
  596. prev->next = NULL;
  597. }
  598. if (!prev) {
  599. more++;
  600. continue;
  601. }
  602. if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
  603. continue;
  604. info = prev;
  605. prev = prev->next;
  606. info->next = curr;
  607. curr = info;
  608. info->mm = vma->vm_mm;
  609. info->vaddr = offset_to_vaddr(vma, offset);
  610. }
  611. mutex_unlock(&mapping->i_mmap_mutex);
  612. if (!more)
  613. goto out;
  614. prev = curr;
  615. while (curr) {
  616. mmput(curr->mm);
  617. curr = curr->next;
  618. }
  619. do {
  620. info = kmalloc(sizeof(struct map_info), GFP_KERNEL);
  621. if (!info) {
  622. curr = ERR_PTR(-ENOMEM);
  623. goto out;
  624. }
  625. info->next = prev;
  626. prev = info;
  627. } while (--more);
  628. goto again;
  629. out:
  630. while (prev)
  631. prev = free_map_info(prev);
  632. return curr;
  633. }
  634. static int
  635. register_for_each_vma(struct uprobe *uprobe, struct uprobe_consumer *new)
  636. {
  637. bool is_register = !!new;
  638. struct map_info *info;
  639. int err = 0;
  640. percpu_down_write(&dup_mmap_sem);
  641. info = build_map_info(uprobe->inode->i_mapping,
  642. uprobe->offset, is_register);
  643. if (IS_ERR(info)) {
  644. err = PTR_ERR(info);
  645. goto out;
  646. }
  647. while (info) {
  648. struct mm_struct *mm = info->mm;
  649. struct vm_area_struct *vma;
  650. if (err && is_register)
  651. goto free;
  652. down_write(&mm->mmap_sem);
  653. vma = find_vma(mm, info->vaddr);
  654. if (!vma || !valid_vma(vma, is_register) ||
  655. file_inode(vma->vm_file) != uprobe->inode)
  656. goto unlock;
  657. if (vma->vm_start > info->vaddr ||
  658. vaddr_to_offset(vma, info->vaddr) != uprobe->offset)
  659. goto unlock;
  660. if (is_register) {
  661. /* consult only the "caller", new consumer. */
  662. if (consumer_filter(new,
  663. UPROBE_FILTER_REGISTER, mm))
  664. err = install_breakpoint(uprobe, mm, vma, info->vaddr);
  665. } else if (test_bit(MMF_HAS_UPROBES, &mm->flags)) {
  666. if (!filter_chain(uprobe,
  667. UPROBE_FILTER_UNREGISTER, mm))
  668. err |= remove_breakpoint(uprobe, mm, info->vaddr);
  669. }
  670. unlock:
  671. up_write(&mm->mmap_sem);
  672. free:
  673. mmput(mm);
  674. info = free_map_info(info);
  675. }
  676. out:
  677. percpu_up_write(&dup_mmap_sem);
  678. return err;
  679. }
  680. static int __uprobe_register(struct uprobe *uprobe, struct uprobe_consumer *uc)
  681. {
  682. consumer_add(uprobe, uc);
  683. return register_for_each_vma(uprobe, uc);
  684. }
  685. static void __uprobe_unregister(struct uprobe *uprobe, struct uprobe_consumer *uc)
  686. {
  687. int err;
  688. if (!consumer_del(uprobe, uc)) /* WARN? */
  689. return;
  690. err = register_for_each_vma(uprobe, NULL);
  691. /* TODO : cant unregister? schedule a worker thread */
  692. if (!uprobe->consumers && !err)
  693. delete_uprobe(uprobe);
  694. }
  695. /*
  696. * uprobe_register - register a probe
  697. * @inode: the file in which the probe has to be placed.
  698. * @offset: offset from the start of the file.
  699. * @uc: information on howto handle the probe..
  700. *
  701. * Apart from the access refcount, uprobe_register() takes a creation
  702. * refcount (thro alloc_uprobe) if and only if this @uprobe is getting
  703. * inserted into the rbtree (i.e first consumer for a @inode:@offset
  704. * tuple). Creation refcount stops uprobe_unregister from freeing the
  705. * @uprobe even before the register operation is complete. Creation
  706. * refcount is released when the last @uc for the @uprobe
  707. * unregisters.
  708. *
  709. * Return errno if it cannot successully install probes
  710. * else return 0 (success)
  711. */
  712. int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
  713. {
  714. struct uprobe *uprobe;
  715. int ret;
  716. /* Uprobe must have at least one set consumer */
  717. if (!uc->handler && !uc->ret_handler)
  718. return -EINVAL;
  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 mm_struct *mm, struct xol_area *area)
  928. {
  929. int ret = -EALREADY;
  930. down_write(&mm->mmap_sem);
  931. if (mm->uprobes_state.xol_area)
  932. goto fail;
  933. if (!area->vaddr) {
  934. /* Try to map as high as possible, this is only a hint. */
  935. area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE,
  936. PAGE_SIZE, 0, 0);
  937. if (area->vaddr & ~PAGE_MASK) {
  938. ret = area->vaddr;
  939. goto fail;
  940. }
  941. }
  942. ret = install_special_mapping(mm, area->vaddr, PAGE_SIZE,
  943. VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO, &area->page);
  944. if (ret)
  945. goto fail;
  946. smp_wmb(); /* pairs with get_xol_area() */
  947. mm->uprobes_state.xol_area = area;
  948. fail:
  949. up_write(&mm->mmap_sem);
  950. return ret;
  951. }
  952. static struct xol_area *__create_xol_area(unsigned long vaddr)
  953. {
  954. struct mm_struct *mm = current->mm;
  955. uprobe_opcode_t insn = UPROBE_SWBP_INSN;
  956. struct xol_area *area;
  957. area = kmalloc(sizeof(*area), GFP_KERNEL);
  958. if (unlikely(!area))
  959. goto out;
  960. area->bitmap = kzalloc(BITS_TO_LONGS(UINSNS_PER_PAGE) * sizeof(long), GFP_KERNEL);
  961. if (!area->bitmap)
  962. goto free_area;
  963. area->page = alloc_page(GFP_HIGHUSER);
  964. if (!area->page)
  965. goto free_bitmap;
  966. area->vaddr = vaddr;
  967. init_waitqueue_head(&area->wq);
  968. /* Reserve the 1st slot for get_trampoline_vaddr() */
  969. set_bit(0, area->bitmap);
  970. atomic_set(&area->slot_count, 1);
  971. copy_to_page(area->page, 0, &insn, UPROBE_SWBP_INSN_SIZE);
  972. if (!xol_add_vma(mm, area))
  973. return area;
  974. __free_page(area->page);
  975. free_bitmap:
  976. kfree(area->bitmap);
  977. free_area:
  978. kfree(area);
  979. out:
  980. return NULL;
  981. }
  982. /*
  983. * get_xol_area - Allocate process's xol_area if necessary.
  984. * This area will be used for storing instructions for execution out of line.
  985. *
  986. * Returns the allocated area or NULL.
  987. */
  988. static struct xol_area *get_xol_area(void)
  989. {
  990. struct mm_struct *mm = current->mm;
  991. struct xol_area *area;
  992. if (!mm->uprobes_state.xol_area)
  993. __create_xol_area(0);
  994. area = mm->uprobes_state.xol_area;
  995. smp_read_barrier_depends(); /* pairs with wmb in xol_add_vma() */
  996. return area;
  997. }
  998. /*
  999. * uprobe_clear_state - Free the area allocated for slots.
  1000. */
  1001. void uprobe_clear_state(struct mm_struct *mm)
  1002. {
  1003. struct xol_area *area = mm->uprobes_state.xol_area;
  1004. if (!area)
  1005. return;
  1006. put_page(area->page);
  1007. kfree(area->bitmap);
  1008. kfree(area);
  1009. }
  1010. void uprobe_start_dup_mmap(void)
  1011. {
  1012. percpu_down_read(&dup_mmap_sem);
  1013. }
  1014. void uprobe_end_dup_mmap(void)
  1015. {
  1016. percpu_up_read(&dup_mmap_sem);
  1017. }
  1018. void uprobe_dup_mmap(struct mm_struct *oldmm, struct mm_struct *newmm)
  1019. {
  1020. newmm->uprobes_state.xol_area = NULL;
  1021. if (test_bit(MMF_HAS_UPROBES, &oldmm->flags)) {
  1022. set_bit(MMF_HAS_UPROBES, &newmm->flags);
  1023. /* unconditionally, dup_mmap() skips VM_DONTCOPY vmas */
  1024. set_bit(MMF_RECALC_UPROBES, &newmm->flags);
  1025. }
  1026. }
  1027. /*
  1028. * - search for a free slot.
  1029. */
  1030. static unsigned long xol_take_insn_slot(struct xol_area *area)
  1031. {
  1032. unsigned long slot_addr;
  1033. int slot_nr;
  1034. do {
  1035. slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE);
  1036. if (slot_nr < UINSNS_PER_PAGE) {
  1037. if (!test_and_set_bit(slot_nr, area->bitmap))
  1038. break;
  1039. slot_nr = UINSNS_PER_PAGE;
  1040. continue;
  1041. }
  1042. wait_event(area->wq, (atomic_read(&area->slot_count) < UINSNS_PER_PAGE));
  1043. } while (slot_nr >= UINSNS_PER_PAGE);
  1044. slot_addr = area->vaddr + (slot_nr * UPROBE_XOL_SLOT_BYTES);
  1045. atomic_inc(&area->slot_count);
  1046. return slot_addr;
  1047. }
  1048. /*
  1049. * xol_get_insn_slot - allocate a slot for xol.
  1050. * Returns the allocated slot address or 0.
  1051. */
  1052. static unsigned long xol_get_insn_slot(struct uprobe *uprobe)
  1053. {
  1054. struct xol_area *area;
  1055. unsigned long xol_vaddr;
  1056. area = get_xol_area();
  1057. if (!area)
  1058. return 0;
  1059. xol_vaddr = xol_take_insn_slot(area);
  1060. if (unlikely(!xol_vaddr))
  1061. return 0;
  1062. /* Initialize the slot */
  1063. copy_to_page(area->page, xol_vaddr,
  1064. uprobe->arch.ixol, sizeof(uprobe->arch.ixol));
  1065. /*
  1066. * We probably need flush_icache_user_range() but it needs vma.
  1067. * This should work on supported architectures too.
  1068. */
  1069. flush_dcache_page(area->page);
  1070. return xol_vaddr;
  1071. }
  1072. /*
  1073. * xol_free_insn_slot - If slot was earlier allocated by
  1074. * @xol_get_insn_slot(), make the slot available for
  1075. * subsequent requests.
  1076. */
  1077. static void xol_free_insn_slot(struct task_struct *tsk)
  1078. {
  1079. struct xol_area *area;
  1080. unsigned long vma_end;
  1081. unsigned long slot_addr;
  1082. if (!tsk->mm || !tsk->mm->uprobes_state.xol_area || !tsk->utask)
  1083. return;
  1084. slot_addr = tsk->utask->xol_vaddr;
  1085. if (unlikely(!slot_addr))
  1086. return;
  1087. area = tsk->mm->uprobes_state.xol_area;
  1088. vma_end = area->vaddr + PAGE_SIZE;
  1089. if (area->vaddr <= slot_addr && slot_addr < vma_end) {
  1090. unsigned long offset;
  1091. int slot_nr;
  1092. offset = slot_addr - area->vaddr;
  1093. slot_nr = offset / UPROBE_XOL_SLOT_BYTES;
  1094. if (slot_nr >= UINSNS_PER_PAGE)
  1095. return;
  1096. clear_bit(slot_nr, area->bitmap);
  1097. atomic_dec(&area->slot_count);
  1098. if (waitqueue_active(&area->wq))
  1099. wake_up(&area->wq);
  1100. tsk->utask->xol_vaddr = 0;
  1101. }
  1102. }
  1103. /**
  1104. * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
  1105. * @regs: Reflects the saved state of the task after it has hit a breakpoint
  1106. * instruction.
  1107. * Return the address of the breakpoint instruction.
  1108. */
  1109. unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs)
  1110. {
  1111. return instruction_pointer(regs) - UPROBE_SWBP_INSN_SIZE;
  1112. }
  1113. /*
  1114. * Called with no locks held.
  1115. * Called in context of a exiting or a exec-ing thread.
  1116. */
  1117. void uprobe_free_utask(struct task_struct *t)
  1118. {
  1119. struct uprobe_task *utask = t->utask;
  1120. struct return_instance *ri, *tmp;
  1121. if (!utask)
  1122. return;
  1123. if (utask->active_uprobe)
  1124. put_uprobe(utask->active_uprobe);
  1125. ri = utask->return_instances;
  1126. while (ri) {
  1127. tmp = ri;
  1128. ri = ri->next;
  1129. put_uprobe(tmp->uprobe);
  1130. kfree(tmp);
  1131. }
  1132. xol_free_insn_slot(t);
  1133. kfree(utask);
  1134. t->utask = NULL;
  1135. }
  1136. /*
  1137. * Allocate a uprobe_task object for the task if if necessary.
  1138. * Called when the thread hits a breakpoint.
  1139. *
  1140. * Returns:
  1141. * - pointer to new uprobe_task on success
  1142. * - NULL otherwise
  1143. */
  1144. static struct uprobe_task *get_utask(void)
  1145. {
  1146. if (!current->utask)
  1147. current->utask = kzalloc(sizeof(struct uprobe_task), GFP_KERNEL);
  1148. return current->utask;
  1149. }
  1150. static int dup_utask(struct task_struct *t, struct uprobe_task *o_utask)
  1151. {
  1152. struct uprobe_task *n_utask;
  1153. struct return_instance **p, *o, *n;
  1154. n_utask = kzalloc(sizeof(struct uprobe_task), GFP_KERNEL);
  1155. if (!n_utask)
  1156. return -ENOMEM;
  1157. t->utask = n_utask;
  1158. p = &n_utask->return_instances;
  1159. for (o = o_utask->return_instances; o; o = o->next) {
  1160. n = kmalloc(sizeof(struct return_instance), GFP_KERNEL);
  1161. if (!n)
  1162. return -ENOMEM;
  1163. *n = *o;
  1164. atomic_inc(&n->uprobe->ref);
  1165. n->next = NULL;
  1166. *p = n;
  1167. p = &n->next;
  1168. n_utask->depth++;
  1169. }
  1170. return 0;
  1171. }
  1172. static void uprobe_warn(struct task_struct *t, const char *msg)
  1173. {
  1174. pr_warn("uprobe: %s:%d failed to %s\n",
  1175. current->comm, current->pid, msg);
  1176. }
  1177. static void dup_xol_work(struct callback_head *work)
  1178. {
  1179. kfree(work);
  1180. if (current->flags & PF_EXITING)
  1181. return;
  1182. if (!__create_xol_area(current->utask->vaddr))
  1183. uprobe_warn(current, "dup xol area");
  1184. }
  1185. /*
  1186. * Called in context of a new clone/fork from copy_process.
  1187. */
  1188. void uprobe_copy_process(struct task_struct *t, unsigned long flags)
  1189. {
  1190. struct uprobe_task *utask = current->utask;
  1191. struct mm_struct *mm = current->mm;
  1192. struct callback_head *work;
  1193. struct xol_area *area;
  1194. t->utask = NULL;
  1195. if (!utask || !utask->return_instances)
  1196. return;
  1197. if (mm == t->mm && !(flags & CLONE_VFORK))
  1198. return;
  1199. if (dup_utask(t, utask))
  1200. return uprobe_warn(t, "dup ret instances");
  1201. /* The task can fork() after dup_xol_work() fails */
  1202. area = mm->uprobes_state.xol_area;
  1203. if (!area)
  1204. return uprobe_warn(t, "dup xol area");
  1205. if (mm == t->mm)
  1206. return;
  1207. /* TODO: move it into the union in uprobe_task */
  1208. work = kmalloc(sizeof(*work), GFP_KERNEL);
  1209. if (!work)
  1210. return uprobe_warn(t, "dup xol area");
  1211. t->utask->vaddr = area->vaddr;
  1212. init_task_work(work, dup_xol_work);
  1213. task_work_add(t, work, true);
  1214. }
  1215. /*
  1216. * Current area->vaddr notion assume the trampoline address is always
  1217. * equal area->vaddr.
  1218. *
  1219. * Returns -1 in case the xol_area is not allocated.
  1220. */
  1221. static unsigned long get_trampoline_vaddr(void)
  1222. {
  1223. struct xol_area *area;
  1224. unsigned long trampoline_vaddr = -1;
  1225. area = current->mm->uprobes_state.xol_area;
  1226. smp_read_barrier_depends();
  1227. if (area)
  1228. trampoline_vaddr = area->vaddr;
  1229. return trampoline_vaddr;
  1230. }
  1231. static void prepare_uretprobe(struct uprobe *uprobe, struct pt_regs *regs)
  1232. {
  1233. struct return_instance *ri;
  1234. struct uprobe_task *utask;
  1235. unsigned long orig_ret_vaddr, trampoline_vaddr;
  1236. bool chained = false;
  1237. if (!get_xol_area())
  1238. return;
  1239. utask = get_utask();
  1240. if (!utask)
  1241. return;
  1242. if (utask->depth >= MAX_URETPROBE_DEPTH) {
  1243. printk_ratelimited(KERN_INFO "uprobe: omit uretprobe due to"
  1244. " nestedness limit pid/tgid=%d/%d\n",
  1245. current->pid, current->tgid);
  1246. return;
  1247. }
  1248. ri = kzalloc(sizeof(struct return_instance), GFP_KERNEL);
  1249. if (!ri)
  1250. goto fail;
  1251. trampoline_vaddr = get_trampoline_vaddr();
  1252. orig_ret_vaddr = arch_uretprobe_hijack_return_addr(trampoline_vaddr, regs);
  1253. if (orig_ret_vaddr == -1)
  1254. goto fail;
  1255. /*
  1256. * We don't want to keep trampoline address in stack, rather keep the
  1257. * original return address of first caller thru all the consequent
  1258. * instances. This also makes breakpoint unwrapping easier.
  1259. */
  1260. if (orig_ret_vaddr == trampoline_vaddr) {
  1261. if (!utask->return_instances) {
  1262. /*
  1263. * This situation is not possible. Likely we have an
  1264. * attack from user-space.
  1265. */
  1266. pr_warn("uprobe: unable to set uretprobe pid/tgid=%d/%d\n",
  1267. current->pid, current->tgid);
  1268. goto fail;
  1269. }
  1270. chained = true;
  1271. orig_ret_vaddr = utask->return_instances->orig_ret_vaddr;
  1272. }
  1273. atomic_inc(&uprobe->ref);
  1274. ri->uprobe = uprobe;
  1275. ri->func = instruction_pointer(regs);
  1276. ri->orig_ret_vaddr = orig_ret_vaddr;
  1277. ri->chained = chained;
  1278. utask->depth++;
  1279. /* add instance to the stack */
  1280. ri->next = utask->return_instances;
  1281. utask->return_instances = ri;
  1282. return;
  1283. fail:
  1284. kfree(ri);
  1285. }
  1286. /* Prepare to single-step probed instruction out of line. */
  1287. static int
  1288. pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long bp_vaddr)
  1289. {
  1290. struct uprobe_task *utask;
  1291. unsigned long xol_vaddr;
  1292. int err;
  1293. utask = get_utask();
  1294. if (!utask)
  1295. return -ENOMEM;
  1296. xol_vaddr = xol_get_insn_slot(uprobe);
  1297. if (!xol_vaddr)
  1298. return -ENOMEM;
  1299. utask->xol_vaddr = xol_vaddr;
  1300. utask->vaddr = bp_vaddr;
  1301. err = arch_uprobe_pre_xol(&uprobe->arch, regs);
  1302. if (unlikely(err)) {
  1303. xol_free_insn_slot(current);
  1304. return err;
  1305. }
  1306. utask->active_uprobe = uprobe;
  1307. utask->state = UTASK_SSTEP;
  1308. return 0;
  1309. }
  1310. /*
  1311. * If we are singlestepping, then ensure this thread is not connected to
  1312. * non-fatal signals until completion of singlestep. When xol insn itself
  1313. * triggers the signal, restart the original insn even if the task is
  1314. * already SIGKILL'ed (since coredump should report the correct ip). This
  1315. * is even more important if the task has a handler for SIGSEGV/etc, The
  1316. * _same_ instruction should be repeated again after return from the signal
  1317. * handler, and SSTEP can never finish in this case.
  1318. */
  1319. bool uprobe_deny_signal(void)
  1320. {
  1321. struct task_struct *t = current;
  1322. struct uprobe_task *utask = t->utask;
  1323. if (likely(!utask || !utask->active_uprobe))
  1324. return false;
  1325. WARN_ON_ONCE(utask->state != UTASK_SSTEP);
  1326. if (signal_pending(t)) {
  1327. spin_lock_irq(&t->sighand->siglock);
  1328. clear_tsk_thread_flag(t, TIF_SIGPENDING);
  1329. spin_unlock_irq(&t->sighand->siglock);
  1330. if (__fatal_signal_pending(t) || arch_uprobe_xol_was_trapped(t)) {
  1331. utask->state = UTASK_SSTEP_TRAPPED;
  1332. set_tsk_thread_flag(t, TIF_UPROBE);
  1333. set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
  1334. }
  1335. }
  1336. return true;
  1337. }
  1338. /*
  1339. * Avoid singlestepping the original instruction if the original instruction
  1340. * is a NOP or can be emulated.
  1341. */
  1342. static bool can_skip_sstep(struct uprobe *uprobe, struct pt_regs *regs)
  1343. {
  1344. if (test_bit(UPROBE_SKIP_SSTEP, &uprobe->flags)) {
  1345. if (arch_uprobe_skip_sstep(&uprobe->arch, regs))
  1346. return true;
  1347. clear_bit(UPROBE_SKIP_SSTEP, &uprobe->flags);
  1348. }
  1349. return false;
  1350. }
  1351. static void mmf_recalc_uprobes(struct mm_struct *mm)
  1352. {
  1353. struct vm_area_struct *vma;
  1354. for (vma = mm->mmap; vma; vma = vma->vm_next) {
  1355. if (!valid_vma(vma, false))
  1356. continue;
  1357. /*
  1358. * This is not strictly accurate, we can race with
  1359. * uprobe_unregister() and see the already removed
  1360. * uprobe if delete_uprobe() was not yet called.
  1361. * Or this uprobe can be filtered out.
  1362. */
  1363. if (vma_has_uprobes(vma, vma->vm_start, vma->vm_end))
  1364. return;
  1365. }
  1366. clear_bit(MMF_HAS_UPROBES, &mm->flags);
  1367. }
  1368. static int is_trap_at_addr(struct mm_struct *mm, unsigned long vaddr)
  1369. {
  1370. struct page *page;
  1371. uprobe_opcode_t opcode;
  1372. int result;
  1373. pagefault_disable();
  1374. result = __copy_from_user_inatomic(&opcode, (void __user*)vaddr,
  1375. sizeof(opcode));
  1376. pagefault_enable();
  1377. if (likely(result == 0))
  1378. goto out;
  1379. result = get_user_pages(NULL, mm, vaddr, 1, 0, 1, &page, NULL);
  1380. if (result < 0)
  1381. return result;
  1382. copy_from_page(page, vaddr, &opcode, UPROBE_SWBP_INSN_SIZE);
  1383. put_page(page);
  1384. out:
  1385. /* This needs to return true for any variant of the trap insn */
  1386. return is_trap_insn(&opcode);
  1387. }
  1388. static struct uprobe *find_active_uprobe(unsigned long bp_vaddr, int *is_swbp)
  1389. {
  1390. struct mm_struct *mm = current->mm;
  1391. struct uprobe *uprobe = NULL;
  1392. struct vm_area_struct *vma;
  1393. down_read(&mm->mmap_sem);
  1394. vma = find_vma(mm, bp_vaddr);
  1395. if (vma && vma->vm_start <= bp_vaddr) {
  1396. if (valid_vma(vma, false)) {
  1397. struct inode *inode = file_inode(vma->vm_file);
  1398. loff_t offset = vaddr_to_offset(vma, bp_vaddr);
  1399. uprobe = find_uprobe(inode, offset);
  1400. }
  1401. if (!uprobe)
  1402. *is_swbp = is_trap_at_addr(mm, bp_vaddr);
  1403. } else {
  1404. *is_swbp = -EFAULT;
  1405. }
  1406. if (!uprobe && test_and_clear_bit(MMF_RECALC_UPROBES, &mm->flags))
  1407. mmf_recalc_uprobes(mm);
  1408. up_read(&mm->mmap_sem);
  1409. return uprobe;
  1410. }
  1411. static void handler_chain(struct uprobe *uprobe, struct pt_regs *regs)
  1412. {
  1413. struct uprobe_consumer *uc;
  1414. int remove = UPROBE_HANDLER_REMOVE;
  1415. bool need_prep = false; /* prepare return uprobe, when needed */
  1416. down_read(&uprobe->register_rwsem);
  1417. for (uc = uprobe->consumers; uc; uc = uc->next) {
  1418. int rc = 0;
  1419. if (uc->handler) {
  1420. rc = uc->handler(uc, regs);
  1421. WARN(rc & ~UPROBE_HANDLER_MASK,
  1422. "bad rc=0x%x from %pf()\n", rc, uc->handler);
  1423. }
  1424. if (uc->ret_handler)
  1425. need_prep = true;
  1426. remove &= rc;
  1427. }
  1428. if (need_prep && !remove)
  1429. prepare_uretprobe(uprobe, regs); /* put bp at return */
  1430. if (remove && uprobe->consumers) {
  1431. WARN_ON(!uprobe_is_active(uprobe));
  1432. unapply_uprobe(uprobe, current->mm);
  1433. }
  1434. up_read(&uprobe->register_rwsem);
  1435. }
  1436. static void
  1437. handle_uretprobe_chain(struct return_instance *ri, struct pt_regs *regs)
  1438. {
  1439. struct uprobe *uprobe = ri->uprobe;
  1440. struct uprobe_consumer *uc;
  1441. down_read(&uprobe->register_rwsem);
  1442. for (uc = uprobe->consumers; uc; uc = uc->next) {
  1443. if (uc->ret_handler)
  1444. uc->ret_handler(uc, ri->func, regs);
  1445. }
  1446. up_read(&uprobe->register_rwsem);
  1447. }
  1448. static bool handle_trampoline(struct pt_regs *regs)
  1449. {
  1450. struct uprobe_task *utask;
  1451. struct return_instance *ri, *tmp;
  1452. bool chained;
  1453. utask = current->utask;
  1454. if (!utask)
  1455. return false;
  1456. ri = utask->return_instances;
  1457. if (!ri)
  1458. return false;
  1459. /*
  1460. * TODO: we should throw out return_instance's invalidated by
  1461. * longjmp(), currently we assume that the probed function always
  1462. * returns.
  1463. */
  1464. instruction_pointer_set(regs, ri->orig_ret_vaddr);
  1465. for (;;) {
  1466. handle_uretprobe_chain(ri, regs);
  1467. chained = ri->chained;
  1468. put_uprobe(ri->uprobe);
  1469. tmp = ri;
  1470. ri = ri->next;
  1471. kfree(tmp);
  1472. utask->depth--;
  1473. if (!chained)
  1474. break;
  1475. BUG_ON(!ri);
  1476. }
  1477. utask->return_instances = ri;
  1478. return true;
  1479. }
  1480. /*
  1481. * Run handler and ask thread to singlestep.
  1482. * Ensure all non-fatal signals cannot interrupt thread while it singlesteps.
  1483. */
  1484. static void handle_swbp(struct pt_regs *regs)
  1485. {
  1486. struct uprobe *uprobe;
  1487. unsigned long bp_vaddr;
  1488. int uninitialized_var(is_swbp);
  1489. bp_vaddr = uprobe_get_swbp_addr(regs);
  1490. if (bp_vaddr == get_trampoline_vaddr()) {
  1491. if (handle_trampoline(regs))
  1492. return;
  1493. pr_warn("uprobe: unable to handle uretprobe pid/tgid=%d/%d\n",
  1494. current->pid, current->tgid);
  1495. }
  1496. uprobe = find_active_uprobe(bp_vaddr, &is_swbp);
  1497. if (!uprobe) {
  1498. if (is_swbp > 0) {
  1499. /* No matching uprobe; signal SIGTRAP. */
  1500. send_sig(SIGTRAP, current, 0);
  1501. } else {
  1502. /*
  1503. * Either we raced with uprobe_unregister() or we can't
  1504. * access this memory. The latter is only possible if
  1505. * another thread plays with our ->mm. In both cases
  1506. * we can simply restart. If this vma was unmapped we
  1507. * can pretend this insn was not executed yet and get
  1508. * the (correct) SIGSEGV after restart.
  1509. */
  1510. instruction_pointer_set(regs, bp_vaddr);
  1511. }
  1512. return;
  1513. }
  1514. /* change it in advance for ->handler() and restart */
  1515. instruction_pointer_set(regs, bp_vaddr);
  1516. /*
  1517. * TODO: move copy_insn/etc into _register and remove this hack.
  1518. * After we hit the bp, _unregister + _register can install the
  1519. * new and not-yet-analyzed uprobe at the same address, restart.
  1520. */
  1521. smp_rmb(); /* pairs with wmb() in install_breakpoint() */
  1522. if (unlikely(!test_bit(UPROBE_COPY_INSN, &uprobe->flags)))
  1523. goto out;
  1524. handler_chain(uprobe, regs);
  1525. if (can_skip_sstep(uprobe, regs))
  1526. goto out;
  1527. if (!pre_ssout(uprobe, regs, bp_vaddr))
  1528. return;
  1529. /* can_skip_sstep() succeeded, or restart if can't singlestep */
  1530. out:
  1531. put_uprobe(uprobe);
  1532. }
  1533. /*
  1534. * Perform required fix-ups and disable singlestep.
  1535. * Allow pending signals to take effect.
  1536. */
  1537. static void handle_singlestep(struct uprobe_task *utask, struct pt_regs *regs)
  1538. {
  1539. struct uprobe *uprobe;
  1540. uprobe = utask->active_uprobe;
  1541. if (utask->state == UTASK_SSTEP_ACK)
  1542. arch_uprobe_post_xol(&uprobe->arch, regs);
  1543. else if (utask->state == UTASK_SSTEP_TRAPPED)
  1544. arch_uprobe_abort_xol(&uprobe->arch, regs);
  1545. else
  1546. WARN_ON_ONCE(1);
  1547. put_uprobe(uprobe);
  1548. utask->active_uprobe = NULL;
  1549. utask->state = UTASK_RUNNING;
  1550. xol_free_insn_slot(current);
  1551. spin_lock_irq(&current->sighand->siglock);
  1552. recalc_sigpending(); /* see uprobe_deny_signal() */
  1553. spin_unlock_irq(&current->sighand->siglock);
  1554. }
  1555. /*
  1556. * On breakpoint hit, breakpoint notifier sets the TIF_UPROBE flag and
  1557. * allows the thread to return from interrupt. After that handle_swbp()
  1558. * sets utask->active_uprobe.
  1559. *
  1560. * On singlestep exception, singlestep notifier sets the TIF_UPROBE flag
  1561. * and allows the thread to return from interrupt.
  1562. *
  1563. * While returning to userspace, thread notices the TIF_UPROBE flag and calls
  1564. * uprobe_notify_resume().
  1565. */
  1566. void uprobe_notify_resume(struct pt_regs *regs)
  1567. {
  1568. struct uprobe_task *utask;
  1569. clear_thread_flag(TIF_UPROBE);
  1570. utask = current->utask;
  1571. if (utask && utask->active_uprobe)
  1572. handle_singlestep(utask, regs);
  1573. else
  1574. handle_swbp(regs);
  1575. }
  1576. /*
  1577. * uprobe_pre_sstep_notifier gets called from interrupt context as part of
  1578. * notifier mechanism. Set TIF_UPROBE flag and indicate breakpoint hit.
  1579. */
  1580. int uprobe_pre_sstep_notifier(struct pt_regs *regs)
  1581. {
  1582. if (!current->mm)
  1583. return 0;
  1584. if (!test_bit(MMF_HAS_UPROBES, &current->mm->flags) &&
  1585. (!current->utask || !current->utask->return_instances))
  1586. return 0;
  1587. set_thread_flag(TIF_UPROBE);
  1588. return 1;
  1589. }
  1590. /*
  1591. * uprobe_post_sstep_notifier gets called in interrupt context as part of notifier
  1592. * mechanism. Set TIF_UPROBE flag and indicate completion of singlestep.
  1593. */
  1594. int uprobe_post_sstep_notifier(struct pt_regs *regs)
  1595. {
  1596. struct uprobe_task *utask = current->utask;
  1597. if (!current->mm || !utask || !utask->active_uprobe)
  1598. /* task is currently not uprobed */
  1599. return 0;
  1600. utask->state = UTASK_SSTEP_ACK;
  1601. set_thread_flag(TIF_UPROBE);
  1602. return 1;
  1603. }
  1604. static struct notifier_block uprobe_exception_nb = {
  1605. .notifier_call = arch_uprobe_exception_notify,
  1606. .priority = INT_MAX-1, /* notified after kprobes, kgdb */
  1607. };
  1608. static int __init init_uprobes(void)
  1609. {
  1610. int i;
  1611. for (i = 0; i < UPROBES_HASH_SZ; i++)
  1612. mutex_init(&uprobes_mmap_mutex[i]);
  1613. if (percpu_init_rwsem(&dup_mmap_sem))
  1614. return -ENOMEM;
  1615. return register_die_notifier(&uprobe_exception_nb);
  1616. }
  1617. __initcall(init_uprobes);