From patchwork Fri Dec 13 16:47:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13907459 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 E7683E7717F for ; Fri, 13 Dec 2024 16:48:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7BF3F6B0095; Fri, 13 Dec 2024 11:48:19 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 797156B0098; Fri, 13 Dec 2024 11:48:19 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 65F2B6B0099; Fri, 13 Dec 2024 11:48:19 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 4831B6B0095 for ; Fri, 13 Dec 2024 11:48:19 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id A1522121073 for ; Fri, 13 Dec 2024 16:48:18 +0000 (UTC) X-FDA: 82890518088.05.5797FF8 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) by imf30.hostedemail.com (Postfix) with ESMTP id 8B7C78000C for ; Fri, 13 Dec 2024 16:47:27 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=DFyO49oQ; spf=pass (imf30.hostedemail.com: domain of 3T2VcZwUKCMwBsttsy66y3w.u64305CF-442Dsu2.69y@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3T2VcZwUKCMwBsttsy66y3w.u64305CF-442Dsu2.69y@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734108484; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=zAuvJ3/1BpungxKC2V7q88+gZ9+3WIZOJ/4nQKuXfFs=; b=amdCHBegbsklooKdhs4hEn8Cr8wDR75Q4iBnEFu3xYbJrLB7FypQIhSYpuf2n3wx0zNdvl 2FQbp3s3cuRAG4XHChaI+qAMQKUsV3zYEqe3N9S82HhaGuHHCxVbX/+ci16MFPddGPf44t c8vqVIz1qAhe7brD4SKbuB8WL6po2W8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734108484; a=rsa-sha256; cv=none; b=S3TNhH//Xq+FNkAUHSpGa3UiE9MvbXx7x/jdw9X0HvBEa1hRep/qhi9RP1pYC5ZvHix/x5 iAeSs87AM6rkKHmM+ad2eis+aHCbIi42yzuHUFV/9blxNsFXDiTGkpE7Uqsl09MHVAYOOx KiCZd2n5dm/19Gj2oGgLfXyrxC0VOmw= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=DFyO49oQ; spf=pass (imf30.hostedemail.com: domain of 3T2VcZwUKCMwBsttsy66y3w.u64305CF-442Dsu2.69y@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3T2VcZwUKCMwBsttsy66y3w.u64305CF-442Dsu2.69y@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-43624b081f3so17361335e9.1 for ; Fri, 13 Dec 2024 08:48:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734108495; x=1734713295; darn=kvack.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=zAuvJ3/1BpungxKC2V7q88+gZ9+3WIZOJ/4nQKuXfFs=; b=DFyO49oQAg8AJNLIrnIBJFmeXjd+sohsaaM6XNqhskeXVIjR/Kn80MqEvwXL7hFKjC t2zjaqUeWZBP6CYX3QCQH8hydYuBaBt0gP0fztfHis+mu1Y7fWOm2tDFIX9ET2VW9/vu IKSkaeQQXgzxxBddWdx2lhZtTSLlpcUEbASEobmGtAk27+A0OBI76OF1ZjtfA+kFN/hW ahKppl1Xdx8GRAVdxY8q/W+D4ZCyjYVqNrjV2M1YJwEiHN4p5o0YFk4qrsE7YInP9Sss Pk/egiXAHvFW9A11SRmrMuFQUfogCpRDtxk+bISsynooWn0rXBWdmt4f1qhqDGEf493q 4Meg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734108495; x=1734713295; 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=zAuvJ3/1BpungxKC2V7q88+gZ9+3WIZOJ/4nQKuXfFs=; b=ksazMrdmbLw1xXo+zkkWuzXa+2qMMrB9FG9JPdZdpA3LPPkDwQ8ztsgre3mI5cbguj U+omSwpgpfhuSLTZFksIithNrWvsHoOptXL6clj9qjUZ/R+uXDWld/aEexZdRWShstx3 rjZVfkbVKi9TI/Gj43Q0N+Q4oPMxCHP/5SRffb0OmMHSgMj287DPmFa4jB87QLBEfcpo lj/TrL4lqYpLWllAdj97INQywFXSF30Pe+VmYZUgEy+HbB4Aee/sFZTL6u4N3MloDYX6 dwtwfZCLfWeuELYpPrJOn2DBBtvpc+KvuMd16fXc06AmSJmPeAhnBIzjFLmqCiPXrlMU OtZw== X-Forwarded-Encrypted: i=1; AJvYcCVzTfPVG3PEYjXFdbkgfNyxPtTEFXPCiHph/k+cjj4DnJzsqtM0nQm0rUBtfoGjF58ZOyW05xWTrQ==@kvack.org X-Gm-Message-State: AOJu0Yx0+EpmvjCP6hFC6nRfLvnIM3X4s690cxjNPoNhEQw0CnEqPHdr 8A9s4XG3p0A7A/5HUY/H5m/YnDU6O8H5/aiawfjNwfcKsynLml/lfk8tUXpo11lKL7lh+xQSgQ= = X-Google-Smtp-Source: AGHT+IHs8ZHuR71xrnh0Wig8GOEjO8jW22DFD8cUeagZ+0AiDaeSDaoT2cCNj9eWJgQpPRurrM0qMZgcmQ== X-Received: from wmbd13.prod.google.com ([2002:a05:600c:58cd:b0:434:9dec:7cc5]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:348f:b0:434:a802:43d with SMTP id 5b1f17b1804b1-4362aa9f682mr28943725e9.27.1734108495459; Fri, 13 Dec 2024 08:48:15 -0800 (PST) Date: Fri, 13 Dec 2024 16:47:57 +0000 In-Reply-To: <20241213164811.2006197-1-tabba@google.com> Mime-Version: 1.0 References: <20241213164811.2006197-1-tabba@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241213164811.2006197-2-tabba@google.com> Subject: [RFC PATCH v4 01/14] mm: Consolidate freeing of typed folios on final folio_put() From: Fuad Tabba To: kvm@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-mm@kvack.org Cc: pbonzini@redhat.com, chenhuacai@kernel.org, mpe@ellerman.id.au, anup@brainfault.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, seanjc@google.com, viro@zeniv.linux.org.uk, brauner@kernel.org, willy@infradead.org, akpm@linux-foundation.org, xiaoyao.li@intel.com, yilun.xu@intel.com, chao.p.peng@linux.intel.com, jarkko@kernel.org, amoorthy@google.com, dmatlack@google.com, yu.c.zhang@linux.intel.com, isaku.yamahata@intel.com, mic@digikod.net, vbabka@suse.cz, vannapurve@google.com, ackerleytng@google.com, mail@maciej.szmigiero.name, david@redhat.com, michael.roth@amd.com, wei.w.wang@intel.com, liam.merwick@oracle.com, isaku.yamahata@gmail.com, kirill.shutemov@linux.intel.com, suzuki.poulose@arm.com, steven.price@arm.com, quic_eberman@quicinc.com, quic_mnalajal@quicinc.com, quic_tsoni@quicinc.com, quic_svaddagi@quicinc.com, quic_cvanscha@quicinc.com, quic_pderrin@quicinc.com, quic_pheragu@quicinc.com, catalin.marinas@arm.com, james.morse@arm.com, yuzenghui@huawei.com, oliver.upton@linux.dev, maz@kernel.org, will@kernel.org, qperret@google.com, keirf@google.com, roypat@amazon.co.uk, shuah@kernel.org, hch@infradead.org, jgg@nvidia.com, rientjes@google.com, jhubbard@nvidia.com, fvdl@google.com, hughd@google.com, jthoughton@google.com, tabba@google.com X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 8B7C78000C X-Stat-Signature: uppqhdsmtpdmc59jxbwa1qmckxbxg73d X-Rspam-User: X-HE-Tag: 1734108447-717745 X-HE-Meta: U2FsdGVkX1+3UMWyjLgqC5Ot+Rrdgw/i05beXAybuwYoW+p6t1nyuymFi1PbCbEHwrnNl3na2suFjeH4v7BLRmsBKe5ZxGKZlOVAjBqipEAjFN5ii4M7+vj5Fdh78BxyEyQSTIcIpzmGoEAyKRwiWHr6lmcs5k/1cl/m3MxwS1xIdiwBNGr3gxawsScBYq6uKX8fzFqN2+95RpMWMhO3laimwfyaB/1509nx10bL8c5aez7M1gdUeOBSFlSv6BiiuXif+Bc3OusLiqdKsOBrfzCwTS4VwzYhCXpBh91IZdyNSKCILU/0L+UPstJg92wlXADnQRaa2fPorXMIYxXdcS3AJC22eEEZ38gA5MhMjhWSnp0xSRnM6M9GmU6mhE1KEhg6BCfMZgBpwOnt5ElaqnxJP5F1pfGn2xNC+Nt5tW3+epHnbx2wS+nDNmpdz+nh1bnkK8811UvRZw1W+gkIGTxRvRxWJJ90gxf5eJt+HBS3H67bv9+ZNUbDSdZVEMo2nCYXRlTNKRUSmnD5UUtcMWpzTMJYpE1dGsIe8u7xhTUp+R1PrrP2myfnfoSMhl2xDGwfDgTo9bIA7zJTIwtM50eezSZazt8hhFPSmzHrOaFJgwa9awYxatNAeYgVKrVyXCrxhQZqThn8jll0baRZFWpqia+ST44+CDMsP7JWrEE0mb31O8iU1ARqRp2TswwcdVBcrGQkczB2dKWEYxmmfPMLUXlHdT1Snrl5WOF/uZEViNGyUw7MYtiNrhSR9goGta8z9qsTAKH371MxftUYMajHudUdNqT0M3qEo96sJcbLQCyZ0DNuldFGu9AyHCDXFtDsDC98zwCVebDJr2lgI82W0M/WtbvEgS+pwqkAjhcqXaxvWOtv8dPGwIATdyF0d+rxt+bfcr6L/Ah++UzVBHMz9YQF+cFymcdm3+V0ItybdgGYEsilEMf3JN9fAFDbpWEhs4cy3kWD8QJkC97 nIcTcJyc Chd4vivk/DHuCZ9IcxmYDRpqGTqx5lmoNbyLRhtUWj+2neWGUSZdiWlrWYOrhd0DdOQSvi/O+DmZ6E+qbWLtNefea1JD8STh91Bq7+4LKk7LCHyT/t+2r5z2QKoldJWZDbGVD/FiXixdoOKKukwsqwxVxIjfKLcB+/cQWw8sfzPCco4hlplUXNgYlef6Z3Wgy08FUlyqvQe49qg6I1Ua4Z/6y+Jeg4TUcPJ2vOqma+I+N/B2gaxUWFnaKAiHZNxrJACWPoUaqIMf5ghmaEVzVbSkpnRkW54cY26/D3MdwjYIq8YNm7HbsgjXgCLqc7L3bjDf1WNQ6hjOrDqHOMbxy2Fh8Mllj/tVnMpp6XmxJmS33BR3wyrHPBtDncw0UWkkN3L8xZvF1Jq2wCDh+eQ6EzCv4HPyH8pAabQg1lvBYrAJsrKJfwiuZc+ER6h2py0HKRW1Fsv3N2hxbMTteQxwbQavPE82Qh93EFG9E3M+MtJ1oXwFEujILnaFxVfXxShWkiyZpWLJdnPS0PZspNVQ1innqg042PdWGm7qXms9ZONspq5oT1K4PrBrYXSCM/BnkhyDP4GpoLe3bbFoWBn0WC7YWoYWq9h5XBkiEKHiOJNV7JnQDKC7/5q6oHWZ3SPkxMENZ9hUNu6ft3ElXE5ze8nd6UKZ6jIGg0lKVwBIUn9RY8vU= X-Bogosity: Ham, tests=bogofilter, spamicity=0.020884, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Some folio types, such as hugetlb, handle freeing their own folios. Moreover, guest_memfd will require being notified once a folio's reference count reaches 0 to facilitate shared to private folio conversion, without the folio actually being freed at that point. As a first step towards that, this patch consolidates freeing folios that have a type. The first user is hugetlb folios. Later in this patch series, guest_memfd will become the second user of this. Suggested-by: David Hildenbrand Signed-off-by: Fuad Tabba --- include/linux/page-flags.h | 15 +++++++++++++++ mm/swap.c | 24 +++++++++++++++++++----- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h index cf46ac720802..aca57802d7c7 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -970,6 +970,21 @@ static inline bool page_has_type(const struct page *page) return page_mapcount_is_type(data_race(page->page_type)); } +static inline int page_get_type(const struct page *page) +{ + return page->page_type >> 24; +} + +static inline bool folio_has_type(const struct folio *folio) +{ + return page_has_type(&folio->page); +} + +static inline int folio_get_type(const struct folio *folio) +{ + return page_get_type(&folio->page); +} + #define FOLIO_TYPE_OPS(lname, fname) \ static __always_inline bool folio_test_##fname(const struct folio *folio) \ { \ diff --git a/mm/swap.c b/mm/swap.c index 10decd9dffa1..6f01b56bce13 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -94,6 +94,20 @@ static void page_cache_release(struct folio *folio) unlock_page_lruvec_irqrestore(lruvec, flags); } +static void free_typed_folio(struct folio *folio) +{ + switch (folio_get_type(folio)) { + case PGTY_hugetlb: + free_huge_folio(folio); + return; + case PGTY_offline: + /* Nothing to do, it's offline. */ + return; + default: + WARN_ON_ONCE(1); + } +} + void __folio_put(struct folio *folio) { if (unlikely(folio_is_zone_device(folio))) { @@ -101,8 +115,8 @@ void __folio_put(struct folio *folio) return; } - if (folio_test_hugetlb(folio)) { - free_huge_folio(folio); + if (unlikely(folio_has_type(folio))) { + free_typed_folio(folio); return; } @@ -934,13 +948,13 @@ void folios_put_refs(struct folio_batch *folios, unsigned int *refs) if (!folio_ref_sub_and_test(folio, nr_refs)) continue; - /* hugetlb has its own memcg */ - if (folio_test_hugetlb(folio)) { + if (unlikely(folio_has_type(folio))) { + /* typed folios have their own memcg, if any */ if (lruvec) { unlock_page_lruvec_irqrestore(lruvec, flags); lruvec = NULL; } - free_huge_folio(folio); + free_typed_folio(folio); continue; } folio_unqueue_deferred_split(folio); From patchwork Fri Dec 13 16:47:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13907460 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 A1216E77182 for ; Fri, 13 Dec 2024 16:48:21 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2B6CC6B0098; Fri, 13 Dec 2024 11:48:21 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 267716B009B; Fri, 13 Dec 2024 11:48:21 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0DF876B009E; Fri, 13 Dec 2024 11:48:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id E0C636B0098 for ; Fri, 13 Dec 2024 11:48:20 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 9EF92160F70 for ; Fri, 13 Dec 2024 16:48:20 +0000 (UTC) X-FDA: 82890517962.23.FDE03F6 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf25.hostedemail.com (Postfix) with ESMTP id 48CD8A0014 for ; Fri, 13 Dec 2024 16:48:00 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=odDBaH5E; spf=pass (imf25.hostedemail.com: domain of 3UWVcZwUKCM4Duvvu08805y.w86527EH-664Fuw4.8B0@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3UWVcZwUKCM4Duvvu08805y.w86527EH-664Fuw4.8B0@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734108487; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=8sbfS8W1wicHM5vsGa5IMowjJ2CAstrX6T9HihvAEa4=; b=XRVOs8oM822zxn9iSXMHLQvcYALupImKzGdByFfIYjOwBE7IwJi7cBH9X7hFvIQ7togvkz 0MMX4GOmIkikMvV1lxPe20Pr1RjMLFI8LaN5Viw5NVTCkEWPuombI7fRRrNjyFmpMNDJ+D z8QMx3vDlMEum6asnBAUErucBUVCeJQ= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=odDBaH5E; spf=pass (imf25.hostedemail.com: domain of 3UWVcZwUKCM4Duvvu08805y.w86527EH-664Fuw4.8B0@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3UWVcZwUKCM4Duvvu08805y.w86527EH-664Fuw4.8B0@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734108487; a=rsa-sha256; cv=none; b=SiChK+PwxQfr/Dkm51bWrKC94IHa/zjLdj11voUGWQ7y0d/BfJZwHukhixypN97iFPW+V1 g/urdestOAcV8MQmi57eCQoKwy7luS5EyEtNmI0Nellg1E7m2vY89UyMZoeMqVkgq9ezWB KjJIT7QG+pimDHX0ae+kCWiNAf1kzJ0= Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4361efc9d23so18649625e9.3 for ; Fri, 13 Dec 2024 08:48:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734108497; x=1734713297; darn=kvack.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=8sbfS8W1wicHM5vsGa5IMowjJ2CAstrX6T9HihvAEa4=; b=odDBaH5E3FRHnnh4/D5oMPez8bem0r70gNtliSp6xj2HfUiPy5Uk4tEFmHBm5z6iUl uf5qaOGp4ir42va0woZMfJXcRoi9Qv22B0E9STDA5y+LTUEwLuO5+HRMjZPyjZt9zN2p aMtiMnU9XqeBRXViC4plV9+9HT9n0pUmaoPY+EW28Y9BFDeHD4/jDVFx0BU8HuKtNFhZ PjOk69Ss7nLuU2/HsAc5gBVumThz544kXoIuTmVtdnVi8X/wDT+AhhaHRSEBRZjjdmeS Bf8mYRQy38CWoN/IRvF/2VWEtUpzyEm+me0w/e1ge2wFAemWnMt1sP5AC3ae/RjuMjWV M3OQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734108497; x=1734713297; 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=8sbfS8W1wicHM5vsGa5IMowjJ2CAstrX6T9HihvAEa4=; b=gCfoJKKeVaqez0EuGHET7GhrY6Hb3n6iRL+nRqOFTm7oKJ5EPcfMKGq+uu72BIqjDG 5RnFF6xMabFHhWOS2x0t+bxacAVfffNX77w2q//gSF/qULCcmy56A83UAj2gEiCLuwK1 ST5oVHT4XfTtmefJqWkFerKpOQzd+SY6DESfemuyQqZUMU4fUf+nqvgxZzjzcKuXFMnB 9Rn2ePnCNkDbBlOW4MEJkHZvvT7jnR8l5/tuG+vuHrrgKvaPFSF5OB0VXg/+VnppTIV/ 9ClO6XzZmwXZ8s+fUD7Vota88k0mkKnuEyF/YNE/SkBh63cQ7YCxJ/UOZz2iKnjiX0Ub Cv4g== X-Forwarded-Encrypted: i=1; AJvYcCW4igGUTzGQHtoKvj5h7k6s3jcmdtjOO0bXYBjYD208YhRdFpZpzOPMTHcSAaekQamKKTMzWxuF1g==@kvack.org X-Gm-Message-State: AOJu0YxT4Zsj814MuAUU7VJRkmdb4bLUofWmyN4Ocj7hhFxcwZtg1w/W KO7iB+DunQ7vNhUqK/GgWtY09YbKscW7haiCny3HOu8yiowfx+RJR1FAID8+OvvYTLRMHKscWg= = X-Google-Smtp-Source: AGHT+IEPhjbYVrPG4OvpW3X6Nrv45wDkaxf1Je2UWVRf/3W/68PxH4JbNqktpfTiAT0Xl8J+JFcRBc200g== X-Received: from wmnb22.prod.google.com ([2002:a05:600c:6d6:b0:434:a4bc:534f]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1da4:b0:434:f753:6012 with SMTP id 5b1f17b1804b1-4362aa509abmr35207575e9.17.1734108497542; Fri, 13 Dec 2024 08:48:17 -0800 (PST) Date: Fri, 13 Dec 2024 16:47:58 +0000 In-Reply-To: <20241213164811.2006197-1-tabba@google.com> Mime-Version: 1.0 References: <20241213164811.2006197-1-tabba@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241213164811.2006197-3-tabba@google.com> Subject: [RFC PATCH v4 02/14] KVM: guest_memfd: Make guest mem use guest mem inodes instead of anonymous inodes From: Fuad Tabba To: kvm@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-mm@kvack.org Cc: pbonzini@redhat.com, chenhuacai@kernel.org, mpe@ellerman.id.au, anup@brainfault.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, seanjc@google.com, viro@zeniv.linux.org.uk, brauner@kernel.org, willy@infradead.org, akpm@linux-foundation.org, xiaoyao.li@intel.com, yilun.xu@intel.com, chao.p.peng@linux.intel.com, jarkko@kernel.org, amoorthy@google.com, dmatlack@google.com, yu.c.zhang@linux.intel.com, isaku.yamahata@intel.com, mic@digikod.net, vbabka@suse.cz, vannapurve@google.com, ackerleytng@google.com, mail@maciej.szmigiero.name, david@redhat.com, michael.roth@amd.com, wei.w.wang@intel.com, liam.merwick@oracle.com, isaku.yamahata@gmail.com, kirill.shutemov@linux.intel.com, suzuki.poulose@arm.com, steven.price@arm.com, quic_eberman@quicinc.com, quic_mnalajal@quicinc.com, quic_tsoni@quicinc.com, quic_svaddagi@quicinc.com, quic_cvanscha@quicinc.com, quic_pderrin@quicinc.com, quic_pheragu@quicinc.com, catalin.marinas@arm.com, james.morse@arm.com, yuzenghui@huawei.com, oliver.upton@linux.dev, maz@kernel.org, will@kernel.org, qperret@google.com, keirf@google.com, roypat@amazon.co.uk, shuah@kernel.org, hch@infradead.org, jgg@nvidia.com, rientjes@google.com, jhubbard@nvidia.com, fvdl@google.com, hughd@google.com, jthoughton@google.com, tabba@google.com X-Rspamd-Queue-Id: 48CD8A0014 X-Stat-Signature: 73p71bjrpxipmp5foqb4b3w8x39utfhs X-Rspam-User: X-Rspamd-Server: rspam11 X-HE-Tag: 1734108480-796182 X-HE-Meta: U2FsdGVkX1/jR/U0vfD7nSn0gdFDg1H9w1be4hPic+TYiYL/wyQN64pwuYHoisth/8rXA7vaJys+pgnRWSYH8VWmy2vNBwNvH0G3co6xJtJjP0FuiwqIuS+m9CJe4BB3v17xgvr3nMoz/C+IX4d5bx93cbc9p52VX8nmHqwQiof9upiGUOv1ajNFiwP+RlCdbZBgoptcS5G362dbzW7REWTo58IP/QPG65bhrGiPKIls1MjijC/vzSVzDQZjdCbJz9grzKl/0SGWa806IIgQg+VT6PFwcm2X5lmrdlvCNTO2ROrzeWqSGXiwotxCOITl0pew07A4vKPwMWdVNCBCyGhNTD5dRuhqJtWftHh7al0sfoGBoiMaL41jzc5NQTxzQEcMS42kcU16c7SzkPS0u+82/rRi9CVKH7XvbTphzuK1W8QGy9kkEusjyfT3Z67dKVDPgZZzD5akTCedmk3SmHBLnzuxloHh2DHmrMquhb5fPe5leIhOwK9RTRSY2g+mbS1gMJ6dRYjuQHjDim28uSfwe8T0qWdpQsf+GMr1n5+nUmuVMlb4C9bYpbtHMHd2F6KMefL75Mnd90c7z1p4g1EUulsl4rG4c9WAo6BRqci5WnCoKvZxA9JDda37bW/3cL3UtLRrdfnRov/nEkMW+yEkdWUOq0TbzoMesIeBuZxd/de9SCWpFMUwyncJ2GGwbVux3TXyaKuU3dvJL3cLE0Qw+D/7OiaX5qWLJbHr3vmmtAr92pNrbKKT3hzwr05KG2QIZp6hLTVfM36ocArhJkWLtdKZOeUY2cAWVl2RTLi5EkT5I6ekMGL+V/y+8YSZrpscyEhRbc8qjfbcUVVBYr8rT6FJ7OXEQyXcegeR88dUZzBBgbdgghrUvoWWpTnUCEc4bkbMTyZSN5Hggbi7RsQhg5nBp/6Qm73gWWSr64sx4Y4vqcW2N7PrDumHMmw54zRjBiIJ6jFtR4TH0af NsSbpyqQ XkromywsFVHWc2VpFCTdke9YNy1T07wLxcEr/iQHpQUoguZqCcvEk8uWCuC5BQAxyNViPGPGoqO2yWWLXomWXqmPNB6TwlspcI0ywyMGmsCkuD7fJkxDuTCvGMY+xfMh5/iiahurN/9cM3BN3W+LSGPCLll0PwIZzI2WDrNKFBKU40qwT9drlzVOsXT1E9llZedYQ2w/y1eQeLN4vG+OM/LmduP6FbZaA/xzpfedd30mJJw2C2Scn9IKn8CP3qifJyggAXYiSQryYNO3ugeZ37z+3XRrlz1qjsqt2V++7umOcAxfcToGhrze04fEGoS9jecBcqIrNTQ3/2+FEjdCbXY0OppqOq7R8YhQKE6CykADYPDYIm14vadcDS5mbWC+OTEDceXJG+CcdbVTCV1u7BJ+mvUkJddRaRcN4jjqB8HIKI1wEI+5yofEvsAx+PYGhkrzy1VAuubCU3i9n3N2b/SP9nGDJMLmOSjLDoWfJMpJqHAr9PALf/x5wt1BH7ZzCQ2cGtF9B+RU8hRXnfflTKQNBLWB7qzfbK0/PEmMIyONbOU9tD9R7aJevYDWldmzxGpsGStJtFTICBsU1PSWmg9iKp9MbMaLHG1KuVjStAf8nuItWQLfLB6SzAFN1WbNxKeBYrGiqb8GcTwzCpugVQkx/6Q== X-Bogosity: Ham, 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: From: Ackerley Tng Using guest mem inodes allows us to store metadata for the backing memory on the inode. Metadata will be added in a later patch to support HugeTLB pages. Metadata about backing memory should not be stored on the file, since the file represents a guest_memfd's binding with a struct kvm, and metadata about backing memory is not unique to a specific binding and struct kvm. Signed-off-by: Ackerley Tng Signed-off-by: Fuad Tabba --- include/uapi/linux/magic.h | 1 + virt/kvm/guest_memfd.c | 119 ++++++++++++++++++++++++++++++------- 2 files changed, 100 insertions(+), 20 deletions(-) diff --git a/include/uapi/linux/magic.h b/include/uapi/linux/magic.h index bb575f3ab45e..169dba2a6920 100644 --- a/include/uapi/linux/magic.h +++ b/include/uapi/linux/magic.h @@ -103,5 +103,6 @@ #define DEVMEM_MAGIC 0x454d444d /* "DMEM" */ #define SECRETMEM_MAGIC 0x5345434d /* "SECM" */ #define PID_FS_MAGIC 0x50494446 /* "PIDF" */ +#define GUEST_MEMORY_MAGIC 0x474d454d /* "GMEM" */ #endif /* __LINUX_MAGIC_H__ */ diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 47a9f68f7b24..198554b1f0b5 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -1,12 +1,17 @@ // SPDX-License-Identifier: GPL-2.0 +#include +#include #include #include #include +#include #include #include #include "kvm_mm.h" +static struct vfsmount *kvm_gmem_mnt; + struct kvm_gmem { struct kvm *kvm; struct xarray bindings; @@ -307,6 +312,38 @@ static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn) return gfn - slot->base_gfn + slot->gmem.pgoff; } +static const struct super_operations kvm_gmem_super_operations = { + .statfs = simple_statfs, +}; + +static int kvm_gmem_init_fs_context(struct fs_context *fc) +{ + struct pseudo_fs_context *ctx; + + if (!init_pseudo(fc, GUEST_MEMORY_MAGIC)) + return -ENOMEM; + + ctx = fc->fs_private; + ctx->ops = &kvm_gmem_super_operations; + + return 0; +} + +static struct file_system_type kvm_gmem_fs = { + .name = "kvm_guest_memory", + .init_fs_context = kvm_gmem_init_fs_context, + .kill_sb = kill_anon_super, +}; + +static void kvm_gmem_init_mount(void) +{ + kvm_gmem_mnt = kern_mount(&kvm_gmem_fs); + BUG_ON(IS_ERR(kvm_gmem_mnt)); + + /* For giggles. Userspace can never map this anyways. */ + kvm_gmem_mnt->mnt_flags |= MNT_NOEXEC; +} + static struct file_operations kvm_gmem_fops = { .open = generic_file_open, .release = kvm_gmem_release, @@ -316,6 +353,8 @@ static struct file_operations kvm_gmem_fops = { void kvm_gmem_init(struct module *module) { kvm_gmem_fops.owner = module; + + kvm_gmem_init_mount(); } static int kvm_gmem_migrate_folio(struct address_space *mapping, @@ -397,11 +436,67 @@ static const struct inode_operations kvm_gmem_iops = { .setattr = kvm_gmem_setattr, }; +static struct inode *kvm_gmem_inode_make_secure_inode(const char *name, + loff_t size, u64 flags) +{ + const struct qstr qname = QSTR_INIT(name, strlen(name)); + struct inode *inode; + int err; + + inode = alloc_anon_inode(kvm_gmem_mnt->mnt_sb); + if (IS_ERR(inode)) + return inode; + + err = security_inode_init_security_anon(inode, &qname, NULL); + if (err) { + iput(inode); + return ERR_PTR(err); + } + + inode->i_private = (void *)(unsigned long)flags; + inode->i_op = &kvm_gmem_iops; + inode->i_mapping->a_ops = &kvm_gmem_aops; + inode->i_mode |= S_IFREG; + inode->i_size = size; + mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER); + mapping_set_inaccessible(inode->i_mapping); + /* Unmovable mappings are supposed to be marked unevictable as well. */ + WARN_ON_ONCE(!mapping_unevictable(inode->i_mapping)); + + return inode; +} + +static struct file *kvm_gmem_inode_create_getfile(void *priv, loff_t size, + u64 flags) +{ + static const char *name = "[kvm-gmem]"; + struct inode *inode; + struct file *file; + + if (kvm_gmem_fops.owner && !try_module_get(kvm_gmem_fops.owner)) + return ERR_PTR(-ENOENT); + + inode = kvm_gmem_inode_make_secure_inode(name, size, flags); + if (IS_ERR(inode)) + return ERR_CAST(inode); + + file = alloc_file_pseudo(inode, kvm_gmem_mnt, name, O_RDWR, + &kvm_gmem_fops); + if (IS_ERR(file)) { + iput(inode); + return file; + } + + file->f_mapping = inode->i_mapping; + file->f_flags |= O_LARGEFILE; + file->private_data = priv; + + return file; +} + static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) { - const char *anon_name = "[kvm-gmem]"; struct kvm_gmem *gmem; - struct inode *inode; struct file *file; int fd, err; @@ -415,32 +510,16 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) goto err_fd; } - file = anon_inode_create_getfile(anon_name, &kvm_gmem_fops, gmem, - O_RDWR, NULL); + file = kvm_gmem_inode_create_getfile(gmem, size, flags); if (IS_ERR(file)) { err = PTR_ERR(file); goto err_gmem; } - file->f_flags |= O_LARGEFILE; - - inode = file->f_inode; - WARN_ON(file->f_mapping != inode->i_mapping); - - inode->i_private = (void *)(unsigned long)flags; - inode->i_op = &kvm_gmem_iops; - inode->i_mapping->a_ops = &kvm_gmem_aops; - inode->i_mode |= S_IFREG; - inode->i_size = size; - mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER); - mapping_set_inaccessible(inode->i_mapping); - /* Unmovable mappings are supposed to be marked unevictable as well. */ - WARN_ON_ONCE(!mapping_unevictable(inode->i_mapping)); - kvm_get_kvm(kvm); gmem->kvm = kvm; xa_init(&gmem->bindings); - list_add(&gmem->entry, &inode->i_mapping->i_private_list); + list_add(&gmem->entry, &file_inode(file)->i_mapping->i_private_list); fd_install(fd, file); return fd; From patchwork Fri Dec 13 16:47:59 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13907461 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 79169E7717F for ; Fri, 13 Dec 2024 16:48:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 047506B009E; Fri, 13 Dec 2024 11:48:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id F370D6B009F; Fri, 13 Dec 2024 11:48:23 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DB3F86B00A0; Fri, 13 Dec 2024 11:48:23 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id B414D6B009E for ; Fri, 13 Dec 2024 11:48:23 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 6AD7142D25 for ; Fri, 13 Dec 2024 16:48:23 +0000 (UTC) X-FDA: 82890517878.06.D566373 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) by imf21.hostedemail.com (Postfix) with ESMTP id 580B21C0002 for ; Fri, 13 Dec 2024 16:47:32 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=XCuNnPg8; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf21.hostedemail.com: domain of 3U2VcZwUKCNAFwxxw2AA270.yA8749GJ-886Hwy6.AD2@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3U2VcZwUKCNAFwxxw2AA270.yA8749GJ-886Hwy6.AD2@flex--tabba.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734108477; a=rsa-sha256; cv=none; b=CU88uAzjwhk76c5wyXziYHl5yh0H1eTxkCrRTFNP5Zk0kYrQMCH0eDBJfT/9y5mKSDZgbj qJg6A3p2Be8BrVMI1q/OoA4mwG/aXO4vp7ypsY7qeC1kKHqPR7PVTroUg5mXkOx5i0lDKP D26DqG+I8jfjP/HhbDC8bUkPllz+OxQ= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=XCuNnPg8; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf21.hostedemail.com: domain of 3U2VcZwUKCNAFwxxw2AA270.yA8749GJ-886Hwy6.AD2@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3U2VcZwUKCNAFwxxw2AA270.yA8749GJ-886Hwy6.AD2@flex--tabba.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734108477; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Nn98e+WujoBnk4EYt4M9w4x2TZk0zk8clmlBtUUAHUE=; b=xdoU4SGFLG97jF78yL79tBByUnOeaj2PX8sFC+QP2iitp2B/SCcdHXsFroF6X/C3yOA2BM KWO1M7iV6GlgXBuB8/Cl56d9EttK+m7J+3vCt5jL8usyNFeManAcJjTCdLATU5d2a1STO9 bK4vFqyVuJh84yXxQjEKgTL77oSMBzA= Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-4359eb032c9so15928765e9.2 for ; Fri, 13 Dec 2024 08:48:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734108500; x=1734713300; darn=kvack.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=Nn98e+WujoBnk4EYt4M9w4x2TZk0zk8clmlBtUUAHUE=; b=XCuNnPg82Y2Y14g1idQRjfWBPmyffspGwo1XlYnsIGRZkykp3Lx0Vw9Nhw1pPJHQMU UHheJExMDuc064yGbryUR9zsa0cKRyj/VfRKBym+I2tn9AfSzzW7V3xTTVd+yfkqlEby Vgh1qi6XoVscBt2g91Z8tqMIOMmc5xiwDSPwOshjx6m8yYL9/gTuA5XprlRjkjU33q94 kmMf0WoBZ+VZ12It395OOJR/ukr5KuMbCwk/oeC5quKRImtCkjcl6lQVJVrq3CBvQgmY WLnSrsuzxcaaxw8W9Wim6lsTXYYzk6RPbr9ikf0TJmIJTSmq2driEWJ5tPzxwMrme2Hp 5e/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734108500; x=1734713300; 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=Nn98e+WujoBnk4EYt4M9w4x2TZk0zk8clmlBtUUAHUE=; b=vots7j25OvwsF/EW0hSGbShiz8ODr/uxPOVfER+kYuKMnFSV03lbW0oUd4nsGrNvcN YnOf7f3O5NSnu6dV+AuxywgwLM9W9VEKSyhiFaLLArNfEGq8btlsXkIhNuVHq1O09xgE chw4L7lc+GVNG4kl79JlxNhqgiJGdh1qWqxCm7qeCzqJbqBqXKBic7Ph+kBCrTDaexxA Mn3A3e4lyqdm8y4UlkBX2n1EqgZ09X+w4IyLXIAxGDkZzBU4+xyZZzVl2stn0ggFBR80 JYDRYYQCvKbs0URI9a0ryM5js5PoQO7QLY947onA2nWIUWub99Lq7Pf+xmfZOSjI3FKe eyCQ== X-Forwarded-Encrypted: i=1; AJvYcCUUa80ekGWDP/8IxP8AuHkjhA+0OrIYlJeXc9L4AqRVBDuo28zCFMoJVlXbC+OM+scewBcye9zGzw==@kvack.org X-Gm-Message-State: AOJu0YywTHK9lXCtJEYiUcpQmn0NrJypP1tNk0UGJEvhnxCR4z24qeEF +dnaKnnANF9pLPmsTLcG30p3h0jUSp+KDm8Q006IlztcV6VSd9bbThiaYzlIT7mc1km4AcbjUQ= = X-Google-Smtp-Source: AGHT+IGbUUdOrO+LdNxgwAJN5RVrV2s69jyBzXe2ihtQ1qCa2+mzNDnn/emZBrvQ9OXRTxBEpqDlu0D2/g== X-Received: from wmma26.prod.google.com ([2002:a05:600c:225a:b0:434:f299:5633]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1e8a:b0:434:f5d1:f10f with SMTP id 5b1f17b1804b1-4362aa52fa7mr31352765e9.17.1734108499591; Fri, 13 Dec 2024 08:48:19 -0800 (PST) Date: Fri, 13 Dec 2024 16:47:59 +0000 In-Reply-To: <20241213164811.2006197-1-tabba@google.com> Mime-Version: 1.0 References: <20241213164811.2006197-1-tabba@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241213164811.2006197-4-tabba@google.com> Subject: [RFC PATCH v4 03/14] KVM: guest_memfd: Introduce kvm_gmem_get_pfn_locked(), which retains the folio lock From: Fuad Tabba To: kvm@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-mm@kvack.org Cc: pbonzini@redhat.com, chenhuacai@kernel.org, mpe@ellerman.id.au, anup@brainfault.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, seanjc@google.com, viro@zeniv.linux.org.uk, brauner@kernel.org, willy@infradead.org, akpm@linux-foundation.org, xiaoyao.li@intel.com, yilun.xu@intel.com, chao.p.peng@linux.intel.com, jarkko@kernel.org, amoorthy@google.com, dmatlack@google.com, yu.c.zhang@linux.intel.com, isaku.yamahata@intel.com, mic@digikod.net, vbabka@suse.cz, vannapurve@google.com, ackerleytng@google.com, mail@maciej.szmigiero.name, david@redhat.com, michael.roth@amd.com, wei.w.wang@intel.com, liam.merwick@oracle.com, isaku.yamahata@gmail.com, kirill.shutemov@linux.intel.com, suzuki.poulose@arm.com, steven.price@arm.com, quic_eberman@quicinc.com, quic_mnalajal@quicinc.com, quic_tsoni@quicinc.com, quic_svaddagi@quicinc.com, quic_cvanscha@quicinc.com, quic_pderrin@quicinc.com, quic_pheragu@quicinc.com, catalin.marinas@arm.com, james.morse@arm.com, yuzenghui@huawei.com, oliver.upton@linux.dev, maz@kernel.org, will@kernel.org, qperret@google.com, keirf@google.com, roypat@amazon.co.uk, shuah@kernel.org, hch@infradead.org, jgg@nvidia.com, rientjes@google.com, jhubbard@nvidia.com, fvdl@google.com, hughd@google.com, jthoughton@google.com, tabba@google.com X-Stat-Signature: h598m6tfnzmwpf7qd5bdwymwze8oxpn5 X-Rspam-User: X-Rspamd-Queue-Id: 580B21C0002 X-Rspamd-Server: rspam08 X-HE-Tag: 1734108452-291279 X-HE-Meta: U2FsdGVkX1+TdQ78aI0uVO9X1PipAOceqN9EC4WPlvnI51C1oVpB2pnrAMd4CfhKNnTHkW3rZXU7+HD8jzBGYjrXTdL3w4wioxwI4f+rk2R9wzGVp62xmEhagBbFb1m8CyRqRfRl0Xtex7qNSfmTPegmYdHm/KbXkmqrd+BO4/xZayE9xJsDhA+S6PJ75idhFHh3zABbBG3LhUQmZ1M9FLF8txEJOIN2TBoQnGWs0IMDHSITXgToP5TZY4GHTIEKcRj25BcD0EMkysHrc/hwqF0CxC78+Vn1zmmJnxXlFXx29kW3YpXsSSsFDeSPzTT2P15wk/2NpNE+1vxdzcSNtx/zJsXtHnSkez3UgGXHcQw65sE1zkzFhtjtL9ryPBA5LzkuKjXoqPjKjSREt1cK8Eno18FBBhBneF4JOiSVJSeZuG1jzGHtcXIYoFOLwb3XLPW16cR6DvySMqGiNEd/T7v7I9R/vJH4lVEjU57wlJWFLNgCLyYrgAOXF+Sap60CcQ/n+6ZGAbvjuNNr5X9OE0LAIcBfBzZTi66JtBdMMY0fROOKw/SueTSNuKnYft8JgDk8PgAKvc4Db047zOhH35OETP+4WFUl4NEfNWX1/BTFsotAH0oSu1Ux04EH6Vh03yNPlnGrX5ykeocJ9cmvP9PL6XMyNcWYZXoCgeKLL4u+7dIbD+W3mX0NF7zRtQ3V7BKl2cpz+f/UqxT0yoHdxQr/xfjqTkpTYTkfZFbZFh/Bcw+TegDzPlDJ40SiB2cPTTFlCEEf9pJbzn5qIQX30wXZRxmx7aF4vhTFPcP6pf5Sesa08kCoAuMsQXklRIj6TIsOLd2aYMxFrByzLBc4IjhhiDkK/O5E0XdmXjZgHJb4PYwN35HoDnJDKovwFBGSHsBIF5Q8BMY8qlJzzjCRHyEDN5Pc/ZCzL7WB8l/hPXgXz1XmVRjhAhtucSgTJnNbuixymSPZUVsPx8PECb4 DeBKVDCH M5ImBEH2P7Ka12damm3LSgvTe8h/Vg1LCxd/CqTNO1jUodAZ3Q9XW7ycZ2Ub5091NoH1BZv9gQWA61U5Scm5I3QbVuiBaT3bN3RCo6L4/EtPVNkNDCH12dCwpjvHs5PPUJgpT/pxiGDTqKAgEK37rn+TErFDw94C0OCrlhnw1RzS33e10aBbONmNu3Z9jO+HVQjsiEetnBTWo4172QufLF7Y4f/vvlzIH1RnwXTG8mo5GBwQuX1ufZF+18TjWo+FqVPnNqcQkW2N4CPYZjwunMd+uA0Ogd7uu/1rXUkGmBilvNTRV6QDAt0UsQI63CkgwKIyiAU1Wyy0YA0be50umzwy1GsDN682RF2PpO+UGvTuoX1L6eUBM4G4e2kd56M3Lab0a8PIeQcv4A4zgGsUIIeXxU8ZxxUn5gf0SuK/RX5Liypb2QPek1IZEGp9DDHTcLSBboqfLHMpJJTSgeF0WVusBKx3Fu3JKf/1dpOyb0A62TZOSvDG4DJaVaJ/g5zyoYVrmeZ8NOnKpHXVhG78hsMw+ENInlTKXEcJTbYHRxt4F+kjb6XzYiTkYQqAcyRzPY+PvRoO+4idigyOx0Z62NKOY8GmSXMXX2FUsp6PF5vUYBizx6falHEvkQp8KjQuhAXSvPFfU2Q3yM0s= X-Bogosity: Ham, tests=bogofilter, spamicity=0.001012, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Create a new variant of kvm_gmem_get_pfn(), which retains the folio lock if it returns successfully. This is needed in subsequent patches in order to protect against races when checking whether a folio can be mapped by the host. Signed-off-by: Fuad Tabba --- include/linux/kvm_host.h | 11 +++++++++++ virt/kvm/guest_memfd.c | 27 ++++++++++++++++++++------- 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 401439bb21e3..cda3ed4c3c27 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2500,6 +2500,9 @@ static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn) int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn, kvm_pfn_t *pfn, struct page **page, int *max_order); +int kvm_gmem_get_pfn_locked(struct kvm *kvm, struct kvm_memory_slot *slot, + gfn_t gfn, kvm_pfn_t *pfn, struct page **page, + int *max_order); #else static inline int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, gfn_t gfn, @@ -2509,6 +2512,14 @@ static inline int kvm_gmem_get_pfn(struct kvm *kvm, KVM_BUG_ON(1, kvm); return -EIO; } +static inline int kvm_gmem_get_pfn_locked(struct kvm *kvm, + struct kvm_memory_slot *slot, + gfn_t gfn, kvm_pfn_t *pfn, + struct page **page, int *max_order) +{ + KVM_BUG_ON(1, kvm); + return -EIO; +} #endif /* CONFIG_KVM_PRIVATE_MEM */ #ifdef CONFIG_HAVE_KVM_ARCH_GMEM_PREPARE diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 198554b1f0b5..6453658d2650 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -672,9 +672,9 @@ static struct folio *__kvm_gmem_get_pfn(struct file *file, return folio; } -int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, - gfn_t gfn, kvm_pfn_t *pfn, struct page **page, - int *max_order) +int kvm_gmem_get_pfn_locked(struct kvm *kvm, struct kvm_memory_slot *slot, + gfn_t gfn, kvm_pfn_t *pfn, struct page **page, + int *max_order) { pgoff_t index = kvm_gmem_get_index(slot, gfn); struct file *file = kvm_gmem_get_file(slot); @@ -694,17 +694,30 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, if (!is_prepared) r = kvm_gmem_prepare_folio(kvm, slot, gfn, folio); - folio_unlock(folio); - - if (!r) + if (!r) { *page = folio_file_page(folio, index); - else + } else { + folio_unlock(folio); folio_put(folio); + } out: fput(file); return r; } +EXPORT_SYMBOL_GPL(kvm_gmem_get_pfn_locked); + +int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, + gfn_t gfn, kvm_pfn_t *pfn, struct page **page, + int *max_order) +{ + int r = kvm_gmem_get_pfn_locked(kvm, slot, gfn, pfn, page, max_order); + + if (!r) + unlock_page(*page); + + return r; +} EXPORT_SYMBOL_GPL(kvm_gmem_get_pfn); #ifdef CONFIG_KVM_GENERIC_PRIVATE_MEM From patchwork Fri Dec 13 16:48:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13907462 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 092B4E77182 for ; Fri, 13 Dec 2024 16:48:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 418C66B00A0; Fri, 13 Dec 2024 11:48:25 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3CF006B00A1; Fri, 13 Dec 2024 11:48:25 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2185A6B00A2; Fri, 13 Dec 2024 11:48:25 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id ECAB76B00A0 for ; Fri, 13 Dec 2024 11:48:24 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id AD7C51A0FC8 for ; Fri, 13 Dec 2024 16:48:24 +0000 (UTC) X-FDA: 82890517920.06.DBE0E75 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) by imf19.hostedemail.com (Postfix) with ESMTP id 9A1DF1A000E for ; Fri, 13 Dec 2024 16:47:55 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=D22tQ+Q5; spf=pass (imf19.hostedemail.com: domain of 3VWVcZwUKCNIHyzzy4CC492.0CA96BIL-AA8Jy08.CF4@flex--tabba.bounces.google.com designates 209.85.221.74 as permitted sender) smtp.mailfrom=3VWVcZwUKCNIHyzzy4CC492.0CA96BIL-AA8Jy08.CF4@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734108475; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=aq6uxvVbG5xxzgybZF/l/V4c8HPIK8uPOyna1cvYXWA=; b=ssCkMs7ljXE1OmDua3b8joTRKBh850IoiiOJQrhCG3+qRC8UEFqZJ6WzAuGN0002kUPoWS 7rzgaHX2UFU9PYwdTga6QfVzXXTCi+WFfU/DTKFY7b6+TIiTW3Q+dc88AG7keSzLQqh1xj pzR60LEIwSBwJfJJc6e31bypLOE7Sbo= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734108475; a=rsa-sha256; cv=none; b=j4om5IYVS4TJVB5LmqHe+aX4C8V6L7I0YGZt0fwbeMc2FX5RTFZbf8+Lo03wpJR8Jk/yrh vbn7b05Z9ij7Jv4sIVCACjNTu8MwELr6+QpT6j8Rc/KsunT7yF3bsLo41GEttxTFrsUPAj pYn2HG/7XoXhfamfD6sx+FJdkTG+2Ps= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=D22tQ+Q5; spf=pass (imf19.hostedemail.com: domain of 3VWVcZwUKCNIHyzzy4CC492.0CA96BIL-AA8Jy08.CF4@flex--tabba.bounces.google.com designates 209.85.221.74 as permitted sender) smtp.mailfrom=3VWVcZwUKCNIHyzzy4CC492.0CA96BIL-AA8Jy08.CF4@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-385e1fd40acso1216305f8f.3 for ; Fri, 13 Dec 2024 08:48:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734108501; x=1734713301; darn=kvack.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=aq6uxvVbG5xxzgybZF/l/V4c8HPIK8uPOyna1cvYXWA=; b=D22tQ+Q5ssJhjtnGOER8DeuON/xZM4ZuurcZ3+CpgS3THt9jdjwZRcsYCqHnE6aZyy Nyx4GMH7fY6fjxW7LZ+r7FUdLflObqiUbWgJR6m9vtQNUbOObRsCBgwrTcUtkpK9ob1D q/g7wrtfhyJZdks1S8Ey9cW1bO3EYls4D6yDPaE1SrGM6aDcPJeOhUC+Pf2cNEV39JE6 KlrVw0twC+OM14QLb8KQcv0GYpnAiBVkuvn5hNK8Vkl3Kx6orKN2/+06UoB4fCQof65v lRiv4ao87T+QR+Etr0irQWyc8N7ouMcNJSyqCgtgFTLP5iIL49NIG/H0RVjrPdTRJOtj rdcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734108501; x=1734713301; 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=aq6uxvVbG5xxzgybZF/l/V4c8HPIK8uPOyna1cvYXWA=; b=vhT/anu57ChBEN/QbhQZELL9UqkL94zMXymTyVUguZXR/Gd+J8LsGaPtLzrPTXGzcl Y7f88xfVHA2Bw54P1Xj6Xsur1nBMUeZNDYXHw1XONsrFLTOxD/AvseHK5QEbS9+ZO/Bo qij0XCzsRw0Vh35gkMz668ucYFoYCJPC5fLV8tYdtQYfXNTIRuUn+Hpfwykx3lHcLQpR vGi7lTQI/DX184IwnidHr93+cgT3Z64kMpnFMtJPHwjxR/v9vjZuLzTW6MtXnehERMPC RMNDhjXvb1SxkJHIZvSmjc/4gaugSDrrl4NouxaGnR0pgLDNvY/rM93RqT5Ct0ZWSpU+ 3sQQ== X-Forwarded-Encrypted: i=1; AJvYcCUZW3tmME3ZNiN0p+TxU7DtJsDrBn8xCO4FUVbZ3byK6Z4GZFR6nhhFcZvAzkUgEeX0rdmcVa8wuA==@kvack.org X-Gm-Message-State: AOJu0YwhmVvyKj+0XdVKtSPJNGcGYC2Ss2yt98UHYUs9C5EXd+q3CTU7 kO4Y+gE6kr8xsMBL4R3WVBBwxoRZ7rnr//I6sYfkDfiPkK3HWCtKzVAxhKl0RmoESzYHBSV2Gw= = X-Google-Smtp-Source: AGHT+IFSvG/fqSl7ZNJ3qm5zCF5qD4QOJhC1Avgrxg1Y/6IRPNfNklxASuKBBLtf2tg/8VDvraqzyfwYkg== X-Received: from wmbjl5.prod.google.com ([2002:a05:600c:6a85:b0:434:f119:f1a]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:4b12:b0:385:fc70:7eb with SMTP id ffacd0b85a97d-38880ac252bmr2943992f8f.12.1734108501569; Fri, 13 Dec 2024 08:48:21 -0800 (PST) Date: Fri, 13 Dec 2024 16:48:00 +0000 In-Reply-To: <20241213164811.2006197-1-tabba@google.com> Mime-Version: 1.0 References: <20241213164811.2006197-1-tabba@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241213164811.2006197-5-tabba@google.com> Subject: [RFC PATCH v4 04/14] KVM: guest_memfd: Track mappability within a struct kvm_gmem_private From: Fuad Tabba To: kvm@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-mm@kvack.org Cc: pbonzini@redhat.com, chenhuacai@kernel.org, mpe@ellerman.id.au, anup@brainfault.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, seanjc@google.com, viro@zeniv.linux.org.uk, brauner@kernel.org, willy@infradead.org, akpm@linux-foundation.org, xiaoyao.li@intel.com, yilun.xu@intel.com, chao.p.peng@linux.intel.com, jarkko@kernel.org, amoorthy@google.com, dmatlack@google.com, yu.c.zhang@linux.intel.com, isaku.yamahata@intel.com, mic@digikod.net, vbabka@suse.cz, vannapurve@google.com, ackerleytng@google.com, mail@maciej.szmigiero.name, david@redhat.com, michael.roth@amd.com, wei.w.wang@intel.com, liam.merwick@oracle.com, isaku.yamahata@gmail.com, kirill.shutemov@linux.intel.com, suzuki.poulose@arm.com, steven.price@arm.com, quic_eberman@quicinc.com, quic_mnalajal@quicinc.com, quic_tsoni@quicinc.com, quic_svaddagi@quicinc.com, quic_cvanscha@quicinc.com, quic_pderrin@quicinc.com, quic_pheragu@quicinc.com, catalin.marinas@arm.com, james.morse@arm.com, yuzenghui@huawei.com, oliver.upton@linux.dev, maz@kernel.org, will@kernel.org, qperret@google.com, keirf@google.com, roypat@amazon.co.uk, shuah@kernel.org, hch@infradead.org, jgg@nvidia.com, rientjes@google.com, jhubbard@nvidia.com, fvdl@google.com, hughd@google.com, jthoughton@google.com, tabba@google.com X-Rspamd-Queue-Id: 9A1DF1A000E X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: q5w89ccz19nsofkyw5zpa7hoicqohqyb X-HE-Tag: 1734108475-477852 X-HE-Meta: U2FsdGVkX19549+q4ogTgpoHa/K9AcEYqHStzWWqeTD6o+eDZjV97twcuUxUPF53zdnFyD7lJFxcQ0TAy1s1z2M/a07V5Ugb0s9oFrDUoh9dWlxkIaHk+IY+dN3oRp3/5n6ScDoF6ZX35aYtBRoyIYujIC3msCtde/Pqkt4TKbkfXz5cEt+NTFb9pqXJfjVzQCw17TPvFBhCg4em6jfz0NxGfZzjT+qau96Ef2Z2lRM1vrsTm91t2kHI99q3tP6HJ7dogZHYDGoGrV887a0iURIE9N50vt31s+NukcZ7uJxhrfsCj9OoaOzdj1TfKv9ELggcvK8R1ulEMeOBVeczO7rqrB5obOXi36CJmCvP5GaTDl4C2pkJ9sJkMzmmHNQLnbCK8bmjrIqwiofXpEIc4ti2V1UsCy1a4viC9buvWBxRTMjZoIfnN7WcQN+cpL3fVcB8X8Uu9/lwoiP2osPP9khCnleYU5HSbXoM46LiriSTBYFRwmOGcIZBC1av0C1b776uh6+pi5s3ilpnWUgcQBXyJYq4657ntauFLd3jGI5iDovoSecQhvNUhrqAhdeEJ4qARm5QFiHOHR2huB8hcTzMETbSpzPy6bNOhOOf9pJ+2VlhjP5TJGgsUrCfTwOGPdvrYYR/FOgBu6hqD3pbsgJ6jGPUm2MtJx3DbzFEfFu45n5UKEriDwibfaSPM2EPskGgSSdIuL0t6yWphm4bftTYXqqejON4KKxVWDji8JbGlRw2YGEnFqg0okwDUN9fdxBYDj96zKIdwO8cJXLbaVg1Y9mVBI5sJN87PFG2p/1dFKEC0OuH1ZPsiWPzAh3oo7F0nm1ID6rIH3LAKO0xYonrnV7oL0E5Cpy0L7uzeZrjFvfB7DW3lzXsWqTJn2AM6ELjf1uAe6MO7QbZ6JoB6lS7QbXJKe4ASRpVMqraUU0zmLtsGdd9lielnqwX3hhggA4MZeR52NoGdMhbbAp 8v2ZiQXZ x1DiBx+R4CoPRnWeAkFkFBiSeHZARFsvvbWg6XQ/A3rXZsMNsD1YX0sKXP/8pGRE+y2li4ltGOW7e5ODdZgyJml2i+FCATVvaD0OUVIH/eRwcvJYDeAr/UHHQUjQorAviHEBMKQzDukRqqvgjWgtVrT086poMIAaIjgFG2he52LM6ZqMWPpddZFyjVQXKWofJ/0a5WuAbJiGzeK6hA/PafEkIE1dQo38gZL2oZUvSyj/KWzsR58UHdeBwewZCiIGjugkd8FCYQIBMPHU/hWFMhtH6vh52Xao1MVyOsnWqo2hP5plXMN6JFyhmSCnah9nvsaJWgqoNcZ+HceT7woO2EDVWDPbvKn+laMftpP2JrYBW8pq4iKtmq82CYX44RjJP2rKr846KsCWk92h/1RJ6rTW2qcH6UA+vvz+vBE/B2xjXb/N2v1dGi8irSsCBaqceHyLI3nPYpk9HH/MAXpWoOrM+/zdCsujV1OP+R+YgRclmG/iYqYPFWMfxxIoCWzocHLYFLItts4MkB6uIqOsQmxaNfRqRUbN2ttce9DYKQzqGwDaGmRrKMYmUNiJCDkyhzoTTqD3oZh1C0AdrnGTvaTIAb7OuE2OHdPnvK+wbVdFndBKcLzoQvuxAIca+LgOFHNTRSLTlPGcimugomEZ56di+8QbIwPxcvgpj5g+g2oPx5noRVeNfOauTyQ== X-Bogosity: Ham, 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: From: Ackerley Tng Track whether guest_memfd memory can be mapped within the inode, since it is property of the guest_memfd's memory contents. The guest_memfd PRIVATE memory attribute is not used for two reasons. First because it reflects the userspace expectation for that memory location, and therefore can be toggled by userspace. The second is, although each guest_memfd file has a 1:1 binding with a KVM instance, the plan is to allow multiple files per inode, e.g. to allow intra-host migration to a new KVM instance, without destroying guest_memfd. Signed-off-by: Ackerley Tng Co-developed-by: Vishal Annapurve Signed-off-by: Vishal Annapurve Co-developed-by: Fuad Tabba Signed-off-by: Fuad Tabba --- virt/kvm/guest_memfd.c | 56 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 5 deletions(-) diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 6453658d2650..0a7b6cf8bd8f 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -18,6 +18,17 @@ struct kvm_gmem { struct list_head entry; }; +struct kvm_gmem_inode_private { +#ifdef CONFIG_KVM_GMEM_MAPPABLE + struct xarray mappable_offsets; +#endif +}; + +static struct kvm_gmem_inode_private *kvm_gmem_private(struct inode *inode) +{ + return inode->i_mapping->i_private_data; +} + /** * folio_file_pfn - like folio_file_page, but return a pfn. * @folio: The folio which contains this index. @@ -312,8 +323,28 @@ static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn) return gfn - slot->base_gfn + slot->gmem.pgoff; } +static void kvm_gmem_evict_inode(struct inode *inode) +{ + struct kvm_gmem_inode_private *private = kvm_gmem_private(inode); + +#ifdef CONFIG_KVM_GMEM_MAPPABLE + /* + * .evict_inode can be called before private data is set up if there are + * issues during inode creation. + */ + if (private) + xa_destroy(&private->mappable_offsets); +#endif + + truncate_inode_pages_final(inode->i_mapping); + + kfree(private); + clear_inode(inode); +} + static const struct super_operations kvm_gmem_super_operations = { - .statfs = simple_statfs, + .statfs = simple_statfs, + .evict_inode = kvm_gmem_evict_inode, }; static int kvm_gmem_init_fs_context(struct fs_context *fc) @@ -440,6 +471,7 @@ static struct inode *kvm_gmem_inode_make_secure_inode(const char *name, loff_t size, u64 flags) { const struct qstr qname = QSTR_INIT(name, strlen(name)); + struct kvm_gmem_inode_private *private; struct inode *inode; int err; @@ -448,10 +480,19 @@ static struct inode *kvm_gmem_inode_make_secure_inode(const char *name, return inode; err = security_inode_init_security_anon(inode, &qname, NULL); - if (err) { - iput(inode); - return ERR_PTR(err); - } + if (err) + goto out; + + err = -ENOMEM; + private = kzalloc(sizeof(*private), GFP_KERNEL); + if (!private) + goto out; + +#ifdef CONFIG_KVM_GMEM_MAPPABLE + xa_init(&private->mappable_offsets); +#endif + + inode->i_mapping->i_private_data = private; inode->i_private = (void *)(unsigned long)flags; inode->i_op = &kvm_gmem_iops; @@ -464,6 +505,11 @@ static struct inode *kvm_gmem_inode_make_secure_inode(const char *name, WARN_ON_ONCE(!mapping_unevictable(inode->i_mapping)); return inode; + +out: + iput(inode); + + return ERR_PTR(err); } static struct file *kvm_gmem_inode_create_getfile(void *priv, loff_t size, From patchwork Fri Dec 13 16:48:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13907463 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 D1045E7717F for ; Fri, 13 Dec 2024 16:48:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 687776B00A1; Fri, 13 Dec 2024 11:48:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 5E9FC6B00A2; Fri, 13 Dec 2024 11:48:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 39FB56B00A3; Fri, 13 Dec 2024 11:48:27 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 16A596B00A1 for ; Fri, 13 Dec 2024 11:48:27 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id BA5978103E for ; Fri, 13 Dec 2024 16:48:26 +0000 (UTC) X-FDA: 82890517164.17.0E6FFE5 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf28.hostedemail.com (Postfix) with ESMTP id C5C61C0026 for ; Fri, 13 Dec 2024 16:47:55 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=JVigBCRz; spf=pass (imf28.hostedemail.com: domain of 3V2VcZwUKCNQJ01106EE6B4.2ECB8DKN-CCAL02A.EH6@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3V2VcZwUKCNQJ01106EE6B4.2ECB8DKN-CCAL02A.EH6@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734108485; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=vJuueBr2gSdijh3IQFMZdg4//VRgMvs45ltnyf1edv8=; b=kJSoYFhio7tHTCSXnS2Z6gAaqq35/f1sE387A45FXOluP9Rx8ebkMuZaoh8FGozwHCB7RP fD+2cT4B3Axd5V3JggBUYHvkTHP8Q+j0P2H+zxBFutOW4oHnU42ppCumd8qgHDlku7U2tW Eyu+TvQtVAt9nNr1lDyLvDploCa0y/8= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=JVigBCRz; spf=pass (imf28.hostedemail.com: domain of 3V2VcZwUKCNQJ01106EE6B4.2ECB8DKN-CCAL02A.EH6@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3V2VcZwUKCNQJ01106EE6B4.2ECB8DKN-CCAL02A.EH6@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734108485; a=rsa-sha256; cv=none; b=YXusgieVrZ4ssTXIM9y+ig2t4D/T/sIAuCON5DpZP9IVyRhLOD8veLQObDni7tJd2cTMHr 96oynegmQvUAKHWweqsaekdwwwU2M4Pj5y7NovHRNcastrDDA53i3jc2o48hkPhg4+2iDA o1DCrgzd5XNRDqTsjKt5ymFy1GS/YOA= Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-43627bb20b5so13235315e9.1 for ; Fri, 13 Dec 2024 08:48:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734108503; x=1734713303; darn=kvack.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=vJuueBr2gSdijh3IQFMZdg4//VRgMvs45ltnyf1edv8=; b=JVigBCRzM5v/u6+Pjo42TTJ4uhJkPadZczmhEJUEFEhtUE+yF6Y35jjjvaO7quxZIe OnakfMa4VNouDdik3CEl6ajSFWFkMxx1NflmmuspTrr3E17/u4mh9IgI7V9i1TL5vL5l wxqV5PpWa/SomlmMopBHYVTuLgbU+K2FCS9+3IVlTDdWJSWDWHaXrZ2ePYekwRVXKYE3 YZ6haPYPU6TS4OuPAt+1EOkY0+PCyzfEdYj/Z4FsdJiNzDLf0xM6tvYjm1u1KbRPa36E pTRI5oN6q4E+9T+w3yixVW9+JZ26QIWbwqam2fBSDtpcAyq17+gS4z+zYrnVyR/gmnTk VxzQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734108503; x=1734713303; 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=vJuueBr2gSdijh3IQFMZdg4//VRgMvs45ltnyf1edv8=; b=dysChiyN8VKca1eSMd/7NPWA271q4b5CrYPaIa8fG2VToSV+J4/TVXDhsK6BOYzav5 G1ZVqZqGeMdhH6ZlS9EWQ+biA6dXsgtio9ld+IhwWiCShKn82YyzrLLi/jCJX+uVGqTB +JfCHf/xdgrhGCxTBjXsWkNxhpLNqLdYaW5ahUKNB0Pf4YNfMXdWrhyb03HGuLvqaKOJ DbejsjWE3KODLTPZY5nZgMqLFFBz5hMlrVt5Ikc4J4cS8foZFQ7fix9pLB1T5FW5RNF3 ZBzG/6i5SA+3M+D7AjUNUDc2YYDcX8D2EzAns7TVEZA70ftLycWytxDHHtkJlKSx5rzf CnJw== X-Forwarded-Encrypted: i=1; AJvYcCVbXc6aSMylCnoPNoROsDSRwJWpONPVwqOIipj5sFgbZLOFExlipfH6J98YiSuYYboiGdHMtv2+5Q==@kvack.org X-Gm-Message-State: AOJu0YwyURwDuWxMeJH+i6lgEun9strWhChz3BVfKtGu/gzku3Ikp2ZR dJUFXmWOGE8ufRW+mhS4slPzPZLrd2ZVwgq33WRTW3jQcGk6mgMeruwH5ycuCN4MTsOFZ4HTwQ= = X-Google-Smtp-Source: AGHT+IFkQF+3tcycznq/pNNLMGlVHQ2lVFQl1W+VwFJakHWzfT3BbDdg+Hikl/qFw92f/o0UH09Yhh+yDw== X-Received: from wmhp18.prod.google.com ([2002:a05:600c:4192:b0:434:9de6:413a]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1e19:b0:435:1a2:2633 with SMTP id 5b1f17b1804b1-4362aa52c65mr29622895e9.15.1734108503647; Fri, 13 Dec 2024 08:48:23 -0800 (PST) Date: Fri, 13 Dec 2024 16:48:01 +0000 In-Reply-To: <20241213164811.2006197-1-tabba@google.com> Mime-Version: 1.0 References: <20241213164811.2006197-1-tabba@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241213164811.2006197-6-tabba@google.com> Subject: [RFC PATCH v4 05/14] KVM: guest_memfd: Folio mappability states and functions that manage their transition From: Fuad Tabba To: kvm@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-mm@kvack.org Cc: pbonzini@redhat.com, chenhuacai@kernel.org, mpe@ellerman.id.au, anup@brainfault.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, seanjc@google.com, viro@zeniv.linux.org.uk, brauner@kernel.org, willy@infradead.org, akpm@linux-foundation.org, xiaoyao.li@intel.com, yilun.xu@intel.com, chao.p.peng@linux.intel.com, jarkko@kernel.org, amoorthy@google.com, dmatlack@google.com, yu.c.zhang@linux.intel.com, isaku.yamahata@intel.com, mic@digikod.net, vbabka@suse.cz, vannapurve@google.com, ackerleytng@google.com, mail@maciej.szmigiero.name, david@redhat.com, michael.roth@amd.com, wei.w.wang@intel.com, liam.merwick@oracle.com, isaku.yamahata@gmail.com, kirill.shutemov@linux.intel.com, suzuki.poulose@arm.com, steven.price@arm.com, quic_eberman@quicinc.com, quic_mnalajal@quicinc.com, quic_tsoni@quicinc.com, quic_svaddagi@quicinc.com, quic_cvanscha@quicinc.com, quic_pderrin@quicinc.com, quic_pheragu@quicinc.com, catalin.marinas@arm.com, james.morse@arm.com, yuzenghui@huawei.com, oliver.upton@linux.dev, maz@kernel.org, will@kernel.org, qperret@google.com, keirf@google.com, roypat@amazon.co.uk, shuah@kernel.org, hch@infradead.org, jgg@nvidia.com, rientjes@google.com, jhubbard@nvidia.com, fvdl@google.com, hughd@google.com, jthoughton@google.com, tabba@google.com X-Rspamd-Queue-Id: C5C61C0026 X-Rspamd-Server: rspam12 X-Stat-Signature: pp4pkcyiy1cd47fjtsozso49sk37obkj X-Rspam-User: X-HE-Tag: 1734108475-734481 X-HE-Meta: U2FsdGVkX19lniVYOSnLxLDidHzXdV/UqoHZEaeycVlfGQsnxZdfDRWbnW/zUJDbJfPBs2P8OOyBFuFnOT5SOCgfndC52iQKhEc+dyjU5ihK4HXhsl2YuOZJ439IrKoMaJ/aw6S8FKHEXmqOW9XD8DUlpOuaVW7y7YP4w3Y3ogsRBG/6fmgapyVMeI1MJc5GFqQyitsWMuJfKIAGopvVq5dJi4m1qT6pifx5k+EnKbbFnq6TF9ZOPvutJOPVvSifllqSjePF4qwBrXTr6rnCO1exKcaXbvgLsOjU6mexRu4TozAR49GRlG3OKMSmW7I0oXPa36mPO6buOCpyHpKvVm0aN+KPHN6zZur18XPQ68JokigW1QEqqzdhJiVDS7NPdtY+EuM9nVRM3g3qQP+tpJ43tx8t1JHcXr3Ymtj6tdNZzZOl7h1n6oe+A04cNFvsTP/yGQlbHaBTAFfQmI8znUeOd+dAIEjwTz4OSd/npPjXOD5RyL07LtOFTue91GZuxFURuOprtPL/k1o3b2Yc8IOhP5Olxec+I5JAk/VGJeGx3zlH4426A/nT/R1EcOkWL4KT4UdlB+SVjwhMZx/w1dyDP2C3TXfCkZqlekcb7pPsYfhIeM2fEEQwp1fdnTvPUcLxSST9sg1ur58esbH0Uc7/QyR48xrzolYgw5ijFUX4FcINqvBrf+sLZbHPXPHtHwnqJduM5g7ItX76LBQafxuSuKMb1fHCl9jBq+lp9J9dpIy/VptngIOW+aHbzTzcIwJbX2HErW5oz//76xLFUDAwgOHsC2L8oODTRf45Qcs0P8F0xowWQ8q39ZZ/ezgyKpJyDkfQ005AgSRIc266SzZlm8W0PazhYt4nl/bqNHV9VG/GDEAfxVHGwdps8ud/35sDADcxPALzfgGaFhh1ZQ+1Vt09mNIsYW1b2IPz/aC2GMn0whSgMy7vIHIF7t4IqQYqeIPyhPyWsFYNS8b X+iFzzmN VeE2Q73mhesC1k/K4CkUgm/qjjk5+Fd9FjmkdkNcKiD6TsgXx8MKKvJWiTLNg6G/E7Ugjql9aqZo2do0xERFmmge0N//TB3NR580i/mONSii8k27A8iVwY39l51JUlGWFPsa5V4TSi6J85ZY9QINkxfWGtPJvhloeooxqf1c0oh7nVGhGFaJ5rgNGe5ezhZg3yxKa6FZ88xx06rBYDsdEzTQDT0ls5q0kgzNwH6Z+MZJbgGPquJzk0azGoOQDZ1NTfJ/jrwcvKdORExgfpyfQtaPJUH9J7KCrbSYpQz/AORk3rVCvlF1S0hkgg1TXaXLxJCc2/JIUOr6FZJf4+EWq12mZQ2gY6FkFcV1g5NINBbnLjrNYj5L/sZZ+3jiXQGy+R8n4oCpVdlBNqvEA0Nk1oewuuzcwHG/3AMxqodLX2XWC4jJiL8bmJlKYZtWy1gPuNSHz56PbuNREe5GjTBS3nKy5+MHvmPUiMTk7OTVQSXMXPUtFIAj2i3QFBBXEUdzHhTN93GU7e4/oJTUGdCTykkf3yiBT4Aa/0o07+fDZh62Gca1SmaNTEVgR9NbRYdtX0zMFFWl0Gag8FnL8yO3eHBtnDcbgPhvYfr0HLk0ia8IRrXi3pGfe0b9Bra/CzJResBx4kxZDNEW7RefwNQpLNk9XhQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000112, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: To allow restricted mapping of guest_memfd folios by the host, guest_memfd needs to track whether they can be mapped and by who, since the mapping will only be allowed under conditions where it safe to access these folios. These conditions depend on the folios being explicitly shared with the host, or not yet exposed to the guest (e.g., at initialization). This patch introduces states that determine whether the host and the guest can fault in the folios as well as the functions that manage transitioning between those states. Signed-off-by: Fuad Tabba --- include/linux/kvm_host.h | 53 ++++++++++++++ virt/kvm/guest_memfd.c | 153 +++++++++++++++++++++++++++++++++++++++ virt/kvm/kvm_main.c | 92 +++++++++++++++++++++++ 3 files changed, 298 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index cda3ed4c3c27..84aa7908a5dd 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2564,4 +2564,57 @@ long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu, struct kvm_pre_fault_memory *range); #endif +#ifdef CONFIG_KVM_GMEM_MAPPABLE +bool kvm_gmem_is_mappable(struct kvm *kvm, gfn_t gfn, gfn_t end); +int kvm_gmem_set_mappable(struct kvm *kvm, gfn_t start, gfn_t end); +int kvm_gmem_clear_mappable(struct kvm *kvm, gfn_t start, gfn_t end); +int kvm_slot_gmem_set_mappable(struct kvm_memory_slot *slot, gfn_t start, + gfn_t end); +int kvm_slot_gmem_clear_mappable(struct kvm_memory_slot *slot, gfn_t start, + gfn_t end); +bool kvm_slot_gmem_is_mappable(struct kvm_memory_slot *slot, gfn_t gfn); +bool kvm_slot_gmem_is_guest_mappable(struct kvm_memory_slot *slot, gfn_t gfn); +#else +static inline bool kvm_gmem_is_mappable(struct kvm *kvm, gfn_t gfn, gfn_t end) +{ + WARN_ON_ONCE(1); + return false; +} +static inline int kvm_gmem_set_mappable(struct kvm *kvm, gfn_t start, gfn_t end) +{ + WARN_ON_ONCE(1); + return -EINVAL; +} +static inline int kvm_gmem_clear_mappable(struct kvm *kvm, gfn_t start, + gfn_t end) +{ + WARN_ON_ONCE(1); + return -EINVAL; +} +static inline int kvm_slot_gmem_set_mappable(struct kvm_memory_slot *slot, + gfn_t start, gfn_t end) +{ + WARN_ON_ONCE(1); + return -EINVAL; +} +static inline int kvm_slot_gmem_clear_mappable(struct kvm_memory_slot *slot, + gfn_t start, gfn_t end) +{ + WARN_ON_ONCE(1); + return -EINVAL; +} +static inline bool kvm_slot_gmem_is_mappable(struct kvm_memory_slot *slot, + gfn_t gfn) +{ + WARN_ON_ONCE(1); + return false; +} +static inline bool kvm_slot_gmem_is_guest_mappable(struct kvm_memory_slot *slot, + gfn_t gfn) +{ + WARN_ON_ONCE(1); + return false; +} +#endif /* CONFIG_KVM_GMEM_MAPPABLE */ + #endif diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 0a7b6cf8bd8f..d1c192927cf7 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -375,6 +375,159 @@ static void kvm_gmem_init_mount(void) kvm_gmem_mnt->mnt_flags |= MNT_NOEXEC; } +#ifdef CONFIG_KVM_GMEM_MAPPABLE +/* + * An enum of the valid states that describe who can map a folio. + * Bit 0: if set guest cannot map the page + * Bit 1: if set host cannot map the page + */ +enum folio_mappability { + KVM_GMEM_ALL_MAPPABLE = 0b00, /* Mappable by host and guest. */ + KVM_GMEM_GUEST_MAPPABLE = 0b10, /* Mappable only by guest. */ + KVM_GMEM_NONE_MAPPABLE = 0b11, /* Not mappable, transient state. */ +}; + +/* + * Marks the range [start, end) as mappable by both the host and the guest. + * Usually called when guest shares memory with the host. + */ +static int gmem_set_mappable(struct inode *inode, pgoff_t start, pgoff_t end) +{ + struct xarray *mappable_offsets = &kvm_gmem_private(inode)->mappable_offsets; + void *xval = xa_mk_value(KVM_GMEM_ALL_MAPPABLE); + pgoff_t i; + int r = 0; + + filemap_invalidate_lock(inode->i_mapping); + for (i = start; i < end; i++) { + r = xa_err(xa_store(mappable_offsets, i, xval, GFP_KERNEL)); + if (r) + break; + } + filemap_invalidate_unlock(inode->i_mapping); + + return r; +} + +/* + * Marks the range [start, end) as not mappable by the host. If the host doesn't + * have any references to a particular folio, then that folio is marked as + * mappable by the guest. + * + * However, if the host still has references to the folio, then the folio is + * marked and not mappable by anyone. Marking it is not mappable allows it to + * drain all references from the host, and to ensure that the hypervisor does + * not transition the folio to private, since the host still might access it. + * + * Usually called when guest unshares memory with the host. + */ +static int gmem_clear_mappable(struct inode *inode, pgoff_t start, pgoff_t end) +{ + struct xarray *mappable_offsets = &kvm_gmem_private(inode)->mappable_offsets; + void *xval_guest = xa_mk_value(KVM_GMEM_GUEST_MAPPABLE); + void *xval_none = xa_mk_value(KVM_GMEM_NONE_MAPPABLE); + pgoff_t i; + int r = 0; + + filemap_invalidate_lock(inode->i_mapping); + for (i = start; i < end; i++) { + struct folio *folio; + int refcount = 0; + + folio = filemap_lock_folio(inode->i_mapping, i); + if (!IS_ERR(folio)) { + refcount = folio_ref_count(folio); + } else { + r = PTR_ERR(folio); + if (WARN_ON_ONCE(r != -ENOENT)) + break; + + folio = NULL; + } + + /* +1 references are expected because of filemap_lock_folio(). */ + if (folio && refcount > folio_nr_pages(folio) + 1) { + /* + * Outstanding references, the folio cannot be faulted + * in by anyone until they're dropped. + */ + r = xa_err(xa_store(mappable_offsets, i, xval_none, GFP_KERNEL)); + } else { + /* + * No outstanding references. Transition the folio to + * guest mappable immediately. + */ + r = xa_err(xa_store(mappable_offsets, i, xval_guest, GFP_KERNEL)); + } + + if (folio) { + folio_unlock(folio); + folio_put(folio); + } + + if (WARN_ON_ONCE(r)) + break; + } + filemap_invalidate_unlock(inode->i_mapping); + + return r; +} + +static bool gmem_is_mappable(struct inode *inode, pgoff_t pgoff) +{ + struct xarray *mappable_offsets = &kvm_gmem_private(inode)->mappable_offsets; + unsigned long r; + + r = xa_to_value(xa_load(mappable_offsets, pgoff)); + + return (r == KVM_GMEM_ALL_MAPPABLE); +} + +static bool gmem_is_guest_mappable(struct inode *inode, pgoff_t pgoff) +{ + struct xarray *mappable_offsets = &kvm_gmem_private(inode)->mappable_offsets; + unsigned long r; + + r = xa_to_value(xa_load(mappable_offsets, pgoff)); + + return (r == KVM_GMEM_ALL_MAPPABLE || r == KVM_GMEM_GUEST_MAPPABLE); +} + +int kvm_slot_gmem_set_mappable(struct kvm_memory_slot *slot, gfn_t start, gfn_t end) +{ + struct inode *inode = file_inode(slot->gmem.file); + pgoff_t start_off = slot->gmem.pgoff + start - slot->base_gfn; + pgoff_t end_off = start_off + end - start; + + return gmem_set_mappable(inode, start_off, end_off); +} + +int kvm_slot_gmem_clear_mappable(struct kvm_memory_slot *slot, gfn_t start, gfn_t end) +{ + struct inode *inode = file_inode(slot->gmem.file); + pgoff_t start_off = slot->gmem.pgoff + start - slot->base_gfn; + pgoff_t end_off = start_off + end - start; + + return gmem_clear_mappable(inode, start_off, end_off); +} + +bool kvm_slot_gmem_is_mappable(struct kvm_memory_slot *slot, gfn_t gfn) +{ + struct inode *inode = file_inode(slot->gmem.file); + unsigned long pgoff = slot->gmem.pgoff + gfn - slot->base_gfn; + + return gmem_is_mappable(inode, pgoff); +} + +bool kvm_slot_gmem_is_guest_mappable(struct kvm_memory_slot *slot, gfn_t gfn) +{ + struct inode *inode = file_inode(slot->gmem.file); + unsigned long pgoff = slot->gmem.pgoff + gfn - slot->base_gfn; + + return gmem_is_guest_mappable(inode, pgoff); +} +#endif /* CONFIG_KVM_GMEM_MAPPABLE */ + static struct file_operations kvm_gmem_fops = { .open = generic_file_open, .release = kvm_gmem_release, diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index de2c11dae231..fffff01cebe7 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3094,6 +3094,98 @@ static int next_segment(unsigned long len, int offset) return len; } +#ifdef CONFIG_KVM_GMEM_MAPPABLE +bool kvm_gmem_is_mappable(struct kvm *kvm, gfn_t start, gfn_t end) +{ + struct kvm_memslot_iter iter; + bool r = true; + + mutex_lock(&kvm->slots_lock); + + kvm_for_each_memslot_in_gfn_range(&iter, kvm_memslots(kvm), start, end) { + struct kvm_memory_slot *memslot = iter.slot; + gfn_t gfn_start, gfn_end, i; + + if (!kvm_slot_can_be_private(memslot)) + continue; + + gfn_start = max(start, memslot->base_gfn); + gfn_end = min(end, memslot->base_gfn + memslot->npages); + if (WARN_ON_ONCE(gfn_start >= gfn_end)) + continue; + + for (i = gfn_start; i < gfn_end; i++) { + r = kvm_slot_gmem_is_mappable(memslot, i); + if (r) + goto out; + } + } +out: + mutex_unlock(&kvm->slots_lock); + + return r; +} + +int kvm_gmem_set_mappable(struct kvm *kvm, gfn_t start, gfn_t end) +{ + struct kvm_memslot_iter iter; + int r = 0; + + mutex_lock(&kvm->slots_lock); + + kvm_for_each_memslot_in_gfn_range(&iter, kvm_memslots(kvm), start, end) { + struct kvm_memory_slot *memslot = iter.slot; + gfn_t gfn_start, gfn_end; + + if (!kvm_slot_can_be_private(memslot)) + continue; + + gfn_start = max(start, memslot->base_gfn); + gfn_end = min(end, memslot->base_gfn + memslot->npages); + if (WARN_ON_ONCE(start >= end)) + continue; + + r = kvm_slot_gmem_set_mappable(memslot, gfn_start, gfn_end); + if (WARN_ON_ONCE(r)) + break; + } + + mutex_unlock(&kvm->slots_lock); + + return r; +} + +int kvm_gmem_clear_mappable(struct kvm *kvm, gfn_t start, gfn_t end) +{ + struct kvm_memslot_iter iter; + int r = 0; + + mutex_lock(&kvm->slots_lock); + + kvm_for_each_memslot_in_gfn_range(&iter, kvm_memslots(kvm), start, end) { + struct kvm_memory_slot *memslot = iter.slot; + gfn_t gfn_start, gfn_end; + + if (!kvm_slot_can_be_private(memslot)) + continue; + + gfn_start = max(start, memslot->base_gfn); + gfn_end = min(end, memslot->base_gfn + memslot->npages); + if (WARN_ON_ONCE(start >= end)) + continue; + + r = kvm_slot_gmem_clear_mappable(memslot, gfn_start, gfn_end); + if (WARN_ON_ONCE(r)) + break; + } + + mutex_unlock(&kvm->slots_lock); + + return r; +} + +#endif /* CONFIG_KVM_GMEM_MAPPABLE */ + /* Copy @len bytes from guest memory at '(@gfn * PAGE_SIZE) + @offset' to @data */ static int __kvm_read_guest_page(struct kvm_memory_slot *slot, gfn_t gfn, void *data, int offset, int len) From patchwork Fri Dec 13 16:48:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13907464 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 5E242E77180 for ; Fri, 13 Dec 2024 16:48:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 996696B00A3; Fri, 13 Dec 2024 11:48:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 944D96B00A4; Fri, 13 Dec 2024 11:48:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 721AA6B00A5; Fri, 13 Dec 2024 11:48:29 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 42E566B00A3 for ; Fri, 13 Dec 2024 11:48:29 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 0B4CFA0CC2 for ; Fri, 13 Dec 2024 16:48:29 +0000 (UTC) X-FDA: 82890518046.27.D7501EC Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) by imf23.hostedemail.com (Postfix) with ESMTP id 14B7D14000A for ; Fri, 13 Dec 2024 16:48:09 +0000 (UTC) Authentication-Results: imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=FKYBsrGa; spf=pass (imf23.hostedemail.com: domain of 3WWVcZwUKCNYL23328GG8D6.4GEDAFMP-EECN24C.GJ8@flex--tabba.bounces.google.com designates 209.85.221.74 as permitted sender) smtp.mailfrom=3WWVcZwUKCNYL23328GG8D6.4GEDAFMP-EECN24C.GJ8@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734108490; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=gdnqwTM+ekDbCobF/sslf1fAX7qCXn0A+3Ru0J9WkCw=; b=BYrNRZi/jFj8MMt1cFLujiGU8rZw1LnZAdQLU7xGKCXsOO+D1cIQnS6/t2yyBYQyqXHqIo mJFtVPpBIYY9FwHSQkOzUQ4gA4dYPbGC/1T55rmZ7cl0rugoq6dPFvw4U1zN8dcEF1OpP3 +bxgUkoAiHTF5QICuPd2P+0HmiXvDWk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734108490; a=rsa-sha256; cv=none; b=z4fTi0Cg51UrAu29H38OTmo+QhnSvYbShZFPZvks4xUEUcQwQG9x06bxV22tvnSeKoKYKA 0yXTBJPNalEnO7qFjbvmiiiPus+WUifNcQ5lMQ9JU55TzKXKICNjjaCyPMZa1tDyNrm3sX GQsmckjkaQ+LXUxCRShzAS/Js0ugGVE= ARC-Authentication-Results: i=1; imf23.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=FKYBsrGa; spf=pass (imf23.hostedemail.com: domain of 3WWVcZwUKCNYL23328GG8D6.4GEDAFMP-EECN24C.GJ8@flex--tabba.bounces.google.com designates 209.85.221.74 as permitted sender) smtp.mailfrom=3WWVcZwUKCNYL23328GG8D6.4GEDAFMP-EECN24C.GJ8@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-386321c8f4bso1157966f8f.0 for ; Fri, 13 Dec 2024 08:48:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734108506; x=1734713306; darn=kvack.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=gdnqwTM+ekDbCobF/sslf1fAX7qCXn0A+3Ru0J9WkCw=; b=FKYBsrGaSZM6kFHFUMHu3C7lOSNaEkTAtV6223eeYVahRGP59zwLcpPAEEc+r8cs0F j8lp5C3hLk0RnOg/IfuRY8AVxemaCIkHMCZbA/veb/td//Aobshxkb6vHAABTDydAnDH DW261mwe7BuLroEBPAN7PfBHVOUZ3ePRw0Yk7xnXcg0K63IPlHa0TG+dVIsi1pMeoOUu 29xE/IiiqHi++7QVHYrfovkOl+PMZ3SDesZP2PdIdeTAiU6wp/uFGnSyJR+rmKBvm2X4 U4hPkcpDeFssxg1G/tux8GOYqQiZkOheAuVQ/65W7vIECNeAkQb0lQJYN0otZK6xRERI R6mg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734108506; x=1734713306; 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=gdnqwTM+ekDbCobF/sslf1fAX7qCXn0A+3Ru0J9WkCw=; b=XPrjOkEyH7mqain9EaXs2FyxIaqmZ/gu6naBeVGQvZQlVBrIb3U5uFwDMMQaExLiIz F44VSARaNOuq44UjXh40tkmQm1ycDELK2o/GYLvPTvpsnbkcEb/zIQmBXOH3Plo7soKw V55mWdMhcG/xEigjsxzKyvkGlkwwYbvrADkiTCB4iiB7kd2JJfNs61mPsaFHpYQEZWUF vQTwpyqwSUyTTd66Lo68ZIIA8LBatjswqFsO0EBIn5EWTURp3JAlkJOeq5LZbTqJndJZ f4QHA0iKh4nbxO6tokEQB/9FF5GFeKkH/eqvQu6hy8zrEF/CzKmPVG9UjlWflCrJJ7B7 U8PQ== X-Forwarded-Encrypted: i=1; AJvYcCUL3XGXbom9mNpKV7KAELUCjVJ+vN92T1e8iHOd+eXOAjiWlnKpbQ25Wsp8igvWuO9D/4IuJFJE6g==@kvack.org X-Gm-Message-State: AOJu0Yy/6Cq2fi30DiC3xbQBgnOPw905UbYYLlqeZL76wr6ZPq1kfsa1 JCmBPvAkmvC+36rPTTY26vlbVo5tIAxNdzlokkRoYlZjT/nIu+pI00Kg0Zbzy+OPFWmzMChPuw= = X-Google-Smtp-Source: AGHT+IHsiL1TU4JB1jwgTJjdMcyuypfsiIlUlbhs/D1z5iTg4X32f0hrac57+zGyUbo1D5HK1o3YHzzCeg== X-Received: from wrpa7.prod.google.com ([2002:adf:eec7:0:b0:382:31e8:c1f8]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:1f82:b0:385:effc:a279 with SMTP id ffacd0b85a97d-3888e0c184dmr1858759f8f.58.1734108505690; Fri, 13 Dec 2024 08:48:25 -0800 (PST) Date: Fri, 13 Dec 2024 16:48:02 +0000 In-Reply-To: <20241213164811.2006197-1-tabba@google.com> Mime-Version: 1.0 References: <20241213164811.2006197-1-tabba@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241213164811.2006197-7-tabba@google.com> Subject: [RFC PATCH v4 06/14] KVM: guest_memfd: Handle final folio_put() of guestmem pages From: Fuad Tabba To: kvm@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-mm@kvack.org Cc: pbonzini@redhat.com, chenhuacai@kernel.org, mpe@ellerman.id.au, anup@brainfault.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, seanjc@google.com, viro@zeniv.linux.org.uk, brauner@kernel.org, willy@infradead.org, akpm@linux-foundation.org, xiaoyao.li@intel.com, yilun.xu@intel.com, chao.p.peng@linux.intel.com, jarkko@kernel.org, amoorthy@google.com, dmatlack@google.com, yu.c.zhang@linux.intel.com, isaku.yamahata@intel.com, mic@digikod.net, vbabka@suse.cz, vannapurve@google.com, ackerleytng@google.com, mail@maciej.szmigiero.name, david@redhat.com, michael.roth@amd.com, wei.w.wang@intel.com, liam.merwick@oracle.com, isaku.yamahata@gmail.com, kirill.shutemov@linux.intel.com, suzuki.poulose@arm.com, steven.price@arm.com, quic_eberman@quicinc.com, quic_mnalajal@quicinc.com, quic_tsoni@quicinc.com, quic_svaddagi@quicinc.com, quic_cvanscha@quicinc.com, quic_pderrin@quicinc.com, quic_pheragu@quicinc.com, catalin.marinas@arm.com, james.morse@arm.com, yuzenghui@huawei.com, oliver.upton@linux.dev, maz@kernel.org, will@kernel.org, qperret@google.com, keirf@google.com, roypat@amazon.co.uk, shuah@kernel.org, hch@infradead.org, jgg@nvidia.com, rientjes@google.com, jhubbard@nvidia.com, fvdl@google.com, hughd@google.com, jthoughton@google.com, tabba@google.com X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 14B7D14000A X-Stat-Signature: eo9opr4jdb957bmuiza64pwxqaztgc6p X-Rspam-User: X-HE-Tag: 1734108489-20947 X-HE-Meta: U2FsdGVkX1+GZMNZo4DiuVVe3N+cfsbFc8vF/RP1KgMKscULxuy4tzOnWuSAkDHbXvjOPmcRwGCbevCzwuVV2ceJoaAHgWgDDy6HzfftHzJzRrpo7o9TgiHsHyoH6H1ONWlndTfPHHEo0IxGb1rLvTtGngYnVclC2E3PzFQjHEFgFBpM3qhXj6mTiDtCM46kWdjA2DTGKkbV6cUwltekP4t0wgGzWF0CY/E5m3m8uo/Pm7zIZJ3b5Ecs6DsnXZ5hDnFc2kp/CkqoI/QmqvZnMUHNmPz2qAA8P6Cogx8vV3LuYSXlyfU57Y2O04jegmoj817FGNBstdbY0akPnZFRIG9KwlBckAk/iDzeKPB6Ik43MI/e3o7F+4BUX/J7whmZ2zzXV9daqT2d8Nwi4xugu/O5KXhLr18ePjf85PsP/99xbpvkjhhDmsTfEaW5hyS2ao0bzxckYbA7itBuOlLJPz07mprlRLcf/edFqNbZVpP/0u4r9zZS5Xo2GrOm4/YH5ZlYYjt/u5FzGY06fQiykD3Zln0uZX0TMYYccj8KNHk5CENMZcWk9OUX7ZGyBmXcBQL0WddYtqcOipqTsf+ECOInO+E4WKWfmLVj5dkPw4eNZeM411A8EWSpsu/CbT8deLum6nuaA7Wx1ECIIUwAD36zuhp8G1IqfrKfaxJDKSQ1NzxrdH3EG+Jky4a8EpQnejhXJTCo0WZu5RMncLAk0MYkzRrLiLVrstofn+5b7iLif4NGM3wqt2a7tO+assNgMyDl460hBoIuuYHs1fZTUO2cWDEBcP9zhtpQaO25bUSms/SQCmJ5XGhQqn2tNqQcNoRE75oPgQ1gdZTHJFBUsbDkxT2TgUZ3N1qZweuq8OKrHXp2qFF5Dikd4TO3mUTL9EcnHp0KaksooO30WdDJPWRz45F1V8I9KCpp1yHwQUo/u9XZMjPf9LyuG5SlMsVN4H47dmntMOzXce6HbzJ +v10G3Dv NSMsW4oan9NdO9ft1p3cxWvFOZxmQGq7SX0ano9VpFI6/eB9gAgTYgLbjW9OOmcfEu3IYg3uINrVDChUKftiimkvWWHfIYmI4KnyDrFTCcHVmMZ3eMTw6oNOOP1cZZHRPG0BEOQ3+5USiwz9yRI9FLVJdoRLed10bn2qsQ3Vgb2dHK0FlraiAs0WNg9lRMVIa2CVRucBfAqhCdhA+qk0gUZrK7+gA57Pis3i6VSS77P4o8M35TfpDITvYIRJkO9a0gYH7Lw+RPuONQ4VbNTNAZT3Zh99hqc9XH99fCTgPwBClAYeqsJNQ15zMXUQz9e6eGa6pAKctghOnyRPGow1TWTd5jSrWzfAGM2gefFK017HtRWidNYXqxInxp7X1HmdY0hloq65dkD0Lakb4uNnLB31nyX+6kzoqXrV2r6+0ZtmyACOQADJfrxsTSxZPTHXak9eSlbcDHJZ+Mw+StAhEdCTVZHqoHN5JaX2L7dHd80iDuRTAoGfvHBzHZX49i84hjX31Oh/RS5Kdl5ma451YMnRnApAKmXf4jL4ifYXpFLMpA93NrFQMAA+K7IcSxg+14+ZtQMnw4ktaf3XUW23VwGtTfAHuEPJ6rxE2+nP1JLDcnCF4NQ5uTfY0H/NC5Yzh3ddDnKG31HhFqw+n27YeN+OMP495Xri8khcfwCn8LzXlqw6VDjvx/ERH9ThgsO4r3m2FgWisNWq2owc= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000001, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Before transitioning a guest_memfd folio to unshared, thereby disallowing access by the host and allowing the hypervisor to transition its view of the guest page as private, we need to be sure that the host doesn't have any references to the folio. This patch introduces a new type for guest_memfd folios, and uses that to register a callback that informs the guest_memfd subsystem when the last reference is dropped, therefore knowing that the host doesn't have any remaining references. Signed-off-by: Fuad Tabba --- The function kvm_slot_gmem_register_callback() isn't used in this series. It will be used later in code that performs unsharing of memory. I have tested it with pKVM, based on downstream code [*]. It's included in this RFC since it demonstrates the plan to handle unsharing of private folios. [*] https://android-kvm.googlesource.com/linux/+/refs/heads/tabba/guestmem-6.13-v4-pkvm --- include/linux/kvm_host.h | 11 +++ include/linux/page-flags.h | 7 ++ mm/debug.c | 1 + mm/swap.c | 4 + virt/kvm/guest_memfd.c | 145 +++++++++++++++++++++++++++++++++++++ 5 files changed, 168 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 84aa7908a5dd..7ada5f78ded4 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2574,6 +2574,8 @@ int kvm_slot_gmem_clear_mappable(struct kvm_memory_slot *slot, gfn_t start, gfn_t end); bool kvm_slot_gmem_is_mappable(struct kvm_memory_slot *slot, gfn_t gfn); bool kvm_slot_gmem_is_guest_mappable(struct kvm_memory_slot *slot, gfn_t gfn); +int kvm_slot_gmem_register_callback(struct kvm_memory_slot *slot, gfn_t gfn); +void kvm_gmem_handle_folio_put(struct folio *folio); #else static inline bool kvm_gmem_is_mappable(struct kvm *kvm, gfn_t gfn, gfn_t end) { @@ -2615,6 +2617,15 @@ static inline bool kvm_slot_gmem_is_guest_mappable(struct kvm_memory_slot *slot, WARN_ON_ONCE(1); return false; } +int kvm_slot_gmem_register_callback(struct kvm_memory_slot *slot, gfn_t gfn) +{ + WARN_ON_ONCE(1); + return -EINVAL; +} +static inline void kvm_gmem_handle_folio_put(struct folio *folio) +{ + WARN_ON_ONCE(1); +} #endif /* CONFIG_KVM_GMEM_MAPPABLE */ #endif diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h index aca57802d7c7..b0e8e43de77c 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -950,6 +950,7 @@ enum pagetype { PGTY_slab = 0xf5, PGTY_zsmalloc = 0xf6, PGTY_unaccepted = 0xf7, + PGTY_guestmem = 0xf8, PGTY_mapcount_underflow = 0xff }; @@ -1099,6 +1100,12 @@ FOLIO_TYPE_OPS(hugetlb, hugetlb) FOLIO_TEST_FLAG_FALSE(hugetlb) #endif +#ifdef CONFIG_KVM_GMEM_MAPPABLE +FOLIO_TYPE_OPS(guestmem, guestmem) +#else +FOLIO_TEST_FLAG_FALSE(guestmem) +#endif + PAGE_TYPE_OPS(Zsmalloc, zsmalloc, zsmalloc) /* diff --git a/mm/debug.c b/mm/debug.c index 95b6ab809c0e..db93be385ed9 100644 --- a/mm/debug.c +++ b/mm/debug.c @@ -56,6 +56,7 @@ static const char *page_type_names[] = { DEF_PAGETYPE_NAME(table), DEF_PAGETYPE_NAME(buddy), DEF_PAGETYPE_NAME(unaccepted), + DEF_PAGETYPE_NAME(guestmem), }; static const char *page_type_name(unsigned int page_type) diff --git a/mm/swap.c b/mm/swap.c index 6f01b56bce13..15220eaabc86 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -37,6 +37,7 @@ #include #include #include +#include #include "internal.h" @@ -103,6 +104,9 @@ static void free_typed_folio(struct folio *folio) case PGTY_offline: /* Nothing to do, it's offline. */ return; + case PGTY_guestmem: + kvm_gmem_handle_folio_put(folio); + return; default: WARN_ON_ONCE(1); } diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index d1c192927cf7..5ecaa5dfcd00 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -387,6 +387,28 @@ enum folio_mappability { KVM_GMEM_NONE_MAPPABLE = 0b11, /* Not mappable, transient state. */ }; +/* + * Unregisters the __folio_put() callback from the folio. + * + * Restores a folio's refcount after all pending references have been released, + * and removes the folio type, thereby removing the callback. Now the folio can + * be freed normaly once all actual references have been dropped. + * + * Must be called with the filemap (inode->i_mapping) invalidate_lock held. + * Must also have exclusive access to the folio: folio must be either locked, or + * gmem holds the only reference. + */ +static void __kvm_gmem_restore_pending_folio(struct folio *folio) +{ + if (WARN_ON_ONCE(folio_mapped(folio) || !folio_test_guestmem(folio))) + return; + + WARN_ON_ONCE(!folio_test_locked(folio) || folio_ref_count(folio) > 1); + + __folio_clear_guestmem(folio); + folio_ref_add(folio, folio_nr_pages(folio)); +} + /* * Marks the range [start, end) as mappable by both the host and the guest. * Usually called when guest shares memory with the host. @@ -400,7 +422,31 @@ static int gmem_set_mappable(struct inode *inode, pgoff_t start, pgoff_t end) filemap_invalidate_lock(inode->i_mapping); for (i = start; i < end; i++) { + struct folio *folio = NULL; + + /* + * If the folio is NONE_MAPPABLE, it indicates that it is + * transitioning to private (GUEST_MAPPABLE). Transition it to + * shared (ALL_MAPPABLE) immediately, and remove the callback. + */ + if (xa_to_value(xa_load(mappable_offsets, i)) == KVM_GMEM_NONE_MAPPABLE) { + folio = filemap_lock_folio(inode->i_mapping, i); + if (WARN_ON_ONCE(IS_ERR(folio))) { + r = PTR_ERR(folio); + break; + } + + if (folio_test_guestmem(folio)) + __kvm_gmem_restore_pending_folio(folio); + } + r = xa_err(xa_store(mappable_offsets, i, xval, GFP_KERNEL)); + + if (folio) { + folio_unlock(folio); + folio_put(folio); + } + if (r) break; } @@ -473,6 +519,105 @@ static int gmem_clear_mappable(struct inode *inode, pgoff_t start, pgoff_t end) return r; } +/* + * Registers a callback to __folio_put(), so that gmem knows that the host does + * not have any references to the folio. It does that by setting the folio type + * to guestmem. + * + * Returns 0 if the host doesn't have any references, or -EAGAIN if the host + * has references, and the callback has been registered. + * + * Must be called with the following locks held: + * - filemap (inode->i_mapping) invalidate_lock + * - folio lock + */ +static int __gmem_register_callback(struct folio *folio, struct inode *inode, pgoff_t idx) +{ + struct xarray *mappable_offsets = &kvm_gmem_private(inode)->mappable_offsets; + void *xval_guest = xa_mk_value(KVM_GMEM_GUEST_MAPPABLE); + int refcount; + + rwsem_assert_held_write_nolockdep(&inode->i_mapping->invalidate_lock); + WARN_ON_ONCE(!folio_test_locked(folio)); + + if (folio_mapped(folio) || folio_test_guestmem(folio)) + return -EAGAIN; + + /* Register a callback first. */ + __folio_set_guestmem(folio); + + /* + * Check for references after setting the type to guestmem, to guard + * against potential races with the refcount being decremented later. + * + * At least one reference is expected because the folio is locked. + */ + + refcount = folio_ref_sub_return(folio, folio_nr_pages(folio)); + if (refcount == 1) { + int r; + + /* refcount isn't elevated, it's now faultable by the guest. */ + r = WARN_ON_ONCE(xa_err(xa_store(mappable_offsets, idx, xval_guest, GFP_KERNEL))); + if (!r) + __kvm_gmem_restore_pending_folio(folio); + + return r; + } + + return -EAGAIN; +} + +int kvm_slot_gmem_register_callback(struct kvm_memory_slot *slot, gfn_t gfn) +{ + unsigned long pgoff = slot->gmem.pgoff + gfn - slot->base_gfn; + struct inode *inode = file_inode(slot->gmem.file); + struct folio *folio; + int r; + + filemap_invalidate_lock(inode->i_mapping); + + folio = filemap_lock_folio(inode->i_mapping, pgoff); + if (WARN_ON_ONCE(IS_ERR(folio))) { + r = PTR_ERR(folio); + goto out; + } + + r = __gmem_register_callback(folio, inode, pgoff); + + folio_unlock(folio); + folio_put(folio); +out: + filemap_invalidate_unlock(inode->i_mapping); + + return r; +} + +/* + * Callback function for __folio_put(), i.e., called when all references by the + * host to the folio have been dropped. This allows gmem to transition the state + * of the folio to mappable by the guest, and allows the hypervisor to continue + * transitioning its state to private, since the host cannot attempt to access + * it anymore. + */ +void kvm_gmem_handle_folio_put(struct folio *folio) +{ + struct xarray *mappable_offsets; + struct inode *inode; + pgoff_t index; + void *xval; + + inode = folio->mapping->host; + index = folio->index; + mappable_offsets = &kvm_gmem_private(inode)->mappable_offsets; + xval = xa_mk_value(KVM_GMEM_GUEST_MAPPABLE); + + filemap_invalidate_lock(inode->i_mapping); + __kvm_gmem_restore_pending_folio(folio); + WARN_ON_ONCE(xa_err(xa_store(mappable_offsets, index, xval, GFP_KERNEL))); + filemap_invalidate_unlock(inode->i_mapping); +} + static bool gmem_is_mappable(struct inode *inode, pgoff_t pgoff) { struct xarray *mappable_offsets = &kvm_gmem_private(inode)->mappable_offsets; From patchwork Fri Dec 13 16:48:03 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13907465 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 37B29E77180 for ; Fri, 13 Dec 2024 16:48:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AF7F76B00A5; Fri, 13 Dec 2024 11:48:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AA5B96B00A6; Fri, 13 Dec 2024 11:48:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 91FBA6B00A7; Fri, 13 Dec 2024 11:48:31 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 705D66B00A5 for ; Fri, 13 Dec 2024 11:48:31 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 2A99841E96 for ; Fri, 13 Dec 2024 16:48:31 +0000 (UTC) X-FDA: 82890518970.29.CF7C8C9 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf26.hostedemail.com (Postfix) with ESMTP id A8B43140004 for ; Fri, 13 Dec 2024 16:48:09 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=TTwQtN+6; spf=pass (imf26.hostedemail.com: domain of 3W2VcZwUKCNgN4554AIIAF8.6IGFCHOR-GGEP46E.ILA@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3W2VcZwUKCNgN4554AIIAF8.6IGFCHOR-GGEP46E.ILA@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734108482; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=vYAjt6KjlCWR3/9CjS9sdQrEPORDtZUsmbS3MjiJTDQ=; b=bFR6o4nlZkCyNhNvWmk2KBYQCYvd+h//uWMX8dmsyv1zghsgP7oBTLbGCL6LB7yZ3M0z+z +sQVaKen6zbXf9KEg/T10DZIDN7r7e82xoiTnn706P1GakAcmhr4TsFeOGx42vK/4LjV+O QIsZNDFyF73Lel5quP3a4KUjvc3pGhI= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=TTwQtN+6; spf=pass (imf26.hostedemail.com: domain of 3W2VcZwUKCNgN4554AIIAF8.6IGFCHOR-GGEP46E.ILA@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3W2VcZwUKCNgN4554AIIAF8.6IGFCHOR-GGEP46E.ILA@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734108482; a=rsa-sha256; cv=none; b=Ug2K7d/4TfSTkzqXayBBKSTFtNigfdv0SnR67tPbiDt83FiYQhl98ORILua9ugP/3AYw7n FIt93Q719Y4cJsdksxceGSEBQQJB2dcvGgZscVvQxZE0W3Hoh1DxuFELpUqUdt3bWb0E+i XnukDBITNp6b0wIBsCdbvlVP1h0gvBY= Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-43621907030so18692875e9.1 for ; Fri, 13 Dec 2024 08:48:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734108508; x=1734713308; darn=kvack.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=vYAjt6KjlCWR3/9CjS9sdQrEPORDtZUsmbS3MjiJTDQ=; b=TTwQtN+65uVuGrYqgBWlkCjhPRIgL/EiTH4d8+RoZEv/UgA2grWwm3EC0+h8cpNhWN G1fYBeKOYZhOtrKo5b4C5Dguo5k7ivByFGIf2hD4c5ns5E9v3L2KviomSExgizJpTOse aBKF0C/FbmMn9a3RYKduotETAGm8AySJGIbZOtbwsMXTq3RaNK5rHsmkCDEbAYfGBG08 UGLw4QPtl2neO+QR+Lceyu3R6KkUBu6I32uvJKQv5g4HAqboR4pDBtlKhG5Acdk96RE7 R442jbfxLaPuIgNBny6Gsf00Bm88dlOVnOmsywtCbD9U8iG96mpgDLh0EjAK3Dk0GDKv gMTQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734108508; x=1734713308; 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=vYAjt6KjlCWR3/9CjS9sdQrEPORDtZUsmbS3MjiJTDQ=; b=AT8D8DR3sym1vlgedespZIAuUoAKeTtr7sPald9wYo6nFfrzOu8wCkaFEV+jo4fDmm kqfnelNl1irrBmgpNtY4YnU/H+FKNpcBFMMeQVRMZYMlJb9X7kEpWLykJk1CErYTso/w HdkhJJy46thdIQWIWnB9O3vEFirqOd7qVQgE1AM9imVNAnBoVxFO/pgs1u2gknbSOUvC 567N0kBD5tFFMnHUUu4xRlbN089K3YbrHgGxdQSMVcizlyhp2+/EHutkvascSa67o2XN S7PrRYcy4lud0BjsZxEDs4wiCuGIdxEw/64tBm/RXJoifeGnOOyOAqE0SqxgvRQqpzVb OhcQ== X-Forwarded-Encrypted: i=1; AJvYcCV8y1V5gNYBS3DmuktOdOHc7SJKFimfpExMJYmuzAHE/wlGwIMj2IY47TRpbP8TEzE7tgRkv0+sLg==@kvack.org X-Gm-Message-State: AOJu0YzM0pevvDCV3+90RGkscfSWG6Ooj08pom7D6070fVkgX49DjTFU y5LxSvTVolxBwkTlK1ht3BGCWsgngmAVieQcHJmc0t2B/DNj5N37weJ8pXWRqbFC8N9MBuNdqA= = X-Google-Smtp-Source: AGHT+IEuIo0g8Wj72K05W3M76Lwgy9hl7xYqHm8Z8UjN79dj1bkbMiQK/ObAF1ojkOHGGxPeIlJxjhjp+Q== X-Received: from wmdv21.prod.google.com ([2002:a05:600c:12d5:b0:434:f1d0:7dc9]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:524c:b0:436:2238:97f6 with SMTP id 5b1f17b1804b1-4362aa1af5emr29426785e9.1.1734108507984; Fri, 13 Dec 2024 08:48:27 -0800 (PST) Date: Fri, 13 Dec 2024 16:48:03 +0000 In-Reply-To: <20241213164811.2006197-1-tabba@google.com> Mime-Version: 1.0 References: <20241213164811.2006197-1-tabba@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241213164811.2006197-8-tabba@google.com> Subject: [RFC PATCH v4 07/14] KVM: guest_memfd: Allow host to mmap guest_memfd() pages when shared From: Fuad Tabba To: kvm@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-mm@kvack.org Cc: pbonzini@redhat.com, chenhuacai@kernel.org, mpe@ellerman.id.au, anup@brainfault.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, seanjc@google.com, viro@zeniv.linux.org.uk, brauner@kernel.org, willy@infradead.org, akpm@linux-foundation.org, xiaoyao.li@intel.com, yilun.xu@intel.com, chao.p.peng@linux.intel.com, jarkko@kernel.org, amoorthy@google.com, dmatlack@google.com, yu.c.zhang@linux.intel.com, isaku.yamahata@intel.com, mic@digikod.net, vbabka@suse.cz, vannapurve@google.com, ackerleytng@google.com, mail@maciej.szmigiero.name, david@redhat.com, michael.roth@amd.com, wei.w.wang@intel.com, liam.merwick@oracle.com, isaku.yamahata@gmail.com, kirill.shutemov@linux.intel.com, suzuki.poulose@arm.com, steven.price@arm.com, quic_eberman@quicinc.com, quic_mnalajal@quicinc.com, quic_tsoni@quicinc.com, quic_svaddagi@quicinc.com, quic_cvanscha@quicinc.com, quic_pderrin@quicinc.com, quic_pheragu@quicinc.com, catalin.marinas@arm.com, james.morse@arm.com, yuzenghui@huawei.com, oliver.upton@linux.dev, maz@kernel.org, will@kernel.org, qperret@google.com, keirf@google.com, roypat@amazon.co.uk, shuah@kernel.org, hch@infradead.org, jgg@nvidia.com, rientjes@google.com, jhubbard@nvidia.com, fvdl@google.com, hughd@google.com, jthoughton@google.com, tabba@google.com X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: A8B43140004 X-Rspam-User: X-Stat-Signature: q9s3t3u3eqcd4s585mi7t97setgri8ub X-HE-Tag: 1734108489-269261 X-HE-Meta: U2FsdGVkX1/9zOv5aWccS0W8lro7fY+4DSjoaiwSKcfsXotjQjTam5it570Ckvz1xFDL9gaPv1Pn+PK9JYoS6F3KeuAiHYkBlw5R19Z6rJqnhEC0aIiQN/Xp3K0bgk2Kiblbw4EC5Pq0LMBMvn1FUxLM+ikZWC8UIdEwEq2RsH///bDY9IJNaxRsfEamha5aO+1vlNQrGsaH0uIgpRXsTAVUeOCZy0I1iFzPIW7/sGa17tDSC0JeN7Q+hcJ/PsZqe9jrf5VzG3DqefK8GTGdfikYYy1B9WHXv8ekmuHXZdKQQRDq0TMEM6wrDVIgCqCC7mj3isSnKhzlck3ira24ueZfIWCjZiXziZ9r07m9Z4Dr310erGjxM68gifC8q3yT9IIb4Ita2Whxpe9vurFf2f5tt8MEwcW0Os3u7xxTpLb8ayI6CFQ+tf9NARFJwRZHMDAS/nhiO1RNpYEF7fzrvkp9BkWv/PKT7m4d5L1LFGJZI2y+ABkJnZHWTloaTk19/dC7Yi+9bCcD/xICubS08R6GJdk6PiPdqXpHhgEheJWqasmY5OLXF7WhSkPQAiPlw0YvhBfhZjgop6mnffFDxBetYbHl0i2ybBly5N/cWSgAvx2/ON9pwcfutaHkt0Y1cNjRJl/T3D93o4FSGXBb+z1QdVRRLmGw6Y3fUT2MXfOePbxBMqfnGKhQSNEw/0GamKgAySsrU9WKEMYNUQMwzx/JTwSNzmxJhRpVQvRiHGhnfvGdnRrsEhDqE+/0BipCjLSMjghWhkzNrS0e/ftKPxMmVo6frvo95zX3HQvzswtBKnyKP1mLSRGE47PHwcm8W1XP6APHyx8AoPzG/KT25WW4uzxlsBnw86QRlwK72KEJkznIN3rbGA1PXR5bqaBB+yr5bciB2nTnCy/STst965xUsx+RQL8FYInbaovcOIjNuuqflPjM2condw5SUjBPIwj83w0oQebN3yZFP2Y yeXBfU22 UUcUQLtdVPtamaDgY7LsLRw6y1lVsJCsVuWYlW2cU6Ph8YZ0k/CgfnhaDWCrHpNNStIWaFw/GO1Ki+QeOb0ltcUkv+HRAe/ifQyaF6+Vew0psRSRAbZFAExhdHS8wU7c9GSBDaxvwUMeQREz9pYwJuYeN8xRqF5C22AaE+f80lqyKZIR/Sr4WfGjjFj+Ez8UgiTnTofcLEYJF6Zs7PT2mwBDPx8KWkFf3rPcFAEXFfeXNX0mB71B82eX4ev1pYOTAaGsIjqikg+EMH4K3jg+UKZ1WiASMJo+hBmeoLzY6saQr7QILjLGlnAeLZ/rzR/j9Zha5zaDuvdbIu5wSNwLmbf/6+5L8/qvmRP4D8E43ajxC7BHBwXS+OjduzTvnz8/3/00RK7pY6Vo05eGNU7bE04Z3/ahUA1CP8fmc0TsyHOD9keEDpqC9qvcOs/4Lxjge11PN7CbtWa+QsdEnh0HLxJQbMl3Z4rU9ihSZTvwIhvw88v3Iob00/2bhleR85DiTvSzlj1PwyaR3vrWduUz1pzxiZY5lFH9STCWr6iEardwjz9MVRqlRwN+skWp2vpbFblEbicPlnDBr2xP/G0+1qakb+UbcKkCfdMRZE8kSbNxu7m5eT0R4o/JC5YbKyw4C38TQWTZyH1tPZ/Y6OVjouZSORGluLbdcXZ9RZlS0uiWevOok/yLkO5YbBM70t+EDBwrbqVEym+cel8U+0nmn/72DiPX4TI+SgEOhLXKUTxp37w9yWmDtAszgnQ3cBhcGEJfL X-Bogosity: Ham, 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: Add support for mmap() and fault() for guest_memfd in the host. The ability to fault in a guest page is contingent on that page being shared with the host. The guest_memfd PRIVATE memory attribute is not used for two reasons. First because it reflects the userspace expectation for that memory location, and therefore can be toggled by userspace. The second is, although each guest_memfd file has a 1:1 binding with a KVM instance, the plan is to allow multiple files per inode, e.g. to allow intra-host migration to a new KVM instance, without destroying guest_memfd. The mapping is restricted to only memory explicitly shared with the host. KVM checks that the host doesn't have any mappings for private memory via the folio's refcount. To avoid races between paths that check mappability and paths that check whether the host has any mappings (via the refcount), the folio lock is held in while either check is being performed. This new feature is gated with a new configuration option, CONFIG_KVM_GMEM_MAPPABLE. Co-developed-by: Ackerley Tng Signed-off-by: Ackerley Tng Co-developed-by: Elliot Berman Signed-off-by: Elliot Berman Signed-off-by: Fuad Tabba --- The functions kvm_gmem_is_mapped(), kvm_gmem_set_mappable(), and int kvm_gmem_clear_mappable() are not used in this patch series. They are intended to be used in future patches [*], which check and toggle mapability when the guest shares/unshares pages with the host. [*] https://android-kvm.googlesource.com/linux/+/refs/heads/tabba/guestmem-6.13-v4-pkvm --- virt/kvm/Kconfig | 4 ++ virt/kvm/guest_memfd.c | 87 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) diff --git a/virt/kvm/Kconfig b/virt/kvm/Kconfig index 54e959e7d68f..59400fd8f539 100644 --- a/virt/kvm/Kconfig +++ b/virt/kvm/Kconfig @@ -124,3 +124,7 @@ config HAVE_KVM_ARCH_GMEM_PREPARE config HAVE_KVM_ARCH_GMEM_INVALIDATE bool depends on KVM_PRIVATE_MEM + +config KVM_GMEM_MAPPABLE + select KVM_PRIVATE_MEM + bool diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 5ecaa5dfcd00..3d3645924db9 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -671,9 +671,88 @@ bool kvm_slot_gmem_is_guest_mappable(struct kvm_memory_slot *slot, gfn_t gfn) return gmem_is_guest_mappable(inode, pgoff); } + +static vm_fault_t kvm_gmem_fault(struct vm_fault *vmf) +{ + struct inode *inode = file_inode(vmf->vma->vm_file); + struct folio *folio; + vm_fault_t ret = VM_FAULT_LOCKED; + + filemap_invalidate_lock_shared(inode->i_mapping); + + folio = kvm_gmem_get_folio(inode, vmf->pgoff); + if (IS_ERR(folio)) { + ret = VM_FAULT_SIGBUS; + goto out_filemap; + } + + if (folio_test_hwpoison(folio)) { + ret = VM_FAULT_HWPOISON; + goto out_folio; + } + + if (!gmem_is_mappable(inode, vmf->pgoff)) { + ret = VM_FAULT_SIGBUS; + goto out_folio; + } + + if (WARN_ON_ONCE(folio_test_guestmem(folio))) { + ret = VM_FAULT_SIGBUS; + goto out_folio; + } + + if (!folio_test_uptodate(folio)) { + unsigned long nr_pages = folio_nr_pages(folio); + unsigned long i; + + for (i = 0; i < nr_pages; i++) + clear_highpage(folio_page(folio, i)); + + folio_mark_uptodate(folio); + } + + vmf->page = folio_file_page(folio, vmf->pgoff); + +out_folio: + if (ret != VM_FAULT_LOCKED) { + folio_unlock(folio); + folio_put(folio); + } + +out_filemap: + filemap_invalidate_unlock_shared(inode->i_mapping); + + return ret; +} + +static const struct vm_operations_struct kvm_gmem_vm_ops = { + .fault = kvm_gmem_fault, +}; + +static int kvm_gmem_mmap(struct file *file, struct vm_area_struct *vma) +{ + if ((vma->vm_flags & (VM_SHARED | VM_MAYSHARE)) != + (VM_SHARED | VM_MAYSHARE)) { + return -EINVAL; + } + + file_accessed(file); + vm_flags_set(vma, VM_DONTDUMP); + vma->vm_ops = &kvm_gmem_vm_ops; + + return 0; +} +#else +static int gmem_set_mappable(struct inode *inode, pgoff_t start, pgoff_t end) +{ + WARN_ON_ONCE(1); + return -EINVAL; +} +#define kvm_gmem_mmap NULL #endif /* CONFIG_KVM_GMEM_MAPPABLE */ static struct file_operations kvm_gmem_fops = { + .mmap = kvm_gmem_mmap, .open = generic_file_open, .release = kvm_gmem_release, .fallocate = kvm_gmem_fallocate, @@ -860,6 +939,14 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) goto err_gmem; } + if (IS_ENABLED(CONFIG_KVM_GMEM_MAPPABLE)) { + err = gmem_set_mappable(file_inode(file), 0, size >> PAGE_SHIFT); + if (err) { + fput(file); + goto err_gmem; + } + } + kvm_get_kvm(kvm); gmem->kvm = kvm; xa_init(&gmem->bindings); From patchwork Fri Dec 13 16:48:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13907466 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 129FCE77180 for ; Fri, 13 Dec 2024 16:48:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D92046B00A7; Fri, 13 Dec 2024 11:48:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D13B46B00A8; Fri, 13 Dec 2024 11:48:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B1A116B00A9; Fri, 13 Dec 2024 11:48:33 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 9225A6B00A7 for ; Fri, 13 Dec 2024 11:48:33 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 4ADB11A10B8 for ; Fri, 13 Dec 2024 16:48:33 +0000 (UTC) X-FDA: 82890518760.18.2D72F67 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) by imf02.hostedemail.com (Postfix) with ESMTP id 3FF7D80014 for ; Fri, 13 Dec 2024 16:47:42 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=LunGMe6j; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf02.hostedemail.com: domain of 3XmVcZwUKCNsQ7887DLLDIB.9LJIFKRU-JJHS79H.LOD@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3XmVcZwUKCNsQ7887DLLDIB.9LJIFKRU-JJHS79H.LOD@flex--tabba.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734108487; a=rsa-sha256; cv=none; b=w1J2kZVBTpOhgbw+ig4LjjaLy2ah2cOmmLmnICx5c2W7InmopCwN78LGmr1JHlDEcYRl5o WHUcZdYD5Whzlh/gQ+TvHyUDFt6iLi5Y7Ff3qGPu4i09clRevzoCFnk7DoZn41gAuyzHXN 5lrywWhbTYfqrfD8dhOLUvSHwSC4rcQ= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=LunGMe6j; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf02.hostedemail.com: domain of 3XmVcZwUKCNsQ7887DLLDIB.9LJIFKRU-JJHS79H.LOD@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3XmVcZwUKCNsQ7887DLLDIB.9LJIFKRU-JJHS79H.LOD@flex--tabba.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734108487; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=W5zxHybLV3NWne95v7ysBYYUl2v+8+sVLD5dfkruHJg=; b=P+JLfavL37ERp/ShyfedOluxRQyJgnyWfOObvQ3UjwkX4WYUBFpeRh0kFHtnYbLFFr/ok8 QVL2SlpGdSpWsqGH3maa5O6I8LkWH9nLvW7Sb2hHnjWuK5VDAJn/jaPyOTWlH/2WSJeYa/ joUrPay6b+nFeb5Vl8wZTC1D0Rekeus= Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-4361c040ba8so4598235e9.1 for ; Fri, 13 Dec 2024 08:48:31 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734108510; x=1734713310; darn=kvack.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=W5zxHybLV3NWne95v7ysBYYUl2v+8+sVLD5dfkruHJg=; b=LunGMe6j7xqv36SHpVqhUyzXKqPjjLTDjXPe4MaC0nbsYMtbo40ozJsVcGwQDsW9Ga Hd2rjd7OV2dZcUjDgffsHkSVKpRlbuuCH2JJsCKAJXf86Q+57qiCTkPzyMsCV4DpDbZd YpEvQralAhWxaoDqEmP6ZQcgNIqLyYe38k9ZhqaYqH7GPq7f9Y7OJ0wWCd4tmlG2P7Cp RV1HK68iVyXWsw9PklwdWSlsfD54/5dkLDl2VR1UwiJQ8djBoqdH5/stiH2vxgPmLsj7 EVQGoHag6ioEZ5/KHfKgFjLi1MBSTPlmk456xEo7GkZgdR9IA2djGKRDiVnT97ThkJnp Jq+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734108510; x=1734713310; 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=W5zxHybLV3NWne95v7ysBYYUl2v+8+sVLD5dfkruHJg=; b=rLNLCeeuSVpSSQa+PxWI46kaqqTVC9uIAGlqpDg47vwfDV/4Md9neJZfruZQ0T6+1H EoqtVWFhuxEOXcyO1gTlGXqJiVNVfV7LtBhgndB39Z5toTS5kYV3+KzTbgV8Z9BILg1k o/5EHlHkjo54o0Om6x1AA0OVDIXNUmZPBigqHQs99a0KTeGXjPp8/+m2FUVFYJcVSWCD Harhjs7vXRBMGOnalIYP8jhEDq96YxyFaP1EjtT/GfjSd+M6iu0mwtxjERZio8Hd/l+g z07dKXOJNQj65Dh1xopl+28EeECJJqoP3SusPpBuDmvW698Kob/j2VteFPk0pMfh7isY R42A== X-Forwarded-Encrypted: i=1; AJvYcCXntKFFeYqFQoeqW9kGw+s4F6QTYwIpQEGs/Sy9MPsjNYIwKmf+/2FbbCQ0gktsSQ0aRd9xGTu4WA==@kvack.org X-Gm-Message-State: AOJu0Yz+uutEejQ9Wi59aSSIbHoGXJ547uWH8e39ALwLynHXkrj7w5kz wDMF+c0gXMC38lPUNOR/x70x88MImNPEy6H+I4LU1Gf9G6YNOcWNDM+1xui87Ef9M+HdeJCzfQ= = X-Google-Smtp-Source: AGHT+IE5qr7blgR8UT1xyGN9dbKV8Uu0Nxy8xzMzxQfp0eU0uQYPzB6TjeBHpyTiJ8UPA6pvno7rqFYahw== X-Received: from wmmu14.prod.google.com ([2002:a05:600c:ce:b0:434:f0a3:7876]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:4b12:b0:385:edb7:69ba with SMTP id ffacd0b85a97d-38880ac5f25mr3423932f8f.1.1734108510104; Fri, 13 Dec 2024 08:48:30 -0800 (PST) Date: Fri, 13 Dec 2024 16:48:04 +0000 In-Reply-To: <20241213164811.2006197-1-tabba@google.com> Mime-Version: 1.0 References: <20241213164811.2006197-1-tabba@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241213164811.2006197-9-tabba@google.com> Subject: [RFC PATCH v4 08/14] KVM: guest_memfd: Add guest_memfd support to kvm_(read|/write)_guest_page() From: Fuad Tabba To: kvm@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-mm@kvack.org Cc: pbonzini@redhat.com, chenhuacai@kernel.org, mpe@ellerman.id.au, anup@brainfault.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, seanjc@google.com, viro@zeniv.linux.org.uk, brauner@kernel.org, willy@infradead.org, akpm@linux-foundation.org, xiaoyao.li@intel.com, yilun.xu@intel.com, chao.p.peng@linux.intel.com, jarkko@kernel.org, amoorthy@google.com, dmatlack@google.com, yu.c.zhang@linux.intel.com, isaku.yamahata@intel.com, mic@digikod.net, vbabka@suse.cz, vannapurve@google.com, ackerleytng@google.com, mail@maciej.szmigiero.name, david@redhat.com, michael.roth@amd.com, wei.w.wang@intel.com, liam.merwick@oracle.com, isaku.yamahata@gmail.com, kirill.shutemov@linux.intel.com, suzuki.poulose@arm.com, steven.price@arm.com, quic_eberman@quicinc.com, quic_mnalajal@quicinc.com, quic_tsoni@quicinc.com, quic_svaddagi@quicinc.com, quic_cvanscha@quicinc.com, quic_pderrin@quicinc.com, quic_pheragu@quicinc.com, catalin.marinas@arm.com, james.morse@arm.com, yuzenghui@huawei.com, oliver.upton@linux.dev, maz@kernel.org, will@kernel.org, qperret@google.com, keirf@google.com, roypat@amazon.co.uk, shuah@kernel.org, hch@infradead.org, jgg@nvidia.com, rientjes@google.com, jhubbard@nvidia.com, fvdl@google.com, hughd@google.com, jthoughton@google.com, tabba@google.com X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 3FF7D80014 X-Stat-Signature: rhny4oa3bwme395e19fcqxf1wxag73ma X-Rspam-User: X-HE-Tag: 1734108462-211046 X-HE-Meta: U2FsdGVkX1/mgS883mlQuteVpCzOXb30DuwpSXoS/O/eLYJAbhM29zFrRVVfjTuieehm8N2PHYSEYX8JbUsF9WGmuL2nlTW1ltudj6wK8nkvKfPMk8qcNC8ZIINFzsTm7NTTqInymMRrkM9pqJz4h8TRtxixoDDbLHxuV2nwT0u632kEJxTMPJlmz1IhXIY4nOmMmZHzvikjWWE/TgsmQBtwq4Us1fnaoYDZcjFi02KSM2zamjv/0gHl8QaM98ahkAtVO+EB7tf9yTOGKvjzxG7LIKd7xvWHRDnjIEjQihB2tFL+LA7a99vlmvZFsiW9nRs76jMwe1J0GWseUkRiP6xYHjtNqUs4Dxk8jKKABO6vRpRllrjR7Le/XQgRNl5J90gCEEGwE5wuT/j43Ad1H5RnJO3WO7v0f9VnES9z3dfjHdtcZB/TsQ5A6UxOxxTpf5H2+OzGP+nNWDa0nAaC55h8pc6OWwi+8ZsphkzJSVcAQnmFyvbumNSMAwaut0fcaGCEYuhuOEquX1si9kQnPR/UO0ZBcBI2fhh67mQcB9xWnR/aBmrbcgN33rkg0gRXjFPgrCrFFSER2zX4FmZNUM6SfFujWY0vESpe2PSTnXJGSXCZKtYg8qoGPqsI8Yh0zd6JE+KlS+KyQ/+5Ze5sTU2wzNKz/z7nkftmFysqkSuU7PAqOJboHNnlieldm8IoYjM4K/JMerfdM+nSXspOHfV0qBtvgOhNTh8Pl9jZPfu7c0Zsp2nwQUSNiYzlIcijNdQkHO4Hh19iz7RDuOMmt+SH5kqn9o+Wxgs6CNrrsTzhij16NRSNPBdSyHRjOW5mS1mZwvA7LolEL3+49RezZWneum1YpKxQiiFD9VZrWM8LKZ/6qthuBLcpACY1+H01aFEAmYUfAHzie3Pef61LKcZiWUWG1ydvmDkOilHJjEwXNMWPOD/lsHO/h6edW+HMEI4W90Fyh5T7HfZCuUz x5C6vWAI ndO6VVOKMMMibt+q0QYmaCI9IPeZP7Rsx0dRwDUd3s1I3HvG/fHM59Bp5MDoZVeQMBuuGwi/02O+HRZzeD9goIMIAkBBDY7is2GI8POvbwK2i21XCed6AGwITC7Qm5wmOQONZ2SnDpENl0v01WRq/w3IFzeQdSZcAWqesmpSiJMXEaHIsThkm7KBfBmCAX2FyxlWARH5DKUefEz64yjACP62f61c9EVFQWK6vdXZ91ZnwHnQlu0/axLZVM+Jt0s3cCoKtaqIssxCN9zkAO2r7baZPeqtPGGhqvjvVrvBP+PHSSivupN6o1yCUN1PHiL93fouBiSETjhhQowCDY1C+4PSTkoogw5EbSEB94/dHTPuccQ3gl8pEZ/wYLjvzD3mLL/6VoPWnVCMJv55tg3wFEEVlflE30XRl53EPUOnhrSa643ziSXMzI0etYBicmV6yRB+Ibcd9oNmIf81+cHF9VcRKG85ueX0iZiL4b1A50S1Ay7+QkJNXVXV3Cgy4409FC1x117E9bAQgYk5urLaa2e2csZIEc3ZmubN/7TL5oZkFYv8sQ5Pxhd61dVBdkvMByRnrUivZyf7Tn3MjEEN+H+UMGXiHvkYUa4+050T+AcE9UAkPOrhtPSPFbhR4yOIYCfdGAdQBBQpLoYf89uEd+3lkWQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000007, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Make kvm_(read|/write)_guest_page() capable of accessing guest memory for slots that don't have a userspace address, but only if the memory is mappable, which also indicates that it is accessible by the host. Signed-off-by: Fuad Tabba --- virt/kvm/kvm_main.c | 133 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 114 insertions(+), 19 deletions(-) diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index fffff01cebe7..53692feb6213 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -3184,23 +3184,110 @@ int kvm_gmem_clear_mappable(struct kvm *kvm, gfn_t start, gfn_t end) return r; } +static int __kvm_read_guest_memfd_page(struct kvm *kvm, + struct kvm_memory_slot *slot, + gfn_t gfn, void *data, int offset, + int len) +{ + struct page *page; + u64 pfn; + int r; + + /* + * Holds the folio lock until after checking whether it can be faulted + * in, to avoid races with paths that change a folio's mappability. + */ + r = kvm_gmem_get_pfn_locked(kvm, slot, gfn, &pfn, &page, NULL); + if (r) + return r; + + if (!kvm_gmem_is_mappable(kvm, gfn, gfn + 1)) { + r = -EPERM; + goto unlock; + } + memcpy(data, page_address(page) + offset, len); +unlock: + unlock_page(page); + if (r) + put_page(page); + else + kvm_release_page_clean(page); + + return r; +} + +static int __kvm_write_guest_memfd_page(struct kvm *kvm, + struct kvm_memory_slot *slot, + gfn_t gfn, const void *data, + int offset, int len) +{ + struct page *page; + u64 pfn; + int r; + + /* + * Holds the folio lock until after checking whether it can be faulted + * in, to avoid races with paths that change a folio's mappability. + */ + r = kvm_gmem_get_pfn_locked(kvm, slot, gfn, &pfn, &page, NULL); + if (r) + return r; + + if (!kvm_gmem_is_mappable(kvm, gfn, gfn + 1)) { + r = -EPERM; + goto unlock; + } + memcpy(page_address(page) + offset, data, len); +unlock: + unlock_page(page); + if (r) + put_page(page); + else + kvm_release_page_dirty(page); + + return r; +} +#else +static int __kvm_read_guest_memfd_page(struct kvm *kvm, + struct kvm_memory_slot *slot, + gfn_t gfn, void *data, int offset, + int len) +{ + WARN_ON_ONCE(1); + return -EIO; +} + +static int __kvm_write_guest_memfd_page(struct kvm *kvm, + struct kvm_memory_slot *slot, + gfn_t gfn, const void *data, + int offset, int len) +{ + WARN_ON_ONCE(1); + return -EIO; +} #endif /* CONFIG_KVM_GMEM_MAPPABLE */ /* Copy @len bytes from guest memory at '(@gfn * PAGE_SIZE) + @offset' to @data */ -static int __kvm_read_guest_page(struct kvm_memory_slot *slot, gfn_t gfn, - void *data, int offset, int len) + +static int __kvm_read_guest_page(struct kvm *kvm, struct kvm_memory_slot *slot, + gfn_t gfn, void *data, int offset, int len) { - int r; unsigned long addr; if (WARN_ON_ONCE(offset + len > PAGE_SIZE)) return -EFAULT; + if (IS_ENABLED(CONFIG_KVM_GMEM_MAPPABLE) && + kvm_slot_can_be_private(slot) && + !slot->userspace_addr) { + return __kvm_read_guest_memfd_page(kvm, slot, gfn, data, + offset, len); + } + addr = gfn_to_hva_memslot_prot(slot, gfn, NULL); if (kvm_is_error_hva(addr)) return -EFAULT; - r = __copy_from_user(data, (void __user *)addr + offset, len); - if (r) + if (__copy_from_user(data, (void __user *)addr + offset, len)) return -EFAULT; return 0; } @@ -3210,7 +3297,7 @@ int kvm_read_guest_page(struct kvm *kvm, gfn_t gfn, void *data, int offset, { struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn); - return __kvm_read_guest_page(slot, gfn, data, offset, len); + return __kvm_read_guest_page(kvm, slot, gfn, data, offset, len); } EXPORT_SYMBOL_GPL(kvm_read_guest_page); @@ -3219,7 +3306,7 @@ int kvm_vcpu_read_guest_page(struct kvm_vcpu *vcpu, gfn_t gfn, void *data, { struct kvm_memory_slot *slot = kvm_vcpu_gfn_to_memslot(vcpu, gfn); - return __kvm_read_guest_page(slot, gfn, data, offset, len); + return __kvm_read_guest_page(vcpu->kvm, slot, gfn, data, offset, len); } EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest_page); @@ -3296,22 +3383,30 @@ EXPORT_SYMBOL_GPL(kvm_vcpu_read_guest_atomic); /* Copy @len bytes from @data into guest memory at '(@gfn * PAGE_SIZE) + @offset' */ static int __kvm_write_guest_page(struct kvm *kvm, - struct kvm_memory_slot *memslot, gfn_t gfn, - const void *data, int offset, int len) + struct kvm_memory_slot *slot, gfn_t gfn, + const void *data, int offset, int len) { - int r; - unsigned long addr; - if (WARN_ON_ONCE(offset + len > PAGE_SIZE)) return -EFAULT; - addr = gfn_to_hva_memslot(memslot, gfn); - if (kvm_is_error_hva(addr)) - return -EFAULT; - r = __copy_to_user((void __user *)addr + offset, data, len); - if (r) - return -EFAULT; - mark_page_dirty_in_slot(kvm, memslot, gfn); + if (IS_ENABLED(CONFIG_KVM_GMEM_MAPPABLE) && + kvm_slot_can_be_private(slot) && + !slot->userspace_addr) { + int r = __kvm_write_guest_memfd_page(kvm, slot, gfn, data, + offset, len); + + if (r) + return r; + } else { + unsigned long addr = gfn_to_hva_memslot(slot, gfn); + + if (kvm_is_error_hva(addr)) + return -EFAULT; + if (__copy_to_user((void __user *)addr + offset, data, len)) + return -EFAULT; + } + + mark_page_dirty_in_slot(kvm, slot, gfn); return 0; } From patchwork Fri Dec 13 16:48:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13907467 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 D21B6E77182 for ; Fri, 13 Dec 2024 16:48:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 74A026B00A9; Fri, 13 Dec 2024 11:48:36 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 720986B00AA; Fri, 13 Dec 2024 11:48:36 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5CE486B00AB; Fri, 13 Dec 2024 11:48:36 -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 380E96B00A9 for ; Fri, 13 Dec 2024 11:48:36 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id ADD081C7885 for ; Fri, 13 Dec 2024 16:48:35 +0000 (UTC) X-FDA: 82890518844.18.E21F87C Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf15.hostedemail.com (Postfix) with ESMTP id E797DA001B for ; Fri, 13 Dec 2024 16:48:01 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=GzvJBX5N; spf=pass (imf15.hostedemail.com: domain of 3YGVcZwUKCN0S9AA9FNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3YGVcZwUKCN0S9AA9FNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734108489; a=rsa-sha256; cv=none; b=TDnlzJulGIKNl7Tt9i1Sm4Bk4a/8qEex6r3C1AhG+pOadUlk9MjKk5rpzoN0osBgHvPSTX P0M37rmuEJpHMtcviPC+/jn0ZpRjhr2Z+kuo5aolgT8to/ZdUyYJFUs3MDZHeUQdMhfXMg acrrHOJnhoCHX2Q8ZsZb8VsRVhCoOvE= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=GzvJBX5N; spf=pass (imf15.hostedemail.com: domain of 3YGVcZwUKCN0S9AA9FNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3YGVcZwUKCN0S9AA9FNNFKD.BNLKHMTW-LLJU9BJ.NQF@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734108489; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ksEVsaSQioDaJqKOOzrRTGd2BPTgCKjKlBLY+w56wZo=; b=VcogB4G+z787bjb6h+7rbhEQO2as3oXc2RMyHNmidBLJlKeNOHJqev7jNg83zrmRUVX+e+ dPNsBQLPhDKlpdChiX8SIySRFmUeHnqK64k19XSuVCV+ITHvxgAZHOit0xfJZwMc7N904J mQq3kxKoYG7W0TUAWmkIFGm6y3Ypk3s= Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-436289a570eso12992485e9.0 for ; Fri, 13 Dec 2024 08:48:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734108512; x=1734713312; darn=kvack.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=ksEVsaSQioDaJqKOOzrRTGd2BPTgCKjKlBLY+w56wZo=; b=GzvJBX5NYySd/v/I+dJHZJcX4loTjNyCBiNelyUB5P6ocYlhDs7aqAUBWPOyrorEz6 G5HzIhSPHvNmuKUsG2SIQkUrTqI/V5SXYqaqORWBuAfsppqi2d0hx0XzL8s6IB/wQS9r YUsoxb7tKu6sMgV6Rq9vx6ipHHQkgEx/1tAIf9qJO9/wi/KKIwwqlodAG48dO6wT/sKK fGOpjmNS1Zu8G8pDGB6F9O9/HcpghduPkQ46QktjxXzrLGpsm7wEINWq3Y05EU3BvKD1 8hgUbFNG/5MyAbCc/+X5r5BcnmYJFkZHIjX7KsBq5fuMlIUxqgje5XGT3zlcoil5aAKC RXeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734108512; x=1734713312; 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=ksEVsaSQioDaJqKOOzrRTGd2BPTgCKjKlBLY+w56wZo=; b=o525SQHl3wJoUCvEgJFDFqmvBs0UPTXYcRUqS52btSqmm+Jp3Dl8y9VeMjaYReXgEY cB0VB5b6NKiPAUZA7d8Jj1XlhbDti0+a3Tp0utjEezz3vho+qFC715FgCEJKeXrfY0K1 P+DdowOuI217FJUHRQIU1jENndPCVOIv6m0j4WzOzkCVgM2hjpOP+3Tz85bc392eEVgQ f2EX18HYBdlU/S0qui6at3Rl5Vqw7r8GJcUb9J9Lba9SRWzlZYGoV37kUom2BYewn3nF GkTXVa9+ir8PCFBXLYlqSa0V/YjZja1N8goMS3TRM8TrWYkJsCZtFDhCEwlbbX0qeTff KVPQ== X-Forwarded-Encrypted: i=1; AJvYcCWJ/xYR3PLTb0tehQgndxIlsLio/HQ5PbHKamrYanOzyfNOlOVqLQEPX4VoC7OmWPddWsueC1efWQ==@kvack.org X-Gm-Message-State: AOJu0YyxgCtJIZcP1itcEMkcXUjelLL7VJ3OIpV6NN2j0quD7OIX4VRD SA3w9UqjxyfRhNZcFGAOqWojUpEFzQbgFJ27jWBF+5Fv7B+KaJUxux3ZVMy3sTvSzwSqGtFzLQ= = X-Google-Smtp-Source: AGHT+IEt4pXQrhABH8gera5DOjDskLThzt7L9I5lJj6Vxwq11yPrteBm6Llct58hlLMQdsD4E964MjHJuQ== X-Received: from wmbg15.prod.google.com ([2002:a05:600c:a40f:b0:436:1534:b059]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:6549:b0:434:f0df:9fd with SMTP id 5b1f17b1804b1-4362aa1b061mr38281225e9.2.1734108512506; Fri, 13 Dec 2024 08:48:32 -0800 (PST) Date: Fri, 13 Dec 2024 16:48:05 +0000 In-Reply-To: <20241213164811.2006197-1-tabba@google.com> Mime-Version: 1.0 References: <20241213164811.2006197-1-tabba@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241213164811.2006197-10-tabba@google.com> Subject: [RFC PATCH v4 09/14] KVM: guest_memfd: Add KVM capability to check if guest_memfd is host mappable From: Fuad Tabba To: kvm@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-mm@kvack.org Cc: pbonzini@redhat.com, chenhuacai@kernel.org, mpe@ellerman.id.au, anup@brainfault.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, seanjc@google.com, viro@zeniv.linux.org.uk, brauner@kernel.org, willy@infradead.org, akpm@linux-foundation.org, xiaoyao.li@intel.com, yilun.xu@intel.com, chao.p.peng@linux.intel.com, jarkko@kernel.org, amoorthy@google.com, dmatlack@google.com, yu.c.zhang@linux.intel.com, isaku.yamahata@intel.com, mic@digikod.net, vbabka@suse.cz, vannapurve@google.com, ackerleytng@google.com, mail@maciej.szmigiero.name, david@redhat.com, michael.roth@amd.com, wei.w.wang@intel.com, liam.merwick@oracle.com, isaku.yamahata@gmail.com, kirill.shutemov@linux.intel.com, suzuki.poulose@arm.com, steven.price@arm.com, quic_eberman@quicinc.com, quic_mnalajal@quicinc.com, quic_tsoni@quicinc.com, quic_svaddagi@quicinc.com, quic_cvanscha@quicinc.com, quic_pderrin@quicinc.com, quic_pheragu@quicinc.com, catalin.marinas@arm.com, james.morse@arm.com, yuzenghui@huawei.com, oliver.upton@linux.dev, maz@kernel.org, will@kernel.org, qperret@google.com, keirf@google.com, roypat@amazon.co.uk, shuah@kernel.org, hch@infradead.org, jgg@nvidia.com, rientjes@google.com, jhubbard@nvidia.com, fvdl@google.com, hughd@google.com, jthoughton@google.com, tabba@google.com X-Rspamd-Queue-Id: E797DA001B X-Stat-Signature: ge8h7kq4muqr5tygrq95egyqd11yuqtr X-Rspam-User: X-Rspamd-Server: rspam09 X-HE-Tag: 1734108481-778455 X-HE-Meta: U2FsdGVkX1963etl1MZRQSETMs8t/n1zmeZuvhrWA8SuAm1Df2/mlSok13DobWTzECHa8JaWzVJ9iXX8g9Fi9UV5YhSs6stO3uAGYrTrveh4pFZpXigh2S9xeQ4QsGz3HT3+KBLCpX9zyWaGZiQZzcRea5utymsz7iysVCpjv+8L2DjJQ13393Vcty2re2vQfPZVe7gB0Y/Q2Fz5IDVUVE3PoUg0aj1eBzPCrd6/JAqbmLhaG+DzCUYKAUPO05C+RsZGJIB5CdSresjZF90fGgYD6XVhC/tA0ofRrnIeHkaLRLlLGJcdhNefTXEUr4YGNrTtI6eVjqhv8gYJl7bZYEJsX14fQxUMWJHECBzAg8/Q7vptT3G3HI0zQ80yIAIOifEFZFiuwVFSXjuC/JIKgyjtnwXU+A0h0sQLWYDSxt6pjNV8CvRLNs/yEluMw1L2b9OB63x0RgjUaza4mKOHxfdKhORoU9il814Qu7Di/25KyxJgWWismTRJkxNBsvLy6ZBfM1tY+E4wL1qP00B74G/oghqi0xZBAau2L0H2YTqbfZVW+55wXYBTQv8HV24nJG2PiX4/DdVCBd+8A4pLJpeplLCQhsi8RGUwTAM/YeKV8w5FdXKSlHMg8C9ZWQ5gnyUYk2z3hUVxUCoAxN5QUpWxL3cStGtN6rzqb2oTnCkhw04grnwI7tm/7iNIfHJzyG/uHfY0r9aIu9MlIGbpg3TkoIQR2GHSyrIQtgzz+RyGf5KO+yYEk79LLYh4jC0oRXsLFm0V4NsleMLV1EXYRbiaw/Pe3OxFTnCVVgAwMcEP552a1z542Rd8XEbQHC4NjsD2PN8CID7cIHyndYbiDjOxzZcj0SG2KIFopWkRA/jyh55qnQuvKge3li760/y73jLDPRA6TpKF5nHEx+UGnzUkRQ3LxYWtts8Hx+1Rl009+1aaJaIHEIrMekjpQaeP/YRJNfTAecII0uqUDtd /GjK/CXC dNTIDL1/p5IzdcY7+krf1EazQzHlXZtZsZEIPgwLxlcwMSNbWUFIlHYD92nNhFDbo+2LDKFMO3WPKq+B0h0GgROVIMakAbvyk7iCsBEA8zZIzW+JWF8jQgGfAakCVxmquhf1vxU2ReOVCqqD+USMuPNd6H9OUkfi+E108QPQDEe2QX3FyIcfmSFGs33gDqJL8r1DteNXoSj/1XWuh+8wKiwCBf9XIUcfYlOnHZ2LbO4QSlcdVmSc7WOHSHXa2oe7GHQVMpdMTE+aV4nw9i06C9dsKa9fdVPZTbgpGl+MJFkBXox7gFY8nl/o3SimtQfdKlCi32q70/uRpuKRAg6Qm0rLPXy3a3DZdt0hOU/fhBscVzZva+RsWkwSqqeuBODgxG4sb7B4O5sj4GAO/ZyqDUYs4W2KYY/gvfTVEugdBzIweu0zZrN/X8aGCZzwVB71uSPpn3x9asRvUiERfImmdjtIF0u6vIAFgu85CQVLVLPrY+HWP0B6GiyaE0piVYr/olv0DcO4t6Qu8Ifkg12n3H5IJKOjdXqUHGoXDZw5zPGHmLtZjY+1fPQ+kNcuYO1qrwd3oWlZwPhTZB7IY8WjjXqgT8sXEYk57BQjFgiEk4impN65RVF9n3pU+Hd59Zao/ffvXDjpT2OhiRJtbtmrltKHgRw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.010378, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Add the KVM capability KVM_CAP_GUEST_MEMFD_MAPPABLE, which is true if mapping guest memory is supported by the host. Signed-off-by: Fuad Tabba --- include/uapi/linux/kvm.h | 1 + virt/kvm/kvm_main.c | 4 ++++ 2 files changed, 5 insertions(+) diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 502ea63b5d2e..021f8ef9979b 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -933,6 +933,7 @@ struct kvm_enable_cap { #define KVM_CAP_PRE_FAULT_MEMORY 236 #define KVM_CAP_X86_APIC_BUS_CYCLES_NS 237 #define KVM_CAP_X86_GUEST_MODE 238 +#define KVM_CAP_GUEST_MEMFD_MAPPABLE 239 struct kvm_irq_routing_irqchip { __u32 irqchip; diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 53692feb6213..0d1c2e95e771 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -4979,6 +4979,10 @@ static int kvm_vm_ioctl_check_extension_generic(struct kvm *kvm, long arg) #ifdef CONFIG_KVM_PRIVATE_MEM case KVM_CAP_GUEST_MEMFD: return !kvm || kvm_arch_has_private_mem(kvm); +#endif +#ifdef CONFIG_KVM_GMEM_MAPPABLE + case KVM_CAP_GUEST_MEMFD_MAPPABLE: + return !kvm || kvm_arch_has_private_mem(kvm); #endif default: break; From patchwork Fri Dec 13 16:48:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13907468 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 85D07E77180 for ; Fri, 13 Dec 2024 16:48:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AF42E6B00AA; Fri, 13 Dec 2024 11:48:38 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A7C2F6B00AB; Fri, 13 Dec 2024 11:48:38 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8F7E06B00AC; Fri, 13 Dec 2024 11:48:38 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 6B9116B00AA for ; Fri, 13 Dec 2024 11:48:38 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 19A75A1095 for ; Fri, 13 Dec 2024 16:48:38 +0000 (UTC) X-FDA: 82890518466.25.E5A49AD Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf17.hostedemail.com (Postfix) with ESMTP id 9E50840016 for ; Fri, 13 Dec 2024 16:48:16 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=o0y9R1g7; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of 3YmVcZwUKCN8UBCCBHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3YmVcZwUKCN8UBCCBHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--tabba.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734108504; a=rsa-sha256; cv=none; b=w/F/N+2PN87i5kdBFTwXJqLMIdQqm7wdG00wPAadYotIup7K7qcJ3joGiqA3qDlhCITYqY ajppQTdIhZ+71OSxr+SA8A7SctE3zf4vNIAZN48AluWIPAyF4TrgavV6Z+cV/Ky7aSyYW9 K9acPedvoSzvaGc7qEW9C8qRKjjnIr4= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=o0y9R1g7; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of 3YmVcZwUKCN8UBCCBHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3YmVcZwUKCN8UBCCBHPPHMF.DPNMJOVY-NNLWBDL.PSH@flex--tabba.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734108504; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=WsEDyhBns9NxCkCIdpMm4WAodyMfjoY3V2TpfAI1mN0=; b=BP1TOByZhQ6AYXOmu14fJqWxK/mzuf6dDUrVgTZ5FjPfE02nJA9OlAQk9hz1HPIYbXwiTc whe5IaRB/zA4k4jhumur7CbDp925GKRvWvY8agovY4msNViQEQBSM+a3r2l4cPJ8+p1vbh iZNfT84X0c5Rbmp6qlv7QsgBAVhDA1I= Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4362f893bfaso4501865e9.1 for ; Fri, 13 Dec 2024 08:48:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734108515; x=1734713315; darn=kvack.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=WsEDyhBns9NxCkCIdpMm4WAodyMfjoY3V2TpfAI1mN0=; b=o0y9R1g7C8dho3mbPxlhqzFyHZ1VUf1fQfZaN1w3kh6yNOENTe5dEdgcAwiOfMEmz8 2eVUhePaEK3afcGgI3QDD0qcV2rHWmHQoGYGI/MOx6QKlSmUm/9X21YqyZLYdCqA4eKP pL1487C3+EBM5uTUSDGAdWHv5tH8yb8UoxOtSbKwbXxSU3QeTDjSTnkmXRSSzw32i1EK iHADXGADpBhVhg97ECLfn9DzMJDvWjPu2ujj+8niKqXWods7wQTbNEDAY4nj8WM3Yz78 OwWki22Ty0s2l/sPfpumToxg+MPnprX0FwGn5lXzh+/ExbCi68FLFR5NEPMuidxNje3u 3WuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734108515; x=1734713315; 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=WsEDyhBns9NxCkCIdpMm4WAodyMfjoY3V2TpfAI1mN0=; b=IaPhlvAsgphoEQJuNJl+wbs1aBIZ/Zsp4UyAxapVTiE5EyxVUFkdWibYjLnRn8OQwR y6+Q0LoS2/dOcxgs+Jb8C34nN1ZbITW+y97Z/mGjMCYv4aTT2/X1X5YqFVfmm1rPtcTe ZhbbiIIHiV86bSNaIn0I9i6yCuOd3C7OEIEK8eUryoKPGXpQ0p73Oz3HXpg433wZHHWe WMBENSmN3TKHU9jcpyQB5Obx5qjIEB/XePaJIQpJkkwaMN6oWgK45L2kGPgfIwIcY8iE Te+LAaHy+wUc7Eb7aoxrlJWzN6Vaxq+DfGlwKLCANOXClgK+tyCeHv6GNmJqATyEWN8Q HRHw== X-Forwarded-Encrypted: i=1; AJvYcCVsIbkhJ0vFam3X9YhBnocJB9QfqyVyUUIDb7V/A4S2TdTexqGPn0YMmhcaep0gNJwrliTxaYD+ag==@kvack.org X-Gm-Message-State: AOJu0YxAqw7lECakD577TK9dsROHjlYTsy5fkCER0dZ/Z52zrlu33auY tbmWSeKuXSQVAVTjhtO/5H5zSVcoV2ecB6i0UW6WtYTCPBDpVkfWwlXpAAN6NFkOEmUO+79VeA= = X-Google-Smtp-Source: AGHT+IGBPIX0XH2E/QAG4VVkoTgxq7y6qEK9iHPxA7arOJ8hwxyGtcqtTyclPQC42evEdmBRc/aMqmUXOw== X-Received: from wmox24.prod.google.com ([2002:a05:600c:1798:b0:434:f7cf:3389]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1da4:b0:434:faa9:5266 with SMTP id 5b1f17b1804b1-4362aa43222mr32447565e9.13.1734108514902; Fri, 13 Dec 2024 08:48:34 -0800 (PST) Date: Fri, 13 Dec 2024 16:48:06 +0000 In-Reply-To: <20241213164811.2006197-1-tabba@google.com> Mime-Version: 1.0 References: <20241213164811.2006197-1-tabba@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241213164811.2006197-11-tabba@google.com> Subject: [RFC PATCH v4 10/14] KVM: guest_memfd: Add a guest_memfd() flag to initialize it as mappable From: Fuad Tabba To: kvm@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-mm@kvack.org Cc: pbonzini@redhat.com, chenhuacai@kernel.org, mpe@ellerman.id.au, anup@brainfault.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, seanjc@google.com, viro@zeniv.linux.org.uk, brauner@kernel.org, willy@infradead.org, akpm@linux-foundation.org, xiaoyao.li@intel.com, yilun.xu@intel.com, chao.p.peng@linux.intel.com, jarkko@kernel.org, amoorthy@google.com, dmatlack@google.com, yu.c.zhang@linux.intel.com, isaku.yamahata@intel.com, mic@digikod.net, vbabka@suse.cz, vannapurve@google.com, ackerleytng@google.com, mail@maciej.szmigiero.name, david@redhat.com, michael.roth@amd.com, wei.w.wang@intel.com, liam.merwick@oracle.com, isaku.yamahata@gmail.com, kirill.shutemov@linux.intel.com, suzuki.poulose@arm.com, steven.price@arm.com, quic_eberman@quicinc.com, quic_mnalajal@quicinc.com, quic_tsoni@quicinc.com, quic_svaddagi@quicinc.com, quic_cvanscha@quicinc.com, quic_pderrin@quicinc.com, quic_pheragu@quicinc.com, catalin.marinas@arm.com, james.morse@arm.com, yuzenghui@huawei.com, oliver.upton@linux.dev, maz@kernel.org, will@kernel.org, qperret@google.com, keirf@google.com, roypat@amazon.co.uk, shuah@kernel.org, hch@infradead.org, jgg@nvidia.com, rientjes@google.com, jhubbard@nvidia.com, fvdl@google.com, hughd@google.com, jthoughton@google.com, tabba@google.com X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 9E50840016 X-Stat-Signature: ma74exkbi5qijyby86ce4mw51pochexn X-HE-Tag: 1734108496-818469 X-HE-Meta: U2FsdGVkX1/Sk+m7mAarDRHKz+nD3r0h+U4FbU2C2dptKY+18Fv79rZr8hpU0GyMig0/OyWTnNA7IraI1O9jkZinAfcNGg1UtT++ToDIMCU0+/tdBamIYLuWaM/v0OVGq91zcbkAlqNkOxi7vN9DjguNXb+X0UTljAEnVblGVKTGxRFev4g3wOtRgwwBNkOPBDC5kpxjeQr51DvCyxuvm3DmfHjovYVw0mJ+oPgxSMa7cP4QLCXLpI3mlf2o8wTAazi6gK9WVSrRtnVUSatcy1xsBQ7b+VvdFKMIXnf9gfKqv2ZigCNoatRofmq8hTe6ciGJx73f6/sJyW5/eBPBDuiCqHtTW2thQ1HQB7TDAu1PQ11G+EDw2Tfp2U4LIggfAebc3MOncr7T/dfOP0c30xXmGaViFTRYDlWdnGHHKPuhpU7vUUh2SiwRHCpz2yOxVQv3qR0YSxEkmCnPom4AKO4WROw6Tc7llWJKK0r5QVm6cttsL727oeOCiFOI88VFsZzSerG1W2+6evXvzoxMQtXoX3lNd1+d24UsPl8OrTQURHBHBb591HCZMX1pxNNTX1vw45yNbQ3u+WPNet0mKY/bmPmIz55CDtk7gomcOuNQkBfRFxL9eKFSfSJncDM+VCFPpy8TzaCJfLstvah5lZs1TQIJf9li15C0j3mlMxiqVkgphVz3IzPamODjmaqEy3YT+/gFKdGV7uYthIohZWZGsCty1a3SWYwDpr7kBH5Gbl894W5fd+F2XVfA3UnRm6q4mXiXZbOxnNPBsLcZh9lY2jICPIl7IMx55nu2cZ6eUYNI1UriggMeHRulpAg/ciaPw+5vgJwLNBCpEUa6LBPqgb6S6BLpwBdRVM/1eyuGrAOLbtTadWrNDEn5Ir9jYQnj3uDq/OiU3i6N/bxFhU70eyhXWmLdWVStzPUmi022qFA/WdtbydXNWjbc5gn+/PgAKkH129sJbgRb9L5 V4WTVYek kOYnfiUEa+RcTjs44O5FMDKeLFNsIll0qBNlSEj6GCcsWbFzKxtbBQHOzfccii38D9W9Q3ANnX/7j+KYAWoAYSkjr6FKd+XJSogZCDvqb+gFf2c7pJTJGpi5WX0I0f8k/bjsnTnKwtKPCLeNHSnldmJsYo5ZWBlsBVBgTmgRK/ErO/oZbnyAyps0h3YmIFxG+jRjg/EPAo0CLvydL5coaQP/IubHrShpXI9qgYCHc1ee3LAFExf3V8Q+H7QahfdLKMzAShAjjigKMYrN717sY3scTnCnB0l7JNX3qlC7oiQhsOT46siUf2Q1/YdvYvnXdH1Pz6eV8UklfK8mNrD9lqODqvlyyMecUzpTVafY6w/qe2mnhXyEVzd/MyfQtLueLVNwZTOglF3OcBwpDPTIAG+6vLW70XKRLZGtVTm5cmBiLJL70Xc7syB8lQnljcPWzS0TVWEKGNgeMJd9ubc15u2IlTs54A7zXaHgutDEAFnIaYg5O2bpjBwBAT8aDJUbJ17qRxJXTaga15zkljVQgREGkJ+ub4JDhb9JNdh5GDOdHtbJNnT7/bP6GG4sLz2j7dK9Zm26ZSM6osOqVGAFAkYJDuibu5Y1Nl2GoT+EE8GdtL7aiAhAlcCPnGgea2cqLeSbGEG8zXYv8Sz5yLfx49lMFRbYKONoChMuvX6kbxQ197YveC4C5p9Fukw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000092, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Not all use cases require guest_memfd() to be mappable by the host when first created. Add a new flag, GUEST_MEMFD_FLAG_INIT_MAPPABLE, which when set on KVM_CREATE_GUEST_MEMFD initializes the memory as mappable by the host. Otherwise, memory is private until shared by the guest with the host. Signed-off-by: Fuad Tabba --- Documentation/virt/kvm/api.rst | 4 ++++ include/uapi/linux/kvm.h | 1 + tools/testing/selftests/kvm/guest_memfd_test.c | 7 +++++-- virt/kvm/guest_memfd.c | 6 +++++- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 454c2aaa155e..60b65d9b8077 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6380,6 +6380,10 @@ most one mapping per page, i.e. binding multiple memory regions to a single guest_memfd range is not allowed (any number of memory regions can be bound to a single guest_memfd file, but the bound ranges must not overlap). +If the capability KVM_CAP_GUEST_MEMFD_MAPPABLE is supported, then the flags +field supports GUEST_MEMFD_FLAG_INIT_MAPPABLE, which initializes the memory +as mappable by the host. + See KVM_SET_USER_MEMORY_REGION2 for additional details. 4.143 KVM_PRE_FAULT_MEMORY diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 021f8ef9979b..b34aed04ffa5 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1566,6 +1566,7 @@ struct kvm_memory_attributes { #define KVM_MEMORY_ATTRIBUTE_PRIVATE (1ULL << 3) #define KVM_CREATE_GUEST_MEMFD _IOWR(KVMIO, 0xd4, struct kvm_create_guest_memfd) +#define GUEST_MEMFD_FLAG_INIT_MAPPABLE (1UL << 0) struct kvm_create_guest_memfd { __u64 size; diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing/selftests/kvm/guest_memfd_test.c index ce687f8d248f..04b4111b7190 100644 --- a/tools/testing/selftests/kvm/guest_memfd_test.c +++ b/tools/testing/selftests/kvm/guest_memfd_test.c @@ -123,7 +123,7 @@ static void test_invalid_punch_hole(int fd, size_t page_size, size_t total_size) static void test_create_guest_memfd_invalid(struct kvm_vm *vm) { size_t page_size = getpagesize(); - uint64_t flag; + uint64_t flag = BIT(0); size_t size; int fd; @@ -134,7 +134,10 @@ static void test_create_guest_memfd_invalid(struct kvm_vm *vm) size); } - for (flag = BIT(0); flag; flag <<= 1) { + if (kvm_has_cap(KVM_CAP_GUEST_MEMFD_MAPPABLE)) + flag = GUEST_MEMFD_FLAG_INIT_MAPPABLE << 1; + + for (; flag; flag <<= 1) { fd = __vm_create_guest_memfd(vm, page_size, flag); TEST_ASSERT(fd == -1 && errno == EINVAL, "guest_memfd() with flag '0x%lx' should fail with EINVAL", diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 3d3645924db9..f33a577295b3 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -939,7 +939,8 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) goto err_gmem; } - if (IS_ENABLED(CONFIG_KVM_GMEM_MAPPABLE)) { + if (IS_ENABLED(CONFIG_KVM_GMEM_MAPPABLE) && + (flags & GUEST_MEMFD_FLAG_INIT_MAPPABLE)) { err = gmem_set_mappable(file_inode(file), 0, size >> PAGE_SHIFT); if (err) { fput(file); @@ -968,6 +969,9 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args) u64 flags = args->flags; u64 valid_flags = 0; + if (IS_ENABLED(CONFIG_KVM_GMEM_MAPPABLE)) + valid_flags |= GUEST_MEMFD_FLAG_INIT_MAPPABLE; + if (flags & ~valid_flags) return -EINVAL; From patchwork Fri Dec 13 16:48:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13907469 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 4B42AE7717F for ; Fri, 13 Dec 2024 16:48:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 995606B00AB; Fri, 13 Dec 2024 11:48:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8F57C6B00AD; Fri, 13 Dec 2024 11:48:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 770916B00AE; Fri, 13 Dec 2024 11:48:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 5202B6B00AB for ; Fri, 13 Dec 2024 11:48:40 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 0D357A107E for ; Fri, 13 Dec 2024 16:48:40 +0000 (UTC) X-FDA: 82890519012.18.C7BDFAF Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) by imf10.hostedemail.com (Postfix) with ESMTP id EC278C0019 for ; Fri, 13 Dec 2024 16:48:26 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=G6Kt36GF; spf=pass (imf10.hostedemail.com: domain of 3ZGVcZwUKCOEWDEEDJRRJOH.FRPOLQXa-PPNYDFN.RUJ@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3ZGVcZwUKCOEWDEEDJRRJOH.FRPOLQXa-PPNYDFN.RUJ@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734108494; a=rsa-sha256; cv=none; b=OEjupIT6S2YUgpqrm4MokKX2+d5Ms5OjMXAN1gRSrKX6rnx1SfLRm4UUcZx+sDrmmgYvuS xGanaJwgE9mjg5W0AtZR/WDDQ60oeTkSiJ14HR4BOJaXbT8dZeqcwPJUL0ASXrTyCebpKn e2A9CJlzl/EBvYwlytEm4e2locO3f0A= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=G6Kt36GF; spf=pass (imf10.hostedemail.com: domain of 3ZGVcZwUKCOEWDEEDJRRJOH.FRPOLQXa-PPNYDFN.RUJ@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3ZGVcZwUKCOEWDEEDJRRJOH.FRPOLQXa-PPNYDFN.RUJ@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734108494; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=DhFiA/KCvNxVvN2dxXqgdhmMKMTNkZQ9H4IjGYFoLJU=; b=Qq+0ISzVXpG9aajAjJ7YYKUTId2CX3LF+QPeCAewHWLOL1YxQwxrt/JeNWk0su31mE2vUY 3GLxP6Ve61xEos2mo9ZIjszCMlJ2glupy4d7SlJ6iYUY39hmvBKrMxXpOWW18V7vlcAbGw gMhUXHmsXaCZm+7k3MWrtFEGajzP4Bc= Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-4362153dcd6so11693135e9.2 for ; Fri, 13 Dec 2024 08:48:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734108517; x=1734713317; darn=kvack.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=DhFiA/KCvNxVvN2dxXqgdhmMKMTNkZQ9H4IjGYFoLJU=; b=G6Kt36GFBUDQ9/H8ejypP1i1jNjVqo5DyLLMRv9VExIGn0zMatYRISS8uaQIncH4T2 bccVoy2qTitUKxRnE7WW9KB0//hFGWYfMXihYaryeLnkJq2OL64IN0tuGMS218dUXNVL bM/cZiNefLce3mlGWc5ExgvRNZLtzvZpRIw0t4KrSK34UnpqLiYI0lt1FlByOtwJ8FCn p6ntCfjQKxLkcDUH8zrlwQbz0r5PMCl9+LTUKJ6JGv9Oggymae26jCdaK6m+L4go37Wa z+aVxytKeastKAzuYDlDrXBepgJbLBL4U3Iqn0ZVn9un01u+nmhpUkonZd56PXXsQv0v oLyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734108517; x=1734713317; 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=DhFiA/KCvNxVvN2dxXqgdhmMKMTNkZQ9H4IjGYFoLJU=; b=YI1ecNc1GX9NuSAbEbUI8bia8BGNZecpb05AkBe9ry1LXu4hcBO3LwKYWfP8ZuR7jk gxDFFiNnJZbQ8nPpNpuhIGa6JtPgqs53wiusmJb29SrlrLVjyeIRIe2H8bkvVBAyoQ3u dnq67um4SI3ESBkulsTEOtUJc3PcV8HlLqOok3npw9EWIFqClrO9ItSSv2J/cI1fzYv7 +kBx7WDikFVQvXvQPFUDaWcmEqte9yy7vcfXpfgSzoV9IN+zR+msXItlwoHYKqS8qLkT qaweehw/nAFdB2i3BwJO/w2SgCoNaJCSjtLXdcILBsT8Y1vqG4XdSb3318wC5vA+AlEp SEEQ== X-Forwarded-Encrypted: i=1; AJvYcCXkAvUwNpwuhbKqBfoCc7sAmrlUgbujDfXYYQLJLQEyhCbW1uk0XVePLdA4syvhCYmTbnc4UIGtMg==@kvack.org X-Gm-Message-State: AOJu0YzroZi8FxX03AtkiRJAa7c8mLjeaRV0b2Wy1EqoSlexmHdTzXGM Nbwtf4m0Beh7ANvxdpSyNY+KXSKzvv7pm4Z2MC0qgeazWPRXNf6pxttGADHy0hHSI6o3987v1A= = X-Google-Smtp-Source: AGHT+IFC3wQu843hFQBCJ/CWlcnzA5CxWqK8aB90iNtnojUxP19hTT+b2JGWT0UhJ1/fNK7qaPLpg8+Hqw== X-Received: from wmmu14.prod.google.com ([2002:a05:600c:ce:b0:434:f0a3:7876]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a7b:cb89:0:b0:436:1af4:5e07 with SMTP id 5b1f17b1804b1-4362aa1561emr25562455e9.1.1734108516946; Fri, 13 Dec 2024 08:48:36 -0800 (PST) Date: Fri, 13 Dec 2024 16:48:07 +0000 In-Reply-To: <20241213164811.2006197-1-tabba@google.com> Mime-Version: 1.0 References: <20241213164811.2006197-1-tabba@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241213164811.2006197-12-tabba@google.com> Subject: [RFC PATCH v4 11/14] KVM: guest_memfd: selftests: guest_memfd mmap() test when mapping is allowed From: Fuad Tabba To: kvm@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-mm@kvack.org Cc: pbonzini@redhat.com, chenhuacai@kernel.org, mpe@ellerman.id.au, anup@brainfault.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, seanjc@google.com, viro@zeniv.linux.org.uk, brauner@kernel.org, willy@infradead.org, akpm@linux-foundation.org, xiaoyao.li@intel.com, yilun.xu@intel.com, chao.p.peng@linux.intel.com, jarkko@kernel.org, amoorthy@google.com, dmatlack@google.com, yu.c.zhang@linux.intel.com, isaku.yamahata@intel.com, mic@digikod.net, vbabka@suse.cz, vannapurve@google.com, ackerleytng@google.com, mail@maciej.szmigiero.name, david@redhat.com, michael.roth@amd.com, wei.w.wang@intel.com, liam.merwick@oracle.com, isaku.yamahata@gmail.com, kirill.shutemov@linux.intel.com, suzuki.poulose@arm.com, steven.price@arm.com, quic_eberman@quicinc.com, quic_mnalajal@quicinc.com, quic_tsoni@quicinc.com, quic_svaddagi@quicinc.com, quic_cvanscha@quicinc.com, quic_pderrin@quicinc.com, quic_pheragu@quicinc.com, catalin.marinas@arm.com, james.morse@arm.com, yuzenghui@huawei.com, oliver.upton@linux.dev, maz@kernel.org, will@kernel.org, qperret@google.com, keirf@google.com, roypat@amazon.co.uk, shuah@kernel.org, hch@infradead.org, jgg@nvidia.com, rientjes@google.com, jhubbard@nvidia.com, fvdl@google.com, hughd@google.com, jthoughton@google.com, tabba@google.com X-Rspamd-Queue-Id: EC278C0019 X-Stat-Signature: 6rpwns17tnb8f8fwtyy7dsyfc6wzetie X-Rspam-User: X-Rspamd-Server: rspam09 X-HE-Tag: 1734108506-812348 X-HE-Meta: U2FsdGVkX1/+T/QVOxHA9xzhHXj1t8TpTGiaTNvy7X6CS2uAO1E9+fd+ioVwhrZCyMgK1reH6a6KzH+RlGbsN4bRrn2VXhd+bNwI9ue7ca2iajxLRyIrH4pnCxa2sQ4odhis14ZA+AlNuFlPdoZ0EJm/6Rnbsflbq/4SnqxJFeJfTrrbPpQd9RrfjAymV+eGaK5jDl1oNsfAKuLm0vh/hNPSX5/wqt0vbNlz5OF7P/I50LZtTBuLxubMSqrfWfxKRVxPMTWb6OGI5FKgJaoFbKE0fGB+xw2ThBBIliZp8LxJSI3S+N9jr57bZ4muu3Ja6wgGoa++VnXdphmRxNk1waw8E805c3qjNCI3rEIHYWldhB1ui++P2qeQXKTRYhcCvMnFG2j4Ojf4FWRpGYKk0OtYiFVkgVWmQbSIz0cLV5KAvweFEXYWcmTknlEQCiGyJ7gfstRUCYt1o6/QYuAIJ/WhKJkwHI3EiTg8rKya6LRgVU66NpUmQpapEtyBicY8ut2GerzgRbqBrF7WM9fYu41L1M4aV4ex04ntC62iQ+SZMZPVKywiPwlElsjFA/FoIUWmOqMayuxfJZ1ruCojX8ign1lDWfxjr83Z2L7E+IXy5hAzO5jQEXWoDl74Sd8ea1XuwpA2MAayK3mcaNx5uuwSwSw9ShC2AuEpGFrTQXjalOFy84safKK/T6h4bsbonP/LQLrTTkbWVs+ZBxL7MuDAq+SS69DK+QRkx5b4UkIpVYXWttNYJOCS6bI6Z817cajAPuFsH63kwsz2ajNNFOj2Mi2wYsdHOMBvot5oz8d7FiORpB1I3D8xbKYs7tYuxUrqNSoaIhwHiwqV0aQpjCQteoBhA+MHACUxZda8iXKDdzZz1Gl8pLsbLIqR8jP5q8vuRD34SUqSRie5OoWwH/ZAVkH9xYKO3/POowN89N/pZWBua6XiLssEmLYRwQY5afexb+xSDmkFrVans7Z aC8VSkwL KMJzRFDdeAekZzOsjDf60aDsaPM3KjZATnpEr0TKHCaSx0xid7dI7X1l0aV7JIIkfubR9Qg6wmsjMYloR+jfLG0lreBxcBbM4h0Z4fbC4S4n8tVxC1tyxgaykAeWCXkjnaiazDZvf2exwa/+Uyn8usga8M4aHa7iwQhvGHbMiX5xAZ4di6b38wrp4Fk4tWlVwJxmDtTgIgzHJfRT+QlO1PLzmA/FDAXsps+K0URan6hjb6G2wD75YfCwF2OoP5YmPLMCwjLMZt1yp5LWKW3zdMVB6mnQy2trPyhTort+HY9HI3UoBSAHFwQpNb0Ce4s8PcEYXPc/7w3Z5yr0N5LFYZsMW+J/tEUe/pRT8N9P2L+K81MvLZbMvehZZChCuV8EvATgGRzFAfGEjOPR/WKTn8Pnc4iNKLbxd+Tcc7BmEXsw+Wqag7KJ4nc6kkZyM5rXWiiwQEJ6A4kCCy/Gbtpt44NQhhK4azECxiwIKwEBBLalXKz2RUBJtSlHuRlQPTn6Zvn0951vDQ39jFdZQR1aVQh/jkFCIjt8yKZm6VxWUngd0Zv3yOvdje6DWjCVEiETnPMW6z1cPt4hwCNum+zaPidi1nc7ONnPpYjAbmZaDtqrADjMWZ2iARsrcfKCIgxmug23xX4dvb90VMWeIhj2Ef6b+Ww== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000752, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Expand the guest_memfd selftests to include testing mapping guest memory if the capability is supported, and that still checks that memory is not mappable if the capability isn't supported. Also, build the guest_memfd selftest for aarch64. Signed-off-by: Fuad Tabba --- tools/testing/selftests/kvm/Makefile | 1 + .../testing/selftests/kvm/guest_memfd_test.c | 57 +++++++++++++++++-- 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/tools/testing/selftests/kvm/Makefile b/tools/testing/selftests/kvm/Makefile index 41593d2e7de9..c998eb3c3b77 100644 --- a/tools/testing/selftests/kvm/Makefile +++ b/tools/testing/selftests/kvm/Makefile @@ -174,6 +174,7 @@ TEST_GEN_PROGS_aarch64 += coalesced_io_test TEST_GEN_PROGS_aarch64 += demand_paging_test TEST_GEN_PROGS_aarch64 += dirty_log_test TEST_GEN_PROGS_aarch64 += dirty_log_perf_test +TEST_GEN_PROGS_aarch64 += guest_memfd_test TEST_GEN_PROGS_aarch64 += guest_print_test TEST_GEN_PROGS_aarch64 += get-reg-list TEST_GEN_PROGS_aarch64 += kvm_create_max_vcpus diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing/selftests/kvm/guest_memfd_test.c index 04b4111b7190..12b5777c2eb5 100644 --- a/tools/testing/selftests/kvm/guest_memfd_test.c +++ b/tools/testing/selftests/kvm/guest_memfd_test.c @@ -34,12 +34,55 @@ static void test_file_read_write(int fd) "pwrite on a guest_mem fd should fail"); } -static void test_mmap(int fd, size_t page_size) +static void test_mmap_allowed(int fd, size_t total_size) { + size_t page_size = getpagesize(); + char *mem; + int ret; + int i; + + mem = mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + TEST_ASSERT(mem != MAP_FAILED, "mmaping() guest memory should pass."); + + memset(mem, 0xaa, total_size); + for (i = 0; i < total_size; i++) + TEST_ASSERT_EQ(mem[i], 0xaa); + + ret = fallocate(fd, FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE, 0, + page_size); + TEST_ASSERT(!ret, "fallocate the first page should succeed"); + + for (i = 0; i < page_size; i++) + TEST_ASSERT_EQ(mem[i], 0x00); + for (; i < total_size; i++) + TEST_ASSERT_EQ(mem[i], 0xaa); + + memset(mem, 0xaa, total_size); + for (i = 0; i < total_size; i++) + TEST_ASSERT_EQ(mem[i], 0xaa); + + ret = munmap(mem, total_size); + TEST_ASSERT(!ret, "munmap should succeed"); +} + +static void test_mmap_denied(int fd, size_t total_size) +{ + size_t page_size = getpagesize(); char *mem; mem = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); TEST_ASSERT_EQ(mem, MAP_FAILED); + + mem = mmap(NULL, total_size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); + TEST_ASSERT_EQ(mem, MAP_FAILED); +} + +static void test_mmap(int fd, size_t total_size) +{ + if (kvm_has_cap(KVM_CAP_GUEST_MEMFD_MAPPABLE)) + test_mmap_allowed(fd, total_size); + else + test_mmap_denied(fd, total_size); } static void test_file_size(int fd, size_t page_size, size_t total_size) @@ -175,13 +218,17 @@ static void test_create_guest_memfd_multiple(struct kvm_vm *vm) int main(int argc, char *argv[]) { - size_t page_size; + uint64_t flags = 0; + struct kvm_vm *vm; size_t total_size; + size_t page_size; int fd; - struct kvm_vm *vm; TEST_REQUIRE(kvm_has_cap(KVM_CAP_GUEST_MEMFD)); + if (kvm_has_cap(KVM_CAP_GUEST_MEMFD_MAPPABLE)) + flags |= GUEST_MEMFD_FLAG_INIT_MAPPABLE; + page_size = getpagesize(); total_size = page_size * 4; @@ -190,10 +237,10 @@ int main(int argc, char *argv[]) test_create_guest_memfd_invalid(vm); test_create_guest_memfd_multiple(vm); - fd = vm_create_guest_memfd(vm, total_size, 0); + fd = vm_create_guest_memfd(vm, total_size, flags); test_file_read_write(fd); - test_mmap(fd, page_size); + test_mmap(fd, total_size); test_file_size(fd, page_size, total_size); test_fallocate(fd, page_size, total_size); test_invalid_punch_hole(fd, page_size, total_size); From patchwork Fri Dec 13 16:48:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13907470 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 DFBE4E7717F for ; Fri, 13 Dec 2024 16:48:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AF4956B00AE; Fri, 13 Dec 2024 11:48:42 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A07796B00AF; Fri, 13 Dec 2024 11:48:42 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8340D6B00B0; Fri, 13 Dec 2024 11:48:42 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 62CF36B00AE for ; Fri, 13 Dec 2024 11:48:42 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 2351112101F for ; Fri, 13 Dec 2024 16:48:42 +0000 (UTC) X-FDA: 82890517836.17.26945B0 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf14.hostedemail.com (Postfix) with ESMTP id D4AF010001A for ; Fri, 13 Dec 2024 16:48:11 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=LihBLwQv; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf14.hostedemail.com: domain of 3Z2VcZwUKCOQZGHHGMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3Z2VcZwUKCOQZGHHGMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--tabba.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734108508; a=rsa-sha256; cv=none; b=qsPdtjSf9fF3Hp5JMwWoaViRjQXJJ6CPjfMQsjvqnYHU0em1H62HN2bxqsuWZaJqFTxxJE 10Lfph9508vI5yN4CJVVACyzTGBG2HEiA69y5zpIQmV4yXrqkXg1kzenWMQ866vOxfafN8 8KXJWStbr3tcQqiMT276m+XCRUIXJIE= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=LihBLwQv; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf14.hostedemail.com: domain of 3Z2VcZwUKCOQZGHHGMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3Z2VcZwUKCOQZGHHGMUUMRK.IUSROTad-SSQbGIQ.UXM@flex--tabba.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734108508; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=g2VJIq0JFQ/ZqzbxK3We3Qf3gNhX7/2qVuXpD8D4O2A=; b=G4oxldY4hGEpr+QrGYYBqxaSL9s/UlKhRy+D4Eoy00NlBGd/35UnZtfY5dc+PfmdrcWbfv HWr4WkZaJsG48Tx3mdGpBBXYiRX5BcW59WErH8MNPuZM9Jsok4lax+eHwcIOGB9MBsocrw jqGPOyIZGThp9iI93z9JiSCf4MPOOJI= Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-43623bf2a83so17816625e9.0 for ; Fri, 13 Dec 2024 08:48:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734108519; x=1734713319; darn=kvack.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=g2VJIq0JFQ/ZqzbxK3We3Qf3gNhX7/2qVuXpD8D4O2A=; b=LihBLwQvy7MuSeBdvS9ysETk9cVZJhDlU/8HwrofMXCHts+qqW6MBRpqAnN0brrK/l ert8iJfjspqqz+zJqw6Vwd3Dzm0JhtgO6Qi0WJq1lJu5v+TckWpO4ZC9FG8YNEJKvFSu ISKEe2iyt5nYG6UYhzM+oxmRhrGkMIwT39sR/U1KLN/jzWh2O2omRGO+UbXnjDUvTh8V OH0OR1ltzqBxxM00d2No3wtkqtwjIrN8re377UFkfHXV3carAExe6vUXy9PVYNt9ymk1 gDXAAwtxo9NEnOK0AjGnFAlCkpwzDEUwgqPbBAQMCxQRg7Xb+SBd5RqSMO/jtObo9x8l VzDg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734108519; x=1734713319; 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=g2VJIq0JFQ/ZqzbxK3We3Qf3gNhX7/2qVuXpD8D4O2A=; b=MTA42BxzXj7plnui+t661P8QM/cxy/DdBW3L55/b2nH4U8kKyJLeGxuELUYQKfTcWz UmnloVf2PBvcTwtB9rvLbN73EFxiv1OSVm9+kyz7dogCkPJE0IaL/PcYTQdSNhdPZ/sT K8pdCtQ30ZPBr/ZtdvXDMie2EWQ7Nrx+U9daX3MEs08MLYRfkUtpvy5bbwfsnkk82SHy oLcTIOH3DZqJYSdCq/bsdSgpsqZZg9HugBikf1d0ywWD6vHRhxYiPQuNXiMftsJNamVt KybVfqWm1GV0CHDGoG+zEoj4foX4TaI+vSQKyf/dYVju1OcicoDmQdlvgGXMPCuZDkNw /kaA== X-Forwarded-Encrypted: i=1; AJvYcCXyAehktbv/ej3zwBZGjmri0tSQERdXUgRFG+jgkxvHhDJ5SLfbKoiK5e4g876njen/wGgi6zriZg==@kvack.org X-Gm-Message-State: AOJu0YxQFXnQetrk+jO4DeTBercF3uPOsB/QALBztelNJUjY/0Vx0vov zZCYnBU3uWCgPG4K5o0jxVPoTs/A7R5Gu+Fle5v3SPN7Ps0PfObyT5RtNdzu8fYztfSEgN5pXQ= = X-Google-Smtp-Source: AGHT+IEHYbi0erAT5PMhFOWMD98RX6BEQ5VdWuD/tN2fh0Tz16i8Z/iaZQVtS7R87H/i8b+meXfFpU9LHw== X-Received: from wmsn8.prod.google.com ([2002:a05:600c:3b88:b0:434:f018:dd30]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:34c2:b0:434:fddf:5c0a with SMTP id 5b1f17b1804b1-4362aa156a5mr33549795e9.3.1734108519019; Fri, 13 Dec 2024 08:48:39 -0800 (PST) Date: Fri, 13 Dec 2024 16:48:08 +0000 In-Reply-To: <20241213164811.2006197-1-tabba@google.com> Mime-Version: 1.0 References: <20241213164811.2006197-1-tabba@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241213164811.2006197-13-tabba@google.com> Subject: [RFC PATCH v4 12/14] KVM: arm64: Skip VMA checks for slots without userspace address From: Fuad Tabba To: kvm@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-mm@kvack.org Cc: pbonzini@redhat.com, chenhuacai@kernel.org, mpe@ellerman.id.au, anup@brainfault.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, seanjc@google.com, viro@zeniv.linux.org.uk, brauner@kernel.org, willy@infradead.org, akpm@linux-foundation.org, xiaoyao.li@intel.com, yilun.xu@intel.com, chao.p.peng@linux.intel.com, jarkko@kernel.org, amoorthy@google.com, dmatlack@google.com, yu.c.zhang@linux.intel.com, isaku.yamahata@intel.com, mic@digikod.net, vbabka@suse.cz, vannapurve@google.com, ackerleytng@google.com, mail@maciej.szmigiero.name, david@redhat.com, michael.roth@amd.com, wei.w.wang@intel.com, liam.merwick@oracle.com, isaku.yamahata@gmail.com, kirill.shutemov@linux.intel.com, suzuki.poulose@arm.com, steven.price@arm.com, quic_eberman@quicinc.com, quic_mnalajal@quicinc.com, quic_tsoni@quicinc.com, quic_svaddagi@quicinc.com, quic_cvanscha@quicinc.com, quic_pderrin@quicinc.com, quic_pheragu@quicinc.com, catalin.marinas@arm.com, james.morse@arm.com, yuzenghui@huawei.com, oliver.upton@linux.dev, maz@kernel.org, will@kernel.org, qperret@google.com, keirf@google.com, roypat@amazon.co.uk, shuah@kernel.org, hch@infradead.org, jgg@nvidia.com, rientjes@google.com, jhubbard@nvidia.com, fvdl@google.com, hughd@google.com, jthoughton@google.com, tabba@google.com X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: D4AF010001A X-Stat-Signature: tss97on9bomurntnzpp8m558der5jhjj X-HE-Tag: 1734108491-597314 X-HE-Meta: U2FsdGVkX1+hAFSLMbp22WzjfSAJ5RzuoE41lOhb+Bl73Fo5bmSJ4nTQDSYBobQRmqCSVcXlu4HCsxmMjb1j0YbrPCwZpnLNfDVWiP8GGIrtFlkbsXeqSANn5TZBTu1U5S822mFjY4opJB4WqC6/rLIS/tiXLpqjpVD/2/GUhsKPVhMK4b7fGQFbxjU4YdAtGbo2SwMYHuNC/We7zXWlnllP1boxojEe+eAQHL0wbywF8t4IcQ8Q2vAOCStRc1O1T39GjcVPCAbYg3OsQF3rnqdMQf67kY4yBBioxah+tAKp+x1ykUwYc3mu9fkxXXeQFFlK+mbPLMtpI5EyyKTyZ0tmnD0tLxvPec9R12oh8kykdnXNLgILVx2X8OzTNwuw6Ylv0f8hQDwsSO8J72HDXiXw02edXBs7ZM1wOwJ7F0q6NqCjjkHlkpR1IXlhhEDSss/Jc7XdFYIFbLfopHV1x5TCF768RoDN+qIBPbKIFRbSHtnl9nUltFh8jqyEaUkFjFj1PttM65kvQnVdXWNUr4MQOFrhB7+y1TzdCiJkQznUt4pFG8frFoYPowV0ME/Lx3wocDWi8CHXHhEILBzGZR7OR74jCKA1DaQpw+FHFJloZrAgvPVZnhzfEY09rrkAsQRERwPztvulZ78J3j8eU5iTrq5etyOLRvr2Gl4p26nP8Bk0OJf1sqAa/oRb57ipd2t/UHgexd2MFxqrsAI9VO3deFvyvh/Zw+YqmjTGZcpUyKxZ0IOZu0zg7fB52/LGeJN47uKCVaggWmvyQExjUPzq5zjEdWaJtSh8fXZykzQ6P6P2tApV/DJxSqlyxLr+81tLYahw43xIWEFoNsIQmxGrfl28L/5VDZVuEUEHc70mG+M9J7/7j1bogkMFS1WRzDYiggSYj9nWJbtOj1wCWlxFIBTeq3LiW8qMJ8TNcCQbGLrga1wtuOFPcHYOvUHz+h4f4X3iq0PCiWvaaFv 6eVf6Nux wteonCzQVJ6LH6ozMy9gvsfJGmYrA4XzTbLutXP0PGuoSuszwWp83jh1YsYMKF2vbdz2PK/UbCRe8rtlVcqspC8r9a2Tl/b/M+gXnK9XWUwaYaChjuBLbfIzqtxHIWJdjqFbJGX/q8KFS4AiIfxlxul8ZIv68S6vIUt4Mb9cbssnLOpJv4v59/l4zlD34U6LAeosvO8HYPyot5zCrpdqZp/StObYS6wzMiss0SEPW73nbYq4T39mguHulTFXQId1qz8HOyH8G77KyYx0fYqOcUeNj7Z0F1TqnNUrldCbeOMe8iE5O2gdjq4MTMoJSz4MVljxIvwRQcB0dfZEf2uP/hg7MdnUAkU22/9pcH3jDuaSKpCTRW+N+5BTgSQveJWO6GCktZ61y5LeXK0qX9gcRqTfl3dbir6pM213uQSU202HafAGgjruo1g/NZZsCrYYHAPmx1Ba9F3DZAQG+ozKQxwQPW7o6O6ckzY68d4kCXRSsoZW04ocgunty0H0odWc4QZbckEWcg3Nemf8iKlbYOTSDJUlnhi/X1kIM9o2c08yK0cp4mQG+WTkZkb9xUbmKjZD7WcWyHT285E07PFsnWHiiQ8MBnYakUrgj X-Bogosity: Ham, tests=bogofilter, spamicity=0.051949, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Memory slots backed by guest memory might be created with no intention of being mapped by the host. These are recognized by not having a userspace address in the memory slot. VMA checks are neither possible nor necessary for this kind of slot, so skip them. Signed-off-by: Fuad Tabba --- arch/arm64/kvm/mmu.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index c9d46ad57e52..342a9bd3848f 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -988,6 +988,10 @@ static void stage2_unmap_memslot(struct kvm *kvm, phys_addr_t size = PAGE_SIZE * memslot->npages; hva_t reg_end = hva + size; + /* Host will not map this private memory without a userspace address. */ + if (kvm_slot_can_be_private(memslot) && !hva) + return; + /* * A memory region could potentially cover multiple VMAs, and any holes * between them, so iterate over all of them to find out if we should @@ -2133,6 +2137,10 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm, hva = new->userspace_addr; reg_end = hva + (new->npages << PAGE_SHIFT); + /* Host will not map this private memory without a userspace address. */ + if ((kvm_slot_can_be_private(new)) && !hva) + return 0; + mmap_read_lock(current->mm); /* * A memory region could potentially cover multiple VMAs, and any holes From patchwork Fri Dec 13 16:48:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13907471 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 4B7E6E77180 for ; Fri, 13 Dec 2024 16:48:51 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3F0B36B00B0; Fri, 13 Dec 2024 11:48:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 39FA46B00B1; Fri, 13 Dec 2024 11:48:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1A48E6B00B2; Fri, 13 Dec 2024 11:48:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id E93686B00B0 for ; Fri, 13 Dec 2024 11:48:44 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 941F8C0488 for ; Fri, 13 Dec 2024 16:48:44 +0000 (UTC) X-FDA: 82890519474.28.63DA74A Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) by imf22.hostedemail.com (Postfix) with ESMTP id 7DC7CC000D for ; Fri, 13 Dec 2024 16:48:15 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="Wd2/PMCy"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf22.hostedemail.com: domain of 3aWVcZwUKCOYbIJJIOWWOTM.KWUTQVcf-UUSdIKS.WZO@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3aWVcZwUKCOYbIJJIOWWOTM.KWUTQVcf-UUSdIKS.WZO@flex--tabba.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734108510; a=rsa-sha256; cv=none; b=ue/UxLxE6PFXylcpNuDSQ5LhmD9RYB7xpoSpgDr8X9UT/VoVQ5jf5/lRQOyUYALtuNv5YW EsYoIuNZT2rRogqYYpiSAbt1laYe4ISczmj3J1scNerfdH3kji9PGQyxepKjhtbtM/AEQz FjBLvgwGHXvrSxNAt8EuzkWa+piz97c= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="Wd2/PMCy"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf22.hostedemail.com: domain of 3aWVcZwUKCOYbIJJIOWWOTM.KWUTQVcf-UUSdIKS.WZO@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3aWVcZwUKCOYbIJJIOWWOTM.KWUTQVcf-UUSdIKS.WZO@flex--tabba.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734108510; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=3eqvqwg5DMqF/8QH18XL4R6P7ndrk18+/AZZCHc/Ifs=; b=EYrPpfi0Ox/6pY0j0ZUnI3dlbo+CSpoLMW5f5APalSOa1UEYqfVtyL0In8Dasr0sVvTWF3 QEDIzQADB8yOxDbDPFdlqqQPbQ1ZVOVXMx9hp5mhLX5dESzMmith+LWoiUkCWGHuHZg+JL TaUll0Hhi/z5/DaehvhNoK64sbDORaY= Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-43628594d34so4597545e9.2 for ; Fri, 13 Dec 2024 08:48:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734108521; x=1734713321; darn=kvack.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=3eqvqwg5DMqF/8QH18XL4R6P7ndrk18+/AZZCHc/Ifs=; b=Wd2/PMCyIwvUD1OmByg+RjPWQLk46sY4J2Oe4aGUGZkMNY38815kq87WHcmj3xdoGK thTQRNM0xdbUmLRINnsbDrygmI5LNCMY0k/3D+8AzfPSAvd0RzwhVWsJfXVq0XZ0yYt4 eXgelYYpBV7Al6LFEnF5gOEjI9h7jmCcdyqxYakSmecWWvo2MqbI6CpFf21rUwNge6o0 QtB3jUptb/T6lTUBq6i1iPZZ8+W02gwT6xMlzHIC3+PC5YNP3LtkDEg1mAPeY/lQo72J RMoJ3o7YBrVGngdP6aAg/ZPBmufJTh8bRBPHHj8sPyqh3p7ccXS3cF9W7B/ONZHfWqxp URaw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734108521; x=1734713321; 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=3eqvqwg5DMqF/8QH18XL4R6P7ndrk18+/AZZCHc/Ifs=; b=u8Ut39Xw+VLZ6oKvopROPJDbhb6ynlNuflpDl+8IQ6e+U3wcyRR1k0rwFI8EMYL5Bf +nAkWrIjkiOFfQrX2vrGVkFzphsvQmIKserfHMyTzfDxavmE9rCuymftegsD7rZQCoPg SJvRdYPvCt/dEZKPNiMceh7o1DlA4GL3z3aPTbWHML2+ZtfZDBmXTBibg2k2H0Fcbhu4 rsfplBfyqP84UA1FdaXI4SPmKoki8zgPJ2VYi8TUYIPV7LFB9rf0SO+XxwC7h0mzZWdS dBxfsmDeOacfWsEa10wHTyoeA6RfGrfsOgpPTU3jHrANfI8yizBgzgW6n2ilCzaY5zd8 zDyg== X-Forwarded-Encrypted: i=1; AJvYcCWU3t+CQFhGirTuhGKaow7Kbz5cinobDMnbYItyTbEkBy6eP6m1uGYy1iT7tOkxQR6grpOVvKhApw==@kvack.org X-Gm-Message-State: AOJu0Yzi4qiWFtOdWP7/1syCSOMcSiOZEmem5RYRt2pzEUbA3ApcKuk+ zret6X77qi90f4hxG0/dMK9p7AvymJdoKcjdSvPJ9U38Tl4G6mwfW+ol+TTN1hNZ6BL/NDQMCw= = X-Google-Smtp-Source: AGHT+IGK0ZDMNsH9IoiRdhl2fIEsYjGp9BzosmVVqKEIzH/3TIloY+9G+GKQNVEw6jbsarqoTxBCMQDUSA== X-Received: from wmlf18.prod.google.com ([2002:a7b:c8d2:0:b0:434:9da4:2fa5]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:3496:b0:434:a07d:b709 with SMTP id 5b1f17b1804b1-4362aab4896mr28738005e9.29.1734108521406; Fri, 13 Dec 2024 08:48:41 -0800 (PST) Date: Fri, 13 Dec 2024 16:48:09 +0000 In-Reply-To: <20241213164811.2006197-1-tabba@google.com> Mime-Version: 1.0 References: <20241213164811.2006197-1-tabba@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241213164811.2006197-14-tabba@google.com> Subject: [RFC PATCH v4 13/14] KVM: arm64: Handle guest_memfd()-backed guest page faults From: Fuad Tabba To: kvm@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-mm@kvack.org Cc: pbonzini@redhat.com, chenhuacai@kernel.org, mpe@ellerman.id.au, anup@brainfault.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, seanjc@google.com, viro@zeniv.linux.org.uk, brauner@kernel.org, willy@infradead.org, akpm@linux-foundation.org, xiaoyao.li@intel.com, yilun.xu@intel.com, chao.p.peng@linux.intel.com, jarkko@kernel.org, amoorthy@google.com, dmatlack@google.com, yu.c.zhang@linux.intel.com, isaku.yamahata@intel.com, mic@digikod.net, vbabka@suse.cz, vannapurve@google.com, ackerleytng@google.com, mail@maciej.szmigiero.name, david@redhat.com, michael.roth@amd.com, wei.w.wang@intel.com, liam.merwick@oracle.com, isaku.yamahata@gmail.com, kirill.shutemov@linux.intel.com, suzuki.poulose@arm.com, steven.price@arm.com, quic_eberman@quicinc.com, quic_mnalajal@quicinc.com, quic_tsoni@quicinc.com, quic_svaddagi@quicinc.com, quic_cvanscha@quicinc.com, quic_pderrin@quicinc.com, quic_pheragu@quicinc.com, catalin.marinas@arm.com, james.morse@arm.com, yuzenghui@huawei.com, oliver.upton@linux.dev, maz@kernel.org, will@kernel.org, qperret@google.com, keirf@google.com, roypat@amazon.co.uk, shuah@kernel.org, hch@infradead.org, jgg@nvidia.com, rientjes@google.com, jhubbard@nvidia.com, fvdl@google.com, hughd@google.com, jthoughton@google.com, tabba@google.com X-Rspam-User: X-Rspamd-Server: rspam03 X-Rspamd-Queue-Id: 7DC7CC000D X-Stat-Signature: 4chfpy3tgefjhias419ywy8e5z1moecw X-HE-Tag: 1734108495-625405 X-HE-Meta: U2FsdGVkX1+IcvHhN/RlvpXbmcK2bDkzGQKa84AxdxtgeAeZA0H6ilr1d97c2aKAexM3ZRj/1JLP61EDFsQNxYY+LEiqX0YerYi1PdoEq/+Qx0fGzhxxsRblN0X+BqhSCiKoYv5TbwXyVmvdLsnL/o/aGqV3JDZt1k5SR3m+QUF+LChichkUl/SCfhfFMib+58YIzYTXxz0Yz5dec1WlS8QnYf2pPpOLlmIq5v7OFdNUqsSFcaMgsku8juwvAeIZdcLLhKBhkZQr4F0NE+O4hosNaS/YZR0OvJmhgYYmN6DsMqtn1zhTrq88Wx7tOY/CLwGODb8D0QH40jvCZHkOwG66CZSsURk+7jKcVMWpFMhK8/C+Bb16FxseM9E0qkdXy5CSACex8sv4s0E7v9aPoie1N7qToFc1XXOI8UlbNK5BjGe3sg34rnmdJxzoOY160nEwA1gBZX20cE1iIBCcBTrnxoJUMlwNI9J6ibYVruo801YVZn6w1N2KR8HU3VUCDk3jU5i0WLsfgvR1PeO7+PYv1NyXL50TJ8+xu2sg/Cot7T3sHYiuxOlyhoJrItvNNWsBRaVgzJN6sPi14nduNSI2N+Tzdom6CBzQXnyrmnrvD8LVfKktZvn8qDDQJ5KHMVLakUr7/9sYsfkHb1J3UqHki1Dp6PzQqUnYrEB6tF6zPEFcUiIcehQwx6U+vG2rpYwLVcU2qbdy4pHt8jIJy/BwPv9C2i28rmZkcFby86NNpGNc+nIvJqjyF1k09w41i0VALtCnod+gmeeWbhrDJgcnZdL9mGC5FrvNamxYLFHW+CnhI86laW4cfnzv0M7GEqOlbAL8dBCkr5+YLgG6KJLSoc16MFobGeWRxHw3ZIM+gFVi54L6rE+tDjm/RyCb3aQNQPLNYr754iNWIFGvxl1LLA7GROaRUz84j4yJQNVMwdIukvzj6IVIqu4d57+YyUDRwcEyS6REUEJfJEn QxOItWKs KulxivjGifTyXiK/LzEuQ3i3mxQuf+ByYDE61BlpurXDCVdS/qaJvh+9KR59umKTpnFpF/gU9bJ+kKuHlgxr4upok2CAnAjgWwucbIcJEZjdk65FaXSxO+djK8xYyA0g1bInhDmmHYc4io5FCOmvR5LTY6KVjMjDNKqPgHGVJdb8h7PKxRYpSwcmMhu9zCKwgUDvvI8wCprdhdFgVJgvb4MmN3HccwaYvhuQMr7kjTYne3R4X/x+T/MtoHR+Owfv4HBa8zWJRhaKbqG1rcX0zXdZm5y841v/V+LYhyE20l2v170KK4gz9qBMTwccRyr+5Cx6ylDDuYkBN2jx/LkYDAcP6khMHyKbFF89vLYkFwo11mQ4/4G8DB17ZfonOcH1SFmQjL2CLA/507ErrvQ9mqQrtUVJf+cqjo6FPhH0B1SjI8o+L+13PE5WdnEI6a5ZHehRXUVx45TUN4Te0cRl3khcql/wI7eS6O8KZAWlVq0d8lD3xk4Fy8nSirH2hEisxeHpEIL2fqtkhNV2lmUvu+evErF5LoumsCOrZg4NjzSz2nxS4xRKWMeHiDaUCkvjqHiWLWkyq8RD0zrv9pcAjswQiqhG/EjXI1msrtFIrAUm6pkoVSHzy+G9RIRmJFXczsAe77lzBjaapdYV6/XgToqqBhg== X-Bogosity: Ham, 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: Add arm64 support for resolving guest page faults on guest_memfd() backed memslots. This support is not contingent on pKVM, or other confidential computing support, and works in both VHE and nVHE modes. Without confidential computing, this support is useful for testing and debugging. In the future, it might also be useful should a user want to use guest_memfd() for all code, whether it's for a protected guest or not. For now, the fault granule is restricted to PAGE_SIZE. Signed-off-by: Fuad Tabba --- arch/arm64/kvm/mmu.c | 111 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 109 insertions(+), 2 deletions(-) diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 342a9bd3848f..1c4b3871967c 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1434,6 +1434,107 @@ static bool kvm_vma_mte_allowed(struct vm_area_struct *vma) return vma->vm_flags & VM_MTE_ALLOWED; } +static int guest_memfd_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, + struct kvm_memory_slot *memslot, bool fault_is_perm) +{ + struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache; + bool exec_fault = kvm_vcpu_trap_is_exec_fault(vcpu); + bool logging_active = memslot_is_logging(memslot); + struct kvm_pgtable *pgt = vcpu->arch.hw_mmu->pgt; + enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_R; + bool write_fault = kvm_is_write_fault(vcpu); + struct mm_struct *mm = current->mm; + gfn_t gfn = gpa_to_gfn(fault_ipa); + struct kvm *kvm = vcpu->kvm; + struct page *page; + kvm_pfn_t pfn; + int ret; + + /* For now, guest_memfd() only supports PAGE_SIZE granules. */ + if (WARN_ON_ONCE(fault_is_perm && + kvm_vcpu_trap_get_perm_fault_granule(vcpu) != PAGE_SIZE)) { + return -EFAULT; + } + + VM_BUG_ON(write_fault && exec_fault); + + if (fault_is_perm && !write_fault && !exec_fault) { + kvm_err("Unexpected L2 read permission error\n"); + return -EFAULT; + } + + /* + * Permission faults just need to update the existing leaf entry, + * and so normally don't require allocations from the memcache. The + * only exception to this is when dirty logging is enabled at runtime + * and a write fault needs to collapse a block entry into a table. + */ + if (!fault_is_perm || (logging_active && write_fault)) { + ret = kvm_mmu_topup_memory_cache(memcache, + kvm_mmu_cache_min_pages(vcpu->arch.hw_mmu)); + if (ret) + return ret; + } + + /* + * Holds the folio lock until mapped in the guest and its refcount is + * stable, to avoid races with paths that check if the folio is mapped + * by the host. + */ + ret = kvm_gmem_get_pfn_locked(kvm, memslot, gfn, &pfn, &page, NULL); + if (ret) + return ret; + + if (!kvm_slot_gmem_is_guest_mappable(memslot, gfn)) { + ret = -EAGAIN; + goto unlock_page; + } + + /* + * Once it's faulted in, a guest_memfd() page will stay in memory. + * Therefore, count it as locked. + */ + if (!fault_is_perm) { + ret = account_locked_vm(mm, 1, true); + if (ret) + goto unlock_page; + } + + read_lock(&kvm->mmu_lock); + if (write_fault) + prot |= KVM_PGTABLE_PROT_W; + + if (exec_fault) + prot |= KVM_PGTABLE_PROT_X; + + if (cpus_have_final_cap(ARM64_HAS_CACHE_DIC)) + prot |= KVM_PGTABLE_PROT_X; + + /* + * Under the premise of getting a FSC_PERM fault, we just need to relax + * permissions. + */ + if (fault_is_perm) + ret = kvm_pgtable_stage2_relax_perms(pgt, fault_ipa, prot); + else + ret = kvm_pgtable_stage2_map(pgt, fault_ipa, PAGE_SIZE, + __pfn_to_phys(pfn), prot, + memcache, + KVM_PGTABLE_WALK_HANDLE_FAULT | + KVM_PGTABLE_WALK_SHARED); + + kvm_release_faultin_page(kvm, page, !!ret, write_fault); + read_unlock(&kvm->mmu_lock); + + if (ret && !fault_is_perm) + account_locked_vm(mm, 1, false); +unlock_page: + unlock_page(page); + put_page(page); + + return ret != -EAGAIN ? ret : 0; +} + static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, struct kvm_s2_trans *nested, struct kvm_memory_slot *memslot, unsigned long hva, @@ -1900,8 +2001,14 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) goto out_unlock; } - ret = user_mem_abort(vcpu, fault_ipa, nested, memslot, hva, - esr_fsc_is_permission_fault(esr)); + if (kvm_slot_can_be_private(memslot)) { + ret = guest_memfd_abort(vcpu, fault_ipa, memslot, + esr_fsc_is_permission_fault(esr)); + } else { + ret = user_mem_abort(vcpu, fault_ipa, nested, memslot, hva, + esr_fsc_is_permission_fault(esr)); + } + if (ret == 0) ret = 1; out: From patchwork Fri Dec 13 16:48:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13907472 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 8DFDCE7717F for ; Fri, 13 Dec 2024 16:48:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7E7E86B00B1; Fri, 13 Dec 2024 11:48:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7218E6B00B2; Fri, 13 Dec 2024 11:48:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5036B6B00B3; Fri, 13 Dec 2024 11:48:47 -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 29F036B00B1 for ; Fri, 13 Dec 2024 11:48:47 -0500 (EST) Received: from smtpin30.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id DCE321C7F09 for ; Fri, 13 Dec 2024 16:48:46 +0000 (UTC) X-FDA: 82890519054.30.3906358 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) by imf06.hostedemail.com (Postfix) with ESMTP id E57E5180017 for ; Fri, 13 Dec 2024 16:48:26 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Vj4DhF1C; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf06.hostedemail.com: domain of 3a2VcZwUKCOgdKLLKQYYQVO.MYWVSXeh-WWUfKMU.YbQ@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3a2VcZwUKCOgdKLLKQYYQVO.MYWVSXeh-WWUfKMU.YbQ@flex--tabba.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734108513; 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: in-reply-to:in-reply-to:references:references:dkim-signature; bh=QFDTUdjhMWOhr77i5TfIxa/V7OmOhu1GE3Wb7is6IxQ=; b=FokPszBH7g+mWc5+69J6dwPmFCi+A7S0MReGSIkPx5wuuKQEq3w0l37mu9Ee9diCv4BZJh TA5/ZykwtCdi91uBeKPr/e94ExIwh7bGnvG8QJmZJzhIRpzQ/4xm3H5rED6o3igWzJapU3 yarSyQIMaWNx4ptvGLhP8qQ/WHwXzRc= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734108513; a=rsa-sha256; cv=none; b=MHaV8BFIVjk68fhweQYuv08TVh/EN6HwfXsCWrgXSsJIxAoUVCU126+Vl2EswQo91CSX5N yZhhi81o1YqLiEjsqIHLzgx0muwYTTmz68bj9KIdqouxT/p8Fh+JwFR5tgeVEqUW0l0gX9 FJtSHILm7MzEIJm/gYzp45CeGYZkewo= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Vj4DhF1C; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf06.hostedemail.com: domain of 3a2VcZwUKCOgdKLLKQYYQVO.MYWVSXeh-WWUfKMU.YbQ@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3a2VcZwUKCOgdKLLKQYYQVO.MYWVSXeh-WWUfKMU.YbQ@flex--tabba.bounces.google.com Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-4361c040ba8so4599425e9.1 for ; Fri, 13 Dec 2024 08:48:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734108524; x=1734713324; darn=kvack.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=QFDTUdjhMWOhr77i5TfIxa/V7OmOhu1GE3Wb7is6IxQ=; b=Vj4DhF1COkwznv7Bqf55iDoXDBvDfJfO9DLBUlK97Yu2MIcQOb2wrLtoHrmSfM9LT/ 22vrfZGrW+zuOan8h3IN2XcdXu9FhnbaqwccDEkyYHQXHqSVRJVY9lCr7Im9ijwJa9qs Egk3iI2gttmRcbXLX82VtJuN1tMcyGyzeR6petyjlj8WxVAk6Fffi/Xs1ZXUSETe7mi0 UhrWFuEVe06EWWHctFkiOGmT1GYx8TKrm1dKAd89Lx3G0ruPNjHVCWvEsfcCl5bS2QNR f473XCAaxG5jvU3zC1AhaIBduyfr3+XZUTQg9e8BuFXkBV3fMlWzooxibYCGLG35YZX3 v0Jw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734108524; x=1734713324; 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=QFDTUdjhMWOhr77i5TfIxa/V7OmOhu1GE3Wb7is6IxQ=; b=L2rl4Q1UtReozR+Na51GWd3RHxetUlgYzVK/R//4cM7f3gQyY+vVB7r4wjw2lZvSCJ Pei8XWwI+nO+PXSUcuKj/B6mpDTXpxOeNkP/+URYtrHlGHVm+BcLWVOluSyFwNtUY9jU HfaAgmc6DFJK/cKYfrrxdq8v5w4B5ZOQpRHCf93Y0l2m4LECBUsAiaEr4XiWkvzI6JEd rT85WOpXKgyoucqNnFv9OQffvr9U6n1OgolPXlBziJGMmJl+uJO8/FetSu3I60zWnHiM mZMyeoioLVkWs0Q+Iw4txzYxC76IV8bDahm1gtaaTiV2OQvdiNhmErdVlM5aaVe4C/D1 84ww== X-Forwarded-Encrypted: i=1; AJvYcCX1UN/XxkUct/db4uKiKex3AODCchH5+6HLk+bK7wHyZ7/ZbQVxWwyX1yB7hF7H3OxJeL5NnLgJ1g==@kvack.org X-Gm-Message-State: AOJu0YzDGNajhNVx9jnJa+DXHhvrV/Mx9kkFbRMY/9vZIhzbrHGKXMxP Uz19tMGeuXY+QM3pPfV+E08EAg86/JNu+zjGYDg8Xs5YlCFBMggMHowMLehTqnDIfiATVL3VtA= = X-Google-Smtp-Source: AGHT+IEdZjijVCjCEZr3PkrGBsAlFaK1miBXntQYNoOTRqQcvW0yt5qcatWxqC8z3zg7zC5MHMXcUa8eHQ== X-Received: from wmbd13.prod.google.com ([2002:a05:600c:58cd:b0:434:9dec:7cc5]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:717:b0:385:ebea:969d with SMTP id ffacd0b85a97d-38880ad91f1mr2174346f8f.22.1734108523614; Fri, 13 Dec 2024 08:48:43 -0800 (PST) Date: Fri, 13 Dec 2024 16:48:10 +0000 In-Reply-To: <20241213164811.2006197-1-tabba@google.com> Mime-Version: 1.0 References: <20241213164811.2006197-1-tabba@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241213164811.2006197-15-tabba@google.com> Subject: [RFC PATCH v4 14/14] KVM: arm64: Enable guest_memfd private memory when pKVM is enabled From: Fuad Tabba To: kvm@vger.kernel.org, linux-arm-msm@vger.kernel.org, linux-mm@kvack.org Cc: pbonzini@redhat.com, chenhuacai@kernel.org, mpe@ellerman.id.au, anup@brainfault.org, paul.walmsley@sifive.com, palmer@dabbelt.com, aou@eecs.berkeley.edu, seanjc@google.com, viro@zeniv.linux.org.uk, brauner@kernel.org, willy@infradead.org, akpm@linux-foundation.org, xiaoyao.li@intel.com, yilun.xu@intel.com, chao.p.peng@linux.intel.com, jarkko@kernel.org, amoorthy@google.com, dmatlack@google.com, yu.c.zhang@linux.intel.com, isaku.yamahata@intel.com, mic@digikod.net, vbabka@suse.cz, vannapurve@google.com, ackerleytng@google.com, mail@maciej.szmigiero.name, david@redhat.com, michael.roth@amd.com, wei.w.wang@intel.com, liam.merwick@oracle.com, isaku.yamahata@gmail.com, kirill.shutemov@linux.intel.com, suzuki.poulose@arm.com, steven.price@arm.com, quic_eberman@quicinc.com, quic_mnalajal@quicinc.com, quic_tsoni@quicinc.com, quic_svaddagi@quicinc.com, quic_cvanscha@quicinc.com, quic_pderrin@quicinc.com, quic_pheragu@quicinc.com, catalin.marinas@arm.com, james.morse@arm.com, yuzenghui@huawei.com, oliver.upton@linux.dev, maz@kernel.org, will@kernel.org, qperret@google.com, keirf@google.com, roypat@amazon.co.uk, shuah@kernel.org, hch@infradead.org, jgg@nvidia.com, rientjes@google.com, jhubbard@nvidia.com, fvdl@google.com, hughd@google.com, jthoughton@google.com, tabba@google.com X-Stat-Signature: jaqu6qkn6ahsqykd3eh49kkwaffc4m8h X-Rspamd-Queue-Id: E57E5180017 X-Rspam-User: X-Rspamd-Server: rspam01 X-HE-Tag: 1734108506-961556 X-HE-Meta: U2FsdGVkX18eJmrY2UJ9/WUIinH+cXks+BgOKtRp5PP3NRirlJun8iKxaLRYl357m/iMmtK3p/6S+fAP0KvE4vJnCc+TTsbviiNCz4jp9TQ/sPx+LzKYc07c2IYA52Ud3zpciZiyQe5kgj8H/L2Psz+GZlIhbx8v5XjVXbzPcJkhcLCiRe8Rzd9vIFJYPkxp2O9EOimpgS6BLhqA2OikGCvCa27UXbsfqLh6jxrKRtn9tnQLj3CrzsTEr7XHrBybIHrsNdHaht6jeICSybS7pJn43zPbj79moNcKuFt2+Xnxo/6SRdkp6yTmL6OgU2cKwEiEjZAlA2hSrJl+Km37ksfmjwDy/h69y/id9x2EALjze8X704NdOcGHxR/hseY2+RmAbWfid4CQqDcB5uB2QQYlI4YWBCeE2Q4QdgAmeS64HMcoC3PQ5eP2eHUfCY6X0rCrywrabtHZPCHLp+sMpcO5/w9lMhP5FEVYpEasztpUixCgo8ttkgt4cNrd+WdC8b7N2HmpAunorLOSlhCIp4bAoGOQynQcN4Tt/oOWtvbuFFfldHbvuSLCDsH2luvOcKX3giQmZiNx9VSu8MtVWgUvXl7W5FFiQXs255Zg8oJohQwyJso+JOsaGyD0V5XtjyXjVdiVP3REiEKMMXFyS+RycgjbCB9WAsZXAf/5T9W21rIuGMN8e3hKAXtVT/x9mGwW1a9LkuIPTeGMPpWL0vcj8KYAdo3ZVYN+O4KczDctyyhSUMxuGKOaFKAz08hgiQrcWhX3HYd/TIx7EyXTe9nG7SxDx/9+l0a/e0tcPo6/o1dYKqxKZ9g0SSctqEpiTgwRrkZzuj1zhxgNhJcgexTGVGWB0laDpRc20+wNjj/rMEpjgjR5JdOnxa86DZSNHXjI44lnncZoT2w65vuvmL4RpsMQm09RrHPuu1QDT456ZdOPV73b3RzPbNZSiQwJ0pk1YsgnB4ZFtLyb8NR 2pN0+Y8J 7ANdSjpJD+dFFkGy2/9eM2u/h0m45fzb4EeZnoDsV5djQS9O5dVP48Gudd9/fIBCr/ISVQHAaY931XMBVC659kqAAIl6fv0WGtJ9SPuOk05+XNTOokuWhsWf6AA4gP2fxjZs0pVYykeTJt932SnODGPXcLWcOl3ShOjrIdc4azJTIcBNlCD8srbOhkOZszU+POAPd5SJEJ4Agy+Nw9dvadZR9vgOQsoFiLrxnRCixygR3fAPd4/s2Oeu8HZQ3jEgJpKgllRGGBYPiaGL+Hnh80kd9m2lW1VV1RiQpDx4cmEHK9GLnaPK30IvV/3J2+A1jiCSd50V9scnjuURPRhDm4runbRoagzptur8jn/02HMfNqiKC8stayGt8VmjoMypHTvjBVuA/xXRVHaTFmUdFVHltlhSyFwQqCBjIRJ/FujFN3pow3RuR42q6+ZAG2kWLdIWgehQpeoQ0niYHdcO2i04f48zCkb95slT30G34e5uPSl0H61dPYk2a7l+bKi129zeviQa35n/NGRgS54sMKmC9ssXnt8WF6VOZjQvZjfEONpaSq0Ai2S7SP5WYciUv+N4/mSQfMEd8yAXTWSolU/UrVMdzdQai/UWLVvXJ0CGcd4Yk1+a6TImJ8X6Fe84Pu1iqBDwurnh2YcTejBTize804HOnz87Lo1mr5nqS7Mg/ddJ5g57Ea2eeYZZnMaAcbeHy X-Bogosity: Ham, tests=bogofilter, spamicity=0.067067, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Implement kvm_arch_has_private_mem() in arm64 when pKVM is enabled, and make it dependent on the configuration option. Also, now that the infrastructure is in place for arm64 to support guest private memory, enable it in the arm64 kernel configuration. Signed-off-by: Fuad Tabba --- arch/arm64/include/asm/kvm_host.h | 3 +++ arch/arm64/kvm/Kconfig | 1 + 2 files changed, 4 insertions(+) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index e18e9244d17a..8dfae9183651 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -1529,4 +1529,7 @@ void kvm_set_vm_id_reg(struct kvm *kvm, u32 reg, u64 val); #define kvm_has_s1poe(k) \ (kvm_has_feat((k), ID_AA64MMFR3_EL1, S1POE, IMP)) +#define kvm_arch_has_private_mem(kvm) \ + (IS_ENABLED(CONFIG_KVM_PRIVATE_MEM) && is_protected_kvm_enabled()) + #endif /* __ARM64_KVM_HOST_H__ */ diff --git a/arch/arm64/kvm/Kconfig b/arch/arm64/kvm/Kconfig index ead632ad01b4..fe3451f244b5 100644 --- a/arch/arm64/kvm/Kconfig +++ b/arch/arm64/kvm/Kconfig @@ -38,6 +38,7 @@ menuconfig KVM select HAVE_KVM_VCPU_RUN_PID_CHANGE select SCHED_INFO select GUEST_PERF_EVENTS if PERF_EVENTS + select KVM_GMEM_MAPPABLE help Support hosting virtualized guest machines.