From: Baoquan He 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 Signed-off-by: Baoquan He --- 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 #include #include +#include #include #include #include @@ -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