From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3FD6760A260A for ; Thu, 29 Oct 2026 14:31:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key, unprotected) header.d=lists.infradead.org header.i=@lists.infradead.org header.a=rsa-sha256 header.s=bombadil.20210309 header.b=tGalYyuJ; dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=S+7+kNzI DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:In-Reply-To:Content-Type: MIME-Version:References:Message-ID:Subject:Cc:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Unsubscribe-Post:List-Owner; bh=mqXY1OcC5ARgzPi+R8XxHlz09YM9rQmQCBQ/7d/aXCU=; b=tGalYyuJqU3ueXEbZFyMW7J5zv pMBDCN4Hz+ni1F5La8OpSKnjg3t3cs2efaRGFSJ4h78DOZwMI/9f3sY5sJdunPjYRZqBxph9AGiN/ /MDpbGizPpLeqVxUC/j9iYBSd1hxJ8EvyE/1iXI75S4nVESc6D6KtGaibtH3XAngdRAI6IGALjoIE 959Fb22fJTZ8ov3Pb3Ha+67oBEkZwwolOdZo/9855zFJ1tIEyH3NMa0plZ7IAP+YUZ3vI12/BlFDt qLIBtWV4FqAGhIUhOvmKujW/H2sc+3UFi0GgtmkBKVJCqspPgiHB5EoMJLmSmR+B7XEG5nUFGAF9H JFCqdHjg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.100.1 #2 (Red Hat Linux)) id 1xMRAC-00000009Wo8-0obe; Thu, 29 Oct 2026 14:31:32 +0000 Received: from sea.source.kernel.org ([2600:3c0a:e001:78e:0:1991:8:25]) by bombadil.infradead.org with esmtps (Exim 4.100.1 #2 (Red Hat Linux)) id 1xMRAB-00000009Wnh-0PNt for kexec@lists.infradead.org; Thu, 29 Oct 2026 14:31:31 +0000 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by sea.source.kernel.org (Postfix) with ESMTP id 4081A43D1C; Thu, 29 Oct 2026 14:31:30 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 670061F01825; Thu, 29 Oct 2026 14:31:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1793284290; bh=mqXY1OcC5ARgzPi+R8XxHlz09YM9rQmQCBQ/7d/aXCU=; h=Date:From:To:Cc:Subject:References:In-Reply-To; b=S+7+kNzIX2ZMdfMr/hr6l4bhKncW3jvATRjUZNWSosKkVDp+gfFYQ4kqjyiXjyyAK gs5kPXW8xG/0KT+HvaTk4xQTZL9v3hHSNBHmZpL8Pp7wWOJucUIy6ZbUAV4KXBR9x1 WYtze+u+asK21+yj3mqxwfp+1brn7eF/wYYE4dYb/mAQp0dP7jvSdnFY4f35xAZv1W 1wJ5WknxozTNNR3uE3WCHiS9+iQbSjFlGEJv0CzUW305T8TDzosH2HSKrlhpFrYV0d IFR/sChwTcpkPvhDuq0AUrOOuXSZfwDloqRcMjT2PK3CjH1QEgeNOgtYdTI1xnrx0/ Ylgvn4jx6UkMQ== Received: from phl-compute-01.internal (phl-compute-01.internal [10.202.2.41]) by mailfauth.ams.internal (Postfix) with ESMTP id D7CDE198003A; Thu, 29 Oct 2026 10:31:14 -0400 (EDT) Received: from phl-frontend-04 ([10.202.2.163]) by phl-compute-01.internal (MEProxy); Thu, 29 Oct 2026 10:31:24 -0400 X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: dmFkZTEZfGskmN/KIALm0CJB+7YSa6+/0z6TYSZsJYszNysoKl9ha19QRgLMFtDXR5YTJU /ZtSllXOANZXHyFMLUtTazs3TqL4wn789TDCt9Clg6UWb/6TcorYCgPnRO1yG/d+ASCNki ef8dq7SubLCazhJOigTaPhJlXCh+AauxneJX30NAaSC9cSSvOTgd/RYPeHFhgwmIxxm9Yd XUnA7opn8M6R0HNovJsW3EeF88ulEUJFLh9pAaky7aJDjYSiN5BtFPOV7TerL4btsXoEPq cp6ZV2vmAclzI+C8mBPXQk99548l2lhaunvBg1pEEnk0CURYD04FMA3h2gwijrqVLBamEX +KoxpWN36HbdMDBokyDgZcydhpS1f9KgI8PaFAed8ADm5abJnc6vuHwnggI8aO9IAtffDI ipCbDbz1M0bdaXw7zTOgeTBkOyOY6iObnEFXJnh8OiD+rhFLRxfOgjlS5bLqPRuXOEA7dc zuovqc0yO0PHGX5SV5M+OqtwEuHlLIRmba3XmtKAmvZQn97ay5w+2tiMYysvPf4WcLEm0y mClz5VTCDYXL6iInsVZEfxH8cbr6DpXm1B3a05NNC7Hkc4aI3diZhsPjvb2EocdX3sTJRy rSmOHFVblnqwyUxgB9IIfYCWAnZ7TnDQAP2A4F7kC+UwttipX8k4t0q/Ks7w X-ME-Proxy: Feedback-ID: i10464835:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 29 Oct 2026 10:31:11 -0400 (EDT) Date: Thu, 29 Oct 2026 14:31:10 +0000 From: Kiryl Shutsemau To: "David Hildenbrand (Arm)" Cc: Andrew Morton , Johannes Weiner , Michal Hocko , Qi Zheng , Shakeel Butt , Lorenzo Stoakes , Kairui Song , Barry Song , Axel Rasmussen , Yuanchu Xie , Wei Xu , Baoquan He , Baolin Wang , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Kees Cook , Peter Zijlstra , Jann Horn , Pedro Falcato , Hugh Dickins , Pasha Tatashin , Pratyush Yadav , "Matthew Wilcox (Oracle)" , Jan Kara , Zi Yan , Lance Yang , Rik van Riel , Harry Yoo , Alistair Popple , Nico Pache , Ryan Roberts , Dev Jain , Usama Arif , Matthew Brost , John Hubbard , Peter Xu , Will Deacon , "Aneesh Kumar K.V" , Nick Piggin , Oscar Salvador , Minchan Kim , Muchun Song , Xu Xin , Chengming Zhou , Chris Li , Kemeng Shi , Nhat Pham , Youngjun Park , Gregory Price , Joshua Hahn , Rakie Kim , Ying Huang , Jason Gunthorpe , linux-mm@kvack.org, linux-kernel@vger.kernel.org, kexec@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-arch@vger.kernel.org, linux-cxl@vger.kernel.org, stable@vger.kernel.org Subject: Re: [PATCH 42/46] mm/huge_memory: split huge PUDs when adjusting VMA boundaries Message-ID: References: <20261027-fixes_core-v1-0-6f74bec9a5bb@kernel.org> <20261027-fixes_core-v1-42-6f74bec9a5bb@kernel.org> <9f4128a4-85fc-4f4b-ac41-b9d348f56e37@kernel.org> X-Archived-By: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <9f4128a4-85fc-4f4b-ac41-b9d348f56e37@kernel.org> X-BeenThere: kexec@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "kexec" X-Spam-Status: No, score=-15.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 On Thu, Oct 29, 2026 at 03:24:29PM +0100, David Hildenbrand (Arm) wrote: > On 10/29/26 12:45, Kiryl Shutsemau wrote: > > On Tue, Oct 27, 2026 at 12:18:02AM +0100, David Hildenbrand (Arm) wrote: > >> PFN mappings such as VFIO PCI BAR mappings can use PUD leaves while > >> allowing VMA splits at page granularity. However, vma_adjust_trans_huge() > >> only handles PMDs. > >> > >> After faulting a writable PUD-sized VFIO BAR mapping, changing its first > >> page to read-only with mprotect() makes the PMD walk treat the PUD leaf > >> as a PMD table. Applying MADV_DONTFORK to the first PMD-sized chunk leaves > >> the PUD spanning two VMAs: the PMD-aligned boundary skips splitting. > >> > >> Walk the page tables once at each adjusted boundary and split a huge > >> PUD or PMD leaf as needed. Handle PUD leaves even when the boundary is > >> PMD-aligned. > >> > >> Fixes: f9e54c3a2f5b7 ("vfio/pci: implement huge_fault support") > >> Cc: stable@vger.kernel.org > >> Signed-off-by: David Hildenbrand (Arm) > >> --- > >> mm/huge_memory.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++-------- > >> 1 file changed, 60 insertions(+), 10 deletions(-) > >> > >> diff --git a/mm/huge_memory.c b/mm/huge_memory.c > >> index 08e58cda7d37d..d090642ac8c2b 100644 > >> --- a/mm/huge_memory.c > >> +++ b/mm/huge_memory.c > >> @@ -3503,16 +3503,66 @@ void split_huge_pmd_address(struct vm_area_struct *vma, unsigned long address, > >> __split_huge_pmd(vma, pmd, address, freeze); > >> } > >> > >> -static inline void split_huge_pmd_if_needed(struct vm_area_struct *vma, unsigned long address) > >> +static bool pud_split_required(const struct vm_area_struct *vma, > >> + unsigned long address) > >> { > >> + if (!IS_ENABLED(CONFIG_HAVE_ARCH_TRANSPARENT_HUGEPAGE_PUD)) > >> + return false; > >> + if (IS_ALIGNED(address, HPAGE_PUD_SIZE)) > >> + return false; > >> /* > >> - * If the new address isn't hpage aligned and it could previously > >> - * contain an hugepage: check if we need to split an huge pmd. > >> + * Only selected VMAs support mapping PUDs for now. Lift the > >> + * restriction once this changes. > >> */ > >> - if (!IS_ALIGNED(address, HPAGE_PMD_SIZE) && > >> - range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PMD_SIZE), > >> - ALIGN(address, HPAGE_PMD_SIZE))) > >> - split_huge_pmd_address(vma, address, false); > >> + if (!vma_is_dax(vma) && !vma_test_any(vma, VMA_PFNMAP_BIT, VMA_MIXEDMAP_BIT)) > >> + return false; > >> + return range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PUD_SIZE), > >> + ALIGN(address, HPAGE_PUD_SIZE)); > >> +} > >> + > >> +static bool pmd_split_required(const struct vm_area_struct *vma, > >> + unsigned long address) > >> +{ > >> + if (IS_ALIGNED(address, HPAGE_PMD_SIZE)) > >> + return false; > >> + > >> + return range_in_vma(vma, ALIGN_DOWN(address, HPAGE_PMD_SIZE), > >> + ALIGN(address, HPAGE_PMD_SIZE)); > >> +} > >> + > >> +static void split_huge_leaf(struct vm_area_struct *vma, > >> + unsigned long address) > >> +{ > >> + const bool split_pud = pud_split_required(vma, address); > >> + const bool split_pmd = pmd_split_required(vma, address); > >> + pgd_t *pgd; > >> + p4d_t *p4d; > >> + pud_t *pud; > >> + pmd_t *pmd; > >> + > >> + /* > >> + * mmap, VMA and rmap write locks protect the walk; the split helpers > >> + * take the page table locks. > >> + */ > >> + if (!split_pud && !split_pmd) > >> + return; > >> + > >> + pgd = pgd_offset(vma->vm_mm, address); > >> + if (!pgd_present(*pgd)) > >> + return; > >> + > >> + p4d = p4d_offset(pgd, address); > >> + if (!p4d_present(*p4d)) > >> + return; > >> + > >> + pud = pud_offset(p4d, address); > >> + if (split_pud && pud_leaf(*pud)) > >> + __split_huge_pud(vma, pud, address); > >> + if (!split_pmd || !pud_present(*pud)) > >> + return; > > > > This looks wrong. > > > > We should bail out here if PUD is was a leaf and wasn't split. > > __split_huge_pud() must split a leave. Either by zapping it (what we do right > now) or by populating page tables (as in Usama's PoC). No, meant the situation when pud_leaf() is true, but split_pud is false, so __split_huge_pud() is never hit. pud_present() is true so we proceed to pmd_offset() -- Kiryl Shutsemau / Kirill A. Shutemov