mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v4 0/1] KVM: trigger uevents when creating or destroying a VM
@ 2017-07-11 12:12 Claudio Imbrenda
  2017-07-11 12:12 ` [PATCH v4 1/1] " Claudio Imbrenda
  0 siblings, 1 reply; 4+ messages in thread
From: Claudio Imbrenda @ 2017-07-11 12:12 UTC (permalink / raw)
  To: kvm; +Cc: borntraeger, pbonzini, linux-kernel, david

This patch adds a few lines to the KVM common code to fire a
KOBJ_CHANGE uevent whenever a KVM VM is created or destroyed. The event
carries five environment variables:

CREATED indicates how many times a new VM has been created. It is
	useful for example to trigger specific actions when the first
	VM is started
COUNT indicates how many VMs are currently active. This can be used for
	logging or monitoring purposes
PID has the pid of the KVM process that has been started or stopped.
	This can be used to perform process-specific tuning.
STATS_PATH contains the path in debugfs to the directory with all the
	runtime statistics for this VM. This is useful for performance
	monitoring and profiling.
EVENT described the type of event, its value can be either "create" or
	"destroy"

Specific udev rules can be then set up in userspace to deal with the
creation or destruction of VMs as needed.

v3 -> v4:
* fixed subject line
* reworked kvm_uevent_notify_change to use struct kobj_uevent_env and
  add_uevent_var for improved readability.

v2 -> v3:
* added EVENT
* shortened the names of the other variables

v1 -> v2:
* added KVM_VM_PID and KVM_VM_STATS_PATH
* some cleanup, the patch should look nicer now
* rebased on 4.12

Claudio Imbrenda (1):
  KVM: trigger uevents when creating or destroying a VM

 virt/kvm/kvm_main.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 74 insertions(+)

-- 
2.7.4

^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v4 1/1] KVM: trigger uevents when creating or destroying a VM
  2017-07-11 12:12 [PATCH v4 0/1] KVM: trigger uevents when creating or destroying a VM Claudio Imbrenda
@ 2017-07-11 12:12 ` Claudio Imbrenda
  2017-07-11 21:13   ` kbuild test robot
  2017-07-12  1:03   ` kbuild test robot
  0 siblings, 2 replies; 4+ messages in thread
From: Claudio Imbrenda @ 2017-07-11 12:12 UTC (permalink / raw)
  To: kvm; +Cc: borntraeger, pbonzini, linux-kernel, david

This patch adds a few lines to the KVM common code to fire a
KOBJ_CHANGE uevent whenever a KVM VM is created or destroyed. The event
carries five environment variables:

CREATED indicates how many times a new VM has been created. It is
	useful for example to trigger specific actions when the first
	VM is started
COUNT indicates how many VMs are currently active. This can be used for
	logging or monitoring purposes
PID has the pid of the KVM process that has been started or stopped.
	This can be used to perform process-specific tuning.
STATS_PATH contains the path in debugfs to the directory with all the
	runtime statistics for this VM. This is useful for performance
	monitoring and profiling.
EVENT described the type of event, its value can be either "create" or
	"destroy"

Specific udev rules can be then set up in userspace to deal with the
creation or destruction of VMs as needed.

Signed-off-by: Claudio Imbrenda <imbrenda@linux.vnet.ibm.com>
---
 virt/kvm/kvm_main.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 74 insertions(+)

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index f0fe9d0..4a7a632 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -130,6 +130,12 @@ EXPORT_SYMBOL_GPL(kvm_rebooting);
 
 static bool largepages_enabled = true;
 
+#define KVM_EVENT_CREATE_VM 0
+#define KVM_EVENT_DESTROY_VM 1
+static void kvm_uevent_notify_change(unsigned int type, struct kvm *kvm);
+static unsigned long long kvm_createvm_count;
+static unsigned long long kvm_active_vms;
+
 bool kvm_is_reserved_pfn(kvm_pfn_t pfn)
 {
 	if (pfn_valid(pfn))
@@ -728,6 +734,7 @@ static void kvm_destroy_vm(struct kvm *kvm)
 	int i;
 	struct mm_struct *mm = kvm->mm;
 
+	kvm_uevent_notify_change(KVM_EVENT_DESTROY_VM, kvm);
 	kvm_destroy_vm_debugfs(kvm);
 	kvm_arch_sync_events(kvm);
 	spin_lock(&kvm_lock);
@@ -3196,6 +3203,7 @@ static int kvm_dev_ioctl_create_vm(unsigned long type)
 		fput(file);
 		return -ENOMEM;
 	}
