From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-8.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 27F82C4320E for ; Wed, 1 Sep 2021 16:27:32 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 0E56861053 for ; Wed, 1 Sep 2021 16:27:32 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245412AbhIAQ21 (ORCPT ); Wed, 1 Sep 2021 12:28:27 -0400 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]:32425 "EHLO us-smtp-delivery-124.mimecast.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S245218AbhIAQ2W (ORCPT ); Wed, 1 Sep 2021 12:28:22 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1630513645; 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=TCAH52YqS9JZQzJKa7DRR1Gpuc8EPZCmTZ5AQVbYtWA=; b=CQSXfa1RsLb8cees6QT9Hv5mRreNLsW5HcBvOcAgXOXFdtnlUYIv8ZrxLTbUPb59GwkJUa dQqvHNVvBvcGGvV7ZyRB9xOaw0ibOKu0yptS5I8P0cI5Pfvo0NXoPZ9BtKiknc2U695N7r gFOSR/rywuYjLZE23EjnzDN9ifKSQEw= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-484-apohYhG0NAeahmusu-pLbQ-1; Wed, 01 Sep 2021 12:27:23 -0400 X-MC-Unique: apohYhG0NAeahmusu-pLbQ-1 Received: by mail-wm1-f72.google.com with SMTP id p11-20020a05600c204b00b002f05aff1663so36625wmg.2 for ; Wed, 01 Sep 2021 09:27:23 -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:organization :message-id:date:user-agent:mime-version:in-reply-to :content-language:content-transfer-encoding; bh=TCAH52YqS9JZQzJKa7DRR1Gpuc8EPZCmTZ5AQVbYtWA=; b=fYGTR3UJdQEDiAK5Q8TlL9YPzZUAxeyJoFCTT2riGE9g6JuOwzv5xLz9htACruGxT/ ZuvTJuOKy+4QkWWRYgWh1J+4+nqoBpOxP8QVK46ogiR4A1gsc+Wd2cSCJnTyV8vSX/BB 7PK7UUidxaAX6ffQL/2kJZKVW29QRAgeov9PguY6CTWL/D59eXR6Q5zPBpFp22jBFObR fHl36+n+a5B8L7Qe4vVX4dDsOY7OMlx8fC2v6zsIceo4SFayaIDeXH8RS7M6zNGA79xf w18Pf2Ee3poSdO5qRRChXD22F02Y0llGtffuIq3gw9ApeiPf5Oa83DTvmgzpGohe5CGR kWUg== X-Gm-Message-State: AOAM5320j+TC0HCr4a7ugNcwzeXM+FyYZZB5pm2y9gelWUMIFInBmeVv 2rsDIyhmgNMSTE2WBctNKrySNyfrhcKuPVLmYHEhYM8lZA6laoPeN0KUTaTSDkrJJ9+2U0OsDcy GqCSunYgmXIFa8CoDm/r1Z8XZ X-Received: by 2002:adf:f9cb:: with SMTP id w11mr259292wrr.382.1630513642555; Wed, 01 Sep 2021 09:27:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzGHmzuvAvqEcnBZI7Y23pSv8aObIhsarRJKP+kEp6VN4fOLNmhGpMU3DYltGLE2gcg2FV3PQ== X-Received: by 2002:adf:f9cb:: with SMTP id w11mr259251wrr.382.1630513642352; Wed, 01 Sep 2021 09:27:22 -0700 (PDT) Received: from [192.168.3.132] (p4ff23f71.dip0.t-ipconnect.de. [79.242.63.113]) by smtp.gmail.com with ESMTPSA id k17sm225645wmj.0.2021.09.01.09.27.20 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 01 Sep 2021 09:27:21 -0700 (PDT) Subject: Re: [RFC] KVM: mm: fd-based approach for supporting KVM guest private memory To: Andy Lutomirski , Yu Zhang Cc: Sean Christopherson , Paolo Bonzini , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , kvm list , Linux Kernel Mailing List , Borislav Petkov , Andrew Morton , Joerg Roedel , Andi Kleen , David Rientjes , Vlastimil Babka , Tom Lendacky , Thomas Gleixner , "Peter Zijlstra (Intel)" , Ingo Molnar , Varad Gautam , Dario Faggioli , the arch/x86 maintainers , linux-mm@kvack.org, linux-coco@lists.linux.dev, "Kirill A. Shutemov" , "Kirill A . Shutemov" , Sathyanarayanan Kuppuswamy , Dave Hansen References: <20210824005248.200037-1-seanjc@google.com> <307d385a-a263-276f-28eb-4bc8dd287e32@redhat.com> <20210827023150.jotwvom7mlsawjh4@linux.intel.com> <8f3630ff-bd6d-4d57-8c67-6637ea2c9560@www.fastmail.com> <20210901102437.g5wrgezmrjqn3mvy@linux.intel.com> From: David Hildenbrand Organization: Red Hat Message-ID: <2b2740ec-fa89-e4c3-d175-824e439874a6@redhat.com> Date: Wed, 1 Sep 2021 18:27:20 +0200 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 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 01.09.21 18:07, Andy Lutomirski wrote: > On 9/1/21 3:24 AM, Yu Zhang wrote: >> On Tue, Aug 31, 2021 at 09:53:27PM -0700, Andy Lutomirski wrote: >>> >>> >>> On Thu, Aug 26, 2021, at 7:31 PM, Yu Zhang wrote: >>>> On Thu, Aug 26, 2021 at 12:15:48PM +0200, David Hildenbrand wrote: >>> >>>> Thanks a lot for this summary. A question about the requirement: do we or >>>> do we not have plan to support assigned device to the protected VM? >>>> >>>> If yes. The fd based solution may need change the VFIO interface as well( >>>> though the fake swap entry solution need mess with VFIO too). Because: >>>> >>>> 1> KVM uses VFIO when assigning devices into a VM. >>>> >>>> 2> Not knowing which GPA ranges may be used by the VM as DMA buffer, all >>>> guest pages will have to be mapped in host IOMMU page table to host pages, >>>> which are pinned during the whole life cycle fo the VM. >>>> >>>> 3> IOMMU mapping is done during VM creation time by VFIO and IOMMU driver, >>>> in vfio_dma_do_map(). >>>> >>>> 4> However, vfio_dma_do_map() needs the HVA to perform a GUP to get the HPA >>>> and pin the page. >>>> >>>> But if we are using fd based solution, not every GPA can have a HVA, thus >>>> the current VFIO interface to map and pin the GPA(IOVA) wont work. And I >>>> doubt if VFIO can be modified to support this easily. >>>> >>>> >>> >>> Do you mean assigning a normal device to a protected VM or a hypothetical protected-MMIO device? >>> >>> If the former, it should work more or less like with a non-protected VM. mmap the VFIO device, set up a memslot, and use it. I'm not sure whether anyone will actually do this, but it should be possible, at least in principle. Maybe someone will want to assign a NIC to a TDX guest. An NVMe device with the understanding that the guest can't trust it wouldn't be entirely crazy ether. >>> >>> If the latter, AFAIK there is no spec for how it would work even in principle. Presumably it wouldn't work quite like VFIO -- instead, the kernel could have a protection-virtual-io-fd mechanism, and that fd could be bound to a memslot in whatever way we settle on for binding secure memory to a memslot. >>> >> >> Thanks Andy. I was asking the first scenario. >> >> Well, I agree it is doable if someone really want some assigned >> device in TD guest. As Kevin mentioned in his reply, HPA can be >> generated, by extending VFIO with a new mapping protocol which >> uses fd+offset, instead of HVA. > > I'm confused. I don't see why any new code is needed for this at all. > Every proposal I've seen for handling TDX memory continues to handle TDX > *shared* memory exactly like regular guest memory today. The only > differences are that more hole punching will be needed, which will > require lightweight memslots (to have many of them), memslots with > holes, or mappings backing memslots with holes (which can be done with > munmap() on current kernels). > > So you can literally just mmap a VFIO device and expect it to work, > exactly like it does right now. Whether the guest will be willing to > use the device will depend on the guest security policy (all kinds of > patches about that are flying around), but if the guest tries to use it, > it really should just work. ... but if you end up mapping private memory into IOMMU of the device and the device ends up accessing that memory, we're in the same position that the host might get capped, just like access from user space, no? Sure, you can map only the complete duplicate shared-memory region into the IOMMU of the device, that would work. Shame vfio mostly always pins all guest memory and you essentially cannot punch holes into the shared memory anymore -- resulting in the worst case in a duplicate memory consumption for your VM. So you'd actually want to map only the *currently* shared pieces into the IOMMU and update the mappings on demand. Having worked on something related, I can only say that 64k individual mappings, and not being able to modify existing mappings except completely deleting them to replace with something new (!atomic), can be quite an issue for bigger VMs. -- Thanks, David / dhildenb