From patchwork Wed Oct 30 13:49:05 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Roy X-Patchwork-Id: 13856548 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 58113D5CCB0 for ; Wed, 30 Oct 2024 13:50:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CD2FD6B0098; Wed, 30 Oct 2024 09:50:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C5BB56B0099; Wed, 30 Oct 2024 09:50:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AAEFA6B009B; Wed, 30 Oct 2024 09:50:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 897EC6B0098 for ; Wed, 30 Oct 2024 09:50:08 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 391761C5FBC for ; Wed, 30 Oct 2024 13:50:08 +0000 (UTC) X-FDA: 82730400732.17.D5C9ECA Received: from smtp-fw-33001.amazon.com (smtp-fw-33001.amazon.com [207.171.190.10]) by imf11.hostedemail.com (Postfix) with ESMTP id 7E4F540021 for ; Wed, 30 Oct 2024 13:49:34 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=MJtRUPEo; dmarc=pass (policy=quarantine) header.from=amazon.co.uk; spf=pass (imf11.hostedemail.com: domain of "prvs=02621381b=roypat@amazon.co.uk" designates 207.171.190.10 as permitted sender) smtp.mailfrom="prvs=02621381b=roypat@amazon.co.uk" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730296088; a=rsa-sha256; cv=none; b=dVKb5aoXiFyerFw/PBHHZ7YxPWrvtO/aEC9zb1cMLQS8STGuUW9h5p1c4ntTnoWFItdZVV alvMnbUZJrooVpLQ9K4PhRigc0ZOOPwPWoutguLmo+bERrAgq4CF5AU0oD7iRHHYr4edsY w6gy48Dxl77s+xQERA9uKRyK6h64fB8= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=MJtRUPEo; dmarc=pass (policy=quarantine) header.from=amazon.co.uk; spf=pass (imf11.hostedemail.com: domain of "prvs=02621381b=roypat@amazon.co.uk" designates 207.171.190.10 as permitted sender) smtp.mailfrom="prvs=02621381b=roypat@amazon.co.uk" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730296088; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=blYJo1yNoaziWCkfOsSivyw3bncyOYuZYYL2mpgphk8=; b=ihZqT+IEQQK2r3XGhyxaFXDGXzzdh8SYkA0WLxFFxUS88q2PAGNwXBSvygHRjb+eL1oZ9M vVLWALqL2fl4JpijcnkvlrL3eFErnKXqPY+qJa8Yuq/gdfLevcaU1iYwkBhW7W5JDTzpGP 8Sqipw1NU6i1ceL4NUCaMd5qVcH+IBs= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazon201209; t=1730296207; x=1761832207; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=blYJo1yNoaziWCkfOsSivyw3bncyOYuZYYL2mpgphk8=; b=MJtRUPEo47mJkkNHPu+PWo3q3C/vRvAB5DL1+yelLuZUhVs9VL3CH8IS 99jRkUWMtwY1pSxVFD0vwvpkdMJy9w9jewKyDYW3DKb8cV8Kqv1CiIQo3 ehUSCavPeZ3edMwW/Z3bslG8Nq67nw330abDejoAjBToMmh316PvTO+Vn A=; X-IronPort-AV: E=Sophos;i="6.11,245,1725321600"; d="scan'208";a="381122622" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.210]) by smtp-border-fw-33001.sea14.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Oct 2024 13:49:59 +0000 Received: from EX19MTAEUA001.ant.amazon.com [10.0.17.79:33275] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.20.52:2525] with esmtp (Farcaster) id c77b8478-8ab2-4663-a8a5-9c2518ad8ef5; Wed, 30 Oct 2024 13:49:56 +0000 (UTC) X-Farcaster-Flow-ID: c77b8478-8ab2-4663-a8a5-9c2518ad8ef5 Received: from EX19D030EUB004.ant.amazon.com (10.252.61.33) by EX19MTAEUA001.ant.amazon.com (10.252.50.192) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Wed, 30 Oct 2024 13:49:49 +0000 Received: from EX19MTAUEA002.ant.amazon.com (10.252.134.9) by EX19D030EUB004.ant.amazon.com (10.252.61.33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Wed, 30 Oct 2024 13:49:48 +0000 Received: from email-imr-corp-prod-pdx-all-2c-8a67eb17.us-west-2.amazon.com (10.43.8.2) by mail-relay.amazon.com (10.252.134.34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34 via Frontend Transport; Wed, 30 Oct 2024 13:49:48 +0000 Received: from ua2d7e1a6107c5b.home (dev-dsk-roypat-1c-dbe2a224.eu-west-1.amazon.com [172.19.88.180]) by email-imr-corp-prod-pdx-all-2c-8a67eb17.us-west-2.amazon.com (Postfix) with ESMTPS id EC34C4032D; Wed, 30 Oct 2024 13:49:37 +0000 (UTC) From: Patrick Roy To: , , , , , , , , CC: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Patrick Roy Subject: [RFC PATCH v3 1/6] arch: introduce set_direct_map_valid_noflush() Date: Wed, 30 Oct 2024 13:49:05 +0000 Message-ID: <20241030134912.515725-2-roypat@amazon.co.uk> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241030134912.515725-1-roypat@amazon.co.uk> References: <20241030134912.515725-1-roypat@amazon.co.uk> MIME-Version: 1.0 X-Rspamd-Queue-Id: 7E4F540021 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: mf4hzu3udp97hiw1fuuwk7shddpzcrmg X-HE-Tag: 1730296174-489159 X-HE-Meta: U2FsdGVkX1+CgGrU4o5gFJDE5p8VlohtWbuIhBCp14iUoqZUqu1/WhDcuSDUbIy9qGKDu4u7HnRHCl16JYCxn5w73tiTN/mOhIPdagRDI5lZCgutuy76Bb4ukesNJw+zjctti02/cRmxJnX0EmObsOeluoKheEEjTl5Mmk7NbFh3B0L8HIHoRDinALGohSR13GDRW8ddWjrtXRgXFkeIWeJBdYAA92gIUSdhxmG/D8vGbg0dB8mM1gWuiwZYk8xCUsqL9cbYB9VSkC1z1+NGhlS7qhorPvuay1dSaM4KUfNfn/nnBQ2htjDy2m00z4zrteKpEwTmIeWezulXlhULR7Dy15cHQS3Vpn3QAH3aNBwTO+nlEimIcI3AX5hu9PknY95MQthiNOPdZV+BcqatKordp5M1oL+TFL0nevw7hJaJjwm22F5o49JngNe55BDTifQpqXUiDP5juiKmhUu8K7UKvR2KnMbSH9/S2cUCY9OdsmP1C7j6gRMS4zIvfLJvjJd0WI3D0Oga2fF+vHPkhpD2aGIICAeaZSA8/anrPkFBj4fsHTjyzSag4q+Ww7DSfXsgRZGIwt3/jvy9DaZ+wsj8DICACwsTwXPGpKmtHjCLQCMRkNSHj2HHS4v2eaatPbrk4XDrCgwh0QCWFyqsgM2B+np5ZbfWGjc5c5MeZ8vyeChApIiYxRlbHXAGWT/tgbWThkb8eHh6CyzDpWcrVUxs10tiJK+0hlvFmKGtKA+QexWpbDe6EOJgjgITLYzUUIubF107VibNmZ4+6lnMhr8E7JujuGyuoPtXjLmpJCQrX8KO+MJR31QBY1z2u5Q85cUVsruz088Tl1VzKNgDcO7YkRzdCRLMuU6hiyL+b/t2g7jTdH3EC9vYi0n5X5oN0KEqPIwqWXX27fDIzmR9rAbWaC8H2CfLXhNSBLsRFb1xtND8CyzkaKAhlPiOPdfVRZsCZM6cv48fd7kWRDg DwoSDMMJ U7DN3QjdJEGVWfvgggTe2M95webJ8okuc13VCKUSau/f+MeXCsxOUGElCtejklWFURLB5cR4i9HiU22JlkKSbKtmnj96wBC2Ja8EmR33ir6QV5NTK2aUMW7WjHt63XTiJr5NkPN7rzpONEv8loYToeervr2r4yiD6Aqgmd9mpoy5e4bM9wWgKLFbg+GNKNZ9NW/RBzeHJ2XXVkw+eWQ+BUNM9cYf6xCB+2fqShXIMB5HPifNvhoCV82WLyHMm0HtaLzrc6QMsWCYeIMqaiqX1I1xW6IABAtl6hRFZ7YKOQoNQKb6FobRlc/Y2KaoasSSr6qoLfDLDHPlKU/1xcoF7wH222kAU3FXcM34yR8uSatAr1QSDPhwVjJqFomB0Dalr4njfN/xAucizQy4Y1ZMeaOj4VQvwuFLMtW812VAa9jt9oajVH+Mnq5hNXUUGishh07Um38vsJ+oxiTI8O/aJlIL3gg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: From: "Mike Rapoport (Microsoft)" From: Mike Rapoport (Microsoft) Add an API that will allow updates of the direct/linear map for a set of physically contiguous pages. It will be used in the following patches. Signed-off-by: Mike Rapoport (Microsoft) Signed-off-by: Patrick Roy --- arch/arm64/include/asm/set_memory.h | 1 + arch/arm64/mm/pageattr.c | 10 ++++++++++ arch/loongarch/include/asm/set_memory.h | 1 + arch/loongarch/mm/pageattr.c | 21 +++++++++++++++++++++ arch/riscv/include/asm/set_memory.h | 1 + arch/riscv/mm/pageattr.c | 15 +++++++++++++++ arch/s390/include/asm/set_memory.h | 1 + arch/s390/mm/pageattr.c | 11 +++++++++++ arch/x86/include/asm/set_memory.h | 1 + arch/x86/mm/pat/set_memory.c | 8 ++++++++ include/linux/set_memory.h | 6 ++++++ 11 files changed, 76 insertions(+) base-commit: 5cb1659f412041e4780f2e8ee49b2e03728a2ba6 diff --git a/arch/arm64/include/asm/set_memory.h b/arch/arm64/include/asm/set_memory.h index 917761feeffdd..98088c043606a 100644 --- a/arch/arm64/include/asm/set_memory.h +++ b/arch/arm64/include/asm/set_memory.h @@ -13,6 +13,7 @@ int set_memory_valid(unsigned long addr, int numpages, int enable); int set_direct_map_invalid_noflush(struct page *page); int set_direct_map_default_noflush(struct page *page); +int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid); bool kernel_page_present(struct page *page); #endif /* _ASM_ARM64_SET_MEMORY_H */ diff --git a/arch/arm64/mm/pageattr.c b/arch/arm64/mm/pageattr.c index 0e270a1c51e64..01225900293ac 100644 --- a/arch/arm64/mm/pageattr.c +++ b/arch/arm64/mm/pageattr.c @@ -192,6 +192,16 @@ int set_direct_map_default_noflush(struct page *page) PAGE_SIZE, change_page_range, &data); } +int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid) +{ + unsigned long addr = (unsigned long)page_address(page); + + if (!can_set_direct_map()) + return 0; + + return set_memory_valid(addr, nr, valid); +} + #ifdef CONFIG_DEBUG_PAGEALLOC void __kernel_map_pages(struct page *page, int numpages, int enable) { diff --git a/arch/loongarch/include/asm/set_memory.h b/arch/loongarch/include/asm/set_memory.h index d70505b6676cb..55dfaefd02c8a 100644 --- a/arch/loongarch/include/asm/set_memory.h +++ b/arch/loongarch/include/asm/set_memory.h @@ -17,5 +17,6 @@ int set_memory_rw(unsigned long addr, int numpages); bool kernel_page_present(struct page *page); int set_direct_map_default_noflush(struct page *page); int set_direct_map_invalid_noflush(struct page *page); +int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid); #endif /* _ASM_LOONGARCH_SET_MEMORY_H */ diff --git a/arch/loongarch/mm/pageattr.c b/arch/loongarch/mm/pageattr.c index ffd8d76021d47..f14b40c968b48 100644 --- a/arch/loongarch/mm/pageattr.c +++ b/arch/loongarch/mm/pageattr.c @@ -216,3 +216,24 @@ int set_direct_map_invalid_noflush(struct page *page) return __set_memory(addr, 1, __pgprot(0), __pgprot(_PAGE_PRESENT | _PAGE_VALID)); } + +int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid) +{ + unsigned long addr = (unsigned long)page_address(page); + pgprot_t set, clear; + + return __set_memory((unsigned long)page_address(page), nr, set, clear); + + if (addr < vm_map_base) + return 0; + + if (valid) { + set = PAGE_KERNEL; + clear = __pgprot(0); + } else { + set = __pgprot(0); + clear = __pgprot(_PAGE_PRESENT | _PAGE_VALID); + } + + return __set_memory(addr, 1, set, clear); +} diff --git a/arch/riscv/include/asm/set_memory.h b/arch/riscv/include/asm/set_memory.h index ab92fc84e1fc9..ea263d3683ef6 100644 --- a/arch/riscv/include/asm/set_memory.h +++ b/arch/riscv/include/asm/set_memory.h @@ -42,6 +42,7 @@ static inline int set_kernel_memory(char *startp, char *endp, int set_direct_map_invalid_noflush(struct page *page); int set_direct_map_default_noflush(struct page *page); +int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid); bool kernel_page_present(struct page *page); #endif /* __ASSEMBLY__ */ diff --git a/arch/riscv/mm/pageattr.c b/arch/riscv/mm/pageattr.c index 271d01a5ba4da..d815448758a19 100644 --- a/arch/riscv/mm/pageattr.c +++ b/arch/riscv/mm/pageattr.c @@ -386,6 +386,21 @@ int set_direct_map_default_noflush(struct page *page) PAGE_KERNEL, __pgprot(_PAGE_EXEC)); } +int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid) +{ + pgprot_t set, clear; + + if (valid) { + set = PAGE_KERNEL; + clear = __pgprot(_PAGE_EXEC); + } else { + set = __pgprot(0); + clear = __pgprot(_PAGE_PRESENT); + } + + return __set_memory((unsigned long)page_address(page), nr, set, clear); +} + #ifdef CONFIG_DEBUG_PAGEALLOC static int debug_pagealloc_set_page(pte_t *pte, unsigned long addr, void *data) { diff --git a/arch/s390/include/asm/set_memory.h b/arch/s390/include/asm/set_memory.h index 06fbabe2f66c9..240bcfbdcdcec 100644 --- a/arch/s390/include/asm/set_memory.h +++ b/arch/s390/include/asm/set_memory.h @@ -62,5 +62,6 @@ __SET_MEMORY_FUNC(set_memory_4k, SET_MEMORY_4K) int set_direct_map_invalid_noflush(struct page *page); int set_direct_map_default_noflush(struct page *page); +int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid); #endif diff --git a/arch/s390/mm/pageattr.c b/arch/s390/mm/pageattr.c index 5f805ad42d4c3..4c7ee74aa130d 100644 --- a/arch/s390/mm/pageattr.c +++ b/arch/s390/mm/pageattr.c @@ -406,6 +406,17 @@ int set_direct_map_default_noflush(struct page *page) return __set_memory((unsigned long)page_to_virt(page), 1, SET_MEMORY_DEF); } +int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid) +{ + unsigned long flags; + + if (valid) + flags = SET_MEMORY_DEF; + else + flags = SET_MEMORY_INV; + + return __set_memory((unsigned long)page_to_virt(page), nr, flags); +} #if defined(CONFIG_DEBUG_PAGEALLOC) || defined(CONFIG_KFENCE) static void ipte_range(pte_t *pte, unsigned long address, int nr) diff --git a/arch/x86/include/asm/set_memory.h b/arch/x86/include/asm/set_memory.h index 4b2abce2e3e7d..cc62ef70ccc0a 100644 --- a/arch/x86/include/asm/set_memory.h +++ b/arch/x86/include/asm/set_memory.h @@ -89,6 +89,7 @@ int set_pages_rw(struct page *page, int numpages); int set_direct_map_invalid_noflush(struct page *page); int set_direct_map_default_noflush(struct page *page); +int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid); bool kernel_page_present(struct page *page); extern int kernel_set_to_readonly; diff --git a/arch/x86/mm/pat/set_memory.c b/arch/x86/mm/pat/set_memory.c index 44f7b2ea6a073..069e421c22474 100644 --- a/arch/x86/mm/pat/set_memory.c +++ b/arch/x86/mm/pat/set_memory.c @@ -2444,6 +2444,14 @@ int set_direct_map_default_noflush(struct page *page) return __set_pages_p(page, 1); } +int set_direct_map_valid_noflush(struct page *page, unsigned nr, bool valid) +{ + if (valid) + return __set_pages_p(page, nr); + + return __set_pages_np(page, nr); +} + #ifdef CONFIG_DEBUG_PAGEALLOC void __kernel_map_pages(struct page *page, int numpages, int enable) { diff --git a/include/linux/set_memory.h b/include/linux/set_memory.h index e7aec20fb44f1..3030d9245f5ac 100644 --- a/include/linux/set_memory.h +++ b/include/linux/set_memory.h @@ -34,6 +34,12 @@ static inline int set_direct_map_default_noflush(struct page *page) return 0; } +static inline int set_direct_map_valid_noflush(struct page *page, + unsigned nr, bool valid) +{ + return 0; +} + static inline bool kernel_page_present(struct page *page) { return true; From patchwork Wed Oct 30 13:49:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Roy X-Patchwork-Id: 13856549 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 48011D5CCAF for ; Wed, 30 Oct 2024 13:50:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C85866B009B; Wed, 30 Oct 2024 09:50:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id C18856B009D; Wed, 30 Oct 2024 09:50:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A14436B00A1; Wed, 30 Oct 2024 09:50:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 78DDA6B009B for ; Wed, 30 Oct 2024 09:50:10 -0400 (EDT) Received: from smtpin19.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 05AF6140E0E for ; Wed, 30 Oct 2024 13:50:09 +0000 (UTC) X-FDA: 82730401404.19.B573A11 Received: from smtp-fw-80009.amazon.com (smtp-fw-80009.amazon.com [99.78.197.220]) by imf25.hostedemail.com (Postfix) with ESMTP id 6CB53A002E for ; Wed, 30 Oct 2024 13:49:49 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=EYnRC2Tl; spf=pass (imf25.hostedemail.com: domain of "prvs=02621381b=roypat@amazon.co.uk" designates 99.78.197.220 as permitted sender) smtp.mailfrom="prvs=02621381b=roypat@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.co.uk ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730296127; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=S8hLaLmpaqH2hbmpwLE9UIqf50NoMS+38eolo/Dz8xQ=; b=UFxJWvyrWc7lNFf6pRBZW+C8dQ6Qs+aN8y0TEb6mujiJa4Ttyl9Y8udlwlNtHIAUpcSBYZ F81AoW4zMtbPNWa3/AeTZSRStX/GYaONUskDFlTSp0DyuZCMvcD+SUeyAeFaUxjFAmGch9 Htmo8ebGCc/tpKq00rxy4iqEWqkP5Zk= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=EYnRC2Tl; spf=pass (imf25.hostedemail.com: domain of "prvs=02621381b=roypat@amazon.co.uk" designates 99.78.197.220 as permitted sender) smtp.mailfrom="prvs=02621381b=roypat@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.co.uk ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730296127; a=rsa-sha256; cv=none; b=a4D7kLaYv6OASJEC+dnWJPtM0KkuuejsZDEbVGBK5ilf69NIDbZ6CsfcQ1peFDw9f2/Kj8 Ako6eg+99qUPZqCQ8MnaCDs8zb1M+k8zqKack2oRc410r3QBjsP668V8RQw0Skin/8JBsf 8mv0rNxFpE6Up+uzmKeGGT/FXI1C1qo= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazon201209; t=1730296207; x=1761832207; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=S8hLaLmpaqH2hbmpwLE9UIqf50NoMS+38eolo/Dz8xQ=; b=EYnRC2Tlqwf1avSDimOE7hFGv86ivwZcSCFSrnfifdAMrXXoo2R2z3TI FGn0jcC4rUall/ZmRwaXTMOe0kDwK86JH5duQUHoDJz/CP1eEWfPJXLcW jGWR8WxB09Xr88WZhqJJzgqe5Do48hmVJ+1mnNcg+i/c9Fw/FNG1SIDV0 Q=; X-IronPort-AV: E=Sophos;i="6.11,245,1725321600"; d="scan'208";a="142980599" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.210]) by smtp-border-fw-80009.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Oct 2024 13:50:06 +0000 Received: from EX19MTAUWC002.ant.amazon.com [10.0.21.151:54967] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.22.121:2525] with esmtp (Farcaster) id 08c8009f-6d75-4831-aeed-52be2404b382; Wed, 30 Oct 2024 13:50:06 +0000 (UTC) X-Farcaster-Flow-ID: 08c8009f-6d75-4831-aeed-52be2404b382 Received: from EX19D003UWC002.ant.amazon.com (10.13.138.169) by EX19MTAUWC002.ant.amazon.com (10.250.64.143) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Wed, 30 Oct 2024 13:50:00 +0000 Received: from EX19MTAUWA002.ant.amazon.com (10.250.64.202) by EX19D003UWC002.ant.amazon.com (10.13.138.169) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.35; Wed, 30 Oct 2024 13:50:00 +0000 Received: from email-imr-corp-prod-pdx-all-2c-8a67eb17.us-west-2.amazon.com (10.25.36.210) by mail-relay.amazon.com (10.250.64.203) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34 via Frontend Transport; Wed, 30 Oct 2024 13:50:00 +0000 Received: from ua2d7e1a6107c5b.home (dev-dsk-roypat-1c-dbe2a224.eu-west-1.amazon.com [172.19.88.180]) by email-imr-corp-prod-pdx-all-2c-8a67eb17.us-west-2.amazon.com (Postfix) with ESMTPS id 8A1224032D; Wed, 30 Oct 2024 13:49:50 +0000 (UTC) From: Patrick Roy To: , , , , , , , , CC: Patrick Roy , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [RFC PATCH v3 2/6] kvm: gmem: add flag to remove memory from kernel direct map Date: Wed, 30 Oct 2024 13:49:06 +0000 Message-ID: <20241030134912.515725-3-roypat@amazon.co.uk> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241030134912.515725-1-roypat@amazon.co.uk> References: <20241030134912.515725-1-roypat@amazon.co.uk> MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 6CB53A002E X-Stat-Signature: bopxx1fxj6au3nua3may9t9gy5ffhhyi X-HE-Tag: 1730296189-180308 X-HE-Meta: U2FsdGVkX19bkFWlYBKSmc8UbYC9aCC/Bx40vSw390dIW7sCvSLHMTCeG1ZQpuWvPRaMwZqRhKRQojVz60CXi7XmY+JSJBDtT1cBNssv3tVsZdMIHrngBoJymyS498j4goAAxMSgQEiUGYyRRKidCPcyx3Asjyy6ErrWDHsoafbTN5IGmlFVgsRB8KMccVA1H4R2ZiHRb++xv8EZH4ZS4z75eRjZGz+cEYNHWjGPjWdQkxEn4eBTJ+Vq2jXAni7X/C/FuEOSwsmynUDDoeGIguUeEaPDAJbOQ2EH8q+49ZJx4+dOJHD615j+Gfom0JHoW9pJPjcYxaRsNI5WiEZ7YRPiEh+gVYmbgmeMI1ATpPA0kmSMuCb0Mb0OsM0ihkdm8JIk9ZhjfTEF//B464ySYesFv6wLolGa0XHOYa8qeaQykHC5r2o3j8iu8xMRy3VJRmtnjOQ+vvCI2CujaBFcsHxzedMTE3/WGr4Hu2YlW0OPdZOL4VQPAjx/lFm6H2Sne4XMI/E3QwbswTnFL4Ic1AQD1RPneHJuMGxAiLqjKPi+WrdZ4CrR6myFzF9D/PriZM6EPTRdtHF7V4zg4TVnJZ6jc1jJwcLB4MP7n66EYpMmsknS7HOUuQIUBFppjWhrjaJV+daGelmzUi+QMAW6onc+0/Jm2m64/CRqc8sxNTdCfRazS7Ce1jegA/H5IQmCVbtxIsJ+cIcZ5O8zJFeTks88n4VuCr8jhc1Hx9bTlYsRnUoZ1sO9fZbxwLWD/S7jgwwioGjNoOiiosZZ2INJ4PQ0egGCK3GLbyyVzrmNx7IkQvoGg/R4yXpA4I6R0seIFhOLemle5Moz8baCSsY8FqdRn61oSLSIF2zSsb/u6Mke6Tpk+p4BVOmRsePUFhExte4ESlwFNYKyfWee64FMm4ORZ1ImNZHT5J+K2ic9r3abfdqaT4MjdmtJqmf516FPKCwweJhnCmodYqO7ftD /asPZPHv ZaZf0GFRKQgjtn6Ya3XTZte1ULdAELfGxrmxZ/pzvX/eWKksdODD2gCRs3cJOmJbMFjYLKsRtzKR5LLVTp5VsXAo7/cQqMSOfVFuxcmbiutYv+XQgixhQ6E6XcClCt8BJigvdCggyiVD2yy7uWnqlOD7j7hkDjJyb/K3TAkjJ6JzduLwcTlRmhOlbzbiBa9YlxLzwHLjIXkP7AFolc4+5hNe+jOWsorbdoz/VhJiQfrm/7p4x80RaV+Mwdhsq//9uBlHhyWhtqRFMYZLNhBgM1dFhuV/qYLqCSU2VgzQKoMjHVetHyHpjgT23ejOKMGuikRQCD9MHd3OAJp9tlQ3CpDcjqWEuHS776NXc3h42bALIPcP1sPMPD/fkN8cklwFd9Re7sNAcjs8SUNQdKP28n2XUB1JlnFFGtgliiyGqsWHJz7OKvn7G4XTEig3/6EYeK/u6dOmvvxmxjtj2YMrdJaYsDg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Add a new flag, KVM_GMEM_NO_DIRECT_MAP, to KVM_CREATE_GUEST_MEMFD, which causes KVM to remove the folios backing this guest_memfd from the direct map after preparation/population. This flag is only exposed on architectures that can set the direct map (the notable exception here being ARM64 if the direct map is not set up at 4K granularity), otherwise EOPNOTSUPP is returned. This patch also implements infrastructure for tracking (temporary) reinsertation of memory ranges into the direct map (more accurately: It allows recording that specific memory ranges deviate from the default direct map setup. Currently the default setup is always "direct map entries removed", but it is trivial to extend this with some "default_state_for_vm_type" mechanism to cover the pKVM usecase of memory starting off with directe map entries present). An xarray tracks this at page granularity, to be compatible with future hugepages usecases that might require subranges of hugetlb folios to have direct map entries restored. This xarray holds entries for each page that has a direct map state deviating from the default, and holes for all pages whose direct map state matches the default, the idea being that these "deviations" will be rare. kvm_gmem_folio_configure_direct_map applies the configuration stored in the xarray to a given folio, and is called for each new gmem folio after preparation/population. Storing direct map state in the gmem inode has two advantages: 1) We can track direct map state at page granularity even for huge folios (see also Ackerley's series on hugetlbfs support in guest_memfd [1]) 2) We can pre-configure the direct map state of not-yet-faulted in folios. This would for example be needed if a VMM is receiving a virtio buffer that the guest is requested it to fill. In this case, the pages backing the guest physical address range of the buffer might not be faulted in yet, and thus would be faulted when the VMM tries to write to them, and at this point we would need to ensure direct map entries are present) Note that this patch does not include operations for manipulating the direct map state xarray, or for changing direct map state of already existing folios. These routines are sketched out in the following patch, although are not needed in this initial patch series. When a gmem folio is freed, it is reinserted into the direct map (and failing this, marked as HWPOISON to avoid any other part of the kernel accidentally touching folios without complete direct map entries). The direct map configuration stored in the xarray is _not_ reset when the folio is freed (although this could be implemented by storing the reference to the xarray in the folio's private data instead of only the inode). [1]: https://lore.kernel.org/kvm/cover.1726009989.git.ackerleytng@google.com/ Signed-off-by: Patrick Roy --- include/uapi/linux/kvm.h | 2 + virt/kvm/guest_memfd.c | 150 +++++++++++++++++++++++++++++++++++---- 2 files changed, 137 insertions(+), 15 deletions(-) diff --git a/include/uapi/linux/kvm.h b/include/uapi/linux/kvm.h index 637efc0551453..81b0f4a236b8c 100644 --- a/include/uapi/linux/kvm.h +++ b/include/uapi/linux/kvm.h @@ -1564,6 +1564,8 @@ struct kvm_create_guest_memfd { __u64 reserved[6]; }; +#define KVM_GMEM_NO_DIRECT_MAP (1ULL << 0) + #define KVM_PRE_FAULT_MEMORY _IOWR(KVMIO, 0xd5, struct kvm_pre_fault_memory) struct kvm_pre_fault_memory { diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 47a9f68f7b247..50ffc2ad73eda 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "kvm_mm.h" @@ -13,6 +14,88 @@ struct kvm_gmem { struct list_head entry; }; +struct kvm_gmem_inode_private { + unsigned long long flags; + + /* + * direct map configuration of the gmem instance this private data + * is associated with. present indices indicate a desired direct map + * configuration deviating from default_direct_map_state (e.g. if + * default_direct_map_state is false/not present, then the xarray + * contains all indices for which direct map entries are restored). + */ + struct xarray direct_map_state; + bool default_direct_map_state; +}; + +static bool kvm_gmem_test_no_direct_map(struct kvm_gmem_inode_private *gmem_priv) +{ + return ((unsigned long)gmem_priv->flags & KVM_GMEM_NO_DIRECT_MAP) != 0; +} + +/* + * Configure the direct map present/not present state of @folio based on + * the xarray stored in the associated inode's private data. + * + * Assumes the folio lock is held. + */ +static int kvm_gmem_folio_configure_direct_map(struct folio *folio) +{ + struct inode *inode = folio_inode(folio); + struct kvm_gmem_inode_private *gmem_priv = inode->i_private; + bool default_state = gmem_priv->default_direct_map_state; + + pgoff_t start = folio_index(folio); + pgoff_t last = start + folio_nr_pages(folio) - 1; + + struct xarray *xa = &gmem_priv->direct_map_state; + unsigned long index; + void *entry; + + pgoff_t range_start = start; + unsigned long npages = 1; + int r = 0; + + if (!kvm_gmem_test_no_direct_map(gmem_priv)) + goto out; + + r = set_direct_map_valid_noflush(folio_page(folio, 0), folio_nr_pages(folio), + default_state); + if (r) + goto out; + + if (!xa_find_after(xa, &range_start, last, XA_PRESENT)) + goto out_flush; + + xa_for_each_range(xa, index, entry, range_start, last) { + ++npages; + + if (index == range_start + npages) + continue; + + r = set_direct_map_valid_noflush(folio_file_page(folio, range_start), npages - 1, + !default_state); + if (r) + goto out_flush; + + range_start = index; + npages = 1; + } + + r = set_direct_map_valid_noflush(folio_file_page(folio, range_start), npages, + !default_state); + +out_flush: + /* + * Use PG_private to track that this folio has had potentially some of + * its direct map entries modified, so that we can restore them in free_folio. + */ + folio_set_private(folio); + flush_tlb_kernel_range(start, start + folio_size(folio)); +out: + return r; +} + /** * folio_file_pfn - like folio_file_page, but return a pfn. * @folio: The folio which contains this index. @@ -42,9 +125,19 @@ static int __kvm_gmem_prepare_folio(struct kvm *kvm, struct kvm_memory_slot *slo return 0; } -static inline void kvm_gmem_mark_prepared(struct folio *folio) + +static inline int kvm_gmem_finalize_folio(struct folio *folio) { + int r = kvm_gmem_folio_configure_direct_map(folio); + + /* + * Parts of the direct map might have been punched out, mark this folio + * as prepared even in the error case to avoid touching parts without + * direct map entries in a potential re-preparation. + */ folio_mark_uptodate(folio); + + return r; } /* @@ -82,11 +175,10 @@ static int kvm_gmem_prepare_folio(struct kvm *kvm, struct kvm_memory_slot *slot, index = ALIGN_DOWN(index, 1 << folio_order(folio)); r = __kvm_gmem_prepare_folio(kvm, slot, index, folio); if (!r) - kvm_gmem_mark_prepared(folio); + r = kvm_gmem_finalize_folio(folio); return r; } - /* * Returns a locked folio on success. The caller is responsible for * setting the up-to-date flag before the memory is mapped into the guest. @@ -249,6 +341,7 @@ static long kvm_gmem_fallocate(struct file *file, int mode, loff_t offset, static int kvm_gmem_release(struct inode *inode, struct file *file) { struct kvm_gmem *gmem = file->private_data; + struct kvm_gmem_inode_private *gmem_priv; struct kvm_memory_slot *slot; struct kvm *kvm = gmem->kvm; unsigned long index; @@ -279,13 +372,17 @@ static int kvm_gmem_release(struct inode *inode, struct file *file) list_del(&gmem->entry); + gmem_priv = inode->i_private; + filemap_invalidate_unlock(inode->i_mapping); mutex_unlock(&kvm->slots_lock); - xa_destroy(&gmem->bindings); kfree(gmem); + xa_destroy(&gmem_priv->direct_map_state); + kfree(gmem_priv); + kvm_put_kvm(kvm); return 0; @@ -357,24 +454,37 @@ static int kvm_gmem_error_folio(struct address_space *mapping, struct folio *fol return MF_DELAYED; } -#ifdef CONFIG_HAVE_KVM_ARCH_GMEM_INVALIDATE static void kvm_gmem_free_folio(struct folio *folio) { +#ifdef CONFIG_HAVE_KVM_ARCH_GMEM_INVALIDATE struct page *page = folio_page(folio, 0); kvm_pfn_t pfn = page_to_pfn(page); int order = folio_order(folio); kvm_arch_gmem_invalidate(pfn, pfn + (1ul << order)); -} #endif + if (folio_test_private(folio)) { + unsigned long start = (unsigned long)folio_address(folio); + + int r = set_direct_map_valid_noflush(folio_page(folio, 0), folio_nr_pages(folio), + true); + /* + * There might be holes left in the folio, better make sure + * nothing tries to touch it again. + */ + if (r) + folio_set_hwpoison(folio); + + flush_tlb_kernel_range(start, start + folio_size(folio)); + } +} + static const struct address_space_operations kvm_gmem_aops = { .dirty_folio = noop_dirty_folio, .migrate_folio = kvm_gmem_migrate_folio, .error_remove_folio = kvm_gmem_error_folio, -#ifdef CONFIG_HAVE_KVM_ARCH_GMEM_INVALIDATE .free_folio = kvm_gmem_free_folio, -#endif }; static int kvm_gmem_getattr(struct mnt_idmap *idmap, const struct path *path, @@ -401,6 +511,7 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) { const char *anon_name = "[kvm-gmem]"; struct kvm_gmem *gmem; + struct kvm_gmem_inode_private *gmem_priv; struct inode *inode; struct file *file; int fd, err; @@ -409,11 +520,14 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) if (fd < 0) return fd; + err = -ENOMEM; gmem = kzalloc(sizeof(*gmem), GFP_KERNEL); - if (!gmem) { - err = -ENOMEM; + if (!gmem) + goto err_fd; + + gmem_priv = kzalloc(sizeof(*gmem_priv), GFP_KERNEL); + if (!gmem_priv) goto err_fd; - } file = anon_inode_create_getfile(anon_name, &kvm_gmem_fops, gmem, O_RDWR, NULL); @@ -427,7 +541,7 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) inode = file->f_inode; WARN_ON(file->f_mapping != inode->i_mapping); - inode->i_private = (void *)(unsigned long)flags; + inode->i_private = gmem_priv; inode->i_op = &kvm_gmem_iops; inode->i_mapping->a_ops = &kvm_gmem_aops; inode->i_mode |= S_IFREG; @@ -442,6 +556,9 @@ static int __kvm_gmem_create(struct kvm *kvm, loff_t size, u64 flags) xa_init(&gmem->bindings); list_add(&gmem->entry, &inode->i_mapping->i_private_list); + xa_init(&gmem_priv->direct_map_state); + gmem_priv->flags = flags; + fd_install(fd, file); return fd; @@ -456,11 +573,14 @@ int kvm_gmem_create(struct kvm *kvm, struct kvm_create_guest_memfd *args) { loff_t size = args->size; u64 flags = args->flags; - u64 valid_flags = 0; + u64 valid_flags = KVM_GMEM_NO_DIRECT_MAP; if (flags & ~valid_flags) return -EINVAL; + if ((flags & KVM_GMEM_NO_DIRECT_MAP) && !can_set_direct_map()) + return -EOPNOTSUPP; + if (size <= 0 || !PAGE_ALIGNED(size)) return -EINVAL; @@ -679,7 +799,6 @@ long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src, long break; } - folio_unlock(folio); WARN_ON(!IS_ALIGNED(gfn, 1 << max_order) || (npages - i) < (1 << max_order)); @@ -695,7 +814,8 @@ long kvm_gmem_populate(struct kvm *kvm, gfn_t start_gfn, void __user *src, long p = src ? src + i * PAGE_SIZE : NULL; ret = post_populate(kvm, gfn, pfn, p, max_order, opaque); if (!ret) - kvm_gmem_mark_prepared(folio); + ret = kvm_gmem_finalize_folio(folio); + folio_unlock(folio); put_folio_and_exit: folio_put(folio); From patchwork Wed Oct 30 13:49:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Roy X-Patchwork-Id: 13856550 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C9DD0D5CCAF for ; Wed, 30 Oct 2024 13:50:25 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5831A6B00B5; Wed, 30 Oct 2024 09:50:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 50A146B00B8; Wed, 30 Oct 2024 09:50:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 384DA6B00B9; Wed, 30 Oct 2024 09:50:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 1762C6B00B5 for ; Wed, 30 Oct 2024 09:50:25 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id C5132A0C70 for ; Wed, 30 Oct 2024 13:50:24 +0000 (UTC) X-FDA: 82730401908.01.2861A4B Received: from smtp-fw-52005.amazon.com (smtp-fw-52005.amazon.com [52.119.213.156]) by imf10.hostedemail.com (Postfix) with ESMTP id 502F9C0026 for ; Wed, 30 Oct 2024 13:50:11 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b="cjFx8J/F"; spf=pass (imf10.hostedemail.com: domain of "prvs=02621381b=roypat@amazon.co.uk" designates 52.119.213.156 as permitted sender) smtp.mailfrom="prvs=02621381b=roypat@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.co.uk ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730296167; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=ynq07QFehgBeqYs8R/uKHAJgtPgDIcbxY2M616fmmig=; b=XaIJPdmHtthqp2oC3dimRaodgEwfxtVcr9BF/Xm7M7gl4f7FNgJBk80gHq+ZoS/Tc1E0hX KTd44IwVksO00aIIAZgShiomhsOhnAmCFcBCg3vZf+5WsNOUKPTvlZBragixzsQngp1gdN 0k6UCEEz1Fwbse3czzWJKxE3Z3IMLf4= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b="cjFx8J/F"; spf=pass (imf10.hostedemail.com: domain of "prvs=02621381b=roypat@amazon.co.uk" designates 52.119.213.156 as permitted sender) smtp.mailfrom="prvs=02621381b=roypat@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.co.uk ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730296167; a=rsa-sha256; cv=none; b=5lB1B6LffufzAdViNiVCqT9a2kncSLjI6yiQQMgCp0rl+5tEA9Dn/k/POZAiB3FKTaMtA0 M/UuTl6Xx5hYLQdjqHibu4gSXxLV9o6L5NsZzWycFQmlbOvl7Sh1JSoEJNelSaexwAEDV9 RsmWGq6o2lybEhiC+pVimUpXPxxcfKw= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazon201209; t=1730296223; x=1761832223; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=ynq07QFehgBeqYs8R/uKHAJgtPgDIcbxY2M616fmmig=; b=cjFx8J/FFsehujTeznsIjh0u/QkzY3eXYmAoz2Laeeqa/2sd2v6ogaQD yIjGVzzeFORwPCB8LfPwcx60BkDKch+AziTsDmsREJpqz64jteZS8RzC9 /YIIX7pFUVYH58ldinCGK92vIzMRq+DXy+S99kN4y0FKuG9tD+zPj2gBl I=; X-IronPort-AV: E=Sophos;i="6.11,245,1725321600"; d="scan'208";a="691697056" Received: from iad12-co-svc-p1-lb1-vlan3.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.43.8.6]) by smtp-border-fw-52005.iad7.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Oct 2024 13:50:17 +0000 Received: from EX19MTAUWA002.ant.amazon.com [10.0.38.20:54095] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.35.102:2525] with esmtp (Farcaster) id e1e6c02d-939e-42c5-ab8b-438e8ae0b6bc; Wed, 30 Oct 2024 13:50:16 +0000 (UTC) X-Farcaster-Flow-ID: e1e6c02d-939e-42c5-ab8b-438e8ae0b6bc Received: from EX19D020UWA002.ant.amazon.com (10.13.138.222) by EX19MTAUWA002.ant.amazon.com (10.250.64.202) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Wed, 30 Oct 2024 13:50:11 +0000 Received: from EX19MTAUWC001.ant.amazon.com (10.250.64.145) by EX19D020UWA002.ant.amazon.com (10.13.138.222) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Wed, 30 Oct 2024 13:50:11 +0000 Received: from email-imr-corp-prod-pdx-all-2c-8a67eb17.us-west-2.amazon.com (10.25.36.210) by mail-relay.amazon.com (10.250.64.145) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34 via Frontend Transport; Wed, 30 Oct 2024 13:50:11 +0000 Received: from ua2d7e1a6107c5b.home (dev-dsk-roypat-1c-dbe2a224.eu-west-1.amazon.com [172.19.88.180]) by email-imr-corp-prod-pdx-all-2c-8a67eb17.us-west-2.amazon.com (Postfix) with ESMTPS id E3BF841309; Wed, 30 Oct 2024 13:50:01 +0000 (UTC) From: Patrick Roy To: , , , , , , , , CC: Patrick Roy , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [RFC PATCH v3 3/6] kvm: gmem: implement direct map manipulation routines Date: Wed, 30 Oct 2024 13:49:07 +0000 Message-ID: <20241030134912.515725-4-roypat@amazon.co.uk> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241030134912.515725-1-roypat@amazon.co.uk> References: <20241030134912.515725-1-roypat@amazon.co.uk> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: o3i56e1wyphegz3z3m81pp7wuu6eepnp X-Rspamd-Queue-Id: 502F9C0026 X-Rspamd-Server: rspam11 X-HE-Tag: 1730296211-520263 X-HE-Meta: U2FsdGVkX1+bxot424zPOVTLvy4nqzt3YZxbh4ie2pDWScqUzunHQQMp69Fp2YhE032LIlvTlFs3/i3xM86RsZwr5xREvSqrkLKmF6DfvHAJVL5YZ6B+KhD0pIauIG529PMPpLyGaX26+ayax2zhq/IuXUaWVMNklodCYvGV8HJcmpMRNh+duqSvL0vb5aofJUd/g5cx4rHPdeKqo90bdNJwS+DGnQ661tHhsqamRbAuoludPs9At0EaymUwMn2iKvhuYzfQM/euAnjxlAddCom5BWUrzMQv8eqZ8nTVoOUqb1Yzl8Z9Mp+EwTtQkwH9ifzbphoLTeJM1Jf9AiM6ov8QY+iyTdsr1wSafEyaFyOh+f5mfhIblIL/PrYcpd+ZVfQ3pevpZtC71so7iniNKH+p8i7yniEuYdlo+2dEprYXE7TIDYbyYXmQQRlwtQr6BkX1C5ZVMw8+k3wDgyemXk7jJ0VDc+qECw0yGALo+OqRKa4ieMOE0r3wpIB1eumaSV7JQqQh5POdgqAW9GM91SOtWrR4I2n4MPAqItrM31DB+t9nR58IN5KuKG6wPQBL0U6VO4YJ82WZMlf/se/cy1JvZKhyK7j+o/GmYyzCjdNLjNa8eOjYx+PcT+Fs2Mha2655fqF7jTEy+wKp6USvognAIVBOLdeOufNoMjSQftNaDA1haYEBdISmuO3Z1nit6DuCvxj+3No5riBMsx39rd9mwS58jHwqlJh1JCiFZrws992BUTcf3RU/MN2Wboh8pvqe3q3957COlFzpelfhMO8OPUOvk8yeWoYFQqmWU8tpvGK3irMa37sGt2wTSkIZ+33WHITDhkf+HLLLvn9uUy4CUkFPDR7aC1YZX9GLUtmsJCZ7mlldnUh07XzLKMwrC871xa6CoydSrlrfQ35LFPbuj1BcRbCQMvkiAq/E+7dXqaT9uJqtPtBPGM3Bzmsx69XQAyiVwYS8IePY2A7 WPO3TrNy K8QMvcBjWEZmaIpB+ahOicOGSEkY8PkpLEEYCRf9IA+JvdYCeNhh1KMMXRUS6H/+ieXJE6Kv7V4NhIDeakFisKL99ctGHCYlsO+oGekla3S7keIRJWPDHTyIUcXZH7MxAIWc2lDf3PxgT3jHnXalYX7DrpQsW0s1UkbFGvYWpRePB+46ba2mvh3XroqHBK7EZ/Hv6SR0oUvuOJIBkrrRuTyGyKHMCZDz5m7aaw+fOrIfoCP8/Mk+pkOSJLaoVFKVgijEfjR/8HRQaIcSw8l5K3L/GKx1GzgGIuv585NNtWVfvbowKNzjlpNVZm3bBUCJc8qzQFDcLQJWw+axGV3sHO0LjupzN2NgbyBBTwdUmMHnjKgoRUIBQaK7dfF9O6mpQ4bnN8pF4t5bN1gOcAHeq7UF0b4tEIgcQOgPAjYyXOECeinnD24N9fALAcg== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Implement (yet unused) routines for manipulating guest_memfd direct map state. This is largely for illustration purposes. kvm_gmem_set_direct_map allows manipulating arbitrary pgoff_t ranges, even if the covered memory has not yet been faulted in (in which case the requested direct map state is recorded in the xarray and will be applied by kvm_gmem_folio_configure_direct_map after the folio is faulted in and prepared/populated). This can be used to realize private/shared conversions on not-yet-faulted in memory, as discussed in the guest_memfd upstream call [1]. kvm_gmem_folio_set_direct_map allows manipulating the direct map entries for a gmem folio that the caller already holds a reference for (whereas kvm_gmem_set_direct_map needs to look up all folios intersecting the given pgoff range in the filemap first). The xa lock serializes calls to kvm_gmem_folio_set_direct_map and kvm_gmem_set_direct_map, while the read side (kvm_gmem_folio_configure_direct_map) is protected by RCU. This is sufficient to ensure consistency between the xarray and the folio's actual direct map state, as kvm_gmem_folio_configure_direct_map is called only for freshly allocated folios, and before the folio lock is dropped for the first time, meaning kvm_gmem_folio_configure_direct_map always does it's set_direct_map calls before either of kvm_gmem_[folio_]set_direct_map get a chance. Even if a concurrent call to kvm_gmem_[folio_]set_direct_map happens, this ensures a sort of "eventual consistency" between xarray and actual direct map configuration by the time kvm_gmem_[folio_]set_direct_map exits. [1]: https://lore.kernel.org/kvm/4b49248b-1cf1-44dc-9b50-ee551e1671ac@redhat.com/ Signed-off-by: Patrick Roy --- virt/kvm/guest_memfd.c | 125 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 125 insertions(+) diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 50ffc2ad73eda..54387828dcc6a 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -96,6 +96,131 @@ static int kvm_gmem_folio_configure_direct_map(struct folio *folio) return r; } +/* + * Updates the range [@start, @end] in @gmem_priv's direct map state xarray to be @state, + * e.g. erasing entries in this range if @state is the default state, and creating + * entries otherwise. + * + * Assumes the xa_lock is held. + */ +static int __kvm_gmem_update_xarray(struct kvm_gmem_inode_private *gmem_priv, pgoff_t start, + pgoff_t end, bool state) +{ + struct xarray *xa = &gmem_priv->direct_map_state; + int r = 0; + + /* + * Cannot use xa_store_range, as multi-indexes cannot easily + * be partially updated. + */ + for (pgoff_t index = start; index < end; ++index) { + if (state == gmem_priv->default_direct_map_state) + __xa_erase(xa, index); + else + /* don't care _what_ we store in the xarray, only care about presence */ + __xa_store(xa, index, gmem_priv, GFP_KERNEL); + + r = xa_err(xa); + if (r) + goto out; + } + +out: + return r; +} + +static int __kvm_gmem_folio_set_direct_map(struct folio *folio, pgoff_t start, pgoff_t end, + bool state) +{ + unsigned long npages = end - start + 1; + struct page *first_page = folio_file_page(folio, start); + + int r = set_direct_map_valid_noflush(first_page, npages, state); + + flush_tlb_kernel_range((unsigned long)page_address(first_page), + (unsigned long)page_address(first_page) + + npages * PAGE_SIZE); + return r; +} + +/* + * Updates the direct map status for the given range from @start to @end (inclusive), returning + * -EINVAL if this range is not completely contained within @folio. Also updates the + * xarray stored in the private data of the inode @folio is attached to. + * + * Takes and drops the folio lock. + */ +static __always_unused int kvm_gmem_folio_set_direct_map(struct folio *folio, pgoff_t start, + pgoff_t end, bool state) +{ + struct inode *inode = folio_inode(folio); + struct kvm_gmem_inode_private *gmem_priv = inode->i_private; + int r = -EINVAL; + + if (!folio_contains(folio, start) || !folio_contains(folio, end)) + goto out; + + xa_lock(&gmem_priv->direct_map_state); + r = __kvm_gmem_update_xarray(gmem_priv, start, end, state); + if (r) + goto unlock_xa; + + folio_lock(folio); + r = __kvm_gmem_folio_set_direct_map(folio, start, end, state); + folio_unlock(folio); + +unlock_xa: + xa_unlock(&gmem_priv->direct_map_state); +out: + return r; +} + +/* + * Updates the direct map status for the given range from @start to @end (inclusive) + * of @inode. Folios in this range have their direct map entries reconfigured, + * and the xarray in the @inode's private data is updated. + */ +static __always_unused int kvm_gmem_set_direct_map(struct inode *inode, pgoff_t start, + pgoff_t end, bool state) +{ + struct kvm_gmem_inode_private *gmem_priv = inode->i_private; + struct folio_batch fbatch; + pgoff_t index = start; + unsigned int count, i; + int r = 0; + + xa_lock(&gmem_priv->direct_map_state); + + r = __kvm_gmem_update_xarray(gmem_priv, start, end, state); + if (r) + goto out; + + folio_batch_init(&fbatch); + while (!filemap_get_folios(inode->i_mapping, &index, end, &fbatch) && !r) { + count = folio_batch_count(&fbatch); + for (i = 0; i < count; i++) { + struct folio *folio = fbatch.folios[i]; + pgoff_t folio_start = max(folio_index(folio), start); + pgoff_t folio_end = + min(folio_index(folio) + folio_nr_pages(folio), + end); + + folio_lock(folio); + r = __kvm_gmem_folio_set_direct_map(folio, folio_start, + folio_end, state); + folio_unlock(folio); + + if (r) + break; + } + folio_batch_release(&fbatch); + } + + xa_unlock(&gmem_priv->direct_map_state); +out: + return r; +} + /** * folio_file_pfn - like folio_file_page, but return a pfn. * @folio: The folio which contains this index. From patchwork Wed Oct 30 13:49:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Roy X-Patchwork-Id: 13856551 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 53439D5CCB0 for ; Wed, 30 Oct 2024 13:50:31 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D705D6B00BB; Wed, 30 Oct 2024 09:50:30 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D20196B00BD; Wed, 30 Oct 2024 09:50:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B9B2B6B00BF; Wed, 30 Oct 2024 09:50:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 9280A6B00BB for ; Wed, 30 Oct 2024 09:50:30 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 4636680C7C for ; Wed, 30 Oct 2024 13:50:30 +0000 (UTC) X-FDA: 82730402328.27.81F4CC1 Received: from smtp-fw-80009.amazon.com (smtp-fw-80009.amazon.com [99.78.197.220]) by imf15.hostedemail.com (Postfix) with ESMTP id D7C4DA0029 for ; Wed, 30 Oct 2024 13:50:01 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=rmbuwUw8; dmarc=pass (policy=quarantine) header.from=amazon.co.uk; spf=pass (imf15.hostedemail.com: domain of "prvs=02621381b=roypat@amazon.co.uk" designates 99.78.197.220 as permitted sender) smtp.mailfrom="prvs=02621381b=roypat@amazon.co.uk" ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730296110; a=rsa-sha256; cv=none; b=cLH4EV66Or6db9tb5j8ge4hvIphjCTXIaxJ3aMtIGx54Jj0CupxVNiFuEJ+JZZxeKHq3x0 O81OHCBK8BSbBpA39QkneGr5g14q72VoDatUQDJON2yn6y9rfK2mL6o2bC9ahQ9FqipQ7J JFlYglMmhTtQiLyBv9pOgn5zVfSQ0wg= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=rmbuwUw8; dmarc=pass (policy=quarantine) header.from=amazon.co.uk; spf=pass (imf15.hostedemail.com: domain of "prvs=02621381b=roypat@amazon.co.uk" designates 99.78.197.220 as permitted sender) smtp.mailfrom="prvs=02621381b=roypat@amazon.co.uk" ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730296110; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=k/QSX0mI8qQz8W316WTJxbTS4bB3Lyue/iJHbqNfB18=; b=pJ8wA6WglSv44ATKR0zL+X3F9TKTL4XjK82qDxJbZkvfwZLlyjwv688EsSs3hy41lPs1Ku nWyDS4yHPCuWwP3TudKUb9MGAe91Ttrk9ih+3429Mjo5QCyCpZqPFtPcV9yquiv+hDKszS FJkDWPCIwmStDuF/S5ASSjUW1oPKTX0= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazon201209; t=1730296228; x=1761832228; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=k/QSX0mI8qQz8W316WTJxbTS4bB3Lyue/iJHbqNfB18=; b=rmbuwUw82tQVMx1tP9SL+XLdoCW7LITdBVr6bGCC6D3x6WOwfR1GG8XH BUitWi6b93Ncwn5j9PHOiDteY1I78t9d9FnpweaURY+XMIwPZXwCfnkEz RxsuAO3wUVWHDcvMvN/fepMjWPBWxiLcBA9xID+arSkBh7BEtGWsYfSAJ o=; X-IronPort-AV: E=Sophos;i="6.11,245,1725321600"; d="scan'208";a="142980689" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.210]) by smtp-border-fw-80009.pdx80.corp.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Oct 2024 13:50:27 +0000 Received: from EX19MTAEUB001.ant.amazon.com [10.0.43.254:32764] by smtpin.naws.eu-west-1.prod.farcaster.email.amazon.dev [10.0.20.52:2525] with esmtp (Farcaster) id cb6221e8-e82a-4b95-aff9-f29080c6cf67; Wed, 30 Oct 2024 13:50:26 +0000 (UTC) X-Farcaster-Flow-ID: cb6221e8-e82a-4b95-aff9-f29080c6cf67 Received: from EX19D014EUA004.ant.amazon.com (10.252.50.41) 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.1258.34; Wed, 30 Oct 2024 13:50:23 +0000 Received: from EX19MTAUEA002.ant.amazon.com (10.252.134.9) by EX19D014EUA004.ant.amazon.com (10.252.50.41) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Wed, 30 Oct 2024 13:50:23 +0000 Received: from email-imr-corp-prod-pdx-all-2c-8a67eb17.us-west-2.amazon.com (10.43.8.2) by mail-relay.amazon.com (10.252.134.34) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34 via Frontend Transport; Wed, 30 Oct 2024 13:50:22 +0000 Received: from ua2d7e1a6107c5b.home (dev-dsk-roypat-1c-dbe2a224.eu-west-1.amazon.com [172.19.88.180]) by email-imr-corp-prod-pdx-all-2c-8a67eb17.us-west-2.amazon.com (Postfix) with ESMTPS id 93E2B41303; Wed, 30 Oct 2024 13:50:13 +0000 (UTC) From: Patrick Roy To: , , , , , , , , CC: Patrick Roy , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [RFC PATCH v3 4/6] kvm: gmem: add trace point for direct map state changes Date: Wed, 30 Oct 2024 13:49:08 +0000 Message-ID: <20241030134912.515725-5-roypat@amazon.co.uk> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241030134912.515725-1-roypat@amazon.co.uk> References: <20241030134912.515725-1-roypat@amazon.co.uk> MIME-Version: 1.0 X-Rspamd-Queue-Id: D7C4DA0029 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: gepd5iehxjyoft3rqrrpqonzdkwmybum X-HE-Tag: 1730296201-449342 X-HE-Meta: U2FsdGVkX18zTCCSwt7sTJREK637v3lNcTQOIAvNfBn+FUWt7mL7TozUHc59rcB2wdrepwxsshCyf+O77xWFq+G3NAQmCeEPBbe9hJucqcOWS9RjMXz9M+PTPG/uepOsbm1nAkXMJRux+bI56YMUArSb/2j4MeqAce5+/FrULvdl+iaOlhk4DfO7TjOgiUvuvK/XGanlD8DS4ullIXvI0vtHJ1l8VidNSkLDcldDjDLP2lY2C+NxELD1INH1xX1t1o6qsdqLtDom3XyS+5cgtVzZ5wxzVUjVEf4aIVU2ZPOQt209f1sW138VWaW8u6uRu5dZALeCc/eUD+mVUkS9JR7exGbikWJD03tR9HemVKd5V07ZjvhcT9Sqwsxw3Y5+YO/hQPO+So6lqWSruLdm1B9RE0EkpYbvJCvaU5o3gYqFp3GSxe9JwbXufrn/MiaIG3Lu8CC4qko8CqzGXwlV+QI0hlD940vgsx3wBx582sWYbnQhaaLmdo33kgIH7EQcCsLdmOm5KmrnNEl0JdVHvadc6soENfeejpCs0PQs/X96IMCt0Vuu9/7sMPOOYm5+0ts0U2mRCRn/FS0q0HDRxGnl95lm6ptiPDTE+2RSDSw4TMbcKnZSDbag8y9cCrI5WysZNW99xRr/waKzZrFldHiZ76HCujVdy6ygqC3z6mBqKvE8eMpi/lAvJucVZqMIlE4wK6o8fffrcjtTvtpTLYtK2iFBEnj4NWmxUnxUdsAIiDBAXd8hOV3yaSXtEAXlUgrjO09MHZO+2cUOjaKNWpf9QwfVe9raAJbVTK4Fgbi29Lzj0VMoJQfBxPBU7dZgdXUsBkhY3BwThZrmHizt5Jdy96nZJSQKcn3N+9rqiqqn7WQQ+KCJFrzrWlZZyxjKvOeYKssTMzxGBVjUoFdzUkPbuy3igONIqjn0g2ERWQxviWQ7cSx43bhPs6JKkM3J4/5sKHXnbUoaJmPg3rq iXPmsnEK hafx2oN39iR7vXJ6lOBXqruQl6T9qMMhYg95kAQBVYkS/ja0wYUIHsrsJzHIVamY9gUl6Tgn44Tjfu0Khj06Izv1xKcSsn1bSYK6ehHs+rlMmsWY5UHQp3/dV8IkQ3G7xudL97X56MLzuoTuI0jZURtbi36At4cRWHkz4Y/issU8ZOaapgu/+81xRR7bDsilVAbewSlUAPBssILtDU/Tj2H0KsRTbbFhYlgbosROLwaUunGvkj2yTeP09E5J6PEEer+fEJ4+R2HwOCqW98Nbqd0cQ8heGYg7y710hjnQQwVvmQ+yA9+UAsACBBxZbYMLxgBrJyzYoJNnRDsf7jLjecgdJPkbOTgRkzFzhLMeFvZjupMLemxcRCvUoHVC0l7uYR8tGn6i9r51gF6o= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Add tracepoints to kvm_gmem_set_direct_map and kvm_gmem_folio_set_direct_map. The above operations can cause folios to be insert/removed into/from the direct map. We want to be able to make sure that only those gmem folios that we expect KVM to access are ever reinserted into the direct map, and that all folios that are temporarily reinserted are also removed again at a later point. Processing ftrace output is one way to verify this. Signed-off-by: Patrick Roy --- include/trace/events/kvm.h | 22 ++++++++++++++++++++++ virt/kvm/guest_memfd.c | 5 +++++ 2 files changed, 27 insertions(+) diff --git a/include/trace/events/kvm.h b/include/trace/events/kvm.h index 74e40d5d4af42..f3d852c18fa08 100644 --- a/include/trace/events/kvm.h +++ b/include/trace/events/kvm.h @@ -489,6 +489,28 @@ TRACE_EVENT(kvm_test_age_hva, TP_printk("mmu notifier test age hva: %#016lx", __entry->hva) ); +#ifdef CONFIG_KVM_PRIVATE_MEM +TRACE_EVENT(kvm_gmem_direct_map_state_change, + TP_PROTO(pgoff_t start, pgoff_t end, bool state), + TP_ARGS(start, end, state), + + TP_STRUCT__entry( + __field(pgoff_t, start) + __field(pgoff_t, end) + __field(bool, state) + ), + + TP_fast_assign( + __entry->start = start; + __entry->end = end; + __entry->state = state; + ), + + TP_printk("changed direct map state of guest_memfd range %lu to %lu to %s", + __entry->start, __entry->end, __entry->state ? "present" : "not present") +); +#endif + #endif /* _TRACE_KVM_MAIN_H */ /* This part must be outside protection */ diff --git a/virt/kvm/guest_memfd.c b/virt/kvm/guest_memfd.c index 54387828dcc6a..a0b3b9cacd361 100644 --- a/virt/kvm/guest_memfd.c +++ b/virt/kvm/guest_memfd.c @@ -7,6 +7,7 @@ #include #include "kvm_mm.h" +#include "trace/events/kvm.h" struct kvm_gmem { struct kvm *kvm; @@ -169,6 +170,8 @@ static __always_unused int kvm_gmem_folio_set_direct_map(struct folio *folio, pg r = __kvm_gmem_folio_set_direct_map(folio, start, end, state); folio_unlock(folio); + trace_kvm_gmem_direct_map_state_change(start, end, state); + unlock_xa: xa_unlock(&gmem_priv->direct_map_state); out: @@ -216,6 +219,8 @@ static __always_unused int kvm_gmem_set_direct_map(struct inode *inode, pgoff_t folio_batch_release(&fbatch); } + trace_kvm_gmem_direct_map_state_change(start, end, state); + xa_unlock(&gmem_priv->direct_map_state); out: return r; From patchwork Wed Oct 30 13:49:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Roy X-Patchwork-Id: 13856552 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6B8DED5CCB0 for ; Wed, 30 Oct 2024 13:50:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E0EE56B00BF; Wed, 30 Oct 2024 09:50:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D95EF6B00C1; Wed, 30 Oct 2024 09:50:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C0F3F6B00C3; Wed, 30 Oct 2024 09:50:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 9C6156B00BF for ; Wed, 30 Oct 2024 09:50:41 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 511301A0D6E for ; Wed, 30 Oct 2024 13:50:41 +0000 (UTC) X-FDA: 82730403252.15.6F5F5E9 Received: from smtp-fw-33001.amazon.com (smtp-fw-33001.amazon.com [207.171.190.10]) by imf09.hostedemail.com (Postfix) with ESMTP id 2CE47140017 for ; Wed, 30 Oct 2024 13:50:20 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b="JEO3SjG/"; spf=pass (imf09.hostedemail.com: domain of "prvs=02621381b=roypat@amazon.co.uk" designates 207.171.190.10 as permitted sender) smtp.mailfrom="prvs=02621381b=roypat@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.co.uk ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730296184; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=4gTnpkauDGYjzwRVywsrh01v5XrS76J2GZbKVdbAfi8=; b=btotkSjmX15YICsSmv6q4axpPKnR7FS4Lbxtl72G2mGj+lL2wD7XSlj+2Zua2YJYjbB8aJ 8Datwr8TF9smJ584BiHHbDpqGSw787IOWhVSxO4eprXNMqG1GIw0DxIzg2qQiZcQ8bkzjV 2yJJDjcQ8FTActkhAZEa5+GOai0M8dU= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b="JEO3SjG/"; spf=pass (imf09.hostedemail.com: domain of "prvs=02621381b=roypat@amazon.co.uk" designates 207.171.190.10 as permitted sender) smtp.mailfrom="prvs=02621381b=roypat@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.co.uk ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730296184; a=rsa-sha256; cv=none; b=hrrjyKaDWU/TNblyllzhYZpxJIL9IpBqRAYffblPtc31F4KGiKhTkx9S5zd9tGb87kgIg9 XgfBAkcWxzsWg6Zk979FB9OyUiN6q2izPosampNQWSp1D+gwUsybKBaSZRddMAFiYX39TP rWDty3FvCoZvtMRZS4IQzd6uR+KK1Wk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazon201209; t=1730296240; x=1761832240; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=4gTnpkauDGYjzwRVywsrh01v5XrS76J2GZbKVdbAfi8=; b=JEO3SjG/x5RKrRDpPfRzP0BnQbGAxEjMc9E83W2ydOFrpM+rp+JpwMAT b0YnqgQEddG0VWrCRBTYrtTkIACoGTjglygDYuSo2GfzHoZDxQ0eGB5nm sMnY1LVS0OcdTyjkqXQsOaS3Oh8i8yG6bzHLaGZwObLoGkyldBlE69Hrl U=; X-IronPort-AV: E=Sophos;i="6.11,245,1725321600"; d="scan'208";a="381122811" Received: from pdx4-co-svc-p1-lb2-vlan2.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.210]) by smtp-border-fw-33001.sea14.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Oct 2024 13:50:38 +0000 Received: from EX19MTAUWB002.ant.amazon.com [10.0.21.151:4737] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.24.36:2525] with esmtp (Farcaster) id 6a75726f-118b-4ebb-b916-ac128103b7c2; Wed, 30 Oct 2024 13:50:37 +0000 (UTC) X-Farcaster-Flow-ID: 6a75726f-118b-4ebb-b916-ac128103b7c2 Received: from EX19D003UWB001.ant.amazon.com (10.13.138.92) by EX19MTAUWB002.ant.amazon.com (10.250.64.231) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Wed, 30 Oct 2024 13:50:34 +0000 Received: from EX19MTAUWB001.ant.amazon.com (10.250.64.248) by EX19D003UWB001.ant.amazon.com (10.13.138.92) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.35; Wed, 30 Oct 2024 13:50:34 +0000 Received: from email-imr-corp-prod-pdx-all-2c-8a67eb17.us-west-2.amazon.com (10.25.36.214) by mail-relay.amazon.com (10.250.64.254) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34 via Frontend Transport; Wed, 30 Oct 2024 13:50:34 +0000 Received: from ua2d7e1a6107c5b.home (dev-dsk-roypat-1c-dbe2a224.eu-west-1.amazon.com [172.19.88.180]) by email-imr-corp-prod-pdx-all-2c-8a67eb17.us-west-2.amazon.com (Postfix) with ESMTPS id AD78B4032D; Wed, 30 Oct 2024 13:50:24 +0000 (UTC) From: Patrick Roy To: , , , , , , , , CC: Patrick Roy , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [RFC PATCH v3 5/6] kvm: document KVM_GMEM_NO_DIRECT_MAP flag Date: Wed, 30 Oct 2024 13:49:09 +0000 Message-ID: <20241030134912.515725-6-roypat@amazon.co.uk> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241030134912.515725-1-roypat@amazon.co.uk> References: <20241030134912.515725-1-roypat@amazon.co.uk> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: irzk8861qga45eiet3gttqqc1hmm197p X-Rspamd-Queue-Id: 2CE47140017 X-Rspamd-Server: rspam11 X-HE-Tag: 1730296220-719021 X-HE-Meta: U2FsdGVkX18sWfb2y8eHnqfqajyVzjYNu0ecU/7oIzz/Ht1lEsW28AEuFcqMjDLZBJzDR4fB6eLgGaWnqhhG9LRIO6HlBXGrsdEDmVadcMRYK8ZrBwqFCM1T/2ixYlBn7UItYYkLnPi6JajaHqc3C4udLXvf9PKXZ3UflspdL8b+HV9qGCRDCCAKKoxPWfzXcunyh0Epek3FuCRDuj3Btesw9fJGQd+QPqdONPBs675j4DOvElxxczWdNnHT4Cn+2tjGpifBkx8001MBQOQqF/LHQsUsdZ0DG0TZhFrVIgPXtZsjPqBbiE5SAlox/YxojZp4sXMXu4GDKaBwMCR0Vc9vRXHGFcXt7Ppmqwm88OruakUjOBEkzXwmiT4Ai4PN4HvRE8zID/MBZ0RzMrZoEC/VplkfU8OsWHNzI1tGQLt0VJBjwhK4NESrY1L0LpQq9otgifGJAPpcFnv2aHSIN34R9eBG3GpPE0qyu0AoE0rpybysDyScaVsbZNEvjL/Zic+9tuAw3WTPmFmt3t9EOKxCpo0Fp2xKbzjWHZVe2xWRrfYzK79ObZuiGe4OEB4elj940SMHTDuP1M6Cyiff2a2/FCjd8GiN+mnjoKfHuFIRoPIZgyxTfd0yUTACD0xr1EU1hF7TBvn5mQ/14ce+mdGhZ1qXo46Q4y/RaiaCINnO+BW8eXFSo9Dg4reXqkSwJQ7LyBf109COgMsdm0Fo45WK0gnyI3J9UQmlPv7yzPhGm3vNjEr2E+FiocJj0fcqIXhqQEjvZ2Oo47x+iZDGj7avOYfuwqorOCZ2Z2MSnsOfPXb7pHgUuel+eKavyB7ql9v4VBV/xd3bMWkap4VvX8d2fVzMdIBovtLzFVy3uBBAyeu1UuC6ckfgKjqJDnc31flO1naNwWfPMfMn3ratGFkE32Dkpd5flfR0OT0aXw3jrei8tBSiDVqwFNMD1nn6BXetkqEeGv6b2l8739K nuPLmyly 24Ck2Y4rDML015w/JdyM7b+Sj5pQvYtsnUTebRRdU96Xus9QATc3vkAf0RVjkySTncsKDMyDKOeLClyk6Q2G5OzahQEFVg7N6bSEMwrKJhiYaq3aSAxlsYPTvRf0dirkoCJ6rK1UNAIdkHxhGfw5wTe09Lnu8JUgLGLj36Dc2HeiVXWJhzrG8A3ShUzRMw4mthgXfOYRYOiTZyIYiImFn0p15RA89DBSAZvY3dvZN59jMtEqayP/1scUTf8hqs6xshZs5DxzDZe1zRjm2+BGkFVLnl+0PRUQdrSWeXT/KU4ilRR6FD77+k4nCqJXeG7SeKeFobZSvXzSH7UlPt+lmiw4AAsV0KAkfKGXVUMYOJrUsIwIvtbxLhBAWkYGhUBG0ucCnqVqUFqXnkRSGxkgiWi3kOblfHtTIaHCbOgOR9aoxswcLTtaqf+HyiA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000114, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Signed-off-by: Patrick Roy --- Documentation/virt/kvm/api.rst | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Documentation/virt/kvm/api.rst b/Documentation/virt/kvm/api.rst index edc070c6e19b2..c8e21c523411c 100644 --- a/Documentation/virt/kvm/api.rst +++ b/Documentation/virt/kvm/api.rst @@ -6382,6 +6382,20 @@ a single guest_memfd file, but the bound ranges must not overlap). See KVM_SET_USER_MEMORY_REGION2 for additional details. +The following flags are defined: + +KVM_GMEM_NO_DIRECT_MAP + Ensure memory backing this guest_memfd inode is unmapped from the kernel's + address space. + +Errors: + + ========== =============================================================== + EOPNOTSUPP `KVM_GMEM_NO_DIRECT_MAP` was set in `flags`, but the host does + not support direct map manipulations. + ========== =============================================================== + + 4.143 KVM_PRE_FAULT_MEMORY --------------------------- From patchwork Wed Oct 30 13:49:10 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Roy X-Patchwork-Id: 13856553 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2F26ED5CCAF for ; Wed, 30 Oct 2024 13:50:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B284D6B00C3; Wed, 30 Oct 2024 09:50:58 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AB0C26B00C4; Wed, 30 Oct 2024 09:50:58 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 901846B00C5; Wed, 30 Oct 2024 09:50:58 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 70E056B00C3 for ; Wed, 30 Oct 2024 09:50:58 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id BCE69ACB55 for ; Wed, 30 Oct 2024 13:50:57 +0000 (UTC) X-FDA: 82730404050.16.35C6A04 Received: from smtp-fw-9102.amazon.com (smtp-fw-9102.amazon.com [207.171.184.29]) by imf03.hostedemail.com (Postfix) with ESMTP id 79E2420022 for ; Wed, 30 Oct 2024 13:50:43 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=SgkMt6J9; spf=pass (imf03.hostedemail.com: domain of "prvs=02621381b=roypat@amazon.co.uk" designates 207.171.184.29 as permitted sender) smtp.mailfrom="prvs=02621381b=roypat@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.co.uk ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1730296042; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=7/qXvSh0Hjany10m5XTRwJaHsD/IFz61ERHpnmidOYs=; b=vgECuLV2EG62n+d0y1nTknenVO56VGiCNkW4CbXjj4Q01P2DY8vzXjwj40ks32osOJK5P7 EF6rTvyk2+FP+hFjAR8Kb6f5ulbEWGnNYT7N9ch/lYhypKOsgDbGN+ZKQheXZxvzBXhPA/ b6Jb8llBz81DVg0TKg1o9mKWTF7zF8I= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=amazon.co.uk header.s=amazon201209 header.b=SgkMt6J9; spf=pass (imf03.hostedemail.com: domain of "prvs=02621381b=roypat@amazon.co.uk" designates 207.171.184.29 as permitted sender) smtp.mailfrom="prvs=02621381b=roypat@amazon.co.uk"; dmarc=pass (policy=quarantine) header.from=amazon.co.uk ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730296042; a=rsa-sha256; cv=none; b=Ip+1/ONyRnpcHA2lWnQ78PDlVsvJDohh6dE9SjXMVsPUm0ES0sXwL61LdBBoK9pYtfaOK+ GBqY2vsKOfMdy6l87uhNB2dazBBZJzbcDH6t6cUKG3NjjWPTbEmy5/7ichn+4RyAtjp8Lu 9zHgYtqNHrhkR8KWXqjAw9gljqiuRKM= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.co.uk; i=@amazon.co.uk; q=dns/txt; s=amazon201209; t=1730296256; x=1761832256; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=7/qXvSh0Hjany10m5XTRwJaHsD/IFz61ERHpnmidOYs=; b=SgkMt6J9HN8iWG2LiVkJbpRYGXJj2T5vudnopPfPxO6nQwuibQIZnVaE BePjy8WnN6Bxq8cIgZ1Lnyw66abb5pWrBHZ3+PBSxn5tsZH+6wtfUbBIj vckF7LDmKOx0NlQY3LDtN7/QphgzD9dVJZfmoI4qyuoTwWgXKCQ8tz5cd A=; X-IronPort-AV: E=Sophos;i="6.11,245,1725321600"; d="scan'208";a="465820264" Received: from pdx4-co-svc-p1-lb2-vlan3.amazon.com (HELO smtpout.prod.us-west-2.prod.farcaster.email.amazon.dev) ([10.25.36.214]) by smtp-border-fw-9102.sea19.amazon.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Oct 2024 13:50:54 +0000 Received: from EX19MTAUWB002.ant.amazon.com [10.0.21.151:12628] by smtpin.naws.us-west-2.prod.farcaster.email.amazon.dev [10.0.35.102:2525] with esmtp (Farcaster) id 00b2c9f1-27f5-4a01-ae54-6fda17405dbb; Wed, 30 Oct 2024 13:50:53 +0000 (UTC) X-Farcaster-Flow-ID: 00b2c9f1-27f5-4a01-ae54-6fda17405dbb Received: from EX19D020UWC002.ant.amazon.com (10.13.138.147) by EX19MTAUWB002.ant.amazon.com (10.250.64.231) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Wed, 30 Oct 2024 13:50:46 +0000 Received: from EX19MTAUEB002.ant.amazon.com (10.252.135.47) by EX19D020UWC002.ant.amazon.com (10.13.138.147) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34; Wed, 30 Oct 2024 13:50:45 +0000 Received: from email-imr-corp-prod-pdx-all-2c-8a67eb17.us-west-2.amazon.com (10.43.8.2) by mail-relay.amazon.com (10.252.135.97) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA) id 15.2.1258.34 via Frontend Transport; Wed, 30 Oct 2024 13:50:45 +0000 Received: from ua2d7e1a6107c5b.home (dev-dsk-roypat-1c-dbe2a224.eu-west-1.amazon.com [172.19.88.180]) by email-imr-corp-prod-pdx-all-2c-8a67eb17.us-west-2.amazon.com (Postfix) with ESMTPS id B5B724032D; Wed, 30 Oct 2024 13:50:35 +0000 (UTC) From: Patrick Roy To: , , , , , , , , CC: Patrick Roy , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , Subject: [RFC PATCH v3 6/6] kvm: selftests: run gmem tests with KVM_GMEM_NO_DIRECT_MAP set Date: Wed, 30 Oct 2024 13:49:10 +0000 Message-ID: <20241030134912.515725-7-roypat@amazon.co.uk> X-Mailer: git-send-email 2.47.0 In-Reply-To: <20241030134912.515725-1-roypat@amazon.co.uk> References: <20241030134912.515725-1-roypat@amazon.co.uk> MIME-Version: 1.0 X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 79E2420022 X-Stat-Signature: 1ccd8mhedk8i3wmfkmeg45qikex8mq3e X-Rspam-User: X-HE-Tag: 1730296243-273635 X-HE-Meta: U2FsdGVkX1/14vU12xIgzcPL9dUNcLRmiGONApETNc1ZQgu7Xcx868yVmOC74osOUKH3LSVZDmqs8HjIRtE+UsJ+7qz+qWLa1iTRFSKLEfBfpVVP629cwERZj+HfeELPD7JVIGyIefCW3pqnQRk9+Vxe+6B72LJpGr7qYXoqTAgv3EmxPWZyEWLYIDJ2eMiJQQlIvkqVGVn+8xUPzKiZp1UyLZeYUDQjBXKooLDTLgQNQb3yKPU2j4qhOy6KHPFAk1tvOSetbE2vfPczkJ1E3RRdGVjliVpJMSfAFApG6cjBj7sdACh6wy98A0wLpR4kbj7ebi6cwy7MhlNZEKVnRAqP1gkE9kr4ZDyagcpDiJomwXpREM9lxQv81upW6mfNDPAz3h3zk4JTwA3M1+b4W7urCEoIyam5UlfAgmAsU5FujLfas/n4r4CYi/b4gvvgeb9VkfPDVe+ayeRKSqpdN8IipMD/MccMkvERCWj54GmxOzN2ls77mHgOO36lxMUCHSZiz1BJE7Bzjj8fxlRYc/7ufvRWIftBP6/4sxcZEKGyf2r0Lxf5rL4Zyhp5taRLflIX/WK9BIK1x8UqIL/uWFI+EODTgdQOluybJ7ShIbCT2qTwISL+PjcbBB1+61YbW8L32hbbKhPVKBbYlbRQ6hujDSByfRfiArhjt9UUH/bgheODRQP0PgMWN0ICxLx4iUJfPArag/dNp4GR1443VydwPhGsQdURq84vXJC2SZFhbVV/8e7FdOIbJBiATfaivzRKnlN0g3aw2yg02NkpPGXqv88iZ8IdZJFNsSx3DyeH/QS2i6hpWlEsqQmAsRJaCC3dbKRC1N9vudtpzUsJDU7Ed/XJoYN1zV6nS5FgiAqBnuGkHK5aKSbXXiz371i23B98lHqts7Mrx6FhqPgBfX62+qIJxIn+WFnwZFM+Ev1AQg/+7BZzWSauAkDcBuuvF0uWyxhViEGIZk01GH4 3jTZbeo8 ryRsU9ZZj5O/rzimyIkS8a81HehVViZiWpZ9Zg745rKEkP9CgMfW46tPoblAYpP63GaaVtV+/5pXoyFESl+NQ4HfHOFsvXRQLXnxCM0dWvqUwXNY1/m5VCf2dLQzVDdxS1bazKZ2n0PGKfv26E953NaJ1TKv39vpSVgWJrzX6t7ukb8ahWRKRRJDtExaM7AuWwv5etEC3LsO5DBNltBPWHacEIvo0GKjmIrJ0V+bXimBzAiOzzRo48a1Vk3mfbWu0jefnwTsYiPd4yYwcd5SryLCh5DMflKm3SyShjzA6Hf28kOybJk6dXeFK6SSQOkOWt51J93vbKPbVNNye+AmCHupQrF4GxghsD8pI6aTQELOMKPSYhElkLT8nZdjomS6O0H57XUOVpVI0oBR8Get67u8/9cQx7mjLvgjF8lnmbGd/eBG8PIQzglP5c8RXPKG5Vc8UDADOBzvQ6nI= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000007, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Also adjust test_create_guest_memfd_invalid, as now BIT(0) is a valid value for flags (note that this also fixes an issue where the loop in test_create_guest_memfd_invalid is a noop. I've posted that fix as a separate patch last week [1]). [1]: https://lore.kernel.org/kvm/20241024095956.3668818-1-roypat@amazon.co.uk/ Signed-off-by: Patrick Roy --- tools/testing/selftests/kvm/guest_memfd_test.c | 2 +- .../selftests/kvm/x86_64/private_mem_conversions_test.c | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/kvm/guest_memfd_test.c b/tools/testing/selftests/kvm/guest_memfd_test.c index ba0c8e9960358..d04f7ff3dfb15 100644 --- a/tools/testing/selftests/kvm/guest_memfd_test.c +++ b/tools/testing/selftests/kvm/guest_memfd_test.c @@ -134,7 +134,7 @@ static void test_create_guest_memfd_invalid(struct kvm_vm *vm) size); } - for (flag = 0; flag; flag <<= 1) { + for (flag = BIT(1); flag; flag <<= 1) { fd = __vm_create_guest_memfd(vm, page_size, flag); TEST_ASSERT(fd == -1 && errno == EINVAL, "guest_memfd() with flag '0x%lx' should fail with EINVAL", diff --git a/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c b/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c index 82a8d88b5338e..dfc78781e93b8 100644 --- a/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c +++ b/tools/testing/selftests/kvm/x86_64/private_mem_conversions_test.c @@ -367,7 +367,7 @@ static void *__test_mem_conversions(void *__vcpu) } static void test_mem_conversions(enum vm_mem_backing_src_type src_type, uint32_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_src_type src_type, uint32_t vm_enable_cap(vm, KVM_CAP_EXIT_HYPERCALL, (1 << KVM_HC_MAP_GPA_RANGE)); - memfd = vm_create_guest_memfd(vm, memfd_size, 0); + memfd = vm_create_guest_memfd(vm, memfd_size, gmem_flags); for (i = 0; i < nr_memslots; i++) vm_mem_add(vm, src_type, BASE_DATA_GPA + slot_size * i, @@ -477,7 +477,8 @@ int main(int argc, char *argv[]) } } - 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, KVM_GMEM_NO_DIRECT_MAP); return 0; }