Skip to content

Commit eed3013

Browse files
sean-jcbonzini
authored andcommitted
KVM: Grab a reference to KVM for VM and vCPU stats file descriptors
Grab a reference to KVM prior to installing VM and vCPU stats file descriptors to ensure the underlying VM and vCPU objects are not freed until the last reference to any and all stats fds are dropped. Note, the stats paths manually invoke fd_install() and so don't need to grab a reference before creating the file. Fixes: ce55c04 ("KVM: stats: Support binary stats retrieval for a VCPU") Fixes: fcfe1ba ("KVM: stats: Support binary stats retrieval for a VM") Reported-by: Zheng Zhang <zheng.zhang@email.ucr.edu> Closes: https://lore.kernel.org/all/CAC_GQSr3xzZaeZt85k_RCBd5kfiOve8qXo7a81Cq53LuVQ5r=Q@mail.gmail.com Cc: stable@vger.kernel.org Cc: Kees Cook <keescook@chromium.org> Signed-off-by: Sean Christopherson <seanjc@google.com> Reviewed-by: Kees Cook <keescook@chromium.org> Message-Id: <20230711230131.648752-2-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 3bcbc20 commit eed3013

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

virt/kvm/kvm_main.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4035,8 +4035,17 @@ static ssize_t kvm_vcpu_stats_read(struct file *file, char __user *user_buffer,
40354035
sizeof(vcpu->stat), user_buffer, size, offset);
40364036
}
40374037

4038+
static int kvm_vcpu_stats_release(struct inode *inode, struct file *file)
4039+
{
4040+
struct kvm_vcpu *vcpu = file->private_data;
4041+
4042+
kvm_put_kvm(vcpu->kvm);
4043+
return 0;
4044+
}
4045+
40384046
static const struct file_operations kvm_vcpu_stats_fops = {
40394047
.read = kvm_vcpu_stats_read,
4048+
.release = kvm_vcpu_stats_release,
40404049
.llseek = noop_llseek,
40414050
};
40424051

@@ -4057,6 +4066,9 @@ static int kvm_vcpu_ioctl_get_stats_fd(struct kvm_vcpu *vcpu)
40574066
put_unused_fd(fd);
40584067
return PTR_ERR(file);
40594068
}
4069+
4070+
kvm_get_kvm(vcpu->kvm);
4071+
40604072
file->f_mode |= FMODE_PREAD;
40614073
fd_install(fd, file);
40624074

@@ -4701,8 +4713,17 @@ static ssize_t kvm_vm_stats_read(struct file *file, char __user *user_buffer,
47014713
sizeof(kvm->stat), user_buffer, size, offset);
47024714
}
47034715

4716+
static int kvm_vm_stats_release(struct inode *inode, struct file *file)
4717+
{
4718+
struct kvm *kvm = file->private_data;
4719+
4720+
kvm_put_kvm(kvm);
4721+
return 0;
4722+
}
4723+
47044724
static const struct file_operations kvm_vm_stats_fops = {
47054725
.read = kvm_vm_stats_read,
4726+
.release = kvm_vm_stats_release,
47064727
.llseek = noop_llseek,
47074728
};
47084729

@@ -4721,6 +4742,9 @@ static int kvm_vm_ioctl_get_stats_fd(struct kvm *kvm)
47214742
put_unused_fd(fd);
47224743
return PTR_ERR(file);
47234744
}
4745+
4746+
kvm_get_kvm(kvm);
4747+
47244748
file->f_mode |= FMODE_PREAD;
47254749
fd_install(fd, file);
47264750

0 commit comments

Comments
 (0)