sparc32: repair cache hooks, mappings and TLB tracking
Subsystems (2)
cover sparc32: repair cache hooks, mappings and TLB tracking
I am submitting nine fixes for SPARC32 cache geometry, user-page copying, folio and anonymous-page maintenance, kmap PTEs and CPU-mask tracking. They apply to linux-sparc for-next at the base commit recorded below and apply independently of my separately posted bit-lock fix.
Since summer 2026, I have been working on getting Linux to boot and run reliably on an SMP HyperSPARC SPARCstation 20, with native GDB builds as my main workload. That work exposed problems in both the HyperSPARC backends and the common SPARC32 cache and memory-management code.
I am submitting the common SPARC32 fixes first because they address architecture-wide hooks and bookkeeping rather than HyperSPARC-specific backends. Some symptoms were exposed by HyperSPARC's cache behaviour, but the affected paths are shared with other SPARC32 processors. I plan to submit the HyperSPARC-specific fixes separately on top of this series.
The SuperSPARC demap serialization change is a separate companion. Both application orders produce the same source tree; neither change narrows mm_cpumask. HyperSPARC-specific fixes and optimizations are not included.
I completed a native GDB 18.1 build on a dual-CPU HyperSPARC RT626 SPARCstation 20 with 512 MB RAM, using a native SPARC32 SMP kernel with the combined fixes, optimizations and separate platform prerequisites. I observed no core dumps, livelock or new kernel messages. This was integration testing before this context-only rebase, not isolated testing of these nine patches. Individual patch test details are retained.
For this rebase, SMP and UP builds and QEMU SS-20 fork/COW smoke tests passed both with and without the Viking companion. Validation builds included the separate PIL and IPI fixes, which are not in this series.
Codex assisted with the rebase, review, validation and cover-letter preparation.
Magnus Lindholm (9):
sparc32: inspect every PROM CPU cache geometry
sparc32: flush the source user alias in copy_user_highpage()
sparc32: do not flush a NULL address in flush_dcache_folio
sparc32: flush the cache when a kmap_local mapping is installed
sparc32: preset modified and referenced bits in kmap PTEs
sparc32: colour-align private file mappings
sparc32: implement flush_anon_page
sparc32: retain mm_cpumask across TLB shootdowns
sparc32: enroll and order CPUs before switching address spaces
arch/sparc/include/asm/cacheflush_32.h | 6 +++ arch/sparc/include/asm/highmem.h | 21 +++++++--- arch/sparc/include/asm/page_32.h | 6 +++ arch/sparc/include/asm/switch_to_32.h | 1 - arch/sparc/kernel/sys_sparc_32.c | 17 +++++++- arch/sparc/mm/init_32.c | 44 ++++++++++++++++++++ arch/sparc/mm/srmmu.c | 56 ++++++++++++++++++++------ 7 files changed, 130 insertions(+), 21 deletions(-)
base-commit: a9e88d617a25306fa7e532b0b8be08df778872cc
Signature · 1 line
2.43.0
PATCH 1/9 sparc32: inspect every PROM CPU cache geometry
Cache discovery runs from setup_arch(), before secondary CPUs are online. Using cpu_online() to terminate the PROM walk therefore stops after the first CPU node on SMP systems. It cannot determine the largest cache and smallest line size across all modules when their geometry differs. Sparse logical CPU numbering is not required to trigger this.
Walk every PROM CPU node on SMP and retain the single-node UP behavior. Start at the first child instead of skipping directly to its sibling, and track whether a CPU was found independently of the end sentinel. No mixed- module hardware failure is claimed by this change.
--- arch/sparc/mm/srmmu.c | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-)
arch/sparc/mm/srmmu.c +5 -8
@@ -964,17 +964,18 @@ static void __init init_vac_layout(void) {phandle nd;int cache_lines; +bool found_cpu = false;char node_str[128 ];#ifdef CONFIG_SMP -int cpu =0 ;unsigned long max_size =0 ;unsigned long min_line_size =0x10000000 ;#endif - nd =prom_getchild (prom_root_node); -while ((nd =prom_getsibling (nd)) !=0 ) { +for (nd =prom_getchild (prom_root_node); nd !=0 ; + nd =prom_getsibling (nd)) {prom_getstring (nd,"device_type" , node_str,sizeof (node_str));if (!strcmp (node_str,"cpu" )) { + found_cpu = true; vac_line_size =prom_getint (nd,"cache-line-size" );if (vac_line_size ==-1 ) {prom_printf ("can't determine cache-line-size, halting.\n" );
@@ -992,16 +993,12 @@ static void __init init_vac_layout(void) max_size = vac_cache_size;if (vac_line_size < min_line_size) min_line_size = vac_line_size; -//FIXME: cpus not contiguous!! - cpu++; -if (cpu >= nr_cpu_ids || !cpu_online (cpu)) -break ;#else break ;#endif } } -if (nd ==0 ) { +if (!found_cpu) {prom_printf ("No CPU nodes found, halting.\n" );prom_halt (); }
Signature · 1 line
2.43.0
PATCH 2/9 sparc32: flush the source user alias in copy_user_highpage()
In reply to Magnus Lindholmcopy_user_highpage() also handles lowmem pages. A source user alias can be dirtied after the fork cache flush but before write protection and its TLB shootdown complete on another CPU. This explains why flushing at fork alone is insufficient; the historical tests below did not isolate that particular interleaving.
The hyperSPARC external cache is virtually indexed, so index bits lie above the page offset and one physical page can occupy 64, 128 or 256 distinct cache locations depending on cache size. Two mappings of a page do not see each other's dirty lines.
copy_user_highpage() reads the source through a kmap_local alias, so lines dirtied through the user mapping sit in a different set and the copy reads stale memory. copy_user_page() then flushes page_address(page), NULL for a highmem page, with no NULL check.
Flush the source page's user alias with flush_cache_page() before reading it, and flush the destination through the address actually written.
Only observable on HyperSPARC: SuperSPARC's caches are physically indexed and MBus snooped, so viking_flush_cache_all() is a window flush and a return.
Fork/COW torture on a two-cpu SPARCstation 20: 7 of 20 runs lost writes before, 0 of 20 after.
The source-alias omission predates Git history; the later folio conversion also removed sparc_flush_page_to_ram()'s NULL guard. This change uses the mapped destination address and covers both cases.
--- arch/sparc/include/asm/page_32.h | 6 ++++++ arch/sparc/mm/init_32.c | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+)
arch/sparc/include/asm/page_32.h +6 -0
@@ -28,6 +28,12 @@ (void )(vaddr); \ } while (0) +#define __HAVE_ARCH_COPY_USER_HIGHPAGE +struct page ; +struct vm_area_struct ; +void copy_user_highpage (struct page *to,struct page *from, +unsigned long vaddr,struct vm_area_struct *vma); +/* passing structs on the Sparc slow us down tremendously... */ /* #define STRICT_MM_TYPECHECKS */
arch/sparc/mm/init_32.c +23 -0
@@ -103,6 +103,29 @@ void sparc_flush_page_to_ram(struct page *page) }EXPORT_SYMBOL (sparc_flush_page_to_ram); +/* +* The kernel reads the source page through a kmap alias, which on a +* virtually indexed cache lands in a different set than the user mapping +* userspace has been dirtying. Flush that alias first, and flush the +* destination through the address actually written rather than +* page_address(), which is NULL for a highmem page. +*/ +void copy_user_highpage (struct page *to,struct page *from, +unsigned long vaddr,struct vm_area_struct *vma) +{ +char *vfrom, *vto; + +if (vma) +flush_cache_page (vma, vaddr,page_to_pfn (from)); + + vfrom =kmap_local_page (from); + vto =kmap_local_page (to); +copy_page (vto, vfrom); +__flush_page_to_ram ((unsigned long )vto); +kunmap_local (vto); +kunmap_local (vfrom); +} +void sparc_flush_folio_to_ram (struct folio *folio) {unsigned long vaddr = (unsigned long )folio_address (folio);
Signature · 1 line
2.43.0
[email protected]
AI reviewRe: [PATCH 2/9] sparc32: flush the source user alias in copy_user_highpage()
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] sparc32: stale kernel cache alias at vfrom in copy_user_highpage() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] sparc32: stale kernel cache alias at vfrom in copy_user_highpage()
--
commit 698c37936dc073ff888986a9b12b4e12b07451f0
Author: Magnus Lindholm <[email protected]>
sparc32: flush the source user alias in copy_user_highpage()
This commit implements copy_user_highpage() on sparc32 to flush the source page's user cache alias before copying and flush the destination page via its mapped kmap_local address.
Quoted 20 lines
arch/sparc/mm/init_32.c +12
-0@@ -103,6 +103,29 @@ void sparc_flush_page_to_ram(struct page *page) }EXPORT_SYMBOL (sparc_flush_page_to_ram); +/* +* The kernel reads the source page through a kmap alias, which on a +* virtually indexed cache lands in a different set than the user mapping +* userspace has been dirtying. Flush that alias first, and flush the +* destination through the address actually written rather than +* page_address(), which is NULL for a highmem page. +*/ +void copy_user_highpage (struct page *to,struct page *from, +unsigned long vaddr,struct vm_area_struct *vma) +{ +char *vfrom, *vto; +
arch/sparc/mm/init_32.c void sparc_flush_page_to_ram(struct page *page) · line 127
Quoted 10 lines
arch/sparc/mm/init_32.c +10
-0+if (vma) +flush_cache_page (vma, vaddr,page_to_pfn (from)); + + vfrom =kmap_local_page (from); + vto =kmap_local_page (to); +copy_page (vto, vfrom); +__flush_page_to_ram ((unsigned long )vto); +kunmap_local (vto); +kunmap_local (vfrom); +}
[Severity: Medium]
Can copy_page(vto, vfrom) leave or hit stale supervisor cache lines at vfrom when from is a lowmem page on HyperSPARC?
For a lowmem page, kmap_local_page(from) returns page_address(from) and kunmap_local(vfrom) returns early without flushing because vfrom is outside the fixmap kmap range. Reading vfrom in copy_page() populates clean supervisor-tagged cache lines at page_address(from).
If the remaining process later reuses from via wp_page_reuse(), writes to vaddr at a different cache colour, and forks again, neither hypersparc_flush_cache_mm() (which uses ASI_M_FLUSH_USER) nor hypersparc_flush_cache_page(vma, vaddr, ...) (which flushes only the user virtual address vaddr) invalidates the supervisor lines at vfrom. The second copy_page(vto, vfrom) will then read stale cached data from vfrom instead of the updated lines written back from vaddr to RAM.
Flushing vfrom with __flush_page_to_ram((unsigned long)vfrom) as well would evict the kernel alias.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH 3/9 sparc32: do not flush a NULL address in flush_dcache_folio
In reply to Magnus Lindholmsparc_flush_folio_to_ram() is sparc32's flush_dcache_folio() and takes the address to flush from folio_address(). For a highmem folio that is NULL, so it flushes virtual address 0, and 0 + PAGE_SIZE and so on for a multi- page folio.
On HyperSPARC, hypersparc_flush_page_to_ram() probes with ASI_M_FLUSH_PROBE, finds nothing mapped at 0 and returns, so nothing is flushed at all.
Returning early would be wrong: flush_dcache_folio() is also the barrier taken before reading a folio userspace may have dirtied, as in filemap_read() when the folio is writably mapped. Fall back to the whole- cache operation, which reaches user aliases at any colour and on SMP is cross-called to every cpu.
Do that only when the folio is mapped by userspace; otherwise the only other mapping is the kmap_local slot, which arch_kmap_local_pre_unmap() writes back on teardown. Without that test block_write_end() makes every buffered write discard 512 KB of VAC, which cost 106 s against 56 s on a 64 MB dd.
With the mapping live and a reader on another cpu, 150 of 150 reads returned stale data before and 0 of 150 after, every first mismatch on a 32-byte boundary, the VAC line size.
The separate HyperSPARC mapped-folio fix completes the lowmem and PKMAP colour handling. The highmem fallback can be expensive for GUP: each mapped folio may require whole-cache maintenance on all CPUs.
--- arch/sparc/mm/init_32.c | 10 ++++++++++ 1 file changed, 10 insertions(+)
arch/sparc/mm/init_32.c +10 -0
@@ -131,6 +131,16 @@ void sparc_flush_folio_to_ram(struct folio *folio)unsigned long vaddr = (unsigned long )folio_address (folio);unsigned int i, nr =folio_nr_pages (folio); +/* +* No kernel address to aim at. Only a user alias can be incoherent +* here; the kmap the kernel used is flushed when it is unmapped. +*/ +if (!vaddr) { +if (folio_mapped (folio)) +flush_cache_all (); +return ; + } +for (i =0 ; i < nr; i++)__flush_page_to_ram (vaddr + i *PAGE_SIZE ); }
Signature · 1 line
2.43.0
PATCH 4/9 sparc32: flush the cache when a kmap_local mapping is installed
In reply to Magnus Lindholmsparc32 defines arch_kmap_local_pre_map() as flush_cache_all(), but generic highmem has never called that hook. The post-conversion flush correction moved cache maintenance into this uncalled hook, so a new kmap can be installed while another mapping of the same page still holds dirty lines at a different colour.
A writer dirties a MAP_SHARED page through its user mapping on one cpu while a reader pread()s the same offset on another: on an SS-20 with two RT626 modules all 100 iterations came back stale, 96288 words wrong. With the flush restored, 0 of 100.
Flush from arch_kmap_local_post_map(), which generic code does call, on both the map and the scheduler restore paths. Running after the pte is installed is safe: the address is not handed to the caller until the hook returns. Route it and the tlb flush through sparc32_kmap_local_post_map() and sparc32_kmap_local_tlb_flush() so later patches have somewhere for a cpu-specific policy.
MBus supplies only optional VA[19:12] "super-set" bits to a virtually indexed cache, so differing colours remain a synonym problem the bus does not resolve.
Reference: MBus Interface Specification, SPARC International, 1997, "Basic Assumptions for Level 2", signal VA[19:12] (MAD[53:46]).
Retain global TLB invalidation. Although the slots are CPU-local, remote translating cache backends can populate entries for them. Local TLB maintenance requires those remote translating users to be removed first and belongs in the later HyperSPARC optimization.
--- arch/sparc/include/asm/highmem.h | 14 ++++++++++---- arch/sparc/mm/srmmu.c | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 4 deletions(-)
arch/sparc/include/asm/highmem.h +10 -4
@@ -26,6 +26,10 @@#include <asm/vaddrs.h> #include <asm/pgtsrmmu.h> +void sparc32_kmap_local_cache_flush (unsigned long vaddr); +void sparc32_kmap_local_tlb_flush (unsigned long vaddr); +void sparc32_kmap_local_post_map (unsigned long vaddr,pte_t pteval); +#define kmap_prot __pgprot(SRMMU_ET_PTE | SRMMU_PRIV | SRMMU_CACHE)extern pte_t *pkmap_page_table;
@@ -48,10 +52,12 @@ extern pte_t *pkmap_page_table;#define flush_cache_kmaps () flush_cache_all()/* FIXME: Use __flush_*_one(vaddr) instead of flush_*_all() -- Anton */ -#define arch_kmap_local_pre_map (vaddr, pteval) flush_cache_all() -#define arch_kmap_local_pre_unmap (vaddr) flush_cache_all() -#define arch_kmap_local_post_map (vaddr, pteval) flush_tlb_all() -#define arch_kmap_local_post_unmap (vaddr) flush_tlb_all() +#define arch_kmap_local_pre_unmap (vaddr) \ + sparc32_kmap_local_cache_flush(vaddr) +#define arch_kmap_local_post_map (vaddr, pteval) \ + sparc32_kmap_local_post_map(vaddr, pteval) +#define arch_kmap_local_post_unmap (vaddr) \ + sparc32_kmap_local_tlb_flush(vaddr)#endif /* __KERNEL__ */
arch/sparc/mm/srmmu.c +21 -0
@@ -88,6 +88,27 @@ static int is_hypersparc;static int srmmu_cache_pagetables; +void sparc32_kmap_local_cache_flush (unsigned long vaddr) +{ +flush_cache_all (); +} + +/* +* Nothing has touched the new mapping yet, so flushing other aliases of this +* page here is as good as doing it before the pte was set. +*/ +void sparc32_kmap_local_post_map (unsigned long vaddr,pte_t pteval) +{ +sparc32_kmap_local_cache_flush (vaddr); +sparc32_kmap_local_tlb_flush (vaddr); +} + +void sparc32_kmap_local_tlb_flush (unsigned long vaddr) +{ +/* Remote translating cache operations can populate these entries. */ +flush_tlb_all (); +} +/* these will be initialized in srmmu_nocache_calcsize() */ static unsigned long srmmu_nocache_size;static unsigned long srmmu_nocache_end;
Signature · 1 line
2.43.0
PATCH 5/9 sparc32: preset modified and referenced bits in kmap PTEs
In reply to Magnus LindholmPreset SRMMU_DIRTY and SRMMU_REF in kernel kmap PTEs, matching PAGE_KERNEL. These mappings do not use hardware M/R transitions for user dirty/accessed accounting. The protection applies to persistent PKMAP entries as well as transient local mappings.
ROSS SPARC RISC User's Guide, section 4.2.1.1, describes hardware table walks and PTE updates when a write or translating operation encounters an unset M/R bit. Presetting the bits avoids those updates for kernel accesses. A whole-context cache flush does not perform that translating access.
This also provides the PTE convention needed by subsequent alias reconciliation, but does not by itself provide that protocol.
--- arch/sparc/include/asm/highmem.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
arch/sparc/include/asm/highmem.h +6 -1
@@ -30,7 +30,12 @@ void sparc32_kmap_local_cache_flush(unsigned long vaddr);void sparc32_kmap_local_tlb_flush (unsigned long vaddr);void sparc32_kmap_local_post_map (unsigned long vaddr,pte_t pteval); -#define kmap_prot __pgprot(SRMMU_ET_PTE | SRMMU_PRIV | SRMMU_CACHE) +/* +* Kernel kmap mappings do not need hardware dirty/accessed tracking. +* Match PAGE_KERNEL and avoid M/R updates during translated accesses. +*/ +#define kmap_prot __pgprot(SRMMU_ET_PTE | SRMMU_PRIV | SRMMU_CACHE | \ + SRMMU_DIRTY | SRMMU_REF)extern pte_t *pkmap_page_table;/*
Signature · 1 line
2.43.0
PATCH 6/9 sparc32: colour-align private file mappings
In reply to Magnus Lindholmarch_get_unmapped_area() aligns MAP_SHARED so a file page is mapped at the colour its page offset implies, but gives MAP_PRIVATE an align_mask of zero. A private file mapping therefore lands at an arbitrary colour, and on a virtually indexed cache it aliases any other mapping of the same page.
Measured on an SS-20 with a 512 KB VAC: every page of a private file mapping sat at the wrong colour, 64 of 64, at three different offsets.
Align them as sparc64 does, whenever there is a file rather than only when the mapping is shared. MAP_FIXED is left alone: the ELF loader places segments with MAP_PRIVATE|MAP_FIXED at addresses that need not preserve colour, so rejecting those makes execve() fail.
Reference: MBus Interface Specification, SPARC International, 1997, signal VA[19:12] (MAD[53:46]).
This changes alignment on HyperSPARC, where SHMLBA follows the VAC size. Each private file mapping can consume up to SHMLBA of extra virtual space.
--- arch/sparc/kernel/sys_sparc_32.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-)
arch/sparc/kernel/sys_sparc_32.c +15 -2
@@ -40,6 +40,15 @@ SYSCALL_DEFINE0(getpagesize)return PAGE_SIZE ;/* Possibly older binaries want 8192 on sun4's? */ } +/* +* A file page reached at two colours is two cache lines, so a kernel-chosen +* mapping of one sits at the colour its page offset implies - private too. +*/ +static bool sparc_needs_colour_align (struct file *filp,unsigned long flags) +{ +return filp || (flags &MAP_SHARED ); +} +unsigned long arch_get_unmapped_area (struct file *filp,unsigned long addr,unsigned long len,unsigned long pgoff,unsigned long flags,vm_flags_t vm_flags) {struct vm_unmapped_area_info info = {};
@@ -50,7 +59,11 @@ unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, unsiif (flags &MAP_FIXED ) {/* We do not accept a shared mapping if it would violate -* cache aliasing constraints. +* cache aliasing constraints. Private file mappings are not +* rejected here: the ELF loader places segments with +* MAP_FIXED at offsets that need not preserve colour, and +* refusing them makes execve() fail. sparc64 draws the line +* in the same place. */ if (!file_hugepage && (flags &MAP_SHARED ) && ((addr - (pgoff <<PAGE_SHIFT )) & (SHMLBA -1 )))
@@ -68,7 +81,7 @@ unsigned long arch_get_unmapped_area(struct file *filp, unsigned long addr, unsi info.low_limit = addr; info.high_limit =TASK_SIZE ;if (!file_hugepage) { - info.align_mask = (flags &MAP_SHARED ) ? + info.align_mask =sparc_needs_colour_align (filp, flags) ? (PAGE_MASK & (SHMLBA -1 )) :0 ; info.align_offset = pgoff <<PAGE_SHIFT ; }else {
Signature · 1 line
2.43.0
PATCH 7/9 sparc32: implement flush_anon_page
In reply to Magnus LindholmGeneric GUP calls flush_anon_page() after pinning a page it is about to reach through a kernel mapping. sparc32 never implemented it, so the user alias can still hold dirty lines the kernel mapping cannot see.
The SMP wrapper checks whether the mm mask contains another CPU and then uses the existing cross-call mechanism; it does not target exactly that mask. HyperSPARC's unconditional all-CPU coverage is a separate fix.
The anonymous-page hook was introduced with a generic no-op fallback, and SPARC32 has used that fallback since then. Supply the missing architecture operation for the GUP coherency contract.
--- arch/sparc/include/asm/cacheflush_32.h | 6 ++++++ arch/sparc/mm/init_32.c | 11 +++++++++++ 2 files changed, 17 insertions(+)
arch/sparc/include/asm/cacheflush_32.h +6 -0
@@ -35,6 +35,12 @@#define flush_page_for_dma (addr) \ sparc32_cachetlb_ops->page_for_dma(addr) +struct vm_area_struct ; + +#define ARCH_HAS_FLUSH_ANON_PAGE +void flush_anon_page (struct vm_area_struct *vma,struct page *page, +unsigned long vaddr); +void sparc_flush_page_to_ram (struct page *page);void sparc_flush_folio_to_ram (struct folio *folio);
arch/sparc/mm/init_32.c +11 -0
@@ -95,6 +95,17 @@ void __init arch_mm_preinit(void) } } +/* +* Called after generic code pins an anon page it will reach through a kernel +* mapping; the user alias may hold dirty lines that mapping cannot see. +*/ +void flush_anon_page (struct vm_area_struct *vma,struct page *page, +unsigned long vaddr) +{ +if (PageAnon (page)) +flush_cache_page (vma, vaddr,page_to_pfn (page)); +} +void sparc_flush_page_to_ram (struct page *page) {unsigned long vaddr = (unsigned long )page_address (page);
Signature · 1 line
2.43.0
PATCH 8/9 sparc32: retain mm_cpumask across TLB shootdowns
In reply to Magnus Lindholmsmp_flush_tlb_mm() narrows mm_cpumask to the current CPU when mm_users is one and current->active_mm matches. That snapshot does not exclude another task obtaining a reference with mmget_not_zero() and using the address space through kthread_use_mm(). Clearing its CPU bit can make subsequent shootdowns miss translations on that CPU. Testing current->mm instead does not serialize against that race.
Remove the narrowing. This follows the SPARC64 fix in commit bafb056ce279 ("sparc64: remove mm_cpumask clearing to fix kthread_use_mm race"). Retain the accumulated CPU mask until normal address-space teardown.
A single-threaded mm that has migrated can consequently need remote shootdowns for the remainder of its lifetime; sun4m broadcasts them to online peers. This trades extra cross-calls for correct coverage.
--- arch/sparc/mm/srmmu.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
arch/sparc/mm/srmmu.c +1 -5
@@ -1624,12 +1624,8 @@ static void smp_flush_cache_mm(struct mm_struct *mm)static void smp_flush_tlb_mm (struct mm_struct *mm) {if (mm->context !=NO_CONTEXT ) { -if (any_other_mm_cpus (mm)) { +if (any_other_mm_cpus (mm))xc1 (local_ops->tlb_mm, (unsigned long )mm); -if (atomic_read (&mm->mm_users) ==1 && current->active_mm == mm) -cpumask_copy (mm_cpumask (mm), -cpumask_of (smp_processor_id ())); - } local_ops->tlb_mm (mm); } }
Signature · 1 line
2.43.0
PATCH 9/9 sparc32: enroll and order CPUs before switching address spaces
In reply to Magnus LindholmThe task-switch macro records a CPU in the incoming active_mm mask, but kthread_use_mm() changes address spaces without switching tasks. A shootdown can therefore miss a CPU using that mm.
Move enrollment to switch_mm(), before installing the context. User task switches and activate_mm() pass here, as does kthread_use_mm(). Lazy kernel threads inherit the already active mm on that CPU; they do not introduce a different hardware context. Keep CPU bits until mm teardown, as in the preceding fix, and remove the duplicate switch_to() update.
SPARC32 smp_mb() currently expands to a compiler barrier. The hash lock in set_bit() is acquired before the mask store, so it does not provide a hardware StoreLoad fence after enrollment. Use STBAR followed by a native LDSTUB of a private stack byte before changing the context. STBAR orders prior stores before the atomic operation, including under PSO; LDSTUB blocks subsequent memory operations. The memory clobber orders the compiler. SPARC Architecture Manual V8 sections 6.2 and 6.3 describe these rules.
The shootdown side updates PTEs using native SWAP before inspecting the mm mask. A newly enrolling CPU must publish its bit before accessing the new address space: either a concurrent shootdown includes it, or it observes the updated tables. A returning CPU remains enrolled and cannot be omitted. Do not rely on processor-specific cache maintenance or a later refcount operation for this ordering. The local helper does not change the general SPARC32 barrier implementation and acquires no shared lock.
--- arch/sparc/include/asm/switch_to_32.h | 1 - arch/sparc/mm/srmmu.c | 16 ++++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-)
arch/sparc/include/asm/switch_to_32.h +0 -1
@@ -58,7 +58,6 @@ extern struct thread_info *current_set[NR_CPUS];#define switch_to (prev, next, last) do { \ SWITCH_ENTER(prev); \ SWITCH_DO_LAZY_FPU(next); \ - cpumask_set_cpu(smp_processor_id(), mm_cpumask(next->active_mm)); \ __asm__ __volatile__( \ "sethi %%hi(here - 0x8), %%o7\n\t" \ "mov %%g6, %%g3\n\t" \
arch/sparc/mm/srmmu.c +16 -0
@@ -467,6 +467,18 @@ static void __init sparc_context_init(int numctx)add_to_free_ctxlist (ctx_list_pool + ctx); } +/* Order CPU enrollment before accesses through the new context. */ +static inline void sparc32_mm_context_barrier (void ) +{ +#ifdef CONFIG_SMP +unsigned char scratch =0 ; + +/* STBAR orders prior stores; LDSTUB blocks subsequent loads. */ + asmvolatile ("stbar\n\tldstub %0, %%g0" + :"+m" (scratch) : :"memory" ); +#endif +} +void switch_mm (struct mm_struct *old_mm,struct mm_struct *mm,struct task_struct *tsk) {
@@ -479,6 +491,10 @@ void switch_mm(struct mm_struct *old_mm, struct mm_struct *mm,srmmu_ctxd_set (&srmmu_context_table[mm->context], mm->pgd); } +/* kthread_use_mm() can enter an mm without a task switch. */ +cpumask_set_cpu (smp_processor_id (),mm_cpumask (mm)); +sparc32_mm_context_barrier (); +if (sparc_cpu_model == sparc_leon)leon_switch_mm ();
Signature · 1 line
2.43.0