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

Jan Beulich posted 1 patch 10 months ago
Failed in applying to current master (apply log)
[PATCH] x86/HVM: improve local variable use in hvm_hap_nested_page_fault()
Posted by Jan Beulich 10 months ago
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.

Signed-off-by: Jan Beulich <jbeulich@suse.com>
---
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.

--- a/xen/arch/x86/hvm/hvm.c
+++ b/xen/arch/x86/hvm/hvm.c
@@ -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;
Re: [PATCH] x86/HVM: improve local variable use in hvm_hap_nested_page_fault()
Posted by Andrew Cooper 10 months ago
On 09/04/2025 2:17 pm, Jan Beulich wrote:
> 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".

> And then gfn can be used in two function calls, rather than re-
> calculating the value there.
>
> Signed-off-by: Jan Beulich <jbeulich@suse.com>

Acked-by: Andrew Cooper <andrew.cooper3@citrix.com>

> ---
> 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.