Skip to contentSkip to conversation

KVM test infrastructure

PATCH In review Martin Doucha 4 participants 16 messages Mar 9, 2022 – Mar 17, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 KVM test infrastructure

Implement LTP infrastructure for tests which require executing a special program inside KVM virtual machine. The infrastructure is split into two parts:
the host library and the guest library.

The host library provides functions for setting up and running virtual machines with test payload built from test sources.

The guest library provides CPU bootstrap code and basic implementation of some C and LTP library functions as well as functions for accessing low-level arch features like special registers, interrupt tables or memory mapping tables.

The test sources will contain both guest-side payload code and host-side setup code separated by preprocessor switch. The files get compiled twice with different compiler options. Once to build the VM payload, once to build the main program that will create the VM and execute the payload inside it.

---

This version should be production ready. I'll send documentation in a separate patch after this gets merged.

 include/mk/env_post.mk                    |   1 +
 include/mk/rules.mk                       |   8 +
 testcases/kernel/Makefile                 |   1 +
 testcases/kernel/kvm/Makefile             |  56 +++
 testcases/kernel/kvm/bootstrap_x86.S      | 368 ++++++++++++++++
 testcases/kernel/kvm/bootstrap_x86_64.S   | 509 ++++++++++++++++++++++
 testcases/kernel/kvm/include/kvm_common.h |  29 ++
 testcases/kernel/kvm/include/kvm_guest.h  |  77 ++++
 testcases/kernel/kvm/include/kvm_host.h   | 115 +++++
 testcases/kernel/kvm/include/kvm_test.h   |  23 +
 testcases/kernel/kvm/include/kvm_x86.h    | 157 +++++++
 testcases/kernel/kvm/lib_guest.c          | 175 ++++++++
 testcases/kernel/kvm/lib_host.c           | 215 +++++++++
 testcases/kernel/kvm/lib_x86.c            | 150 +++++++
 testcases/kernel/kvm/linker/payload.lds   |  11 +
 testcases/kernel/kvm/linker/x86.lds       |  68 +++
 testcases/kernel/kvm/linker/x86_64.lds    |  68 +++
 17 files changed, 2031 insertions(+)
 create mode 100644 testcases/kernel/kvm/Makefile
 create mode 100644 testcases/kernel/kvm/bootstrap_x86.S
 create mode 100644 testcases/kernel/kvm/bootstrap_x86_64.S
 create mode 100644 testcases/kernel/kvm/include/kvm_common.h
 create mode 100644 testcases/kernel/kvm/include/kvm_guest.h
 create mode 100644 testcases/kernel/kvm/include/kvm_host.h
 create mode 100644 testcases/kernel/kvm/include/kvm_test.h
 create mode 100644 testcases/kernel/kvm/include/kvm_x86.h
 create mode 100644 testcases/kernel/kvm/lib_guest.c
 create mode 100644 testcases/kernel/kvm/lib_host.c
 create mode 100644 testcases/kernel/kvm/lib_x86.c
 create mode 100644 testcases/kernel/kvm/linker/payload.lds
 create mode 100644 testcases/kernel/kvm/linker/x86.lds
 create mode 100644 testcases/kernel/kvm/linker/x86_64.lds
include/mk/env_post.mk +1 -0
index ec045c40d..dc4df41d3 100644
@@ -32,6 +32,7 @@ ENV_POST_LOADED = 1
 # poor software design if you need more than one search directory, and
 # would suggest creating a general purpose static library to that end.
 vpath %.c $(abs_srcdir)
+vpath %.S $(abs_srcdir)
 
 # For config.h, et all.
 CPPFLAGS			+= -I$(top_srcdir)/include -I$(top_builddir)/include -I$(top_srcdir)/include/old/
include/mk/rules.mk +8 -0
index a60e6705a..32d8d05a7 100644
@@ -1,5 +1,13 @@
 target_rel_dir := $(if $(cwd_rel_from_top),$(cwd_rel_from_top)/,)
 
+%.o: %.S
+ifdef VERBOSE
+	$(AS) $(ASFLAGS) -c -o $@ $<
+else
+	@$(AS) $(ASFLAGS) -c -o $@ $<
+	@echo AS $(target_rel_dir)$@
+endif
+
 %.o: %.c
 ifdef VERBOSE
 	$(CC) $(CPPFLAGS) $(CFLAGS) -c -o $@ $<
testcases/kernel/Makefile +1 -0
index d44856c57..c0fa24147 100644
@@ -29,6 +29,7 @@ SUBDIRS			+= connectors \
 			   io \
 			   ipc \
 			   irq \
+			   kvm \
 			   logging \
 			   mem \
 			   numa \
testcases/kernel/kvm/Makefile new file +56 -0

56 lines not on this page · raw message

testcases/kernel/kvm/bootstrap_x86.S new file +368 -0

368 lines not on this page · raw message

testcases/kernel/kvm/bootstrap_x86_64.S new file +509 -0

509 lines not on this page · raw message

testcases/kernel/kvm/include/kvm_common.h new file +29 -0
new file mode 100644
index 000000000..eae8fcf6e
@@ -0,0 +1,29 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2021 SUSE LLC <[email protected]>
+ *
+ * Common definitions for communication between KVM guest and host.
+ */
+
+#ifndef KVM_COMMON_H_
+#define KVM_COMMON_H_
+
+#define KVM_TNONE	-1	/* "No result" status value */
+
+/*
+ * Result value indicating end of test. If the test program exits using
+ * the HLT instruction with any valid result value other than KVM_TEXIT or
+ * TBROK, KVM runner will automatically resume VM execution after printing
+ * the message.
+ */
+#define KVM_TEXIT	0xff
+
+#define KVM_RESULT_BASEADDR 0xfffff000
+#define KVM_RESULT_SIZE 0x1000
+
+struct tst_kvm_result {
+	int result;
+	char message[0];
+};
+
+#endif /* KVM_COMMON_H_ */
testcases/kernel/kvm/include/kvm_guest.h new file +77 -0

77 lines not on this page · raw message

testcases/kernel/kvm/include/kvm_host.h new file +115 -0

115 lines not on this page · raw message

testcases/kernel/kvm/include/kvm_test.h new file +23 -0
new file mode 100644
index 000000000..55989eaa5
@@ -0,0 +1,23 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2021 SUSE LLC <[email protected]>
+ *
+ * Minimal test library for KVM tests
+ */
+
+#ifndef KVM_TEST_H_
+#define KVM_TEST_H_
+
+#ifdef COMPILE_PAYLOAD
+
+  #include "kvm_guest.h"
+  #include "kvm_common.h"
+
+#else
+
+  #include "tst_test.h"
+  #include "kvm_host.h"
+
+#endif /* COMPILE_PAYLOAD */
+
+#endif /* KVM_TEST_H_ */
testcases/kernel/kvm/include/kvm_x86.h new file +157 -0

157 lines not on this page · raw message

testcases/kernel/kvm/lib_guest.c new file +175 -0

175 lines not on this page · raw message

testcases/kernel/kvm/lib_host.c new file +215 -0

215 lines not on this page · raw message

testcases/kernel/kvm/lib_x86.c new file +150 -0

150 lines not on this page · raw message

testcases/kernel/kvm/linker/payload.lds new file +11 -0
new file mode 100644
index 000000000..a544fd37a
@@ -0,0 +1,11 @@
+TARGET(binary)
+
+SECTIONS
+{
+	.data :
+	{
+		kvm_payload_start = .;
+		KEEP(*(.data))
+		kvm_payload_end = .;
+	}
+}
testcases/kernel/kvm/linker/x86.lds new file +68 -0

68 lines not on this page · raw message

testcases/kernel/kvm/linker/x86_64.lds new file +68 -0

68 lines not on this page · raw message

Signature · 5 lines
2.35.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#1 lore raw

PATCH 2/2 Add test for CVE 2021-38198

---
 runtest/kvm                            |   1 +
 testcases/kernel/kvm/.gitignore        |   1 +
 testcases/kernel/kvm/kvm_pagefault01.c | 235 +++++++++++++++++++++++++
 3 files changed, 237 insertions(+)
 create mode 100644 runtest/kvm
 create mode 100644 testcases/kernel/kvm/.gitignore
 create mode 100644 testcases/kernel/kvm/kvm_pagefault01.c
runtest/kvm new file +1 -0
new file mode 100644
index 000000000..16e7c07ff
@@ -0,0 +1 @@
+kvm_pagefault01 kvm_pagefault01
testcases/kernel/kvm/.gitignore new file +1 -0
new file mode 100644
index 000000000..349260359
@@ -0,0 +1 @@
+/kvm_pagefault01
testcases/kernel/kvm/kvm_pagefault01.c new file +235 -0

235 lines not on this page · raw message

Signature · 5 lines
2.35.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#2 lore raw

Re: [LTP] [PATCH 2/2] Add test for CVE 2021-38198

Hi Martin,

Quoted 43 lines · Martin Doucha
---
 runtest/kvm                            |   1 +
 testcases/kernel/kvm/.gitignore        |   1 +
 testcases/kernel/kvm/kvm_pagefault01.c | 235 +++++++++++++++++++++++++
 3 files changed, 237 insertions(+)
 create mode 100644 runtest/kvm
 create mode 100644 testcases/kernel/kvm/.gitignore
 create mode 100644 testcases/kernel/kvm/kvm_pagefault01.c
runtest/kvm new file +1 -0
new file mode 100644
index 000000000..16e7c07ff
@@ -0,0 +1 @@
+kvm_pagefault01 kvm_pagefault01
testcases/kernel/kvm/.gitignore new file +1 -0
new file mode 100644
index 000000000..349260359
@@ -0,0 +1 @@
+/kvm_pagefault01
testcases/kernel/kvm/kvm_pagefault01.c new file +13 -0
new file mode 100644
index 000000000..e7d957f3e
@@ -0,0 +1,235 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2020 SUSE LLC
+ * Author: Nicolai Stange <[email protected]>
+ * LTP port: Martin Doucha <[email protected]>
+ */
+
+/*\
+ * CVE 2021-38198
+ *
+ * Check that x86_64 KVM correctly enforces (lack of) write permissions
+ * in 4-level and 5-level memory page table mode. Missing page faults fixed in:
+ *

testcases/kernel/kvm/kvm_pagefault01.c line 23

Quoted 10 lines
testcases/kernel/kvm/kvm_pagefault01.c new file +10 -0
+ *  commit b1bd5cba3306691c771d558e94baa73e8b0b96b7
+ *  Author: Lai Jiangshan <[email protected]>
+ *  Date:   Thu Jun 3 13:24:55 2021 +0800
+ *
+ *  KVM: X86: MMU: Use the correct inherited permissions to get shadow page
+ */
+
+#include "kvm_test.h"
+
+#ifdef COMPILE_PAYLOAD

Minor nit; I'd prefer guest to payload.

