Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2109524rwb; Thu, 17 Nov 2022 06:42:02 -0800 (PST) X-Google-Smtp-Source: AA0mqf746TjXs6NMIlNZqwFrrY9H8gf06D2gwhW4YsXIMrfO+3V3odX1omCqQ2o1kK+iG1rOb8eA X-Received: by 2002:a17:906:810:b0:7ad:d7f9:1f96 with SMTP id e16-20020a170906081000b007add7f91f96mr2315729ejd.384.1668696121948; Thu, 17 Nov 2022 06:42:01 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668696121; cv=none; d=google.com; s=arc-20160816; b=shnN1O0QqwYqFrNoClM1WezFX79kJ+iSqkjZZizVK92sz1MKUITIXLdnyF8jfHsnJZ /LSKS9oY7J0FVQS9lZkS6PEYwfZO341YqDtl5iAEAGzhek9bisZRDK69zURNsvwLqEx/ QPsVECxTyfBw0vg+QL5PLusHhZGweEZEcdZ43aXztoLzJxb7IHCEnt2e9walPGU0FQng /2zf040douSCP2v4kY63jcIQ1J7TYo334CxVWx3NUkb2sIdEPYJUT8S+Q1gS7KtUU+s1 0hOhkbQX7mGcn+PsMXAqRqA62qPQ+TH5dXBEki/hn0bruTi75xBbK5klVCYZY46IOta/ RWZw== 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-transfer-encoding :content-disposition:mime-version:references:reply-to:message-id :subject:cc:to:from:date:dkim-signature; bh=hUUiueIZnxwzjomZNHqeEP2tdN5I90IW67dgTtIO9IY=; b=ZNpXrVBYRc+xPan3kpUSLoV1Pal9td+m8tMGvJnLOzffr7QOCOykql2i9PJ9gEyaFY w3frRtLCwi/NQaugm+dWUEtVIULldEz0Z4EZ5s56pW5e42/PBjVKIKOfNeqaB0HV1WVp YILQr+beKNXRGQRXP30Mr543lG/UM5eMMMZWZiXJ/XzvaH+RYRXRqu6WxMQCgTfktrlI Rp4zljPy/2RVMajx0ejf0Wm6Ix3CfSkR0pkzTMOv8Z9LGHHlgefAXGFkea+j3td3nmC6 RlTt0nZ7OFLfW3qcrftz0C2OR8EtHapJlLUeZ16hr/7fbxfxew1zwV7ms0W7JKhF8y9m +ggg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=NNK2xzoK; 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=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p17-20020a056402501100b00460ce6c3a1asi993630eda.454.2022.11.17.06.41.39; Thu, 17 Nov 2022 06:42:01 -0800 (PST) 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=@intel.com header.s=Intel header.b=NNK2xzoK; 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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240220AbiKQOVh (ORCPT + 93 others); Thu, 17 Nov 2022 09:21:37 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51196 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240373AbiKQOVd (ORCPT ); Thu, 17 Nov 2022 09:21:33 -0500 Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0F6EC77225; Thu, 17 Nov 2022 06:21:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1668694890; x=1700230890; h=date:from:to:cc:subject:message-id:reply-to:references: mime-version:content-transfer-encoding:in-reply-to; bh=QAS2h8V2dfght1SJU/Na0JcJSN/AEZSVl08JRBsGjdI=; b=NNK2xzoKjwI6qrmzmGmeKxLrNmv1MfC5ADjD6m8RHOc5qInpX8GUo6Kc DGxFqUcjxrFSxNgjlG6pAFy+WDmFjandV5TQM4N6SsjF0ito1788+EDl+ H5+XYRvEc5yS8+cPIzx8zzTd3zL73kJ1dSrkXXa/Zs6DUma1Ls4bUAUH8 7OPnFgL4uM5NiL++90N5G2kik3Z8LhOB4Ee65e3421zTIjyEi7ViQHN+V qUmBIlieRBS0vf/6d1u/PWIq6fQo/FdDSlVTcNrbhFh8rjJeNTtnJJ1ZW 0vusI+e60KUZESmSxqDOLI5XdLgX6B/nwtsIM49grAuhoJJhuZ8f1gd25 g==; X-IronPort-AV: E=McAfee;i="6500,9779,10534"; a="312874347" X-IronPort-AV: E=Sophos;i="5.96,171,1665471600"; d="scan'208";a="312874347" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Nov 2022 06:21:29 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10534"; a="634066426" X-IronPort-AV: E=Sophos;i="5.96,171,1665471600"; d="scan'208";a="634066426" Received: from chaop.bj.intel.com (HELO localhost) ([10.240.193.75]) by orsmga007.jf.intel.com with ESMTP; 17 Nov 2022 06:21:17 -0800 Date: Thu, 17 Nov 2022 22:16:53 +0800 From: Chao Peng To: Alex =?iso-8859-1?Q?Benn=E9e?= Cc: kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, linux-doc@vger.kernel.org, qemu-devel@nongnu.org, Paolo Bonzini , Jonathan Corbet , Sean Christopherson , 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 , tabba@google.com, Michael Roth , mhocko@suse.com, Muchun Song , wei.w.wang@intel.com, Viresh Kumar , Mathieu Poirier , AKASHI Takahiro Subject: Re: [PATCH v9 0/8] KVM: mm: fd-based approach for supporting KVM Message-ID: <20221117141653.GE422408@chaop.bj.intel.com> Reply-To: Chao Peng References: <20221025151344.3784230-1-chao.p.peng@linux.intel.com> <87k03xbvkt.fsf@linaro.org> <20221116050022.GC364614@chaop.bj.intel.com> <87v8nf8bte.fsf@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <87v8nf8bte.fsf@linaro.org> X-Spam-Status: No, score=-7.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_HI,SPF_HELO_NONE, SPF_NONE 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 Wed, Nov 16, 2022 at 09:40:23AM +0000, Alex Benn?e wrote: > > Chao Peng writes: > > > On Mon, Nov 14, 2022 at 11:43:37AM +0000, Alex Benn?e wrote: > >> > >> Chao Peng writes: > >> > >> > >> > Introduction > >> > ============ > >> > KVM userspace being able to crash the host is horrible. Under current > >> > KVM architecture, all guest memory is inherently accessible from KVM > >> > userspace and is exposed to the mentioned crash issue. The goal of this > >> > series is to provide a solution to align mm and KVM, on a userspace > >> > inaccessible approach of exposing guest memory. > >> > > >> > Normally, KVM populates secondary page table (e.g. EPT) by using a host > >> > virtual address (hva) from core mm page table (e.g. x86 userspace page > >> > table). This requires guest memory being mmaped into KVM userspace, but > >> > this is also the source where the mentioned crash issue can happen. In > >> > theory, apart from those 'shared' memory for device emulation etc, guest > >> > memory doesn't have to be mmaped into KVM userspace. > >> > > >> > This series introduces fd-based guest memory which will not be mmaped > >> > into KVM userspace. KVM populates secondary page table by using a > >> > fd/offset pair backed by a memory file system. The fd can be created > >> > from a supported memory filesystem like tmpfs/hugetlbfs and KVM can > >> > directly interact with them with newly introduced in-kernel interface, > >> > therefore remove the KVM userspace from the path of accessing/mmaping > >> > the guest memory. > >> > > >> > Kirill had a patch [2] to address the same issue in a different way. It > >> > tracks guest encrypted memory at the 'struct page' level and relies on > >> > HWPOISON to reject the userspace access. The patch has been discussed in > >> > several online and offline threads and resulted in a design document [3] > >> > which is also the original proposal for this series. Later this patch > >> > series evolved as more comments received in community but the major > >> > concepts in [3] still hold true so recommend reading. > >> > > >> > The patch series may also be useful for other usages, for example, pure > >> > software approach may use it to harden itself against unintentional > >> > access to guest memory. This series is designed with these usages in > >> > mind but doesn't have code directly support them and extension might be > >> > needed. > >> > >> There are a couple of additional use cases where having a consistent > >> memory interface with the kernel would be useful. > > > > Thanks very much for the info. But I'm not so confident that the current > > memfd_restricted() implementation can be useful for all these usages. > > > >> > >> - Xen DomU guests providing other domains with VirtIO backends > >> > >> Xen by default doesn't give other domains special access to a domains > >> memory. The guest can grant access to regions of its memory to other > >> domains for this purpose. > > > > I'm trying to form my understanding on how this could work and what's > > the benefit for a DomU guest to provide memory through memfd_restricted(). > > AFAICS, memfd_restricted() can help to hide the memory from DomU userspace, > > but I assume VirtIO backends are still in DomU uerspace and need access > > that memory, right? > > They need access to parts of the memory. At the moment you run your > VirtIO domains in the Dom0 and give them access to the whole of a DomU's > address space - however the Xen model is by default the guests memory is > inaccessible to other domains on the system. The DomU guest uses the Xen > grant model to expose portions of its address space to other domains - > namely for the VirtIO queues themselves and any pages containing buffers > involved in the VirtIO transaction. My thought was that looks like a > guest memory interface which is mostly inaccessible (private) with some > holes in it where memory is being explicitly shared with other domains. Yes, similar in conception. For KVM, memfd_restricted() is used by host OS, guest will issue conversion between private and shared for its memory range. This is similar to Xen DomU guest grants its memory to other domains. Similarly, I guess to make memfd_restricted() being really useful for Xen, it should be run on the VirtIO backend domain (e.g. equivalent to the host position for KVM). > > What I want to achieve is a common userspace API with defined semantics > for what happens when private and shared regions are accessed. Because > having each hypervisor/confidential computing architecture define its > own special API for accessing this memory is just a recipe for > fragmentation and makes sharing common VirtIO backends impossible. Yes, I agree. That's interesting to explore. > > > > >> > >> - pKVM on ARM > >> > >> Similar to Xen, pKVM moves the management of the page tables into the > >> hypervisor and again doesn't allow those domains to share memory by > >> default. > > > > Right, we already had some discussions on this in the past versions. > > > >> > >> - VirtIO loopback > >> > >> This allows for VirtIO devices for the host kernel to be serviced by > >> backends running in userspace. Obviously the memory userspace is > >> allowed to access is strictly limited to the buffers and queues > >> because giving userspace unrestricted access to the host kernel would > >> have consequences. > > > > Okay, but normal memfd_create() should work for it, right? And > > memfd_restricted() instead may not work as it unmaps the memory from > > userspace. > > > >> > >> All of these VirtIO backends work with vhost-user which uses memfds to > >> pass references to guest memory from the VMM to the backend > >> implementation. > > > > Sounds to me these are the places where normal memfd_create() can act on. > > VirtIO backends work on the mmap-ed memory which currently is not the > > case for memfd_restricted(). memfd_restricted() has different design > > purpose that unmaps the memory from userspace and employs some kernel > > callbacks so other kernel modules can make use of the memory with these > > callbacks instead of userspace virtual address. > > Maybe my understanding is backwards then. Are you saying a guest starts > with all its memory exposed and then selectively unmaps the private > regions? Is this driven by the VMM or the guest itself? For confidential computing usages, normally guest starts with all guest memory being private, e.g, cannot be accessed by host. The memory will be lived in memfd_restricted() memory and not exposed to host userspace VMM like QEMU. Guest then can selectively map its private sub regions (e.g. VirtIO queue in the guest VirtIO frontend driver) as shared so host backend driver in QEMU can see it. When this happens, new shared mapping will be established in KVM and the new memory will be provided from normal mmap-able memory, then QEMU can do whatever it can do for the device emulation. Thanks, Chao > > -- > Alex Benn?e