From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pf1-f202.google.com (mail-pf1-f202.google.com [209.85.210.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5B8D3274B2A for ; Thu, 21 May 2026 13:31:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779370277; cv=none; b=bOa99PBYBoukehDCC9n6gm/CFj66/rxgluBebKncTtZFg7jloBIs3CM2VApZOzszLCf6ch1r9EgUoG6+nMegig5j7+kmv60ckkrIP8pOX1cOo4kwyJEIFtZqDu2ozsUtlRsDpMgdojuBJsZIss3Po45jp6rUKn35ydaDJO04VI4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1779370277; c=relaxed/simple; bh=t+Zvw3OJCdd7aLvPU2Y07KNUlkbBJoLjXPqHeDhHkts=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=YmIsJso/86tYiSz4yrGhGS6AMhln1jeJLiCNUOml2CTWqi4sMpVMz2uJZ1ohdWd2zcHQrCrsgVUG+4Qro6BEj84ZvrptWwWaMbNlWOe0m4Z4R4umnNbEFpyn23oUHBjrogJ9P6zuEoMoojw5qvY3SrUfVn4JAEEp3/9SCiSr0xU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=fAaliF31; arc=none smtp.client-ip=209.85.210.202 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--seanjc.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="fAaliF31" Received: by mail-pf1-f202.google.com with SMTP id d2e1a72fcca58-83eefe4867eso3335135b3a.1 for ; Thu, 21 May 2026 06:31:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1779370276; x=1779975076; darn=lists.linux.dev; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=55Qyjr301rCPWrgG1oq6BJ9BiVpqhO1a4dUCIfsketY=; b=fAaliF31Jo8nO0eiT2kp0op1wb4/HyOUz7CXLMtupsZW1glCkjM284ONN95vQSwg94 JPzYeRC9AprDXN/dWKVfN32kDLPjv781fOgQfMP4e+26+QvMrJAonYPaHL9kuc0DLMeK E/zEEAHz8JAz8M6CModNLrMQZDzHkRSXoeIyO480hY1rjig4fLZ5r7TdThrwpkGO8Qsj nWIF5zExKGMMOqp+3TLHQ2jIZerkisfQgqXHL+xo6ORBl23emIh8R42mn4GjnhaX/Yrg VkLVOwb2cfwvacjYyHvegi5LXOj08vOwMQSr//hPX5Llv6ft5rAQMlp7ZRyQbSQ1BgfA p57w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1779370276; x=1779975076; 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=55Qyjr301rCPWrgG1oq6BJ9BiVpqhO1a4dUCIfsketY=; b=YsXyQRJm9nLpww2QbwzZ2DE9OPE9jd6bmytYvlKqZ47sxxXxfktdI9oZWYFnzG0ic7 L7EM7svxE1RYSXHSfWVUjqCmGkaNj0Qj9hWZv4el+QIMm01vWQu/6jsfa9xtnOvwd0dQ I4JscnrpE8U0AAwsGSuXK7M9D3ByKcsMfqgx+et37hL9u6OPiVZQe8Kbys2NhzyS8BY0 WfgetEpNDuXNnm4zIEMsLuOohF2Xr6dA0JVW4BIV9o8G4+3mgav7yCDxPv8lAiWpRcSv 4uxRRRbo2Ax8kPC7HgfWnCCF5n9yRslj5xR2wSY1+qtEPEUDuWVsanE9x30xUrSmQcgt rvlA== X-Forwarded-Encrypted: i=1; AFNElJ9l84r5AIuVnPenx1gTWTI9VdF0EKvpHOK7T+ms+cokrpu+xigUofXuRLmWoooAgzQuvDocjxGZ273P@lists.linux.dev X-Gm-Message-State: AOJu0YyzIYf+hY4lVkbhsZIhdRIgHNm2SOB+ctOnmxa8hsu1lIkwHC0O gmuUAm0sGtro9s6daGLxXlYSU+rKUss40GB89FSsJKs0INHQlyT1jyh7Pxfyu2DwnmBCu2ZcXQR 4wOyLPQ== X-Received: from pfblt3.prod.google.com ([2002:a05:6a00:7443:b0:839:65e2:e48f]) (user=seanjc job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:1494:b0:83f:2568:d469 with SMTP id d2e1a72fcca58-8414adfae19mr3245544b3a.32.1779370274841; Thu, 21 May 2026 06:31:14 -0700 (PDT) Date: Thu, 21 May 2026 06:31:14 -0700 In-Reply-To: Precedence: bulk X-Mailing-List: linux-coco@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260507-gmem-inplace-conversion-v6-0-91ab5a8b19a4@google.com> <20260507-gmem-inplace-conversion-v6-5-91ab5a8b19a4@google.com> Message-ID: Subject: Re: [PATCH v6 05/43] KVM: guest_memfd: Wire up kvm_get_memory_attributes() to per-gmem attributes From: Sean Christopherson To: Fuad Tabba Cc: Ackerley Tng , aik@amd.com, andrew.jones@linux.dev, binbin.wu@linux.intel.com, brauner@kernel.org, chao.p.peng@linux.intel.com, david@kernel.org, ira.weiny@intel.com, jmattson@google.com, jthoughton@google.com, michael.roth@amd.com, oupton@kernel.org, pankaj.gupta@amd.com, qperret@google.com, rick.p.edgecombe@intel.com, rientjes@google.com, shivankg@amd.com, steven.price@arm.com, willy@infradead.org, wyihan@google.com, yan.y.zhao@intel.com, forkloop@google.com, pratyush@kernel.org, suzuki.poulose@arm.com, aneesh.kumar@kernel.org, liam@infradead.org, Paolo Bonzini , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , Steven Rostedt , Masami Hiramatsu , Mathieu Desnoyers , Jonathan Corbet , Shuah Khan , Shuah Khan , Vishal Annapurve , Andrew Morton , Chris Li , Kairui Song , Kemeng Shi , Nhat Pham , Baoquan He , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , Youngjun Park , Qi Zheng , Shakeel Butt , Kiryl Shutsemau , Jason Gunthorpe , Vlastimil Babka , kvm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-trace-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mm@kvack.org, linux-coco@lists.linux.dev Content-Type: text/plain; charset="us-ascii" On Thu, May 21, 2026, Fuad Tabba wrote: > On Wed, 20 May 2026 at 22:44, Ackerley Tng wrote: > > > > Fuad Tabba writes: > > > > > > > > [...snip...] > > > > > >> +unsigned long kvm_gmem_get_memory_attributes(struct kvm *kvm, gfn_t gfn) > > >> +{ > > >> + struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn); > > >> + struct inode *inode; > > >> + > > >> + /* > > >> + * If this gfn has no associated memslot, there's no chance of the gfn > > >> + * being backed by private memory, since guest_memfd must be used for > > >> + * private memory, and guest_memfd must be associated with some memslot. > > >> + */ > > >> + if (!slot) > > >> + return 0; > > >> + > > >> + CLASS(gmem_get_file, file)(slot); > > >> + if (!file) > > >> + return 0; > > >> + > > >> + inode = file_inode(file); > > >> + > > >> + /* > > >> + * Rely on the maple tree's internal RCU lock to ensure a > > >> + * stable result. This result can become stale as soon as the > > >> + * lock is dropped, so the caller _must_ still protect > > >> + * consumption of private vs. shared by checking > > >> + * mmu_invalidate_retry_gfn() under mmu_lock to serialize > > >> + * against ongoing attribute updates. > > >> + */ > > >> + return kvm_gmem_get_attributes(inode, kvm_gmem_get_index(slot, gfn)); > > >> +} > > > > > > Doesn't this imply that all consumers of kvm_mem_is_private() should > > > validate the result using mmu_lock and the invalidation sequence? > > > > Let me know how I can improve the comment. > > Given Sean's context, the comment is good I think. I would quibble > with the the "_must_ still protect" phrasing being a bit too strict. > > Maybe just soften it slightly to acknowledge the exception? Something like: > > * lock is dropped, so callers that require a strict result _must_ protect > * consumption of private vs. shared by checking mmu_invalidate_retry_gfn() > * under mmu_lock to serialize against ongoing attribute updates. Callers > * doing lockless reads must be able to tolerate a stale result. > > That aligns the comment with how KVM is actually using it today. That > said, this is nitpicking. Feel free to use or ignore. Hmm, I wonder if we can figure out a way to consolidate some documentation, because this is _exactly_ the same pattern that x86's host_pfn_mapping_level() deals with (see its big comment below). There's also the stale comment in kvm_invalidate_memslot(), which, stating the obvious, speaks to the memslot+SRCU side of things. Maybe it makes sense to to find a central location for one giant comment about how how MMU notifier events and memslot+SRCU protections work? And then refer to that in paths where some asset needs to be tied into MMU notifiers and/or memslots+SRCU? [*] https://lore.kernel.org/all/agcbWe8s9lmPuJwG@google.com /* * Lookup the mapping level for @gfn in the current mm. * * WARNING! Use of host_pfn_mapping_level() requires the caller and the end * consumer to be tied into KVM's handlers for MMU notifier events! * * There are several ways to safely use this helper: * * - Check mmu_invalidate_retry_gfn() after grabbing the mapping level, before * consuming it. In this case, mmu_lock doesn't need to be held during the * lookup, but it does need to be held while checking the MMU notifier. * * - Hold mmu_lock AND ensure there is no in-progress MMU notifier invalidation * event for the hva. This can be done by explicit checking the MMU notifier * or by ensuring that KVM already has a valid mapping that covers the hva. * * - Do not use the result to install new mappings, e.g. use the host mapping * level only to decide whether or not to zap an entry. In this case, it's * not required to hold mmu_lock (though it's highly likely the caller will * want to hold mmu_lock anyways, e.g. to modify SPTEs). * * Note! The lookup can still race with modifications to host page tables, but * the above "rules" ensure KVM will not _consume_ the result of the walk if a * race with the primary MMU occurs. */