Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp3437720rwi; Wed, 12 Oct 2022 01:51:29 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6Z9PDBZ2n8FrTYmJ1OjBrbkmID5ZiDCEFQy/j3/8TVwqqGykbNI+c5+CAWo8wRqlbKt3GH X-Received: by 2002:a17:907:6288:b0:78d:ab30:c374 with SMTP id nd8-20020a170907628800b0078dab30c374mr12982070ejc.266.1665564688854; Wed, 12 Oct 2022 01:51:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665564688; cv=none; d=google.com; s=arc-20160816; b=F6wa4Fzi4BEsVsemYN/59p5QSClyo1tW9qQtLMLOAW4O+eKfg8/D4WlVK4Igy98UJG kZsfzWV2KeB6zIdHvtQpNxNPcsXvtukNbJsCEnzFIz1TEtQgmZTLqa9nleVPzpYm+6gN nKMNTb+GXVATzQUNd6vOduvr+AjfKVes90ZtQ5xnoeW6A5YS5N9MosypoKu8P9mPnEL3 8FowZFHZ+izWCely/SdznIvXu9wejih3tKXpFHMGG2A4xdxaDK9hJN0+shwARCqBzdSb pdS/cad9TXsvxR7Iyi12RPMNRj6TTJr9iPOqHFTkoWebFKPejcrtdxXn1vLYpzyFZj8k FluA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=UykTJ8CCHrVodFVhG+pHMmXqqKcWQVVQmCxVhpiykbc=; b=KKvyRwaI0JpmnlJ7j6WMDWP/xkE6iPCHd9twhM2QAvBD6PofYqggra1Kd4YLjk3DjI yoCozF75+1VSeE5sHH7WSmNA/GBWD7RzC/71BZ21U77aVmrBBwFXjkA69Akc7iJpYS66 1Tk7dfXnWZMF9LH5b7ttQA8PY5Y0ju9nz0uMzpbe4hV2d0Z60lF6FIOJ2eDsnOje294L N0IXaWW8j3RjdRV/a1s9VNAuXUGwy8dZU1gHSCbo0BO+oT1wOVb1d11GHabGjr0z6G+B yP377dgrIbQXnQlojf2ssC5HBygucW4ofjxsBengGRZ+ELtiFSbWhTzt7D644CutTmaM wrjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=kleJYJtM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id g8-20020a056402090800b004597671e0ddsi17005346edz.338.2022.10.12.01.51.02; Wed, 12 Oct 2022 01:51:28 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=kleJYJtM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229881AbiJLIOb (ORCPT + 99 others); Wed, 12 Oct 2022 04:14:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42082 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229603AbiJLIO3 (ORCPT ); Wed, 12 Oct 2022 04:14:29 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 88D4E9DDA7; Wed, 12 Oct 2022 01:14:28 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 1BF1D61473; Wed, 12 Oct 2022 08:14:28 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EF244C433D6; Wed, 12 Oct 2022 08:14:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1665562467; bh=LztmwvXdNCm1f+34a+megCcYhhpdDqS6aiTmicYdPF8=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=kleJYJtMEas4yS4RIXkTvXxxmFB47lcONJVV4sKjv+gcua2rPdtmCSedjkBabAc5b hURHMNdHWx7r4wowISznvsizi5Im+t49R31DXBrxGFTPbODdfTQ45XZDHhKYo+JvHL t6QsJW8EDjUc9rhZsiuHIKhUNbaOtRdEwyO+hSXYPGCmYEKpWNusDs75tgGv3i7DGB Cabm5X3AcR4lCdWi6S4ohFHrXaIkXFb5OiLHlboF0GIBH8p74FABaO+Qze8RI5WE1J AOOcOtKMetuPMcewlepYLN3mlBduTOUDyUr3WWYASoOrEnFMPOJaqW2LeRPKXF8rL8 D1OfYmz8/fNBA== Date: Wed, 12 Oct 2022 11:14:24 +0300 From: Jarkko Sakkinen To: Chao Peng Cc: Sean Christopherson , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-api@vger.kernel.org, linux-doc@vger.kernel.org, qemu-devel@nongnu.org, Paolo Bonzini , Jonathan Corbet , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H . Peter Anvin" , Hugh Dickins , Jeff Layton , "J . Bruce Fields" , Andrew Morton , Shuah Khan , Mike Rapoport , Steven Price , "Maciej S . Szmigiero" , Vlastimil Babka , Vishal Annapurve , Yu Zhang , "Kirill A . Shutemov" , luto@kernel.org, jun.nakajima@intel.com, dave.hansen@intel.com, ak@linux.intel.com, david@redhat.com, aarcange@redhat.com, ddutile@redhat.com, dhildenb@redhat.com, Quentin Perret , Michael Roth , mhocko@suse.com, Muchun Song , wei.w.wang@intel.com Subject: Re: [PATCH v8 2/8] KVM: Extend the memslot to support fd-based private memory Message-ID: References: <20220915142913.2213336-3-chao.p.peng@linux.intel.com> <20221010082507.GA3144879@chaop.bj.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20221010082507.GA3144879@chaop.bj.intel.com> X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Oct 10, 2022 at 04:25:07PM +0800, Chao Peng wrote: > On Sat, Oct 08, 2022 at 08:35:47PM +0300, Jarkko Sakkinen wrote: > > On Sat, Oct 08, 2022 at 07:15:17PM +0300, Jarkko Sakkinen wrote: > > > On Sat, Oct 08, 2022 at 12:54:32AM +0300, Jarkko Sakkinen wrote: > > > > On Fri, Oct 07, 2022 at 02:58:54PM +0000, Sean Christopherson wrote: > > > > > On Fri, Oct 07, 2022, Jarkko Sakkinen wrote: > > > > > > On Thu, Oct 06, 2022 at 03:34:58PM +0000, Sean Christopherson wrote: > > > > > > > On Thu, Oct 06, 2022, Jarkko Sakkinen wrote: > > > > > > > > On Thu, Oct 06, 2022 at 05:58:03PM +0300, Jarkko Sakkinen wrote: > > > > > > > > > On Thu, Sep 15, 2022 at 10:29:07PM +0800, Chao Peng wrote: > > > > > > > > > > This new extension, indicated by the new flag KVM_MEM_PRIVATE, adds two > > > > > > > > > > additional KVM memslot fields private_fd/private_offset to allow > > > > > > > > > > userspace to specify that guest private memory provided from the > > > > > > > > > > private_fd and guest_phys_addr mapped at the private_offset of the > > > > > > > > > > private_fd, spanning a range of memory_size. > > > > > > > > > > > > > > > > > > > > The extended memslot can still have the userspace_addr(hva). When use, a > > > > > > > > > > single memslot can maintain both private memory through private > > > > > > > > > > fd(private_fd/private_offset) and shared memory through > > > > > > > > > > hva(userspace_addr). Whether the private or shared part is visible to > > > > > > > > > > guest is maintained by other KVM code. > > > > > > > > > > > > > > > > > > What is anyway the appeal of private_offset field, instead of having just > > > > > > > > > 1:1 association between regions and files, i.e. one memfd per region? > > > > > > > > > > > > > > Modifying memslots is slow, both in KVM and in QEMU (not sure about Google's VMM). > > > > > > > E.g. if a vCPU converts a single page, it will be forced to wait until all other > > > > > > > vCPUs drop SRCU, which can have severe latency spikes, e.g. if KVM is faulting in > > > > > > > memory. KVM's memslot updates also hold a mutex for the entire duration of the > > > > > > > update, i.e. conversions on different vCPUs would be fully serialized, exacerbating > > > > > > > the SRCU problem. > > > > > > > > > > > > > > KVM also has historical baggage where it "needs" to zap _all_ SPTEs when any > > > > > > > memslot is deleted. > > > > > > > > > > > > > > Taking both a private_fd and a shared userspace address allows userspace to convert > > > > > > > between private and shared without having to manipulate memslots. > > > > > > > > > > > > Right, this was really good explanation, thank you. > > > > > > > > > > > > Still wondering could this possibly work (or not): > > > > > > > > > > > > 1. Union userspace_addr and private_fd. > > > > > > > > > > No, because userspace needs to be able to provide both userspace_addr (shared > > > > > memory) and private_fd (private memory) for a single memslot. > > > > > > > > Got it, thanks for clearing my misunderstandings on this topic, and it > > > > is quite obviously visible in 5/8 and 7/8. I.e. if I got it right, > > > > memblock can be partially private, and you dig the shared holes with > > > > KVM_MEMORY_ENCRYPT_UNREG_REGION. We have (in Enarx) ATM have memblock > > > > per host mmap, I was looking into this dilated by that mindset but makes > > > > definitely sense to support that. > > > > > > For me the most useful reference with this feature is kvm_set_phys_mem() > > > implementation in privmem-v8 branch. Took while to find it because I did > > > not have much experience with QEMU code base. I'd even recommend to mention > > > that function in the cover letter because it is really good reference on > > > how this feature is supposed to be used. > > That's a good point, I can mention that if people find useful. Yeah, I did implementation for Enarx (https://www.enarx.dev/) using just that part as a reference. It has all the essentials what you need to consider when you are already using KVM API, and want to add private regions. BR, Jarkko