uprobes.c 39 KB

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