From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 62DE1C71136 for ; Wed, 18 Jun 2025 02:25:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E123C6B0088; Tue, 17 Jun 2025 22:25:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DC3126B0089; Tue, 17 Jun 2025 22:25:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CD8FC6B008A; Tue, 17 Jun 2025 22:25:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id BE4A76B0088 for ; Tue, 17 Jun 2025 22:25:46 -0400 (EDT) Received: from smtpin26.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 7052B1618E0 for ; Wed, 18 Jun 2025 02:25:46 +0000 (UTC) X-FDA: 83566930692.26.9BAF9AA Received: from out-177.mta0.migadu.com (out-177.mta0.migadu.com [91.218.175.177]) by imf17.hostedemail.com (Postfix) with ESMTP id 84B774000C for ; Wed, 18 Jun 2025 02:25:44 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="olPnJ/Ft"; spf=pass (imf17.hostedemail.com: domain of lance.yang@linux.dev designates 91.218.175.177 as permitted sender) smtp.mailfrom=lance.yang@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1750213544; a=rsa-sha256; cv=none; b=jjK6YxNY7c0DbCBlourCBcX0dQL65x5lunNECOASGRy6j4eagXlcoPYyMXLIyoGee/7HS4 6btDV+nKS++qGqq27PGr04JXDAa0Mj1mF80LgUHVjHf5TGRe7K5/KFFc0WFa3A3FSI24F7 CjXdoxFtwjX6jLwz0irdn4IQ4FSoFxU= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b="olPnJ/Ft"; spf=pass (imf17.hostedemail.com: domain of lance.yang@linux.dev designates 91.218.175.177 as permitted sender) smtp.mailfrom=lance.yang@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1750213544; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=WRnT8oSN8k3lo5CGyLRxRbzv9ULV2vj8IJVFOUE6h9A=; b=Irytj9pVUjreWt8D70mvNrkbysi+o1+hHn1Mt7blduUmpYRHRagBzfGGnQp5jlJmj/5YhZ m9UFYMCoQCtowDquTh2NRnbKdQeDJI778Tuk9Qg2D225f+WoVNgu+hku3zWI6jgXZHU3rC uChclYE+SADiN0rB9sINk+W0dKaZtk8= Message-ID: DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1750213542; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=WRnT8oSN8k3lo5CGyLRxRbzv9ULV2vj8IJVFOUE6h9A=; b=olPnJ/FtjhUAXcFuf0XSpr3qb4c5xrdg+M22XKq4JMIybh8zteDsQ8Z2tnkY/S2cNc4ggi jlX4Ytm3vMiya30fLJ8CUlcma0UMwBVeduQoArL230FvZnplmF4DfSM9RsPneAuRe/uwzW EyjgsKfmraLTlrD1j/21rtXlKEvjUeg= Date: Wed, 18 Jun 2025 10:25:36 +0800 MIME-Version: 1.0 Subject: Re: [PATCH v4] mm: use per_vma lock for MADV_DONTNEED Content-Language: en-US To: Barry Song <21cnbao@gmail.com> Cc: akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Barry Song , "Liam R. Howlett" , David Hildenbrand , Vlastimil Babka , Jann Horn , Suren Baghdasaryan , Lokesh Gidra , Tangquan Zheng , Qi Zheng , Lance Yang , Lorenzo Stoakes , Zi Li References: <20250607220150.2980-1-21cnbao@gmail.com> <309d22ca-6cd9-4601-8402-d441a07d9443@lucifer.local> X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. From: Lance Yang In-Reply-To: <309d22ca-6cd9-4601-8402-d441a07d9443@lucifer.local> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 84B774000C X-Stat-Signature: wuktzpx7pgt45aithp6cm9d8cpcdmt98 X-Rspam-User: X-HE-Tag: 1750213544-470827 X-HE-Meta: U2FsdGVkX196NU7htsuVvJ0ZszQu9xZW7YEa9nWr2GjsHh+XXvcPImFG7uthohiV1PEEuqP3VBO8Q3SLr+GNX+02vWTSTwABXlza1bbN1H8AyfxDe1eYF1bSvfTAVCwRaOENPjfNC1CWMEBLp7qwuSbJED8OLtRfbo3F/e1dQynpkl4NOEAixxJSkEbY0/9uKQ7ouv4GBwEZoJXC9JGDg/JJ3Cxv61CWJ3tToLkmSAbHA+IDzF4wQlEDlVxD/qL2rI88boWlMooJNyZRfT1DvfyT28AAdghDqNhl0bXDUzmwyIBFtUv2JId0PbYkglUlPOJkecPK5HAqKaTEsDNwEZAEU622LH+UkON5x9Rde42O/K5cWzhbyLFjCNFcuJnyMdk6za/5hEfczCafyIvq+jKc7RIIlsQtAoPFSZOjuFMjzWpj/llb4G8qOKBGyXObjw/5BeCo0Tkl7stERXGNySuSim+lE5ngSGoLuEk90gptjm27vC2N2AYKsqTmSxmKGRC/DBFu2HnH7IqJBDbRgyu+pYS23zU4O8Q/FibCZfLkeSgqFSwfH8MfJtENBKvotE7mLAuXSJKTIvv5iDCEBOSoXZqly+GKQ4WqmrJBVZAz46UpY0oBvW1h5eaOeQdd1uah+CGnCE1DLJzTmTfOXIea6AI8KFv6vZw9x0R7ePZeb4ZKvvzOKC6Fth8Ii3zuw+Y6m0xKZypP/nTElZl0KWiuxIwZv/0finmd7kjOV6L98IxGkc+VBSSQR+CnwpHcpmLAfijjJ4GhxNSwv9cM9VhTCuLP3oCNeAA2nwGcK/bOx+D+UNUoViOMcKbcEz1FduMbsCBYeX0ElpSQNz2whddfaUBU3no08Ks68evKB3HE1XuPXEIdzuORpfPd1GEg4hAEZ9JRIzuezhvzPg6Cb1lIUTFPXONNJzfACNlAYYmd7rf3doU1aQ3yy76JnvjxLFvU35Hjx0o6UFrJrU6 q5NlVHp5 d6eox2DkbrpF3uuvKjjfrlJ9+QCnFqu9doMoH8RvW/8T2zjPlSE3GNaaDFZhxlP29RV9p+BG/p1sg3OI+4/78h8/yYxn7IZWFAqdiJe5tm6ey4bFnhdF7dZ3aoFHSL2ubHE0TUxj07sigOo0PxZycueRT4vb7nUG+3CiFsvTQjZXp9ilNSBQlNhvECVRgXwawvVg4BSp0jhx1PZlHRBQ2IyA27fdiPsQ7KLNk41Rx9LEUA/SBICdzjnjMmZYSyy6vPRf0dZRv1JYK7w5I2T0r+R0MzNXeqwT1rHZbSoavzToFbxqFgweySd/LJQwsWhWwbCAO X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Hi all, Crazy, the per-VMA lock for madvise is an absolute game-changer ;) On 2025/6/17 21:38, Lorenzo Stoakes wrote: [...] > > On Sun, Jun 08, 2025 at 10:01:50AM +1200, Barry Song wrote: >> From: Barry Song >> >> Certain madvise operations, especially MADV_DONTNEED, occur far more >> frequently than other madvise options, particularly in native and Java >> heaps for dynamic memory management. >> >> Currently, the mmap_lock is always held during these operations, even when >> unnecessary. This causes lock contention and can lead to severe priority >> inversion, where low-priority threads—such as Android's HeapTaskDaemon— >> hold the lock and block higher-priority threads. >> >> This patch enables the use of per-VMA locks when the advised range lies >> entirely within a single VMA, avoiding the need for full VMA traversal. In >> practice, userspace heaps rarely issue MADV_DONTNEED across multiple VMAs. >> >> Tangquan’s testing shows that over 99.5% of memory reclaimed by Android >> benefits from this per-VMA lock optimization. After extended runtime, >> 217,735 madvise calls from HeapTaskDaemon used the per-VMA path, while >> only 1,231 fell back to mmap_lock. >> >> To simplify handling, the implementation falls back to the standard >> mmap_lock if userfaultfd is enabled on the VMA, avoiding the complexity of >> userfaultfd_remove(). >> >> Many thanks to Lorenzo's work[1] on: >> "Refactor the madvise() code to retain state about the locking mode >> utilised for traversing VMAs. >> >> Then use this mechanism to permit VMA locking to be done later in the >> madvise() logic and also to allow altering of the locking mode to permit >> falling back to an mmap read lock if required." >> >> One important point, as pointed out by Jann[2], is that >> untagged_addr_remote() requires holding mmap_lock. This is because >> address tagging on x86 and RISC-V is quite complex. >> >> Until untagged_addr_remote() becomes atomic—which seems unlikely in >> the near future—we cannot support per-VMA locks for remote processes. >> So for now, only local processes are supported. Just to put some numbers on it, I ran a micro-benchmark with 100 parallel threads, where each thread calls madvise() on its own 1GiB chunk of 64KiB mTHP-backed memory. The performance gain is huge: 1) MADV_DONTNEED saw its average time drop from 0.0508s to 0.0270s (~47% faster) 2) MADV_FREE saw its average time drop from 0.3078s to 0.1095s (~64% faster) Thanks, Lance