Skip to contentSkip to conversation

x86/HVM: improve local variable use in hvm_hap_nested_page_fault()

PATCH In review Jan Beulich 2 participants 2 messages Apr 9, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86/HVM: improve local variable use in hvm_hap_nested_page_fault()

First gfn can be set just once, rather than (conditionally) twice.

And then gfn can be used in two function calls, rather than re- calculating the value there.

---
I wasn't quite sure about continuing to use an open-coded shift. PFN_DOWN() could be used, or paddr_to_pfn(). Neither looks to be an overly good fit to translate a gaddr to a gfn, yet gaddr_to_gfn() can't be used quite nicely either as long as gfn isn't of type gfn_t.

xen/arch/x86/hvm/hvm.c +6 -5
@@ -1794,7 +1794,7 @@ void hvm_inject_event(const struct x86_e
 int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
                               struct npfec npfec)
 {
-    unsigned long gfn = gpa >> PAGE_SHIFT;
+    unsigned long gfn;
     p2m_type_t p2mt;
     p2m_access_t p2ma;
     mfn_t mfn;
@@ -1841,12 +1841,13 @@ int hvm_hap_nested_page_fault(paddr_t gp
                 hvm_inject_hw_exception(X86_EXC_GP, 0);
             return 1;
         case NESTEDHVM_PAGEFAULT_L0_ERROR:
-            /* gpa is now translated to l1 guest address, update gfn. */
-            gfn = gpa >> PAGE_SHIFT;
+            /* gpa is now translated to l1 guest address. */
             break;
         }
     }
 
+    gfn = gpa >> PAGE_SHIFT;
+
     /*
      * No need to do the P2M lookup for internally handled MMIO, benefiting
      * - 32-bit WinXP (& older Windows) on AMD CPUs for LAPIC accesses,
@@ -1854,7 +1855,7 @@ int hvm_hap_nested_page_fault(paddr_t gp
      */
     if ( !nestedhvm_vcpu_in_guestmode(curr) && hvm_mmio_internal(gpa) )
     {
-        if ( !handle_mmio_with_translation(gla, gpa >> PAGE_SHIFT, npfec) )
+        if ( !handle_mmio_with_translation(gla, gfn, npfec) )
             hvm_inject_hw_exception(X86_EXC_GP, 0);
         rc = 1;
         goto out;
@@ -1982,7 +1983,7 @@ int hvm_hap_nested_page_fault(paddr_t gp
          (npfec.write_access &&
           (p2m_is_discard_write(p2mt) || (p2mt == p2m_ioreq_server))) )
     {
-        if ( !handle_mmio_with_translation(gla, gpa >> PAGE_SHIFT, npfec) )
+        if ( !handle_mmio_with_translation(gla, gfn, npfec) )
             hvm_inject_hw_exception(X86_EXC_GP, 0);
         rc = 1;
         goto out_put_gfn;
Acked-by on v1 1/1
#1 lore raw
Quoted 1 line · Jan Beulich

First gfn can be set just once, rather than (conditionally) twice.

This wants a comma after First, or you're implying "the first gfn can be..." which not the meaning you're trying to convey.  You also don't really want one before "rather".

Quoted 4 lines

And then gfn can be used in two function calls, rather than re- calculating the value there.

Quoted 5 lines

---
I wasn't quite sure about continuing to use an open-coded shift. PFN_DOWN() could be used, or paddr_to_pfn(). Neither looks to be an overly good fit to translate a gaddr to a gfn, yet gaddr_to_gfn() can't be used quite nicely either as long as gfn isn't of type gfn_t.

I think it wants to stay like this here.