From patchwork Wed Mar 12 17:58:14 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 873595 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5FD801E8325 for ; Wed, 12 Mar 2025 17:58:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741802311; cv=none; b=LbjNHcGZW7QfS6AZBfkeHAGAn6s3Bn+szGLAokivixeWqyGpLtwmYc4SsLfzW71NVNDmW0Y7bfDoAZlnhGJndACVTCf0lv0tqSgDXVAcXjfv3JiF2WemkApkkVodzEqkDffmppScPF7o4skWn17V9r4l7SWRuauwWX7fpNgzDvY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741802311; c=relaxed/simple; bh=u+cSBS6/5HVzpl9qyVlXKlFIoi4sTEN5V0QcfIMXDLs=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=SYM/XPQmctK3IZOGwT/yNHzz1p6m0shLEBifaAfKD2t6trilAXySvEVb4XZKhn5/icTlScozmEezOiFXKANNvtwgvo6jC2kwpBA2lvjktTdGvcSE24LJ1pD0UwWX3eaZ3rNK95BlO4lHVaDGqhCPw7jf0/3HcD5S5y2TJlndDmc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--tabba.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=HsKnEIQ7; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--tabba.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="HsKnEIQ7" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-43947979ce8so696265e9.0 for ; Wed, 12 Mar 2025 10:58:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1741802307; x=1742407107; darn=vger.kernel.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=a0XpxYSrLEkx+yIzSfmPSimPPvYP4gD4dCRRx7Mg8GE=; b=HsKnEIQ7zBXODMNJ4dJ7IZSwy+GMFFD54NLCTWlb7Fsv5lSV2Er0RpCeDsxkEa1MTw 53EuDQdlnv+lWQUBAG0rvNGjs6B3vkjyID5MS8hs/Jna2QMGyijroEsZFVA3I56AYSlT xS4MoGH1DKzt26l39cbpLl9Gb5piLho/WDfCT5tBqjw3UxWbJeRy71bX98OM6xhYVtXF uirLP4zKKzUKra/OBWzhxNJN0eBcXGifrhufzu1LKIUpHRBzu9aSGvMm2vRxtWMcO/I/ qFv5wqQVpogvKEX8wZ6EaV5ANEAIGCox5VsglEcIXOQgpA45EUu0VZZBmSPRuVeB6mmJ xJ6A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741802307; x=1742407107; 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=a0XpxYSrLEkx+yIzSfmPSimPPvYP4gD4dCRRx7Mg8GE=; b=rRO0qDEnYPR16y0klOynvwyQw3r8QEizi7Dy+xRKVHRBRAzTYWyyGrQNy2yCQPZi32 65MkbwvlPDLi1x1IGKINmeknfgyEmr4lFfYJh5LCnXW5UAjt1iOEy2RrqdDE11dBZOML 7tCVM4CtVyglpcRwJcEd9yb54WaEB1205HCioH7RkHJkti09fJQUxIc/hmFEenUQBR+c TfgWV/Z2taMj7Icr0ewr5Ar+qogAhx6nqh9evjJkfM8ztyABpCw7cXuBMFXohrHT8itW 2kXwZR/1x+9/h+iY75KpiN5Z+vr9Rde+NVtQIWb1j4yXuDKjlZp1tNdryu6ADZ+g1nDT 1Uaw== X-Forwarded-Encrypted: i=1; AJvYcCX+wT3hybez2JoZ/unTLGkOute/TDRUpELkVOjesfRzquLaOks1mo/mY8mFROpxO3//uJF6PFx+wec9hHhT@vger.kernel.org X-Gm-Message-State: AOJu0YyykRPEJICijJzPzgS+REGH20Z7xiUUnyBBm8xGfekbnA6d+/8V xvYrX0t7UZNXs/bzoEjR01ZXzfvJcGiDQU7P6dXy+o6D+YSi4F/T2ADvdFVDwO+TlwBeTGj+4Q= = X-Google-Smtp-Source: AGHT+IE5UDnnh3yo17yLxT5VX1BO5KQAMs8ianrYZo0AfgH0k/k4UkL9Aw8NmMGZsi98ECarhiOo1EOQ4Q== X-Received: from wmbfp13.prod.google.com ([2002:a05:600c:698d:b0:43c:fb36:c419]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:3512:b0:439:8c80:6af4 with SMTP id 5b1f17b1804b1-43d168d4625mr1074115e9.19.1741802307671; Wed, 12 Mar 2025 10:58:27 -0700 (PDT) Date: Wed, 12 Mar 2025 17:58:14 +0000 In-Reply-To: <20250312175824.1809636-1-tabba@google.com> Precedence: bulk X-Mailing-List: linux-arm-msm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250312175824.1809636-1-tabba@google.com> X-Mailer: git-send-email 2.49.0.rc0.332.g42c0ae87b1-goog Message-ID: <20250312175824.1809636-2-tabba@google.com> Subject: [PATCH v6 01/10] 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, 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, peterx@redhat.com, tabba@google.com 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 Acked-by: Vlastimil Babka Acked-by: David Hildenbrand Signed-off-by: Fuad Tabba --- include/linux/page-flags.h | 15 +++++++++++++++ mm/swap.c | 23 ++++++++++++++++++----- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h index 36d283552f80..6dc2494bd002 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -953,6 +953,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 fc8281ef4241..47bc1bb919cc 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -94,6 +94,19 @@ 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)) { +#ifdef CONFIG_HUGETLBFS + case PGTY_hugetlb: + free_huge_folio(folio); + return; +#endif + default: + WARN_ON_ONCE(1); + } +} + void __folio_put(struct folio *folio) { if (unlikely(folio_is_zone_device(folio))) { @@ -101,8 +114,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; } @@ -966,13 +979,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 Wed Mar 12 17:58:15 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 872849 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 307B72571A3 for ; Wed, 12 Mar 2025 17:58:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741802313; cv=none; b=Ot32qkb4puG2i3A09sCDpJSagBcFjUgA3lNSnSEFw2I4hj0rYbIr+XsFio3ZnSBNRLDe7zpra5hIhtjF77BY15ZJ/5lRfd/VDJ4AbmsD3md1FzqWIWPZvLu7vOpI16mV0rmaeM8nCMuP168PU67FFMdLk1dpPN2fvvrx14/SWdY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741802313; c=relaxed/simple; bh=DE5SQ9QQ7pg1t8bcHulsVBC516s9NBbF9b2TCSRWhrE=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=jiKezM4xVy83YpGCrnHgZeN1MqLkFTpOkdqi/UK9zSQFn5lOOOiY3EoCCcf5tgn7dXhg+A6R2u6yPNmrLNphdfjUdE8ZpTouRSygjPvVKfVT0z0judpyAD6JBEKtbXQsSs27O8Z7VaCBr8nnSgbHlVqqNvpoqt09CpSZCQEYX+4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--tabba.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=YVlrTEnv; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--tabba.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="YVlrTEnv" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-43d007b2c79so431945e9.2 for ; Wed, 12 Mar 2025 10:58:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1741802309; x=1742407109; darn=vger.kernel.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=ZH2ddFSySXL8ljh40ScGRRkvOagkKQt0yLw/o+/C9xs=; b=YVlrTEnvS/mArfraNJ6Njg2NDyWAtR1pXL87nKDvWL5UUSPv4V8hU6jHRtF6rUEiWh AsHyAOAnPFJugpJdCkf+2xZH5OahStVcM+lEhxOBPvgYZJWXKO4AL8lWs1oBLho5Gm9a CCTxSRwlGRIw2lor+5BLy03y3fpmoccu7p5PDG2mYeVS1WFd4LlvshZgHmNwwqqHz2sD 15GYzNdqY5/HLtGGJhYhKO9VbYu5BVJsjMbX+Fa5okuNIhf60HnMdabF1PoMxvLFAm1q d//i4Xun91Hg9ANJXRihMQ/G/oFjExMXzfTKJMLDouPTaDRap+qMCbQ7hG1S9NP9BmYP c4YQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741802309; x=1742407109; 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=ZH2ddFSySXL8ljh40ScGRRkvOagkKQt0yLw/o+/C9xs=; b=m5e9FRx+6siqqI7WIbC9qdgnSVLOmPKrsTV9GjNC6g7U4pjEjAFW5ntb+gxXjnoDUq o33lIXoYYWNN26u5yu2X3x+t+peIP0WDbL8CMFYZ6Bk2QqYLHW1D9zHnqa9w+/nlqYP1 cM4zXb0zJAVdrgydp8DPFByVkdzxaHBzgaXoMk2zEidsdDnpAekDENsU0+wgOdyMQ0YD OKe2V0eV9pcomyeG68Y6t6GPdc4gDIV0Lb4wQOmvCc7Lc0Fy0ksJ5qiJjJ1oWO4SQ/Uj vxhcsKmaBJ0v5R/BB0/0ZhLajJ9Uor70bAc5SETQYL2/pA9ZbfkwbfRe3mDetnzHr0X+ mMUQ== X-Forwarded-Encrypted: i=1; AJvYcCUaqYH96Sg7YA/QXw/aL+cN3kqLdsdu4ltvpqfFLYf4e5Y1eq8/huyT4hmsbBjM6xoO9d+6ZOXDHw4ONq+c@vger.kernel.org X-Gm-Message-State: AOJu0Yyh2joE8wXuM867hKUP32JSLB01A0hMagU+W7biAtvLPBIvNlg0 6ERA0va+jUMOOAPD36ZOlrwB3NGNoKLFLL9enki7gs4n5gqh7toZEQxeNpAvn3gQvSfBWHSkTA= = X-Google-Smtp-Source: AGHT+IG3QL0FD0w8QJ+6eYUXr0ym7H16e0KnHlntRiSz0AzMSuHdXLBjD/jyLDhLgORTX5AXNY0xHban7Q== X-Received: from wmbgz9-n2.prod.google.com ([2002:a05:600c:8889:20b0:43c:fcbd:f2eb]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4f90:b0:43d:fa:1f9a with SMTP id 5b1f17b1804b1-43d01c22acfmr99270665e9.30.1741802309618; Wed, 12 Mar 2025 10:58:29 -0700 (PDT) Date: Wed, 12 Mar 2025 17:58:15 +0000 In-Reply-To: <20250312175824.1809636-1-tabba@google.com> Precedence: bulk X-Mailing-List: linux-arm-msm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250312175824.1809636-1-tabba@google.com> X-Mailer: git-send-email 2.49.0.rc0.332.g42c0ae87b1-goog Message-ID: <20250312175824.1809636-3-tabba@google.com> Subject: [PATCH v6 02/10] KVM: guest_memfd: Handle final folio_put() of guest_memfd 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, 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, peterx@redhat.com, tabba@google.com 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, which isn't activated in this series but is here as a placeholder and to facilitate the code in the subsequent patch series. This will be used in the future 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. This patch also introduces the configuration option, KVM_GMEM_SHARED_MEM, which toggles support for mapping guest_memfd shared memory at the host. Signed-off-by: Fuad Tabba Acked-by: Vlastimil Babka Acked-by: David Hildenbrand --- include/linux/kvm_host.h | 7 +++++++ include/linux/page-flags.h | 16 ++++++++++++++++ mm/debug.c | 1 + mm/swap.c | 9 +++++++++ virt/kvm/Kconfig | 4 ++++ 5 files changed, 37 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index f34f4cfaa513..7788e3625f6d 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2571,4 +2571,11 @@ long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu, struct kvm_pre_fault_memory *range); #endif +#ifdef CONFIG_KVM_GMEM_SHARED_MEM +static inline void kvm_gmem_handle_folio_put(struct folio *folio) +{ + WARN_ONCE(1, "A placeholder that shouldn't trigger. Work in progress."); +} +#endif + #endif diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h index 6dc2494bd002..daeee9a38e4c 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -933,6 +933,7 @@ enum pagetype { PGTY_slab = 0xf5, PGTY_zsmalloc = 0xf6, PGTY_unaccepted = 0xf7, + PGTY_guestmem = 0xf8, PGTY_mapcount_underflow = 0xff }; @@ -1082,6 +1083,21 @@ FOLIO_TYPE_OPS(hugetlb, hugetlb) FOLIO_TEST_FLAG_FALSE(hugetlb) #endif +/* + * guestmem folios are used to back VM memory as managed by guest_memfd. Once + * the last reference is put, instead of freeing these folios back to the page + * allocator, they are returned to guest_memfd. + * + * For now, guestmem will only be set on these folios as long as they cannot be + * mapped to user space ("private state"), with the plan of always setting that + * type once typed folios can be mapped to user space cleanly. + */ +#ifdef CONFIG_KVM_GMEM_SHARED_MEM +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 8d2acf432385..08bc42c6cba8 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 47bc1bb919cc..241880a46358 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -38,6 +38,10 @@ #include #include +#ifdef CONFIG_KVM_GMEM_SHARED_MEM +#include +#endif + #include "internal.h" #define CREATE_TRACE_POINTS @@ -101,6 +105,11 @@ static void free_typed_folio(struct folio *folio) case PGTY_hugetlb: free_huge_folio(folio); return; +#endif +#ifdef CONFIG_KVM_GMEM_SHARED_MEM + case PGTY_guestmem: + kvm_gmem_handle_folio_put(folio); + return; #endif default: WARN_ON_ONCE(1); diff --git a/virt/kvm/Kconfig b/virt/kvm/Kconfig index 54e959e7d68f..4e759e8020c5 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_SHARED_MEM + select KVM_PRIVATE_MEM + bool From patchwork Wed Mar 12 17:58:16 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 873594 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 10FFE25744D for ; Wed, 12 Mar 2025 17:58:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741802314; cv=none; b=nZ/6Yt2wj4YR6j8Zaff23egKiVgmGcmHZkui/jBzlJN/lLD/wMi688Qrky4pTn06Z1VnETqi2MhiZfb07MsmUqYd6J9cw+HDNnECAnUBbuAEOIs7cCy+3R73d4u3sSJYPRdYg/DbmSCZXTTWhgSbOplRJjxgrChgTP4CoCNFKgI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741802314; c=relaxed/simple; bh=3FS0AjD6bP0RaVtYsMwEFCHIRnAMnaOeO0UFnDNd6iI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=VnOuaANyeTeHZV16WVu6QlIimhTUgejbH0KR8u+eBblxGGw1w8NB1Z2onDKNON7ZwVw35m4Yr2JUNgP8DECRVs/QNyCKKr3JRWyrCXFMSM/grhR/H76/wXO16uxM1HLLcy8YOINwl5rRaa57qM9hV9fnx2Q8nPfQPFBEYWJgn8w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--tabba.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Y66iuVne; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--tabba.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Y66iuVne" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-3912580843fso81278f8f.2 for ; Wed, 12 Mar 2025 10:58:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1741802311; x=1742407111; darn=vger.kernel.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=R85+vuKuEEUS0jeCyrC0DyFve/WtnRZfc0oDmfyiJIQ=; b=Y66iuVneY6rtnVjbdhjsRoe1vbHBW3LPkhqIJUE4drpZCe8iQI8mruva9Q9Ks0YgcV y3quLskRJRbWTJtDDk3+fHpfkJF/DzZ1rK/YwvBf4ZpjcHer4v6ZueicG4eJFSM5iyvA sWsh6uI89N3zEyJuYp39OO+s+JBMkGhOKCtrGBtFJ8FYJceGj9gJ95qoV1LfKFuG3tQB BRCVSYbhmhRgajEK0P+aPiKiWhwo5S0Goooj6g3AqsjnC3oDJPg7hO6dMQ7RyytTuq6w u8YB7QUX6Bbd0X2U+fZWAzmCZhTt3c+NE1WtVGtoCgavnKAdYQpz67PTegMdVS6pzV1c 0TPA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741802311; x=1742407111; 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=R85+vuKuEEUS0jeCyrC0DyFve/WtnRZfc0oDmfyiJIQ=; b=nGmyUhZ3L1Zf6f5JlNsUuM2FKcwIgF90cmI2YiYtH+sS6kTZBg2qKNccj6rcXTViO/ MmrSL1gRC19NvJUP+wkxGJUgLA4hcsXkg5IsqaTplBxjbS4Aeb3HWSZkUGlBq4Ld540C xyB2Dba0LSF1HEjEc7ZK9Ejk+EQkWq2/cX8BB7k+yfj1N5S0jNlsHkps9KDA31zHm66S VKuOFsuLLhSmlrg4EH+gFIKli2Sn8H9wxvNkEmS2QHQK6V70BcIZv3wKo+gKrbtKpuUG ycA6SRwUYJ/rsunOVPiaHqcdwmFOWnaBpSZjscPA5BULHs2MnadLdctECf7J7ZNZblUI bnOQ== X-Forwarded-Encrypted: i=1; AJvYcCWraNnUEfsTDzjNC0wYeV+xRWr+wPsF40cgIxAE2wQv3JcTHE6UHL2xl6VbSu2gAGBOZWjVTrvsqskBYHeK@vger.kernel.org X-Gm-Message-State: AOJu0Yx97crD6JKtfynDNc7Zb/5OsAbFhb8BESgDRvA88/D9Jq/pmCl0 tQzI6oedFEMolei8Bux+BLrRHvUAxFwGer3KyXL9DVg2XrhtHuUxn6E/+hIkm3Xpgm89Xhwgaw= = X-Google-Smtp-Source: AGHT+IEjA25qbz56/ZivYmOA/8CzIw+8OnvV/Ld9F8g5Xe1iEl9v0x7DnaQxJbHqJscC5/DJshWP+7a2dg== X-Received: from wrbgx5.prod.google.com ([2002:a05:6000:4705:b0:391:3c12:d0cb]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:178c:b0:391:3768:f448 with SMTP id ffacd0b85a97d-3926c5a51cemr8018324f8f.49.1741802311548; Wed, 12 Mar 2025 10:58:31 -0700 (PDT) Date: Wed, 12 Mar 2025 17:58:16 +0000 In-Reply-To: <20250312175824.1809636-1-tabba@google.com> Precedence: bulk X-Mailing-List: linux-arm-msm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250312175824.1809636-1-tabba@google.com> X-Mailer: git-send-email 2.49.0.rc0.332.g42c0ae87b1-goog Message-ID: <20250312175824.1809636-4-tabba@google.com> Subject: [PATCH v6 03/10] KVM: guest_memfd: Handle kvm_gmem_handle_folio_put() for KVM as a module 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, 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, peterx@redhat.com, tabba@google.com In some architectures, KVM could be defined as a module. If there is a pending folio_put() while KVM is unloaded, the system could crash. By having a helper check for that and call the function only if it's available, we are able to handle that case more gracefully. Signed-off-by: Fuad Tabba --- This patch could be squashed with the previous one of the maintainers think it would be better. --- include/linux/kvm_host.h | 5 +---- mm/swap.c | 20 +++++++++++++++++++- virt/kvm/guest_memfd.c | 8 ++++++++ 3 files changed, 28 insertions(+), 5 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 7788e3625f6d..3ad0719bfc4f 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2572,10 +2572,7 @@ long kvm_arch_vcpu_pre_fault_memory(struct kvm_vcpu *vcpu, #endif #ifdef CONFIG_KVM_GMEM_SHARED_MEM -static inline void kvm_gmem_handle_folio_put(struct folio *folio) -{ - WARN_ONCE(1, "A placeholder that shouldn't trigger. Work in progress."); -} +void kvm_gmem_handle_folio_put(struct folio *folio); #endif #endif diff --git a/mm/swap.c b/mm/swap.c index 241880a46358..27dfd75536c8 100644 --- a/mm/swap.c +++ b/mm/swap.c @@ -98,6 +98,24 @@ static void page_cache_release(struct folio *folio) unlock_page_lruvec_irqrestore(lruvec, flags); } +#ifdef CONFIG_KVM_GMEM_SHARED_MEM +static void gmem_folio_put(struct folio *folio) +{ +#if IS_MODULE(CONFIG_KVM) + void (*fn)(struct folio *folio); + + fn = symbol_get(kvm_gmem_handle_folio_put); + if (WARN_ON_ONCE(!fn)) + return; + + fn(folio); + symbol_put(kvm_gmem_handle_folio_put); +#else + kvm_gmem_handle_folio_put(folio); +#endif +} +#endif + static void free_typed_folio(struct folio *folio) { switch (folio_get_type(folio)) { @@ -108,7 +126,7 @@ static void free_typed_folio(struct folio *folio) #endif #ifdef CONFIG_KVM_GMEM_SHARED_MEM case PGTY_guestmem: - kvm_gmem_handle_folio_put(folio); + gmem_folio_put(folio); return; #endif default: diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index b2aa6bf24d3a..5fc414becae5 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -13,6 +13,14 @@ struct kvm_gmem { struct list_head entry; }; +#ifdef CONFIG_KVM_GMEM_SHARED_MEM +void kvm_gmem_handle_folio_put(struct folio *folio) +{ + WARN_ONCE(1, "A placeholder that shouldn't trigger. Work in progress."); +} +EXPORT_SYMBOL_GPL(kvm_gmem_handle_folio_put); +#endif /* CONFIG_KVM_GMEM_SHARED_MEM */ + /** * folio_file_pfn - like folio_file_page, but return a pfn. * @folio: The folio which contains this index. From patchwork Wed Mar 12 17:58:17 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 872848 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 45919254B02 for ; Wed, 12 Mar 2025 17:58:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741802317; cv=none; b=XLCXNT8z6YNYyiar05ctLDA4PFeugTL0Z09sAWmGySImzt8YfhyIBWkFUCfi9ZgTmj0E8fvZOX6XI+9Uy4UXEaC+1RLcGAFk4VitHkadpXOTLevb9GSJlZVyJMz0vyuS9yJotly4IytzLgLBml1BgnaI+XTkxmQ3tqnwVrf6Waw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741802317; c=relaxed/simple; bh=0+R6PiRiVf6Qfgl6Xb/wXg3JYf8Soq67nwhFbPIQJKM=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=JJvmA1ExL5GsNMtTObXtzYXidMtImrki1F+oejVPnerUofjEuLwgIPbxLQ+T4vFpDGnsix+5YkoD0TBfD2qUq7wCBWlBZIH5col+nTEeRLamOVFJOd23nRaN93Az/A7J45DBKxiicEDe5Nu2M7o86cOdRe0Kl75mEzKLE3oVaY0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--tabba.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ykbRyp2W; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--tabba.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ykbRyp2W" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-43bd8fd03a2so306705e9.3 for ; Wed, 12 Mar 2025 10:58:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1741802313; x=1742407113; darn=vger.kernel.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=ja4c56NjFxjN3VuYRxoRbDc+8encrsyBoZ8FkcBEBAk=; b=ykbRyp2WRU4AT6iLugfQ1mGMrdf59qyDKZud5oI2dZ5/24peaNeV/STAbB6VZ9ZMle QrD/WsIEjvscNbQQj6I308Xuzy5ei4/R3JDQx6KJl3WNzf6LB8n9SntXQtpLVLGvNhYh Qn4d8P+U+Jt6ggujjQ+hMnojnzfPIUKXQQ8TAt9aKqMXjkORGv2qaFpDRRFgBOsEUKNU 02vxEJG3MJztHCctACydULMfZVWLELkmNEDVEGJEajSfNTTqcCNhEs32pGBIO/+Y88Jj MuX0J2kElylEeKE0HWVAf3CnTVrTFyAISoF/BH5OPEI2faQCJSXpSXpiyKNuDI4pupXp lUaQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741802313; x=1742407113; 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=ja4c56NjFxjN3VuYRxoRbDc+8encrsyBoZ8FkcBEBAk=; b=StiG44UQcN3AxzzUyf9DHV6CmgckTbredvbYqOrIE99pkSeyc7buKN8fqbRPwz4IWW uxRsY8oTbJeYhfcZKDxTkGss7FulA+2V/zzZvD1qUwcUJjHwKUPk/qc1ZEOsYvAJl+lU O0TGTwDJ8DIS+okgCW7cDvBeYWGmIPJ/j0RgV8vzDPktfVAg8Q2B4F41sBfB25aez0EE avqUmHbL5q38dXBpbvjZNcOK62xqrlOrLwqtnXv16mF5UsVOKLhS14xiSX/bs7kozefR emvBKAwmGhMCpXadkaF6+8FvBNXrBmLydoPaVFKGFu1FTqyS/FGtuPnCDyLoqXwB7Gcn Gbyg== X-Forwarded-Encrypted: i=1; AJvYcCXX1Mtu2EWuk9Bj9XeOuFonhIeCEKPuISX8QfzV/CfuU7FYaI6M2zMWOYrkp5ks8UMRkvAIhpCUryaCsjDN@vger.kernel.org X-Gm-Message-State: AOJu0YwX2VetX3MPEvmycYVEWi8Wq9gkRTBEJx0Fu1McqOkcUKs3OPtU TD/3xC8ueEIb6AFAvDjRk+p03ltT9bCV1CdFHSXasnZGFuHl2ppkA/2EApxFc4AGQ6oCGNd6LA= = X-Google-Smtp-Source: AGHT+IH6LramvrAethqSEJ43co3nUgcdaI6Hl8zlyi/Cxvas2W6mO2vl5zZ/oeJPP/4J6sjTcPIlLBCdsQ== X-Received: from wmbfl17.prod.google.com ([2002:a05:600c:b91:b0:43c:f5f7:f76a]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1ca0:b0:43c:f5e4:895e with SMTP id 5b1f17b1804b1-43cf5e48a1dmr101433505e9.1.1741802313446; Wed, 12 Mar 2025 10:58:33 -0700 (PDT) Date: Wed, 12 Mar 2025 17:58:17 +0000 In-Reply-To: <20250312175824.1809636-1-tabba@google.com> Precedence: bulk X-Mailing-List: linux-arm-msm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250312175824.1809636-1-tabba@google.com> X-Mailer: git-send-email 2.49.0.rc0.332.g42c0ae87b1-goog Message-ID: <20250312175824.1809636-5-tabba@google.com> Subject: [PATCH v6 04/10] KVM: guest_memfd: Allow host to map guest_memfd() 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, 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, peterx@redhat.com, tabba@google.com Add support for mmap() and fault() for guest_memfd backed memory in the host for VMs that support in-place conversion between shared and private. To that end, this patch adds the ability to check whether the VM type supports in-place conversion, and only allows mapping its memory if that's the case. Also add the KVM capability KVM_CAP_GMEM_SHARED_MEM, which indicates that the VM supports shared memory in guest_memfd, or that the host can create VMs that support shared memory. Supporting shared memory implies that memory can be mapped when shared with the host. This is controlled by the KVM_GMEM_SHARED_MEM configuration option. Signed-off-by: Fuad Tabba --- include/linux/kvm_host.h | 11 +++++ include/uapi/linux/kvm.h | 1 + virt/kvm/guest_memfd.c | 102 +++++++++++++++++++++++++++++++++++++++ virt/kvm/kvm_main.c | 4 ++ 4 files changed, 118 insertions(+) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 3ad0719bfc4f..601bbcaa5e41 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -728,6 +728,17 @@ static inline bool kvm_arch_has_private_mem(struct kvm *kvm) } #endif +/* + * Arch code must define kvm_arch_gmem_supports_shared_mem if support for + * private memory is enabled and it supports in-place shared/private conversion. + */ +#if !defined(kvm_arch_gmem_supports_shared_mem) && !IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM) +static inline bool kvm_arch_gmem_supports_shared_mem(struct kvm *kvm) +{ + return false; +} +#endif + #ifndef kvm_arch_has_readonly_mem static inline bool kvm_arch_has_readonly_mem(struct kvm *kvm) { diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 45e6d8fca9b9..117937a895da 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -929,6 +929,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_GMEM_SHARED_MEM 239 struct kvm_irq_routing_irqchip { __u32 irqchip; diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 5fc414becae5..eea44e003ed1 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -320,7 +320,109 @@ static pgoff_t kvm_gmem_get_index(struct kvm_memory_slot *slot, gfn_t gfn) return gfn - slot->base_gfn + slot->gmem.pgoff; } +#ifdef CONFIG_KVM_GMEM_SHARED_MEM +static bool folio_offset_is_shared(const struct folio *folio, struct file *file, pgoff_t index) +{ + struct kvm_gmem *gmem = file->private_data; + + VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); + + /* For now, VMs that support shared memory share all their memory. */ + return kvm_arch_gmem_supports_shared_mem(gmem->kvm); +} + +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)) { + int err = PTR_ERR(folio); + + if (err == -EAGAIN) + ret = VM_FAULT_RETRY; + else + ret = vmf_error(err); + + goto out_filemap; + } + + if (folio_test_hwpoison(folio)) { + ret = VM_FAULT_HWPOISON; + goto out_folio; + } + + if (!folio_offset_is_shared(folio, vmf->vma->vm_file, vmf->pgoff)) { + ret = VM_FAULT_SIGBUS; + goto out_folio; + } + + /* + * Shared folios would not be marked as "guestmem" so far, and we only + * expect shared folios at this point. + */ + if (WARN_ON_ONCE(folio_test_guestmem(folio))) { + ret = VM_FAULT_SIGBUS; + goto out_folio; + } + + /* No support for huge pages. */ + if (WARN_ON_ONCE(folio_test_large(folio))) { + ret = VM_FAULT_SIGBUS; + goto out_folio; + } + + if (!folio_test_uptodate(folio)) { + clear_highpage(folio_page(folio, 0)); + kvm_gmem_mark_prepared(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) +{ + struct kvm_gmem *gmem = file->private_data; + + if (!kvm_arch_gmem_supports_shared_mem(gmem->kvm)) + return -ENODEV; + + 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 +#define kvm_gmem_mmap NULL +#endif /* CONFIG_KVM_GMEM_SHARED_MEM */ + static struct file_operations kvm_gmem_fops = { + .mmap = kvm_gmem_mmap, .open = generic_file_open, .release = kvm_gmem_release, .fallocate = kvm_gmem_fallocate, diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c index ba0327e2d0d3..38f0f402ea46 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -4830,6 +4830,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_SHARED_MEM + case KVM_CAP_GMEM_SHARED_MEM: + return !kvm || kvm_arch_gmem_supports_shared_mem(kvm); #endif default: break; From patchwork Wed Mar 12 17:58:18 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 873593 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 851392586CC for ; Wed, 12 Mar 2025 17:58:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741802320; cv=none; b=C+KjC5P1OHo97ewQJW8n4AO9u7ZZx+WlX5kcWkX/PTl+T6VOhLak8G1VU5WmFaFTyJpK588SkjDerb7hXTHbS6MbDhoLG1kEdzsKyRxNHo+zN2DuwlMEnSJdBOOuLi9EvkOq3tVUgNXyaeWmUV4H6n92nyKJ5GAwsSgTIAQesaI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741802320; c=relaxed/simple; bh=7edp0M7HHdxVW9XbqQLME1hZfP7MTAt+1whGabMX0dY=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=b4EX+FWJkzuSw/+XCceHP09MRz6+RcZWYuawyOhr3YOFLTjqFSpaI4DxXBueB4klEoboyZ8/p4o788IvaYDW8r56pP4on5+eUxEyk8IQNtYghFv6c/SaBa0v5ERmhga89wz9/NSPRARL7DL7CaJuLKoTsZdvz+uWA80NB/Ed+is= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--tabba.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=XKyN6VIR; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--tabba.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="XKyN6VIR" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-438e180821aso337675e9.1 for ; Wed, 12 Mar 2025 10:58:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1741802317; x=1742407117; darn=vger.kernel.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=ntFxQQNzKKyJM2lZzgIL3uflnY89YGPG26G1RQYhb04=; b=XKyN6VIRtdA0BAWlGmvHZzE+xJdcaWvqNntO1pu6ILQYq5cVlTcnFm2RymtzleuSvC SDM/qTIrc0mjddtwFr2WzLV7JER12JfsrbBKyuViOsHz7hXKYthRCFjGIZDoIZFQkjT5 6HeBy62JGevJi1IgG0JkIreXMxI/vy4VE4A6NdYT9JWqfb02PXbjMAKqht04U4hXMLFf aDJegy1YMLArhrjVJ0osPzODGiR4JcxOgIkhT6uSA3A905zMiWC+Ep0wmrjAaP1Dx9+d dTETVbst6dAc2HPzOLefIg3upps4JXQHmFDhZ+7aAkMVjVlQumR8h54/9zybCpieMLU+ ey9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741802317; x=1742407117; 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=ntFxQQNzKKyJM2lZzgIL3uflnY89YGPG26G1RQYhb04=; b=MS0RQBf7IZ8h2nWzU0dUxCFb7DR8pAvmgdxzrBlE1KCcgx+NpG8xSmAI03OHllHfkF KfdVL4SCAyoN51Zcmh9Z7n7kUaT6ICS5sfDw9inkYS5Xo8c1QGIXNJsKPtCpbwyuWkuZ BYIlADWt8oLXgA9TuVpOSd9gHrzT6KEfsAFINApurEv5uUIql0V01Io8XzV0Lz3W29Y9 V24iuUrNGUDHFJLSmmZNIBeDJJj/v8XJkXv/8c7qFyRW+UiI1JQy3qgnbQIyinGiBcOz mHjT3UjT2WJPgisymwV9YhPyHpT5bfP0UbFuQmS5X317X4JGBiAxlzCGPlcmjtiuxKgP q1XA== X-Forwarded-Encrypted: i=1; AJvYcCU5H2clDohSRa3zlA+PaMP/IhbDC/xYf2aTQ28If+kD9G/aPBHA8Pb0WjVzAlioyl7jZyKhmhrvhfDiWwCv@vger.kernel.org X-Gm-Message-State: AOJu0YzYpCHmdDPS/8JNbt4JM8StnWXv10mwnmXcWfyAsWg+jv/DUwbc O6izBTG8y1FD6v//pPXbqi2UtJGLQAKXqQYUD7VyaLcE8K1ZbPBNjYZEmn395cZSqVnXwk6wdg= = X-Google-Smtp-Source: AGHT+IHx2UGNc3sxVMFxnpaPRw6zK2nrSSbS+Z6p3nzgLh0TKg+a144XW1OZZX2F42XTW0aAYQXWeSM0QA== X-Received: from wmbhc26.prod.google.com ([2002:a05:600c:871a:b0:43c:f050:8fdf]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4751:b0:43c:fffc:7886 with SMTP id 5b1f17b1804b1-43cfffc7ca2mr104987635e9.8.1741802315742; Wed, 12 Mar 2025 10:58:35 -0700 (PDT) Date: Wed, 12 Mar 2025 17:58:18 +0000 In-Reply-To: <20250312175824.1809636-1-tabba@google.com> Precedence: bulk X-Mailing-List: linux-arm-msm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250312175824.1809636-1-tabba@google.com> X-Mailer: git-send-email 2.49.0.rc0.332.g42c0ae87b1-goog Message-ID: <20250312175824.1809636-6-tabba@google.com> Subject: [PATCH v6 05/10] KVM: guest_memfd: Handle in-place shared memory as guest_memfd backed memory 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, 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, peterx@redhat.com, tabba@google.com For VMs that allow sharing guest_memfd backed memory in-place, handle that memory the same as "private" guest_memfd memory. This means that faulting that memory in the host or in the guest will go through the guest_memfd subsystem. Note that the word "private" in the name of the function kvm_mem_is_private() doesn't necessarily indicate that the memory isn't shared, but is due to the history and evolution of guest_memfd and the various names it has received. In effect, this function is used to multiplex between the path of a normal page fault and the path of a guest_memfd backed page fault. Signed-off-by: Fuad Tabba --- include/linux/kvm_host.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 601bbcaa5e41..3d5595a71a2a 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2521,7 +2521,8 @@ static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn) #else static inline bool kvm_mem_is_private(struct kvm *kvm, gfn_t gfn) { - return false; + return kvm_arch_gmem_supports_shared_mem(kvm) && + kvm_slot_can_be_private(gfn_to_memslot(kvm, gfn)); } #endif /* CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES */ From patchwork Wed Mar 12 17:58:19 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 872847 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 58E9D2566E1 for ; Wed, 12 Mar 2025 17:58:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741802321; cv=none; b=THjahWwkwLBqqYCDfJ5jGF4cKL7dVWbPKCxX3vPGEZjl3gK1hlZbn0CoR8DjW0mqMpirOIToFlRPTYt6epaebGo/j4Wz/MTjvo/rj/h9F3dA44o5z/4wBY+RGUMc7m4Qu+XdhVaP25KPbnVyPe3f+oIKm/6wyWDPzAV0SV7KOQc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741802321; c=relaxed/simple; bh=tZucr18dFkzlB3deJ6wbqtZ+bsyrb7slNtwTBa7RNbo=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=lTlR4ctmI52Dr3ElZvri29JiQBaoiK5az5QkcRn9o5xu2TfE+FB3zvyMHkvBfSjZ5HQzWEXqRjM17r5bUs1VuCgWVOcIsJOa7KDWm6w6RrOYLoNACW0njghTFdILDisudVUjYWdz89ZiR10EpG4qb39O+DlmtX6raZnTJhSFSJc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--tabba.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=K6xosv+R; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--tabba.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="K6xosv+R" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-43d025a52c2so402055e9.0 for ; Wed, 12 Mar 2025 10:58:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1741802318; x=1742407118; darn=vger.kernel.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=Wx8w/RZ95u4Jazhb7Dc1SjbWTPE/GXa4O3urxkMUigQ=; b=K6xosv+Re/1yS+0++zsehDJXwpa8iLxb5wcADpWixPHIAXnM8R+3bgiRDLALiv0E2y Shiazdz1GuLqxvbXa7Zdjkrx6FGFMQeBFXGt2fAQIJlcN9W4iOnRNZlH5+WoH2sNfHYc vV5euj1zKYMe6AuPDzBg+6u5eyF9uqBVqqBRY9QoSvzIwCOZvEbCGfKrnEf+mCraEnFK deoMnnxESQ9KjBm6Scshx69YYkuvyihnSNy2jJttu59PmdrxZ/Qc8oRpAhOAhHAOs1W8 1z55RxSNGrVOLrVRuApT/yr6Pe0Xm+5Kj5fCQA8aM2Z4jgHP3tTvWG0HNIgNzMoKrtLU UUyg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741802318; x=1742407118; 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=Wx8w/RZ95u4Jazhb7Dc1SjbWTPE/GXa4O3urxkMUigQ=; b=vs/cOJSMi6tG9orGs/xeqKGwW2Qisj4y+zUyJjrTUzSYWBb4Bm3x7DxsBNwt6ZFw26 d+2iAQoWVN9vXcUthGBZwzRYW5kquYVHY/swc4fSYLdIOJO1w+jv0vNiw2GIcjrTyjY9 WhZ8qyqqJkfm/5XSza5j4SUogXw0E0607y6WOZNZrVjf8Hw6OeEknf4RVRyES9aXLns7 al9eutLpqPArxnJetmohh5/VglTO+8+PhTFawebCZJKeKkOhsTIgmPZJ/6B4KUhoycTm +i1MbSVkmdT7HUH7sdSSRZ4DrzmmN9dJRti2Pf2makvSoSbMU3JeYmBHcJGbGbAXUpna iyAQ== X-Forwarded-Encrypted: i=1; AJvYcCWupOr6qWzWu/5lvXq59yJRpLnem6AWu8lXkC3rGiYiJN6nuBe/J4EaAvhrPG4LIzik8ABVzwpkBPkMvUjf@vger.kernel.org X-Gm-Message-State: AOJu0YxL0TwFPthh0zgttBAw52jmL2s2r/xKEUExbj5TIYjSo1NAzcVp MFVPG80kwX/6leUFQpWj/VAJb6VpVrhbV5sTIaXQUTu495PeQylENigPUm/yIu6FxmqcDJrsIA= = X-Google-Smtp-Source: AGHT+IFJlj4nTok+ueA0jACRwns8uL5LH08IddJBcx9wqowAWc71zjFE+8nTZuhjVvmJcfe8xTEJ+RPsaQ== X-Received: from wmsp15.prod.google.com ([2002:a05:600c:1d8f:b0:43c:ec4d:6037]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4fce:b0:43b:cd0d:9466 with SMTP id 5b1f17b1804b1-43d07fd485emr45933555e9.9.1741802317784; Wed, 12 Mar 2025 10:58:37 -0700 (PDT) Date: Wed, 12 Mar 2025 17:58:19 +0000 In-Reply-To: <20250312175824.1809636-1-tabba@google.com> Precedence: bulk X-Mailing-List: linux-arm-msm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250312175824.1809636-1-tabba@google.com> X-Mailer: git-send-email 2.49.0.rc0.332.g42c0ae87b1-goog Message-ID: <20250312175824.1809636-7-tabba@google.com> Subject: [PATCH v6 06/10] KVM: x86: Mark KVM_X86_SW_PROTECTED_VM as supporting guest_memfd shared memory 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, 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, peterx@redhat.com, tabba@google.com The KVM_X86_SW_PROTECTED_VM type is meant for experimentation and does not have any underlying support for protected guests. This makes it a good candidate for testing mapping shared memory. Therefore, when the kconfig option is enabled, mark KVM_X86_SW_PROTECTED_VM as supporting shared memory. This means that this memory is considered by guest_memfd to be shared with the host, with the possibility of in-place conversion between shared and private. This allows the host to map and fault in guest_memfd memory belonging to this VM type. Signed-off-by: Fuad Tabba --- arch/x86/include/asm/kvm_host.h | 5 +++++ arch/x86/kvm/Kconfig | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h index 32ae3aa50c7e..b874e54a5ee4 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -2246,8 +2246,13 @@ void kvm_configure_mmu(bool enable_tdp, int tdp_forced_root_level, #ifdef CONFIG_KVM_PRIVATE_MEM #define kvm_arch_has_private_mem(kvm) ((kvm)->arch.has_private_mem) + +#define kvm_arch_gmem_supports_shared_mem(kvm) \ + (IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM) && \ + ((kvm)->arch.vm_type == KVM_X86_SW_PROTECTED_VM)) #else #define kvm_arch_has_private_mem(kvm) false +#define kvm_arch_gmem_supports_shared_mem(kvm) false #endif #define kvm_arch_has_readonly_mem(kvm) (!(kvm)->arch.has_protected_state) diff --git a/arch/x86/kvm/Kconfig b/arch/x86/kvm/Kconfig index ea2c4f21c1ca..22d1bcdaad58 100644 --- a/arch/x86/kvm/Kconfig +++ b/arch/x86/kvm/Kconfig @@ -45,7 +45,8 @@ config KVM_X86 select HAVE_KVM_PM_NOTIFIER if PM select KVM_GENERIC_HARDWARE_ENABLING select KVM_GENERIC_PRE_FAULT_MEMORY - select KVM_GENERIC_PRIVATE_MEM if KVM_SW_PROTECTED_VM + select KVM_PRIVATE_MEM if KVM_SW_PROTECTED_VM + select KVM_GMEM_SHARED_MEM if KVM_SW_PROTECTED_VM select KVM_WERROR if WERROR config KVM From patchwork Wed Mar 12 17:58:20 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 873592 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 71B35256C6D for ; Wed, 12 Mar 2025 17:58:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741802323; cv=none; b=AnQKcaMPzOVddL6tCSxsxiFq/ql8uxS0lQTmn2dRCLLgDYMijXr4X7EXDrvccUafa3G0BgdUJiP88WIIXTJ3yxcgWfVh8o9wL5fDqtkgTxl+GS+iuOnnw+OyO3esVuqdTmyiPX/20gW+Aq0IDxuxKEUHSxAsXrtu/KmrJ3SqZ+Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741802323; c=relaxed/simple; bh=VybER0/fqEB9tPzYLKG2iDPLQp0h2XcPiWdpbCfTdBA=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=IQbeoSn4FVhjyYkDtl/Y4guCni8hS7V3Z7cycg5PFmo9MY8s0J0h7s4N4Mb5ou7aFCOkyn5B8yrmYBubQkraVeppLnOlYpSaIWEqOlN0F2gYRbynwKsEZHj5W6n8SIiX9R+0aAf56UeuQQmv8HzsFjm1kK6+wEXfGWB02lsWP9Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--tabba.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=s0zEue3g; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--tabba.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="s0zEue3g" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-43ceb011ea5so305095e9.2 for ; Wed, 12 Mar 2025 10:58:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1741802320; x=1742407120; darn=vger.kernel.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=Awd1f7IbYeX5crgm51oNRh01M9SxrhzmMhHWchIyiT8=; b=s0zEue3g1XjN+sWCOprDzcIcvGKAaMOL4qWn2gNd1TYeH98rQ4u7wZYKRXUqFop5ll ACpk/IroTUd279roJZyV7SXf+y9xuvTfDwf8fGZORtqMnS8XG4SG06u4m0iiz6F0ifvW j7s/lliNRrQJjgsV1m7HXfmxpR3yO+yMFOJHjDagMj3XG5BsLtCWO1l1FDe7AzvWNK4p urqdxstfCLcv0G7xdUdwtpgdFgFVlFKfyLTpALeDsE379gRfuuoBEs7hs0JGWcLBHR7/ dcB7pr0WiH+b+bGghbgaF+zmSLHlS8CQu83B4NR5yfJM3KJ4hRQ/lNUC+ILc6aAjJgKJ ZLGw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741802320; x=1742407120; 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=Awd1f7IbYeX5crgm51oNRh01M9SxrhzmMhHWchIyiT8=; b=ugXl8wsf9o8UO0gdYi/DzpbTuCJ/H+JnHxRfjRrmwh9gxjou2eHQb+yL8WOBQ5gndg yD3NAuhQOXC9pq5Xbu/Cmu12cyFpUaM/ft2jy/69Uy6tsXminLVnw5g7z8a7lb/qoPU+ BAngcLq0EGq1V9R3P81+j9VZvRvUST0RXGz5ndsE/VVI0wGWhm/dqCdlsLeeuTgHUSTs Zh42w7RGTeB7nhaM+K3VQkGRlC1nMiN0/T/1xFAgBmr4jVLI5SDrtL0R6Gf4lhkFIvpN NRRltmG7WbfOD0WFm0wtWCTM4mZaoBNb1tZsa8OSCaOprsOMHnKxpqV3K0qYce4sTijP Gwng== X-Forwarded-Encrypted: i=1; AJvYcCVk8VcEcPnMSC67cnZ0/LEYP0yoWDT7autw7VldC0cUOHTBVle9noHpjA/VkJrwzZf0sLAbOKLVOazBA2vL@vger.kernel.org X-Gm-Message-State: AOJu0Yy54mhZVrj6KzE6B3JG8FeGPpJME1k6/HHURAbbKU0ZV+D4tUI1 voWzCbGw5aiK9PkG2+n1ZvWEACm3BvzSOLUuWrL8kQutcRo57rfDnejf6+WHUwczbPRZAk5DFA= = X-Google-Smtp-Source: AGHT+IHj9tfnXDE4zwGrzMtBNQTrEX37Nr1SjsVHZdIiFflWByxQ+d97M9AkHPF0ggQs0Ylalb5/M39WuQ== X-Received: from wmbbh12.prod.google.com ([2002:a05:600c:3d0c:b0:43c:e9df:2ecd]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:4751:b0:43c:fffc:7886 with SMTP id 5b1f17b1804b1-43cfffc7ca2mr104991245e9.8.1741802319732; Wed, 12 Mar 2025 10:58:39 -0700 (PDT) Date: Wed, 12 Mar 2025 17:58:20 +0000 In-Reply-To: <20250312175824.1809636-1-tabba@google.com> Precedence: bulk X-Mailing-List: linux-arm-msm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250312175824.1809636-1-tabba@google.com> X-Mailer: git-send-email 2.49.0.rc0.332.g42c0ae87b1-goog Message-ID: <20250312175824.1809636-8-tabba@google.com> Subject: [PATCH v6 07/10] 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, 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, peterx@redhat.com, tabba@google.com To simplify the code and to make the assumptions clearer, refactor user_mem_abort() by immediately setting force_pte to true if the conditions are met. Also, remove the comment about logging_active being guaranteed to never be true for VM_PFNMAP memslots, since it's not technically correct right now. No functional change intended. Signed-off-by: Fuad Tabba --- arch/arm64/kvm/mmu.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 1f55b0c7b11d..887ffa1f5b14 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1460,7 +1460,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; @@ -1472,6 +1472,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 || is_protected_kvm_enabled(); long vma_pagesize, fault_granule; enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_R; struct kvm_pgtable *pgt; @@ -1521,16 +1522,10 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, return -EFAULT; } - /* - * logging_active is guaranteed to never be true for VM_PFNMAP - * memslots. - */ - if (logging_active || is_protected_kvm_enabled()) { - force_pte = true; + 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 Wed Mar 12 17:58:21 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 872846 Received: from mail-wm1-f74.google.com (mail-wm1-f74.google.com [209.85.128.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A3BD2258CCB for ; Wed, 12 Mar 2025 17:58:43 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741802326; cv=none; b=YB2Y7pP3HIGyjZzl/DtzL0hWKv/YT0+tYgbQrCe+MeJr60zLMxr6gUcwAcY8HyYAzLGSBXU82pd8FJNehUNPfHOlyXkaiV4x9HbDj7K6sCJ8Mu0D/+MvFJIpBAtwBmN9gUAiKz7MmVujNygNtyH6YyT1ApqFLYJ1Q8S+pMXsgVY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741802326; c=relaxed/simple; bh=JrTvyHiXKHvsC898MaHDvFWzZFHSfI05OZDM7sRwtrc=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=irE4bWaPSUbsve5Sh7rN/6EDKgySD+XNUpVAZIq8oYTz8B4DtkcJwfhJIPbCIRTfOisD/7RtoAUh5jhqvY9EmaQQ++BDA43/gxW/eiFElmkizPwmXzSDTZcDqyrB5p/ojihSPRWlfXbQ2xAud+Po1TnQhDyxACMa6jNYsYBJ7lg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--tabba.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=SoUTC50M; arc=none smtp.client-ip=209.85.128.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--tabba.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="SoUTC50M" Received: by mail-wm1-f74.google.com with SMTP id 5b1f17b1804b1-43cfd9b833bso5599715e9.1 for ; Wed, 12 Mar 2025 10:58:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1741802322; x=1742407122; darn=vger.kernel.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=We8c70tl+Zx5FQcQtlW287aL/adOZePxySoQID49z3A=; b=SoUTC50MIyZdgx/Nu3+DD5sfwK4hcE1FUKf7LOzJYQmxgxD3VrkwTeC17ZAX0nw1OQ 2iLgJMUaOSIC9bGrxDYgW2w9x8/m1OSZEUv4onpdhMMHoSWTbisUKHXPvqfPt7hPBRey q2mZ5ebX3mR7pzv/lOa3VO6V41lU6ydimBH6AuFD6OIbW5oCJXm5z+HuuSic8crUxv0p TSxwlG5SgJpLmuVBDMdADXQZd/nzVaRSkA1Lpy8oUrsSq1FaFvkbzJSWxo8slr+MhmG7 jtxC5CUAZdAiETpaf5C4VfAQqNZSi/HmeHLzf9xgdXwezdZkTsr2odVEs4oein3sCUYH tP+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741802322; x=1742407122; 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=We8c70tl+Zx5FQcQtlW287aL/adOZePxySoQID49z3A=; b=vUf+hX8iNNnJNmb3v/TyHHfRwLSVCGVkO14ffKUk0vAshcGLeB+ykqXoc3C7biygrW eb5XWa2zeemkmX6tSjl+VHrgo7EjKksKQE+9DL42Tck1A0serh0RNbelQFxlt4U3L2Pd MOJQIqQz7w9YaY5sY/erS60FXC7I8ip4sn9PQ0LeFVW9lqELFpcsjyDhf6KnE3wbVSBG 6BIM7R11cC0ewgOyZr0dly+Om6GvQvDUvl9HB/5HManT5PwgYsjojrz+3ZPw9wzwyjFH hZwUTm5Zi7yAnltnsRdpJj2QiPnSoiIBaWW8tnyQBRtoU6yzWlbZri/JkW8Ti+y/GM3g 25SQ== X-Forwarded-Encrypted: i=1; AJvYcCVzdcu9BV1VNVAYikykhJUJYhSVsx+oKsBAPscCDQW0LmPXYe/bj2tQR5CdtjtN5sMbN0li3h0np6PDDjHO@vger.kernel.org X-Gm-Message-State: AOJu0Yyqs/Bs7M0kirYGpd9SdWSHWTfCyJfeaNFoFec2q9Z6mkHnbkzs vWbNyC3hES61C7VkhtJ2y4QPuoTGGt98HGHq85V+hbZ/dW/v+IMya78AqPs/xCX5ettJtcqXZA= = X-Google-Smtp-Source: AGHT+IFgL9irGVEt2YWfb1UQbIwVL/VNkbXIcKNXEidvOBQCQVT8F8I3RbRKchYk4z05AIfWZpjSXgIKCw== X-Received: from wmbeq9.prod.google.com ([2002:a05:600c:8489:b0:43b:c9fc:b269]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1913:b0:439:8e95:796a with SMTP id 5b1f17b1804b1-43d161ea047mr4367375e9.13.1741802321988; Wed, 12 Mar 2025 10:58:41 -0700 (PDT) Date: Wed, 12 Mar 2025 17:58:21 +0000 In-Reply-To: <20250312175824.1809636-1-tabba@google.com> Precedence: bulk X-Mailing-List: linux-arm-msm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250312175824.1809636-1-tabba@google.com> X-Mailer: git-send-email 2.49.0.rc0.332.g42c0ae87b1-goog Message-ID: <20250312175824.1809636-9-tabba@google.com> Subject: [PATCH v6 08/10] 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, 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, peterx@redhat.com, tabba@google.com Add arm64 support for handling guest page faults on guest_memfd backed memslots. For now, the fault granule is restricted to PAGE_SIZE. Signed-off-by: Fuad Tabba --- arch/arm64/kvm/mmu.c | 65 +++++++++++++++++++++++++++------------- include/linux/kvm_host.h | 5 ++++ virt/kvm/kvm_main.c | 5 ---- 3 files changed, 50 insertions(+), 25 deletions(-) diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index 887ffa1f5b14..adb0681fc1c6 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1454,6 +1454,30 @@ 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; + + ret = kvm_gmem_get_pfn(kvm, slot, gfn, &pfn, page, NULL); + if (!ret) { + *writable = !memslot_is_readonly(slot); + return pfn; + } + + if (ret == -EHWPOISON) + return KVM_PFN_ERR_HWPOISON; + + return KVM_PFN_ERR_NOSLOT_MASK; +} + 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, @@ -1461,19 +1485,20 @@ 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 vm_area_struct *vma; + struct vm_area_struct *vma = NULL; short vma_shift; void *memcache; - gfn_t gfn; + gfn_t gfn = ipa >> PAGE_SHIFT; kvm_pfn_t pfn; bool logging_active = memslot_is_logging(memslot); - bool force_pte = logging_active || is_protected_kvm_enabled(); - long vma_pagesize, fault_granule; + bool is_gmem = kvm_mem_is_private(kvm, gfn); + bool force_pte = logging_active || is_gmem || is_protected_kvm_enabled(); + long vma_pagesize, fault_granule = PAGE_SIZE; enum kvm_pgtable_prot prot = KVM_PGTABLE_PROT_R; struct kvm_pgtable *pgt; struct page *page; @@ -1510,16 +1535,22 @@ 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_gmem) { + 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; + } + + vfio_allow_any_uc = vma->vm_flags & VM_ALLOW_ANY_UNCACHED; + mte_allowed = kvm_vma_mte_allowed(vma); } if (force_pte) @@ -1590,18 +1621,13 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, ipa &= ~(vma_pagesize - 1); } - 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(). @@ -1609,8 +1635,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_gmem); 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 3d5595a71a2a..ec3bedc18eab 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -1882,6 +1882,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 38f0f402ea46..3e40acb9f5c0 100644 --- a/virt/kvm/kvm_main.c +++ b/virt/kvm/kvm_main.c @@ -2624,11 +2624,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 Wed Mar 12 17:58:22 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 873591 Received: from mail-wr1-f74.google.com (mail-wr1-f74.google.com [209.85.221.74]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 22E7D2594B4 for ; Wed, 12 Mar 2025 17:58:45 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.221.74 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741802327; cv=none; b=QeNqdhGzrTia0VnoC4umJUyN/7FX51uO4kJ4jtmeU4gbUJqUtF6yF3aEMFYTGwqQXRMaAPUBVFyhyxUtaYABFoZR9fE4jLx61HYXznpl05b2DoMw+07DhPv6R+SZ9uYV69eGtngfiaVhvp1+Ibkbu5onkdwr87KgQtbOWvOzHSU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741802327; c=relaxed/simple; bh=Lq+619vRZ6FPCIIv9LOKJWBsVi+N3bTSFDaCYzs2CWg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=c8xP3rgWues/pTjr/xStSt4/NV4SinX7Se6v0AbeBIdNDbaQn5cX9/efLrliVTMHWVV+WO5NweWv0affegKxHjTgnNCDEPULd2geYvQ/YSp9LDcymtb93ILocejpHgGvdhP4yKjT9pZDhThFNYkE+huGy1ZYrcZE4qinnFAkZjg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--tabba.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=ImdWKVcc; arc=none smtp.client-ip=209.85.221.74 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--tabba.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="ImdWKVcc" Received: by mail-wr1-f74.google.com with SMTP id ffacd0b85a97d-39135d31ca4so74904f8f.1 for ; Wed, 12 Mar 2025 10:58:45 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1741802324; x=1742407124; darn=vger.kernel.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=hYJFKrO7ehNxwQgkQPklR1qHjwdzPiYZpyonfD7j1as=; b=ImdWKVccJf761T9VymPHRzChhRz8Mlh0HJkjqP0Pc6PxMbng+Mi5I5wGO46Ea3YY/L Gh6XEAjDdfa7tHZNEVsXBsevvzQJWoLiVXjzKy1L9GfHxS2Id5xKmOQqozqCXgWhi2fY DcvqLfkgXorAyUXBe7+vXWosZBzDHoYBovG2yHexaXUFJd+G0Z0M9xoUkXpt+91dhZla tAzh37pXOu1B6CPRCBu27Y+a0NyltPOa5QnUxGe1zDcRskGUdHh2sIqqjDacjOpyWp6o 1DjsViVjdgee3oBgqe0+iGDC5qNf3I0ovBo4u4gxADQhQgugRY1JioLIo13kgHDuGFFE /z0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741802324; x=1742407124; 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=hYJFKrO7ehNxwQgkQPklR1qHjwdzPiYZpyonfD7j1as=; b=XX639MJrbhEIdM4GjQ9VlaREUTM69N/4sT+YB7s5Lgw213Ztb+izSdLJOAsccA+v5P cQ9f4vF1+2iC7+NnA3lj2kykKux9jk1SGokxDyTIMgPutaQ4RnbmVdBOY2aR6bAId0x/ zv9ESPUFp3j9nw/Q0KY5KwN4mC3HkUwpqMpMVHNdizT1P5r+yRRx1+9MdcGJL/cK3xz2 BDb//n9wTgbpbViFVw3WtgGMkEg39ZfDFOw1btF6uAKr87AvVwDoWIpyfdyR1Z/MKt0j BvuIzYj5WRh/S+bSY2hXhqEuq2+IjsDMbj1cMte4GZ+yTEpCF0KjEA87d/c16ybJSXxz z3xQ== X-Forwarded-Encrypted: i=1; AJvYcCXdwsvPTF/ams+YIYWz+mtTI2fCpL7bCw2ZzU/8/AhV9BNOtw4TQtH0fv0Plj7AmQdbwROy2eNLCe5a0IZ+@vger.kernel.org X-Gm-Message-State: AOJu0YzpusfJW0z7d7x9P09PAnyLD5YQA0lzcNxePsDqL4rEktqPYLR8 r22YLBQozbxECP/Z8OUKbtFqwaFfxE3H51X4bInQQb8d8hAVI7N9BPAIA0Hbq26+Jm7CzR4/qg= = X-Google-Smtp-Source: AGHT+IHQSaL0y42vC9qHHbU0sI/iFbdLjbiwdGaYuErHiOt/6W3ltnpQGYP4gPL3EaUPVT6jNAZ9Hud5Ag== X-Received: from wmbfj15.prod.google.com ([2002:a05:600c:c8f:b0:43c:f122:1874]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6000:4022:b0:38f:23c4:208c with SMTP id ffacd0b85a97d-39568a1c22bmr422481f8f.18.1741802324225; Wed, 12 Mar 2025 10:58:44 -0700 (PDT) Date: Wed, 12 Mar 2025 17:58:22 +0000 In-Reply-To: <20250312175824.1809636-1-tabba@google.com> Precedence: bulk X-Mailing-List: linux-arm-msm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250312175824.1809636-1-tabba@google.com> X-Mailer: git-send-email 2.49.0.rc0.332.g42c0ae87b1-goog Message-ID: <20250312175824.1809636-10-tabba@google.com> Subject: [PATCH v6 09/10] KVM: arm64: Enable mapping guest_memfd in arm64 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, 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, peterx@redhat.com, tabba@google.com Enable mapping guest_memfd in arm64. For now, it applies to all VMs in arm64 that use guest_memfd. In the future, new VM types can restrict this via kvm_arch_gmem_supports_shared_mem(). Signed-off-by: Fuad Tabba --- arch/arm64/include/asm/kvm_host.h | 10 ++++++++++ arch/arm64/kvm/Kconfig | 1 + 2 files changed, 11 insertions(+) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm_host.h index d919557af5e5..b3b154b81d97 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -1543,4 +1543,14 @@ 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)) +static inline bool kvm_arch_has_private_mem(struct kvm *kvm) +{ + return IS_ENABLED(CONFIG_KVM_PRIVATE_MEM); +} + +static inline bool kvm_arch_gmem_supports_shared_mem(struct kvm *kvm) +{ + return IS_ENABLED(CONFIG_KVM_GMEM_SHARED_MEM); +} + #endif /* __ARM64_KVM_HOST_H__ */ diff --git a/arch/arm64/kvm/Kconfig b/arch/arm64/kvm/Kconfig index ead632ad01b4..4830d8805bed 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_SHARED_MEM help Support hosting virtualized guest machines. From patchwork Wed Mar 12 17:58:23 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Fuad Tabba X-Patchwork-Id: 872845 Received: from mail-wm1-f73.google.com (mail-wm1-f73.google.com [209.85.128.73]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 74E491D5176 for ; Wed, 12 Mar 2025 17:58:47 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.73 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741802329; cv=none; b=U1kP7J+Hkj6yxkDE7/e/CiEmxxjyCy+Mg/JuTxLhX2i+WgiY/vMp63P0cTGPn9KhPutdR2xnRwAQ2+lWHybA09lDMkMgPpmFzPTtbJHs+zWSfM9+Vz5zTIOK8wrOSec0dww1RgZd/iQxBu83reZGENLT2XimyYl3uzfc6eZli2Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741802329; c=relaxed/simple; bh=dBp330nwbC7q4iPH2IoIUZ3Q0GJxinDrCajbj2wj9s0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=OvLN/XNrtzPWzVlJla6JLaI90eXgq/41/ERWjsE5SaILR6dGNZaROMfE5lBv5PSibWcuxhie1lmbuCxaW2IAp30YFkpYdp/ZZWqYSUXs/4RbEV2e77BYyffrsCVRw8faiwnuG0vFSng/IQzFAbG59VOi2lSz7XFfGBfYhsboIZE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--tabba.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=VMenEBkN; arc=none smtp.client-ip=209.85.128.73 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--tabba.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="VMenEBkN" Received: by mail-wm1-f73.google.com with SMTP id 5b1f17b1804b1-43d025a52c2so402685e9.0 for ; Wed, 12 Mar 2025 10:58:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1741802326; x=1742407126; darn=vger.kernel.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=IlIj3TjUhtkedIPYTVHeRQbthVzaFoCCtbaBiaH1aMs=; b=VMenEBkNgjG7/kRyc1xc2e8vAftptPXLVORUiTRFurTxFfUYQZmorBRSvN25uudUQy g585dTGZOz/Lo2aDflADS5rS8YAJwyAMzptUjN7nw9foymv438eP1y+dgkEGt7tvGEmm fsQgP/2l/0AFPQqu7PkvI5fbOy/X9r0g1X25i4akgDJjq9Q1ij8vKtvjxh06razNV8TI Pi49jUkd7f4K8XDYsjrZ8dOX49YoQMflOZcFPfeJndWdDsO9UzDySd7SGFx7ZNeq47JZ B4aVHH5vMOcJypUAEa6vD6UOiasGrn64Gh4Q0ZS797E84ulmvBYiSjCeX/pkubxJRUvM 7chA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1741802326; x=1742407126; 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=IlIj3TjUhtkedIPYTVHeRQbthVzaFoCCtbaBiaH1aMs=; b=GQMNbJhEqgc32i3b0NRWLaE+ClRJ7GLN9qZOc2oAY4WaCSyGo5JtHNKVR3dqYVXPrP hqd4tjcG2/rXlo6HTPPzITMrdszML16sCjRHfr1bA2821295CgdtGN3hQ0oqGdxgIEN9 KDqj9gm/B1jE9gCI8ZP0IOI90yHor5J2cVczUgvqypWc+m/TPvT1ZvcAZJCE9UDEnpY3 0Nexo2a49Z2+K8Vxi7uKaPTdoICBWWVfmJqYw5EAfMusTimIbdVEWd28rTuXo1f2zjMg k0luGW8p9DrY6+JDzWB1wl3TaonkLwHEXDZOSJ0d/4O1XrzBAf4HAixbe3szg2CPDIM7 KaLg== X-Forwarded-Encrypted: i=1; AJvYcCXfGglD4ZflkLcPFxIQ/HbDRw/Y2HaLt1Yhmtq0nz6OTLV5Bf4evLvGc9cmXLFI6IbzzsW3c3q+0PikEtzp@vger.kernel.org X-Gm-Message-State: AOJu0YwnkYqbfH/hmq0whyC+hbK3eKeWQUR/LLoBE6fMYteFoIeu9HHZ kKKu/gCPbMCRR038IqBGVXoMyRhJQ7j6xrL9tgq+/RGteROPUG40lt5GmBwv1LFym46u8ZEggg= = X-Google-Smtp-Source: AGHT+IEjvTr2HYGSAjPDkNMw4G4npR8OoTHD1VZTAuD0hSm6xCoVT94NRgIvuCOHObtSUaBMEiPsUwonRg== X-Received: from wmqa16.prod.google.com ([2002:a05:600c:3490:b0:43c:fa2a:77d]) (user=tabba job=prod-delivery.src-stubby-dispatcher) by 2002:a05:600c:1d09:b0:43c:efed:732b with SMTP id 5b1f17b1804b1-43cefed78bcmr140968325e9.5.1741802326090; Wed, 12 Mar 2025 10:58:46 -0700 (PDT) Date: Wed, 12 Mar 2025 17:58:23 +0000 In-Reply-To: <20250312175824.1809636-1-tabba@google.com> Precedence: bulk X-Mailing-List: linux-arm-msm@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250312175824.1809636-1-tabba@google.com> X-Mailer: git-send-email 2.49.0.rc0.332.g42c0ae87b1-goog Message-ID: <20250312175824.1809636-11-tabba@google.com> Subject: [PATCH v6 10/10] 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, 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, peterx@redhat.com, tabba@google.com Expand the guest_memfd selftests to include testing mapping guest memory for VM types that support it. Also, build the guest_memfd selftest for aarch64. Signed-off-by: Fuad Tabba --- tools/testing/selftests/kvm/Makefile.kvm | 1 + .../testing/selftests/kvm/guest_memfd_test.c | 75 +++++++++++++++++-- 2 files changed, 70 insertions(+), 6 deletions(-) diff --git a/tools/testing/selftests/kvm/Makefile.kvm b/tools/testing/selftests/kvm/Makefile.kvm index 4277b983cace..c9a3f30e28dd 100644 --- a/tools/testing/selftests/kvm/Makefile.kvm +++ b/tools/testing/selftests/kvm/Makefile.kvm @@ -160,6 +160,7 @@ TEST_GEN_PROGS_arm64 += coalesced_io_test TEST_GEN_PROGS_arm64 += demand_paging_test TEST_GEN_PROGS_arm64 += dirty_log_test TEST_GEN_PROGS_arm64 += dirty_log_perf_test +TEST_GEN_PROGS_arm64 += guest_memfd_test TEST_GEN_PROGS_arm64 += guest_print_test TEST_GEN_PROGS_arm64 += get-reg-list TEST_GEN_PROGS_arm64 += 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 ce687f8d248f..38c501e49e0e 100644 --- a/tools/testing/selftests/kvm/guest_memfd_test.c +++ b/tools/testing/selftests/kvm/guest_memfd_test.c @@ -34,12 +34,48 @@ 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(); + const char val = 0xaa; + 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, val, total_size); + for (i = 0; i < total_size; i++) + TEST_ASSERT_EQ(mem[i], val); + + 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], val); + + memset(mem, val, total_size); + for (i = 0; i < total_size; i++) + TEST_ASSERT_EQ(mem[i], val); + + 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_file_size(int fd, size_t page_size, size_t total_size) @@ -170,19 +206,27 @@ static void test_create_guest_memfd_multiple(struct kvm_vm *vm) close(fd1); } -int main(int argc, char *argv[]) +unsigned long get_shared_type(void) { - size_t page_size; +#ifdef __x86_64__ + return KVM_X86_SW_PROTECTED_VM; +#endif + return 0; +} + +void test_vm_type(unsigned long type, bool is_shared) +{ + 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)); page_size = getpagesize(); total_size = page_size * 4; - vm = vm_create_barebones(); + vm = vm_create_barebones_type(type); test_create_guest_memfd_invalid(vm); test_create_guest_memfd_multiple(vm); @@ -190,10 +234,29 @@ int main(int argc, char *argv[]) fd = vm_create_guest_memfd(vm, total_size, 0); test_file_read_write(fd); - test_mmap(fd, page_size); + + if (is_shared) + test_mmap_allowed(fd, total_size); + else + test_mmap_denied(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); close(fd); + kvm_vm_release(vm); +} + +int main(int argc, char *argv[]) +{ +#ifndef __aarch64__ + /* For now, arm64 only supports shared guest memory. */ + test_vm_type(VM_TYPE_DEFAULT, false); +#endif + + if (kvm_has_cap(KVM_CAP_GMEM_SHARED_MEM)) + test_vm_type(get_shared_type(), true); + + return 0; }