Quoted 141 lines
testcases/kernel/kvm/kvm_pagefault01.c new file +141 -0
+#ifdef __x86_64__
+
+#include "kvm_x86.h"
+
+#define PTE_BITMASK 0x1ff
+#define PAGESIZE 0x1000
+
+int handle_page_fault(void *userdata, struct kvm_interrupt_frame *ifrm,
+	unsigned long errcode)
+{
+	struct kvm_cregs buf;
+
+	kvm_read_cregs(&buf);
+
+	/* Check that the page fault was caused by write to *readonly below */
+	if (buf.cr2 == (uintptr_t)userdata) {
+		tst_res(TPASS, "KVM enforces memory write permissions");
+		kvm_exit();
+	}
+
+	/* Unexpected page fault, fall back to default handler */
+	return 0;
+}
+
+void main(void)
+{
+	uintptr_t tmp;
+	struct page_table_entry_pae *subpte, *pte = kvm_pagetable;
+	int val, *writable, *readonly, *cacher1, *cacher2;
+
+	if (!(kvm_rdmsr(MSR_EFER) & EFER_LMA))
+		tst_brk(TBROK, "Bootstrap did not enable 64bit paging");
+
+	/*
+	 * Find the first page table entry which branches. This entry was
+	 * configured by bootstrap as follows:
+	 * 0x00000000 - 0x3fffffff in pte[0] (identity mapped)
+	 * 0x40000000 - 0x7fffffff in pte[1] (identity mapped)
+	 * 0x80000000 - 0xbfffffff in pte[2] (unmapped)
+	 * 0xc0000000 - 0xffffffff in pte[3] (only last page identity mapped)
+	 */
+	while (!pte[1].present) {
+		tmp = kvm_get_page_address_pae(pte);
+		pte = (struct page_table_entry_pae *)tmp;
+	}
+
+	/*
+	 * Setup mapping above the 32bit address space. The test needs two
+	 * different unused 1GB chunks of address space. Remapping part of
+	 * the lower 4GB address space would make it harder to reproduce
+	 * the bug because any memory access in the same 1GB chunk (even
+	 * fetching instructions by the CPU) could evict entries from page
+	 * table cache and force the bypassable write permission check
+	 * to happen even on buggy kernels.
+	 *
+	 * Allocate 3 pages for page table + 2 pages for data
+	 */
+	writable = tst_heap_alloc_aligned(5 * PAGESIZE, PAGESIZE);
+	memset(writable, 0, 5 * PAGESIZE);
+	tmp = (uintptr_t)writable;
+	pte[4].address = tmp >> 12;
+	pte[4].user_access = 1;
+	pte[4].writable = 1;
+	pte[4].present = 1;
+	pte[5] = pte[4];
+	pte[5].writable = 0;
+
+	subpte = (struct page_table_entry_pae *)tmp;
+	subpte[0].address = (tmp + PAGESIZE) >> 12;
+	subpte[0].user_access = 1;
+	subpte[0].writable = 0;
+	subpte[0].present = 1;
+	subpte[1].address = (tmp + 2 * PAGESIZE) >> 12;
+	subpte[1].user_access = 1;
+	subpte[1].writable = 1;
+	subpte[1].present = 1;
+
+	subpte = (struct page_table_entry_pae *)(tmp + PAGESIZE);
+	subpte[0].address = (tmp + 3 * PAGESIZE) >> 12;
+	subpte[0].user_access = 1;
+	subpte[0].writable = 1;
+	subpte[0].present = 1;
+
+	subpte = (struct page_table_entry_pae *)(tmp + 2 * PAGESIZE);
+	subpte[0].address = (tmp + 4 * PAGESIZE) >> 12;
+	subpte[0].user_access = 1;
+	subpte[0].writable = 1;
+	subpte[0].present = 1;
+
+	/* Create pointers into the new mapping */
+	cacher1 = (int *)0x100000000ULL;
+	writable = (int *)0x100200000ULL;
+	cacher2 = (int *)0x140000000ULL;
+	readonly = (int *)0x140200000ULL;
+	tst_set_interrupt_callback(INTR_PAGE_FAULT, handle_page_fault,
+		readonly);
+
+	/* Fill page table cache */
+	val = *cacher1;
+	*writable = val;
+	val = *cacher2;
+
+	/* Trigger page fault (unless the kernel is vulnerable) */
+	*readonly = val;
+
+	/* This line should be unreachable */
+	tst_res(TFAIL, "Write to read-only address did not page fault");
+}
+
+#else /* __x86_64__ */
+TST_TEST_TCONF("Test supported only on x86_64");
+#endif /* __x86_64__ */
+
+#else /* COMPILE_PAYLOAD */
+
+#include <ctype.h>
+#include <stdio.h>
+#include <unistd.h>
+#include "tst_module.h"
+
+#define TDP_MMU_SYSFILE "/sys/module/kvm/parameters/tdp_mmu"
+#define TDP_AMD_SYSFILE "/sys/module/kvm_amd/parameters/npt"
+#define TDP_INTEL_SYSFILE "/sys/module/kvm_intel/parameters/ept"
+
+#define BUF_SIZE 64
+
+static int read_bool_sys_param(const char *filename)
+{
+	char buf[BUF_SIZE];
+	int i, fd, ret;
+
+	fd = open(filename, O_RDONLY);
+
+	if (fd < 0)
+		return -1;
+
+	ret = read(fd, buf, BUF_SIZE - 1);
+	SAFE_CLOSE(fd);
+
+	if (ret < 1)
+		return -1;

testcases/kernel/kvm/kvm_pagefault01.c line 174

Quoted 10 lines
testcases/kernel/kvm/kvm_pagefault01.c new file +10 -0
+
+	buf[ret] = '\0';
+
+	for (i = 0; buf[i] && !isspace(buf[i]); i++)
+		;
+
+	buf[i] = '\0';
+
+	if (isdigit(buf[0])) {
+		sscanf(buf, "%d", &ret);

checkpatch complains that the return value is not checked. Also it wants you to use tst_parse_int.

Quoted 13 lines
testcases/kernel/kvm/kvm_pagefault01.c new file +13 -0
+		return ret;
+	}
+
+	if (!strcasecmp(buf, "N"))
+		return 0;
+
+	/* Assume that any other value than 0 or N means the param is enabled */
+	return 1;
+}
+
+static void reload_module(const char *module, char *arg)
+{
+	const char *const argv[] = {"modprobe", module, arg, NULL};

testcases/kernel/kvm/kvm_pagefault01.c line 197

Quoted 10 lines
testcases/kernel/kvm/kvm_pagefault01.c new file +10 -0
+
+	tst_res(TINFO, "Reloading module %s with parameter %s", module, arg);
+	tst_module_unload(module);
+	tst_cmd(argv, NULL, NULL, 0);
+}
+
+static void disable_tdp(void)
+{
+	if (!access(TDP_MMU_SYSFILE, F_OK)) {
+		/* FIXME: Is this sufficient to disable TDP? */

What happens if this doesn't work and TDP is enabled? I seem to have it enabled and the test still passes even if I comment out the call to disable_tdp.

I'm wondering whether it will be easy to tell if a test failure is due to TDP or if it can result in silent false negatives?

Quoted 14 lines
testcases/kernel/kvm/kvm_pagefault01.c new file +14 -0
+		SAFE_FILE_PRINTF(TDP_MMU_SYSFILE, "0");
+		return;
+	}
+
+	if (read_bool_sys_param(TDP_AMD_SYSFILE) > 0)
+		reload_module("kvm_amd", "npt=0");
+
+	if (read_bool_sys_param(TDP_INTEL_SYSFILE) > 0)
+		reload_module("kvm_intel", "ept=0");
+}
+
+static void setup(void)
+{
+	disable_tdp();

testcases/kernel/kvm/kvm_pagefault01.c line 221

Quoted 10 lines
testcases/kernel/kvm/kvm_pagefault01.c new file +10 -0
+	tst_kvm_setup();
+}
+
+static struct tst_test test = {
+	.test_all = tst_kvm_run,
+	.setup = setup,
+	.cleanup = tst_kvm_cleanup,
+	.needs_root = 1,
+	.save_restore = (const char *const []) {
+		"?/sys/module/kvm/parameters/tdp_mmu",

This needs updating to use struct tst_path_val.

Quoted 16 lines
testcases/kernel/kvm/kvm_pagefault01.c new file +14 -0
+		NULL
+	},
+	.supported_archs = (const char *const []) {
+		"x86_64",
+		NULL
+	},
+	.tags = (struct tst_tag[]){
+		{"linux-git", "b1bd5cba3306"},
+		{"CVE", "2021-38198"},
+		{}
+	}
+};
+
+#endif /* COMPILE_PAYLOAD */
Signature · 1 line
2.35.1
Signature · 5 lines
Thank you,
Richard.

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#3 lore raw

