From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753168AbdBTMWs (ORCPT ); Mon, 20 Feb 2017 07:22:48 -0500 Received: from mga04.intel.com ([192.55.52.120]:42975 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752346AbdBTMWq (ORCPT ); Mon, 20 Feb 2017 07:22:46 -0500 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.35,186,1484035200"; d="scan'208";a="826345968" From: "Reshetova, Elena" To: Paolo Bonzini , "linux-kernel@vger.kernel.org" CC: "linux-edac@vger.kernel.org" , "x86@kernel.org" , "sparclinux@vger.kernel.org" , "linux-s390@vger.kernel.org" , "kvm@vger.kernel.org" , "peterz@infradead.org" , "gregkh@linuxfoundation.org" , "davem@davemloft.net" , "tglx@linutronix.de" , "mingo@redhat.com" , "Luck, Tony" , "hpa@zytor.com" , Hans Liljestrand , Kees Cook , David Windsor Subject: RE: [PATCH 4/4] kvm: convert kvm.users_count from atomic_t to refcount_t Thread-Topic: [PATCH 4/4] kvm: convert kvm.users_count from atomic_t to refcount_t Thread-Index: AQHSi2mGnv7lMmoNLky88uDgydOpzKFxxIqAgAAMkOA= Date: Mon, 20 Feb 2017 12:22:35 +0000 Message-ID: <2236FBA76BA1254E88B949DDB74E612B41C4D934@IRSMSX102.ger.corp.intel.com> References: <1487588781-15123-1-git-send-email-elena.reshetova@intel.com> <1487588781-15123-5-git-send-email-elena.reshetova@intel.com> <220f229f-0e3e-2a72-62c1-1677021c3987@redhat.com> In-Reply-To: <220f229f-0e3e-2a72-62c1-1677021c3987@redhat.com> Accept-Language: en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: x-originating-ip: [163.33.239.180] Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: 8bit X-MIME-Autoconverted: from base64 to 8bit by mail.home.local id v1KCMqkk024419 > On 20/02/2017 12:06, Elena Reshetova wrote: > > 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 > > Signed-off-by: Hans Liljestrand > > Signed-off-by: Kees Cook > > Signed-off-by: David Windsor > > Looks good, will apply after the merge window. > > Paolo Thank you very much! Best Regards, Elena. > > > --- > > include/linux/kvm_host.h | 3 ++- > > virt/kvm/kvm_main.c | 8 ++++---- > > 2 files changed, 6 insertions(+), 5 deletions(-) > > > > diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h > > index cda457b..7fa05a5 100644 > > --- a/include/linux/kvm_host.h > > +++ b/include/linux/kvm_host.h > > @@ -26,6 +26,7 @@ > > #include > > #include > > #include > > +#include > > #include > > > > #include > > @@ -402,7 +403,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; > > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c > > index dcd1c12..6ae5775 100644 > > --- a/virt/kvm/kvm_main.c > > +++ b/virt/kvm/kvm_main.c > > @@ -622,7 +622,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); > > @@ -745,13 +745,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); > > @@ -3640,7 +3640,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)) { > >