]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
KVM: SEV: Use PFN_DOWN() to simplify "number of pages" math when pinning memory
authorSean Christopherson <seanjc@google.com>
Fri, 13 Mar 2026 00:33:01 +0000 (17:33 -0700)
committerSean Christopherson <seanjc@google.com>
Fri, 3 Apr 2026 16:37:25 +0000 (09:37 -0700)
Use PFN_DOWN() instead of open coded equivalents in sev_pin_memory() to
simplify the code and make it easier to read.

No functional change intended (verified before and after versions of the
generated code are identical).

Reviewed-by: Liam Merwick <liam.merwick@oracle.com>
Tested-by: Liam Merwick <liam.merwick@oracle.com>
Link: https://patch.msgid.link/20260313003302.3136111-5-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
arch/x86/kvm/svm/sev.c

index f37e23496b64d64ab11d18eefb14a701b54c06f6..15ac2b907260d6e353bf69cc7489c7f0065376c6 100644 (file)
@@ -682,7 +682,6 @@ static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
        int npinned;
        unsigned long total_npages, lock_limit;
        struct page **pages;
-       unsigned long first, last;
        int ret;
 
        lockdep_assert_held(&kvm->lock);
@@ -692,12 +691,10 @@ static struct page **sev_pin_memory(struct kvm *kvm, unsigned long uaddr,
 
        /*
         * Calculate the number of pages that need to be pinned to cover the
-        * entire range.  Note!  This isn't simply ulen >> PAGE_SHIFT, as KVM
+        * entire range.  Note!  This isn't simply PFN_DOWN(ulen), as KVM
         * doesn't require the incoming address+size to be page aligned!
         */
-       first = (uaddr & PAGE_MASK) >> PAGE_SHIFT;
-       last = ((uaddr + ulen - 1) & PAGE_MASK) >> PAGE_SHIFT;
-       npages = (last - first + 1);
+       npages = PFN_DOWN(uaddr + ulen - 1) - PFN_DOWN(uaddr) + 1;
        if (npages > INT_MAX)
                return ERR_PTR(-EINVAL);