Optimize large folio interaction with deferred split
Subsystems (3)
cover Optimize large folio interaction with deferred split
Hi All,
This is a small series in support of my work to enable the use of large folios for anonymous memory (currently called "FLEXIBLE_THP") [1]. It first makes it possible to add large, non-pmd-mappable folios to the deferred split queue. Then it modifies zap_pte_range() to batch-remove spans of physically contiguous pages from the rmap, which means that in the common case, we elide the need to ever put the folio on the deferred split queue, thus reducing lock contention and improving performance.
This becomes more visible once we have lots of large anonymous folios in the system, and Huang Ying has suggested solving this needs to be a prerequisit for merging the main FLEXIBLE_THP work.
The series applies on top of v6.5-rc2 and a branch is available at [2].
I don't have a full test run with the latest versions of all the patches on top of the latest baseline, so not posting results formally. I can get these if people feel they are neccessary though. But anecdotally, for the kernel compilation workload, this series reduces kernel time by ~4% and reduces real-time by ~0.4%, compared with [1].
[1] https://lore.kernel.org/linux-mm/[email protected]/
[2] https://gitlab.arm.com/linux-arm/linux-rr/-/tree/features/granule_perf/deferredsplit-lkml_v1
Thanks,
Ryan
Ryan Roberts (3):
mm: Allow deferred splitting of arbitrary large anon folios
mm: Implement folio_remove_rmap_range()
mm: Batch-zap large anonymous folio PTE mappings
include/linux/rmap.h | 2 + mm/memory.c | 119 +++++++++++++++++++++++++++++++++++++++++++ mm/rmap.c | 67 +++++++++++++++++++++++- 3 files changed, 187 insertions(+), 1 deletion(-)
--
2.25.1
PATCH 1/3 mm: Allow deferred splitting of arbitrary large anon folios
In preparation for the introduction of large folios for anonymous memory, we would like to be able to split them when they have unmapped subpages, in order to free those unused pages under memory pressure. So remove the artificial requirement that the large folio needed to be at least PMD-sized.
--- mm/rmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
mm/rmap.c +1 -1
@@ -1430,7 +1430,7 @@ void page_remove_rmap(struct page *page, struct vm_area_struct *vma,* page of the folio is unmapped and at least one page * is still mapped. */ -if (folio_test_pmd_mappable (folio) &&folio_test_anon (folio)) +if (folio_test_large (folio) &&folio_test_anon (folio))if (!compound || nr < nr_pmdmapped)deferred_split_folio (folio); }
Signature · 1 line
2.25.1
Re: [PATCH v1 1/3] mm: Allow deferred splitting of arbitrary large anon folios
Quoted 9 lines · Ryan Roberts
In preparation for the introduction of large folios for anonymous memory, we would like to be able to split them when they have unmapped subpages, in order to free those unused pages under memory pressure. So remove the artificial requirement that the large folio needed to be at least PMD-sized.
Signed-off-by Ryan Roberts <[email protected]>Reviewed-by Yu Zhao <[email protected]>Reviewed-by Yin Fengwei <[email protected]>
mm/rmap.c void page_remove_rmap(struct page *page, struct vm_area_struct *vma, · line 1435
Quoted 5 lines
mm/rmap.c +1
-1*/ -if (folio_test_pmd_mappable (folio) &&folio_test_anon (folio)) +if (folio_test_large (folio) &&folio_test_anon (folio))if (!compound || nr < nr_pmdmapped)deferred_split_folio (folio);
I wonder if it's worth introducing a folio_test_deferred_split() (better naming appreciated ...) to allow us to allocate order-1 folios and not do horrible things. Maybe it's not worth supporting order-1 folios; we're always better off going to order-2 immediately. Just thinking.
Re: [PATCH v1 1/3] mm: Allow deferred splitting of arbitrary large anon folios
Quoted 7 lines · Matthew Wilcox
Quoted 6 lines · Ryan Roberts
In preparation for the introduction of large folios for anonymous memory, we would like to be able to split them when they have unmapped subpages, in order to free those unused pages under memory pressure. So remove the artificial requirement that the large folio needed to be at least PMD-sized.
Quoted 5 lines
Quoted 3 lines
Signed-off-by Ryan Roberts <[email protected]>Reviewed-by Yu Zhao <[email protected]>Reviewed-by Yin Fengwei <[email protected]>Reviewed-by Matthew Wilcox (Oracle) <[email protected]>
Thanks!
Quoted 11 lines
mm/rmap.c void page_remove_rmap(struct page *page, struct vm_area_struct *vma, · line 1435
Quoted 6 lines
mm/rmap.c +1
-1*/ -if (folio_test_pmd_mappable (folio) &&folio_test_anon (folio)) +if (folio_test_large (folio) &&folio_test_anon (folio))if (!compound || nr < nr_pmdmapped)deferred_split_folio (folio);I wonder if it's worth introducing a
folio_test_deferred_split()(better naming appreciated ...) to allow us to allocate order-1 folios and not do horrible things. Maybe it's not worth supporting order-1 folios; we're always better off going to order-2 immediately. Just thinking.
There is more than just _deferred_list in the 3rd page; you also have _flags_2a and _head_2a. I guess you know much better than me what they store. But I'm guessing its harder than jsut not splitting an order-1 page?
With the direction of large anon folios (_not_ retrying with every order down to 0), I'm not sure what the use case would be for order-1 anyway?
Re: [PATCH v1 1/3] mm: Allow deferred splitting of arbitrary large anon folios
Quoted 28 lines · Ryan Roberts
Quoted 7 lines · Matthew Wilcox
Quoted 6 lines · Ryan Roberts
In preparation for the introduction of large folios for anonymous memory, we would like to be able to split them when they have unmapped subpages, in order to free those unused pages under memory pressure. So remove the artificial requirement that the large folio needed to be at least PMD-sized.
Quoted 5 lines
Quoted 3 lines
Signed-off-by Ryan Roberts <[email protected]>Reviewed-by Yu Zhao <[email protected]>Reviewed-by Yin Fengwei <[email protected]>Reviewed-by Matthew Wilcox (Oracle) <[email protected]>Thanks!
Quoted 11 lines
mm/rmap.c void page_remove_rmap(struct page *page, struct vm_area_struct *vma, · line 1435
Quoted 6 lines
*/
mm/rmap.c +1
-1-if (folio_test_pmd_mappable (folio) &&folio_test_anon (folio)) +if (folio_test_large (folio) &&folio_test_anon (folio))if (!compound || nr < nr_pmdmapped)deferred_split_folio (folio);I wonder if it's worth introducing a
folio_test_deferred_split()(better naming appreciated ...) to allow us to allocate order-1 folios and not do horrible things. Maybe it's not worth supporting order-1 folios; we're always better off going to order-2 immediately. Just thinking.
Quoted 6 lines
There is more than just
_deferred_listin the 3rd page; you also have_flags_2aand_head_2a. I guess you know much better than me what they store. But I'm guessing its harder than jsut not splitting an order-1 page?With the direction of large anon folios (_not_ retrying with every order down to 0), I'm not sure what the use case would be for order-1 anyway?
Just noting that we might need some struct-page space for better mapcount/shared tracking, which might get hard for order-1 pages.
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v1 1/3] mm: Allow deferred splitting of arbitrary large anon folios
Quoted 29 lines · David Hildenbrand
Quoted 28 lines · Ryan Roberts
Quoted 7 lines · Matthew Wilcox
Quoted 6 lines · Ryan Roberts
In preparation for the introduction of large folios for anonymous memory, we would like to be able to split them when they have unmapped subpages, in order to free those unused pages under memory pressure. So remove the artificial requirement that the large folio needed to be at least PMD-sized.
Quoted 5 lines
Quoted 3 lines
Signed-off-by Ryan Roberts <[email protected]>Reviewed-by Yu Zhao <[email protected]>Reviewed-by Yin Fengwei <[email protected]>Reviewed-by Matthew Wilcox (Oracle) <[email protected]>Thanks!
Quoted 11 lines
mm/rmap.c void page_remove_rmap(struct page *page, struct vm_area_struct *vma, · line 1435
Quoted 6 lines
*/
mm/rmap.c +1
-1-if (folio_test_pmd_mappable (folio) &&folio_test_anon (folio)) +if (folio_test_large (folio) &&folio_test_anon (folio))if (!compound || nr < nr_pmdmapped)deferred_split_folio (folio);I wonder if it's worth introducing a
folio_test_deferred_split()(better naming appreciated ...) to allow us to allocate order-1 folios and not do horrible things. Maybe it's not worth supporting order-1 folios; we're always better off going to order-2 immediately. Just thinking.
Quoted 3 lines
Quoted 3 lines
There is more than just
_deferred_listin the 3rd page; you also have_flags_2aand_head_2a. I guess you know much better than me what they store. But I'm guessing its harder than jsut not splitting an order-1 page?
Those are page->flags and page->compound_head for the third page in the folio. They don't really need a name; nothing refers to them, but it's important that space not be reused ;-)
This is slightly different from _flags_1; we do have some flags which reuse the bits (they're labelled as PF_SECOND). Right now, it's only PF_has_hwpoisoned, but we used to have PF_double_map. Others may arise.
Quoted 5 lines
Quoted 2 lines
With the direction of large anon folios (_not_ retrying with every order down to 0), I'm not sure what the use case would be for order-1 anyway?
Just noting that we might need some struct-page space for better mapcount/shared tracking, which might get hard for order-1 pages.
My assumption had been that we'd be able to reuse the _entire_mapcount and _nr_pages_mapped fields and not spill into the third page, but the third page is definitely available today if we want it. I'm fine with disallowing order-1 anon/file folios forever.
Re: [PATCH v1 1/3] mm: Allow deferred splitting of arbitrary large anon folios
Quoted 7 lines · Matthew Wilcox
Those are
page->flagsandpage->compound_headfor the third page in the folio. They don't really need a name; nothing refers to them, but it's important that space not be reused ;-)This is slightly different from
_flags_1; we do have some flags which reuse the bits (they're labelled asPF_SECOND). Right now, it's onlyPG_has_hwpoisoned, but we used to havePG_double_map. Others may arise.
Sorry, this was incomplete. We do still have per-page flags! HWPoison is the obvious one, but PG_head is per-page (... think about it ...) PG_anon_exclusive is actually per-page.
Most of the flags labelled as PF_ANY are mislabelled. PG_private and PG_private2 are never set/cleared/tested on tail pages. PG_young and PG_idle are only ever tested on the head page, but some code incorrectly sets them on tail pages, where those bits are ignored. I tried to fix that a while ago, but the patch was overlooked and I couldn't be bothered to try all that hard. I have no clue about PG_vmemmap_self_hosted. I think PG_isolated is probably never set on compound pages. PG_owner_priv_1 is a disaster, as you might expect.
Re: [PATCH v1 1/3] mm: Allow deferred splitting of arbitrary large anon folios
In reply to Matthew WilcoxQuoted 42 lines · Matthew Wilcox
Quoted 29 lines · David Hildenbrand
Quoted 28 lines · Ryan Roberts
Quoted 7 lines · Matthew Wilcox
Quoted 6 lines · Ryan Roberts
In preparation for the introduction of large folios for anonymous memory, we would like to be able to split them when they have unmapped subpages, in order to free those unused pages under memory pressure. So remove the artificial requirement that the large folio needed to be at least PMD-sized.
Quoted 5 lines
Quoted 3 lines
Signed-off-by Ryan Roberts <[email protected]>Reviewed-by Yu Zhao <[email protected]>Reviewed-by Yin Fengwei <[email protected]>Reviewed-by Matthew Wilcox (Oracle) <[email protected]>Thanks!
Quoted 11 lines
mm/rmap.c void page_remove_rmap(struct page *page, struct vm_area_struct *vma, · line 1435
Quoted 6 lines
*/mm/rmap.c +1
-1-if (folio_test_pmd_mappable (folio) &&folio_test_anon (folio)) +if (folio_test_large (folio) &&folio_test_anon (folio))if (!compound || nr < nr_pmdmapped)deferred_split_folio (folio);I wonder if it's worth introducing a
folio_test_deferred_split()(better naming appreciated ...) to allow us to allocate order-1 folios and not do horrible things. Maybe it's not worth supporting order-1 folios; we're always better off going to order-2 immediately. Just thinking.Quoted 3 lines
Quoted 3 lines
There is more than just
_deferred_listin the 3rd page; you also have_flags_2aand_head_2a. I guess you know much better than me what they store. But I'm guessing its harder than jsut not splitting an order-1 page?Those are
page->flagsandpage->compound_headfor the third page in the folio. They don't really need a name; nothing refers to them, but it's important that space not be reused ;-)This is slightly different from
_flags_1; we do have some flags which reuse the bits (they're labelled asPF_SECOND). Right now, it's onlyPF_has_hwpoisoned, but we used to havePF_double_map. Others may arise.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
With the direction of large anon folios (_not_ retrying with every order down to 0), I'm not sure what the use case would be for order-1 anyway?
Just noting that we might need some struct-page space for better mapcount/shared tracking, which might get hard for order-1 pages.
My assumption had been that we'd be able to reuse the
_entire_mapcountand_nr_pages_mappedfields and not spill into the third page, but the
We most likely have to keep _entire_mapcount to keep "PMD mapped" working (I don't think we can not account that, some user space relies on that). Reusing _nr_pages_mapped for _total_mapcount would work until we need more bits.
But once we want to sort out some other questions like "is this folio mapped shared or mapped exclusive" we might need more space.
What I am playing with right now to tackle that would most probably not fit in there (but I'll keep trying ;) ).
Quoted 2 lines
third page is definitely available today if we want it. I'm fine with disallowing order-1 anon/file folios forever.
Yes, let's first sort out the open issues before going down that path (might not really be worth it after all).
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v1 1/3] mm: Allow deferred splitting of arbitrary large anon folios
In reply to Ryan RobertsQuoted 18 lines · Ryan Roberts
In preparation for the introduction of large folios for anonymous memory, we would like to be able to split them when they have unmapped subpages, in order to free those unused pages under memory pressure. So remove the artificial requirement that the large folio needed to be at least PMD-sized.
Signed-off-by Ryan Roberts <[email protected]>Reviewed-by Yu Zhao <[email protected]>Reviewed-by Yin Fengwei <[email protected]>--- mm/rmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mm/rmap.c +0
-0
mm/rmap.c void page_remove_rmap(struct page *page, struct vm_area_struct *vma, · line 1435
Quoted 7 lines
mm/rmap.c +1
-1@@ -1430,7 +1430,7 @@ void page_remove_rmap(struct page *page, struct vm_area_struct *vma,* page of the folio is unmapped and at least one page * is still mapped. */ -if (folio_test_pmd_mappable (folio) &&folio_test_anon (folio)) +if (folio_test_large (folio) &&folio_test_anon (folio))if (!compound || nr < nr_pmdmapped)deferred_split_folio (folio);
!compound will always be true I guess, so nr_pmdmapped == 0 (which will always be the case) will be ignored.
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v1 1/3] mm: Allow deferred splitting of arbitrary large anon folios
Quoted 14 lines · David Hildenbrand
Quoted 13 lines · Ryan Roberts
In preparation for the introduction of large folios for anonymous memory, we would like to be able to split them when they have unmapped subpages, in order to free those unused pages under memory pressure. So remove the artificial requirement that the large folio needed to be at least PMD-sized.
Signed-off-by Ryan Roberts <[email protected]>Reviewed-by Yu Zhao <[email protected]>Reviewed-by Yin Fengwei <[email protected]>---
mm/rmap.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 16 lines
mm/rmap.c void page_remove_rmap(struct page *page, struct · line 1436
Quoted 13 lines
mm/rmap.c +1
-1@@ -1430,7 +1430,7 @@ void page_remove_rmap(struct page *page, structvm_area_struct *vma, * page of the folio is unmapped and at least one page * is still mapped. */ - if (folio_test_pmd_mappable(folio) && folio_test_anon(folio)) + if (folio_test_large(folio) && folio_test_anon(folio)) if (!compound || nr < nr_pmdmapped) deferred_split_folio(folio);!compound will always be true I guess, so
nr_pmdmapped== 0 (which will always be the case) will be ignored.
I don't follow why !compound will always be true. This function is page_remove_rmap() (not folio_remove_rmap_range() which I add in a later patch). page_remove_rmap() can work on pmd-mapped pages where compound=true is passed in.
Quoted 3 lines
Reviewed-by David Hildenbrand <[email protected]>
Re: [PATCH v1 1/3] mm: Allow deferred splitting of arbitrary large anon folios
Quoted 32 lines · Ryan Roberts
Quoted 14 lines · David Hildenbrand
Quoted 13 lines · Ryan Roberts
In preparation for the introduction of large folios for anonymous memory, we would like to be able to split them when they have unmapped subpages, in order to free those unused pages under memory pressure. So remove the artificial requirement that the large folio needed to be at least PMD-sized.
Signed-off-by Ryan Roberts <[email protected]>Reviewed-by Yu Zhao <[email protected]>Reviewed-by Yin Fengwei <[email protected]>--- mm/rmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)Quoted 16 lines
mm/rmap.c void page_remove_rmap(struct page *page, struct · line 1436
Quoted 13 lines
mm/rmap.c +1
-1@@ -1430,7 +1430,7 @@ void page_remove_rmap(struct page *page, structvm_area_struct *vma, * page of the folio is unmapped and at least one page * is still mapped. */ - if (folio_test_pmd_mappable(folio) && folio_test_anon(folio)) + if (folio_test_large(folio) && folio_test_anon(folio)) if (!compound || nr < nr_pmdmapped) deferred_split_folio(folio);!compound will always be true I guess, so
nr_pmdmapped== 0 (which will always be the case) will be ignored.
Quoted 3 lines
I don't follow why !compound will always be true. This function is
page_remove_rmap()(notfolio_remove_rmap_range()which I add in a later patch).page_remove_rmap()can work on pmd-mapped pages where compound=true is passed in.
I was talking about the folio_test_pmd_mappable() -> folio_test_large() change. For folio_test_large() && !folio_test_pmd_mappable() I expect that we'll never pass in "compound=true".
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v1 1/3] mm: Allow deferred splitting of arbitrary large anon folios
Quoted 33 lines · David Hildenbrand
Quoted 32 lines · Ryan Roberts
Quoted 30 lines · David Hildenbrand
Quoted 18 lines · Ryan Roberts
In preparation for the introduction of large folios for anonymous memory, we would like to be able to split them when they have unmapped subpages, in order to free those unused pages under memory pressure. So remove the artificial requirement that the large folio needed to be at least PMD-sized.
Signed-off-by Ryan Roberts <[email protected]>Reviewed-by Yu Zhao <[email protected]>Reviewed-by Yin Fengwei <[email protected]>---
mm/rmap.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)mm/rmap.c +0
-0mm/rmap.c void page_remove_rmap(struct page *page, struct · line 1436
Quoted 8 lines
mm/rmap.c +1
-1@@ -1430,7 +1430,7 @@ void page_remove_rmap(struct page *page, structvm_area_struct *vma, * page of the folio is unmapped and at least one page * is still mapped. */ - if (folio_test_pmd_mappable(folio) && folio_test_anon(folio)) + if (folio_test_large(folio) && folio_test_anon(folio)) if (!compound || nr < nr_pmdmapped) deferred_split_folio(folio);!compound will always be true I guess, so
nr_pmdmapped== 0 (which will always be the case) will be ignored.
Quoted 8 lines
Quoted 3 lines
I don't follow why !compound will always be true. This function is
page_remove_rmap()(notfolio_remove_rmap_range()which I add in a later patch).page_remove_rmap()can work on pmd-mapped pages where compound=true is passed in.I was talking about the
folio_test_pmd_mappable()->folio_test_large()change. Forfolio_test_large()&& !folio_test_pmd_mappable() I expect that we'll never pass in "compound=true".
Sorry David, I've been staring at the code and your comment, and I still don't understand your point. I assumed you were trying to say that compound is always false and therefore "if (!compound || nr < nr_pmdmapped)" can be removed? But its not the case that compound is always false; it will be true when called to remove a pmd-mapped compound page. What change are you suggesting, exactly?
Re: [PATCH v1 1/3] mm: Allow deferred splitting of arbitrary large anon folios
Quoted 43 lines · Ryan Roberts
Quoted 33 lines · David Hildenbrand
Quoted 32 lines · Ryan Roberts
Quoted 30 lines · David Hildenbrand
Quoted 18 lines · Ryan Roberts
In preparation for the introduction of large folios for anonymous memory, we would like to be able to split them when they have unmapped subpages, in order to free those unused pages under memory pressure. So remove the artificial requirement that the large folio needed to be at least PMD-sized.
Signed-off-by Ryan Roberts <[email protected]>Reviewed-by Yu Zhao <[email protected]>Reviewed-by Yin Fengwei <[email protected]>--- mm/rmap.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)mm/rmap.c +0
-0mm/rmap.c void page_remove_rmap(struct page *page, struct · line 1436
Quoted 8 lines
mm/rmap.c +1
-1@@ -1430,7 +1430,7 @@ void page_remove_rmap(struct page *page, structvm_area_struct *vma, * page of the folio is unmapped and at least one page * is still mapped. */ - if (folio_test_pmd_mappable(folio) && folio_test_anon(folio)) + if (folio_test_large(folio) && folio_test_anon(folio)) if (!compound || nr < nr_pmdmapped) deferred_split_folio(folio);!compound will always be true I guess, so
nr_pmdmapped== 0 (which will always be the case) will be ignored.Quoted 8 lines
Quoted 3 lines
I don't follow why !compound will always be true. This function is
page_remove_rmap()(notfolio_remove_rmap_range()which I add in a later patch).page_remove_rmap()can work on pmd-mapped pages where compound=true is passed in.I was talking about the
folio_test_pmd_mappable()->folio_test_large()change. Forfolio_test_large()&& !folio_test_pmd_mappable() I expect that we'll never pass in "compound=true".
Quoted 5 lines
Sorry David, I've been staring at the code and your comment, and I still don't understand your point. I assumed you were trying to say that compound is always false and therefore "if (!compound || nr <
nr_pmdmapped)" can be removed? But its not the case that compound is always false; it will be true when called to remove a pmd-mapped compound page.
Let me try again:
Assume, as I wrote, that we are given a folio that is "folio_test_large() && !folio_test_pmd_mappable()". That is, a folio that is *not* pmd mappable.
If it's not pmd-mappable, certainly, nr_pmdmapped == 0, and therefore, "nr < nr_pmdmapped" will never ever trigger.
The only way to have it added to the deferred split queue is, therefore "if (!compound)".
So *for these folios*, we will always pass "compound == false" to make that "if (!compound)" succeed.
Does that make sense?
Quoted 1 line
What change are you suggesting, exactly?
Oh, I never suggested a change (I even gave you my RB). I was just thinking out loud.
Signature · 3 lines
Cheers, David / dhildenb
Re: [PATCH v1 1/3] mm: Allow deferred splitting of arbitrary large anon folios
Quoted 60 lines · David Hildenbrand
Quoted 45 lines · Ryan Roberts
Quoted 39 lines · David Hildenbrand
Quoted 32 lines · Ryan Roberts
Quoted 14 lines · David Hildenbrand
Quoted 13 lines · Ryan Roberts
In preparation for the introduction of large folios for anonymous memory, we would like to be able to split them when they have unmapped subpages, in order to free those unused pages under memory pressure. So remove the artificial requirement that the large folio needed to be at least PMD-sized.
Signed-off-by Ryan Roberts <[email protected]>Reviewed-by Yu Zhao <[email protected]>Reviewed-by Yin Fengwei <[email protected]>---
mm/rmap.c| 2 +-1 file changed, 1 insertion(+), 1 deletion(-)Quoted 16 lines
mm/rmap.c void page_remove_rmap(struct page *page, struct · line 1436
Quoted 13 lines
mm/rmap.c +1
-1@@ -1430,7 +1430,7 @@ void page_remove_rmap(struct page *page, structvm_area_struct *vma, * page of the folio is unmapped and at least one page * is still mapped. */ - if (folio_test_pmd_mappable(folio) && folio_test_anon(folio)) + if (folio_test_large(folio) && folio_test_anon(folio)) if (!compound || nr < nr_pmdmapped) deferred_split_folio(folio);!compound will always be true I guess, so
nr_pmdmapped== 0 (which will always be the case) will be ignored.Quoted 5 lines
I don't follow why !compound will always be true. This function is
page_remove_rmap()(notfolio_remove_rmap_range()which I add in a later patch).page_remove_rmap()can work on pmd-mapped pages where compound=true is passed in.Quoted 4 lines
I was talking about the
folio_test_pmd_mappable()->folio_test_large()change. Forfolio_test_large()&& !folio_test_pmd_mappable() I expect that we'll never pass in "compound=true".Quoted 5 lines
Sorry David, I've been staring at the code and your comment, and I still don't understand your point. I assumed you were trying to say that compound is always false and therefore "if (!compound || nr <
nr_pmdmapped)" can be removed? But its not the case that compound is always false; it will be true when called to remove a pmd-mapped compound page.Let me try again:
Assume, as I wrote, that we are given a folio that is "
folio_test_large()&& !folio_test_pmd_mappable()". That is, a folio that is *not* pmd mappable.If it's not pmd-mappable, certainly,
nr_pmdmapped== 0, and therefore, "nr <nr_pmdmapped" will never ever trigger.
Quoted 8 lines
The only way to have it added to the deferred split queue is, therefore "if (!compound)".
So *for these folios*, we will always pass "compound == false" to make that "if (!compound)" succeed.
Does that make sense?
Yes I agree with all of this. I thought you were pointing out an issue or proposing a change to the logic. Hence my confusion.
Quoted 6 lines
Quoted 2 lines
What change are you suggesting, exactly?
Oh, I never suggested a change (I even gave you my RB). I was just thinking out loud.
PATCH 2/3 mm: Implement folio_remove_rmap_range()
In reply to Ryan RobertsLike page_remove_rmap() but batch-removes the rmap for a range of pages belonging to a folio. This can provide a small speedup due to less manipuation of the various counters. But more crucially, if removing the rmap for all pages of a folio in a batch, there is no need to (spuriously) add it to the deferred split list, which saves significant cost when there is contention for the split queue lock.
All contained pages are accounted using the order-0 folio (or base page) scheme.
--- include/linux/rmap.h | 2 ++ mm/rmap.c | 65 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+)
include/linux/rmap.h +2 -0
@@ -200,6 +200,8 @@ void page_add_file_rmap(struct page *, struct vm_area_struct *, bool compound);void page_remove_rmap (struct page *,struct vm_area_struct *,bool compound); +void folio_remove_rmap_range (struct folio *folio,struct page *page, +int nr,struct vm_area_struct *vma);void hugepage_add_anon_rmap (struct page *,struct vm_area_struct *,unsigned long address,rmap_t flags);
mm/rmap.c +65 -0
71 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH v1 2/3] mm: Implement folio_remove_rmap_range()
Quoted 10 lines · Ryan Roberts
+/*
+ *folio_remove_rmap_range- take down pte mappings from a range of pages
+ * belonging to a folio. All pages are accounted as small pages.
+ * @folio: folio that all pages belong to
+ * @page: first page in range to remove mapping from
+ * @nr: number of pages in range to remove mapping from
+ * @vma: the vm area from which the mapping is removed
+ *
+ * The caller needs to hold the pte lock.
+ */
This could stand a little reworking. How about this?
/**
* folio_remove_rmap_range - Take down PTE mappings from a range of pages.
* @folio: Folio containing all pages in range.
* @page: First page in range to unmap.
* @nr: Number of pages to unmap.
* @vma: The VM area containing the range.
*
* All pages in the range must belong to the same VMA & folio. They
* must be mapped with PTEs, not a PMD.
*
* Context: Caller holds the pte lock.
*/
Quoted 15 lines
mm/rmap.c +15
-0+void folio_remove_rmap_range (struct folio *folio,struct page *page, +int nr,struct vm_area_struct *vma) +{ +atomic_t *mapped = &folio->_nr_pages_mapped; +int nr_unmapped =0 ; +int nr_mapped; +bool last; +enum node_stat_item idx; + +if (unlikely (folio_test_hugetlb (folio))) { +VM_WARN_ON_FOLIO (1 , folio); +return ; + } + +if (!folio_test_large (folio)) {
mm/rmap.c void page_add_file_rmap(struct page *page, struct vm_area_struct *vma, · line 1396
Quoted 10 lines
mm/rmap.c +10
-0+/* Is this the page's last map to be removed? */ + last =atomic_add_negative (-1 , &page->_mapcount); + nr_unmapped = last; + }else { +for (; nr !=0 ; nr--, page++) { +/* Is this the page's last map to be removed? */ + last =atomic_add_negative (-1 , &page->_mapcount); +if (last) { +/* Page still mapped if folio mapped entirely */ + nr_mapped =atomic_dec_return_relaxed (mapped);
We're still doing one atomic op per page on the folio's nr_pages_mapped ... is it possible to batch this and use atomic_sub_return_relaxed()?
Re: [PATCH v1 2/3] mm: Implement folio_remove_rmap_range()
Quoted 14 lines · Matthew Wilcox
Quoted 10 lines · Ryan Roberts
+/*
+ *folio_remove_rmap_range- take down pte mappings from a range of pages
+ * belonging to a folio. All pages are accounted as small pages.
+ * @folio: folio that all pages belong to
+ * @page: first page in range to remove mapping from
+ * @nr: number of pages in range to remove mapping from
+ * @vma: the vm area from which the mapping is removed
+ *
+ * The caller needs to hold the pte lock.
+ */This could stand a little reworking. How about this?
Quoted 12 lines
/**
*folio_remove_rmap_range- Take down PTE mappings from a range of pages.
* @folio: Folio containing all pages in range.
* @page: First page in range to unmap.
* @nr: Number of pages to unmap.
* @vma: The VM area containing the range.
*
* All pages in the range must belong to the same VMA & folio. They
* must be mapped with PTEs, not a PMD.
*
* Context: Caller holds the pte lock.
*/
LGTM! thanks.
Quoted 21 lines
Quoted 21 lines
mm/rmap.c +20
-0+void folio_remove_rmap_range (struct folio *folio,struct page *page, +int nr,struct vm_area_struct *vma) +{ +atomic_t *mapped = &folio->_nr_pages_mapped; +int nr_unmapped =0 ; +int nr_mapped; +bool last; +enum node_stat_item idx; + +if (unlikely (folio_test_hugetlb (folio))) { +VM_WARN_ON_FOLIO (1 , folio); +return ; + } + +if (!folio_test_large (folio)) { +/* Is this the page's last map to be removed? */ + last =atomic_add_negative (-1 , &page->_mapcount); + nr_unmapped = last; + }else { +for (; nr !=0 ; nr--, page++) {
Quoted 8 lines
mm/rmap.c void page_add_file_rmap(struct page *page, struct vm_area_struct *vma, · line 1396
Quoted 5 lines
mm/rmap.c +5
-0+/* Is this the page's last map to be removed? */ + last =atomic_add_negative (-1 , &page->_mapcount); +if (last) { +/* Page still mapped if folio mapped entirely */ + nr_mapped =atomic_dec_return_relaxed (mapped);We're still doing one atomic op per page on the folio's
nr_pages_mapped... is it possible to batch this and useatomic_sub_return_relaxed()?
Good spot, something like this:
} else {
for (; nr != 0; nr--, page++) {
/* Is this the page's last map to be removed? */
last = atomic_add_negative(-1, &page->_mapcount);
if (last)
nr_unmapped++;
}
/* Pages still mapped if folio mapped entirely */
nr_mapped = atomic_sub_return_relaxed(nr_unmapped, mapped);
if (nr_mapped >= COMPOUND_MAPPED)
nr_unmapped = 0;
}Re: [PATCH v1 2/3] mm: Implement folio_remove_rmap_range()
Quoted 5 lines · Ryan Roberts
Quoted 2 lines
We're still doing one atomic op per page on the folio's
nr_pages_mapped... is it possible to batch this and useatomic_sub_return_relaxed()?Good spot, something like this:
Quoted 13 lines
} else {
for (; nr != 0; nr--, page++) {
/* Is this the page's last map to be removed? */
last = atomic_add_negative(-1, &page->_mapcount);
if (last)
nr_unmapped++;
}
/* Pages still mapped if folio mapped entirely */
nr_mapped = atomic_sub_return_relaxed(nr_unmapped, mapped);
if (nr_mapped >= COMPOUND_MAPPED)
nr_unmapped = 0;
}I think that's right, but my eyes always go slightly crossed trying to read the new mapcount scheme.
Re: [PATCH v1 2/3] mm: Implement folio_remove_rmap_range()
In reply to Ryan RobertsQuoted 34 lines · Ryan Roberts
Like
page_remove_rmap()but batch-removes the rmap for a range of pages belonging to a folio. This can provide a small speedup due to less manipuation of the various counters. But more crucially, if removing the rmap for all pages of a folio in a batch, there is no need to (spuriously) add it to the deferred split list, which saves significant cost when there is contention for the split queue lock.All contained pages are accounted using the order-0 folio (or base page) scheme.
Signed-off-by Ryan Roberts <[email protected]>--- include/linux/rmap.h | 2 ++ mm/rmap.c | 65 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+)include/linux/rmap.h +2
-0@@ -200,6 +200,8 @@ void page_add_file_rmap(struct page *, struct vm_area_struct *, bool compound);void page_remove_rmap (struct page *,struct vm_area_struct *,bool compound); +void folio_remove_rmap_range (struct folio *folio,struct page *page, +int nr,struct vm_area_struct *vma);void hugepage_add_anon_rmap (struct page *,struct vm_area_struct *,unsigned long address,rmap_t flags);mm/rmap.c +0
-0
mm/rmap.c void page_add_file_rmap(struct page *page, struct vm_area_struct *vma, · line 1367
Quoted 9 lines
mm/rmap.c +6
-0@@ -1359,6 +1359,71 @@ void page_add_file_rmap(struct page *page, struct vm_area_struct *vma,mlock_vma_folio (folio, vma, compound); } +/* +* folio_remove_rmap_range - take down pte mappings from a range of pages +* belonging to a folio. All pages are accounted as small pages. +* @folio: folio that all pages belong to +* @page: first page in range to remove mapping from +* @nr: number of pages in range to remove mapping from
We might need some checks to make sure [page, page+nr] is in the range of the folio. Something like:
page >= &folio->page && page + nr < (&folio->page + folio_nr_pages(folio))
Quoted 64 lines
mm/rmap.c +59
-0+* @vma: the vm area from which the mapping is removed +* +* The caller needs to hold the pte lock. +*/ +void folio_remove_rmap_range (struct folio *folio,struct page *page, +int nr,struct vm_area_struct *vma) +{ +atomic_t *mapped = &folio->_nr_pages_mapped; +int nr_unmapped =0 ; +int nr_mapped; +bool last; +enum node_stat_item idx; + +if (unlikely (folio_test_hugetlb (folio))) { +VM_WARN_ON_FOLIO (1 , folio); +return ; + } + +if (!folio_test_large (folio)) { +/* Is this the page's last map to be removed? */ + last =atomic_add_negative (-1 , &page->_mapcount); + nr_unmapped = last; + }else { +for (; nr !=0 ; nr--, page++) { +/* Is this the page's last map to be removed? */ + last =atomic_add_negative (-1 , &page->_mapcount); +if (last) { +/* Page still mapped if folio mapped entirely */ + nr_mapped =atomic_dec_return_relaxed (mapped); +if (nr_mapped <COMPOUND_MAPPED ) + nr_unmapped++; + } + } + } + +if (nr_unmapped) { + idx =folio_test_anon (folio) ?NR_ANON_MAPPED :NR_FILE_MAPPED ; +__lruvec_stat_mod_folio (folio, idx, -nr_unmapped); + +/* +* Queue anon THP for deferred split if we have just unmapped at +* least 1 page, while at least 1 page remains mapped. +*/ +if (folio_test_large (folio) &&folio_test_anon (folio)) +if (nr_mapped) +deferred_split_folio (folio); + } + +/* +* It would be tidy to reset folio_test_anon mapping when fully +* unmapped, but that might overwrite a racing page_add_anon_rmap +* which increments mapcount after us but sets mapping before us: +* so leave the reset to free_pages_prepare, and remember that +* it's only reliable while mapped. +*/ + +munlock_vma_folio (folio, vma, false); +} +/** * page_remove_rmap - take down pte mapping from a page * @page: page to remove mapping from Signature · 1 line
2.25.1
Everything else looks good to me. Reviewed-by: Zi Yan <[email protected]>
--
Best Regards,
Yan, Zi
Re: [PATCH v1 2/3] mm: Implement folio_remove_rmap_range()
Quoted 49 lines · Zi Yan
Quoted 34 lines · Ryan Roberts
Like
page_remove_rmap()but batch-removes the rmap for a range of pages belonging to a folio. This can provide a small speedup due to less manipuation of the various counters. But more crucially, if removing the rmap for all pages of a folio in a batch, there is no need to (spuriously) add it to the deferred split list, which saves significant cost when there is contention for the split queue lock.All contained pages are accounted using the order-0 folio (or base page) scheme.
Signed-off-by Ryan Roberts <[email protected]>--- include/linux/rmap.h | 2 ++ mm/rmap.c | 65 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+)include/linux/rmap.h +2
-0@@ -200,6 +200,8 @@ void page_add_file_rmap(struct page *, struct vm_area_struct *, bool compound);void page_remove_rmap (struct page *,struct vm_area_struct *,bool compound); +void folio_remove_rmap_range (struct folio *folio,struct page *page, +int nr,struct vm_area_struct *vma);void hugepage_add_anon_rmap (struct page *,struct vm_area_struct *,unsigned long address,rmap_t flags);mm/rmap.c +0
-0mm/rmap.c void page_add_file_rmap(struct page *page, struct vm_area_struct *vma, · line 1367
Quoted 9 lines
mm/rmap.c +6
-0@@ -1359,6 +1359,71 @@ void page_add_file_rmap(struct page *page, struct vm_area_struct *vma,mlock_vma_folio (folio, vma, compound); } +/* +* folio_remove_rmap_range - take down pte mappings from a range of pages +* belonging to a folio. All pages are accounted as small pages. +* @folio: folio that all pages belong to +* @page: first page in range to remove mapping from +* @nr: number of pages in range to remove mapping from We might need some checks to make sure [page, page+nr] is in the range of the folio. Something like:
Quoted 1 line
page >=
&folio->page&& page + nr < (&folio->page+folio_nr_pages(folio))
No problem. Is a VM_WARN_ON() appropriate for something like this?
Quoted 71 lines
Quoted 65 lines
mm/rmap.c +59
-0+* @vma: the vm area from which the mapping is removed +* +* The caller needs to hold the pte lock. +*/ +void folio_remove_rmap_range (struct folio *folio,struct page *page, +int nr,struct vm_area_struct *vma) +{ +atomic_t *mapped = &folio->_nr_pages_mapped; +int nr_unmapped =0 ; +int nr_mapped; +bool last; +enum node_stat_item idx; + +if (unlikely (folio_test_hugetlb (folio))) { +VM_WARN_ON_FOLIO (1 , folio); +return ; + } + +if (!folio_test_large (folio)) { +/* Is this the page's last map to be removed? */ + last =atomic_add_negative (-1 , &page->_mapcount); + nr_unmapped = last; + }else { +for (; nr !=0 ; nr--, page++) { +/* Is this the page's last map to be removed? */ + last =atomic_add_negative (-1 , &page->_mapcount); +if (last) { +/* Page still mapped if folio mapped entirely */ + nr_mapped =atomic_dec_return_relaxed (mapped); +if (nr_mapped <COMPOUND_MAPPED ) + nr_unmapped++; + } + } + } + +if (nr_unmapped) { + idx =folio_test_anon (folio) ?NR_ANON_MAPPED :NR_FILE_MAPPED ; +__lruvec_stat_mod_folio (folio, idx, -nr_unmapped); + +/* +* Queue anon THP for deferred split if we have just unmapped at +* least 1 page, while at least 1 page remains mapped. +*/ +if (folio_test_large (folio) &&folio_test_anon (folio)) +if (nr_mapped) +deferred_split_folio (folio); + } + +/* +* It would be tidy to reset folio_test_anon mapping when fully +* unmapped, but that might overwrite a racing page_add_anon_rmap +* which increments mapcount after us but sets mapping before us: +* so leave the reset to free_pages_prepare, and remember that +* it's only reliable while mapped. +*/ + +munlock_vma_folio (folio, vma, false); +} +/** * page_remove_rmap - take down pte mapping from a page * @page: page to remove mapping from Signature · 1 line
2.25.1Everything else looks good to me. Reviewed-by: Zi Yan <[email protected]>
--
Best Regards,
Yan, Zi
Re: [PATCH v1 2/3] mm: Implement folio_remove_rmap_range()
Quoted 50 lines · Ryan Roberts
Quoted 49 lines · Zi Yan
Quoted 34 lines · Ryan Roberts
Like
page_remove_rmap()but batch-removes the rmap for a range of pages belonging to a folio. This can provide a small speedup due to less manipuation of the various counters. But more crucially, if removing the rmap for all pages of a folio in a batch, there is no need to (spuriously) add it to the deferred split list, which saves significant cost when there is contention for the split queue lock.All contained pages are accounted using the order-0 folio (or base page) scheme.
Signed-off-by Ryan Roberts <[email protected]>--- include/linux/rmap.h | 2 ++ mm/rmap.c | 65 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+)include/linux/rmap.h +2
-0@@ -200,6 +200,8 @@ void page_add_file_rmap(struct page *, struct vm_area_struct *, bool compound);void page_remove_rmap (struct page *,struct vm_area_struct *,bool compound); +void folio_remove_rmap_range (struct folio *folio,struct page *page, +int nr,struct vm_area_struct *vma);void hugepage_add_anon_rmap (struct page *,struct vm_area_struct *,unsigned long address,rmap_t flags);mm/rmap.c +0
-0mm/rmap.c void page_add_file_rmap(struct page *page, struct vm_area_struct *vma, · line 1367
Quoted 9 lines
mm/rmap.c +6
-0@@ -1359,6 +1359,71 @@ void page_add_file_rmap(struct page *page, struct vm_area_struct *vma,mlock_vma_folio (folio, vma, compound); } +/* +* folio_remove_rmap_range - take down pte mappings from a range of pages +* belonging to a folio. All pages are accounted as small pages. +* @folio: folio that all pages belong to +* @page: first page in range to remove mapping from +* @nr: number of pages in range to remove mapping from We might need some checks to make sure [page, page+nr] is in the range of the folio. Something like:
Quoted 3 lines
Quoted 1 line
page >=
&folio->page&& page + nr < (&folio->page+folio_nr_pages(folio))No problem. Is a
VM_WARN_ON()appropriate for something like this?
VM_WARN_ON_ONCE() might be better.
--
Best Regards,
Yan, Zi
Re: [PATCH v1 2/3] mm: Implement folio_remove_rmap_range()
In reply to Ryan RobertsQuoted 11 lines · Ryan Roberts
Like
page_remove_rmap()but batch-removes the rmap for a range of pages belonging to a folio. This can provide a small speedup due to less manipuation of the various counters. But more crucially, if removing the rmap for all pages of a folio in a batch, there is no need to (spuriously) add it to the deferred split list, which saves significant cost when there is contention for the split queue lock.All contained pages are accounted using the order-0 folio (or base page) scheme.
Signed-off-by Ryan Roberts <[email protected]>
Regards
Yin, Fengwei
Quoted 94 lines
--- include/linux/rmap.h | 2 ++ mm/rmap.c | 65 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+)include/linux/rmap.h +2
-0@@ -200,6 +200,8 @@ void page_add_file_rmap(struct page *, struct vm_area_struct *, bool compound);void page_remove_rmap (struct page *,struct vm_area_struct *,bool compound); +void folio_remove_rmap_range (struct folio *folio,struct page *page, +int nr,struct vm_area_struct *vma);void hugepage_add_anon_rmap (struct page *,struct vm_area_struct *,unsigned long address,rmap_t flags);mm/rmap.c +65
-0@@ -1359,6 +1359,71 @@ void page_add_file_rmap(struct page *page, struct vm_area_struct *vma,mlock_vma_folio (folio, vma, compound); } +/* +* folio_remove_rmap_range - take down pte mappings from a range of pages +* belonging to a folio. All pages are accounted as small pages. +* @folio: folio that all pages belong to +* @page: first page in range to remove mapping from +* @nr: number of pages in range to remove mapping from +* @vma: the vm area from which the mapping is removed +* +* The caller needs to hold the pte lock. +*/ +void folio_remove_rmap_range (struct folio *folio,struct page *page, +int nr,struct vm_area_struct *vma) +{ +atomic_t *mapped = &folio->_nr_pages_mapped; +int nr_unmapped =0 ; +int nr_mapped; +bool last; +enum node_stat_item idx; + +if (unlikely (folio_test_hugetlb (folio))) { +VM_WARN_ON_FOLIO (1 , folio); +return ; + } + +if (!folio_test_large (folio)) { +/* Is this the page's last map to be removed? */ + last =atomic_add_negative (-1 , &page->_mapcount); + nr_unmapped = last; + }else { +for (; nr !=0 ; nr--, page++) { +/* Is this the page's last map to be removed? */ + last =atomic_add_negative (-1 , &page->_mapcount); +if (last) { +/* Page still mapped if folio mapped entirely */ + nr_mapped =atomic_dec_return_relaxed (mapped); +if (nr_mapped <COMPOUND_MAPPED ) + nr_unmapped++; + } + } + } + +if (nr_unmapped) { + idx =folio_test_anon (folio) ?NR_ANON_MAPPED :NR_FILE_MAPPED ; +__lruvec_stat_mod_folio (folio, idx, -nr_unmapped); + +/* +* Queue anon THP for deferred split if we have just unmapped at +* least 1 page, while at least 1 page remains mapped. +*/ +if (folio_test_large (folio) &&folio_test_anon (folio)) +if (nr_mapped) +deferred_split_folio (folio); + } + +/* +* It would be tidy to reset folio_test_anon mapping when fully +* unmapped, but that might overwrite a racing page_add_anon_rmap +* which increments mapcount after us but sets mapping before us: +* so leave the reset to free_pages_prepare, and remember that +* it's only reliable while mapped. +*/ + +munlock_vma_folio (folio, vma, false); +} +/** * page_remove_rmap - take down pte mapping from a page * @page: page to remove mapping from
Re: [PATCH v1 2/3] mm: Implement folio_remove_rmap_range()
In reply to Ryan RobertsQuoted 74 lines · Ryan Roberts
Like
page_remove_rmap()but batch-removes the rmap for a range of pages belonging to a folio. This can provide a small speedup due to less manipuation of the various counters. But more crucially, if removing the rmap for all pages of a folio in a batch, there is no need to (spuriously) add it to the deferred split list, which saves significant cost when there is contention for the split queue lock.All contained pages are accounted using the order-0 folio (or base page) scheme.
Signed-off-by Ryan Roberts <[email protected]>--- include/linux/rmap.h | 2 ++ mm/rmap.c | 65 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+)include/linux/rmap.h +2
-0@@ -200,6 +200,8 @@ void page_add_file_rmap(struct page *, struct vm_area_struct *, bool compound);void page_remove_rmap (struct page *,struct vm_area_struct *,bool compound); +void folio_remove_rmap_range (struct folio *folio,struct page *page, +int nr,struct vm_area_struct *vma);void hugepage_add_anon_rmap (struct page *,struct vm_area_struct *,unsigned long address,rmap_t flags);mm/rmap.c +37
-0@@ -1359,6 +1359,71 @@ void page_add_file_rmap(struct page *page, struct vm_area_struct *vma,mlock_vma_folio (folio, vma, compound); } +/* +* folio_remove_rmap_range - take down pte mappings from a range of pages +* belonging to a folio. All pages are accounted as small pages. +* @folio: folio that all pages belong to +* @page: first page in range to remove mapping from +* @nr: number of pages in range to remove mapping from +* @vma: the vm area from which the mapping is removed +* +* The caller needs to hold the pte lock. +*/ +void folio_remove_rmap_range (struct folio *folio,struct page *page, +int nr,struct vm_area_struct *vma) +{ +atomic_t *mapped = &folio->_nr_pages_mapped; +int nr_unmapped =0 ; +int nr_mapped; +bool last; +enum node_stat_item idx; + +if (unlikely (folio_test_hugetlb (folio))) { +VM_WARN_ON_FOLIO (1 , folio); +return ; + } + +if (!folio_test_large (folio)) { +/* Is this the page's last map to be removed? */ + last =atomic_add_negative (-1 , &page->_mapcount); + nr_unmapped = last; + }else { +for (; nr !=0 ; nr--, page++) { +/* Is this the page's last map to be removed? */ + last =atomic_add_negative (-1 , &page->_mapcount); +if (last) { +/* Page still mapped if folio mapped entirely */ + nr_mapped =atomic_dec_return_relaxed (mapped); +if (nr_mapped <COMPOUND_MAPPED ) + nr_unmapped++;
mm/rmap.c void page_add_file_rmap(struct page *page, struct vm_area_struct *vma, · line 1408
Quoted 10 lines
mm/rmap.c +10
-0+ } + } + } + +if (nr_unmapped) { + idx =folio_test_anon (folio) ?NR_ANON_MAPPED :NR_FILE_MAPPED ; +__lruvec_stat_mod_folio (folio, idx, -nr_unmapped); + +/* +* Queue anon THP for deferred split if we have just unmapped at
Just some nitpicks. So feel free to ignore.
s/anon THP/large folio/ ?
mm/rmap.c void page_add_file_rmap(struct page *page, struct vm_area_struct *vma, · line 1412
Quoted 4 lines
mm/rmap.c +4
-0+* least 1 page, while at least 1 page remains mapped. +*/ +if (folio_test_large (folio) &&folio_test_anon (folio)) +if (nr_mapped)
if (folio_test_large(folio) && folio_test_anon(folio) && nr_mapped) ?
Quoted 7 lines
mm/rmap.c +7
-0+deferred_split_folio (folio); + } + +/* +* It would be tidy to reset folio_test_anon mapping when fully +* unmapped, but that might overwrite a racing page_add_anon_rmap +* which increments mapcount after us but sets mapping before us:
mm/rmap.c void page_add_file_rmap(struct page *page, struct vm_area_struct *vma, · line 1429
Quoted 10 lines
mm/rmap.c +7
-0+* so leave the reset to free_pages_prepare, and remember that +* it's only reliable while mapped. +*/ + +munlock_vma_folio (folio, vma, false); +} +/** * page_remove_rmap - take down pte mapping from a page * @page: page to remove mapping from
Best Regards,
Huang, Ying
Re: [PATCH v1 2/3] mm: Implement folio_remove_rmap_range()
Quoted 89 lines · Ying
Quoted 74 lines · Ryan Roberts
Like
page_remove_rmap()but batch-removes the rmap for a range of pages belonging to a folio. This can provide a small speedup due to less manipuation of the various counters. But more crucially, if removing the rmap for all pages of a folio in a batch, there is no need to (spuriously) add it to the deferred split list, which saves significant cost when there is contention for the split queue lock.All contained pages are accounted using the order-0 folio (or base page) scheme.
Signed-off-by Ryan Roberts <[email protected]>--- include/linux/rmap.h | 2 ++ mm/rmap.c | 65 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+)include/linux/rmap.h +2
-0@@ -200,6 +200,8 @@ void page_add_file_rmap(struct page *, struct vm_area_struct *, bool compound);void page_remove_rmap (struct page *,struct vm_area_struct *,bool compound); +void folio_remove_rmap_range (struct folio *folio,struct page *page, +int nr,struct vm_area_struct *vma);void hugepage_add_anon_rmap (struct page *,struct vm_area_struct *,unsigned long address,rmap_t flags);mm/rmap.c +37
-0@@ -1359,6 +1359,71 @@ void page_add_file_rmap(struct page *page, struct vm_area_struct *vma,mlock_vma_folio (folio, vma, compound); } +/* +* folio_remove_rmap_range - take down pte mappings from a range of pages +* belonging to a folio. All pages are accounted as small pages. +* @folio: folio that all pages belong to +* @page: first page in range to remove mapping from +* @nr: number of pages in range to remove mapping from +* @vma: the vm area from which the mapping is removed +* +* The caller needs to hold the pte lock. +*/ +void folio_remove_rmap_range (struct folio *folio,struct page *page, +int nr,struct vm_area_struct *vma) +{ +atomic_t *mapped = &folio->_nr_pages_mapped; +int nr_unmapped =0 ; +int nr_mapped; +bool last; +enum node_stat_item idx; + +if (unlikely (folio_test_hugetlb (folio))) { +VM_WARN_ON_FOLIO (1 , folio); +return ; + } + +if (!folio_test_large (folio)) { +/* Is this the page's last map to be removed? */ + last =atomic_add_negative (-1 , &page->_mapcount); + nr_unmapped = last; + }else { +for (; nr !=0 ; nr--, page++) { +/* Is this the page's last map to be removed? */ + last =atomic_add_negative (-1 , &page->_mapcount); +if (last) { +/* Page still mapped if folio mapped entirely */ + nr_mapped =atomic_dec_return_relaxed (mapped); +if (nr_mapped <COMPOUND_MAPPED ) + nr_unmapped++;mm/rmap.c void page_add_file_rmap(struct page *page, struct vm_area_struct *vma, · line 1408
Quoted 10 lines
mm/rmap.c +10
-0+ } + } + } + +if (nr_unmapped) { + idx =folio_test_anon (folio) ?NR_ANON_MAPPED :NR_FILE_MAPPED ; +__lruvec_stat_mod_folio (folio, idx, -nr_unmapped); + +/* +* Queue anon THP for deferred split if we have just unmapped at Just some nitpicks. So feel free to ignore.
Quoted 1 line
s/anon THP/large folio/ ?
ACK
Quoted 7 lines
mm/rmap.c void page_add_file_rmap(struct page *page, struct vm_area_struct *vma, · line 1412
Quoted 5 lines
mm/rmap.c +4
-0+* least 1 page, while at least 1 page remains mapped. +*/ +if (folio_test_large (folio) &&folio_test_anon (folio)) +if (nr_mapped)if (folio_test_large(folio) && folio_test_anon(folio) && nr_mapped) ?
ACK : I'll make these changes for the next version.
Quoted 21 lines
Quoted 8 lines
mm/rmap.c +7
-0+deferred_split_folio (folio); + } + +/* +* It would be tidy to reset folio_test_anon mapping when fully +* unmapped, but that might overwrite a racing page_add_anon_rmap +* which increments mapcount after us but sets mapping before us: mm/rmap.c void page_add_file_rmap(struct page *page, struct vm_area_struct *vma, · line 1429
Quoted 10 lines
mm/rmap.c +7
-0+* so leave the reset to free_pages_prepare, and remember that +* it's only reliable while mapped. +*/ + +munlock_vma_folio (folio, vma, false); +} +/** * page_remove_rmap - take down pte mapping from a page * @page: page to remove mapping from Best Regards,
Huang, Ying
Re: [PATCH v1 2/3] mm: Implement folio_remove_rmap_range()
In reply to Ryan RobertsQuoted 39 lines · Ryan Roberts
Like
page_remove_rmap()but batch-removes the rmap for a range of pages belonging to a folio. This can provide a small speedup due to less manipuation of the various counters. But more crucially, if removing the rmap for all pages of a folio in a batch, there is no need to (spuriously) add it to the deferred split list, which saves significant cost when there is contention for the split queue lock.All contained pages are accounted using the order-0 folio (or base page) scheme.
Signed-off-by Ryan Roberts <[email protected]>--- include/linux/rmap.h | 2 ++ mm/rmap.c | 65 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+)include/linux/rmap.h +2
-0@@ -200,6 +200,8 @@ void page_add_file_rmap(struct page *, struct vm_area_struct *, bool compound);void page_remove_rmap (struct page *,struct vm_area_struct *,bool compound); +void folio_remove_rmap_range (struct folio *folio,struct page *page, +int nr,struct vm_area_struct *vma);void hugepage_add_anon_rmap (struct page *,struct vm_area_struct *,unsigned long address,rmap_t flags);mm/rmap.c +2
-0@@ -1359,6 +1359,71 @@ void page_add_file_rmap(struct page *page, struct vm_area_struct *vma,mlock_vma_folio (folio, vma, compound); } +/* +* folio_remove_rmap_range - take down pte mappings from a range of pages
mm/rmap.c void page_add_file_rmap(struct page *page, struct vm_area_struct *vma, · line 1373
Quoted 10 lines
mm/rmap.c +10
-0+* belonging to a folio. All pages are accounted as small pages. +* @folio: folio that all pages belong to +* @page: first page in range to remove mapping from +* @nr: number of pages in range to remove mapping from +* @vma: the vm area from which the mapping is removed +* +* The caller needs to hold the pte lock. +*/ +void folio_remove_rmap_range (struct folio *folio,struct page *page, +int nr,struct vm_area_struct *vma)
Can we call folio_remove_ramp_range() in page_remove_rmap() if !compound? This can give us some opportunities to reduce code duplication?
Best Regards,
Huang, Ying
Quoted 56 lines
mm/rmap.c +53
-0+{ +atomic_t *mapped = &folio->_nr_pages_mapped; +int nr_unmapped =0 ; +int nr_mapped; +bool last; +enum node_stat_item idx; + +if (unlikely (folio_test_hugetlb (folio))) { +VM_WARN_ON_FOLIO (1 , folio); +return ; + } + +if (!folio_test_large (folio)) { +/* Is this the page's last map to be removed? */ + last =atomic_add_negative (-1 , &page->_mapcount); + nr_unmapped = last; + }else { +for (; nr !=0 ; nr--, page++) { +/* Is this the page's last map to be removed? */ + last =atomic_add_negative (-1 , &page->_mapcount); +if (last) { +/* Page still mapped if folio mapped entirely */ + nr_mapped =atomic_dec_return_relaxed (mapped); +if (nr_mapped <COMPOUND_MAPPED ) + nr_unmapped++; + } + } + } + +if (nr_unmapped) { + idx =folio_test_anon (folio) ?NR_ANON_MAPPED :NR_FILE_MAPPED ; +__lruvec_stat_mod_folio (folio, idx, -nr_unmapped); + +/* +* Queue anon THP for deferred split if we have just unmapped at +* least 1 page, while at least 1 page remains mapped. +*/ +if (folio_test_large (folio) &&folio_test_anon (folio)) +if (nr_mapped) +deferred_split_folio (folio); + } + +/* +* It would be tidy to reset folio_test_anon mapping when fully +* unmapped, but that might overwrite a racing page_add_anon_rmap +* which increments mapcount after us but sets mapping before us: +* so leave the reset to free_pages_prepare, and remember that +* it's only reliable while mapped. +*/ + +munlock_vma_folio (folio, vma, false); +} +/** * page_remove_rmap - take down pte mapping from a page * @page: page to remove mapping from
Re: [PATCH v1 2/3] mm: Implement folio_remove_rmap_range()
Quoted 39 lines · Ying
Quoted 37 lines · Ryan Roberts
Like
page_remove_rmap()but batch-removes the rmap for a range of pages belonging to a folio. This can provide a small speedup due to less manipuation of the various counters. But more crucially, if removing the rmap for all pages of a folio in a batch, there is no need to (spuriously) add it to the deferred split list, which saves significant cost when there is contention for the split queue lock.All contained pages are accounted using the order-0 folio (or base page) scheme.
Signed-off-by Ryan Roberts <[email protected]>--- include/linux/rmap.h | 2 ++ mm/rmap.c | 65 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+)include/linux/rmap.h +2
-0@@ -200,6 +200,8 @@ void page_add_file_rmap(struct page *, struct vm_area_struct *, bool compound);void page_remove_rmap (struct page *,struct vm_area_struct *,bool compound); +void folio_remove_rmap_range (struct folio *folio,struct page *page, +int nr,struct vm_area_struct *vma);void hugepage_add_anon_rmap (struct page *,struct vm_area_struct *,unsigned long address,rmap_t flags);mm/rmap.c +0
-0@@ -1359,6 +1359,71 @@ void page_add_file_rmap(struct page *page, struct vm_area_struct *vma,mlock_vma_folio (folio, vma, compound); }
Quoted 16 lines
mm/rmap.c void page_add_file_rmap(struct page *page, struct vm_area_struct *vma, · line 1373
Quoted 12 lines
mm/rmap.c +12
-0+/* +* folio_remove_rmap_range - take down pte mappings from a range of pages +* belonging to a folio. All pages are accounted as small pages. +* @folio: folio that all pages belong to +* @page: first page in range to remove mapping from +* @nr: number of pages in range to remove mapping from +* @vma: the vm area from which the mapping is removed +* +* The caller needs to hold the pte lock. +*/ +void folio_remove_rmap_range (struct folio *folio,struct page *page, +int nr,struct vm_area_struct *vma)Can we call
folio_remove_ramp_range()inpage_remove_rmap()if !compound? This can give us some opportunities to reduce code duplication?
I considered that, but if felt like the savings were pretty small so my opinion was that it was cleaner not to do this. This is the best I came up with. Perhaps you can see further improvements?
void page_remove_rmap(struct page *page, struct vm_area_struct *vma,
bool compound)
{
struct folio *folio = page_folio(page);
atomic_t *mapped = &folio->_nr_pages_mapped;
int nr = 0, nr_pmdmapped = 0;
bool last;
enum node_stat_item idx;
VM_BUG_ON_PAGE(compound && !PageHead(page), page);
/* Hugetlb pages are not counted in NR_*MAPPED */
if (unlikely(folio_test_hugetlb(folio))) {
/* hugetlb pages are always mapped with pmds */
atomic_dec(&folio->_entire_mapcount);
return;
}
/* Is page being unmapped by PTE? Is this its last map to be removed? */
if (likely(!compound)) {
folio_remove_rmap_range(folio, page, 1, vma);
return;
} else if (folio_test_pmd_mappable(folio)) {
/* That test is redundant: it's for safety or to optimize out */
last = atomic_add_negative(-1, &folio->_entire_mapcount);
if (last) {
nr = atomic_sub_return_relaxed(COMPOUND_MAPPED, mapped);
if (likely(nr < COMPOUND_MAPPED)) {
nr_pmdmapped = folio_nr_pages(folio);
nr = nr_pmdmapped - (nr & FOLIO_PAGES_MAPPED);
/* Raced ahead of another remove and an add? */
if (unlikely(nr < 0))
nr = 0;
} else {
/* An add of COMPOUND_MAPPED raced ahead */
nr = 0;
}
}
}
if (nr_pmdmapped) {
if (folio_test_anon(folio))
idx = NR_ANON_THPS;
else if (folio_test_swapbacked(folio))
idx = NR_SHMEM_PMDMAPPED;
else
idx = NR_FILE_PMDMAPPED;
__lruvec_stat_mod_folio(folio, idx, -nr_pmdmapped);
}
if (nr) {
idx = folio_test_anon(folio) ? NR_ANON_MAPPED : NR_FILE_MAPPED;
__lruvec_stat_mod_folio(folio, idx, -nr);
/*
* Queue anon THP for deferred split if at least one
* page of the folio is unmapped and at least one page
* is still mapped.
*/
if (folio_test_anon(folio) && nr < nr_pmdmapped)
deferred_split_folio(folio);
}
/*
* It would be tidy to reset folio_test_anon mapping when fully
* unmapped, but that might overwrite a racing page_add_anon_rmap
* which increments mapcount after us but sets mapping before us:
* so leave the reset to free_pages_prepare, and remember that
* it's only reliable while mapped.
*/
munlock_vma_folio(folio, vma, compound);
}Quoted 4 lines
Best Regards,
Huang, Ying
PATCH 3/3 mm: Batch-zap large anonymous folio PTE mappings
In reply to Ryan RobertsThis allows batching the rmap removal with folio_remove_rmap_range(), which means we avoid spuriously adding a partially unmapped folio to the deferrred split queue in the common case, which reduces split queue lock contention.
Previously each page was removed from the rmap individually with page_remove_rmap(). If the first page belonged to a large folio, this would cause page_remove_rmap() to conclude that the folio was now partially mapped and add the folio to the deferred split queue. But subsequent calls would cause the folio to become fully unmapped, meaning there is no value to adding it to the split queue.
--- mm/memory.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+)
mm/memory.c +119 -0
@@ -1391,6 +1391,95 @@ zap_install_uffd_wp_if_needed(struct vm_area_struct *vma,pte_install_uffd_wp_if_needed (vma, addr, pte, pteval); } +static inline unsigned long page_addr (struct page *page, +struct page *anchor,unsigned long anchor_addr) +{ +unsigned long offset; +unsigned long addr; + + offset = (page_to_pfn (page) -page_to_pfn (anchor)) <<PAGE_SHIFT ; + addr = anchor_addr + offset; + +if (anchor > page) { +if (addr > anchor_addr) +return 0 ; + }else { +if (addr < anchor_addr) +return ULONG_MAX ; + } + +return addr; +} + +static int calc_anon_folio_map_pgcount (struct folio *folio, +struct page *page,pte_t *pte, +unsigned long addr,unsigned long end) +{ +pte_t ptent; +int floops; +int i; +unsigned long pfn; + + end =min (page_addr (&folio->page +folio_nr_pages (folio), page, addr), + end); + floops = (end - addr) >>PAGE_SHIFT ; + pfn =page_to_pfn (page); + pfn++; + pte++; + +for (i =1 ; i < floops; i++) { + ptent =ptep_get (pte); + +if (!pte_present (ptent) || +pte_pfn (ptent) != pfn) { +return i; + } + + pfn++; + pte++; + } + +return floops; +} + +static unsigned long zap_anon_pte_range (struct mmu_gather *tlb, +struct vm_area_struct *vma, +struct page *page,pte_t *pte, +unsigned long addr,unsigned long end, +bool *full_out) +{ +struct folio *folio =page_folio (page); +struct mm_struct *mm = tlb->mm; +pte_t ptent; +int pgcount; +int i; +bool full; + + pgcount =calc_anon_folio_map_pgcount (folio, page, pte, addr, end); + +for (i =0 ; i < pgcount;) { + ptent =ptep_get_and_clear_full (mm, addr, pte, tlb->fullmm); +tlb_remove_tlb_entry (tlb, pte, addr); + full =__tlb_remove_page (tlb, page,0 ); + +if (unlikely (page_mapcount (page) <1 )) +print_bad_pte (vma, addr, ptent, page); + + i++; + page++; + pte++; + addr +=PAGE_SIZE ; + +if (unlikely (full)) +break ; + } + +folio_remove_rmap_range (folio, page - i, i, vma); + + *full_out = full; +return i; +} + static unsigned long zap_pte_range(struct mmu_gather *tlb, struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr, unsigned long end,
@@ -1428,6 +1517,36 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, page =vm_normal_page (vma, addr, ptent);if (unlikely (!should_zap_page (details, page)))continue ; + +/* +* Batch zap large anonymous folio mappings. This allows +* batching the rmap removal, which means we avoid +* spuriously adding a partially unmapped folio to the +* deferrred split queue in the common case, which +* reduces split queue lock contention. Require the VMA +* to be anonymous to ensure that none of the PTEs in +* the range require zap_install_uffd_wp_if_needed(). +*/ +if (page &&PageAnon (page) &&vma_is_anonymous (vma)) { +bool full; +int pgcount; + + pgcount =zap_anon_pte_range (tlb, vma, + page, pte, addr, end, &full); + + rss[mm_counter (page)] -= pgcount; + pgcount--; + pte += pgcount; + addr += pgcount <<PAGE_SHIFT ; + +if (unlikely (full)) { + force_flush =1 ; + addr +=PAGE_SIZE ; +break ; + } +continue ; + } + ptent =ptep_get_and_clear_full (mm, addr, pte, tlb->fullmm);tlb_remove_tlb_entry (tlb, pte, addr);
Signature · 1 line
2.25.1
Re: [PATCH v1 3/3] mm: Batch-zap large anonymous folio PTE mappings
Quoted 127 lines · Ryan Roberts
This allows batching the rmap removal with
folio_remove_rmap_range(), which means we avoid spuriously adding a partially unmapped folio to the deferrred split queue in the common case, which reduces split queue lock contention.Previously each page was removed from the rmap individually with
page_remove_rmap(). If the first page belonged to a large folio, this would causepage_remove_rmap()to conclude that the folio was now partially mapped and add the folio to the deferred split queue. But subsequent calls would cause the folio to become fully unmapped, meaning there is no value to adding it to the split queue.Signed-off-by Ryan Roberts <[email protected]>--- mm/memory.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+)mm/memory.c +95
-0@@ -1391,6 +1391,95 @@ zap_install_uffd_wp_if_needed(struct vm_area_struct *vma,pte_install_uffd_wp_if_needed (vma, addr, pte, pteval); } +static inline unsigned long page_addr (struct page *page, +struct page *anchor,unsigned long anchor_addr) +{ +unsigned long offset; +unsigned long addr; + + offset = (page_to_pfn (page) -page_to_pfn (anchor)) <<PAGE_SHIFT ; + addr = anchor_addr + offset; + +if (anchor > page) { +if (addr > anchor_addr) +return 0 ; + }else { +if (addr < anchor_addr) +return ULONG_MAX ; + } + +return addr; +} + +static int calc_anon_folio_map_pgcount (struct folio *folio, +struct page *page,pte_t *pte, +unsigned long addr,unsigned long end) +{ +pte_t ptent; +int floops; +int i; +unsigned long pfn; + + end =min (page_addr (&folio->page +folio_nr_pages (folio), page, addr), + end); + floops = (end - addr) >>PAGE_SHIFT ; + pfn =page_to_pfn (page); + pfn++; + pte++; + +for (i =1 ; i < floops; i++) { + ptent =ptep_get (pte); + +if (!pte_present (ptent) || +pte_pfn (ptent) != pfn) { +return i; + } + + pfn++; + pte++; + } + +return floops; +} + +static unsigned long zap_anon_pte_range (struct mmu_gather *tlb, +struct vm_area_struct *vma, +struct page *page,pte_t *pte, +unsigned long addr,unsigned long end, +bool *full_out) +{ +struct folio *folio =page_folio (page); +struct mm_struct *mm = tlb->mm; +pte_t ptent; +int pgcount; +int i; +bool full; + + pgcount =calc_anon_folio_map_pgcount (folio, page, pte, addr, end); + +for (i =0 ; i < pgcount;) { + ptent =ptep_get_and_clear_full (mm, addr, pte, tlb->fullmm); +tlb_remove_tlb_entry (tlb, pte, addr); + full =__tlb_remove_page (tlb, page,0 ); + +if (unlikely (page_mapcount (page) <1 )) +print_bad_pte (vma, addr, ptent, page); + + i++; + page++; + pte++; + addr +=PAGE_SIZE ; + +if (unlikely (full)) +break ; + } + +folio_remove_rmap_range (folio, page - i, i, vma); + + *full_out = full; +return i; +} + static unsigned long zap_pte_range(struct mmu_gather *tlb, struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr, unsigned long end,@@ -1428,6 +1517,36 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, page =vm_normal_page (vma, addr, ptent);if (unlikely (!should_zap_page (details, page)))continue ; + +/* +* Batch zap large anonymous folio mappings. This allows +* batching the rmap removal, which means we avoid +* spuriously adding a partially unmapped folio to the +* deferrred split queue in the common case, which
mm/memory.c static unsigned long zap_pte_range(struct mmu_gather *tlb, · line 1535
Quoted 10 lines
mm/memory.c +10
-0+* reduces split queue lock contention. Require the VMA +* to be anonymous to ensure that none of the PTEs in +* the range require zap_install_uffd_wp_if_needed(). +*/ +if (page &&PageAnon (page) &&vma_is_anonymous (vma)) { +bool full; +int pgcount; + + pgcount =zap_anon_pte_range (tlb, vma, + page, pte, addr, end, &full);
Are you trying to zap as many ptes as possible if all these ptes are within a folio? If so, why not calculate end before calling zap_anon_pte_range()? That would make zap_anon_pte_range() simpler. Also check if page is part of a large folio first to make sure you can batch.
Quoted 19 lines
mm/memory.c +14
-0+ + rss[mm_counter (page)] -= pgcount; + pgcount--; + pte += pgcount; + addr += pgcount <<PAGE_SHIFT ; + +if (unlikely (full)) { + force_flush =1 ; + addr +=PAGE_SIZE ; +break ; + } +continue ; + } + ptent =ptep_get_and_clear_full (mm, addr, pte, tlb->fullmm);tlb_remove_tlb_entry (tlb, pte, addr);Signature · 1 line
2.25.1
--
Best Regards,
Yan, Zi
Re: [PATCH v1 3/3] mm: Batch-zap large anonymous folio PTE mappings
Quoted 134 lines · Zi Yan
Quoted 132 lines · Ryan Roberts
This allows batching the rmap removal with
folio_remove_rmap_range(), which means we avoid spuriously adding a partially unmapped folio to the deferrred split queue in the common case, which reduces split queue lock contention.Previously each page was removed from the rmap individually with
page_remove_rmap(). If the first page belonged to a large folio, this would causepage_remove_rmap()to conclude that the folio was now partially mapped and add the folio to the deferred split queue. But subsequent calls would cause the folio to become fully unmapped, meaning there is no value to adding it to the split queue.Signed-off-by Ryan Roberts <[email protected]>--- mm/memory.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+)mm/memory.c +100
-0@@ -1391,6 +1391,95 @@ zap_install_uffd_wp_if_needed(struct vm_area_struct *vma,pte_install_uffd_wp_if_needed (vma, addr, pte, pteval); } +static inline unsigned long page_addr (struct page *page, +struct page *anchor,unsigned long anchor_addr) +{ +unsigned long offset; +unsigned long addr; + + offset = (page_to_pfn (page) -page_to_pfn (anchor)) <<PAGE_SHIFT ; + addr = anchor_addr + offset; + +if (anchor > page) { +if (addr > anchor_addr) +return 0 ; + }else { +if (addr < anchor_addr) +return ULONG_MAX ; + } + +return addr; +} + +static int calc_anon_folio_map_pgcount (struct folio *folio, +struct page *page,pte_t *pte, +unsigned long addr,unsigned long end) +{ +pte_t ptent; +int floops; +int i; +unsigned long pfn; + + end =min (page_addr (&folio->page +folio_nr_pages (folio), page, addr), + end); + floops = (end - addr) >>PAGE_SHIFT ; + pfn =page_to_pfn (page); + pfn++; + pte++; + +for (i =1 ; i < floops; i++) { + ptent =ptep_get (pte); + +if (!pte_present (ptent) || +pte_pfn (ptent) != pfn) { +return i; + } + + pfn++; + pte++; + } + +return floops; +} + +static unsigned long zap_anon_pte_range (struct mmu_gather *tlb, +struct vm_area_struct *vma, +struct page *page,pte_t *pte, +unsigned long addr,unsigned long end, +bool *full_out) +{ +struct folio *folio =page_folio (page); +struct mm_struct *mm = tlb->mm; +pte_t ptent; +int pgcount; +int i; +bool full; + + pgcount =calc_anon_folio_map_pgcount (folio, page, pte, addr, end); + +for (i =0 ; i < pgcount;) { + ptent =ptep_get_and_clear_full (mm, addr, pte, tlb->fullmm); +tlb_remove_tlb_entry (tlb, pte, addr); + full =__tlb_remove_page (tlb, page,0 ); + +if (unlikely (page_mapcount (page) <1 )) +print_bad_pte (vma, addr, ptent, page); + + i++; + page++; + pte++; + addr +=PAGE_SIZE ; + +if (unlikely (full)) +break ; + } + +folio_remove_rmap_range (folio, page - i, i, vma); + + *full_out = full; +return i; +} + static unsigned long zap_pte_range(struct mmu_gather *tlb, struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr, unsigned long end,@@ -1428,6 +1517,36 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, page =vm_normal_page (vma, addr, ptent);if (unlikely (!should_zap_page (details, page)))continue ; + +/* +* Batch zap large anonymous folio mappings. This allows +* batching the rmap removal, which means we avoid +* spuriously adding a partially unmapped folio to the +* deferrred split queue in the common case, which +* reduces split queue lock contention. Require the VMA +* to be anonymous to ensure that none of the PTEs in +* the range require zap_install_uffd_wp_if_needed(). +*/ +if (page &&PageAnon (page) &&vma_is_anonymous (vma)) {
Quoted 8 lines
mm/memory.c static unsigned long zap_pte_range(struct mmu_gather *tlb, · line 1535
Quoted 5 lines
mm/memory.c +5
-0+bool full; +int pgcount; + + pgcount =zap_anon_pte_range (tlb, vma, + page, pte, addr, end, &full);Are you trying to zap as many ptes as possible if all these ptes are within a folio?
Yes.
Quoted 2 lines
If so, why not calculate end before calling
zap_anon_pte_range()? That would makezap_anon_pte_range()simpler.
I'm not sure I follow. That's currently done in calc_anon_folio_map_pgcount(). I could move it to here, but I'm not sure that makes things simpler, just puts more code in here and less in there?
Quoted 2 lines
Also check if page is part of
a large folio first to make sure you can batch.
Yeah that's fair. I'd be inclined to put that in zap_anon_pte_range() to short circuit calc_anon_folio_map_pgcount(). But ultimately zap_anon_pte_range() would still zap the single pte.
Quoted 25 lines
Quoted 20 lines
mm/memory.c +14
-0+ + rss[mm_counter (page)] -= pgcount; + pgcount--; + pte += pgcount; + addr += pgcount <<PAGE_SHIFT ; + +if (unlikely (full)) { + force_flush =1 ; + addr +=PAGE_SIZE ; +break ; + } +continue ; + } + ptent =ptep_get_and_clear_full (mm, addr, pte, tlb->fullmm);tlb_remove_tlb_entry (tlb, pte, addr);Signature · 1 line
2.25.1--
Best Regards,
Yan, Zi
Re: [PATCH v1 3/3] mm: Batch-zap large anonymous folio PTE mappings
Quoted 144 lines · Ryan Roberts
Quoted 134 lines · Zi Yan
Quoted 132 lines · Ryan Roberts
This allows batching the rmap removal with
folio_remove_rmap_range(), which means we avoid spuriously adding a partially unmapped folio to the deferrred split queue in the common case, which reduces split queue lock contention.Previously each page was removed from the rmap individually with
page_remove_rmap(). If the first page belonged to a large folio, this would causepage_remove_rmap()to conclude that the folio was now partially mapped and add the folio to the deferred split queue. But subsequent calls would cause the folio to become fully unmapped, meaning there is no value to adding it to the split queue.Signed-off-by Ryan Roberts <[email protected]>--- mm/memory.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+)mm/memory.c +100
-0@@ -1391,6 +1391,95 @@ zap_install_uffd_wp_if_needed(struct vm_area_struct *vma,pte_install_uffd_wp_if_needed (vma, addr, pte, pteval); } +static inline unsigned long page_addr (struct page *page, +struct page *anchor,unsigned long anchor_addr) +{ +unsigned long offset; +unsigned long addr; + + offset = (page_to_pfn (page) -page_to_pfn (anchor)) <<PAGE_SHIFT ; + addr = anchor_addr + offset; + +if (anchor > page) { +if (addr > anchor_addr) +return 0 ; + }else { +if (addr < anchor_addr) +return ULONG_MAX ; + } + +return addr; +} + +static int calc_anon_folio_map_pgcount (struct folio *folio, +struct page *page,pte_t *pte, +unsigned long addr,unsigned long end) +{ +pte_t ptent; +int floops; +int i; +unsigned long pfn; + + end =min (page_addr (&folio->page +folio_nr_pages (folio), page, addr), + end); + floops = (end - addr) >>PAGE_SHIFT ; + pfn =page_to_pfn (page); + pfn++; + pte++; + +for (i =1 ; i < floops; i++) { + ptent =ptep_get (pte); + +if (!pte_present (ptent) || +pte_pfn (ptent) != pfn) { +return i; + } + + pfn++; + pte++; + } + +return floops; +} + +static unsigned long zap_anon_pte_range (struct mmu_gather *tlb, +struct vm_area_struct *vma, +struct page *page,pte_t *pte, +unsigned long addr,unsigned long end, +bool *full_out) +{ +struct folio *folio =page_folio (page); +struct mm_struct *mm = tlb->mm; +pte_t ptent; +int pgcount; +int i; +bool full; + + pgcount =calc_anon_folio_map_pgcount (folio, page, pte, addr, end); + +for (i =0 ; i < pgcount;) { + ptent =ptep_get_and_clear_full (mm, addr, pte, tlb->fullmm); +tlb_remove_tlb_entry (tlb, pte, addr); + full =__tlb_remove_page (tlb, page,0 ); + +if (unlikely (page_mapcount (page) <1 )) +print_bad_pte (vma, addr, ptent, page); + + i++; + page++; + pte++; + addr +=PAGE_SIZE ; + +if (unlikely (full)) +break ; + } + +folio_remove_rmap_range (folio, page - i, i, vma); + + *full_out = full; +return i; +} + static unsigned long zap_pte_range(struct mmu_gather *tlb, struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr, unsigned long end,@@ -1428,6 +1517,36 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, page =vm_normal_page (vma, addr, ptent);if (unlikely (!should_zap_page (details, page)))continue ; + +/* +* Batch zap large anonymous folio mappings. This allows +* batching the rmap removal, which means we avoid +* spuriously adding a partially unmapped folio to the +* deferrred split queue in the common case, which +* reduces split queue lock contention. Require the VMA +* to be anonymous to ensure that none of the PTEs in +* the range require zap_install_uffd_wp_if_needed(). +*/ +if (page &&PageAnon (page) &&vma_is_anonymous (vma)) {Quoted 8 lines
mm/memory.c static unsigned long zap_pte_range(struct mmu_gather *tlb, · line 1535
Quoted 5 lines
mm/memory.c +5
-0+bool full; +int pgcount; + + pgcount =zap_anon_pte_range (tlb, vma, + page, pte, addr, end, &full);Are you trying to zap as many ptes as possible if all these ptes are within a folio?
Quoted 8 lines
Yes.
Quoted 2 lines
If so, why not calculate end before calling
zap_anon_pte_range()? That would makezap_anon_pte_range()simpler.I'm not sure I follow. That's currently done in
calc_anon_folio_map_pgcount(). I could move it to here, but I'm not sure that makes things simpler, just puts more code in here and less in there?
Otherwise your zap_anon_pte_range() is really zap_anon_pte_in_folio_range() or some other more descriptive name. When I first look at the name, I thought PTEs will be zapped until the end. But that is not the case when I look at the code. And future users can easily be confused too and use it in a wrong way.
BTW, page_addr() needs a better name and is easily confused with existing page_address().
Quoted 34 lines
Quoted 3 lines
Also check if page is part of
a large folio first to make sure you can batch.Yeah that's fair. I'd be inclined to put that in
zap_anon_pte_range()to short circuitcalc_anon_folio_map_pgcount(). But ultimatelyzap_anon_pte_range()would still zap the single pte.Quoted 25 lines
Quoted 20 lines
mm/memory.c +14
-0+ + rss[mm_counter (page)] -= pgcount; + pgcount--; + pte += pgcount; + addr += pgcount <<PAGE_SHIFT ; + +if (unlikely (full)) { + force_flush =1 ; + addr +=PAGE_SIZE ; +break ; + } +continue ; + } + ptent =ptep_get_and_clear_full (mm, addr, pte, tlb->fullmm);tlb_remove_tlb_entry (tlb, pte, addr);Signature · 1 line
2.25.1--
Best Regards,
Yan, Zi
--
Best Regards,
Yan, Zi
Re: [PATCH v1 3/3] mm: Batch-zap large anonymous folio PTE mappings
Quoted 151 lines · Zi Yan
Quoted 149 lines · Ryan Roberts
Quoted 134 lines · Zi Yan
Quoted 132 lines · Ryan Roberts
This allows batching the rmap removal with
folio_remove_rmap_range(), which means we avoid spuriously adding a partially unmapped folio to the deferrred split queue in the common case, which reduces split queue lock contention.Previously each page was removed from the rmap individually with
page_remove_rmap(). If the first page belonged to a large folio, this would causepage_remove_rmap()to conclude that the folio was now partially mapped and add the folio to the deferred split queue. But subsequent calls would cause the folio to become fully unmapped, meaning there is no value to adding it to the split queue.Signed-off-by Ryan Roberts <[email protected]>--- mm/memory.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+)mm/memory.c +100
-0@@ -1391,6 +1391,95 @@ zap_install_uffd_wp_if_needed(struct vm_area_struct *vma,pte_install_uffd_wp_if_needed (vma, addr, pte, pteval); } +static inline unsigned long page_addr (struct page *page, +struct page *anchor,unsigned long anchor_addr) +{ +unsigned long offset; +unsigned long addr; + + offset = (page_to_pfn (page) -page_to_pfn (anchor)) <<PAGE_SHIFT ; + addr = anchor_addr + offset; + +if (anchor > page) { +if (addr > anchor_addr) +return 0 ; + }else { +if (addr < anchor_addr) +return ULONG_MAX ; + } + +return addr; +} + +static int calc_anon_folio_map_pgcount (struct folio *folio, +struct page *page,pte_t *pte, +unsigned long addr,unsigned long end) +{ +pte_t ptent; +int floops; +int i; +unsigned long pfn; + + end =min (page_addr (&folio->page +folio_nr_pages (folio), page, addr), + end); + floops = (end - addr) >>PAGE_SHIFT ; + pfn =page_to_pfn (page); + pfn++; + pte++; + +for (i =1 ; i < floops; i++) { + ptent =ptep_get (pte); + +if (!pte_present (ptent) || +pte_pfn (ptent) != pfn) { +return i; + } + + pfn++; + pte++; + } + +return floops; +} + +static unsigned long zap_anon_pte_range (struct mmu_gather *tlb, +struct vm_area_struct *vma, +struct page *page,pte_t *pte, +unsigned long addr,unsigned long end, +bool *full_out) +{ +struct folio *folio =page_folio (page); +struct mm_struct *mm = tlb->mm; +pte_t ptent; +int pgcount; +int i; +bool full; + + pgcount =calc_anon_folio_map_pgcount (folio, page, pte, addr, end); + +for (i =0 ; i < pgcount;) { + ptent =ptep_get_and_clear_full (mm, addr, pte, tlb->fullmm); +tlb_remove_tlb_entry (tlb, pte, addr); + full =__tlb_remove_page (tlb, page,0 ); + +if (unlikely (page_mapcount (page) <1 )) +print_bad_pte (vma, addr, ptent, page); + + i++; + page++; + pte++; + addr +=PAGE_SIZE ; + +if (unlikely (full)) +break ; + } + +folio_remove_rmap_range (folio, page - i, i, vma); + + *full_out = full; +return i; +} + static unsigned long zap_pte_range(struct mmu_gather *tlb, struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr, unsigned long end,@@ -1428,6 +1517,36 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, page =vm_normal_page (vma, addr, ptent);if (unlikely (!should_zap_page (details, page)))continue ; + +/* +* Batch zap large anonymous folio mappings. This allows +* batching the rmap removal, which means we avoid +* spuriously adding a partially unmapped folio to the +* deferrred split queue in the common case, which +* reduces split queue lock contention. Require the VMA +* to be anonymous to ensure that none of the PTEs in +* the range require zap_install_uffd_wp_if_needed(). +*/ +if (page &&PageAnon (page) &&vma_is_anonymous (vma)) {Quoted 8 lines
mm/memory.c static unsigned long zap_pte_range(struct mmu_gather *tlb, · line 1535
Quoted 5 lines
mm/memory.c +5
-0+bool full; +int pgcount; + + pgcount =zap_anon_pte_range (tlb, vma, + page, pte, addr, end, &full);Are you trying to zap as many ptes as possible if all these ptes are within a folio?
Yes.
Quoted 2 lines
If so, why not calculate end before calling
zap_anon_pte_range()? That would makezap_anon_pte_range()simpler.
Quoted 8 lines
Quoted 3 lines
I'm not sure I follow. That's currently done in
calc_anon_folio_map_pgcount(). I could move it to here, but I'm not sure that makes things simpler, just puts more code in here and less in there?Otherwise your
zap_anon_pte_range()is reallyzap_anon_pte_in_folio_range()or some other more descriptive name. When I first look at the name, I thought PTEs will be zapped until the end. But that is not the case when I look at the code. And future users can easily be confused too and use it in a wrong way.
OK I see your point. OK let me pull the page count calculation into here and pass it to zap_anon_pte_range(). Then I think we can keep the name as is?
Quoted 3 lines
BTW,
page_addr()needs a better name and is easily confused with existingpage_address().
Yeah... I'll try to think of something for v2.
Quoted 40 lines
Quoted 35 lines
Quoted 4 lines
Also check if page is part of
a large folio first to make sure you can batch.Yeah that's fair. I'd be inclined to put that in
zap_anon_pte_range()to short circuitcalc_anon_folio_map_pgcount(). But ultimatelyzap_anon_pte_range()would still zap the single pte.Quoted 25 lines
Quoted 20 lines
mm/memory.c +14
-0+ + rss[mm_counter (page)] -= pgcount; + pgcount--; + pte += pgcount; + addr += pgcount <<PAGE_SHIFT ; + +if (unlikely (full)) { + force_flush =1 ; + addr +=PAGE_SIZE ; +break ; + } +continue ; + } + ptent =ptep_get_and_clear_full (mm, addr, pte, tlb->fullmm);tlb_remove_tlb_entry (tlb, pte, addr);Signature · 1 line
2.25.1--
Best Regards,
Yan, Zi--
Best Regards,
Yan, Zi
Re: [PATCH v1 3/3] mm: Batch-zap large anonymous folio PTE mappings
Quoted 156 lines · Ryan Roberts
Quoted 155 lines · Zi Yan
Quoted 152 lines · Ryan Roberts
Quoted 134 lines · Zi Yan
Quoted 132 lines · Ryan Roberts
This allows batching the rmap removal with
folio_remove_rmap_range(), which means we avoid spuriously adding a partially unmapped folio to the deferrred split queue in the common case, which reduces split queue lock contention.Previously each page was removed from the rmap individually with
page_remove_rmap(). If the first page belonged to a large folio, this would causepage_remove_rmap()to conclude that the folio was now partially mapped and add the folio to the deferred split queue. But subsequent calls would cause the folio to become fully unmapped, meaning there is no value to adding it to the split queue.Signed-off-by Ryan Roberts <[email protected]>--- mm/memory.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+)mm/memory.c +100
-0@@ -1391,6 +1391,95 @@ zap_install_uffd_wp_if_needed(struct vm_area_struct *vma,pte_install_uffd_wp_if_needed (vma, addr, pte, pteval); } +static inline unsigned long page_addr (struct page *page, +struct page *anchor,unsigned long anchor_addr) +{ +unsigned long offset; +unsigned long addr; + + offset = (page_to_pfn (page) -page_to_pfn (anchor)) <<PAGE_SHIFT ; + addr = anchor_addr + offset; + +if (anchor > page) { +if (addr > anchor_addr) +return 0 ; + }else { +if (addr < anchor_addr) +return ULONG_MAX ; + } + +return addr; +} + +static int calc_anon_folio_map_pgcount (struct folio *folio, +struct page *page,pte_t *pte, +unsigned long addr,unsigned long end) +{ +pte_t ptent; +int floops; +int i; +unsigned long pfn; + + end =min (page_addr (&folio->page +folio_nr_pages (folio), page, addr), + end); + floops = (end - addr) >>PAGE_SHIFT ; + pfn =page_to_pfn (page); + pfn++; + pte++; + +for (i =1 ; i < floops; i++) { + ptent =ptep_get (pte); + +if (!pte_present (ptent) || +pte_pfn (ptent) != pfn) { +return i; + } + + pfn++; + pte++; + } + +return floops; +} + +static unsigned long zap_anon_pte_range (struct mmu_gather *tlb, +struct vm_area_struct *vma, +struct page *page,pte_t *pte, +unsigned long addr,unsigned long end, +bool *full_out) +{ +struct folio *folio =page_folio (page); +struct mm_struct *mm = tlb->mm; +pte_t ptent; +int pgcount; +int i; +bool full; + + pgcount =calc_anon_folio_map_pgcount (folio, page, pte, addr, end); + +for (i =0 ; i < pgcount;) { + ptent =ptep_get_and_clear_full (mm, addr, pte, tlb->fullmm); +tlb_remove_tlb_entry (tlb, pte, addr); + full =__tlb_remove_page (tlb, page,0 ); + +if (unlikely (page_mapcount (page) <1 )) +print_bad_pte (vma, addr, ptent, page); + + i++; + page++; + pte++; + addr +=PAGE_SIZE ; + +if (unlikely (full)) +break ; + } + +folio_remove_rmap_range (folio, page - i, i, vma); + + *full_out = full; +return i; +} + static unsigned long zap_pte_range(struct mmu_gather *tlb, struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr, unsigned long end,@@ -1428,6 +1517,36 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, page =vm_normal_page (vma, addr, ptent);if (unlikely (!should_zap_page (details, page)))continue ; + +/* +* Batch zap large anonymous folio mappings. This allows +* batching the rmap removal, which means we avoid +* spuriously adding a partially unmapped folio to the +* deferrred split queue in the common case, which +* reduces split queue lock contention. Require the VMA +* to be anonymous to ensure that none of the PTEs in +* the range require zap_install_uffd_wp_if_needed(). +*/ +if (page &&PageAnon (page) &&vma_is_anonymous (vma)) {Quoted 8 lines
mm/memory.c static unsigned long zap_pte_range(struct mmu_gather *tlb, · line 1535
Quoted 5 lines
mm/memory.c +5
-0+bool full; +int pgcount; + + pgcount =zap_anon_pte_range (tlb, vma, + page, pte, addr, end, &full);Are you trying to zap as many ptes as possible if all these ptes are within a folio?
Yes.
Quoted 2 lines
If so, why not calculate end before calling
zap_anon_pte_range()? That would makezap_anon_pte_range()simpler.I'm not sure I follow. That's currently done in
calc_anon_folio_map_pgcount(). I could move it to here, but I'm not sure that makes things simpler, just puts more code in here and less in there?
Quoted 7 lines
Quoted 4 lines
Otherwise your
zap_anon_pte_range()is reallyzap_anon_pte_in_folio_range()or some other more descriptive name. When I first look at the name, I thought PTEs will be zapped until the end. But that is not the case when I look at the code. And future users can easily be confused too and use it in a wrong way.OK I see your point. OK let me pull the page count calculation into here and pass it to
zap_anon_pte_range(). Then I think we can keep the name as is?
Yes. Thanks.
Quoted 48 lines
Quoted 5 lines
BTW,
page_addr()needs a better name and is easily confused with existingpage_address().Yeah... I'll try to think of something for v2.
Quoted 40 lines
Quoted 35 lines
Quoted 4 lines
Also check if page is part of
a large folio first to make sure you can batch.Yeah that's fair. I'd be inclined to put that in
zap_anon_pte_range()to short circuitcalc_anon_folio_map_pgcount(). But ultimatelyzap_anon_pte_range()would still zap the single pte.Quoted 25 lines
Quoted 20 lines
mm/memory.c +14
-0+ + rss[mm_counter (page)] -= pgcount; + pgcount--; + pte += pgcount; + addr += pgcount <<PAGE_SHIFT ; + +if (unlikely (full)) { + force_flush =1 ; + addr +=PAGE_SIZE ; +break ; + } +continue ; + } + ptent =ptep_get_and_clear_full (mm, addr, pte, tlb->fullmm);tlb_remove_tlb_entry (tlb, pte, addr);Signature · 1 line
2.25.1--
Best Regards,
Yan, Zi--
Best Regards,
Yan, Zi
--
Best Regards,
Yan, Zi
Re: [PATCH v1 3/3] mm: Batch-zap large anonymous folio PTE mappings
In reply to Ryan RobertsQuoted 122 lines · Ryan Roberts
This allows batching the rmap removal with
folio_remove_rmap_range(), which means we avoid spuriously adding a partially unmapped folio to the deferrred split queue in the common case, which reduces split queue lock contention.Previously each page was removed from the rmap individually with
page_remove_rmap(). If the first page belonged to a large folio, this would causepage_remove_rmap()to conclude that the folio was now partially mapped and add the folio to the deferred split queue. But subsequent calls would cause the folio to become fully unmapped, meaning there is no value to adding it to the split queue.Signed-off-by Ryan Roberts <[email protected]>--- mm/memory.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+)mm/memory.c +90
-0@@ -1391,6 +1391,95 @@ zap_install_uffd_wp_if_needed(struct vm_area_struct *vma,pte_install_uffd_wp_if_needed (vma, addr, pte, pteval); } +static inline unsigned long page_addr (struct page *page, +struct page *anchor,unsigned long anchor_addr) +{ +unsigned long offset; +unsigned long addr; + + offset = (page_to_pfn (page) -page_to_pfn (anchor)) <<PAGE_SHIFT ; + addr = anchor_addr + offset; + +if (anchor > page) { +if (addr > anchor_addr) +return 0 ; + }else { +if (addr < anchor_addr) +return ULONG_MAX ; + } + +return addr; +} + +static int calc_anon_folio_map_pgcount (struct folio *folio, +struct page *page,pte_t *pte, +unsigned long addr,unsigned long end) +{ +pte_t ptent; +int floops; +int i; +unsigned long pfn; + + end =min (page_addr (&folio->page +folio_nr_pages (folio), page, addr), + end); + floops = (end - addr) >>PAGE_SHIFT ; + pfn =page_to_pfn (page); + pfn++; + pte++; + +for (i =1 ; i < floops; i++) { + ptent =ptep_get (pte); + +if (!pte_present (ptent) || +pte_pfn (ptent) != pfn) { +return i; + } + + pfn++; + pte++; + } + +return floops; +} + +static unsigned long zap_anon_pte_range (struct mmu_gather *tlb, +struct vm_area_struct *vma, +struct page *page,pte_t *pte, +unsigned long addr,unsigned long end, +bool *full_out) +{ +struct folio *folio =page_folio (page); +struct mm_struct *mm = tlb->mm; +pte_t ptent; +int pgcount; +int i; +bool full; + + pgcount =calc_anon_folio_map_pgcount (folio, page, pte, addr, end); + +for (i =0 ; i < pgcount;) { + ptent =ptep_get_and_clear_full (mm, addr, pte, tlb->fullmm); +tlb_remove_tlb_entry (tlb, pte, addr); + full =__tlb_remove_page (tlb, page,0 ); + +if (unlikely (page_mapcount (page) <1 )) +print_bad_pte (vma, addr, ptent, page); + + i++; + page++; + pte++; + addr +=PAGE_SIZE ; + +if (unlikely (full)) +break ; + } + +folio_remove_rmap_range (folio, page - i, i, vma); + + *full_out = full; +return i; +} + static unsigned long zap_pte_range(struct mmu_gather *tlb, struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr, unsigned long end,@@ -1428,6 +1517,36 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, page =vm_normal_page (vma, addr, ptent);if (unlikely (!should_zap_page (details, page)))continue ; +
mm/memory.c static unsigned long zap_pte_range(struct mmu_gather *tlb, · line 1530
Quoted 10 lines
mm/memory.c +10
-0+/* +* Batch zap large anonymous folio mappings. This allows +* batching the rmap removal, which means we avoid +* spuriously adding a partially unmapped folio to the +* deferrred split queue in the common case, which +* reduces split queue lock contention. Require the VMA +* to be anonymous to ensure that none of the PTEs in +* the range require zap_install_uffd_wp_if_needed(). +*/ +if (page &&PageAnon (page) &&vma_is_anonymous (vma)) {
Why this is only for anonymous page? I suppose it can support file mapping also.
Regards
Yin, Fengwei
Quoted 22 lines
mm/memory.c +19
-0+bool full; +int pgcount; + + pgcount =zap_anon_pte_range (tlb, vma, + page, pte, addr, end, &full); + + rss[mm_counter (page)] -= pgcount; + pgcount--; + pte += pgcount; + addr += pgcount <<PAGE_SHIFT ; + +if (unlikely (full)) { + force_flush =1 ; + addr +=PAGE_SIZE ; +break ; + } +continue ; + } + ptent =ptep_get_and_clear_full (mm, addr, pte, tlb->fullmm);tlb_remove_tlb_entry (tlb, pte, addr);
Re: [PATCH v1 3/3] mm: Batch-zap large anonymous folio PTE mappings
Quoted 128 lines · Yin Fengwei
Quoted 125 lines · Ryan Roberts
This allows batching the rmap removal with
folio_remove_rmap_range(), which means we avoid spuriously adding a partially unmapped folio to the deferrred split queue in the common case, which reduces split queue lock contention.Previously each page was removed from the rmap individually with
page_remove_rmap(). If the first page belonged to a large folio, this would causepage_remove_rmap()to conclude that the folio was now partially mapped and add the folio to the deferred split queue. But subsequent calls would cause the folio to become fully unmapped, meaning there is no value to adding it to the split queue.Signed-off-by Ryan Roberts <[email protected]>--- mm/memory.c | 119 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+)mm/memory.c +93
-0@@ -1391,6 +1391,95 @@ zap_install_uffd_wp_if_needed(struct vm_area_struct *vma,pte_install_uffd_wp_if_needed (vma, addr, pte, pteval); } +static inline unsigned long page_addr (struct page *page, +struct page *anchor,unsigned long anchor_addr) +{ +unsigned long offset; +unsigned long addr; + + offset = (page_to_pfn (page) -page_to_pfn (anchor)) <<PAGE_SHIFT ; + addr = anchor_addr + offset; + +if (anchor > page) { +if (addr > anchor_addr) +return 0 ; + }else { +if (addr < anchor_addr) +return ULONG_MAX ; + } + +return addr; +} + +static int calc_anon_folio_map_pgcount (struct folio *folio, +struct page *page,pte_t *pte, +unsigned long addr,unsigned long end) +{ +pte_t ptent; +int floops; +int i; +unsigned long pfn; + + end =min (page_addr (&folio->page +folio_nr_pages (folio), page, addr), + end); + floops = (end - addr) >>PAGE_SHIFT ; + pfn =page_to_pfn (page); + pfn++; + pte++; + +for (i =1 ; i < floops; i++) { + ptent =ptep_get (pte); + +if (!pte_present (ptent) || +pte_pfn (ptent) != pfn) { +return i; + } + + pfn++; + pte++; + } + +return floops; +} + +static unsigned long zap_anon_pte_range (struct mmu_gather *tlb, +struct vm_area_struct *vma, +struct page *page,pte_t *pte, +unsigned long addr,unsigned long end, +bool *full_out) +{ +struct folio *folio =page_folio (page); +struct mm_struct *mm = tlb->mm; +pte_t ptent; +int pgcount; +int i; +bool full; + + pgcount =calc_anon_folio_map_pgcount (folio, page, pte, addr, end); + +for (i =0 ; i < pgcount;) { + ptent =ptep_get_and_clear_full (mm, addr, pte, tlb->fullmm); +tlb_remove_tlb_entry (tlb, pte, addr); + full =__tlb_remove_page (tlb, page,0 ); + +if (unlikely (page_mapcount (page) <1 )) +print_bad_pte (vma, addr, ptent, page); + + i++; + page++; + pte++; + addr +=PAGE_SIZE ; + +if (unlikely (full)) +break ; + } + +folio_remove_rmap_range (folio, page - i, i, vma); + + *full_out = full; +return i; +} + static unsigned long zap_pte_range(struct mmu_gather *tlb, struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr, unsigned long end,@@ -1428,6 +1517,36 @@ static unsigned long zap_pte_range(struct mmu_gather *tlb, page =vm_normal_page (vma, addr, ptent);if (unlikely (!should_zap_page (details, page)))continue ; + +/* +* Batch zap large anonymous folio mappings. This allows +* batching the rmap removal, which means we avoid
Quoted 8 lines
mm/memory.c static unsigned long zap_pte_range(struct mmu_gather *tlb, · line 1530
Quoted 7 lines
mm/memory.c +7
-0+* spuriously adding a partially unmapped folio to the +* deferrred split queue in the common case, which +* reduces split queue lock contention. Require the VMA +* to be anonymous to ensure that none of the PTEs in +* the range require zap_install_uffd_wp_if_needed(). +*/ +if (page &&PageAnon (page) &&vma_is_anonymous (vma)) {Why this is only for anonymous page? I suppose it can support file mapping also.
I was trying to avoid the complexity. For file-backed pages, there is a bunch of dirty and access management stuff that needs to happen (see "if (!PageAnon(page)) {" a bit further down). And for file-backed VMAs (even if the page is anon, I think?) zap_install_uffd_wp_if_needed() might do some extra work, which again I didn't want to have to drag into zap_anon_pte_range().
I guess it's implementable, but given only anon folios will be deferred-split and anon folios in a file-backed vma will all be single page, I didn't feel that the extra complexity would add anything performance-wise.
Quoted 27 lines
Regards
Yin, FengweiQuoted 22 lines
mm/memory.c +19
-0+bool full; +int pgcount; + + pgcount =zap_anon_pte_range (tlb, vma, + page, pte, addr, end, &full); + + rss[mm_counter (page)] -= pgcount; + pgcount--; + pte += pgcount; + addr += pgcount <<PAGE_SHIFT ; + +if (unlikely (full)) { + force_flush =1 ; + addr +=PAGE_SIZE ; +break ; + } +continue ; + } + ptent =ptep_get_and_clear_full (mm, addr, pte, tlb->fullmm);tlb_remove_tlb_entry (tlb, pte, addr);