summaryrefslogtreecommitdiffstats
path: root/drivers/md/dm-cache-background-tracker.c
diff options
context:
space:
mode:
authorJinke Han <jinkehan@didiglobal.com>2026-09-08 15:37:42 +0800
committerIngo Molnar <mingo@kernel.org>2026-09-17 09:48:42 +0200
commita5f7a5bb3b7f28ba7e4fa246775b29a0e5537255 (patch)
tree73fef824f9a846cf935869d0dbc5070ef6e2f741 /drivers/md/dm-cache-background-tracker.c
downloadlinux-stable-a5f7a5bb3b7f28ba7e4fa246775b29a0e5537255.tar.gz
linux-stable-a5f7a5bb3b7f28ba7e4fa246775b29a0e5537255.zip
x86/kprobes: Fix crash when probing CS CALL instructionsgrafted
When using eBPF to probe CS CALL instructions within a function, a crash can be triggered. The eBPF tool probes offset 257 of the __hrtimer_run_queues() function: <__hrtimer_run_queues+249>: nopl 0x0(%rax,%rax,1) <__hrtimer_run_queues+254>: mov %r14,%rdi <__hrtimer_run_queues+257>: cs call <__x86_indirect_thunk_r12> <__hrtimer_run_queues+263>: mov %eax,%r12d <__hrtimer_run_queues+266>: xchg %ax,%ax <__hrtimer_run_queues+268>: mov %r13,%rdi Which triggers this crash: BUG: unable to handle page fault for address: 00000000000f41c9 #PF: supervisor write access in kernel mode #PF: error_code(0x0002) - not-present page PGD 0 P4D 0 Oops: 0002 [#1] SMP NOPTI CPU: 1 PID: 0 Comm: swapper/1 Kdump: loaded Tainted: P RIP: 0010:__hrtimer_run_queues+0x106/0x230 Note that __hrtimer_run_queues+0x106 is __hrtimer_run_queues+262, which is at the 6th byte of the above CS CALL instruction. Since the CS CALL instruction occupies 6 bytes, the exception occurred in the middle of that call instruction. The root cause is that when using eBPF tools to probe in the middle of a function, a kprobe with INT3 is used as the underlying implementation. During single-step emulation of the original CALL instruction, int3_emulate_call() assumes that the probed CALL instruction is 5 bytes long. However, the actual CS-prefixed CALL instruction occupies 6 bytes, so it constructs an incorrect exception return address. When the CPU returns from the kprobe handler, the next instruction to be executed is at the address of the last byte of that CS CALL instruction. Coincidentally, starting from that address, the CPU fetches and decodes a completely different instruction, which ultimately triggers a kernel crash. Fix the issue by using the actual instruction length obtained from the instruction decoder when constructing the exception return address, rather than relying on the hardcoded CALL_INSN_SIZE macro. [ mingo: Refined the changelog ] Fixes: 6256e668b7af ("x86/kprobes: Use int3 instead of debug trap for single-step") Suggested-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> Signed-off-by: Jinke Han <jinkehan@didiglobal.com> Signed-off-by: Ingo Molnar <mingo@kernel.org> Reviewed-by: Masami Hiramatsu (Google) <mhiramat@kernel.org> Acked-by: Yafang Shao <laoar.shao@gmail.com> Acked-by: Borislav Petkov <bp@alien8.de> Cc: Peter Zijlstra <peterz@infradead.org> Link: https://patch.msgid.link/20260908073742.GA10517@didi-ThinkCentre-M920t-N000
Diffstat (limited to 'drivers/md/dm-cache-background-tracker.c')
-rw-r--r--drivers/md/dm-cache-background-tracker.c239
1 files changed, 239 insertions, 0 deletions
diff --git a/drivers/md/dm-cache-background-tracker.c b/drivers/md/dm-cache-background-tracker.c
new file mode 100644
index 000000000..2ca3a3e2e
--- /dev/null
+++ b/drivers/md/dm-cache-background-tracker.c
@@ -0,0 +1,239 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * Copyright (C) 2017 Red Hat. All rights reserved.
+ *
+ * This file is released under the GPL.
+ */
+
+#include "dm-cache-background-tracker.h"
+
+/*----------------------------------------------------------------*/
+
+#define DM_MSG_PREFIX "dm-background-tracker"
+
+struct background_tracker {
+ unsigned int max_work;
+ atomic_t pending_promotes;
+ atomic_t pending_writebacks;
+ atomic_t pending_demotes;
+
+ struct list_head issued;
+ struct list_head queued;
+ struct rb_root pending;
+};
+
+struct kmem_cache *btracker_work_cache = NULL;
+
+struct background_tracker *btracker_create(unsigned int max_work)
+{
+ struct background_tracker *b = kmalloc_obj(*b);
+
+ if (!b) {
+ DMERR("couldn't create background_tracker");
+ return NULL;
+ }
+
+ b->max_work = max_work;
+ atomic_set(&b->pending_promotes, 0);
+ atomic_set(&b->pending_writebacks, 0);
+ atomic_set(&b->pending_demotes, 0);
+
+ INIT_LIST_HEAD(&b->issued);
+ INIT_LIST_HEAD(&b->queued);
+
+ b->pending = RB_ROOT;
+
+ return b;
+}
+EXPORT_SYMBOL_GPL(btracker_create);
+
+void btracker_destroy(struct background_tracker *b)
+{
+ struct bt_work *w, *tmp;
+
+ BUG_ON(!list_empty(&b->issued));
+ list_for_each_entry_safe (w, tmp, &b->queued, list) {
+ list_del(&w->list);
+ kmem_cache_free(btracker_work_cache, w);
+ }
+
+ kfree(b);
+}
+EXPORT_SYMBOL_GPL(btracker_destroy);
+
+static int cmp_oblock(dm_oblock_t lhs, dm_oblock_t rhs)
+{
+ if (from_oblock(lhs) < from_oblock(rhs))
+ return -1;
+
+ if (from_oblock(rhs) < from_oblock(lhs))
+ return 1;
+
+ return 0;
+}
+
+static bool __insert_pending(struct background_tracker *b,
+ struct bt_work *nw)
+{
+ int cmp;
+ struct bt_work *w;
+ struct rb_node **new = &b->pending.rb_node, *parent = NULL;
+
+ while (*new) {
+ w = container_of(*new, struct bt_work, node);
+
+ parent = *new;
+ cmp = cmp_oblock(w->work.oblock, nw->work.oblock);
+ if (cmp < 0)
+ new = &((*new)->rb_left);
+
+ else if (cmp > 0)
+ new = &((*new)->rb_right);
+
+ else
+ /* already present */
+ return false;
+ }
+
+ rb_link_node(&nw->node, parent, new);
+ rb_insert_color(&nw->node, &b->pending);
+
+ return true;
+}
+
+static struct bt_work *__find_pending(struct background_tracker *b,
+ dm_oblock_t oblock)
+{
+ int cmp;
+ struct bt_work *w;
+ struct rb_node **new = &b->pending.rb_node;
+
+ while (*new) {
+ w = container_of(*new, struct bt_work, node);
+
+ cmp = cmp_oblock(w->work.oblock, oblock);
+ if (cmp < 0)
+ new = &((*new)->rb_left);
+
+ else if (cmp > 0)
+ new = &((*new)->rb_right);
+
+ else
+ break;
+ }
+
+ return *new ? w : NULL;
+}
+
+
+static void update_stats(struct background_tracker *b, struct policy_work *w, int delta)
+{
+ switch (w->op) {
+ case POLICY_PROMOTE:
+ atomic_add(delta, &b->pending_promotes);
+ break;
+
+ case POLICY_DEMOTE:
+ atomic_add(delta, &b->pending_demotes);
+ break;
+
+ case POLICY_WRITEBACK:
+ atomic_add(delta, &b->pending_writebacks);
+ break;
+ }
+}
+
+unsigned int btracker_nr_demotions_queued(struct background_tracker *b)
+{
+ return atomic_read(&b->pending_demotes);
+}
+EXPORT_SYMBOL_GPL(btracker_nr_demotions_queued);
+
+static bool max_work_reached(struct background_tracker *b)
+{
+ return atomic_read(&b->pending_promotes) +
+ atomic_read(&b->pending_writebacks) +
+ atomic_read(&b->pending_demotes) >= b->max_work;
+}
+
+static struct bt_work *alloc_work(struct background_tracker *b)
+{
+ if (max_work_reached(b))
+ return NULL;
+
+ return kmem_cache_alloc(btracker_work_cache, GFP_NOWAIT);
+}
+
+int btracker_queue(struct background_tracker *b,
+ struct policy_work *work,
+ struct policy_work **pwork)
+{
+ struct bt_work *w;
+
+ if (pwork)
+ *pwork = NULL;
+
+ w = alloc_work(b);
+ if (!w)
+ return -ENOMEM;
+
+ memcpy(&w->work, work, sizeof(*work));
+
+ if (!__insert_pending(b, w)) {
+ /*
+ * There was a race, we'll just ignore this second
+ * bit of work for the same oblock.
+ */
+ kmem_cache_free(btracker_work_cache, w);
+ return -EINVAL;
+ }
+
+ if (pwork) {
+ *pwork = &w->work;
+ list_add(&w->list, &b->issued);
+ } else
+ list_add(&w->list, &b->queued);
+ update_stats(b, &w->work, 1);
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(btracker_queue);
+
+/*
+ * Returns -ENODATA if there's no work.
+ */
+int btracker_issue(struct background_tracker *b, struct policy_work **work)
+{
+ struct bt_work *w;
+
+ if (list_empty(&b->queued))
+ return -ENODATA;
+
+ w = list_first_entry(&b->queued, struct bt_work, list);
+ list_move(&w->list, &b->issued);
+ *work = &w->work;
+
+ return 0;
+}
+EXPORT_SYMBOL_GPL(btracker_issue);
+
+void btracker_complete(struct background_tracker *b,
+ struct policy_work *op)
+{
+ struct bt_work *w = container_of(op, struct bt_work, work);
+
+ update_stats(b, &w->work, -1);
+ rb_erase(&w->node, &b->pending);
+ list_del(&w->list);
+ kmem_cache_free(btracker_work_cache, w);
+}
+EXPORT_SYMBOL_GPL(btracker_complete);
+
+bool btracker_promotion_already_present(struct background_tracker *b,
+ dm_oblock_t oblock)
+{
+ return __find_pending(b, oblock) != NULL;
+}
+EXPORT_SYMBOL_GPL(btracker_promotion_already_present);
+
+/*----------------------------------------------------------------*/