From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D85B7606A32D for ; Wed, 2 Dec 2026 08:28:53 +0000 (UTC) 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=HGeKuGGA Received: by kanga.kvack.org (Postfix) id 070B36B05CE; Wed, 2 Dec 2026 03:28:53 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 0219A6B05EB; Wed, 2 Dec 2026 03:28:52 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E7A0F6B05EC; Wed, 2 Dec 2026 03:28:52 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id BF5ED6B05CE for ; Wed, 2 Dec 2026 03:28:52 -0500 (EST) Received: from smtpin07.hostedemail.com (lb01a-stub [10.200.18.249]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 4F92680208 for ; Wed, 2 Dec 2026 08:28:52 +0000 (UTC) X-FDA: 85498367304.07.05D8A00 Received: from mta0.migadu.com (out-103.mta0.migadu.com [91.218.175.103]) by imf01.hostedemail.com (Postfix) with ESMTP id 2337D40004 for ; Wed, 2 Dec 2026 08:28:49 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=HGeKuGGA; spf=pass (imf01.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.103 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Seal: i=1; a=rsa-sha256; d=hostedemail.com; s=arc-20220608; cv=none; t=1796200130; b=m9Y2RrB/BeWVMBv1V+sfX4s60Hk5sbDvA7x9IPwOKHCCysExzq2TB2ZL2V7dl9EFSjDTq3 aBT70PCkxzOzZ/06nd4gd5VfK7obHzJHf51Yi50/M72jJW6nxA3tSA1PebZ+XCBGv1B4oz bOteMQzOdbBhDFgJlWbJ7G64Prgy5IQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1796200130; 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: references:dkim-signature; bh=5Brp8lAZ935sxLnG0A3vyOJVYc82FpnTQBcCfFf4xN8=; b=N6MiuZ+wpaGt+AXsPdS9rSPgVKCpn165ddQU7Tx8M0vkdH2xq83VtvhezcHl1tze/xibO0 xKUD+lJA/MNVJV3UtpEu3Hf9x4E7EcsIloE7XstwXtZW8GuO0vgmCbWM9uNkGH107H/BTG HWRhrMIE7yjzqVa9ZP2ieiD7GGcRREM= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=HGeKuGGA; spf=pass (imf01.hostedemail.com: domain of baoquan.he@linux.dev designates 91.218.175.103 as permitted sender) smtp.mailfrom=baoquan.he@linux.dev; dmarc=pass (policy=none) header.from=linux.dev X-Envelope-To: linux-mm@kvack.org DKIM-Signature: a=rsa-sha256; bh=e9fPXp/majm4/vLa83uHYChun4GIGn7AvEsW4IaoRBE=; c=simple/simple; d=linux.dev; h=from:to:subject:date:message-id:mime-version:content-type; s=key1; t=1796200128; v=1; x=1796804928; b=HGeKuGGAQf7aCdFYxt6rqEAQRWHTG5MgJ5YqHgP7TWS6kgFnDDnxOCL2bP261rcAXX/Nu7/5 FjlOtaUFt0chwa9J7Z5FunkjGFoh7GCpKBO14EiccNK+P0Hk/WLwTvUhLoTbHIMw/ZOJW8CFqRQ YJKy1unfaYFo7ADVMvGbGkA4= X-Envelope-To: linux-mm@kvack.org Received: by mta12.migadu.com with ESMTPS id 9a7ab43f8f90e108; Wed, 02 Dec 2026 08:28:48 +0000 X-Mizu-Trace-ID: 9a7ab43f8f90e108 X-Migadu-Flow: FLOW_OUT From: Baoquan He To: linux-mm@kvack.org Cc: david@kernel.org, liam@infradead.org, vbabka@kernel.org, rppt@kernel.org, mhocko@suse.com, akpm@linux-foundation.org, kasong@tencent.com, baohua@kernel.org, qi.zheng@linux.dev, shakeel.butt@linux.dev, baoquan.he@linux.dev, baolin.wang@linux.alibaba.com, hannes@cmpxchg.org Subject: [RFC PATCH 0/2] mm: multi-gen LRU: fix the Bloom filter size and keys Date: Wed, 2 Dec 2026 16:28:39 +0800 Message-ID: <20261202082841.294888-1-baoquan.he@linux.dev> X-Mailer: git-send-email 2.52.0 X-Archived-By: MIME-Version: 1.0 Content-type: text/plain Content-Transfer-Encoding: 8bit X-Rspam-User: X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 2337D40004 X-Spam-Status: No, score=-13.8 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Stat-Signature: hhbs4wps8c4yaojixxfejer3ogg9y6u1 X-HE-Tag: 1796200129-327867 X-HE-Meta: U2FsdGVkX1/UfhYwTgeZy5+lT5kNQfxsfvxcwrXv44Qtf8z/LvkTufGTAeS9G/gRgWk/bHKuRnkaNHbG76RvirsAwo2gl0fFerJiwpRcuAwK/3NB3DtPIbc+UKZiEAm6zDSwIa67NoU5S8K56k3ov9fHm/xQp6LcdCpX0Q2OizwFvhHnTuTnS8iqZXVNa3P2wpdWnYtpFYIHG4mXSWZyjScJw0XRarlxTqL8TmQ2KVeAnMO8cpUK0S8PhCkewWNViRCRzvqlMkvL7lYKQojvgeLiJQdQntIuZ95aA5XOnSX5uty7rQgwd89b9fbLsqzs+CpFHxsJbb4tNKmf5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5HgUFOfMDrM8zyGiblrhutrkXm4FT2XvhCN94MShp4CkPOI2UgGtKUOo2WkqLK1A== Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 The draft patches have been queued in my local branch for long time, so organize them and retest, then post for reviewing. This applies on top of the PUD-level Bloom filter series: [PATCH v6 0/4] skip empty PUD subtrees during aging with a PUD-level Bloom filter https://lore.kernel.org/all/20261127061630.11290-1-baoquan.he@linux.dev/T/#u Problem ======= The PMD-level Bloom filter has a fixed size of m=2^15 bits and builds its two keys by splitting one 30-bit hash in half. Both choices make the false positive rate depend on things we do not control. Size ---- For k=2 the rate is (1 - e^(-2n/m))^2, where n is the number of insertions into the filter. With m fixed the rate follows n: 0.4% at n=1026, 20.1% at n=9731, 48.3% at n=19459 and 70.5% at n=30000. A memcg with a small working set on a node pays for a 4 KiB bitmap it does not need, and a large memcg still gets a rate far above the ~20% the design targets. Keys ----- The two keys are adjacent slices of one multiplication, so they are not independent, while the formula and the rungs below assume that they are. The lower slice is also the less mixed one, because multiplication carries entropy upward. The practical cost of that is small. On the scattered layout a real page table has, see test 2 below, the old filter came out at 0.89x to 0.99x of the formula, so the rungs would have been slightly conservative rather than wrong. Patch 1 is not there to lower the rate. It makes the rate follow the formula whatever the address layout, which is what patch 2 needs in order to pick a size. Approach ======== Patch 1 replaces the hash. get_item_key() now builds key[0] and key[1] from two jhash_2words() calls with different seeds. The keys are independent, so the rate matches the formula, and they can be masked to any width without correlating the two keys. The cost is about 12 extra cycles per call, measured with rdtsc: hash_ptr takes 1.4 cycles, two jhash_2words take 13.0. Patch 2 makes the size follow n. m is picked from five rungs, each a power-of-two size, using the insertion count of the other filter, so n decides the size of the next generation: n < 1200 -> shift=12 (m=2^12, 512 B) n < 4800 -> shift=14 (m=2^14, 2 KB) n < 9700 -> shift=15 (m=2^15, 4 KB) [default] n < 19000 -> shift=16 (m=2^16, 8 KB) n >= 19000 -> shift=17 (m=2^17, 16 KB) Each threshold is where the formula reaches 20%, rounded down. So the rate climbs from 6.6% to 20.0% inside a rung and drops back when the filter moves up one. About 20% at the top of every rung is the design target. A filter can now be reallocated while page table walkers are still running, so the bitmap, its shift and its insertion count are published as one RCU-protected object and freed after a grace period. The bitmap is a separate allocation so that it stays exactly 2^shift bits. Inline it and the header moves every rung up one kmalloc bucket and raises the allocation order by one. These allocations must not reclaim, because they can happen in the reclaim path. Test ==== Two kinds of evidence, both read the live filters through a debug patch. 1) Filter state in the kernel. probe_rung.sh runs one worker that maps hot 2 MiB regions, where every 4 KiB page is touched in a loop, and cold ones, where one page is touched. Only hot regions enter the filter, since suitable_to_scan() needs at least 1/8 of the PTE slots young. So n is the number of hot regions. The worker is aged three times. Pass 1 is force_scan, which ignores the filter and walks every PMD, so all hot regions get recorded. Pass 2 is normal and lets reset_bloom_filter() pick the size. Pass 3 is normal and is the one measured, which is the steady state in the table below. The read happens while the worker runs, because exiting frees its address space and resets the filters. hot anon n shift m set fp_est fp_theory 2 GiB 1026 12 4096 1632 0.1588 0.1553 7 GiB 3504 14 16384 5707 0.1213 0.1211 9.5 GiB 4867 15 32768 8392 0.0656 0.0660 12 GiB 6147 15 32768 10276 0.0983 0.0979 19 GiB 9731 16 65536 16816 0.0658 0.0660 38 GiB 19459 17 131072 33594 0.0657 0.0660 The rung follows n, all five are reachable, and the measured rate is within 2.3% of the formula at every size. The n=3504 row also shows that the thresholds above are the ones in use: 1200 <= 3504 < 4800 gives shift 14, where a 3000 threshold would give 15. Compared with the old fixed m=2^15: n old (m=2^15) new (measured) filter memory 1026 0.4% 15.9% 8x less 3504 3.7% 12.1% 2x less 4867 6.6% 6.6% same 6147 9.8% 9.8% same 9731 20.1% 6.6% 2x more 19459 48.3% 6.6% 4x more 30000 70.5% 13.5% 4x more 2) Page table walking cost. This is the number the filter controls. The two counters are standard in both kernels, so the same script measures both: F MM_NONLEAF_FOUND PMDs the filter let through A MM_NONLEAF_ADDED of those, PMDs that had enough young leaves to be worth walking F - A is the number of PMDs walked for nothing, i.e. false positives, and each one costs a walk over 512 PTE slots. Dividing by the number of cold PMDs probed gives the rate the filter actually achieved. One worker maps dense hot PMDs interleaved with sparse cold PMDs in an 8 PMD pattern. The interleaving matters: a PUD that holds only cold PMDs is skipped by the PUD-level filter one level up, so those PMDs never reach the PMD-level filter and their false positives are never seen. One force_scan pass fills the filter, two normal passes let it resize, and the third normal pass is measured. base is the same kernel without the two Bloom patches. 2) Page table walking cost. Two counters are standard in both kernels, so the same script measures both: F MM_NONLEAF_FOUND PMDs the filter let through A MM_NONLEAF_ADDED of those, PMDs worth walking (F - A) is the PMDs walked for nothing, that is false positives, and each one costs 512 PTE slot checks. The worker interleaves hot and cold PMDs in an 8 PMD pattern. A PUD that holds only cold PMDs is skipped by the PUD filter one level up, so those PMDs never reach the PMD filter and their false positives would go unseen. The three passes are the same as above. base is the same kernel without the two patches. n = 13746, 8247 cold PMDs probed base this series A, useful PMD walks 13748 13746 F - A, wasted PMD walks 2371 942 -60.3% realised false positive rate 28.8% 11.4% 2.5x better wasted PTE slot checks 1213952 482304 -60.3% T, PTE slots examined 8249993 7517491 -8.9% T / A, slots per useful PMD 600 547 -8.9% filter at measurement m=2^15 shift 16 n = 20736, 6912 cold PMDs probed base this series A, useful PMD walks 20737 20738 F - A, wasted PMD walks 3517 502 -85.7% realised false positive rate 50.9% 7.3% 7.0x better wasted PTE slot checks 1800704 257024 -85.7% T, PTE slots examined 12415530 10871607 -12.4% T / A, slots per useful PMD 599 524 -12.4% filter at measurement m=2^15 shift 17 For the same useful work one aging pass scans 8.9% fewer PTE slots at n=13746 and 12.4% fewer at n=20736, and the walks it wastes drop by 2.5x and 7.0x. The gain grows with n because the fixed m=2^15 saturates while the rungs keep scaling. Both runs check out: (A + F - A) * 512 reproduces T to within 0.03%. The saving is a tenth of the walk, not all of it, because walk_pmd_range() tests pmd_young() before it consults the filter. A PMD that was never accessed is already skipped for free, so the filter can only save a walk on a PMD that is young but has too few young pages. That is the shape the test above builds: one warm page out of 512. On hardware where the MMU does not set PMD accessed bits the filter would see every PMD, and the saving would be larger. Note: I will upload all the testing scripts and kernel debug patch onto github or somewhere, in case anyone wants to give it a shot. Known limitations ================= n can grow faster than the filter. reset_bloom_filter() sizes from the previous generation, so a workload whose hot set jumps by more than one rung pays one generation at a high rate: n=4866 in shift 12 (512 B) 0.8178 n=6146 in shift 14 (2 KB) 0.2796 n=9730 in shift 12 (512 B) 0.9810 n=19458 in shift 16 (8 KB) 0.1994 It corrects itself in the next generation. The reallocation also happens when memory is tight, which is when a higher-order allocation fails most easily. On failure the previous filter is kept, so the worst case is the previous rung. With no filter at all the walker scans every PMD, which is the behaviour before the filters existed. Baoquan He (2): mm: multi-gen LRU: use Jenkins hash for Bloom filter mm: multi-gen LRU: scale Bloom filter size to insertion count include/linux/mmzone.h | 24 ++++- mm/vmscan.c | 193 ++++++++++++++++++++++++++++++----------- 2 files changed, 162 insertions(+), 55 deletions(-) -- 2.52.0