mm/page_alloc: Fix race conditions on getting migratetype in buffered_rmqueue
Subsystems (2)
PATCH 1/1 mm/page_alloc: Fix race conditions on getting migratetype in buffered_rmqueue
From: Hui Zhu <[email protected]>
To test the patch [1], I use KGTP and a script [2] to show NR_FREE_CMA_PAGES and gross of cma_nr_free. The values are always not same.
I check the code of pages alloc and free and found that race conditions on getting migratetype in buffered_rmqueue.
Then I add move the code of getting migratetype inside the zone->lock protection part.
Because this issue will affect system even if the Linux kernel does't have [1]. So I post this patch separately.
This patchset is based on fc7f0dd381720ea5ee5818645f7d0e9dece41cb0.
[1] https://lkml.org/lkml/2015/1/18/28
[2] https://github.com/teawater/kgtp/blob/dev/add-ons/cma_free.py
--- mm/page_alloc.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)
mm/page_alloc.c +7 -4
@@ -1694,11 +1694,12 @@ again: }spin_lock_irqsave (&zone->lock, flags); page =__rmqueue (zone, order, migratetype); +if (page) + migratetype =get_pageblock_migratetype (page); +else + goto failed_unlock;spin_unlock (&zone->lock); -if (!page) - goto failed; -__mod_zone_freepage_state (zone, -(1 << order), -get_freepage_migratetype (page)); +__mod_zone_freepage_state (zone, -(1 << order), migratetype); }__mod_zone_page_state (zone,NR_ALLOC_BATCH , -(1 << order));
@@ -1715,6 +1716,8 @@ again: goto again;return page; +failed_unlock: +spin_unlock (&zone->lock); failed:local_irq_restore (flags);return NULL ;
Signature · 7 lines
1.9.3 -- To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM, see: http://www.linux-mm.org/ . Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 6 lines · Hui Zhu
From: Hui Zhu <[email protected]>
To test the patch [1], I use KGTP and a script [2] to show
NR_FREE_CMA_PAGESand gross ofcma_nr_free. The values are always not same.
I check the code of pages alloc and free and found that race conditions on getting migratetype inbuffered_rmqueue.
Can you elaborate? What does this races with, are you dynamically changing the size of CMA area, or what? The migratetype here is based on which free list the page was found on. Was it misplaced then? Wasn't Joonsoo's recent series supposed to eliminate this?
Quoted 2 lines
Then I add move the code of getting migratetype inside the
zone->lockprotection part.
Not just that, you are also reading migratetype from pageblock bitmap instead of the one embedded in the free page. Which is more expensive and we already do that more often than we would like to because of CMA. And it appears to be a wrong fix for a possible misplacement bug. If there's such misplacement, the wrong stats are not the only problem.
Quoted 3 lines
Because this issue will affect system even if the Linux kernel does't have [1]. So I post this patch separately.
But we can't test that without [1], right? Maybe the issue is introduced by [1]?
Quoted 33 lines
This patchset is based on
fc7f0dd381720ea5ee5818645f7d0e9dece41cb0.[1] https://lkml.org/lkml/2015/1/18/28
[2] https://github.com/teawater/kgtp/blob/dev/add-ons/cma_free.pySigned-off-by Hui Zhu <[email protected]>--- mm/page_alloc.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)mm/page_alloc.c +5
-4@@ -1694,11 +1694,12 @@ again: }spin_lock_irqsave (&zone->lock, flags); page =__rmqueue (zone, order, migratetype); +if (page) + migratetype =get_pageblock_migratetype (page); +else + goto failed_unlock;spin_unlock (&zone->lock); -if (!page) - goto failed; -__mod_zone_freepage_state (zone, -(1 << order), -get_freepage_migratetype (page)); +__mod_zone_freepage_state (zone, -(1 << order), migratetype); }__mod_zone_page_state (zone,NR_ALLOC_BATCH , -(1 << order));
mm/page_alloc.c again: · line 1723
Quoted 8 lines
mm/page_alloc.c +2
-0@@ -1715,6 +1716,8 @@ again: goto again;return page; +failed_unlock: +spin_unlock (&zone->lock); failed:local_irq_restore (flags);return NULL ;
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
Quoted 11 lines · Vlastimil Babka
Quoted 8 lines · Hui Zhu
From: Hui Zhu <[email protected]>
To test the patch [1], I use KGTP and a script [2] to show
NR_FREE_CMA_PAGES
and gross ofcma_nr_free. The values are always not same.
I check the code of pages alloc and free and found that race conditions on getting migratetype inbuffered_rmqueue.
Quoted 4 lines
Can you elaborate? What does this races with, are you dynamically changing the size of CMA area, or what? The migratetype here is based on which free list the page was found on. Was it misplaced then? Wasn't Joonsoo's recent series supposed to eliminate this?
My bad.
I thought move_freepages has race condition with this part. But I missed it will check PageBuddy before set_freepage_migratetype. Sorry for that.
I will do more work around this one and [1].
Thanks for your review.
Best,
Hui
Quoted 52 lines
Quoted 3 lines
Then I add move the code of getting migratetype inside the
zone->lockprotection part.Not just that, you are also reading migratetype from pageblock bitmap instead of the one embedded in the free page. Which is more expensive and we already do that more often than we would like to because of CMA. And it appears to be a wrong fix for a possible misplacement bug. If there's such misplacement, the wrong stats are not the only problem.
Quoted 3 lines
Because this issue will affect system even if the Linux kernel does't have [1]. So I post this patch separately.
But we can't test that without [1], right? Maybe the issue is introduced by [1]?
Quoted 32 lines
This patchset is based on
fc7f0dd381720ea5ee5818645f7d0e9dece41cb0.[1] https://lkml.org/lkml/2015/1/18/28
[2] https://github.com/teawater/kgtp/blob/dev/add-ons/cma_free.pySigned-off-by Hui Zhu <[email protected]>--- mm/page_alloc.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-)mm/page_alloc.c +5
-4@@ -1694,11 +1694,12 @@ again: }spin_lock_irqsave (&zone->lock, flags); page =__rmqueue (zone, order, migratetype); +if (page) + migratetype =get_pageblock_migratetype (page); +else + goto failed_unlock;spin_unlock (&zone->lock); -if (!page) - goto failed; -__mod_zone_freepage_state (zone, -(1 << order), -get_freepage_migratetype (page)); +__mod_zone_freepage_state (zone, -(1 << order), migratetype); }__mod_zone_page_state (zone,NR_ALLOC_BATCH , -(1 << order));
Quoted 10 lines
mm/page_alloc.c again: · line 1722
Quoted 8 lines
mm/page_alloc.c +1
-0@@ -1715,6 +1716,8 @@ again: goto again;return page; +failed_unlock: +spin_unlock (&zone->lock); failed:local_irq_restore (flags);return NULL ;
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>