+	kvm_uevent_notify_change(KVM_EVENT_CREATE_VM, kvm);
 
 	fd_install(r, file);
 	return r;
@@ -3848,6 +3856,72 @@ static const struct file_operations *stat_fops[] = {
 	[KVM_STAT_VM]   = &vm_stat_fops,
 };
 
+static void kvm_uevent_notify_change(unsigned int type, struct kvm *kvm)
+{
+	struct kobj_uevent_env *env;
+	char *tmp, *pathbuf = NULL;
+	unsigned long long created, active;
+
+	if (!kvm_dev.this_device || !kvm)
+		return;
+
+	spin_lock(&kvm_lock);
+	if (type == KVM_EVENT_CREATE_VM) {
+		kvm_createvm_count++;
+		kvm_active_vms++;
+	} else if (type == KVM_EVENT_DESTROY_VM) {
+		kvm_active_vms--;
+	}
+	created = kvm_createvm_count;
+	active = kvm_active_vms;
+	spin_unlock(&kvm_lock);
+
+	env = kzalloc(sizeof(*env), GFP_KERNEL);
+	if (!env)
+		return;
+
+	add_uevent_var(env, "CREATED=%llu", created);
+	add_uevent_var(env, "COUNT=%llu", active);
+
+	if (kvm->mm && kvm->mm->owner) {
+		add_uevent_var(env, "PID=%d", kvm->mm->owner->pid);
+	} else if (kvm->debugfs_dentry) {
+		char p[ITOA_MAX_LEN];
+
+		snprintf(p, sizeof(p), "%s", kvm->debugfs_dentry->d_name.name);
+		tmp = strchrnul(p + 1, '-');
+		*tmp = '\0';
+		add_uevent_var(env, "PID=%s", p);
+	}
+
+	if (type == KVM_EVENT_CREATE_VM)
+		add_uevent_var(env, "EVENT=create");
+	else if (type == KVM_EVENT_DESTROY_VM)
+		add_uevent_var(env, "EVENT=destroy");
+
+	if (kvm->debugfs_dentry) {
+		pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
+		if (pathbuf) {
+			/* sizeof counts the final '\0' */
+			int len = sizeof("STATS_PATH=") - 1;
+			const char *pvar = "STATS_PATH=";
+
+			tmp = dentry_path_raw(kvm->debugfs_dentry,
+					      pathbuf + len,
+					      PATH_MAX - len);
+			if (!IS_ERR(tmp)) {
+				memcpy(tmp - len, pvar, len);
+				env->envp[env->envp_idx++] = tmp - len;
+			}
+		}
+	}
+	/* no need for checks, since we are adding at most only 5 keys */
+	env->envp[env->envp_idx++] = NULL;
+	kobject_uevent_env(&kvm_dev.this_device->kobj, KOBJ_CHANGE, env->envp);
+	kfree(env);
+	kfree(pathbuf);
+}
+
 static int kvm_init_debug(void)
 {
 	int r = -EEXIST;
-- 
2.7.4

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v4 1/1] KVM: trigger uevents when creating or destroying a VM
  2017-07-11 12:12 ` [PATCH v4 1/1] " Claudio Imbrenda
@ 2017-07-11 21:13   ` kbuild test robot
  2017-07-12  1:03   ` kbuild test robot
  1 sibling, 0 replies; 4+ messages in thread
From: kbuild test robot @ 2017-07-11 21:13 UTC (permalink / raw)
  To: Claudio Imbrenda
  Cc: kbuild-all, kvm, borntraeger, pbonzini, linux-kernel, david

[-- Attachment #1: Type: text/plain, Size: 3501 bytes --]

Hi Claudio,

[auto build test ERROR on kvm/linux-next]
[also build test ERROR on v4.12 next-20170711]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Claudio-Imbrenda/KVM-trigger-uevents-when-creating-or-destroying-a-VM/20170712-023748
base:   https://git.kernel.org/pub/scm/virt/kvm/kvm.git linux-next
config: x86_64-allyesdebian (attached as .config)
compiler: gcc-6 (Debian 6.2.0-3) 6.2.0 20160901
reproduce:
        # save the attached .config to linux build tree
        make ARCH=x86_64 

All errors (new ones prefixed by >>):

   arch/x86/kvm/../../../virt/kvm/kvm_main.c: In function 'kvm_uevent_notify_change':
>> arch/x86/kvm/../../../virt/kvm/kvm_main.c:3892:24: error: 'struct mm_struct' has no member named 'owner'
     if (kvm->mm && kvm->mm->owner) {
                           ^~
   arch/x86/kvm/../../../virt/kvm/kvm_main.c:3893:40: error: 'struct mm_struct' has no member named 'owner'
      add_uevent_var(env, "PID=%d", kvm->mm->owner->pid);
                                           ^~

vim +3892 arch/x86/kvm/../../../virt/kvm/kvm_main.c

  3864	
  3865	static void kvm_uevent_notify_change(unsigned int type, struct kvm *kvm)
  3866	{
  3867		struct kobj_uevent_env *env;
  3868		char *tmp, *pathbuf = NULL;
  3869		unsigned long long created, active;
  3870	
  3871		if (!kvm_dev.this_device || !kvm)
  3872			return;
  3873	
  3874		spin_lock(&kvm_lock);
  3875		if (type == KVM_EVENT_CREATE_VM) {
  3876			kvm_createvm_count++;
  3877			kvm_active_vms++;
  3878		} else if (type == KVM_EVENT_DESTROY_VM) {
  3879			kvm_active_vms--;
  3880		}
  3881		created = kvm_createvm_count;
  3882		active = kvm_active_vms;
  3883		spin_unlock(&kvm_lock);
  3884	
  3885		env = kzalloc(sizeof(*env), GFP_KERNEL);
  3886		if (!env)
  3887			return;
  3888	
  3889		add_uevent_var(env, "CREATED=%llu", created);
  3890		add_uevent_var(env, "COUNT=%llu", active);
  3891	
> 3892		if (kvm->mm && kvm->mm->owner) {
  3893			add_uevent_var(env, "PID=%d", kvm->mm->owner->pid);
  3894		} else if (kvm->debugfs_dentry) {
  3895			char p[ITOA_MAX_LEN];
  3896	
  3897			snprintf(p, sizeof(p), "%s", kvm->debugfs_dentry->d_name.name);
  3898			tmp = strchrnul(p + 1, '-');
  3899			*tmp = '\0';
  3900			add_uevent_var(env, "PID=%s", p);
  3901		}
  3902	
  3903		if (type == KVM_EVENT_CREATE_VM)
  3904			add_uevent_var(env, "EVENT=create");
  3905		else if (type == KVM_EVENT_DESTROY_VM)
  3906			add_uevent_var(env, "EVENT=destroy");
  3907	
  3908		if (kvm->debugfs_dentry) {
  3909			pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
  3910			if (pathbuf) {
  3911				/* sizeof counts the final '\0' */
  3912				int len = sizeof("STATS_PATH=") - 1;
  3913				const char *pvar = "STATS_PATH=";
  3914	
  3915				tmp = dentry_path_raw(kvm->debugfs_dentry,
  3916						      pathbuf + len,
  3917						      PATH_MAX - len);
  3918				if (!IS_ERR(tmp)) {
  3919					memcpy(tmp - len, pvar, len);
  3920					env->envp[env->envp_idx++] = tmp - len;
  3921				}
  3922			}
  3923		}
  3924		/* no need for checks, since we are adding at most only 5 keys */
  3925		env->envp[env->envp_idx++] = NULL;
  3926		kobject_uevent_env(&kvm_dev.this_device->kobj, KOBJ_CHANGE, env->envp);
  3927		kfree(env);
  3928		kfree(pathbuf);
  3929	}
  3930	

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation

[-- Attachment #2: .config.gz --]
[-- Type: application/gzip, Size: 39119 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH v4 1/1] KVM: trigger uevents when creating or destroying a VM
  2017-07-11 12:12 ` [PATCH v4 1/1] " Claudio Imbrenda
  2017-07-11 21:13   ` kbuild test robot
@ 2017-07-12  1:03   ` kbuild test robot
  1 sibling, 0 replies; 4+ messages in thread
