Linux-mm Archive on 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,
	Baoquan He <hebaoquan@kylinos.cn>
Subject: [RFC PATCH 2/2] mm: multi-gen LRU: scale Bloom filter size to insertion count
Date: Wed,  2 Dec 2026 16:28:41 +0800	[thread overview]
Message-ID: <20261202082841.294888-3-baoquan.he@linux.dev> (raw)
In-Reply-To: <20261202082841.294888-1-baoquan.he@linux.dev>

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



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

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]

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-3-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=hebaoquan@kylinos.cn \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox