From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) (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 C42BF4028DE for ; Tue, 9 Jun 2026 11:46:16 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.129.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781005578; cv=none; b=NCmACSfCiA/DxUsyqu3X3hSfz6sDSGHQ2tqW8Y4JO+V/+4gnXh/1b7WHWllsUPUW2UuPrrD2S8/0D3Jy0TTe+24ZuSlwivGNaPEDoBcLQIwy1GaLFT5SSIn4K7MMQ/IB2NfeSCZDL9jwDQlIRRVx51gnqylaKwVC8/Gdk2C4SPM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781005578; c=relaxed/simple; bh=1lbcWab7GwIYoBT+eUrh9kDucFZ2dw98N/H5VM/sR9k=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=CYR+2p9yl2gMhDXTl9Tn/+Yqy05qEVgbeJ5DHowAUBJwOmnxFRbQv2vgkRmIVKjsxLJPEIq572l/eok27+I9gT0LN0DJ/sbNVQ1HTB18fo7rMZyDBdCbP/1c04SRdrFbvJE5oXVMzJJyoJLEKNnnK5hU2/A+IiMbfjS8bNbt0h4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=ejEjuJSK; arc=none smtp.client-ip=170.10.129.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ejEjuJSK" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1781005575; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=ObOhARDnYoUDbb8i2n+rNUP4GwUq2eJPlLo8BLvhF8w=; b=ejEjuJSK1NjIzUoMdjJMnPumflmyZ2KPRYCsdmp2GFi5SrAMNQpfQg33lm+ij7NBuO0UmX gPQzlylYGLrYvgRUnWBtzU/C7lwvDpASXQdvd8jyuvPm10gb+VRoEo/4EfSYJhC9i/X0Ro 3u4WJ0ipDMOJ4HrkK8GBtX1EamdGv0c= Received: from mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-349-FQy-Xck9MNmZwWsqXVTlww-1; Tue, 09 Jun 2026 07:46:12 -0400 X-MC-Unique: FQy-Xck9MNmZwWsqXVTlww-1 X-Mimecast-MFC-AGG-ID: FQy-Xck9MNmZwWsqXVTlww_1781005569 Received: from mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.17]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-03.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id C943B19540C1; Tue, 9 Jun 2026 11:46:08 +0000 (UTC) Received: from p1.redhat.com (unknown [10.44.22.33]) by mx-prod-int-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 24004195419E; Tue, 9 Jun 2026 11:45:58 +0000 (UTC) From: Nico Pache To: linux-kernel@vger.kernel.org, linux-mm@kvack.org, Usama Arif , Yu Zhao Cc: aarcange@redhat.com, Nico Pache , Andrew Morton , David Hildenbrand , Xu Xin , Chengming Zhou , Lorenzo Stoakes , Zi Yan , Baolin Wang , "Liam R. Howlett" , Ryan Roberts , Dev Jain , Barry Song , Lance Yang , Matthew Brost , Joshua Hahn , Rakie Kim , Byungchul Park , Gregory Price , Ying Huang , Alistair Popple Subject: [PATCH mm-unstable v1 2/3] mm/migrate.c: Prevent folio splitting from interacting with KSM Date: Tue, 9 Jun 2026 05:46:14 -0600 Message-ID: <20260609114619.144416-3-npache@redhat.com> In-Reply-To: <20260609114619.144416-1-npache@redhat.com> References: <20260609114619.144416-1-npache@redhat.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Scanned-By: MIMEDefang 3.0 on 10.30.177.17 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. Fix this by restricting TTU_USE_SHARED_ZEROPAGE being set in the case that KSM is running and the VMA has VM_MERGEABLE. Fixes: b1f202060afe ("mm: remap unused subpages to shared zeropage when splitting isolated thp") Signed-off-by: Nico Pache --- mm/migrate.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/mm/migrate.c b/mm/migrate.c index d9b23909d716..f410f972fc5e 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -304,6 +304,15 @@ static bool try_to_map_unused_to_zeropage(struct page_vma_mapped_walk *pvmw, if (PageCompound(page) || PageHWPoison(page)) return false; + /* + * Let KSM handle the zero-filled page deduplication according to its + * own rate limit (pages_to_scan) and policy (use_zero_pages). Without + * this, a KSM-triggered THP split would remap all zero-filled subpages + * to the shared zeropage as a side effect. + */ + if (ksm_is_running() && (pvmw->vma->vm_flags & VM_MERGEABLE)) + 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); -- 2.54.0