aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPaul Burton <paul.burton@mips.com>2018-12-04 18:44:12 -0500
committerPaul Burton <paul.burton@mips.com>2018-12-05 17:46:44 -0500
commitff4dd232ec45a0e45ea69f28f069f2ab22b4908a (patch)
tree090efa40f8b338157c348ff79401e8a943a0abd1
parent4089caa7d0fd95ef01436089a4122c40984456dc (diff)
MIPS: Expand MIPS32 ASIDs to 64 bits
ASIDs have always been stored as unsigned longs, ie. 32 bits on MIPS32 kernels. This is problematic because it is feasible for the ASID version to overflow & wrap around to zero. We currently attempt to handle this overflow by simply setting the ASID version to 1, using asid_first_version(), but we make no attempt to account for the fact that there may be mm_structs with stale ASIDs that have versions which we now reuse due to the overflow & wrap around. Encountering this requires that: 1) A struct mm_struct X is active on CPU A using ASID (V,n). 2) That mm is not used on CPU A for the length of time that it takes for CPU A's asid_cache to overflow & wrap around to the same version V that the mm had in step 1. During this time tasks using the mm could either be sleeping or only scheduled on other CPUs. 3) Some other mm Y becomes active on CPU A and is allocated the same ASID (V,n). 4) mm X now becomes active on CPU A again, and now incorrectly has the same ASID as mm Y. Where struct mm_struct ASIDs are represented above in the format (version, EntryHi.ASID), and on a typical MIPS32 system version will be 24 bits wide & EntryHi.ASID will be 8 bits wide. The length of time required in step 2 is highly dependent upon the CPU & workload, but for a hypothetical 2GHz CPU running a workload which generates a new ASID every 10000 cycles this period is around 248 days. Due to this long period of time & the fact that tasks need to be scheduled in just the right (or wrong, depending upon your inclination) way, this is obviously a difficult bug to encounter but it's entirely possible as evidenced by reports. In order to fix this, simply extend ASIDs to 64 bits even on MIPS32 builds. This will extend the period of time required for the hypothetical system above to encounter the problem from 28 days to around 3 trillion years, which feels safely outside of the realms of possibility. The cost of this is slightly more generated code in some commonly executed paths, but this is pretty minimal: | Code Size Gain | Percentage -----------------------|----------------|------------- decstation_defconfig | +270 | +0.00% 32r2el_defconfig | +652 | +0.01% 32r6el_defconfig | +1000 | +0.01% I have been unable to measure any change in performance of the LMbench lat_ctx or lat_proc tests resulting from the 64b ASIDs on either 32r2el_defconfig+interAptiv or 32r6el_defconfig+I6500 systems. Signed-off-by: Paul Burton <paul.burton@mips.com> Suggested-by: James Hogan <jhogan@kernel.org> References: https://lore.kernel.org/linux-mips/80B78A8B8FEE6145A87579E8435D78C30205D5F3@fzex.ruijie.com.cn/ References: https://lore.kernel.org/linux-mips/1488684260-18867-1-git-send-email-jiwei.sun@windriver.com/ Cc: Jiwei Sun <jiwei.sun@windriver.com> Cc: Yu Huabing <yhb@ruijie.com.cn> Cc: stable@vger.kernel.org # 2.6.12+ Cc: linux-mips@vger.kernel.org
-rw-r--r--arch/mips/include/asm/cpu-info.h2
-rw-r--r--arch/mips/include/asm/mmu.h2
-rw-r--r--arch/mips/include/asm/mmu_context.h10
-rw-r--r--arch/mips/mm/c-r3k.c2
4 files changed, 7 insertions, 9 deletions
diff --git a/arch/mips/include/asm/cpu-info.h b/arch/mips/include/asm/cpu-info.h
index a41059d47d31..ed7ffe4e63a3 100644
--- a/arch/mips/include/asm/cpu-info.h
+++ b/arch/mips/include/asm/cpu-info.h
@@ -50,7 +50,7 @@ struct guest_info {
50#define MIPS_CACHE_PINDEX 0x00000020 /* Physically indexed cache */ 50#define MIPS_CACHE_PINDEX 0x00000020 /* Physically indexed cache */
51 51
52struct cpuinfo_mips { 52struct cpuinfo_mips {
53 unsigned long asid_cache; 53 u64 asid_cache;
54#ifdef CONFIG_MIPS_ASID_BITS_VARIABLE 54#ifdef CONFIG_MIPS_ASID_BITS_VARIABLE
55 unsigned long asid_mask; 55 unsigned long asid_mask;
56#endif 56#endif
diff --git a/arch/mips/include/asm/mmu.h b/arch/mips/include/asm/mmu.h
index 0740be7d5d4a..24d6b42345fb 100644
--- a/arch/mips/include/asm/mmu.h
+++ b/arch/mips/include/asm/mmu.h
@@ -7,7 +7,7 @@
7#include <linux/wait.h> 7#include <linux/wait.h>
8 8
9typedef struct { 9typedef struct {
10 unsigned long asid[NR_CPUS]; 10 u64 asid[NR_CPUS];
11 void *vdso; 11 void *vdso;
12 atomic_t fp_mode_switching; 12 atomic_t fp_mode_switching;
13 13
diff --git a/arch/mips/include/asm/mmu_context.h b/arch/mips/include/asm/mmu_context.h
index 94414561de0e..a589585be21b 100644
--- a/arch/mips/include/asm/mmu_context.h
+++ b/arch/mips/include/asm/mmu_context.h
@@ -76,14 +76,14 @@ extern unsigned long pgd_current[];
76 * All unused by hardware upper bits will be considered 76 * All unused by hardware upper bits will be considered
77 * as a software asid extension. 77 * as a software asid extension.
78 */ 78 */
79static unsigned long asid_version_mask(unsigned int cpu) 79static inline u64 asid_version_mask(unsigned int cpu)
80{ 80{
81 unsigned long asid_mask = cpu_asid_mask(&cpu_data[cpu]); 81 unsigned long asid_mask = cpu_asid_mask(&cpu_data[cpu]);
82 82
83 return ~(asid_mask | (asid_mask - 1)); 83 return ~(u64)(asid_mask | (asid_mask - 1));
84} 84}
85 85
86static unsigned long asid_first_version(unsigned int cpu) 86static inline u64 asid_first_version(unsigned int cpu)
87{ 87{
88 return ~asid_version_mask(cpu) + 1; 88 return ~asid_version_mask(cpu) + 1;
89} 89}
@@ -102,14 +102,12 @@ static inline void enter_lazy_tlb(struct mm_struct *mm, struct task_struct *tsk)
102static inline void 102static inline void
103get_new_mmu_context(struct mm_struct *mm, unsigned long cpu) 103get_new_mmu_context(struct mm_struct *mm, unsigned long cpu)
104{ 104{
105 unsigned long asid = asid_cache(cpu); 105 u64 asid = asid_cache(cpu);
106 106
107 if (!((asid += cpu_asid_inc()) & cpu_asid_mask(&cpu_data[cpu]))) { 107 if (!((asid += cpu_asid_inc()) & cpu_asid_mask(&cpu_data[cpu]))) {
108 if (cpu_has_vtag_icache) 108 if (cpu_has_vtag_icache)
109 flush_icache_all(); 109 flush_icache_all();
110 local_flush_tlb_all(); /* start new asid cycle */ 110 local_flush_tlb_all(); /* start new asid cycle */
111 if (!asid) /* fix version if needed */
112 asid = asid_first_version(cpu);
113 } 111 }
114 112
115 cpu_context(cpu, mm) = asid_cache(cpu) = asid; 113 cpu_context(cpu, mm) = asid_cache(cpu) = asid;
diff --git a/arch/mips/mm/c-r3k.c b/arch/mips/mm/c-r3k.c
index 3466fcdae0ca..01848cdf2074 100644
--- a/arch/mips/mm/c-r3k.c
+++ b/arch/mips/mm/c-r3k.c
@@ -245,7 +245,7 @@ static void r3k_flush_cache_page(struct vm_area_struct *vma,
245 pmd_t *pmdp; 245 pmd_t *pmdp;
246 pte_t *ptep; 246 pte_t *ptep;
247 247
248 pr_debug("cpage[%08lx,%08lx]\n", 248 pr_debug("cpage[%08llx,%08lx]\n",
249 cpu_context(smp_processor_id(), mm), addr); 249 cpu_context(smp_processor_id(), mm), addr);
250 250
251 /* No ASID => no such page in the cache. */ 251 /* No ASID => no such page in the cache. */