mm: Don't scrub pages while holding heap lock in alloc_heap_pages()
PATCH v2 1/1 mm: Don't scrub pages while holding heap lock in alloc_heap_pages()
Instead, preserve PGC_need_scrub bit when setting PGC_state_inuse state while still under the lock and clear those pages later.
Note that we still need to grub the lock when clearing PGC_need_scrub bit since count_info might be updated during MCE handling in mark_page_offline().
---
I couldn't convince myself that just marking the head as PGC_state_inuse under the lock is safe, mostly because of how MCE handler may write the state while the allocator is walking (lock-free) the buddy.
This patch only deals with PGC_need_scrub bit which is outside state bits. _PGC_need_scrub is aliased to _PGC_allocated and I think is safe to use but we could redefine it to something else (PGC_cacheattr_base?).
xen/common/page_alloc.c | 44 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 34 insertions(+), 10 deletions(-)
xen/common/page_alloc.c +34 -10
@@ -860,6 +860,7 @@ static struct page_info *alloc_heap_pages(struct page_info *pg;bool need_tlbflush = false;uint32_t tlbflush_timestamp =0 ; +unsigned int dirty_cnt =0 ;/* Make sure there are enough bits in memflags for nodeID. */ BUILD_BUG_ON ((_MEMF_bits - _MEMF_node) < (8 *sizeof (nodeid_t)));
@@ -953,14 +954,11 @@ static struct page_info *alloc_heap_pages(/* Reference count must continuously be zero for free pages. */ BUG_ON ((pg[i].count_info & ~PGC_need_scrub) != PGC_state_free); -if (test_bit (_PGC_need_scrub, &pg[i].count_info) ) - { -if ( !(memflags & MEMF_no_scrub) ) -scrub_one_page (&pg[i]); - node_need_scrub[node]--; - } +/* PGC_need_scrub can only be set if first_dirty is valid */ +ASSERT (first_dirty !=INVALID_DIRTY_IDX || !(pg[i].count_info & PGC_need_scrub)); - pg[i].count_info = PGC_state_inuse; +/* Preserve PGC_need_scrub so we can check it after lock is dropped. */ + pg[i].count_info = PGC_state_inuse | (pg[i].count_info & PGC_need_scrub);if ( !(memflags & MEMF_no_tlbflush) ) accumulate_tlbflush(&need_tlbflush, &pg[i],
@@ -974,13 +972,39 @@ static struct page_info *alloc_heap_pages(* guest can control its own visibility of/through the cache. */ flush_page_to_ram (page_to_mfn (&pg[i]), !(memflags & MEMF_no_icache_flush)); - -if ( !(memflags & MEMF_no_scrub) ) -check_one_page (&pg[i]); }spin_unlock (&heap_lock); +if ( first_dirty !=INVALID_DIRTY_IDX || + (scrub_debug && !(memflags & MEMF_no_scrub)) ) + { +for ( i =0 ; i < (1U << order); i++ ) + { +if (test_bit (_PGC_need_scrub, &pg[i].count_info) ) + { +if ( !(memflags & MEMF_no_scrub) ) +scrub_one_page (&pg[i]); + + dirty_cnt++; + +spin_lock (&heap_lock); + pg[i].count_info &= ~PGC_need_scrub; +spin_unlock (&heap_lock); + } + +if ( !(memflags & MEMF_no_scrub) ) +check_one_page (&pg[i]); + } + +if ( dirty_cnt ) + { +spin_lock (&heap_lock); + node_need_scrub[node] -= dirty_cnt; +spin_unlock (&heap_lock); + } + } +if ( need_tlbflush )filtered_flush_tlb_mask (tlbflush_timestamp);
Signature · 7 lines
1.8.3.1 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xen.org/xen-devel
Quoted 4 lines
Quoted 1 line
Quoted 1 line · On 05.09.17 at 16:11, <[email protected]> wrote:
I couldn't convince myself that just marking the head as
PGC_state_inuseunder the lock is safe, mostly because of how MCE handler may write the state while the allocator is walking (lock-free) the buddy.
Good point.
xen/common/page_alloc.c static struct page_info *alloc_heap_pages( · line 980
Quoted 13 lines
xen/common/page_alloc.c +2
-3@@ -974,13 +972,39 @@ static struct page_info *alloc_heap_pages(* guest can control its own visibility of/through the cache. */ flush_page_to_ram (page_to_mfn (&pg[i]), !(memflags & MEMF_no_icache_flush)); - -if ( !(memflags & MEMF_no_scrub) ) -check_one_page (&pg[i]); }spin_unlock (&heap_lock); +if ( first_dirty !=INVALID_DIRTY_IDX || + (scrub_debug && !(memflags & MEMF_no_scrub)) )
Why does scrub_debug matter here?.
Quoted 7 lines
xen/common/page_alloc.c +7
-0+ { +for ( i =0 ; i < (1U << order); i++ ) + { +if (test_bit (_PGC_need_scrub, &pg[i].count_info) ) + { +if ( !(memflags & MEMF_no_scrub) ) +scrub_one_page (&pg[i]);
xen/common/page_alloc.c static struct page_info *alloc_heap_pages( · line 997
Quoted 10 lines
xen/common/page_alloc.c +10
-0+ + dirty_cnt++; + +spin_lock (&heap_lock); + pg[i].count_info &= ~PGC_need_scrub; +spin_unlock (&heap_lock); + } + +if ( !(memflags & MEMF_no_scrub) ) +check_one_page (&pg[i]);
Wouldn't this better be "else if", as checking a page just scrubbed doesn't look very useful?
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 27 lines
Quoted 4 lines
xen/common/page_alloc.c +0
-0@@ -974,13 +972,39 @@ static struct page_info *alloc_heap_pages(* guest can control its own visibility of/through the cache. */ xen/common/page_alloc.c static struct page_info *alloc_heap_pages( · line 980
Quoted 10 lines
xen/common/page_alloc.c +2
-3flush_page_to_ram (page_to_mfn (&pg[i]), !(memflags & MEMF_no_icache_flush)); - -if ( !(memflags & MEMF_no_scrub) ) -check_one_page (&pg[i]); }spin_unlock (&heap_lock); +if ( first_dirty !=INVALID_DIRTY_IDX || + (scrub_debug && !(memflags & MEMF_no_scrub)) )Why does
scrub_debugmatter here?.Quoted 11 lines
xen/common/page_alloc.c +11
-0+ { +for ( i =0 ; i < (1U << order); i++ ) + { +if (test_bit (_PGC_need_scrub, &pg[i].count_info) ) + { +if ( !(memflags & MEMF_no_scrub) ) +scrub_one_page (&pg[i]); + + dirty_cnt++; + +spin_lock (&heap_lock);
Quoted 8 lines
xen/common/page_alloc.c static struct page_info *alloc_heap_pages( · line 997
Quoted 6 lines
xen/common/page_alloc.c +6
-0+ pg[i].count_info &= ~PGC_need_scrub; +spin_unlock (&heap_lock); + } + +if ( !(memflags & MEMF_no_scrub) ) +check_one_page (&pg[i]);Wouldn't this better be "else if", as checking a page just scrubbed doesn't look very useful?
For both of these questions --- we don't want to miss a poisoned page. For example, if a page was poisoned but for some reason is not marked PGC_need_scrub.
Of course, we risk a false positive if a guest wrote the page with the same pattern.
-boris
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 38 lines
Quoted 6 lines
xen/common/page_alloc.c static struct page_info *alloc_heap_pages( · line 974
Quoted 6 lines · On 05.09.17 at 16:42, <[email protected]> wrote:
xen/common/page_alloc.c +0
-0@@ -974,13 +972,39 @@ static struct page_info *alloc_heap_pages(* guest can control its own visibility of/through the cache. */ flush_page_to_ram(page_to_mfn(&pg[i]), !(memflags &
MEMF_no_icache_flush));Quoted 22 lines
xen/common/page_alloc.c static struct page_info *alloc_heap_pages( · line 980
Quoted 9 lines
xen/common/page_alloc.c +2
-3- -if ( !(memflags & MEMF_no_scrub) ) -check_one_page (&pg[i]); }spin_unlock (&heap_lock); +if ( first_dirty !=INVALID_DIRTY_IDX || + (scrub_debug && !(memflags & MEMF_no_scrub)) )Why does
scrub_debugmatter here?.Quoted 11 lines
xen/common/page_alloc.c +11
-0+ { +for ( i =0 ; i < (1U << order); i++ ) + { +if (test_bit (_PGC_need_scrub, &pg[i].count_info) ) + { +if ( !(memflags & MEMF_no_scrub) ) +scrub_one_page (&pg[i]); + + dirty_cnt++; + +spin_lock (&heap_lock);Quoted 8 lines
xen/common/page_alloc.c static struct page_info *alloc_heap_pages( · line 997
Quoted 6 lines
xen/common/page_alloc.c +6
-0+ pg[i].count_info &= ~PGC_need_scrub; +spin_unlock (&heap_lock); + } + +if ( !(memflags & MEMF_no_scrub) ) +check_one_page (&pg[i]);Wouldn't this better be "else if", as checking a page just scrubbed doesn't look very useful?
Quoted 3 lines
For both of these questions --- we don't want to miss a poisoned page. For example, if a page was poisoned but for some reason is not marked
PGC_need_scrub.
While I can accept this as an answer to the first question, I don't see how it relates to the second one: When MEMF_no_scrub is clear, the code above obviously scrubs the page in the first if() body - what's the point of passing it to check_one_page() right afterwards?
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 34 lines · Boris Ostrovsky
Quoted 26 lines
xen/common/page_alloc.c static struct page_info *alloc_heap_pages( · line 980
Quoted 13 lines
xen/common/page_alloc.c +2
-3@@ -974,13 +972,39 @@ static struct page_info *alloc_heap_pages(* guest can control its own visibility of/through the cache. */ flush_page_to_ram (page_to_mfn (&pg[i]), !(memflags & MEMF_no_icache_flush)); - -if ( !(memflags & MEMF_no_scrub) ) -check_one_page (&pg[i]); }spin_unlock (&heap_lock); +if ( first_dirty !=INVALID_DIRTY_IDX || + (scrub_debug && !(memflags & MEMF_no_scrub)) )Why does
scrub_debugmatter here?.Quoted 11 lines
xen/common/page_alloc.c +11
-0+ { +for ( i =0 ; i < (1U << order); i++ ) + { +if (test_bit (_PGC_need_scrub, &pg[i].count_info) ) + { +if ( !(memflags & MEMF_no_scrub) ) +scrub_one_page (&pg[i]); + + dirty_cnt++; + +spin_lock (&heap_lock);Quoted 8 lines
xen/common/page_alloc.c static struct page_info *alloc_heap_pages( · line 997
Quoted 6 lines
xen/common/page_alloc.c +6
-0+ pg[i].count_info &= ~PGC_need_scrub; +spin_unlock (&heap_lock); + } + +if ( !(memflags & MEMF_no_scrub) ) +check_one_page (&pg[i]);Wouldn't this better be "else if", as checking a page just scrubbed doesn't look very useful?
Quoted 6 lines
For both of these questions --- we don't want to miss a poisoned page. For example, if a page was poisoned but for some reason is not marked
PGC_need_scrub.Of course, we risk a false positive if a guest wrote the page with the same pattern.
Just in case I wasn't clear --- I will remove scrub_debug test and add 'else' for this reason. Even though it's a debug-only feature I think we shouldn't do this.
-boris
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 41 lines
Quoted 1 line
Quoted 1 line · On 05.09.17 at 16:54, <[email protected]> wrote:
Quoted 4 lines · Boris Ostrovsky
Quoted 4 lines
xen/common/page_alloc.c static struct page_info *alloc_heap_pages( · line 974
Quoted 4 lines
xen/common/page_alloc.c +0
-0@@ -974,13 +972,39 @@ static struct page_info *alloc_heap_pages(* guest can control its own visibility of/through the cache. */ flush_page_to_ram(page_to_mfn(&pg[i]), !(memflags &
MEMF_no_icache_flush));Quoted 34 lines
Quoted 22 lines
xen/common/page_alloc.c static struct page_info *alloc_heap_pages( · line 980
Quoted 9 lines
xen/common/page_alloc.c +2
-3- -if ( !(memflags & MEMF_no_scrub) ) -check_one_page (&pg[i]); }spin_unlock (&heap_lock); +if ( first_dirty !=INVALID_DIRTY_IDX || + (scrub_debug && !(memflags & MEMF_no_scrub)) )Why does
scrub_debugmatter here?.Quoted 11 lines
xen/common/page_alloc.c +11
-0+ { +for ( i =0 ; i < (1U << order); i++ ) + { +if (test_bit (_PGC_need_scrub, &pg[i].count_info) ) + { +if ( !(memflags & MEMF_no_scrub) ) +scrub_one_page (&pg[i]); + + dirty_cnt++; + +spin_lock (&heap_lock);Quoted 8 lines
xen/common/page_alloc.c static struct page_info *alloc_heap_pages( · line 997
Quoted 6 lines
xen/common/page_alloc.c +6
-0+ pg[i].count_info &= ~PGC_need_scrub; +spin_unlock (&heap_lock); + } + +if ( !(memflags & MEMF_no_scrub) ) +check_one_page (&pg[i]);Wouldn't this better be "else if", as checking a page just scrubbed doesn't look very useful?
For both of these questions --- we don't want to miss a poisoned page. For example, if a page was poisoned but for some reason is not marked
PGC_need_scrub.
Quoted 6 lines
Quoted 2 lines
Of course, we risk a false positive if a guest wrote the page with the same pattern.
Just in case I wasn't clear --- I will remove
scrub_debugtest and add 'else' for this reason. Even though it's a debug-only feature I think we shouldn't do this.
Perhaps I'm dense, but it sounds to me like you agree to do a change you think you shouldn't do.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Quoted 41 lines · Jan Beulich
Quoted 41 lines
Quoted 1 line
Quoted 1 line · On 05.09.17 at 16:54, <[email protected]> wrote:
Quoted 4 lines · Boris Ostrovsky
Quoted 4 lines
xen/common/page_alloc.c static struct page_info *alloc_heap_pages( · line 974
Quoted 4 lines
xen/common/page_alloc.c +0
-0@@ -974,13 +972,39 @@ static struct page_info *alloc_heap_pages(* guest can control its own visibility of/through the cache. */ flush_page_to_ram(page_to_mfn(&pg[i]), !(memflags &
MEMF_no_icache_flush));Quoted 34 lines
Quoted 22 lines
xen/common/page_alloc.c static struct page_info *alloc_heap_pages( · line 980
Quoted 9 lines
xen/common/page_alloc.c +2
-3- -if ( !(memflags & MEMF_no_scrub) ) -check_one_page (&pg[i]); }spin_unlock (&heap_lock); +if ( first_dirty !=INVALID_DIRTY_IDX || + (scrub_debug && !(memflags & MEMF_no_scrub)) )Why does
scrub_debugmatter here?.Quoted 11 lines
xen/common/page_alloc.c +11
-0+ { +for ( i =0 ; i < (1U << order); i++ ) + { +if (test_bit (_PGC_need_scrub, &pg[i].count_info) ) + { +if ( !(memflags & MEMF_no_scrub) ) +scrub_one_page (&pg[i]); + + dirty_cnt++; + +spin_lock (&heap_lock);Quoted 8 lines
xen/common/page_alloc.c static struct page_info *alloc_heap_pages( · line 997
Quoted 6 lines
xen/common/page_alloc.c +6
-0+ pg[i].count_info &= ~PGC_need_scrub; +spin_unlock (&heap_lock); + } + +if ( !(memflags & MEMF_no_scrub) ) +check_one_page (&pg[i]);Wouldn't this better be "else if", as checking a page just scrubbed doesn't look very useful?
For both of these questions --- we don't want to miss a poisoned page. For example, if a page was poisoned but for some reason is not marked
PGC_need_scrub.
Quoted 7 lines
Quoted 5 lines
Quoted 2 lines
Of course, we risk a false positive if a guest wrote the page with the same pattern.
Just in case I wasn't clear --- I will remove
scrub_debugtest and add 'else' for this reason. Even though it's a debug-only feature I think we shouldn't do this.Perhaps I'm dense, but it sounds to me like you agree to do a change you think you shouldn't do.
What I meant was that with current code we have a chance of a false positive and that, even though it's a debug-only case, is probably not the way to go.
If I make the change that you suggested this false positive will not occur (although at the cost of reduced test coverage).
-boris
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel