Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1584546

Re: [PATCH 4/4] kvm: convert kvm.users_count from atomic_t to refcount_t

From Paolo Bonzini <pbonzini@redhat.com>
Newsgroups linux.kernel
Subject Re: [PATCH 4/4] kvm: convert kvm.users_count from atomic_t to refcount_t
Date 2017-02-20 12:40 +0100
Message-ID <tcUtY-453-15@gated-at.bofh.it> (permalink)
References <tcU0V-3Tt-1@gated-at.bofh.it> <tcU0W-3Tt-13@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


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 <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>

Looks good, will apply after the merge window.

Paolo

> ---
>  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 <linux/context_tracking.h>
>  #include <linux/irqbypass.h>
>  #include <linux/swait.h>
> +#include <linux/refcount.h>
>  #include <asm/signal.h>
>  
>  #include <linux/kvm.h>
> @@ -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)) {
> 

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH 0/4] arch subsystem refcounter conversions Elena Reshetova <elena.reshetova@intel.com> - 2017-02-20 12:10 +0100
  [PATCH 3/4] sparc: convert mdesc_handle.refcnt from atomic_t to refcount_t Elena Reshetova <elena.reshetova@intel.com> - 2017-02-20 12:10 +0100
    Re: [PATCH 3/4] sparc: convert mdesc_handle.refcnt from atomic_t  to refcount_t David Miller <davem@davemloft.net> - 2017-02-20 16:00 +0100
  [PATCH 1/4] s390: convert debug_info.ref_count from atomic_t to refcount_t Elena Reshetova <elena.reshetova@intel.com> - 2017-02-20 12:10 +0100
    Re: [PATCH 1/4] s390: convert debug_info.ref_count from atomic_t to  refcount_t Heiko Carstens <heiko.carstens@de.ibm.com> - 2017-02-20 14:30 +0100
      RE: [PATCH 1/4] s390: convert debug_info.ref_count from atomic_t to  refcount_t "Reshetova, Elena" <elena.reshetova@intel.com> - 2017-02-20 14:40 +0100
      Re: [PATCH 1/4] s390: convert debug_info.ref_count from atomic_t to  refcount_t Heiko Carstens <heiko.carstens@de.ibm.com> - 2017-02-20 14:40 +0100
      Re: [PATCH 1/4] s390: convert debug_info.ref_count from atomic_t to  refcount_t Peter Zijlstra <peterz@infradead.org> - 2017-02-20 14:40 +0100
  [PATCH 4/4] kvm: convert kvm.users_count from atomic_t to refcount_t Elena Reshetova <elena.reshetova@intel.com> - 2017-02-20 12:10 +0100
    Re: [PATCH 4/4] kvm: convert kvm.users_count from atomic_t to  refcount_t Paolo Bonzini <pbonzini@redhat.com> - 2017-02-20 12:40 +0100
      RE: [PATCH 4/4] kvm: convert kvm.users_count from atomic_t to  refcount_t "Reshetova, Elena" <elena.reshetova@intel.com> - 2017-02-20 13:30 +0100
  [PATCH 2/4] x86: convert threshold_bank.cpus from atomic_t to refcount_t Elena Reshetova <elena.reshetova@intel.com> - 2017-02-20 12:10 +0100
    Re: [PATCH 2/4] x86: convert threshold_bank.cpus from atomic_t to  refcount_t Borislav Petkov <bp@alien8.de> - 2017-02-20 12:30 +0100
      RE: [PATCH 2/4] x86: convert threshold_bank.cpus from atomic_t to  refcount_t "Reshetova, Elena" <elena.reshetova@intel.com> - 2017-02-20 13:30 +0100
        Re: [PATCH 2/4] x86: convert threshold_bank.cpus from atomic_t to  refcount_t Borislav Petkov <bp@alien8.de> - 2017-02-20 13:40 +0100
      Re: [PATCH 2/4] x86: convert threshold_bank.cpus from atomic_t to refcount_t Kees Cook <keescook@chromium.org> - 2017-02-21 21:50 +0100
        Re: [PATCH 2/4] x86: convert threshold_bank.cpus from atomic_t to  refcount_t Borislav Petkov <bp@alien8.de> - 2017-02-22 10:40 +0100

csiph-web