Received: by 2002:a05:7412:b795:b0:e2:908c:2ebd with SMTP id iv21csp4231rdb; Wed, 1 Nov 2023 14:56:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGtToBJR7gTQ9f45FMoVfV7LwYXyENXbRAoXS06lfeYhXFXTNt5N7Aq1bDOQq8dswJE3IhB X-Received: by 2002:a17:907:1c1a:b0:9ba:33ef:fe4c with SMTP id nc26-20020a1709071c1a00b009ba33effe4cmr2986008ejc.64.1698875814808; Wed, 01 Nov 2023 14:56:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698875814; cv=none; d=google.com; s=arc-20160816; b=HAv62zkJN7GcN6IlA3LPuHovLTb0LjkmPB5zuJ85oq7Llalxns+KxL3e61eML5j2V2 c4Rgak946E9QOpRNUaMDx975oSgzT1/moo25XI4PMpmsg6CJvoXfGE8Fg4atj/f+C7tl wgyYluF8D+QGYKEUWyorYUq+GYDu2fRewFP1fkEpUuaVrN9of16BdxZiSVBW44fSm+Os mY03dw6NY73iprhCZgZgXXdDObC2wwi1YApKtxsodnB419zpjAIcTNjJYsuVZUdXw6Lj g91r/+cLs0SACcSFQAWtDHtVIC22/btQPyGbl/yLnqO7IEZc2U4GAEVEXcpkZVr/ajJJ v9LA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=PcqXHpLnYUoCfOstmB/gusaSnz1wSaaIZvticgi9YrM=; fh=7qfmZqbAAQZvvB1jb24P2S6ewyTKmKV8ZEBWKlHGLY0=; b=tWbvZ+xt+l5rQwDe5PhHXgThnMCBIRCLURqtJwWaEnh1Xq4D6ookHz5LchyPinFX9v 0ZTysP8GKZ8NxfYxkOXBgTn0VdWDYZKIeZrcySyjylEOz8BZbDUbEAY82ibPSK3IuLDB chGc/fH9lQ1seqLHKZVw+j1TbuxZ24r11YegAH3y0l81xbkZhQGUpSnYW7vwtFbAqnhN llocFxht8E+QKWc0lpIHcfFpLNLAsa13X6RQP3JMCvlkbWzyBLVAGzwqRfPYdy9dE3Me hcedujoN83oK8pIfhHKYLROykstFrNIA4j6m/6efvS2Sk7yE4RFqUworzf62hkhWgjwJ tEIg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="yordn67/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from fry.vger.email (fry.vger.email. [23.128.96.38]) by mx.google.com with ESMTPS id gs7-20020a1709072d0700b009a5e55d5824si298964ejc.145.2023.11.01.14.56.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 01 Nov 2023 14:56:54 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) client-ip=23.128.96.38; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b="yordn67/"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.38 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by fry.vger.email (Postfix) with ESMTP id 22A8A808BD21; Wed, 1 Nov 2023 14:56:11 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at fry.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229445AbjKAVzx (ORCPT + 99 others); Wed, 1 Nov 2023 17:55:53 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54674 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232848AbjKAVzw (ORCPT ); Wed, 1 Nov 2023 17:55:52 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 275AD125 for ; Wed, 1 Nov 2023 14:55:49 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id 3f1490d57ef6-da033914f7cso315870276.0 for ; Wed, 01 Nov 2023 14:55:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698875748; x=1699480548; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=PcqXHpLnYUoCfOstmB/gusaSnz1wSaaIZvticgi9YrM=; b=yordn67/B9coJ27wikY+cNB1RREF7cB4b3Ue4KVs4bUxR7NMszr2uB6DsTcvRFezm4 Mv9vIV4D1zL7Ebq8eYUvDJaTsA+b6DbfwYGOyfYZ+gn6JBg2zh32vssNDt0PgQWyi3Hc V30xoKLPHpSqs3H12W5kHbw59Cp3oq89IhbOXGRukrdOHcIips6mw6fvttACqcV26Iao o3OJKk/xvzHC16DLzD0pyWC3byA8iAor+alj9Jl/Ez2piZBXn5OAsYe77vVBVgD3KaOP Uo0at9aIB4srFmOFz36vXvyhDqsKT+KVB/6JAS3Kq2OtaQ1RroksSDGcgJr8iz7Y9trj xkLg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698875748; x=1699480548; 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=PcqXHpLnYUoCfOstmB/gusaSnz1wSaaIZvticgi9YrM=; b=WZGaDetqEicFnkQ5GkckZ1dCqMHmSbafYzFKJI7RuQbiBgvFMQnPFMs9bcTxnIwjg3 GYYoq+smNu9l9Mmd/4sBRzAzusLGJoKNgMuQjaS/fgYhxY5qJViXz1vkHbAKkkqGpRxb dZgtCkrNjMSxMjBjQLfOC8LeHis0tZJ1/ia05eOEY56Oo1Q6ITmeS75CHi+LF6hgFY6O WP0ByBzm1/5wBI1zEg6YqQBGp0eG9dMka8jKG64wsmQSHtBq2WcpdGCGtoomkWgIS6aW pJ7KzypS9F89Rx9b/uGt2XHyqS1ReJVeSdCzSjjgMv+/Kja913w2WcKbglpTgTOoi4Bz ZhXA== X-Gm-Message-State: AOJu0YwojN7UfsE5KcYxcoVOv/AYdFuJ5qibsilSZDMmuLUN38VWocss DFEB2HCy+Ep+ViOPsJcJDIUgjMJ7ugg= X-Received: from zagreus.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:5c37]) (user=seanjc job=sendgmr) by 2002:a05:6902:1083:b0:d9a:c3b8:4274 with SMTP id v3-20020a056902108300b00d9ac3b84274mr405001ybu.7.1698875748263; Wed, 01 Nov 2023 14:55:48 -0700 (PDT) Date: Wed, 1 Nov 2023 14:55:46 -0700 In-Reply-To: Mime-Version: 1.0 References: <20231027182217.3615211-1-seanjc@google.com> <20231027182217.3615211-17-seanjc@google.com> Message-ID: Subject: Re: [PATCH v13 16/35] KVM: Add KVM_CREATE_GUEST_MEMFD ioctl() for guest-specific backing memory From: Sean Christopherson To: Fuad Tabba Cc: Paolo Bonzini , Marc Zyngier , Oliver Upton , Huacai Chen , Michael Ellerman , Anup Patel , Paul Walmsley , Palmer Dabbelt , Albert Ou , Alexander Viro , Christian Brauner , "Matthew Wilcox (Oracle)" , Andrew Morton , kvm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, kvmarm@lists.linux.dev, linux-mips@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, kvm-riscv@lists.infradead.org, linux-riscv@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Xiaoyao Li , Xu Yilun , Chao Peng , Jarkko Sakkinen , Anish Moorthy , David Matlack , Yu Zhang , Isaku Yamahata , "=?utf-8?Q?Micka=C3=ABl_Sala=C3=BCn?=" , Vlastimil Babka , Vishal Annapurve , Ackerley Tng , Maciej Szmigiero , David Hildenbrand , Quentin Perret , Michael Roth , Wang , Liam Merwick , Isaku Yamahata , "Kirill A . Shutemov" Content-Type: text/plain; charset="us-ascii" X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE, USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on fry.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (fry.vger.email [0.0.0.0]); Wed, 01 Nov 2023 14:56:11 -0700 (PDT) On Wed, Nov 01, 2023, Fuad Tabba wrote: > > > > @@ -1034,6 +1034,9 @@ static void kvm_destroy_dirty_bitmap(struct kvm_memory_slot *memslot) > > > > /* This does not remove the slot from struct kvm_memslots data structures */ > > > > static void kvm_free_memslot(struct kvm *kvm, struct kvm_memory_slot *slot) > > > > { > > > > + if (slot->flags & KVM_MEM_PRIVATE) > > > > + kvm_gmem_unbind(slot); > > > > + > > > > > > Should this be called after kvm_arch_free_memslot()? Arch-specific ode > > > might need some of the data before the unbinding, something I thought > > > might be necessary at one point for the pKVM port when deleting a > > > memslot, but realized later that kvm_invalidate_memslot() -> > > > kvm_arch_guest_memory_reclaimed() was the more logical place for it. > > > Also, since that seems to be the pattern for arch-specific handlers in > > > KVM. > > > > Maybe? But only if we can about symmetry between the allocation and free paths > > I really don't think kvm_arch_free_memslot() should be doing anything beyond a > > "pure" free. E.g. kvm_arch_free_memslot() is also called after moving a memslot, > > which hopefully we never actually have to allow for guest_memfd, but any code in > > kvm_arch_free_memslot() would bring about "what if" questions regarding memslot > > movement. I.e. the API is intended to be a "free arch metadata associated with > > the memslot". > > > > Out of curiosity, what does pKVM need to do at kvm_arch_guest_memory_reclaimed()? > > It's about the host reclaiming ownership of guest memory when tearing > down a protected guest. In pKVM, we currently teardown the guest and > reclaim its memory when kvm_arch_destroy_vm() is called. The problem > with guestmem is that kvm_gmem_unbind() could get called before that > happens, after which the host might try to access the unbound guest > memory. Since the host hasn't reclaimed ownership of the guest memory > from hyp, hilarity ensues (it crashes). > > Initially, I hooked reclaim guest memory to kvm_free_memslot(), but > then I needed to move the unbind later in the function. I realized > later that kvm_arch_guest_memory_reclaimed() gets called earlier (at > the right time), and is more aptly named. Aha! I suspected that might be the case. TDX and SNP also need to solve the same problem of "reclaiming" memory before it can be safely accessed by the host. The plan is to add an arch hook (or two?) into guest_memfd that is invoked when memory is freed from guest_memfd. Hooking kvm_arch_guest_memory_reclaimed() isn't completely correct as deleting a memslot doesn't *guarantee* that guest memory is actually reclaimed (which reminds me, we need to figure out a better name for that thing before introducing kvm_arch_gmem_invalidate()). The effective false positives aren't fatal for the current usage because the hook is used only for x86 SEV guests to flush caches. An unnecessary flush can cause performance issues, but it doesn't affect correctness. For TDX and SNP, and IIUC pKVM, false positives are fatal because KVM could assign memory back to the host that is still owned by guest_memfd. E.g. a misbehaving userspace could prematurely delete a memslot. And the more fun example is intrahost migration, where the plan is to allow pointing multiple guest_memfd files at a single guest_memfd inode: https://lore.kernel.org/all/cover.1691446946.git.ackerleytng@google.com There was a lot of discussion for this, but it's scattered all over the place. The TL;DR is is that the inode will represent physical memory, and a file will represent a given "struct kvm" instance's view of that memory. And so the memory isn't reclaimed until the inode is truncated/punched. I _think_ this reflects the most recent plan from the guest_memfd side: https://lore.kernel.org/all/1233d749211c08d51f9ca5d427938d47f008af1f.1689893403.git.isaku.yamahata@intel.com