From nobody Thu Dec 18 02:07:47 2025 Received: from fra-out-013.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-013.esa.eu-central-1.outbound.mail-perimeter.amazon.com [63.178.132.221]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 54192332902; Fri, 5 Dec 2025 16:58:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=63.178.132.221 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764953910; cv=none; b=NCkUSfShe9LxLckxmMqujVPhVaRh5KYbC3j/v9EIilDtYPLnqqNkdK17zYzKaai4K1JiL668ycvSUkokYlsW8hatLtsBga8Dy9me2njGnN2wQ7FfxhRX8yW8Ck8GSnWaAQEElXUOfZ+BnfKIz1w3Vz1iH3aVnnu51hZoqrbI6yk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764953910; c=relaxed/simple; bh=Vwbdvg++f2ttKWUxXJ9GmOhGjgSvNbfER95LUrDWy8o=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: Content-Type:MIME-Version; b=scWD5K3FK0G1+TYWC6/lhOTh30grtyTGCWaADAfFEYFTylZ+Hwui2VWYDXIUn1HKO17NCEkFfpLcriNHaqBr1PZPto/2OQB/pViO5d/Z4VUJaol+lOBTf63+L/S95OmFVgKD99jfoZ71TU0vG3oXS0ZkCVT3ElXXWeVvdhEm9/0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.co.uk; spf=pass smtp.mailfrom=amazon.co.uk; dkim=pass (2048-bit key) header.d=amazon.co.uk header.i=@amazon.co.uk header.b=o1trhqVm; arc=none smtp.client-ip=63.178.132.221 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.co.uk header.i=@amazon.co.uk header.b="o1trhqVm" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazoncorp2; t=1764953901; x=1796489901; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=OTta7SWGM4sSlljWxtUnxsblM5lcOxRPzU4BqTYg0Qg=; b=o1trhqVmc0qGmAlwXc+qL2Djupnk4K3ErFcuPgm4ARXvFuAkAAeFlmcf aBGaFqwxcIW7pEfkukTyPK2TgpgnAT09qhSH1iuB0XR94ghKoZR6qi38X wgZylmghp5C05MSVwyhqDGgrswRMy6X3pq5KNJKHjaPHTNW0JMIpeYfqL rijh5j/qce7+os/q2rASni1YzXRyMVlYYbNETN7q7X8R+rbm4mj5tCd/u 45qT9nMbBBEcVbHHk7nOUbOVwgvtKs44Vh0f73OpUgHaRb69foaT/yfz2 XfFdXkt1xcL6Xu17THrYnxoh7TRHpvvKE+B6yPJxzvRjuOsiau5g2WNRr w==; X-CSE-ConnectionGUID: tCSZJcjsRGW05HlXnAg+rw== X-CSE-MsgGUID: DdJLRKRZQbmfkk+SpQ0udg== X-IronPort-AV: E=Sophos;i="6.20,252,1758585600"; d="scan'208";a="6196568" Received: from ip-10-6-3-216.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.3.216]) by internal-fra-out-013.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Dec 2025 16:57:58 +0000 Received: from EX19MTAEUC001.ant.amazon.com [54.240.197.225:10986] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.26.175:2525] with esmtp (Farcaster) id 2bf591aa-fe6a-4778-8f28-ccc43634a12b; Fri, 5 Dec 2025 16:57:58 +0000 (UTC) X-Farcaster-Flow-ID: 2bf591aa-fe6a-4778-8f28-ccc43634a12b Received: from EX19D005EUB002.ant.amazon.com (10.252.51.103) by EX19MTAEUC001.ant.amazon.com (10.252.51.155) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.29; Fri, 5 Dec 2025 16:57:57 +0000 Received: from EX19D005EUB003.ant.amazon.com (10.252.51.31) by EX19D005EUB002.ant.amazon.com (10.252.51.103) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.29; Fri, 5 Dec 2025 16:57:57 +0000 Received: from EX19D005EUB003.ant.amazon.com ([fe80::b825:becb:4b38:da0c]) by EX19D005EUB003.ant.amazon.com ([fe80::b825:becb:4b38:da0c%3]) with mapi id 15.02.2562.029; Fri, 5 Dec 2025 16:57:57 +0000 From: "Kalyazin, Nikita" To: "kvm@vger.kernel.org" , "linux-doc@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "kvmarm@lists.linux.dev" , "linux-fsdevel@vger.kernel.org" , "linux-mm@kvack.org" , "bpf@vger.kernel.org" , "linux-kselftest@vger.kernel.org" CC: "pbonzini@redhat.com" , "corbet@lwn.net" , "maz@kernel.org" , "oupton@kernel.org" , "joey.gouly@arm.com" , "suzuki.poulose@arm.com" , "yuzenghui@huawei.com" , "catalin.marinas@arm.com" , "will@kernel.org" , "seanjc@google.com" , "tglx@linutronix.de" , "mingo@redhat.com" , "bp@alien8.de" , "dave.hansen@linux.intel.com" , "x86@kernel.org" , "hpa@zytor.com" , "luto@kernel.org" , "peterz@infradead.org" , "willy@infradead.org" , "akpm@linux-foundation.org" , "david@kernel.org" , "lorenzo.stoakes@oracle.com" , "Liam.Howlett@oracle.com" , "vbabka@suse.cz" , "rppt@kernel.org" , "surenb@google.com" , "mhocko@suse.com" , "ast@kernel.org" , "daniel@iogearbox.net" , "andrii@kernel.org" , "martin.lau@linux.dev" , "eddyz87@gmail.com" , "song@kernel.org" , "yonghong.song@linux.dev" , "john.fastabend@gmail.com" , "kpsingh@kernel.org" , "sdf@fomichev.me" , "haoluo@google.com" , "jolsa@kernel.org" , "jgg@ziepe.ca" , "jhubbard@nvidia.com" , "peterx@redhat.com" , "jannh@google.com" , "pfalcato@suse.de" , "shuah@kernel.org" , "riel@surriel.com" , "baohua@kernel.org" , "ryan.roberts@arm.com" , "jgross@suse.com" , "yu-cheng.yu@intel.com" , "kas@kernel.org" , "coxu@redhat.com" , "kevin.brodsky@arm.com" , "ackerleytng@google.com" , "maobibo@loongson.cn" , "prsampat@amd.com" , "mlevitsk@redhat.com" , "isaku.yamahata@intel.com" , "jmattson@google.com" , "jthoughton@google.com" , "linux-arm-kernel@lists.infradead.org" , "vannapurve@google.com" , "jackmanb@google.com" , "aneesh.kumar@kernel.org" , "patrick.roy@linux.dev" , "Thomson, Jack" , "Itazuri, Takahiro" , "Manwaring, Derek" , "Cali, Marco" , "Kalyazin, Nikita" Subject: [PATCH v8 01/13] x86: export set_direct_map_valid_noflush to KVM module Thread-Topic: [PATCH v8 01/13] x86: export set_direct_map_valid_noflush to KVM module Thread-Index: AQHcZghOBziVXkJRtEWVlaaPn0Dq3Q== Date: Fri, 5 Dec 2025 16:57:57 +0000 Message-ID: <20251205165743.9341-2-kalyazin@amazon.com> References: <20251205165743.9341-1-kalyazin@amazon.com> In-Reply-To: <20251205165743.9341-1-kalyazin@amazon.com> Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: Content-Transfer-Encoding: quoted-printable Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Patrick Roy Use the per-module export functionality to allow KVM (and only KVM) access to set_direct_map_valid_noflush(). This allows guest_memfd to remove its memory from the direct map, even if KVM is built as a module. Only do this on x86, as only x86 and arm64 support guest_memfd, and arm64 does not support building KVM as a module. Direct map removal gives guest_memfd the same protection that memfd_secret enjoys, such as hardening against Spectre-like attacks through in-kernel gadgets. Signed-off-by: Patrick Roy Signed-off-by: Nikita Kalyazin --- arch/x86/mm/pat/set_memory.c | 1 + 1 file changed, 1 insertion(+) diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index 970981893c9b..a7a88b598d99 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -2655,6 +2655,7 @@ int set_direct_map_valid_noflush(struct page *page, u= nsigned nr, bool valid) =20 return __set_pages_np(page, nr); } +EXPORT_SYMBOL_FOR_MODULES(set_direct_map_valid_noflush, "kvm"); =20 #ifdef CONFIG_DEBUG_PAGEALLOC void __kernel_map_pages(struct page *page, int numpages, int enable) --=20 2.50.1 From nobody Thu Dec 18 02:07:47 2025 Received: from fra-out-007.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-007.esa.eu-central-1.outbound.mail-perimeter.amazon.com [3.75.33.185]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 39B1A13A258; Fri, 5 Dec 2025 16:59:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=3.75.33.185 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764953989; cv=none; b=LRM/Q5R4AkKuuVT3ZLuFTgjx9Y/cZllhuR3o6nmg9tXqESGWmuYspxtxYoVLQDEbN+V97l1YM3JHB2WzvXFp+nSIzXxXV22KWFNnfEVea4ogVrh7f9V4jVPJtLdZdwhZlNj6xb2pdQ1MqefiRZnsoyKQtZgQu5ESIYQKQi/RdfQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764953989; c=relaxed/simple; bh=dn3h+9x0Guls9b+4MTJSqmKY7y8139bpAHZnXTB1xhQ=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: Content-Type:MIME-Version; b=t4eJyzy1OmZsZ2gSEG7SDxTj0hm/7p+REHrudM7UQ8uJI66l9yeWMnSiq/K6qAHPeJKAnOM2VAto90ivOf80RJYahCZ+A9/lV+e0YVOrBGpJbIypSeskupdOuYFLAKF/6vsdrgXzbWwxB8z5Fn0bvZx2XBEGRoTKjPL0jU3ALpA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.co.uk; spf=pass smtp.mailfrom=amazon.co.uk; dkim=pass (2048-bit key) header.d=amazon.co.uk header.i=@amazon.co.uk header.b=rO75HUE1; arc=none smtp.client-ip=3.75.33.185 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.co.uk header.i=@amazon.co.uk header.b="rO75HUE1" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazoncorp2; t=1764953985; x=1796489985; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=DAa+s0ltL2AWBGJLNkppOiWQM65ZNjJ1swchhaZKNTI=; b=rO75HUE1uvjdWcLqeVGeaDlu7Tp4gPVGLvCoHmzmey8vsX2xoNGTb8jF WwZeuu6mvS5Opb3BzxS9wHc4xqal6O8INo5/lwHNc+s+LIOT33Mwes6QW kV/Hng6Sxb698OLB9jFiOjZ/0sIoHZFsDpI+E7g+R6h/mX6VPstWSIpBQ hWUpPxxHxKspJwAvdIvAxKLaI0K3jO8H4jXFmpBpzYSZ5b71TCxM1QQuf QogXPLLWp7G9/bhKej2fuzzSSUyJJXEEkhr05RufX/e3mraPNbWCBjdqK V9/VIDh+2UdT4DgBJE65A0K7JzlozYiHxK5TmjInkAkHIqIcwcq1/LFL9 g==; X-CSE-ConnectionGUID: q0GqOJJ7TEuIVrXmD2UySw== X-CSE-MsgGUID: W7Hjf9rtRji/ZjNjf2UQHw== X-IronPort-AV: E=Sophos;i="6.20,252,1758585600"; d="scan'208";a="6301667" Received: from ip-10-6-6-97.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.6.97]) by internal-fra-out-007.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Dec 2025 16:58:09 +0000 Received: from EX19MTAEUA001.ant.amazon.com [54.240.197.233:3739] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.37.108:2525] with esmtp (Farcaster) id 30a38f77-2a8d-47e1-ad78-b78782ea5839; Fri, 5 Dec 2025 16:58:09 +0000 (UTC) X-Farcaster-Flow-ID: 30a38f77-2a8d-47e1-ad78-b78782ea5839 Received: from EX19D005EUB004.ant.amazon.com (10.252.51.126) by EX19MTAEUA001.ant.amazon.com (10.252.50.223) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.29; Fri, 5 Dec 2025 16:58:09 +0000 Received: from EX19D005EUB003.ant.amazon.com (10.252.51.31) by EX19D005EUB004.ant.amazon.com (10.252.51.126) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.29; Fri, 5 Dec 2025 16:58:08 +0000 Received: from EX19D005EUB003.ant.amazon.com ([fe80::b825:becb:4b38:da0c]) by EX19D005EUB003.ant.amazon.com ([fe80::b825:becb:4b38:da0c%3]) with mapi id 15.02.2562.029; Fri, 5 Dec 2025 16:58:08 +0000 From: "Kalyazin, Nikita" To: "kvm@vger.kernel.org" , "linux-doc@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "kvmarm@lists.linux.dev" , "linux-fsdevel@vger.kernel.org" , "linux-mm@kvack.org" , "bpf@vger.kernel.org" , "linux-kselftest@vger.kernel.org" CC: "pbonzini@redhat.com" , "corbet@lwn.net" , "maz@kernel.org" , "oupton@kernel.org" , "joey.gouly@arm.com" , "suzuki.poulose@arm.com" , "yuzenghui@huawei.com" , "catalin.marinas@arm.com" , "will@kernel.org" , "seanjc@google.com" , "tglx@linutronix.de" , "mingo@redhat.com" , "bp@alien8.de" , "dave.hansen@linux.intel.com" , "x86@kernel.org" , "hpa@zytor.com" , "luto@kernel.org" , "peterz@infradead.org" , "willy@infradead.org" , "akpm@linux-foundation.org" , "david@kernel.org" , "lorenzo.stoakes@oracle.com" , "Liam.Howlett@oracle.com" , "vbabka@suse.cz" , "rppt@kernel.org" , "surenb@google.com" , "mhocko@suse.com" , "ast@kernel.org" , "daniel@iogearbox.net" , "andrii@kernel.org" , "martin.lau@linux.dev" , "eddyz87@gmail.com" , "song@kernel.org" , "yonghong.song@linux.dev" , "john.fastabend@gmail.com" , "kpsingh@kernel.org" , "sdf@fomichev.me" , "haoluo@google.com" , "jolsa@kernel.org" , "jgg@ziepe.ca" , "jhubbard@nvidia.com" , "peterx@redhat.com" , "jannh@google.com" , "pfalcato@suse.de" , "shuah@kernel.org" , "riel@surriel.com" , "baohua@kernel.org" , "ryan.roberts@arm.com" , "jgross@suse.com" , "yu-cheng.yu@intel.com" , "kas@kernel.org" , "coxu@redhat.com" , "kevin.brodsky@arm.com" , "ackerleytng@google.com" , "maobibo@loongson.cn" , "prsampat@amd.com" , "mlevitsk@redhat.com" , "isaku.yamahata@intel.com" , "jmattson@google.com" , "jthoughton@google.com" , "linux-arm-kernel@lists.infradead.org" , "vannapurve@google.com" , "jackmanb@google.com" , "aneesh.kumar@kernel.org" , "patrick.roy@linux.dev" , "Thomson, Jack" , "Itazuri, Takahiro" , "Manwaring, Derek" , "Cali, Marco" , "Kalyazin, Nikita" Subject: [PATCH v8 02/13] x86/tlb: export flush_tlb_kernel_range to KVM module Thread-Topic: [PATCH v8 02/13] x86/tlb: export flush_tlb_kernel_range to KVM module Thread-Index: AQHcZghUt46N9MNDx0Sv0bdQIhD0sg== Date: Fri, 5 Dec 2025 16:58:08 +0000 Message-ID: <20251205165743.9341-3-kalyazin@amazon.com> References: <20251205165743.9341-1-kalyazin@amazon.com> In-Reply-To: <20251205165743.9341-1-kalyazin@amazon.com> Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: Content-Transfer-Encoding: quoted-printable Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Patrick Roy After direct map removal, a TLB flush must be done to ensure that the just-unmapped memory cannot be accessed through stale TLB entries. This export is only needed on x86, as arm64 (the only other architecture supporting guest_memfd currently) does not allow building KVM as a module. Cc: Thomas Gleixner Signed-off-by: Patrick Roy Signed-off-by: Nikita Kalyazin --- arch/x86/include/asm/tlbflush.h | 3 ++- arch/x86/mm/tlb.c | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/tlbflush.h b/arch/x86/include/asm/tlbflus= h.h index 00daedfefc1b..6f57f7eb621b 100644 --- a/arch/x86/include/asm/tlbflush.h +++ b/arch/x86/include/asm/tlbflush.h @@ -317,7 +317,6 @@ extern void flush_tlb_all(void); extern void flush_tlb_mm_range(struct mm_struct *mm, unsigned long start, unsigned long end, unsigned int stride_shift, bool freed_tables); -extern void flush_tlb_kernel_range(unsigned long start, unsigned long end); =20 static inline void flush_tlb_page(struct vm_area_struct *vma, unsigned lon= g a) { @@ -483,6 +482,8 @@ static inline void cpu_tlbstate_update_lam(unsigned lon= g lam, u64 untag_mask) #endif #endif /* !MODULE */ =20 +extern void flush_tlb_kernel_range(unsigned long start, unsigned long end); + static inline void __native_tlb_flush_global(unsigned long cr4) { native_write_cr4(cr4 ^ X86_CR4_PGE); diff --git a/arch/x86/mm/tlb.c b/arch/x86/mm/tlb.c index 5d221709353e..cce591d26e4c 100644 --- a/arch/x86/mm/tlb.c +++ b/arch/x86/mm/tlb.c @@ -1561,6 +1561,7 @@ void flush_tlb_kernel_range(unsigned long start, unsi= gned long end) =20 put_flush_tlb_info(); } +EXPORT_SYMBOL_FOR_MODULES(flush_tlb_kernel_range, "kvm"); =20 /* * This can be used from process context to figure out what the value of --=20 2.50.1 From nobody Thu Dec 18 02:07:47 2025 Received: from fra-out-006.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-006.esa.eu-central-1.outbound.mail-perimeter.amazon.com [18.197.217.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7FC4C3451A6; Fri, 5 Dec 2025 16:58:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=18.197.217.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764953931; cv=none; b=OXnkXpdH73vueTUJ2McH4cFxHkuI3r5CSPxmfVj9/unCYcvt6jxW9XTbt8HdOGOZE/b7Xb2GT9VnreoDZTfTG1Dg36Jreh2GEiCT/lPdUk+a7HknDr+Qc3/SdN6LZajRci2oLITIMWWyoBgkhvTb8f4Mw7S/ae8XxOtWro9LnXA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764953931; c=relaxed/simple; bh=4bavK0iph9koAlnHX2Wy2xe770aVguNdisa0TR0XACY=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: Content-Type:MIME-Version; b=vD0UOK+i4XGbAVmxTIBl6HpH20WVVi0xke0uBqxszjz/DngBRa2AiTk6/29e1vJAQqPXQqjHxFkc9FHx7nZJO7HMnSoIgkGh53IYxbJgOHTf0Aa583dWUM7pRnUk51l5D2wuswBnWRM5sArEbvpOkRKDQDyMEZ+1SbkZ3UMO90E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.co.uk; spf=pass smtp.mailfrom=amazon.co.uk; dkim=pass (2048-bit key) header.d=amazon.co.uk header.i=@amazon.co.uk header.b=M9UhwKuZ; arc=none smtp.client-ip=18.197.217.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.co.uk header.i=@amazon.co.uk header.b="M9UhwKuZ" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazoncorp2; t=1764953924; x=1796489924; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=XlbB/AHh0YQTX9rpyDTJlTKE99RJ4c4rYvfLyG5A7fY=; b=M9UhwKuZQrxbLRkABkd56gJKDSO7Vy0jFO/ty6wPW8SLUuAYFAZxykRX Cfh+FArrC13u4fOL4ZbFmgqilYNwF+d/q5j6FsATyl6lqCxn0LzPBC9sI 3gJA3C+/6QykNX43OUUgtxkWWIvdOjdqWLue6y8w/SK+8HYtCE7K7pmR5 SDEVbMBYpfkG2n7m4bmAIZVcqsUFiM/IWMhnf4CCECw4SW92//Js15Xoy HAaBrW+JHQ8dVMKjzc1h0eOsBc/0GQ63T1N+wKiHX0YhTBbT/E5COR+uk DN7BIz7LU+CN7fZfEL9D79yDcriL4bUv2iX/09x1cIIZlUqftCN9FKFB/ g==; X-CSE-ConnectionGUID: tZu3dFNkQvqA1ZyZ7qwk7w== X-CSE-MsgGUID: cc5HHGcuQSOXn+liRVfvJw== X-IronPort-AV: E=Sophos;i="6.20,252,1758585600"; d="scan'208";a="6301792" Received: from ip-10-6-11-83.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.11.83]) by internal-fra-out-006.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Dec 2025 16:58:21 +0000 Received: from EX19MTAEUA001.ant.amazon.com [54.240.197.233:7206] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.0.231:2525] with esmtp (Farcaster) id b66dbefc-1774-4ce5-8285-0803812e64ba; Fri, 5 Dec 2025 16:58:20 +0000 (UTC) X-Farcaster-Flow-ID: b66dbefc-1774-4ce5-8285-0803812e64ba Received: from EX19D005EUB001.ant.amazon.com (10.252.51.12) by EX19MTAEUA001.ant.amazon.com (10.252.50.223) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.29; Fri, 5 Dec 2025 16:58:20 +0000 Received: from EX19D005EUB003.ant.amazon.com (10.252.51.31) by EX19D005EUB001.ant.amazon.com (10.252.51.12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.29; Fri, 5 Dec 2025 16:58:20 +0000 Received: from EX19D005EUB003.ant.amazon.com ([fe80::b825:becb:4b38:da0c]) by EX19D005EUB003.ant.amazon.com ([fe80::b825:becb:4b38:da0c%3]) with mapi id 15.02.2562.029; Fri, 5 Dec 2025 16:58:20 +0000 From: "Kalyazin, Nikita" To: "kvm@vger.kernel.org" , "linux-doc@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "kvmarm@lists.linux.dev" , "linux-fsdevel@vger.kernel.org" , "linux-mm@kvack.org" , "bpf@vger.kernel.org" , "linux-kselftest@vger.kernel.org" CC: "pbonzini@redhat.com" , "corbet@lwn.net" , "maz@kernel.org" , "oupton@kernel.org" , "joey.gouly@arm.com" , "suzuki.poulose@arm.com" , "yuzenghui@huawei.com" , "catalin.marinas@arm.com" , "will@kernel.org" , "seanjc@google.com" , "tglx@linutronix.de" , "mingo@redhat.com" , "bp@alien8.de" , "dave.hansen@linux.intel.com" , "x86@kernel.org" , "hpa@zytor.com" , "luto@kernel.org" , "peterz@infradead.org" , "willy@infradead.org" , "akpm@linux-foundation.org" , "david@kernel.org" , "lorenzo.stoakes@oracle.com" , "Liam.Howlett@oracle.com" , "vbabka@suse.cz" , "rppt@kernel.org" , "surenb@google.com" , "mhocko@suse.com" , "ast@kernel.org" , "daniel@iogearbox.net" , "andrii@kernel.org" , "martin.lau@linux.dev" , "eddyz87@gmail.com" , "song@kernel.org" , "yonghong.song@linux.dev" , "john.fastabend@gmail.com" , "kpsingh@kernel.org" , "sdf@fomichev.me" , "haoluo@google.com" , "jolsa@kernel.org" , "jgg@ziepe.ca" , "jhubbard@nvidia.com" , "peterx@redhat.com" , "jannh@google.com" , "pfalcato@suse.de" , "shuah@kernel.org" , "riel@surriel.com" , "baohua@kernel.org" , "ryan.roberts@arm.com" , "jgross@suse.com" , "yu-cheng.yu@intel.com" , "kas@kernel.org" , "coxu@redhat.com" , "kevin.brodsky@arm.com" , "ackerleytng@google.com" , "maobibo@loongson.cn" , "prsampat@amd.com" , "mlevitsk@redhat.com" , "isaku.yamahata@intel.com" , "jmattson@google.com" , "jthoughton@google.com" , "linux-arm-kernel@lists.infradead.org" , "vannapurve@google.com" , "jackmanb@google.com" , "aneesh.kumar@kernel.org" , "patrick.roy@linux.dev" , "Thomson, Jack" , "Itazuri, Takahiro" , "Manwaring, Derek" , "Cali, Marco" , "Kalyazin, Nikita" Subject: [PATCH v8 03/13] mm: introduce AS_NO_DIRECT_MAP Thread-Topic: [PATCH v8 03/13] mm: introduce AS_NO_DIRECT_MAP Thread-Index: AQHcZghb1bjlwJbbGEq/dQeHI0u86A== Date: Fri, 5 Dec 2025 16:58:19 +0000 Message-ID: <20251205165743.9341-4-kalyazin@amazon.com> References: <20251205165743.9341-1-kalyazin@amazon.com> In-Reply-To: <20251205165743.9341-1-kalyazin@amazon.com> Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: Content-Transfer-Encoding: quoted-printable Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Patrick Roy Add AS_NO_DIRECT_MAP for mappings where direct map entries of folios are set to not present. Currently, mappings that match this description are secretmem mappings (memfd_secret()). Later, some guest_memfd configurations will also fall into this category. Reject this new type of mappings in all locations that currently reject secretmem mappings, on the assumption that if secretmem mappings are rejected somewhere, it is precisely because of an inability to deal with folios without direct map entries, and then make memfd_secret() use AS_NO_DIRECT_MAP on its address_space to drop its special vma_is_secretmem()/secretmem_mapping() checks. This drops a optimization in gup_fast_folio_allowed() where secretmem_mapping() was only called if CONFIG_SECRETMEM=3Dy. secretmem is enabled by default since commit b758fe6df50d ("mm/secretmem: make it on by default"), so the secretmem check did not actually end up elided in most cases anymore anyway. Use a new flag instead of overloading AS_INACCESSIBLE (which is already set by guest_memfd) because not all guest_memfd mappings will end up being direct map removed (e.g. in pKVM setups, parts of guest_memfd that can be mapped to userspace should also be GUP-able, and generally not have restrictions on who can access it). Acked-by: Mike Rapoport (Microsoft) Acked-by: David Hildenbrand (Red Hat)" Signed-off-by: Patrick Roy Signed-off-by: Nikita Kalyazin Acked-by: Vlastimil Babka --- include/linux/pagemap.h | 16 ++++++++++++++++ include/linux/secretmem.h | 18 ------------------ lib/buildid.c | 4 ++-- mm/gup.c | 19 +++++-------------- mm/mlock.c | 2 +- mm/secretmem.c | 8 ++------ 6 files changed, 26 insertions(+), 41 deletions(-) diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h index a17fabbc0269..d51e0c0404e2 100644 --- a/include/linux/pagemap.h +++ b/include/linux/pagemap.h @@ -213,6 +213,7 @@ enum mapping_flags { AS_WRITEBACK_MAY_DEADLOCK_ON_RECLAIM =3D 9, AS_KERNEL_FILE =3D 10, /* mapping for a fake kernel file that shouldn't account usage to user cgroups */ + AS_NO_DIRECT_MAP =3D 11, /* Folios in the mapping are not in the direct m= ap */ /* Bits 16-25 are used for FOLIO_ORDER */ AS_FOLIO_ORDER_BITS =3D 5, AS_FOLIO_ORDER_MIN =3D 16, @@ -348,6 +349,21 @@ static inline bool mapping_writeback_may_deadlock_on_r= eclaim(const struct addres return test_bit(AS_WRITEBACK_MAY_DEADLOCK_ON_RECLAIM, &mapping->flags); } =20 +static inline void mapping_set_no_direct_map(struct address_space *mapping) +{ + set_bit(AS_NO_DIRECT_MAP, &mapping->flags); +} + +static inline bool mapping_no_direct_map(const struct address_space *mappi= ng) +{ + return test_bit(AS_NO_DIRECT_MAP, &mapping->flags); +} + +static inline bool vma_has_no_direct_map(const struct vm_area_struct *vma) +{ + return vma->vm_file && mapping_no_direct_map(vma->vm_file->f_mapping); +} + static inline gfp_t mapping_gfp_mask(const struct address_space *mapping) { return mapping->gfp_mask; diff --git a/include/linux/secretmem.h b/include/linux/secretmem.h index e918f96881f5..0ae1fb057b3d 100644 --- a/include/linux/secretmem.h +++ b/include/linux/secretmem.h @@ -4,28 +4,10 @@ =20 #ifdef CONFIG_SECRETMEM =20 -extern const struct address_space_operations secretmem_aops; - -static inline bool secretmem_mapping(struct address_space *mapping) -{ - return mapping->a_ops =3D=3D &secretmem_aops; -} - -bool vma_is_secretmem(struct vm_area_struct *vma); bool secretmem_active(void); =20 #else =20 -static inline bool vma_is_secretmem(struct vm_area_struct *vma) -{ - return false; -} - -static inline bool secretmem_mapping(struct address_space *mapping) -{ - return false; -} - static inline bool secretmem_active(void) { return false; diff --git a/lib/buildid.c b/lib/buildid.c index c4b0f376fb34..89e567954284 100644 --- a/lib/buildid.c +++ b/lib/buildid.c @@ -65,8 +65,8 @@ static int freader_get_folio(struct freader *r, loff_t fi= le_off) =20 freader_put_folio(r); =20 - /* reject secretmem folios created with memfd_secret() */ - if (secretmem_mapping(r->file->f_mapping)) + /* reject folios without direct map entries (e.g. from memfd_secret() or = guest_memfd()) */ + if (mapping_no_direct_map(r->file->f_mapping)) return -EFAULT; =20 r->folio =3D filemap_get_folio(r->file->f_mapping, file_off >> PAGE_SHIFT= ); diff --git a/mm/gup.c b/mm/gup.c index a8ba5112e4d0..719e4dbecc09 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -11,7 +11,6 @@ #include #include #include -#include =20 #include #include @@ -1216,7 +1215,7 @@ static int check_vma_flags(struct vm_area_struct *vma= , unsigned long gup_flags) if ((gup_flags & FOLL_SPLIT_PMD) && is_vm_hugetlb_page(vma)) return -EOPNOTSUPP; =20 - if (vma_is_secretmem(vma)) + if (vma_has_no_direct_map(vma)) return -EFAULT; =20 if (write) { @@ -2724,7 +2723,7 @@ EXPORT_SYMBOL(get_user_pages_unlocked); * This call assumes the caller has pinned the folio, that the lowest page= table * level still points to this folio, and that interrupts have been disable= d. * - * GUP-fast must reject all secretmem folios. + * GUP-fast must reject all folios without direct map entries (such as sec= retmem). * * Writing to pinned file-backed dirty tracked folios is inherently proble= matic * (see comment describing the writable_file_mapping_allowed() function). = We @@ -2739,7 +2738,6 @@ static bool gup_fast_folio_allowed(struct folio *foli= o, unsigned int flags) { bool reject_file_backed =3D false; struct address_space *mapping; - bool check_secretmem =3D false; unsigned long mapping_flags; =20 /* @@ -2751,18 +2749,10 @@ static bool gup_fast_folio_allowed(struct folio *fo= lio, unsigned int flags) reject_file_backed =3D true; =20 /* We hold a folio reference, so we can safely access folio fields. */ - - /* secretmem folios are always order-0 folios. */ - if (IS_ENABLED(CONFIG_SECRETMEM) && !folio_test_large(folio)) - check_secretmem =3D true; - - if (!reject_file_backed && !check_secretmem) - return true; - if (WARN_ON_ONCE(folio_test_slab(folio))) return false; =20 - /* hugetlb neither requires dirty-tracking nor can be secretmem. */ + /* hugetlb neither requires dirty-tracking nor can be without direct map.= */ if (folio_test_hugetlb(folio)) return true; =20 @@ -2800,8 +2790,9 @@ static bool gup_fast_folio_allowed(struct folio *foli= o, unsigned int flags) * At this point, we know the mapping is non-null and points to an * address_space object. */ - if (check_secretmem && secretmem_mapping(mapping)) + if (mapping_no_direct_map(mapping)) return false; + /* The only remaining allowed file system is shmem. */ return !reject_file_backed || shmem_mapping(mapping); } diff --git a/mm/mlock.c b/mm/mlock.c index bb0776f5ef7c..506452ee8cad 100644 --- a/mm/mlock.c +++ b/mm/mlock.c @@ -474,7 +474,7 @@ static int mlock_fixup(struct vma_iterator *vmi, struct= vm_area_struct *vma, =20 if (newflags =3D=3D oldflags || (oldflags & VM_SPECIAL) || is_vm_hugetlb_page(vma) || vma =3D=3D get_gate_vma(current->mm) || - vma_is_dax(vma) || vma_is_secretmem(vma) || (oldflags & VM_DROPPABLE)) + vma_is_dax(vma) || vma_has_no_direct_map(vma) || (oldflags & VM_DROPP= ABLE)) /* don't set VM_LOCKED or VM_LOCKONFAULT and don't count */ goto out; =20 diff --git a/mm/secretmem.c b/mm/secretmem.c index b59350daffe3..c1eafe29c3e8 100644 --- a/mm/secretmem.c +++ b/mm/secretmem.c @@ -134,11 +134,6 @@ static int secretmem_mmap_prepare(struct vm_area_desc = *desc) return 0; } =20 -bool vma_is_secretmem(struct vm_area_struct *vma) -{ - return vma->vm_ops =3D=3D &secretmem_vm_ops; -} - static const struct file_operations secretmem_fops =3D { .release =3D secretmem_release, .mmap_prepare =3D secretmem_mmap_prepare, @@ -156,7 +151,7 @@ static void secretmem_free_folio(struct folio *folio) folio_zero_segment(folio, 0, folio_size(folio)); } =20 -const struct address_space_operations secretmem_aops =3D { +static const struct address_space_operations secretmem_aops =3D { .dirty_folio =3D noop_dirty_folio, .free_folio =3D secretmem_free_folio, .migrate_folio =3D secretmem_migrate_folio, @@ -205,6 +200,7 @@ static struct file *secretmem_file_create(unsigned long= flags) =20 mapping_set_gfp_mask(inode->i_mapping, GFP_HIGHUSER); mapping_set_unevictable(inode->i_mapping); + mapping_set_no_direct_map(inode->i_mapping); =20 inode->i_op =3D &secretmem_iops; inode->i_mapping->a_ops =3D &secretmem_aops; --=20 2.50.1 From nobody Thu Dec 18 02:07:47 2025 Received: from fra-out-002.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-002.esa.eu-central-1.outbound.mail-perimeter.amazon.com [3.65.3.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 00333345CAA; Fri, 5 Dec 2025 16:58:39 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=3.65.3.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764953928; cv=none; b=bkTtZQ9pBODBf2izGu/24jAaLllX2N7Y7KtjWnIra/uXJLC6NQ8YWwMJnqUespB0BKfSUq+pqT0aloi+C1RxjWJtR5D3fkoZsEsCZoAu+Uz1Xvc6xMaimH7jbHdMY1KJ8A1wthh79o6XKdgf4ZProlavUlY1YJTj8ZfkVNETp7o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764953928; c=relaxed/simple; bh=k7DMbX0wyubmJGZBX6TueBCo09mTrJp483ZqbOKKV4w=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: Content-Type:MIME-Version; b=onSEQHeQz+TY3YdlOJ/It/eFbtlAziw98vDkaSAXrNKoox0apxj9XSQvHr4HxxvifAbNDXKgL1yCOryWfT+lrUk5ZLy+Xd1lSZhWQiw/xk0S2rq/JW9Q2097MV77nICrqU58wvTCCqcnf2T0dw7GtH2GGDTADkoFrE8PMWBzphQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.co.uk; spf=pass smtp.mailfrom=amazon.co.uk; dkim=pass (2048-bit key) header.d=amazon.co.uk header.i=@amazon.co.uk header.b=CzS95BzF; arc=none smtp.client-ip=3.65.3.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.co.uk header.i=@amazon.co.uk header.b="CzS95BzF" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazoncorp2; t=1764953923; x=1796489923; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=EkhXi5kdHmtd1AcwEg7Y2Ijcajvi2mTnsK0V4nCbHJg=; b=CzS95BzF0iFNCbNhedbiu/+ktpl8x/9H6J6LZeS7zjsEQKfJxqljG7B1 GLkK327aWD3FHce80K6TWDHhO48QBLL1nFMrMtVLfqsdOMDnZNZldeKuf HT6Lsrb9xWZUDJIytGmvvyFSUnY82tz+jtQgF6Wo4c/FKaeItFFdzwk33 rn1vsnQ6ioYfSvKAtfukJR+wTfSwQUlYZ9DIe0Kp+rFVCacXLbpiX/6U3 cyLQwmwza4CpI0gqN6+4g7ApVONhZPV0zual3G/alnQfZLJKz35DeGYBo 8YOWUDn72piN/foAd/sqRH/YqGo72Njxvt3ZrUXwi5+HFwxSKIMNq36cY g==; X-CSE-ConnectionGUID: Y1gfsvFBTC+sTxrM0g1LzQ== X-CSE-MsgGUID: /iob45L3Ra68xjbcCTFGDA== X-IronPort-AV: E=Sophos;i="6.20,252,1758585600"; d="scan'208";a="6306253" Received: from ip-10-6-6-97.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.6.97]) by internal-fra-out-002.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Dec 2025 16:58:36 +0000 Received: from EX19MTAEUA001.ant.amazon.com [54.240.197.233:30659] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.6.214:2525] with esmtp (Farcaster) id 003a5827-a72c-426f-be78-9e2d9b40bfe8; Fri, 5 Dec 2025 16:58:36 +0000 (UTC) X-Farcaster-Flow-ID: 003a5827-a72c-426f-be78-9e2d9b40bfe8 Received: from EX19D005EUB002.ant.amazon.com (10.252.51.103) by EX19MTAEUA001.ant.amazon.com (10.252.50.223) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.29; Fri, 5 Dec 2025 16:58:32 +0000 Received: from EX19D005EUB003.ant.amazon.com (10.252.51.31) by EX19D005EUB002.ant.amazon.com (10.252.51.103) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.29; Fri, 5 Dec 2025 16:58:31 +0000 Received: from EX19D005EUB003.ant.amazon.com ([fe80::b825:becb:4b38:da0c]) by EX19D005EUB003.ant.amazon.com ([fe80::b825:becb:4b38:da0c%3]) with mapi id 15.02.2562.029; Fri, 5 Dec 2025 16:58:31 +0000 From: "Kalyazin, Nikita" To: "kvm@vger.kernel.org" , "linux-doc@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "kvmarm@lists.linux.dev" , "linux-fsdevel@vger.kernel.org" , "linux-mm@kvack.org" , "bpf@vger.kernel.org" , "linux-kselftest@vger.kernel.org" CC: "pbonzini@redhat.com" , "corbet@lwn.net" , "maz@kernel.org" , "oupton@kernel.org" , "joey.gouly@arm.com" , "suzuki.poulose@arm.com" , "yuzenghui@huawei.com" , "catalin.marinas@arm.com" , "will@kernel.org" , "seanjc@google.com" , "tglx@linutronix.de" , "mingo@redhat.com" , "bp@alien8.de" , "dave.hansen@linux.intel.com" , "x86@kernel.org" , "hpa@zytor.com" , "luto@kernel.org" , "peterz@infradead.org" , "willy@infradead.org" , "akpm@linux-foundation.org" , "david@kernel.org" , "lorenzo.stoakes@oracle.com" , "Liam.Howlett@oracle.com" , "vbabka@suse.cz" , "rppt@kernel.org" , "surenb@google.com" , "mhocko@suse.com" , "ast@kernel.org" , "daniel@iogearbox.net" , "andrii@kernel.org" , "martin.lau@linux.dev" , "eddyz87@gmail.com" , "song@kernel.org" , "yonghong.song@linux.dev" , "john.fastabend@gmail.com" , "kpsingh@kernel.org" , "sdf@fomichev.me" , "haoluo@google.com" , "jolsa@kernel.org" , "jgg@ziepe.ca" , "jhubbard@nvidia.com" , "peterx@redhat.com" , "jannh@google.com" , "pfalcato@suse.de" , "shuah@kernel.org" , "riel@surriel.com" , "baohua@kernel.org" , "ryan.roberts@arm.com" , "jgross@suse.com" , "yu-cheng.yu@intel.com" , "kas@kernel.org" , "coxu@redhat.com" , "kevin.brodsky@arm.com" , "ackerleytng@google.com" , "maobibo@loongson.cn" , "prsampat@amd.com" , "mlevitsk@redhat.com" , "isaku.yamahata@intel.com" , "jmattson@google.com" , "jthoughton@google.com" , "linux-arm-kernel@lists.infradead.org" , "vannapurve@google.com" , "jackmanb@google.com" , "aneesh.kumar@kernel.org" , "patrick.roy@linux.dev" , "Thomson, Jack" , "Itazuri, Takahiro" , "Manwaring, Derek" , "Cali, Marco" , "Kalyazin, Nikita" Subject: [PATCH v8 04/13] KVM: guest_memfd: Add stub for kvm_arch_gmem_invalidate Thread-Topic: [PATCH v8 04/13] KVM: guest_memfd: Add stub for kvm_arch_gmem_invalidate Thread-Index: AQHcZghi3A+Ke3NY2kGcY3fh9aT3hA== Date: Fri, 5 Dec 2025 16:58:31 +0000 Message-ID: <20251205165743.9341-5-kalyazin@amazon.com> References: <20251205165743.9341-1-kalyazin@amazon.com> In-Reply-To: <20251205165743.9341-1-kalyazin@amazon.com> Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: Content-Transfer-Encoding: quoted-printable Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Patrick Roy Add a no-op stub for kvm_arch_gmem_invalidate if CONFIG_HAVE_KVM_ARCH_GMEM_INVALIDATE=3Dn. This allows defining kvm_gmem_free_folio without ifdef-ery, which allows more cleanly using guest_memfd's free_folio callback for non-arch-invalidation related code. Acked-by: David Hildenbrand (Red Hat)" Signed-off-by: Nikita Kalyazin Acked-by: Vlastimil Babka --- include/linux/kvm_host.h | 2 ++ virt/kvm/guest_memfd.c | 4 ---- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index d93f75b05ae2..27796a09d29b 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -2589,6 +2589,8 @@ long kvm_gmem_populate(struct kvm *kvm, gfn_t gfn, vo= id __user *src, long npages =20 #ifdef CONFIG_HAVE_KVM_ARCH_GMEM_INVALIDATE void kvm_arch_gmem_invalidate(kvm_pfn_t start, kvm_pfn_t end); +#else +static inline void kvm_arch_gmem_invalidate(kvm_pfn_t start, kvm_pfn_t end= ) { } #endif =20 #ifdef CONFIG_KVM_GENERIC_PRE_FAULT_MEMORY diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index fdaea3422c30..92e7f8c1f303 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -527,7 +527,6 @@ static int kvm_gmem_error_folio(struct address_space *m= apping, struct folio *fol return MF_DELAYED; } =20 -#ifdef CONFIG_HAVE_KVM_ARCH_GMEM_INVALIDATE static void kvm_gmem_free_folio(struct folio *folio) { struct page *page =3D folio_page(folio, 0); @@ -536,15 +535,12 @@ static void kvm_gmem_free_folio(struct folio *folio) =20 kvm_arch_gmem_invalidate(pfn, pfn + (1ul << order)); } -#endif =20 static const struct address_space_operations kvm_gmem_aops =3D { .dirty_folio =3D noop_dirty_folio, .migrate_folio =3D kvm_gmem_migrate_folio, .error_remove_folio =3D kvm_gmem_error_folio, -#ifdef CONFIG_HAVE_KVM_ARCH_GMEM_INVALIDATE .free_folio =3D kvm_gmem_free_folio, -#endif }; =20 static int kvm_gmem_setattr(struct mnt_idmap *idmap, struct dentry *dentry, --=20 2.50.1 From nobody Thu Dec 18 02:07:47 2025 Received: from fra-out-010.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-010.esa.eu-central-1.outbound.mail-perimeter.amazon.com [63.178.143.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D57B033B6EC; Fri, 5 Dec 2025 16:59:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=63.178.143.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764953968; cv=none; b=ayENAE5IZ9P4DenTRO2VlNGFxBxOfINrYfRCIJtUcC6EJiNprMX3KReyPQhLpg7xoWmO7Dg/R9q4ugG8VWPs7wYmaemul3TPn8VXpob4tPDHHpf4YptRQ758D3uBZKLabwIIYetf+WKRLVuYNx8b32bp42PQ/PBG95dS+sNgq4Q= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764953968; c=relaxed/simple; bh=uhOnoz0vRA0Mw6TMzG3XyGqNrE21ewYHC3GSSUy61/k=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: Content-Type:MIME-Version; b=H1g9qrcXctbDHMGCZXmnfcfs+lcC3zBm6i8/Dxgy/MSfeE5z3T2kUHsSlynTvbBuOotE473LVdJz88pU3xZ/VqCdR4oXeyp/Z6A9v7JtViADkVSHPHWeaHWoLSURe3MmCr92R7CWJMDmafW1MQIkThpbdpS5XwYIvYfB83UYhtM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.co.uk; spf=pass smtp.mailfrom=amazon.co.uk; dkim=pass (2048-bit key) header.d=amazon.co.uk header.i=@amazon.co.uk header.b=AtmH1GMu; arc=none smtp.client-ip=63.178.143.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.co.uk header.i=@amazon.co.uk header.b="AtmH1GMu" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazoncorp2; t=1764953949; x=1796489949; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=ioJNos7hxVKCZgZqEai7R/nyP1L59yWcXHZaL1WkvB4=; b=AtmH1GMu5wunsBUX2Pqtyk37LVPvx21PVDRQsIIstgBvu0R71hMSt2dr PVxNRq3tmsZRWGIwf2YeTpaar4iAB490YJJhmVHX7yc6jxsyN0RaFAVKZ xwJj9tKtC3HPnljVR1huJA8551VMVcIUaOvJXa2O/AXIH2GlZgyf/zAxa Qr0w2X9cZL9BqRAv2ChiYmrwW+QLizSTcfu+E7fhXn380SZOpMGiYvg8n 3ySvnZiX8KenkHxzQ4iBcLBsyCe2FutFMqLPHPWz3y8wHehIY4dt6B1mE s9eDYREc0+SR52xx2WWcAYwuJscKu4tJBv9177sVE92gG0Q2oE4BDhTKJ A==; X-CSE-ConnectionGUID: iB+IdF+LR5aqvP28mlAo6g== X-CSE-MsgGUID: Oewbkl1pSgGBoL2ZAj/lYw== X-IronPort-AV: E=Sophos;i="6.20,252,1758585600"; d="scan'208";a="6199842" Received: from ip-10-6-11-83.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.11.83]) by internal-fra-out-010.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Dec 2025 16:58:44 +0000 Received: from EX19MTAEUA002.ant.amazon.com [54.240.197.232:25188] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.34.107:2525] with esmtp (Farcaster) id 64f2d293-0d8e-44f4-ba51-89912e231554; Fri, 5 Dec 2025 16:58:44 +0000 (UTC) X-Farcaster-Flow-ID: 64f2d293-0d8e-44f4-ba51-89912e231554 Received: from EX19D005EUB004.ant.amazon.com (10.252.51.126) by EX19MTAEUA002.ant.amazon.com (10.252.50.126) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.29; Fri, 5 Dec 2025 16:58:44 +0000 Received: from EX19D005EUB003.ant.amazon.com (10.252.51.31) by EX19D005EUB004.ant.amazon.com (10.252.51.126) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.29; Fri, 5 Dec 2025 16:58:43 +0000 Received: from EX19D005EUB003.ant.amazon.com ([fe80::b825:becb:4b38:da0c]) by EX19D005EUB003.ant.amazon.com ([fe80::b825:becb:4b38:da0c%3]) with mapi id 15.02.2562.029; Fri, 5 Dec 2025 16:58:43 +0000 From: "Kalyazin, Nikita" To: "kvm@vger.kernel.org" , "linux-doc@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "kvmarm@lists.linux.dev" , "linux-fsdevel@vger.kernel.org" , "linux-mm@kvack.org" , "bpf@vger.kernel.org" , "linux-kselftest@vger.kernel.org" CC: "pbonzini@redhat.com" , "corbet@lwn.net" , "maz@kernel.org" , "oupton@kernel.org" , "joey.gouly@arm.com" , "suzuki.poulose@arm.com" , "yuzenghui@huawei.com" , "catalin.marinas@arm.com" , "will@kernel.org" , "seanjc@google.com" , "tglx@linutronix.de" , "mingo@redhat.com" , "bp@alien8.de" , "dave.hansen@linux.intel.com" , "x86@kernel.org" , "hpa@zytor.com" , "luto@kernel.org" , "peterz@infradead.org" , "willy@infradead.org" , "akpm@linux-foundation.org" , "david@kernel.org" , "lorenzo.stoakes@oracle.com" , "Liam.Howlett@oracle.com" , "vbabka@suse.cz" , "rppt@kernel.org" , "surenb@google.com" , "mhocko@suse.com" , "ast@kernel.org" , "daniel@iogearbox.net" , "andrii@kernel.org" , "martin.lau@linux.dev" , "eddyz87@gmail.com" , "song@kernel.org" , "yonghong.song@linux.dev" , "john.fastabend@gmail.com" , "kpsingh@kernel.org" , "sdf@fomichev.me" , "haoluo@google.com" , "jolsa@kernel.org" , "jgg@ziepe.ca" , "jhubbard@nvidia.com" , "peterx@redhat.com" , "jannh@google.com" , "pfalcato@suse.de" , "shuah@kernel.org" , "riel@surriel.com" , "baohua@kernel.org" , "ryan.roberts@arm.com" , "jgross@suse.com" , "yu-cheng.yu@intel.com" , "kas@kernel.org" , "coxu@redhat.com" , "kevin.brodsky@arm.com" , "ackerleytng@google.com" , "maobibo@loongson.cn" , "prsampat@amd.com" , "mlevitsk@redhat.com" , "isaku.yamahata@intel.com" , "jmattson@google.com" , "jthoughton@google.com" , "linux-arm-kernel@lists.infradead.org" , "vannapurve@google.com" , "jackmanb@google.com" , "aneesh.kumar@kernel.org" , "patrick.roy@linux.dev" , "Thomson, Jack" , "Itazuri, Takahiro" , "Manwaring, Derek" , "Cali, Marco" , "Kalyazin, Nikita" Subject: [PATCH v8 05/13] KVM: guest_memfd: Add flag to remove from direct map Thread-Topic: [PATCH v8 05/13] KVM: guest_memfd: Add flag to remove from direct map Thread-Index: AQHcZghpe03+EzzQP0GBbIS7sNUUeQ== Date: Fri, 5 Dec 2025 16:58:43 +0000 Message-ID: <20251205165743.9341-6-kalyazin@amazon.com> References: <20251205165743.9341-1-kalyazin@amazon.com> In-Reply-To: <20251205165743.9341-1-kalyazin@amazon.com> Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: Content-Transfer-Encoding: quoted-printable Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Patrick Roy Add GUEST_MEMFD_FLAG_NO_DIRECT_MAP flag for KVM_CREATE_GUEST_MEMFD() ioctl. When set, guest_memfd folios will be removed from the direct map after preparation, with direct map entries only restored when the folios are freed. To ensure these folios do not end up in places where the kernel cannot deal with them, set AS_NO_DIRECT_MAP on the guest_memfd's struct address_space if GUEST_MEMFD_FLAG_NO_DIRECT_MAP is requested. Note that this flag causes removal of direct map entries for all guest_memfd folios independent of whether they are "shared" or "private" (although current guest_memfd only supports either all folios in the "shared" state, or all folios in the "private" state if GUEST_MEMFD_FLAG_MMAP is not set). The usecase for removing direct map entries of also the shared parts of guest_memfd are a special type of non-CoCo VM where, host userspace is trusted to have access to all of guest memory, but where Spectre-style transient execution attacks through the host kernel's direct map should still be mitigated. In this setup, KVM retains access to guest memory via userspace mappings of guest_memfd, which are reflected back into KVM's memslots via userspace_addr. This is needed for things like MMIO emulation on x86_64 to work. Direct map entries are zapped right before guest or userspace mappings of gmem folios are set up, e.g. in kvm_gmem_fault_user_mapping() or kvm_gmem_get_pfn() [called from the KVM MMU code]. The only place where a gmem folio can be allocated without being mapped anywhere is kvm_gmem_populate(), where handling potential failures of direct map removal is not possible (by the time direct map removal is attempted, the folio is already marked as prepared, meaning attempting to re-try kvm_gmem_populate() would just result in -EEXIST without fixing up the direct map state). These folios are then removed form the direct map upon kvm_gmem_get_pfn(), e.g. when they are mapped into the guest later. Signed-off-by: Patrick Roy Signed-off-by: Nikita Kalyazin --- Documentation/virt/kvm/api.rst | 22 ++++++++----- include/linux/kvm_host.h | 12 +++++++ include/uapi/linux/kvm.h | 1 + virt/kvm/guest_memfd.c | 60 ++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 9 deletions(-) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index 01a3abef8abb..c5f54f1370c8 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6440,15 +6440,19 @@ a single guest_memfd file, but the bound ranges mus= t not overlap). The capability KVM_CAP_GUEST_MEMFD_FLAGS enumerates the `flags` that can be specified via KVM_CREATE_GUEST_MEMFD. Currently defined flags: =20 - =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D - GUEST_MEMFD_FLAG_MMAP Enable using mmap() on the guest_memfd file - descriptor. - GUEST_MEMFD_FLAG_INIT_SHARED Make all memory in the file shared during - KVM_CREATE_GUEST_MEMFD (memory files created - without INIT_SHARED will be marked private). - Shared memory can be faulted into host user= space - page tables. Private memory cannot. - =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D + =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D + GUEST_MEMFD_FLAG_MMAP Enable using mmap() on the guest_memfd fi= le + descriptor. + GUEST_MEMFD_FLAG_INIT_SHARED Make all memory in the file shared during + KVM_CREATE_GUEST_MEMFD (memory files crea= ted + without INIT_SHARED will be marked privat= e). + Shared memory can be faulted into host us= erspace + page tables. Private memory cannot. + GUEST_MEMFD_FLAG_NO_DIRECT_MAP The guest_memfd instance will behave simi= larly + to memfd_secret, and unmaps the memory ba= cking + it from the kernel's address space before + being passed off to userspace or the gues= t. + =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D =20 When the KVM MMU performs a PFN lookup to service a guest fault and the ba= cking guest_memfd has the GUEST_MEMFD_FLAG_MMAP set, then the fault will always = be diff --git a/include/linux/kvm_host.h b/include/linux/kvm_host.h index 27796a09d29b..d4d5306075bf 100644 --- a/include/linux/kvm_host.h +++ b/include/linux/kvm_host.h @@ -738,10 +738,22 @@ static inline u64 kvm_gmem_get_supported_flags(struct= kvm *kvm) if (!kvm || kvm_arch_supports_gmem_init_shared(kvm)) flags |=3D GUEST_MEMFD_FLAG_INIT_SHARED; =20 + if (kvm_arch_gmem_supports_no_direct_map()) + flags |=3D GUEST_MEMFD_FLAG_NO_DIRECT_MAP; + return flags; } #endif =20 +#ifdef CONFIG_KVM_GUEST_MEMFD +#ifndef kvm_arch_gmem_supports_no_direct_map +static inline bool kvm_arch_gmem_supports_no_direct_map(void) +{ + return false; +} +#endif +#endif /* CONFIG_KVM_GUEST_MEMFD */ + #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 dddb781b0507..60341e1ba1be 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1612,6 +1612,7 @@ struct kvm_memory_attributes { #define KVM_CREATE_GUEST_MEMFD _IOWR(KVMIO, 0xd4, struct kvm_create_guest= _memfd) #define GUEST_MEMFD_FLAG_MMAP (1ULL << 0) #define GUEST_MEMFD_FLAG_INIT_SHARED (1ULL << 1) +#define GUEST_MEMFD_FLAG_NO_DIRECT_MAP (1ULL << 2) =20 struct kvm_create_guest_memfd { __u64 size; diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 92e7f8c1f303..ec4966a47d5e 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -7,6 +7,9 @@ #include #include #include +#include + +#include =20 #include "kvm_mm.h" =20 @@ -76,6 +79,49 @@ static int __kvm_gmem_prepare_folio(struct kvm *kvm, str= uct kvm_memory_slot *slo return 0; } =20 +#define KVM_GMEM_FOLIO_NO_DIRECT_MAP BIT(0) + +static bool kvm_gmem_folio_no_direct_map(struct folio *folio) +{ + return ((u64) folio->private) & KVM_GMEM_FOLIO_NO_DIRECT_MAP; +} + +static int kvm_gmem_folio_zap_direct_map(struct folio *folio) +{ + int r =3D 0; + unsigned long addr =3D (unsigned long) folio_address(folio); + u64 gmem_flags =3D GMEM_I(folio_inode(folio))->flags; + + if (kvm_gmem_folio_no_direct_map(folio) || !(gmem_flags & GUEST_MEMFD_FLA= G_NO_DIRECT_MAP)) + goto out; + + r =3D set_direct_map_valid_noflush(folio_page(folio, 0), folio_nr_pages(f= olio), + false); + + if (r) + goto out; + + folio->private =3D (void *) KVM_GMEM_FOLIO_NO_DIRECT_MAP; + flush_tlb_kernel_range(addr, addr + folio_size(folio)); + +out: + return r; +} + +static void kvm_gmem_folio_restore_direct_map(struct folio *folio) +{ + /* + * Direct map restoration cannot fail, as the only error condition + * for direct map manipulation is failure to allocate page tables + * when splitting huge pages, but this split would have already + * happened in set_direct_map_invalid_noflush() in kvm_gmem_folio_zap_dir= ect_map(). + * Thus set_direct_map_valid_noflush() here only updates prot bits. + */ + if (kvm_gmem_folio_no_direct_map(folio)) + set_direct_map_valid_noflush(folio_page(folio, 0), folio_nr_pages(folio), + true); +} + static inline void kvm_gmem_mark_prepared(struct folio *folio) { folio_mark_uptodate(folio); @@ -398,6 +444,7 @@ static vm_fault_t kvm_gmem_fault_user_mapping(struct vm= _fault *vmf) struct inode *inode =3D file_inode(vmf->vma->vm_file); struct folio *folio; vm_fault_t ret =3D VM_FAULT_LOCKED; + int err; =20 if (((loff_t)vmf->pgoff << PAGE_SHIFT) >=3D i_size_read(inode)) return VM_FAULT_SIGBUS; @@ -423,6 +470,12 @@ static vm_fault_t kvm_gmem_fault_user_mapping(struct v= m_fault *vmf) kvm_gmem_mark_prepared(folio); } =20 + err =3D kvm_gmem_folio_zap_direct_map(folio); + if (err) { + ret =3D vmf_error(err); + goto out_folio; + } + vmf->page =3D folio_file_page(folio, vmf->pgoff); =20 out_folio: @@ -533,6 +586,8 @@ static void kvm_gmem_free_folio(struct folio *folio) kvm_pfn_t pfn =3D page_to_pfn(page); int order =3D folio_order(folio); =20 + kvm_gmem_folio_restore_direct_map(folio); + kvm_arch_gmem_invalidate(pfn, pfn + (1ul << order)); } =20 @@ -596,6 +651,9 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t si= ze, u64 flags) /* Unmovable mappings are supposed to be marked unevictable as well. */ WARN_ON_ONCE(!mapping_unevictable(inode->i_mapping)); =20 + if (flags & GUEST_MEMFD_FLAG_NO_DIRECT_MAP) + mapping_set_no_direct_map(inode->i_mapping); + GMEM_I(inode)->flags =3D flags; =20 file =3D alloc_file_pseudo(inode, kvm_gmem_mnt, name, O_RDWR, &kvm_gmem_f= ops); @@ -807,6 +865,8 @@ int kvm_gmem_get_pfn(struct kvm *kvm, struct kvm_memory= _slot *slot, if (!is_prepared) r =3D kvm_gmem_prepare_folio(kvm, slot, gfn, folio); =20 + kvm_gmem_folio_zap_direct_map(folio); + folio_unlock(folio); =20 if (!r) --=20 2.50.1 From nobody Thu Dec 18 02:07:47 2025 Received: from fra-out-011.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-011.esa.eu-central-1.outbound.mail-perimeter.amazon.com [52.28.197.132]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0202A346FB3; Fri, 5 Dec 2025 16:59:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=52.28.197.132 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764953973; cv=none; b=iMZQM9CUzMl17r3JsDzkBo8b6L04LrYzrG+ag7d2mv9jO5j6rvaGrNuJEcfIe2x0zj6sbEf+/uzBLoW3SWkWs8wOgqJFCy80geQwYEYP0CySx2aUIXSLvm9Gw9WbuxL9Rzzd7dyRFIKtxzVr2GGyoJ/sNr96agKIHkwRNwLygfc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764953973; c=relaxed/simple; bh=uscAv0ZOrJ7JzQOd3sbmaT4mGOxC0tA+wUUmeuMqHpg=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: Content-Type:MIME-Version; b=DoBuvG4I38D3o5ZgAUuEZBqU9NV3VfhmTnmj5acugghc+AqW8lKTXdd2dakoyRFW8uAAsComSs2f1kTef511jPvfvxjmxqXewdRPhYez04CrJhKkVDU0tbLD9TOnm5aXlMFp3goYqTfXE+wDC+ePl9uv+UlweTVpksfQFYhYdMU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.co.uk; spf=pass smtp.mailfrom=amazon.co.uk; dkim=pass (2048-bit key) header.d=amazon.co.uk header.i=@amazon.co.uk header.b=SywS7VRk; arc=none smtp.client-ip=52.28.197.132 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.co.uk header.i=@amazon.co.uk header.b="SywS7VRk" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazoncorp2; t=1764953961; x=1796489961; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=Qx10YQyM1n7Bzq/TmzHM+nwXVd+92kjURQXs15/Kk1I=; b=SywS7VRkKJM9KJb4ld5sOJM50XOwZDRQmZP8ACWbFe28G4+rx5U1BQ1d RrvTkvaigNyN61WjWHVH+q5qVwV77ZsxzyXv01V2ST5GWDt2B8sRmAeJ8 r4WOCiksP0AbYg6EGh3t+0kRoVbel5YvTUsDZ4qfsffRO89Lg/3lQxsw4 44NdQuS48d/PxNdK13nV/U+n9/MeuC+7UuqYNdu/REIuKaU2kKIccEHD0 DeP6Zx2M4zsH46peNIvjf00XZcrkHCIOcursPsn0a6oEGBS+peuokw1Ag fzMtrR2+eCSud4416g4X0pk6hs0GvuyfSMezj/3wTeJgFOAaIipVBJStx g==; X-CSE-ConnectionGUID: 6oIRaqr8Rkuu7bomYo5Pgg== X-CSE-MsgGUID: Du9SD6hVSkaAOIpNm4V2fg== X-IronPort-AV: E=Sophos;i="6.20,252,1758585600"; d="scan'208";a="6191211" Received: from ip-10-6-11-83.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.11.83]) by internal-fra-out-011.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Dec 2025 16:58:56 +0000 Received: from EX19MTAEUA002.ant.amazon.com [54.240.197.232:29292] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.37.108:2525] with esmtp (Farcaster) id d6b08ff2-69cd-4ef4-91c7-3c44faa72354; Fri, 5 Dec 2025 16:58:56 +0000 (UTC) X-Farcaster-Flow-ID: d6b08ff2-69cd-4ef4-91c7-3c44faa72354 Received: from EX19D005EUB001.ant.amazon.com (10.252.51.12) by EX19MTAEUA002.ant.amazon.com (10.252.50.126) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.29; Fri, 5 Dec 2025 16:58:56 +0000 Received: from EX19D005EUB003.ant.amazon.com (10.252.51.31) by EX19D005EUB001.ant.amazon.com (10.252.51.12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.29; Fri, 5 Dec 2025 16:58:55 +0000 Received: from EX19D005EUB003.ant.amazon.com ([fe80::b825:becb:4b38:da0c]) by EX19D005EUB003.ant.amazon.com ([fe80::b825:becb:4b38:da0c%3]) with mapi id 15.02.2562.029; Fri, 5 Dec 2025 16:58:55 +0000 From: "Kalyazin, Nikita" To: "kvm@vger.kernel.org" , "linux-doc@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "kvmarm@lists.linux.dev" , "linux-fsdevel@vger.kernel.org" , "linux-mm@kvack.org" , "bpf@vger.kernel.org" , "linux-kselftest@vger.kernel.org" CC: "pbonzini@redhat.com" , "corbet@lwn.net" , "maz@kernel.org" , "oupton@kernel.org" , "joey.gouly@arm.com" , "suzuki.poulose@arm.com" , "yuzenghui@huawei.com" , "catalin.marinas@arm.com" , "will@kernel.org" , "seanjc@google.com" , "tglx@linutronix.de" , "mingo@redhat.com" , "bp@alien8.de" , "dave.hansen@linux.intel.com" , "x86@kernel.org" , "hpa@zytor.com" , "luto@kernel.org" , "peterz@infradead.org" , "willy@infradead.org" , "akpm@linux-foundation.org" , "david@kernel.org" , "lorenzo.stoakes@oracle.com" , "Liam.Howlett@oracle.com" , "vbabka@suse.cz" , "rppt@kernel.org" , "surenb@google.com" , "mhocko@suse.com" , "ast@kernel.org" , "daniel@iogearbox.net" , "andrii@kernel.org" , "martin.lau@linux.dev" , "eddyz87@gmail.com" , "song@kernel.org" , "yonghong.song@linux.dev" , "john.fastabend@gmail.com" , "kpsingh@kernel.org" , "sdf@fomichev.me" , "haoluo@google.com" , "jolsa@kernel.org" , "jgg@ziepe.ca" , "jhubbard@nvidia.com" , "peterx@redhat.com" , "jannh@google.com" , "pfalcato@suse.de" , "shuah@kernel.org" , "riel@surriel.com" , "baohua@kernel.org" , "ryan.roberts@arm.com" , "jgross@suse.com" , "yu-cheng.yu@intel.com" , "kas@kernel.org" , "coxu@redhat.com" , "kevin.brodsky@arm.com" , "ackerleytng@google.com" , "maobibo@loongson.cn" , "prsampat@amd.com" , "mlevitsk@redhat.com" , "isaku.yamahata@intel.com" , "jmattson@google.com" , "jthoughton@google.com" , "linux-arm-kernel@lists.infradead.org" , "vannapurve@google.com" , "jackmanb@google.com" , "aneesh.kumar@kernel.org" , "patrick.roy@linux.dev" , "Thomson, Jack" , "Itazuri, Takahiro" , "Manwaring, Derek" , "Cali, Marco" , "Kalyazin, Nikita" Subject: [PATCH v8 06/13] KVM: x86: define kvm_arch_gmem_supports_no_direct_map() Thread-Topic: [PATCH v8 06/13] KVM: x86: define kvm_arch_gmem_supports_no_direct_map() Thread-Index: AQHcZghwpwkQ2L3MZ0qB0yKOOmzCSA== Date: Fri, 5 Dec 2025 16:58:55 +0000 Message-ID: <20251205165743.9341-7-kalyazin@amazon.com> References: <20251205165743.9341-1-kalyazin@amazon.com> In-Reply-To: <20251205165743.9341-1-kalyazin@amazon.com> Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: Content-Transfer-Encoding: quoted-printable Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Patrick Roy x86 supports GUEST_MEMFD_FLAG_NO_DIRECT_MAP whenever direct map modifications are possible (which is always the case). Signed-off-by: Patrick Roy Signed-off-by: Nikita Kalyazin --- arch/x86/include/asm/kvm_host.h | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_hos= t.h index 5a3bfa293e8b..68bd29a52f24 100644 --- a/arch/x86/include/asm/kvm_host.h +++ b/arch/x86/include/asm/kvm_host.h @@ -28,6 +28,7 @@ #include #include #include +#include =20 #include #include @@ -2481,4 +2482,12 @@ static inline bool kvm_arch_has_irq_bypass(void) return enable_device_posted_irqs; } =20 +#ifdef CONFIG_KVM_GUEST_MEMFD +static inline bool kvm_arch_gmem_supports_no_direct_map(void) +{ + return can_set_direct_map(); +} +#define kvm_arch_gmem_supports_no_direct_map kvm_arch_gmem_supports_no_dir= ect_map +#endif /* CONFIG_KVM_GUEST_MEMFD */ + #endif /* _ASM_X86_KVM_HOST_H */ --=20 2.50.1 From nobody Thu Dec 18 02:07:47 2025 Received: from fra-out-013.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-013.esa.eu-central-1.outbound.mail-perimeter.amazon.com [63.178.132.221]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 13E143358A7; Fri, 5 Dec 2025 16:59:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=63.178.132.221 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764953973; cv=none; b=q1XZZALvmtGGABlDZabZYOd+tP84MyD9r6oLUw5gLx4Ux7Sb68JMYQd0Ae7EpyRt188ACv84z5/33Qimf2ccyilqetE2HWjrpye9FMVT4h3k/KN1S7nkdFkFsI+iWiIYM8TrneRR/ztFquW+ZonoaCHDlfa5bSTlZ6DI1lAtmwA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764953973; c=relaxed/simple; bh=Ll4+gFY6xgocJ4sAVlTLpUB/YiZ64ywS7lKYvFBu4Ts=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: Content-Type:MIME-Version; b=jSSw4W9prjlLqViQj2TCPuMo11Kgtp+y5UJoU4SANJr7Q8F0njv/a/qpX4dz5kB8gCpvhxToQYKD8lx1psWbYFmsYbw1nOkVMKE/Jh/zdtyuGoEjZ4eKaJh6NkSBWsC/84AH2K5qo5kV7Ng4EnvbdIhIua6y1ro3JCEC3cFD7uE= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.co.uk; spf=pass smtp.mailfrom=amazon.co.uk; dkim=pass (2048-bit key) header.d=amazon.co.uk header.i=@amazon.co.uk header.b=KLFJbW22; arc=none smtp.client-ip=63.178.132.221 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.co.uk header.i=@amazon.co.uk header.b="KLFJbW22" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazoncorp2; t=1764953958; x=1796489958; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=W7GJfZU7LO05jHXKZKMobVu/d9jql0a0jk/2tfUdHpg=; b=KLFJbW22vyfwNVbyIQN4+0IxkgGvNkOaEFh20/znrD3A4aAqfRYAqTMs AqmNsvaT7NA1LN9+ufJNdBPyDcF3brZQB22teOlveVtA7nh8TcwMqlQAj eY/5nU19BJutLC97dr+6UQjiGv3VaFW6pBikaKESgpVlC2rgcIBzxP+3x b7VZ/AbyAip6VKNy6gnMF7oodo6iKqR4aQyUVMBDaBfZOHpfjkGCOV7jW 47bkh3xczOAepRg4O0SuY5PCGTxbBswdg5eZ2zzDC2dU23IdXd4P7kH34 VqLALsBm4oxdF9ZuIgMg4aHwCUx3xYYKx8ORkeL0N2MEF1jH9QHasEoNN A==; X-CSE-ConnectionGUID: UrLe81L+QuOcbGqQwPNB0A== X-CSE-MsgGUID: 9doc33xfT2uUuc+3NSn9iA== X-IronPort-AV: E=Sophos;i="6.20,252,1758585600"; d="scan'208";a="6196628" Received: from ip-10-6-11-83.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.11.83]) by internal-fra-out-013.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Dec 2025 16:59:07 +0000 Received: from EX19MTAEUB001.ant.amazon.com [54.240.197.234:25054] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.45.40:2525] with esmtp (Farcaster) id 207ea9c1-766b-47ab-853b-ca8cb9de4fa5; Fri, 5 Dec 2025 16:59:07 +0000 (UTC) X-Farcaster-Flow-ID: 207ea9c1-766b-47ab-853b-ca8cb9de4fa5 Received: from EX19D005EUB002.ant.amazon.com (10.252.51.103) by EX19MTAEUB001.ant.amazon.com (10.252.51.28) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.29; Fri, 5 Dec 2025 16:59:07 +0000 Received: from EX19D005EUB003.ant.amazon.com (10.252.51.31) by EX19D005EUB002.ant.amazon.com (10.252.51.103) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.29; Fri, 5 Dec 2025 16:59:06 +0000 Received: from EX19D005EUB003.ant.amazon.com ([fe80::b825:becb:4b38:da0c]) by EX19D005EUB003.ant.amazon.com ([fe80::b825:becb:4b38:da0c%3]) with mapi id 15.02.2562.029; Fri, 5 Dec 2025 16:59:06 +0000 From: "Kalyazin, Nikita" To: "kvm@vger.kernel.org" , "linux-doc@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "kvmarm@lists.linux.dev" , "linux-fsdevel@vger.kernel.org" , "linux-mm@kvack.org" , "bpf@vger.kernel.org" , "linux-kselftest@vger.kernel.org" CC: "pbonzini@redhat.com" , "corbet@lwn.net" , "maz@kernel.org" , "oupton@kernel.org" , "joey.gouly@arm.com" , "suzuki.poulose@arm.com" , "yuzenghui@huawei.com" , "catalin.marinas@arm.com" , "will@kernel.org" , "seanjc@google.com" , "tglx@linutronix.de" , "mingo@redhat.com" , "bp@alien8.de" , "dave.hansen@linux.intel.com" , "x86@kernel.org" , "hpa@zytor.com" , "luto@kernel.org" , "peterz@infradead.org" , "willy@infradead.org" , "akpm@linux-foundation.org" , "david@kernel.org" , "lorenzo.stoakes@oracle.com" , "Liam.Howlett@oracle.com" , "vbabka@suse.cz" , "rppt@kernel.org" , "surenb@google.com" , "mhocko@suse.com" , "ast@kernel.org" , "daniel@iogearbox.net" , "andrii@kernel.org" , "martin.lau@linux.dev" , "eddyz87@gmail.com" , "song@kernel.org" , "yonghong.song@linux.dev" , "john.fastabend@gmail.com" , "kpsingh@kernel.org" , "sdf@fomichev.me" , "haoluo@google.com" , "jolsa@kernel.org" , "jgg@ziepe.ca" , "jhubbard@nvidia.com" , "peterx@redhat.com" , "jannh@google.com" , "pfalcato@suse.de" , "shuah@kernel.org" , "riel@surriel.com" , "baohua@kernel.org" , "ryan.roberts@arm.com" , "jgross@suse.com" , "yu-cheng.yu@intel.com" , "kas@kernel.org" , "coxu@redhat.com" , "kevin.brodsky@arm.com" , "ackerleytng@google.com" , "maobibo@loongson.cn" , "prsampat@amd.com" , "mlevitsk@redhat.com" , "isaku.yamahata@intel.com" , "jmattson@google.com" , "jthoughton@google.com" , "linux-arm-kernel@lists.infradead.org" , "vannapurve@google.com" , "jackmanb@google.com" , "aneesh.kumar@kernel.org" , "patrick.roy@linux.dev" , "Thomson, Jack" , "Itazuri, Takahiro" , "Manwaring, Derek" , "Cali, Marco" , "Kalyazin, Nikita" Subject: [PATCH v8 07/13] KVM: arm64: define kvm_arch_gmem_supports_no_direct_map() Thread-Topic: [PATCH v8 07/13] KVM: arm64: define kvm_arch_gmem_supports_no_direct_map() Thread-Index: AQHcZgh3WLdsPS6cE06bWor8xc18DA== Date: Fri, 5 Dec 2025 16:59:06 +0000 Message-ID: <20251205165743.9341-8-kalyazin@amazon.com> References: <20251205165743.9341-1-kalyazin@amazon.com> In-Reply-To: <20251205165743.9341-1-kalyazin@amazon.com> Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: Content-Transfer-Encoding: quoted-printable Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Patrick Roy Support for GUEST_MEMFD_FLAG_NO_DIRECT_MAP on arm64 depends on 1) direct map manipulations at 4k granularity being possible, and 2) FEAT_S2FWB. 1) is met whenever the direct map is set up at 4k granularity (e.g. not with huge/gigantic pages) at boottime, as due to ARM's break-before-make semantics, breaking huge mappings into 4k mappings in the direct map is not possible (BBM would require temporary invalidation of the entire huge mapping, even if only a 4k subrange should be zapped, which will probably crash the kernel). However, current defconfigs select for example CONFIG_RO_DATA_FULL_DEFAULT_ENABLED, which forces a 4k direct map. 2) is required to allow KVM to elide cache coherency operations when installing stage 2 page tables, which require the direct map to be entry for the newly mapped memory to be present (which it will not be, as guest_memfd would have removed direct map entries in kvm_gmem_get_pfn()). Cc: Will Deacon Signed-off-by: Patrick Roy Signed-off-by: Nikita Kalyazin --- arch/arm64/include/asm/kvm_host.h | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/arch/arm64/include/asm/kvm_host.h b/arch/arm64/include/asm/kvm= _host.h index ac7f970c7883..d431ca7d4fc9 100644 --- a/arch/arm64/include/asm/kvm_host.h +++ b/arch/arm64/include/asm/kvm_host.h @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -1654,5 +1655,17 @@ static __always_inline enum fgt_group_id __fgt_reg_t= o_group_id(enum vcpu_sysreg \ p; \ }) +#ifdef CONFIG_KVM_GUEST_MEMFD +static inline bool kvm_arch_gmem_supports_no_direct_map(void) +{ + /* + * Without FWB, direct map access is needed in kvm_pgtable_stage2_map(), + * as it calls dcache_clean_inval_poc(). + */ + return can_set_direct_map() && cpus_have_final_cap(ARM64_HAS_STAGE2_FWB); +} +#define kvm_arch_gmem_supports_no_direct_map kvm_arch_gmem_supports_no_dir= ect_map +#endif /* CONFIG_KVM_GUEST_MEMFD */ + =20 #endif /* __ARM64_KVM_HOST_H__ */ --=20 2.50.1 From nobody Thu Dec 18 02:07:47 2025 Received: from fra-out-011.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-011.esa.eu-central-1.outbound.mail-perimeter.amazon.com [52.28.197.132]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EEF1E2DC763; Fri, 5 Dec 2025 16:59:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=52.28.197.132 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764953974; cv=none; b=CcZO0Dv1gu2g9jpWmGGn7zF/rq+4zQRl7Zom4znHBQe8HsFHh3IlzPFszNwhkouyY1G/WOCwPl6+5MA5oxkZ6MNzkIFL1kQswr/slvrG0so6JqhawfMCSdEzwljZOqsscwRfdnVOzFZwFintCo272Dp6LoFKfVUY0SxNb39j110= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764953974; c=relaxed/simple; bh=l3J7MMjkFcUdCnCJi5gFhSLq2InQjltnE6+7oF8VtNM=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: Content-Type:MIME-Version; b=tpEDDPbzNefzBoWMLLoiVfFuHAgmVc5oKm8SRH22oAySKoil7RHe4dTkcF0G0UN9EpyWymCrKVa2oPickofee8NiV3dHD00BrbF4SW30Fdgj2dhzCFWG+sYVwUC9ji3U8wtAkUwHn61D3RQ4VgZv6sBX8itzDRdmESLzPIdRC+I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.co.uk; spf=pass smtp.mailfrom=amazon.co.uk; dkim=pass (2048-bit key) header.d=amazon.co.uk header.i=@amazon.co.uk header.b=bshxLSCM; arc=none smtp.client-ip=52.28.197.132 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.co.uk header.i=@amazon.co.uk header.b="bshxLSCM" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazoncorp2; t=1764953966; x=1796489966; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=CWGOx1LJU1Lp2rVsMU/Gh8xhbQtWU2hA9WJ9kcqa2lY=; b=bshxLSCMSny3rnT7HvmgFHC58ip8vx+IFOgv/U21xjHy7yGkeeUUDDVS foyRqMnXDKmRijL1M5LS3EgGoaWMo7DfZP+vVzGnuUxltLyddKDo6dbIU BO7cq3Zq/Z23qNa7ZMdh7Bei/ZFujgWhdIV8gP/+g05HqVMA9bhcZ4itC NmS/P5XDMVb5LyUbQ8t4ud1Fh+ybzrABHa7au6xIpCQ83QsMUsw8IhHRJ LKoVz9QGCtI3b2E5mdeuMcD/aRQZ4Lufmr9U8nXs0U6VtmDPjOYpJTvf2 aRJIQg2lwaMNxsTcGFvNBO/5ClmHdP3fLgNgEfwQzIsZFE2YFKRDYUD4T Q==; X-CSE-ConnectionGUID: CA8/vzSqQKyCPtR3yMet3A== X-CSE-MsgGUID: 8BJPWskwRySewdUd9tj6Rw== X-IronPort-AV: E=Sophos;i="6.20,252,1758585600"; d="scan'208";a="6191229" Received: from ip-10-6-3-216.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.3.216]) by internal-fra-out-011.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Dec 2025 16:59:19 +0000 Received: from EX19MTAEUB001.ant.amazon.com [54.240.197.226:10319] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.37.108:2525] with esmtp (Farcaster) id 90fbd84d-f4a1-4839-8024-b62656e1a5de; Fri, 5 Dec 2025 16:59:18 +0000 (UTC) X-Farcaster-Flow-ID: 90fbd84d-f4a1-4839-8024-b62656e1a5de Received: from EX19D005EUB004.ant.amazon.com (10.252.51.126) by EX19MTAEUB001.ant.amazon.com (10.252.51.26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.29; Fri, 5 Dec 2025 16:59:18 +0000 Received: from EX19D005EUB003.ant.amazon.com (10.252.51.31) by EX19D005EUB004.ant.amazon.com (10.252.51.126) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.29; Fri, 5 Dec 2025 16:59:17 +0000 Received: from EX19D005EUB003.ant.amazon.com ([fe80::b825:becb:4b38:da0c]) by EX19D005EUB003.ant.amazon.com ([fe80::b825:becb:4b38:da0c%3]) with mapi id 15.02.2562.029; Fri, 5 Dec 2025 16:59:17 +0000 From: "Kalyazin, Nikita" To: "kvm@vger.kernel.org" , "linux-doc@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "kvmarm@lists.linux.dev" , "linux-fsdevel@vger.kernel.org" , "linux-mm@kvack.org" , "bpf@vger.kernel.org" , "linux-kselftest@vger.kernel.org" CC: "pbonzini@redhat.com" , "corbet@lwn.net" , "maz@kernel.org" , "oupton@kernel.org" , "joey.gouly@arm.com" , "suzuki.poulose@arm.com" , "yuzenghui@huawei.com" , "catalin.marinas@arm.com" , "will@kernel.org" , "seanjc@google.com" , "tglx@linutronix.de" , "mingo@redhat.com" , "bp@alien8.de" , "dave.hansen@linux.intel.com" , "x86@kernel.org" , "hpa@zytor.com" , "luto@kernel.org" , "peterz@infradead.org" , "willy@infradead.org" , "akpm@linux-foundation.org" , "david@kernel.org" , "lorenzo.stoakes@oracle.com" , "Liam.Howlett@oracle.com" , "vbabka@suse.cz" , "rppt@kernel.org" , "surenb@google.com" , "mhocko@suse.com" , "ast@kernel.org" , "daniel@iogearbox.net" , "andrii@kernel.org" , "martin.lau@linux.dev" , "eddyz87@gmail.com" , "song@kernel.org" , "yonghong.song@linux.dev" , "john.fastabend@gmail.com" , "kpsingh@kernel.org" , "sdf@fomichev.me" , "haoluo@google.com" , "jolsa@kernel.org" , "jgg@ziepe.ca" , "jhubbard@nvidia.com" , "peterx@redhat.com" , "jannh@google.com" , "pfalcato@suse.de" , "shuah@kernel.org" , "riel@surriel.com" , "baohua@kernel.org" , "ryan.roberts@arm.com" , "jgross@suse.com" , "yu-cheng.yu@intel.com" , "kas@kernel.org" , "coxu@redhat.com" , "kevin.brodsky@arm.com" , "ackerleytng@google.com" , "maobibo@loongson.cn" , "prsampat@amd.com" , "mlevitsk@redhat.com" , "isaku.yamahata@intel.com" , "jmattson@google.com" , "jthoughton@google.com" , "linux-arm-kernel@lists.infradead.org" , "vannapurve@google.com" , "jackmanb@google.com" , "aneesh.kumar@kernel.org" , "patrick.roy@linux.dev" , "Thomson, Jack" , "Itazuri, Takahiro" , "Manwaring, Derek" , "Cali, Marco" , "Kalyazin, Nikita" Subject: [PATCH v8 08/13] KVM: selftests: load elf via bounce buffer Thread-Topic: [PATCH v8 08/13] KVM: selftests: load elf via bounce buffer Thread-Index: AQHcZgh+ROJLYEtTXUy85DKC2famHw== Date: Fri, 5 Dec 2025 16:59:17 +0000 Message-ID: <20251205165743.9341-9-kalyazin@amazon.com> References: <20251205165743.9341-1-kalyazin@amazon.com> In-Reply-To: <20251205165743.9341-1-kalyazin@amazon.com> Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: Content-Transfer-Encoding: quoted-printable Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Patrick Roy If guest memory is backed using a VMA that does not allow GUP (e.g. a userspace mapping of guest_memfd when the fd was allocated using GUEST_MEMFD_FLAG_NO_DIRECT_MAP), then directly loading the test ELF binary into it via read(2) potentially does not work. To nevertheless support loading binaries in this cases, do the read(2) syscall using a bounce buffer, and then memcpy from the bounce buffer into guest memory. Signed-off-by: Patrick Roy Signed-off-by: Nikita Kalyazin --- .../testing/selftests/kvm/include/test_util.h | 1 + tools/testing/selftests/kvm/lib/elf.c | 8 +++---- tools/testing/selftests/kvm/lib/io.c | 23 +++++++++++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testin= g/selftests/kvm/include/test_util.h index b4872ba8ed12..8140e59b59e5 100644 --- a/tools/testing/selftests/kvm/include/test_util.h +++ b/tools/testing/selftests/kvm/include/test_util.h @@ -48,6 +48,7 @@ do { \ =20 ssize_t test_write(int fd, const void *buf, size_t count); ssize_t test_read(int fd, void *buf, size_t count); +ssize_t test_read_bounce(int fd, void *buf, size_t count); int test_seq_read(const char *path, char **bufp, size_t *sizep); =20 void __printf(5, 6) test_assert(bool exp, const char *exp_str, diff --git a/tools/testing/selftests/kvm/lib/elf.c b/tools/testing/selftest= s/kvm/lib/elf.c index f34d926d9735..e829fbe0a11e 100644 --- a/tools/testing/selftests/kvm/lib/elf.c +++ b/tools/testing/selftests/kvm/lib/elf.c @@ -31,7 +31,7 @@ static void elfhdr_get(const char *filename, Elf64_Ehdr *= hdrp) * the real size of the ELF header. */ unsigned char ident[EI_NIDENT]; - test_read(fd, ident, sizeof(ident)); + test_read_bounce(fd, ident, sizeof(ident)); TEST_ASSERT((ident[EI_MAG0] =3D=3D ELFMAG0) && (ident[EI_MAG1] =3D=3D ELF= MAG1) && (ident[EI_MAG2] =3D=3D ELFMAG2) && (ident[EI_MAG3] =3D=3D ELFMAG3), "ELF MAGIC Mismatch,\n" @@ -79,7 +79,7 @@ static void elfhdr_get(const char *filename, Elf64_Ehdr *= hdrp) offset_rv =3D lseek(fd, 0, SEEK_SET); TEST_ASSERT(offset_rv =3D=3D 0, "Seek to ELF header failed,\n" " rv: %zi expected: %i", offset_rv, 0); - test_read(fd, hdrp, sizeof(*hdrp)); + test_read_bounce(fd, hdrp, sizeof(*hdrp)); TEST_ASSERT(hdrp->e_phentsize =3D=3D sizeof(Elf64_Phdr), "Unexpected physical header size,\n" " hdrp->e_phentsize: %x\n" @@ -146,7 +146,7 @@ void kvm_vm_elf_load(struct kvm_vm *vm, const char *fil= ename) =20 /* Read in the program header. */ Elf64_Phdr phdr; - test_read(fd, &phdr, sizeof(phdr)); + test_read_bounce(fd, &phdr, sizeof(phdr)); =20 /* Skip if this header doesn't describe a loadable segment. */ if (phdr.p_type !=3D PT_LOAD) @@ -187,7 +187,7 @@ void kvm_vm_elf_load(struct kvm_vm *vm, const char *fil= ename) " expected: 0x%jx", n1, errno, (intmax_t) offset_rv, (intmax_t) phdr.p_offset); - test_read(fd, addr_gva2hva(vm, phdr.p_vaddr), + test_read_bounce(fd, addr_gva2hva(vm, phdr.p_vaddr), phdr.p_filesz); } } diff --git a/tools/testing/selftests/kvm/lib/io.c b/tools/testing/selftests= /kvm/lib/io.c index fedb2a741f0b..74419becc8bc 100644 --- a/tools/testing/selftests/kvm/lib/io.c +++ b/tools/testing/selftests/kvm/lib/io.c @@ -155,3 +155,26 @@ ssize_t test_read(int fd, void *buf, size_t count) =20 return num_read; } + +/* Test read via intermediary buffer + * + * Same as test_read, except read(2)s happen into a bounce buffer that is = memcpy'd + * to buf. For use with buffers that cannot be GUP'd (e.g. guest_memfd VMA= s if + * guest_memfd was created with GUEST_MEMFD_FLAG_NO_DIRECT_MAP). + */ +ssize_t test_read_bounce(int fd, void *buf, size_t count) +{ + void *bounce_buffer; + ssize_t num_read; + + TEST_ASSERT(count >=3D 0, "Unexpected count, count: %li", count); + + bounce_buffer =3D malloc(count); + TEST_ASSERT(bounce_buffer !=3D NULL, "Failed to allocate bounce buffer"); + + num_read =3D test_read(fd, bounce_buffer, count); + memcpy(buf, bounce_buffer, num_read); + free(bounce_buffer); + + return num_read; +} --=20 2.50.1 From nobody Thu Dec 18 02:07:47 2025 Received: from fra-out-009.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-009.esa.eu-central-1.outbound.mail-perimeter.amazon.com [3.64.237.68]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 21CA0288B1; Fri, 5 Dec 2025 16:59:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=3.64.237.68 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764954002; cv=none; b=eOdIMsAqZSl9MXjjUJYJkB6ThhMIxbG0fMxd+rgEcbUC4fheZty44X0t3VIu1WmHGsOYx4d6pWPBYawaxU+d46IUvgyDt7i2nezHvunby/eoT8VcHmcnBl2Q4yUPJaHkZs8+tw5wVuUyZnSt68fd+d1DA3bh4bYyuH3EZ07espQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764954002; c=relaxed/simple; bh=AABMWarGiUEk3N84paWR25V2S5vEufTAnkCw+SKGO2k=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: Content-Type:MIME-Version; b=mwPedl1lFRYoYuEPxp4UIcwcCvUKG7RxL12ccx4OkO9NU9n1Aqq0lk9wJbO6qwiTtnkGxojc+O4oUE0qVapY7t2y5g+jFP00FTZuJ0lt7KV6IX8hrEHi9CSGnmrf+jp2eyxzwH6OIofDvuoNqmt0Oq0AP2XJQ/h6DvtmuO1RN94= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.co.uk; spf=pass smtp.mailfrom=amazon.co.uk; dkim=pass (2048-bit key) header.d=amazon.co.uk header.i=@amazon.co.uk header.b=lks2CoKv; arc=none smtp.client-ip=3.64.237.68 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.co.uk header.i=@amazon.co.uk header.b="lks2CoKv" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazoncorp2; t=1764953998; x=1796489998; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=d0fkQSK/G4NqBdB6Hn9NRubMn6LJOB2ByURYL1BmaOQ=; b=lks2CoKvdEihMBwIFvXdMogzHFMIOvMV0MbCJdXzyC3lYdInI1iJjn7l S4RY4yNhGRprGF/jcX984io6nVooBr+Q4EIz65R1xMtcGCOez41GwA7PT CvsEEskT1kVfkKBIYDn8Umb9fuaXRBVXbW/FJ6tntC7QXgBCjN7nMddzf UNZ7GfPHW24cdIxttFRorWaedO1mTimG86JhKz5Chwzi6vVqSl0jbZRt5 IwAAGhSvStGH8/Zss9De0Yfe6k64tZIpVEMZsjlbeHXjPexqmseb5+6KK 9Myj8vzYm4L1W4v6AeCPba/vDlsn6v9zojEhpkq2AcrV/sUHYrkrCgEZ2 A==; X-CSE-ConnectionGUID: bnmufHvTQDaW1bi4gnrtaw== X-CSE-MsgGUID: txDCSJQgRROMr7WjGghLjw== X-IronPort-AV: E=Sophos;i="6.20,252,1758585600"; d="scan'208";a="6206122" Received: from ip-10-6-11-83.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.11.83]) by internal-fra-out-009.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Dec 2025 16:59:31 +0000 Received: from EX19MTAEUB001.ant.amazon.com [54.240.197.234:4306] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.34.107:2525] with esmtp (Farcaster) id 1dd59fbf-337b-4e4d-998c-8c9374aef030; Fri, 5 Dec 2025 16:59:31 +0000 (UTC) X-Farcaster-Flow-ID: 1dd59fbf-337b-4e4d-998c-8c9374aef030 Received: from EX19D005EUB001.ant.amazon.com (10.252.51.12) by EX19MTAEUB001.ant.amazon.com (10.252.51.26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.29; Fri, 5 Dec 2025 16:59:29 +0000 Received: from EX19D005EUB003.ant.amazon.com (10.252.51.31) by EX19D005EUB001.ant.amazon.com (10.252.51.12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.29; Fri, 5 Dec 2025 16:59:29 +0000 Received: from EX19D005EUB003.ant.amazon.com ([fe80::b825:becb:4b38:da0c]) by EX19D005EUB003.ant.amazon.com ([fe80::b825:becb:4b38:da0c%3]) with mapi id 15.02.2562.029; Fri, 5 Dec 2025 16:59:28 +0000 From: "Kalyazin, Nikita" To: "kvm@vger.kernel.org" , "linux-doc@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "kvmarm@lists.linux.dev" , "linux-fsdevel@vger.kernel.org" , "linux-mm@kvack.org" , "bpf@vger.kernel.org" , "linux-kselftest@vger.kernel.org" CC: "pbonzini@redhat.com" , "corbet@lwn.net" , "maz@kernel.org" , "oupton@kernel.org" , "joey.gouly@arm.com" , "suzuki.poulose@arm.com" , "yuzenghui@huawei.com" , "catalin.marinas@arm.com" , "will@kernel.org" , "seanjc@google.com" , "tglx@linutronix.de" , "mingo@redhat.com" , "bp@alien8.de" , "dave.hansen@linux.intel.com" , "x86@kernel.org" , "hpa@zytor.com" , "luto@kernel.org" , "peterz@infradead.org" , "willy@infradead.org" , "akpm@linux-foundation.org" , "david@kernel.org" , "lorenzo.stoakes@oracle.com" , "Liam.Howlett@oracle.com" , "vbabka@suse.cz" , "rppt@kernel.org" , "surenb@google.com" , "mhocko@suse.com" , "ast@kernel.org" , "daniel@iogearbox.net" , "andrii@kernel.org" , "martin.lau@linux.dev" , "eddyz87@gmail.com" , "song@kernel.org" , "yonghong.song@linux.dev" , "john.fastabend@gmail.com" , "kpsingh@kernel.org" , "sdf@fomichev.me" , "haoluo@google.com" , "jolsa@kernel.org" , "jgg@ziepe.ca" , "jhubbard@nvidia.com" , "peterx@redhat.com" , "jannh@google.com" , "pfalcato@suse.de" , "shuah@kernel.org" , "riel@surriel.com" , "baohua@kernel.org" , "ryan.roberts@arm.com" , "jgross@suse.com" , "yu-cheng.yu@intel.com" , "kas@kernel.org" , "coxu@redhat.com" , "kevin.brodsky@arm.com" , "ackerleytng@google.com" , "maobibo@loongson.cn" , "prsampat@amd.com" , "mlevitsk@redhat.com" , "isaku.yamahata@intel.com" , "jmattson@google.com" , "jthoughton@google.com" , "linux-arm-kernel@lists.infradead.org" , "vannapurve@google.com" , "jackmanb@google.com" , "aneesh.kumar@kernel.org" , "patrick.roy@linux.dev" , "Thomson, Jack" , "Itazuri, Takahiro" , "Manwaring, Derek" , "Cali, Marco" , "Kalyazin, Nikita" Subject: [PATCH v8 09/13] KVM: selftests: set KVM_MEM_GUEST_MEMFD in vm_mem_add() if guest_memfd != -1 Thread-Topic: [PATCH v8 09/13] KVM: selftests: set KVM_MEM_GUEST_MEMFD in vm_mem_add() if guest_memfd != -1 Thread-Index: AQHcZgiEOs+x69XXckO+A8F3rNWd2g== Date: Fri, 5 Dec 2025 16:59:28 +0000 Message-ID: <20251205165743.9341-10-kalyazin@amazon.com> References: <20251205165743.9341-1-kalyazin@amazon.com> In-Reply-To: <20251205165743.9341-1-kalyazin@amazon.com> Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: Content-Transfer-Encoding: quoted-printable Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Patrick Roy Have vm_mem_add() always set KVM_MEM_GUEST_MEMFD in the memslot flags if a guest_memfd is passed in as an argument. This eliminates the possibility where a guest_memfd instance is passed to vm_mem_add(), but it ends up being ignored because the flags argument does not specify KVM_MEM_GUEST_MEMFD at the same time. This makes it easy to support more scenarios in which no vm_mem_add() is not passed a guest_memfd instance, but is expected to allocate one. Currently, this only happens if guest_memfd =3D=3D -1 but flags & KVM_MEM_GUEST_MEMFD !=3D 0, but later vm_mem_add() will gain support for loading the test code itself into guest_memfd (via GUEST_MEMFD_FLAG_MMAP) if requested via a special vm_mem_backing_src_type, at which point having to make sure the src_type and flags are in-sync becomes cumbersome. Signed-off-by: Patrick Roy Signed-off-by: Nikita Kalyazin --- tools/testing/selftests/kvm/lib/kvm_util.c | 24 +++++++++++++--------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/sel= ftests/kvm/lib/kvm_util.c index 8279b6ced8d2..56ddbca91850 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -1057,21 +1057,25 @@ void vm_mem_add(struct kvm_vm *vm, enum vm_mem_back= ing_src_type src_type, =20 region->backing_src_type =3D src_type; =20 - if (flags & KVM_MEM_GUEST_MEMFD) { - if (guest_memfd < 0) { + if (guest_memfd < 0) { + if (flags & KVM_MEM_GUEST_MEMFD) { uint32_t guest_memfd_flags =3D 0; TEST_ASSERT(!guest_memfd_offset, "Offset must be zero when creating new guest_memfd"); guest_memfd =3D vm_create_guest_memfd(vm, mem_size, guest_memfd_flags); - } else { - /* - * Install a unique fd for each memslot so that the fd - * can be closed when the region is deleted without - * needing to track if the fd is owned by the framework - * or by the caller. - */ - guest_memfd =3D kvm_dup(guest_memfd); } + } else { + /* + * Install a unique fd for each memslot so that the fd + * can be closed when the region is deleted without + * needing to track if the fd is owned by the framework + * or by the caller. + */ + guest_memfd =3D kvm_dup(guest_memfd); + } + + if (guest_memfd > 0) { + flags |=3D KVM_MEM_GUEST_MEMFD; =20 region->region.guest_memfd =3D guest_memfd; region->region.guest_memfd_offset =3D guest_memfd_offset; --=20 2.50.1 From nobody Thu Dec 18 02:07:47 2025 Received: from fra-out-009.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-009.esa.eu-central-1.outbound.mail-perimeter.amazon.com [3.64.237.68]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B398C348886; Fri, 5 Dec 2025 16:59:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=3.64.237.68 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764954003; cv=none; b=r5tz+Y4EUZaE7K4py5QlG6VDP40Uzgo3QF7vX6Lrk2xe6xrvlk469xGrbfYYcSqoGZIhmsLHBtBf6Fok64rNpOTKGG+fRZLfhjY9h7meEHuaycsKaVOXvdRJplm8NIa6vjufDEvetHlMaNPyel15b2reZaVcCkKb9EZ54gaB40s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764954003; c=relaxed/simple; bh=52mHrkP05gYzT147dW4uInww81IpjV4wn8Xsu+UrgMw=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: Content-Type:MIME-Version; b=pK0yIPl0n6vBXYJtMpminM20X9fvcD9efoA54t+0K0uiitcFV4ka8IWit0YMv0fki7mE/CGHTBFaZ3AdFb5bzw4bmfD5gdLA+V5pEXGsPt/jaZOrytx7TPEgLoq371vJXr4I3c5VVWKrL1kxfLEvtC8vyPRqkrCtWDShQ2z3zD0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.co.uk; spf=pass smtp.mailfrom=amazon.co.uk; dkim=pass (2048-bit key) header.d=amazon.co.uk header.i=@amazon.co.uk header.b=LkpHFV9v; arc=none smtp.client-ip=3.64.237.68 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.co.uk header.i=@amazon.co.uk header.b="LkpHFV9v" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazoncorp2; t=1764953998; x=1796489998; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=OprfmizXxq8RRTSlSSc6Z/sKanOls1Ur59Qhbz1MdVc=; b=LkpHFV9v/Yt04CZIHYyAIQPsIUeVyM2p/yJAaaKoQ/VUpgLi7e+xK5zO s4DScDKAOMTwVTja5uvmXfdDkI1YH63ghvmQSedRJA/Xg4s94Wp72R7vv UyT8D+MEXMZQXizShO9yQ/ZYEYuvEqmkh1nDOFK3tiBmpPv0Sm3ZFJ/4V vTKl1zp+NLFpziLiBlLg26DAP84y08sfixeFHRR5tjr5kjCB45kwC0X5S iC4XIJwFkBPMHCMd67Xk8clE7GAI0MYBywXQgN198P/mKEc4/WOpFQf0y eBt6dq9kHq5m7WcUdpQRL1uI/O/7dSRqfXnfJ9ohdWW5PQDNTIgEZUNGE A==; X-CSE-ConnectionGUID: wdWaJSd5SF6NQona6voUBg== X-CSE-MsgGUID: mLPb0a/XSKCaS9kn1GTYnw== X-IronPort-AV: E=Sophos;i="6.20,252,1758585600"; d="scan'208";a="6206137" Received: from ip-10-6-3-216.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.3.216]) by internal-fra-out-009.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Dec 2025 16:59:49 +0000 Received: from EX19MTAEUB002.ant.amazon.com [54.240.197.232:11504] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.24.148:2525] with esmtp (Farcaster) id 34fe6e69-dd5b-4480-b95e-ddc8757a2feb; Fri, 5 Dec 2025 16:59:49 +0000 (UTC) X-Farcaster-Flow-ID: 34fe6e69-dd5b-4480-b95e-ddc8757a2feb Received: from EX19D005EUB002.ant.amazon.com (10.252.51.103) by EX19MTAEUB002.ant.amazon.com (10.252.51.79) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.29; Fri, 5 Dec 2025 16:59:41 +0000 Received: from EX19D005EUB003.ant.amazon.com (10.252.51.31) by EX19D005EUB002.ant.amazon.com (10.252.51.103) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.29; Fri, 5 Dec 2025 16:59:40 +0000 Received: from EX19D005EUB003.ant.amazon.com ([fe80::b825:becb:4b38:da0c]) by EX19D005EUB003.ant.amazon.com ([fe80::b825:becb:4b38:da0c%3]) with mapi id 15.02.2562.029; Fri, 5 Dec 2025 16:59:40 +0000 From: "Kalyazin, Nikita" To: "kvm@vger.kernel.org" , "linux-doc@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "kvmarm@lists.linux.dev" , "linux-fsdevel@vger.kernel.org" , "linux-mm@kvack.org" , "bpf@vger.kernel.org" , "linux-kselftest@vger.kernel.org" CC: "pbonzini@redhat.com" , "corbet@lwn.net" , "maz@kernel.org" , "oupton@kernel.org" , "joey.gouly@arm.com" , "suzuki.poulose@arm.com" , "yuzenghui@huawei.com" , "catalin.marinas@arm.com" , "will@kernel.org" , "seanjc@google.com" , "tglx@linutronix.de" , "mingo@redhat.com" , "bp@alien8.de" , "dave.hansen@linux.intel.com" , "x86@kernel.org" , "hpa@zytor.com" , "luto@kernel.org" , "peterz@infradead.org" , "willy@infradead.org" , "akpm@linux-foundation.org" , "david@kernel.org" , "lorenzo.stoakes@oracle.com" , "Liam.Howlett@oracle.com" , "vbabka@suse.cz" , "rppt@kernel.org" , "surenb@google.com" , "mhocko@suse.com" , "ast@kernel.org" , "daniel@iogearbox.net" , "andrii@kernel.org" , "martin.lau@linux.dev" , "eddyz87@gmail.com" , "song@kernel.org" , "yonghong.song@linux.dev" , "john.fastabend@gmail.com" , "kpsingh@kernel.org" , "sdf@fomichev.me" , "haoluo@google.com" , "jolsa@kernel.org" , "jgg@ziepe.ca" , "jhubbard@nvidia.com" , "peterx@redhat.com" , "jannh@google.com" , "pfalcato@suse.de" , "shuah@kernel.org" , "riel@surriel.com" , "baohua@kernel.org" , "ryan.roberts@arm.com" , "jgross@suse.com" , "yu-cheng.yu@intel.com" , "kas@kernel.org" , "coxu@redhat.com" , "kevin.brodsky@arm.com" , "ackerleytng@google.com" , "maobibo@loongson.cn" , "prsampat@amd.com" , "mlevitsk@redhat.com" , "isaku.yamahata@intel.com" , "jmattson@google.com" , "jthoughton@google.com" , "linux-arm-kernel@lists.infradead.org" , "vannapurve@google.com" , "jackmanb@google.com" , "aneesh.kumar@kernel.org" , "patrick.roy@linux.dev" , "Thomson, Jack" , "Itazuri, Takahiro" , "Manwaring, Derek" , "Cali, Marco" , "Kalyazin, Nikita" Subject: [PATCH v8 10/13] KVM: selftests: Add guest_memfd based vm_mem_backing_src_types Thread-Topic: [PATCH v8 10/13] KVM: selftests: Add guest_memfd based vm_mem_backing_src_types Thread-Index: AQHcZgiLyS0oCQO1nUODCs70+4I+GA== Date: Fri, 5 Dec 2025 16:59:40 +0000 Message-ID: <20251205165743.9341-11-kalyazin@amazon.com> References: <20251205165743.9341-1-kalyazin@amazon.com> In-Reply-To: <20251205165743.9341-1-kalyazin@amazon.com> Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: Content-Transfer-Encoding: quoted-printable Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Patrick Roy Allow selftests to configure their memslots such that userspace_addr is set to a MAP_SHARED mapping of the guest_memfd that's associated with the memslot. This setup is the configuration for non-CoCo VMs, where all guest memory is backed by a guest_memfd whose folios are all marked shared, but KVM is still able to access guest memory to provide functionality such as MMIO emulation on x86. Add backing types for normal guest_memfd, as well as direct map removed guest_memfd. Signed-off-by: Patrick Roy Signed-off-by: Nikita Kalyazin --- .../testing/selftests/kvm/include/kvm_util.h | 18 ++++++ .../testing/selftests/kvm/include/test_util.h | 7 +++ tools/testing/selftests/kvm/lib/kvm_util.c | 61 ++++++++++--------- tools/testing/selftests/kvm/lib/test_util.c | 8 +++ 4 files changed, 65 insertions(+), 29 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing= /selftests/kvm/include/kvm_util.h index 81f4355ff28a..6689b43810c1 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -641,6 +641,24 @@ static inline bool is_smt_on(void) =20 void vm_create_irqchip(struct kvm_vm *vm); =20 +static inline uint32_t backing_src_guest_memfd_flags(enum vm_mem_backing_s= rc_type t) +{ + uint32_t flags =3D 0; + + switch (t) { + case VM_MEM_SRC_GUEST_MEMFD_NO_DIRECT_MAP: + flags |=3D GUEST_MEMFD_FLAG_NO_DIRECT_MAP; + fallthrough; + case VM_MEM_SRC_GUEST_MEMFD: + flags |=3D GUEST_MEMFD_FLAG_MMAP | GUEST_MEMFD_FLAG_INIT_SHARED; + break; + default: + break; + } + + return flags; +} + static inline int __vm_create_guest_memfd(struct kvm_vm *vm, uint64_t size, uint64_t flags) { diff --git a/tools/testing/selftests/kvm/include/test_util.h b/tools/testin= g/selftests/kvm/include/test_util.h index 8140e59b59e5..ea6de20ce8ef 100644 --- a/tools/testing/selftests/kvm/include/test_util.h +++ b/tools/testing/selftests/kvm/include/test_util.h @@ -152,6 +152,8 @@ enum vm_mem_backing_src_type { VM_MEM_SRC_ANONYMOUS_HUGETLB_16GB, VM_MEM_SRC_SHMEM, VM_MEM_SRC_SHARED_HUGETLB, + VM_MEM_SRC_GUEST_MEMFD, + VM_MEM_SRC_GUEST_MEMFD_NO_DIRECT_MAP, NUM_SRC_TYPES, }; =20 @@ -184,6 +186,11 @@ static inline bool backing_src_is_shared(enum vm_mem_b= acking_src_type t) return vm_mem_backing_src_alias(t)->flag & MAP_SHARED; } =20 +static inline bool backing_src_is_guest_memfd(enum vm_mem_backing_src_type= t) +{ + return t =3D=3D VM_MEM_SRC_GUEST_MEMFD || t =3D=3D VM_MEM_SRC_GUEST_MEMFD= _NO_DIRECT_MAP; +} + static inline bool backing_src_can_be_huge(enum vm_mem_backing_src_type t) { return t !=3D VM_MEM_SRC_ANONYMOUS && t !=3D VM_MEM_SRC_SHMEM; diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/sel= ftests/kvm/lib/kvm_util.c index 56ddbca91850..28ee51253909 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -1013,6 +1013,33 @@ void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backi= ng_src_type src_type, alignment =3D 1; #endif =20 + if (guest_memfd < 0) { + if ((flags & KVM_MEM_GUEST_MEMFD) || backing_src_is_guest_memfd(src_type= )) { + uint32_t guest_memfd_flags =3D backing_src_guest_memfd_flags(src_type); + + TEST_ASSERT(!guest_memfd_offset, + "Offset must be zero when creating new guest_memfd"); + guest_memfd =3D vm_create_guest_memfd(vm, mem_size, guest_memfd_flags); + } + } else { + /* + * Install a unique fd for each memslot so that the fd + * can be closed when the region is deleted without + * needing to track if the fd is owned by the framework + * or by the caller. + */ + guest_memfd =3D kvm_dup(guest_memfd); + } + + if (guest_memfd > 0) { + flags |=3D KVM_MEM_GUEST_MEMFD; + + region->region.guest_memfd =3D guest_memfd; + region->region.guest_memfd_offset =3D guest_memfd_offset; + } else { + region->region.guest_memfd =3D -1; + } + /* * When using THP mmap is not guaranteed to returned a hugepage aligned * address so we have to pad the mmap. Padding is not needed for HugeTLB @@ -1028,10 +1055,13 @@ void vm_mem_add(struct kvm_vm *vm, enum vm_mem_back= ing_src_type src_type, if (alignment > 1) region->mmap_size +=3D alignment; =20 - region->fd =3D -1; - if (backing_src_is_shared(src_type)) + if (backing_src_is_guest_memfd(src_type)) + region->fd =3D guest_memfd; + else if (backing_src_is_shared(src_type)) region->fd =3D kvm_memfd_alloc(region->mmap_size, src_type =3D=3D VM_MEM_SRC_SHARED_HUGETLB); + else + region->fd =3D -1; =20 region->mmap_start =3D kvm_mmap(region->mmap_size, PROT_READ | PROT_WRITE, vm_mem_backing_src_alias(src_type)->flag, @@ -1056,33 +1086,6 @@ void vm_mem_add(struct kvm_vm *vm, enum vm_mem_backi= ng_src_type src_type, } =20 region->backing_src_type =3D src_type; - - if (guest_memfd < 0) { - if (flags & KVM_MEM_GUEST_MEMFD) { - uint32_t guest_memfd_flags =3D 0; - TEST_ASSERT(!guest_memfd_offset, - "Offset must be zero when creating new guest_memfd"); - guest_memfd =3D vm_create_guest_memfd(vm, mem_size, guest_memfd_flags); - } - } else { - /* - * Install a unique fd for each memslot so that the fd - * can be closed when the region is deleted without - * needing to track if the fd is owned by the framework - * or by the caller. - */ - guest_memfd =3D kvm_dup(guest_memfd); - } - - if (guest_memfd > 0) { - flags |=3D KVM_MEM_GUEST_MEMFD; - - region->region.guest_memfd =3D guest_memfd; - region->region.guest_memfd_offset =3D guest_memfd_offset; - } else { - region->region.guest_memfd =3D -1; - } - region->unused_phy_pages =3D sparsebit_alloc(); if (vm_arch_has_protected_memory(vm)) region->protected_phy_pages =3D sparsebit_alloc(); diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/se= lftests/kvm/lib/test_util.c index 8a1848586a85..ce9fe0271515 100644 --- a/tools/testing/selftests/kvm/lib/test_util.c +++ b/tools/testing/selftests/kvm/lib/test_util.c @@ -306,6 +306,14 @@ const struct vm_mem_backing_src_alias *vm_mem_backing_= src_alias(uint32_t i) */ .flag =3D MAP_SHARED, }, + [VM_MEM_SRC_GUEST_MEMFD] =3D { + .name =3D "guest_memfd", + .flag =3D MAP_SHARED, + }, + [VM_MEM_SRC_GUEST_MEMFD_NO_DIRECT_MAP] =3D { + .name =3D "guest_memfd_no_direct_map", + .flag =3D MAP_SHARED, + } }; _Static_assert(ARRAY_SIZE(aliases) =3D=3D NUM_SRC_TYPES, "Missing new backing src types?"); --=20 2.50.1 From nobody Thu Dec 18 02:07:47 2025 Received: from fra-out-008.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-008.esa.eu-central-1.outbound.mail-perimeter.amazon.com [35.158.23.94]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0BA1D330322; Fri, 5 Dec 2025 17:01:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=35.158.23.94 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764954094; cv=none; b=CDB6lr3L51zMgnPwy0pZChTb0WRAEpcf3Ay18UTwB1ofO9v3TeV3dr3658XxnF4+iYStk9Wu+QnBig7TgBO7vrg3TlEGUSfAdi9koXE/Nvbn36GlrXQ0GHi4PnN2Xraid0MyjAALmllvgJJQ5IFcAN99h0kQQsQL2eME5pwfSX0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764954094; c=relaxed/simple; bh=+XLKM1HMOFNNPu4cU3SY1UKMhfjGTgNxGUTHgOxbOaY=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: Content-Type:MIME-Version; b=pSU/MJVyX+xCao0t5gy3ssSuPU0GjZm5ar3Ot//O8421cqpFs5ugv29r2BVoE3HHNxWMf57ZNOmfUtyePOEYTK9vTpY878pb2ohHF8T5uhntc148AcCwMwIX9Nk9MfbDklQasZH6Npx0cwFcIcjV0y7/QrpC3LzFdJsmzj4AIlY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.co.uk; spf=pass smtp.mailfrom=amazon.co.uk; dkim=pass (2048-bit key) header.d=amazon.co.uk header.i=@amazon.co.uk header.b=Fy20tpch; arc=none smtp.client-ip=35.158.23.94 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.co.uk header.i=@amazon.co.uk header.b="Fy20tpch" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazoncorp2; t=1764954084; x=1796490084; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=j+Z5wrixfI4H8rTwsRxl2yKp4vaZueJxgsH7jlHoBL4=; b=Fy20tpchJXJ9KK2XB0MgvqiXjn4BBp+3PWGW9SXjIyNYu2pgwy5aTlo+ Mc+txq4b9SloR6IjHdsPRlG+IGpL138JW0eTzwEZSsySu5qurRoCrLjby mmgSfabNwMom53LeloEmmck/rhYbpOIMmZuHVz4mbhpX62nQG6krDub+N LK0cmkaZwdXxMK2f/Ry0XvqhljHGujpYPUe/jO/2Q3vncL6eymdJWOnzl 2JZkHsTMQ8vgdrKRpcQpUyuuP/edVl0LHfz1iDMIqHkcCQbzQ22BCVd+Y b8/k1dcfQyqv4qIos+ptksfNY/TiTlvw9rkWzCuG+CUEryZeFXIYYvXM7 w==; X-CSE-ConnectionGUID: MRvNL6aoRryvv4gsH+MURw== X-CSE-MsgGUID: jK4+obf/SNGZvM6PfooJcQ== X-IronPort-AV: E=Sophos;i="6.20,252,1758585600"; d="scan'208";a="6306432" Received: from ip-10-6-11-83.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.11.83]) by internal-fra-out-008.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Dec 2025 16:59:52 +0000 Received: from EX19MTAEUC002.ant.amazon.com [54.240.197.236:11955] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.26.175:2525] with esmtp (Farcaster) id ebbc9af5-ef2d-44ce-9711-a58d362cbdb5; Fri, 5 Dec 2025 16:59:52 +0000 (UTC) X-Farcaster-Flow-ID: ebbc9af5-ef2d-44ce-9711-a58d362cbdb5 Received: from EX19D005EUB004.ant.amazon.com (10.252.51.126) by EX19MTAEUC002.ant.amazon.com (10.252.51.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.29; Fri, 5 Dec 2025 16:59:52 +0000 Received: from EX19D005EUB003.ant.amazon.com (10.252.51.31) by EX19D005EUB004.ant.amazon.com (10.252.51.126) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.29; Fri, 5 Dec 2025 16:59:51 +0000 Received: from EX19D005EUB003.ant.amazon.com ([fe80::b825:becb:4b38:da0c]) by EX19D005EUB003.ant.amazon.com ([fe80::b825:becb:4b38:da0c%3]) with mapi id 15.02.2562.029; Fri, 5 Dec 2025 16:59:51 +0000 From: "Kalyazin, Nikita" To: "kvm@vger.kernel.org" , "linux-doc@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "kvmarm@lists.linux.dev" , "linux-fsdevel@vger.kernel.org" , "linux-mm@kvack.org" , "bpf@vger.kernel.org" , "linux-kselftest@vger.kernel.org" CC: "pbonzini@redhat.com" , "corbet@lwn.net" , "maz@kernel.org" , "oupton@kernel.org" , "joey.gouly@arm.com" , "suzuki.poulose@arm.com" , "yuzenghui@huawei.com" , "catalin.marinas@arm.com" , "will@kernel.org" , "seanjc@google.com" , "tglx@linutronix.de" , "mingo@redhat.com" , "bp@alien8.de" , "dave.hansen@linux.intel.com" , "x86@kernel.org" , "hpa@zytor.com" , "luto@kernel.org" , "peterz@infradead.org" , "willy@infradead.org" , "akpm@linux-foundation.org" , "david@kernel.org" , "lorenzo.stoakes@oracle.com" , "Liam.Howlett@oracle.com" , "vbabka@suse.cz" , "rppt@kernel.org" , "surenb@google.com" , "mhocko@suse.com" , "ast@kernel.org" , "daniel@iogearbox.net" , "andrii@kernel.org" , "martin.lau@linux.dev" , "eddyz87@gmail.com" , "song@kernel.org" , "yonghong.song@linux.dev" , "john.fastabend@gmail.com" , "kpsingh@kernel.org" , "sdf@fomichev.me" , "haoluo@google.com" , "jolsa@kernel.org" , "jgg@ziepe.ca" , "jhubbard@nvidia.com" , "peterx@redhat.com" , "jannh@google.com" , "pfalcato@suse.de" , "shuah@kernel.org" , "riel@surriel.com" , "baohua@kernel.org" , "ryan.roberts@arm.com" , "jgross@suse.com" , "yu-cheng.yu@intel.com" , "kas@kernel.org" , "coxu@redhat.com" , "kevin.brodsky@arm.com" , "ackerleytng@google.com" , "maobibo@loongson.cn" , "prsampat@amd.com" , "mlevitsk@redhat.com" , "isaku.yamahata@intel.com" , "jmattson@google.com" , "jthoughton@google.com" , "linux-arm-kernel@lists.infradead.org" , "vannapurve@google.com" , "jackmanb@google.com" , "aneesh.kumar@kernel.org" , "patrick.roy@linux.dev" , "Thomson, Jack" , "Itazuri, Takahiro" , "Manwaring, Derek" , "Cali, Marco" , "Kalyazin, Nikita" Subject: [PATCH v8 11/13] KVM: selftests: cover GUEST_MEMFD_FLAG_NO_DIRECT_MAP in existing selftests Thread-Topic: [PATCH v8 11/13] KVM: selftests: cover GUEST_MEMFD_FLAG_NO_DIRECT_MAP in existing selftests Thread-Index: AQHcZgiSLmjQ8ZJCx0KLKn2zyDItvg== Date: Fri, 5 Dec 2025 16:59:51 +0000 Message-ID: <20251205165743.9341-12-kalyazin@amazon.com> References: <20251205165743.9341-1-kalyazin@amazon.com> In-Reply-To: <20251205165743.9341-1-kalyazin@amazon.com> Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: Content-Transfer-Encoding: quoted-printable Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Patrick Roy Extend mem conversion selftests to cover the scenario that the guest can fault in and write gmem-backed guest memory even if its direct map removed. Also cover the new flag in guest_memfd_test.c tests. Signed-off-by: Patrick Roy Signed-off-by: Nikita Kalyazin --- tools/testing/selftests/kvm/guest_memfd_test.c | 17 ++++++++++++++++- .../kvm/x86/private_mem_conversions_test.c | 7 ++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing= /selftests/kvm/guest_memfd_test.c index 618c937f3c90..9615018a1a67 100644 --- a/tools/testing/selftests/kvm/guest_memfd_test.c +++ b/tools/testing/selftests/kvm/guest_memfd_test.c @@ -403,6 +403,17 @@ static void test_guest_memfd(unsigned long vm_type) __test_guest_memfd(vm, GUEST_MEMFD_FLAG_MMAP | GUEST_MEMFD_FLAG_INIT_SHARED); =20 + if (flags & GUEST_MEMFD_FLAG_NO_DIRECT_MAP) { + __test_guest_memfd(vm, GUEST_MEMFD_FLAG_NO_DIRECT_MAP); + if (flags & GUEST_MEMFD_FLAG_MMAP) + __test_guest_memfd(vm, GUEST_MEMFD_FLAG_NO_DIRECT_MAP | + GUEST_MEMFD_FLAG_MMAP); + if (flags & GUEST_MEMFD_FLAG_INIT_SHARED) + __test_guest_memfd(vm, GUEST_MEMFD_FLAG_NO_DIRECT_MAP | + GUEST_MEMFD_FLAG_MMAP | + GUEST_MEMFD_FLAG_INIT_SHARED); + } + kvm_vm_free(vm); } =20 @@ -445,10 +456,14 @@ static void test_guest_memfd_guest(void) TEST_ASSERT(vm_check_cap(vm, KVM_CAP_GUEST_MEMFD_FLAGS) & GUEST_MEMFD_FLA= G_INIT_SHARED, "Default VM type should support INIT_SHARED, supported flags =3D 0x%= x", vm_check_cap(vm, KVM_CAP_GUEST_MEMFD_FLAGS)); + TEST_ASSERT(vm_check_cap(vm, KVM_CAP_GUEST_MEMFD_FLAGS) & GUEST_MEMFD_FLA= G_NO_DIRECT_MAP, + "Default VM type should support INIT_SHARED, supported flags =3D 0x%= x", + vm_check_cap(vm, KVM_CAP_GUEST_MEMFD_FLAGS)); =20 size =3D vm->page_size; fd =3D vm_create_guest_memfd(vm, size, GUEST_MEMFD_FLAG_MMAP | - GUEST_MEMFD_FLAG_INIT_SHARED); + GUEST_MEMFD_FLAG_INIT_SHARED | + GUEST_MEMFD_FLAG_NO_DIRECT_MAP); vm_set_user_memory_region2(vm, slot, KVM_MEM_GUEST_MEMFD, gpa, size, NULL= , fd, 0); =20 mem =3D kvm_mmap(size, PROT_READ | PROT_WRITE, MAP_SHARED, fd); diff --git a/tools/testing/selftests/kvm/x86/private_mem_conversions_test.c= b/tools/testing/selftests/kvm/x86/private_mem_conversions_test.c index 1969f4ab9b28..8767cb4a037e 100644 --- a/tools/testing/selftests/kvm/x86/private_mem_conversions_test.c +++ b/tools/testing/selftests/kvm/x86/private_mem_conversions_test.c @@ -367,7 +367,7 @@ static void *__test_mem_conversions(void *__vcpu) } =20 static void test_mem_conversions(enum vm_mem_backing_src_type src_type, ui= nt32_t nr_vcpus, - uint32_t nr_memslots) + uint32_t nr_memslots, uint64_t gmem_flags) { /* * Allocate enough memory so that each vCPU's chunk of memory can be @@ -394,7 +394,7 @@ static void test_mem_conversions(enum vm_mem_backing_sr= c_type src_type, uint32_t =20 vm_enable_cap(vm, KVM_CAP_EXIT_HYPERCALL, (1 << KVM_HC_MAP_GPA_RANGE)); =20 - memfd =3D vm_create_guest_memfd(vm, memfd_size, 0); + memfd =3D vm_create_guest_memfd(vm, memfd_size, gmem_flags); =20 for (i =3D 0; i < nr_memslots; i++) vm_mem_add(vm, src_type, BASE_DATA_GPA + slot_size * i, @@ -474,7 +474,8 @@ int main(int argc, char *argv[]) } } =20 - test_mem_conversions(src_type, nr_vcpus, nr_memslots); + test_mem_conversions(src_type, nr_vcpus, nr_memslots, 0); + test_mem_conversions(src_type, nr_vcpus, nr_memslots, GUEST_MEMFD_FLAG_NO= _DIRECT_MAP); =20 return 0; } --=20 2.50.1 From nobody Thu Dec 18 02:07:47 2025 Received: from fra-out-010.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-010.esa.eu-central-1.outbound.mail-perimeter.amazon.com [63.178.143.178]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D5D8634BA5A; Fri, 5 Dec 2025 17:00:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=63.178.143.178 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764954023; cv=none; b=nj8DtgvO84cU13zruVKGunCAu1+e/m3NZKHCyOxJfNUg3A6rtBW8B/iywt+a9vzhiRssaUdStVI8rs0VCZELlyq1F8gWdZYg/wM0k4kfbp2jKuzpbWL2iMN7BgcPVNy+BCnlLXFf3kSFxRD1gjycYz4dz4vL5g5W4OTrUJzRnI0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764954023; c=relaxed/simple; bh=CfY0lpSCCusf0guumIaaTp6oo1XE7bsYy+BZCBBAICw=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: Content-Type:MIME-Version; b=ewi8UJaEWdxwqNsOWWzjCusrYI7EqsmudS+eBGUN0coFzrW7/kJNH/GAbkNVFw5a9Cpkpg/exgt2rKHfk0njCPK0qPYuA4laPaIiCFmdhnxQryZdLbiATFNKfiQ1SuOazyendydBJmTG+WBcYcKHUn9LJzJO4LJsFBS0FMRZoz4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.co.uk; spf=pass smtp.mailfrom=amazon.co.uk; dkim=pass (2048-bit key) header.d=amazon.co.uk header.i=@amazon.co.uk header.b=FntdWTAP; arc=none smtp.client-ip=63.178.143.178 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.co.uk header.i=@amazon.co.uk header.b="FntdWTAP" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazoncorp2; t=1764954014; x=1796490014; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=IZkODn83RzbQJlBtvngM/cf4CG/FWZhQcx+73Doqz/A=; b=FntdWTAPVrMmLcLjacoguqlMTDigr49C1ZmoFc/uJfK1lXxPggrijFBR l1Y4PVMOJvGhKZpaTAUhHh+D72iJkEZTmzyyaSx2jQg1SQDG65jP8bCEw dCJrqj3mITQ+dPo59Hw+G/RzvTL90hiAwibWyM0rAElXUP3WE0+9dhgUg 0WzYgjvACWwTWQ3RG4VNx7oJ7S2DRlDqJ8gK+T0F0DoB4FPWPhBZx5dm9 ItkXjjhwpognG3UwB4/pyn8omnd22DLDctk/eoB0dttZS8877bvfFlvzj ZtRUTinQiyh7OfT0XMTWaVYOy14GttwB1ETu2Q/JG69bXUOgK/+OOxLAa Q==; X-CSE-ConnectionGUID: 70z/0/sGTBusENXFXlvOxA== X-CSE-MsgGUID: VsA/mhPGSZae+4M1x3NuUg== X-IronPort-AV: E=Sophos;i="6.20,252,1758585600"; d="scan'208";a="6199879" Received: from ip-10-6-6-97.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.6.97]) by internal-fra-out-010.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Dec 2025 17:00:06 +0000 Received: from EX19MTAEUA001.ant.amazon.com [54.240.197.233:10609] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.6.214:2525] with esmtp (Farcaster) id f8a46f7a-3831-4d67-8580-6fc0412f1880; Fri, 5 Dec 2025 17:00:06 +0000 (UTC) X-Farcaster-Flow-ID: f8a46f7a-3831-4d67-8580-6fc0412f1880 Received: from EX19D005EUB001.ant.amazon.com (10.252.51.12) by EX19MTAEUA001.ant.amazon.com (10.252.50.223) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.29; Fri, 5 Dec 2025 17:00:03 +0000 Received: from EX19D005EUB003.ant.amazon.com (10.252.51.31) by EX19D005EUB001.ant.amazon.com (10.252.51.12) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.29; Fri, 5 Dec 2025 17:00:03 +0000 Received: from EX19D005EUB003.ant.amazon.com ([fe80::b825:becb:4b38:da0c]) by EX19D005EUB003.ant.amazon.com ([fe80::b825:becb:4b38:da0c%3]) with mapi id 15.02.2562.029; Fri, 5 Dec 2025 17:00:03 +0000 From: "Kalyazin, Nikita" To: "kvm@vger.kernel.org" , "linux-doc@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "kvmarm@lists.linux.dev" , "linux-fsdevel@vger.kernel.org" , "linux-mm@kvack.org" , "bpf@vger.kernel.org" , "linux-kselftest@vger.kernel.org" CC: "pbonzini@redhat.com" , "corbet@lwn.net" , "maz@kernel.org" , "oupton@kernel.org" , "joey.gouly@arm.com" , "suzuki.poulose@arm.com" , "yuzenghui@huawei.com" , "catalin.marinas@arm.com" , "will@kernel.org" , "seanjc@google.com" , "tglx@linutronix.de" , "mingo@redhat.com" , "bp@alien8.de" , "dave.hansen@linux.intel.com" , "x86@kernel.org" , "hpa@zytor.com" , "luto@kernel.org" , "peterz@infradead.org" , "willy@infradead.org" , "akpm@linux-foundation.org" , "david@kernel.org" , "lorenzo.stoakes@oracle.com" , "Liam.Howlett@oracle.com" , "vbabka@suse.cz" , "rppt@kernel.org" , "surenb@google.com" , "mhocko@suse.com" , "ast@kernel.org" , "daniel@iogearbox.net" , "andrii@kernel.org" , "martin.lau@linux.dev" , "eddyz87@gmail.com" , "song@kernel.org" , "yonghong.song@linux.dev" , "john.fastabend@gmail.com" , "kpsingh@kernel.org" , "sdf@fomichev.me" , "haoluo@google.com" , "jolsa@kernel.org" , "jgg@ziepe.ca" , "jhubbard@nvidia.com" , "peterx@redhat.com" , "jannh@google.com" , "pfalcato@suse.de" , "shuah@kernel.org" , "riel@surriel.com" , "baohua@kernel.org" , "ryan.roberts@arm.com" , "jgross@suse.com" , "yu-cheng.yu@intel.com" , "kas@kernel.org" , "coxu@redhat.com" , "kevin.brodsky@arm.com" , "ackerleytng@google.com" , "maobibo@loongson.cn" , "prsampat@amd.com" , "mlevitsk@redhat.com" , "isaku.yamahata@intel.com" , "jmattson@google.com" , "jthoughton@google.com" , "linux-arm-kernel@lists.infradead.org" , "vannapurve@google.com" , "jackmanb@google.com" , "aneesh.kumar@kernel.org" , "patrick.roy@linux.dev" , "Thomson, Jack" , "Itazuri, Takahiro" , "Manwaring, Derek" , "Cali, Marco" , "Kalyazin, Nikita" Subject: [PATCH v8 12/13] KVM: selftests: stuff vm_mem_backing_src_type into vm_shape Thread-Topic: [PATCH v8 12/13] KVM: selftests: stuff vm_mem_backing_src_type into vm_shape Thread-Index: AQHcZgiZNqXeJ9oFP0ueYDENIOOt0A== Date: Fri, 5 Dec 2025 17:00:02 +0000 Message-ID: <20251205165743.9341-13-kalyazin@amazon.com> References: <20251205165743.9341-1-kalyazin@amazon.com> In-Reply-To: <20251205165743.9341-1-kalyazin@amazon.com> Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: Content-Transfer-Encoding: quoted-printable Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Patrick Roy Use one of the padding fields in struct vm_shape to carry an enum vm_mem_backing_src_type value, to give the option to overwrite the default of VM_MEM_SRC_ANONYMOUS in __vm_create(). Overwriting this default will allow tests to create VMs where the test code is backed by mmap'd guest_memfd instead of anonymous memory. Signed-off-by: Patrick Roy Signed-off-by: Nikita Kalyazin --- .../testing/selftests/kvm/include/kvm_util.h | 19 ++++++++++--------- tools/testing/selftests/kvm/lib/kvm_util.c | 2 +- tools/testing/selftests/kvm/lib/x86/sev.c | 1 + .../selftests/kvm/pre_fault_memory_test.c | 1 + 4 files changed, 13 insertions(+), 10 deletions(-) diff --git a/tools/testing/selftests/kvm/include/kvm_util.h b/tools/testing= /selftests/kvm/include/kvm_util.h index 6689b43810c1..4bc4af9a40cf 100644 --- a/tools/testing/selftests/kvm/include/kvm_util.h +++ b/tools/testing/selftests/kvm/include/kvm_util.h @@ -192,7 +192,7 @@ enum vm_guest_mode { struct vm_shape { uint32_t type; uint8_t mode; - uint8_t pad0; + uint8_t src_type; uint16_t pad1; }; =20 @@ -200,14 +200,15 @@ kvm_static_assert(sizeof(struct vm_shape) =3D=3D size= of(uint64_t)); =20 #define VM_TYPE_DEFAULT 0 =20 -#define VM_SHAPE(__mode) \ -({ \ - struct vm_shape shape =3D { \ - .mode =3D (__mode), \ - .type =3D VM_TYPE_DEFAULT \ - }; \ - \ - shape; \ +#define VM_SHAPE(__mode) \ +({ \ + struct vm_shape shape =3D { \ + .mode =3D (__mode), \ + .type =3D VM_TYPE_DEFAULT, \ + .src_type =3D VM_MEM_SRC_ANONYMOUS \ + }; \ + \ + shape; \ }) =20 #if defined(__aarch64__) diff --git a/tools/testing/selftests/kvm/lib/kvm_util.c b/tools/testing/sel= ftests/kvm/lib/kvm_util.c index 28ee51253909..268a4520633b 100644 --- a/tools/testing/selftests/kvm/lib/kvm_util.c +++ b/tools/testing/selftests/kvm/lib/kvm_util.c @@ -467,7 +467,7 @@ struct kvm_vm *__vm_create(struct vm_shape shape, uint3= 2_t nr_runnable_vcpus, if (is_guest_memfd_required(shape)) flags |=3D KVM_MEM_GUEST_MEMFD; =20 - vm_userspace_mem_region_add(vm, VM_MEM_SRC_ANONYMOUS, 0, 0, nr_pages, fla= gs); + vm_userspace_mem_region_add(vm, shape.src_type, 0, 0, nr_pages, flags); for (i =3D 0; i < NR_MEM_REGIONS; i++) vm->memslots[i] =3D 0; =20 diff --git a/tools/testing/selftests/kvm/lib/x86/sev.c b/tools/testing/self= tests/kvm/lib/x86/sev.c index c3a9838f4806..d920880e4fc0 100644 --- a/tools/testing/selftests/kvm/lib/x86/sev.c +++ b/tools/testing/selftests/kvm/lib/x86/sev.c @@ -164,6 +164,7 @@ struct kvm_vm *vm_sev_create_with_one_vcpu(uint32_t typ= e, void *guest_code, struct vm_shape shape =3D { .mode =3D VM_MODE_DEFAULT, .type =3D type, + .src_type =3D VM_MEM_SRC_ANONYMOUS, }; struct kvm_vm *vm; struct kvm_vcpu *cpus[1]; diff --git a/tools/testing/selftests/kvm/pre_fault_memory_test.c b/tools/te= sting/selftests/kvm/pre_fault_memory_test.c index 93e603d91311..8a4d5af53fab 100644 --- a/tools/testing/selftests/kvm/pre_fault_memory_test.c +++ b/tools/testing/selftests/kvm/pre_fault_memory_test.c @@ -165,6 +165,7 @@ static void __test_pre_fault_memory(unsigned long vm_ty= pe, bool private) const struct vm_shape shape =3D { .mode =3D VM_MODE_DEFAULT, .type =3D vm_type, + .src_type =3D VM_MEM_SRC_ANONYMOUS, }; struct kvm_vcpu *vcpu; struct kvm_run *run; --=20 2.50.1 From nobody Thu Dec 18 02:07:47 2025 Received: from fra-out-006.esa.eu-central-1.outbound.mail-perimeter.amazon.com (fra-out-006.esa.eu-central-1.outbound.mail-perimeter.amazon.com [18.197.217.180]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5BD6D2FD1C5; Fri, 5 Dec 2025 17:00:17 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=18.197.217.180 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764954024; cv=none; b=EPNZPR/HWyPer2ui1rQVngzC5T42acIwoQHiTIdejjtDhBuaoIJY+PlTDEJ/E+ufznXkN0H5Wht5vqThY67p9FUmkOQoBK1VAEDyhB0mIpf3J8iMMT3ES0EgzE0rUO6X5ff9FJtNB62UiUPVcWW18AQaHDYtG8q9Y8tzgIxh69s= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1764954024; c=relaxed/simple; bh=5UpnxnEVgU0Oy/yU6lakZ+7I/hP5dkdcXUkzHcJlwzI=; h=From:To:CC:Subject:Date:Message-ID:References:In-Reply-To: Content-Type:MIME-Version; b=aLH30B/+AeIcEgwnU6Z9LucdqDjIop2o+SrTeuHKeq/4g0CTgQznXwIeuozmbPrpn6baXC6WVl/P1Wqmtn4qba2PmScKqkudUmhePxVCEgtyb5Y6ZF9/rsNam8oJwBoFLGs9fcFH+xwfiaqVHu4mVhNV9Z7wNbsNDsrTjPa6QWU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.co.uk; spf=pass smtp.mailfrom=amazon.co.uk; dkim=pass (2048-bit key) header.d=amazon.co.uk header.i=@amazon.co.uk header.b=Lzn5FLGC; arc=none smtp.client-ip=18.197.217.180 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=amazon.co.uk Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=amazon.co.uk header.i=@amazon.co.uk header.b="Lzn5FLGC" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazoncorp2; t=1764954020; x=1796490020; h=from:to:cc:subject:date:message-id:references: in-reply-to:content-transfer-encoding:mime-version; bh=sUXffJ10pcwposVmC9vM2kcbCnj2+0WuRal0d/tv1pE=; b=Lzn5FLGC6ghtL8M3CSFradccijxjrsRnwlzD231N5unmDCDd3edABjzU b9WA+8CaZPLXFw9lrcJDdhWY4NkPGBmOV62Lf7qipimQjC7VwhLTYkn2U KkK2zkodVlnqk2CSiTI5zM9go4ZpTOcbXbtc9ChJMuhmgTV7JOEz3Mhz3 AxtrRq5Pr347+Xd438zbB2M4Wym2miQnwQqp8s38qu9u4kQ/eisxoh3S2 R/VARxM6IA5Qknbt/Il5RfW0OGklj5i4nFEOdkKtTi+mJ5VBwd5Dr90x3 kRwkX6H00t7fA+xtD5mn1gQF1rRzJnpdwYRArwSVt47+MwvGOIzlQ0iWF A==; X-CSE-ConnectionGUID: P9803+feTBiSUBA+N8ttpw== X-CSE-MsgGUID: Pg8pNwcjQpCugjo7oQu4Vw== X-IronPort-AV: E=Sophos;i="6.20,252,1758585600"; d="scan'208";a="6301836" Received: from ip-10-6-6-97.eu-central-1.compute.internal (HELO smtpout.naws.eu-central-1.prod.farcaster.email.amazon.dev) ([10.6.6.97]) by internal-fra-out-006.esa.eu-central-1.outbound.mail-perimeter.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 05 Dec 2025 17:00:15 +0000 Received: from EX19MTAEUC002.ant.amazon.com [54.240.197.236:1229] by smtpin.naws.eu-central-1.prod.farcaster.email.amazon.dev [10.0.34.107:2525] with esmtp (Farcaster) id bb13a913-5eca-4334-b10b-a564cc743b40; Fri, 5 Dec 2025 17:00:14 +0000 (UTC) X-Farcaster-Flow-ID: bb13a913-5eca-4334-b10b-a564cc743b40 Received: from EX19D005EUB002.ant.amazon.com (10.252.51.103) by EX19MTAEUC002.ant.amazon.com (10.252.51.181) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.29; Fri, 5 Dec 2025 17:00:14 +0000 Received: from EX19D005EUB003.ant.amazon.com (10.252.51.31) by EX19D005EUB002.ant.amazon.com (10.252.51.103) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.2562.29; Fri, 5 Dec 2025 17:00:14 +0000 Received: from EX19D005EUB003.ant.amazon.com ([fe80::b825:becb:4b38:da0c]) by EX19D005EUB003.ant.amazon.com ([fe80::b825:becb:4b38:da0c%3]) with mapi id 15.02.2562.029; Fri, 5 Dec 2025 17:00:14 +0000 From: "Kalyazin, Nikita" To: "kvm@vger.kernel.org" , "linux-doc@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "kvmarm@lists.linux.dev" , "linux-fsdevel@vger.kernel.org" , "linux-mm@kvack.org" , "bpf@vger.kernel.org" , "linux-kselftest@vger.kernel.org" CC: "pbonzini@redhat.com" , "corbet@lwn.net" , "maz@kernel.org" , "oupton@kernel.org" , "joey.gouly@arm.com" , "suzuki.poulose@arm.com" , "yuzenghui@huawei.com" , "catalin.marinas@arm.com" , "will@kernel.org" , "seanjc@google.com" , "tglx@linutronix.de" , "mingo@redhat.com" , "bp@alien8.de" , "dave.hansen@linux.intel.com" , "x86@kernel.org" , "hpa@zytor.com" , "luto@kernel.org" , "peterz@infradead.org" , "willy@infradead.org" , "akpm@linux-foundation.org" , "david@kernel.org" , "lorenzo.stoakes@oracle.com" , "Liam.Howlett@oracle.com" , "vbabka@suse.cz" , "rppt@kernel.org" , "surenb@google.com" , "mhocko@suse.com" , "ast@kernel.org" , "daniel@iogearbox.net" , "andrii@kernel.org" , "martin.lau@linux.dev" , "eddyz87@gmail.com" , "song@kernel.org" , "yonghong.song@linux.dev" , "john.fastabend@gmail.com" , "kpsingh@kernel.org" , "sdf@fomichev.me" , "haoluo@google.com" , "jolsa@kernel.org" , "jgg@ziepe.ca" , "jhubbard@nvidia.com" , "peterx@redhat.com" , "jannh@google.com" , "pfalcato@suse.de" , "shuah@kernel.org" , "riel@surriel.com" , "baohua@kernel.org" , "ryan.roberts@arm.com" , "jgross@suse.com" , "yu-cheng.yu@intel.com" , "kas@kernel.org" , "coxu@redhat.com" , "kevin.brodsky@arm.com" , "ackerleytng@google.com" , "maobibo@loongson.cn" , "prsampat@amd.com" , "mlevitsk@redhat.com" , "isaku.yamahata@intel.com" , "jmattson@google.com" , "jthoughton@google.com" , "linux-arm-kernel@lists.infradead.org" , "vannapurve@google.com" , "jackmanb@google.com" , "aneesh.kumar@kernel.org" , "patrick.roy@linux.dev" , "Thomson, Jack" , "Itazuri, Takahiro" , "Manwaring, Derek" , "Cali, Marco" , "Kalyazin, Nikita" Subject: [PATCH v8 13/13] KVM: selftests: Test guest execution from direct map removed gmem Thread-Topic: [PATCH v8 13/13] KVM: selftests: Test guest execution from direct map removed gmem Thread-Index: AQHcZgifgxQmXVCcFkmA//3xLcHAXw== Date: Fri, 5 Dec 2025 17:00:14 +0000 Message-ID: <20251205165743.9341-14-kalyazin@amazon.com> References: <20251205165743.9341-1-kalyazin@amazon.com> In-Reply-To: <20251205165743.9341-1-kalyazin@amazon.com> Accept-Language: en-GB, en-US Content-Language: en-US X-MS-Has-Attach: X-MS-TNEF-Correlator: Content-Transfer-Encoding: quoted-printable Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" From: Patrick Roy Add a selftest that loads itself into guest_memfd (via GUEST_MEMFD_FLAG_MMAP) and triggers an MMIO exit when executed. This exercises x86 MMIO emulation code inside KVM for guest_memfd-backed memslots where the guest_memfd folios are direct map removed. Particularly, it validates that x86 MMIO emulation code (guest page table walks + instruction fetch) correctly accesses gmem through the VMA that's been reflected into the memslot's userspace_addr field (instead of trying to do direct map accesses). Signed-off-by: Patrick Roy Signed-off-by: Nikita Kalyazin --- .../selftests/kvm/set_memory_region_test.c | 52 +++++++++++++++++-- 1 file changed, 48 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/kvm/set_memory_region_test.c b/tools/t= esting/selftests/kvm/set_memory_region_test.c index 7fe427ff9b38..6c57fb036b20 100644 --- a/tools/testing/selftests/kvm/set_memory_region_test.c +++ b/tools/testing/selftests/kvm/set_memory_region_test.c @@ -602,6 +602,41 @@ static void test_mmio_during_vectoring(void) =20 kvm_vm_free(vm); } + +static void guest_code_trigger_mmio(void) +{ + /* + * Read some GPA that is not backed by a memslot. KVM consider this + * as MMIO and tell userspace to emulate the read. + */ + READ_ONCE(*((uint64_t *)MEM_REGION_GPA)); + + GUEST_DONE(); +} + +static void test_guest_memfd_mmio(void) +{ + struct kvm_vm *vm; + struct kvm_vcpu *vcpu; + struct vm_shape shape =3D { + .mode =3D VM_MODE_DEFAULT, + .src_type =3D VM_MEM_SRC_GUEST_MEMFD_NO_DIRECT_MAP, + }; + pthread_t vcpu_thread; + + pr_info("Testing MMIO emulation for instructions in gmem\n"); + + vm =3D __vm_create_shape_with_one_vcpu(shape, &vcpu, 0, guest_code_trigge= r_mmio); + + virt_map(vm, MEM_REGION_GPA, MEM_REGION_GPA, 1); + + pthread_create(&vcpu_thread, NULL, vcpu_worker, vcpu); + + /* If the MMIO read was successfully emulated, the vcpu thread will exit = */ + pthread_join(vcpu_thread, NULL); + + kvm_vm_free(vm); +} #endif =20 int main(int argc, char *argv[]) @@ -625,10 +660,19 @@ int main(int argc, char *argv[]) test_add_max_memory_regions(); =20 #ifdef __x86_64__ - if (kvm_has_cap(KVM_CAP_GUEST_MEMFD) && - (kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM))) { - test_add_private_memory_region(); - test_add_overlapping_private_memory_regions(); + if (kvm_has_cap(KVM_CAP_GUEST_MEMFD)) { + uint64_t valid_flags =3D kvm_check_cap(KVM_CAP_GUEST_MEMFD_FLAGS); + + if (kvm_check_cap(KVM_CAP_VM_TYPES) & BIT(KVM_X86_SW_PROTECTED_VM)) { + test_add_private_memory_region(); + test_add_overlapping_private_memory_regions(); + } + + if ((valid_flags & GUEST_MEMFD_FLAG_MMAP) + && (valid_flags & GUEST_MEMFD_FLAG_NO_DIRECT_MAP)) + test_guest_memfd_mmio(); + else + pr_info("Skipping tests requiring GUEST_MEMFD_FLAG_MMAP | GUEST_MEMFD_F= LAG_NO_DIRECT_MAP"); } else { pr_info("Skipping tests for KVM_MEM_GUEST_MEMFD memory regions\n"); } --=20 2.50.1