From nobody Mon Apr 6 16:45:36 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 2E9DC34107A for ; Wed, 18 Mar 2026 20:40:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773866402; cv=none; b=e+R+zs6DnvuekudVZsZgE62TMnm3Tp92cGJTUaSxVlz7luK7mF+tbmtEWFb3mbibIGfDt6LSLCDL5Q8bTDpaE4+pSK2T4DIygYiIbjH7SzEsY0kxWOm9HcyWDaB+ElwkXdRtmCm3RwwA69gJimmov4Dn50lBlk7VXx+KUhHfHyQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773866402; c=relaxed/simple; bh=Aa0MO4lda9XCau+hln7tuomgpTWeyeT58RJMegaGG+U=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=sYFc9GYAAO5LZ4lYm7fhvCy37EwOcDJcm15vi9h3Lm+4UdpAaDHhXVRfmzcFOeU6g7zC2FwAQYn1o2d4mlB/1CCY7+ksYM1E6Yxxe5DcCeSJRHX3KZvTwEDW5s8V7a0kgrLwrtEG10xsyiITuFssqlHLd9YlLpehJDGayU91eqQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=r663G5hF; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="r663G5hF" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 5EB34C19424; Wed, 18 Mar 2026 20:40:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773866401; bh=Aa0MO4lda9XCau+hln7tuomgpTWeyeT58RJMegaGG+U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=r663G5hFuynLBWFIAVuC1rRzPlY1j+MVXVEJUwDhkUKmMZ1r2VgBjPfhWh0zp8rA5 mfjUebr77i3N4ny5TUqUMqhDjkiwjnmeMXRjz83xu64yBHHX9t0oa6amJnnyob4/5h RrSdAUHfB5fX3/ZQyVOCMJ7PxaM8Emfnalr4PwN9GbP+7w67XR9jcRLUjCUO9HHgCx 0vyBQMCAMibUfzZ+IHV6Si3QGqD+tuAsbnK41FgcB5lPYh9vjWMdtUAIjjiQX1Djwm bUz/uiYQKnKT337AESwIRtG8wUig/4+nXgjBhVe8A+8ReX/FLxPtUcXekriUzPcI3Y RVU6rbgkgoX9w== From: "Lorenzo Stoakes (Oracle)" To: Andrew Morton Cc: David Hildenbrand , Zi Yan , Baolin Wang , "Liam R . Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 1/8] mm/huge_memory: simplify vma_is_specal_huge() Date: Wed, 18 Mar 2026 20:39:23 +0000 Message-ID: <84712e37721c557f887e4a405759be31a4961100.1773865827.git.ljs@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This function is confused - it overloads the term 'special' yet again, checks for DAX but in many cases the code explicitly excludes DAX before invoking the predicate. It also unnecessarily checks for vma->vm_file - this has to be present for a driver to have set VMA_MIXEDMAP_BIT or VMA_PFNMAP_BIT. In fact, a far simpler form of this is to reverse the DAX predicate and return false if DAX is set. This makes sense from the point of view of 'special' as in vm_normal_page(), as DAX actually does potentially have retrievable folios. Also there's no need to have this in mm.h so move it to huge_memory.c. Signed-off-by: Lorenzo Stoakes (Oracle) --- include/linux/huge_mm.h | 4 ++-- include/linux/mm.h | 16 ---------------- mm/huge_memory.c | 30 +++++++++++++++++++++++------- 3 files changed, 25 insertions(+), 25 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index bd7f0e1d8094..61fda1672b29 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -83,7 +83,7 @@ extern struct kobj_attribute thpsize_shmem_enabled_attr; * file is never split and the MAX_PAGECACHE_ORDER limit does not apply to * it. Same to PFNMAPs where there's neither page* nor pagecache. */ -#define THP_ORDERS_ALL_SPECIAL \ +#define THP_ORDERS_ALL_SPECIAL_DAX \ (BIT(PMD_ORDER) | BIT(PUD_ORDER)) #define THP_ORDERS_ALL_FILE_DEFAULT \ ((BIT(MAX_PAGECACHE_ORDER + 1) - 1) & ~BIT(0)) @@ -92,7 +92,7 @@ extern struct kobj_attribute thpsize_shmem_enabled_attr; * Mask of all large folio orders supported for THP. */ #define THP_ORDERS_ALL \ - (THP_ORDERS_ALL_ANON | THP_ORDERS_ALL_SPECIAL | THP_ORDERS_ALL_FILE_DEFAU= LT) + (THP_ORDERS_ALL_ANON | THP_ORDERS_ALL_SPECIAL_DAX | THP_ORDERS_ALL_FILE_D= EFAULT) =20 enum tva_type { TVA_SMAPS, /* Exposing "THPeligible:" in smaps. */ diff --git a/include/linux/mm.h b/include/linux/mm.h index 6f0a3edb24e1..50d68b092204 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -5077,22 +5077,6 @@ long copy_folio_from_user(struct folio *dst_folio, const void __user *usr_src, bool allow_pagefault); =20 -/** - * vma_is_special_huge - Are transhuge page-table entries considered speci= al? - * @vma: Pointer to the struct vm_area_struct to consider - * - * Whether transhuge page-table entries are considered "special" following - * the definition in vm_normal_page(). - * - * Return: true if transhuge page-table entries should be considered speci= al, - * false otherwise. - */ -static inline bool vma_is_special_huge(const struct vm_area_struct *vma) -{ - return vma_is_dax(vma) || (vma->vm_file && - (vma->vm_flags & (VM_PFNMAP | VM_MIXEDMAP))); -} - #endif /* CONFIG_TRANSPARENT_HUGEPAGE || CONFIG_HUGETLBFS */ =20 #if MAX_NUMNODES > 1 diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 3fc02913b63e..f76edfa91e96 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -100,6 +100,14 @@ static inline bool file_thp_enabled(struct vm_area_str= uct *vma) return !inode_is_open_for_write(inode) && S_ISREG(inode->i_mode); } =20 +/* If returns true, we are unable to access the VMA's folios. */ +static bool vma_is_special_huge(struct vm_area_struct *vma) +{ + if (vma_is_dax(vma)) + return false; + return vma_test_any(vma, VMA_PFNMAP_BIT, VMA_MIXEDMAP_BIT); +} + unsigned long __thp_vma_allowable_orders(struct vm_area_struct *vma, vm_flags_t vm_flags, enum tva_type type, @@ -113,8 +121,8 @@ unsigned long __thp_vma_allowable_orders(struct vm_area= _struct *vma, /* Check the intersection of requested and supported orders. */ if (vma_is_anonymous(vma)) supported_orders =3D THP_ORDERS_ALL_ANON; - else if (vma_is_special_huge(vma)) - supported_orders =3D THP_ORDERS_ALL_SPECIAL; + else if (vma_is_dax(vma) || vma_is_special_huge(vma)) + supported_orders =3D THP_ORDERS_ALL_SPECIAL_DAX; else supported_orders =3D THP_ORDERS_ALL_FILE_DEFAULT; =20 @@ -2431,7 +2439,7 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_ar= ea_struct *vma, tlb->fullmm); arch_check_zapped_pmd(vma, orig_pmd); tlb_remove_pmd_tlb_entry(tlb, pmd, addr); - if (!vma_is_dax(vma) && vma_is_special_huge(vma)) { + if (vma_is_special_huge(vma)) { if (arch_needs_pgtable_deposit()) zap_deposited_table(tlb->mm, pmd); spin_unlock(ptl); @@ -2933,7 +2941,7 @@ int zap_huge_pud(struct mmu_gather *tlb, struct vm_ar= ea_struct *vma, orig_pud =3D pudp_huge_get_and_clear_full(vma, addr, pud, tlb->fullmm); arch_check_zapped_pud(vma, orig_pud); tlb_remove_pud_tlb_entry(tlb, pud, addr); - if (!vma_is_dax(vma) && vma_is_special_huge(vma)) { + if (vma_is_special_huge(vma)) { spin_unlock(ptl); /* No zero page support yet */ } else { @@ -3084,7 +3092,7 @@ static void __split_huge_pmd_locked(struct vm_area_st= ruct *vma, pmd_t *pmd, */ if (arch_needs_pgtable_deposit()) zap_deposited_table(mm, pmd); - if (!vma_is_dax(vma) && vma_is_special_huge(vma)) + if (vma_is_special_huge(vma)) return; if (unlikely(pmd_is_migration_entry(old_pmd))) { const softleaf_t old_entry =3D softleaf_from_pmd(old_pmd); @@ -4645,8 +4653,16 @@ static void split_huge_pages_all(void) =20 static inline bool vma_not_suitable_for_thp_split(struct vm_area_struct *v= ma) { - return vma_is_special_huge(vma) || (vma->vm_flags & VM_IO) || - is_vm_hugetlb_page(vma); + if (vma_is_dax(vma)) + return true; + if (vma_is_special_huge(vma)) + return true; + if (vma_test(vma, VMA_IO_BIT)) + return true; + if (is_vm_hugetlb_page(vma)) + return true; + + return false; } =20 static int split_huge_pages_pid(int pid, unsigned long vaddr_start, --=20 2.53.0 From nobody Mon Apr 6 16:45:36 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 9B37E34BA21 for ; Wed, 18 Mar 2026 20:40:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773866404; cv=none; b=ddPSvEnmZtmaEKVPWtgkDG2BmfVHFLKyravnjNZsp4tjNf9hCWufXZyWymdT2FV8PNYq+x5weWHoOtw0n4//meJvMThh/ckOCzyD3NSTpb1CLU1BWYTBayXUkWA7mJKZytkuWzPWzkIADzgjeoKq4CO+8S+5/jtOzVSlc02ntrU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773866404; c=relaxed/simple; bh=/BZKRHWTrmwni6hMvARLgtvWyItsLi87T5oiCEhDDRg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=eRH55cG+VVMg6eAzFmVdUnO26SRVOHX6uyxoC+YNud5o4CPtD8LTP+dpQjWPoozExHCwcM0EyOQY7s6QAezp9RyScxM7rYQVBgPdX7Osz2ouujm7uyaT1CPbohzEl1XdQgu/HHGnFjjUzxY9gNlaUPM/nt6qq2kBGTU8n4N4W3I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=dNk7YNpO; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="dNk7YNpO" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1F280C19424; Wed, 18 Mar 2026 20:40:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773866404; bh=/BZKRHWTrmwni6hMvARLgtvWyItsLi87T5oiCEhDDRg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=dNk7YNpOW+pv4P+2l27X2/icD0/NxcJ921AC8DS6z39Slv9DqzhAXbWgPIsSWn6r/ rJ3jqrKwviThEq8g/mo6h4ZQj4EnZW4ZjFiI15vHcV1toqvYQLm8wLNCcysSQ7wZiB qzfvjqTE5Wg6x98z2qpWrza6Gsxl7/5tCtEXHBi6PRh6hPsiXtma64fkrZt7yVMqqw j4prB1i7ERZQEfXi/rk3utBZImLtREU1BAO9h4J0gSCnduQ2jcycSHDf1Q4y3B4MyR h6vLN2Wq94SBl4FMFR3yLVwdyhEllyNz/u9oXvphiLnEG9T72IWycl2YWIKzg2/oxs ltlXs/ZDvoeVw== From: "Lorenzo Stoakes (Oracle)" To: Andrew Morton Cc: David Hildenbrand , Zi Yan , Baolin Wang , "Liam R . Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 2/8] mm/huge: avoid big else branch in zap_huge_pmd() Date: Wed, 18 Mar 2026 20:39:24 +0000 Message-ID: <10be43ead023e9f65333bcaf4064d9d197111405.1773865827.git.ljs@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" We don't need to have an extra level of indentation, we can simply exit early in the first two branches. Signed-off-by: Lorenzo Stoakes (Oracle) Acked-by: Qi Zheng Reviewed-by: Baolin Wang --- mm/huge_memory.c | 87 +++++++++++++++++++++++++----------------------- 1 file changed, 45 insertions(+), 42 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index f76edfa91e96..4ebe1f19341e 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2421,8 +2421,10 @@ static inline void zap_deposited_table(struct mm_str= uct *mm, pmd_t *pmd) int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr) { - pmd_t orig_pmd; + struct folio *folio =3D NULL; + int flush_needed =3D 1; spinlock_t *ptl; + pmd_t orig_pmd; =20 tlb_change_page_size(tlb, HPAGE_PMD_SIZE); =20 @@ -2443,59 +2445,60 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_= area_struct *vma, if (arch_needs_pgtable_deposit()) zap_deposited_table(tlb->mm, pmd); spin_unlock(ptl); - } else if (is_huge_zero_pmd(orig_pmd)) { + return 1; + } + if (is_huge_zero_pmd(orig_pmd)) { if (!vma_is_dax(vma) || arch_needs_pgtable_deposit()) zap_deposited_table(tlb->mm, pmd); spin_unlock(ptl); - } else { - struct folio *folio =3D NULL; - int flush_needed =3D 1; + return 1; + } =20 - if (pmd_present(orig_pmd)) { - struct page *page =3D pmd_page(orig_pmd); + if (pmd_present(orig_pmd)) { + struct page *page =3D pmd_page(orig_pmd); =20 - folio =3D page_folio(page); - folio_remove_rmap_pmd(folio, page, vma); - WARN_ON_ONCE(folio_mapcount(folio) < 0); - VM_BUG_ON_PAGE(!PageHead(page), page); - } else if (pmd_is_valid_softleaf(orig_pmd)) { - const softleaf_t entry =3D softleaf_from_pmd(orig_pmd); + folio =3D page_folio(page); + folio_remove_rmap_pmd(folio, page, vma); + WARN_ON_ONCE(folio_mapcount(folio) < 0); + VM_BUG_ON_PAGE(!PageHead(page), page); + } else if (pmd_is_valid_softleaf(orig_pmd)) { + const softleaf_t entry =3D softleaf_from_pmd(orig_pmd); =20 - folio =3D softleaf_to_folio(entry); - flush_needed =3D 0; + folio =3D softleaf_to_folio(entry); + flush_needed =3D 0; =20 - if (!thp_migration_supported()) - WARN_ONCE(1, "Non present huge pmd without pmd migration enabled!"); - } + if (!thp_migration_supported()) + WARN_ONCE(1, "Non present huge pmd without pmd migration enabled!"); + } =20 - if (folio_test_anon(folio)) { + if (folio_test_anon(folio)) { + zap_deposited_table(tlb->mm, pmd); + add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR); + } else { + if (arch_needs_pgtable_deposit()) zap_deposited_table(tlb->mm, pmd); - add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR); - } else { - if (arch_needs_pgtable_deposit()) - zap_deposited_table(tlb->mm, pmd); - add_mm_counter(tlb->mm, mm_counter_file(folio), - -HPAGE_PMD_NR); - - /* - * Use flush_needed to indicate whether the PMD entry - * is present, instead of checking pmd_present() again. - */ - if (flush_needed && pmd_young(orig_pmd) && - likely(vma_has_recency(vma))) - folio_mark_accessed(folio); - } + add_mm_counter(tlb->mm, mm_counter_file(folio), + -HPAGE_PMD_NR); =20 - if (folio_is_device_private(folio)) { - folio_remove_rmap_pmd(folio, &folio->page, vma); - WARN_ON_ONCE(folio_mapcount(folio) < 0); - folio_put(folio); - } + /* + * Use flush_needed to indicate whether the PMD entry + * is present, instead of checking pmd_present() again. + */ + if (flush_needed && pmd_young(orig_pmd) && + likely(vma_has_recency(vma))) + folio_mark_accessed(folio); + } =20 - spin_unlock(ptl); - if (flush_needed) - tlb_remove_page_size(tlb, &folio->page, HPAGE_PMD_SIZE); + if (folio_is_device_private(folio)) { + folio_remove_rmap_pmd(folio, &folio->page, vma); + WARN_ON_ONCE(folio_mapcount(folio) < 0); + folio_put(folio); } + + spin_unlock(ptl); + if (flush_needed) + tlb_remove_page_size(tlb, &folio->page, HPAGE_PMD_SIZE); + return 1; } =20 --=20 2.53.0 From nobody Mon Apr 6 16:45:36 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 96E6C33C51D for ; Wed, 18 Mar 2026 20:40:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773866407; cv=none; b=fbJdINdZPJO0w/VBrLJKKy38k+Xx8F9iaIpMgZgwrRidJqAGDThZYzL/mMbmCqAEEESGCxARbqnRW1mE+21Q5Pw/cjxzYENSTrb32lNP+N0x/kwAUm04V25Tf5OYKgI+5vvBn7D3tlnTVuHC5RGa08jVMKocR2iScQGFLQG0/ro= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773866407; c=relaxed/simple; bh=7Zih2Kv0Dm+TrjDetzDdmQ+LGnrh29EyXLjilJ0KrRI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=rAph4D8A06SFZVlDEXOB1pwjtdMuTZF7pJpp5Hf4ci9o94JKziPwGld0D4xBphIno7rphwB0g5T/1FucaRfsTSpT6jXOdgBitJ+SF7oKf4o3nQTgPBdx0yXoSmpEvQshv5gQKCjROg4e5aRVXz/P35UBqHBxyB5lhA9Lz0om9Ms= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Zu1L7gfj; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Zu1L7gfj" Received: by smtp.kernel.org (Postfix) with ESMTPSA id CAE49C19424; Wed, 18 Mar 2026 20:40:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773866407; bh=7Zih2Kv0Dm+TrjDetzDdmQ+LGnrh29EyXLjilJ0KrRI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Zu1L7gfjbQFmjKt1J6VBjQ1d7W1ZUN6DCgHduL35Q1r56dv5jtqd2SVAdmtF8MenG UHmbv21UlczDU7YSEwXY4glnWufkHg3qNjRue1ZXCNLeMm7XBHznw+zHQQ+7e2OBUM SEhwFoK1SsEFnsbV9g+f7RD9tfkblSAKHKEEYETGWUfAm85p+PSEpWo6jkad3xNWnD CI/LqRjVQuWEND/quhDHS5654NtqaL6cO+mLZkLZy52stB7CD9IWO+/y9XsJNKKFA0 9yHinYu2OigIAOOyta9aZaOCrL19O2Bo57nsBfsD1SkX2mZS8F5njyknLQ/9vmy9wP FIgyWXKZuhfcA== From: "Lorenzo Stoakes (Oracle)" To: Andrew Morton Cc: David Hildenbrand , Zi Yan , Baolin Wang , "Liam R . Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 3/8] mm/huge_memory: have zap_huge_pmd return a boolean, add kdoc Date: Wed, 18 Mar 2026 20:39:25 +0000 Message-ID: <20e00d784adf1540c32b46024a709dfe1be93019.1773865827.git.ljs@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" There's no need to use the ancient approach of returning an integer here, just return a boolean. Also update flush_needed to be a boolean, similarly. Also add a kdoc comment describing the function. Signed-off-by: Lorenzo Stoakes (Oracle) Acked-by: Qi Zheng Reviewed-by: Baolin Wang --- include/linux/huge_mm.h | 4 ++-- mm/huge_memory.c | 23 ++++++++++++++++------- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index 61fda1672b29..2949e5acff35 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -27,8 +27,8 @@ static inline void huge_pud_set_accessed(struct vm_fault = *vmf, pud_t orig_pud) vm_fault_t do_huge_pmd_wp_page(struct vm_fault *vmf); bool madvise_free_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *= vma, pmd_t *pmd, unsigned long addr, unsigned long next); -int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, pmd_t= *pmd, - unsigned long addr); +bool zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, pmd_= t *pmd, + unsigned long addr); int zap_huge_pud(struct mmu_gather *tlb, struct vm_area_struct *vma, pud_t= *pud, unsigned long addr); bool move_huge_pmd(struct vm_area_struct *vma, unsigned long old_addr, diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 4ebe1f19341e..bba1ba1f6b67 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2418,11 +2418,20 @@ static inline void zap_deposited_table(struct mm_st= ruct *mm, pmd_t *pmd) mm_dec_nr_ptes(mm); } =20 -int zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, +/** + * zap_huge_pmd - Zap a huge THP which is of PMD size. + * @tlb: The MMU gather TLB state associated with the operation. + * @vma: The VMA containing the range to zap. + * @pmd: A pointer to the leaf PMD entry. + * @addr: The virtual address for the range to zap. + * + * Returns: %true on success, %false otherwise. + */ +bool zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr) { struct folio *folio =3D NULL; - int flush_needed =3D 1; + bool flush_needed =3D true; spinlock_t *ptl; pmd_t orig_pmd; =20 @@ -2430,7 +2439,7 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_ar= ea_struct *vma, =20 ptl =3D __pmd_trans_huge_lock(pmd, vma); if (!ptl) - return 0; + return false; /* * For architectures like ppc64 we look at deposited pgtable * when calling pmdp_huge_get_and_clear. So do the @@ -2445,13 +2454,13 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_= area_struct *vma, if (arch_needs_pgtable_deposit()) zap_deposited_table(tlb->mm, pmd); spin_unlock(ptl); - return 1; + return true; } if (is_huge_zero_pmd(orig_pmd)) { if (!vma_is_dax(vma) || arch_needs_pgtable_deposit()) zap_deposited_table(tlb->mm, pmd); spin_unlock(ptl); - return 1; + return true; } =20 if (pmd_present(orig_pmd)) { @@ -2465,7 +2474,7 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_ar= ea_struct *vma, const softleaf_t entry =3D softleaf_from_pmd(orig_pmd); =20 folio =3D softleaf_to_folio(entry); - flush_needed =3D 0; + flush_needed =3D false; =20 if (!thp_migration_supported()) WARN_ONCE(1, "Non present huge pmd without pmd migration enabled!"); @@ -2499,7 +2508,7 @@ int zap_huge_pmd(struct mmu_gather *tlb, struct vm_ar= ea_struct *vma, if (flush_needed) tlb_remove_page_size(tlb, &folio->page, HPAGE_PMD_SIZE); =20 - return 1; + return true; } =20 #ifndef pmd_move_must_withdraw --=20 2.53.0 From nobody Mon Apr 6 16:45:36 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 63B35352927 for ; Wed, 18 Mar 2026 20:40:10 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773866410; cv=none; b=fgCYNbIfXtecOLXEYwRimgwkX2dh4joXsdKzNwfEqXQWEm3LmHf76pTdAuTg6cstEidvX+2grxY3RP8GErumnQ6zBDSiodDE9C6JStzjdivnnnXUTBweY1CHK07sP4Qg7Jt861eIhBSRAyC/hF3ReQmafx2bo9lXArE9mJe+xk4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773866410; c=relaxed/simple; bh=4hGJuEdcThcMdbeM2AM1MWkN/V5ubnZTBsubak6jBD8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EpnbWtoh6PTyDclEgRgURdC4nSmi0Iuw8+6Lt2KzRKTLUy2GQwK6Clhx11xjGrTlYExk2SsVo4OakPxEHXuqpyyzVSUsGoOyuOmwAUpRbj1BifDf7yfql5Z8aOhIqtkQmYIsRSP+oAIJwWrTmiuISZDvfuyowFVvDJLTtdr5biM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=A+LWPAJd; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="A+LWPAJd" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 99554C2BC9E; Wed, 18 Mar 2026 20:40:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773866410; bh=4hGJuEdcThcMdbeM2AM1MWkN/V5ubnZTBsubak6jBD8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=A+LWPAJdQaFkpuzW67ZvVhCebOVm8I33QNmUh9lnf2px0US1OI/uyMqp2yEy7cLdL QJwBo24QvR4hvDHKYSKAs0WUOCZwUJg4DAzHUE9pbjk4TnSEJULI+p8807T2xRdtbG +fCum19lJQpsThRLuu8Jt8+67nzQ8IWZbzby8E+AvSavNf8mSthpV0mlUVzZIqrsyW GegAnY1LCd5nmt2Qcxd2lhney36z0ywkzYaKejcQGyaCVT37FiSwPuO90aeLZTNclQ lid7oh1gBOTesI8cEPb2SXJqT1PAeCzUefKPghOKJRSSCZ2xKMOc9TKaEnV2ebWyIq fuSWFxidRtemg== From: "Lorenzo Stoakes (Oracle)" To: Andrew Morton Cc: David Hildenbrand , Zi Yan , Baolin Wang , "Liam R . Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 4/8] mm/huge_memory: handle buggy PMD entry in zap_huge_pmd() Date: Wed, 18 Mar 2026 20:39:26 +0000 Message-ID: <8ffa393ad86b9b0ecd9b001ca88706ce2f9fe003.1773865827.git.ljs@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" A recent bug I analysed [0] managed to, through a bug in the userfaultfd implementation, reach an invalid point in the zap_huge_pmd() code where the PMD was none of: - A non-DAX, PFN or mixed map. - The huge zero folio - A present PMD entry - A softleaf entry The code at this point calls folio_test_anon() on a known-NULL folio. Having logic like this explicitly NULL dereference in the code is hard to understand, and makes debugging potentially more difficult. Add an else branch to handle this case and WARN() and exit indicating failure. [0]:https://lore.kernel.org/all/6b3d7ad7-49e1-407a-903d-3103704160d8@lucife= r.local/ Signed-off-by: Lorenzo Stoakes (Oracle) --- mm/huge_memory.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index bba1ba1f6b67..8e6b7ba11448 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2478,6 +2478,10 @@ bool zap_huge_pmd(struct mmu_gather *tlb, struct vm_= area_struct *vma, =20 if (!thp_migration_supported()) WARN_ONCE(1, "Non present huge pmd without pmd migration enabled!"); + } else { + WARN_ON_ONCE(true); + spin_unlock(ptl); + return false; } =20 if (folio_test_anon(folio)) { --=20 2.53.0 From nobody Mon Apr 6 16:45:36 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 66E6B343216 for ; Wed, 18 Mar 2026 20:40:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773866413; cv=none; b=lSR9vZBIn9WSj+yjRQxI6805aYyyZOR2ckVbUU5f570x2MiZ+EqkNy7WM+SgT7NgDrpCyVTygT+cMuzI8cBE9osVqb7KG3PiZAhpIcg0+F5BCoEu79aYIZWr59VCLMBpyyemFeiEI5swt8lBJzFP6HYybejkJk8rafPYdqbl7Eo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773866413; c=relaxed/simple; bh=PITG7FjF940ShIQWg3bTh2lmckgtj7Y/LuersIz6UxE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aDjXm73kC4oXgW7Ijg0EXqpV5hrg+YyiRxBpYMMH764W+IhvCDpmdP3UcsiPmQfHqxdV/oCmKsGoQUPem0DPVIS+umBiWXFkDz95f6sZqvM+LAIKr5iAABgKdE9aHCATMoQk+0HBBGDvwV3E56AZcmw/2w10yImsdr28Tw2m3u4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=OdvEiXHC; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="OdvEiXHC" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 87D6DC19424; Wed, 18 Mar 2026 20:40:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773866413; bh=PITG7FjF940ShIQWg3bTh2lmckgtj7Y/LuersIz6UxE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OdvEiXHCpeAbqal9LePZD/KHjW3xBrUm7WV0dIIJQr2KHDb3x/ltFr/AuEnIk2paw wWs3EG2ql2D4SXuru/wxiChjn9zjb15QQsXtyZd5RhebbR0QnuIwxdd+ay4ZeggAOZ Z5Lr15mPo/gj7zSTKc4lfSuIdCLYGFuYJUxAlLGtBxBuO/MVGE1+n7cQDQy2VuqQIL xrUOv5aQRhpvjNwjhLxW2+IagPnKUJAmB70c8wSjPy18bHhvdTWX1N+912BbMXCTie EqXMRYupVviG4B2gFzp8wzi47A1F1WH9UudPsq8jHspuEgLAIfgc6BY5906b+UR5sE aNu/8gElozfFw== From: "Lorenzo Stoakes (Oracle)" To: Andrew Morton Cc: David Hildenbrand , Zi Yan , Baolin Wang , "Liam R . Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 5/8] mm/huge_memory: add a common exit path to zap_huge_pmd() Date: Wed, 18 Mar 2026 20:39:27 +0000 Message-ID: <3d8ec0634fc3465b200ca678fe097097cf6ad1bc.1773865827.git.ljs@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Other than when we acquire the PTL, we always need to unlock the PTL, and optionally need to flush on exit. The code is currently very duplicated in this respect, so default flush_needed to false, set it true in the case in which it's required, then share the same logic for all exit paths. This also makes flush_needed make more sense as a function-scope value (we don't need to flush for the PFN map/mixed map, zero huge, error cases for instance). Signed-off-by: Lorenzo Stoakes (Oracle) --- mm/huge_memory.c | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 8e6b7ba11448..724e1de74367 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2431,7 +2431,8 @@ bool zap_huge_pmd(struct mmu_gather *tlb, struct vm_a= rea_struct *vma, pmd_t *pmd, unsigned long addr) { struct folio *folio =3D NULL; - bool flush_needed =3D true; + bool flush_needed =3D false; + bool ret =3D true; spinlock_t *ptl; pmd_t orig_pmd; =20 @@ -2453,19 +2454,18 @@ bool zap_huge_pmd(struct mmu_gather *tlb, struct vm= _area_struct *vma, if (vma_is_special_huge(vma)) { if (arch_needs_pgtable_deposit()) zap_deposited_table(tlb->mm, pmd); - spin_unlock(ptl); - return true; + goto out; } if (is_huge_zero_pmd(orig_pmd)) { if (!vma_is_dax(vma) || arch_needs_pgtable_deposit()) zap_deposited_table(tlb->mm, pmd); - spin_unlock(ptl); - return true; + goto out; } =20 if (pmd_present(orig_pmd)) { struct page *page =3D pmd_page(orig_pmd); =20 + flush_needed =3D true; folio =3D page_folio(page); folio_remove_rmap_pmd(folio, page, vma); WARN_ON_ONCE(folio_mapcount(folio) < 0); @@ -2474,14 +2474,13 @@ bool zap_huge_pmd(struct mmu_gather *tlb, struct vm= _area_struct *vma, const softleaf_t entry =3D softleaf_from_pmd(orig_pmd); =20 folio =3D softleaf_to_folio(entry); - flush_needed =3D false; =20 if (!thp_migration_supported()) WARN_ONCE(1, "Non present huge pmd without pmd migration enabled!"); } else { WARN_ON_ONCE(true); - spin_unlock(ptl); - return false; + ret =3D false; + goto out; } =20 if (folio_test_anon(folio)) { @@ -2508,11 +2507,11 @@ bool zap_huge_pmd(struct mmu_gather *tlb, struct vm= _area_struct *vma, folio_put(folio); } =20 +out: spin_unlock(ptl); if (flush_needed) tlb_remove_page_size(tlb, &folio->page, HPAGE_PMD_SIZE); - - return true; + return ret; } =20 #ifndef pmd_move_must_withdraw --=20 2.53.0 From nobody Mon Apr 6 16:45:36 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 273013559F5 for ; Wed, 18 Mar 2026 20:40:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773866416; cv=none; b=NQQpP2aCrxrwpYCKH/EWp99qPX7ORGSNYaOnVDPl+hwTXY5dyxiMGv1fcc2eQu0PrLfkfczyrym/NnpNFWC+1Eyxh8PKnaCpfKJIEKBrNA2yx7t0wF5bSiAm66gIUYgWF08eejID8EO2mP8yt90pMmqKQbhxrUr00vQpPI6RFqE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773866416; c=relaxed/simple; bh=af0POnNSVHYyaB+ZRCwPVvoBRXZ0k7zusmNNNRbS3RI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=iDcAL3eFvI45P4+/Hze1tHSuwzKQtvT7tPHQm8kmuMptJlPfa+TMdGI/mgWfwZK2FtVZt+lctK0k8w73xNOctmbIEKjquGtwyc09u/sMFQB6IIAzJU7Pf9xSvgcFO3LI3joYn4RQrm5dLNCLsvXzeDURRKUOu6MK1vBkPxYwWHo= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=R66hjQO9; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="R66hjQO9" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 563F0C19421; Wed, 18 Mar 2026 20:40:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773866415; bh=af0POnNSVHYyaB+ZRCwPVvoBRXZ0k7zusmNNNRbS3RI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=R66hjQO9gPI/+AqeZ9wyB7YmnSLhPg14m/GgYHcdPrKGGWOPEBeYRRAaW8M+mT9FM kRSvynMt0d/CyxlFDyKRzJK6uPAapKi8NjH2skSRO2QjHKXu8dVoLl77TysXPl+c+h 9Sw4lz/NdnC89NKj9VCEjDgSqhLypsRE1fxV22l/UbIPPmK3dkCpBZTUNqdRN65cGp A2SJrA0PtZEB0SFjYJDmRQIKBg5Hn3c3KgyPd258Ca+xLffH/nFRSw3ILoDUCLQfru afJ7rEtLQLN4db4HxwFSl7yWuLEeszTPu6gqmHdvSfJwTXkveFAvjEtFynMGSVTXWZ GotTE+OUZj2aw== From: "Lorenzo Stoakes (Oracle)" To: Andrew Morton Cc: David Hildenbrand , Zi Yan , Baolin Wang , "Liam R . Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 6/8] mm/huge_memory: remove unnecessary VM_BUG_ON_PAGE() Date: Wed, 18 Mar 2026 20:39:28 +0000 Message-ID: <56df2cf900132ae12e8b71ed95519b5b476e1618.1773865827.git.ljs@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" This has been around since the beginnings of the THP implementation. I think we can safely assume that, if we have a THP folio, it will have a head page. Signed-off-by: Lorenzo Stoakes (Oracle) Reviewed-by: Baolin Wang --- mm/huge_memory.c | 1 - 1 file changed, 1 deletion(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 724e1de74367..015f6d679d26 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2469,7 +2469,6 @@ bool zap_huge_pmd(struct mmu_gather *tlb, struct vm_a= rea_struct *vma, folio =3D page_folio(page); folio_remove_rmap_pmd(folio, page, vma); WARN_ON_ONCE(folio_mapcount(folio) < 0); - VM_BUG_ON_PAGE(!PageHead(page), page); } else if (pmd_is_valid_softleaf(orig_pmd)) { const softleaf_t entry =3D softleaf_from_pmd(orig_pmd); =20 --=20 2.53.0 From nobody Mon Apr 6 16:45:36 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 C8F4C34B1AB for ; Wed, 18 Mar 2026 20:40:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773866418; cv=none; b=KY85d10wZpnkX9ZAjakP7SbjraYYMwb1X+SphGt7TcQM3RgGW1ImF20kKc9lKM5VADxiTNi8gBZgV+2hNmLL817kAY7GKG9hTMl0lzX5AO0rOm2o5VBw+xtheZvAqqK3rOGJEahvmCijQSLQlqhq1L/eL2pV/wK/IYOmIG5ENE8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773866418; c=relaxed/simple; bh=E01AFawUgRlaY07x436+rCWEekEc+HPRai65dkjw64E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ULWk9pqr3dfp8ta0DS7aT+4yELK+GXqPB9YG1zjeNT/YTcOEfn5aOGvB2C39KrnUDCPC3u5ybkcc4Ql89AtR/SgIqgwm7jKNYP0wy1f1jlcX+tVf3X1mLH68jA6PZSfqihIvktUpgxYrMQV9nRWmoNelEY+1G8zyvzix45Ohw6A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=L+fyh4Zy; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="L+fyh4Zy" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0ADB9C2BC9E; Wed, 18 Mar 2026 20:40:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773866418; bh=E01AFawUgRlaY07x436+rCWEekEc+HPRai65dkjw64E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=L+fyh4ZydYkd4WFvJ9Xsbol19Tx+juyZovtGPhzUXgZNtJEdqz4+Fp13fEM+TrkZX VZ7noDLS1+abHTKUs0XHYP8SRahlrrqTW5A2R/HBTZlt2xdzyZxCEcGxl51u/kZfIT SH7rIPlU/CGxGsokn7r37FvNu8M+jOSm2CxuVoo2y3AnqXKXrT2J8O4iZKqccrGVhI zHECftogKwOR60GR3J96WVFWxPjWucpOc9zqveiqMuOHKOAW345mkJODgvtvojmMx1 0a9WHW1xwqBq1cM1O7GBUYa3yQyrlCukYZaAXGfv/nqVfbmdan7GpQmJfE7El0O4yZ /CW4HSV0JOa0A== From: "Lorenzo Stoakes (Oracle)" To: Andrew Morton Cc: David Hildenbrand , Zi Yan , Baolin Wang , "Liam R . Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 7/8] mm/huge_memory: deduplicate zap deposited table call Date: Wed, 18 Mar 2026 20:39:29 +0000 Message-ID: <3e6343f91f8811fd1cf66f03ddde493daf95c0e3.1773865827.git.ljs@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Rather than having separate logic for each case determining whether to zap the deposited table, simply track this via a boolean. We check separately if the architecture requires it. Also use pmd_folio() direct in the present case. Signed-off-by: Lorenzo Stoakes (Oracle) --- mm/huge_memory.c | 21 +++++++++------------ 1 file changed, 9 insertions(+), 12 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 015f6d679d26..bcc74b0172fa 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2432,6 +2432,7 @@ bool zap_huge_pmd(struct mmu_gather *tlb, struct vm_a= rea_struct *vma, { struct folio *folio =3D NULL; bool flush_needed =3D false; + bool needs_deposit =3D false; bool ret =3D true; spinlock_t *ptl; pmd_t orig_pmd; @@ -2451,23 +2452,18 @@ bool zap_huge_pmd(struct mmu_gather *tlb, struct vm= _area_struct *vma, tlb->fullmm); arch_check_zapped_pmd(vma, orig_pmd); tlb_remove_pmd_tlb_entry(tlb, pmd, addr); - if (vma_is_special_huge(vma)) { - if (arch_needs_pgtable_deposit()) - zap_deposited_table(tlb->mm, pmd); + if (vma_is_special_huge(vma)) goto out; - } if (is_huge_zero_pmd(orig_pmd)) { - if (!vma_is_dax(vma) || arch_needs_pgtable_deposit()) - zap_deposited_table(tlb->mm, pmd); + needs_deposit =3D !vma_is_dax(vma); goto out; } if (pmd_present(orig_pmd)) { - struct page *page =3D pmd_page(orig_pmd); + folio =3D pmd_folio(orig_pmd); flush_needed =3D true; - folio =3D page_folio(page); - folio_remove_rmap_pmd(folio, page, vma); + folio_remove_rmap_pmd(folio, &folio->page, vma); WARN_ON_ONCE(folio_mapcount(folio) < 0); } else if (pmd_is_valid_softleaf(orig_pmd)) { const softleaf_t entry =3D softleaf_from_pmd(orig_pmd); @@ -2483,11 +2479,9 @@ bool zap_huge_pmd(struct mmu_gather *tlb, struct vm_= area_struct *vma, } if (folio_test_anon(folio)) { - zap_deposited_table(tlb->mm, pmd); + needs_deposit =3D true; add_mm_counter(tlb->mm, MM_ANONPAGES, -HPAGE_PMD_NR); } else { - if (arch_needs_pgtable_deposit()) - zap_deposited_table(tlb->mm, pmd); add_mm_counter(tlb->mm, mm_counter_file(folio), -HPAGE_PMD_NR); @@ -2507,6 +2501,9 @@ bool zap_huge_pmd(struct mmu_gather *tlb, struct vm_a= rea_struct *vma, } out: + if (arch_needs_pgtable_deposit() || needs_deposit) + zap_deposited_table(tlb->mm, pmd); + spin_unlock(ptl); if (flush_needed) tlb_remove_page_size(tlb, &folio->page, HPAGE_PMD_SIZE); -- 2.53.0 From nobody Mon Apr 6 16:45:36 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 618D93559F5 for ; Wed, 18 Mar 2026 20:40:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773866421; cv=none; b=sqh8XG3JAondC7uiD8fuYvzJlPtssDeKdxUbUVjHLdoyimUVLUoD0cYFEOFEQMe4tP7DI0427E5Czvdm9mEQYPwyuRkHMh305nmV1NwJgbWFYksbVD5QM7gqgINVkSRLsX1lCh8IiGE+KoiZJdTNqraEWJfKA13NPEITUAXTTRI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1773866421; c=relaxed/simple; bh=tBNKEPKePH5rEEJcTRad3eBiHZXAi6VWZ/qpvNX9iPk=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HyUZ70W20L1dFgXrZhmgC6/ML0dcYOnFKLx6HFIvhiQ8uwkOqrO8qdalaAa4J4qwJa/jKupjcznGQ+e8EuvfRvcBrztHskWFjeyfc3iwosowF09/htxEzOE9XPryqFcS+Xo99SbyPDdC7zzn+2KPyzzTi4wvnoo2o+YL3LD8VJY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=DxUHWIwf; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="DxUHWIwf" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B3C76C19421; Wed, 18 Mar 2026 20:40:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773866421; bh=tBNKEPKePH5rEEJcTRad3eBiHZXAi6VWZ/qpvNX9iPk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=DxUHWIwfNCja3hhGITdGYGgMfnGvvhVMGmciAOR/AP3E9eTKa/Jt1sibs6P5qkpJu nae2V11wRFGLg5fWk8LVf5OcEWRVgWQb1TmrTqePSLHh521a//R5Pm7VmDlCcerWKF r+sfCgei15flhHfFC5LdWpzEesuiI8YdeFyNwiwQp9/XXHPL63LuTzEUpsbhEOOn14 YXQEnqQpHxge/smqA6tixVDENxuecMZ7dQ10HOi56YxLT1a5N2LpsEz3qjXsHS/gwh Bh/DiQp58C7YWKmdEp1s9NjA6L+7A1CLV2vko/qFGtqFUUpx+JyjzYuZoWquy4jacF +FVuOlQqP6rUg== From: "Lorenzo Stoakes (Oracle)" To: Andrew Morton Cc: David Hildenbrand , Zi Yan , Baolin Wang , "Liam R . Howlett" , Nico Pache , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: [PATCH 8/8] mm/huge_memory: deduplicate zap_huge_pmd() further by tracking state Date: Wed, 18 Mar 2026 20:39:30 +0000 Message-ID: <102016790a04d115dff734765caa25be25771405.1773865827.git.ljs@kernel.org> X-Mailer: git-send-email 2.53.0 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The flush_needed boolean is really tracking whether a PMD entry is present, so use it that way directly and rename it to is_present. Deduplicate the folio_remove_rmap_pmd() and folio map count warning between present and device private by tracking where we need to remove the rmap. We can also remove the comment about using flush_needed to track whether a PMD entry is present as it's now irrelevant. Signed-off-by: Lorenzo Stoakes (Oracle) --- mm/huge_memory.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index bcc74b0172fa..f6caa6d35659 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2430,9 +2430,10 @@ static inline void zap_deposited_table(struct mm_str= uct *mm, pmd_t *pmd) bool zap_huge_pmd(struct mmu_gather *tlb, struct vm_area_struct *vma, pmd_t *pmd, unsigned long addr) { + bool needs_remove_rmap =3D false; struct folio *folio =3D NULL; - bool flush_needed =3D false; bool needs_deposit =3D false; + bool is_present =3D false; bool ret =3D true; spinlock_t *ptl; pmd_t orig_pmd; @@ -2450,6 +2451,7 @@ bool zap_huge_pmd(struct mmu_gather *tlb, struct vm_a= rea_struct *vma, */ orig_pmd =3D pmdp_huge_get_and_clear_full(vma, addr, pmd, tlb->fullmm); + arch_check_zapped_pmd(vma, orig_pmd); tlb_remove_pmd_tlb_entry(tlb, pmd, addr); if (vma_is_special_huge(vma)) @@ -2459,17 +2461,15 @@ bool zap_huge_pmd(struct mmu_gather *tlb, struct vm= _area_struct *vma, goto out; } =20 - if (pmd_present(orig_pmd)) { + is_present =3D pmd_present(orig_pmd); + if (is_present) { folio =3D pmd_folio(orig_pmd); - - flush_needed =3D true; - folio_remove_rmap_pmd(folio, &folio->page, vma); - WARN_ON_ONCE(folio_mapcount(folio) < 0); + needs_remove_rmap =3D true; } else if (pmd_is_valid_softleaf(orig_pmd)) { const softleaf_t entry =3D softleaf_from_pmd(orig_pmd); =20 folio =3D softleaf_to_folio(entry); - + needs_remove_rmap =3D folio_is_device_private(folio); if (!thp_migration_supported()) WARN_ONCE(1, "Non present huge pmd without pmd migration enabled!"); } else { @@ -2485,27 +2485,25 @@ bool zap_huge_pmd(struct mmu_gather *tlb, struct vm= _area_struct *vma, add_mm_counter(tlb->mm, mm_counter_file(folio), -HPAGE_PMD_NR); =20 - /* - * Use flush_needed to indicate whether the PMD entry - * is present, instead of checking pmd_present() again. - */ - if (flush_needed && pmd_young(orig_pmd) && + if (is_present && pmd_young(orig_pmd) && likely(vma_has_recency(vma))) folio_mark_accessed(folio); } =20 - if (folio_is_device_private(folio)) { + if (needs_remove_rmap) { folio_remove_rmap_pmd(folio, &folio->page, vma); WARN_ON_ONCE(folio_mapcount(folio) < 0); - folio_put(folio); } =20 out: if (arch_needs_pgtable_deposit() || needs_deposit) zap_deposited_table(tlb->mm, pmd); =20 + if (needs_remove_rmap && !is_present) + folio_put(folio); + spin_unlock(ptl); - if (flush_needed) + if (is_present) tlb_remove_page_size(tlb, &folio->page, HPAGE_PMD_SIZE); return ret; } --=20 2.53.0