Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp61950pxj; Wed, 26 May 2021 16:11:43 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyaqDSDo/fH5NRKGlOXJgJhJemlQ7W02lr6WYCeKPMjbcx5NQPjBWHN2umpdaH9z9nJxlh7 X-Received: by 2002:a05:6402:3446:: with SMTP id l6mr640365edc.305.1622070703221; Wed, 26 May 2021 16:11:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622070703; cv=none; d=google.com; s=arc-20160816; b=dpIB7hdkmOr36ofdWpJ4Eb6Dr99Q1D6/g++ukxC2iIrKoLq0EeP3oG2md4Jhv9Bf0l PZ9sQqoM/QPsAvtjgtUckzwsvdQ7mORiTw7Wyf7Ah03sVjdKNg+y6QWxpbY9Zuf0pUnD pyu8pksw5lAaP0DoSLs9D+NNd0wQ7PiqhMt/YxfqLc0lsB9+z/UkVSef2DtCe0sSNeZY CA2kfuL8zPdDCGOLXzOJapQLayGy6AADpfEGEgdxPfvad+ntfsPqX9ZBQwt7qSEdrSrj WHeWfzxsvYCcl8oQtiX2epS4KKo9B8xFzUdUO3F3sNZc0R1xbRBOjDPY/pkzjp31CVFV yOng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=94syvOS2iq38iWgkmR3FPLbAVi2GFd1p/dJkRiGRhzE=; b=wGP894pBQ+iZ/8hHkzuxR//lYX7p9oQjeiYRvIiAB/oABKw1MSa+YnUDdMuy24XiT0 3SzmEI13nNLJiHjjxcq7G948ehLmJM8K1j3tnjOGygmgvXDYOBTFgPwxmE3JElRH1Zww G8/ItrmiL+TUzHHArbwwFJR55uTzTmvBYzN0L5oVfOkpsIvZ0f9sjVgpbALiDdSysB0+ 1L4ieZZI+nSPIR1SWXgF4xjGOWzjWZAX+V/thb/m9EnXfo6xm+342l/oIoGUZHg66jZw /bWubvany7FriI8GFSI3CkuCVr/3fqJs7KZf3OI4UViYTZFWCs2I8wtJCxoQY7cOR4Wi VnGA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=eYyQfHe5; 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 hq40si470071ejc.161.2021.05.26.16.11.10; Wed, 26 May 2021 16:11:43 -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=eYyQfHe5; 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 S235558AbhEZSd1 (ORCPT + 99 others); Wed, 26 May 2021 14:33:27 -0400 Received: from us-smtp-delivery-124.mimecast.com ([216.205.24.124]:49933 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235544AbhEZSd0 (ORCPT ); Wed, 26 May 2021 14:33:26 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1622053914; 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=94syvOS2iq38iWgkmR3FPLbAVi2GFd1p/dJkRiGRhzE=; b=eYyQfHe51CTv8OvkyM9yBMtuII2JFa60adtTursqTIMN0VsYxHnilOnLCc8jRKyull4iRm DystXtF+O8qPaLM7A/Yy2n1lUdnRDLhjr2sCOGO1YDSimKE1eQulisJGq/zyEORNFpYyhv aqK9U2jQrbwMWeudJUCo4UZb7zudKDk= Received: from mail-ej1-f71.google.com (mail-ej1-f71.google.com [209.85.218.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-569-2HmfZqTYPRePwsKBxqaFgw-1; Wed, 26 May 2021 14:31:52 -0400 X-MC-Unique: 2HmfZqTYPRePwsKBxqaFgw-1 Received: by mail-ej1-f71.google.com with SMTP id rs12-20020a170907036cb02903e0c5dcb92dso716127ejb.15 for ; Wed, 26 May 2021 11:31:52 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=94syvOS2iq38iWgkmR3FPLbAVi2GFd1p/dJkRiGRhzE=; b=qHWG4kmAsprvhi3dNYzoPsDy2/y747gDWDa7wObO4zK5wez/IhJ0//RDtgghUM/7bG xvtfS8/shSJ+81xXXq5YY7I/E11mdK8nfiHpkH1NJfsBf2WX+N3sFNXCqA+V1TpW6LLW WSQJjGgwbCOaI3dpIJ9ObwNXA3rNnRR7c/IW1stu3O5ysB25j8nrJRb5Q/qIQgTVTbaC 8PmZbkeffGz9m/sH3p624x84+tb6EVn8IoHroz8tCOwyCnYHqKobypvOt5gka2x19ejh ZcLXdEwYcuK9n5gIUCnl4pglWWmqDJuilJY1qRo1iuP9FV+IemcmPI5RYDQNkWjMcx3f j1Nw== X-Gm-Message-State: AOAM530SlMzWOrg1VW0NILCryv0VCkI+Zu/Ox8LodBOKzVGYT6smFnK5 2gmtkQ/zEC8cHwWsJ54L0PL4DEj7nIJqsmZJXDaqwlSkh4iycZb6krFBrNdybB1nqPPDBroVRdV WGnlbFiXtYqBjZO3Jg5o0X5Tz X-Received: by 2002:a50:fd9a:: with SMTP id o26mr38385069edt.76.1622053911609; Wed, 26 May 2021 11:31:51 -0700 (PDT) X-Received: by 2002:a50:fd9a:: with SMTP id o26mr38385033edt.76.1622053911300; Wed, 26 May 2021 11:31:51 -0700 (PDT) Received: from ?IPv6:2001:b07:6468:f312:c8dd:75d4:99ab:290a? ([2001:b07:6468:f312:c8dd:75d4:99ab:290a]) by smtp.gmail.com with ESMTPSA id dh21sm12995260edb.28.2021.05.26.11.31.49 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 26 May 2021 11:31:50 -0700 (PDT) Subject: Re: [PATCH v2 08/10] KVM: selftests: create alias mappings when using shared memory To: Axel Rasmussen , David Matlack Cc: Aaron Lewis , Alexander Graf , Andrew Jones , Andrew Morton , Ben Gardon , Emanuele Giuseppe Esposito , Eric Auger , Jacob Xu , Makarand Sonare , Oliver Upton , Peter Xu , Shuah Khan , Yanan Wang , kvm list , LKML , Linuxkselftest References: <20210519200339.829146-1-axelrasmussen@google.com> <20210519200339.829146-9-axelrasmussen@google.com> From: Paolo Bonzini Message-ID: <553e0fdc-53ff-10a6-385a-2cf47f00899f@redhat.com> Date: Wed, 26 May 2021 20:31:47 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.8.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 26/05/21 19:22, Axel Rasmussen wrote: > I applied this change on top of kvm/master and tested it, and indeed > it compiles and works correctly. > > Paolo, feel free to take this with a: > > Reviewed-by: Axel Rasmussen > > Or alternatively if you prefer I'm happy to send it as a real > git-send-email patch. Yes, I squashed it. Paolo > On Tue, May 25, 2021 at 4:50 PM David Matlack wrote: >> >> On Wed, May 19, 2021 at 1:04 PM Axel Rasmussen wrote: >>> >>> 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) { >> >> This patch fails to compile on top of with db0670ce3361 ("KVM: >> selftests: Keep track of memslots more efficiently"). >> >> This can be reproduced by checking out kvm/master and running `make -C >> tools/testing/selftests/kvm`. >> >> The following diff fixes the compilation error but I did not have time >> to test it yet: >> >> diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c >> b/tools/testing/selftests/kvm/lib/kvm_util.c >> index c98db1846e1b..28e528c19d28 100644 >> --- a/tools/testing/selftests/kvm/lib/kvm_util.c >> +++ b/tools/testing/selftests/kvm/lib/kvm_util.c >> @@ -1374,19 +1374,17 @@ vm_paddr_t addr_hva2gpa(struct kvm_vm *vm, void *hva) >> void *addr_gpa2alias(struct kvm_vm *vm, vm_paddr_t gpa) >> { >> struct userspace_mem_region *region; >> + uintptr_t offset; >> >> - list_for_each_entry(region, &vm->userspace_mem_regions, list) { >> - if (!region->host_alias) >> - continue; >> + region = userspace_mem_region_find(vm, gpa, gpa); >> + if (!region) >> + return NULL; >> >> - 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)); >> - } >> + if (!region->host_alias) >> + return NULL; >> >> - return NULL; >> + offset = gpa - region->region.guest_phys_addr; >> + return (void *) ((uintptr_t) region->host_alias + offset); >> } >> >> /* >> >> >> >>> + 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 >>> >