mm: Fix races between swapoff and flush dcache
Subsystems (10)
RFC 1/1 mm: Fix races between swapoff and flush dcache
From: Huang Ying <[email protected]>
Quoted 1 line
From commit
4b3ef9daa4fc("mm/swap: split swap cache into 64MB
trunks") on, after swapoff, the address_space associated with the swap device will be freed. So page_mapping() users which may touch the address_space need some kind of mechanism to prevent the address_space from being freed during accessing.
The dcache flushing functions (flush_dcache_page(), etc) in architecture specific code may access the address_space of swap device for anonymous pages in swap cache via page_mapping() function. But in some cases there are no mechanisms to prevent the swap device from being swapoff, for example,
CPU1 CPU2__get_user_pages() swapoff()
flush_dcache_page()
mapping = page_mapping()
... exit_swap_address_space()
... kvfree(spaces)
mapping_mapped(mapping)The address space may be accessed after being freed.
But from cachetlb.txt and Russell King, flush_dcache_page() only care about file cache pages, for anonymous pages, flush_anon_page() should be used. The implementation of flush_dcache_page() in all architectures follows this too. They will check whether page_mapping() is NULL and whether mapping_mapped() is true to determine whether to flush the dcache immediately. And they will use interval tree (mapping->i_mmap) to find all user space mappings. While mapping_mapped() and mapping->i_mmap isn't used by anonymous pages in swap cache at all.
So, to fix the race between swapoff and flush dcache, __page_mapping() is add to return the address_space for file cache pages and NULL otherwise. All page_mapping() invoking in flush dcache functions are replaced with __page_mapping().
The patch is only build tested, because I have no machine with architecture other than x86.
--- arch/arc/mm/cache.c | 2 +- arch/arm/mm/copypage-v4mc.c | 2 +- arch/arm/mm/copypage-v6.c | 2 +- arch/arm/mm/copypage-xscale.c | 2 +- arch/arm/mm/fault-armv.c | 2 +- arch/arm/mm/flush.c | 6 +++--- arch/mips/mm/cache.c | 2 +- arch/nios2/mm/cacheflush.c | 4 ++-- arch/parisc/kernel/cache.c | 4 ++-- arch/score/mm/cache.c | 4 ++-- arch/sh/mm/cache-sh4.c | 2 +- arch/sh/mm/cache-sh7705.c | 2 +- arch/sparc/kernel/smp_64.c | 8 ++++---- arch/sparc/mm/init_64.c | 6 +++--- arch/sparc/mm/tlb.c | 2 +- arch/unicore32/mm/flush.c | 2 +- arch/unicore32/mm/mmu.c | 2 +- arch/xtensa/mm/cache.c | 2 +- include/linux/mm.h | 1 + mm/util.c | 20 ++++++++++++++++++++ 20 files changed, 49 insertions(+), 28 deletions(-)
arch/arc/mm/cache.c +1 -1
@@ -833,7 +833,7 @@ void flush_dcache_page(struct page *page) }/* don't handle anon pages here */ - mapping =page_mapping (page); + mapping =__page_mapping (page);if (!mapping)return ;
arch/arm/mm/copypage-v4mc.c +1 -1
@@ -70,7 +70,7 @@ void v4_mc_copy_user_highpage(struct page *to, struct page *from,void *kto =kmap_atomic (to);if (!test_and_set_bit (PG_dcache_clean, &from->flags)) -__flush_dcache_page (page_mapping (from), from); +__flush_dcache_page (__page_mapping (from), from);raw_spin_lock (&minicache_lock);
arch/arm/mm/copypage-v6.c +1 -1
@@ -76,7 +76,7 @@ static void v6_copy_user_highpage_aliasing(struct page *to,unsigned long kfrom, kto;if (!test_and_set_bit (PG_dcache_clean, &from->flags)) -__flush_dcache_page (page_mapping (from), from); +__flush_dcache_page (__page_mapping (from), from);/* FIXME: not highmem safe */ discard_old_kernel_data (page_address (to));
arch/arm/mm/copypage-xscale.c +1 -1
@@ -90,7 +90,7 @@ void xscale_mc_copy_user_highpage(struct page *to, struct page *from,void *kto =kmap_atomic (to);if (!test_and_set_bit (PG_dcache_clean, &from->flags)) -__flush_dcache_page (page_mapping (from), from); +__flush_dcache_page (__page_mapping (from), from);raw_spin_lock (&minicache_lock);
arch/arm/mm/fault-armv.c +1 -1
@@ -195,7 +195,7 @@ void update_mmu_cache(struct vm_area_struct *vma, unsigned long addr,if (page ==ZERO_PAGE (0 ))return ; - mapping =page_mapping (page); + mapping =__page_mapping (page);if (!test_and_set_bit (PG_dcache_clean, &page->flags))__flush_dcache_page (mapping, page);if (mapping) {
arch/arm/mm/flush.c +3 -3
@@ -285,7 +285,7 @@ void __sync_icache_dcache(pte_t pteval) page =pfn_to_page (pfn);if (cache_is_vipt_aliasing ()) - mapping =page_mapping (page); + mapping =__page_mapping (page);else mapping =NULL ;
@@ -333,7 +333,7 @@ void flush_dcache_page(struct page *page)return ; } - mapping =page_mapping (page); + mapping =__page_mapping (page);if (!cache_ops_need_broadcast () && mapping && !page_mapcount (page))
@@ -363,7 +363,7 @@ void flush_kernel_dcache_page(struct page *page)if (cache_is_vivt () ||cache_is_vipt_aliasing ()) {struct address_space *mapping; - mapping =page_mapping (page); + mapping =__page_mapping (page);if (!mapping ||mapping_mapped (mapping)) {void *addr;
arch/mips/mm/cache.c +1 -1
@@ -86,7 +86,7 @@ SYSCALL_DEFINE3(cacheflush, unsigned long, addr, unsigned long, bytes,void __flush_dcache_page (struct page *page) { -struct address_space *mapping =page_mapping (page); +struct address_space *mapping =__page_mapping (page);unsigned long addr;if (mapping && !mapping_mapped (mapping)) {
arch/nios2/mm/cacheflush.c +2 -2
@@ -180,7 +180,7 @@ void flush_dcache_page(struct page *page)if (page ==ZERO_PAGE (0 ))return ; - mapping =page_mapping (page); + mapping =__page_mapping (page);/* Flush this page if there are aliases. */ if (mapping && !mapping_mapped (mapping)) {
@@ -215,7 +215,7 @@ void update_mmu_cache(struct vm_area_struct *vma,if (page ==ZERO_PAGE (0 ))return ; - mapping =page_mapping (page); + mapping =__page_mapping (page);if (!test_and_set_bit (PG_dcache_clean, &page->flags))__flush_dcache_page (mapping, page);
arch/parisc/kernel/cache.c +2 -2
@@ -88,7 +88,7 @@ update_mmu_cache(struct vm_area_struct *vma, unsigned long address, pte_t *ptep)return ; page =pfn_to_page (pfn); -if (page_mapping (page) &&test_bit (PG_dcache_dirty, &page->flags)) { +if (__page_mapping (page) &&test_bit (PG_dcache_dirty, &page->flags)) {flush_kernel_dcache_page_addr (pfn_va (pfn));clear_bit (PG_dcache_dirty, &page->flags); }else if (parisc_requires_coherency ())
@@ -304,7 +304,7 @@ __flush_cache_page(struct vm_area_struct *vma, unsigned long vmaddr,void flush_dcache_page (struct page *page) { -struct address_space *mapping =page_mapping (page); +struct address_space *mapping =__page_mapping (page);struct vm_area_struct *mpnt;unsigned long offset;unsigned long addr, old_addr =0 ;
arch/score/mm/cache.c +2 -2
@@ -54,7 +54,7 @@ static void flush_data_cache_page(unsigned long addr)void flush_dcache_page (struct page *page) { -struct address_space *mapping =page_mapping (page); +struct address_space *mapping =__page_mapping (page);unsigned long addr;if (PageHighMem (page))
@@ -86,7 +86,7 @@ void __update_cache(struct vm_area_struct *vma, unsigned long address,if (unlikely (!pfn_valid (pfn)))return ; page =pfn_to_page (pfn); -if (page_mapping (page) &&test_bit (PG_dcache_dirty, &(page)->flags)) { +if (__page_mapping (page) &&test_bit (PG_dcache_dirty, &(page)->flags)) { addr = (unsigned long )page_address (page);if (exec)flush_data_cache_page (addr);
arch/sh/mm/cache-sh4.c +1 -1
@@ -112,7 +112,7 @@ static void sh4_flush_dcache_page(void *arg)struct page *page = arg;unsigned long addr = (unsigned long )page_address (page);#ifndef CONFIG_SMP -struct address_space *mapping =page_mapping (page); +struct address_space *mapping =__page_mapping (page);if (mapping && !mapping_mapped (mapping))clear_bit (PG_dcache_clean, &page->flags);
arch/sh/mm/cache-sh7705.c +1 -1
@@ -136,7 +136,7 @@ static void __flush_dcache_page(unsigned long phys)static void sh7705_flush_dcache_page (void *arg) {struct page *page = arg; -struct address_space *mapping =page_mapping (page); +struct address_space *mapping =__page_mapping (page);if (mapping && !mapping_mapped (mapping))clear_bit (PG_dcache_clean, &page->flags);
arch/sparc/kernel/smp_64.c +4 -4
@@ -929,9 +929,9 @@ static inline void __local_flush_dcache_page(struct page *page)#ifdef DCACHE_ALIASING_POSSIBLE __flush_dcache_page (page_address (page), ((tlb_type == spitfire) && -page_mapping (page) !=NULL )); +__page_mapping (page) !=NULL ));#else -if (page_mapping (page) !=NULL && +if (__page_mapping (page) !=NULL && tlb_type == spitfire)__flush_icache_page (__pa (page_address (page)));#endif
@@ -958,7 +958,7 @@ void smp_flush_dcache_page_impl(struct page *page, int cpu)if (tlb_type == spitfire) { data0 = ((u64)&xcall_flush_dcache_page_spitfire); -if (page_mapping (page) !=NULL ) +if (__page_mapping (page) !=NULL ) data0 |= ((u64 )1 <<32 ); }else if (tlb_type == cheetah || tlb_type == cheetah_plus) {#ifdef DCACHE_ALIASING_POSSIBLE
@@ -994,7 +994,7 @@ void flush_dcache_page_all(struct mm_struct *mm, struct page *page) pg_addr =page_address (page);if (tlb_type == spitfire) { data0 = ((u64)&xcall_flush_dcache_page_spitfire); -if (page_mapping (page) !=NULL ) +if (__page_mapping (page) !=NULL ) data0 |= ((u64 )1 <<32 ); }else if (tlb_type == cheetah || tlb_type == cheetah_plus) {#ifdef DCACHE_ALIASING_POSSIBLE
arch/sparc/mm/init_64.c +3 -3
@@ -206,9 +206,9 @@ inline void flush_dcache_page_impl(struct page *page)#ifdef DCACHE_ALIASING_POSSIBLE __flush_dcache_page (page_address (page), ((tlb_type == spitfire) && -page_mapping (page) !=NULL )); +__page_mapping (page) !=NULL ));#else -if (page_mapping (page) !=NULL && +if (__page_mapping (page) !=NULL && tlb_type == spitfire)__flush_icache_page (__pa (page_address (page)));#endif
@@ -490,7 +490,7 @@ void flush_dcache_page(struct page *page) this_cpu =get_cpu (); - mapping =page_mapping (page); + mapping =__page_mapping (page);if (mapping && !mapping_mapped (mapping)) {int dirty =test_bit (PG_dcache_dirty, &page->flags);if (dirty) {
arch/sparc/mm/tlb.c +1 -1
@@ -128,7 +128,7 @@ void tlb_batch_add(struct mm_struct *mm, unsigned long vaddr, goto no_cache_flush;/* A real file page? */ - mapping =page_mapping (page); + mapping =__page_mapping (page);if (!mapping) goto no_cache_flush;
arch/unicore32/mm/flush.c +1 -1
@@ -83,7 +83,7 @@ void flush_dcache_page(struct page *page)if (page ==ZERO_PAGE (0 ))return ; - mapping =page_mapping (page); + mapping =__page_mapping (page);if (mapping && !mapping_mapped (mapping))clear_bit (PG_dcache_clean, &page->flags);
arch/unicore32/mm/mmu.c +1 -1
@@ -503,7 +503,7 @@ void update_mmu_cache(struct vm_area_struct *vma, unsigned long addr,if (page ==ZERO_PAGE (0 ))return ; - mapping =page_mapping (page); + mapping =__page_mapping (page);if (!test_and_set_bit (PG_dcache_clean, &page->flags))__flush_dcache_page (mapping, page);if (mapping)
arch/xtensa/mm/cache.c +1 -1
@@ -127,7 +127,7 @@ EXPORT_SYMBOL(copy_user_highpage);void flush_dcache_page (struct page *page) { -struct address_space *mapping =page_mapping (page); +struct address_space *mapping =__page_mapping (page);/* * If we have a mapping but the page is not mapped to user-space
include/linux/mm.h +1 -0
@@ -1146,6 +1146,7 @@ static inline pgoff_t page_index(struct page *page) }bool page_mapped (struct page *page); +struct address_space *__page_mapping (struct page *page);struct address_space *page_mapping (struct page *page);/*
mm/util.c +20 -0
@@ -490,6 +490,26 @@ struct anon_vma *page_anon_vma(struct page *page)return __page_rmapping (page); } +/* +* For file cache pages, return the address_space, otherwise return NULL +*/ +struct address_space *__page_mapping (struct page *page) +{ +struct address_space *mapping; + + page =compound_head (page); + +/* This happens if someone calls flush_dcache_page on slab page */ +if (unlikely (PageSlab (page))) +return NULL ; + + mapping = page->mapping; +if ((unsigned long )mapping &PAGE_MAPPING_ANON ) +return NULL ; + +return (void *)((unsigned long )mapping & ~PAGE_MAPPING_FLAGS ); +} +struct address_space *page_mapping (struct page *page) {struct address_space *mapping;
Signature · 7 lines
2.15.1 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>