Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Kuan-Wei Chiu <visitorckw@gmail.com>
To: corbet@lwn.net, colyli@kernel.org, kent.overstreet@linux.dev,
	akpm@linux-foundation.org, robertpang@google.com
Cc: linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org,
	linux-bcache@vger.kernel.org, jserv@ccns.ncku.edu.tw,
	Kuan-Wei Chiu <visitorckw@gmail.com>,
	stable@vger.kernel.org
Subject: [PATCH 8/8] bcache: Fix the tail IO latency regression by using equality-aware min heap API
Date: Wed, 11 Jun 2025 05:55:16 +0800	[thread overview]
Message-ID: <20250610215516.1513296-9-visitorckw@gmail.com> (raw)
In-Reply-To: <20250610215516.1513296-1-visitorckw@gmail.com>

Commit 866898efbb25 ("bcache: remove heap-related macros and switch to
generic min_heap") replaced the original top-down heap macros in bcache
with the generic min heap library, which uses a bottom-up heapify
strategy. However, in scenarios like invalidate_buckets_lru() -
especially before the cache is fully populated - many buckets remain
unfilled. This causes new_bucket_prio() to frequently return zero,
leading to a high rate of equal comparisons.

Bottom-up sift_down performs up to 2 * log2(n) comparisons in such
cases, resulting in a performance regression.

Switch to the _eqaware variants of the min heap API to restore the
original top-down sift_down behavior, which requires only O(1)
comparisons when many elements are equal.

Also use the inline versions of the heap functions to avoid performance
degradation introduced by commit 92a8b224b833 ("lib/min_heap: introduce
non-inline versions of min heap API functions"), as
invalidate_buckets_lru() is on a performance-critical hot path.

Fixes: 866898efbb25 ("bcache: remove heap-related macros and switch to generic min_heap")
Fixes: 92a8b224b833 ("lib/min_heap: introduce non-inline versions of min heap API functions")
Reported-by: Robert Pang <robertpang@google.com>
Closes: https://lore.kernel.org/linux-bcache/CAJhEC06F_AtrPgw2-7CvCqZgeStgCtitbD-ryuPpXQA-JG5XXw@mail.gmail.com
Cc: stable@vger.kernel.org # 6.11+
Signed-off-by: Kuan-Wei Chiu <visitorckw@gmail.com>
---
 drivers/md/bcache/alloc.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/drivers/md/bcache/alloc.c b/drivers/md/bcache/alloc.c
index 8998e61efa40..625c5c4eb962 100644
--- a/drivers/md/bcache/alloc.c
+++ b/drivers/md/bcache/alloc.c
@@ -207,15 +207,16 @@ static void invalidate_buckets_lru(struct cache *ca)
 		if (!bch_can_invalidate_bucket(ca, b))
 			continue;
 
-		if (!min_heap_full(&ca->heap))
-			min_heap_push(&ca->heap, &b, &bucket_max_cmp_callback, ca);
-		else if (!new_bucket_max_cmp(&b, min_heap_peek(&ca->heap), ca)) {
+		if (!min_heap_full_inline(&ca->heap))
+			min_heap_push_inline(&ca->heap, &b, &bucket_max_cmp_callback, ca);
+		else if (!new_bucket_max_cmp(&b, min_heap_peek_inline(&ca->heap), ca)) {
 			ca->heap.data[0] = b;
-			min_heap_sift_down(&ca->heap, 0, &bucket_max_cmp_callback, ca);
+			min_heap_sift_down_eqaware_inline(&ca->heap, 0, &bucket_max_cmp_callback,
+							  ca);
 		}
 	}
 
-	min_heapify_all(&ca->heap, &bucket_min_cmp_callback, ca);
+	min_heapify_all_eqaware_inline(&ca->heap, &bucket_min_cmp_callback, ca);
 
 	while (!fifo_full(&ca->free_inc)) {
 		if (!ca->heap.nr) {
@@ -227,8 +228,8 @@ static void invalidate_buckets_lru(struct cache *ca)
 			wake_up_gc(ca->set);
 			return;
 		}
-		b = min_heap_peek(&ca->heap)[0];
-		min_heap_pop(&ca->heap, &bucket_min_cmp_callback, ca);
+		b = min_heap_peek_inline(&ca->heap)[0];
+		min_heap_pop_eqaware_inline(&ca->heap, &bucket_min_cmp_callback, ca);
 
 		bch_invalidate_one_bucket(ca, b);
 	}
-- 
2.34.1


  parent reply	other threads:[~2025-06-10 21:56 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-06-10 21:55 [PATCH 0/8] Fix bcache regression with equality-aware heap APIs Kuan-Wei Chiu
2025-06-10 21:55 ` [PATCH 1/8] lib min_heap: Add equal-elements-aware sift_down variant Kuan-Wei Chiu
2025-06-12 13:00   ` Robert Pang
2025-06-13  6:17     ` Kuan-Wei Chiu
2025-06-10 21:55 ` [PATCH 2/8] lib min_heap: Add typedef for sift_down function pointer Kuan-Wei Chiu
2025-06-10 21:55 ` [PATCH 3/8] lib min_heap: add eqaware variant of min_heapify_all() Kuan-Wei Chiu
2025-06-10 21:55 ` [PATCH 4/8] lib min_heap: add eqaware variant of min_heap_pop() Kuan-Wei Chiu
2025-06-10 21:55 ` [PATCH 5/8] lib min_heap: add eqaware variant of min_heap_pop_push() Kuan-Wei Chiu
2025-06-10 21:55 ` [PATCH 6/8] lib min_heap: add eqaware variant of min_heap_del() Kuan-Wei Chiu
2025-06-10 21:55 ` [PATCH 7/8] Documentation/core-api: min_heap: Document _eqaware variants of min-heap APIs Kuan-Wei Chiu
2025-06-10 21:55 ` Kuan-Wei Chiu [this message]
2025-06-12  1:48 ` [PATCH 0/8] Fix bcache regression with equality-aware heap APIs Andrew Morton
2025-06-12  1:54   ` Andrew Morton
2025-06-13  6:15   ` Kuan-Wei Chiu
2025-06-13 14:26     ` Robert Pang
2025-06-13 18:04       ` Andrew Morton
2025-06-13 23:19         ` Kuan-Wei Chiu
2025-06-14  1:31           ` Andrew Morton

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=20250610215516.1513296-9-visitorckw@gmail.com \
    --to=visitorckw@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=colyli@kernel.org \
    --cc=corbet@lwn.net \
    --cc=jserv@ccns.ncku.edu.tw \
    --cc=kent.overstreet@linux.dev \
    --cc=linux-bcache@vger.kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=robertpang@google.com \
    --cc=stable@vger.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