* [RFC PATCH 0/2] mm: multi-gen LRU: fix the Bloom filter size and keys
@ 2026-12-02 8:28 Baoquan He
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
0 siblings, 2 replies; 3+ messages in thread
From: Baoquan He @ 2026-12-02 8:28 UTC (permalink / raw)
To: linux-mm
Cc: david, liam, vbabka, rppt, mhocko, akpm, kasong, baohua, qi.zheng,
shakeel.butt, baoquan.he, baolin.wang, hannes
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
^ permalink raw reply [flat|nested] 3+ messages in thread
* [RFC PATCH 1/2] mm: multi-gen LRU: use Jenkins hash for Bloom filter
2026-12-02 8:28 [RFC PATCH 0/2] mm: multi-gen LRU: fix the Bloom filter size and keys Baoquan He
@ 2026-12-02 8:28 ` Baoquan He
2026-12-02 8:28 ` [RFC PATCH 2/2] mm: multi-gen LRU: scale Bloom filter size to insertion count Baoquan He
1 sibling, 0 replies; 3+ messages in thread
From: Baoquan He @ 2026-12-02 8:28 UTC (permalink / raw)
To: linux-mm
Cc: david, liam, vbabka, rppt, mhocko, akpm, kasong, baohua, qi.zheng,
shakeel.butt, baoquan.he, baolin.wang, hannes, Baoquan He
From: Baoquan He <hebaoquan@kylinos.cn>
get_item_key() built its two keys by splitting one 30-bit hash in half, so
the keys were adjacent slices of a single multiplication and not
independent. The false positive rate formula assumes k independent hash
functions, so the realised rate did not follow the formula.
Build the two keys from two jhash_2words() calls with different seeds
instead. They can then be masked to any filter width without correlating
the two keys, which is what the dynamic sizing in the next patch needs.
Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
Signed-off-by: Baoquan He <baoquan.he@linux.dev>
---
mm/vmscan.c | 19 +++++++++++++------
1 file changed, 13 insertions(+), 6 deletions(-)
diff --git a/mm/vmscan.c b/mm/vmscan.c
index 7b49a27bd3cc..e46f14b666d1 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -41,6 +41,7 @@
#include <linux/memcontrol.h>
#include <linux/migrate.h>
#include <linux/delayacct.h>
+#include <linux/jhash.h>
#include <linux/sysctl.h>
#include <linux/memory-tiers.h>
#include <linux/oom.h>
@@ -2872,7 +2873,6 @@ static bool __maybe_unused seq_is_valid(struct lruvec *lruvec)
* 2. And when reallocating, it's worth scaling its size according to the number
* of inserted entries in the other filter, to reduce the memory overhead on
* small systems and false positives on large systems.
- * 3. Jenkins' hash function is an alternative to Knuth's.
*/
#define BLOOM_FILTER_SHIFT 15
@@ -2881,14 +2881,21 @@ static inline int filter_gen_from_seq(unsigned long seq)
return seq % NR_BLOOM_FILTERS;
}
+/*
+ * Produce two independent hash keys within [0, BIT(BLOOM_FILTER_SHIFT))
+ * using Jenkins hash (lookup3). Two independent 32-bit hashes with
+ * different seeds replace the single Knuth multiplicative hash produced
+ * by hash_ptr(), yielding better distribution for PMD addresses.
+ */
static void get_item_key(void *item, int *key)
{
- u32 hash = hash_ptr(item, BLOOM_FILTER_SHIFT * 2);
-
- BUILD_BUG_ON(BLOOM_FILTER_SHIFT * 2 > BITS_PER_TYPE(u32));
+ unsigned long ptr = (unsigned long)item;
+ u32 lo = (u32)ptr;
+ u32 hi = (u32)(ptr >> 32);
+ u32 mask = BIT(BLOOM_FILTER_SHIFT) - 1;
- key[0] = hash & (BIT(BLOOM_FILTER_SHIFT) - 1);
- key[1] = hash >> BLOOM_FILTER_SHIFT;
+ key[0] = jhash_2words(lo, hi, 0) & mask;
+ key[1] = jhash_2words(lo, hi, 0x9e3779b9UL) & mask;
}
static bool test_bloom_filter(struct lru_gen_mm_state *mm_state, int level,
--
2.52.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [RFC PATCH 2/2] mm: multi-gen LRU: scale Bloom filter size to insertion count
2026-12-02 8:28 [RFC PATCH 0/2] mm: multi-gen LRU: fix the Bloom filter size and keys Baoquan He
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 ` Baoquan He
1 sibling, 0 replies; 3+ messages in thread
From: Baoquan He @ 2026-12-02 8:28 UTC (permalink / raw)
To: linux-mm
Cc: david, liam, vbabka, rppt, mhocko, akpm, kasong, baohua, qi.zheng,
shakeel.butt, baoquan.he, baolin.wang, hannes, Baoquan He
From: Baoquan He <hebaoquan@kylinos.cn>
The filter used a fixed m=2^15 bits, so its false positive rate,
(1 - e^(-2n/m))^2, followed n rather than the ~20% target: far below it
for a small working set, far above it for a large one.
Pick m from five rungs, each a power-of-two size. reset_bloom_filter()
sizes the next generation from the insertion count of the other filter:
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 rate reaches 20% rounded down, so it stays at
or below 20% until the filter moves up a rung. The top rung caps a filter
at 16 KB, so past ~39000 insertions the rate rises again.
A filter can now be reallocated while page table walkers run, so publish
the bitmap, its shift and its insertion count as one RCU object and free
it after a grace period. Reusing the buffer when the size is unchanged is
safe: a walker racing with the clear only skips a PMD it may have kept, or
walks one extra PMD.
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, so a
higher order fails more easily under fragmentation. On failure the
previous filter is kept; with none, the walker scans every PMD, as before
the filters existed.
Signed-off-by: Baoquan He <hebaoquan@kylinos.cn>
Signed-off-by: Baoquan He <baoquan.he@linux.dev>
---
include/linux/mmzone.h | 24 +++++-
mm/vmscan.c | 184 +++++++++++++++++++++++++++++------------
2 files changed, 154 insertions(+), 54 deletions(-)
diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h
index a1437728180d..be9dd773413d 100644
--- a/include/linux/mmzone.h
+++ b/include/linux/mmzone.h
@@ -595,6 +595,22 @@ enum bloom_filter_level {
BLOOM_FILTER_LEVELS,
};
+/*
+ * A Bloom filter, its size and its insertion count are published as one
+ * RCU-protected object so that a concurrent reader never pairs a new bitmap
+ * with a stale shift, which would let it index past the end of the bitmap.
+ */
+struct lru_gen_bloom {
+ /* deferred free, see reset_bloom_filter() */
+ struct rcu_head rcu;
+ /* log2 size of the bitmap */
+ unsigned int shift;
+ /* number of insertions, used to size the next filter */
+ atomic_t nr;
+ /* the bitmap, indexed by get_item_key() */
+ unsigned long *bits;
+};
+
struct lru_gen_mm_state {
/* synced with max_seq after each iteration */
unsigned long seq;
@@ -602,8 +618,12 @@ struct lru_gen_mm_state {
struct list_head *head;
/* where the last iteration ended before */
struct list_head *tail;
- /* Bloom filters flip after each iteration, indexed by level */
- unsigned long *filters[BLOOM_FILTER_LEVELS][NR_BLOOM_FILTERS];
+ /*
+ * Bloom filters flip after each iteration, indexed by level. NULL means
+ * the filter is not allocated, e.g. the spare one was freed after the
+ * last aging.
+ */
+ struct lru_gen_bloom __rcu *filters[BLOOM_FILTER_LEVELS][NR_BLOOM_FILTERS];
/* the mm stats for debugging */
unsigned long stats[NR_HIST_GENS][NR_MM_STATS];
};
diff --git a/mm/vmscan.c b/mm/vmscan.c
index e46f14b666d1..16c1d5329a59 100644
--- a/mm/vmscan.c
+++ b/mm/vmscan.c
@@ -2845,10 +2845,11 @@ static bool __maybe_unused seq_is_valid(struct lruvec *lruvec)
******************************************************************************/
/*
- * Bloom filters with m=1<<15, k=2 and the false positive rates of ~1/5 when
- * n=10,000 and ~1/2 when n=20,000, where, conventionally, m is the number of
- * bits in a bitmap, k is the number of hash functions and n is the number of
- * inserted items.
+ * Bloom filters with k=2, where, conventionally, m is the number of bits in a
+ * bitmap, k is the number of hash functions and n is the number of inserted
+ * items. m is picked from the power-of-two tiers below according to the number
+ * of entries the filter has accumulated, aiming at a false positive rate of
+ * ~1/5.
*
* Page table walkers use one of the two filters to reduce their search space.
* To get rid of non-leaf entries that no longer have enough leaf entries, the
@@ -2862,19 +2863,10 @@ static bool __maybe_unused seq_is_valid(struct lruvec *lruvec)
* 1 GiB subtree (a PUD): BLOOM_FILTER_PUD next to BLOOM_FILTER_PMD. A false
* positive at the PMD level only costs a PTE walk when the PUD level also
* passes. A false positive at the PUD level is rare, because it holds far
- * fewer keys, but it costs 512 PMD walks. So both levels keep the same m and
- * k, even though the PUD level holds much fewer keys.
- *
- * For future optimizations:
- * 1. It's not necessary to keep both filters of a level all the time.
- * The spare one can be freed after the RCU grace period and reallocated
- * if needed again. Now: two levels of double buffering take 16 KiB
- * per lruvec.
- * 2. And when reallocating, it's worth scaling its size according to the number
- * of inserted entries in the other filter, to reduce the memory overhead on
- * small systems and false positives on large systems.
+ * fewer keys, but it costs 512 PMD walks. Both levels pick m independently
+ * from their own insertion count and keep k the same, so the PUD level ends
+ * up with a smaller filter than the PMD level.
*/
-#define BLOOM_FILTER_SHIFT 15
static inline int filter_gen_from_seq(unsigned long seq)
{
@@ -2882,17 +2874,15 @@ static inline int filter_gen_from_seq(unsigned long seq)
}
/*
- * Produce two independent hash keys within [0, BIT(BLOOM_FILTER_SHIFT))
- * using Jenkins hash (lookup3). Two independent 32-bit hashes with
- * different seeds replace the single Knuth multiplicative hash produced
- * by hash_ptr(), yielding better distribution for PMD addresses.
+ * Produce two independent hash keys within [0, BIT(shift)) using
+ * Jenkins hash (lookup3).
*/
-static void get_item_key(void *item, int *key)
+static void get_item_key(void *item, unsigned int shift, int *key)
{
unsigned long ptr = (unsigned long)item;
u32 lo = (u32)ptr;
u32 hi = (u32)(ptr >> 32);
- u32 mask = BIT(BLOOM_FILTER_SHIFT) - 1;
+ u32 mask = BIT(shift) - 1;
key[0] = jhash_2words(lo, hi, 0) & mask;
key[1] = jhash_2words(lo, hi, 0x9e3779b9UL) & mask;
@@ -2901,53 +2891,146 @@ static void get_item_key(void *item, int *key)
static bool test_bloom_filter(struct lru_gen_mm_state *mm_state, int level,
unsigned long seq, void *item)
{
+ struct lru_gen_bloom *filter;
int key[2];
- unsigned long *filter;
- int gen = filter_gen_from_seq(seq);
+ bool ret = true;
- filter = READ_ONCE(mm_state->filters[level][gen]);
- if (!filter)
- return true;
-
- get_item_key(item, key);
+ /*
+ * A page table walker can still be walking the just finished generation
+ * after mm_state->seq has moved on, so the filter it reads can be
+ * released at any time; keep the read side RCU protected.
+ */
+ rcu_read_lock();
+ filter = rcu_dereference(mm_state->filters[level][filter_gen_from_seq(seq)]);
+ if (filter) {
+ get_item_key(item, filter->shift, key);
+ ret = test_bit(key[0], filter->bits) && test_bit(key[1], filter->bits);
+ }
+ rcu_read_unlock();
- return test_bit(key[0], filter) && test_bit(key[1], filter);
+ return ret;
}
static void update_bloom_filter(struct lru_gen_mm_state *mm_state, int level,
unsigned long seq, void *item)
{
+ struct lru_gen_bloom *filter;
int key[2];
- unsigned long *filter;
- int gen = filter_gen_from_seq(seq);
- filter = READ_ONCE(mm_state->filters[level][gen]);
+ rcu_read_lock();
+ filter = rcu_dereference(mm_state->filters[level][filter_gen_from_seq(seq)]);
+ if (filter) {
+ get_item_key(item, filter->shift, key);
+ set_bit(key[0], filter->bits);
+ set_bit(key[1], filter->bits);
+ atomic_inc(&filter->nr);
+ }
+ rcu_read_unlock();
+}
+
+/*
+ * Power-of-two Bloom filter sizes. With k=2 the false positive rate is
+ * (1 - e^(-2n/m))^2, which reaches ~20% at n = 0.2964*m, i.e. m ~= 3.37*n.
+ * The n limits below are those points rounded down, so the rate stays at
+ * or below ~20% until the filter moves up a rung.
+ */
+#define BLOOM_FILTER_SHIFT_MIN 12 /* 512 B, n < 1200 */
+#define BLOOM_FILTER_SHIFT_LOW 14 /* 2 KB, n < 4800 */
+#define BLOOM_FILTER_SHIFT_DEF 15 /* 4 KB, n < 9700 */
+#define BLOOM_FILTER_SHIFT_HIGH 16 /* 8 KB, n < 19000 */
+#define BLOOM_FILTER_SHIFT_MAX 17 /* 16 KB, n >= 19000 */
+
+/* these allocations can happen in the reclaim path, so never reclaim here */
+#define BLOOM_FILTER_GFP (__GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN)
+
+static void destroy_bloom_filter(struct lru_gen_bloom *filter)
+{
if (!filter)
return;
- get_item_key(item, key);
+ bitmap_free(filter->bits);
+ kfree(filter);
+}
+
+static void free_bloom_filter(struct rcu_head *rcu)
+{
+ destroy_bloom_filter(container_of(rcu, struct lru_gen_bloom, rcu));
+}
+
+static void put_bloom_filter(struct lru_gen_bloom *filter)
+{
+ if (filter)
+ call_rcu(&filter->rcu, free_bloom_filter);
+}
+
+static struct lru_gen_bloom *alloc_bloom_filter(unsigned int shift)
+{
+ struct lru_gen_bloom *filter;
+
+ filter = kzalloc_obj(*filter, BLOOM_FILTER_GFP);
+ if (!filter)
+ return NULL;
+
+ filter->bits = bitmap_zalloc(BIT(shift), BLOOM_FILTER_GFP);
+ if (!filter->bits) {
+ kfree(filter);
+ return NULL;
+ }
+
+ filter->shift = shift;
+
+ return filter;
+}
+
+static unsigned int bloom_filter_shift(unsigned int nr)
+{
+ if (nr < 1200)
+ return BLOOM_FILTER_SHIFT_MIN;
+ if (nr < 4800)
+ return BLOOM_FILTER_SHIFT_LOW;
+ if (nr < 9700)
+ return BLOOM_FILTER_SHIFT_DEF;
+ if (nr < 19000)
+ return BLOOM_FILTER_SHIFT_HIGH;
- if (!test_bit(key[0], filter))
- set_bit(key[0], filter);
- if (!test_bit(key[1], filter))
- set_bit(key[1], filter);
+ return BLOOM_FILTER_SHIFT_MAX;
}
static void reset_bloom_filter(struct lru_gen_mm_state *mm_state, int level,
unsigned long seq)
{
- unsigned long *filter;
+ struct lru_gen_bloom *old, *filter;
int gen = filter_gen_from_seq(seq);
+ int other = (gen + 1) % NR_BLOOM_FILTERS;
+ unsigned int shift;
- filter = mm_state->filters[level][gen];
- if (filter) {
- bitmap_clear(filter, 0, BIT(BLOOM_FILTER_SHIFT));
- return;
+ old = rcu_dereference_protected(mm_state->filters[level][other], 1);
+ shift = old ? bloom_filter_shift(atomic_read(&old->nr)) :
+ BLOOM_FILTER_SHIFT_DEF;
+
+ /* detach the spare one so that nobody sees it half built */
+ old = rcu_dereference_protected(xchg(&mm_state->filters[level][gen], NULL), 1);
+
+ if (old && old->shift == shift) {
+ filter = old;
+ old = NULL;
+ } else {
+ filter = alloc_bloom_filter(shift);
+
+ /* out of memory: keep what we have rather than dropping it */
+ if (!filter) {
+ if (!old)
+ return;
+
+ filter = old;
+ old = NULL;
+ }
}
- filter = bitmap_zalloc(BIT(BLOOM_FILTER_SHIFT),
- __GFP_HIGH | __GFP_NOMEMALLOC | __GFP_NOWARN);
- WRITE_ONCE(mm_state->filters[level][gen], filter);
+ bitmap_clear(filter->bits, 0, BIT(filter->shift));
+ atomic_set(&filter->nr, 0);
+ rcu_assign_pointer(mm_state->filters[level][gen], filter);
+ put_bloom_filter(old);
}
/******************************************************************************
@@ -6351,12 +6434,9 @@ void lru_gen_exit_memcg(struct mem_cgroup *memcg)
if (!mm_state)
continue;
- for (level = 0; level < BLOOM_FILTER_LEVELS; level++) {
- for (i = 0; i < NR_BLOOM_FILTERS; i++) {
- bitmap_free(mm_state->filters[level][i]);
- mm_state->filters[level][i] = NULL;
- }
- }
+ for (level = 0; level < BLOOM_FILTER_LEVELS; level++)
+ for (i = 0; i < NR_BLOOM_FILTERS; i++)
+ destroy_bloom_filter(xchg(&mm_state->filters[level][i], NULL));
}
}
--
2.52.0
^ permalink raw reply related [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-12-02 8:29 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-02 8:28 [RFC PATCH 0/2] mm: multi-gen LRU: fix the Bloom filter size and keys Baoquan He
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
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.