Is it OK to store `kvm` on the task_struct? What if the thread that
originally created the VM exits? From the documentation it seems
like a VM is associated with an address space and not a specific
thread, so maybe it should be stored on mm_struct?
From Documentation/virtual/kvm/api.txt:
Only run VM ioctls from the same process (address space) that was used
to create the VM.
-Nikita
Is it OK to store `kvm` on the task_struct? What if the thread that
originally created the VM exits? From the documentation it seems
like a VM is associated with an address space and not a specific
thread, so maybe it should be stored on mm_struct?
Yes, ioctls accessing the kvm can happen from all threads.
From Documentation/virtual/kvm/api.txt:
Only run VM ioctls from the same process (address space) that was used
to create the VM.
-Nikita
Is it OK to store `kvm` on the task_struct? What if the thread that
originally created the VM exits? From the documentation it seems
like a VM is associated with an address space and not a specific
thread, so maybe it should be stored on mm_struct?
Yes, ioctls accessing the kvm can happen from all threads.
Good point, thank you for the tips! I'll move kvm pointer to mm_struct.
quoted
From Documentation/virtual/kvm/api.txt:
Only run VM ioctls from the same process (address space) that was used
to create the VM.
-Nikita
Is it OK to store `kvm` on the task_struct? What if the thread that
originally created the VM exits? From the documentation it seems
like a VM is associated with an address space and not a specific
thread, so maybe it should be stored on mm_struct?
Yes, ioctls accessing the kvm can happen from all threads.
Good point, thank you for the tips! I'll move kvm pointer to mm_struct.
quoted
quoted
From Documentation/virtual/kvm/api.txt:
Only run VM ioctls from the same process (address space) that was used
to create the VM.
-Nikita
@@ -27,6 +27,7 @@ typedef int vm_fault_t;structaddress_space;structmem_cgroup;structhmm;+structkvm;/**Eachphysicalpageinthesystemhasastructpageassociatedwith
@@ -489,10 +490,19 @@ struct mm_struct {/* HMM needs to track a few things per mm */structhmm*hmm;#endif+#if IS_ENABLED(CONFIG_KVM)+structkvm*kvm;+#endif}__randomize_layout;externstructmm_structinit_mm;+#if IS_ENABLED(CONFIG_KVM)+staticinlinestructkvm*mm_kvm(structmm_struct*mm){returnmm->kvm;}+#else+staticinlinestructkvm*mm_kvm(structmm_struct*mm){returnNULL;}+#endif+staticinlinevoidmm_init_cpumask(structmm_struct*mm){#ifdef CONFIG_CPUMASK_OFFSTACK
struct address_space;
struct mem_cgroup;
struct hmm;
+struct kvm;
/*
* Each physical page in the system has a struct page associated with
@@ -489,10 +490,19 @@ struct mm_struct {
A A A A /* HMM needs to track a few things per mm */
A A A A struct hmm *hmm;
#endif
+#if IS_ENABLED(CONFIG_KVM)
+A A A struct kvm *kvm;
+#endif
} __randomize_layout;
extern struct mm_struct init_mm;
+#if IS_ENABLED(CONFIG_KVM)
+static inline struct kvm *mm_kvm(struct mm_struct *mm) { return mm->kvm; }
+#else
+static inline struct kvm *mm_kvm(struct mm_struct *mm) { return NULL; }
+#endif
+
static inline void mm_init_cpumask(struct mm_struct *mm)
{
#ifdef CONFIG_CPUMASK_OFFSTACK
A A A A if (type == KVM_EVENT_CREATE_VM) {
A A A A A A A add_uevent_var(env, "EVENT=create");
A A A A A A A kvm->userspace_pid = task_pid_nr(current);
-A A A A A A A current->kvm = kvm;
+A A A A A A A current->mm->kvm = kvm;
I think you also need to reset kvm to NULL once the VM is
destroyed, otherwise it would point to dangling memory.
A A A A A A A A A A A A A A kvm->userspace_pid = task_pid_nr(current);
A A A A A A A A A A A A A A current->mm->kvm = kvm;
A A A A A A } else if (type == KVM_EVENT_DESTROY_VM) {
+A A A A A A A A A A A A A A current->mm->kvm = NULL;
A A A A A A A A A A A A A A add_uevent_var(env, "EVENT=destroy");
A A A A A A }
A A A A A A add_uevent_var(env, "PID=%d", kvm->userspace_pid);
I think you should put both code snippets somewhere else. This has probably nothing to do
with the uevent. Instead this should go into kvm_destroy_vm and kvm_create_vm. Make sure
to take care of the error handling.
Can you point us to the original discussion about the why and what you are
trying to achieve?
struct address_space;
struct mem_cgroup;
struct hmm;
+struct kvm;
/*
* Each physical page in the system has a struct page associated with
@@ -489,10 +490,19 @@ struct mm_struct {
A A A A /* HMM needs to track a few things per mm */
A A A A struct hmm *hmm;
#endif
+#if IS_ENABLED(CONFIG_KVM)
+A A A struct kvm *kvm;
+#endif
} __randomize_layout;
extern struct mm_struct init_mm;
+#if IS_ENABLED(CONFIG_KVM)
+static inline struct kvm *mm_kvm(struct mm_struct *mm) { return mm->kvm; }
+#else
+static inline struct kvm *mm_kvm(struct mm_struct *mm) { return NULL; }
+#endif
+
static inline void mm_init_cpumask(struct mm_struct *mm)
{
#ifdef CONFIG_CPUMASK_OFFSTACK
A A A A if (type == KVM_EVENT_CREATE_VM) {
A A A A A A A add_uevent_var(env, "EVENT=create");
A A A A A A A kvm->userspace_pid = task_pid_nr(current);
-A A A A A A A current->kvm = kvm;
+A A A A A A A current->mm->kvm = kvm;
I think you also need to reset kvm to NULL once the VM is
destroyed, otherwise it would point to dangling memory.
A A A A A A A A A A A A A A kvm->userspace_pid = task_pid_nr(current);
A A A A A A A A A A A A A A current->mm->kvm = kvm;
A A A A A A } else if (type == KVM_EVENT_DESTROY_VM) {
+A A A A A A A A A A A A A A current->mm->kvm = NULL;
A A A A A A A A A A A A A A add_uevent_var(env, "EVENT=destroy");
A A A A A A }
A A A A A A add_uevent_var(env, "PID=%d", kvm->userspace_pid);
I think you should put both code snippets somewhere else. This has probably nothing to do
with the uevent. Instead this should go into kvm_destroy_vm and kvm_create_vm. Make sure
to take care of the error handling.
OK. Will set the pointer late and reset it early like this. Since
there are several error conditions after kvm_create_vm(), it may be
more convenient to set it in kvm_dev_ioctl_create_vm(), when there are
no more errors to handle:
@@ -3206,6 +3207,7 @@ static int kvm_dev_ioctl_create_vm(unsigned long type)fput(file);return-ENOMEM;}+current->mm->kvm=kvm;kvm_uevent_notify_change(KVM_EVENT_CREATE_VM,kvm);fd_install(r,file);
Can you point us to the original discussion about the why and what you are
trying to achieve?
It's the initial RFC post. [PATCH 0] describes some background info.
Basically we're implementing /proc/PID/idle_bitmap for user space to
walk page tables and get "accessed" bits. Since VM's "accessed" bits
will be reflected in EPT (or AMD NPT), we'll need to walk EPT when
detected it is QEMU main process.
Thanks,
Fengguang