From patchwork Fri Jan 17 16:29:47 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13943548 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 D7CBBC02188 for ; Fri, 17 Jan 2025 16:30:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 66EAB6B008A; Fri, 17 Jan 2025 11:30:09 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 646316B008C; Fri, 17 Jan 2025 11:30:09 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4724D6B0092; Fri, 17 Jan 2025 11:30:09 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 2CBB86B008A for ; Fri, 17 Jan 2025 11:30:09 -0500 (EST) Received: from smtpin29.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id DBD71160600 for ; Fri, 17 Jan 2025 16:30:08 +0000 (UTC) X-FDA: 83017480896.29.71C6DA0 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) by imf21.hostedemail.com (Postfix) with ESMTP id E9F6A1C001B for ; Fri, 17 Jan 2025 16:30:06 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=umF23x0T; spf=pass (imf21.hostedemail.com: domain of 3jYWKZwUKCAYzghhgmuumrk.iusrot03-ssq1giq.uxm@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3jYWKZwUKCAYzghhgmuumrk.iusrot03-ssq1giq.uxm@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=1737131407; 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=MygWVLHFA/Ogghouk9TRsaDRRHDK/dBL9FKQqgrSGb0=; b=hXuoMgDrUSATHrcaGl0aHSnA19fQCWs5FlIFGvKpTMYPmuWOXWUv7Mj0DCsATPDp9XcdbM /AImDleUfnBU7Fn+4bBrtXXJf4RkxR9vGKcAKY9tR1spBD8VxMoUPqZKQc2CcM/TheX7Qb zjEL1tFpYkSjdXS+QPjJ6ZR1qxPpWuI= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=umF23x0T; spf=pass (imf21.hostedemail.com: domain of 3jYWKZwUKCAYzghhgmuumrk.iusrot03-ssq1giq.uxm@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3jYWKZwUKCAYzghhgmuumrk.iusrot03-ssq1giq.uxm@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737131407; a=rsa-sha256; cv=none; b=rqyJ3UmI9SJ82H7FvJb0DqwriTl22zmjWHvoua0Gjhxg8gqE77SXa8k3+VMVHkSrj0weXy 2TuRrErBoPpzHN1oyT1vcOP91KYHEia8+wMzx7YlMx00XwgpJ5T+N9PORvz8Ht1FuiWkSk I+PRV813mMb/79lHP7Od05hp7P2lgYs= Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-4361efc9d1fso16507265e9.2 for ; Fri, 17 Jan 2025 08:30:06 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737131405; x=1737736205; 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=MygWVLHFA/Ogghouk9TRsaDRRHDK/dBL9FKQqgrSGb0=; b=umF23x0Tk3a85+gQtmzN+3aFnpW3JcQ3N6Zd8d/BLWdBNMDIdoXp8QmlQzBixVUODC HcLtbxN5Ns02nNcRqYXTGsiVSt0cuqNr/nPtjtVEJjq/AgYYVA2/7uNjSNn7RFLv6K1J KAvdNMxseKnRh2+4gvhe32C4Wat1Y3hz8fG8bmt1jkfwc11bu9nwoG7fqK01JgnS1sVl 8DDuMgb5pCgc5Yo4lXfnPMpgBTwa5VyCLAaUnuIYiknMJi0Rn7cVWSVttAcZW8rqU6NU b4dhyuQxYZnbjuYaakBXhzLUWabuoiVbsSS4ZPgsS/2zFMTWyCUyJ0GjefcKFVlYtIya HD5g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737131405; x=1737736205; 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=MygWVLHFA/Ogghouk9TRsaDRRHDK/dBL9FKQqgrSGb0=; b=kGClyYNPuu1RCpQqZDquJJWXYdGeWffHeW8dixhqhwcGXRyOu+wI3DNZYyXeZB+GLM mbA0kwrBPedi0jq3BQrNruJp9z2wzicTJ9S9fpolDSLssKxqqyqm9SNLCSDh/SrLaQmr aGqXOCdqVIoMpgtSDJL2iHhWpIYthzhZdw8aVqjPQAAf1JNFWq85bfzVqo3QLaUB4Q4s 0vj0WMnd59vZE5OuBmgqI+CDtj0/DxepoJcG6IQB4FhMs6JW582L+n6zv0p9BqbicMdq mAnC7wK9K7VV14P9XkufsjJc9fmaNewy+9iQBhhCSdYXLtwDYLA878fATTTPCUM/UgqS 3ZOw== X-Forwarded-Encrypted: i=1; AJvYcCWtJfn9/F7wCm/BpIXSic4svhozxuGpDsVBXO+3PX3vx2eIRPTF4P7xuSxHh9IK3ai7hBT6D1jE3Q==@kvack.org X-Gm-Message-State: AOJu0YyGDxflAWKnMgEkM/DJ9y8LcOsrpxye4C88YG30d/zLw9wDZwjN E1JEPz6j9jR8Xk/fS79wmaEAHCVxUnQ37LMbavjE5NyWmDnx5Cg2YOwkM3PqlUkHcyiu5UlIzg= = X-Google-Smtp-Source: AGHT+IEHuE0O1Pmu3qZGQgThvn78WhmP89neBbw/jiEtDzguRVKTsO5hOjuvNpTFKXWiuoSlKkap9d2uOA== X-Received: from wmsp9.prod.google.com ([2002:a05:600c:1d89:b0:434:9da4:2fa5]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:524c:b0:432:7c08:d0ff with SMTP id 5b1f17b1804b1-4389143768fmr35144945e9.23.1737131405542; Fri, 17 Jan 2025 08:30:05 -0800 (PST) Date: Fri, 17 Jan 2025 16:29:47 +0000 In-Reply-To: <20250117163001.2326672-1-tabba@google.com> Mime-Version: 1.0 References: <20250117163001.2326672-1-tabba@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <20250117163001.2326672-2-tabba@google.com> Subject: [RFC PATCH v5 01/15] 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-Queue-Id: E9F6A1C001B X-Stat-Signature: 5ccdb4ijisb5scj6n7hbbwtzxbo7ay3n X-Rspamd-Server: rspam08 X-Rspam-User: X-HE-Tag: 1737131406-26934 X-HE-Meta: U2FsdGVkX19Sj8nmEH/UbRoK60kmGWtrUqmAGsbk7DYNtp8IW+3oWiy42Sa3KmpmoLrA+Hv5zvP0IRWTnZiVg0BGzsWG5PJIPbl1Xd5HczEOyxhaUVwsm5V7AJMSgF+Mdo2Vocj9o8lUg3yInkd5Zy+80aAQp90wcyy+AD0QHlkwuhtFk01KEzVm0IvFr78IlHQZhXQPN3XJdhI9K4nD9Tlkm/yuJdRs/L8IZZNz1Xnsg8Euj38WYNVruNiynt0TZjnPBqvcCUppwmlQldyBzaEbHzrJP523hJGO72u1B4GL8dRb0Hv3KrfIEfGquB9HN87bN/POXZboSN+3mklM6Dih0ewitntlZnqo8k2LjPeGatw2U1quWeJ2KUH/KaFHorQd3h06xolOghEUq3c6i1pov/Ro66tL8W2+tFh0wAVOM1G4bkgjRU9tOFgtgoNyVuyI4sA95oBU/Ai53UmUKHJ7U3Du2pSNJ2xpy082cU5vJKrUN3up6B3Hwa/d4Q7L3VUbboer6y6rnTeDCGqFDbJqjKBMOuma8tTb6ORNc0U0aOD2FwBmPejoY3vdowJ2zdbucEbGs4dOnCxIxy1xL0+7+UMl6l00L/D+qsxWjLLWBSx3IotTebywmbbB0plW4klKd3al6Ev2wape9uytRlRlYgIVu1fR4eyplEr8WjpW3oSX4UunCDm7u8CEPWZmW+cy1FjXI7yIOLXPzd0ahIF6wL+xDzXdTpVSwaZRCbgEprQOpoz8/wlhFdhZvyEVGhJ26Yod4Yxq4uRMtGYvEOdZBfml+01M80Hnzsv2/bYCcfo6zVM0SaQUJseOYSqdgIpZi7SCt8fzvfJ7QmOikZWNoD8r4mi0Q++y7QmiZDiQ96l3U8ZQH2ZjwMIfp9wEy0Aqv1on+aKH/ret6d3TXi0G3Y/8sCUJdoJoywScyGzXqWu5JS7/HuPdz1eJ0bqSH/YviUs6v3iywbL+WXm 5jqXKF8S YwluCYPRAZnVmT6RODK3wLeW1gl3xEnhfJxLp8mAoFUQMx/Zm/K1vWS9ORQOmWG1yVWTS2e6tR6tKP5O/C1n6k0HTyb6Sijz/T4jkbUAfMkJr04pdJA8qp0tdP4TbGBN4Ar67Bt+obihv2M8Zt8RmK0vuagp0Y1Dk9uMWKrZmgoi39w3WCHuqbOZzHEo/rc9xChBz7YE6LVu7VeThW8cItV/lUt+1Z9OoP30GZ9mRXWiAYW51dCA2LUnKlnP8bY1X1X4dUKQmjzglZwFcjXaD9U0qxrGMiYxdOZ6aQxjVUp7vn29g7fvitotj9WmwVzRZu4Z+GHf3cVJjr4lkfLMRnYmwGSO8E1LB2mjYyvyoxTQmYCc7lpXuzNnjWrwNEZv9wsRyOfU51IAkwUDfXd4zBHujJq9IR2UGZegg9NeZRu8t/hjUANQo/END902abDW50Ex4CiqEvXo2tUEwKKfD8EaUTkEAHXsQW7obXmbU+gKdTMLs+XAASPBke3RrSlBkB0ZDOgL6O5E2oJX5e6HRtnjpPQ7Pk/ebrJpYnyJdTJ+uKqwuaEiSvDcH005YKTH5uvh93SpDFTIDaaTIVdfltfXhk5LGxLwp/9EcgzEVM7t91W3dzv73ksLQK41gvXNpH8hbV89PMnattyQXQnJJtvk3MX0UCI2Qul7p9bVBQQXPnCs= 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: 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 691506bdf2c5..6615f2f59144 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -962,6 +962,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 Jan 17 16:29:48 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13943549 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 9BB26C02185 for ; Fri, 17 Jan 2025 16:30:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 313366B007B; Fri, 17 Jan 2025 11:30:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2EBB86B0092; Fri, 17 Jan 2025 11:30:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 165296B0082; Fri, 17 Jan 2025 11:30:12 -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 E169D6B0092 for ; Fri, 17 Jan 2025 11:30:11 -0500 (EST) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 733CCAF006 for ; Fri, 17 Jan 2025 16:30:11 +0000 (UTC) X-FDA: 83017481022.23.A03128B Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) by imf19.hostedemail.com (Postfix) with ESMTP id 915C41A000D for ; Fri, 17 Jan 2025 16:30:09 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=BVjivHJE; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf19.hostedemail.com: domain of 3kIWKZwUKCAk2jkkjpxxpun.lxvurw36-vvt4jlt.x0p@flex--tabba.bounces.google.com designates 209.85.221.73 as permitted sender) smtp.mailfrom=3kIWKZwUKCAk2jkkjpxxpun.lxvurw36-vvt4jlt.x0p@flex--tabba.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737131409; a=rsa-sha256; cv=none; b=tnLnwo5U8zmpKbjiMs6q2Ie895h7lHPqi14MBhsKcERGQ0nmXgIdZrgd5snUbqfCuZvWnv JZuZscKCA8k5umRJtpKOijZsW43/OZzuprhqQ40MkgVuGSVzKcO9WgMCqvLbzUIt8ZjQ6k qiLarx4m/eXps4qHQoTTfURaJpziZ5k= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=BVjivHJE; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf19.hostedemail.com: domain of 3kIWKZwUKCAk2jkkjpxxpun.lxvurw36-vvt4jlt.x0p@flex--tabba.bounces.google.com designates 209.85.221.73 as permitted sender) smtp.mailfrom=3kIWKZwUKCAk2jkkjpxxpun.lxvurw36-vvt4jlt.x0p@flex--tabba.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737131409; 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=9YRI2GIhhUOLx/mMfOtyHYUw3FTFP1R09oMm4E6AB0U=; b=UAPenj7vips1/FltcvBuNcMeXP0tJJikCI09okTSGh6gE92yZplXpSrLWd8NR4y+D4ocbx YNmPHKflBbgcn7Hkdsjkx6lUYd6B+cufAoo5f9gDUbWJeqVKIkDITkl1idnFlR/dmGT8el KnrdxtvZEgUGfA3zMJfRnpqR7ExqSi8= Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-385e1fd40acso1324683f8f.3 for ; Fri, 17 Jan 2025 08:30:09 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737131408; x=1737736208; 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=9YRI2GIhhUOLx/mMfOtyHYUw3FTFP1R09oMm4E6AB0U=; b=BVjivHJEsbsktigQ6QRFCp8QdyJqzB/rbL68bRixUWycvAt6zNeHbFq1iriA7BT8tA Tv3tXqDliSJ4GnuBCNfJ0EWv2lwyaWyUQaVvTL9vUGs82PfypVFBeTY5JsybfMEMXu6g uxuJX7Y6dYBdzuMcpjaNAvnv3kLvFSc5XGL3DXpxAbmQVcJST03/hn9AjaH3tYO9Ii2L kJ7DIBYfaDi9rf6MfN0z1NcgJsiMGxLsnSmxlVBd+MpI4hakm4waT+le0jw4DlktoiU/ TFx1bDryMNdfXLz2yy1p9OHHxBVVnALS8ajtJOKyWYV5HywxAxpauUplVtMAN8jLo1B3 0myg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737131408; x=1737736208; 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=9YRI2GIhhUOLx/mMfOtyHYUw3FTFP1R09oMm4E6AB0U=; b=AzL2DNCn9WvnPx+58IUFGzY9gDgQ/eFiaTjNdALLV0R8qyosGHqfTCWdZeh4e21/MJ CvxIrxbLSf8uI7MYtDBA/blwU7vBYpw4nxsbIn65t0fs83XmA3Gv85vHbMJMV9YhcEHu N45z3ua+sl+1XRItBmXZyicxFCsJ0Q+dA3Idrc4yQJINm3R+vDH85TZTAhV/lSQmFg1x /3RYrigKT34onfeSYv4B+EMPd2M4gJeNSf8FBfw2ItPLQyezHUhZ39ZMQY9S5pCGFWA5 VJiF7jSiMk32SqjAj8vhetWWmPo7aqk2887w1fpryMIm3rB+cz9Q4RD6wY2RJlkhKhQJ tteg== X-Forwarded-Encrypted: i=1; AJvYcCW5RwVaRDRq/kdtqwHpMTo670drOb79IxkZ6cFy1dv9FiLo+rIlWGFqKVD5og5yXk9Tg6FF5lkJYQ==@kvack.org X-Gm-Message-State: AOJu0Yz7RWOttQ2Ga6fEwYYdYEevVJO1f47qdkl6+OA74jesOrLpVmRa cw2HJdUSYygvAYMXnpzFpunEwvmlv40aRZY1yMuRippEsgir7PnNb7fCzDQN+mr+kY4YgOHw/Q= = X-Google-Smtp-Source: AGHT+IHbh7tAYRJxv8EI6lff/OXusDsu+aDTx9P7xBOFqoriEiJIwcFSIe8wJktyjNcSv9lIWkJSWAOMyA== X-Received: from wmbfp21.prod.google.com ([2002:a05:600c:6995:b0:434:f018:dd30]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a5d:440e:0:b0:385:faaa:9d1d with SMTP id ffacd0b85a97d-38bf58e8751mr3238769f8f.35.1737131408023; Fri, 17 Jan 2025 08:30:08 -0800 (PST) Date: Fri, 17 Jan 2025 16:29:48 +0000 In-Reply-To: <20250117163001.2326672-1-tabba@google.com> Mime-Version: 1.0 References: <20250117163001.2326672-1-tabba@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <20250117163001.2326672-3-tabba@google.com> Subject: [RFC PATCH v5 02/15] 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-Server: rspam04 X-Rspamd-Queue-Id: 915C41A000D X-Stat-Signature: r3yqtau8kgxyej5g3s1bxc3h3qymbeku X-Rspam-User: X-HE-Tag: 1737131409-112796 X-HE-Meta: U2FsdGVkX18dHnffbyECeRZlObh8PRFgN5ya9kPtKKW5lZBGJO40gj3wbaypIaDbYO9Hb8DZdzYSOLl6EeCQ9nkkfbvq7b/n56B+u3mhQ5nSlP/+aHC8beepsuYROXYhjgINm5+mUuYQ1iKo7eTqeYdnLTkQSRVFPO73YAq2nBtglOIq1jKK6y34zzkKRfWI88SM+L3bzu1MfQloBwwtfdLq61P0wjyA5BllH3AU56WFd64xleaXyp/2q4KWtIBTzV2p4wQyRTF4Ksfj3ejSmfKg2vDowxc0WY8nDiZDgAMJ/jCo8BbvoqSDoOfL6MEWPQYjfZrr44B5RWC252NK+QbqdoocpoQ7oZ7ip9W+g7KuYUX8oXXOHJDh1pzEZ4xrW/fscEvcWfGZwB0m2p7ogAz2+oko9sEvPwwGGqNG/fK3dk46MRcjnuIgvpH0rDAJE10K9AGXrLwDr7Xs91Zbc1yAxmP+zWIOZHFCvMN4j9UAGYOrrAYgq1yKO/1mgDdLav6PXHTje2yzTwq428NgWy5IBJXaHqMIKjLgk8m90UhGyCqxFwRYDl8ZClH+tTikNiabp8dGYIRLdu6yeMn4EIK7SNhb/KJLFOM1zhkrkG0qNUxxQqTuO01ncYEUp4peCVYC8/1WBv6IQ4cCtgNCtt62xCFOKw1lj3Rl42CQEsKWEPral1rl4kfLAXm6hL2/izuQLx/4SBgXq2QJw+hkzzyIKEbsOJuiWmZ0Qmf3EmwUNTtIUHL4Xv8jeyuA8iphFcbV006FK1xSjBq17BYwunOFc1N4sIPJQnW6XWltREqYIr/Gjg/uuYDrBx54Xz/X2hg4h+AtH53tzmQBCfejL/49ZJ/ilooTXd69dQIZATHWIJUvBzEZa2gepXSgfPuKUBMLICdggmKFcD8QrtfK2+bWfza7KZWIY3A1jPMAD9e/Z4P/K8L3yWcb0ASYH/RtKMfY7Bu6T4Du2nkKdYT 3tv8OAQk 9M6PQV2vuiM1Ch3rWxILpDE8SuC5QvztLXJjEzBcAGRBGulBO+lDoT8ZRuAsv6G+LBfWCPyENwTg71ZAk5idqpYt2utZOdmuFHYSW46wn0H2iP9mnwrlvYvZjMe1uAcedddxUgMElbeTtB9rTIblcrfjzvQL4elo9QDgIUs/i/x/HDLFihzLoQKv+Kyo/U7bZcQ9sJILoXqZrlo8Yklkb+99rR4oJt8LWVUEsO4uTL9Lw42BXWigA0EYHxUyd+ILWEvRycMq7rEvJwSkS60LDicZaIFLS6CWGhOOcWLJ1nQcZEaD3pIfQcc4seH0wxNjNjZo+Wx+OOaxlxrJf+D74o/1U2BkXcdWyg80HTr9sNGlNV/k0x3PDU0EuSvqJFueJkafnaSflMXK29bi5PN4Q6kGRmIZy93vx9nMOiWMKDcOw/mSfPuQXYcIEnBFceaHY1bWjp84peh/q6lz91afKZ/1OxZipCULPfEwfjIWc3gf0mQaKSaMUaNW2zFxfmn0tLEIXfacfiEnsI96KZXh9YxOUrZpvkGYN57KXubDzio1GOkKDB2rlET6AnLlGelbJFlkCFEHwCipyebifY2CS0WTOVTXgGYtRVIi3FxDVlUf43ix0S7qNlSYFN99RR53PrQay6ZeZRagA7QiwjGEUeDBzhA== 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 Jan 17 16:29:49 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13943550 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 2A75DC02183 for ; Fri, 17 Jan 2025 16:30:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2C8206B0083; Fri, 17 Jan 2025 11:30:14 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 24E576B0092; Fri, 17 Jan 2025 11:30:14 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0C9566B0093; Fri, 17 Jan 2025 11:30:14 -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 D92F06B0092 for ; Fri, 17 Jan 2025 11:30:13 -0500 (EST) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 78FF4140592 for ; Fri, 17 Jan 2025 16:30:13 +0000 (UTC) X-FDA: 83017481106.11.7B0EB39 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf08.hostedemail.com (Postfix) with ESMTP id 910C0160005 for ; Fri, 17 Jan 2025 16:30:11 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=2c3g3IgY; spf=pass (imf08.hostedemail.com: domain of 3koWKZwUKCAs4lmmlrzzrwp.nzxwty58-xxv6lnv.z2r@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3koWKZwUKCAs4lmmlrzzrwp.nzxwty58-xxv6lnv.z2r@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=1737131411; 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=i68yYctGbzvQYlSdS0p+4WdM6JfkNAVslQef6pXRQ/Y=; b=HlmkPPvD4hi9XS6anStbD86ehmOBPenaRUEYOGBaxgf7UQrucw6yTapYyF/PrFPWeZc/ix 00pKg8qi+aN0lKls8h9bw6qVKZEVOi2i+zvJnBh4mf4jf8beGUfQfRlOZBBRQT40m2ru3H eohDXaG7KMmD3zF9FG7WijWKLz6LNQE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737131411; a=rsa-sha256; cv=none; b=dRE3t23abnmghnPXeUH744fcmFSzM8q1oxduBW3ZCqOs1iNfm4VYxn5Ah/BhJUA72M0uGi VuA38xy078zpxSkBKqJoar2/oavxePzZXY4MFc1/jPQiKH4sqm3G3i3RM5XLNHOrPdiAdn S1+PSnrotSkFIb5RP7dQK5Ozcm5C1S4= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=2c3g3IgY; spf=pass (imf08.hostedemail.com: domain of 3koWKZwUKCAs4lmmlrzzrwp.nzxwty58-xxv6lnv.z2r@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3koWKZwUKCAs4lmmlrzzrwp.nzxwty58-xxv6lnv.z2r@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4361eb83f46so16494435e9.3 for ; Fri, 17 Jan 2025 08:30:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737131410; x=1737736210; 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=i68yYctGbzvQYlSdS0p+4WdM6JfkNAVslQef6pXRQ/Y=; b=2c3g3IgYiwkkBklIom/8dGFtkHTfixXOsxZ4NN9qsS7KtaAE+l5jc2AeWJzbPLrrtH RNDhXsWiLrh+BL+VxHoE15QNdsMc3WigEw++5LZUQbCn0o2bM5pQKMz8fFn2YmglkzjM PT047Z63OQemTxWN5+MN/xKI+SQ+VPmU+beO0N/ged3A+7idRe0HTfzSSkdoLqs/IJgd Ny/XeSGpFCF5izq9f7DTG04p/GA/EhsTOD5Rf6qxU7ntV2b/zW40flsmPgjPeMaBtCiA DsNPL172Y65rYa3w908WtgBYlLRClZH9yqil1a7xG+F4s3/fRc8JkJ9MKceGjohDySF4 8Snw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737131410; x=1737736210; 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=i68yYctGbzvQYlSdS0p+4WdM6JfkNAVslQef6pXRQ/Y=; b=C8Bu1X22z7PMfh8v87iBPjstA3O7BuUiwKriQn2jnMg12w6P6GOkb1PW5VRFOt6LSe T4h6XMxueNjGRrzZrHaHtZgWpWHuZOFXX7lHX6kPw8x9KiGw5cz9sytFjNI8kccFmYo2 JiT61FWizGuMfuBLPD5bRWooDr/GJJ1BnqVSwcN/ChbN3f4mc/N3uxHvsZt5BKwna9M6 ucz1QU2o9kP0BPXB5affbhNxC5/CJuymNFyQiK/jPOcBxZIZ/ve9/uVUYnvchCbXeOoT cIRIvvWoQqTC9xy8axSrhPRLwENvecE9UsBWnH8yQtS2yG1PZzLZTuRMt3Az/8xwDGb5 YkiQ== X-Forwarded-Encrypted: i=1; AJvYcCWakPXW1NZSSGCyZpOc0rn/3A0r5OnC8Gzs5Gbdy82ADL3pcTZKjrSxyJoVClg2zj4HI7N2eySIVQ==@kvack.org X-Gm-Message-State: AOJu0YyGeUA+m5Zgw3MPpuHfZsBd2QEqkkGN1flTRPHS2u31MM7Rlxkf zRbyJwM9S/l2DL0HoIW3PcvtS3FtarOuo4mSJkC18IgPssLaAtB82SA6/wkoF2L4jVjqcA2RZA= = X-Google-Smtp-Source: AGHT+IGdZ+elMcO0tC2o/blUEAO2sesjnET5jVfx30pYB41ogtDO4dGi3tBo1ONh1MJc7o8U8tLatcsfwg== X-Received: from wmbhc13.prod.google.com ([2002:a05:600c:870d:b0:42c:bfc2:aa72]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:3b88:b0:436:488f:4d8 with SMTP id 5b1f17b1804b1-438913cdba3mr42250775e9.11.1737131410248; Fri, 17 Jan 2025 08:30:10 -0800 (PST) Date: Fri, 17 Jan 2025 16:29:49 +0000 In-Reply-To: <20250117163001.2326672-1-tabba@google.com> Mime-Version: 1.0 References: <20250117163001.2326672-1-tabba@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <20250117163001.2326672-4-tabba@google.com> Subject: [RFC PATCH v5 03/15] 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: et1eg5ptm4esoirb6qxetmt4z7ixr6ir X-Rspamd-Queue-Id: 910C0160005 X-Rspam-User: X-Rspamd-Server: rspam06 X-HE-Tag: 1737131411-713387 X-HE-Meta: U2FsdGVkX1/LXPBk4bXilolyO/I2IuoSnIMrArtW5EjlWeXIQ5Tuk7yg9PhCmvqQIlI8mMJVCupWKD00ZWxW4Ug3lfqVDqyF8bl91Enpa49L8glZfivbmUlOXXuME6+GQvcUW2mLZtNvPJYRFycVSMIqELplmKyiDcamo+nVlK7P/TBrsGJexJUAyI6QZMakqIRmXXA81tn86gRdZ72NLHshneOuwx7ak7N46CPic/kPgOV1AkcAxEbfgLpFgFGZAVcnDUYK1QXLWy4wBh0FURHJMJFTOQ/OyqR42u3Vrqi5nnwHsqSW9wyDHRkZy2VOjAyTNlrhh5pXfEO5yQ0nnSg4fwT/rAQDXKRkxA4N7thJykRG4k7X5YuhpP3akERMKp1OT2IUI7MuZJJjVGQQ+TFf9oMrAgfeb0zvhwomNJrhw+NkBnfSxnGN6RpGgRtbCF7JtaqoI9pHofv/LLmr7z1aum+umDxxLYe81nJvad5as0H3fthBVj6c3VfzK0rjeLJLxhU+yc1lCu20imUwUEp433kHC54KmVpmAt+okOWAaAwVmEQLiJs0q7ll9txxoO18dliym5vC6nikYgSeX9TaVPxiWbVfIibZomyR1LKjS0WaZ87NNvVzMoICtnUBTufos+IjfwK2WzF1eFRcdMZg5nI4r3FTLwYH04Cc2BpADS7F29Z15nt3UChF9hAn8HtyQhw+9neNnn42kwBBGmxUkVTFB+19/4zRVGIsCjFbqPoVsesBjHp6TSaQUag3M1p48l5YygPGTDPPDUieVOo41ErK3T5ktIS60FQ7dRiSnFdx/2eOkGOFTr9NnrwWFXw0PnBBceL+/9+K04PIFpSzRYN0q6DoN3te7JCeAjrB3gAHhSint+v+B7Td5LQTAAtZKF8JFrrG+y+wtRRgVPcBD5RCik9gzQ6oS5sX8T3qRKd60BPsV/zVYuamfnufLsDmmrBPl83bUhF3+GL XpHoHRzv /kDblyjyZqEojBoSwQgSJmtcAA6CUwikhc9L6Psk1aMRdB6b/bseMXAxXvgkDRM4I5pSh/5S9Cl96MayOae4DkuTdFXHUy52y2XEdDSNT9/xhpv3AhYkO3KnpFjA/ZVYdJ/yrj0gGnOUUdxmDDP5t9ggF6ijjP9B/9kV6j5/hbcxBQFZYUrVRYI2U8xapsm9yri55IxNL9tqHLbLrtM54C4HPWAVYSDTcIxeWz1GiXt8e4hIMaSIqHsjAHqfLK/zdhJGwpKYpYIwTWfHWO6lHrT8+FQGEkYLR6rmN9n6QZkh3Y2semVl8DQQdBZqseFEualdAIKvdtL7+ZViy5Kr9VspZx/vrDl59PpmXN6KK+hmkILuj5BFFz0P+hwZzRSj/tWJCn/bQecqOv8NdwMT9TIMqbJi2lJhDKV0+fpCx32IZGyMw7D0KrvjWLpbgOw/iVF3W3GHu0fkDZc9R9lJcnij7sf1fWeebXvLPykHkTkxyzpBBuYt9FCz/CKN4m5ORhCDjLk+TyvYEWayaHLdNC2nJPnAZocxuUSCHXsFj953QOxpe5dAdZduvQLp2oFYWptt7uJeW3bG0E2nXgjW4dvacp/8u2vMlTbr6irL5vxo6cA1Rs+GPDhEyxHRXOUFVGNkHbeZrVXqZEGgXjoEM+VXtaw== 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: 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 Jan 17 16:29:50 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13943551 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 E7484C02183 for ; Fri, 17 Jan 2025 16:30:17 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 53AED6B0095; Fri, 17 Jan 2025 11:30:16 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 44AB26B0098; Fri, 17 Jan 2025 11:30:16 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 29EB76B0096; Fri, 17 Jan 2025 11:30:16 -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 005026B0093 for ; Fri, 17 Jan 2025 11:30:15 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 8A2E5160587 for ; Fri, 17 Jan 2025 16:30:15 +0000 (UTC) X-FDA: 83017481190.22.70A381E Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf01.hostedemail.com (Postfix) with ESMTP id A976E4000A for ; Fri, 17 Jan 2025 16:30:13 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="tjoD/Hta"; spf=pass (imf01.hostedemail.com: domain of 3lIWKZwUKCA06noont11tyr.p1zyv07A-zzx8npx.14t@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3lIWKZwUKCA06noont11tyr.p1zyv07A-zzx8npx.14t@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=1737131413; 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=FO66kchh8bGzCA1fstTaZk+RuA83oGXAMjee9RM67m8=; b=XzG+hYIYYLXDyJtTatRGmmR2cWhAHFS3XlfhMENT/I8+JloxvtzuphjS1+qvMQeFwfwvDf /o0OTLqIdFHLnX1VXrWO63wjVCU1DnDMb6JbAMIxPjoMXWebgahFitlM3MeRadkI/F+EdF krWWUNhvOwfRg5szjpqbGpG7vF/iSdk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737131413; a=rsa-sha256; cv=none; b=Jmx5OUQ1hTYHKBfl1bgqk5TVkdTbEza55VcUNOwnVTdK3FdzsHteQYy9ppetEgi0zs3G7E Wqr6J5E+v/FI3FGa55tHvBIpeLm6BCswX0xEgfcMVrTzbCSBwIdOkgSiWS1BJKENeEkmt5 d1XaEEVEkIDhsVVtpSTrPl/y703IXKE= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b="tjoD/Hta"; spf=pass (imf01.hostedemail.com: domain of 3lIWKZwUKCA06noont11tyr.p1zyv07A-zzx8npx.14t@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3lIWKZwUKCA06noont11tyr.p1zyv07A-zzx8npx.14t@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4359206e1e4so18938175e9.2 for ; Fri, 17 Jan 2025 08:30:13 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737131412; x=1737736212; 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=FO66kchh8bGzCA1fstTaZk+RuA83oGXAMjee9RM67m8=; b=tjoD/HtaRhO/wY+zzXzHGCgqyoz0OkCPiX5Dfa6d1vlgDYXDTuuqufJByDSvOxSIlA mZlE+mm5kRFjyRL0qjcj9UfmzA58pfd7jzm5VW7McLxS+JgDYErtyOx3jrAF2cFlLVvA xsf0o1c0fIB1Jr0Lh3F5PiYWarXdMxD3Ky0axensX+Fu+HePceA1e9BbZGjFciFY57Di l5fnQRNUCAoLaVslOgZXOWJRhPk9RDRW8iNCNcwrRSsq0k0Cl2UaGfQ5i74tA3frzY1x xOvBnlYa8R3YjMkkeMH2yqiz+FAGn427HdWLh/3bTbOz4wvA4rVJttlSYwJobJqWIMLd B2wQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737131412; x=1737736212; 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=FO66kchh8bGzCA1fstTaZk+RuA83oGXAMjee9RM67m8=; b=bRNGyMBMvaXRMb4hVcrJRpVjj3vIlpfUYipWBV2ZfzZ7ebHkREamqMKkrswUhaU8tp 8eWku+UorHDZvbEC+POsQ4WFj6UyRf8tOPCFSDV4H5OGnw3LzFENdcfagyvNPai0lnXK x7SeUTZn30Vb9JeAMnKALk8fyL/F+WrnlSsb9ssy2An4fi3+IX9NWFAKLXwWpMgAfnO6 SQ6V9Ue/aF+7DV95dw9kaAv00yOKXh6CgxVa13lV4/WiUq2eEaqgCbn0EOpSzE0kAnWG av24hB6x0rbLlY+njvwPgGx847Cj3AFcP54aBQBu8uOsuXDTYGyEZZli4A7KMsTC+X8A gMyg== X-Forwarded-Encrypted: i=1; AJvYcCWiH3gZaQSJnYknax596y1MPAyVW6fRPz1qnVqpY8zm11EWK5j7pU4ncJb/Ijfm8YfgtJbwNuDQYw==@kvack.org X-Gm-Message-State: AOJu0Yw6c0AK5KXfoLxSgxCPuhKd1TDBQEtH8jgQKzzPtaoVmoVqXC6V pPL2tm1+JLPFu264lQ87rMd59ZxSN9g8eDkJqhHjLL67VDlx8lbXP3hRKSJWXUDvZ8ni4UlLTQ= = X-Google-Smtp-Source: AGHT+IFZgD2R16tEYYHXnP1+7iY9g8PAift3he39jh3cPUQ7kJHFSZqengXdwyt2ds9qO8MFRhz37dcAmQ== X-Received: from wmbfm15.prod.google.com ([2002:a05:600c:c0f:b0:434:9e7b:42c1]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:5112:b0:434:a734:d279 with SMTP id 5b1f17b1804b1-438913f27femr40187145e9.16.1737131412324; Fri, 17 Jan 2025 08:30:12 -0800 (PST) Date: Fri, 17 Jan 2025 16:29:50 +0000 In-Reply-To: <20250117163001.2326672-1-tabba@google.com> Mime-Version: 1.0 References: <20250117163001.2326672-1-tabba@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <20250117163001.2326672-5-tabba@google.com> Subject: [RFC PATCH v5 04/15] 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: A976E4000A X-Stat-Signature: worp89ji1wrdmxyiqnu8mdjnmjwb5359 X-Rspam-User: X-Rspamd-Server: rspam12 X-HE-Tag: 1737131413-181998 X-HE-Meta: U2FsdGVkX18d4Xc5YiVQAuPZ3fkyH8m0BP0u0J36v2PP5CFV6j4ufAW7ogwRF32k7UCeijz6qfkHaFqNGZlKLvLhg88Alk5Dik1AFj0aRe9LvhgH5izaOkJ8f28jcrEAYPqM0DGh53pxluT50b1IlksqVo8xoOlCHfBjaLzZa51Kn25jT0+i97/tMeit0Hkf4fUX+nrigwfa0vyJ1DklY4qXRtwuHuFAEI/qFQaxrNx5uYV7l1b5g8dE4RkUQskQJuE1WVjzczmVGF9qWsjd8rBjRYtYoEishRv9CC9bInhLgmWFM2imeHP+N6CAp05ekqRwFOJfMHF+IYOs8+kBUYTnY8N8p6qe4yEJAHW4vTRzViHY5spt3cnOg3lJcfqNoLXeuHxg1ixsaBub3P9DfvDsQSul7jjIBY0eazDdU1brJjJyva2/iXwBjNtYGXhOQZytrheCh9BKiYQsnWaKo+emHZ5KV84rDQ5h+R3uYtYHLQ1S0COwnku5nbkoxoSJvaUAvS934nM0sZR3GMdOXUMbsu7AlqMKRZzDL46jDGx/UnSuewdRCpTM/RbrkbqKIVPOuCEzA8STl1AfoYReOPqlPiZ6S5crX1iaYlG5vNWv3P+toh76ee2eH4WEP3rHnR0jg+XFuhGGFZ9A8yF3NG2mbwmdpWdfAlZViDNvNnL1auwHLJMChr3RmE1xI0ZQ8vy8gHWwuaTl7zD+iMKFLujgyJ1u+Q8vU7xT62RSKYZsIOWZykcW5qQt2wWlVdL/n6wl/Merm4UF1ATGMlWh8kYutB1CXHKQTkdVaI//4aLhStU+jaKYw0sLhrz1D2TRMJIeP93HmyU24xj+UkWdLYGY3qbEwe3HM4V2UHcGITmp8qKsn0C+tFB3kewNGg18PfQYOkz4UBVWqUSUAkEw/JRiHcwd/xs5WVMSMkU+a3nnJQpIMOnkLwprFYJ6+Y0RZEB5TAUm1DV3nV1ZB7u MpRCvPET CjKze3Rhp8Kl4FlAtQRoO7tjxoKVjPznFQwlYTK7xE20BjJw9866k5474BclrB72749MyTro4kWD7vZU3v+l6DXDZxcLwdH2rBJ0V6uGjZY/bJ7TeH58jPu1NDJaDeth93dtteF4veshsOeI7M9u+Wnxg/CLrw+crZDCfmNYVSuWFyYUjZ/ROegX13jz/63zsOJXcEQHp/I7ZEhWZux+sfcfKviODjI1JBb+UHiNOEtjGXg6V730dpyhW/xpIm7k3Ctbp05dNrWaoemIYm5TCLCuHyU7f9ETicFtMV6L9p8oun6zal0K/mVYDbpuI2A+aGPod6NiUY45+KyQOzEG00Ohl4bixFDo450UwT1vqEMz48K7/TZLYaYqt/0zLQ95VTIbMS7+b8rfYUZsFxIc/j5vKhCBMHumdEfNMQs6zy5CnJo783G4BkC4uRWvK+MvyqBu8WyDgbAdoqgu5OjDiWXQ6hPMrAl46Z8J9Kr+NqiujrD/AUJp6afm1bRT2pL4xlPlU7QoIjCYeuUzei+kqBfHT0HooFnNcFeWzyz2rt/wkhfBAjOvghPV7v6NHd1BS3JNiQ/q7wVQzmHh9S6wn/AJ/G+pMXmRjz0UK1R8vgAF8KIFAESO+a9Vhlb5+vwp7rUjjR52ckBaghtjeWLyQ99oa5mB8oRINSYe1p+uc4Y9B1abYqAvDNntmPg== 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 Jan 17 16:29:51 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13943552 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 9B943C02183 for ; Fri, 17 Jan 2025 16:30:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EFD806B0096; Fri, 17 Jan 2025 11:30:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EAC186B0098; Fri, 17 Jan 2025 11:30:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CD98F6B009A; Fri, 17 Jan 2025 11:30:18 -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 9E2066B0096 for ; Fri, 17 Jan 2025 11:30:18 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 33A4DB054B for ; Fri, 17 Jan 2025 16:30:18 +0000 (UTC) X-FDA: 83017481316.27.1926264 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf02.hostedemail.com (Postfix) with ESMTP id CAAEC8001C for ; Fri, 17 Jan 2025 16:30:15 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=IWP8tNzK; spf=pass (imf02.hostedemail.com: domain of 3loWKZwUKCA88pqqpv33v0t.r310x29C-11zAprz.36v@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3loWKZwUKCA88pqqpv33v0t.r310x29C-11zAprz.36v@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=1737131415; 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=LVZ6/wJnW1NLmDdlnZeeMFjWuJzNbaR38/28aWmB+Ok=; b=vQioO/SOTzvqQt+t3i3jlwacOaOGijiUlpXnrmj5ueYUMIWafONu+0nUlPXocd/LAarihY SnleKltg9HqfqKY3CTr+EtmsfmKwzJNsw9D6cbehFs8AcQL/63nAWcj3aRIheYv8tkVfH+ 4LtYblQxzMsz+tItjabeoj39yfzSNnw= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=IWP8tNzK; spf=pass (imf02.hostedemail.com: domain of 3loWKZwUKCA88pqqpv33v0t.r310x29C-11zAprz.36v@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3loWKZwUKCA88pqqpv33v0t.r310x29C-11zAprz.36v@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737131415; a=rsa-sha256; cv=none; b=K+7xBq+czMbUtycYYOuvSU2hFGL6hh1BNPQoVZVA5WDCr0WJllgrC18wYEDjSRKAhQfHxi 85lSvjgfzknMWyR5SnkPwSaCt252nSRFZZj5UdzIp+qKeiqcDbJqiIxs0m0L2Hn/hLg+tH WGNYYQWz7RME0qISzlKsHn4oJSvQBSg= Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-43673af80a6so17314115e9.1 for ; Fri, 17 Jan 2025 08:30:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737131414; x=1737736214; 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=LVZ6/wJnW1NLmDdlnZeeMFjWuJzNbaR38/28aWmB+Ok=; b=IWP8tNzKGfYoeNl9LBoutrAshrOj19x5/t6r4WKGQU9Hh8lBW6wY8zNC4CJHpnh+Uh oryuajmN0ZsIPwcwbg02z355mbQWdZ8cRQoJMMBjpHW83vdLEgiDTaQZuNVFKbowfwF0 UFTGMTpFJiX3S5+4eb8uPM2LV01kOaLw2ROpDtJw+VtwC4o8gdsvMZ3QoJ2Lk3uSvEdn gRxwt3CkjdlB0C80xCOzMGS9QBI2F+IgeB/65vcpFNQ+xEODeoqxCfwWPKO4gYxCoC+d 3wegkLqalMZhC9C4mM4PPdX6fEPEyC3+oWgioMOa/O0IYkhkVOJkPZBfK21mOkll9Db6 PAHg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737131414; x=1737736214; 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=LVZ6/wJnW1NLmDdlnZeeMFjWuJzNbaR38/28aWmB+Ok=; b=n4mPTa09Qe4NxxYE8+IaD2BSbJLOBDqOvf3WjvJ7dhhMA/ifAt8YyaJ17Wy2sVN5dN 794iqUQ2vieRYnYIYXUeA5AQU6+7M0np6/nvFtWBZvyy8b1wFSGRyscHkwds4sVzPfV7 /54RYyy3Gq+cyLrEOR0Vwkj754SfTeemzIixwIhGBlgQqmHHlojRdNr0VEJTFEL7r7si Qfs40cJmDhQQNoVTqg5hXT+wlwp2LZRuGrXmrtnx6YPhZl4OC9LgtjTHX4T1TGeA5VM6 hDNcwIpCKrKF8iPJrtcySR2IKJnnr4CWOJTjaqw5GHO1Z84936wRwvMqaNZYieASoQ9A 0Gdg== X-Forwarded-Encrypted: i=1; AJvYcCUL3Q3m8CkmQcL+ucuRJ5Us3mt6XzwmtMRe5IdbMP9n9DKWAlYoNWex0Vcvb1T+pZs+uKQ7GMXEhA==@kvack.org X-Gm-Message-State: AOJu0YxjCRb9QRgdup25Pa5Rbed4kx+Y2s1xcnskw+oN1isa2npJvBbk +FN/mxHvBy3NJ4thm3ofrg7QxWjhoXGJVPNiNjCD4muWj8RHDGFibGDeSaNTmYbevS9GQQVK5w= = X-Google-Smtp-Source: AGHT+IHCaOLf3AxGrXp3UT2DCvE92fOOg4PnB/FoRBbpczstO0JHX8UoSu03mcJ4e4/3vk20WdvjqLoW7Q== X-Received: from wmgg21.prod.google.com ([2002:a05:600d:15:b0:434:f271:522e]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:5027:b0:434:f5c0:3288 with SMTP id 5b1f17b1804b1-43891430ed1mr33942345e9.29.1737131414416; Fri, 17 Jan 2025 08:30:14 -0800 (PST) Date: Fri, 17 Jan 2025 16:29:51 +0000 In-Reply-To: <20250117163001.2326672-1-tabba@google.com> Mime-Version: 1.0 References: <20250117163001.2326672-1-tabba@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <20250117163001.2326672-6-tabba@google.com> Subject: [RFC PATCH v5 05/15] 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-Server: rspam05 X-Rspamd-Queue-Id: CAAEC8001C X-Stat-Signature: nks1iqgp84t1wz7emapsdw6yw19opqn9 X-Rspam-User: X-HE-Tag: 1737131415-362474 X-HE-Meta: U2FsdGVkX19LU9ZB5h5Dk5nkOvSUxi1gHJB8/SQN0VpzLpRL7QIHx0vxMj1phoPU7oxyes3+FqNmIh7FFshzBbmpXJDpA+YAQNjEYDaYTJ0Y9IFO5l6juTQDiRuB8HwRuX3reBcNXr0iQjuLWHRKw7259kmQR93jtr5PpSAQ8JXG9vnTQnP2MANtg7Ii+8DeDj+EFcBNcjDq1ylDshhVBRX+iu3T9jPiwwfppvnyrN86cnI9v8vfpGkf58HeBeiQNF/QxY7k01EKzvsBRq/einEtlcBTjO0dER3Ubfv5u+Iq7o23FcijYDKNHA2gfHAWNYkl3O8ZwCPikfd9DSN0zHLwHaxrv5UYdrL+Yb0QbDwJdwB1PHjInU7WlB0bNSpAq+NPDpfobNdhmvb4OnN7NSCKu6VQKhvZuAfcsluT6vpZwg2NwkF6B8DzzHudgaPVxpdgJo0PcIggzCQMCLQdbW+LEvcQSKfklQpAljTZJp6B7er+ksRXf75eZghmRf46VF8q+wyjr/LFEyMiXnkPkVRw/KUL2W1OI491n804cwanP+pOm7wDkSd03ezuVAsxHEiAe2BS618i4kHKoVOWldNzXP3mw0YAFSAT+KXqi+H5VECJcWDNjBRkIbA6N2BdmeuX5lInD52BFE1vS2WRRT20QGcuV785xUf8bbSmYcnWkKf6FicgT4zoKiJygZmFiKdzPb8H5Gczxd2n7AuuSLO4CyUTuGI0p/tBQmb0GvYmztMLQ1GuhcCWAVscrbAvnwDhQ/CLPCb/qYhV2G4lRrm8RNpiIghtvnBBCgNM+Pv1NuzJErBvwKo2C3o9J2CdsS59iH+BXC6FjMTCtjhMx2ogntiiXzIbpJNmsUPc8SWDqiiqjFjX0BYilnSlntLYrbIeN3Q4uupYQS6hVidm/X7TePHANRwarCwY1akIgDmuefrhFa/NJENxEQQpwlhTBswWTyosEiFpr4rPZ4Y VfvcIflO ffvlW8bOKFpvCqKzuqrU+201MnaRBe2PVv0tm0A+Kq44gAaY86iNh0uv6Vvhmx2egW2Y2d3FTyaUAB71M59uF3ANCJ2juWKZkGHavIPslDP5AnMuFSh2TCaBaotOXW/YARk4xEB7xxpBkxabXqVT237Sgrq+IVKA2IXNXqr2p/GdhQHcVC5yqY/rw3L4v9NU9u7MuYikA4jlN7UQiw0nIWpFJBSv6sIiY1/tI6McJtg7A7+GxxALtOjqSpfwaSJGRf/xKVIK6w0zCQUa77EM3B75SIrv+iu0Eoac750xH9dg0n55Ndga7m+PWodS3eya7xx33WxelWAZqnWgbbXpCd9KvylPbhqraLCpsJMWIREthilW17AqD9TuU+iPJK0F/YF2doHiO3b1nIchiAQgVYxmJoIicnu+RCWyxtbPlOWZ3H4AyWV/l5EU59DH8Ch0ZN8hQgSFUcUBxQ+MTukjXnO9DMlAPH78ruf4Urq4kzF4tt/eWI0jBgZgOXc04UCzErHdwWUpeTY/ynEKi2ydimcl0HoIGG/iqX+SH08citGk20U6fbR3E/jE7Je7pFIVAvK1Xd3oBTHUG1E8UCMzJNqVCknayNFWpliBdO5FTmyNcse+ZPLR2q3bvEiwiX7R0rFcGzKKnZGVevjbLZUjaNUibzw== 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: 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 Jan 17 16:29:52 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13943553 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 82DADC02188 for ; Fri, 17 Jan 2025 16:30:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5B0986B0098; Fri, 17 Jan 2025 11:30:20 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 50DA36B009A; Fri, 17 Jan 2025 11:30:20 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3887F6B009B; Fri, 17 Jan 2025 11:30:20 -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 1A2176B0098 for ; Fri, 17 Jan 2025 11:30:20 -0500 (EST) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id CF8F01C7D3E for ; Fri, 17 Jan 2025 16:30:19 +0000 (UTC) X-FDA: 83017481358.09.4A058A9 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf26.hostedemail.com (Postfix) with ESMTP id EA9EB140007 for ; Fri, 17 Jan 2025 16:30:17 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=3U76Nmht; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf26.hostedemail.com: domain of 3mIWKZwUKCBEArssrx55x2v.t532z4BE-331Crt1.58x@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3mIWKZwUKCBEArssrx55x2v.t532z4BE-331Crt1.58x@flex--tabba.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737131418; a=rsa-sha256; cv=none; b=mk4Ks7hYwlTfxawh6biZ8XP32REhxyrAdFWVJpN8kTj0xRMiMNrz6R+/HTW9+VjHO9EF1O AIvBZXtndrM/QPzBJbQlDL+FNFTXpU6wE7rzdxlfz9nDjlHOgHvtIwZ5i/pTCr1JTFACVP PyperUKErFhBexrynG0BnjcXUBq6dsM= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=3U76Nmht; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf26.hostedemail.com: domain of 3mIWKZwUKCBEArssrx55x2v.t532z4BE-331Crt1.58x@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3mIWKZwUKCBEArssrx55x2v.t532z4BE-331Crt1.58x@flex--tabba.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737131418; 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=qs5nf37zcwmaOIZ9951hFlwOisjQG4rTL5i8c6zaQ+w=; b=J5oP9Ne9TfL3HDK7SN6fQWoWTqOEUqunyVQIcPWxkaYmjE4rNhsxlSRRAnYr/DaYj223+b XVmaOIirzQ5/vUNrwgFIwYrq4cYoLi4Bn3bhyAhTuQmx38okj8NRqxCWFb6iB8wBqdEfN6 RJ1fDcpRupicHAkvzCbC3wv4Db+HW/o= Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-436289a570eso19046645e9.0 for ; Fri, 17 Jan 2025 08:30:17 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737131416; x=1737736216; 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=qs5nf37zcwmaOIZ9951hFlwOisjQG4rTL5i8c6zaQ+w=; b=3U76NmhtxS4SyyZ5OjRxid6jfUZkD9f+waz+ikeCZHPSWUR2DTsu76qRxGH25rJSK6 NRLS1y5Iupq6+D8MhvexcxAZpWLWNt3MrWac+Vhhy+fPUtW6My6C9Ab47vI/IFK0G686 qLAiiUtAMq2ywh/Cr41/ArO6YqAwGvG9PxyTy3JQO9sze1Scs5+7WxWCFpOYywR9cIFV 0Lfpqp3WkkF3ypwEonPRJIff7HGv+1Ou1AvjvJXq3r36PATXrchUFg7Y5OxeGVWk/lAX P81sLYRoN8E2ibVQ8/rg7K4I/Qk+w9AeGHhAok4pU5Vs2EmNUzw6iN33hrXjwHRh0ARU RfWQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737131416; x=1737736216; 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=qs5nf37zcwmaOIZ9951hFlwOisjQG4rTL5i8c6zaQ+w=; b=LHY9UsoLX7z/rhNtQqdSQIkc860AWWeEgnAlkWrqHoE7DbLLgHKo5z0tO3JVkojs5U aZ0bC45FfiDPONAV/FY53zSTc7xrxdJ9a23IbrczII/MIFPfONe3THmpHDNxc6X7gjIR zkxA8edCGIqvLLKUg8ZI0HinqwbkJz/K1B2zzacjtYaSy6e9aCWd16XksRDGbvwSvM5o ep6Hl5TiqE3vXK3iJlSpNz30wJwPRDCt5t+RmYM3J3qTtFBtfobx+uI5q5vN16SctiMU suMs5ORSreTY8g1DZ4YJ6X8VWrpTxHwfEa/dJ3LT9ZnBG4YhF5qxInsSb3uW2AxW0Npl kFug== X-Forwarded-Encrypted: i=1; AJvYcCWFYWd/3YNLBh//3SRQFfXbVFJo3pip5+5QzHDKqqOCV9q2kZnD21eyG8HBUuHDnSO3rruHz1YwfQ==@kvack.org X-Gm-Message-State: AOJu0Ywa7CDmVf/kCs4XjplMDZAMBfIouwPhHn1lUmV2TbNgNYjP4qvK /iFTDNSaOpnIi+Jf5oDhh+BKFI6932SjsjwFcXT5uc32RlsiqXVxroSDsgbSTQjwyB2gc8Y+/A= = X-Google-Smtp-Source: AGHT+IFheEUtlIt1ATVto/VUlbQijnnQ4cyUB/ujy7iW3r3fDB6NNV0j355jsd6xDd1kNpmbUluyV4STMw== X-Received: from wmrn39.prod.google.com ([2002:a05:600c:5027:b0:436:e748:58a3]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4fd1:b0:428:d31:ef25 with SMTP id 5b1f17b1804b1-438913e3369mr37880265e9.12.1737131416570; Fri, 17 Jan 2025 08:30:16 -0800 (PST) Date: Fri, 17 Jan 2025 16:29:52 +0000 In-Reply-To: <20250117163001.2326672-1-tabba@google.com> Mime-Version: 1.0 References: <20250117163001.2326672-1-tabba@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <20250117163001.2326672-7-tabba@google.com> Subject: [RFC PATCH v5 06/15] 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-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: EA9EB140007 X-Stat-Signature: u36ayy5y1863a1dg5qkgpcefp5btz5pw X-HE-Tag: 1737131417-869088 X-HE-Meta: U2FsdGVkX1+7C/4eMzn3hwWXb0zn9LpqWGXQdcx12zoSB6SfnC09KAT//lTJyxhCMqcAZBq9t65tWTur4RFjeUYJICKSqxu6avTrO97j0trRyO6bvXq34FdTTQrIlcX5kXkZ5D+KNJw9CFjYi/VsbndgGcyMwwsN2uXnOHNoTgTQoDB5RbAPgyLbH1C9jvy3ZfjSQ/q7FQGvmfF88eb+Etk3B5c8eb7pshnT+UYUve+hHkcfWPK7IWIFT0/2pdOFQBkM/5dAR5brMITUu4n5Zz6f7IwcvI7hXAc6rtCUwU3TBBw3aLdbemrdkcSWPbVPV7bPSW37nUwwPq5+65GLU5AhQJqLMlTGpQzBbXE/29dZ1a4qts7pEOJ/8UOsU03eTrfTnRDrTXRXqP0hubVMV6qp4BliABnj31Sh9djo+6hhZpNbziSS1/Ckyy/5w53rLRkEhRMgGvdO0RrE1Hb0hnoKk/vwbfo146QHd1Go0lLyuGXd9tgKP7AuPhE/dLmsqaUunCb3ooPqHkS+eMU6oZzwl/b5tH3pS+BMnrxpQ+T99CtaveWLu+9/vPuWV36zwTeNHkErYreLPj3xxMGJn88sQfDkkvtsFjWgSx5+NIkiUuj+qPNYd7FRLTlzPfLHHOp7e+uA7q1ocxMCk1Z72gyoZCbA4FAItAHxsXlrg9cVvmfQ/7Yszqni7rcjGqgL5knJnoRIRJ6yhJfNvzSfExSYSPMtrTKGt/GvkEsMSYvvrhMwIVbFfA4yYsQNxGDmq3/7FLFTQt7UjDeTTfePZNy4118K9UPLnAjR5aBrOFL2TQrEdGDpS4kRSq7Z3ieKcxhKh2jLX7N8d4wAv0HghN7uhLBd0+W1NMKh8iZx4WeDKlP6m7bOFkZQ6SCPx7ACqgjEdmSEmp3jvm+jLJNB4l1A7ssHbRmSrzbDwy+M4/kKu22JXn+c9dHv+DqH6Z9Msy5X8iFvnBclO1TqXLT O2NSjwCJ dCkP5lZkG0xSu+n3RMaPu5SMmf29QdpNWzX2AzyQPB7tnhf07BxKRfkVNL9eHywiYs7sh8WWbJbE60yTTIEwSngHBKoTZkCa/mhsBjhW7VvzchyN0RH8qHG0AYfGH8TYcYgj6zQmheVWLl3sHLbxJU6Ai+WLmdd0iStqYzgkwn6oTZTg9A367ElGwOF7fqRH/g8P0F1NrWgPzvZWMzQODTHvAvfauMnaXXz/Utmqg544x7AGIOJnBewetI9IcwSN9iDlLv8F9Vy4l5r08rREd9x/2uMvJ2p9BWHV9qcei6jg8gGn1NjpU5AiM3EtCCXVsQTiy1/oF8w4vKmZH18t8YWMzbFKtPHRmEiuagSxDvnzGAu5xlUjMDyTVfVh7I4+nDRO5guRJ0vLiLyCUgCrC1JHrbORNwURouu8mfQ/0QjKC7pzuw/JEIvwkEZASPahMW3zB8NoIAxno4ZWSVK2ypGuvk3/GJ6TCFVUQOGH0iI9f1Bq8LnKAXKvIIi8hxN1SU3/FUSbUJNvlmyyTW87wu3exGo0T5XN9HfvfCcxCW3qC2zKtHKlYSd+eW+QhSAPg1L+ziMNxHapxJnsLJZag3fEvsqx2N8fYG7L8sS2rt1Je4FMpuRGhQHxu4IjNKkQ4z6QQ6V8LNSh/ll/MeX2cy09NYDgV4ssmB6zN9o4+BbNS7gCMYdTJ07cGM8P9ESwInjUKdjbuZOvBiUI= 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: 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-v5-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..63e6d6dd98b3 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; } +static inline 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 6615f2f59144..bab3cac1f93b 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -942,6 +942,7 @@ enum pagetype { PGTY_slab = 0xf5, PGTY_zsmalloc = 0xf6, PGTY_unaccepted = 0xf7, + PGTY_guestmem = 0xf8, PGTY_mapcount_underflow = 0xff }; @@ -1091,6 +1092,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..722afd9f8742 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 Jan 17 16:29:53 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13943554 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 7FA51C02185 for ; Fri, 17 Jan 2025 16:30:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 056256B009D; Fri, 17 Jan 2025 11:30:23 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 005AF6B009C; Fri, 17 Jan 2025 11:30:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D9B256B009D; Fri, 17 Jan 2025 11:30:22 -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 BA1086B009B for ; Fri, 17 Jan 2025 11:30:22 -0500 (EST) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 25B2F45517 for ; Fri, 17 Jan 2025 16:30:22 +0000 (UTC) X-FDA: 83017481484.15.F8B934C Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) by imf12.hostedemail.com (Postfix) with ESMTP id 3023B40007 for ; Fri, 17 Jan 2025 16:30:19 +0000 (UTC) Authentication-Results: imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=RoE7W9uj; spf=pass (imf12.hostedemail.com: domain of 3moWKZwUKCBMCtuutz77z4x.v75416DG-553Etv3.7Az@flex--tabba.bounces.google.com designates 209.85.221.74 as permitted sender) smtp.mailfrom=3moWKZwUKCBMCtuutz77z4x.v75416DG-553Etv3.7Az@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=1737131420; 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=gdAdQseomovuuCB1UMT0wPLl0A/JMqD3/SFKXOl1KPs=; b=j89om7VhihyfJAwUik7bkoUhww7XCab1OsC6dXX5sLDejHWLDwfZWuwPhWoUZ0SYqwIBRb SfDr780zyJsr7E4eiFQtgLDVjwD2O9UCQqx51JMHkuWMfzteYWaT713QIQ7tqV3B8TiFOc g0mQMf5ViHCYVdFO6T4GF6+rtax3DvM= ARC-Authentication-Results: i=1; imf12.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=RoE7W9uj; spf=pass (imf12.hostedemail.com: domain of 3moWKZwUKCBMCtuutz77z4x.v75416DG-553Etv3.7Az@flex--tabba.bounces.google.com designates 209.85.221.74 as permitted sender) smtp.mailfrom=3moWKZwUKCBMCtuutz77z4x.v75416DG-553Etv3.7Az@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737131420; a=rsa-sha256; cv=none; b=VwwlhSk0I4Z9+0UGBOUwihiPkf1k0jPXJ8rW/sDdI/q/cd8f5pE1RM29SxiBeNMZnr0k0R eaIMGzUSzrkU6L1DPNY/1kKhnGDhRHSrHfCWuME8KVKzTdt4tqSK2luMkYKmIyt5N+MfUx 3/VTiopCbTN/WJjl2zcSIGtP9rOm74Y= Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-38bee9ae3b7so1545647f8f.1 for ; Fri, 17 Jan 2025 08:30:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737131419; x=1737736219; 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=gdAdQseomovuuCB1UMT0wPLl0A/JMqD3/SFKXOl1KPs=; b=RoE7W9uj5sCkwyy5SpMHvdEHCHqP9WrprAIDWDvRukOevf2Sbor4E4A73VKnVUp4Zc Xaa14yy+Qseb+KG+xICZMv/XscuJ99FtMwp3mALQFi/xyjCuMLgmQ+SuP7Xe18vQp0nj Wm8p2LySj7AjC5oudbiXFvTKL2l4/BJX02wj1T14Bvmmjn1NQgy1vz+9xdQVO4TYauX0 3RPydW4o6N8wgiI89XMkQT4ZiTCIxfnhV9nfAgBUuC3ueBpTtGKbv5oc+jP/EHwlPSTQ 1j4S6sx2wFrrUGD03I3HFwAVBL6LbWxFZ5Z9J4kQGe0eWy1xwRdFBvX1nlRfKJy7LXdG Zhbw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737131419; x=1737736219; 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=gdAdQseomovuuCB1UMT0wPLl0A/JMqD3/SFKXOl1KPs=; b=HN5B1bNp1rOW4chvIKpv8hxq0ExG7iicQHq3RVfCSoLBXRzOTD3xPHI+A/rWZzWMy7 ddatIXB8PrtMdXmhyU3Jq/BXhovxeiE6XvQLoIdfCqg8oeQqLN3y4iruOAOIBi08t25N qXIXKwiwQsc9Oy4ItghG/UAe0Jt2ajG8LfOzfp9rjF01REHt1vv3y31RoMIKv1HDKOsg L5SKzqWSM6ItZLjXMHx67ZKlWzqBOgW2K9UmVlBjVAx9QZ5V/EjC4nOiQfZnCx8m02Cy b1qXl3DcZev+sjFMzmfxDeIGt9PQgOyAPQdOrA1QwahNwWGz4dTgRfSmdZVVz3zjrBAT IJ0w== X-Forwarded-Encrypted: i=1; AJvYcCWua32dtRavPJTmFoqlMiutOaggbsJaLhsKe1sZ5M8FSeGJ4y9D0IfUf7O5XqbFSizRJ7/ss1K1KA==@kvack.org X-Gm-Message-State: AOJu0YwxUOCTvzf3LbYMDSop/xJw1fP7Hm7A38jBt9NCu3PzTbTwCAn6 tzATylIyFSRiT2UM2EXBHIpawnu8cIelwPvLZ9+fXrVH7RoEDXRIBMkUrYHz4JVrkf9KVaamrg= = X-Google-Smtp-Source: AGHT+IGoCiOpQZ+XPGh3+m7uM/r7mJz3ngOqFFIWL6Sni4tqd4XQp6adGu7aEk+5zToL2X1ioL4lGzVu/A== X-Received: from wmbjh12.prod.google.com ([2002:a05:600c:a08c:b0:434:f5f3:3314]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:1a8c:b0:38b:ed6f:f00f with SMTP id ffacd0b85a97d-38bf56635a1mr2878108f8f.17.1737131418732; Fri, 17 Jan 2025 08:30:18 -0800 (PST) Date: Fri, 17 Jan 2025 16:29:53 +0000 In-Reply-To: <20250117163001.2326672-1-tabba@google.com> Mime-Version: 1.0 References: <20250117163001.2326672-1-tabba@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <20250117163001.2326672-8-tabba@google.com> Subject: [RFC PATCH v5 07/15] 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: rspam05 X-Rspamd-Queue-Id: 3023B40007 X-Stat-Signature: x8fr7phz9smpwfczon7o9d7b3jrc8dxu X-Rspam-User: X-HE-Tag: 1737131419-120585 X-HE-Meta: U2FsdGVkX1/rXT5N/dfOJExd/X6OwNIvrCEplCPJa024Rr8g5WeovAqJiUPyoJPj+7XvvlnJ6dgQTSdeDCUGzQHl2+QRw+k6R19XDkI84LBSxEsTyx9jzLRr99y2jiszsWPzwHQYWAY3OravdIj4Th5EtS1kHfSZa9yuEyPTlH/jbi9YdCgJGsD5uqKA5ylkdFeT2Hnot3UunvFpT+sJ8UGJ0IGWpu5CEaiVSPICS4umq08ypQDMJeIHDvyYQPFsu3VVn9rlFJon88O6mU3geteDnUSlqj15Ctqn9jTnUeCx9MUIUq6wsJjc/GeQaSLIcwj/8nYwb5KKb4+ahjM0mRmoR34Xe0+N5R5t3oUDJIkMwunpg0N3Rp173hhP7WpFzKtyfZPjNLxK38UYy6ZhzelcW+XjwWJdmiTHxXD2CJMp72Bu5uHRP7LkHuI4C8OrFM6byibrXoL2NbIR8II531CcTuEJcxhuNwL22YaCK8bsiBEriqL3VwcV8j8f6jWFNbXzxU7XgfcfipiMsblCgFo2n/iKv9+ckwoYmEHUkt7IYM+zC9IoUIzCPhv1Hxtl4UiN8io1vNQW37oJHqsjWu/krNsFOPTdddhAivSNmTBTrlfIPPPfk9HVPqk3H3hutR6cDiB9z7jhTy/qtD8/zD+DJdMnDxszOfNjJxqskkSWlnnl142WbyTk8FUZ4zPy9ASmKwkByRkgr9qCgprNkkh+3rhX2F1+S0UxPWXQen1Ceqy3A3cp0bWGfiGZd5kBONzrpFh0tAc2joBxSYvoZ9YnH3cbQu0Jm6lF4Sa1jf96zVxjVWeiddz44rP/EKAZhkAYY4hhGt9WiJpvpPiPQPUgk9GRnCjfhiAttbXUbkWYzhOEah1MokJ7W/d1FgA7ZEWGr8x7WLI0HjYn/3Qr/gX4r9gg5fEQbO07RW+rXuJbrJOgC07oifZ6EoP6aBQh8E9TFwilwYjxkP6RCQx 5GNPGV/L SxmqQMOMiP1fQDKNExgyOnYV7BXgvkCSlffO+YL6/iMT6+RdK2snVlOIo15dCLQH9RXsw0IcwKnUnSlsVezQpqKXmcTsCi9fVCTaF4vq0d1/66r99bpYW2M/1Zpwqegth7JWJF4QbCLLgSZ1SoOUiifXCPMhJT/20dwG1RSlasJtyQrYxahhG7uiKfDsXm/Vr2pCrfZ3JP5tR25in1+wzBI9zXJAEO98MMrS4C5XHA0bBsZtVmSGbdKWMf3rZ+gNH61JR0/ePK9DxbONOrsjQ9lbHvNEeXia7Hbwcc0kmK0nIwg0DlCC7/gqOZmZr9enX8LZ05bqJ26DlblkDmJhJ07Fh6VFWsdnVGk/06JYeMh64CILu3M0gDkGZXAFxIyr0YNgLDf40Sa0ku8MAgYFaCReuo/7rSXR3lzOI59vm8edyqmH/EeOwTjXo/UUyUQwyExWKpUcf7gMcVLvo1hBKtJCjE9PTZ/2iE/lCPTmkIXYs+/p9jWP/jgTNz37iKvIHUZ8Dk3m2ENbHiE5bT8+2l33SivijUm0xealmpaGEurvMpuEWV2wHG730+6QQA6L6skVhi9uDYIdNEipO/QTq524AobD74YMdfqz3D7axqud46mkL9HefMTh3xTuLiKOsM/NuaOpOTCO15Z400+JTM15c8gkMmXmqAFI35rBocfGxgw32Cxh0mTMQ4t5X0A0WAmuS2EOTLUhmbCouxaT8KW2Aee95arpAIPpW1m+4ZBCeqqdHW2V7jE2/9XbcHafsa31nSe6fbCrnvbk= 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-v5-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 722afd9f8742..159ffa17f562 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 Jan 17 16:29:54 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13943555 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 1EFE2C02185 for ; Fri, 17 Jan 2025 16:30:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B7A5E6B009B; Fri, 17 Jan 2025 11:30:24 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B002C6B009E; Fri, 17 Jan 2025 11:30:24 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 958066B009F; Fri, 17 Jan 2025 11:30:24 -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 6B8CB6B009B for ; Fri, 17 Jan 2025 11:30:24 -0500 (EST) Received: from smtpin18.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 298481A05B2 for ; Fri, 17 Jan 2025 16:30:24 +0000 (UTC) X-FDA: 83017481568.18.7CFBFE8 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) by imf07.hostedemail.com (Postfix) with ESMTP id 2FB754000B for ; Fri, 17 Jan 2025 16:30:21 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=q47Jv5iS; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of 3nIWKZwUKCBUEvwwv19916z.x97638FI-775Gvx5.9C1@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3nIWKZwUKCBUEvwwv19916z.x97638FI-775Gvx5.9C1@flex--tabba.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737131422; a=rsa-sha256; cv=none; b=SKATEc8DozljNQhJda5mc03UPCa72scjpYx1xyJD6gvrAKwETQL/DFnU4rUaBYXoMBXvaf Ct9mE2odXrM1Qho7MqSb3XFoMl1JumyeRbbWbT8rUxM8GUbYyUx+zBhMjbBFFfsnPxuZOb 5hwXIy4Wy3NdExxIxKlxPUJMn9qCS+A= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=q47Jv5iS; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of 3nIWKZwUKCBUEvwwv19916z.x97638FI-775Gvx5.9C1@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3nIWKZwUKCBUEvwwv19916z.x97638FI-775Gvx5.9C1@flex--tabba.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737131422; 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=OBxaOc1Oq/7r5hpyeIybyfEKVewLBSFq8w5fQigelgs=; b=ETLzCEC1XVtHl1rxx5+Nnbw/ljBLVB9crxGlNeRlUsaQWzSJ1rSg6C26dfv4rQOsAi3RI9 wqdsNGJEpmVZHKC10yzgxYHeqlx7Y+Z2iLizb+F5vgczJkE6HZXIetNRW6IwoE1Wg6nJ9k U9GEJPXMHaXKqtkCfj7MnPxnolOqCEY= Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-43631d8d9c7so11920065e9.1 for ; Fri, 17 Jan 2025 08:30:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737131421; x=1737736221; 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=OBxaOc1Oq/7r5hpyeIybyfEKVewLBSFq8w5fQigelgs=; b=q47Jv5iSFmVEtEFSJKsIaQi0MEk7oGxWdVGCQkEptLcVNQcGLuagcxwXyiWBKThEIy q/fSZcT7Si74pAj5DnYXa5dtbDX2+YfmVpI/8kb+U/j9xEPo790jIQo0rm4eQ7QKveMu 6PxAkUgWy/lnNdmgDYlw5CtFwQzku783FsprrLsygVJshWkI72I/GjYJ2FiIDYZkdRBB 8kfWR7rEKepS+/IpwGR2OofYYl25T/i4YM28yco9yX7scoIChvqpTeJoD0lO16yXA9Ko 2R5bXKfbTIXT7aNh0x6x/tMgNP33O6SP16d6Do6kcx2FCYsC4JsW5406qmtIgfVxq/1H WPZg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737131421; x=1737736221; 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=OBxaOc1Oq/7r5hpyeIybyfEKVewLBSFq8w5fQigelgs=; b=s7XQFO07i+6L+Aq1hC03Oxp1UVBWfyp25ZTkvkLclpbedH9/yFl47culUMi9vBIEZO R3JBkb2rsRrGs9jq7Kx2AhPMPL8I/XoktOmG3InmnsmAZtC3evryhMRSc/MdfGapxUmC XB6+TpKg5EekKZ5Vr183L0IYzBVwg+E1r4a8h9X57VIiTn1YnMhX/h4RETk2ix25igGv 3otcB7AKJ5tcDnthTwzpTLhrn4F1hR1dLE9fytnlcpKRGcenfQomVFEz4Bhy/Jlb2NPK W3IEN/o/6SZI6qeJdmo1DwsaLAcdgOpfc1zleDTRRyQXSDInN8gToaql1yOdVuZt2tl6 D82g== X-Forwarded-Encrypted: i=1; AJvYcCWDJbmge8f/KsZ9rKJSTaBMQ8jD71vnNfVgfSOC/zNBlwk9DjJ96283UrAt/HNeBc6aacuRYxbkGA==@kvack.org X-Gm-Message-State: AOJu0YznzTBPFpgxb366oW5xuG/pD5+YXByahQZZ4MvEief4IYRK72gM 7FNciy7LiqGow93Lz1KmVuZKecLNi3AbAEDELH5MDRBWvARDbLJPVrpIwTJ5gYyBwvXxjFcpVQ= = X-Google-Smtp-Source: AGHT+IFkVUsgGxCt8u55ieXPYflUnbcSMN6hxhrnCXAB/29entNP9EJfjs2xWohE/JZhqpVEKXf+xlIxAw== X-Received: from wmqe20.prod.google.com ([2002:a05:600c:4e54:b0:434:f350:9fc]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:8712:b0:436:1b0b:2633 with SMTP id 5b1f17b1804b1-438918d9008mr33597535e9.9.1737131420833; Fri, 17 Jan 2025 08:30:20 -0800 (PST) Date: Fri, 17 Jan 2025 16:29:54 +0000 In-Reply-To: <20250117163001.2326672-1-tabba@google.com> Mime-Version: 1.0 References: <20250117163001.2326672-1-tabba@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <20250117163001.2326672-9-tabba@google.com> Subject: [RFC PATCH v5 08/15] 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-Rspam-User: X-Rspamd-Server: rspam01 X-Rspamd-Queue-Id: 2FB754000B X-Stat-Signature: m1w8hq9rue8344b3xfn56djdga893o3z X-HE-Tag: 1737131421-248992 X-HE-Meta: U2FsdGVkX1/wraAkI1Za1VMji3nLp3J7jMUmW2wy0A5Ucf7eToKDA8fbAjTZCcX0qEYRTecw5Fg+JOii/sO0S9VjHz2qbmtKvs2UJGohmdjDdXoQaWJauDOuyXVEud15Qruk4MS+gs9Ns4VumTpsFVU5CVqqxpzxH1588llxt0g2WPwsQY9Y0571SkpIkc1EcgSMLC/T76lb82sziBp+QRIrphhO0ZRScMRqVUFtZ3/Pbjl0DWCVzVK0+Xyg7tG4dQjzleh9AJ++ZsCmpo6FWoifoUDAMmIZEuwPmd2mOqVJlhuxXbwAdOB2rOxmdaHfMiJaIOCI97G4E7rbHvDOvZ196NAmzUrerx8oMQrDrJJ5BqIAkT3D2mIdCIT3fGK9EDwpbDnpopUe++DWbDCi3nJfwhnKo3zGgoIRzOaU9kjwlWi2C9rajwGi9uMJQ8RWYieTJN3QvVsSh0l1NLF7e32fH/DMsWCiCLeaHWlb31Lu69DK98NAB0HmoJe+BKywOo6RdQbIx/89rW6YE4CbRvmCy9XWOyMURJmpvE52xef1syNn4nu9e5gwy4kwm7TUAJV9StwNm51M8nrT3JJim6Cip/ZnfeIpj6ko/tl/sJMLD0zM8U33U72XrNEWfYtJNsyq3AzaA+G2NYnBlsiqp5jWM3Y7B5OjzxX6TSKgP3i+iRQq98rsnmycv2EsLCgNaIpsNrgq6s6tOdfV0cHXYlOYr4gI6HwT3g/4HuIOJn6jOY2q3wrn1LyY5Tok0G2e3vO5oh4CEGGlL9VztQ/lLYqxnl4DOXUHWxewrtT8XBi7d5oBvyIl3loaqedIXUoMvFXgG2P1p1VK042xsmgQRYJqjsY/op43Sa+KIcwrukqM/hzIOwP2kwZTlaHirIn3TD05qxYin8snrRu+fmEOgJxAIFq2uQZWYgFVKUVccl9bQEoDjpFJ/U6srtGIreeGNzjGR724elFQT5Mkrey iXYn71Eb HrWGmHZnGG4IQSI+dvjzOtjMJoxuVknDVSGDZt1hMgotmcFM39ziAOFFNYJcObPsP5OLDqP3yRriJ4ACX45vqOgSiDXvn7G0dG7TD9FXcm9D3RFZrfcADwknS7mmUF6i+FnsG9YpPtrvlaz+eofxX8k1qPtWcjHbZbCdF9EOfsPa0pHwbC7UHARmIqW8OpqqXv5AHqdhSRnAS5d3Gi3JheofGWGZEKZ43ViTYwp+zlpwRuzuhH6e3wNsG5hV/UJDr3+u/wkDsdwiCQF74gMkOZ/tga8AZbXA4nRsMeKvbu8PlSpNojLB3cTM+ISZW0vLwtqqOX4bL18PtlnZ2JslS7Qq/tucui+Dsbjt/fbdT5bx7+gyPyJYpoHNK9LgU2zzD5DpUBCTPWXRSx9yhQ4GMxYN2CSEn2+iR1nvK67bTDI0mUyyqsnPbb8vulpOV0gsgS4VVarSJUDNfL5LYOzV8OGm9ygslGDGYOoyiaN4zSE4hEYv/aXw4HuOF6SHq0ay70m+2Gd+Uegl9fDpZbS4jjGZ4KOcOmSk+f3mnPJ+u3FvNJGSM01dcjJVo5Bk9p1Kyls+CPOXwbQNdygk+2j2bEO50qkVgqGAfxff4EJCOIZDNQbKRjCwFoNztpNA9LnFDAiw7SeAOjBZ7nrmErKQ30jQXOA== 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: 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 Jan 17 16:29:55 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13943556 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 99114C02183 for ; Fri, 17 Jan 2025 16:30:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2EF6C6B009E; Fri, 17 Jan 2025 11:30:27 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 29D676B009F; Fri, 17 Jan 2025 11:30:27 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0C9B56B00A0; Fri, 17 Jan 2025 11:30:27 -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 E05586B009E for ; Fri, 17 Jan 2025 11:30:26 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 962C51405D2 for ; Fri, 17 Jan 2025 16:30:26 +0000 (UTC) X-FDA: 83017481652.10.047E019 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf05.hostedemail.com (Postfix) with ESMTP id 6376F100017 for ; Fri, 17 Jan 2025 16:30:24 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=pYVqleML; spf=pass (imf05.hostedemail.com: domain of 3noWKZwUKCBcGxyyx3BB381.zB985AHK-997Ixz7.BE3@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3noWKZwUKCBcGxyyx3BB381.zB985AHK-997Ixz7.BE3@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=1737131424; 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=BW49JWG2Px+0iema5hxnfEbJmT8jYzYvP3ZBCGvznBQ=; b=5x+wJusWK5w3gF8h6087ErHfHzn0i1PQLyVMLhdf4q32KqFUyd1h5f7Gxey/kSRDR7oup+ ENC+NnSVp9BIXzSZawdOsOBXaj7D2ujHmFsGfkVoWo/A0dAaaFKG8oROG9vOPOEmhguYly h1AdcPSvvQ0m9p0euiDINhwRK5EokRc= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=pYVqleML; spf=pass (imf05.hostedemail.com: domain of 3noWKZwUKCBcGxyyx3BB381.zB985AHK-997Ixz7.BE3@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3noWKZwUKCBcGxyyx3BB381.zB985AHK-997Ixz7.BE3@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737131424; a=rsa-sha256; cv=none; b=7m1fj43POSi/3eaRWYdcmH0VqoJbQ4Z4MhcfaD3/bdfpOAOUcgID93PP8Z5qCu9WSeOtYD /2rwu2Hoh7GkA0ZGF3imwruD9qFXyIGIgj+DGNm3imZkevxy+Npj8jTpPsx7cWt7BR2uaq MWSJVu9VixKBI1qXqtuJ9+WSZsNIwzs= Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-43619b135bcso11494575e9.1 for ; Fri, 17 Jan 2025 08:30:24 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737131423; x=1737736223; 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=BW49JWG2Px+0iema5hxnfEbJmT8jYzYvP3ZBCGvznBQ=; b=pYVqleMLTQJvYsc2Fpltbbt/Y+P85ROTmJxQRKnNUbF5AbESJxDqkORnXBh4LPGK2q W97aejbKIg0mySwCE1pUSCGu36IQW+v4I3BQVVTumxI6Kace8aCEiJ+9W/M6pKuPG5X/ 9AbGRwHsNb83hx1Wom9AGlNBn57ce8WklLMMUEJlKBtpoNsK5c1ymL1/yesWoM1PGHUs IVwNscL0JzwjPsmklmZ0lovCIIVDulwYBy3Opqm0uC5iqWwJ6cU+bcoj90JJpRiF7+p0 sRJdGsDu34nWmS9/1BTsNyeeb8OXaPSRnZIJLrIaLGU4TzEKsPp6lXbKKXpQIUFABzRw zlUg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737131423; x=1737736223; 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=BW49JWG2Px+0iema5hxnfEbJmT8jYzYvP3ZBCGvznBQ=; b=lqcAx5cvon88PbrDxTpMBUwyH4V6b/HfFj/wS7U3NgqAVOjBU6F2DaiEA5zvHI+eXI GoJJVuKAWGrfj7e90QQBOv+a18RAAWFMFtjkcsquRPCgrcBBVReU/8g2Xrbjchhq1hRm QPygVac4f2po6vxXm9U4VRb3azUNl+GcFYG510Op6C1qdF+m8A6Drbx5l5wHS1eQmPPm JDRWbdrreZ6u6oLRNebiLvhP8MBfOE2r13qBhIZv+6y6yreRQYydw1FOQqT671KLxkBB pv1Q3LDiy45LoP39c3eX/y4tcPWWBqEyo1rMch636PySjMVyQgBL+LQsUDyJrxePHJej MPKA== X-Forwarded-Encrypted: i=1; AJvYcCW5jSM7o8bs8WM6qtv9/zTIItUUtGllDA2QKFiCofyki0UwSS/F0ZIubG3ACv7xRqH2o/zlIO4Img==@kvack.org X-Gm-Message-State: AOJu0Yyi51rvi53VM0TLnSQAEDDuCL0b4zWKyhJYDPMtKxWM1Kgdyrzv 27dIw4s1Uetv3ZquY/KwXwy66znP82hNv81D79stcrnQq0GYXNCkSd7DFQHN4G2OnLKQkG/9Tw= = X-Google-Smtp-Source: AGHT+IFy1wFm+ziznvd4kirGkUVDSDuqUcY6omN8qTjIUTRguG98z5VICWvWS3y2Y1jXpECmR3gNFjH8Yg== X-Received: from wmqe10.prod.google.com ([2002:a05:600c:4e4a:b0:434:a4bc:534f]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1da8:b0:434:a711:ace4 with SMTP id 5b1f17b1804b1-4389eca3ca1mr12533835e9.17.1737131422953; Fri, 17 Jan 2025 08:30:22 -0800 (PST) Date: Fri, 17 Jan 2025 16:29:55 +0000 In-Reply-To: <20250117163001.2326672-1-tabba@google.com> Mime-Version: 1.0 References: <20250117163001.2326672-1-tabba@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <20250117163001.2326672-10-tabba@google.com> Subject: [RFC PATCH v5 09/15] 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-Server: rspam02 X-Rspamd-Queue-Id: 6376F100017 X-Stat-Signature: rjfudrq7fr8qeeyfkqbxx4uba7w9cugf X-Rspam-User: X-HE-Tag: 1737131424-153736 X-HE-Meta: U2FsdGVkX1+rbfGmiCmoL2xaSLWc5mOoX0zDpOEJYxFsourVSyagyZu8DkgePISjcDWfdc6YTT5SqFXx+epcrvMsCODvcwdooOSKQuA/WEzUZ6987NGJ6TUYAA+1o4Ltne7nz3rQvVN5Ke2CVveGe1fs0dTgpDdqPnxHsVca0oxpkvs1VPlP/QHLryR2h4JwvYsxW73jbEvzA0eHYYCHcjYt7GR0PExnzOVb2MgI/N6YrNwxPBSfpzquA7Rp2GyCwOoX6C9S4srMGKVI29IcUhhi8m3jMkbSv1L1Zmn3aUGJoRjPzW6hk2arjk3O4vSjDnmFNGMoFKJOP9prM6wPAdo6mH/HNyOn7nV7MLjXGRIGarGOLnTvE+07sL2J4vvrku1PtB7GwsQzGPONSfXmK3Jfmx7gP3NQzxgJHKeGGLJt2E9lZ+ymDI+2BqkqyUSzCFD8MGotGke7Zo0Zr3Zz6fgselGRuFfb2sLhrj39kdcCKGAO6Zr5Iryukk2yfAJVZCe18LxbpYf+vrOs45RivX513x4bhNtgFGs6hg4NpGz1cNGCCCA9I/GW3WL6C7RQ8MeNGJse0psJximTqjzMXfFFPSxcVcr3n3eAmcMBcqwTDGedVvDHY2jN3WtpQ7IgISMLrdAxtpqsh0PwO1SEFxibSmO0YgMPBDNEv2OLKWtZffAK4lfscNhUFelsSj7dD67LMG7qtrp6/rUFEeYoQtC8v19r0Q2JyWk9ifAlWqzIvk35eoqyIlEV/405fn0mBBcEwDsZ5EPVQvOo9P+9zsv02mC1TjCZvN9YVADapTUpQgyDc9PXbNH14YEmmkYzsaStjyXv8rpxyp9Plj5jqZgHMhKvzSH9v5ID6WmW0+1SKc7UKv0AH1rTx5hbo9DwtU3nsrQG5nfHIh43LQMRANMUinZz4UBvdqWQfHYx45h+qAe3obLLyYpXdEGd7PAd+x1O8F5dk2R41ZHob6u XnusXbet nW89ygZJAyOKgs3zQu78I0fXbGhH1Zhys3ae8mbbhPxaWH2tHHHcZArYADG5wKnOe4if5BzrWc9Ip9cDdmWGj5Ieseqi0Hw9m06/+OPXEVXU+wX6RtQdC9SutSsKbpXJew5iVL//0DbAtY2AWB1IScqSwfrXFM5i4RXKOxUpEq0LhRBFtBbp3wKa3xtl+0SGZBERNgr5F4th3ifZ9gMYpnKh0FZl6G/556jEB1A5UHUtg8rNwoj/KLV1MRYwEXLG9TViLfXxb9aHCuKNpKIGZeZry70hBCYlka7zB14AgodRoKjDDFgYahP6uZKCBLgVQNa6FbVuQaGr3AHEm9rwk/Ub79bUJnjZG9dBGUr9IZe4dr4H1Lq/oI0FkBoA4YS7GqH4rjBeLHoropcM5hVBm2BqKyTnlpSEDndyIJErTyFhr3dnWDANvg6Mex0exnpjARXaQ8rH1X0sOsMaCXf0DHmcZfHe+htrefnxgGamghKP3vquabglKeq8EA9fX1Zdvl7liMXWzs1xxW1CBVlV1x/gz/7533u8js4vkrZZPMtZl8xdmq6XiGlAWpsQ7Ay7Q2lfxUg72Sg5/D3JBhO75LH0EJ2pt3yhXU5+7nQLRfW7YkTVpM0DYECTruwcqqAcIZ/0rxmpiCE9AhQBdUJrQdph6vw== 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 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 Jan 17 16:29:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13943557 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 83BDFC02183 for ; Fri, 17 Jan 2025 16:30:34 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1681B6B009F; Fri, 17 Jan 2025 11:30:29 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0EE606B00A1; Fri, 17 Jan 2025 11:30:29 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ED2C66B00A2; Fri, 17 Jan 2025 11:30:28 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id B20B26B009F for ; Fri, 17 Jan 2025 11:30:28 -0500 (EST) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 71825B05A9 for ; Fri, 17 Jan 2025 16:30:28 +0000 (UTC) X-FDA: 83017481736.21.151B97B Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf25.hostedemail.com (Postfix) with ESMTP id 7AAE4A001E for ; Fri, 17 Jan 2025 16:30:26 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=irTyNm3g; spf=pass (imf25.hostedemail.com: domain of 3oYWKZwUKCBoJ01106EE6B4.2ECB8DKN-CCAL02A.EH6@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3oYWKZwUKCBoJ01106EE6B4.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=1737131426; 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=Xo0QGcWhFZe9U6hSQiSPOH6tARFYvfNnZ2E+qSQ4HQk=; b=pe4tbVBOrPrKU7VyINwpjJi2JZ+sY3g5E1bnF5ZfoiJwAjXSKtndmIXpw5RHThSoxScbBD TnMq9VewrH6HtGvlPll5J3N/KR7GuxeFV02Q5UoM2fPIAPN0DgIX/fAkqVQ/BncuiyewQv WR3SO4+/pxmSJJQ0IRZNIVCmQF+aaoU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737131426; a=rsa-sha256; cv=none; b=RF71ZEssHaaZN3z2vACZ/GCOe1iQPBqSiK7Yyr4np1qHnlrniuFzzsiESZeinAc5yrbovR 4KeMOyrClEf16vWAPYoPLH2cc4HE5UyGnORGQclDIRwP7+Xteb/U+pniMZUVVlZcgokIWf OH12iTf0lLhY+9TesDlYQ4mwp99B3HU= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=irTyNm3g; spf=pass (imf25.hostedemail.com: domain of 3oYWKZwUKCBoJ01106EE6B4.2ECB8DKN-CCAL02A.EH6@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3oYWKZwUKCBoJ01106EE6B4.2ECB8DKN-CCAL02A.EH6@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4361efc9dc6so11805875e9.3 for ; Fri, 17 Jan 2025 08:30:26 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737131425; x=1737736225; 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=Xo0QGcWhFZe9U6hSQiSPOH6tARFYvfNnZ2E+qSQ4HQk=; b=irTyNm3gNTt0cHVIU85E4rQfXQzZPRQrnJfs0ZouhdGmVTlNAkcyd3YffWmIoEg2a+ qutaogtiYhihQO6xJx2PbnxVYsMABRiDSpAhuuOg3w/chYMcvzHi1h1C5296mH8XnDuQ NFmuE3Fj9/kbmEUikcufvNT6XJRKxFcisj9L4IqgqxJCPyRM5lnaRw6v+XjLEDIP/x1I agm4T+gzcfCF7JBXNkXRFy3Pu7Lo1U7vPSVhaUZMbzTQEUSKmQMPHeyJT0Mx4cRpneIt 9OuvjGa4qDJz93L0Mxzj2nIPhVDUPrOWXzCvrW6Xx/UzOt8Zx6L8Na+Rkerabad/TcYZ 8GeQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737131425; x=1737736225; 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=Xo0QGcWhFZe9U6hSQiSPOH6tARFYvfNnZ2E+qSQ4HQk=; b=QQg0awuWyk4gEQ2c9aEpZL2+9aoXuhEwZtv27O43dk/bjc3hFYSLoj45SLMS0hW1EB 6oteow+Ts9/fmeMlXvV1/zz/MNevqkcQ57PnYlv78Vx2V2bM+kTE6Zg7rtWI6FG3KPlI TbcQHsvQMbmWRiTCLlvW87t1aRoiRijo2OHIH7XE62JnzPC2ggTtt8mUxd9JYp1EFst/ cxjnTCir/7SCbIU95QJKvg59H3wxIk4i15VP/+WN8pXqsgK68L6xf5POIjFEnnEO8r3F FfiliwKXbUiMp3cLrskGUpYFnRC3jq+BSNhkLLEL5+QvGQM1Gy7jewvIiC+vIeLI6Nlw Xbdw== X-Forwarded-Encrypted: i=1; AJvYcCVrtHpEzKt3qdP86z0vm1ylCwzY39Ize2aYA4WQtwQgGS6G7hbrJUVmfflZiuGDNItxZT2/fQoK2A==@kvack.org X-Gm-Message-State: AOJu0YzWawpAQe/XTVYJSWuNc2ZDILtYnzkT1T4xqeUPqYjJcXnUe3x/ yySSCXOONFG9Uonpq6I8k5eWOZNZi5wjglclernMUyaNpOGL1H0knrTzefVcbqUs07kQeqb/5Q= = X-Google-Smtp-Source: AGHT+IEr5gHEVjnL0bNACXR5FJY9vyiyHwTa1xX0TfEkZNy7DAbAsx+X4Pqj72+4G5i8n4A/x2j0nCfypw== X-Received: from wmsn40.prod.google.com ([2002:a05:600c:3ba8:b0:434:a1af:5d39]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1f82:b0:436:fbe0:cebe with SMTP id 5b1f17b1804b1-43891452f6emr37291625e9.30.1737131425133; Fri, 17 Jan 2025 08:30:25 -0800 (PST) Date: Fri, 17 Jan 2025 16:29:56 +0000 In-Reply-To: <20250117163001.2326672-1-tabba@google.com> Mime-Version: 1.0 References: <20250117163001.2326672-1-tabba@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <20250117163001.2326672-11-tabba@google.com> Subject: [RFC PATCH v5 10/15] 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-Rspamd-Queue-Id: 7AAE4A001E X-Stat-Signature: opjr9xh1i3wed4fyhxik9nhqdup714xb X-Rspam-User: X-Rspamd-Server: rspam12 X-HE-Tag: 1737131426-913978 X-HE-Meta: U2FsdGVkX1/5fa+qMPebho9M1CR7fQKMkFruw3yZOHOwowNqQQxx14n0yMW1zNmLMj7aQonmhNJ1rtIaKGw44zAQlNPkWnuge4ai/nHDNtqTMcXFPeg8Sm0Mn/wjbO+zYddWntwLAaWQcZ8f0zASgdnD+CDHXDj5r/+C2AqQKuYl0D1Ay6mL/kflRmwJeM3jTKLLGRS9K9Mr2sb/qE4tYR2JBJHetk7k+MWhu3NgRusGWh611LW52kMH0fS2TMPwLQOzEn/f7Rnu8xW0hC30i8g7gri04UkUK1ByapFrLG2Ym/7SbNAyb2JfQ0y3ADGciT1x6Gh2Nm3wYWK90Yeh9QRX/yVqFIKfzZibwXlGGVhYO2qynqhn3IC1AKKqvWcxi/m5EoYLXdh30BwsX4JiMQUrPILSKKSJ6Kia2NlQC5JEsEVEFqCOHotUbXm4x1PoNQm4h5vHTIg3/l2cFCNC7T1JC7m9zhQrqD/TEm9MQNoNYCblxNj5l9Q7iizCzj7bEQon9LykK4dfoA5TA2JiXqenQ8OIgtZkirPVU5xw51Hvj5FoFvnFcuOsBxLDpi8byn8mhtsK9cnMeR3woBOqOlQB9TLfANXmxsLHojiKQaTy+hWF/9aQRXeuQh0n3NSdfFQMyVXLTTQajEtoZ/sUt/S22R+FI+WLj669hgslt4jyJU4uhVSgU0bFIIHFGNuwvL7wOD3B+H+6zMWPPbRfFD6i5mlhP7ylB7bVKkbmLf3CUXsnS16HTnuEmfKZSYcjdju+LJTAeqyAvOdf0/nsZTeH1PFbZ/j/c/bZ91Wja/j/2VngzYPQMWbxN71M+Ds+j+Pb802RMOPrtJNLCEja06pBpqEfs2SJgZmXjxnjpdEElkbFrBUa3Vy2WvDRGpOLFiK/bf0iFkiF7QKdFnCudRYKZHt2KIRr05rHe4EKH6dGTzjzlb01P4/0dV8jMuPagN11f/yZ6Z4psczyUP1 jBJJjQPn Y3JFha2jmYyzqcRKxZk0xtfyMcY5z59Ig3+TMqGD+Mdqhkz+Y/WUxhEL8GE4WztNTNgbv+PLMo+f7CGuTIU7EfaLni6M3Pcx5lkGoY8LMeInL86qXNYTy6WoFG7Z4Whp4X+pl2qoEmbDo7EslNa9x+qCBKVitsHtB09XZdZ6a2jSdgfggSBE1Nb9r2+lcTLCmusa5VrM2tr/HwjqUXLMxVQ5b0IWpxiWq+du6bgyKohcN2rDMdNRlpnzpjUHdfx5kWJm7ukY5LXW3BbaC66bc9fiQnN7M3wz8E9iGc/L3WdAx/8uVANHFV6+Gew4r7OHm3fN02k0OqEv+L0CwtjnZDQK8efOFpwLaGBKxbJtqBwhdbBgvEPCEAHxdNhrO1ISWpzdLXS2BhKFEKQwPDEcERkzHo6ltOdiVIk8Ng+bWcRJW/K1u4HjdgUNBf/2zqNtHQwz20fKHN2VsSWvBQG47iH9FoafUtV7dj6cYoaTu1mDOEfe6ZZJexW7OuJ+z/PcDsDRmg8MpwlFZ5P0MaxvHd8cb0/v2fE2RUZxSjrvIcMb/jUoOXC9Tvkh/Wi//CbpvnAtVF7xfMJnUSkGTf8KuZFznhbawgbbxZ5owvJsqNXfw8cEhR6JELeZvpZMxfzDcJ4PpMasrZybwSPHsvfYEgDg0jnwunXjioGA+/VrXPhBV5uw9p+DGyzYdow== 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: 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 f15b61317aad..2a8571b1629f 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6383,6 +6383,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 159ffa17f562..932c23f6b2e5 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 Jan 17 16:29:57 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13943558 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 6AD23C02183 for ; Fri, 17 Jan 2025 16:30:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8DF926B00A2; Fri, 17 Jan 2025 11:30:31 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 8B81C6B00A3; Fri, 17 Jan 2025 11:30:31 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7319E6B00A4; Fri, 17 Jan 2025 11:30:31 -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 502616B00A2 for ; Fri, 17 Jan 2025 11:30:31 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id A555445592 for ; Fri, 17 Jan 2025 16:30:30 +0000 (UTC) X-FDA: 83017481820.02.D7CD65A Received: from mail-wr1-f73.google.com (mail-wr1-f73.google.com [209.85.221.73]) by imf25.hostedemail.com (Postfix) with ESMTP id AEAFAA0018 for ; Fri, 17 Jan 2025 16:30:28 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=mu8t9VFV; spf=pass (imf25.hostedemail.com: domain of 3o4WKZwUKCBwL23328GG8D6.4GEDAFMP-EECN24C.GJ8@flex--tabba.bounces.google.com designates 209.85.221.73 as permitted sender) smtp.mailfrom=3o4WKZwUKCBwL23328GG8D6.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=1737131428; 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=L3JE5H2O+HhGpe+U3oFgZgOkWGJvxazt/6lMsPLsK3g=; b=Sr0Pe5ZjgyvdRW2q+tAk84S8T41I6DeqD0nRky5W8PE19F5tI4q6F4T3d3b8DMU3Vap+Qo 8/ybyKfeZqZRKc4TPDTusZgXli7uFUs5rOn10ZssvuGRJD5GNhYWZW+GY/cVokgS+1C9+I 181ObxiLOQzZZwutjwf7R3+/mjqaLuw= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=mu8t9VFV; spf=pass (imf25.hostedemail.com: domain of 3o4WKZwUKCBwL23328GG8D6.4GEDAFMP-EECN24C.GJ8@flex--tabba.bounces.google.com designates 209.85.221.73 as permitted sender) smtp.mailfrom=3o4WKZwUKCBwL23328GG8D6.4GEDAFMP-EECN24C.GJ8@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737131428; a=rsa-sha256; cv=none; b=tIUNIfr5a2VObNbbSwGxvjJmXkZChMbSo52Q1xe2Pyl2y31Ndi21aoh5MEIRMDym0PUr1k FPTgdgwe+C6l9k+wsnSXBsJY+nsBmFc7xuMM1OZYNbZ9sX2spYDSF2ne9+mSv5cyQIaomi 0K2NLnwvru+FO2pYcsM+QWUdNisjQEg= Received: by mail-wr1-f73.google.com with SMTP id ffacd0b85a97d-38a2140a400so1867191f8f.0 for ; Fri, 17 Jan 2025 08:30:28 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737131427; x=1737736227; 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=L3JE5H2O+HhGpe+U3oFgZgOkWGJvxazt/6lMsPLsK3g=; b=mu8t9VFVQ/B5Q8RvmirSp5Xg6gF58f5EokeX81t+DyEpn23SH18vU72MPJXaITdNmt fQIBxEW8qvsNXjxK6vdAHWc/dw0ZrB0u8cjbtAoPB6t+G+btvmYaQMuyELUx1PRjq7vh D3Up61Sk441xnWy5TmOHAIK3rlHrOMVEMJyHwkxEMHyNs3SVGQGc0VUFPN/0sQyV1OVe MW24EFqXO/gSVQS3z6OxSGjleAZi4rWJcSL5XA7dU4ztZYGeUxO/WQiQjJ5e00nHTo/+ ZSD2zqMuFWdKqe04KfKAtEypWRuKP3CdI6Rfj6rEX6NqRp6gpDWXcT+gCZe1LLhnqgJG Rkhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737131427; x=1737736227; 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=L3JE5H2O+HhGpe+U3oFgZgOkWGJvxazt/6lMsPLsK3g=; b=EiT0io8CkhndJjKGVrkdZpAjol7wsp3IS+nh6KLfjtwcjAmE8wkn4jpgK9vzPKx3+f h/ED+Ii8bfSRiPdmOr92IXNtrbNymVLu4tW8lt2QHu+diwafqVbKP2xuGZaMM4IfK1pj dXzVQPpg/G+0fMg7M4WZEooozwyZt8orYG6ov89keoJeHWX4ucxjA0G1GrYLFLfJEmp/ CA8HvGN4/P0r3JOOReOfQQs4SnscqiCt0/huQ7TLnJ1Mpt7IPAVk3xmUiJuYZ0mzwIJe wxoS5MgenKphLVuQ8E8xhBeeqQ24IW3ltpX9+EcD44lgFcuOjO40raCS2lAYKVNSu4P9 r1gQ== X-Forwarded-Encrypted: i=1; AJvYcCX/FzDNnqXeOQo2JbAv7KcA34f0wexihIOymZvVJNYlZ/ugvRQUrgTnrP6gLXAqqYYu2Ay54twC1w==@kvack.org X-Gm-Message-State: AOJu0YwyC2CCGNvHxyaCFuxGgpVGevhmsOkkenkmo9/NrglE8vZXbNT9 4LwD50s7joBhe82IMLr1dwRZnux7fKNiRXP1cqNzAqwavR/H4qXs4arVOEwn3uXMFUeNkuSKjQ= = X-Google-Smtp-Source: AGHT+IGRpQ4nyI6BTUrkjbZ8ozpuqJEpQ2vY5BcNInNhu8r9xyJzkDeNESuZ6QV5cyzLhvncD1YckbOmdA== X-Received: from wmsn40.prod.google.com ([2002:a05:600c:3ba8:b0:434:a1af:5d39]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:402a:b0:38a:a047:6c0b with SMTP id ffacd0b85a97d-38bf57a97e0mr4283682f8f.35.1737131427228; Fri, 17 Jan 2025 08:30:27 -0800 (PST) Date: Fri, 17 Jan 2025 16:29:57 +0000 In-Reply-To: <20250117163001.2326672-1-tabba@google.com> Mime-Version: 1.0 References: <20250117163001.2326672-1-tabba@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <20250117163001.2326672-12-tabba@google.com> Subject: [RFC PATCH v5 11/15] 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-Server: rspam07 X-Rspamd-Queue-Id: AEAFAA0018 X-Stat-Signature: 75reicpzpz9hzotze9zmos3ewqy3tcq1 X-Rspam-User: X-HE-Tag: 1737131428-763644 X-HE-Meta: U2FsdGVkX1/aYJ5WYF8DT1qD3yEE/DRGzBuKcCnhjmJEvjg/h8h/Q9k+PZ89kLQ0ZUAX+V/9vC3mNDUwvKh8ODg0yI9uZ7B0xNTFoOk29lePqTiC5ceG+WgVUzWqRfbRX0Deq9L0DRwCelqTAk23QIzh6L6dDtMPKKJ7EsSqMl+MB/J6irLE1hesGvNlUlPbH32sbRioWY9PSN3K9aoeh7zRJxJm6syA/pmkRg/7yrzMA7b7Gl0aj3mdI/usAkOIpFtrdtkdFmRjojO6QJM2ZflOwxjgcMDz0n9ie+OnXHh/mkDUi31qvttY7xZ+DYkdHqJBFyGIuI158RciMTYhhnwtBlDlR85dT+NxsYrbrpmEzzDS0CpOVXokv9omGs6Dz/dHuHeYU2ITgbh/HvQ+BzJyCw0qhplkY48WhYGGgjbUjgOQxdW/LnzzgfgFX9LPKBW74B9p+e6k6NJD/LUzUR8pIRy01fB73AeMrv51uyT7hq2/ohFxJFzOtFERndgr4+6frbIQNZIHB6qCfxKxB3DkvNAAgCuiUTPR3W0iZf3dIqWegbfHGkg0em9EY2tQy6/QugkveM6giUM9w163PGJMbl7MohGZD/ljGP0mY+CdW5ajFZ2W89s/33+DI0jMrbovZnh/4INQBFoC7zvWHYH1KHI401S+1IlMHgVNcf/Wn0Zaznhwo/RpgL+03qABfk4juhdOO9aUzxhsubQ2VC+c+z0idQO/Q7UBBmR28fKYV1rWWP4Whd0Em77/EiEPEYJIVW9fw52RAKh1sMrGOsx7lfDaGBWH1RKpuXCmvNeh8MCZAQFAAA3l9JyUS+43JdjUiSQnrNRg6zHtqeQs6mr1xBaTWJpbHsaiAZ5cdjsV1JPva6IdSNnAVJBi7xqSFv7y1pZNeI4XkFBbBns3w+Gm4NGNqspOtysQh6yAnuyHXAf4dPDNWEA1f4Il4X/yngIhKHSIeISzDH9qizV mt31Lrdf 6ubtevmgn0Y6+S+57As3chgm6EPFEb3dsZrKd6JhL6VJd+ppqs7+Og7h3l+VukC9NnggJT0G1tcYfsxOFTkVrE7MQza4Vzhw/H7uU1eJjyE3zqGpitCRWhMToOtBJNOhxJxsBontMHqc8GlN4im2/dt1/VFYyZto7TARC0DVNbfHSA7CfQmgj3kDEpNc4sQ5z0fc/8QLONkblpxWOY1Vs2RDPTSFpUyyfvGmm/eD0B1RXB7a4sclyqO7hhZ2evqCYhbkw6S3FHFdlZBPUojPIw14qYbDnMnlkhCdHR2tT2nrTS5l84V4R4uhsOooxyY0Bqpvqwlkr7btvpwiqDpWjWObZKjiAP0O1fAN/zzgIOEHh5XXOrJfuexD9RDROrZ/5x7o7WcmPA+UlPWCV2oXUb7PcOnzo9Mbh5OG/V6OrwZYjd8CqnKUGYdsZtlSVIAo2pJWmIMjVYb6YmEkbibllWXhuNIwd/d+SZLIvlESrHDYsN4T/ydpOf6uYDZ2kjADK56B3K8urz15s2wtKopZQAXfwUB7kzwHh9ZwilPOzbqo5dhxCXGX/frM8ycvfyGpIFPDzv1zSOELjv12WiDGQpW4CNzM0gDaIOsZI2dJ5YNcB2kpLm5lVRM1JFAO7aFvGR3S9KVnwc+4HNzM= 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: 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 Jan 17 16:29:58 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13943559 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 5CCECC02185 for ; Fri, 17 Jan 2025 16:30:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 4274F6B00A4; Fri, 17 Jan 2025 11:30:33 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3D4DD6B00A5; Fri, 17 Jan 2025 11:30:33 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 24F276B00A6; Fri, 17 Jan 2025 11:30: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 004706B00A4 for ; Fri, 17 Jan 2025 11:30:32 -0500 (EST) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id A3A78C05CD for ; Fri, 17 Jan 2025 16:30:32 +0000 (UTC) X-FDA: 83017481904.27.D561392 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) by imf28.hostedemail.com (Postfix) with ESMTP id B987EC0008 for ; Fri, 17 Jan 2025 16:30:30 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=cw7WDe93; spf=pass (imf28.hostedemail.com: domain of 3pYWKZwUKCB4N4554AIIAF8.6IGFCHOR-GGEP46E.ILA@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3pYWKZwUKCB4N4554AIIAF8.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=1737131430; 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=shrLyHvMMRQKlPtYXYDSAaDe6YKxFfwFxlxNmwxqD3g=; b=sSlVlpenNui+ihtbrXTMc2wxhoMzaNJZlwReTVVguuQjlmIKTgiI1MRjq2lcYEiEHrJcBA qeqypOb8bUDbGb715AYgI6n2DMPXQWDfy1dVDx5kgPDgDwZ5wdvh24dzh/eaka9Q7eLT3W IWIO2Ft4/LsmRzajpekrLKyzxFcWhqA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737131430; a=rsa-sha256; cv=none; b=tmTECPWNyJZOsj8HfDvt1Xx7/TwADBYrO/CXGc0DNy4ZQAP7tkI5bUUsWe4v6W7H9WiBcm eaXUvUmFMHQccSrbCbk75C8WKvBFtct2w/zViPRa8R+FhA6W04R97XlDf9FkywGRF/Mx1x tziriBwP02F2nxANap085y0KphSteRM= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=cw7WDe93; spf=pass (imf28.hostedemail.com: domain of 3pYWKZwUKCB4N4554AIIAF8.6IGFCHOR-GGEP46E.ILA@flex--tabba.bounces.google.com designates 209.85.128.74 as permitted sender) smtp.mailfrom=3pYWKZwUKCB4N4554AIIAF8.6IGFCHOR-GGEP46E.ILA@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-43621907030so18987345e9.1 for ; Fri, 17 Jan 2025 08:30:30 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737131429; x=1737736229; 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=shrLyHvMMRQKlPtYXYDSAaDe6YKxFfwFxlxNmwxqD3g=; b=cw7WDe93zX0XHV/8biFz+LJ+2AUgoZOFWVx4XUYm1dllvbPfd+X1vEY5XL10QkBvK8 2VFFAOYTifkm+tvhy8ng9BTmob0P7eEHSNnboWJqX49PcKXAjgAEWprRWZErsSTIomvM ITMDM7CtzO/pmSdDF45Vndf4UIoF/m2fWrOmAq/Qu60b2xdC0C3YWMVIvfGy+HCLICQj 7fgOY9eQL6j6q+CknOZDz+Ro2KrAVEncmnfbHcqkYzKEvs/6xRMZhAFTFIgZ3JUvz1zr LfRcYX10Y5b5RRvOOl33Ck88iL8sqW9ia6leh4vDT5PPIBmvU2Oz+FyVAVuV5jkwEWsA LVbA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737131429; x=1737736229; 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=shrLyHvMMRQKlPtYXYDSAaDe6YKxFfwFxlxNmwxqD3g=; b=Z1n2HCw7tSV9WKXn6yCPfT569HwuPoRk9QVcqEBpaazKw+03uGkG4r+4TpGLxoWANG 9Ya12f+2LWXeVcKhTMu/YT2Ub1+VivZcLPaxUFNFFw3f0ODkopd8VepFLY8F3RZbynL+ +N6RXGOaoa2weRl0jaod2ODeICplkoWcPRTY027RGQ1J+zx2u46VGyH+eAe6xq+PHZfl 2iikc3nF0jBi7ZOwCsGB2A2J99NFZmIFSBm2RiFdYimL1z4PwEaOqOilH4nkEg8UaVpd W9k7K1dXUniMvPF3VzK08tSC8sPro8Y9H3x5eeqz6WQkey7Ei6jRPCDhDuJsdlI6z1I8 sV7g== X-Forwarded-Encrypted: i=1; AJvYcCUYMlxsPIWVjEkLW+6xRL8Y37qw4iJceUKDLU59IMuD5mNMkUVNPvNd43C2GBeGyIL8jITOKP0yTg==@kvack.org X-Gm-Message-State: AOJu0Yx6w3N2MSKLL7klNMQkTY7bdLy/X41KlnLDq6rVm7QGOkGHCPcf vNwGqzS4X+PXkJP81xA/JhYgjURFNtQIsDRICbvZb9hvCwV3zsT9I+v3uwbXfjmy7Cb3S5yNRw= = X-Google-Smtp-Source: AGHT+IE3/c1HcXzGSPchtf4vKV9TLTAsRjAq6kyeRfhWxPrR5PiskpFVLxhd84Kq6MdjNi2BKyWHIITf2g== X-Received: from wmsp9.prod.google.com ([2002:a05:600c:1d89:b0:434:9da4:2fa5]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:35c3:b0:434:f131:1e71 with SMTP id 5b1f17b1804b1-438913cf2e0mr36260775e9.8.1737131429376; Fri, 17 Jan 2025 08:30:29 -0800 (PST) Date: Fri, 17 Jan 2025 16:29:58 +0000 In-Reply-To: <20250117163001.2326672-1-tabba@google.com> Mime-Version: 1.0 References: <20250117163001.2326672-1-tabba@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <20250117163001.2326672-13-tabba@google.com> Subject: [RFC PATCH v5 12/15] 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-Stat-Signature: y5bn89q53jfgwg7sh745nb5k57yto9yp X-Rspamd-Queue-Id: B987EC0008 X-Rspam-User: X-Rspamd-Server: rspam06 X-HE-Tag: 1737131430-106759 X-HE-Meta: U2FsdGVkX1+d0hBK75wvJLuhFi7MW2bzGPoQGyMVk5R6XgAw1B0BU0k5FLPyQGQ622/58cjq50eIXGUV6DtPc+6zZ7diG/JEDRAy0400qZmro34rP8RM+botegHca3A6qJ40lJM8ZqFa8Kp2D3n+NMcntavqYsppLiPAo8OYuotvSIAC8qT2zqzGiZ8H6IVyP/RGmZHiOi7yoYmZnWMwy3meY/OLi8OAMcLaEiMocYudjmXSY3E73m4NaIa7K72T9vTcpKwXsAEGLBFLdL7RpvdDmWobjVc+e2+lj79zO+vcFy+YOsXZQ0HlCNK2zgXC9y0nnYc6yfMiMSN7HPtkkkWiGGgG6agJoq205ryKMwOROlx0tJ6wqZedqMcnrmTRQl4HnODLLCeWTxOfN1MWz/QUbvH7JV1u8xdzA1PsRhCTlgQaitTUrrP6mjUYIobHp1NjnIj+cIleSTYSv+v+cux3BwwnrlTk6PgiYqdUttkyOLDOonG16Uu2leEJ82lOW2p/BJLB9OMQSklOu+VjHzDAQ458MDgARxzEqi0CC73W/VrcPkdPeYxMvAEydhEOxa5B88rGwXAL3nvo/HsDwDat2yb4RZjMHi+qZOpLRekZxGPkiYjaJ7YFbRku4FRCdqtb8vUiKfedjqr0xEXT2pPvgNPh31SiCpciepjMssG6mLh5R7Q1AICex/zluil4L0XdKtANts04CO42nE34gP/MpIZFSADlQpGmHLXxerdAySaLVJkwz2DxWtufNQpz8X/pr0GJzl8cShCes6sx+uLk7wnIdwwxxtqrkp7JOaiFNk7rCWkf86R++nW6CjY7iNtj15i+CFHoKXPcQPnzFppomIEtQfF2Z9KAz54go1XW08B2c2M6xYEVg1BDe+8lyPCEmVF5e3cou4oeGcS6koIHR8tdyQr7yXmnzO2UIqdr2SajNBftWh2KLlpsau0r3MHSHkMPLIg1NhhNcH2 jAzR+jJp wnTktHd5ybv4KI2MQceX7TAQ/LQJ7OR83iu5aXzHs6Jmi5MYZP/ulBTp69tCiCofZ4vBcfVawz6OBk5Idnam/BmoWrflBC1X96d4Agk9PW9jKSM01SBooFGUIIUz4WwKdg50tZxfFIJtHcPOExHRooox7x4TVNNUXtoBeRRCi2I3WPOFQmSOtkZUYA1r/pOUZk3pvYHYZpx3marIN+HAKwB7hMb8tCcjHwMksZCPJMSbNiALlBSC99iX3cVpoIQLJ4xc4k3VYUDUeIsEqWtnnOKPFtZ/YtAOhfCUdjOc0hsAPeHuYtmKl6nYEZTQ+eAdWNtV2Nh9RKqTntHbfKWHfdllInmek9vdztTacP/V9ZWG7FfucVbnFUUo4Q/iZ/1/sclfgQthlSxPfHtSlu+Jhr9wTjyvBxG7uxCyjXDdTFV2TZEkheggljA2svRHOX5kRF86njx6Gm9zareN667hZWbnzxxsIpz+thJcKrAaRpWgaFil1/AFUG0TG7WzsfFWeT4JiEtSO1tZNHxJqjr4W83bAwpUctiyIYWCbfq+nAaLI6UgmCs3zsyR1oILDqIrj602vTh/cgvVkoxeuVa9q7dLpzbv1eCHaQ4kT 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: 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 Jan 17 16:29:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13943560 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 13151C02183 for ; Fri, 17 Jan 2025 16:30:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 69C186B00A5; Fri, 17 Jan 2025 11:30:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 626146B00A8; Fri, 17 Jan 2025 11:30:35 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 44B006B00A7; Fri, 17 Jan 2025 11:30:35 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 230E96B00A5 for ; Fri, 17 Jan 2025 11:30:35 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id D027512052D for ; Fri, 17 Jan 2025 16:30:34 +0000 (UTC) X-FDA: 83017481988.24.618E9CD Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf11.hostedemail.com (Postfix) with ESMTP id D2A9640010 for ; Fri, 17 Jan 2025 16:30:32 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=1n4haQ5I; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3p4WKZwUKCCAP6776CKKCHA.8KIHEJQT-IIGR68G.KNC@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3p4WKZwUKCCAP6776CKKCHA.8KIHEJQT-IIGR68G.KNC@flex--tabba.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737131433; a=rsa-sha256; cv=none; b=yXWEaWkel79evm+5briwbpetsNrfFBHvKsHxU0ONng/EGoI84XtxCcCQppJcCIX6VnKITo URUy/PWKQSh83M+dobw+KLvVJtNxUQsOYvWrrqjerd+uPzK3EgPPsLbSF4i1RH4fYI1GUB KFvRYra/OM7zvkKrZqAiOPSFQnAbbjE= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=1n4haQ5I; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3p4WKZwUKCCAP6776CKKCHA.8KIHEJQT-IIGR68G.KNC@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3p4WKZwUKCCAP6776CKKCHA.8KIHEJQT-IIGR68G.KNC@flex--tabba.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1737131433; 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=3HB24ys9olZtoFbzLvLFGPKqOLdaE1YgibZ0bUHAkc0=; b=eXKWOT3HlewZ06w+FrZxstYlG8eQNlVoGVohaxEPEi5In7Qb0P2IDX76B0/48ziI7/Mmyt lxYhPTD7CAywvfvsfAc/oq+LemlQK24zM9QyUpniwVaQC2up3cNVUJ1Xc4KQvvTvt9NKZs LuG/QGWelj/iH6SLC9bsR0emL7D9bxU= Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-4361d4e8359so17265955e9.3 for ; Fri, 17 Jan 2025 08:30:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737131431; x=1737736231; 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=3HB24ys9olZtoFbzLvLFGPKqOLdaE1YgibZ0bUHAkc0=; b=1n4haQ5IQSSKjY0PHgw4KJ5PBzA8tdcgCS1vdPI15Mmd4u8bEK67R0tU6ON83UCkk3 kwi/5jkZQtWJ6ei8s9IQAFlacur4X73NdiR2VBQS455ql4iuiq3NvnFHnQFt1aNIlD+h VjMd9wu/Ailbk+wPHlU1RDEQw7WLebT2vAYKQ4GVLdA6HbH68Zz4QeiuqFoqa5WKusMw snd4z2PyxfqpURQpNzD4H9sLtugnsNL7nblgK2KYseDCThTRsHKTxph6Y39Acuidk/vP kH6k5UFLhUujc6MfT3OdPUyfWpFWGj7J7R0tnQjAuk5kP3NrIYgPdCP0BNNye3j8LZ4p C3Sg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737131431; x=1737736231; 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=3HB24ys9olZtoFbzLvLFGPKqOLdaE1YgibZ0bUHAkc0=; b=AojbdLRpdmixQbHBQASpaZlVTVuzRLFl5sJh8sV0b+rMvUsA6PViWHpHdSqoyNuWZC KOl4JZorCzhQ5d6Y/Dxf8aKXzt9ZN0bx6FjNb7Px7EWTYzNAe2rqAGwp7GUn9a6lfyk3 RSgscFs9a+/Ufy63SoNr0W1DXg1MVYkGN5b3WHYZOwKuKZ3IhX8Nj06H/prLQnwn/0fl cRmemcQwGcLkw+IU7x9ddmc5U0E7S1HVaDBUgSrVjwijXU/kO8naDwR5/7pQisf+GkJv 3MQfeStR2qGTmqUsGUu+BnJnFnl253//On05DOyusj8CgqCIfjsBUvq2Q1nymsNs+b/t qM+w== X-Forwarded-Encrypted: i=1; AJvYcCUVDjF6fSbSkS0Mo7Ah4k7rad+oewl/iTRTcUuWy8B79YzlHKkWyITVYK3holqvKqzt3s0G/ILwIA==@kvack.org X-Gm-Message-State: AOJu0YxxldozXaTetEeJuWA4KuAJs5xflHZ0CPHn6iUvcPq2r3OEWRlH DBabkcvEN94Wrpt09iMTRRgLy2t0PBpsrs9gK+fdSnO2eCPbVjp3jLrl0YFm+T4Ns7F7R0/Feg= = X-Google-Smtp-Source: AGHT+IGXpNihxZ+huy1r8urGkN90UYrt/W0V/muXsXaxH9j2ly0sDzEqcBhbyJ7KDudMWMW1llJmatLXLw== X-Received: from wmsd5.prod.google.com ([2002:a05:600c:3ac5:b0:436:e660:a347]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:3ba7:b0:435:21a1:b109 with SMTP id 5b1f17b1804b1-438913bdcc7mr32672145e9.2.1737131431466; Fri, 17 Jan 2025 08:30:31 -0800 (PST) Date: Fri, 17 Jan 2025 16:29:59 +0000 In-Reply-To: <20250117163001.2326672-1-tabba@google.com> Mime-Version: 1.0 References: <20250117163001.2326672-1-tabba@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <20250117163001.2326672-14-tabba@google.com> Subject: [RFC PATCH v5 13/15] KVM: arm64: Refactor user_mem_abort() calculation of force_pte 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: rspam01 X-Rspamd-Queue-Id: D2A9640010 X-Stat-Signature: 4kq1t1xioxqk47hzaz5ymwqhtrguy8ug X-HE-Tag: 1737131432-307007 X-HE-Meta: U2FsdGVkX1+8ulA1DuipF1fC5VbDaWyPEwbFyMfWWcIdO5/rhNVycctIkHCM6XJlVo3qSzEyJzhEH3DmNX5QkcLuSmMCnczGv2gAi9gusxBTyoSs1B/dhP8xKoisW+pCg2GqvmYp8bECcxlC2SSnQnl2B+QaQWARKufYH4FIXZd5Q+Amgc71N3KtZfrq5DSlsEgJJxY9y4UcuoWZYaV0OERGULSqcOeInu6yaJsVaLKEWYoaneHWGiMi+gF1tWVjxsCfSW9KxmycPNxzWDGtto1KNTDHFASi/a1DZVg0EhvdaFrZh93DPgiYEsfMfUq0CuB8V3sIwU94g2ZRXFc/l7PGdsd8iIm65TPigCYjPwjEWJx7KV6MQ7TxbRMbVzE31+7tyAVlatOYLI+VmKKc7Qx7seGxY/QvUQbDS6VDIWY6hc8sHTAOuDmHekCQws7H4g9MH7mLcSPKPQhazmiK7gutK6R2FFucF0FP1bcc5dLm6Q+T/WknEfou6xf8mOhZLpOVSI90oz+k9YXJU52eSWc0QgiRzEEgj74MUlfIrmnHbvNV0Os3laknVkGuMMRJx/FVA0nO4YK7FPCrPJVKRfqxSmBZUyj7dpZ4xjYgxzK8Ywlo3z9hdGwcG6/EemhUUqwVDBi2cVFgbpW78dsNIv90sEQsxXa3yKtJUxfaR0QEsn3pYW4TOIVCx7aXjGRgQhobifPsAiXBU95qfSv39XgAKL4wY4oZG9mSHRjYZrqNxUK0D1/BgI5tBm7FqrNvi/QVbcr7tQZXz5jwOpJ/LfNr4RM1b1Arl1JDlP5M2y7UMezIJMyPhfR2sgUH8TqdEficz44go6RiXYbMg4UIEMqYoaNguSNkZCvgGx1mNYTahnLgj5Jyv3kc7UaekQ7Ks2r7gh7iiQChgGyZxZI9QAlnNVspbqyDOwOEct7djUd2D3ovnDJnQ+6W1wy/mIzkCJ8i6YPrKEkpwkUCawV RXoKZBvp eF359ckZes5zeJzsDsUloBWaKZfI08ZZxKFSSUiEL2sAxA3PwSNq/QDRk1a3NH81IrurcOszSaX1Ql5IY8aOfGjfR1DAj9Y2fSa0MffSgDO/faWSf7VnpExF110kptTGzEPCvyNMrajhHewS8thguW5cpaD+5Knao7IaCAFqb5y7bC6Ja1YesboA4h8PabsBLJw2RES1GIswLYrZYNzGvbQEf5ajXfdG/I7dimnq+gX1wCC91aLHLLZtd9uTmElmQUKAr0Bky6C8MbMRVdqRz3FT5l2/u3u3CvhcguzAA/K7dI2Wnd/Lg0Ma1M82qgctUyfvRCQ/2uZmiBdIkPIq9c+IvqPiw7nmb/YHceFPP6amkLcAmLvatMDDMla91w5ed8jTYDfYHPPy0GbA9ffhEfnI21HdCgBOfwmaGVZl2kBkW81sMUhpbzPKyZjwHsEjcwgk+oIg90VeuOaQcJAu1J6i1/KgcrAul1dz0a4iS3sb1fhQTdxztthnCZ9zZyFvGO5QdkeNxv3kiWfZTXSCuU83F19QQlXeLCEBz2XXEqXaesNl8myFloP5ynT4m5BbN/wBj0gO77EfxmrfPyRL4YjHlp2oYfGCTovgS53cfifc2/6yoLwPlV39kj82PRQ0aedkl1W1AIC/HB72qLgEGsKO/cA== 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: To simplify the code and to make the assumptions clearer, refactor user_mem_abort() by immediately setting force_pte to true if logging_active is true. Also, add a check to ensure that the assumption that logging_active is guaranteed to never be true for VM_PFNMAP memslot is true. Signed-off-by: Fuad Tabba --- arch/arm64/kvm/mmu.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 342a9bd3848f..9b1921c1a1a0 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1440,7 +1440,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, bool fault_is_perm) { int ret = 0; - bool write_fault, writable, force_pte = false; + bool write_fault, writable; bool exec_fault, mte_allowed; bool device = false, vfio_allow_any_uc = false; unsigned long mmu_seq; @@ -1452,6 +1452,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, gfn_t gfn; kvm_pfn_t pfn; bool logging_active = memslot_is_logging(memslot); + bool force_pte = logging_active; long vma_pagesize, fault_granule; enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_R; struct kvm_pgtable *pgt; @@ -1497,12 +1498,13 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, * logging_active is guaranteed to never be true for VM_PFNMAP * memslots. */ - if (logging_active) { - force_pte = true; + if (WARN_ON_ONCE(logging_active && (vma->vm_flags & VM_PFNMAP))) + return -EFAULT; + + if (force_pte) vma_shift = PAGE_SHIFT; - } else { + else vma_shift = get_vma_page_shift(vma, hva); - } switch (vma_shift) { #ifndef __PAGETABLE_PMD_FOLDED From patchwork Fri Jan 17 16:30:00 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13943561 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 17A67C02185 for ; Fri, 17 Jan 2025 16:30:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9CC1F6B00A7; Fri, 17 Jan 2025 11:30:37 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 956216B00A8; Fri, 17 Jan 2025 11:30:37 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 77FC56B00A9; Fri, 17 Jan 2025 11:30:37 -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 415946B00A7 for ; Fri, 17 Jan 2025 11:30:37 -0500 (EST) Received: from smtpin05.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id E300E80520 for ; Fri, 17 Jan 2025 16:30:36 +0000 (UTC) X-FDA: 83017482072.05.BC8F1DE Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf16.hostedemail.com (Postfix) with ESMTP id F0E4518000F for ; Fri, 17 Jan 2025 16:30:34 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Yl7YMu68; spf=pass (imf16.hostedemail.com: domain of 3qYWKZwUKCCIR8998EMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3qYWKZwUKCCIR8998EMMEJC.AMKJGLSV-KKIT8AI.MPE@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=1737131435; 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=XhTbcufgZ8AmBiYfbJkpLsBTfzEQgi+lO4KPsoUjlS0=; b=ALk9JJOUBqL03vRDP3jCxYPhJ2qZck9ku2NP24Nclb2vKgc2NO2c6DrxVahtAHiRlCa5nn 5+bNKTWQSX68ZriVknbfWDApzfkE3xNrzKrQkqc/ExYW3pR08mzF+yHzwTAACDYNAHBXYv r7grqs3Esf+JygAz35R6Ny77CP0/7TE= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Yl7YMu68; spf=pass (imf16.hostedemail.com: domain of 3qYWKZwUKCCIR8998EMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3qYWKZwUKCCIR8998EMMEJC.AMKJGLSV-KKIT8AI.MPE@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737131435; a=rsa-sha256; cv=none; b=SkAzUF9rj83aIrJvYjIwd2sJr6pz5E8040Ulrc477ChhI9zECmcvJAmL0CCs5VESmXT3SI HWbDs6YMYWG+QXO5KUJrlYc3+o1NxNocoH+csggYBQd0kV+bFmVqNLSsNRJPJmlIfpDaWG tsrdByMHRhtLrG4MZoYuxTXMDi9Zr9w= Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-436225d4389so15551195e9.1 for ; Fri, 17 Jan 2025 08:30:34 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737131433; x=1737736233; 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=XhTbcufgZ8AmBiYfbJkpLsBTfzEQgi+lO4KPsoUjlS0=; b=Yl7YMu68AYYjBkP+Pyf9x4X2duhl8ciCEhQ+XZdN8pIkzVRk5dJCYrcCBx2tY3Fl1o augKjRKRaZiI0a6SKJ1qV5cu21OKAsGYxAw7oWwqyzT4XODQPO1Mb9i5lD+VOXh4yXv4 L9eJKVy+7MRG6zIedANrna97YSR7HIGnzxYxz2kb1fQjcBx08Y9tKxm8wht6WACHM3Ad +jZIAXMnzbLluCgPN9Bamxke/boU8ZFG1CUfDIHcNeXNRGS0/5z/+k9sVszx8Sg4aGVT esYKWFncCwlCHXCEuy7Vlw745/+8E5vbDuBhX4xF3mFy0xZbwXq3r9Vbcz8Q3z1m3XOb k6Nw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737131433; x=1737736233; 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=XhTbcufgZ8AmBiYfbJkpLsBTfzEQgi+lO4KPsoUjlS0=; b=dvX8cXt8WKbs1Iy2KRH/ofBrhEjSdRQ8cGlFK1AQ8f3D5SQV8yYtoEhjMVyNfRNj2C O7+/G4G7e0ZDtJEKiWf5DWs/AfETtps2D9x5muejA+Fkl5H2nkWpwA4tHFHX5L8SLMw8 iJNKOPqSCMUxvugeQB0GW5gSncZkKuDMjZPL9qiWaFyzakXXf4a1bsCM+i8AC5/gDQIU 4/Euvyy/J1RpLUFxlcUt6yOL/BpTB2mcvgMywCqEoW5uiDk6w9BEyzLIPAiNdEcOBBUd Cbu3WD6B/qG04cazi0zCN8qaURcNfvbh3wR5k0C5BYm2yyerdl6J53Yk+8qEhhKe9Ju8 UXcA== X-Forwarded-Encrypted: i=1; AJvYcCW7NCcQur1lP6le5CgEmN4U1CdeRHAniS3b0dTzHbyWE07ArU3L2caTjCbDvIHtJhXnWq0IHDbjRw==@kvack.org X-Gm-Message-State: AOJu0Yx+CIs9loDdV3fXu5TcOxsocsLnL9XJacjAvAg856GkvtAOBQLT 0TLoABjpmZiDi6tjD1pUd77eXYh0YbP6iY5zMr/hGMp5bp6Y42wUrlOo2gfDV5DTgkdNZZBR4A= = X-Google-Smtp-Source: AGHT+IFPdHeUQIAQ3HgrviyPTwLb4E9cZejUsbEzIa0xJyMDU0HG5sEo7q/ik0GAG7K45x/YUf7mmw9Knw== X-Received: from wmgg9.prod.google.com ([2002:a05:600d:9:b0:434:feb1:add1]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:698c:b0:434:f9ad:7222 with SMTP id 5b1f17b1804b1-438918d3bdcmr32291175e9.7.1737131433509; Fri, 17 Jan 2025 08:30:33 -0800 (PST) Date: Fri, 17 Jan 2025 16:30:00 +0000 In-Reply-To: <20250117163001.2326672-1-tabba@google.com> Mime-Version: 1.0 References: <20250117163001.2326672-1-tabba@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <20250117163001.2326672-15-tabba@google.com> Subject: [RFC PATCH v5 14/15] 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-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: F0E4518000F X-Stat-Signature: dbgarkrnd4gr3o7ao9hbtwaunyoabbsh X-Rspam-User: X-HE-Tag: 1737131434-789833 X-HE-Meta: U2FsdGVkX18tK8KrYti/JtFMYXp8oMlE371luP5+w9wAp7loCxuqLt/XpI6/PpJqGEnQQywR83c5Qd/hhbdwmT8oYv756Fw8JWgkrwL8//LtzkoK5GF+4wXZA7AG/bDPSkoBXBAcslLSzaoBhCf3Y1oF4JyE2PduoX4J0MLq9AFdJ9IFNauXyIutiiVOQCcWcz6b9ohmICMOiZQFmAteYh/d+t8LQO4Y0mf15NEgjjbsoqVaoNuc+80z1+5Jur3Z+L5vqHKtI6rn/mJoY5F1+Nwrm8bNv9nFtQHwGiDjx3BFd9GXcmwmIGrBEHc+IA72NIk4NLQlwi+6wepycR9shn3JeIQ6f0ZKHBTPIJtFB07FZ6V2Oy46+TlzsvhpVLQZ67kGSxbgst2hX4jdK1CuPq1D4YpeBTVlD1gFKYDU3PD5JMD9lcj5hiVIQngglcfPa5ydRvnAnpIEMzJgfzq5PHCfTd2koVh4uLFVOD5VQPNC4YjlKrYm7L3iR1woYn42wgGUabvdJs+fmOioMJhv+GdWf8eGcZVKfjPCB9mrUdjJMv80q8BDcSLaZHu5+4eZoXMfPjVNa6+dgKvlnBxcqMuiuqbEwJEWetNeYAB8mIrvy/K/Fp6imC2ltuCv9FhHNClOXMC4r7GWQw4It7HxGCk/HEJEUKstwKru58cUMo6wLznVK8S9W5HaBkU8wl5ZbyNn6NvAWfQQmBtx0p7KXogYzkBwrfWsdvAWHSbpRGN/aHmjtJlgSAZRWQSgIYS2KeFZccmlC8W82Q5BAURs5dXdivxkPJ45gUHXEVFs1KhI2mQfGANjfAQ+k3Tpxm5PsgM4A3GRT1cM6sS22KR6qO01DBviGquuN3uBaACT1YOktqmPr8LWuwMSm5mzsWqEUhdNVbGXuBYSTsqvMo6VK3fdKl1YKvOCaIf5V1icluhw302oBlu7P+rJB9zMxcwdKkcX4rrCxm5RIdIkj3R 2wr/7qsq lAMWNr2QDOdmRot0PXVqk8lF9Sb7A4d9f0xI3SZ6Bf8mMSfp/W0ngvX5vG1BkkaWNwFgGdFwlf6mUxX6H4BN3G4p2YoNCkzPs1Q/uHXKonhmy559WF9kiFTm/EE4KrDAj/a0FUSYa6yh6kWIbEHIEfh9XeaY2fAQiV14maSAWeHgptMT9efF1eR42wEUz2RARmPvQiTFi9kYvfBPrNt+GULkzo5hPFD/r8bz2hOJCAqSgPWee7QH7wjM7xlEbn7LZVVt7BkCh55xsii6fo+bWduU7iUtwPkJ5dodY38Fvxc64Gxn24w2yyFDHydwsRsl2S+9AcIvokgDMa3XSE2vEchUGPq3GKKkmVB3B17zXW2KxGVJpuypx/R91WZS7lGaoDyPRH8fQ07+2qFl5fgzT/b0+f+XnwPqSEEE1QObJOHZB0jV3rSjR6aLmciGQFFXyWH7InLZubXbo+bhnsPTaZrl6MRxSruJ0YFHJ7SovHB4VniH0RbfbQRZ1VFaG9aHJgvX56PAqasmgszksAuClRs81RAC8/WAmvRGJNOKfD14BnOc+gZLMpNSrJwahfcviEmZPXKtHZFZ5pcPaqyqAThNx1wdbCPheiUIWN8XVFvYtEJeCmdxdRT5KfOUHAsza/xRMCc3SX02UMZ/44miOgb3gFw== 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 | 86 ++++++++++++++++++++++++++++------------ include/linux/kvm_host.h | 5 +++ virt/kvm/kvm_main.c | 5 --- 3 files changed, 66 insertions(+), 30 deletions(-) diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 9b1921c1a1a0..adf23618e2a0 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1434,6 +1434,39 @@ static bool kvm_vma_mte_allowed(struct vm_area_struct *vma) return vma->vm_flags & VM_MTE_ALLOWED; } +static kvm_pfn_t faultin_pfn(struct kvm *kvm, struct kvm_memory_slot *slot, + gfn_t gfn, bool write_fault, bool *writable, + struct page **page, bool is_private) +{ + kvm_pfn_t pfn; + int ret; + + if (!is_private) + return __kvm_faultin_pfn(slot, gfn, write_fault ? FOLL_WRITE : 0, writable, page); + + *writable = false; + + if (WARN_ON_ONCE(write_fault && memslot_is_readonly(slot))) + return KVM_PFN_ERR_NOSLOT_MASK; + + ret = kvm_gmem_get_pfn(kvm, slot, gfn, &pfn, page, NULL); + if (!ret) { + *writable = write_fault; + return pfn; + } + + if (ret == -EHWPOISON) + return KVM_PFN_ERR_HWPOISON; + + return KVM_PFN_ERR_NOSLOT_MASK; +} + +static bool is_private_mem(struct kvm *kvm, struct kvm_memory_slot *memslot, phys_addr_t ipa) +{ + return kvm_arch_has_private_mem(kvm) && kvm_slot_can_be_private(memslot) && + (kvm_mem_is_private(kvm, ipa >> PAGE_SHIFT) || !memslot->userspace_addr); +} + 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, @@ -1441,24 +1474,25 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, { int ret = 0; bool write_fault, writable; - bool exec_fault, mte_allowed; + bool exec_fault, mte_allowed = false; bool device = false, vfio_allow_any_uc = false; unsigned long mmu_seq; phys_addr_t ipa = fault_ipa; struct kvm *kvm = vcpu->kvm; struct kvm_mmu_memory_cache *memcache = &vcpu->arch.mmu_page_cache; - struct vm_area_struct *vma; + struct vm_area_struct *vma = NULL; short vma_shift; gfn_t gfn; kvm_pfn_t pfn; bool logging_active = memslot_is_logging(memslot); - bool force_pte = logging_active; - long vma_pagesize, fault_granule; + bool is_private = is_private_mem(kvm, memslot, fault_ipa); + bool force_pte = logging_active || is_private; + long vma_pagesize, fault_granule = PAGE_SIZE; enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_R; struct kvm_pgtable *pgt; struct page *page; - if (fault_is_perm) + if (fault_is_perm && !is_private) fault_granule = kvm_vcpu_trap_get_perm_fault_granule(vcpu); write_fault = kvm_is_write_fault(vcpu); exec_fault = kvm_vcpu_trap_is_exec_fault(vcpu); @@ -1482,24 +1516,30 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, return ret; } + mmap_read_lock(current->mm); + /* * Let's check if we will get back a huge page backed by hugetlbfs, or * get block mapping for device MMIO region. */ - mmap_read_lock(current->mm); - vma = vma_lookup(current->mm, hva); - if (unlikely(!vma)) { - kvm_err("Failed to find VMA for hva 0x%lx\n", hva); - mmap_read_unlock(current->mm); - return -EFAULT; - } + if (!is_private) { + vma = vma_lookup(current->mm, hva); + if (unlikely(!vma)) { + kvm_err("Failed to find VMA for hva 0x%lx\n", hva); + mmap_read_unlock(current->mm); + return -EFAULT; + } - /* - * logging_active is guaranteed to never be true for VM_PFNMAP - * memslots. - */ - if (WARN_ON_ONCE(logging_active && (vma->vm_flags & VM_PFNMAP))) - return -EFAULT; + /* + * logging_active is guaranteed to never be true for VM_PFNMAP + * memslots. + */ + if (WARN_ON_ONCE(logging_active && (vma->vm_flags & VM_PFNMAP))) + return -EFAULT; + + vfio_allow_any_uc = vma->vm_flags & VM_ALLOW_ANY_UNCACHED; + mte_allowed = kvm_vma_mte_allowed(vma); + } if (force_pte) vma_shift = PAGE_SHIFT; @@ -1570,17 +1610,14 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, } gfn = ipa >> PAGE_SHIFT; - mte_allowed = kvm_vma_mte_allowed(vma); - - vfio_allow_any_uc = vma->vm_flags & VM_ALLOW_ANY_UNCACHED; /* Don't use the VMA after the unlock -- it may have vanished */ vma = NULL; /* * Read mmu_invalidate_seq so that KVM can detect if the results of - * vma_lookup() or __kvm_faultin_pfn() become stale prior to - * acquiring kvm->mmu_lock. + * vma_lookup() or faultin_pfn() become stale prior to acquiring + * kvm->mmu_lock. * * Rely on mmap_read_unlock() for an implicit smp_rmb(), which pairs * with the smp_wmb() in kvm_mmu_invalidate_end(). @@ -1588,8 +1625,7 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, mmu_seq = vcpu->kvm->mmu_invalidate_seq; mmap_read_unlock(current->mm); - pfn = __kvm_faultin_pfn(memslot, gfn, write_fault ? FOLL_WRITE : 0, - &writable, &page); + pfn = faultin_pfn(kvm, memslot, gfn, write_fault, &writable, &page, is_private); if (pfn == KVM_PFN_ERR_HWPOISON) { kvm_send_hwpoison_signal(hva, vma_shift); return 0; diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 63e6d6dd98b3..76ebd496feda 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1853,6 +1853,11 @@ static inline int memslot_id(struct kvm *kvm, gfn_t gfn) return gfn_to_memslot(kvm, gfn)->id; } +static inline bool memslot_is_readonly(const struct kvm_memory_slot *slot) +{ + return slot->flags & KVM_MEM_READONLY; +} + static inline gfn_t hva_to_gfn_memslot(unsigned long hva, struct kvm_memory_slot *slot) { diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index 0d1c2e95e771..1fdfa8c89c04 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2622,11 +2622,6 @@ unsigned long kvm_host_page_size(struct kvm_vcpu *vcpu, gfn_t gfn) return size; } -static bool memslot_is_readonly(const struct kvm_memory_slot *slot) -{ - return slot->flags & KVM_MEM_READONLY; -} - static unsigned long __gfn_to_hva_many(const struct kvm_memory_slot *slot, gfn_t gfn, gfn_t *nr_pages, bool write) { From patchwork Fri Jan 17 16:30:01 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 13943562 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 23013C02188 for ; Fri, 17 Jan 2025 16:30:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A8F096B00A8; Fri, 17 Jan 2025 11:30:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A167A6B00A9; Fri, 17 Jan 2025 11:30:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8B7256B00AA; Fri, 17 Jan 2025 11:30:39 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 669C56B00A8 for ; Fri, 17 Jan 2025 11:30:39 -0500 (EST) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 0C56C1404E2 for ; Fri, 17 Jan 2025 16:30:39 +0000 (UTC) X-FDA: 83017482198.08.2768BC7 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) by imf03.hostedemail.com (Postfix) with ESMTP id 2C3F02001F for ; Fri, 17 Jan 2025 16:30:36 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=tc0cWyCr; spf=pass (imf03.hostedemail.com: domain of 3q4WKZwUKCCQTABBAGOOGLE.COMLINUX-MMKVACK.ORG@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3q4WKZwUKCCQTABBAGOOGLE.COMLINUX-MMKVACK.ORG@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=1737131437; 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=sA2koc6FnlEdGg8+Jdk53M7JzqxYQGk2KRekvS8wCYA=; b=2z8vQqeTQYOxLyb3k/xq/O1SzBu/e4MmQlC58CU0elMPnC77J/2/AIuc16Wp0+aRlKcND0 oKvw8IWf8wyWKfGp3kOhDaK7jZnrT62OWqQr/cBS71CIKU0jzAAYkAKKC1EwgmRF0anmW/ txxrX8VqYgRzjvxBbhllJb+MB3SX+mc= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=tc0cWyCr; spf=pass (imf03.hostedemail.com: domain of 3q4WKZwUKCCQTABBAGOOGLE.COMLINUX-MMKVACK.ORG@flex--tabba.bounces.google.com designates 209.85.128.73 as permitted sender) smtp.mailfrom=3q4WKZwUKCCQTABBAGOOGLE.COMLINUX-MMKVACK.ORG@flex--tabba.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1737131437; a=rsa-sha256; cv=none; b=X3DkKSrWm7SKS4x6Yaebp9ouI9Hb8wJLGcRQNDslvdZx2PTtiB/lbIZsf0Wh32Ei02sxbF zPR/O4bK7vkWiamlaL10TP+PNonZSQC4bn00bIqrbgU/4YQnDn0TCp/AC6QgN+pB7cuheP 1TZZ637jmDJlNp1d7rgzk08td1XtIgU= Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-436219070b4so11027465e9.1 for ; Fri, 17 Jan 2025 08:30:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1737131436; x=1737736236; 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=sA2koc6FnlEdGg8+Jdk53M7JzqxYQGk2KRekvS8wCYA=; b=tc0cWyCrFGcuomx3TI4qRYl/TDkK4SVCFfyqyPkRj9VOxfot23yu/+cozEaHVYizU3 +ObHA0nZZmH1m1Jr50Mm0Yjy1xXaIjdcTIQRDC7YyBSKdWsCaVbHosF848FBhJm2eGxk +g+hRmKCztV8GYkCSZjc89E4IUIMxD9+6tDXjSo2zhrCMplJsx2czye9Ohb8sK8O9HZO atQCnWqKljwwtkYRK2hk09nsgLrOX7700OGRc9XAROcjHq6jdN/AcsAo+Aeyvb1GL2Eb 97TooWf+i0JidxvPTMwJ9tRwdLdVV9b3P2ra+jxVkUbUI9rsr7zBqqIwimH2wAi2taKA EB5w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737131436; x=1737736236; 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=sA2koc6FnlEdGg8+Jdk53M7JzqxYQGk2KRekvS8wCYA=; b=Tni7uw8DtVkfa2BU7qqLVQsk/frjrKNXZig+ELSeRx25PXKy5VpXpFUn3R5Hk6N2RP nACRRhmlWkVHs9G8AgObnqgjUHzq8pIP3D3KhuSr328uV7IhABWIikehQDOY6OjiGgF1 f7uIH7FDN/IOSOwmaaWlDPNI6D63Tk20Xw5Iue8Rr7J4KeN4aOoCJ4a4SK89DtL3QRmO V8js2w7KTYd5q1cIyKZD5Fm4sdxGfl75v8uiX3ovL1diUiErKTzRC7KNTodObPtbCVW7 IaVNiFgBq0Y24c7R/7xcMftiUUBh+xqt//ozQFnQ3DT4Ya/hmEEa5Bn9ddq3jjmgc/aC vafg== X-Forwarded-Encrypted: i=1; AJvYcCWKNB+JD3K6EmfOq/XvlMzQHdMxa+XYUPvWUmuLjMLF2dnUe7clgYwDL1RQz+NFq7mmujPg/ganqw==@kvack.org X-Gm-Message-State: AOJu0Yzmi4Y8Pvz0/pvLHaY76imt57OQIORa/No3L8u1IP35ZAiHsm8G C1sRb0UL9XGUosvLlpZhZIOXfNLWZWyLo0iSiVd9Ple6PrTCTu+Ut+107S73lqZiNSKg6PCO4Q= = X-Google-Smtp-Source: AGHT+IH7ncc07mfLHgb8ED3VwsB7jAbdBmUiBlZXXQcjVPwCGbKKIuWYuOy6q8Z5dH2n+Fog8Taxczd8hg== X-Received: from wmrn7.prod.google.com ([2002:a05:600c:5007:b0:436:185e:c91d]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:5486:b0:434:a10f:c3 with SMTP id 5b1f17b1804b1-438913cae48mr33877825e9.9.1737131435727; Fri, 17 Jan 2025 08:30:35 -0800 (PST) Date: Fri, 17 Jan 2025 16:30:01 +0000 In-Reply-To: <20250117163001.2326672-1-tabba@google.com> Mime-Version: 1.0 References: <20250117163001.2326672-1-tabba@google.com> X-Mailer: git-send-email 2.48.0.rc2.279.g1de40edade-goog Message-ID: <20250117163001.2326672-16-tabba@google.com> Subject: [RFC PATCH v5 15/15] 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-Rspamd-Queue-Id: 2C3F02001F X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 8efwfty5smcp54i643sjta5dd4t5uoy7 X-HE-Tag: 1737131436-873399 X-HE-Meta: U2FsdGVkX1/kUGDPpon/hRzJR2v8zVA8IFXwfB2ut0K69VnL3FzyJAZqp1Z4wWJtscTYup3y7hc3Z5VchmIJDF8CReS/qIIVzDmogRUe3yYIDNK8kAutyx0RIZug5yube9SAWhroiHDjzy3trrYHIxB7ma80A0DadQX4hMUg6WeR/x7x7f6zNy3/oPOyTSDvvUnD0ewZjVzXa14ELv8e4pSb/ojwWfKlTNpan4CQ7fLqS+mswQ/6l3u2EPVNxAqKNuH52hISER9yJmcnooqbF7y5z31vH4Bf8uFln0thdSx3FrpE+8cgIQLejvBpL8MXEgivfEr4RhGqa9oN/gGSS+XQTGE+4nWH32KdCOHIIF7nHa/k1+bYNAfh/8sTdUabyS6oataEI39L3xPpT86S5VvFjwaG9XcwjcpAD4ZzVptilEkgSaaFcZkdTVtVq+dXD2NVEDv8uOpDTrmlvzIuqZMsQ1wptpPaTnFj2cWqyHq84yr5Qy2xaFnk1BnOI/W+PoSWrJO/BbHIsqaitQF64pV9IXWPQxq2LUueYz3I4G1FKpe1ofIcmVh9LOJow5h0E7apa+dHSndThvnJfhJ6JwQF0oQHQ2HmIDhvrBF3jCeJ9feUJ3qdExPhjE8hVruQdG9OM7qbhPU3YLYK+YI/v2oP73KntDwPJkwZnz7uN19ppS++ergOqW6QM6w54nXgOEuRS9GmheXxVOtRQYZMGGqdmruIRVt4fjuGoXWIWvsuVB9Gm1mJXg9nQWaumdeGQUXMQX4gKygP29LNa9sDYtQjJKkulB3RUCgy9cYe4htw8zc0tWvvvu2J5a7kbtlSMPjSnLyBrgadU62hDGB4V+5yj5IcOOsTZnpesQ2iH54w0H9z/k0+Ban91b/4zYB5IITJd81Vdy3hkWt/RzmqxYOpV863eFk1D6TMzrP4bXIhyM6lUuxAOBbGJJaUpFiQnhh4N6Ca7Ed+EBaDuYy v3qNnUeR xzJamRhU20LkZDbZL0PAlQlVKDztiqxOC7YUIpFYcC7IgQhPIF5+NN4Z0HBf+UDUSsDXVMhIFNvFIlHQugNhyDMRQR1OWFBBpuS3bO7uZ2Rb4vH8AcglG05Sr4lymuOqtwTYve1Fxo+XQJyWafrHsHizubjnAJotIQowZD2rXPt+FE0DNWt1cSUDh+bWURknGSWxeJlkoAB/MRvznOc60OEctJYYcixv2YS1Ata9S8Y/iPJqHZB8eBjGK35AqpZcHAtttGCe3+aATiuLf1kXpZmSJYkUIUm2u0u0wgFVBKJfj/ml0p/ihvxgEDZCEn+0YUhY79pZ1M9mnwjwVLeiOj2oToit4YE2jIz257GUMuNlQnT8nK81M33ShiE8AHJPgE0bTdkICFQxQ1VqZDgXW0E/3u7XuWStWA1mQd1+QQnkrKKc2W1/6zIUANNsrYyxG+f+ZSFiJ90VDsXmec3PW9WMpDrTwSaYNgQkZ2E2HQOa6mGJoMtMH79gxIPj26mrwND6RqKkGLzw82tAsb5c0QhxopMFCGu/+ckzqKtcuLbgcUr8PJX60IuvOcDfsVI0oH2uKHY/3tfT820K4+dcN1bmC/trxol+cFSWRrRSVmYMzGXadSK8IocQ28XWXggUUnAR8qS618uyYVM9i5uDFNkngJz5EY90xlY8vSeqfj5thmosYMRa5VtSZT7Wiv/KRuLUY 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: 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.