From patchwork Wed Nov 13 22:34:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Elliot Berman X-Patchwork-Id: 13874357 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 366B0D637CF for ; Wed, 13 Nov 2024 22:34:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9A0B86B0093; Wed, 13 Nov 2024 17:34:48 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 94EF06B0098; Wed, 13 Nov 2024 17:34:48 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7C8CA6B0095; Wed, 13 Nov 2024 17:34:48 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 5E6256B008C for ; Wed, 13 Nov 2024 17:34:48 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 08AB4A0A65 for ; Wed, 13 Nov 2024 22:34:48 +0000 (UTC) X-FDA: 82782525630.13.A59F3F0 Received: from mx0b-0031df01.pphosted.com (mx0b-0031df01.pphosted.com [205.220.180.131]) by imf15.hostedemail.com (Postfix) with ESMTP id 6CDD0A000E for ; Wed, 13 Nov 2024 22:34:01 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=quicinc.com header.s=qcppdkim1 header.b=Bvk4DMG2; spf=pass (imf15.hostedemail.com: domain of quic_eberman@quicinc.com designates 205.220.180.131 as permitted sender) smtp.mailfrom=quic_eberman@quicinc.com; dmarc=pass (policy=none) header.from=quicinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1731537197; h=from:from:sender: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=TGYFoQgLrHaKKvL0/25+aVVEULzKFimBMlOW1rCGOnI=; b=2qNyl9sOV88i/BwNjuDhvmzOJCzxUrvXcpm6lK1xtx5CB3TkiU1ABr5l6rhf80RfGPTS9m fH0WWUzYilO3nrLlIzoq9sSUj/GzjF2MGsL+2hhtfYmwijtTBizqXCPG+ViUeAnyl9Jmzd vJn4K1I9sZSzbrOKavM7X3DpCExUGTw= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1731537197; a=rsa-sha256; cv=none; b=PKV3qwWTlJDToYLPg4kPZmfIcDBaFO5j9MWHpcRcrw8z4l906FwszFcbhcfKWnCL2YKNRb NM2FfrjeKD4at5bKCJXjf5IfaZ3qM66rXgZILsTTwvvKAgKS3TyjD+eQOnxj3WE8UGWclg o+w+LSHm+KsayRxAvuj2FEudJ3pO21s= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=quicinc.com header.s=qcppdkim1 header.b=Bvk4DMG2; spf=pass (imf15.hostedemail.com: domain of quic_eberman@quicinc.com designates 205.220.180.131 as permitted sender) smtp.mailfrom=quic_eberman@quicinc.com; dmarc=pass (policy=none) header.from=quicinc.com Received: from pps.filterd (m0279868.ppops.net [127.0.0.1]) by mx0a-0031df01.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 4ADH1Xmi026885; Wed, 13 Nov 2024 22:34:41 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=quicinc.com; h= cc:content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to; s=qcppdkim1; bh= TGYFoQgLrHaKKvL0/25+aVVEULzKFimBMlOW1rCGOnI=; b=Bvk4DMG2itvq5EQR eTrHfjC8729o7GjceXKjac4hQeN/IvVzg7w3r8MmNz07s2lbgA3yY9C8l83gWU9E kTk+9UTVRPs2NwmDFVgE0/bGvcWtrNpowW/c+Ofo2KA3srrXpCOZz/FvCanevtfo Mfg3mP80NTN2Od5VVjwST//CLTc3800PgPpKIJE9DgJ4WBzN0VlPHdDn+wxkuUy5 Aza8RZO6AEXuJ/3QhycVsvC5XfRMGCPrAccVzyZwljIX59VGoLf/nARQbxJFe741 fKszmidbA/Kj2OLVV0jLP+751URfEUyI/TdiRDDpSki273oh0N2XpFJEge86F4XT ailrXQ== Received: from nasanppmta03.qualcomm.com (i-global254.qualcomm.com [199.106.103.254]) by mx0a-0031df01.pphosted.com (PPS) with ESMTPS id 42vt731w70-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 13 Nov 2024 22:34:40 +0000 (GMT) Received: from nasanex01b.na.qualcomm.com (nasanex01b.na.qualcomm.com [10.46.141.250]) by NASANPPMTA03.qualcomm.com (8.18.1.2/8.18.1.2) with ESMTPS id 4ADMYd3q019845 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Wed, 13 Nov 2024 22:34:39 GMT Received: from hu-eberman-lv.qualcomm.com (10.49.16.6) by nasanex01b.na.qualcomm.com (10.46.141.250) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.2.1544.9; Wed, 13 Nov 2024 14:34:39 -0800 From: Elliot Berman Date: Wed, 13 Nov 2024 14:34:36 -0800 Subject: [PATCH RFC v3 1/2] KVM: guest_memfd: Convert .free_folio() to .release_folio() MIME-Version: 1.0 Message-ID: <20241113-guestmem-library-v3-1-71fdee85676b@quicinc.com> References: <20241113-guestmem-library-v3-0-71fdee85676b@quicinc.com> In-Reply-To: <20241113-guestmem-library-v3-0-71fdee85676b@quicinc.com> To: Paolo Bonzini , Andrew Morton , Sean Christopherson , "Fuad Tabba" , Ackerley Tng , "Mike Rapoport" , "H. Peter Anvin" CC: James Gowans , , , , , , , Elliot Berman X-Mailer: b4 0.14.2 X-Originating-IP: [10.49.16.6] X-ClientProxiedBy: nalasex01b.na.qualcomm.com (10.47.209.197) To nasanex01b.na.qualcomm.com (10.46.141.250) X-QCInternal: smtphost X-Proofpoint-Virus-Version: vendor=nai engine=6200 definitions=5800 signatures=585085 X-Proofpoint-ORIG-GUID: UG0i_TuQukSkwCJybahNRt05tvZ6Wd4j X-Proofpoint-GUID: UG0i_TuQukSkwCJybahNRt05tvZ6Wd4j X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.60.29 definitions=2024-09-06_09,2024-09-06_01,2024-09-02_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 suspectscore=0 priorityscore=1501 lowpriorityscore=0 impostorscore=0 adultscore=0 clxscore=1015 mlxlogscore=999 mlxscore=0 phishscore=0 bulkscore=0 malwarescore=0 spamscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.19.0-2409260000 definitions=main-2411130185 X-Rspamd-Server: rspam10 X-Stat-Signature: zk3qy3eewb4n9e6penejg79pm7cwih6u X-Rspamd-Queue-Id: 6CDD0A000E X-Rspam-User: X-HE-Tag: 1731537241-933577 X-HE-Meta: U2FsdGVkX1/+onWGCNIYn5KHpiTdqft8wulL78J55NXfAK9w3yBI1+18nAeQC5Srva2qAgGAuXSTZq2nkj2Xb3XXRO6phDFY6dIoHhIEj6oOd2gUPAMo81JdYhJYmgIUW6eu6wevyypBZm+NQnT4B7Dk9MWKZRvwR5zONmKLfRvO5tBjHmjGATqnz6dWQO7SuyI7hZqQFIqtjttfP+QXgWLl3vYb2Nui067jYDLPbS1jg+eNKllQG27AcoCIZwMDlJw3oBOgYkf+RI+kAbJShpAmV4IiR2BgmVrG0dH7sU8LcodyhRZ+uF6FlprYKUA704S1h2GqlkAPsFRX17j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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: List-Subscribe: List-Unsubscribe: When guest_memfd becomes a library, a callback will need to be made to the owner (KVM SEV) to transition pages back to hypervisor-owned/shared state. This is currently being done as part of .free_folio() address space op, but this callback shouldn't assume that the mapping still exists. guest_memfd library will need the mapping to still exist to look up its operations table. .release_folio() and .invalidate_folio() address space ops can serve the same purpose here. The key difference between release_folio() and free_folio() is whether the mapping is still valid at time of the callback. This approach was discussed in the link in the footer, but not taken because free_folio() was easier to implement. Link: https://lore.kernel.org/kvm/20231016115028.996656-1-michael.roth@amd.com/ Signed-off-by: Elliot Berman --- virt/kvm/guest_memfd.c | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 47a9f68f7b247f4cba0c958b4c7cd9458e7c46b4..13f83ad8a4c26ba82aca4f2684f22044abb4bc19 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -358,22 +358,35 @@ static int kvm_gmem_error_folio(struct address_space *mapping, struct folio *fol } #ifdef CONFIG_HAVE_KVM_ARCH_GMEM_INVALIDATE -static void kvm_gmem_free_folio(struct folio *folio) +static bool kvm_gmem_release_folio(struct folio *folio, gfp_t gfp) { struct page *page = folio_page(folio, 0); kvm_pfn_t pfn = page_to_pfn(page); int order = folio_order(folio); kvm_arch_gmem_invalidate(pfn, pfn + (1ul << order)); + + return true; +} + +static void kvm_gmem_invalidate_folio(struct folio *folio, size_t offset, + size_t len) +{ + WARN_ON_ONCE(offset != 0); + WARN_ON_ONCE(len != folio_size(folio)); + + if (offset == 0 && len == folio_size(folio)) + filemap_release_folio(folio, 0); } #endif static const struct address_space_operations kvm_gmem_aops = { .dirty_folio = noop_dirty_folio, - .migrate_folio = kvm_gmem_migrate_folio, + .migrate_folio = kvm_gmem_migrate_folio, .error_remove_folio = kvm_gmem_error_folio, #ifdef CONFIG_HAVE_KVM_ARCH_GMEM_INVALIDATE - .free_folio = kvm_gmem_free_folio, + .release_folio = kvm_gmem_release_folio, + .invalidate_folio = kvm_gmem_invalidate_folio, #endif };