]> git.ipfire.org Git - thirdparty/kernel/linux.git/commit
KVM: guest_memfd: Define a CLASS to get+put guest_memfd file from a memslot
authorSean Christopherson <seanjc@google.com>
Tue, 7 Oct 2025 22:23:56 +0000 (15:23 -0700)
committerSean Christopherson <seanjc@google.com>
Mon, 20 Oct 2025 13:30:46 +0000 (06:30 -0700)
commit0bb4d9c39b76b7453040ec8fb27f69f8437d6fe1
tree8facdd98716dac186a36ceb583af2151ac6cd325
parente66438bb81c4ca773b51292c27e6b5baa34f9a5e
KVM: guest_memfd: Define a CLASS to get+put guest_memfd file from a memslot

Add a CLASS to handle getting and putting a guest_memfd file given a
memslot to reduce the amount of related boilerplate, and more importantly
to minimize the chances of forgetting to put the file (thankfully the bug
that prompted this didn't escape initial testing).

Define a CLASS instead of using __free(fput) as _free() comes with subtle
caveats related to FILO ordering (objects are freed in the order in which
they are declared), and the recommended solution/workaround (declare file
pointers exactly when they are initialized) is visually jarring relative
to KVM's (and the kernel's) overall strict adherence to not mixing
declarations and code.  E.g. the use in kvm_gmem_populate() would be:

slot = gfn_to_memslot(kvm, start_gfn);
if (!kvm_slot_has_gmem(slot))
return -EINVAL;

struct file *file __free(fput) = kvm_gmem_get_file(slot;
if (!file)
return -EFAULT;

filemap_invalidate_lock(file->f_mapping);

Note, using CLASS() still declares variables in the middle of code, but
the syntactic sugar obfuscates the declaration, i.e. hides the anomaly to
a large extent.

No functional change intended.

Link: https://lore.kernel.org/r/20251007222356.348349-1-seanjc@google.com
Signed-off-by: Sean Christopherson <seanjc@google.com>
virt/kvm/guest_memfd.c