Fix few rmap-related THP bugs
Subsystems (9)
v3 cover Fix few rmap-related THP bugs
The patch fixes handing PTE-mapped THPs in page_referenced() and page_idle_clear_pte_refs().
To achieve that I've intrdocued new helper -- page_vma_mapped_walk() -- which replaces all page_check_address{,_transhuge}() and covers all THP cases.
Patchset overview:
- First patch fixes one uprobe bug (unrelated to the rest of the
patchset, just spotted it at the same time);
- Patches 2-5 fix handling PTE-mapped THPs in page_referenced(),
page_idle_clear_pte_refs() and rmap core;
- Patches 6-12 convert all page_check_address{,_transhuge}() users (plus remove_migration_pte()) to page_vma_mapped_walk() and drop unused helpers.
I think the fixes are not critical enough for stable@ as they don't lead to crashes or hangs, only suboptimal behaviour.
Please review and consider applying.
v3:
- fix page_vma_mapped_walk() breakage;
- fix one more build error reported by 0-day testing;
- Add few Acked-by/Reviewed-by;
v2:
- address feedback from Andrew;
- fix build errors noticed by 0-day testing.
Kirill A. Shutemov (12):
uprobes: split THPs before trying replace them
mm: introduce page_vma_mapped_walk()
mm: fix handling PTE-mapped THPs in page_referenced()
mm: fix handling PTE-mapped THPs in page_idle_clear_pte_refs() mm, rmap: check all VMAs that PTE-mapped THP can be part of
mm: convert page_mkclean_one() to use page_vma_mapped_walk()
mm: convert try_to_unmap_one() to use page_vma_mapped_walk() mm, ksm: convert write_protect_page() to use page_vma_mapped_walk() mm, uprobes: convert __replace_page() to use page_vma_mapped_walk()
mm: convert page_mapped_in_vma() to use page_vma_mapped_walk()
mm: drop page_check_address{,_transhuge}
mm: convert remove_migration_pte() to use page_vma_mapped_walk()
include/linux/rmap.h | 52 ++--- kernel/events/uprobes.c | 26 ++- mm/Makefile | 6 +- mm/huge_memory.c | 25 +-- mm/internal.h | 9 +- mm/ksm.c | 34 +-- mm/migrate.c | 104 ++++----- mm/page_idle.c | 34 +-- mm/page_vma_mapped.c | 218 ++++++++++++++++++ mm/rmap.c | 574 +++++++++++++++++++----------------------------- 10 files changed, 573 insertions(+), 509 deletions(-) create mode 100644 mm/page_vma_mapped.c
Signature · 7 lines
2.11.0 -- 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>
PATCH v3 1/12 uprobes: split THPs before trying replace them
For THPs page_check_address() always fails. It leads to endless loop in uprobe_write_opcode().
Testcase with huge-tmpfs (not sure if it's possible to trigger this uprobe codepath for anon memory):
mount -t debugfs none /sys/kernel/debug
mount -t tmpfs -o huge=always none /mnt
gcc -Wall -O2 -o /mnt/test -x c - <<EOF
int main(void)
{
return 0;
}
/* Padding to map the code segment with huge pmd */
asm (".zero 2097152");
EOF
echo 'p /mnt/test:0' > /sys/kernel/debug/tracing/uprobe_events
echo 1 > /sys/kernel/debug/tracing/events/uprobes/enable
/mnt/testLet's split THPs before trying to replace.
--- kernel/events/uprobes.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/events/uprobes.c +2 -2
@@ -300,8 +300,8 @@ int uprobe_write_opcode(struct mm_struct *mm, unsigned long vaddr, retry:/* Read the page with vaddr into memory */ - ret =get_user_pages_remote (NULL , mm, vaddr,1 ,FOLL_FORCE , &old_page, - &vma,NULL ); + ret =get_user_pages_remote (NULL , mm, vaddr,1 , +FOLL_FORCE |FOLL_SPLIT , &old_page, &vma,NULL );if (ret <=0 )return ret;
Signature · 7 lines
2.11.0 -- 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>
Re: [PATCHv3 01/12] uprobes: split THPs before trying replace them
Quoted 6 lines · Kirill A. Shutemov
For THPs
page_check_address()always fails. It leads to endless loop inuprobe_write_opcode().Testcase with huge-tmpfs (not sure if it's possible to trigger this uprobe codepath for anon memory):
No, you can't probe the anonymous memory,
kernel/events/uprobes.c int uprobe_write_opcode(struct mm_struct *mm, unsigned long vaddr, · line 306
Quoted 12 lines
kernel/events/uprobes.c +2
-2@@ -300,8 +300,8 @@ int uprobe_write_opcode(struct mm_struct *mm, unsigned long vaddr, retry:/* Read the page with vaddr into memory */ - ret =get_user_pages_remote (NULL , mm, vaddr,1 ,FOLL_FORCE , &old_page, - &vma,NULL ); + ret =get_user_pages_remote (NULL , mm, vaddr,1 , +FOLL_FORCE |FOLL_SPLIT , &old_page, &vma,NULL );if (ret <=0 )return ret;
Thanks,
--
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>
PATCH v3 2/12 mm: introduce page_vma_mapped_walk()
In reply to Kirill A. ShutemovThe patch introduces new interface to check if a page is mapped into a vma. It aims to address shortcomings of page_check_address{,_transhuge}.
Existing interface is not able to handle PTE-mapped THPs: it only finds the first PTE. The rest lefted unnoticed.
page_vma_mapped_walk() iterates over all possible mapping of the page in the vma.
--- include/linux/rmap.h | 26 +++++++ mm/Makefile | 6 +- mm/huge_memory.c | 9 ++- mm/page_vma_mapped.c | 188 +++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 224 insertions(+), 5 deletions(-) create mode 100644 mm/page_vma_mapped.c
include/linux/rmap.h +26 -0
@@ -9,6 +9,7 @@#include <linux/mm.h> #include <linux/rwsem.h> #include <linux/memcontrol.h> +#include <linux/highmem.h> /* * The anon_vma heads a list of private "related" vmas, to scan if
@@ -232,6 +233,31 @@ static inline bool page_check_address_transhuge(struct page *page, }#endif +/* Avoid racy checks */ +#define PVMW_SYNC (1 << 0) +/* Look for migarion entries rather than present PTEs */ +#define PVMW_MIGRATION (1 << 1) + +struct page_vma_mapped_walk { +struct page *page; +struct vm_area_struct *vma; +unsigned long address; +pmd_t *pmd; +pte_t *pte; +spinlock_t *ptl; +unsigned int flags; +}; + +static inline void page_vma_mapped_walk_done (struct page_vma_mapped_walk *pvmw) +{ +if (pvmw->pte) +pte_unmap (pvmw->pte); +if (pvmw->ptl) +spin_unlock (pvmw->ptl); +} + +bool page_vma_mapped_walk (struct page_vma_mapped_walk *pvmw); +/* * Used by swapoff to help locate where page is expected in vma. */
mm/Makefile +4 -2
@@ -23,8 +23,10 @@ KCOV_INSTRUMENT_vmstat.o := n mmu-y := nommu.o mmu-$(CONFIG_MMU) := gup.o highmem.o memory.o mincore.o \ - mlock.o mmap.o mprotect.o mremap.o msync.o rmap.o \ - vmalloc.o pagewalk.o pgtable-generic.o + mlock.o mmap.o mprotect.o mremap.o msync.o \ + page_vma_mapped.o pagewalk.o pgtable-generic.o \ + rmap.o vmalloc.o + ifdef CONFIG_CROSS_MEMORY_ATTACH mmu-$(CONFIG_MMU) += process_vm_access.o
mm/huge_memory.c +6 -3
@@ -1862,9 +1862,12 @@ static void freeze_page(struct page *page)static void unfreeze_page (struct page *page) {int i; - -for (i =0 ; i <HPAGE_PMD_NR ; i++) -remove_migration_ptes (page + i, page + i, true); +if (PageTransHuge (page)) { +remove_migration_ptes (page, page, true); + }else { +for (i =0 ; i <HPAGE_PMD_NR ; i++) +remove_migration_ptes (page + i, page + i, true); + } } static void __split_huge_page_tail(struct page *head, int tail,
mm/page_vma_mapped.c new file +188 -0
188 lines not on this page · raw message
Signature · 7 lines
2.11.0 -- 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>
PATCH v3 3/12 mm: fix handling PTE-mapped THPs in page_referenced()
In reply to Kirill A. ShutemovFor PTE-mapped THP page_check_address_transhuge() is not adequate: it cannot find all relevant PTEs, only the first one. It means we can miss some references of the page and it can result in suboptimal decisions by vmscan.
Let's switch it to page_vma_mapped_walk().
I don't think it's subject for stable@: it's not fatal. The only side effect is that THP can be swapped out when it shouldn't.
--- mm/rmap.c | 66 ++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 34 insertions(+), 32 deletions(-)
mm/rmap.c +34 -32
@@ -886,45 +886,48 @@ struct page_referenced_arg {static int page_referenced_one (struct page *page,struct vm_area_struct *vma,unsigned long address,void *arg) { -struct mm_struct *mm = vma->vm_mm;struct page_referenced_arg *pra = arg; -pmd_t *pmd; -pte_t *pte; -spinlock_t *ptl; +struct page_vma_mapped_walk pvmw = { + .page = page, + .vma = vma, + .address = address, + };int referenced =0 ; -if (!page_check_address_transhuge (page, mm, address, &pmd, &pte, &ptl)) -return SWAP_AGAIN ; +while (page_vma_mapped_walk (&pvmw)) { + address = pvmw.address; -if (vma->vm_flags &VM_LOCKED ) { -if (pte) -pte_unmap (pte); -spin_unlock (ptl); - pra->vm_flags |=VM_LOCKED ; -return SWAP_FAIL ;/* To break the loop */ - } +if (vma->vm_flags &VM_LOCKED ) { +page_vma_mapped_walk_done (&pvmw); + pra->vm_flags |=VM_LOCKED ; +return SWAP_FAIL ;/* To break the loop */ + } -if (pte) { -if (ptep_clear_flush_young_notify (vma, address, pte)) { -/* -* Don't treat a reference through a sequentially read -* mapping as such. If the page has been used in -* another mapping, we will catch it; if this other -* mapping is already gone, the unmap path will have -* set PG_referenced or activated the page. -*/ -if (likely (!(vma->vm_flags &VM_SEQ_READ ))) +if (pvmw.pte) { +if (ptep_clear_flush_young_notify (vma, address, + pvmw.pte)) { +/* +* Don't treat a reference through +* a sequentially read mapping as such. +* If the page has been used in another mapping, +* we will catch it; if this other mapping is +* already gone, the unmap path will have set +* PG_referenced or activated the page. +*/ +if (likely (!(vma->vm_flags &VM_SEQ_READ ))) + referenced++; + } + }else if (IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE )) { +if (pmdp_clear_flush_young_notify (vma, address, + pvmw.pmd)) referenced++; + }else { +/* unexpected pmd-mapped page? */ +WARN_ON_ONCE (1 ); } -pte_unmap (pte); - }else if (IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE )) { -if (pmdp_clear_flush_young_notify (vma, address, pmd)) - referenced++; - }else { -/* unexpected pmd-mapped page? */ -WARN_ON_ONCE (1 ); + + pra->mapcount--; } -spin_unlock (ptl);if (referenced)clear_page_idle (page);
@@ -936,7 +939,6 @@ static int page_referenced_one(struct page *page, struct vm_area_struct *vma, pra->vm_flags |= vma->vm_flags; } - pra->mapcount--;if (!pra->mapcount)return SWAP_SUCCESS ;/* To break the loop */
Signature · 7 lines
2.11.0 -- 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>
Re: [PATCHv3 03/12] mm: fix handling PTE-mapped THPs in page_referenced()
Quoted 9 lines · Kirill A. Shutemov
For PTE-mapped THP
page_check_address_transhuge()is not adequate: it cannot find all relevant PTEs, only the first one. It means we can miss some references of the page and it can result in suboptimal decisions by vmscan.Let's switch it to
page_vma_mapped_walk().I don't think it's subject for stable@: it's not fatal. The only side effect is that THP can be swapped out when it shouldn't.
Please be more specific about the situation when this happens and how a user can recognize this is going on. In other words when should I consider backporting this series.
Also the interface is quite awkward imho. Why cannot we provide a callback into page_vma_mapped_walk and call it for each pte/pmd that matters to the given page? Wouldn't that be much easier than the loop around page_vma_mapped_walk iterator?
Quoted 105 lines
Signed-off-by Kirill A. Shutemov <[email protected]>--- mm/rmap.c | 66 ++++++++++++++++++++++++++++++++------------------------------- 1 file changed, 34 insertions(+), 32 deletions(-)mm/rmap.c +34
-32@@ -886,45 +886,48 @@ struct page_referenced_arg {static int page_referenced_one (struct page *page,struct vm_area_struct *vma,unsigned long address,void *arg) { -struct mm_struct *mm = vma->vm_mm;struct page_referenced_arg *pra = arg; -pmd_t *pmd; -pte_t *pte; -spinlock_t *ptl; +struct page_vma_mapped_walk pvmw = { + .page = page, + .vma = vma, + .address = address, + };int referenced =0 ; -if (!page_check_address_transhuge (page, mm, address, &pmd, &pte, &ptl)) -return SWAP_AGAIN ; +while (page_vma_mapped_walk (&pvmw)) { + address = pvmw.address; -if (vma->vm_flags &VM_LOCKED ) { -if (pte) -pte_unmap (pte); -spin_unlock (ptl); - pra->vm_flags |=VM_LOCKED ; -return SWAP_FAIL ;/* To break the loop */ - } +if (vma->vm_flags &VM_LOCKED ) { +page_vma_mapped_walk_done (&pvmw); + pra->vm_flags |=VM_LOCKED ; +return SWAP_FAIL ;/* To break the loop */ + } -if (pte) { -if (ptep_clear_flush_young_notify (vma, address, pte)) { -/* -* Don't treat a reference through a sequentially read -* mapping as such. If the page has been used in -* another mapping, we will catch it; if this other -* mapping is already gone, the unmap path will have -* set PG_referenced or activated the page. -*/ -if (likely (!(vma->vm_flags &VM_SEQ_READ ))) +if (pvmw.pte) { +if (ptep_clear_flush_young_notify (vma, address, + pvmw.pte)) { +/* +* Don't treat a reference through +* a sequentially read mapping as such. +* If the page has been used in another mapping, +* we will catch it; if this other mapping is +* already gone, the unmap path will have set +* PG_referenced or activated the page. +*/ +if (likely (!(vma->vm_flags &VM_SEQ_READ ))) + referenced++; + } + }else if (IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE )) { +if (pmdp_clear_flush_young_notify (vma, address, + pvmw.pmd)) referenced++; + }else { +/* unexpected pmd-mapped page? */ +WARN_ON_ONCE (1 ); } -pte_unmap (pte); - }else if (IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE )) { -if (pmdp_clear_flush_young_notify (vma, address, pmd)) - referenced++; - }else { -/* unexpected pmd-mapped page? */ -WARN_ON_ONCE (1 ); + + pra->mapcount--; } -spin_unlock (ptl);if (referenced)clear_page_idle (page);@@ -936,7 +939,6 @@ static int page_referenced_one(struct page *page, struct vm_area_struct *vma, pra->vm_flags |= vma->vm_flags; } - pra->mapcount--;if (!pra->mapcount)return SWAP_SUCCESS ;/* To break the loop */ Signature · 7 lines
2.11.0 -- 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>
Signature · 8 lines
Michal Hocko SUSE Labs -- 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>
Re: [PATCHv3 03/12] mm: fix handling PTE-mapped THPs in page_referenced()
Quoted 6 lines · Michal Hocko
Quoted 5 lines · Kirill A. Shutemov
For PTE-mapped THP
page_check_address_transhuge()is not adequate: it cannot find all relevant PTEs, only the first one. It means we can miss some references of the page and it can result in suboptimal decisions by vmscan.
Quoted 8 lines
Quoted 4 lines
Let's switch it to
page_vma_mapped_walk().I don't think it's subject for stable@: it's not fatal. The only side effect is that THP can be swapped out when it shouldn't.
Please be more specific about the situation when this happens and how a user can recognize this is going on. In other words when should I consider backporting this series.
The first you need huge PMD to get split with split_huge_pmd(). It can happen due to munmap(), mprotect(), mremap(), etc. After split_huge_pmd() we have THP mapped with bunch of PTEs instead of single PMD.
The bug is that the kernel only sees pte_young() on the PTEs that maps the first 4k, but not the rest. So if your access pattern touches the THP, but not the first 4k, the page can be reclaimed unfairly and possibly re-faulted from swap soon after.
I don't think it's visible to user, except as unneeded swap-out/swap-in in on rare occasion.
Quoted 4 lines
Also the interface is quite awkward imho. Why cannot we provide a callback into
page_vma_mapped_walkand call it for each pte/pmd that matters to the given page? Wouldn't that be much easier than the loop aroundpage_vma_mapped_walkiterator?
I don't agree that interface with call back would be easier. You would also need to pass down additional context with packing/unpacking it on both ends. I don't think it makes interface less awkward.
But it's matter of taste.
Signature · 7 lines
Kirill A. Shutemov -- 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>
PATCH v3 4/12 mm: fix handling PTE-mapped THPs in page_idle_clear_pte_refs()
In reply to Kirill A. ShutemovFor PTE-mapped THP page_check_address_transhuge() is not adequate: it cannot find all relevant PTEs, only the first one.i
Let's switch it to page_vma_mapped_walk().
I don't think it's subject for stable@: it's not fatal.
--- mm/page_idle.c | 34 +++++++++++++++++----------------- 1 file changed, 17 insertions(+), 17 deletions(-)
mm/page_idle.c +17 -17
@@ -54,27 +54,27 @@ static int page_idle_clear_pte_refs_one(struct page *page,struct vm_area_struct *vma, unsigned long addr, void *arg) { - struct mm_struct *mm = vma->vm_mm; -pmd_t *pmd; -pte_t *pte; -spinlock_t *ptl; + struct page_vma_mapped_walk pvmw = { + .page = page, + .vma = vma, + .address = addr, + };bool referenced = false; -if (!page_check_address_transhuge (page, mm, addr, &pmd, &pte, &ptl)) -return SWAP_AGAIN ; - -if (pte) { - referenced =ptep_clear_young_notify (vma, addr, pte); -pte_unmap (pte); - }else if (IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE )) { - referenced =pmdp_clear_young_notify (vma, addr, pmd); - }else { -/* unexpected pmd-mapped page? */ -WARN_ON_ONCE (1 ); +while (page_vma_mapped_walk (&pvmw)) { + addr = pvmw.address; +if (pvmw.pte) { + referenced =ptep_clear_young_notify (vma, addr, + pvmw.pte); + }else if (IS_ENABLED (CONFIG_TRANSPARENT_HUGEPAGE )) { + referenced =pmdp_clear_young_notify (vma, addr, + pvmw.pmd); + }else { +/* unexpected pmd-mapped page? */ +WARN_ON_ONCE (1 ); + } } -spin_unlock (ptl); -if (referenced) {clear_page_idle (page);/*
Signature · 7 lines
2.11.0 -- 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>
PATCH v3 5/12 mm, rmap: check all VMAs that PTE-mapped THP can be part of
In reply to Kirill A. ShutemovCurrent rmap code can miss a VMA that maps PTE-mapped THP if the first suppage of the THP was unmapped from the VMA.
We need to walk rmap for the whole range of offsets that THP covers, not only the first one.
vma_address() also need to be corrected to check the range instead of the first subpage.
--- mm/internal.h | 9 ++++++--- mm/rmap.c | 16 ++++++++++------ 2 files changed, 16 insertions(+), 9 deletions(-)
mm/internal.h +6 -3
@@ -333,12 +333,15 @@ __vma_address(struct page *page, struct vm_area_struct *vma)static inline unsigned long vma_address (struct page *page,struct vm_area_struct *vma) { -unsigned long address =__vma_address (page, vma); +unsigned long start, end; + + start =__vma_address (page, vma); + end = start +PAGE_SIZE * (hpage_nr_pages (page) -1 );/* page should be within @vma mapping range */ -VM_BUG_ON_VMA (address < vma->vm_start || address >= vma->vm_end, vma); +VM_BUG_ON_VMA (end < vma->vm_start || start >= vma->vm_end, vma); -return address; +return max (start, vma->vm_start); }#else /* !CONFIG_MMU */
mm/rmap.c +10 -6
@@ -1757,7 +1757,7 @@ static int rmap_walk_anon(struct page *page, struct rmap_walk_control *rwc, bool locked) {struct anon_vma *anon_vma; -pgoff_t pgoff; +pgoff_t pgoff_start, pgoff_end;struct anon_vma_chain *avc;int ret =SWAP_AGAIN ;
@@ -1771,8 +1771,10 @@ static int rmap_walk_anon(struct page *page, struct rmap_walk_control *rwc,if (!anon_vma)return ret; - pgoff =page_to_pgoff (page); - anon_vma_interval_tree_foreach(avc, &anon_vma->rb_root, pgoff, pgoff) { + pgoff_start =page_to_pgoff (page); + pgoff_end = pgoff_start +hpage_nr_pages (page) -1 ; +anon_vma_interval_tree_foreach (avc, &anon_vma->rb_root, + pgoff_start, pgoff_end) {struct vm_area_struct *vma = avc->vma;unsigned long address =vma_address (page, vma);
@@ -1810,7 +1812,7 @@ static int rmap_walk_file(struct page *page, struct rmap_walk_control *rwc, bool locked) {struct address_space *mapping =page_mapping (page); -pgoff_t pgoff; +pgoff_t pgoff_start, pgoff_end;struct vm_area_struct *vma;int ret =SWAP_AGAIN ;
@@ -1825,10 +1827,12 @@ static int rmap_walk_file(struct page *page, struct rmap_walk_control *rwc,if (!mapping)return ret; - pgoff =page_to_pgoff (page); + pgoff_start =page_to_pgoff (page); + pgoff_end = pgoff_start +hpage_nr_pages (page) -1 ;if (!locked)i_mmap_lock_read (mapping); - vma_interval_tree_foreach(vma, &mapping->i_mmap, pgoff, pgoff) { +vma_interval_tree_foreach (vma, &mapping->i_mmap, + pgoff_start, pgoff_end) {unsigned long address =vma_address (page, vma);cond_resched ();
Signature · 7 lines
2.11.0 -- 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>
PATCH v3 6/12 mm: convert page_mkclean_one() to use page_vma_mapped_walk()
In reply to Kirill A. ShutemovFor consistency, it worth converting all page_check_address() to page_vma_mapped_walk(), so we could drop the former.
PMD handling here is future-proofing, we don't have users yet. ext4 with huge pages will be the first.
--- mm/rmap.c | 68 ++++++++++++++++++++++++++++++++++++++++++--------------------- 1 file changed, 45 insertions(+), 23 deletions(-)
mm/rmap.c +45 -23
@@ -1017,34 +1017,56 @@ int page_referenced(struct page *page,static int page_mkclean_one (struct page *page,struct vm_area_struct *vma,unsigned long address,void *arg) { -struct mm_struct *mm = vma->vm_mm; -pte_t *pte; -spinlock_t *ptl; -int ret =0 ; +struct page_vma_mapped_walk pvmw = { + .page = page, + .vma = vma, + .address = address, + .flags =PVMW_SYNC , + };int *cleaned = arg; - pte =page_check_address (page, mm, address, &ptl,1 ); -if (!pte) - goto out; - -if (pte_dirty (*pte) ||pte_write (*pte)) { -pte_t entry; +while (page_vma_mapped_walk (&pvmw)) { +int ret =0 ; + address = pvmw.address; +if (pvmw.pte) { +pte_t entry; +pte_t *pte = pvmw.pte; + +if (!pte_dirty (*pte) && !pte_write (*pte)) +continue ; + +flush_cache_page (vma, address,pte_pfn (*pte)); + entry =ptep_clear_flush (vma, address, pte); + entry =pte_wrprotect (entry); + entry =pte_mkclean (entry); +set_pte_at (vma->vm_mm, address, pte, entry); + ret =1 ; + }else { +#ifdef CONFIG_TRANSPARENT_HUGE_PAGECACHE +pmd_t *pmd = pvmw.pmd; +pmd_t entry; + +if (!pmd_dirty (*pmd) && !pmd_write (*pmd)) +continue ; + +flush_cache_page (vma, address,page_to_pfn (page)); + entry =pmdp_huge_clear_flush (vma, address, pmd); + entry =pmd_wrprotect (entry); + entry =pmd_mkclean (entry); +set_pmd_at (vma->vm_mm, address, pmd, entry); + ret =1 ; +#else +/* unexpected pmd-mapped page? */ +WARN_ON_ONCE (1 ); +#endif + } -flush_cache_page (vma, address,pte_pfn (*pte)); - entry =ptep_clear_flush (vma, address, pte); - entry =pte_wrprotect (entry); - entry =pte_mkclean (entry); -set_pte_at (mm, address, pte, entry); - ret =1 ; +if (ret) { +mmu_notifier_invalidate_page (vma->vm_mm, address); + (*cleaned)++; + } } -pte_unmap_unlock (pte, ptl); - -if (ret) { -mmu_notifier_invalidate_page (mm, address); - (*cleaned)++; - } -out:return SWAP_AGAIN ; }
Signature · 7 lines
2.11.0 -- 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>
PATCH v3 7/12 mm: convert try_to_unmap_one() to use page_vma_mapped_walk()
In reply to Kirill A. ShutemovFor consistency, it worth converting all page_check_address() to page_vma_mapped_walk(), so we could drop the former.
It also makes freeze_page() as we walk though rmap only once.
--- mm/huge_memory.c | 16 +--- mm/rmap.c | 260 ++++++++++++++++++++++++++++--------------------------- 2 files changed, 137 insertions(+), 139 deletions(-)
mm/huge_memory.c +4 -12
@@ -1839,24 +1839,16 @@ void vma_adjust_trans_huge(struct vm_area_struct *vma,static void freeze_page (struct page *page) {enum ttu_flags ttu_flags =TTU_IGNORE_MLOCK |TTU_IGNORE_ACCESS | -TTU_RMAP_LOCKED ; -int i, ret; +TTU_RMAP_LOCKED |TTU_SPLIT_HUGE_PMD ; +int ret;VM_BUG_ON_PAGE (!PageHead (page), page);if (PageAnon (page)) ttu_flags |=TTU_MIGRATION ; -/* We only need TTU_SPLIT_HUGE_PMD once */ - ret =try_to_unmap (page, ttu_flags |TTU_SPLIT_HUGE_PMD ); -for (i =1 ; !ret && i <HPAGE_PMD_NR ; i++) { -/* Cut short if the page is unmapped */ -if (page_count (page) ==1 ) -return ; - - ret =try_to_unmap (page + i, ttu_flags); - } -VM_BUG_ON_PAGE (ret, page + i -1 ); + ret =try_to_unmap (page, ttu_flags); +VM_BUG_ON_PAGE (ret, page); }static void unfreeze_page (struct page *page)
mm/rmap.c +133 -127
311 lines not on this page · raw message
Signature · 7 lines
2.11.0 -- 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>
PATCH v3 8/12 mm, ksm: convert write_protect_page() to use page_vma_mapped_walk()
In reply to Kirill A. ShutemovFor consistency, it worth converting all page_check_address() to page_vma_mapped_walk(), so we could drop the former.
--- mm/ksm.c | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-)
mm/ksm.c +18 -16
@@ -850,33 +850,35 @@ static int write_protect_page(struct vm_area_struct *vma, struct page *page, pte_t *orig_pte) {struct mm_struct *mm = vma->vm_mm; -unsigned long addr; -pte_t *ptep; -spinlock_t *ptl; +struct page_vma_mapped_walk pvmw = { + .page = page, + .vma = vma, + };int swapped;int err = -EFAULT ;unsigned long mmun_start;/* For mmu_notifiers */ unsigned long mmun_end;/* For mmu_notifiers */ - addr =page_address_in_vma (page, vma); -if (addr == -EFAULT ) + pvmw.address =page_address_in_vma (page, vma); +if (pvmw.address == -EFAULT ) goto out;BUG_ON (PageTransCompound (page)); - mmun_start = addr; - mmun_end = addr +PAGE_SIZE ; + mmun_start = pvmw.address; + mmun_end = pvmw.address +PAGE_SIZE ;mmu_notifier_invalidate_range_start (mm, mmun_start, mmun_end); - ptep =page_check_address (page, mm, addr, &ptl,0 ); -if (!ptep) +if (!page_vma_mapped_walk (&pvmw)) goto out_mn; +if (WARN_ONCE (!pvmw.pte,"Unexpected PMD mapping?" )) + goto out_unlock; -if (pte_write (*ptep) ||pte_dirty (*ptep)) { +if (pte_write (*pvmw.pte) ||pte_dirty (*pvmw.pte)) {pte_t entry; swapped =PageSwapCache (page); -flush_cache_page (vma, addr,page_to_pfn (page)); +flush_cache_page (vma, pvmw.address,page_to_pfn (page));/* * Ok this is tricky, when get_user_pages_fast() run it doesn't * take any lock, therefore the check that we are going to make
@@ -886,25 +888,25 @@ static int write_protect_page(struct vm_area_struct *vma, struct page *page,* this assure us that no O_DIRECT can happen after the check * or in the middle of the check. */ - entry =ptep_clear_flush_notify (vma, addr, ptep); + entry =ptep_clear_flush_notify (vma, pvmw.address, pvmw.pte);/* * Check that no O_DIRECT or similar I/O is in progress on the * page */ if (page_mapcount (page) +1 + swapped !=page_count (page)) { -set_pte_at (mm, addr, ptep, entry); +set_pte_at (mm, pvmw.address, pvmw.pte, entry); goto out_unlock; }if (pte_dirty (entry))set_page_dirty (page); entry =pte_mkclean (pte_wrprotect (entry)); -set_pte_at_notify (mm, addr, ptep, entry); +set_pte_at_notify (mm, pvmw.address, pvmw.pte, entry); } - *orig_pte = *ptep; + *orig_pte = *pvmw.pte; err =0 ; out_unlock: -pte_unmap_unlock (ptep, ptl); +page_vma_mapped_walk_done (&pvmw); out_mn:mmu_notifier_invalidate_range_end (mm, mmun_start, mmun_end); out:
Signature · 7 lines
2.11.0 -- 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>
PATCH v3 9/12 mm, uprobes: convert __replace_page() to use page_vma_mapped_walk()
In reply to Kirill A. ShutemovFor consistency, it worth converting all page_check_address() to page_vma_mapped_walk(), so we could drop the former.
--- kernel/events/uprobes.c | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-)
kernel/events/uprobes.c +14 -8
@@ -153,14 +153,19 @@ static int __replace_page(struct vm_area_struct *vma, unsigned long addr, struct page *old_page, struct page *new_page) { struct mm_struct *mm = vma->vm_mm; -spinlock_t *ptl; -pte_t *ptep; +struct page_vma_mapped_walk pvmw = { + .page = old_page, + .vma = vma, + .address = addr, + };int err;/* For mmu_notifiers */ const unsigned long mmun_start = addr;const unsigned long mmun_end = addr +PAGE_SIZE ;struct mem_cgroup *memcg; +VM_BUG_ON_PAGE (PageTransHuge (old_page), old_page); + err =mem_cgroup_try_charge (new_page, vma->vm_mm,GFP_KERNEL , &memcg, false);if (err)
@@ -171,11 +176,11 @@ static int __replace_page(struct vm_area_struct *vma, unsigned long addr,mmu_notifier_invalidate_range_start (mm, mmun_start, mmun_end); err = -EAGAIN ; - ptep =page_check_address (old_page, mm, addr, &ptl,0 ); -if (!ptep) { +if (!page_vma_mapped_walk (&pvmw)) {mem_cgroup_cancel_charge (new_page, memcg, false); goto unlock; } +VM_BUG_ON_PAGE (addr != pvmw.address, old_page);get_page (new_page);page_add_new_anon_rmap (new_page, vma, addr, false);
@@ -187,14 +192,15 @@ static int __replace_page(struct vm_area_struct *vma, unsigned long addr,inc_mm_counter (mm,MM_ANONPAGES ); } -flush_cache_page (vma, addr,pte_pfn (*ptep)); -ptep_clear_flush_notify (vma, addr, ptep); -set_pte_at_notify (mm, addr, ptep,mk_pte (new_page, vma->vm_page_prot)); +flush_cache_page (vma, addr,pte_pfn (*pvmw.pte)); +ptep_clear_flush_notify (vma, addr, pvmw.pte); +set_pte_at_notify (mm, addr, pvmw.pte, +mk_pte (new_page, vma->vm_page_prot));page_remove_rmap (old_page, false);if (!page_mapped (old_page))try_to_free_swap (old_page); -pte_unmap_unlock (ptep, ptl); +page_vma_mapped_walk_done (&pvmw);if (vma->vm_flags &VM_LOCKED )munlock_vma_page (old_page);
Signature · 7 lines
2.11.0 -- 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>
PATCH v3 10/12 mm: convert page_mapped_in_vma() to use page_vma_mapped_walk()
In reply to Kirill A. ShutemovFor consistency, it worth converting all page_check_address() to page_vma_mapped_walk(), so we could drop the former.
--- mm/page_vma_mapped.c | 30 ++++++++++++++++++++++++++++++ mm/rmap.c | 26 -------------------------- 2 files changed, 30 insertions(+), 26 deletions(-)
mm/page_vma_mapped.c +30 -0
@@ -186,3 +186,33 @@ next_pte: do { } } } + +/** +* page_mapped_in_vma - check whether a page is really mapped in a VMA +* @page: the page to test +* @vma: the VMA to test +* +* Returns 1 if the page is mapped into the page tables of the VMA, 0 +* if the page is not mapped into the page tables of this VMA. Only +* valid for normal file or anonymous VMAs. +*/ +int page_mapped_in_vma (struct page *page,struct vm_area_struct *vma) +{ +struct page_vma_mapped_walk pvmw = { + .page = page, + .vma = vma, + .flags =PVMW_SYNC , + }; +unsigned long start, end; + + start =__vma_address (page, vma); + end = start +PAGE_SIZE * (hpage_nr_pages (page) -1 ); + +if (unlikely (end < vma->vm_start || start >= vma->vm_end)) +return 0 ; + pvmw.address =max (start, vma->vm_start); +if (!page_vma_mapped_walk (&pvmw)) +return 0 ; +page_vma_mapped_walk_done (&pvmw); +return 1 ; +}
mm/rmap.c +0 -26
@@ -756,32 +756,6 @@ pte_t *__page_check_address(struct page *page, struct mm_struct *mm,return NULL ; } -/** -* page_mapped_in_vma - check whether a page is really mapped in a VMA -* @page: the page to test -* @vma: the VMA to test -* -* Returns 1 if the page is mapped into the page tables of the VMA, 0 -* if the page is not mapped into the page tables of this VMA. Only -* valid for normal file or anonymous VMAs. -*/ -int page_mapped_in_vma (struct page *page,struct vm_area_struct *vma) -{ -unsigned long address; -pte_t *pte; -spinlock_t *ptl; - - address =__vma_address (page, vma); -if (unlikely (address < vma->vm_start || address >= vma->vm_end)) -return 0 ; - pte =page_check_address (page, vma->vm_mm, address, &ptl,1 ); -if (!pte)/* the page is not in this mm */ -return 0 ; -pte_unmap_unlock (pte, ptl); - -return 1 ; -} -#ifdef CONFIG_TRANSPARENT_HUGEPAGE /* * Check that @page is mapped at @address into @mm. In contrast to
Signature · 7 lines
2.11.0 -- 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>
PATCH v3 11/12 mm: drop page_check_address{,_transhuge}
In reply to Kirill A. ShutemovAll users are gone. Let's drop them.
--- include/linux/rmap.h | 36 -------------- mm/rmap.c | 138 --------------------------------------------------- 2 files changed, 174 deletions(-)
include/linux/rmap.h +0 -36
@@ -197,42 +197,6 @@ int page_referenced(struct page *, int is_locked,int try_to_unmap (struct page *,enum ttu_flags flags); -/* -* Used by uprobes to replace a userspace page safely -*/ -pte_t *__page_check_address (struct page *,struct mm_struct *, -unsigned long ,spinlock_t **,int ); - -static inline pte_t *page_check_address (struct page *page,struct mm_struct *mm, -unsigned long address, -spinlock_t **ptlp,int sync) -{ -pte_t *ptep; - -__cond_lock (*ptlp, ptep =__page_check_address (page, mm, address, - ptlp, sync)); -return ptep; -} - -/* -* Used by idle page tracking to check if a page was referenced via page -* tables. -*/ -#ifdef CONFIG_TRANSPARENT_HUGEPAGE -bool page_check_address_transhuge (struct page *page,struct mm_struct *mm, -unsigned long address,pmd_t **pmdp, -pte_t **ptep,spinlock_t **ptlp); -#else -static inline bool page_check_address_transhuge (struct page *page, -struct mm_struct *mm,unsigned long address, -pmd_t **pmdp,pte_t **ptep,spinlock_t **ptlp) -{ - *ptep =page_check_address (page, mm, address, ptlp,0 ); - *pmdp =NULL ; -return !!*ptep; -} -#endif -/* Avoid racy checks */ #define PVMW_SYNC (1 << 0)/* Look for migarion entries rather than present PTEs */
mm/rmap.c +0 -138
144 lines not on this page · raw message
Signature · 7 lines
2.11.0 -- 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>
PATCH v3 12/12 mm: convert remove_migration_pte() to use page_vma_mapped_walk()
In reply to Kirill A. Shutemovremove_migration_pte() also can easily be converted to page_vma_mapped_walk().
--- mm/migrate.c | 104 +++++++++++++++++++++++------------------------------------ 1 file changed, 41 insertions(+), 63 deletions(-)
mm/migrate.c +41 -63
@@ -193,82 +193,60 @@ void putback_movable_pages(struct list_head *l)/* * Restore a potential migration pte to a working pte entry */ -static int remove_migration_pte (struct page *new,struct vm_area_struct *vma, +static int remove_migration_pte (struct page *page,struct vm_area_struct *vma,unsigned long addr,void *old) { -struct mm_struct *mm = vma->vm_mm; +struct page_vma_mapped_walk pvmw = { + .page = old, + .vma = vma, + .address = addr, + .flags =PVMW_SYNC |PVMW_MIGRATION , + }; +struct page *new; +pte_t pte;swp_entry_t entry; -pmd_t *pmd; -pte_t *ptep, pte; -spinlock_t *ptl; -if (unlikely (PageHuge (new))) { - ptep =huge_pte_offset (mm, addr); -if (!ptep) - goto out; - ptl =huge_pte_lockptr (hstate_vma (vma), mm, ptep); - }else { - pmd =mm_find_pmd (mm, addr); -if (!pmd) - goto out; +VM_BUG_ON_PAGE (PageTail (page), page); +while (page_vma_mapped_walk (&pvmw)) { + new = page - pvmw.page->index + +linear_page_index (vma, pvmw.address); - ptep =pte_offset_map (pmd, addr); +get_page (new); + pte =pte_mkold (mk_pte (new,READ_ONCE (vma->vm_page_prot))); +if (pte_swp_soft_dirty (*pvmw.pte)) + pte =pte_mksoft_dirty (pte); -/* -* Peek to check is_swap_pte() before taking ptlock? No, we -* can race mremap's move_ptes(), which skips anon_vma lock. -*/ - - ptl =pte_lockptr (mm, pmd); - } - -spin_lock (ptl); - pte = *ptep; -if (!is_swap_pte (pte)) - goto unlock; - - entry =pte_to_swp_entry (pte); - -if (!is_migration_entry (entry) || -migration_entry_to_page (entry) != old) - goto unlock; - -get_page (new); - pte =pte_mkold (mk_pte (new,READ_ONCE (vma->vm_page_prot))); -if (pte_swp_soft_dirty (*ptep)) - pte =pte_mksoft_dirty (pte); - -/* Recheck VMA as permissions can change since migration started */ -if (is_write_migration_entry (entry)) - pte =maybe_mkwrite (pte, vma); +/* Recheck VMA as permissions can change since migration started */ + entry =pte_to_swp_entry (*pvmw.pte); +if (is_write_migration_entry (entry)) + pte =maybe_mkwrite (pte, vma);#ifdef CONFIG_HUGETLB_PAGE -if (PageHuge (new)) { - pte =pte_mkhuge (pte); - pte =arch_make_huge_pte (pte, vma, new,0 ); - } +if (PageHuge (new)) { + pte =pte_mkhuge (pte); + pte =arch_make_huge_pte (pte, vma, new,0 ); + }#endif -flush_dcache_page (new); -set_pte_at (mm, addr, ptep, pte); +flush_dcache_page (new); +set_pte_at (vma->vm_mm, pvmw.address, pvmw.pte, pte); -if (PageHuge (new)) { -if (PageAnon (new)) -hugepage_add_anon_rmap (new, vma, addr); +if (PageHuge (new)) { +if (PageAnon (new)) +hugepage_add_anon_rmap (new, vma, pvmw.address); +else +page_dup_rmap (new, true); + }else if (PageAnon (new)) +page_add_anon_rmap (new, vma, pvmw.address, false);else -page_dup_rmap (new, true); - }else if (PageAnon (new)) -page_add_anon_rmap (new, vma, addr, false); -else -page_add_file_rmap (new, false); +page_add_file_rmap (new, false); -if (vma->vm_flags &VM_LOCKED && !PageTransCompound (new)) -mlock_vma_page (new); +if (vma->vm_flags &VM_LOCKED && !PageTransCompound (new)) +mlock_vma_page (new); + +/* No need to invalidate - it was non-present before */ +update_mmu_cache (vma, pvmw.address, pvmw.pte); + } -/* No need to invalidate - it was non-present before */ -update_mmu_cache (vma, addr, ptep); -unlock: -pte_unmap_unlock (ptep, ptl); -out:return SWAP_AGAIN ; }
Signature · 7 lines
2.11.0 -- 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>