x86/PoD: correct ordering of checks in p2m_pod_zero_check()
PATCH 1/1 x86/PoD: correct ordering of checks in p2m_pod_zero_check()
Commit 0537d246f8db ("mm: add 'is_special_page' inline function...") moved the is_special_page() checks first in its respective changes to PoD code. While this is fine for p2m_pod_zero_check_superpage(), the validity of the MFN is inferred in both cases from the p2m_is_ram() check, which therefore also needs to come first in this 2nd instance.
Take the opportunity and address latent UB here as well - transform the MFN into struct page_info * only after having established that this is a valid page.
---
I will admit that this was build tested only. I did observe the crash late yesterday while in the office, but got around to analyzing it only today, where I'm again restricted in what I can reasonably test.
xen/arch/x86/mm/p2m-pod.c +11 -9
@@ -877,23 +877,25 @@ p2m_pod_zero_check(struct p2m_domain *p2for ( i =0 ; i < count; i++ ) {p2m_access_t a; -struct page_info *pg; mfns[i] = p2m->get_entry (p2m, gfns[i], types + i, &a,0 ,NULL ,NULL ); - pg =mfn_to_page (mfns[i]);/* * If this is ram, and not a pagetable or a special page, and * probably not mapped elsewhere, map it; otherwise, skip. */ -if ( !is_special_page (pg) &&p2m_is_ram (types[i]) && - (pg->count_info & PGC_allocated) && - !(pg->count_info & PGC_page_table) && - ((pg->count_info & PGC_count_mask) <= max_ref) ) - map[i] =map_domain_page (mfns[i]); -else - map[i] =NULL ; + map[i] =NULL ; +if (p2m_is_ram (types[i]) ) + { +const struct page_info *pg =mfn_to_page (mfns[i]); + +if ( !is_special_page (pg) && + (pg->count_info & PGC_allocated) && + !(pg->count_info & PGC_page_table) && + ((pg->count_info & PGC_count_mask) <= max_ref) ) + map[i] =map_domain_page (mfns[i]); + } }/*
Quoted 14 lines
-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: 07 April 2020 12:08
To: [email protected]
Cc: Andrew Cooper <[email protected]>; Roger Pau Monné <[email protected]>; Wei Liu <[email protected]>; Paul Durrant <[email protected]>
Subject: [PATCH] x86/PoD: correct ordering of checks inp2m_pod_zero_check()Commit
0537d246f8db("mm: add 'is_special_page' inline function...") moved theis_special_page()checks first in its respective changes to PoD code. While this is fine forp2m_pod_zero_check_superpage(), the validity of the MFN is inferred in both cases from thep2m_is_ram()check, which therefore also needs to come first in this 2nd instance.
Quoted 5 lines
Take the opportunity and address latent UB here as well - transform the MFN into struct
page_info* only after having established that this is a valid page.Signed-off-by Jan Beulich <[email protected]>
...with a suggestion below
Quoted 22 lines
---
I will admit that this was build tested only. I did observe the crash late yesterday while in the office, but got around to analyzing it only today, where I'm again restricted in what I can reasonably test.xen/arch/x86/mm/p2m-pod.c +0
-3@@ -877,23 +877,25 @@ p2m_pod_zero_check(struct p2m_domain *p2for ( i =0 ; i < count; i++ ) {p2m_access_t a; -struct page_info *pg; mfns[i] = p2m->get_entry (p2m, gfns[i], types + i, &a,0 ,NULL ,NULL ); - pg =mfn_to_page (mfns[i]);/* * If this is ram, and not a pagetable or a special page, and * probably not mapped elsewhere, map it; otherwise, skip. */ - if ( !is_special_page(pg) && p2m_is_ram(types[i]) &&
xen/arch/x86/mm/p2m-pod.c p2m_pod_zero_check(struct p2m_domain *p2 · line 891
Quoted 10 lines
xen/arch/x86/mm/p2m-pod.c +4
-6- (pg->count_info & PGC_allocated) && - !(pg->count_info & PGC_page_table) && - ((pg->count_info & PGC_count_mask) <= max_ref) ) - map[i] = map_domain_page(mfns[i]); - else - map[i] =NULL ; + map[i] =NULL ; +if (p2m_is_ram (types[i]) ) + { +const struct page_info *pg =mfn_to_page (mfns[i]);
Perhaps have local scope stack variable for count_info too?
Quoted 10 lines
xen/arch/x86/mm/p2m-pod.c +7
-0+ +if ( !is_special_page (pg) && + (pg->count_info & PGC_allocated) && + !(pg->count_info & PGC_page_table) && + ((pg->count_info & PGC_count_mask) <= max_ref) ) + map[i] =map_domain_page (mfns[i]); + } }/*
Quoted 14 lines · Paul Durrant
Quoted 14 lines
-----Original Message-----
From: Jan Beulich <[email protected]>
Sent: 07 April 2020 12:08
To: [email protected]
Cc: Andrew Cooper <[email protected]>; Roger Pau Monné <[email protected]>; Wei Liu <[email protected]>; Paul Durrant <[email protected]>
Subject: [PATCH] x86/PoD: correct ordering of checks inp2m_pod_zero_check()Commit
0537d246f8db("mm: add 'is_special_page' inline function...") moved theis_special_page()checks first in its respective changes to PoD code. While this is fine forp2m_pod_zero_check_superpage(), the validity of the MFN is inferred in both cases from thep2m_is_ram()check, which therefore also needs to come first in this 2nd instance.
Quoted 7 lines
Quoted 5 lines
Take the opportunity and address latent UB here as well - transform the MFN into struct
page_info* only after having established that this is a valid page.Signed-off-by Jan Beulich <[email protected]>Reviewed-by Paul Durrant <[email protected]>
Thanks.
Quoted 28 lines
...with a suggestion below
Quoted 26 lines
---
I will admit that this was build tested only. I did observe the crash late yesterday while in the office, but got around to analyzing it only today, where I'm again restricted in what I can reasonably test.xen/arch/x86/mm/p2m-pod.c +0
-7@@ -877,23 +877,25 @@ p2m_pod_zero_check(struct p2m_domain *p2for ( i =0 ; i < count; i++ ) {p2m_access_t a; -struct page_info *pg; mfns[i] = p2m->get_entry (p2m, gfns[i], types + i, &a,0 ,NULL ,NULL ); - pg =mfn_to_page (mfns[i]);/* * If this is ram, and not a pagetable or a special page, and * probably not mapped elsewhere, map it; otherwise, skip. */ -if ( !is_special_page (pg) &&p2m_is_ram (types[i]) && - (pg->count_info & PGC_allocated) && - !(pg->count_info & PGC_page_table) && - ((pg->count_info & PGC_count_mask) <= max_ref) ) - map[i] =map_domain_page (mfns[i]);
Quoted 8 lines
xen/arch/x86/mm/p2m-pod.c p2m_pod_zero_check(struct p2m_domain *p2 · line 891
Quoted 6 lines
xen/arch/x86/mm/p2m-pod.c +4
-2-else - map[i] =NULL ; + map[i] =NULL ; +if (p2m_is_ram (types[i]) ) + { +const struct page_info *pg =mfn_to_page (mfns[i]);Perhaps have local scope stack variable for
count_infotoo?
I'd view this as useful only if ...
Quoted 2 lines
xen/arch/x86/mm/p2m-pod.c p2m_pod_zero_check(struct p2m_domain *p2 · line 893
Quoted 2 lines
xen/arch/x86/mm/p2m-pod.c +2
-0+ + if ( !is_special_page(pg) &&
... this could then also be made make use of it.
Jan
Quoted 11 lines · Jan Beulich
Commit
0537d246f8db("mm: add 'is_special_page' inline function...") moved theis_special_page()checks first in its respective changes to PoD code. While this is fine forp2m_pod_zero_check_superpage(), the validity of the MFN is inferred in both cases from thep2m_is_ram()check, which therefore also needs to come first in this 2nd instance.Take the opportunity and address latent UB here as well - transform the MFN into struct
page_info* only after having established that this is a valid page.Signed-off-by Jan Beulich <[email protected]>