Skip to content

Commit fdeaf7e

Browse files
Claudio Imbrendabonzini
authored andcommitted
KVM: make pid available for uevents without debugfs
Simplify and improve the code so that the PID is always available in the uevent even when debugfs is not available. This adds a userspace_pid field to struct kvm, as per Radim's suggestion, so that the PID can be retrieved on destruction too. Acked-by: Janosch Frank <[email protected]> Fixes: 286de8f ("KVM: trigger uevents when creating or destroying a VM") Signed-off-by: Claudio Imbrenda <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent fa19871 commit fdeaf7e

File tree

2 files changed

+13
-23
lines changed

2 files changed

+13
-23
lines changed

include/linux/kvm_host.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,7 @@ struct kvm {
445445
struct kvm_stat_data **debugfs_stat_data;
446446
struct srcu_struct srcu;
447447
struct srcu_struct irq_srcu;
448+
pid_t userspace_pid;
448449
};
449450

450451
#define kvm_err(fmt, ...) \

virt/kvm/kvm_main.c

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3883,7 +3883,6 @@ static const struct file_operations *stat_fops[] = {
38833883
static void kvm_uevent_notify_change(unsigned int type, struct kvm *kvm)
38843884
{
38853885
struct kobj_uevent_env *env;
3886-
char *tmp, *pathbuf = NULL;
38873886
unsigned long long created, active;
38883887

38893888
if (!kvm_dev.this_device || !kvm)
@@ -3907,38 +3906,28 @@ static void kvm_uevent_notify_change(unsigned int type, struct kvm *kvm)
39073906
add_uevent_var(env, "CREATED=%llu", created);
39083907
add_uevent_var(env, "COUNT=%llu", active);
39093908

3910-
if (type == KVM_EVENT_CREATE_VM)
3909+
if (type == KVM_EVENT_CREATE_VM) {
39113910
add_uevent_var(env, "EVENT=create");
3912-
else if (type == KVM_EVENT_DESTROY_VM)
3911+
kvm->userspace_pid = task_pid_nr(current);
3912+
} else if (type == KVM_EVENT_DESTROY_VM) {
39133913
add_uevent_var(env, "EVENT=destroy");
3914+
}
3915+
add_uevent_var(env, "PID=%d", kvm->userspace_pid);
39143916

39153917
if (kvm->debugfs_dentry) {
3916-
char p[ITOA_MAX_LEN];
3917-
3918-
snprintf(p, sizeof(p), "%s", kvm->debugfs_dentry->d_name.name);
3919-
tmp = strchrnul(p + 1, '-');
3920-
*tmp = '\0';
3921-
add_uevent_var(env, "PID=%s", p);
3922-
pathbuf = kmalloc(PATH_MAX, GFP_KERNEL);
3923-
if (pathbuf) {
3924-
/* sizeof counts the final '\0' */
3925-
int len = sizeof("STATS_PATH=") - 1;
3926-
const char *pvar = "STATS_PATH=";
3927-
3928-
tmp = dentry_path_raw(kvm->debugfs_dentry,
3929-
pathbuf + len,
3930-
PATH_MAX - len);
3931-
if (!IS_ERR(tmp)) {
3932-
memcpy(tmp - len, pvar, len);
3933-
env->envp[env->envp_idx++] = tmp - len;
3934-
}
3918+
char *tmp, *p = kmalloc(PATH_MAX, GFP_KERNEL);
3919+
3920+
if (p) {
3921+
tmp = dentry_path_raw(kvm->debugfs_dentry, p, PATH_MAX);
3922+
if (!IS_ERR(tmp))
3923+
add_uevent_var(env, "STATS_PATH=%s", tmp);
3924+
kfree(p);
39353925
}
39363926
}
39373927
/* no need for checks, since we are adding at most only 5 keys */
39383928
env->envp[env->envp_idx++] = NULL;
39393929
kobject_uevent_env(&kvm_dev.this_device->kobj, KOBJ_CHANGE, env->envp);
39403930
kfree(env);
3941-
kfree(pathbuf);
39423931
}
39433932

39443933
static int kvm_init_debug(void)

0 commit comments

Comments
 (0)