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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2C272C433FE for ; Wed, 16 Nov 2022 05:05:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3A6488E0001; Wed, 16 Nov 2022 00:05:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 356286B0072; Wed, 16 Nov 2022 00:05:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1F76C8E0001; Wed, 16 Nov 2022 00:05:02 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 0CF596B0071 for ; Wed, 16 Nov 2022 00:05:02 -0500 (EST) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id D3CAE1403CA for ; Wed, 16 Nov 2022 05:05:01 +0000 (UTC) X-FDA: 80138116002.07.E8BE1DD Received: from mga12.intel.com (mga12.intel.com [192.55.52.136]) by imf10.hostedemail.com (Postfix) with ESMTP id 187FFC0004 for ; Wed, 16 Nov 2022 05:04:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1668575100; x=1700111100; h=date:from:to:cc:subject:message-id:reply-to:references: mime-version:content-transfer-encoding:in-reply-to; bh=foUP09rbOFmy8/q+NFuW/TBTGS2xITtwfC1ci5ozoPE=; b=mCChdka7D0DMDZXTc912yBKDA8nZVXoL6sEuxtPjKB22G8Rxg6sbpuWk 5Qx4iWQU/WdnqADeAg2Vircx5K7/poJoqD8SYQBcnybpqZAix+U6pMSGy ItLT4nBaiJlpg0pgWVXLHm3gFQ8rlECPJzv2g8dNd+dOZH/9mpXz/xHH9 eS+rMewXQvXmmnPzfH7Ar4fMsNwpO6RXVEy4d6OnzMlv3J+stbHK1SURJ 8JSgKJ401h+qoB1yoVGWaCoyUqdQ1P2NoNwlX0Ve+n97vP2bl35uNqR4j g33JPPeZhp6diH6yz199klbMzgrGRP/pmuCDAygrcdltpoG3EdLkvYe0O w==; X-IronPort-AV: E=McAfee;i="6500,9779,10532"; a="292158285" X-IronPort-AV: E=Sophos;i="5.96,167,1665471600"; d="scan'208";a="292158285" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 15 Nov 2022 21:04:58 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10532"; a="708008436" X-IronPort-AV: E=Sophos;i="5.96,167,1665471600"; d="scan'208";a="708008436" Received: from chaop.bj.intel.com (HELO localhost) ([10.240.193.75]) by fmsmga004.fm.intel.com with ESMTP; 15 Nov 2022 21:04:47 -0800 Date: Wed, 16 Nov 2022 13:00:22 +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: <20221116050022.GC364614@chaop.bj.intel.com> Reply-To: Chao Peng References: <20221025151344.3784230-1-chao.p.peng@linux.intel.com> <87k03xbvkt.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: <87k03xbvkt.fsf@linaro.org> ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1668575100; h=from:from:sender:reply-to: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:dkim-signature; bh=Hp61ks/iUJ2Ii2481RLLUXXTqBSvccfSbMKS7OgbAJk=; b=lNAtpW5/6MP+WDHmHyFUxg8jaJMOrSF3/BgVK6IIgDD/4JQdVaMQN/Zf9d2bHAPszRMP6k iqumwpgB0vvfPWsYEP+4XfCgmkofNwng+nv08NLQBmwzkjZx1kzSWaxg+ervSQMM/i41uG qRU4tF1cxKWTcoQiguZM6h7CnOhMsWM= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=none ("invalid DKIM record") header.d=intel.com header.s=Intel header.b=mCChdka7; dmarc=fail reason="No valid SPF" header.from=intel.com (policy=none); spf=none (imf10.hostedemail.com: domain of chao.p.peng@linux.intel.com has no SPF policy when checking 192.55.52.136) smtp.mailfrom=chao.p.peng@linux.intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1668575100; a=rsa-sha256; cv=none; b=j6QwzHO09IVwWubKU6GwOOplUXqtAN82L5zprI5DCWA0Ih+fXQYb/7/QE6hRjDQVVxo8hS zUGpt/R1kDlj9CgLWGZ4y2qZYrOQsyHFJepSz6Ryc9VjkRLEG8yXHKgd9oR3y5rCF0jTa9 6T8HhidLXdx4z/uWmEH47HJ82UFHFVM= X-Rspam-User: X-Stat-Signature: 9iczyujorwkup3pd3i48udpm6xdjxj6j X-Rspamd-Queue-Id: 187FFC0004 Authentication-Results: imf10.hostedemail.com; dkim=none ("invalid DKIM record") header.d=intel.com header.s=Intel header.b=mCChdka7; dmarc=fail reason="No valid SPF" header.from=intel.com (policy=none); spf=none (imf10.hostedemail.com: domain of chao.p.peng@linux.intel.com has no SPF policy when checking 192.55.52.136) smtp.mailfrom=chao.p.peng@linux.intel.com X-Rspamd-Server: rspam07 X-HE-Tag: 1668575099-304036 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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? > > - 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. Chao > > > mm change > > ========= > > Introduces a new memfd_restricted system call which can create memory > > file that is restricted from userspace access via normal MMU operations > > like read(), write() or mmap() etc and the only way to use it is > > passing it to a third kernel module like KVM and relying on it to > > access the fd through the newly added restrictedmem kernel interface. > > The restrictedmem interface bridges the memory file subsystems > > (tmpfs/hugetlbfs etc) and their users (KVM in this case) and provides > > bi-directional communication between them. > > > > > > KVM change > > ========== > > Extends the KVM memslot to provide guest private (encrypted) memory from > > a fd. With this extension, a single memslot can maintain both private > > memory through private fd (restricted_fd/restricted_offset) and shared > > (unencrypted) memory through userspace mmaped host virtual address > > (userspace_addr). For a particular guest page, the corresponding page in > > KVM memslot can be only either private or shared and only one of the > > shared/private parts of the memslot is visible to guest. For how this > > new extension is used in QEMU, please refer to kvm_set_phys_mem() in > > below TDX-enabled QEMU repo. > > > > Introduces new KVM_EXIT_MEMORY_FAULT exit to allow userspace to get the > > chance on decision-making for shared <-> private memory conversion. The > > exit can be an implicit conversion in KVM page fault handler or an > > explicit conversion from guest OS. > > > > Extends existing SEV ioctls KVM_MEMORY_ENCRYPT_{UN,}REG_REGION to > > convert a guest page between private <-> shared. The data maintained in > > these ioctls tells the truth whether a guest page is private or shared > > and this information will be used in KVM page fault handler to decide > > whether the private or the shared part of the memslot is visible to > > guest. > > > > > -- > Alex Bennée