Skip to contentSkip to conversation

x86/p2m: type checking adjustments

PATCH Superseded Jan Beulich 2 participants 5 messages Feb 23, 2022 – Feb 24, 2022

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover x86/p2m: type checking adjustments

While the first change is a bug fix (for, admittedly, a case which apparently hasn't occurred in practice, or else we would have had bug reports), it already puts in place an instance of what the 2nd patch is proposing for perhaps wider use.

1: make p2m_get_page_from_gfn() handle grant and shared cases better 2: aid the compiler in folding p2m_is_...()

Jan

#1 lore raw

PATCH 1/2 x86/p2m: make p2m_get_page_from_gfn() handle grant and shared cases better

Grant P2M entries, which are covered by p2m_is_any_ram(), wouldn't pass the get_page() unless the grant was a local one. These need to take the same path as foreign entries. Just the assertion there is not valid for local grants, and hence it triggering needs to be avoided.

Shared entries, when unshare is requested, would bypass the retrieval of "page" and thus always take the error path rather than actually trying to unshare by taking the slow path.

---
Using | instead of || helps the compiler fold the two p2m_is_*().

xen/arch/x86/mm/p2m.c +3 -2
@@ -584,11 +584,11 @@ struct page_info *p2m_get_page_from_gfn(
              && !((q & P2M_UNSHARE) && p2m_is_shared(*t)) )
         {
             page = mfn_to_page(mfn);
-            if ( unlikely(p2m_is_foreign(*t)) )
+            if ( unlikely(p2m_is_foreign(*t) | p2m_is_grant(*t)) )
             {
                 struct domain *fdom = page_get_owner_and_reference(page);
 
-                ASSERT(fdom != p2m->domain);
+                ASSERT(!p2m_is_foreign(*t) || fdom != p2m->domain);
                 if ( fdom == NULL )
                     page = NULL;
             }
@@ -607,6 +607,7 @@ struct page_info *p2m_get_page_from_gfn(
 
         /* Error path: not a suitable GFN at all */
         if ( !p2m_is_ram(*t) && !p2m_is_paging(*t) && !p2m_is_pod(*t) &&
+             (!p2m_is_shared(*t) || !(q & P2M_UNSHARE)) &&
              !mem_sharing_is_fork(p2m->domain) )
             return NULL;
     }
#2 lore raw

Re: [PATCH 1/2] x86/p2m: make p2m_get_page_from_gfn() handle grant and shared cases better

xen/arch/x86/mm/p2m.c struct page_info *p2m_get_page_from_gfn( · line 613

Quoted 8 lines
xen/arch/x86/mm/p2m.c +1 -0
@@ -607,6 +607,7 @@ struct page_info *p2m_get_page_from_gfn(
 
         /* Error path: not a suitable GFN at all */
         if ( !p2m_is_ram(*t) && !p2m_is_paging(*t) && !p2m_is_pod(*t) &&
+             (!p2m_is_shared(*t) || !(q & P2M_UNSHARE)) &&
              !mem_sharing_is_fork(p2m->domain) )
             return NULL;
     }

I don't follow what this is fixing. A shared entry would return true to p2m_is_ram() - p2m_ram_shared is listed under P2M_RAM_TYPES - so the rest of the if statement would never be checked. So if we get past that check we know we definitely don't have a shared entry, ie p2m_is_shared must be false ie the check for P2M_UNSHARE is dead code. Am I missing something?

Tamas

Tamas

#3 lore raw

Re: [PATCH 1/2] x86/p2m: make p2m_get_page_from_gfn() handle grant and shared cases better

Quoted 15 lines · Tamas K Lengyel

xen/arch/x86/mm/p2m.c struct page_info *p2m_get_page_from_gfn( · line 613

Quoted 8 lines
xen/arch/x86/mm/p2m.c +1 -0
@@ -607,6 +607,7 @@ struct page_info *p2m_get_page_from_gfn(
 
         /* Error path: not a suitable GFN at all */
         if ( !p2m_is_ram(*t) && !p2m_is_paging(*t) && !p2m_is_pod(*t) &&
+             (!p2m_is_shared(*t) || !(q & P2M_UNSHARE)) &&
              !mem_sharing_is_fork(p2m->domain) )
             return NULL;
     }

I don't follow what this is fixing. A shared entry would return true to p2m_is_ram() - p2m_ram_shared is listed under P2M_RAM_TYPES - so the rest of the if statement would never be checked. So if we get past that check we know we definitely don't have a shared entry, ie p2m_is_shared must be false ie the check for P2M_UNSHARE is dead code. Am I missing something?

No, I am. I mistakenly took p2m_is_any_ram() to include the shared case, but p2m_is_ram() to not do so. Thanks for pointing out, and I'm actually happy to be able to droop this hunk.

Jan

#4 lore raw

RFC PATCH 2/2 x86/p2m: aid the compiler in folding p2m_is_...()

In reply to Jan Beulich

By using | instead of || or (in the negated form) && chances increase for the compiler to recognize that both predicates can actually be folded into an expression requiring just a single branch (via OR-ing together the respective P2M_*_TYPES constants).

---
RFC: The 3-way checks look to be a general problem for gcc, but even in
some 2-way cases it doesn't manage to fold the expressions. Hence it's worth considering to go farther with this transformation, as long as the idea isn't disliked in general.

xen/arch/x86/mm/p2m.c +2 -2
@@ -606,7 +606,7 @@ struct page_info *p2m_get_page_from_gfn(
             return page;
 
         /* Error path: not a suitable GFN at all */
-        if ( !p2m_is_ram(*t) && !p2m_is_paging(*t) && !p2m_is_pod(*t) &&
+        if ( !(p2m_is_ram(*t) | p2m_is_paging(*t) | p2m_is_pod(*t)) &&
              (!p2m_is_shared(*t) || !(q & P2M_UNSHARE)) &&
              !mem_sharing_is_fork(p2m->domain) )
             return NULL;
@@ -840,7 +840,7 @@ p2m_remove_page(struct p2m_domain *p2m,
     for ( i = 0; i < (1UL << page_order); ++i )
     {
         p2m->get_entry(p2m, gfn_add(gfn, i), &t, &a, 0, NULL, NULL);
-        if ( !p2m_is_hole(t) && !p2m_is_special(t) && !p2m_is_shared(t) )
+        if ( !(p2m_is_hole(t) | p2m_is_special(t) | p2m_is_shared(t)) )
         {
             set_gpfn_from_mfn(mfn_x(mfn) + i, gfn_x(gfn) + i);
             paging_mark_pfn_dirty(p2m->domain, _pfn(gfn_x(gfn) + i));