xen/mm: Avoid assuming the page is inuse in assign_pages()
PATCH v2 1/1 xen/mm: Avoid assuming the page is inuse in assign_pages()
From: Julien Grall <[email protected]>
At the moment, assign_pages() on the page to be inuse (PGC_state_inuse) and the state value to be 0.
However, the code may race with the page offlining code (see offline_page()). Depending on the ordering, the page may be in offlining state (PGC_state_offlining) before it is assigned to a domain.
On debug build, this may result to hit the assert or just clobber the state. On non-debug build, the state will get clobbered.
Incidentally the flag PGC_broken will get clobbered as well.
Grab the heap_lock to prevent a race with offline_page() and keep the state and broken flag around.
---
Changes in v2:
- Superseed <[email protected]>
- Fix the race with offline_page()
--- xen/common/page_alloc.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)
xen/common/page_alloc.c +14 -2
@@ -2283,15 +2283,27 @@ int assign_pages(get_knownalive_domain (d); } +spin_lock (&heap_lock);for ( i =0 ; i < (1 << order); i++ ) { +/* +* We should only be here if the page is inuse or offlining. +* The latter happen if we race with mark_page_offline() as we +* don't hold the heap_lock. +*/ +ASSERT (page_state_is (&pg[i], inuse) || +page_state_is (&pg[i], offlining)); +ASSERT (!(pg[i].count_info & ~(PGC_state | PGC_broken)));ASSERT (page_get_owner (&pg[i]) ==NULL ); -ASSERT (!pg[i].count_info);page_set_owner (&pg[i], d);smp_wmb ();/* Domain pointer must be visible before updating refcnt. */ - pg[i].count_info = PGC_allocated |1 ; + + pg[i].count_info &= PGC_state | PGC_broken; + pg[i].count_info |= PGC_allocated |1 ; +page_list_add_tail (&pg[i], &d->page_list); } +spin_unlock (&heap_lock); out:spin_unlock (&d->page_alloc_lock);
Signature · 7 lines
2.17.1 _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 21 lines
-----Original Message-----
From: Julien Grall <[email protected]>
Sent: 06 February 2020 10:39
To: [email protected]
Cc: [email protected]; Durrant, Paul <[email protected]>; Grall, Julien <[email protected]>
Subject: [PATCH v2] xen/mm: Avoid assuming the page is inuse inassign_pages()From: Julien Grall <[email protected]>
At the moment,
assign_pages()on the page to be inuse (PGC_state_inuse) and the state value to be 0.However, the code may race with the page offlining code (see
offline_page()). Depending on the ordering, the page may be in offlining state (PGC_state_offlining) before it is assigned to a domain.On debug build, this may result to hit the assert or just clobber the state. On non-debug build, the state will get clobbered.
Quoted 6 lines
Incidentally the flag
PGC_brokenwill get clobbered as well.Grab the
heap_lockto prevent a race withoffline_page()and keep the state and broken flag around.Signed-off-by Julien Grall <[email protected]>
This seems like a reasonable change. I guess having assign_pages() take the global lock is no more problem than its existing call to domain_adjust_tot_pages() which also takes the same lock.
Paul
Quoted 45 lines
---
Changes in v2:
- Superseed <[email protected]>
- Fix the race withoffline_page()--- xen/common/page_alloc.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-)xen/common/page_alloc.c +9
-1@@ -2283,15 +2283,27 @@ int assign_pages(get_knownalive_domain (d); } +spin_lock (&heap_lock);for ( i =0 ; i < (1 << order); i++ ) { +/* +* We should only be here if the page is inuse or offlining. +* The latter happen if we race with mark_page_offline() as we +* don't hold the heap_lock. +*/ +ASSERT (page_state_is (&pg[i], inuse) || +page_state_is (&pg[i], offlining)); +ASSERT (!(pg[i].count_info & ~(PGC_state | PGC_broken)));ASSERT (page_get_owner (&pg[i]) ==NULL ); -ASSERT (!pg[i].count_info);page_set_owner (&pg[i], d);smp_wmb ();/* Domain pointer must be visible before updating refcnt. */
xen/common/page_alloc.c +5
-2- pg[i].count_info = PGC_allocated |1 ; + + pg[i].count_info &= PGC_state | PGC_broken; + pg[i].count_info |= PGC_allocated |1 ; +page_list_add_tail (&pg[i], &d->page_list); } +spin_unlock (&heap_lock); out:spin_unlock (&d->page_alloc_lock); --
Quoted 1 line
2.17.1
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Hi Paul,
Quoted 21 lines · Paul
Quoted 21 lines
-----Original Message-----
From: Julien Grall <[email protected]>
Sent: 06 February 2020 10:39
To: [email protected]
Cc: [email protected]; Durrant, Paul <[email protected]>; Grall, Julien <[email protected]>
Subject: [PATCH v2] xen/mm: Avoid assuming the page is inuse inassign_pages()From: Julien Grall <[email protected]>
At the moment,
assign_pages()on the page to be inuse (PGC_state_inuse) and the state value to be 0.However, the code may race with the page offlining code (see
offline_page()). Depending on the ordering, the page may be in offlining state (PGC_state_offlining) before it is assigned to a domain.On debug build, this may result to hit the assert or just clobber the state. On non-debug build, the state will get clobbered.
Quoted 8 lines
Quoted 6 lines
Incidentally the flag
PGC_brokenwill get clobbered as well.Grab the
heap_lockto prevent a race withoffline_page()and keep the state and broken flag around.Signed-off-by Julien Grall <[email protected]>This seems like a reasonable change. I guess having
assign_pages()take the global lock is no more problem than its existing call todomain_adjust_tot_pages()which also takes the same lock.
That's my understanding. Summarizing our discussion IRL for the other, it is not clear whether the lock is enough here.
From my understanding the sequence
pg[i].count_info &= ...;
pg[i].count_info |= ...;
could result to multiple read/write from the compiler. We could use a single assignment, but I still don't think this prevent the compiler to be use multiple read/write.
The concern would be a race with get_page_owner_and_reference(). If 1 is set before the rest of the bits, then you may be able to get the page.
So I might want to use write_atomic() below. Any opinion?
Cheers,
Signature · 6 lines
Julien Grall _______________________________________________ Xen-devel mailing list [email protected] https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 53 lines
-----Original Message-----
From: Julien Grall <[email protected]>
Sent: 06 February 2020 11:17
To: Durrant, Paul <[email protected]>; [email protected]
Cc: Grall, Julien <[email protected]>
Subject: Re: [PATCH v2] xen/mm: Avoid assuming the page is inuse inassign_pages()Hi Paul,
Quoted 5 lines · Paul
Quoted 5 lines
-----Original Message-----
From: Julien Grall <[email protected]>
Sent: 06 February 2020 10:39
To: [email protected]
Cc: [email protected]; Durrant, Paul <[email protected]>; Grall,Julien
Quoted 11 lines
Quoted 11 lines
<[email protected]>
Subject: [PATCH v2] xen/mm: Avoid assuming the page is inuse inassign_pages()From: Julien Grall <[email protected]>
At the moment,
assign_pages()on the page to be inuse (PGC_state_inuse) and the state value to be 0.However, the code may race with the page offlining code (see
offline_page()). Depending on the ordering, the page may be inofflining
Quoted 5 lines
Quoted 5 lines
state (
PGC_state_offlining) before it is assigned to a domain.On debug build, this may result to hit the assert or just clobber the state. On non-debug build, the state will get clobbered.
Quoted 8 lines
Quoted 6 lines
Incidentally the flag
PGC_brokenwill get clobbered as well.Grab the
heap_lockto prevent a race withoffline_page()and keep the state and broken flag around.Signed-off-by Julien Grall <[email protected]>This seems like a reasonable change. I guess having
assign_pages()takethe global lock is no more problem than its existing call to
domain_adjust_tot_pages()which also takes the same lock.That's my understanding. Summarizing our discussion IRL for the other, it is not clear whether the lock is enough here.
From my understanding the sequence
pg[i].count_info &= ...;
pg[i].count_info |= ...;
Quoted 9 lines
could result to multiple read/write from the compiler. We could use a single assignment, but I still don't think this prevent the compiler to be use multiple read/write.
The concern would be a race with
get_page_owner_and_reference(). If 1 is set before the rest of the bits, then you may be able to get the page.So I might want to use
write_atomic()below. Any opinion?
TBH I wonder if we ought to say that any update to count_info ought to be done by a write_atomic (where it's not already done by cmpxchg).
Paul
Quoted 4 lines
Cheers,
--
Julien Grall
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 57 lines · Paul
Quoted 57 lines
-----Original Message-----
From: Julien Grall <[email protected]>
Sent: 06 February 2020 11:17
To: Durrant, Paul <[email protected]>; [email protected]
Cc: Grall, Julien <[email protected]>
Subject: Re: [PATCH v2] xen/mm: Avoid assuming the page is inuse inassign_pages()Hi Paul,
Quoted 5 lines · Paul
Quoted 5 lines
-----Original Message-----
From: Julien Grall <[email protected]>
Sent: 06 February 2020 10:39
To: [email protected]
Cc: [email protected]; Durrant, Paul <[email protected]>; Grall,Julien
Quoted 11 lines
Quoted 11 lines
<[email protected]>
Subject: [PATCH v2] xen/mm: Avoid assuming the page is inuse inassign_pages()From: Julien Grall <[email protected]>
At the moment,
assign_pages()on the page to be inuse (PGC_state_inuse) and the state value to be 0.However, the code may race with the page offlining code (see
offline_page()). Depending on the ordering, the page may be inofflining
Quoted 5 lines
Quoted 5 lines
state (
PGC_state_offlining) before it is assigned to a domain.On debug build, this may result to hit the assert or just clobber the state. On non-debug build, the state will get clobbered.
Quoted 8 lines
Quoted 6 lines
Incidentally the flag
PGC_brokenwill get clobbered as well.Grab the
heap_lockto prevent a race withoffline_page()and keep the state and broken flag around.Signed-off-by Julien Grall <[email protected]>This seems like a reasonable change. I guess having
assign_pages()takethe global lock is no more problem than its existing call to
domain_adjust_tot_pages()which also takes the same lock.That's my understanding. Summarizing our discussion IRL for the other, it is not clear whether the lock is enough here.
From my understanding the sequence
pg[i].count_info &= ...;
pg[i].count_info |= ...;could result to multiple read/write from the compiler. We could use a single assignment, but I still don't think this prevent the compiler to be use multiple read/write.
Quoted 8 lines
Quoted 5 lines
The concern would be a race with
get_page_owner_and_reference(). If 1 is set before the rest of the bits, then you may be able to get the page.So I might want to use
write_atomic()below. Any opinion?TBH I wonder if we ought to say that any update to
count_infoought to be done by awrite_atomic(where it's not already done by cmpxchg).
I agree.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 57 lines · Jan Beulich
Quoted 56 lines · Paul
Quoted 56 lines
-----Original Message-----
From: Julien Grall <[email protected]>
Sent: 06 February 2020 11:17
To: Durrant, Paul <[email protected]>; [email protected]
Cc: Grall, Julien <[email protected]>
Subject: Re: [PATCH v2] xen/mm: Avoid assuming the page is inuse inassign_pages()Hi Paul,
Quoted 5 lines · Paul
Quoted 5 lines
-----Original Message-----
From: Julien Grall <[email protected]>
Sent: 06 February 2020 10:39
To: [email protected]
Cc: [email protected]; Durrant, Paul <[email protected]>; Grall,Julien
Quoted 11 lines
Quoted 11 lines
<[email protected]>
Subject: [PATCH v2] xen/mm: Avoid assuming the page is inuse inassign_pages()From: Julien Grall <[email protected]>
At the moment,
assign_pages()on the page to be inuse (PGC_state_inuse) and the state value to be 0.However, the code may race with the page offlining code (see
offline_page()). Depending on the ordering, the page may be inofflining
Quoted 5 lines
Quoted 5 lines
state (
PGC_state_offlining) before it is assigned to a domain.On debug build, this may result to hit the assert or just clobber the state. On non-debug build, the state will get clobbered.
Quoted 7 lines
Quoted 6 lines
Incidentally the flag
PGC_brokenwill get clobbered as well.Grab the
heap_lockto prevent a race withoffline_page()and keep the state and broken flag around.Signed-off-by Julien Grall <[email protected]>This seems like a reasonable change. I guess having
assign_pages()takethe global lock is no more problem than its existing call to
domain_adjust_tot_pages()which also takes the same lock.That's my understanding. Summarizing our discussion IRL for the other, it is not clear whether the lock is enough here.
From my understanding the sequence
pg[i].count_info &= ...;
pg[i].count_info |= ...;could result to multiple read/write from the compiler. We could use a single assignment, but I still don't think this prevent the compiler to be use multiple read/write.
Quoted 8 lines
Quoted 7 lines
Quoted 5 lines
The concern would be a race with
get_page_owner_and_reference(). If 1 is set before the rest of the bits, then you may be able to get the page.So I might want to use
write_atomic()below. Any opinion?TBH I wonder if we ought to say that any update to
count_infoought to be done by awrite_atomic(where it's not already done by cmpxchg).I agree.
It won't fix anything, and gives the compiler a harder time.
write_atomic() is a mov instruction. It prohibits the use of and/or $imm, mem encodings.
If multiple reads/writes are a concern then the only valid code generation is for *every* modification of count info to use locked operations.
Swapping regular C for a single mov instruction here is not going to fix a bug, if such a bug exists.
~Andrew
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel
Quoted 8 lines · Julien Grall
However, the code may race with the page offlining code (see
offline_page()). Depending on the ordering, the page may be in offlining state (PGC_state_offlining) before it is assigned to a domain.On debug build, this may result to hit the assert or just clobber the state. On non-debug build, the state will get clobbered.
Incidentally the flag
PGC_brokenwill get clobbered as well.
As mentioned when I first pointed out this issue, it is wider than just assign_pages() afaict, which is specifically why I said I wouldn't expect you to want to deal with it alongside the "implicit inuse" aspect. Fixing just one instance of it without also addressing the others isn't going to help. IOW you could leave the code the way it was in v1 in this regard, and then we (you, me, or yet someone else) take care of the race aspect globally for the tree.
Jan
_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xenproject.org/mailman/listinfo/xen-devel