[LTP] [PATCH 1/2] KVM test infrastructure

Martin Doucha mdoucha@suse.cz
Wed Mar 16 18:03:40 CET 2022


On 15. 03. 22 16:00, Richard Palethorpe wrote:
> Hi Martin,
> 
> Martin Doucha <mdoucha@suse.cz> writes:
>> +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.

>> +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;
>> +		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.

>> +	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';
>> +}

-- 
Martin Doucha   mdoucha@suse.cz
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic


More information about the ltp mailing list