Received: by 2002:a05:6520:4211:b029:f4:110d:56bc with SMTP id o17csp1585448lkv; Wed, 19 May 2021 13:24:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJznNS5Hw+/OAJWdnFiWv21VMF2lRCltF4cLqTCOWR19G6aBJ6MUWdKEzUMfKR+AroSE+L7p X-Received: by 2002:a17:906:3949:: with SMTP id g9mr957454eje.7.1621455852548; Wed, 19 May 2021 13:24:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1621455852; cv=none; d=google.com; s=arc-20160816; b=cnNKclhukHU1D8ay+lA34Hkpim8yVYcKIDzRxeoxO2dryxYAchUyfZvDRref2Mb3dk FeSUgksOyF7FxSuyeqO88pwP3tPE1FV0Bc9QvS1zeZXWaaNEKTWnf4q9XH9PhCVoxugO ni7aZJnpi0FJkSwChGoh+TNOlSl8ROLnbZtd+jenK1hkeQHQEdoAumoAGBuL7wQkvJyL +nyNdll/qRbw8mVpbrYTl/SA5ozd1rKZCJvbcd58dIu66moeHLYWVD+XpAAQgYyX/6WJ 76sdzGJyLw0oHYbnDliPyK217GztgZr8VuIOzRWZ2zK4k16vn+umb8xixJsV51vBqBeb YCBQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=MjsFjVBl/FxOvPsR75EG0eCugTJDkb3SkVrZ8fG721M=; b=SGyWI61FmuBqSIRaWlFCrTDwHg/iOFnDsCOq5WhSgUrWgej5yi+ATnNeI7MLImGq+H STNXxdxMkhXhRgdkLQE1BiWJ9uqXYkVa7F76UNzQM+86npD4xvaUTUWDN/Q7uUK1Y1Bg jZ/sLnwybTy8tPViJqTD8sP7snThxkVzIhDu+Jbkd+nsb0JIGAbBUE6YUlPoPJJd2Wl3 SZd4nr4e3Ad/l1xU2zwCU8FogE51WsL8v3vVcWpW3vamxYCu19gid43u+k1C9cvgkXei c1y1RgglL+tASMyVavGRjPsVgaEzkL58TtwMXFsAi7BWq1AC/xXp9NOmZKGbsMm1lsD0 hDmA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=spQmodnH; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id m21si214330edc.194.2021.05.19.13.23.49; Wed, 19 May 2021 13:24:12 -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=@google.com header.s=20161025 header.b=spQmodnH; 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=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233018AbhESUFw (ORCPT + 99 others); Wed, 19 May 2021 16:05:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33242 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232865AbhESUFZ (ORCPT ); Wed, 19 May 2021 16:05:25 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A256BC06138A for ; Wed, 19 May 2021 13:04:04 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id d4-20020a25b5c40000b02904f8e3c8c6c9so19211737ybg.14 for ; Wed, 19 May 2021 13:04:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=MjsFjVBl/FxOvPsR75EG0eCugTJDkb3SkVrZ8fG721M=; b=spQmodnH3l9aCJPUDO3n5Znh2ZyIosaGKET15ANW77RodOarCKRMYdd4ZTT5Dgoc2k Q4gX08tS5MxCuv/Rv2D9zK6mx0pptSUfomMvtiC0DKBscF7RNhNAJ8qBYhuNbU+KJzGD 9Di8fjJ16HT2NQ7g9Mylhd9Q4vBdD04+fGgLLFo0eDjpvWSuFHBCTuvHG5j7p7MiEgcB nlHhRoPl/wldjZApTsit3FNusyqwxSfZqMO5YBm/DF1cpfS5jB5ral14SuCmUiyYDllQ geaoVtg8aJuRrdHrE/1M/XsAjWrP2HqnJiPQ1zWuJIASN//juooGnCdv0WEiCYw1hQUJ KtmQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=MjsFjVBl/FxOvPsR75EG0eCugTJDkb3SkVrZ8fG721M=; b=dOckbhrgiPot3iFim6luaTRhJ+5vqiQmzXuUNy0b+pyr30eYJpsfIVH7HGIPoJgvXl DynZvludvlFh1Za89immJIVjhNo+pl+QqVNk3rPsj3wMJXuiUAXY0LPpPLLqLEf0Ca7w aVfpvw/xtb3DeOljioaTUgEwWWQVNLy6SkFq524IG32R6dkxvymeqSmOwDHI4UR3Ndgt 5FM6POehwV69WANOY85Sx7ADWMlH8f/bJ8r9TJTjB5qnweTydfNLUwToqdmzCFqpcc0w yihClnIlAntMHuVX14IXHmtMMxrzur733NOWeesMRU1eVaz2Xn8lSpb2VE1l+jtJXUWU BqLQ== X-Gm-Message-State: AOAM531WLXWNSa+xhhLvX7XSBCCvs1uFJAseO56F3DPOq/b7v3TQ6nuN gFASAw7PlvmTJNWtGvVU+vABgtwQRabdzzMltK7+ X-Received: from ajr0.svl.corp.google.com ([2620:15c:2cd:203:7eb5:10bb:834a:d5ec]) (user=axelrasmussen job=sendgmr) by 2002:a25:ec02:: with SMTP id j2mr2058895ybh.248.1621454643850; Wed, 19 May 2021 13:04:03 -0700 (PDT) Date: Wed, 19 May 2021 13:03:37 -0700 In-Reply-To: <20210519200339.829146-1-axelrasmussen@google.com> Message-Id: <20210519200339.829146-9-axelrasmussen@google.com> Mime-Version: 1.0 References: <20210519200339.829146-1-axelrasmussen@google.com> X-Mailer: git-send-email 2.31.1.751.gd2f1c929bd-goog Subject: [PATCH v2 08/10] KVM: selftests: create alias mappings when using shared memory From: Axel Rasmussen To: Aaron Lewis , Alexander Graf , Andrew Jones , Andrew Morton , Ben Gardon , Emanuele Giuseppe Esposito , Eric Auger , Jacob Xu , Makarand Sonare , Oliver Upton , Paolo Bonzini , Peter Xu , Shuah Khan , Yanan Wang Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, Axel Rasmussen Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When a memory region is added with a src_type specifying that it should use some kind of shared memory, also create an alias mapping to the same underlying physical pages. And, add an API so tests can get access to these alias addresses. Basically, for a guest physical address, let us look up the analogous host *alias* address. In a future commit, we'll modify the demand paging test to take advantage of this to exercise UFFD minor faults. The idea is, we pre-fault the underlying pages *via the alias*. When the *guest* faults, it gets a "minor" fault (PTEs don't exist yet, but a page is already in the page cache). Then, the userfaultfd theads can handle the fault: they could potentially modify the underlying memory *via the alias* if they wanted to, and then they install the PTEs and let the guest carry on via a UFFDIO_CONTINUE ioctl. Reviewed-by: Ben Gardon Signed-off-by: Axel Rasmussen --- .../testing/selftests/kvm/include/kvm_util.h | 1 + tools/testing/selftests/kvm/lib/kvm_util.c | 51 +++++++++++++++++++ .../selftests/kvm/lib/kvm_util_internal.h | 2 + 3 files changed, 54 insertions(+) diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing/selftests/kvm/include/kvm_util.h index a8f022794ce3..0624f25a6803 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -146,6 +146,7 @@ void virt_map(struct kvm_vm *vm, uint64_t vaddr, uint64_t paddr, void *addr_gpa2hva(struct kvm_vm *vm, vm_paddr_t gpa); void *addr_gva2hva(struct kvm_vm *vm, vm_vaddr_t gva); vm_paddr_t addr_hva2gpa(struct kvm_vm *vm, void *hva); +void *addr_gpa2alias(struct kvm_vm *vm, vm_paddr_t gpa); /* * Address Guest Virtual to Guest Physical diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/selftests/kvm/lib/kvm_util.c index e4a8d0c43c5e..0b88d1bbc1e0 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -811,6 +811,19 @@ void vm_userspace_mem_region_add(struct kvm_vm *vm, /* Add to linked-list of memory regions. */ list_add(®ion->list, &vm->userspace_mem_regions); + + /* If shared memory, create an alias. */ + if (region->fd >= 0) { + region->mmap_alias = mmap(NULL, region->mmap_size, + PROT_READ | PROT_WRITE, + vm_mem_backing_src_alias(src_type)->flag, + region->fd, 0); + TEST_ASSERT(region->mmap_alias != MAP_FAILED, + "mmap of alias failed, errno: %i", errno); + + /* Align host alias address */ + region->host_alias = align(region->mmap_alias, alignment); + } } /* @@ -1239,6 +1252,44 @@ vm_paddr_t addr_hva2gpa(struct kvm_vm *vm, void *hva) return -1; } +/* + * Address VM physical to Host Virtual *alias*. + * + * Input Args: + * vm - Virtual Machine + * gpa - VM physical address + * + * Output Args: None + * + * Return: + * Equivalent address within the host virtual *alias* area, or NULL + * (without failing the test) if the guest memory is not shared (so + * no alias exists). + * + * When vm_create() and related functions are called with a shared memory + * src_type, we also create a writable, shared alias mapping of the + * underlying guest memory. This allows the host to manipulate guest memory + * without mapping that memory in the guest's address space. And, for + * userfaultfd-based demand paging, we can do so without triggering userfaults. + */ +void *addr_gpa2alias(struct kvm_vm *vm, vm_paddr_t gpa) +{ + struct userspace_mem_region *region; + + list_for_each_entry(region, &vm->userspace_mem_regions, list) { + if (!region->host_alias) + continue; + + if ((gpa >= region->region.guest_phys_addr) + && (gpa <= (region->region.guest_phys_addr + + region->region.memory_size - 1))) + return (void *) ((uintptr_t) region->host_alias + + (gpa - region->region.guest_phys_addr)); + } + + return NULL; +} + /* * VM Create IRQ Chip * diff --git a/tools/testing/selftests/kvm/lib/kvm_util_internal.h b/tools/testing/selftests/kvm/lib/kvm_util_internal.h index 91ce1b5d480b..a25af33d4a9c 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util_internal.h +++ b/tools/testing/selftests/kvm/lib/kvm_util_internal.h @@ -16,7 +16,9 @@ struct userspace_mem_region { int fd; off_t offset; void *host_mem; + void *host_alias; void *mmap_start; + void *mmap_alias; size_t mmap_size; struct list_head list; }; -- 2.31.1.751.gd2f1c929bd-goog