context.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2012 Regents of the University of California
  4. * Copyright (C) 2017 SiFive
  5. * Copyright (C) 2021 Western Digital Corporation or its affiliates.
  6. */
  7. #include <linux/bitops.h>
  8. #include <linux/cpumask.h>
  9. #include <linux/mm.h>
  10. #include <linux/percpu.h>
  11. #include <linux/slab.h>
  12. #include <linux/spinlock.h>
  13. #include <linux/static_key.h>
  14. #include <asm/tlbflush.h>
  15. #include <asm/cacheflush.h>
  16. #include <asm/mmu_context.h>
  17. #include <asm/switch_to.h>
  18. #ifdef CONFIG_MMU
  19. DEFINE_STATIC_KEY_FALSE(use_asid_allocator);
  20. static unsigned long num_asids;
  21. static atomic_long_t current_version;
  22. static DEFINE_RAW_SPINLOCK(context_lock);
  23. static cpumask_t context_tlb_flush_pending;
  24. static unsigned long *context_asid_map;
  25. static DEFINE_PER_CPU(atomic_long_t, active_context);
  26. static DEFINE_PER_CPU(unsigned long, reserved_context);
  27. static bool check_update_reserved_context(unsigned long cntx,
  28. unsigned long newcntx)
  29. {
  30. int cpu;
  31. bool hit = false;
  32. /*
  33. * Iterate over the set of reserved CONTEXT looking for a match.
  34. * If we find one, then we can update our mm to use new CONTEXT
  35. * (i.e. the same CONTEXT in the current_version) but we can't
  36. * exit the loop early, since we need to ensure that all copies
  37. * of the old CONTEXT are updated to reflect the mm. Failure to do
  38. * so could result in us missing the reserved CONTEXT in a future
  39. * version.
  40. */
  41. for_each_possible_cpu(cpu) {
  42. if (per_cpu(reserved_context, cpu) == cntx) {
  43. hit = true;
  44. per_cpu(reserved_context, cpu) = newcntx;
  45. }
  46. }
  47. return hit;
  48. }
  49. static void __flush_context(void)
  50. {
  51. int i;
  52. unsigned long cntx;
  53. /* Must be called with context_lock held */
  54. lockdep_assert_held(&context_lock);
  55. /* Update the list of reserved ASIDs and the ASID bitmap. */
  56. bitmap_zero(context_asid_map, num_asids);
  57. /* Mark already active ASIDs as used */
  58. for_each_possible_cpu(i) {
  59. cntx = atomic_long_xchg_relaxed(&per_cpu(active_context, i), 0);
  60. /*
  61. * If this CPU has already been through a rollover, but
  62. * hasn't run another task in the meantime, we must preserve
  63. * its reserved CONTEXT, as this is the only trace we have of
  64. * the process it is still running.
  65. */
  66. if (cntx == 0)
  67. cntx = per_cpu(reserved_context, i);
  68. __set_bit(cntx2asid(cntx), context_asid_map);
  69. per_cpu(reserved_context, i) = cntx;
  70. }
  71. /* Mark ASID #0 as used because it is used at boot-time */
  72. __set_bit(0, context_asid_map);
  73. /* Queue a TLB invalidation for each CPU on next context-switch */
  74. cpumask_setall(&context_tlb_flush_pending);
  75. }
  76. static unsigned long __new_context(struct mm_struct *mm)
  77. {
  78. static u32 cur_idx = 1;
  79. unsigned long cntx = atomic_long_read(&mm->context.id);
  80. unsigned long asid, ver = atomic_long_read(&current_version);
  81. /* Must be called with context_lock held */
  82. lockdep_assert_held(&context_lock);
  83. if (cntx != 0) {
  84. unsigned long newcntx = ver | cntx2asid(cntx);
  85. /*
  86. * If our current CONTEXT was active during a rollover, we
  87. * can continue to use it and this was just a false alarm.
  88. */
  89. if (check_update_reserved_context(cntx, newcntx))
  90. return newcntx;
  91. /*
  92. * We had a valid CONTEXT in a previous life, so try to
  93. * re-use it if possible.
  94. */
  95. if (!__test_and_set_bit(cntx2asid(cntx), context_asid_map))
  96. return newcntx;
  97. }
  98. /*
  99. * Allocate a free ASID. If we can't find one then increment
  100. * current_version and flush all ASIDs.
  101. */
  102. asid = find_next_zero_bit(context_asid_map, num_asids, cur_idx);
  103. if (asid != num_asids)
  104. goto set_asid;
  105. /* We're out of ASIDs, so increment current_version */
  106. ver = atomic_long_add_return_relaxed(BIT(SATP_ASID_BITS), &current_version);
  107. /* Flush everything */
  108. __flush_context();
  109. /* We have more ASIDs than CPUs, so this will always succeed */
  110. asid = find_next_zero_bit(context_asid_map, num_asids, 1);
  111. set_asid:
  112. __set_bit(asid, context_asid_map);
  113. cur_idx = asid;
  114. return asid | ver;
  115. }
  116. static void set_mm_asid(struct mm_struct *mm, unsigned int cpu)
  117. {
  118. unsigned long flags;
  119. bool need_flush_tlb = false;
  120. unsigned long cntx, old_active_cntx;
  121. cntx = atomic_long_read(&mm->context.id);
  122. /*
  123. * If our active_context is non-zero and the context matches the
  124. * current_version, then we update the active_context entry with a
  125. * relaxed cmpxchg.
  126. *
  127. * Following is how we handle racing with a concurrent rollover:
  128. *
  129. * - We get a zero back from the cmpxchg and end up waiting on the
  130. * lock. Taking the lock synchronises with the rollover and so
  131. * we are forced to see the updated verion.
  132. *
  133. * - We get a valid context back from the cmpxchg then we continue
  134. * using old ASID because __flush_context() would have marked ASID
  135. * of active_context as used and next context switch we will
  136. * allocate new context.
  137. */
  138. old_active_cntx = atomic_long_read(&per_cpu(active_context, cpu));
  139. if (old_active_cntx &&
  140. (cntx2version(cntx) == atomic_long_read(&current_version)) &&
  141. atomic_long_cmpxchg_relaxed(&per_cpu(active_context, cpu),
  142. old_active_cntx, cntx))
  143. goto switch_mm_fast;
  144. raw_spin_lock_irqsave(&context_lock, flags);
  145. /* Check that our ASID belongs to the current_version. */
  146. cntx = atomic_long_read(&mm->context.id);
  147. if (cntx2version(cntx) != atomic_long_read(&current_version)) {
  148. cntx = __new_context(mm);
  149. atomic_long_set(&mm->context.id, cntx);
  150. }
  151. if (cpumask_test_and_clear_cpu(cpu, &context_tlb_flush_pending))
  152. need_flush_tlb = true;
  153. atomic_long_set(&per_cpu(active_context, cpu), cntx);
  154. raw_spin_unlock_irqrestore(&context_lock, flags);
  155. switch_mm_fast:
  156. csr_write(CSR_SATP, virt_to_pfn(mm->pgd) |
  157. (cntx2asid(cntx) << SATP_ASID_SHIFT) |
  158. satp_mode);
  159. if (need_flush_tlb)
  160. local_flush_tlb_all();
  161. }
  162. static void set_mm_noasid(struct mm_struct *mm)
  163. {
  164. /* Switch the page table and blindly nuke entire local TLB */
  165. csr_write(CSR_SATP, virt_to_pfn(mm->pgd) | satp_mode);
  166. local_flush_tlb_all_asid(0);
  167. }
  168. static inline void set_mm(struct mm_struct *prev,
  169. struct mm_struct *next, unsigned int cpu)
  170. {
  171. /*
  172. * The mm_cpumask indicates which harts' TLBs contain the virtual
  173. * address mapping of the mm. Compared to noasid, using asid
  174. * can't guarantee that stale TLB entries are invalidated because
  175. * the asid mechanism wouldn't flush TLB for every switch_mm for
  176. * performance. So when using asid, keep all CPUs footmarks in
  177. * cpumask() until mm reset.
  178. */
  179. cpumask_set_cpu(cpu, mm_cpumask(next));
  180. if (static_branch_unlikely(&use_asid_allocator)) {
  181. set_mm_asid(next, cpu);
  182. } else {
  183. cpumask_clear_cpu(cpu, mm_cpumask(prev));
  184. set_mm_noasid(next);
  185. }
  186. }
  187. static int __init asids_init(void)
  188. {
  189. unsigned long asid_bits, old;
  190. /* Figure-out number of ASID bits in HW */
  191. old = csr_read(CSR_SATP);
  192. asid_bits = old | (SATP_ASID_MASK << SATP_ASID_SHIFT);
  193. csr_write(CSR_SATP, asid_bits);
  194. asid_bits = (csr_read(CSR_SATP) >> SATP_ASID_SHIFT) & SATP_ASID_MASK;
  195. asid_bits = fls_long(asid_bits);
  196. csr_write(CSR_SATP, old);
  197. /*
  198. * In the process of determining number of ASID bits (above)
  199. * we polluted the TLB of current HART so let's do TLB flushed
  200. * to remove unwanted TLB enteries.
  201. */
  202. local_flush_tlb_all();
  203. /* Pre-compute ASID details */
  204. if (asid_bits) {
  205. num_asids = 1 << asid_bits;
  206. }
  207. /*
  208. * Use ASID allocator only if number of HW ASIDs are
  209. * at-least twice more than CPUs
  210. */
  211. if (num_asids > (2 * num_possible_cpus())) {
  212. atomic_long_set(&current_version, BIT(SATP_ASID_BITS));
  213. context_asid_map = bitmap_zalloc(num_asids, GFP_KERNEL);
  214. if (!context_asid_map)
  215. panic("Failed to allocate bitmap for %lu ASIDs\n",
  216. num_asids);
  217. __set_bit(0, context_asid_map);
  218. static_branch_enable(&use_asid_allocator);
  219. pr_info("ASID allocator using %lu bits (%lu entries)\n",
  220. asid_bits, num_asids);
  221. } else {
  222. pr_info("ASID allocator disabled (%lu bits)\n", asid_bits);
  223. }
  224. return 0;
  225. }
  226. early_initcall(asids_init);
  227. #else
  228. static inline void set_mm(struct mm_struct *prev,
  229. struct mm_struct *next, unsigned int cpu)
  230. {
  231. /* Nothing to do here when there is no MMU */
  232. }
  233. #endif
  234. /*
  235. * When necessary, performs a deferred icache flush for the given MM context,
  236. * on the local CPU. RISC-V has no direct mechanism for instruction cache
  237. * shoot downs, so instead we send an IPI that informs the remote harts they
  238. * need to flush their local instruction caches. To avoid pathologically slow
  239. * behavior in a common case (a bunch of single-hart processes on a many-hart
  240. * machine, ie 'make -j') we avoid the IPIs for harts that are not currently
  241. * executing a MM context and instead schedule a deferred local instruction
  242. * cache flush to be performed before execution resumes on each hart. This
  243. * actually performs that local instruction cache flush, which implicitly only
  244. * refers to the current hart.
  245. *
  246. * The "cpu" argument must be the current local CPU number.
  247. */
  248. static inline void flush_icache_deferred(struct mm_struct *mm, unsigned int cpu,
  249. struct task_struct *task)
  250. {
  251. #ifdef CONFIG_SMP
  252. if (cpumask_test_and_clear_cpu(cpu, &mm->context.icache_stale_mask)) {
  253. /*
  254. * Ensure the remote hart's writes are visible to this hart.
  255. * This pairs with a barrier in flush_icache_mm.
  256. */
  257. smp_mb();
  258. /*
  259. * If cache will be flushed in switch_to, no need to flush here.
  260. */
  261. if (!(task && switch_to_should_flush_icache(task)))
  262. local_flush_icache_all();
  263. }
  264. #endif
  265. }
  266. void switch_mm(struct mm_struct *prev, struct mm_struct *next,
  267. struct task_struct *task)
  268. {
  269. unsigned int cpu;
  270. if (unlikely(prev == next))
  271. return;
  272. membarrier_arch_switch_mm(prev, next, task);
  273. /*
  274. * Mark the current MM context as inactive, and the next as
  275. * active. This is at least used by the icache flushing
  276. * routines in order to determine who should be flushed.
  277. */
  278. cpu = smp_processor_id();
  279. set_mm(prev, next, cpu);
  280. flush_icache_deferred(next, cpu, task);
  281. }