From patchwork Tue Jul 4 09:03:38 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Claudio Imbrenda X-Patchwork-Id: 9824367 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id E6412602F0 for ; Tue, 4 Jul 2017 09:03:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D38BF27F3E for ; Tue, 4 Jul 2017 09:03:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C4D862844C; Tue, 4 Jul 2017 09:03:53 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9AB8426E4A for ; Tue, 4 Jul 2017 09:03:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752298AbdGDJDp (ORCPT ); Tue, 4 Jul 2017 05:03:45 -0400 Received: from mx0a-001b2d01.pphosted.com ([148.163.156.1]:37607 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752289AbdGDJDo (ORCPT ); Tue, 4 Jul 2017 05:03:44 -0400 Received: from pps.filterd (m0098404.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.20/8.16.0.20) with SMTP id v6493YYH125571 for ; Tue, 4 Jul 2017 05:03:44 -0400 Received: from e06smtp11.uk.ibm.com (e06smtp11.uk.ibm.com [195.75.94.107]) by mx0a-001b2d01.pphosted.com with ESMTP id 2bg4vtbpyn-1 (version=TLSv1.2 cipher=AES256-SHA bits=256 verify=NOT) for ; Tue, 04 Jul 2017 05:03:43 -0400 Received: from localhost by e06smtp11.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 4 Jul 2017 10:03:41 +0100 Received: from b06cxnps3075.portsmouth.uk.ibm.com (9.149.109.195) by e06smtp11.uk.ibm.com (192.168.101.141) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Tue, 4 Jul 2017 10:03:39 +0100 Received: from d06av23.portsmouth.uk.ibm.com (d06av23.portsmouth.uk.ibm.com [9.149.105.59]) by b06cxnps3075.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id v6493c0U18612542; Tue, 4 Jul 2017 09:03:38 GMT Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 53083A4055; Tue, 4 Jul 2017 10:01:06 +0100 (BST) Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 2435AA4051; Tue, 4 Jul 2017 10:01:06 +0100 (BST) Received: from p-imbrenda.boeblingen.de.ibm.com (unknown [9.152.224.193]) by d06av23.portsmouth.uk.ibm.com (Postfix) with ESMTPS; Tue, 4 Jul 2017 10:01:06 +0100 (BST) From: Claudio Imbrenda To: kvm@vger.kernel.org Cc: borntraeger@de.ibm.com, pbonzini@redhat.com, linux-kernel@vger.kernel.org Subject: [PATCH v1 1/1] KVM: trigger uevents when starting or stopping a VM Date: Tue, 4 Jul 2017 11:03:38 +0200 X-Mailer: git-send-email 2.7.4 X-TM-AS-GCONF: 00 x-cbid: 17070409-0040-0000-0000-000003DAD2F3 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 17070409-0041-0000-0000-000020752FA8 Message-Id: <1499159018-19782-1-git-send-email-imbrenda@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2017-07-04_04:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 spamscore=0 suspectscore=3 malwarescore=0 phishscore=0 adultscore=0 bulkscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1703280000 definitions=main-1707040157 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 two environment variables: KVM_VM_CREATED which indicates how many times a new VM has been created, KVM_VM_COUNT which indicates how many VMs are currently active. 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 --- virt/kvm/kvm_main.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 6e3b12c..f67aa59 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -130,6 +130,10 @@ EXPORT_SYMBOL_GPL(kvm_rebooting); static bool largepages_enabled = true; +static void kvm_uevent_notify_change(u64 created, u64 active); +static u64 kvm_createvm_count; +static u64 kvm_active_vms; + bool kvm_is_reserved_pfn(kvm_pfn_t pfn) { if (pfn_valid(pfn)) @@ -627,6 +631,7 @@ static struct kvm *kvm_create_vm(unsigned long type) { int r, i; struct kvm *kvm = kvm_arch_alloc_vm(); + u64 activevms, createdvms; if (!kvm) return ERR_PTR(-ENOMEM); @@ -686,9 +691,12 @@ static struct kvm *kvm_create_vm(unsigned long type) spin_lock(&kvm_lock); list_add(&kvm->vm_list, &vm_list); + createdvms = ++kvm_createvm_count; + activevms = ++kvm_active_vms; spin_unlock(&kvm_lock); preempt_notifier_inc(); + kvm_uevent_notify_change(createdvms, activevms); return kvm; @@ -739,11 +747,14 @@ static void kvm_destroy_vm(struct kvm *kvm) { int i; struct mm_struct *mm = kvm->mm; + u64 activevms, createdvms; kvm_destroy_vm_debugfs(kvm); kvm_arch_sync_events(kvm); spin_lock(&kvm_lock); list_del(&kvm->vm_list); + activevms = --kvm_active_vms; + createdvms = kvm_createvm_count; spin_unlock(&kvm_lock); kvm_free_irq_routing(kvm); for (i = 0; i < KVM_NR_BUSES; i++) { @@ -767,6 +778,7 @@ static void kvm_destroy_vm(struct kvm *kvm) preempt_notifier_dec(); hardware_disable_all(); mmdrop(mm); + kvm_uevent_notify_change(createdvms, activevms); } void kvm_get_kvm(struct kvm *kvm) @@ -3864,6 +3876,19 @@ static const struct file_operations *stat_fops[] = { [KVM_STAT_VM] = &vm_stat_fops, }; +static void kvm_uevent_notify_change(u64 created, u64 active) +{ + char createvm_buf[40]; + char activevm_buf[40]; + char *ptr[3] = {createvm_buf, activevm_buf, NULL}; + + if (!kvm_dev.this_device) + return; + snprintf(createvm_buf, 40, "KVM_VM_CREATED=%llu", created); + snprintf(activevm_buf, 40, "KVM_VM_ACTIVE=%llu", active); + kobject_uevent_env(&kvm_dev.this_device->kobj, KOBJ_CHANGE, ptr); +} + static int kvm_init_debug(void) { int r = -EEXIST;