uprobes.c 39 KB

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