KVM: Introduce file_to_kvm_<arch>() infrastructure

Add a macro mechanism that generates an opt-in and arch-namespaced
file_to_kvm_<arch>() to convert a file reference to a kvm object if the
file handle represents a kvm handle.

Activate for x86 and for s390 (native KVM only).

Suggested-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Steffen Eiden <seiden@linux.ibm.com>
Reviewed-by: Claudio Imbrenda <imbrenda@linux.ibm.com>
Acked-by: Janosch Frank <frankja@linux.ibm.com>
Acked-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Christian Borntraeger <borntraeger@linux.ibm.com>
This commit is contained in:
Steffen Eiden 2026-09-28 15:07:02 +02:00 committed by Christian Borntraeger
parent df2908090c
commit deaf98ff52
5 changed files with 28 additions and 0 deletions

View File

@ -27,6 +27,8 @@
#include <asm/isc.h>
#include <asm/guarded_storage.h>
#define kvm_file_to_kvm_arch s390
#define KVM_HAVE_MMU_RWLOCK
#define KVM_MAX_VCPUS 255

View File

@ -44,6 +44,8 @@
#include <hyperv/hvhdk.h>
#define kvm_file_to_kvm_arch x86
#define __KVM_HAVE_ARCH_VCPU_DEBUGFS
/*

View File

@ -61,6 +61,7 @@ exports_grep_trailer := --include='*.[ch]' -nrw $(srctree)/virt/kvm $(srctree)/a
-e kvm_page_track_unregister_notifier \
-e kvm_write_track_add_gfn \
-e kvm_write_track_remove_gfn \
-e kvm_file_to_kvm_fn \
-e kvm_get_kvm \
-e kvm_get_kvm_safe \
-e kvm_put_kvm

View File

@ -1082,6 +1082,18 @@ void kvm_get_kvm(struct kvm *kvm);
bool kvm_get_kvm_safe(struct kvm *kvm);
void kvm_put_kvm(struct kvm *kvm);
bool file_is_kvm(struct file *file);
/*
* Architectures define kvm_file_to_kvm_arch to <arch>
* to get a typed, arch-namespaced helper:
*
* struct kvm *file_to_kvm_<arch>(struct file *file)
*/
#ifdef kvm_file_to_kvm_arch
#define kvm_file_to_kvm_fn CONCATENATE(file_to_kvm_, kvm_file_to_kvm_arch)
struct kvm *kvm_file_to_kvm_fn(struct file *file);
#endif /* kvm_file_to_kvm_arch */
void kvm_put_kvm_no_destroy(struct kvm *kvm);
static inline struct kvm_memslots *__kvm_memslots(struct kvm *kvm, int as_id)

View File

@ -5496,6 +5496,17 @@ bool file_is_kvm(struct file *file)
}
EXPORT_SYMBOL_FOR_KVM_INTERNAL(file_is_kvm);
#ifdef kvm_file_to_kvm_arch
struct kvm *kvm_file_to_kvm_fn(struct file *file)
{
if (!file || file->f_op != &kvm_vm_fops)
return NULL;
return file->private_data;
}
EXPORT_SYMBOL_GPL(kvm_file_to_kvm_fn);
#endif
static int kvm_dev_ioctl_create_vm(unsigned long type)
{
char fdname[ITOA_MAX_LEN + 1];