mm: thp: clear PageDoubleMap flag when the last PMD map gone
Subsystems (2)
PATCH 1/1 mm: thp: clear PageDoubleMap flag when the last PMD map gone
File THP sets PageDoubleMap flag when the first it gets PTE mapped, but the flag is never cleared until the THP is freed. This result in unbalanced state although it is not a big deal.
Clear the flag when the last compound_mapcount is gone. It should be cleared when all the PTE maps are gone (become PMD mapped only) as well, but this needs check all subpage's _mapcount every time any subpage's rmap is removed, the overhead may be not worth. The anonymous THP also just clears PageDoubleMap flag when the last PMD map is gone.
---
Hugh thought it is unnecessary to fix it completely due to the overhead
(https://lkml.org/lkml/2019/10/22/1011), but it sounds simple to achieve the similar balance as anonymous THP.
mm/rmap.c | 3 +++ 1 file changed, 3 insertions(+)
mm/rmap.c +3 -0
@@ -1236,6 +1236,9 @@ static void page_remove_file_rmap(struct page *page, bool compound)__dec_node_page_state (page,NR_SHMEM_PMDMAPPED ); else__dec_node_page_state (page ,NR_FILE_PMDMAPPED ); + +/* The last PMD map is gone */ +ClearPageDoubleMap (compound_head (page)); }else {if (!atomic_add_negative (-1 , &page->_mapcount)) goto out;
Signature · 1 line
1.8.3.1
Quoted 11 lines · Yang Shi
File THP sets PageDoubleMap flag when the first it gets PTE mapped, but the flag is never cleared until the THP is freed. This result in unbalanced state although it is not a big deal.
Clear the flag when the last
compound_mapcountis gone. It should be cleared when all the PTE maps are gone (become PMD mapped only) as well, but this needs check all subpage's_mapcountevery time any subpage's rmap is removed, the overhead may be not worth. The anonymous THP also just clears PageDoubleMap flag when the last PMD map is gone.
Quoted 4 lines
Cc Hugh Dickins <[email protected]>Cc Kirill A. Shutemov <[email protected]>Cc Andrea Arcangeli <[email protected]>Signed-off-by Yang Shi <[email protected]>
Looks good to me. Thanks!
Quoted 26 lines
---
Hugh thought it is unnecessary to fix it completely due to the overhead
(https://lkml.org/lkml/2019/10/22/1011), but it sounds simple to achieve the similar balance as anonymous THP.mm/rmap.c | 3 +++ 1 file changed, 3 insertions(+)mm/rmap.c +3
-1@@ -1236,6 +1236,9 @@ static void page_remove_file_rmap(struct page *page, bool compound)__dec_node_page_state (page,NR_SHMEM_PMDMAPPED ); else__dec_node_page_state (page ,NR_FILE_PMDMAPPED ); + +/* The last PMD map is gone */ +ClearPageDoubleMap (compound_head (page)); }else {if (!atomic_add_negative (-1 , &page->_mapcount)) goto out; --1.8.3.1
Quoted 9 lines · Yang Shi
File THP sets PageDoubleMap flag when the first it gets PTE mapped, but the flag is never cleared until the THP is freed. This result in unbalanced state although it is not a big deal.
Clear the flag when the last
compound_mapcountis gone. It should be cleared when all the PTE maps are gone (become PMD mapped only) as well, but this needs check all subpage's_mapcountevery time any subpage's rmap is removed, the overhead may be not worth. The anonymous THP also just clears PageDoubleMap flag when the last PMD map is gone.
NAK, sorry.
The key difference with anon THP that file THP can be mapped again with PMD after all PMD (or all) mappings are gone.
Your patch breaks the case when you map the page with PMD again while the page is still mapped with PTEs. Who would set PageDoubleMap() in this case?
Signature · 1 line
Kirill A. Shutemov
Quoted 10 lines · Kirill A. Shutemov
Quoted 9 lines · Yang Shi
File THP sets PageDoubleMap flag when the first it gets PTE mapped, but the flag is never cleared until the THP is freed. This result in unbalanced state although it is not a big deal.
Clear the flag when the last
compound_mapcountis gone. It should be cleared when all the PTE maps are gone (become PMD mapped only) as well, but this needs check all subpage's_mapcountevery time any subpage's rmap is removed, the overhead may be not worth. The anonymous THP also just clears PageDoubleMap flag when the last PMD map is gone.
Quoted 8 lines
NAK, sorry.
The key difference with anon THP that file THP can be mapped again with PMD after all PMD (or all) mappings are gone.
Your patch breaks the case when you map the page with PMD again while the page is still mapped with PTEs. Who would set
PageDoubleMap()in this case?
Aha, yes, you are right. I missed that point. However, I'm wondering we might move this up a little bit like this:
mm/rmap.c +0 -0
@@ -1230,15 +1230,17 @@ static void page_remove_file_rmap(struct page *page, bool compound)
if (atomic_add_negative(-1, &page[i]._mapcount)) nr++;
}
+
+ /* No PTE map anymore */
+ if (nr == HPAGE_PMD_NR)
+ ClearPageDoubleMap(compound_head(page)); +
if (!atomic_add_negative(-1, compound_mapcount_ptr(page))) goto out;
if (PageSwapBacked(page)) __dec_node_page_state(page, NR_SHMEM_PMDMAPPED); else
__dec_node_page_state(page, NR_FILE_PMDMAPPED); -
- /* The last PMD map is gone */
- ClearPageDoubleMap(compound_head(page));
} else {
if (!atomic_add_negative(-1, &page->_mapcount)) goto out;
This should guarantee no PTE map anymore, it should be safe to clear the flag.
Quoted 1 line
Quoted 53 lines · Yang Shi
Quoted 10 lines · Kirill A. Shutemov
Quoted 9 lines · Yang Shi
File THP sets PageDoubleMap flag when the first it gets PTE mapped, but the flag is never cleared until the THP is freed. This result in unbalanced state although it is not a big deal.
Clear the flag when the last
compound_mapcountis gone. It should be cleared when all the PTE maps are gone (become PMD mapped only) as well, but this needs check all subpage's_mapcountevery time any subpage's rmap is removed, the overhead may be not worth. The anonymous THP also just clears PageDoubleMap flag when the last PMD map is gone.Quoted 8 lines
NAK, sorry.
The key difference with anon THP that file THP can be mapped again with PMD after all PMD (or all) mappings are gone.
Your patch breaks the case when you map the page with PMD again while the page is still mapped with PTEs. Who would set
PageDoubleMap()in this case?Aha, yes, you are right. I missed that point. However, I'm wondering we might move this up a little bit like this:
mm/rmap.c +5
-0@@ -1230,15 +1230,17 @@ static void page_remove_file_rmap(struct page *page, bool compound) if (atomic_add_negative(-1, &page[i]._mapcount)) nr++; } + + /* No PTE map anymore */ + if (nr == HPAGE_PMD_NR) + ClearPageDoubleMap(compound_head(page)); + if (!atomic_add_negative(-1, compound_mapcount_ptr(page))) goto out; if (PageSwapBacked(page)) __dec_node_page_state(page, NR_SHMEM_PMDMAPPED);else
__dec_node_page_state(page, NR_FILE_PMDMAPPED); -
- /* The last PMD map is gone */
- ClearPageDoubleMap(compound_head(page));
} else {
if (!atomic_add_negative(-1,&page->_mapcount)) goto out;
Quoted 2 lines
This should guarantee no PTE map anymore, it should be safe to clear the flag.
At first glance looks safe, but let me think more about it. I didn't expect it be that easy :P
Signature · 1 line
Kirill A. Shutemov
Quoted 54 lines · Kirill A. Shutemov
Quoted 53 lines · Yang Shi
Quoted 10 lines · Kirill A. Shutemov
Quoted 9 lines · Yang Shi
File THP sets PageDoubleMap flag when the first it gets PTE mapped, but the flag is never cleared until the THP is freed. This result in unbalanced state although it is not a big deal.
Clear the flag when the last
compound_mapcountis gone. It should be cleared when all the PTE maps are gone (become PMD mapped only) as well, but this needs check all subpage's_mapcountevery time any subpage's rmap is removed, the overhead may be not worth. The anonymous THP also just clears PageDoubleMap flag when the last PMD map is gone.Quoted 8 lines
NAK, sorry.
The key difference with anon THP that file THP can be mapped again with PMD after all PMD (or all) mappings are gone.
Your patch breaks the case when you map the page with PMD again while the page is still mapped with PTEs. Who would set
PageDoubleMap()in this case?Aha, yes, you are right. I missed that point. However, I'm wondering we might move this up a little bit like this:
mm/rmap.c +5
-0@@ -1230,15 +1230,17 @@ static void page_remove_file_rmap(struct page *page, bool compound) if (atomic_add_negative(-1, &page[i]._mapcount)) nr++; } + + /* No PTE map anymore */ + if (nr == HPAGE_PMD_NR) + ClearPageDoubleMap(compound_head(page)); + if (!atomic_add_negative(-1, compound_mapcount_ptr(page))) goto out; if (PageSwapBacked(page)) __dec_node_page_state(page, NR_SHMEM_PMDMAPPED);else
__dec_node_page_state(page, NR_FILE_PMDMAPPED); -
- /* The last PMD map is gone */
- ClearPageDoubleMap(compound_head(page));
} else {
if (!atomic_add_negative(-1,&page->_mapcount)) goto out;
Quoted 5 lines
Quoted 2 lines
This should guarantee no PTE map anymore, it should be safe to clear the flag.
At first glance looks safe, but let me think more about it. I didn't expect it be that easy :P
How do you protect from races? What prevents other thread/process to map the page as PTE after you've calculated 'nr'?
I don't remember the code that well, but I believe we don't require PageLock for all cases... Or do we?
Signature · 1 line
Kirill A. Shutemov
Quoted 57 lines · Kirill A. Shutemov
Quoted 52 lines · Kirill A. Shutemov
Quoted 51 lines · Yang Shi
Quoted 10 lines · Kirill A. Shutemov
Quoted 9 lines · Yang Shi
File THP sets PageDoubleMap flag when the first it gets PTE mapped, but the flag is never cleared until the THP is freed. This result in unbalanced state although it is not a big deal.
Clear the flag when the last
compound_mapcountis gone. It should be cleared when all the PTE maps are gone (become PMD mapped only) as well, but this needs check all subpage's_mapcountevery time any subpage's rmap is removed, the overhead may be not worth. The anonymous THP also just clears PageDoubleMap flag when the last PMD map is gone.Quoted 8 lines
NAK, sorry.
The key difference with anon THP that file THP can be mapped again with PMD after all PMD (or all) mappings are gone.
Your patch breaks the case when you map the page with PMD again while the page is still mapped with PTEs. Who would set
PageDoubleMap()in this case?Aha, yes, you are right. I missed that point. However, I'm wondering we might move this up a little bit like this:
mm/rmap.c +5
-3@@ -1230,15 +1230,17 @@ static void page_remove_file_rmap(struct page *page, bool compound) if (atomic_add_negative(-1, &page[i]._mapcount)) nr++; } + + /* No PTE map anymore */ + if (nr == HPAGE_PMD_NR) + ClearPageDoubleMap(compound_head(page)); + if (!atomic_add_negative(-1, compound_mapcount_ptr(page))) goto out; if (PageSwapBacked(page)) __dec_node_page_state(page, NR_SHMEM_PMDMAPPED); else __dec_node_page_state(page, NR_FILE_PMDMAPPED); - - /* The last PMD map is gone */ - ClearPageDoubleMap(compound_head(page)); } else { if (!atomic_add_negative(-1, &page->_mapcount))goto out;
Quoted 4 lines
Quoted 2 lines
This should guarantee no PTE map anymore, it should be safe to clear the flag.
At first glance looks safe, but let me think more about it. I didn't expect it be that easy :P
Quoted 5 lines
How do you protect from races? What prevents other thread/process to map the page as PTE after you've calculated 'nr'?
I don't remember the code that well, but I believe we don't require PageLock for all cases... Or do we?
No, page lock is required by adding PTE rmap, but not required when removing rmap, i.e. huge pmd split. It looks we can't prevent from the races for processes, threads are protected by ptl.