kvm: convert kvm.users_count from atomic_t to refcount_t
authorElena Reshetova <elena.reshetova@intel.com>
Mon, 20 Feb 2017 11:06:21 +0000 (13:06 +0200)
committerRadim Krčmář <rkrcmar@redhat.com>
Wed, 1 Mar 2017 16:03:21 +0000 (17:03 +0100)
refcount_t type and corresponding API should be
used instead of atomic_t when the variable is used as
a reference counter. This allows to avoid accidental
refcounter overflows that might lead to use-after-free
situations.

Signed-off-by: Elena Reshetova <elena.reshetova@intel.com>
Signed-off-by: Hans Liljestrand <ishkamiel@gmail.com>
Signed-off-by: Kees Cook <keescook@chromium.org>
Signed-off-by: David Windsor <dwindsor@gmail.com>
Signed-off-by: Radim Krčmář <rkrcmar@redhat.com>
include/linux/kvm_host.h
virt/kvm/kvm_main.c

index 8d69d51507483871f0da69e6e641c964eb451583..2c14ad9809da94bde727f3ebc744fabd47673f98 100644 (file)
@@ -26,6 +26,7 @@
 #include <linux/context_tracking.h>
 #include <linux/irqbypass.h>
 #include <linux/swait.h>
+#include <linux/refcount.h>
 #include <asm/signal.h>
 
 #include <linux/kvm.h>
@@ -401,7 +402,7 @@ struct kvm {
 #endif
        struct kvm_vm_stat stat;
        struct kvm_arch arch;
-       atomic_t users_count;
+       refcount_t users_count;
 #ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
        struct kvm_coalesced_mmio_ring *coalesced_mmio_ring;
        spinlock_t ring_lock;
index cc4d6e0dd2a2333b260947f54dfadec3f23edcf8..c6b7aff634bece6e454fdb346e449863da9e3b69 100644 (file)
@@ -617,7 +617,7 @@ static struct kvm *kvm_create_vm(unsigned long type)
        mutex_init(&kvm->lock);
        mutex_init(&kvm->irq_lock);
        mutex_init(&kvm->slots_lock);
-       atomic_set(&kvm->users_count, 1);
+       refcount_set(&kvm->users_count, 1);
        INIT_LIST_HEAD(&kvm->devices);
 
        r = kvm_arch_init_vm(kvm, type);
@@ -747,13 +747,13 @@ static void kvm_destroy_vm(struct kvm *kvm)
 
 void kvm_get_kvm(struct kvm *kvm)
 {
-       atomic_inc(&kvm->users_count);
+       refcount_inc(&kvm->users_count);
 }
 EXPORT_SYMBOL_GPL(kvm_get_kvm);
 
 void kvm_put_kvm(struct kvm *kvm)
 {
-       if (atomic_dec_and_test(&kvm->users_count))
+       if (refcount_dec_and_test(&kvm->users_count))
                kvm_destroy_vm(kvm);
 }
 EXPORT_SYMBOL_GPL(kvm_put_kvm);
@@ -3639,7 +3639,7 @@ static int kvm_debugfs_open(struct inode *inode, struct file *file,
         * To avoid the race between open and the removal of the debugfs
         * directory we test against the users count.
         */
-       if (!atomic_add_unless(&stat_data->kvm->users_count, 1, 0))
+       if (!refcount_inc_not_zero(&stat_data->kvm->users_count))
                return -ENOENT;
 
        if (simple_attr_open(inode, file, get, set, fmt)) {