Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp225252pxb; Thu, 20 Jan 2022 12:10:06 -0800 (PST) X-Google-Smtp-Source: ABdhPJzyEhJr1xqzlFC4S1NIKWA7/z12b5/E1f25MmyrXxx7MK18VmsHPY9i4ASwRHx6C/+C4qT6 X-Received: by 2002:a17:902:6b02:b0:149:9c7d:3ae7 with SMTP id o2-20020a1709026b0200b001499c7d3ae7mr319647plk.51.1642709406537; Thu, 20 Jan 2022 12:10:06 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642709406; cv=none; d=google.com; s=arc-20160816; b=QayNIwmljANz1bIOel4M7w9P8Cp7l531V+S5e9IiAyZGQ2Cdnl8EUKEXxJ2ltvmRV3 FPj1KwJy52Z3bXDZffGvoU2e4ccTjeldf+VQYKkNnIxArfRi6+ZV8HVUMEYTe3jXva1h p3c6/FNMW7bhHkXfCphaPD5xMbSoHxO92RG/O4zNajsXvckrH1xsGBh4gk5WZDjQhVcd 7MBV5k0LzfII/VhOtAPF+iMHjsRPfUSYMpBoyz2jeZKx1FNeYsPfBhySuad/NDFYvhNd OSUHnLWKtzrkCCGMoju5S7UkrQpBZmYCZDmZuGlDEW+jn07A9o5k9LVVbyNF6P+KC+kX dUOA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:in-reply-to:subject :references:cc:to:from:content-language:user-agent:mime-version:date :message-id; bh=p5uQiLk1Fd/T6obUjbzXoaGvVOzoDWuB15j3w8CGRZo=; b=n60JaXZZ41mwzn4sgOrc4MsZtaOuiiRrlUGfxDWUi48ICOznn60ik//hbTH9jplnyw Va11u6UBlpTEhCr8Zf7lDJ1MF0pp5zFfuSAVcj5sbc9uSVKhy6a24DJMLWqIIhqZY35D ifHPg5BwY2dyJByzIV+UgFfx/hAMbwJYW0PXtRaTGhiad3z+WoLRhimbZIFYUQM7W6Kk udO3n+FhvJAaX+JGDVp3YrVHnB9RPmj9jnFjOBGuiTEFrPm2/jBZYXkemzWwJIMdQhbL Utj8kexreE4aviL6hxnajUABEq1bq6oS+59oFDfxf2bbqcEmFuMU/vuNL6EPo8l4bsRi OIkA== ARC-Authentication-Results: i=1; mx.google.com; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f124si4428957pgc.532.2022.01.20.12.09.53; Thu, 20 Jan 2022 12:10:06 -0800 (PST) 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; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236158AbiARRaK (ORCPT + 99 others); Tue, 18 Jan 2022 12:30:10 -0500 Received: from vps-vb.mhejs.net ([37.28.154.113]:43150 "EHLO vps-vb.mhejs.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347244AbiARRaI (ORCPT ); Tue, 18 Jan 2022 12:30:08 -0500 Received: from MUA by vps-vb.mhejs.net with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1n9sIu-0000RU-G7; Tue, 18 Jan 2022 18:29:56 +0100 Message-ID: <28a005b7-9ae3-fe0d-b003-9aedba27dc85@maciej.szmigiero.name> Date: Tue, 18 Jan 2022 18:29:50 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.4.0 Content-Language: en-US From: "Maciej S. Szmigiero" To: Nikunj A Dadhania Cc: Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Brijesh Singh , Tom Lendacky , Peter Gonda , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Paolo Bonzini References: <20220118110621.62462-1-nikunj@amd.com> <20220118110621.62462-7-nikunj@amd.com> <010ef70c-31a2-2831-a2a7-950db14baf23@maciej.szmigiero.name> Subject: Re: [RFC PATCH 6/6] KVM: SVM: Pin SEV pages in MMU during sev_launch_update_data() In-Reply-To: <010ef70c-31a2-2831-a2a7-950db14baf23@maciej.szmigiero.name> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 18.01.2022 16:00, Maciej S. Szmigiero wrote: > Hi Nikunj, > > On 18.01.2022 12:06, Nikunj A Dadhania wrote: >> From: Sean Christopherson >> >> Pin the memory for the data being passed to launch_update_data() >> because it gets encrypted before the guest is first run and must >> not be moved which would corrupt it. >> >> Signed-off-by: Sean Christopherson >> [ * Changed hva_to_gva() to take an extra argument and return gpa_t. >>    * Updated sev_pin_memory_in_mmu() error handling. >>    * As pinning/unpining pages is handled within MMU, removed >>      {get,put}_user(). ] >> Signed-off-by: Nikunj A Dadhania >> --- >>   arch/x86/kvm/svm/sev.c | 122 ++++++++++++++++++++++++++++++++++++++++- >>   1 file changed, 119 insertions(+), 3 deletions(-) >> >> diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c >> index 14aeccfc500b..1ae714e83a3c 100644 >> --- a/arch/x86/kvm/svm/sev.c >> +++ b/arch/x86/kvm/svm/sev.c >> @@ -22,6 +22,7 @@ >>   #include >>   #include >> +#include "mmu.h" >>   #include "x86.h" >>   #include "svm.h" >>   #include "svm_ops.h" >> @@ -490,6 +491,110 @@ static unsigned long get_num_contig_pages(unsigned long idx, >>       return pages; >>   } >> +#define SEV_PFERR_RO (PFERR_USER_MASK) >> +#define SEV_PFERR_RW (PFERR_WRITE_MASK | PFERR_USER_MASK) >> + >> +static struct kvm_memory_slot *hva_to_memslot(struct kvm *kvm, >> +                          unsigned long hva) >> +{ >> +    struct kvm_memslots *slots = kvm_memslots(kvm); >> +    struct kvm_memory_slot *memslot; >> +    int bkt; >> + >> +    kvm_for_each_memslot(memslot, bkt, slots) { >> +        if (hva >= memslot->userspace_addr && >> +            hva < memslot->userspace_addr + >> +            (memslot->npages << PAGE_SHIFT)) >> +            return memslot; >> +    } >> + >> +    return NULL; >> +} > > We have kvm_for_each_memslot_in_hva_range() now, please don't do a linear > search through memslots. > You might need to move the aforementioned macro from kvm_main.c to some > header file, though. Besides performance considerations I can't see the code here taking into account the fact that a hva can map to multiple memslots (they an overlap in the host address space). Thanks, Maciej