Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp750300ybz; Wed, 15 Apr 2020 18:08:26 -0700 (PDT) X-Google-Smtp-Source: APiQypKv/YKIU/bX/VR/4VYMdZ4s/J2uspeMg70LtjYKChLTvaGsxOVi6e+z1vQRJGSQvJnNHLgm X-Received: by 2002:a50:a68a:: with SMTP id e10mr27484335edc.113.1586999306714; Wed, 15 Apr 2020 18:08:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586999306; cv=none; d=google.com; s=arc-20160816; b=kRfdry3+tWg+MUO6UqgdnmRpjrW+4PeKFzYbsVY8CL6d+eMr2Y2XjjpP9WCYCnZm+7 MMkAIMQSF2J3+BBbMcDiff7wA7WTOdgDldm9vzkhN1AgjLZmXH5uj98hKvaJNomiIHlz x+ZFAfnrMWztEQgTH8Pqc13wpFK2/sysob4v9mJ3+5ziLBvIdjiYE0n5qhrpoUoyXADq nAycfR7Wc5QHGsvzM4XKiPdc4HYZ6aoSaiv5/bM0TQMUXOykM1ddnVfy1rDY8ihx4Lzo 4JJs8xxsQJ+W8H1etp+QEz6Qj9nfR4NYGCrEnv92cwilaDACmq6+NAycp1fwoWKwofU+ LRFQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:dkim-signature; bh=dT66b+xhm+XGbqWaP327XREwdRAFpOiRNwTNoWDM3ic=; b=054L0Lcp5K2mlE+IttRnAmrWGV/GECDtPwrRqmU7JU66rHx99yHWwkFrCQkFFsKnQA SOCUvqxCLJWUv2yl0hF4tWe+5dkylXNG63bCFYUhNI/FNS2CNHaNa28JIyHdInE/RnH9 gsf9l+Vt+1OimId04RYuYEGysJYhZ4wwoyBrrI4kUS45dOX7/MEKCu+dE1LxBQqoXMiF TqZ3NAUJP3IaCyXZYR//b5r5msGs+1yy3R/1BG4ni2xaO4TwXAKORiXn94mNsHSk3rnq halw1rMGWCjkQzWpjaBpS5lGSYXnQzCKkEjZfIHMWqkmMMWILOS8aXWJzlK8qro7/CUl IJ+Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=dQJk7MvQ; spf=pass (google.com: 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 y18si8865473edt.554.2020.04.15.18.07.58; Wed, 15 Apr 2020 18:08:26 -0700 (PDT) Received-SPF: pass (google.com: 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=dQJk7MvQ; spf=pass (google.com: 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 S2442133AbgDOUp1 (ORCPT + 99 others); Wed, 15 Apr 2020 16:45:27 -0400 Received: from us-smtp-2.mimecast.com ([205.139.110.61]:38889 "EHLO us-smtp-delivery-1.mimecast.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S2442111AbgDOUpX (ORCPT ); Wed, 15 Apr 2020 16:45:23 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1586983521; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:in-reply-to:in-reply-to:references:references; bh=dT66b+xhm+XGbqWaP327XREwdRAFpOiRNwTNoWDM3ic=; b=dQJk7MvQLDRFck6M2HLaPoI471qO4Q8H/hZSy5yzA9k+In5pYvpDaLwhAjVXXxshUOQ6dk ZrSNuAGU0Yd/iwBLyCP5drrQI1h5fR+AOfdYVKOnXn9Gam+200pXVNWs/TiN715uxxj3SW S9qX6cgUYi4L5/AiKREy5ozvLfl/j1w= 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-466-4ZXvfiFCPNinfxFfWM4ewQ-1; Wed, 15 Apr 2020 16:45:20 -0400 X-MC-Unique: 4ZXvfiFCPNinfxFfWM4ewQ-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 223FF107B267; Wed, 15 Apr 2020 20:45:17 +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 2657D5DA66; Wed, 15 Apr 2020 20:45:12 +0000 (UTC) From: Wainer dos Santos Moschetta To: pbonzini@redhat.com, kvm@vger.kernel.org Cc: drjones@redhat.com, sean.j.christopherson@intel.com, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, krish.sadhukhan@oracle.com Subject: [PATCH 1/1] selftests: kvm: Add overlapped memory regions test Date: Wed, 15 Apr 2020 17:45:05 -0300 Message-Id: <20200415204505.10021-2-wainersm@redhat.com> In-Reply-To: <20200415204505.10021-1-wainersm@redhat.com> References: <20200415204505.10021-1-wainersm@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add the test_overlap_memory_regions() test case in set_memory_region_test. This should check that overlapping memory regions on the guest physical address cannot be added. Signed-off-by: Wainer dos Santos Moschetta --- .../selftests/kvm/set_memory_region_test.c | 75 ++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/tools/testing/selftests/kvm/set_memory_region_test.c b/tools/testing/selftests/kvm/set_memory_region_test.c index 260e638826dc..74a987002273 100644 --- a/tools/testing/selftests/kvm/set_memory_region_test.c +++ b/tools/testing/selftests/kvm/set_memory_region_test.c @@ -331,6 +331,8 @@ static void test_add_max_memory_regions(void) uint64_t mem_reg_npages; void *mem; + pr_info("Testing KVM_CAP_NR_MEMSLOTS memory regions can be added\n"); + 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"); @@ -338,7 +340,8 @@ static void test_add_max_memory_regions(void) vm = vm_create(VM_MODE_DEFAULT, 0, O_RDWR); - mem_reg_npages = vm_calc_num_guest_pages(VM_MODE_DEFAULT, MEM_REGION_SIZE); + 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", @@ -365,6 +368,75 @@ static void test_add_max_memory_regions(void) kvm_vm_free(vm); } +/* + * Test it cannot add memory slots with overlapped regions. + * + * The following cases are covered: + * + * 0x100000 0x300000 + * 0x0 0x200000 0x400000 + * slot0 | |---2MB--| (SUCCESS) + * slot1 |---2MB--| (FAIL) + * slot2 |---2MB--| (SUCCESS) + * slot3 |---2MB--| (FAIL) + * slot4 |---2MB--| (FAIL) + * slot5 |---2MB--| (SUCCESS) + */ +void test_overlap_memory_regions(void) +{ + int i; + int ret; + int vm_fd; + struct kvm_userspace_memory_region kvm_region; + struct kvm_vm *vm; + struct slot_t { + uint64_t guest_addr; + int exp_ret; /* Expected ioctl return value */ + }; + struct slot_t slots[] = {{0x200000, 0}, {0x100000, -1}, {0x000000, 0}, + {0x200000, -1}, {0x300000, -1}, {0x400000, 0} + }; + uint64_t mem_reg_npages; + void *mem; + + pr_info("Testing KVM_SET_USER_MEMORY_REGION with overlapped memory regions\n"); + + vm = vm_create(VM_MODE_DEFAULT, 0, O_RDWR); + vm_fd = vm_get_fd(vm); + + pr_info("Working with memory region of %iMB\n", MEM_REGION_SIZE >> 20); + mem_reg_npages = vm_calc_num_guest_pages(VM_MODE_DEFAULT, + MEM_REGION_SIZE); + + 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"); + + kvm_region.flags = 0; + kvm_region.memory_size = MEM_REGION_SIZE; + kvm_region.userspace_addr = (uint64_t) mem; + + for (i = 0; i < sizeof(slots)/sizeof(struct slot_t); i++) { + pr_info("Add slot %i, guest address 0x%06lx, expect rc=%i\n", + i, slots[i].guest_addr, slots[i].exp_ret); + if (slots[i].exp_ret == 0) { + vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, + slots[i].guest_addr, i, + mem_reg_npages, 0); + } else { + kvm_region.slot = i; + kvm_region.guest_phys_addr = slots[i].guest_addr; + ret = ioctl(vm_fd, KVM_SET_USER_MEMORY_REGION, + &kvm_region); + TEST_ASSERT(ret == -1 && errno == EEXIST, + "Adding overlapped memory region should fail with EEXIT"); + } + } + + munmap(mem, MEM_REGION_SIZE); + kvm_vm_free(vm); +} + int main(int argc, char *argv[]) { #ifdef __x86_64__ @@ -383,6 +455,7 @@ int main(int argc, char *argv[]) #endif test_add_max_memory_regions(); + test_overlap_memory_regions(); #ifdef __x86_64__ if (argc > 1) -- 2.17.2