From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=95.215.58.164 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796354180; b=f1GNjZof3wKjZV05gW5NpV/X1XYkrbSPcIivOOIyT0qisn0vCliJtjETfnN2SmCZsC7V LVlBtEj9vhYWa3QaBm5i31XE8yeTkCn/eUvM6qHiZGwCZOgapMFtsBufM8zHqa1XKwRby wpHT4lYnVvSmiSY06yKvFMvxmgWPHcDadAmsmEqgrcZaGCTolPnKQ/PrhrrJk21ay0PeT FLrtO94KlmGstnAor1rqkwbLI/A4AxklodWJ4x4bUk27FG5/vlqec3pQKrqe92KyKtjdw W2b/yKWTadplSxAxKQMxtsiZYzI8MPsHx8TKXNNpMrgwUsYDNBaZZ2w26rqFT5H5XOg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796354180; h=DMARC-Filter:X-Envelope-To:DKIM-Signature:X-Envelope-To:Received: X-Mizu-Trace-ID:X-Migadu-Flow:From:To:Cc:Subject:Date:Message-ID: X-Mailer:In-Reply-To:References:MIME-Version:Content-Type: Content-Transfer-Encoding; bh=mnioRUFfdv1kKStqgAfiEZ62noFga+Mn80U9zWXDVoQ=; b=mLXg8nkzYzHqwXNYNHzcBN6hK54Opc9F/YA9N2/SUnVPpQ9/My4j+II/MBRX8VxcrxFC Wc10TzX0NxS7h9pYNt6iKH9Fvxn6KYxVQmtQQuwKf7M457ghej61Oj4MEpQnozotlXkGe pavG3w8cqd8AYvv8y9dlC7r5pAMDPq7t71V/nxqZLLHoqbgGK4kn6vMhnLBbhHODBNMcN OZfsOJujHaOwo3QSBRfeyYOS3WvmZG2ORZ7NQqchjt+CN1hVbws2dS6+LgLwSjwsUVDGS cRNOmk/QMAIzvM0lwS8FFKAhXNd4ASkMad+oRJUNPLuwaavpfrJOW1dPXBEzOv7m9HQ== 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="HUaN2/53"; arc=none smtp.remote-ip=95.215.58.164 Received: from mta1.migadu.com (out-164.mta1.migadu.com [95.215.58.164]) (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 C8B8B606A322 for ; Fri, 4 Dec 2026 03:16:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org C8B8B606A322 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=HUaN2/53 X-Envelope-To: linux-kernel@vger.kernel.org DKIM-Signature: a=rsa-sha256; bh=4QM6xVpX+MTwBBGQv75CFVQudiYXVpdvnXWDg9JhaSs=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796354177; v=1; x=1796958977; b=HUaN2/53lCc2J/xEzBLUnJKT4/YRCX+ACeg0oCf+gNFNuIRvn/5rHYuVvzjYWlMC6nynCeak weF1E2/HXmiOaMTA1RZ5xExGSAFatwgfH51NcpgxFdN1Rmzs2luHfIRw63OVz8hRuS7QWC5DtCL ZE0xyCzYmeTJYO0tqpWfEwxU= X-Envelope-To: linux-kernel@vger.kernel.org Received: by smtp.migadu.com with ESMTPS id 124c5a4128913f7d; Fri, 04 Dec 2026 03:16:17 +0000 X-Mizu-Trace-ID: 124c5a4128913f7d X-Migadu-Flow: FLOW_OUT From: Lance Yang To: alex@ghiti.fr Cc: akpm@linux-foundation.org, david@kernel.org, ziy@nvidia.com, baolin.wang@linux.alibaba.com, liam@infradead.org, nico.pache@linux.dev, ryan.roberts@arm.com, dev.jain@arm.com, baohua@kernel.org, lance.yang@linux.dev, usama.arif@linux.dev, kas@kernel.org, vbabka@kernel.org, rppt@kernel.org, mhocko@suse.com, ljs@kernel.org, surenb@google.com, shakeel.butt@linux.dev, pfalcato@suse.de, jannh@google.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [RFC PATCH] mm: madvise: don't split a PMD on partial THP MADV_DONTNEED Date: Fri, 4 Dec 2026 11:16:11 +0800 Message-ID: <20261204031611.7827-1-lance.yang@linux.dev> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20261202102818.1317130-1-alex@ghiti.fr> References: <20261202102818.1317130-1-alex@ghiti.fr> 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: 8bit On Wed, Dec 02, 2026 at 11:28:10AM +0100, Alexandre Ghiti wrote: >MADV_DONTNEED on a range that covers only part of a PMD-mapped THP splits >the PMD and zaps the PTEs of the range. The THP itself is not freed: it >stays allocated and charged as a whole, now partially mapped, until the >deferred split shrinker splits it under memory pressure. Until then the >process has lost the PMD mapping, and the TLB reach that comes with it, for >no memory benefit. > >Besides, a THP is checked for being underused only once: it is queued on >the deferred split list when it is created, and the shrinker drops it from >the list for good once it has found it not underused. A long-lived THP >whose pages are freed later is never looked at again, so its unused memory >can't be reclaimed. > >When the underused THP shrinker can reclaim the range later, zero it in >place instead: it reads as zeroes, as MADV_DONTNEED requires, and the PMD >mapping stays. The THP is queued again on the deferred split list, so the >shrinker looks at it again. > >For an exclusive anonymous THP, MADV_DONTNEED on part of it thus no longer >leaves it partially mapped: the range reads as zeroes at once, and the >memory is given back when reclaim needs it, once the THP holds more than >max_ptes_none zeroed pages. Below that, the THP is not split even under >memory pressure, where the partially mapped THP used to be split: it keeps >up to max_ptes_none zeroed pages, the waste khugepaged already allows when >collapsing. > >The range is zapped as before when the zeroed pages could not be freed, >e.g. with max_ptes_none at 511 (the default), or when anyone else may see >the THP, e.g. after fork or through a GUP pin. Nice work (looked pretty good at first). >Results on a large production workload (THP=always, max_ptes_none=0, >allocator purging with MADV_DONTNEED), same allocator configuration before >and after this patch: > > THPs and memory change > ------------------------------------ ------ > THPs partially mapped -97% > memory mapped by THPs (AnonHugePages) +29% > underused THP splits +145% Emm ... some work may shift from workload threads to deferred split, I guess :D Afraid overall host CPU usage could go up even if workload p99 latency improves ... right? > PMD splits +10% > workload anonymous memory +0.2% > > Performance change > ------------------------------------ ------ > p50 latency -1.7% > p99 latency -2.3% > throughput within noise > dTLB misses per instruction -0.4% > IPC +0.2% And yeah, I understand why you want to keep PMD mappings intact. Some workloads may already rely on MADV_DONTNEED zapping mappings (a partial discard splits a PMD mapping into PTE mappings). FWIW, An opt-in flag for process_madvise() or a new madvise() advice might make more sense, just saying ... [...] Cheers, Lance