MADV_FREE: respect pte_dirty, not PG_dirty.
Subsystems (9)
cover MADV_FREE: respect pte_dirty, not PG_dirty.
MADV_FREE relies on the dirty bit in page table entry to decide whether VM allows to discard the page or not.
IOW, if page table entry includes marked dirty bit, VM shouldn't discard the page.
However, as one of exmaple, if swap-in by read fault happens, page table entry point out the page doesn't have marked dirty bit so MADV_FREE might discard the page wrongly.
For avoiding the problem, MADV_FREE did more checks with PageDirty and PageSwapCache. It worked out because swapped-in page lives on swap cache and since it was evicted from the swap cache, the page has PG_dirty flag. So both page flags checks effectively prevent wrong discarding by MADV_FREE.
A problem in above logic is that swapped-in page has PG_dirty since they are removed from swap cache so VM cannot consider those pages as freeable any more alghouth madvise_free is called in future. Look at below example for detail.
ptr = malloc();
memset(ptr);
..
..
.. heavy memory pressure so all of pages are swapped out ..
..
var = *ptr; -> a page swapped-in and removed from swapcache.
page table doesn't mark dirty bit and page descriptor includes PG_dirty
..
..madvise_free(ptr);
..
..
..
.. heavy memory pressure again.
.. In this time, VM cannot discard the page because the page .. has *PG_dirty*
Rather than relying on the PG_dirty of page descriptor for preventing discarding a page, dirty bit in page table is more straightforward and simple.
So, this patch try to make page table entry's dirty bit mark so it doesn't need to take care of PG_dirty.
For it, it fixes several cases(e.g, KSM, migration, swapin, swapoff) then, finally it makes MADV_FREE simple.
With this, it removes complicated logic and makes freeable page checking by madvise_free simple.(ie, +90/-108).
Of course, we could solve above mentioned PG_Dirty problem.
I tested this patchset(memcg, tmpfs, swapon/off, THP, KSM) and found no problem but it still needs careful review.
Minchan Kim (6):
mm: keep dirty bit on KSM page
mm: keep dirty bit on anonymous page migration
mm: mark dirty bit on swapped-in page
mm: mark dirty bit on unuse_pte
mm: decouple PG_dirty from MADV_FREE
mm: MADV_FREE refactoring
include/linux/rmap.h | 9 ++---- mm/ksm.c | 19 ++++++++++--- mm/madvise.c | 13 --------- mm/memory.c | 6 ++-- mm/migrate.c | 4 +++ mm/rmap.c | 78 +++++++++++++++++++++++++--------------------------- mm/swapfile.c | 6 +++- mm/vmscan.c | 63 ++++++++++++++---------------------------- 8 files changed, 90 insertions(+), 108 deletions(-)
Signature · 7 lines
1.9.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>
RFC 1/6 mm: keep dirty bit on KSM page
I encountered segfault of test program while I tested MADV_FREE with KSM. By investigation,
1. A KSM page is mapped on page table of A, B processes with !pte_dirty(but it marked the page as PG_dirty if pte_dirty is on)
2. MADV_FREE of A process can remove the page from swap cache if it was in there and then clear *PG_dirty* to indicate we could discard it instead of swapping out.
3. So, the KSM page's status is !pte_dirty of A, B processes && !PageDirty.
4. VM judges it as freeable page and discard it.
5. Process B encounters segfault even though B didn't call MADV_FREE.
Clearing PG_dirty after anonymous page is removed from swap cache was no problem on integrity POV for private page(ie, normal anon page, not KSM). Just worst case caused by that was unnecessary write out which we have avoided it if same data is already on swap.
However, with introducing MADV_FREE, it could make above problem so this patch fixes it with keeping dirty bit of the page table when the page is replaced with KSM page.
--- mm/ksm.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)
mm/ksm.c +15 -4
@@ -901,9 +901,8 @@ static int write_protect_page(struct vm_area_struct *vma, struct page *page,set_pte_at (mm, addr, ptep, entry); goto out_unlock; } -if (pte_dirty (entry)) -set_page_dirty (page); - entry =pte_mkclean (pte_wrprotect (entry)); + + entry =pte_wrprotect (entry);set_pte_at_notify (mm, addr, ptep, entry); } *orig_pte = *ptep;
@@ -932,11 +931,13 @@ static int replace_page(struct vm_area_struct *vma, struct page *page,struct mm_struct *mm = vma->vm_mm;pmd_t *pmd;pte_t *ptep; +pte_t entry;spinlock_t *ptl;unsigned long addr;int err = -EFAULT ;unsigned long mmun_start;/* For mmu_notifiers */ unsigned long mmun_end;/* For mmu_notifiers */ +bool dirty; addr =page_address_in_vma (page, vma);if (addr == -EFAULT )
@@ -956,12 +957,22 @@ static int replace_page(struct vm_area_struct *vma, struct page *page, goto out_mn; } + dirty =pte_dirty (*ptep);get_page (kpage);page_add_anon_rmap (kpage, vma, addr);flush_cache_page (vma, addr,pte_pfn (*ptep));ptep_clear_flush_notify (vma, addr, ptep); -set_pte_at_notify (mm, addr, ptep,mk_pte (kpage, vma->vm_page_prot)); + + entry =mk_pte (kpage, vma->vm_page_prot); +/* +* Keep a dirty bit to prevent a KSM page sudden freeing +* by MADV_FREE. +*/ +if (dirty) + entry =pte_mkdirty (entry); + +set_pte_at_notify (mm, addr, ptep, entry);page_remove_rmap (page);if (!page_mapped (page))
Signature · 7 lines
1.9.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>
RFC 2/6 mm: keep dirty bit on anonymous page migration
In reply to Minchan KimCurrently, If VM migrates anonymous page, we lose dirty bit of page table entry. Instead, VM translates dirty bit of page table as PG_dirty of page flags. It was okay because dirty bit of page table for anonymous page was no matter to swap out. Instead, VM took care of PG_dirty.
However, with introducing MADV_FREE, it's important to keep page table's dirty bit because It could make MADV_FREE handling logics more straighforward without taking care of PG_dirty.
This patch aims for preparing to remove PG_dirty check for MADV_FREE.
--- mm/migrate.c | 4 ++++ 1 file changed, 4 insertions(+)
mm/migrate.c +4 -0
@@ -151,6 +151,10 @@ static int remove_migration_pte(struct page *new, struct vm_area_struct *vma,if (is_write_migration_entry (entry)) pte =maybe_mkwrite (pte, vma); +/* MADV_FREE relies on pte_dirty. */ +if (PageAnon (new)) + pte =pte_mkdirty (pte); +#ifdef CONFIG_HUGETLB_PAGE if (PageHuge (new)) { pte =pte_mkhuge (pte);
Signature · 7 lines
1.9.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>
RFC 3/6 mm: mark dirty bit on swapped-in page
In reply to Minchan KimBasically, MADV_FREE relys on the dirty bit in page table entry to decide whether VM allows to discard the page or not. IOW, if page table entry includes marked dirty bit, VM shouldn't discard the page.
However, if swap-in by read fault happens, page table entry point out the page doesn't have marked dirty bit so MADV_FREE might discard the page wrongly.
To fix the problem, this patch marks page table entry of page swapping-in as dirty so VM shouldn't discard the page suddenly under us.
With MADV_FREE point of view, marking dirty unconditionally is no problem because we dropped swapped page in MADV_FREE sycall context(ie, Look at madvise_free_pte_range) so every swapping-in pages are no MADV_FREE hinted pages.
--- mm/memory.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
mm/memory.c +4 -2
@@ -2557,9 +2557,11 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,inc_mm_counter_fast (mm,MM_ANONPAGES );dec_mm_counter_fast (mm,MM_SWAPENTS ); - pte =mk_pte (page, vma->vm_page_prot); + +/* Mark dirty bit of page table because MADV_FREE relies on it */ + pte =pte_mkdirty (mk_pte (page, vma->vm_page_prot));if ((flags &FAULT_FLAG_WRITE ) &&reuse_swap_page (page)) { - pte =maybe_mkwrite (pte_mkdirty (pte), vma); + pte =maybe_mkwrite (pte, vma); flags &= ~FAULT_FLAG_WRITE ; ret |=VM_FAULT_WRITE ; exclusive =1 ;
Signature · 7 lines
1.9.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>
Re: [RFC 3/6] mm: mark dirty bit on swapped-in page
Quoted 35 lines · Minchan Kim
Basically,
MADV_FREErelys on the dirty bit in page table entry to decide whether VM allows to discard the page or not. IOW, if page table entry includes marked dirty bit, VM shouldn't discard the page.However, if swap-in by read fault happens, page table entry point out the page doesn't have marked dirty bit so
MADV_FREEmight discard the page wrongly.To fix the problem, this patch marks page table entry of page swapping-in as dirty so VM shouldn't discard the page suddenly under us.
With
MADV_FREEpoint of view, marking dirty unconditionally is no problem because we dropped swapped page inMADV_FREEsycallcontext(ie, Look at madvise_free_pte_range)so every swapping-in pages are noMADV_FREEhinted pages.Cc Hugh Dickins <[email protected]>Cc Cyrill Gorcunov <[email protected]>Cc Pavel Emelyanov <[email protected]>Reported-by Yalin Wang <[email protected]>Signed-off-by Minchan Kim <[email protected]>--- mm/memory.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)mm/memory.c +0
-0@@ -2557,9 +2557,11 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,inc_mm_counter_fast (mm,MM_ANONPAGES );dec_mm_counter_fast (mm,MM_SWAPENTS );
mm/memory.c static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma, · line 2567
Quoted 10 lines
mm/memory.c +4
-2- pte =mk_pte (page, vma->vm_page_prot); + +/* Mark dirty bit of page table because MADV_FREE relies on it */ + pte =pte_mkdirty (mk_pte (page, vma->vm_page_prot));if ((flags &FAULT_FLAG_WRITE ) &&reuse_swap_page (page)) { - pte =maybe_mkwrite (pte_mkdirty (pte), vma); + pte =maybe_mkwrite (pte, vma); flags &= ~FAULT_FLAG_WRITE ; ret |=VM_FAULT_WRITE ; exclusive =1 ;
Hi Minchan! Really sorry for delay in reply. Look, I don't understand the moment -- if page has fault on read then before the patch the PTE won't carry the dirty flag but now we do set it up unconditionally and to me it looks somehow strange at least because this as well sets soft-dirty bit on pages which were not modified but only swapped out. Am I missing something obvious?
--
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: [RFC 3/6] mm: mark dirty bit on swapped-in page
Hello Cyrill,
Quoted 45 lines · Cyrill Gorcunov
Quoted 44 lines · Minchan Kim
Basically,
MADV_FREErelys on the dirty bit in page table entry to decide whether VM allows to discard the page or not. IOW, if page table entry includes marked dirty bit, VM shouldn't discard the page.However, if swap-in by read fault happens, page table entry point out the page doesn't have marked dirty bit so
MADV_FREEmight discard the page wrongly.To fix the problem, this patch marks page table entry of page swapping-in as dirty so VM shouldn't discard the page suddenly under us.
With
MADV_FREEpoint of view, marking dirty unconditionally is no problem because we dropped swapped page inMADV_FREEsycallcontext(ie, Look at madvise_free_pte_range)so every swapping-in pages are noMADV_FREEhinted pages.Cc Hugh Dickins <[email protected]>Cc Cyrill Gorcunov <[email protected]>Cc Pavel Emelyanov <[email protected]>Reported-by Yalin Wang <[email protected]>Signed-off-by Minchan Kim <[email protected]>--- mm/memory.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)mm/memory.c +4
-2@@ -2557,9 +2557,11 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,inc_mm_counter_fast (mm,MM_ANONPAGES );dec_mm_counter_fast (mm,MM_SWAPENTS ); - pte =mk_pte (page, vma->vm_page_prot); + +/* Mark dirty bit of page table because MADV_FREE relies on it */ + pte =pte_mkdirty (mk_pte (page, vma->vm_page_prot));if ((flags &FAULT_FLAG_WRITE ) &&reuse_swap_page (page)) { - pte =maybe_mkwrite (pte_mkdirty (pte), vma); + pte =maybe_mkwrite (pte, vma); flags &= ~FAULT_FLAG_WRITE ; ret |=VM_FAULT_WRITE ;
Quoted 8 lines
mm/memory.c static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma, · line 2567
Quoted 1 line
mm/memory.c +0
-0exclusive =1 ;Hi Minchan! Really sorry for delay in reply. Look, I don't understand the moment -- if page has fault on read then before the patch the PTE won't carry the dirty flag but now we do set it up unconditionally and to me it looks somehow strange at least because this as well sets soft-dirty bit on pages which were not modified but only swapped out. Am I missing something obvious?
It's same one I sent a while ago and you said it's okay at that time. ;-) Okay, It might be lack of description compared to one I sent long time ago because I moved some part of description to another patch and I didn't Cc you. Sorry. I hope below will remind you.
https://www.mail-archive.com/linux-kernel%40vger.kernel.org/msg857827.html
In summary, the problem is that in MADV_FREE point of view, clean anonymous page(ie, no dirty) in page table entry has a problem about sudden discarding under us by reclaimer. Otherwise, VM cannot discard MADV_FREE hinted pages by PageDirty flag of page descriptor.
This patchset aims for solving the problem.
Please feel free to ask if you have questions without wasting your time unless you can remind after reading above URL
Thanks for looking!
--
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: [RFC 3/6] mm: mark dirty bit on swapped-in page
Quoted 23 lines · Minchan Kim
Quoted 14 lines
Quoted 14 lines
+++
b/mm/memory.cmm/memory.c +4
-2@@ -2557,9 +2557,11 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,inc_mm_counter_fast (mm,MM_ANONPAGES );dec_mm_counter_fast (mm,MM_SWAPENTS ); - pte =mk_pte (page, vma->vm_page_prot); + +/* Mark dirty bit of page table because MADV_FREE relies on it */ + pte =pte_mkdirty (mk_pte (page, vma->vm_page_prot));if ((flags &FAULT_FLAG_WRITE ) &&reuse_swap_page (page)) { - pte =maybe_mkwrite (pte_mkdirty (pte), vma); + pte =maybe_mkwrite (pte, vma); flags &= ~FAULT_FLAG_WRITE ; ret |=VM_FAULT_WRITE ;Quoted 8 lines
mm/memory.c static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma, · line 2567
Quoted 1 line
mm/memory.c +0
-0exclusive =1 ;Hi Minchan! Really sorry for delay in reply. Look, I don't understand the moment -- if page has fault on read then before the patch the PTE won't carry the dirty flag but now we do set it up unconditionally and to me it looks somehow strange at least because this as well sets soft-dirty bit on pages which were not modified but only swapped out. Am I missing something obvious?
Quoted 1 line
It's same one I sent a while ago and you said it's okay at that time. ;-)
Ah, I recall. If there is no way to escape dirtifying the page in pte itself maybe we should at least not make it softdirty on read faults?
Quoted 11 lines
Okay, It might be lack of description compared to one I sent long time ago because I moved some part of description to another patch and I didn't Cc you. Sorry. I hope below will remind you.
https://www.mail-archive.com/linux-kernel%40vger.kernel.org/msg857827.html
In summary, the problem is that in
MADV_FREEpoint of view, clean anonymouspage(ie, no dirty)in page table entry has a problem about sudden discarding under us by reclaimer. Otherwise, VM cannot discardMADV_FREEhinted pages by PageDirty flag of page descriptor.
Quoted 5 lines
This patchset aims for solving the problem.
Please feel free to ask if you have questions without wasting your time unless you can remind after reading above URLThanks for looking!
--
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: [RFC 3/6] mm: mark dirty bit on swapped-in page
Quoted 24 lines · Cyrill Gorcunov
Quoted 23 lines · Minchan Kim
Quoted 22 lines
Quoted 5 lines
+++
b/mm/memory.c(quoted diff) +0
-0@@ -2557,9 +2557,11 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma, inc_mm_counter_fast(mm, MM_ANONPAGES); dec_mm_counter_fast(mm, MM_SWAPENTS);Review static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma, · line 2567
Quoted 10 lines
(quoted diff) +4
-2- pte = mk_pte(page, vma->vm_page_prot); + + /* Mark dirty bit of page table because MADV_FREE relies on it */ + pte = pte_mkdirty(mk_pte(page, vma->vm_page_prot)); if ((flags & FAULT_FLAG_WRITE) && reuse_swap_page(page)) { - pte = maybe_mkwrite(pte_mkdirty(pte), vma); + pte = maybe_mkwrite(pte, vma); flags &= ~FAULT_FLAG_WRITE; ret |= VM_FAULT_WRITE; exclusive = 1;Hi Minchan! Really sorry for delay in reply. Look, I don't understand the moment -- if page has fault on read then before the patch the PTE won't carry the dirty flag but now we do set it up unconditionally and to me it looks somehow strange at least because this as well sets soft-dirty bit on pages which were not modified but only swapped out. Am I missing something obvious?
Quoted 4 lines
Quoted 1 line
It's same one I sent a while ago and you said it's okay at that time. ;-)
Ah, I recall. If there is no way to escape dirtifying the page in pte itself maybe we should at least not make it softdirty on read faults?
You mean this?
mm/memory.c +7 -2
@@ -2557,9 +2557,14 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,inc_mm_counter_fast (mm,MM_ANONPAGES );dec_mm_counter_fast (mm,MM_SWAPENTS ); - pte =mk_pte (page, vma->vm_page_prot); + +/* Mark dirty bit of page table because MADV_FREE relies on it */ + pte =pte_mkdirty (mk_pte (page, vma->vm_page_prot)); +if (!flgas &FAULT_FLAG_WRITE ) + pte = pte_clear_flags(pte, _PAGE_SOFT_DIRTY) +if ((flags &FAULT_FLAG_WRITE ) &&reuse_swap_page (page)) { - pte =maybe_mkwrite (pte_mkdirty (pte), vma); + pte =maybe_mkwrite (pte, vma); flags &= ~FAULT_FLAG_WRITE ; ret |=VM_FAULT_WRITE ; exclusive =1 ;
It could be doable if everyone doesn't have strong objection on this patchset.
I will wait more review.
Thanks.
Quoted 17 lines
Quoted 17 lines
Okay, It might be lack of description compared to one I sent long time ago because I moved some part of description to another patch and I didn't Cc you. Sorry. I hope below will remind you.
https://www.mail-archive.com/linux-kernel%40vger.kernel.org/msg857827.html
In summary, the problem is that in
MADV_FREEpoint of view, clean anonymouspage(ie, no dirty)in page table entry has a problem about sudden discarding under us by reclaimer. Otherwise, VM cannot discardMADV_FREEhinted pages by PageDirty flag of page descriptor.This patchset aims for solving the problem.
Please feel free to ask if you have questions without wasting your time unless you can remind after reading above URLThanks for looking!
Signature · 8 lines
Kind regards, Minchan Kim -- 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: [RFC 3/6] mm: mark dirty bit on swapped-in page
Quoted 28 lines · Minchan Kim
Quoted 3 lines
Ah, I recall. If there is no way to escape dirtifying the page in pte itself maybe we should at least not make it softdirty on read faults?
You mean this?
mm/memory.c +7
-2@@ -2557,9 +2557,14 @@ static int do_swap_page(struct mm_struct *mm, struct vm_area_struct *vma,inc_mm_counter_fast (mm,MM_ANONPAGES );dec_mm_counter_fast (mm,MM_SWAPENTS ); - pte =mk_pte (page, vma->vm_page_prot); + +/* Mark dirty bit of page table because MADV_FREE relies on it */ + pte =pte_mkdirty (mk_pte (page, vma->vm_page_prot)); +if (!flgas &FAULT_FLAG_WRITE ) + pte = pte_clear_flags(pte, _PAGE_SOFT_DIRTY) +if ((flags &FAULT_FLAG_WRITE ) &&reuse_swap_page (page)) { - pte =maybe_mkwrite (pte_mkdirty (pte), vma); + pte =maybe_mkwrite (pte, vma); flags &= ~FAULT_FLAG_WRITE ; ret |=VM_FAULT_WRITE ; exclusive =1 ;
Quoted 4 lines
It could be doable if everyone doesn't have strong objection on this patchset.
I will wait more review.
Yeah, something like this. Lets wait for opinions, 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>
RFC 4/6 mm: mark dirty bit on unuse_pte
In reply to Minchan KimBasically, MADV_FREE relys on the dirty bit in page table entry to decide whether VM allows to discard the page or not. IOW, if page table entry includes marked dirty bit, VM shouldn't discard the page.
However, if swapoff happens, page table entry point out the page doesn't have marked dirty bit so MADV_FREE might discard the page wrongly.
To fix the problem, this patch marks page table entry of page as dirty when swapoff hanppens VM shouldn't discard the page suddenly under us.
With MADV_FREE point of view, marking dirty unconditionally is no problem because we dropped swapped page in MADV_FREE sycall context(ie, Look at madvise_free_pte_range) so every swapping-in pages are no MADV_FREE hinted pages.
--- mm/swapfile.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-)
mm/swapfile.c +5 -1
@@ -1118,8 +1118,12 @@ static int unuse_pte(struct vm_area_struct *vma, pmd_t *pmd,dec_mm_counter (vma->vm_mm,MM_SWAPENTS );inc_mm_counter (vma->vm_mm,MM_ANONPAGES );get_page (page); +/* +* For preventing sudden freeing by MADV_FREE, pte must have a +* dirty flag. +*/ set_pte_at (vma->vm_mm, addr, pte, -pte_mkold (mk_pte (page, vma->vm_page_prot))); +pte_mkdirty (pte_mkold (mk_pte (page, vma->vm_page_prot))));if (page == swapcache) {page_add_anon_rmap (page, vma, addr);mem_cgroup_commit_charge (page, memcg, true);
Signature · 7 lines
1.9.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>
RFC 5/6 mm: decouple PG_dirty from MADV_FREE
In reply to Minchan KimBasically, MADV_FREE relies on dirty bit in page table entry to decide whether VM allows to discard the page or not. IOW, if page table entry includes marked dirty bit, VM shouldn't discard the page.
However, as a example, if swap-in by read fault happens, page table entry doesn't have dirty bit so MADV_FREE could discard the page wrongly.
For avoiding the problem, MADV_FREE did more checks with PageDirty and PageSwapCache. It worked out because swapped-in page lives on swap cache and since it is evicted from the swap cache, the page has PG_dirty flag. So both page flags check effectively prevent wrong discarding by MADV_FREE.
However, a problem in above logic is that swapped-in page has PG_dirty since they are removed from swap cache so VM cannot consider those pages as freeable any more alghouth madvise_free is called in future. Look at below example for detail.
ptr = malloc();
memset(ptr);
..
..
.. heavy memory pressure so all of pages are swapped out ..
..
var = *ptr; -> a page swapped-in and removed from swapcache.
page table doesn't mark dirty bit and page descriptor includes PG_dirty
..
..madvise_free(ptr);
..
..
..
.. heavy memory pressure again.
.. In this time, VM cannot discard the page because the page .. has *PG_dirty*
So, rather than relying on the PG_dirty of page descriptor for preventing discarding a page, dirty bit in page table is more straightforward and simple.
Now, every anonymous page handling(ex, anon/swap/cow fault handling, KSM, THP, Migration) takes care of pte dirty bit to keep it so we don't need to check PG_dirty to identify MADV_FREE hinted page so this patch removes PageDirty check.
With this, it removes complicated logic and makes freeable page checking as well as solving above mentioned problem.
--- mm/rmap.c | 2 +- mm/vmscan.c | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-)
mm/rmap.c +1 -1
@@ -1280,7 +1280,7 @@ static int try_to_unmap_one(struct page *page, struct vm_area_struct *vma,if (flags &TTU_FREE ) {VM_BUG_ON_PAGE (PageSwapCache (page), page); -if (!dirty && !PageDirty (page)) { +if (!dirty) {/* It's a freeable page by MADV_FREE */ dec_mm_counter (mm,MM_ANONPAGES ); goto discard;
mm/vmscan.c +1 -2
@@ -805,8 +805,7 @@ static enum page_references page_check_references(struct page *page,return PAGEREF_KEEP ; } -if (PageAnon (page) && !pte_dirty && !PageSwapCache (page) && - !PageDirty (page)) +if (PageAnon (page) && !pte_dirty && !PageSwapCache (page)) *freeable = true;/* Reclaim if clean, defer dirty pages to writeback */
Signature · 7 lines
1.9.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>
RFC 6/6 mm: MADV_FREE refactoring
In reply to Minchan KimThis patch does some clean up.
Firstly, it removes unnecessary PageSwapCache and ClearPageDirty in madvise_fre_pte_range. The reason I did in there is to prevent wrong discarding by non-dirty marked page table entries of anonymous pages.
However, eariler patches in this patchset removed dependency between MADV_FREE and PG_dirty and took care of anonymous page's dirty bit in page table so there is no reason to add such logics in fast path now.
Secondly, this patch removes freeable check in page_referenced. It was pointess because we could decide freeable in try_to_unmap_one with just pte_dirty. If it is judged by freeable page(ie, page table doesn't have dirty bit), just doesn't store swap location of the pte (ie, nuke the page table entry ). Otherwise, we could store swap location on pte so that the page should be swapped-out.
This patch introduces SWAP_DISCARD which represents the passed page should be discarded instead of swapping.
It is set if page is no mapped at any page table and there is no swap slot for the page. If so, shrink_page_list does ClearPageDirty for skipping pageout and then VM reclaims it.
--- include/linux/rmap.h | 9 ++---- mm/madvise.c | 13 --------- mm/rmap.c | 78 +++++++++++++++++++++++++--------------------------- mm/vmscan.c | 62 ++++++++++++++--------------------------- 4 files changed, 62 insertions(+), 100 deletions(-)
include/linux/rmap.h +3 -6
@@ -184,8 +184,7 @@ static inline void page_dup_rmap(struct page *page)* Called from mm/vmscan.c to handle paging out */ int page_referenced (struct page *,int is_locked, -struct mem_cgroup *memcg,unsigned long *vm_flags, -int *is_pte_dirty); +struct mem_cgroup *memcg,unsigned long *vm_flags);#define TTU_ACTION (x) ((x) & TTU_ACTION_MASK)
@@ -262,12 +261,9 @@ int rmap_walk(struct page *page, struct rmap_walk_control *rwc);static inline int page_referenced (struct page *page,int is_locked,struct mem_cgroup *memcg, -unsigned long *vm_flags, -int *is_pte_dirty) +unsigned long *vm_flags) { *vm_flags =0 ; -if (is_pte_dirty) - *is_pte_dirty =0 ;return 0 ; }
@@ -288,5 +284,6 @@ static inline int page_mkclean(struct page *page)#define SWAP_AGAIN 1#define SWAP_FAIL 2#define SWAP_MLOCK 3 +#define SWAP_DISCARD 4#endif /* _LINUX_RMAP_H */
mm/madvise.c +0 -13
@@ -317,19 +317,6 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr,if (!page)continue ; -if (PageSwapCache (page)) { -if (!trylock_page (page)) -continue ; - -if (!try_to_free_swap (page)) { -unlock_page (page); -continue ; - } - -ClearPageDirty (page); -unlock_page (page); - } -/* * Some of architecture(ex, PPC) don't update TLB * with set_pte_at and tlb_remove_tlb_entry so for
mm/rmap.c +38 -40
171 lines not on this page · raw message
mm/vmscan.c +21 -41
131 lines not on this page · raw message
Signature · 7 lines
1.9.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>