uprobes.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671
  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, unsigned long offset)
  489. {
  490. struct page *page;
  491. void *vaddr;
  492. unsigned long off1;
  493. unsigned long idx;
  494. if (!filp)
  495. return -EINVAL;
  496. if (!mapping->a_ops->readpage)
  497. return -EIO;
  498. idx = (unsigned long)(offset >> PAGE_CACHE_SHIFT);
  499. off1 = 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 + off1, 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, loff_t vaddr)
  561. {
  562. unsigned long addr;
  563. int ret;
  564. /*
  565. * If probe is being deleted, unregister thread could be done with
  566. * the vma-rmap-walk through. Adding a probe now can be fatal since
  567. * nobody will be able to cleanup. Also we could be from fork or
  568. * mremap path, where the probe might have already been inserted.
  569. * Hence behave as if probe already existed.
  570. */
  571. if (!uprobe->consumers)
  572. return -EEXIST;
  573. addr = (unsigned long)vaddr;
  574. if (!(uprobe->flags & UPROBE_COPY_INSN)) {
  575. ret = copy_insn(uprobe, vma->vm_file);
  576. if (ret)
  577. return ret;
  578. if (is_swbp_insn((uprobe_opcode_t *)uprobe->arch.insn))
  579. return -ENOTSUPP;
  580. ret = arch_uprobe_analyze_insn(&uprobe->arch, mm, addr);
  581. if (ret)
  582. return ret;
  583. /* write_opcode() assumes we don't cross page boundary */
  584. BUG_ON((uprobe->offset & ~PAGE_MASK) +
  585. UPROBE_SWBP_INSN_SIZE > PAGE_SIZE);
  586. uprobe->flags |= UPROBE_COPY_INSN;
  587. }
  588. /*
  589. * Ideally, should be updating the probe count after the breakpoint
  590. * has been successfully inserted. However a thread could hit the
  591. * breakpoint we just inserted even before the probe count is
  592. * incremented. If this is the first breakpoint placed, breakpoint
  593. * notifier might ignore uprobes and pass the trap to the thread.
  594. * Hence increment before and decrement on failure.
  595. */
  596. atomic_inc(&mm->uprobes_state.count);
  597. ret = set_swbp(&uprobe->arch, mm, addr);
  598. if (ret)
  599. atomic_dec(&mm->uprobes_state.count);
  600. return ret;
  601. }
  602. static void
  603. remove_breakpoint(struct uprobe *uprobe, struct mm_struct *mm, loff_t vaddr)
  604. {
  605. if (!set_orig_insn(&uprobe->arch, mm, (unsigned long)vaddr, true))
  606. atomic_dec(&mm->uprobes_state.count);
  607. }
  608. /*
  609. * There could be threads that have already hit the breakpoint. They
  610. * will recheck the current insn and restart if find_uprobe() fails.
  611. * See find_active_uprobe().
  612. */
  613. static void delete_uprobe(struct uprobe *uprobe)
  614. {
  615. unsigned long flags;
  616. spin_lock_irqsave(&uprobes_treelock, flags);
  617. rb_erase(&uprobe->rb_node, &uprobes_tree);
  618. spin_unlock_irqrestore(&uprobes_treelock, flags);
  619. iput(uprobe->inode);
  620. put_uprobe(uprobe);
  621. atomic_dec(&uprobe_events);
  622. }
  623. struct map_info {
  624. struct map_info *next;
  625. struct mm_struct *mm;
  626. loff_t vaddr;
  627. };
  628. static inline struct map_info *free_map_info(struct map_info *info)
  629. {
  630. struct map_info *next = info->next;
  631. kfree(info);
  632. return next;
  633. }
  634. static struct map_info *
  635. build_map_info(struct address_space *mapping, loff_t offset, bool is_register)
  636. {
  637. unsigned long pgoff = offset >> PAGE_SHIFT;
  638. struct prio_tree_iter iter;
  639. struct vm_area_struct *vma;
  640. struct map_info *curr = NULL;
  641. struct map_info *prev = NULL;
  642. struct map_info *info;
  643. int more = 0;
  644. again:
  645. mutex_lock(&mapping->i_mmap_mutex);
  646. vma_prio_tree_foreach(vma, &iter, &mapping->i_mmap, pgoff, pgoff) {
  647. if (!valid_vma(vma, is_register))
  648. continue;
  649. if (!prev && !more) {
  650. /*
  651. * Needs GFP_NOWAIT to avoid i_mmap_mutex recursion through
  652. * reclaim. This is optimistic, no harm done if it fails.
  653. */
  654. prev = kmalloc(sizeof(struct map_info),
  655. GFP_NOWAIT | __GFP_NOMEMALLOC | __GFP_NOWARN);
  656. if (prev)
  657. prev->next = NULL;
  658. }
  659. if (!prev) {
  660. more++;
  661. continue;
  662. }
  663. if (!atomic_inc_not_zero(&vma->vm_mm->mm_users))
  664. continue;
  665. info = prev;
  666. prev = prev->next;
  667. info->next = curr;
  668. curr = info;
  669. info->mm = vma->vm_mm;
  670. info->vaddr = vma_address(vma, offset);
  671. }
  672. mutex_unlock(&mapping->i_mmap_mutex);
  673. if (!more)
  674. goto out;
  675. prev = curr;
  676. while (curr) {
  677. mmput(curr->mm);
  678. curr = curr->next;
  679. }
  680. do {
  681. info = kmalloc(sizeof(struct map_info), GFP_KERNEL);
  682. if (!info) {
  683. curr = ERR_PTR(-ENOMEM);
  684. goto out;
  685. }
  686. info->next = prev;
  687. prev = info;
  688. } while (--more);
  689. goto again;
  690. out:
  691. while (prev)
  692. prev = free_map_info(prev);
  693. return curr;
  694. }
  695. static int register_for_each_vma(struct uprobe *uprobe, bool is_register)
  696. {
  697. struct map_info *info;
  698. int err = 0;
  699. info = build_map_info(uprobe->inode->i_mapping,
  700. uprobe->offset, is_register);
  701. if (IS_ERR(info))
  702. return PTR_ERR(info);
  703. while (info) {
  704. struct mm_struct *mm = info->mm;
  705. struct vm_area_struct *vma;
  706. loff_t vaddr;
  707. if (err)
  708. goto free;
  709. down_write(&mm->mmap_sem);
  710. vma = find_vma(mm, (unsigned long)info->vaddr);
  711. if (!vma || !valid_vma(vma, is_register))
  712. goto unlock;
  713. vaddr = vma_address(vma, uprobe->offset);
  714. if (vma->vm_file->f_mapping->host != uprobe->inode ||
  715. vaddr != info->vaddr)
  716. goto unlock;
  717. if (is_register) {
  718. err = install_breakpoint(uprobe, mm, vma, info->vaddr);
  719. /*
  720. * We can race against uprobe_mmap(), see the
  721. * comment near uprobe_hash().
  722. */
  723. if (err == -EEXIST)
  724. err = 0;
  725. } else {
  726. remove_breakpoint(uprobe, mm, info->vaddr);
  727. }
  728. unlock:
  729. up_write(&mm->mmap_sem);
  730. free:
  731. mmput(mm);
  732. info = free_map_info(info);
  733. }
  734. return err;
  735. }
  736. static int __uprobe_register(struct uprobe *uprobe)
  737. {
  738. return register_for_each_vma(uprobe, true);
  739. }
  740. static void __uprobe_unregister(struct uprobe *uprobe)
  741. {
  742. if (!register_for_each_vma(uprobe, false))
  743. delete_uprobe(uprobe);
  744. /* TODO : cant unregister? schedule a worker thread */
  745. }
  746. /*
  747. * uprobe_register - register a probe
  748. * @inode: the file in which the probe has to be placed.
  749. * @offset: offset from the start of the file.
  750. * @uc: information on howto handle the probe..
  751. *
  752. * Apart from the access refcount, uprobe_register() takes a creation
  753. * refcount (thro alloc_uprobe) if and only if this @uprobe is getting
  754. * inserted into the rbtree (i.e first consumer for a @inode:@offset
  755. * tuple). Creation refcount stops uprobe_unregister from freeing the
  756. * @uprobe even before the register operation is complete. Creation
  757. * refcount is released when the last @uc for the @uprobe
  758. * unregisters.
  759. *
  760. * Return errno if it cannot successully install probes
  761. * else return 0 (success)
  762. */
  763. int uprobe_register(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
  764. {
  765. struct uprobe *uprobe;
  766. int ret;
  767. if (!inode || !uc || uc->next)
  768. return -EINVAL;
  769. if (offset > i_size_read(inode))
  770. return -EINVAL;
  771. ret = 0;
  772. mutex_lock(uprobes_hash(inode));
  773. uprobe = alloc_uprobe(inode, offset);
  774. if (uprobe && !consumer_add(uprobe, uc)) {
  775. ret = __uprobe_register(uprobe);
  776. if (ret) {
  777. uprobe->consumers = NULL;
  778. __uprobe_unregister(uprobe);
  779. } else {
  780. uprobe->flags |= UPROBE_RUN_HANDLER;
  781. }
  782. }
  783. mutex_unlock(uprobes_hash(inode));
  784. put_uprobe(uprobe);
  785. return ret;
  786. }
  787. /*
  788. * uprobe_unregister - unregister a already registered probe.
  789. * @inode: the file in which the probe has to be removed.
  790. * @offset: offset from the start of the file.
  791. * @uc: identify which probe if multiple probes are colocated.
  792. */
  793. void uprobe_unregister(struct inode *inode, loff_t offset, struct uprobe_consumer *uc)
  794. {
  795. struct uprobe *uprobe;
  796. if (!inode || !uc)
  797. return;
  798. uprobe = find_uprobe(inode, offset);
  799. if (!uprobe)
  800. return;
  801. mutex_lock(uprobes_hash(inode));
  802. if (consumer_del(uprobe, uc)) {
  803. if (!uprobe->consumers) {
  804. __uprobe_unregister(uprobe);
  805. uprobe->flags &= ~UPROBE_RUN_HANDLER;
  806. }
  807. }
  808. mutex_unlock(uprobes_hash(inode));
  809. if (uprobe)
  810. put_uprobe(uprobe);
  811. }
  812. /*
  813. * Of all the nodes that correspond to the given inode, return the node
  814. * with the least offset.
  815. */
  816. static struct rb_node *find_least_offset_node(struct inode *inode)
  817. {
  818. struct uprobe u = { .inode = inode, .offset = 0};
  819. struct rb_node *n = uprobes_tree.rb_node;
  820. struct rb_node *close_node = NULL;
  821. struct uprobe *uprobe;
  822. int match;
  823. while (n) {
  824. uprobe = rb_entry(n, struct uprobe, rb_node);
  825. match = match_uprobe(&u, uprobe);
  826. if (uprobe->inode == inode)
  827. close_node = n;
  828. if (!match)
  829. return close_node;
  830. if (match < 0)
  831. n = n->rb_left;
  832. else
  833. n = n->rb_right;
  834. }
  835. return close_node;
  836. }
  837. /*
  838. * For a given inode, build a list of probes that need to be inserted.
  839. */
  840. static void build_probe_list(struct inode *inode, struct list_head *head)
  841. {
  842. struct uprobe *uprobe;
  843. unsigned long flags;
  844. struct rb_node *n;
  845. spin_lock_irqsave(&uprobes_treelock, flags);
  846. n = find_least_offset_node(inode);
  847. for (; n; n = rb_next(n)) {
  848. uprobe = rb_entry(n, struct uprobe, rb_node);
  849. if (uprobe->inode != inode)
  850. break;
  851. list_add(&uprobe->pending_list, head);
  852. atomic_inc(&uprobe->ref);
  853. }
  854. spin_unlock_irqrestore(&uprobes_treelock, flags);
  855. }
  856. /*
  857. * Called from mmap_region.
  858. * called with mm->mmap_sem acquired.
  859. *
  860. * Return -ve no if we fail to insert probes and we cannot
  861. * bail-out.
  862. * Return 0 otherwise. i.e:
  863. *
  864. * - successful insertion of probes
  865. * - (or) no possible probes to be inserted.
  866. * - (or) insertion of probes failed but we can bail-out.
  867. */
  868. int uprobe_mmap(struct vm_area_struct *vma)
  869. {
  870. struct list_head tmp_list;
  871. struct uprobe *uprobe;
  872. struct inode *inode;
  873. int ret, count;
  874. if (!atomic_read(&uprobe_events) || !valid_vma(vma, true))
  875. return 0;
  876. inode = vma->vm_file->f_mapping->host;
  877. if (!inode)
  878. return 0;
  879. INIT_LIST_HEAD(&tmp_list);
  880. mutex_lock(uprobes_mmap_hash(inode));
  881. build_probe_list(inode, &tmp_list);
  882. ret = 0;
  883. count = 0;
  884. list_for_each_entry(uprobe, &tmp_list, pending_list) {
  885. loff_t vaddr;
  886. if (!ret) {
  887. vaddr = vma_address(vma, uprobe->offset);
  888. if (vaddr < vma->vm_start || vaddr >= vma->vm_end) {
  889. put_uprobe(uprobe);
  890. continue;
  891. }
  892. ret = install_breakpoint(uprobe, vma->vm_mm, vma, vaddr);
  893. /*
  894. * We can race against uprobe_register(), see the
  895. * comment near uprobe_hash().
  896. */
  897. if (ret == -EEXIST) {
  898. ret = 0;
  899. if (!is_swbp_at_addr(vma->vm_mm, vaddr))
  900. continue;
  901. /*
  902. * Unable to insert a breakpoint, but
  903. * breakpoint lies underneath. Increment the
  904. * probe count.
  905. */
  906. atomic_inc(&vma->vm_mm->uprobes_state.count);
  907. }
  908. if (!ret)
  909. count++;
  910. }
  911. put_uprobe(uprobe);
  912. }
  913. mutex_unlock(uprobes_mmap_hash(inode));
  914. if (ret)
  915. atomic_sub(count, &vma->vm_mm->uprobes_state.count);
  916. return ret;
  917. }
  918. /*
  919. * Called in context of a munmap of a vma.
  920. */
  921. void uprobe_munmap(struct vm_area_struct *vma, unsigned long start, unsigned long end)
  922. {
  923. struct list_head tmp_list;
  924. struct uprobe *uprobe;
  925. struct inode *inode;
  926. if (!atomic_read(&uprobe_events) || !valid_vma(vma, false))
  927. return;
  928. if (!atomic_read(&vma->vm_mm->uprobes_state.count))
  929. return;
  930. inode = vma->vm_file->f_mapping->host;
  931. if (!inode)
  932. return;
  933. INIT_LIST_HEAD(&tmp_list);
  934. mutex_lock(uprobes_mmap_hash(inode));
  935. build_probe_list(inode, &tmp_list);
  936. list_for_each_entry(uprobe, &tmp_list, pending_list) {
  937. loff_t vaddr;
  938. vaddr = vma_address(vma, uprobe->offset);
  939. if (vaddr >= start && vaddr < end) {
  940. /*
  941. * An unregister could have removed the probe before
  942. * unmap. So check before we decrement the count.
  943. */
  944. if (is_swbp_at_addr(vma->vm_mm, vaddr) == 1)
  945. atomic_dec(&vma->vm_mm->uprobes_state.count);
  946. }
  947. put_uprobe(uprobe);
  948. }
  949. mutex_unlock(uprobes_mmap_hash(inode));
  950. }
  951. /* Slot allocation for XOL */
  952. static int xol_add_vma(struct xol_area *area)
  953. {
  954. struct mm_struct *mm;
  955. int ret;
  956. area->page = alloc_page(GFP_HIGHUSER);
  957. if (!area->page)
  958. return -ENOMEM;
  959. ret = -EALREADY;
  960. mm = current->mm;
  961. down_write(&mm->mmap_sem);
  962. if (mm->uprobes_state.xol_area)
  963. goto fail;
  964. ret = -ENOMEM;
  965. /* Try to map as high as possible, this is only a hint. */
  966. area->vaddr = get_unmapped_area(NULL, TASK_SIZE - PAGE_SIZE, PAGE_SIZE, 0, 0);
  967. if (area->vaddr & ~PAGE_MASK) {
  968. ret = area->vaddr;
  969. goto fail;
  970. }
  971. ret = install_special_mapping(mm, area->vaddr, PAGE_SIZE,
  972. VM_EXEC|VM_MAYEXEC|VM_DONTCOPY|VM_IO, &area->page);
  973. if (ret)
  974. goto fail;
  975. smp_wmb(); /* pairs with get_xol_area() */
  976. mm->uprobes_state.xol_area = area;
  977. ret = 0;
  978. fail:
  979. up_write(&mm->mmap_sem);
  980. if (ret)
  981. __free_page(area->page);
  982. return ret;
  983. }
  984. static struct xol_area *get_xol_area(struct mm_struct *mm)
  985. {
  986. struct xol_area *area;
  987. area = mm->uprobes_state.xol_area;
  988. smp_read_barrier_depends(); /* pairs with wmb in xol_add_vma() */
  989. return area;
  990. }
  991. /*
  992. * xol_alloc_area - Allocate process's xol_area.
  993. * This area will be used for storing instructions for execution out of
  994. * line.
  995. *
  996. * Returns the allocated area or NULL.
  997. */
  998. static struct xol_area *xol_alloc_area(void)
  999. {
  1000. struct xol_area *area;
  1001. area = kzalloc(sizeof(*area), GFP_KERNEL);
  1002. if (unlikely(!area))
  1003. return NULL;
  1004. area->bitmap = kzalloc(BITS_TO_LONGS(UINSNS_PER_PAGE) * sizeof(long), GFP_KERNEL);
  1005. if (!area->bitmap)
  1006. goto fail;
  1007. init_waitqueue_head(&area->wq);
  1008. if (!xol_add_vma(area))
  1009. return area;
  1010. fail:
  1011. kfree(area->bitmap);
  1012. kfree(area);
  1013. return get_xol_area(current->mm);
  1014. }
  1015. /*
  1016. * uprobe_clear_state - Free the area allocated for slots.
  1017. */
  1018. void uprobe_clear_state(struct mm_struct *mm)
  1019. {
  1020. struct xol_area *area = mm->uprobes_state.xol_area;
  1021. if (!area)
  1022. return;
  1023. put_page(area->page);
  1024. kfree(area->bitmap);
  1025. kfree(area);
  1026. }
  1027. /*
  1028. * uprobe_reset_state - Free the area allocated for slots.
  1029. */
  1030. void uprobe_reset_state(struct mm_struct *mm)
  1031. {
  1032. mm->uprobes_state.xol_area = NULL;
  1033. atomic_set(&mm->uprobes_state.count, 0);
  1034. }
  1035. /*
  1036. * - search for a free slot.
  1037. */
  1038. static unsigned long xol_take_insn_slot(struct xol_area *area)
  1039. {
  1040. unsigned long slot_addr;
  1041. int slot_nr;
  1042. do {
  1043. slot_nr = find_first_zero_bit(area->bitmap, UINSNS_PER_PAGE);
  1044. if (slot_nr < UINSNS_PER_PAGE) {
  1045. if (!test_and_set_bit(slot_nr, area->bitmap))
  1046. break;
  1047. slot_nr = UINSNS_PER_PAGE;
  1048. continue;
  1049. }
  1050. wait_event(area->wq, (atomic_read(&area->slot_count) < UINSNS_PER_PAGE));
  1051. } while (slot_nr >= UINSNS_PER_PAGE);
  1052. slot_addr = area->vaddr + (slot_nr * UPROBE_XOL_SLOT_BYTES);
  1053. atomic_inc(&area->slot_count);
  1054. return slot_addr;
  1055. }
  1056. /*
  1057. * xol_get_insn_slot - If was not allocated a slot, then
  1058. * allocate a slot.
  1059. * Returns the allocated slot address or 0.
  1060. */
  1061. static unsigned long xol_get_insn_slot(struct uprobe *uprobe, unsigned long slot_addr)
  1062. {
  1063. struct xol_area *area;
  1064. unsigned long offset;
  1065. void *vaddr;
  1066. area = get_xol_area(current->mm);
  1067. if (!area) {
  1068. area = xol_alloc_area();
  1069. if (!area)
  1070. return 0;
  1071. }
  1072. current->utask->xol_vaddr = xol_take_insn_slot(area);
  1073. /*
  1074. * Initialize the slot if xol_vaddr points to valid
  1075. * instruction slot.
  1076. */
  1077. if (unlikely(!current->utask->xol_vaddr))
  1078. return 0;
  1079. current->utask->vaddr = slot_addr;
  1080. offset = current->utask->xol_vaddr & ~PAGE_MASK;
  1081. vaddr = kmap_atomic(area->page);
  1082. memcpy(vaddr + offset, uprobe->arch.insn, MAX_UINSN_BYTES);
  1083. kunmap_atomic(vaddr);
  1084. return current->utask->xol_vaddr;
  1085. }
  1086. /*
  1087. * xol_free_insn_slot - If slot was earlier allocated by
  1088. * @xol_get_insn_slot(), make the slot available for
  1089. * subsequent requests.
  1090. */
  1091. static void xol_free_insn_slot(struct task_struct *tsk)
  1092. {
  1093. struct xol_area *area;
  1094. unsigned long vma_end;
  1095. unsigned long slot_addr;
  1096. if (!tsk->mm || !tsk->mm->uprobes_state.xol_area || !tsk->utask)
  1097. return;
  1098. slot_addr = tsk->utask->xol_vaddr;
  1099. if (unlikely(!slot_addr || IS_ERR_VALUE(slot_addr)))
  1100. return;
  1101. area = tsk->mm->uprobes_state.xol_area;
  1102. vma_end = area->vaddr + PAGE_SIZE;
  1103. if (area->vaddr <= slot_addr && slot_addr < vma_end) {
  1104. unsigned long offset;
  1105. int slot_nr;
  1106. offset = slot_addr - area->vaddr;
  1107. slot_nr = offset / UPROBE_XOL_SLOT_BYTES;
  1108. if (slot_nr >= UINSNS_PER_PAGE)
  1109. return;
  1110. clear_bit(slot_nr, area->bitmap);
  1111. atomic_dec(&area->slot_count);
  1112. if (waitqueue_active(&area->wq))
  1113. wake_up(&area->wq);
  1114. tsk->utask->xol_vaddr = 0;
  1115. }
  1116. }
  1117. /**
  1118. * uprobe_get_swbp_addr - compute address of swbp given post-swbp regs
  1119. * @regs: Reflects the saved state of the task after it has hit a breakpoint
  1120. * instruction.
  1121. * Return the address of the breakpoint instruction.
  1122. */
  1123. unsigned long __weak uprobe_get_swbp_addr(struct pt_regs *regs)
  1124. {
  1125. return instruction_pointer(regs) - UPROBE_SWBP_INSN_SIZE;
  1126. }
  1127. /*
  1128. * Called with no locks held.
  1129. * Called in context of a exiting or a exec-ing thread.
  1130. */
  1131. void uprobe_free_utask(struct task_struct *t)
  1132. {
  1133. struct uprobe_task *utask = t->utask;
  1134. if (!utask)
  1135. return;
  1136. if (utask->active_uprobe)
  1137. put_uprobe(utask->active_uprobe);
  1138. xol_free_insn_slot(t);
  1139. kfree(utask);
  1140. t->utask = NULL;
  1141. }
  1142. /*
  1143. * Called in context of a new clone/fork from copy_process.
  1144. */
  1145. void uprobe_copy_process(struct task_struct *t)
  1146. {
  1147. t->utask = NULL;
  1148. }
  1149. /*
  1150. * Allocate a uprobe_task object for the task.
  1151. * Called when the thread hits a breakpoint for the first time.
  1152. *
  1153. * Returns:
  1154. * - pointer to new uprobe_task on success
  1155. * - NULL otherwise
  1156. */
  1157. static struct uprobe_task *add_utask(void)
  1158. {
  1159. struct uprobe_task *utask;
  1160. utask = kzalloc(sizeof *utask, GFP_KERNEL);
  1161. if (unlikely(!utask))
  1162. return NULL;
  1163. utask->active_uprobe = NULL;
  1164. current->utask = utask;
  1165. return utask;
  1166. }
  1167. /* Prepare to single-step probed instruction out of line. */
  1168. static int
  1169. pre_ssout(struct uprobe *uprobe, struct pt_regs *regs, unsigned long vaddr)
  1170. {
  1171. if (xol_get_insn_slot(uprobe, vaddr) && !arch_uprobe_pre_xol(&uprobe->arch, regs))
  1172. return 0;
  1173. return -EFAULT;
  1174. }
  1175. /*
  1176. * If we are singlestepping, then ensure this thread is not connected to
  1177. * non-fatal signals until completion of singlestep. When xol insn itself
  1178. * triggers the signal, restart the original insn even if the task is
  1179. * already SIGKILL'ed (since coredump should report the correct ip). This
  1180. * is even more important if the task has a handler for SIGSEGV/etc, The
  1181. * _same_ instruction should be repeated again after return from the signal
  1182. * handler, and SSTEP can never finish in this case.
  1183. */
  1184. bool uprobe_deny_signal(void)
  1185. {
  1186. struct task_struct *t = current;
  1187. struct uprobe_task *utask = t->utask;
  1188. if (likely(!utask || !utask->active_uprobe))
  1189. return false;
  1190. WARN_ON_ONCE(utask->state != UTASK_SSTEP);
  1191. if (signal_pending(t)) {
  1192. spin_lock_irq(&t->sighand->siglock);
  1193. clear_tsk_thread_flag(t, TIF_SIGPENDING);
  1194. spin_unlock_irq(&t->sighand->siglock);
  1195. if (__fatal_signal_pending(t) || arch_uprobe_xol_was_trapped(t)) {
  1196. utask->state = UTASK_SSTEP_TRAPPED;
  1197. set_tsk_thread_flag(t, TIF_UPROBE);
  1198. set_tsk_thread_flag(t, TIF_NOTIFY_RESUME);
  1199. }
  1200. }
  1201. return true;
  1202. }
  1203. /*
  1204. * Avoid singlestepping the original instruction if the original instruction
  1205. * is a NOP or can be emulated.
  1206. */
  1207. static bool can_skip_sstep(struct uprobe *uprobe, struct pt_regs *regs)
  1208. {
  1209. if (arch_uprobe_skip_sstep(&uprobe->arch, regs))
  1210. return true;
  1211. uprobe->flags &= ~UPROBE_SKIP_SSTEP;
  1212. return false;
  1213. }
  1214. static struct uprobe *find_active_uprobe(unsigned long bp_vaddr, int *is_swbp)
  1215. {
  1216. struct mm_struct *mm = current->mm;
  1217. struct uprobe *uprobe = NULL;
  1218. struct vm_area_struct *vma;
  1219. down_read(&mm->mmap_sem);
  1220. vma = find_vma(mm, bp_vaddr);
  1221. if (vma && vma->vm_start <= bp_vaddr) {
  1222. if (valid_vma(vma, false)) {
  1223. struct inode *inode;
  1224. loff_t offset;
  1225. inode = vma->vm_file->f_mapping->host;
  1226. offset = bp_vaddr - vma->vm_start;
  1227. offset += (vma->vm_pgoff << PAGE_SHIFT);
  1228. uprobe = find_uprobe(inode, offset);
  1229. }
  1230. if (!uprobe)
  1231. *is_swbp = is_swbp_at_addr(mm, bp_vaddr);
  1232. } else {
  1233. *is_swbp = -EFAULT;
  1234. }
  1235. up_read(&mm->mmap_sem);
  1236. return uprobe;
  1237. }
  1238. /*
  1239. * Run handler and ask thread to singlestep.
  1240. * Ensure all non-fatal signals cannot interrupt thread while it singlesteps.
  1241. */
  1242. static void handle_swbp(struct pt_regs *regs)
  1243. {
  1244. struct uprobe_task *utask;
  1245. struct uprobe *uprobe;
  1246. unsigned long bp_vaddr;
  1247. int uninitialized_var(is_swbp);
  1248. bp_vaddr = uprobe_get_swbp_addr(regs);
  1249. uprobe = find_active_uprobe(bp_vaddr, &is_swbp);
  1250. if (!uprobe) {
  1251. if (is_swbp > 0) {
  1252. /* No matching uprobe; signal SIGTRAP. */
  1253. send_sig(SIGTRAP, current, 0);
  1254. } else {
  1255. /*
  1256. * Either we raced with uprobe_unregister() or we can't
  1257. * access this memory. The latter is only possible if
  1258. * another thread plays with our ->mm. In both cases
  1259. * we can simply restart. If this vma was unmapped we
  1260. * can pretend this insn was not executed yet and get
  1261. * the (correct) SIGSEGV after restart.
  1262. */
  1263. instruction_pointer_set(regs, bp_vaddr);
  1264. }
  1265. return;
  1266. }
  1267. utask = current->utask;
  1268. if (!utask) {
  1269. utask = add_utask();
  1270. /* Cannot allocate; re-execute the instruction. */
  1271. if (!utask)
  1272. goto cleanup_ret;
  1273. }
  1274. utask->active_uprobe = uprobe;
  1275. handler_chain(uprobe, regs);
  1276. if (uprobe->flags & UPROBE_SKIP_SSTEP && can_skip_sstep(uprobe, regs))
  1277. goto cleanup_ret;
  1278. utask->state = UTASK_SSTEP;
  1279. if (!pre_ssout(uprobe, regs, bp_vaddr)) {
  1280. user_enable_single_step(current);
  1281. return;
  1282. }
  1283. cleanup_ret:
  1284. if (utask) {
  1285. utask->active_uprobe = NULL;
  1286. utask->state = UTASK_RUNNING;
  1287. }
  1288. if (uprobe) {
  1289. if (!(uprobe->flags & UPROBE_SKIP_SSTEP))
  1290. /*
  1291. * cannot singlestep; cannot skip instruction;
  1292. * re-execute the instruction.
  1293. */
  1294. instruction_pointer_set(regs, bp_vaddr);
  1295. put_uprobe(uprobe);
  1296. }
  1297. }
  1298. /*
  1299. * Perform required fix-ups and disable singlestep.
  1300. * Allow pending signals to take effect.
  1301. */
  1302. static void handle_singlestep(struct uprobe_task *utask, struct pt_regs *regs)
  1303. {
  1304. struct uprobe *uprobe;
  1305. uprobe = utask->active_uprobe;
  1306. if (utask->state == UTASK_SSTEP_ACK)
  1307. arch_uprobe_post_xol(&uprobe->arch, regs);
  1308. else if (utask->state == UTASK_SSTEP_TRAPPED)
  1309. arch_uprobe_abort_xol(&uprobe->arch, regs);
  1310. else
  1311. WARN_ON_ONCE(1);
  1312. put_uprobe(uprobe);
  1313. utask->active_uprobe = NULL;
  1314. utask->state = UTASK_RUNNING;
  1315. user_disable_single_step(current);
  1316. xol_free_insn_slot(current);
  1317. spin_lock_irq(&current->sighand->siglock);
  1318. recalc_sigpending(); /* see uprobe_deny_signal() */
  1319. spin_unlock_irq(&current->sighand->siglock);
  1320. }
  1321. /*
  1322. * On breakpoint hit, breakpoint notifier sets the TIF_UPROBE flag. (and on
  1323. * subsequent probe hits on the thread sets the state to UTASK_BP_HIT) and
  1324. * allows the thread to return from interrupt.
  1325. *
  1326. * On singlestep exception, singlestep notifier sets the TIF_UPROBE flag and
  1327. * also sets the state to UTASK_SSTEP_ACK and allows the thread to return from
  1328. * interrupt.
  1329. *
  1330. * While returning to userspace, thread notices the TIF_UPROBE flag and calls
  1331. * uprobe_notify_resume().
  1332. */
  1333. void uprobe_notify_resume(struct pt_regs *regs)
  1334. {
  1335. struct uprobe_task *utask;
  1336. utask = current->utask;
  1337. if (!utask || utask->state == UTASK_BP_HIT)
  1338. handle_swbp(regs);
  1339. else
  1340. handle_singlestep(utask, regs);
  1341. }
  1342. /*
  1343. * uprobe_pre_sstep_notifier gets called from interrupt context as part of
  1344. * notifier mechanism. Set TIF_UPROBE flag and indicate breakpoint hit.
  1345. */
  1346. int uprobe_pre_sstep_notifier(struct pt_regs *regs)
  1347. {
  1348. struct uprobe_task *utask;
  1349. if (!current->mm || !atomic_read(&current->mm->uprobes_state.count))
  1350. /* task is currently not uprobed */
  1351. return 0;
  1352. utask = current->utask;
  1353. if (utask)
  1354. utask->state = UTASK_BP_HIT;
  1355. set_thread_flag(TIF_UPROBE);
  1356. return 1;
  1357. }
  1358. /*
  1359. * uprobe_post_sstep_notifier gets called in interrupt context as part of notifier
  1360. * mechanism. Set TIF_UPROBE flag and indicate completion of singlestep.
  1361. */
  1362. int uprobe_post_sstep_notifier(struct pt_regs *regs)
  1363. {
  1364. struct uprobe_task *utask = current->utask;
  1365. if (!current->mm || !utask || !utask->active_uprobe)
  1366. /* task is currently not uprobed */
  1367. return 0;
  1368. utask->state = UTASK_SSTEP_ACK;
  1369. set_thread_flag(TIF_UPROBE);
  1370. return 1;
  1371. }
  1372. static struct notifier_block uprobe_exception_nb = {
  1373. .notifier_call = arch_uprobe_exception_notify,
  1374. .priority = INT_MAX-1, /* notified after kprobes, kgdb */
  1375. };
  1376. static int __init init_uprobes(void)
  1377. {
  1378. int i;
  1379. for (i = 0; i < UPROBES_HASH_SZ; i++) {
  1380. mutex_init(&uprobes_mutex[i]);
  1381. mutex_init(&uprobes_mmap_mutex[i]);
  1382. }
  1383. return register_die_notifier(&uprobe_exception_nb);
  1384. }
  1385. module_init(init_uprobes);
  1386. static void __exit exit_uprobes(void)
  1387. {
  1388. }
  1389. module_exit(exit_uprobes);