kmmio.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Support for MMIO probes.
  3. * Benefit many code from kprobes
  4. * (C) 2002 Louis Zhuang <[email protected]>.
  5. * 2007 Alexander Eichner
  6. * 2008 Pekka Paalanen <[email protected]>
  7. */
  8. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  9. #include <linux/list.h>
  10. #include <linux/rculist.h>
  11. #include <linux/spinlock.h>
  12. #include <linux/hash.h>
  13. #include <linux/export.h>
  14. #include <linux/kernel.h>
  15. #include <linux/uaccess.h>
  16. #include <linux/ptrace.h>
  17. #include <linux/preempt.h>
  18. #include <linux/percpu.h>
  19. #include <linux/kdebug.h>
  20. #include <linux/mutex.h>
  21. #include <linux/io.h>
  22. #include <linux/slab.h>
  23. #include <asm/cacheflush.h>
  24. #include <asm/tlbflush.h>
  25. #include <linux/errno.h>
  26. #include <asm/debugreg.h>
  27. #include <linux/mmiotrace.h>
  28. #define KMMIO_PAGE_HASH_BITS 4
  29. #define KMMIO_PAGE_TABLE_SIZE (1 << KMMIO_PAGE_HASH_BITS)
  30. struct kmmio_fault_page {
  31. struct list_head list;
  32. struct kmmio_fault_page *release_next;
  33. unsigned long addr; /* the requested address */
  34. pteval_t old_presence; /* page presence prior to arming */
  35. bool armed;
  36. /*
  37. * Number of times this page has been registered as a part
  38. * of a probe. If zero, page is disarmed and this may be freed.
  39. * Used only by writers (RCU) and post_kmmio_handler().
  40. * Protected by kmmio_lock, when linked into kmmio_page_table.
  41. */
  42. int count;
  43. bool scheduled_for_release;
  44. };
  45. struct kmmio_delayed_release {
  46. struct rcu_head rcu;
  47. struct kmmio_fault_page *release_list;
  48. };
  49. struct kmmio_context {
  50. struct kmmio_fault_page *fpage;
  51. struct kmmio_probe *probe;
  52. unsigned long saved_flags;
  53. unsigned long addr;
  54. int active;
  55. };
  56. static DEFINE_SPINLOCK(kmmio_lock);
  57. /* Protected by kmmio_lock */
  58. unsigned int kmmio_count;
  59. /* Read-protected by RCU, write-protected by kmmio_lock. */
  60. static struct list_head kmmio_page_table[KMMIO_PAGE_TABLE_SIZE];
  61. static LIST_HEAD(kmmio_probes);
  62. static struct list_head *kmmio_page_list(unsigned long addr)
  63. {
  64. unsigned int l;
  65. pte_t *pte = lookup_address(addr, &l);
  66. if (!pte)
  67. return NULL;
  68. addr &= page_level_mask(l);
  69. return &kmmio_page_table[hash_long(addr, KMMIO_PAGE_HASH_BITS)];
  70. }
  71. /* Accessed per-cpu */
  72. static DEFINE_PER_CPU(struct kmmio_context, kmmio_ctx);
  73. /*
  74. * this is basically a dynamic stabbing problem:
  75. * Could use the existing prio tree code or
  76. * Possible better implementations:
  77. * The Interval Skip List: A Data Structure for Finding All Intervals That
  78. * Overlap a Point (might be simple)
  79. * Space Efficient Dynamic Stabbing with Fast Queries - Mikkel Thorup
  80. */
  81. /* Get the kmmio at this addr (if any). You must be holding RCU read lock. */
  82. static struct kmmio_probe *get_kmmio_probe(unsigned long addr)
  83. {
  84. struct kmmio_probe *p;
  85. list_for_each_entry_rcu(p, &kmmio_probes, list) {
  86. if (addr >= p->addr && addr < (p->addr + p->len))
  87. return p;
  88. }
  89. return NULL;
  90. }
  91. /* You must be holding RCU read lock. */
  92. static struct kmmio_fault_page *get_kmmio_fault_page(unsigned long addr)
  93. {
  94. struct list_head *head;
  95. struct kmmio_fault_page *f;
  96. unsigned int l;
  97. pte_t *pte = lookup_address(addr, &l);
  98. if (!pte)
  99. return NULL;
  100. addr &= page_level_mask(l);
  101. head = kmmio_page_list(addr);
  102. list_for_each_entry_rcu(f, head, list) {
  103. if (f->addr == addr)
  104. return f;
  105. }
  106. return NULL;
  107. }
  108. static void clear_pmd_presence(pmd_t *pmd, bool clear, pmdval_t *old)
  109. {
  110. pmd_t new_pmd;
  111. pmdval_t v = pmd_val(*pmd);
  112. if (clear) {
  113. *old = v;
  114. new_pmd = pmd_mkinvalid(*pmd);
  115. } else {
  116. /* Presume this has been called with clear==true previously */
  117. new_pmd = __pmd(*old);
  118. }
  119. set_pmd(pmd, new_pmd);
  120. }
  121. static void clear_pte_presence(pte_t *pte, bool clear, pteval_t *old)
  122. {
  123. pteval_t v = pte_val(*pte);
  124. if (clear) {
  125. *old = v;
  126. /* Nothing should care about address */
  127. pte_clear(&init_mm, 0, pte);
  128. } else {
  129. /* Presume this has been called with clear==true previously */
  130. set_pte_atomic(pte, __pte(*old));
  131. }
  132. }
  133. static int clear_page_presence(struct kmmio_fault_page *f, bool clear)
  134. {
  135. unsigned int level;
  136. pte_t *pte = lookup_address(f->addr, &level);
  137. if (!pte) {
  138. pr_err("no pte for addr 0x%08lx\n", f->addr);
  139. return -1;
  140. }
  141. switch (level) {
  142. case PG_LEVEL_2M:
  143. clear_pmd_presence((pmd_t *)pte, clear, &f->old_presence);
  144. break;
  145. case PG_LEVEL_4K:
  146. clear_pte_presence(pte, clear, &f->old_presence);
  147. break;
  148. default:
  149. pr_err("unexpected page level 0x%x.\n", level);
  150. return -1;
  151. }
  152. flush_tlb_one_kernel(f->addr);
  153. return 0;
  154. }
  155. /*
  156. * Mark the given page as not present. Access to it will trigger a fault.
  157. *
  158. * Struct kmmio_fault_page is protected by RCU and kmmio_lock, but the
  159. * protection is ignored here. RCU read lock is assumed held, so the struct
  160. * will not disappear unexpectedly. Furthermore, the caller must guarantee,
  161. * that double arming the same virtual address (page) cannot occur.
  162. *
  163. * Double disarming on the other hand is allowed, and may occur when a fault
  164. * and mmiotrace shutdown happen simultaneously.
  165. */
  166. static int arm_kmmio_fault_page(struct kmmio_fault_page *f)
  167. {
  168. int ret;
  169. WARN_ONCE(f->armed, KERN_ERR pr_fmt("kmmio page already armed.\n"));
  170. if (f->armed) {
  171. pr_warn("double-arm: addr 0x%08lx, ref %d, old %d\n",
  172. f->addr, f->count, !!f->old_presence);
  173. }
  174. ret = clear_page_presence(f, true);
  175. WARN_ONCE(ret < 0, KERN_ERR pr_fmt("arming at 0x%08lx failed.\n"),
  176. f->addr);
  177. f->armed = true;
  178. return ret;
  179. }
  180. /** Restore the given page to saved presence state. */
  181. static void disarm_kmmio_fault_page(struct kmmio_fault_page *f)
  182. {
  183. int ret = clear_page_presence(f, false);
  184. WARN_ONCE(ret < 0,
  185. KERN_ERR "kmmio disarming at 0x%08lx failed.\n", f->addr);
  186. f->armed = false;
  187. }
  188. /*
  189. * This is being called from do_page_fault().
  190. *
  191. * We may be in an interrupt or a critical section. Also prefecthing may
  192. * trigger a page fault. We may be in the middle of process switch.
  193. * We cannot take any locks, because we could be executing especially
  194. * within a kmmio critical section.
  195. *
  196. * Local interrupts are disabled, so preemption cannot happen.
  197. * Do not enable interrupts, do not sleep, and watch out for other CPUs.
  198. */
  199. /*
  200. * Interrupts are disabled on entry as trap3 is an interrupt gate
  201. * and they remain disabled throughout this function.
  202. */
  203. int kmmio_handler(struct pt_regs *regs, unsigned long addr)
  204. {
  205. struct kmmio_context *ctx;
  206. struct kmmio_fault_page *faultpage;
  207. int ret = 0; /* default to fault not handled */
  208. unsigned long page_base = addr;
  209. unsigned int l;
  210. pte_t *pte = lookup_address(addr, &l);
  211. if (!pte)
  212. return -EINVAL;
  213. page_base &= page_level_mask(l);
  214. /*
  215. * Preemption is now disabled to prevent process switch during
  216. * single stepping. We can only handle one active kmmio trace
  217. * per cpu, so ensure that we finish it before something else
  218. * gets to run. We also hold the RCU read lock over single
  219. * stepping to avoid looking up the probe and kmmio_fault_page
  220. * again.
  221. */
  222. preempt_disable();
  223. rcu_read_lock();
  224. faultpage = get_kmmio_fault_page(page_base);
  225. if (!faultpage) {
  226. /*
  227. * Either this page fault is not caused by kmmio, or
  228. * another CPU just pulled the kmmio probe from under
  229. * our feet. The latter case should not be possible.
  230. */
  231. goto no_kmmio;
  232. }
  233. ctx = this_cpu_ptr(&kmmio_ctx);
  234. if (ctx->active) {
  235. if (page_base == ctx->addr) {
  236. /*
  237. * A second fault on the same page means some other
  238. * condition needs handling by do_page_fault(), the
  239. * page really not being present is the most common.
  240. */
  241. pr_debug("secondary hit for 0x%08lx CPU %d.\n",
  242. addr, smp_processor_id());
  243. if (!faultpage->old_presence)
  244. pr_info("unexpected secondary hit for address 0x%08lx on CPU %d.\n",
  245. addr, smp_processor_id());
  246. } else {
  247. /*
  248. * Prevent overwriting already in-flight context.
  249. * This should not happen, let's hope disarming at
  250. * least prevents a panic.
  251. */
  252. pr_emerg("recursive probe hit on CPU %d, for address 0x%08lx. Ignoring.\n",
  253. smp_processor_id(), addr);
  254. pr_emerg("previous hit was at 0x%08lx.\n", ctx->addr);
  255. disarm_kmmio_fault_page(faultpage);
  256. }
  257. goto no_kmmio;
  258. }
  259. ctx->active++;
  260. ctx->fpage = faultpage;
  261. ctx->probe = get_kmmio_probe(page_base);
  262. ctx->saved_flags = (regs->flags & (X86_EFLAGS_TF | X86_EFLAGS_IF));
  263. ctx->addr = page_base;
  264. if (ctx->probe && ctx->probe->pre_handler)
  265. ctx->probe->pre_handler(ctx->probe, regs, addr);
  266. /*
  267. * Enable single-stepping and disable interrupts for the faulting
  268. * context. Local interrupts must not get enabled during stepping.
  269. */
  270. regs->flags |= X86_EFLAGS_TF;
  271. regs->flags &= ~X86_EFLAGS_IF;
  272. /* Now we set present bit in PTE and single step. */
  273. disarm_kmmio_fault_page(ctx->fpage);
  274. /*
  275. * If another cpu accesses the same page while we are stepping,
  276. * the access will not be caught. It will simply succeed and the
  277. * only downside is we lose the event. If this becomes a problem,
  278. * the user should drop to single cpu before tracing.
  279. */
  280. return 1; /* fault handled */
  281. no_kmmio:
  282. rcu_read_unlock();
  283. preempt_enable_no_resched();
  284. return ret;
  285. }
  286. /*
  287. * Interrupts are disabled on entry as trap1 is an interrupt gate
  288. * and they remain disabled throughout this function.
  289. * This must always get called as the pair to kmmio_handler().
  290. */
  291. static int post_kmmio_handler(unsigned long condition, struct pt_regs *regs)
  292. {
  293. int ret = 0;
  294. struct kmmio_context *ctx = this_cpu_ptr(&kmmio_ctx);
  295. if (!ctx->active) {
  296. /*
  297. * debug traps without an active context are due to either
  298. * something external causing them (f.e. using a debugger while
  299. * mmio tracing enabled), or erroneous behaviour
  300. */
  301. pr_warn("unexpected debug trap on CPU %d.\n", smp_processor_id());
  302. goto out;
  303. }
  304. if (ctx->probe && ctx->probe->post_handler)
  305. ctx->probe->post_handler(ctx->probe, condition, regs);
  306. /* Prevent racing against release_kmmio_fault_page(). */
  307. spin_lock(&kmmio_lock);
  308. if (ctx->fpage->count)
  309. arm_kmmio_fault_page(ctx->fpage);
  310. spin_unlock(&kmmio_lock);
  311. regs->flags &= ~X86_EFLAGS_TF;
  312. regs->flags |= ctx->saved_flags;
  313. /* These were acquired in kmmio_handler(). */
  314. ctx->active--;
  315. BUG_ON(ctx->active);
  316. rcu_read_unlock();
  317. preempt_enable_no_resched();
  318. /*
  319. * if somebody else is singlestepping across a probe point, flags
  320. * will have TF set, in which case, continue the remaining processing
  321. * of do_debug, as if this is not a probe hit.
  322. */
  323. if (!(regs->flags & X86_EFLAGS_TF))
  324. ret = 1;
  325. out:
  326. return ret;
  327. }
  328. /* You must be holding kmmio_lock. */
  329. static int add_kmmio_fault_page(unsigned long addr)
  330. {
  331. struct kmmio_fault_page *f;
  332. f = get_kmmio_fault_page(addr);
  333. if (f) {
  334. if (!f->count)
  335. arm_kmmio_fault_page(f);
  336. f->count++;
  337. return 0;
  338. }
  339. f = kzalloc(sizeof(*f), GFP_ATOMIC);
  340. if (!f)
  341. return -1;
  342. f->count = 1;
  343. f->addr = addr;
  344. if (arm_kmmio_fault_page(f)) {
  345. kfree(f);
  346. return -1;
  347. }
  348. list_add_rcu(&f->list, kmmio_page_list(f->addr));
  349. return 0;
  350. }
  351. /* You must be holding kmmio_lock. */
  352. static void release_kmmio_fault_page(unsigned long addr,
  353. struct kmmio_fault_page **release_list)
  354. {
  355. struct kmmio_fault_page *f;
  356. f = get_kmmio_fault_page(addr);
  357. if (!f)
  358. return;
  359. f->count--;
  360. BUG_ON(f->count < 0);
  361. if (!f->count) {
  362. disarm_kmmio_fault_page(f);
  363. if (!f->scheduled_for_release) {
  364. f->release_next = *release_list;
  365. *release_list = f;
  366. f->scheduled_for_release = true;
  367. }
  368. }
  369. }
  370. /*
  371. * With page-unaligned ioremaps, one or two armed pages may contain
  372. * addresses from outside the intended mapping. Events for these addresses
  373. * are currently silently dropped. The events may result only from programming
  374. * mistakes by accessing addresses before the beginning or past the end of a
  375. * mapping.
  376. */
  377. int register_kmmio_probe(struct kmmio_probe *p)
  378. {
  379. unsigned long flags;
  380. int ret = 0;
  381. unsigned long size = 0;
  382. unsigned long addr = p->addr & PAGE_MASK;
  383. const unsigned long size_lim = p->len + (p->addr & ~PAGE_MASK);
  384. unsigned int l;
  385. pte_t *pte;
  386. spin_lock_irqsave(&kmmio_lock, flags);
  387. if (get_kmmio_probe(addr)) {
  388. ret = -EEXIST;
  389. goto out;
  390. }
  391. pte = lookup_address(addr, &l);
  392. if (!pte) {
  393. ret = -EINVAL;
  394. goto out;
  395. }
  396. kmmio_count++;
  397. list_add_rcu(&p->list, &kmmio_probes);
  398. while (size < size_lim) {
  399. if (add_kmmio_fault_page(addr + size))
  400. pr_err("Unable to set page fault.\n");
  401. size += page_level_size(l);
  402. }
  403. out:
  404. spin_unlock_irqrestore(&kmmio_lock, flags);
  405. /*
  406. * XXX: What should I do here?
  407. * Here was a call to global_flush_tlb(), but it does not exist
  408. * anymore. It seems it's not needed after all.
  409. */
  410. return ret;
  411. }
  412. EXPORT_SYMBOL(register_kmmio_probe);
  413. static void rcu_free_kmmio_fault_pages(struct rcu_head *head)
  414. {
  415. struct kmmio_delayed_release *dr = container_of(
  416. head,
  417. struct kmmio_delayed_release,
  418. rcu);
  419. struct kmmio_fault_page *f = dr->release_list;
  420. while (f) {
  421. struct kmmio_fault_page *next = f->release_next;
  422. BUG_ON(f->count);
  423. kfree(f);
  424. f = next;
  425. }
  426. kfree(dr);
  427. }
  428. static void remove_kmmio_fault_pages(struct rcu_head *head)
  429. {
  430. struct kmmio_delayed_release *dr =
  431. container_of(head, struct kmmio_delayed_release, rcu);
  432. struct kmmio_fault_page *f = dr->release_list;
  433. struct kmmio_fault_page **prevp = &dr->release_list;
  434. unsigned long flags;
  435. spin_lock_irqsave(&kmmio_lock, flags);
  436. while (f) {
  437. if (!f->count) {
  438. list_del_rcu(&f->list);
  439. prevp = &f->release_next;
  440. } else {
  441. *prevp = f->release_next;
  442. f->release_next = NULL;
  443. f->scheduled_for_release = false;
  444. }
  445. f = *prevp;
  446. }
  447. spin_unlock_irqrestore(&kmmio_lock, flags);
  448. /* This is the real RCU destroy call. */
  449. call_rcu(&dr->rcu, rcu_free_kmmio_fault_pages);
  450. }
  451. /*
  452. * Remove a kmmio probe. You have to synchronize_rcu() before you can be
  453. * sure that the callbacks will not be called anymore. Only after that
  454. * you may actually release your struct kmmio_probe.
  455. *
  456. * Unregistering a kmmio fault page has three steps:
  457. * 1. release_kmmio_fault_page()
  458. * Disarm the page, wait a grace period to let all faults finish.
  459. * 2. remove_kmmio_fault_pages()
  460. * Remove the pages from kmmio_page_table.
  461. * 3. rcu_free_kmmio_fault_pages()
  462. * Actually free the kmmio_fault_page structs as with RCU.
  463. */
  464. void unregister_kmmio_probe(struct kmmio_probe *p)
  465. {
  466. unsigned long flags;
  467. unsigned long size = 0;
  468. unsigned long addr = p->addr & PAGE_MASK;
  469. const unsigned long size_lim = p->len + (p->addr & ~PAGE_MASK);
  470. struct kmmio_fault_page *release_list = NULL;
  471. struct kmmio_delayed_release *drelease;
  472. unsigned int l;
  473. pte_t *pte;
  474. pte = lookup_address(addr, &l);
  475. if (!pte)
  476. return;
  477. spin_lock_irqsave(&kmmio_lock, flags);
  478. while (size < size_lim) {
  479. release_kmmio_fault_page(addr + size, &release_list);
  480. size += page_level_size(l);
  481. }
  482. list_del_rcu(&p->list);
  483. kmmio_count--;
  484. spin_unlock_irqrestore(&kmmio_lock, flags);
  485. if (!release_list)
  486. return;
  487. drelease = kmalloc(sizeof(*drelease), GFP_ATOMIC);
  488. if (!drelease) {
  489. pr_crit("leaking kmmio_fault_page objects.\n");
  490. return;
  491. }
  492. drelease->release_list = release_list;
  493. /*
  494. * This is not really RCU here. We have just disarmed a set of
  495. * pages so that they cannot trigger page faults anymore. However,
  496. * we cannot remove the pages from kmmio_page_table,
  497. * because a probe hit might be in flight on another CPU. The
  498. * pages are collected into a list, and they will be removed from
  499. * kmmio_page_table when it is certain that no probe hit related to
  500. * these pages can be in flight. RCU grace period sounds like a
  501. * good choice.
  502. *
  503. * If we removed the pages too early, kmmio page fault handler might
  504. * not find the respective kmmio_fault_page and determine it's not
  505. * a kmmio fault, when it actually is. This would lead to madness.
  506. */
  507. call_rcu(&drelease->rcu, remove_kmmio_fault_pages);
  508. }
  509. EXPORT_SYMBOL(unregister_kmmio_probe);
  510. static int
  511. kmmio_die_notifier(struct notifier_block *nb, unsigned long val, void *args)
  512. {
  513. struct die_args *arg = args;
  514. unsigned long* dr6_p = (unsigned long *)ERR_PTR(arg->err);
  515. if (val == DIE_DEBUG && (*dr6_p & DR_STEP))
  516. if (post_kmmio_handler(*dr6_p, arg->regs) == 1) {
  517. /*
  518. * Reset the BS bit in dr6 (pointed by args->err) to
  519. * denote completion of processing
  520. */
  521. *dr6_p &= ~DR_STEP;
  522. return NOTIFY_STOP;
  523. }
  524. return NOTIFY_DONE;
  525. }
  526. static struct notifier_block nb_die = {
  527. .notifier_call = kmmio_die_notifier
  528. };
  529. int kmmio_init(void)
  530. {
  531. int i;
  532. for (i = 0; i < KMMIO_PAGE_TABLE_SIZE; i++)
  533. INIT_LIST_HEAD(&kmmio_page_table[i]);
  534. return register_die_notifier(&nb_die);
  535. }
  536. void kmmio_cleanup(void)
  537. {
  538. int i;
  539. unregister_die_notifier(&nb_die);
  540. for (i = 0; i < KMMIO_PAGE_TABLE_SIZE; i++) {
  541. WARN_ONCE(!list_empty(&kmmio_page_table[i]),
  542. KERN_ERR "kmmio_page_table not empty at cleanup, any further tracing will leak memory.\n");
  543. }
  544. }