From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 789CA606A322 for ; Thu, 3 Dec 2026 10:28:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=iu4Nip5s Received: by kanga.kvack.org (Postfix) id 839B36B05BD; Thu, 3 Dec 2026 05:28:39 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7EA616B05C2; Thu, 3 Dec 2026 05:28:39 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6D93B6B05C3; Thu, 3 Dec 2026 05:28:39 -0500 (EST) 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 D6FD26B05BD for ; Thu, 3 Dec 2026 05:28:38 -0500 (EST) Received: from smtpin12.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 471E3405BE for ; Thu, 3 Dec 2026 10:28:38 +0000 (UTC) X-FDA: 85502297916.12.E503235 Received: from tor.source.kernel.org (tor.source.kernel.org [172.105.4.254]) by imf26.hostedemail.com (Postfix) with ESMTP id 9B3DB14000C for ; Thu, 3 Dec 2026 10:28:36 +0000 (UTC) Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=iu4Nip5s; spf=pass (imf26.hostedemail.com: domain of sashiko-bot@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=sashiko-bot@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796293716; b=AMITOdrJqWPT1F4KOBTzo2/5abxXvdtX5drjtC/wvrNEOM5iJOT8u+ew/krOQsKPwmzqIm Mw9zoN0e19PQBNvLGLxCD2gtjW1PH1d24+yVObWjc7FRNtQ2ulq0mf+JJU4GeTsBc+Qj2U K3/iGuv/KRsZ6O5QPOp9yVO5SbeD5s8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796293716; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=uPPWh01eNh1ur7UFhSYuWdWrOYjOBj03d6XDBzCw91w=; b=B6h3BEsYwy6I57LpRE2NHhexblUK/l1PA84M2JnF6GLi8W7DfYsHzTGbrXFCJjqiuqYwPT L/sFpDRSolk/1mePYG1+KL06XOsPgqF6L1GoKB3xFCNVxKF6oVeaNIJ7Ue8P+dumyLjzyf fIgZREbUcJeoQ95c8heUImTGgm9VnGY= ARC-Authentication-Results: i=1; imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20260515 header.b=iu4Nip5s; spf=pass (imf26.hostedemail.com: domain of sashiko-bot@kernel.org designates 172.105.4.254 as permitted sender) smtp.mailfrom=sashiko-bot@kernel.org; dmarc=pass (policy=quarantine) header.from=kernel.org Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id 63B49626A0; Thu, 3 Dec 2026 10:28:35 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 31D5C1F000C1; Thu, 3 Dec 2026 10:28:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796293715; bh=uPPWh01eNh1ur7UFhSYuWdWrOYjOBj03d6XDBzCw91w=; h=From:Subject:Reply-To:To:Cc:In-Reply-To:References:Date; b=iu4Nip5spoOeWEuw1AXS2syzxfThvI/+O9LwMQ5/5xrbtI6PUoLfY5ynQ2RL4Tjo0 j7psHYuD9cExZOen7R6TTtV/2GcL/ncaoMjwJB7aMWgxZ5K8COtlFrvDa22HiOipbA LN/s6OrBx+JTx+TBc8N+ePp3RprfMRQ4Qz0LggYGllzqvNz8sHvJoc0VfaKNkl7ArG I2OorEaCqWsB1PhqCcSeB4AWdBgPfsjOnyaZzud1wXW/DxDCm7ebPlZKecXLSwrU6q Rcytf6DAqKBs3eVKN9ploYpkJvO26WHzf8WTzwiuj7kB85WR73wKJADOkfK0YZqH4m pkUEiUKNpkv3g== Message-ID: From: sashiko-bot@kernel.org Subject: Re: [RFC PATCH] mm: madvise: don't split a PMD on partial THP MADV_DONTNEED Reply-To: sashiko-reviews@lists.linux.dev To: "Alexandre Ghiti" , "Andrew Morton" , "David Hildenbrand" , "Zi Yan" Cc: "Baolin Wang" , "Liam R. Howlett" , "Nico Pache" , "Ryan Roberts" , "Dev Jain" , "Barry Song" , "Lance Yang" , "Usama Arif" , "Kiryl Shutsemau" , "Vlastimil Babka" , "Mike Rapoport" , "Michal Hocko" , "Lorenzo Stoakes" , "Suren Baghdasaryan" , "Shakeel Butt" , "Pedro Falcato" , "Jann Horn" , linux-mm@kvack.org In-Reply-To: <20261202102818.1317130-1-alex@ghiti.fr> References: <20261202102818.1317130-1-alex@ghiti.fr> Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Date: Thu, 03 Dec 2026 10:28:33 +0000 X-Rspamd-Server: rspam08 X-Rspam-User: X-Spam-Status: No, score=-10.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS autolearn=ham autolearn_force=no version=4.0.1 X-Stat-Signature: eo7s8zdc1q7kpqszecc6ubgi7gc4ebi5 X-Rspamd-Queue-Id: 9B3DB14000C X-HE-Tag: 1796293716-947247 X-HE-Meta: U2F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lMa/8ZYzFf1YiFhFBzuAc/RTBQuxpA58ZfOu7az8gdiOU4kwwKyUq9WLy1nqaUFl9WzxWUV+KAU4EZz/xMMrf2ZK4cd3jZ39SZVOJJHforIkZ0UN22gwH71tcx3cMC/041EDjdtXcaiWaB1VkQ06iWhmXJN2ghmP3UDvzlfHj81Y7OyliWbIp6jmPOIksKsJSbDUgUXrzQ8fH2tdVUJNONiK9gwum9nOlRnnTvAxLDKN5MOfyTAwtZbReYPtuxbra3FDdpV2gBNm+PpF5jdsntDx6Y+W4LYwCRAhgAXhxnEYYr/9XX32LrNIXCvncCurQ1Cd9BUcm6nrq+XGIptNKY32GgMs86nGOY+aw6HNqprhGnUVmteJgODtn95VhIMxd6X08bNVtH5bnYscRwgJ87+zwVlEdplPhD8Nlzz8lu+i7RkCADhtA+RhB69Np3w4NEoKhWL4AM0nuLsUM0IKViw3pvrh04CN2b+0SaEqmpvwlqvPtUZ611fW+4DYsHuAGngo8MNTEuvOR2CO4uzwHISj131RqF7wyfxnTNc/2/P6s8Msvkzlv97KU3u+/5OP8BFMax+rMqZdKWSWSm4aNclmpuxh2NLOO5laPqDKLJgx5qgvUXREG7xef73nRWbQEPP5KEqOorYJRhW6YhZrM9FnyPWEDt/DDsDv66T8dQKBqdvAz2B2LKtxxs/N4LqQPwAs06IZoheorAV/VxcYd9mttEE/0px83V3nOxWXNw= Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 X-Archived-By: Thank you for your contribution! Sashiko AI review found 7 potential issues to consider: - [Medium] mm: VM_WARN_ON_FOLIO() in deferred_split_folio() from zero_huge_pmd_range() - [Medium] mm: zero_huge_pmd_range() redirties lazyfree THPs and cancels MADV_FREE - [Medium] mm: touch_pmd() in zero_huge_pmd_range() corrupts x86 shadow stack PMDs - [Medium] mm: race in zero_huge_pmd_range() drops underused THPs from split queue - [Medium] mm: missing cache synchronization after zeroing in zero_huge_pmd_range() - [Medium] mm: zero_huge_pmd_range() fails to queue swapcache THP on deferred split list - [Medium] mm:huge_memory: reclaim and lazyfree regressions in zero_huge_pmd_range() -- commit 68aeca962725b589429831991dcbf23772370b18 Author: Alexandre Ghiti mm: madvise: don't split a PMD on partial THP MADV_DONTNEED This commit zeroes partial MADV_DONTNEED ranges of exclusive PMD-mapped anonymous THPs in place and queues the THP on the deferred split list instead of splitting the PMD immediately. > diff --git a/mm/huge_memory.c b/mm/huge_memory.c > index 4fc888ddfb69f..121da93971941 100644 > --- a/mm/huge_memory.c > +++ b/mm/huge_memory.c > @@ -2623,6 +2623,75 @@ bool zap_huge_pmd(struct mmu_gather *tlb, struct v= m_area_struct *vma, > return true; > } > =20 > +/* > + * MADV_DONTNEED on [addr, end), part of a PMD-mapped THP: zero it in pl= ace > + * instead of splitting the PMD, and queue the THP for the deferred split > + * shrinker, which frees the zeroed pages once the THP is underused. Ret= urn > + * false if the caller must split the PMD and zap the range instead. > + */ > +bool zero_huge_pmd_range(struct vm_area_struct *vma, pmd_t *pmd, > + unsigned long addr, unsigned long end) > +{ > + unsigned long haddr =3D addr & HPAGE_PMD_MASK; > + struct folio *folio; > + spinlock_t *ptl; > + pmd_t orig_pmd; > + bool ret; > + > + /* With max_ptes_none at 511, no THP is ever underused. */ > + if (!READ_ONCE(split_underused_thp) || > + khugepaged_max_ptes_none =3D=3D HPAGE_PMD_NR - 1) > + return false; > + > + if ((vma->vm_flags & VM_MTE) || userfaultfd_armed(vma) || > + mm_forbids_zeropage(vma->vm_mm)) > + return false; > + > + ptl =3D pmd_trans_huge_lock(pmd, vma); > + if (!ptl) > + return false; > + orig_pmd =3D pmdp_get(pmd); > + if (is_huge_zero_pmd(orig_pmd)) { > + spin_unlock(ptl); > + return true; > + } > + > + folio =3D pmd_present(orig_pmd) ? > + vm_normal_folio_pmd(vma, haddr, orig_pmd) : NULL; > + if (!folio || !folio_test_anon(folio) || > + !PageAnonExclusive(&folio->page) || folio_test_mlocked(folio) || > + folio_contain_hwpoisoned_page(folio) || > + folio_ref_count(folio) !=3D folio_expected_ref_count(folio) || > + !folio_trylock(folio)) { > + spin_unlock(ptl); > + return false; > + } [Severity: Medium] Can a swapcache THP pass this check and then fail to be queued by deferred_split_folio()? folio_expected_ref_count() includes folio_test_swapcache(folio) << order, so an exclusive PMD-mapped THP in the swapcache (for instance, after shrink_folio_list() allocated swap and try_to_unmap_one() stopped on a concurrent VMA_LOCKONFAULT walk before splitting the PMD) is accepted here and zeroed in place. However, deferred_split_folio() returns immediately when folio_test_swapcache(folio) is true: mm/huge_memory.c:deferred_split_folio() { ... if (folio_test_swapcache(folio)) return; ... } Because zero_huge_pmd_range() neither frees the swapcache via folio_free_swap() nor rejects swapcache folios, it returns true without queueing the THP on the deferred split list, so the underused THP shrinker will not reclaim the zeroed pages. > + folio_get(folio); > + spin_unlock(ptl); > + > + flush_cache_range(vma, addr, end); > + clear_contig_highpages(folio_page(folio, (addr - haddr) >> PAGE_SHIFT), > + addr, (end - addr) >> PAGE_SHIFT); [Severity: Medium] Does zeroing an already-mapped folio via clear_contig_highpages() without flush_dcache_folio() leave stale instruction or data cache lines on architectures with non-coherent I-caches or aliasing D-caches? clear_contig_highpages() writes zeroes through the kernel linear mapping, and flush_cache_range() is a no-op on architectures such as arm64 and riscv that track cache cleanliness via PG_dcache_clean. Without flush_dcache_folio(folio) after zeroing, PG_dcache_clean stays set and the zeroed lines are not cleaned to PoU or invalidated from the I-cache, so an executable VMA (or a JIT VMA later mprotected back to PROT_EXEC, where __sync_icache_dcache() skips sync_icache_aliases() when PG_dcache_clean is set) can execute stale instructions from before MADV_DONTNEED. > + /* > + * Mark the PMD dirty, and soft-dirty, as a write through it would. If > + * the PMD was split meanwhile, let the caller zap the range instead. > + */ > + ptl =3D pmd_lock(vma->vm_mm, pmd); > + orig_pmd =3D pmdp_get(pmd); > + ret =3D pmd_present(orig_pmd) && pmd_trans_huge(orig_pmd) && > + pmd_folio(orig_pmd) =3D=3D folio; > + if (ret) { > + touch_pmd(vma, haddr, pmd, true); [Severity: Medium] Does calling touch_pmd(vma, haddr, pmd, true) on a lazyfree THP redirty the entire PMD and cancel an earlier MADV_FREE? When MADV_FREE is called on a PMD-mapped THP, madvise_free_huge_pmd() clears PG_dirty, marks the PMD old and clean, and clears PG_swapbacked via folio_mark_lazyfree(). A subsequent partial MADV_DONTNEED now calls touch_pmd(..., true), setting pmd_mkdirty() and pmd_mkyoung() on the whole PMD. When reclaim later inspects or splits the PMD in __discard_anon_folio_pmd_locked() or __split_huge_pmd_locked() -> ttu_anon_lazyfree_folio(), pmd_dirty() is transferred to PG_dirty on the folio, which causes reclaim to restore PG_swapbacked and swap out the remaining pages instead of discarding them without I/O. [Severity: Medium] Can touch_pmd(vma, haddr, pmd, true) corrupt an x86 shadow stack PMD? On x86, a writable shadow stack PMD in a VM_SHADOW_STACK VMA is encoded with _PAGE_RW=3D0 and _PAGE_DIRTY=3D1. touch_pmd() calls pmd_mkdirty(entry) without maybe_pmd_mkwrite(entry, vma): arch/x86/include/asm/pgtable.h:pmd_mkdirty() { pmd =3D pmd_set_flags(pmd, _PAGE_DIRTY | _PAGE_SOFT_DIRTY); return pmd_mksaveddirty(pmd); } Because _PAGE_RW is 0, pmd_mksaveddirty() clears _PAGE_DIRTY and sets _PAGE_SAVED_DIRTY, and pmdp_set_access_flags() writes that entry into *pmd, turning the shadow stack PMD into a read-only non-shadow-stack PMD. [Severity: Medium] Does keeping the THP mapped with partially_mapped=3Dfalse and marking the PMD young cause shrink_folio_list() to swap out underused THPs in full instead of splitting them? In shrink_folio_list(), queued large folios are only split before folio_alloc_swap() when folio_test_partially_mapped(folio) is true: mm/vmscan.c:shrink_folio_list() { ... if (data_race(!list_empty(&folio->_deferred_list) && folio_test_partially_mapped(folio)) && split_folio_to_list(folio, folio_list)) goto activate_locked; } Because shrink_lruvec() runs shrink_folio_list() before shrink_slab() (and MADV_PAGEOUT / DAMON_PAGEOUT do not run shrink_slab() at all), an underused THP queued with partially_mapped=3Dfalse is not split before folio_alloc_swap() and is written to a 2MB swap cluster in full, including the MADV_DONTNEED-zeroed pages. In addition, touch_pmd() marks the PMD young, causing folio_check_references() to treat a cold THP being partially discarded as recently accessed. > + deferred_split_folio(folio, false); > + } [Severity: Medium] Can deferred_split_folio(folio, false) trigger VM_WARN_ON_FOLIO(folio_test_partially_mapped(folio), folio) here? deferred_split_folio() warns whenever partially_mapped is false and PG_partially_mapped is already set on the folio: mm/huge_memory.c:deferred_split_folio() { ... if (partially_mapped) { ... } else { /* partially mapped folios cannot become non-partially mapped */ VM_WARN_ON_FOLIO(folio_test_partially_mapped(folio), folio); } ... } PG_partially_mapped is sticky until the folio is split or freed. If a PMD-mapped THP was shared across fork(), the child split its PMD and unmapped some PTEs, and try_to_migrate() temporarily converted the parent's PMD to a migration entry, __folio_remove_rmap() saw the last PMD unmapped while fewer than HPAGE_PMD_NR PTEs remained in the child and called deferred_split_folio(folio, true), setting PG_partially_mapped (which migrate_folio_move() also copies to the destination folio). Once the child exits and a write fault in do_huge_pmd_wp_page() restores PageAnonExclusive on the parent's PMD, a partial MADV_DONTNEED reaches deferred_split_folio(folio, false) and hits the warning. > + spin_unlock(ptl); > + > + folio_unlock(folio); > + folio_put(folio); > + return ret; > +} [Severity: Medium] Can a concurrent deferred_split_scan() run between folio_unlock() and folio_put(), fail split_folio() because of the extra reference, and drop the underused THP from the split queue? CPU0 CPU1 zero_huge_pmd_range() folio_get(folio) ... deferred_split_folio(folio, false) spin_unlock(ptl) folio_unlock(folio) deferred_split_scan() thp_underused(folio) -> true folio_trylock(folio) split_folio(folio) -> -EAGAIN folio_unlock(folio) /* !partially_mapped: not requeue= d */ folio_put(folio) Because deferred_split_scan() only requeues folios after a failed split_folio() when folio_test_partially_mapped(folio) is true, the underused THP is removed from deferred_split_lru and its zeroed pages are not reclaimed. Dropping the temporary folio reference before folio_unlock() (or queueing after dropping the extra reference) would close this window. --=20 Sashiko AI review =C2=B7 https://sashiko.dev/#/patchset/20261202102818.1317= 130-1-alex@ghiti.fr?part=3D1