From nobody Thu Oct 2 09:17:07 2025 Received: from fhigh-a4-smtp.messagingengine.com (fhigh-a4-smtp.messagingengine.com [103.168.172.155]) (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 CB66C302179 for ; Thu, 18 Sep 2025 11:22:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.155 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758194527; cv=none; b=DRjGhoTFVlvA3VaGNtTajkwZU5GAnvq8/TPMZOh13Xm445UD23GStk1JT9m2p5KEks7YAtmLh3gKdcwljDyI/XDlIZTBc3VqZlaRiir6nvTPRYdkFW1HyPDQTCgEs+INfI20uBUsCTWZfkyAoqU9SlGPJ8XqsrU+I2z7J9ZxUaM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758194527; c=relaxed/simple; bh=XChvTvuq/7NeECGDeUSWMtBEaCBZNCfi/y1ZkjW4cx4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=EvRk9kibQw2Sn2i7rT8jmvv4dumiGow/Cx1Ew9lk1wX0q45JNuAAL/SpskC98WJHTZKM5HrXFQmgGMw0dDwJdLFyqopaNYrEm9iLPjIEkJBxZHgLO+gMsC4FpxvHnZHcc5uEc9xXs1gRVd7DS5D0ioD6ByPlG6DxO2cEOA3XeB4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=shutemov.name; spf=pass smtp.mailfrom=shutemov.name; dkim=pass (2048-bit key) header.d=shutemov.name header.i=@shutemov.name header.b=BjlnSC4I; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=O/N6HGa7; arc=none smtp.client-ip=103.168.172.155 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=shutemov.name Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=shutemov.name Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=shutemov.name header.i=@shutemov.name header.b="BjlnSC4I"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="O/N6HGa7" Received: from phl-compute-02.internal (phl-compute-02.internal [10.202.2.42]) by mailfhigh.phl.internal (Postfix) with ESMTP id BBA7C1400189; Thu, 18 Sep 2025 07:22:04 -0400 (EDT) Received: from phl-mailfrontend-01 ([10.202.2.162]) by phl-compute-02.internal (MEProxy); Thu, 18 Sep 2025 07:22:04 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shutemov.name; h=cc:cc:content-transfer-encoding:content-type:date:date:from :from:in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm3; t=1758194524; x= 1758280924; bh=TC0qa4dJ8ZfxpD8eOHAvcq6Kh4/gpC+GYroIRc1A+zs=; b=B jlnSC4I5QYishrFKnKK9wwyolspBZIm5+esjfdskwA4hWQHwKzW5aWOl8JP9pi6o yQw6Gwiku0EOklDIv2OfURaJ5tTTsAlzCXHK5oOnX+bdNpfur0ComdoAbdBkB3HG p0uImfDZSugFVh0wSpnz6EYcbZRQ4vO5BLMyP2ZbPI6bHY2WTH6ecO/X2PT1AI8K cM1QemB8AlPgdUZG2JpTouBOWzMzpdb/4xEspMEnWzlc/byyYb/P/h58xvzh2NnU CCGr5eZ6v35UYgc6OHniB+SSNfDP0lIWIvlKLTaPlCuseQnX+td5ErNl3n4YXCvM T/p8EHOxOD+j8wqnp+NfQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; t=1758194524; x=1758280924; bh=T C0qa4dJ8ZfxpD8eOHAvcq6Kh4/gpC+GYroIRc1A+zs=; b=O/N6HGa7hAuX5edV4 bGnn2ILo8TPFaev6z6I0mwCtmOAEfdX+3nYBwWA3z5IH3xVB/aSZGBp56/3OVPVE IPrSAn6AaMXOeG6v2Wl5c5PniiPDfff4Nfqxpg31U1TRrUGKAsSsLS7+pOJMSWsW 7gb/gSJvxOV7fkmUOKt2Y2HcTrN4+JGCI9u0S2+hwVUjeFwQ7/V4GKNxBkqisF6d 8RIrxJjUv7bt+5f3kX8vi7lJPLyk4Vo7gdZjs+Kt5y6Oaze9Ba/5V9Brgt+rj+O9 yMkpKvheyoX5SURB29+fmqC0gJ6cweLNZ36iUytieeZf2dukPzJagYKnMqBuo87f NAGUw== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdeggdegiedvtdcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjug hrpefhvfevufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpehkihhrihhllhes shhhuhhtvghmohhvrdhnrghmvgenucggtffrrghtthgvrhhnpeejgeevheeuieefheekle eiheefkeekieevteefudefveehhfevffeludejgfehueenucevlhhushhtvghrufhiiigv pedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehkihhrihhllhesshhhuhhtvghmohhvrd hnrghmvgdpnhgspghrtghpthhtohepudejpdhmohguvgepshhmthhpohhuthdprhgtphht thhopegrkhhpmheslhhinhhugidqfhhouhhnuggrthhiohhnrdhorhhgpdhrtghpthhtoh epuggrvhhiugesrhgvughhrghtrdgtohhmpdhrtghpthhtohephhhughhhugesghhoohhg lhgvrdgtohhmpdhrtghpthhtohepfihilhhlhiesihhnfhhrrgguvggrugdrohhrghdprh gtphhtthhopehlohhrvghniihordhsthhorghkvghssehorhgrtghlvgdrtghomhdprhgt phhtthhopehlihgrmhdrhhhofihlvghtthesohhrrggtlhgvrdgtohhmpdhrtghpthhtoh epvhgsrggskhgrsehsuhhsvgdrtgiipdhrtghpthhtoheprhhpphhtsehkvghrnhgvlhdr ohhrghdprhgtphhtthhopehsuhhrvghnsgesghhoohhglhgvrdgtohhm X-ME-Proxy: Feedback-ID: ie3994620:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 18 Sep 2025 07:22:03 -0400 (EDT) From: kirill@shutemov.name To: Andrew Morton , David Hildenbrand , Hugh Dickins , Matthew Wilcox Cc: Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Rik van Riel , Harry Yoo , Johannes Weiner , Shakeel Butt , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Kiryl Shutsemau Subject: [PATCH 1/2] mm/fault: Try to map the entire file folio in finish_fault() Date: Thu, 18 Sep 2025 12:21:56 +0100 Message-ID: <20250918112157.410172-2-kirill@shutemov.name> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20250918112157.410172-1-kirill@shutemov.name> References: <20250918112157.410172-1-kirill@shutemov.name> 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" From: Kiryl Shutsemau The finish_fault() function uses per-page fault for file folios. This only occurs for file folios smaller than PMD_SIZE. The comment suggests that this approach prevents RSS inflation. However, it only prevents RSS accounting. The folio is still mapped to the process, and the fact that it is mapped by a single PTE does not affect memory pressure. Additionally, the kernel's ability to map large folios as PMD if they are large enough does not support this argument. When possible, map large folios in one shot. This reduces the number of minor page faults and allows for TLB coalescing. Mapping large folios at once will allow the rmap code to mlock it on add, as it will recognize that it is fully mapped and mlocking is safe. Signed-off-by: Kiryl Shutsemau --- mm/memory.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 0ba4f6b71847..812a7d9f6531 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -5386,13 +5386,8 @@ vm_fault_t finish_fault(struct vm_fault *vmf) =20 nr_pages =3D folio_nr_pages(folio); =20 - /* - * Using per-page fault to maintain the uffd semantics, and same - * approach also applies to non shmem/tmpfs faults to avoid - * inflating the RSS of the process. - */ - if (!vma_is_shmem(vma) || unlikely(userfaultfd_armed(vma)) || - unlikely(needs_fallback)) { + /* Using per-page fault to maintain the uffd semantics */ + if (unlikely(userfaultfd_armed(vma)) || unlikely(needs_fallback)) { nr_pages =3D 1; } else if (nr_pages > 1) { pgoff_t idx =3D folio_page_idx(folio, page); --=20 2.50.1 From nobody Thu Oct 2 09:17:07 2025 Received: from fout-a6-smtp.messagingengine.com (fout-a6-smtp.messagingengine.com [103.168.172.149]) (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 9695D3043AC for ; Thu, 18 Sep 2025 11:22:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=103.168.172.149 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758194529; cv=none; b=uTh7IGiCz9NT2V8dNowbJmyN/j9bBSYJVinGqJKMuByl42o0GOjRZV/oJM5K53oRx8srrW4m9OCblaYYVlC4A1vXrGFzmxIduZua8qQ0XaqW4Asu7hXh23y48wiZ/35DhTCFQZMAR81UhwihD34I91fs1Rl/zhP/3OSVkjtTMv8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1758194529; c=relaxed/simple; bh=DsJtSVR2FAiVms+3NUAosA4r7xeLm1fdPX2FqIYU1H8=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Vyx1px7qMVXDLZbq03nYfdo69HtzZcS2L1hTiF1MB0iB6gwHRpnbCyvxQSv7kMyTOBlbpbth0kWYnhEYHJn8Aj07cOkSqhhnkSYgJDwF+z03BCXj8PDw9t5//udSMcgZCaqNemxXFeP24/K7HhMrfnYWCFN8Xex+sTM/LbP4++Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=shutemov.name; spf=pass smtp.mailfrom=shutemov.name; dkim=pass (2048-bit key) header.d=shutemov.name header.i=@shutemov.name header.b=HPN7TsEa; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b=MnNJsRCt; arc=none smtp.client-ip=103.168.172.149 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=shutemov.name Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=shutemov.name Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=shutemov.name header.i=@shutemov.name header.b="HPN7TsEa"; dkim=pass (2048-bit key) header.d=messagingengine.com header.i=@messagingengine.com header.b="MnNJsRCt" Received: from phl-compute-09.internal (phl-compute-09.internal [10.202.2.49]) by mailfout.phl.internal (Postfix) with ESMTP id ABA85EC027C; Thu, 18 Sep 2025 07:22:06 -0400 (EDT) Received: from phl-mailfrontend-02 ([10.202.2.163]) by phl-compute-09.internal (MEProxy); Thu, 18 Sep 2025 07:22:06 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shutemov.name; h=cc:cc:content-transfer-encoding:content-type:date:date:from :from:in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to; s=fm3; t=1758194526; x= 1758280926; bh=JF8klN+Ypxvh4q7BTrSxqIYNo6Zuu1c2pmf4rhg8wJA=; b=H PN7TsEadiv1A6p5PKtA+HdyGDKp2K5fYXJUx8wc+7Wge1zjaf50Op5oByjV+U9dj g1qqVdEVrltShIqs7AQ0nYKDyWVJvpbpnJ/SfOsHgsxLJycqeTHG6UHFWphBr6sv A74Z1//gyXP+zUfZmOFyMepjTGYcZWzlsMYRcVMMpYEeDBxrIHEvfmvAfLr238dV mTErZ932q/+LYJXiyXXyI1Y5sTUIAwGsejwYt42AAozbcwYR0zi5zlIno7zmCM/A Zht6+CLq5tYNHTgCBJOvZB/1FaSwzy07G32w4eKsdYlTj1cxVNtYRZ+8i1S5pjx0 UWpC0R8vBBlGq/lBLytAg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:subject:subject:to:to:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm1; t=1758194526; x=1758280926; bh=J F8klN+Ypxvh4q7BTrSxqIYNo6Zuu1c2pmf4rhg8wJA=; b=MnNJsRCtFpCQ8/0Cy wat4pb/PirvlQfkhl7MaLNQ38toBIi2WIqACffDnK1jbbVGLtdV/bI+r+OiNEAje 8iXDazoJ5nGTXuG+OxtJWXkkDuJepEamwUaT1SsLpcWNr1FSdvcoPBP4nv0DCwQi Kjq3hOhdUcHY7ThJclrnGCb1HYnScvL8Joa3p5EODCK1xOST3rRFltej+i0pnU4a WpM5NPW5np7cxrZaIY5JEOUraHfHS82OVb4ef+MvFJvzvqy/OGkAiIHglBuiexeV tvxgNGUdfni1VWmCpoxS4T1EjO7fTqWr3PMic9/nUvlO5HiLAoi4xOsSWE9Qy7+B P8tGA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeffedrtdeggdegiedulecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpuffrtefokffrpgfnqfghnecuuegr ihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjug hrpefhvfevufffkffojghfggfgsedtkeertdertddtnecuhfhrohhmpehkihhrihhllhes shhhuhhtvghmohhvrdhnrghmvgenucggtffrrghtthgvrhhnpeejgeevheeuieefheekle eiheefkeekieevteefudefveehhfevffeludejgfehueenucevlhhushhtvghrufhiiigv pedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpehkihhrihhllhesshhhuhhtvghmohhvrd hnrghmvgdpnhgspghrtghpthhtohepudejpdhmohguvgepshhmthhpohhuthdprhgtphht thhopegrkhhpmheslhhinhhugidqfhhouhhnuggrthhiohhnrdhorhhgpdhrtghpthhtoh epuggrvhhiugesrhgvughhrghtrdgtohhmpdhrtghpthhtohephhhughhhugesghhoohhg lhgvrdgtohhmpdhrtghpthhtohepfihilhhlhiesihhnfhhrrgguvggrugdrohhrghdprh gtphhtthhopehlohhrvghniihordhsthhorghkvghssehorhgrtghlvgdrtghomhdprhgt phhtthhopehlihgrmhdrhhhofihlvghtthesohhrrggtlhgvrdgtohhmpdhrtghpthhtoh epvhgsrggskhgrsehsuhhsvgdrtgiipdhrtghpthhtoheprhhpphhtsehkvghrnhgvlhdr ohhrghdprhgtphhtthhopehsuhhrvghnsgesghhoohhglhgvrdgtohhm X-ME-Proxy: Feedback-ID: ie3994620:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 18 Sep 2025 07:22:05 -0400 (EDT) From: kirill@shutemov.name To: Andrew Morton , David Hildenbrand , Hugh Dickins , Matthew Wilcox Cc: Lorenzo Stoakes , "Liam R. Howlett" , Vlastimil Babka , Mike Rapoport , Suren Baghdasaryan , Michal Hocko , Rik van Riel , Harry Yoo , Johannes Weiner , Shakeel Butt , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Kiryl Shutsemau Subject: [PATCH 2/2] mm/rmap: Improve mlock tracking for large folios Date: Thu, 18 Sep 2025 12:21:57 +0100 Message-ID: <20250918112157.410172-3-kirill@shutemov.name> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20250918112157.410172-1-kirill@shutemov.name> References: <20250918112157.410172-1-kirill@shutemov.name> 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" From: Kiryl Shutsemau The kernel currently does not mlock large folios when adding them to rmap, stating that it is difficult to confirm that the folio is fully mapped and safe to mlock it. However, nowadays the caller passes a number of pages of the folio that are getting mapped, making it easy to check if the entire folio is mapped to the VMA. mlock the folio on rmap if it is fully mapped to the VMA. Signed-off-by: Kiryl Shutsemau Acked-by: David Hildenbrand Acked-by: Johannes Weiner Acked-by: Shakeel Butt Reviewed-by: Lorenzo Stoakes --- mm/rmap.c | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/mm/rmap.c b/mm/rmap.c index 568198e9efc2..ca8d4ef42c2d 100644 --- a/mm/rmap.c +++ b/mm/rmap.c @@ -1478,13 +1478,8 @@ static __always_inline void __folio_add_anon_rmap(st= ruct folio *folio, PageAnonExclusive(cur_page), folio); } =20 - /* - * For large folio, only mlock it if it's fully mapped to VMA. It's - * not easy to check whether the large folio is fully mapped to VMA - * here. Only mlock normal 4K folio and leave page reclaim to handle - * large folio. - */ - if (!folio_test_large(folio)) + /* Only mlock it if the folio is fully mapped to the VMA */ + if (folio_nr_pages(folio) =3D=3D nr_pages) mlock_vma_folio(folio, vma); } =20 @@ -1620,8 +1615,8 @@ static __always_inline void __folio_add_file_rmap(str= uct folio *folio, nr =3D __folio_add_rmap(folio, page, nr_pages, vma, level, &nr_pmdmapped); __folio_mod_stat(folio, nr, nr_pmdmapped); =20 - /* See comments in folio_add_anon_rmap_*() */ - if (!folio_test_large(folio)) + /* Only mlock it if the folio is fully mapped to the VMA */ + if (folio_nr_pages(folio) =3D=3D nr_pages) mlock_vma_folio(folio, vma); } =20 --=20 2.50.1