Re: [LTP] [PATCH 2/2] Add test for CVE 2021-38198

Quoted 8 lines · Richard Palethorpe

Hi Martin,

testcases/kernel/kvm/kvm_pagefault01.c line 174

Quoted 2 lines · Martin Doucha
testcases/kernel/kvm/kvm_pagefault01.c new file +2 -0
+	if (isdigit(buf[0])) {
+		sscanf(buf, "%d", &ret);

checkpatch complains that the return value is not checked. Also it wants you to use tst_parse_int.

if (isdigit()) above ensures that the return value does not matter. But I'll change it to tst_parse_int() in v2.

Quoted 11 lines

testcases/kernel/kvm/kvm_pagefault01.c line 197

Quoted 4 lines
testcases/kernel/kvm/kvm_pagefault01.c new file +4 -0
+static void disable_tdp(void)
+{
+	if (!access(TDP_MMU_SYSFILE, F_OK)) {
+		/* FIXME: Is this sufficient to disable TDP? */

What happens if this doesn't work and TDP is enabled? I seem to have it enabled and the test still passes even if I comment out the call to disable_tdp.

I'm wondering whether it will be easy to tell if a test failure is due to TDP or if it can result in silent false negatives?

AFAIK, the kernel bug was fixed before the tdp_mmu sysfile was introduced. So somebody will have to revert the fix and run the test on custom kernel to answer that question.

The bug is not reproducible with TDP enabled.

Quoted 4 lines

testcases/kernel/kvm/kvm_pagefault01.c line 221

Quoted 2 lines
testcases/kernel/kvm/kvm_pagefault01.c new file +2 -0
+	.save_restore = (const char *const []) {
+		"?/sys/module/kvm/parameters/tdp_mmu",

This needs updating to use struct tst_path_val.

Yes, the patch was sent before the .save_restore structure was changed.

Signature · 10 lines
Martin Doucha   [email protected]
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#4 lore raw

Re: [LTP] [PATCH 2/2] Add test for CVE 2021-38198

Hello,

Quoted 22 lines · Martin Doucha
Quoted 8 lines · Richard Palethorpe

Hi Martin,

testcases/kernel/kvm/kvm_pagefault01.c line 174

Quoted 2 lines · Martin Doucha
testcases/kernel/kvm/kvm_pagefault01.c new file +2 -0
+	if (isdigit(buf[0])) {
+		sscanf(buf, "%d", &ret);

checkpatch complains that the return value is not checked. Also it wants you to use tst_parse_int.

if (isdigit()) above ensures that the return value does not matter. But I'll change it to tst_parse_int() in v2.

Quoted 9 lines

testcases/kernel/kvm/kvm_pagefault01.c line 197

Quoted 4 lines
testcases/kernel/kvm/kvm_pagefault01.c new file +4 -0
+static void disable_tdp(void)
+{
+	if (!access(TDP_MMU_SYSFILE, F_OK)) {
+		/* FIXME: Is this sufficient to disable TDP? */

What happens if this doesn't work and TDP is enabled? I seem to have it enabled and the test still passes even if I comment out the call to disable_tdp.

Quoted 5 lines
Quoted 2 lines

I'm wondering whether it will be easy to tell if a test failure is due to TDP or if it can result in silent false negatives?

AFAIK, the kernel bug was fixed before the tdp_mmu sysfile was introduced.

Ah I see TDP is very new https://lwn.net/Articles/832835/

Quoted 2 lines

So somebody will have to revert the fix and run the test on custom kernel to answer that question.

Did you reproduce the bug?

I could try this next week.

Quoted 9 lines

The bug is not reproducible with TDP enabled.

Quoted 4 lines

testcases/kernel/kvm/kvm_pagefault01.c line 221

Quoted 2 lines
testcases/kernel/kvm/kvm_pagefault01.c new file +2 -0
+	.save_restore = (const char *const []) {
+		"?/sys/module/kvm/parameters/tdp_mmu",

This needs updating to use struct tst_path_val.

Yes, the patch was sent before the .save_restore structure was changed.

Signature · 5 lines
Thank you,
Richard.

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

Re: [LTP] [PATCH 2/2] Add test for CVE 2021-38198

Quoted 7 lines · Richard Palethorpe
Quoted 2 lines · Martin Doucha

So somebody will have to revert the fix and run the test on custom kernel to answer that question.

Did you reproduce the bug?

I could try this next week.

Yes, I've verified the reproducer on older SLE-12SP5 and SLE-15SP3 kernels. But none of them currently have the tdp_mmu sysfile so the test had to use the Intel/AMD KVM module reload fallback.

Signature · 10 lines
Martin Doucha   [email protected]
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw

Re: [LTP] [PATCH 2/2] Add test for CVE 2021-38198

Hello Martin,

Quoted 5 lines · Martin Doucha
Quoted 4 lines · Richard Palethorpe
Quoted 2 lines · Martin Doucha

So somebody will have to revert the fix and run the test on custom kernel to answer that question.

Quoted 7 lines
Quoted 3 lines

Did you reproduce the bug?

I could try this next week.

Yes, I've verified the reproducer on older SLE-12SP5 and SLE-15SP3 kernels. But none of them currently have the tdp_mmu sysfile so the test had to use the Intel/AMD KVM module reload fallback.

I see, in that case I don't think it's important to check it.

Signature · 5 lines
Thank you,
Richard.

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#7 lore raw

Re: [LTP] [PATCH 2/2] Add test for CVE 2021-38198

Quoted 6 lines · Richard Palethorpe
Quoted 3 lines · Martin Doucha

Yes, I've verified the reproducer on older SLE-12SP5 and SLE-15SP3 kernels. But none of them currently have the tdp_mmu sysfile so the test had to use the Intel/AMD KVM module reload fallback.

I see, in that case I don't think it's important to check it.

It's still useful to test the reproducer on a known broken kernel with TDP disabled via tdp_mmu sysfile instead of KVM module reload. That's the control flow that'll be used on all future kernels so we should check whether it really works.

Signature · 10 lines
Martin Doucha   [email protected]
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#8 lore raw
In reply to Martin Doucha

Hi Martin,

this patchset looks really great.

Unfortunately there are some compilation time errors [1].
Various Debian/Ubuntu versions (but not all) and Alpine have problem to compile it [1]:
/usr/bin/ld: bootstrap_x86_64.o: relocation R_X86_64_16 against symbol `kvm_gdt32_desc' can not be used when making a PIE object; recompile with -fPIE

Can you please have look into it?

Kind regards,
Petr

[1] https://github.com/pevik/ltp/actions/runs/1959152447

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#9 lore raw
Quoted 11 lines · Petr Vorel

Hi Martin,

this patchset looks really great.

Unfortunately there are some compilation time errors [1]. Various Debian/Ubuntu versions (but not all) and Alpine have problem to compile it [1]: /usr/bin/ld: bootstrap_x86_64.o: relocation R_X86_64_16 against symbol `kvm_gdt32_desc' can not be used when making a PIE object; recompile with -fPIE

Can you please have look into it?

Can you try whether this one-liner fixes the error? The diff will probably get corrupted by my mail client because GUEST_CFLAGS is too long but it's trivial to apply this by hand:

testcases/kernel/kvm/Makefile +1 -2
index 139358961..24ca91fc3 100644
@@ -9,7 +9,7 @@ ASFLAGS =
 CPPFLAGS += -I$(abs_srcdir)/include
 GUEST_CPPFLAGS = $(CPPFLAGS) -DCOMPILE_PAYLOAD
 GUEST_CFLAGS = -ffreestanding -O2 -Wall -fno-asynchronous-unwind-tables
-mno-mmx -mno-sse
-GUEST_LDFLAGS = -nostdlib -Wl,--build-id=none
+GUEST_LDFLAGS = -nostdlib -Wl,--build-id=none,-no-pie
 GUEST_LDLIBS =
 

FILTER_OUT_MAKE_TARGETS := lib_guest lib_host lib_x86

Signature · 10 lines
Martin Doucha   [email protected]
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#10 lore raw
Quoted 22 lines · Petr Vorel
Quoted 11 lines

Hi Martin,

this patchset looks really great.

Unfortunately there are some compilation time errors [1]. Various Debian/Ubuntu versions (but not all) and Alpine have problem to compile it [1]: /usr/bin/ld: bootstrap_x86_64.o: relocation R_X86_64_16 against symbol `kvm_gdt32_desc' can not be used when making a PIE object; recompile with -fPIE

Can you please have look into it?

Can you try whether this one-liner fixes the error? The diff will probably get corrupted by my mail client because GUEST_CFLAGS is too long but it's trivial to apply this by hand:

testcases/kernel/kvm/Makefile +0 -0
index 139358961..24ca91fc3 100644

testcases/kernel/kvm/Makefile ASFLAGS =

Quoted 9 lines
testcases/kernel/kvm/Makefile +1 -2
@@ -9,7 +9,7 @@ ASFLAGS =
 CPPFLAGS += -I$(abs_srcdir)/include
 GUEST_CPPFLAGS = $(CPPFLAGS) -DCOMPILE_PAYLOAD
 GUEST_CFLAGS = -ffreestanding -O2 -Wall -fno-asynchronous-unwind-tables
-mno-mmx -mno-sse
-GUEST_LDFLAGS = -nostdlib -Wl,--build-id=none
+GUEST_LDFLAGS = -nostdlib -Wl,--build-id=none,-no-pie
 GUEST_LDLIBS =
 
 FILTER_OUT_MAKE_TARGETS := lib_guest lib_host lib_x86

Unfortunately, just adding -no-pie to GUEST_LDFLAGS does not help:
https://github.com/pevik/ltp/actions/runs/1965302083

/usr/bin/ld: bootstrap_x86_64.o: relocation R_X86_64_16 against symbol `kvm_gdt32_desc' can not be used when making a PIE object; recompile with -fPIE /usr/bin/ld: -pie: error: PHDR segment not covered by LOAD segment

FYI slightly different OS failed on your original version, but that's probably not interesting:
https://github.com/pevik/ltp/actions/runs/1959152447

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#11 lore raw
In reply to Martin Doucha

Hi Martin,

Quoted 11 lines · Martin Doucha

Implement LTP infrastructure for tests which require executing a special program inside KVM virtual machine. The infrastructure is split into two parts:
the host library and the guest library.

The host library provides functions for setting up and running virtual machines with test payload built from test sources.

The guest library provides CPU bootstrap code and basic implementation of some C and LTP library functions as well as functions for accessing low-level arch features like special registers, interrupt tables or memory mapping tables.

Quoted 5 lines

The test sources will contain both guest-side payload code and host-side setup code separated by preprocessor switch. The files get compiled twice with different compiler options. Once to build the VM payload, once to build the main program that will create the VM and execute the payload inside it.

This is great!

Due to its size I'll just comment as I get chance to read it.

Quoted 46 lines
testcases/kernel/kvm/lib_host.c new file +40 -0
new file mode 100644
index 000000000..d070c8106
@@ -0,0 +1,215 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (C) 2021 SUSE LLC <[email protected]>
+ *
+ * KVM host library for setting up and running virtual machine tests.
+ */
+
+#include <stdlib.h>
+#include <errno.h>
+
+#define TST_NO_DEFAULT_MAIN
+#include "tst_test.h"
+#include "kvm_host.h"
+
+static struct tst_kvm_instance test_vm = { .vm_fd = -1 };
+
+const unsigned char tst_kvm_reset_code[VM_RESET_CODE_SIZE] = {
+	0xea, 0x00, 0x10, 0x00, 0x00	/* JMP 0x1000 */
+};
+
+void tst_kvm_validate_result(int value)
+{
+	int ttype, valid_result[] = {TPASS, TFAIL, TBROK, TWARN, TINFO, TCONF};
+	size_t i;
+
+	if (value == KVM_TNONE)
+		tst_brk(TBROK, "KVM test did not return any result");
+
+	ttype = TTYPE_RESULT(value);
+
+	for (i = 0; i < ARRAY_SIZE(valid_result); i++) {
+		if (ttype == valid_result[i])
+			return;
+	}
+
+	tst_brk(TBROK, "KVM test returned invalid result value %d", value);
+}
+
+void tst_kvm_print_result(const struct tst_kvm_result *result)
+{

testcases/kernel/kvm/lib_host.c line 50

Quoted 10 lines
testcases/kernel/kvm/lib_host.c new file +10 -0
+	int ttype;
+
+	tst_kvm_validate_result(result->result);
+	ttype = TTYPE_RESULT(result->result);
+
+	if (ttype == TBROK)
+		tst_brk(ttype, "%s", result->message);
+	else
+		tst_res(ttype, "%s", result->message);
+}

Could you please pass the file and lineno from the test?

Quoted 72 lines
testcases/kernel/kvm/lib_host.c new file +72 -0
+
+void *tst_kvm_alloc_memory(int vm, unsigned int slot, uint64_t baseaddr,
+	size_t size, unsigned int flags)
+{
+	size_t pagesize;
+	void *ret;
+	struct kvm_userspace_memory_region memslot = {
+		.slot = slot,
+		.flags = flags
+	};
+
+	pagesize = SAFE_SYSCONF(_SC_PAGESIZE);
+	size += (baseaddr % pagesize) + pagesize - 1;
+	baseaddr -= baseaddr % pagesize;
+	size -= size % pagesize;
+	ret = tst_alloc(size);
+
+	memslot.guest_phys_addr = baseaddr;
+	memslot.memory_size = size;
+	memslot.userspace_addr = (uintptr_t)ret;
+	SAFE_IOCTL(vm, KVM_SET_USER_MEMORY_REGION, &memslot);
+	return ret;
+}
+
+struct kvm_cpuid2 *tst_kvm_get_cpuid(int sysfd)
+{
+	unsigned int count;
+	int result;
+	struct kvm_cpuid2 *ret;
+
+	if (!SAFE_IOCTL(sysfd, KVM_CHECK_EXTENSION, KVM_CAP_EXT_CPUID))
+		return NULL;
+
+	for (count = 8; count < 1 << 30; count *= 2) {
+		ret = SAFE_MALLOC(sizeof(struct kvm_cpuid2) +
+			count * sizeof(struct kvm_cpuid_entry2));
+		ret->nent = count;
+		errno = 0;
+		result = ioctl(sysfd, KVM_GET_SUPPORTED_CPUID, ret);
+
+		if (!result)
+			return ret;
+
+		free(ret);
+
+		if (errno != E2BIG)
+			break;
+	}
+
+	tst_brk(TBROK | TERRNO, "ioctl(KVM_GET_SUPPORTED_CPUID) failed");
+	return NULL;
+}
+
+void tst_kvm_create_instance(struct tst_kvm_instance *inst, size_t ram_size)
+{
+	int sys_fd;
+	size_t pagesize, result_pageaddr = KVM_RESULT_BASEADDR;
+	char *vm_result, *reset_ptr;
+	struct kvm_cpuid2 *cpuid_data;
+	const size_t payload_size = kvm_payload_end - kvm_payload_start;
+
+	memset(inst, 0, sizeof(struct tst_kvm_instance));
+	inst->vm_fd = -1;
+	inst->vcpu_fd = -1;
+	inst->vcpu_info = MAP_FAILED;
+
+	pagesize = SAFE_SYSCONF(_SC_PAGESIZE);
+	result_pageaddr -= result_pageaddr % pagesize;
+
+	if (payload_size + MIN_FREE_RAM > ram_size - VM_KERNEL_BASEADDR) {
+		ram_size = payload_size + MIN_FREE_RAM + VM_KERNEL_BASEADDR;
+		ram_size += 1024 * 1024 - 1;

testcases/kernel/kvm/lib_host.c line 132

Quoted 10 lines
testcases/kernel/kvm/lib_host.c new file +10 -0
+		ram_size -= ram_size % (1024 * 1024);
+		tst_res(TWARN, "RAM size increased to %zu bytes", ram_size);
+	}
+
+	if (ram_size > result_pageaddr) {
+		ram_size = result_pageaddr;
+		tst_res(TWARN, "RAM size truncated to %zu bytes", ram_size);
+	}
+
+	/* Create VM */

These comments are pretty redundant when we have ioctl's like KVM_CREATE_VM and KVM_CREATE_VCPU. There are much harder things to understand in this patchset.

Quoted 83 lines
testcases/kernel/kvm/lib_host.c new file +83 -0
+	sys_fd = SAFE_OPEN("/dev/kvm", O_RDWR);
+	inst->vcpu_info_size = SAFE_IOCTL(sys_fd, KVM_GET_VCPU_MMAP_SIZE, 0);
+	inst->vm_fd = SAFE_IOCTL(sys_fd, KVM_CREATE_VM, 0);
+	cpuid_data = tst_kvm_get_cpuid(sys_fd);
+	SAFE_CLOSE(sys_fd);
+
+	/* Create virtual CPU */
+	inst->vcpu_fd = SAFE_IOCTL(inst->vm_fd, KVM_CREATE_VCPU, 0);
+
+	if (cpuid_data) {
+		SAFE_IOCTL(inst->vcpu_fd, KVM_SET_CPUID2, cpuid_data);
+		free(cpuid_data);
+	}
+
+	inst->vcpu_info = SAFE_MMAP(NULL, inst->vcpu_info_size,
+		PROT_READ | PROT_WRITE, MAP_SHARED, inst->vcpu_fd, 0);
+
+	/* Set VM memory banks and install test program */
+	inst->ram = tst_kvm_alloc_memory(inst->vm_fd, 0, 0, ram_size, 0);
+	vm_result = tst_kvm_alloc_memory(inst->vm_fd, 1, KVM_RESULT_BASEADDR,
+		KVM_RESULT_SIZE, 0);
+	memset(vm_result, 0, KVM_RESULT_SIZE);
+	memcpy(inst->ram + VM_KERNEL_BASEADDR, kvm_payload_start, payload_size);
+
+	reset_ptr = vm_result + (VM_RESET_BASEADDR % pagesize);
+	memcpy(reset_ptr, tst_kvm_reset_code, sizeof(tst_kvm_reset_code));
+	inst->result = (struct tst_kvm_result *)(vm_result +
+		(KVM_RESULT_BASEADDR % pagesize));
+	inst->result->result = KVM_TNONE;
+	inst->result->message[0] = '\0';
+}
+
+void tst_kvm_run_instance(struct tst_kvm_instance *inst)
+{
+	struct kvm_regs regs;
+
+	while (1) {
+		inst->result->result = KVM_TNONE;
+		inst->result->message[0] = '\0';
+		SAFE_IOCTL(inst->vcpu_fd, KVM_RUN, 0);
+
+		if (inst->vcpu_info->exit_reason != KVM_EXIT_HLT) {
+			SAFE_IOCTL(inst->vcpu_fd, KVM_GET_REGS, &regs);
+			tst_brk(TBROK,
+				"Unexpected VM exit, RIP=0x%llx, reason=%u",
+				regs.rip, inst->vcpu_info->exit_reason);
+		}
+
+		if (inst->result->result == KVM_TEXIT)
+			break;
+
+		tst_kvm_print_result(inst->result);
+	}
+}
+
+void tst_kvm_destroy_instance(struct tst_kvm_instance *inst)
+{
+	if (inst->vm_fd < 0)
+		return;
+
+	if (inst->vcpu_info != MAP_FAILED)
+		SAFE_MUNMAP(inst->vcpu_info, inst->vcpu_info_size);
+
+	if (inst->vcpu_fd >= 0)
+		SAFE_CLOSE(inst->vcpu_fd);
+
+	SAFE_CLOSE(inst->vm_fd);
+}
+
+void tst_kvm_setup(void)
+{
+	tst_kvm_create_instance(&test_vm, DEFAULT_RAM_SIZE);
+}
+
+void tst_kvm_run(void)
+{
+	tst_kvm_run_instance(&test_vm);
+}
+
+void tst_kvm_cleanup(void)
+{
+	tst_kvm_destroy_instance(&test_vm);
+}
Signature · 5 lines
Thank you,
Richard.

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#12 lore raw
Quoted 17 lines · Richard Palethorpe

Hi Martin,

testcases/kernel/kvm/lib_host.c line 50

Quoted 12 lines · Martin Doucha
testcases/kernel/kvm/lib_host.c new file +12 -0
+void tst_kvm_print_result(const struct tst_kvm_result *result)
+{
+	int ttype;
+
+	tst_kvm_validate_result(result->result);
+	ttype = TTYPE_RESULT(result->result);
+
+	if (ttype == TBROK)
+		tst_brk(ttype, "%s", result->message);
+	else
+		tst_res(ttype, "%s", result->message);
+}

Could you please pass the file and lineno from the test?

I've skipped that mainly because passing the filename would require an extra string buffer in the result structure. But I guess I can pass just a 64bit string address since the filename is a string constant and then read the text from the VM memory buffer.

Quoted 25 lines
Quoted 25 lines
testcases/kernel/kvm/lib_host.c new file +25 -0
+void tst_kvm_create_instance(struct tst_kvm_instance *inst, size_t ram_size)
+{
+	int sys_fd;
+	size_t pagesize, result_pageaddr = KVM_RESULT_BASEADDR;
+	char *vm_result, *reset_ptr;
+	struct kvm_cpuid2 *cpuid_data;
+	const size_t payload_size = kvm_payload_end - kvm_payload_start;
+
+	memset(inst, 0, sizeof(struct tst_kvm_instance));
+	inst->vm_fd = -1;
+	inst->vcpu_fd = -1;
+	inst->vcpu_info = MAP_FAILED;
+
+	pagesize = SAFE_SYSCONF(_SC_PAGESIZE);
+	result_pageaddr -= result_pageaddr % pagesize;
+
+	if (payload_size + MIN_FREE_RAM > ram_size - VM_KERNEL_BASEADDR) {
+		ram_size = payload_size + MIN_FREE_RAM + VM_KERNEL_BASEADDR;
+		ram_size += 1024 * 1024 - 1;
+		ram_size -= ram_size % (1024 * 1024);
+		tst_res(TWARN, "RAM size increased to %zu bytes", ram_size);
+	}
+
+	if (ram_size > result_pageaddr) {
+		ram_size = result_pageaddr;
Quoted 8 lines

testcases/kernel/kvm/lib_host.c line 132

Quoted 4 lines
testcases/kernel/kvm/lib_host.c new file +4 -0
+		tst_res(TWARN, "RAM size truncated to %zu bytes", ram_size);
+	}
+
+	/* Create VM */

These comments are pretty redundant when we have ioctl's like KVM_CREATE_VM and KVM_CREATE_VCPU. There are much harder things to understand in this patchset.

I know that the code is straightforward. But it's quite dense so I've added the comments as section headers for easier navigation when you need to change something.

Quoted 31 lines
Quoted 31 lines
testcases/kernel/kvm/lib_host.c new file +31 -0
+	sys_fd = SAFE_OPEN("/dev/kvm", O_RDWR);
+	inst->vcpu_info_size = SAFE_IOCTL(sys_fd, KVM_GET_VCPU_MMAP_SIZE, 0);
+	inst->vm_fd = SAFE_IOCTL(sys_fd, KVM_CREATE_VM, 0);
+	cpuid_data = tst_kvm_get_cpuid(sys_fd);
+	SAFE_CLOSE(sys_fd);
+
+	/* Create virtual CPU */
+	inst->vcpu_fd = SAFE_IOCTL(inst->vm_fd, KVM_CREATE_VCPU, 0);
+
+	if (cpuid_data) {
+		SAFE_IOCTL(inst->vcpu_fd, KVM_SET_CPUID2, cpuid_data);
+		free(cpuid_data);
+	}
+
+	inst->vcpu_info = SAFE_MMAP(NULL, inst->vcpu_info_size,
+		PROT_READ | PROT_WRITE, MAP_SHARED, inst->vcpu_fd, 0);
+
+	/* Set VM memory banks and install test program */
+	inst->ram = tst_kvm_alloc_memory(inst->vm_fd, 0, 0, ram_size, 0);
+	vm_result = tst_kvm_alloc_memory(inst->vm_fd, 1, KVM_RESULT_BASEADDR,
+		KVM_RESULT_SIZE, 0);
+	memset(vm_result, 0, KVM_RESULT_SIZE);
+	memcpy(inst->ram + VM_KERNEL_BASEADDR, kvm_payload_start, payload_size);
+
+	reset_ptr = vm_result + (VM_RESET_BASEADDR % pagesize);
+	memcpy(reset_ptr, tst_kvm_reset_code, sizeof(tst_kvm_reset_code));
+	inst->result = (struct tst_kvm_result *)(vm_result +
+		(KVM_RESULT_BASEADDR % pagesize));
+	inst->result->result = KVM_TNONE;
+	inst->result->message[0] = '\0';
+}
Signature · 10 lines
Martin Doucha   [email protected]
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#13 lore raw

Hi Martin,

Quoted 10 lines · Martin Doucha
Quoted 5 lines
Quoted 1 line

+ /* Create VM */

These comments are pretty redundant when we have ioctl's like KVM_CREATE_VM and KVM_CREATE_VCPU. There are much harder things to understand in this patchset.

I know that the code is straightforward. But it's quite dense so I've added the comments as section headers for easier navigation when you need to change something.

I have no dissent on the comments.

But I'm wondering do we need a check if the SUT support creating VM before doing that? Or, should we guarantee the KVM module is enabled.

I simply built these codes and got TBROK on my KVM guest. Probably we should avoid this but I hoping it can skip gracefully.

[root@local-guest-rhel83 ~]# virt-what
kvm

# ./kvm_pagefault01
tst_sys_conf.c:64: TINFO: Path not found:
'/sys/module/kvm/parameters/tdp_mmu'
tst_test.c:1458: TINFO: Timeout per run is 0h 05m 00s
lib_host.c:133: TBROK: open(/dev/kvm,2,0000) failed: ENOENT (2)

Summary:

passed   0
failed   0
broken   1
skipped  0

warnings 0

Signature · 2 lines
Regards,
Li Wang
Signature · 2 lines
-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#14 lore raw
In reply to Martin Doucha

Hi,

Quoted 19 lines · Martin Doucha
Quoted 17 lines · Richard Palethorpe

Hi Martin,

testcases/kernel/kvm/lib_host.c line 50

Quoted 12 lines · Martin Doucha
testcases/kernel/kvm/lib_host.c new file +12 -0
+void tst_kvm_print_result(const struct tst_kvm_result *result)
+{
+	int ttype;
+
+	tst_kvm_validate_result(result->result);
+	ttype = TTYPE_RESULT(result->result);
+
+	if (ttype == TBROK)
+		tst_brk(ttype, "%s", result->message);
+	else
+		tst_res(ttype, "%s", result->message);
+}

Could you please pass the file and lineno from the test?

Quoted 4 lines

I've skipped that mainly because passing the filename would require an extra string buffer in the result structure. But I guess I can pass just a 64bit string address since the filename is a string constant and then read the text from the VM memory buffer.

Either way sounds good to me.

Quoted 35 lines
Quoted 26 lines
Quoted 26 lines
testcases/kernel/kvm/lib_host.c new file +25 -0
+void tst_kvm_create_instance(struct tst_kvm_instance *inst, size_t ram_size)
+{
+	int sys_fd;
+	size_t pagesize, result_pageaddr = KVM_RESULT_BASEADDR;
+	char *vm_result, *reset_ptr;
+	struct kvm_cpuid2 *cpuid_data;
+	const size_t payload_size = kvm_payload_end - kvm_payload_start;
+
+	memset(inst, 0, sizeof(struct tst_kvm_instance));
+	inst->vm_fd = -1;
+	inst->vcpu_fd = -1;
+	inst->vcpu_info = MAP_FAILED;
+
+	pagesize = SAFE_SYSCONF(_SC_PAGESIZE);
+	result_pageaddr -= result_pageaddr % pagesize;
+
+	if (payload_size + MIN_FREE_RAM > ram_size - VM_KERNEL_BASEADDR) {
+		ram_size = payload_size + MIN_FREE_RAM + VM_KERNEL_BASEADDR;
+		ram_size += 1024 * 1024 - 1;
+		ram_size -= ram_size % (1024 * 1024);
+		tst_res(TWARN, "RAM size increased to %zu bytes", ram_size);
+	}
+
+	if (ram_size > result_pageaddr) {
+		ram_size = result_pageaddr;
Quoted 8 lines

testcases/kernel/kvm/lib_host.c line 132

Quoted 4 lines
testcases/kernel/kvm/lib_host.c new file +4 -0
+		tst_res(TWARN, "RAM size truncated to %zu bytes", ram_size);
+	}
+
+	/* Create VM */

These comments are pretty redundant when we have ioctl's like KVM_CREATE_VM and KVM_CREATE_VCPU. There are much harder things to understand in this patchset.

Quoted 3 lines

I know that the code is straightforward. But it's quite dense so I've added the comments as section headers for easier navigation when you need to change something.

Makes sense. However the style guide forbids commenting the obvious.

Signature · 5 lines
Thank you,
Richard.

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#15 lore raw
In reply to Richard Palethorpe

Hello,

Quoted 21 lines · Richard Palethorpe

Hi Martin,

Quoted 11 lines · Martin Doucha

Implement LTP infrastructure for tests which require executing a special program inside KVM virtual machine. The infrastructure is split into two parts:
the host library and the guest library.

The host library provides functions for setting up and running virtual machines with test payload built from test sources.

The guest library provides CPU bootstrap code and basic implementation of some C and LTP library functions as well as functions for accessing low-level arch features like special registers, interrupt tables or memory mapping tables.

Quoted 5 lines

The test sources will contain both guest-side payload code and host-side setup code separated by preprocessor switch. The files get compiled twice with different compiler options. Once to build the VM payload, once to build the main program that will create the VM and execute the payload inside it.

Quoted 3 lines

This is great!

Due to its size I'll just comment as I get chance to read it.

The rest looks good. I only put minimal effort into understanding the assembly and linking, but it looks clean.

I also tested with nested virt and compiling with Clang.

Signature · 5 lines
Thank you,
Richard.

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp