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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E691CC7EE23 for ; Fri, 12 May 2023 18:02:07 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S238253AbjELSCE (ORCPT ); Fri, 12 May 2023 14:02:04 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52406 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S238296AbjELSBw (ORCPT ); Fri, 12 May 2023 14:01:52 -0400 Received: from mail-pf1-x44a.google.com (mail-pf1-x44a.google.com [IPv6:2607:f8b0:4864:20::44a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 625ED9023 for ; Fri, 12 May 2023 11:01:12 -0700 (PDT) Received: by mail-pf1-x44a.google.com with SMTP id d2e1a72fcca58-643fdfb437aso36782217b3a.0 for ; Fri, 12 May 2023 11:01:12 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1683914472; x=1686506472; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=Z/RyvdhRSuvZsZhvfHUuTowaban3YDaGtW+c8hQBLj0=; b=IJUktVCGqDdARLcamFDMEGSu8Z3sEjpHgueFS689K9Sln+F1ZGXbN/RSV+LQ3Dw8iH 1POCGYUyEgMXCbp1UoBZNPItAbqDglVKm5aWwVriowPVm3/AMSe0QeSQn2CfIgWFYpM+ rwDcXZo/18l663XXEP4qehDysj6U7SqreJc/HM5j4K6KgrZgxyOQtItkOmKecWap8eKL k8bFwDCsTjB9oGGwIz4EDUpqhwcPoF9TUPFHWbmu7gbNxjoriwITGY3+xm/TogF/AYcM 0kr1jp+FwqqLYYG/ing9cEa7NxSFX9x9t0wBcM+quZH+7NQkWvDzvrTnimYk6JemPdEN g/vA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683914472; x=1686506472; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Z/RyvdhRSuvZsZhvfHUuTowaban3YDaGtW+c8hQBLj0=; b=bpDqjn5px6xDE0ZDB2pg5RVyRwirqMBmsWdVc7Djafe8s/kcaOgGxVWBrZebRniRnC 0WNcB+Yqwo5wYXWC4gpYgfLQmuO7IzUUYNY5iFiLaJ7LSYoXGiqGIb0JARR9mwx9VRhY pQjkZIiJQvBhoROggpztcphwNztZS1Jslz8Hvyi7/7OKlf4Uz7a0EOjfJUGcwyGOb61t kmI5g28suwl/wJ78XFBi6QrfLxA+Z4OJztj9O5fky06l/Wq0QvfzVfa2E/ZMz8uakx37 /GxWCysX4ktpmlY7OGhfxCbE+ZYp7KfimB18NTQTyaExNjEmUzQOJ7RwDCIiFxo3YdWT qIVw== X-Gm-Message-State: AC+VfDwG6K/u7GkVatvoDhgD0frCGDT4/OD0SGCpqzv5zXKBk5xlhJ7B mpX79YBvDy7p2kmVBd4dZDxDiWk0rIE= X-Google-Smtp-Source: ACHHUZ6oEKN9QtM/X1xSAjt1bTyG5mjGAb2SF6yxjS3NMaTWMLy0GKhsobVe8roxJleXWQIy9F0G7Q5iU20= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a17:903:288d:b0:1ad:b459:a7e8 with SMTP id ku13-20020a170903288d00b001adb459a7e8mr2366446plb.1.1683914471769; Fri, 12 May 2023 11:01:11 -0700 (PDT) Date: Fri, 12 May 2023 11:01:10 -0700 In-Reply-To: <20230512002124.3sap3kzxpegwj3n2@amd.com> Mime-Version: 1.0 References: <658018f9-581c-7786-795a-85227c712be0@redhat.com> <1ed06a62-05a1-ebe6-7ac4-5b35ba272d13@redhat.com> <9efef45f-e9f4-18d1-0120-f0fc0961761c@redhat.com> <5869f50f-0858-ab0c-9049-4345abcf5641@redhat.com> <20230512002124.3sap3kzxpegwj3n2@amd.com> Message-ID: Subject: Re: Rename restrictedmem => guardedmem? (was: Re: [PATCH v10 0/9] KVM: mm: fd-based approach for supporting KVM) From: Sean Christopherson To: Michael Roth Cc: David Hildenbrand , Chao Peng , Paolo Bonzini , Vitaly Kuznetsov , Jim Mattson , Joerg Roedel , "Maciej S . Szmigiero" , Vlastimil Babka , Vishal Annapurve , Yu Zhang , "Kirill A . Shutemov" , dhildenb@redhat.com, Quentin Perret , tabba@google.com, wei.w.wang@intel.com, Mike Rapoport , Liam Merwick , Isaku Yamahata , Jarkko Sakkinen , Ackerley Tng , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, Hugh Dickins , Christian Brauner Content-Type: text/plain; charset="us-ascii" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, May 11, 2023, Michael Roth wrote: > On Fri, Apr 21, 2023 at 06:33:26PM -0700, Sean Christopherson wrote: > > > > Code is available here if folks want to take a look before any kind of formal > > posting: > > > > https://github.com/sean-jc/linux.git x86/kvm_gmem_solo > > Hi Sean, > > I've been working on getting the SNP patches ported to this but I'm having > some trouble working out a reasonable scheme for how to work the > RMPUPDATE hooks into the proposed design. > > One of the main things is kvm_gmem_punch_hole(): this is can free pages > back to the host whenever userspace feels like it. Pages that are still > marked private in the RMP table will blow up the host if they aren't returned > to the normal state before handing them back to the kernel. So I'm trying to > add a hook, orchestrated by kvm_arch_gmem_invalidate(), to handle that, > e.g.: > > static long kvm_gmem_punch_hole(struct file *file, int mode, loff_t offset, > loff_t len) > { > struct kvm_gmem *gmem = file->private_data; > pgoff_t start = offset >> PAGE_SHIFT; > pgoff_t end = (offset + len) >> PAGE_SHIFT; > struct kvm *kvm = gmem->kvm; > > /* > * Bindings must stable across invalidation to ensure the start+end > * are balanced. > */ > filemap_invalidate_lock(file->f_mapping); > kvm_gmem_invalidate_begin(kvm, gmem, start, end); > > /* Handle arch-specific cleanups before releasing pages */ > kvm_arch_gmem_invalidate(kvm, gmem, start, end); > truncate_inode_pages_range(file->f_mapping, offset, offset + len); > > kvm_gmem_invalidate_end(kvm, gmem, start, end); > filemap_invalidate_unlock(file->f_mapping); > > return 0; > } > > But there's another hook, kvm_arch_gmem_set_mem_attributes(), needed to put > the page in its intended state in the RMP table prior to mapping it into the > guest's NPT. IMO, this approach is wrong. kvm->mem_attr_array is the source of truth for whether userspace wants _guest_ physical pages mapped private vs. shared, but the attributes array has zero insight into the _host_ physical pages. I.e. SNP shouldn't hook kvm_mem_attrs_changed(), because operating on the RMP from that code is fundamentally wrong. A good analogy is moving a memslot (ignoring that AFAIK no VMM actually moves memslots, but it's a good analogy for KVM internals). KVM needs to zap all mappings for the old memslot gfn, but KVM does not create mappings for the new memslot gfn. Same for changing attributes; unmap, but never map. As for the unmapping side of things, kvm_unmap_gfn_range() will unmap all relevant NPT entries, and the elevated mmu_invalidate_in_progress will prevent KVM from establishing a new NPT mapping. And mmu_invalidate_in_progress will reach '0' only after both truncation _and_ kvm_vm_ioctl_set_mem_attributes() complete, i.e. KVM can create new mappings only when both kvm->mem_attr_array and any relevant guest_mem bindings have reached steady state. That leaves the question of when/where to do RMP updates. Off the cuff, I think RMP updates (and I _think_ also TDX page conversions) should _always_ be done in the context of either (a) file truncation (make host owned due, a.k.a. TDX reclaim) or (b) allocating a new page/folio in guest_mem, a.k.a. kvm_gmem_get_folio(). Under the hood, even though the gfn is the same, the backing pfn is different, i.e. installing a shared mapping should _never_ need to touch the RMP because pages common from the normal (non-guest_mem) pool must already be host owned. > Currently I'm calling that hook via kvm_vm_ioctl_set_mem_attributes(), just > after kvm->mem_attr_array is updated based on the ioctl. The reasoning there > is that KVM MMU can then rely on the existing mmu_invalidate_seq logic to > ensure both the state in the mem_attr_array and the RMP table are in sync and > up-to-date once MMU lock is acquired and MMU is ready to map it, or retry > #NPF otherwise. > > But for kvm_gmem_punch_hole(), kvm_vm_ioctl_set_mem_attributes() can potentially > result in something like the following sequence if I implement things as above: > > CPU0: kvm_gmem_punch_hole(): > kvm_gmem_invalidate_begin() > kvm_arch_gmem_invalidate() // set pages to default/shared state in RMP table before free'ing > CPU1: kvm_vm_ioctl_set_mem_attributes(): > kvm_arch_gmem_set_mem_attributes() // maliciously set pages to private in RMP table > CPU0: truncate_inode_pages_range() // HOST BLOWS UP TOUCHING PRIVATE PAGES > kvm_arch_gmem_invalidate_end() > > One quick and lazy solution is to rely on the fact that > kvm_vm_ioctl_set_mem_attributes() holds the kvm->slots_lock throughout the > entire begin()/end() portion of the invalidation sequence, and to similarly > hold the kvm->slots_lock throughout the begin()/end() sequence in > kvm_gmem_punch_hole() to prevent any interleaving. > > But I'd imagine overloading kvm->slots_lock is not the proper approach. But > would introducing a similar mutex to keep these operations grouped/atomic be > a reasonable approach to you, or should we be doing something else entirely > here? > > Keep in mind that RMP updates can't be done while holding KVM->mmu_lock > spinlock, because we also need to unmap pages from the directmap, which can > lead to scheduling-while-atomic BUG()s[1], so that's another constraint we > need to work around. > > Thanks! > > -Mike > > [1] https://lore.kernel.org/linux-coco/20221214194056.161492-7-michael.roth@amd.com/T/#m45a1af063aa5ac0b9314d6a7d46eecb1253bba7a > > > > > [1] https://lore.kernel.org/all/ff5c5b97-acdf-9745-ebe5-c6609dd6322e@google.com > > [2] https://lore.kernel.org/all/20230418-anfallen-irdisch-6993a61be10b@brauner > > [3] https://lore.kernel.org/linux-mm/20200522125214.31348-1-kirill.shutemov@linux.intel.com