From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.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 85E3913DDD9 for ; Thu, 8 Aug 2024 18:39:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723142393; cv=none; b=lt2XiyPmip+R1OUit9sklNNOFsXndlmriDONA9QI6zzsTvYgD2Wlz3lXjwVAAEIrpu3CjltoMc6l0QeJ7LBhBf8TMGs90LoM0errW1SlLbjVASP/mVUz4jgfVI6y2s2NKhYlkDBqHkQakbxHbsyWNsGEGlR3dTEH4yBC19AAz68= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1723142393; c=relaxed/simple; bh=RwFJhJLgvF0/ZS2geOC+B9PruxoqhM8ayNYYcVVP5GI=; h=Date:In-Reply-To:Mime-Version:Message-ID:Subject:From:To:Cc: Content-Type; b=KuKX6t7PqnKav4FMp2aFsU6V2DsnrSkNajlLrTKLeRspU6mDoAnpDJwJuYMgSuavOt8qKZRYY3ppZHnScjx3mYWQN13CyAOS0A/+VC31Pg9EykNm60fAipk8Fs1Ymfyai3YRA2ik/4IIht22nvCAYSPI6CjzXfQGtAaaQGR77IA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--ackerleytng.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=n7z5RBhe; arc=none smtp.client-ip=209.85.214.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--ackerleytng.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="n7z5RBhe" Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-1fd774c3b8eso19757295ad.0 for ; Thu, 08 Aug 2024 11:39:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1723142390; x=1723747190; darn=vger.kernel.org; h=cc:to:from:subject:message-id:mime-version:in-reply-to:date:from:to :cc:subject:date:message-id:reply-to; bh=rGCKEHWds3nLjc1rwbe/3j/9p13PqyT95btT2PVEHJQ=; b=n7z5RBheyozDL2PIH3VQaVWA1lBvvOfW4pcZEJGePnZpjQCiKrU3NxY3yMS/UrGEPp Zjk8Vs1Smf76VZjamessf27kixf6PkKHcuFPS4TrCso7Z0zCJYmiHouiG3KF5GoO03M4 GwC1jgeSbwuOYgcOfa5Z3TCcqN0hLTWOmFgQOn201U8m3MywJ/+jQD6MR1vMH9q0/Pab qanETUxtnylFaEMZr28s89ecx64kSD3mRRSpMF9nhJRu7+Bv0978+CANCs+lN3F8doN3 LFG+YXZsNg4Vu4rdW1U/ifhSoY/MQkZi5v8DAcZq4evleLOA76KIbpQ/pMe9h/NnKBmG DCPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1723142390; x=1723747190; h=cc:to:from:subject:message-id:mime-version:in-reply-to:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=rGCKEHWds3nLjc1rwbe/3j/9p13PqyT95btT2PVEHJQ=; b=Nzlv3sHQN3j/wyvCmWTjdQzUDYa8U1op2IeGzsKPepNzraAi7FZWmyLrrUTqp2pfdF UZgxUgFp3e5cLJEEmWvAzjdW9OGEIxT/OTp2aqbCK+1OzLUbPl/xb5UZqyyVLl5ChvrC odX1m8x2ccgxpv9lWAVf3FTxPQo/nDntovFfjSzjuCeo3k3eQ3B7smvEr4dgFlm3lIfD JdyTvYCDghnae2U7DDPw7wKoCE4FmlFUR/WqC6bHXeGCE6gDOqLaJjLkthqMbOT2JNwF 9bYxBaS3VEdEzeYUxWXleC4zq0A1hicUWQiRSFLngCoCtIra/1fQpsubxFHeQT+JuWWa ySPg== X-Forwarded-Encrypted: i=1; AJvYcCX7ikBny7MM9Zu1S7Hsu3U7aFNFcvX9VNOh+7yhcx6a52WgWUmwLi0G8G7WIbrW5hjrPihVoQIhclMfUiyLKlSSWOVIi9X9Hqmlus/8 X-Gm-Message-State: AOJu0YyOTImf5J4H5+mC+PmrLExONXU+VnNvtdJ/+scjFVbfunsVo9Z/ gdBgy+IXp8QnaRtx4gULDcXKRmrMQ8fSC+ecM9YIFmdpn7sgfPgQiSyXy/sbTkiJQWtAsJOGW8z nswRAVzeZVvGzSQ1IvI5lNg== X-Google-Smtp-Source: AGHT+IFHR6HjyQprVWt7cfvq/+n/27mLEEJUa7fdG1j2U7c3HTuf0p1YaAhdrnA4goWQYkAB/MjKv4gqqjBOaHPwkQ== X-Received: from ackerleytng-ctop.c.googlers.com ([fda3:e722:ac3:cc00:7f:e700:c0a8:13f8]) (user=ackerleytng job=sendgmr) by 2002:a17:903:1105:b0:1fb:526a:5d60 with SMTP id d9443c01a7336-20096d3b493mr1368265ad.4.1723142389804; Thu, 08 Aug 2024 11:39:49 -0700 (PDT) Date: Thu, 08 Aug 2024 18:39:48 +0000 In-Reply-To: <20240805-guest-memfd-lib-v1-1-e5a29a4ff5d7@quicinc.com> (message from Elliot Berman on Mon, 5 Aug 2024 11:34:47 -0700) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Message-ID: Subject: Re: [PATCH RFC 1/4] mm: Introduce guest_memfd From: Ackerley Tng To: Elliot Berman Cc: akpm@linux-foundation.org, pbonzini@redhat.com, seanjc@google.com, tabba@google.com, david@redhat.com, roypat@amazon.co.uk, qperret@google.com, linux-coco@lists.linux.dev, linux-arm-msm@vger.kernel.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, kvm@vger.kernel.org, quic_eberman@quicinc.com, vannapurve@google.com Content-Type: text/plain; charset="UTF-8" Elliot Berman writes: > In preparation for adding more features to KVM's guest_memfd, refactor > and introduce a library which abstracts some of the core-mm decisions > about managing folios associated with the file. The goal of the refactor > serves two purposes: > > Provide an easier way to reason about memory in guest_memfd. With KVM > supporting multiple confidentiality models (TDX, SEV-SNP, pKVM, ARM > CCA), and coming support for allowing kernel and userspace to access > this memory, it seems necessary to create a stronger abstraction between > core-mm concerns and hypervisor concerns. > > Provide a common implementation for other hypervisors (Gunyah) to use. > > Signed-off-by: Elliot Berman > --- > include/linux/guest_memfd.h | 44 +++++++ > mm/Kconfig | 3 + > mm/Makefile | 1 + > mm/guest_memfd.c | 285 ++++++++++++++++++++++++++++++++++++++++++++ > 4 files changed, 333 insertions(+) > > diff --git a/include/linux/guest_memfd.h b/include/linux/guest_memfd.h > new file mode 100644 > index 000000000000..be56d9d53067 > --- /dev/null > +++ b/include/linux/guest_memfd.h > @@ -0,0 +1,44 @@ > +/* SPDX-License-Identifier: GPL-2.0-only */ > +/* > + * Copyright (c) 2024 Qualcomm Innovation Center, Inc. All rights reserved. > + */ > + > +#ifndef _LINUX_GUEST_MEMFD_H > +#define _LINUX_GUEST_MEMFD_H > + > +#include > + > +/** > + * struct guest_memfd_operations - ops provided by owner to manage folios > + * @invalidate_begin: called when folios should be unmapped from guest. > + * May fail if folios couldn't be unmapped from guest. > + * Required. > + * @invalidate_end: called after invalidate_begin returns success. Optional. > + * @prepare: called before a folio is mapped into the guest address space. > + * Optional. > + * @release: Called when releasing the guest_memfd file. Required. > + */ > +struct guest_memfd_operations { > + int (*invalidate_begin)(struct inode *inode, pgoff_t offset, unsigned long nr); > + void (*invalidate_end)(struct inode *inode, pgoff_t offset, unsigned long nr); > + int (*prepare)(struct inode *inode, pgoff_t offset, struct folio *folio); > + int (*release)(struct inode *inode); > +}; > + > +/** > + * @GUEST_MEMFD_GRAB_UPTODATE: Ensure pages are zeroed/up to date. > + * If trusted hyp will do it, can ommit this flag > + * @GUEST_MEMFD_PREPARE: Call the ->prepare() op, if present. > + */ > +enum { > + GUEST_MEMFD_GRAB_UPTODATE = BIT(0), > + GUEST_MEMFD_PREPARE = BIT(1), > +}; I interpreted the current state of the code after patch [1] to mean that the definition of the uptodate flag means "prepared for guest use", so the two enum values here are probably actually the same thing. For SEV, this means calling rmp_make_private(), so I guess when the page allowed to be faulted in to userspace, rmp_make_shared() would have to be called on the page. Shall we continue to have the uptodate flag mean "prepared for guest use" (whether prepared for shared or private use)? Then we can have another enum to request a zeroed page (which will have no accompanying page flag)? Or could we remove the zeroing feature, since it was meant to be handled by trusted hypervisor or hardware in the first place? It was listed as a TODO before being removed in [2]. I like the idea of having flags to control what is done to the page, perhaps removing the page from the direct map could be yet another enum. > + > +struct folio *guest_memfd_grab_folio(struct file *file, pgoff_t index, u32 flags); > +struct file *guest_memfd_alloc(const char *name, > + const struct guest_memfd_operations *ops, > + loff_t size, unsigned long flags); > +bool is_guest_memfd(struct file *file, const struct guest_memfd_operations *ops); > + > +#endif > [1] https://lore.kernel.org/all/20240726185157.72821-15-pbonzini@redhat.com/ [2] https://lore.kernel.org/all/20240726185157.72821-8-pbonzini@redhat.com/