From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.146 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796439461; b=w0cLizOfJQscdx2Q3wvHiGagfJLFvlrNUoDt5nDA2iOArrBQ5vw+mvcJ14epYuLljTVd axzayvf/+dcoX+UTD8uX8b7DloPUuPCCMk4YMapylVXRl4hlDx2F/V0VmFG19+NLlMXYf /DKtzP9peBcSt5nTg3dtgIJJ/PVaDQLO4SIG3qqrjoZ3Spn0J99ulIXGH6xDQn7lT7qBe YVpP3ftNrV0jABX4dG3MkvzgC7Qhrgg9kqUb9FCV/ydrBz4QNF9IZ0fnkv+FwM95Gaf3Q 9mTW/5tzmujFn041wmMPFs5cDhH+ib88IqkaVwYZKuvulADWhFV9HnjCPMptnBeRXEw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796439461; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:Date:Subject:MIME-Version: Content-Type:Content-Transfer-Encoding:Message-Id:X-B4-Tracking: X-Change-ID:To:Cc:X-Mailer; bh=rCyx9zi5wpt5ranmeCTT11K7mm3HZ7ghmbOa3eP8W5k=; b=gLWZoltfgROuLjRxrXVa1AWiHmDhyIGX/Cpmy/ML6cSO0csXwSI9H7UyUmzDbt1Ct67G tB6nWKC3Ci9bF9Gi9zg9aH2wkflkv+DVBQVuzy7/SzDpdZ1KLufWr13kNZht94D60FzHk PUEIp1o24/SfPBtW/2qro4KCOcCrfqTMprXd7rFU3YN21RjEPd6Ky9/9IvP2z/JyZ10ho 0HdwqiWn6ih9xtVhJ2LGicCgNZIKZqYGItPsgNVnTVRK/BkFdtQW6QInSBgJUgRm3ILGO mfEb3D4VZ3I6qAiZ+H9gBTMSyXGfTQviaqlFzznBmg4YIaEKuzLgfMJN+cTJSs6uvcg== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=OmczG+6G; arc=none smtp.remote-ip=95.215.58.146 Received: from mta1.migadu.com (out-146.mta1.migadu.com [95.215.58.146]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0DF87606A322 for ; Sat, 5 Dec 2026 02:57:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 0DF87606A322 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key, unprotected) header.d=linux.dev header.i=@linux.dev header.a=rsa-sha256 header.s=key1 header.b=OmczG+6G X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=+ty/2KPxf+gZ5hwFXStyAWzYmSGpLgNWcCXJbEa2ijY=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796439457; v=1; x=1797044257; b=OmczG+6GzNztvetTxbnE1uxsOHfocfVD5PZTJwSocwL1a848WiWGvFw7pxtDoO5CsOiu5Wg2 mUkJr+npPFsxoLPETgstxJk+CwcInGv1UbuW7mKg3+4fva0G6EMGT5juGQjR794iNh91NeI/eLr MYdWTJUv33gBYCm7zE9tAGuU= X-Envelope-To: linux-kernel@vger.kernel.org Received: by mta12.migadu.com with ESMTPS id 12e9bacece4fbb37; Sat, 05 Dec 2026 02:57:37 +0000 X-Mizu-Trace-ID: 12e9bacece4fbb37 X-Migadu-Flow: FLOW_OUT From: "Nico Pache (Red Hat)" Date: Fri, 04 Dec 2026 19:57:24 -0700 Subject: [PATCH v4] mm/ksm: prevent zero-page remapping during ksmd-triggered THP splits X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 7bit Message-Id: <20261204-b4-ksm_fixv2-v4-1-c5053d804e00@linux.dev> X-B4-Tracking: v=1; b=H4sIAAAAAAAC/3WNywrCMBBFf0WyNpJMY9K68j9EpGknNj5aSdpQK f13k7pQBGE2l3vPmYl4dBY92a0m4jBYb7s2BrFekaop2zNSW8dMgIHkDLZUC3r195OxYwAK2jA JWEAhJInIw2EsFt3h+M5+0Bes+uRIi8b6vnPP5V+AtPujjsdoriUvayYKbvj+Ztth3NQYSFKH7 ANzgB84o5walWOplCqUkN/wPM8vy7H9e/cAAAA= X-Change-ID: 20261025-b4-ksm_fixv2-2bf062e92946 To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Andrew Morton , David Hildenbrand , Zi Yan , Baolin Wang , "Liam R. Howlett" , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Usama Arif , Kiryl Shutsemau , Yu Zhao , Xu Xin , Chengming Zhou , Matthew Brost , Joshua Hahn , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple , Usama Arif , aarcange@redhat.com, "Nico Pache (Red Hat)" X-Mailer: b4 0.17-dev Since commit b1f202060afe ("mm: remap unused subpages to shared zeropage when splitting isolated thp"), splitting an anonymous THP remaps all zero-filled subpages to the shared zeropage via TTU_USE_SHARED_ZEROPAGE. This flag is set unconditionally for every anonymous folio split, including splits triggered by KSM. When KSM is enabled with THP=always, this causes two regressions: 1. use_zero_pages=1: KSM calls try_to_merge_one_page() which triggers split_huge_page(). The split remaps all 512 zero-filled subpages to the shared zeropage at once, freeing the entire 2MB THP when KSM only intended to process a single 4KB page. This bypasses KSM's pages_to_scan rate limiting, causing ~1GB to be freed almost instantly. 2. use_zero_pages=0: The same split side-effect occurs through the stable/unstable tree merge paths. Each pages_to_scan iteration triggers an expensive split_huge_page() that silently frees 2MB, while the scanner wastes cycles on tree searches for zero-filled pages that were already freed as a side-effect. Introduce current_is_ksmd(), to check if the current task is the KSM thread. Use it in try_to_map_unused_to_zeropage() to skip zero-page remapping when the split is triggered by ksmd. Allow remapping during reclaim running in ksmd's context. Fixes: b1f202060afe ("mm: remap unused subpages to shared zeropage when splitting isolated thp") Signed-off-by: Nico Pache (Red Hat) --- This patch fixes a bug in KSM where the pages_to_scan limit is bypassed and the policy (use_zero_pages) is ignored. Commit b1f202060afe ("mm: remap unused subpages to shared zeropage when splitting isolated thp") added zero-page remapping when an anonymous folio is split, including splits triggered by KSM. Skip zero-page remapping when ksmd triggers the split, while allowing remapping during reclaim running in ksmd's context. Changes in v4: - wrap the check in unlikely() V3: https://lore.kernel.org/all/20261122-b4-ksm_fixv2-v3-1-f78ea7779746@linux.dev V2: https://lore.kernel.org/all/20261025-b4-ksm_fixv2-v2-0-8b61ad0491f1@linux.dev V1: https://lore.kernel.org/all/20260609114619.144416-1-npache@redhat.com/ RFC: https://lore.kernel.org/all/20260508170509.640851-1-npache@redhat.com/ --- include/linux/ksm.h | 6 ++++++ mm/ksm.c | 5 +++++ mm/migrate.c | 8 ++++++++ 3 files changed, 19 insertions(+) diff --git a/include/linux/ksm.h b/include/linux/ksm.h index a0d358a1feb6..23c8e9f60118 100644 --- a/include/linux/ksm.h +++ b/include/linux/ksm.h @@ -17,6 +17,7 @@ #ifdef CONFIG_KSM int ksm_madvise(struct vm_area_struct *vma, unsigned long start, unsigned long end, int advice, vm_flags_t *vm_flags); +bool current_is_ksmd(void); vma_flags_t ksm_vma_flags(struct mm_struct *mm, const struct file *file, vma_flags_t vma_flags); int ksm_enable_merge_any(struct mm_struct *mm); @@ -144,6 +145,11 @@ static inline int ksm_madvise(struct vm_area_struct *vma, unsigned long start, return 0; } +static inline bool current_is_ksmd(void) +{ + return false; +} + static inline struct folio *ksm_might_need_to_copy(struct folio *folio, struct page *page, struct vm_area_struct *vma, unsigned long addr) { diff --git a/mm/ksm.c b/mm/ksm.c index d24b129932bf..60656b05419a 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2873,6 +2873,11 @@ static int ksm_scan_thread(void *nothing) return 0; } +bool current_is_ksmd(void) +{ + return kthread_func(current) == ksm_scan_thread; +} + static bool __ksm_should_add_vma(const struct file *file, vma_flags_t vma_flags) { if (vma_flags_test(&vma_flags, VMA_MERGEABLE_BIT)) diff --git a/mm/migrate.c b/mm/migrate.c index 7841968dd1f5..29cbb8aecc01 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -305,6 +305,14 @@ static bool try_to_map_unused_to_zeropage(struct page_vma_mapped_walk *pvmw, if (PageCompound(page) || PageHWPoison(page)) return false; + /* + * When a split is triggered by KSM, let KSM take care of scanning for + * and deduplicating the shared zeropage. Reclaim in the ksmd context + * should still be able to reclaim zero-filled pages. + */ + if (unlikely(current_is_ksmd() && !current->reclaim_state)) + return false; + VM_BUG_ON_PAGE(!PageAnon(page), page); VM_BUG_ON_PAGE(!PageLocked(page), page); VM_BUG_ON_PAGE(pte_present(old_pte), page); --- base-commit: 2076c0591c1f232f3c1e6ce047f189bcf59610b4 change-id: 20261025-b4-ksm_fixv2-2bf062e92946 Best regards, -- Nico Pache (Red Hat)