context.c 9.5 KB

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