All of lore.kernel.org
 help / color / mirror / Atom feed
From: Baoquan He <baoquan.he@linux.dev>
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	[thread overview]
Message-ID: <20261202082841.294888-1-baoquan.he@linux.dev> (raw)

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



             reply	other threads:[~2026-12-02  8:28 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02  8:28 Baoquan He [this message]
2026-12-02  8:28 ` [RFC PATCH 1/2] mm: multi-gen LRU: use Jenkins hash for Bloom filter Baoquan He
2026-12-02  8:28 ` [RFC PATCH 2/2] mm: multi-gen LRU: scale Bloom filter size to insertion count Baoquan He

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261202082841.294888-1-baoquan.he@linux.dev \
    --to=baoquan.he@linux.dev \
    --cc=akpm@linux-foundation.org \
    --cc=baohua@kernel.org \
    --cc=baolin.wang@linux.alibaba.com \
    --cc=david@kernel.org \
    --cc=hannes@cmpxchg.org \
    --cc=kasong@tencent.com \
    --cc=liam@infradead.org \
    --cc=linux-mm@kvack.org \
    --cc=mhocko@suse.com \
    --cc=qi.zheng@linux.dev \
    --cc=rppt@kernel.org \
    --cc=shakeel.butt@linux.dev \
    --cc=vbabka@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.