mm/page_alloc: fix counting of free pages after take off from buddy
Subsystems (2)
RFC PATCH 1/1 mm/page_alloc: fix counting of free pages after take off from buddy
Recently we found there is a lot MemFree left in /proc/meminfo after do a lot of pages soft offline.
I think it's incorrect since NR_FREE_PAGES should not contain HWPoison pages. After take_page_off_buddy, the page is no longer belong to buddy allocator, and will not be used any more, but we maybe missed accounting NR_FREE_PAGES in this situation.
--- mm/page_alloc.c | 1 + 1 file changed, 1 insertion(+)
mm/page_alloc.c +1 -0
@@ -8947,6 +8947,7 @@ bool take_page_off_buddy(struct page *page)del_page_from_free_list (page_head, zone, page_order);break_down_buddy_pages (zone, page_head, page,0 , page_order, migratetype); +__mod_zone_page_state (zone,NR_FREE_PAGES ,-1 ); ret = true;break ; }
Signature · 1 line
2.17.1
Quoted 19 lines · Ding Hui
Recently we found there is a lot MemFree left in
/proc/meminfoafter do a lot of pages soft offline.I think it's incorrect since
NR_FREE_PAGESshould not contain HWPoison pages. Aftertake_page_off_buddy, the page is no longer belong to buddy allocator, and will not be used any more, but we maybe missed accountingNR_FREE_PAGESin this situation.Signed-off-by Ding Hui <[email protected]>--- mm/page_alloc.c | 1 + 1 file changed, 1 insertion(+)mm/page_alloc.c +0
-0
mm/page_alloc.c bool take_page_off_buddy(struct page *page) · line 8953
Quoted 7 lines
mm/page_alloc.c +1
-0@@ -8947,6 +8947,7 @@ bool take_page_off_buddy(struct page *page)del_page_from_free_list (page_head, zone, page_order);break_down_buddy_pages (zone, page_head, page,0 , page_order, migratetype); +__mod_zone_page_state (zone,NR_FREE_PAGES ,-1 ); ret = true;break ; }
Should this use __mod_zone_freepage_state() instead?
Signature · 3 lines
Thanks, David / dhildenb
Quoted 14 lines · David Hildenbrand
Quoted 13 lines · Ding Hui
Recently we found there is a lot MemFree left in
/proc/meminfoafter do a lot of pages soft offline.I think it's incorrect since
NR_FREE_PAGESshould not contain HWPoison pages. Aftertake_page_off_buddy, the page is no longer belong to buddy allocator, and will not be used any more, but we maybe missed accountingNR_FREE_PAGESin this situation.Signed-off-by Ding Hui <[email protected]>---
mm/page_alloc.c| 1 +1 file changed, 1 insertion(+)
Quoted 16 lines
mm/page_alloc.c bool take_page_off_buddy(struct page *page) · line 8953
Quoted 13 lines
mm/page_alloc.c +1
-0@@ -8947,6 +8947,7 @@ bool take_page_off_buddy(struct page *page) del_page_from_free_list(page_head, zone, page_order); break_down_buddy_pages(zone, page_head, page, 0, page_order, migratetype); + __mod_zone_page_state(zone, NR_FREE_PAGES, -1); ret = true; break; }Should this use
__mod_zone_freepage_state()instead?
Yes, you're right.
I'll use it in v2.
Signature · 2 lines
Thanks, - Ding Hui
PATCH v2 1/1 mm/page_alloc: fix counting of free pages after take off from buddy
Recently we found there is a lot MemFree left in /proc/meminfo after do a lot of pages soft offline.
I think it's incorrect since NR_FREE_PAGES should not contain HWPoison pages. For offline free pages, after a successful call take_page_off_buddy(), the page is no longer belong to buddy allocator, and will not be used any more, but we missed accounting NR_FREE_PAGES in this situation.
Do update like rmqueue() does.
---
V2:
use __mod_zone_freepage_state instead of __mod_zone_page_state
mm/page_alloc.c | 1 + 1 file changed, 1 insertion(+)
mm/page_alloc.c +1 -0
@@ -8947,6 +8947,7 @@ bool take_page_off_buddy(struct page *page)del_page_from_free_list (page_head, zone, page_order);break_down_buddy_pages (zone, page_head, page,0 , page_order, migratetype); +__mod_zone_freepage_state (zone,-1 , migratetype); ret = true;break ; }
Signature · 1 line
2.17.1
Quoted 23 lines · Ding Hui
Recently we found there is a lot MemFree left in
/proc/meminfoafter do a lot of pages soft offline.I think it's incorrect since
NR_FREE_PAGESshould not contain HWPoison pages. For offline free pages, after a successful calltake_page_off_buddy(), the page is no longer belong to buddy allocator, and will not be used any more, but we missed accountingNR_FREE_PAGESin this situation.Do update like
rmqueue()does.Signed-off-by Ding Hui <[email protected]>---
V2:
use__mod_zone_freepage_stateinstead of__mod_zone_page_statemm/page_alloc.c | 1 + 1 file changed, 1 insertion(+)mm/page_alloc.c +0
-0
mm/page_alloc.c bool take_page_off_buddy(struct page *page) · line 8950
Quoted 4 lines
mm/page_alloc.c +1
-0@@ -8947,6 +8947,7 @@ bool take_page_off_buddy(struct page *page)del_page_from_free_list (page_head, zone, page_order);break_down_buddy_pages (zone, page_head, page,0 , page_order, migratetype); +__mod_zone_freepage_state (zone,-1 , migratetype);
Page offline code (see set_migratetype_isolate()) seems to handle NR_FREE_PAGES counter in its own way, so I think that it's more correct to call __mod_zone_freepage_state() only when is_migrate_isolate(migratetype)) is false.
Otherwise, the patch looks good to me.
Thanks,
Naoya Horiguchi
Quoted 6 lines
mm/page_alloc.c +0
-0ret = true;break ; }Signature · 1 line
2.17.1
Quoted 34 lines · HORIGUCHI NAOYA(堀口 直也)
Quoted 23 lines · Ding Hui
Recently we found there is a lot MemFree left in
/proc/meminfoafter do a lot of pages soft offline.I think it's incorrect since
NR_FREE_PAGESshould not contain HWPoison pages. For offline free pages, after a successful calltake_page_off_buddy(), the page is no longer belong to buddy allocator, and will not be used any more, but we missed accountingNR_FREE_PAGESin this situation.Do update like
rmqueue()does.Signed-off-by Ding Hui <[email protected]>---
V2:
use__mod_zone_freepage_stateinstead of__mod_zone_page_statemm/page_alloc.c | 1 + 1 file changed, 1 insertion(+)mm/page_alloc.c +0
-0mm/page_alloc.c bool take_page_off_buddy(struct page *page) · line 8950
Quoted 4 lines
mm/page_alloc.c +1
-0@@ -8947,6 +8947,7 @@ bool take_page_off_buddy(struct page *page)del_page_from_free_list (page_head, zone, page_order);break_down_buddy_pages (zone, page_head, page,0 , page_order, migratetype); +__mod_zone_freepage_state (zone,-1 , migratetype);Page offline code (see
set_migratetype_isolate()) seems to handleNR_FREE_PAGEScounter in its own way, so I think that it's more correct to call__mod_zone_freepage_state()only whenis_migrate_isolate(migratetype)) is false.
Quoted 2 lines
Otherwise, the patch looks good to me.
Thanks for reply and suggestion, I'll send v3 patch later.
Quoted 5 lines
Quoted 5 lines
mm/page_alloc.c +0
-0ret = true;break ; }Signature · 1 line
2.17.1
Signature · 2 lines
Thanks, - Ding Hui
Quoted 14 lines · David Hildenbrand
Quoted 13 lines · Ding Hui
Recently we found there is a lot MemFree left in
/proc/meminfoafter do a lot of pages soft offline.I think it's incorrect since
NR_FREE_PAGESshould not contain HWPoison pages. Aftertake_page_off_buddy, the page is no longer belong to buddy allocator, and will not be used any more, but we maybe missed accountingNR_FREE_PAGESin this situation.Signed-off-by Ding Hui <[email protected]>--- mm/page_alloc.c | 1 + 1 file changed, 1 insertion(+)
Quoted 15 lines
mm/page_alloc.c bool take_page_off_buddy(struct page *page) · line 8953
Quoted 13 lines
mm/page_alloc.c +1
-0@@ -8947,6 +8947,7 @@ bool take_page_off_buddy(struct page *page)del_page_from_free_list (page_head, zone, page_order);break_down_buddy_pages (zone, page_head, page,0 , page_order, migratetype); +__mod_zone_page_state (zone,NR_FREE_PAGES ,-1 ); ret = true;break ; }Should this use
__mod_zone_freepage_state()instead?
Yes, __mod_zone_freepage_state() looks better to me.
And I think that maybe an additional __mod_zone_freepage_state() in unpoison_memory() is necessary to cancel the decrement. I thought of the following, but it doesn't build because get_pfnblock_migratetype() is available only in mm/page_alloc.c, so you might want to add a small exported routine in mm/page_alloc.c and let it called from unpoison_memory().
@@ -1899,8 +1899,12 @@ int unpoison_memory(unsigned long pfn)
} if (!get_hwpoison_page(p, flags, 0)) {
- if (TestClearPageHWPoison(p))
+ if (TestClearPageHWPoison(p)) {
+ int migratetype = get_pfnblock_migratetype(p, pfn);
+
num_poisoned_pages_dec();
+ __mod_zone_freepage_state(page_zone(p), 1, migratetype);
+ }
unpoison_pr_info("Unpoison: Software-unpoisoned free page %#lx\n",
pfn, &unpoison_rs);
return 0;Thanks,
Naoya Horiguchi
Quoted 42 lines · HORIGUCHI NAOYA(堀口 直也)
Quoted 14 lines · David Hildenbrand
Quoted 13 lines · Ding Hui
Recently we found there is a lot MemFree left in
/proc/meminfoafter do a lot of pages soft offline.I think it's incorrect since
NR_FREE_PAGESshould not contain HWPoison pages. Aftertake_page_off_buddy, the page is no longer belong to buddy allocator, and will not be used any more, but we maybe missed accountingNR_FREE_PAGESin this situation.Signed-off-by Ding Hui <[email protected]>--- mm/page_alloc.c | 1 + 1 file changed, 1 insertion(+)Quoted 15 lines
mm/page_alloc.c bool take_page_off_buddy(struct page *page) · line 8953
Quoted 13 lines
mm/page_alloc.c +1
-0@@ -8947,6 +8947,7 @@ bool take_page_off_buddy(struct page *page)del_page_from_free_list (page_head, zone, page_order);break_down_buddy_pages (zone, page_head, page,0 , page_order, migratetype); +__mod_zone_page_state (zone,NR_FREE_PAGES ,-1 ); ret = true;break ; }Should this use
__mod_zone_freepage_state()instead?Yes,
__mod_zone_freepage_state()looks better to me.And I think that maybe an additional
__mod_zone_freepage_state()inunpoison_memory()is necessary to cancel the decrement. I thought of the following, but it doesn't build becauseget_pfnblock_migratetype()is available only inmm/page_alloc.c, so you might want to add a small exported routine inmm/page_alloc.cand let it called fromunpoison_memory().@@ -1899,8 +1899,12 @@ int unpoison_memory(unsigned long pfn) }
Quoted 12 lines
if (!get_hwpoison_page(p, flags, 0)) {
- if (TestClearPageHWPoison(p))
+ if (TestClearPageHWPoison(p)) {
+ int migratetype = get_pfnblock_migratetype(p, pfn);
+
num_poisoned_pages_dec();
+ __mod_zone_freepage_state(page_zone(p), 1, migratetype);
+ }
unpoison_pr_info("Unpoison: Software-unpoisoned free page %#lx\n",
pfn, &unpoison_rs);
return 0;I think there is another problem:
In normal case, we keep the last refcount of the hwpoison page, so get_hwpoison_page should return 1. The NR_FREE_PAGES will be adjusted when call put_page. At race condition, we maybe leak the page because we does not put it back to buddy in unpoison_memory, however the HWPoison flag, num_poisoned_pages, NR_FREE_PAGES is adjusted correctly.
CPU0 CPU1
soft_offline_page
soft_offline_free_page
page_handle_poison
take_page_off_buddy
SetPageHWPoison
unpoison_memory
if (!get_hwpoison_page(p))
TestClearPageHWPoison
num_poisoned_pages_dec
__mod_zone_freepage_state
return 0
/* miss put the page back to buddy */
page_ref_inc
num_poisoned_pages_incHow about do nothing and return -EBUSY (so the caller can retry) if unpoison a zero refcount page , or return 0 like 230ac719c500 ("mm/hwpoison: don't try to unpoison containment-failed pages") does ?
@@ -1736,11 +1736,9 @@ int unpoison_memory(unsigned long pfn)
} if (!get_hwpoison_page(p, flags, 0)) { - if (TestClearPageHWPoison(p))
- num_poisoned_pages_dec();
- unpoison_pr_info("Unpoison: Software-unpoisoned free page
%#lx\n",
+ unpoison_pr_info("Unpoison: Software-unpoisoned zero refcount
page %#lx\n",
pfn, &unpoison_rs);
- return 0;
+ return -EBUSY;
}
lock_page(page);Signature · 2 lines
Thanks, - Ding Hui
Quoted 55 lines · Ding Hui
Quoted 39 lines · HORIGUCHI NAOYA(堀口 直也)
Quoted 14 lines · David Hildenbrand
Quoted 13 lines · Ding Hui
Recently we found there is a lot MemFree left in
/proc/meminfoafter do a lot of pages soft offline.I think it's incorrect since
NR_FREE_PAGESshould not contain HWPoison pages. Aftertake_page_off_buddy, the page is no longer belong to buddy allocator, and will not be used any more, but we maybe missed accountingNR_FREE_PAGESin this situation.Signed-off-by Ding Hui <[email protected]>--- mm/page_alloc.c | 1 + 1 file changed, 1 insertion(+)Quoted 15 lines
mm/page_alloc.c bool take_page_off_buddy(struct page *page) · line 8953
Quoted 13 lines
mm/page_alloc.c +1
-0@@ -8947,6 +8947,7 @@ bool take_page_off_buddy(struct page *page)del_page_from_free_list (page_head, zone, page_order);break_down_buddy_pages (zone, page_head, page,0 , page_order, migratetype); +__mod_zone_page_state (zone,NR_FREE_PAGES ,-1 ); ret = true;break ; }Should this use
__mod_zone_freepage_state()instead?Yes,
__mod_zone_freepage_state()looks better to me.And I think that maybe an additional
__mod_zone_freepage_state()inunpoison_memory()is necessary to cancel the decrement. I thought of the following, but it doesn't build becauseget_pfnblock_migratetype()is available only inmm/page_alloc.c, so you might want to add a small exported routine inmm/page_alloc.cand let it called fromunpoison_memory().Quoted 14 lines
@@ -1899,8 +1899,12 @@ int unpoison_memory(unsigned long pfn) } if (!get_hwpoison_page(p, flags, 0)) { - if (TestClearPageHWPoison(p)) + if (TestClearPageHWPoison(p)) { + int migratetype = get_pfnblock_migratetype(p, pfn); + num_poisoned_pages_dec(); + __mod_zone_freepage_state(page_zone(p), 1, migratetype); + } unpoison_pr_info("Unpoison: Software-unpoisoned free page %#lx\n", pfn, &unpoison_rs); return 0;
Quoted 4 lines
I think there is another problem:
In normal case, we keep the last refcount of the hwpoison page, soget_hwpoison_pageshould return 1. TheNR_FREE_PAGESwill be adjusted when callput_page.
I think that take_page_off_buddy() should not be called for this case (the error page have remaining refcount). So it seems that no need to update NR_FREE_PAGES ?
Quoted 8 lines
At race condition, we maybe leak the page because we does not put it back to buddy in
unpoison_memory, however the HWPoison flag,num_poisoned_pages,NR_FREE_PAGESis adjusted correctly.CPU0 CPU1
soft_offline_page
soft_offline_free_page
Quoted 12 lines
page_handle_poison
take_page_off_buddy
SetPageHWPoison
unpoison_memory
if (!get_hwpoison_page(p))
TestClearPageHWPoison
num_poisoned_pages_dec
__mod_zone_freepage_state
return 0
/* miss put the page back to buddy */
page_ref_inc
num_poisoned_pages_incThanks for checking this, unpoison_memory() is racy. Recently we are suggesting to introduce mf_mutex by [1]. Although this patch is not merged to mainline yet, but it could be used to prevent the above race too.
[1] https://lore.kernel.org/linux-mm/[email protected]/
Quoted 9 lines
How about do nothing and return
-EBUSY(so the caller can retry) if unpoison a zero refcount page , or return 0 like230ac719c500("mm/hwpoison: don't try to unpoison containment-failed pages") does ?@@ -1736,11 +1736,9 @@ int unpoison_memory(unsigned long pfn) }if (!get_hwpoison_page(p, flags, 0)) {
Quoted 8 lines
- if (TestClearPageHWPoison(p))
-num_poisoned_pages_dec();
- unpoison_pr_info("Unpoison: Software-unpoisoned free page %#lx\n",
+ unpoison_pr_info("Unpoison: Software-unpoisoned zero refcount page
%#lx\n",
pfn,&unpoison_rs);
- return 0;
+ return-EBUSY;
Currently unpoison_memory() does not work as reverse operation of take_page_off_buddy() (it's simply broken), so implementing it at one time would be better. I'll take time to fix unpoison_memory().
Thanks,
Naoya Horiguchi
Quoted 56 lines · HORIGUCHI NAOYA(堀口 直也)
Quoted 55 lines · Ding Hui
Quoted 53 lines · HORIGUCHI NAOYA(堀口 直也)
Quoted 14 lines · David Hildenbrand
Quoted 13 lines · Ding Hui
Recently we found there is a lot MemFree left in
/proc/meminfoafter do a lot of pages soft offline.I think it's incorrect since
NR_FREE_PAGESshould not contain HWPoison pages. Aftertake_page_off_buddy, the page is no longer belong to buddy allocator, and will not be used any more, but we maybe missed accountingNR_FREE_PAGESin this situation.Signed-off-by Ding Hui <[email protected]>--- mm/page_alloc.c | 1 + 1 file changed, 1 insertion(+)Quoted 15 lines
mm/page_alloc.c bool take_page_off_buddy(struct page *page) · line 8953
Quoted 13 lines
mm/page_alloc.c +1
-0@@ -8947,6 +8947,7 @@ bool take_page_off_buddy(struct page *page)del_page_from_free_list (page_head, zone, page_order);break_down_buddy_pages (zone, page_head, page,0 , page_order, migratetype); +__mod_zone_page_state (zone,NR_FREE_PAGES ,-1 ); ret = true;break ; }Should this use
__mod_zone_freepage_state()instead?Yes,
__mod_zone_freepage_state()looks better to me.And I think that maybe an additional
__mod_zone_freepage_state()inunpoison_memory()is necessary to cancel the decrement. I thought of the following, but it doesn't build becauseget_pfnblock_migratetype()is available only inmm/page_alloc.c, so you might want to add a small exported routine inmm/page_alloc.cand let it called fromunpoison_memory().@@ -1899,8 +1899,12 @@ int unpoison_memory(unsigned long pfn) } if (!get_hwpoison_page(p, flags, 0)) { - if (TestClearPageHWPoison(p)) + if (TestClearPageHWPoison(p)) { + int migratetype = get_pfnblock_migratetype(p, pfn); + num_poisoned_pages_dec(); + __mod_zone_freepage_state(page_zone(p), 1, migratetype); + } unpoison_pr_info("Unpoison: Software-unpoisoned free page %#lx\n", pfn, &unpoison_rs); return 0;
Quoted 9 lines
Quoted 4 lines
I think there is another problem:
In normal case, we keep the last refcount of the hwpoison page, soget_hwpoison_pageshould return 1. TheNR_FREE_PAGESwill be adjusted when callput_page.I think that
take_page_off_buddy()should not be called for this case (the error page have remaining refcount). So it seems that no need to updateNR_FREE_PAGES?
Yes, take_page_off_buddy() only used for free pages, but we will call page_ref_inc() after that, on the other hand for in used pages, we increased the refcount by get_any_page(), so in both cases, the hwpoisoned pages have refcount great than zero.
I think there is no need to update NR_FREE_PAGES explicitly in unpoison_memory(), the put_page() will help us to update NR_FREE_PAGES and put the page back to buddy.
Quoted 21 lines
Quoted 8 lines
At race condition, we maybe leak the page because we does not put it back to buddy in
unpoison_memory, however the HWPoison flag,num_poisoned_pages,NR_FREE_PAGESis adjusted correctly.CPU0 CPU1
soft_offline_page
soft_offline_free_pageQuoted 12 lines
page_handle_poison take_page_off_buddy SetPageHWPoison unpoison_memory if (!get_hwpoison_page(p)) TestClearPageHWPoison num_poisoned_pages_dec __mod_zone_freepage_state return 0 /* miss put the page back to buddy */ page_ref_inc num_poisoned_pages_inc
Quoted 6 lines
Thanks for checking this,
unpoison_memory()is racy. Recently we are suggesting to introducemf_mutexby [1]. Although this patch is not merged to mainline yet, but it could be used to prevent the above race too.
I'll look forward to it, thanks.
Quoted 18 lines
Quoted 9 lines
How about do nothing and return
-EBUSY(so the caller can retry) if unpoison a zero refcount page , or return 0 like230ac719c500("mm/hwpoison: don't try to unpoison containment-failed pages") does ?@@ -1736,11 +1736,9 @@ int unpoison_memory(unsigned long pfn) }if (!get_hwpoison_page(p, flags, 0)) {Quoted 8 lines
- if (TestClearPageHWPoison(p))
-num_poisoned_pages_dec();
- unpoison_pr_info("Unpoison: Software-unpoisoned free page %#lx\n",
+ unpoison_pr_info("Unpoison: Software-unpoisoned zero refcount page
%#lx\n",
pfn,&unpoison_rs);
- return 0;
+ return-EBUSY;
Quoted 4 lines
Currently
unpoison_memory()does not work as reverse operation oftake_page_off_buddy()(it's simply broken), so implementing it at one time would be better. I'll take time to fixunpoison_memory().
Thanks for your work.
Actually, I'm not sure about the exactly meaning of "broken", it seems that the basic function of unpoison_memory() is ok if not considered the race conditions.
Signature · 2 lines
Thanks, - Ding Hui