[PATCH v2 3/5] kvm: attach the exit reason of vcpu as auxstr to KVM_RUN output
Masatake YAMATO
yamato at redhat.com
Sun May 20 20:27:08 UTC 2018
In KVM, a virtual machine implementation like Qemu can access a vcpu
via ioctl. KVM_RUN is an ioctl command to enter vcpu. The command
returns the control for some reasons: Needs of device emulation or
consuming time slices are typical ones. The vmi takes a different
action for the reason.
We, strace users, want to know the reason to understand kvm. This
change prints the reason as auxstr if -K option is given, and if
strace runs on Linux 4.16.0 or higher, which includes
e46b469278a59781f9b25ff608af84892963821b, "kvm: embed vcpu id to
dentry of vcpu anon inode."
The way to get the reason is a bit complicated because the ioctl
doesn't return it to the userspace directly. Instead, the vmi and kvm
communicate via an area of the process virtual memory where the fd of
vcpu is mmap'ed. strace must peek the area to know the reason.
The change does three things: (1) recording the area for given vcpu
when the target calls VCPU_CREATE to vcpu_info_list per tcb data
field, (2) verifying the data recorded in vcpu_info_list before
doing (3), and (3) decoding the exit reason field of the area.
The change is not so simple because there is a case that strace
doesn't have a chance to do (1) if -p option is used. In the case,
vcpu_info data created in the step (2).
The area has more fields than "exit reason." Dumping them is future
work.
* defs.h [HAVE_LINUX_KVM_H]: (kvm_run_structure_decoder_init,
kvm_vcpu_info_free): New declarations.
(struct tcb): Add new field vcpu_info_list.
* strace.c (usage): Describe -K option.
(init): Introduce -K option. Call
kvm_run_structure_decoder_init to enable for attaching the exit reason
to auxstr.
(droptcb): Call kvm_vcpu_info_free.
* xlat/kvm_exit_reason.in: The list for decoding the exit reason.
* kvm.c: Include xmalloc.h and mmap_cache.h.
(dump_kvm_run_structure): New file static variable.
(kvm_run_structure_decoder_init): New function.
(vcpu_info): New struct definition representing the 3-tuple: vcpu file
descriptor, id of the vcpu, and mmap'ed entry.
(vcpu_find, vcpu_alloc, vcpu_register, vcpu_getinfo,
kvm_vcpu_info_free): New functions to access tcb's vcpu_info_list
field and vcpu_info data type.
(is_map_for_file, map_len): New helper functions.
(kvm_ioclt_run_attach_auxstr, kvm_ioctl_decode_run): New functions
decoding vcpu exit reason and attaching the decoded data to auxstr
field of tcb.
(kvm_ioctl_create_vcpu): Call vcpu_register to make an entry mapping a
file descriptor and the vcpu id associated with the fd.
(kvm_ioctl): Call kvm_ioctl_decode_run.
Changes in v2:
* Don't include unnecessary sys/mman.h.
* Remove dump_kvm_run_structure_level. Introduce dump_kvm_run_structure, a
boolean variable.
* Remove redundant local proxy vairables.
* Use xcalloc instead of xcalloc.
* Don't use space before function name and parenthesis.
* Use `for` instead of `while`.
* Don't redundant `if' statement for making return value.
* Use string_to_uint instead of using a custom function.
* Strip prefix from vcpu file name to get cpuid.
* Use STR_STRIP_PREFIX.
* Print cpuid on entering syscall.
* Use umove instead of umoven.
* Use xlookup_ex instead of xlookup.
* Use syserror as replacement of `tcp->u_rval < 0' condition.
All suggested by ldv.
Signed-off-by: Masatake YAMATO <yamato at redhat.com>
---
defs.h | 9 ++
kvm.c | 217 +++++++++++++++++++++++++++++++++++++++-
strace.c | 30 ++++++
xlat/kvm_exit_reason.in | 29 ++++++
4 files changed, 284 insertions(+), 1 deletion(-)
create mode 100644 xlat/kvm_exit_reason.in
diff --git a/defs.h b/defs.h
index b9f03312..da32f378 100644
--- a/defs.h
+++ b/defs.h
@@ -220,6 +220,10 @@ struct tcb {
struct mmap_cache_t *mmap_cache;
+#ifdef HAVE_LINUX_KVM_H
+ struct vcpu_info *vcpu_info_list;
+#endif
+
#ifdef ENABLE_STACKTRACE
void *unwind_ctx;
struct unwind_queue_t *unwind_queue;
@@ -850,6 +854,11 @@ extern void unwind_tcb_print(struct tcb *);
extern void unwind_tcb_capture(struct tcb *);
#endif
+#ifdef HAVE_LINUX_KVM_H
+extern void kvm_run_structure_decoder_init(void);
+extern void kvm_vcpu_info_free(struct tcb *);
+#endif
+
static inline int
printstrn(struct tcb *tcp, kernel_ulong_t addr, kernel_ulong_t len)
{
diff --git a/kvm.c b/kvm.c
index 86fd9e50..2811acc2 100644
--- a/kvm.c
+++ b/kvm.c
@@ -34,13 +34,171 @@
# include <linux/kvm.h>
# include "print_fields.h"
# include "arch_kvm.c"
+# include "xmalloc.h"
+# include "mmap_cache.h"
+
+struct vcpu_info {
+ int fd;
+ int cpuid;
+ long mmap_addr;
+ unsigned long mmap_len;
+ bool resolved;
+ struct vcpu_info *next;
+};
+
+static bool dump_kvm_run_structure;
+
+static struct vcpu_info *
+vcpu_find(struct tcb *const tcp, int fd)
+{
+ for (struct vcpu_info *vcpu_info = tcp->vcpu_info_list;
+ vcpu_info;
+ vcpu_info = vcpu_info->next)
+ if (vcpu_info->fd == fd)
+ return vcpu_info;
+
+ return NULL;
+}
+
+static struct vcpu_info *
+vcpu_alloc(struct tcb *const tcp, int fd, int cpuid)
+{
+ struct vcpu_info *vcpu_info = xcalloc(1, sizeof(*vcpu_info));
+
+ vcpu_info->fd = fd;
+ vcpu_info->cpuid = cpuid;
+
+ vcpu_info->next = tcp->vcpu_info_list;
+ tcp->vcpu_info_list = vcpu_info;
+
+ return vcpu_info;
+}
+
+void
+kvm_vcpu_info_free(struct tcb *tcp)
+{
+ struct vcpu_info *head, *next;
+
+ for (head = tcp->vcpu_info_list; head; head = next) {
+ next = head->next;
+ free(head);
+ }
+
+ tcp->vcpu_info_list = NULL;
+}
+
+static void
+vcpu_register(struct tcb *const tcp, int fd, int cpuid)
+{
+ struct vcpu_info *vcpu_info = vcpu_find(tcp, fd);
+
+ if (!vcpu_info)
+ vcpu_info = vcpu_alloc(tcp, fd, cpuid);
+ else if (vcpu_info->cpuid != cpuid)
+ {
+ vcpu_info->cpuid = cpuid;
+ vcpu_info->resolved = false;
+ }
+}
+
+static bool
+is_map_for_file(struct mmap_cache_entry_t *map_info, void *data)
+{
+ /* major version for anon inode may be given in get_anon_bdev()
+ * in linux kernel.
+ *
+ * *p = MKDEV(0, dev & MINORMASK);
+ *-----------------^
+ */
+ return map_info->binary_filename &&
+ map_info->major == 0 &&
+ strcmp(map_info->binary_filename, (const char *)data) == 0;
+}
+
+static unsigned long
+map_len(struct mmap_cache_entry_t *map_info)
+{
+ return map_info->start_addr < map_info->end_addr
+ ? map_info->end_addr - map_info->start_addr
+ : 0;
+}
+
+#define VCPU_DENTRY_PREFIX "anon_inode:kvm-vcpu:"
+#define VCPU_DENTRY_PREFIX_LEN (sizeof(VCPU_DENTRY_PREFIX) - 1)
+
+static struct vcpu_info*
+vcpu_get_info(struct tcb *const tcp, int fd)
+{
+ struct vcpu_info *vcpu_info = vcpu_find(tcp, fd);
+ struct mmap_cache_entry_t *map_info;
+ enum mmap_cache_rebuild_result mc_stat;
+
+ mc_stat = mmap_cache_rebuild_if_invalid(tcp, __func__);
+ if (mc_stat == MMAP_CACHE_REBUILD_NOCACHE)
+ return NULL;
+
+ if (vcpu_info && vcpu_info->resolved) {
+ if (mc_stat == MMAP_CACHE_REBUILD_READY)
+ return vcpu_info;
+ else {
+ map_info = mmap_cache_search(tcp, vcpu_info->mmap_addr);
+ if ((map_info
+ && strncmp(map_info->binary_filename,
+ VCPU_DENTRY_PREFIX, VCPU_DENTRY_PREFIX_LEN) == 0)) {
+ int cpuid = string_to_uint(STR_STRIP_PREFIX(map_info->binary_filename,
+ VCPU_DENTRY_PREFIX));
+ if (cpuid < 0)
+ return NULL;
+ else if (vcpu_info->cpuid == cpuid)
+ return vcpu_info;
+ else
+ vcpu_info->resolved = false;
+ } else
+ /* The vcpu vma may be mremap'ed. */
+ vcpu_info->resolved = false;
+ }
+ }
+
+ /* Slow path */
+ char path[PATH_MAX + 1];
+ if (getfdpath(tcp, fd, path, sizeof(path)) >= 0
+ && strncmp(path, VCPU_DENTRY_PREFIX, VCPU_DENTRY_PREFIX_LEN) == 0) {
+ map_info = mmap_cache_search_custom(tcp, is_map_for_file, path);
+ if (map_info) {
+ int cpuid = string_to_uint(STR_STRIP_PREFIX(path,
+ VCPU_DENTRY_PREFIX));
+ if (cpuid < 0)
+ return NULL;
+ else if (!vcpu_info)
+ vcpu_info = vcpu_alloc(tcp, fd, cpuid);
+ else if (vcpu_info->cpuid != cpuid)
+ vcpu_info->cpuid = cpuid;
+ vcpu_info->mmap_addr = map_info->start_addr;
+ vcpu_info->mmap_len = map_len(map_info);
+ vcpu_info->resolved = true;
+ } else
+ vcpu_info->resolved = false;
+ }
+
+ if (!vcpu_info->resolved)
+ vcpu_info = NULL;
+ return vcpu_info;
+}
static int
kvm_ioctl_create_vcpu(struct tcb *const tcp, const kernel_ulong_t arg)
{
uint32_t cpuid = arg;
- tprintf(", %u", cpuid);
+ if (entering(tcp)) {
+ tprintf(", %u", cpuid);
+ if (dump_kvm_run_structure)
+ return 0;
+ } else {
+ if (dump_kvm_run_structure && tcp->u_rval >= 0)
+ vcpu_register(tcp, tcp->u_rval, (int)cpuid);
+ }
+
return RVAL_IOCTL_DECODED | RVAL_FD;
}
@@ -103,6 +261,53 @@ kvm_ioctl_decode_sregs(struct tcb *const tcp, const unsigned int code,
}
# endif /* HAVE_STRUCT_KVM_SREGS */
+# include "xlat/kvm_exit_reason.h"
+static void
+kvm_ioctl_run_attach_auxstr(struct tcb *const tcp,
+ struct vcpu_info *info)
+
+{
+ static struct kvm_run vcpu_run_struct;
+
+ if (info->mmap_len < sizeof(vcpu_run_struct))
+ return;
+
+ if (umove(tcp, info->mmap_addr, &vcpu_run_struct) < 0)
+ return;
+
+ tcp->auxstr = xlookup_ex(kvm_exit_reason, vcpu_run_struct.exit_reason,
+ "KVM_EXIT_???");
+}
+
+static int
+kvm_ioctl_decode_run(struct tcb *const tcp)
+{
+ int fd;
+ struct vcpu_info *info;
+ int r;
+
+ if (entering(tcp))
+ return 0;
+
+ r = RVAL_DECODED;
+
+ if (syserror(tcp))
+ return r;
+ if (dump_kvm_run_structure) {
+ tcp->auxstr = NULL;
+ fd = tcp->u_arg[0];
+ info = vcpu_get_info(tcp, fd);
+
+ if (info) {
+ kvm_ioctl_run_attach_auxstr(tcp, info);
+ if (tcp->auxstr)
+ r |= RVAL_STR;
+ }
+ }
+
+ return r;
+}
+
int
kvm_ioctl(struct tcb *const tcp, const unsigned int code, const kernel_ulong_t arg)
{
@@ -129,7 +334,10 @@ kvm_ioctl(struct tcb *const tcp, const unsigned int code, const kernel_ulong_t a
case KVM_CREATE_VM:
return RVAL_DECODED | RVAL_FD;
+
case KVM_RUN:
+ return kvm_ioctl_decode_run(tcp);
+
case KVM_GET_VCPU_MMAP_SIZE:
case KVM_GET_API_VERSION:
default:
@@ -137,4 +345,11 @@ kvm_ioctl(struct tcb *const tcp, const unsigned int code, const kernel_ulong_t a
}
}
+void
+kvm_run_structure_decoder_init(void)
+{
+ dump_kvm_run_structure = true;
+ mmap_cache_enable();
+}
+
#endif /* HAVE_LINUX_KVM_H */
diff --git a/strace.c b/strace.c
index 4b374857..8adfc251 100644
--- a/strace.c
+++ b/strace.c
@@ -256,6 +256,13 @@ Output format:\n\
-k obtain stack trace between each syscall\n\
"
#endif
+"\
+"
+#ifdef HAVE_LINUX_KVM_H
+"\
+ -K show the exit reason of KVM run\n\
+"
+#endif
"\
-o file send trace output to FILE instead of stderr\n\
-q suppress messages about attaching, detaching, etc.\n\
@@ -819,6 +826,10 @@ droptcb(struct tcb *tcp)
unwind_tcb_fin(tcp);
#endif
+#ifdef HAVE_LINUX_KVM_H
+ kvm_vcpu_info_free(tcp);
+#endif
+
if (tcp->mmap_cache)
tcp->mmap_cache->free_fn(tcp, __func__);
@@ -1572,6 +1583,9 @@ init(int argc, char *argv[])
{
int c, i;
int optF = 0;
+#ifdef HAVE_LINUX_KVM_H
+ bool kvm_vcpu = false;
+#endif
if (!program_invocation_name || !*program_invocation_name) {
static char name[] = "strace";
@@ -1596,6 +1610,9 @@ init(int argc, char *argv[])
while ((c = getopt(argc, argv, "+"
#ifdef ENABLE_STACKTRACE
"k"
+#endif
+#ifdef HAVE_LINUX_KVM_H
+ "K"
#endif
"a:Ab:cCdDe:E:fFhiI:o:O:p:P:qrs:S:tTu:vVwxX:yz")) != EOF) {
switch (c) {
@@ -1659,6 +1676,14 @@ init(int argc, char *argv[])
case 'k':
stack_trace_enabled = true;
break;
+#endif
+#ifdef HAVE_LINUX_KVM_H
+ case 'K':
+ if (os_release >= KERNEL_VERSION(4, 16, 0))
+ kvm_vcpu = true;
+ else
+ error_msg("-K option needs Linux 4.16.0 or higher");
+ break;
#endif
case 'o':
outfname = optarg;
@@ -1791,6 +1816,11 @@ init(int argc, char *argv[])
unwind_init();
#endif
+#ifdef HAVE_LINUX_KVM_H
+ if (kvm_vcpu)
+ kvm_run_structure_decoder_init();
+#endif
+
/* See if they want to run as another user. */
if (username != NULL) {
struct passwd *pent;
diff --git a/xlat/kvm_exit_reason.in b/xlat/kvm_exit_reason.in
new file mode 100644
index 00000000..085790a0
--- /dev/null
+++ b/xlat/kvm_exit_reason.in
@@ -0,0 +1,29 @@
+KVM_EXIT_UNKNOWN 0
+KVM_EXIT_EXCEPTION 1
+KVM_EXIT_IO 2
+KVM_EXIT_HYPERCALL 3
+KVM_EXIT_DEBUG 4
+KVM_EXIT_HLT 5
+KVM_EXIT_MMIO 6
+KVM_EXIT_IRQ_WINDOW_OPEN 7
+KVM_EXIT_SHUTDOWN 8
+KVM_EXIT_FAIL_ENTRY 9
+KVM_EXIT_INTR 10
+KVM_EXIT_SET_TPR 11
+KVM_EXIT_TPR_ACCESS 12
+KVM_EXIT_S390_SIEIC 13
+KVM_EXIT_S390_RESET 14
+# /* deprecated */
+KVM_EXIT_DCR 15
+KVM_EXIT_NMI 16
+KVM_EXIT_INTERNAL_ERROR 17
+KVM_EXIT_OSI 18
+KVM_EXIT_PAPR_HCALL 19
+KVM_EXIT_S390_UCONTROL 20
+KVM_EXIT_WATCHDOG 21
+KVM_EXIT_S390_TSCH 22
+KVM_EXIT_EPR 23
+KVM_EXIT_SYSTEM_EVENT 24
+KVM_EXIT_S390_STSI 25
+KVM_EXIT_IOAPIC_EOI 26
+KVM_EXIT_HYPERV 27
--
2.17.0
More information about the Strace-devel
mailing list