Received: by 2002:a25:6193:0:0:0:0:0 with SMTP id v141csp3445742ybb; Mon, 13 Apr 2020 08:14:03 -0700 (PDT) X-Google-Smtp-Source: APiQypI/mwaY1U4YCutoNH9dvfl7pJHw1ylgYZ6iKBFTTxoA0xsVzGKihfyfF8Ka/sPLH7N5lFjJ X-Received: by 2002:a50:cf05:: with SMTP id c5mr10455415edk.330.1586790843702; Mon, 13 Apr 2020 08:14:03 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586790843; cv=none; d=google.com; s=arc-20160816; b=eXoLU5fhBFh9RvphQarQdvFUQoN7IdbIGp3zlG69K7LZemuCDeopkeq1nyiRXigXC1 UbD7yYpmwIa7TzWQ8DEs5nWk1UrngmKhGt6guUU6fskLwSxZhRF9q/MFy9sfVuzIwKbp O4wT+9UmLpVSu17BjX6qIsPHpIEC2QqG+FpWKbCs6RjGmOm5XuXjgel5KBvig6HEAUqb zEQZSwb0N3yXP7ORLC+oF17UePbhdwsFcKn+643k0N9RTwx71oph/2JJmF6rAiwcLNj2 UrzM2RWshxY8OOr8U5Bw2OtH0CWn6N+y5f2SMng4rRI/+gn/qnt73pC7PlhDThtHXQV2 nD5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-language :content-transfer-encoding:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject:dkim-signature; bh=EBYP0E/9LflU1vJkdddl+wmzzsAGwHYVvotwoO+EIpA=; b=XjCl8a9WM1tpVgpEuu8COAgNkth9lOmcdBpHwgSl7Ie+YZIUhDSgn5IzxHVIXGQs/S T/ws4hxWuZ8jCghGFpRyDoicNPro04AhlttYoDWBU/uEHAA2bNkbTpqslV+8ntsaiKu4 28MGRlqn73xLJX4sy+RRyja8oY3zSG8urypbkV2dgXc3mJKKza+n8biqR+pgoMlVX333 zmCF24lw+nrROIuM3nJhFMffPX5pYZjK2nbHFd+bANfu1F4u4AYGHMowZuCnxXF+6rm+ bdOEZ/H4HgtoINjxVMARFDivwn/ORqAIPsFUYh8mMYVynSxPwI6z32VbJO7Wjz6ogfhQ 83Bw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=OwljJPYR; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r8si7061611edl.85.2020.04.13.08.13.38; Mon, 13 Apr 2020 08:14:03 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=OwljJPYR; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729815AbgDMNXH (ORCPT + 99 others); Mon, 13 Apr 2020 09:23:07 -0400 Received: from us-smtp-delivery-1.mimecast.com ([207.211.31.120]:28577 "EHLO us-smtp-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1729807AbgDMNXF (ORCPT ); Mon, 13 Apr 2020 09:23:05 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1586784183; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=EBYP0E/9LflU1vJkdddl+wmzzsAGwHYVvotwoO+EIpA=; b=OwljJPYRfgw8i6gJEw1T1JfrGcYacNVnwHIE3MlHEx7EisCZWsedYt1y3dK0KHnJKG8TpD GcyHxG/NqnXlk/XoUmWXJAgdYF+6HTjyuMilIe0ceM2rIgL0gB6bbdKaDJJweWln7IFpUs d/KmatlWi7A3yaFSAW0C0aw/mxTFYIU= Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-325-n-oKRo39OEKW3SiF2cqIRg-1; Mon, 13 Apr 2020 09:22:59 -0400 X-MC-Unique: n-oKRo39OEKW3SiF2cqIRg-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 92BFC8017F3; Mon, 13 Apr 2020 13:22:57 +0000 (UTC) Received: from localhost.localdomain (ovpn-116-15.gru2.redhat.com [10.97.116.15]) by smtp.corp.redhat.com (Postfix) with ESMTP id 361FB60BE1; Mon, 13 Apr 2020 13:22:42 +0000 (UTC) Subject: Re: [PATCH 10/10] selftests: kvm: Add testcase for creating max number of memslots To: Sean Christopherson , Paolo Bonzini , Christian Borntraeger , Janosch Frank Cc: David Hildenbrand , Cornelia Huck , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Peter Xu , Andrew Jones References: <20200410231707.7128-1-sean.j.christopherson@intel.com> <20200410231707.7128-11-sean.j.christopherson@intel.com> From: Wainer dos Santos Moschetta Message-ID: Date: Mon, 13 Apr 2020 10:22:40 -0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.4.0 MIME-Version: 1.0 In-Reply-To: <20200410231707.7128-11-sean.j.christopherson@intel.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Content-Language: en-US X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 4/10/20 8:17 PM, Sean Christopherson wrote: > From: Wainer dos Santos Moschetta > > This patch introduces test_add_max_memory_regions(), which checks > that a VM can have added memory slots up to the limit defined in > KVM_CAP_NR_MEMSLOTS. Then attempt to add one more slot to > verify it fails as expected. > > Signed-off-by: Wainer dos Santos Moschetta > Reviewed-by: Andrew Jones > Signed-off-by: Sean Christopherson > --- > .../selftests/kvm/set_memory_region_test.c | 65 +++++++++++++++++-- > 1 file changed, 60 insertions(+), 5 deletions(-) Putting the memory region related tests together into a single test file makes sense to me. Acked-by: Wainer dos Santos Moschetta > > diff --git a/tools/testing/selftests/kvm/set_memory_region_test.c b/tools/testing/selftests/kvm/set_memory_region_test.c > index 0f36941ebb96..cdf5024b2452 100644 > --- a/tools/testing/selftests/kvm/set_memory_region_test.c > +++ b/tools/testing/selftests/kvm/set_memory_region_test.c > @@ -9,6 +9,7 @@ > #include > #include > #include > +#include > > #include > > @@ -18,14 +19,18 @@ > > #define VCPU_ID 0 > > -#ifdef __x86_64__ > /* > - * Somewhat arbitrary location and slot, intended to not overlap anything. The > - * location and size are specifically 2mb sized/aligned so that the initial > - * region corresponds to exactly one large page. > + * s390x needs at least 1MB alignment, and the x86_64 MOVE/DELETE tests need a > + * 2MB sized and aligned region so that the initial region corresponds to > + * exactly one large page. > */ > -#define MEM_REGION_GPA 0xc0000000 > #define MEM_REGION_SIZE 0x200000 > + > +#ifdef __x86_64__ > +/* > + * Somewhat arbitrary location and slot, intended to not overlap anything. > + */ > +#define MEM_REGION_GPA 0xc0000000 > #define MEM_REGION_SLOT 10 > > static const uint64_t MMIO_VAL = 0xbeefull; > @@ -318,6 +323,54 @@ static void test_zero_memory_regions(void) > kvm_vm_free(vm); > } > > +/* > + * Test it can be added memory slots up to KVM_CAP_NR_MEMSLOTS, then any > + * tentative to add further slots should fail. > + */ > +static void test_add_max_memory_regions(void) > +{ > + int ret; > + struct kvm_vm *vm; > + uint32_t max_mem_slots; > + uint32_t slot; > + uint64_t guest_addr = 0x0; > + uint64_t mem_reg_npages; > + void *mem; > + > + max_mem_slots = kvm_check_cap(KVM_CAP_NR_MEMSLOTS); > + TEST_ASSERT(max_mem_slots > 0, > + "KVM_CAP_NR_MEMSLOTS should be greater than 0"); > + pr_info("Allowed number of memory slots: %i\n", max_mem_slots); > + > + vm = vm_create(VM_MODE_DEFAULT, 0, O_RDWR); > + > + mem_reg_npages = vm_calc_num_guest_pages(VM_MODE_DEFAULT, MEM_REGION_SIZE); > + > + /* Check it can be added memory slots up to the maximum allowed */ > + pr_info("Adding slots 0..%i, each memory region with %dK size\n", > + (max_mem_slots - 1), MEM_REGION_SIZE >> 10); > + for (slot = 0; slot < max_mem_slots; slot++) { > + vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, > + guest_addr, slot, mem_reg_npages, > + 0); > + guest_addr += MEM_REGION_SIZE; > + } > + > + /* Check it cannot be added memory slots beyond the limit */ > + mem = mmap(NULL, MEM_REGION_SIZE, PROT_READ | PROT_WRITE, > + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); > + TEST_ASSERT(mem != MAP_FAILED, "Failed to mmap() host"); > + > + ret = ioctl(vm_get_fd(vm), KVM_SET_USER_MEMORY_REGION, > + &(struct kvm_userspace_memory_region) {slot, 0, guest_addr, > + MEM_REGION_SIZE, (uint64_t) mem}); > + TEST_ASSERT(ret == -1 && errno == EINVAL, > + "Adding one more memory slot should fail with EINVAL"); > + > + munmap(mem, MEM_REGION_SIZE); > + kvm_vm_free(vm); > +} > + > int main(int argc, char *argv[]) > { > #ifdef __x86_64__ > @@ -329,6 +382,8 @@ int main(int argc, char *argv[]) > > test_zero_memory_regions(); > > + test_add_max_memory_regions(); > + > #ifdef __x86_64__ > if (argc > 1) > loops = atoi(argv[1]);