From: kbuild test robot @ 2017-07-12  1:03 UTC (permalink / raw)
  To: Claudio Imbrenda
  Cc: kbuild-all, kvm, borntraeger, pbonzini, linux-kernel, david

[-- Attachment #1: Type: text/plain, Size: 3681 bytes --]

Hi Claudio,

[auto build test ERROR on kvm/linux-next]
[also build test ERROR on v4.12 next-20170711]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]

url:    https://github.com/0day-ci/linux/commits/Claudio-Imbrenda/KVM-trigger-uevents-when-creating-or-destroying-a-VM/20170712-023748
base:   https://git.kernel.org/pub/scm/virt/kvm/kvm.git linux-next
config: powerpc-defconfig (attached as .config)
compiler: powerpc64-linux-gnu-gcc (Debian 6.3.0-18) 6.3.0 20170516
reproduce:
        wget https://raw.githubusercontent.com/01org/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross
        chmod +x ~/bin/make.cross
        # save the attached .config to linux build tree
        make.cross ARCH=powerpc 

All errors (new ones prefixed by >>):

   arch/powerpc/kvm/../../../virt/kvm/kvm_main.c: In function 'kvm_uevent_notify_change':
>> arch/powerpc/kvm/../../../virt/kvm/kvm_main.c:3892:24: error: 'struct mm_struct' has no member named 'owner'
     if (kvm->mm && kvm->mm->owner) {
                           ^~
   arch/powerpc/kvm/../../../virt/kvm/kvm_main.c:3893:40: error: 'struct mm_struct' has no member named 'owner'
      add_uevent_var(env, "PID=%d", kvm->mm->owner->pid);
                                           ^~

vim +3892 arch/powerpc/kvm/../../../virt/kvm/kvm_main.c

  3864	
  3865	static void kvm_uevent_notify_change(unsigned int type, struct kvm *kvm)
  3866	{
  3867		struct kobj_uevent_env *env;
  3868		char *tmp, *pathbuf = NULL;
  3869		unsigned long long created, active;
  3870	
  3871		if (!kvm_dev.this_device || !kvm)
  3872			return;
  3873	
  3874		spin_lock(&kvm_lock);
  3875		if (type == KVM_EVENT_CREATE_VM) {
  3876			kvm_createvm_count++;
  3877			kvm_active_vms++;
  3878		} else if (type == KVM_EVENT_DESTROY_VM) {
  3879			kvm_active_vms--;
  3880		}
  3881		created = kvm_createvm_count;
  3882		active = kvm_active_vms;
  3883		spin_unlock(&kvm_lock);
  3884	
  3885		env = kzalloc(sizeof(*env), GFP_KERNEL);
  3886		if (!env)
  3887			return;
  3888	
  3889		add_uevent_var(env, "CREATED=%llu", created);
  3890		add_uevent_var(env, "COUNT=%llu", active);
  3891	
> 3892		if (kvm->mm && kvm->mm->owner) {
  3893			add_uevent_var(env, "PID=%d", kvm->mm->owner->pid);
  3894		} else if (kvm->debugfs_dentry) {
  3895			char p[ITOA_MAX_LEN];
  3896	
  3897			snprintf(p, sizeof(p), "%s", kvm->debugfs_dentry->d_name.name);
  3898			tmp = strchrnul(p + 1, '-');
  3899			*tmp = '\0';
  3900			add_uevent_var(env, "PID=%s", p);
  3901		}
  3902	
  3903		if (type == KVM_EVENT_CREATE_VM)
  3904			add_uevent_var(env, "EVENT=create");
  3905		else if (type == KVM_EVENT_DESTROY_VM)
  3906			add_uevent_var(env, "EVENT=destroy");
  3907	
  3908		if (kvm->debugfs_dentry) {
  3909			pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
  3910			if (pathbuf) {
  3911				/* sizeof counts the final '\0' */
  3912				int len = sizeof("STATS_PATH=") - 1;
  3913				const char *pvar = "STATS_PATH=";
  3914	
  3915				tmp = dentry_path_raw(kvm->debugfs_dentry,
  3916						      pathbuf + len,
  3917						      PATH_MAX - len);
  3918				if (!IS_ERR(tmp)) {
  3919					memcpy(tmp - len, pvar, len);
  3920					env->envp[env->envp_idx++] = tmp - len;
  3921				}
  3922			}
  3923		}
  3924		/* no need for checks, since we are adding at most only 5 keys */
  3925		env->envp[env->envp_idx++] = NULL;
  3926		kobject_uevent_env(&kvm_dev.this_device->kobj, KOBJ_CHANGE, env->envp);
  3927		kfree(env);
  3928		kfree(pathbuf);
  3929	}
  3930	

---
0-DAY kernel test infrastructure                Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all                   Intel Corporation

[-- Attachment #2: .config.gz --]
[-- Type: application/gzip, Size: 23467 bytes --]

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2017-07-12  1:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-07-11 12:12 [PATCH v4 0/1] KVM: trigger uevents when creating or destroying a VM Claudio Imbrenda
2017-07-11 12:12 ` [PATCH v4 1/1] " Claudio Imbrenda
2017-07-11 21:13   ` kbuild test robot
2017-07-12  1:03   ` kbuild test robot

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®