Compare

0fa993581aaa58a34ae3dcebebe0fbf593fcb441..7cbaa0f4579f48e92639c6abb761fdef8fbade47

entry: Account the deferred hrtimer rearm as hardirq context for lockdep

On a desktop debug kernel (PROVE_LOCKING, KASAN, LOCKDEP_CHAINS_BITS=19)
lockdep switches itself off after about two days of uptime with
"BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!", and locking is not validated
until the next reboot. In the last such boot the pool ran out after 46.6
hours, and a fifth of it, 533509 of 2621431 chain hlocks in 63838 of
344717 chains, had been created by the deferred hrtimer rearm on return
from interrupt.

Make lockdep account that rearm as part of the timer interrupt it
belongs to.

Commit 15dd3a948855
("hrtimer: Push reprogramming timers into the interrupt return path")
moved the reprogramming of the clock event device out of the timer
interrupt into the interrupt return path. On return to kernel mode
irqentry_exit_to_kernel_mode_after_preempt() performs it after
irq_exit_rcu() has already left lockdep's hardirq context. lockdep
therefore records hrtimer_bases.lock and the timekeeper sequence count
as taken by the interrupted context, on top of the locks that context
holds, and every distinct held-lock stack an interrupt hits adds two
lock chains. When the rearm runs from irq_enter_rcu() or
__irq_exit_rcu() it is still inside lockdep's hardirq context, and there
the same acquisitions reuse one pair of chains.

The dependencies recorded on the return path are "lock held with
interrupts enabled -> hrtimer_bases.lock". Such a lock is hardirq-unsafe
and hrtimer_bases.lock is hardirq-safe, so a path in the reverse
direction is already reported by the irq-safety checks without these
edges.

Re-enter lockdep's hardirq context around the rearm on the kernel return
path. The return to user mode path holds no locks and is left alone, as
is the rearm in __schedule(), which runs under the runqueue lock.

With the same kernel tree, configuration and workload, one hour of
uptime including a full kernel build, the chains of this shape, with
hrtimer_bases.lock and optionally the timekeeper sequence count on top
of a held-lock stack outside the scheduler, go from 13266 (96187 chain
hlocks, 15.9% of those in use) to 4. Those 4 come from clock_was_set(),
which takes cpus_read_lock() and each CPU's hrtimer_bases.lock and reads
the clock under it.

Fixes: 15dd3a948855 ("hrtimer: Push reprogramming timers into the interrupt return path")
Signed-off-by: Mikhail Gavrilov <mikhail.v.gavrilov@gmail.com>
Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org>
Link: https://patch.msgid.link/20261013071318.33300-1-mikhail.v.gavrilov@gmail.com

Mikhail Gavrilov <mikhail.v.gavrilov@gmail.com> · 2026-10-13 07:13 UTC · committed by Peter Zijlstra

Browse files
9 files changed, +71 −47
modified include/linux/futex.h
@@ -23,28 +23,32 @@ struct task_struct;
23 23 * 01 : Shared futex (PTHREAD_PROCESS_SHARED)
24 24 * mapped on a file (reference on the underlying inode)
25 25 * 10 : Shared futex (PTHREAD_PROCESS_SHARED)
26 - * (but private mapping on an mm, and reference taken on it)
26 + * (but private mapping on an mm)
27 27 */
28 28
29 - #define FUT_OFF_INODE 1 /* We set bit 0 if key has a reference on inode */
30 - #define FUT_OFF_MMSHARED 2 /* We set bit 1 if key has a reference on mm */
29 + #define FUT_OFF_INODE 1 /* We set bit 0 if shared key identifies an inode */
30 + #define FUT_OFF_MMSHARED 2 /* We set bit 1 if shared key identifies an mm */
31 31
32 32 union futex_key {
33 + /* For FUT_OFF_INODE */
33 34 struct {
34 35 u64 i_seq;
35 36 unsigned long pgoff;
36 37 unsigned int offset;
37 38 /* unsigned int node; */
38 39 } shared;
40 +
41 + /* For FUT_OFF_MMSHARED or private */
39 42 struct {
40 43 union {
41 - struct mm_struct *mm;
42 - u64 __tmp;
44 + struct mm_struct *mm; /* for private */
45 + u64 mm_seq; /* for FUT_OFF_MMSHARED */
43 46 };
44 47 unsigned long address;
45 48 unsigned int offset;
46 49 /* unsigned int node; */
47 50 } private;
51 +
48 52 struct {
49 53 u64 ptr;
50 54 unsigned long word;
@@ -152,11 +156,10 @@ static inline void futex_set_vdso_cs_range(struct futex_mm_data *fd, unsigned in
152 156 static inline void futex_fixup_robust_unlock(struct pt_regs *regs) { }
153 157 #endif /* !CONFIG_FUTEX_ROBUST_UNLOCK */
154 158
155 -
156 - #if defined(CONFIG_FUTEX_PRIVATE_HASH) || defined(CONFIG_FUTEX_ROBUST_UNLOCK)
159 + #ifdef CONFIG_FUTEX
157 160 void futex_mm_init(struct mm_struct *mm);
158 161 #else
159 162 static inline void futex_mm_init(struct mm_struct *mm) { }
160 - #endif
163 + #endif /* !CONFIG_FUTEX */
161 164
162 165 #endif /* _LINUX_FUTEX_H */
modified include/linux/futex_types.h
@@ -85,10 +85,12 @@ struct futex_unlock_cs_ranges { };
85 85 * struct futex_mm_data - Futex related per MM data
86 86 * @phash: Futex private hash related data
87 87 * @unlock: Futex unlock VDSO critical sections
88 + * @unique_id: Unique ID of the MM, for FUT_OFF_MMSHARED futexes
88 89 */
89 90 struct futex_mm_data {
90 91 struct futex_mm_phash phash;
91 92 struct futex_unlock_cs_ranges unlock;
93 + atomic64_t unique_id;
92 94 };
93 95 #else /* CONFIG_FUTEX */
94 96 struct futex_sched_data { };
modified include/linux/irq-entry-common.h
@@ -468,6 +468,25 @@ static inline void irqentry_exit_to_kernel_mode_preempt(struct pt_regs *regs,
468 468 irqentry_exit_cond_resched();
469 469 }
470 470
471 + /*
472 + * The deferred hrtimer rearm is the tail of the timer interrupt, moved to the
473 + * interrupt return path so that it runs after a possible reschedule. By now
474 + * irq_exit_rcu() has left lockdep's hardirq context. Re-enter it for the
475 + * rearm, so that the hrtimer base lock and the timekeeping sequence count are
476 + * not chained onto the locks held by the interrupted context, which would add
477 + * new lock chains for every lock stack an interrupt hits.
478 + *
479 + * The rearm on return to user mode needs no such annotation: no locks are
480 + * held there, so it uses the same lock chains as the rearm in hardirq
481 + * context.
482 + */
483 + static __always_inline void irqentry_hrtimer_rearm_deferred(void)
484 + {
485 + lockdep_hardirq_enter();
486 + hrtimer_rearm_deferred();
487 + lockdep_hardirq_exit();
488 + }
489 +
471 490 /**
472 491 * irqentry_exit_to_kernel_mode_after_preempt - Establish trace, lockdep and RCU state
473 492 * @regs: Pointer to current's pt_regs
@@ -491,7 +510,7 @@ irqentry_exit_to_kernel_mode_after_preempt(struct pt_regs *regs, irqentry_state_
491 510 */
492 511 if (state.exit_rcu) {
493 512 instrumentation_begin();
494 - hrtimer_rearm_deferred();
513 + irqentry_hrtimer_rearm_deferred();
495 514 /* Tell the tracer that IRET will enable interrupts */
496 515 trace_hardirqs_on_prepare();
497 516 lockdep_hardirqs_on_prepare();
@@ -502,7 +521,7 @@ irqentry_exit_to_kernel_mode_after_preempt(struct pt_regs *regs, irqentry_state_
502 521 }
503 522
504 523 instrumentation_begin();
505 - hrtimer_rearm_deferred();
524 + irqentry_hrtimer_rearm_deferred();
506 525 /* Covers both tracing and lockdep */
507 526 trace_hardirqs_on();
508 527 instrumentation_end();
modified kernel/futex/core.c
@@ -439,45 +439,51 @@ struct hrtimer_sleeper *futex_setup_timer(ktime_t *time, struct hrtimer_sleeper
439 439 }
440 440
441 441 /*
442 - * Generate a machine wide unique identifier for this inode.
442 + * Generate a machine wide unique identifier for this object (inode or mm).
443 443 *
444 444 * This relies on u64 not wrapping in the life-time of the machine; which with
445 445 * 1ns resolution means almost 585 years.
446 - *
447 - * This further relies on the fact that a well formed program will not unmap
448 - * the file while it has a (shared) futex waiting on it. This mapping will have
449 - * a file reference which pins the mount and inode.
450 - *
451 - * If for some reason an inode gets evicted and read back in again, it will get
452 - * a new sequence number and will _NOT_ match, even though it is the exact same
453 - * file.
454 - *
455 - * It is important that futex_match() will never have a false-positive, esp.
456 - * for PI futexes that can mess up the state. The above argues that false-negatives
457 - * are only possible for malformed programs.
458 446 */
459 - static u64 get_inode_sequence_number(struct inode *inode)
447 + static u64 get_object_id(atomic64_t *object_seq)
460 448 {
461 - static atomic64_t i_seq;
449 + static atomic64_t last_assigned_seq;
462 450 u64 old;
463 451
464 - /* Does the inode already have a sequence number? */
465 - old = atomic64_read(&inode->i_sequence);
452 + /* Does the object already have a sequence number? */
453 + old = atomic64_read(object_seq);
466 454 if (likely(old))
467 455 return old;
468 456
469 457 for (;;) {
470 - u64 new = atomic64_inc_return(&i_seq);
458 + u64 new = atomic64_inc_return(&last_assigned_seq);
471 459 if (WARN_ON_ONCE(!new))
472 460 continue;
473 461
474 462 old = 0;
475 - if (!atomic64_try_cmpxchg_relaxed(&inode->i_sequence, &old, new))
463 + if (!atomic64_try_cmpxchg_relaxed(object_seq, &old, new))
476 464 return old;
477 465 return new;
478 466 }
479 467 }
480 468
469 + /*
470 + * This relies on the fact that a well formed program will not unmap
471 + * the file while it has a (shared) futex waiting on it. This mapping will have
472 + * a file reference which pins the mount and inode.
473 + *
474 + * If for some reason an inode gets evicted and read back in again, it will get
475 + * a new sequence number and will _NOT_ match, even though it is the exact same
476 + * file.
477 + *
478 + * It is important that futex_match() will never have a false-positive, esp.
479 + * for PI futexes that can mess up the state. The above argues that false-negatives
480 + * are only possible for malformed programs.
481 + */
482 + static u64 get_inode_sequence_number(struct inode *inode)
483 + {
484 + return get_object_id(&inode->i_sequence);
485 + }
486 +
481 487 /**
482 488 * get_futex_key() - Get parameters which are the keys for a futex
483 489 * @uaddr: virtual address of the futex
@@ -683,8 +689,8 @@ again:
683 689 goto out;
684 690 }
685 691
686 - key->both.offset |= FUT_OFF_MMSHARED; /* ref taken on mm */
687 - key->private.mm = mm;
692 + key->both.offset |= FUT_OFF_MMSHARED;
693 + key->private.mm_seq = get_object_id(&mm->futex.unique_id);
688 694 key->private.address = address;
689 695
690 696 } else {
@@ -2055,13 +2061,12 @@ static void futex_robust_unlock_init_mm(struct futex_mm_data *fd)
2055 2061 static inline void futex_robust_unlock_init_mm(struct futex_mm_data *fd) { }
2056 2062 #endif /* !CONFIG_FUTEX_ROBUST_UNLOCK */
2057 2063
2058 - #if defined(CONFIG_FUTEX_PRIVATE_HASH) || defined(CONFIG_FUTEX_ROBUST_UNLOCK)
2059 2064 void futex_mm_init(struct mm_struct *mm)
2060 2065 {
2066 + atomic64_set(&mm->futex.unique_id, 0);
2061 2067 futex_hash_init_mm(&mm->futex);
2062 2068 futex_robust_unlock_init_mm(&mm->futex);
2063 2069 }
2064 - #endif
2065 2070
2066 2071 int futex_hash_prctl(unsigned long arg2, unsigned long arg3, unsigned long arg4)
2067 2072 {
modified kernel/trace/Makefile
@@ -146,12 +146,11 @@ KASAN_SANITIZE_undefsyms_base.o := y
146 146
147 147 UNDEFINED_ALLOWLIST = __asan __gcov __kasan __kcsan __hwasan __sancov __sanitizer __tsan __ubsan __msan \
148 148 __aeabi_unwind_cpp __s390_indirect_jump __x86_indirect_thunk simple_ring_buffer \
149 - current_thread_info_reg \
150 - $(shell $(NM) -u $(obj)/undefsyms_base.o 2>/dev/null | awk '$$2 {print $$2}')
149 + $(shell $(NM) -u $(obj)/undefsyms_base.o 2>/dev/null | awk '{print $$2}')
151 150
152 151 quiet_cmd_check_undefined = NM $<
153 152 cmd_check_undefined = \
154 - undefsyms=$$($(NM) -u $< | awk '$$2 {print $$2}' | grep -v $(addprefix -e , $(UNDEFINED_ALLOWLIST)) || true); \
153 + undefsyms=$$($(NM) -u $< | grep -v $(addprefix -e , $(UNDEFINED_ALLOWLIST)) || true); \
155 154 if [ -n "$$undefsyms" ]; then \
156 155 echo "Unexpected symbols in $<:" >&2; \
157 156 echo "$$undefsyms" >&2; \
modified kernel/trace/ring_buffer.c
@@ -2380,9 +2380,6 @@ int ring_buffer_meta_seq_init(struct file *file, struct trace_buffer *buffer, in
2380 2380 struct seq_file *m;
2381 2381 int ret;
2382 2382
2383 - if (!cpumask_test_cpu(cpu, buffer->cpumask))
2384 - return -ENODEV;
2385 -
2386 2383 ret = seq_open(file, &rb_meta_seq_ops);
2387 2384 if (ret)
2388 2385 return ret;
@@ -3630,8 +3627,10 @@ static void rb_inc_iter(struct ring_buffer_iter *iter)
3630 3627 /* Return the index into the sub-buffers for a given sub-buffer */
3631 3628 static int rb_meta_subbuf_idx(struct ring_buffer_cpu_meta *meta, void *subbuf)
3632 3629 {
3633 - void *subbuf_array = rb_subbufs_from_meta(meta);
3630 + void *subbuf_array;
3634 3631
3632 + subbuf_array = (void *)meta + sizeof(int) * meta->nr_subbufs;
3633 + subbuf_array = (void *)ALIGN((unsigned long)subbuf_array, meta->subbuf_size);
3635 3634 return (subbuf - subbuf_array) / meta->subbuf_size;
3636 3635 }
3637 3636
modified kernel/trace/trace.c
@@ -5881,7 +5881,7 @@ static int tracing_buffer_meta_open(struct inode *inode, struct file *filp)
5881 5881
5882 5882 ret = ring_buffer_meta_seq_init(filp, tr->array_buffer.buffer, cpu);
5883 5883 if (ret < 0)
5884 - trace_array_put(tr);
5884 + __trace_array_put(tr);
5885 5885 return ret;
5886 5886 }
5887 5887
modified kernel/trace/trace_events_hist.c
@@ -289,7 +289,7 @@ static u64 hist_field_log2(struct hist_field *hist_field,
289 289
290 290 u64 val = hist_fn_call(operand, elt, buffer, rbe, event);
291 291
292 - return order_base_2(val);
292 + return (u64) ilog2(roundup_pow_of_two(val));
293 293 }
294 294
295 295 static u64 hist_field_bucket(struct hist_field *hist_field,
@@ -3090,7 +3090,7 @@ create_field_var_hist(struct hist_trigger_data *target_hist_data,
3090 3090 key_field = hist_data->fields[i];
3091 3091 if (!first)
3092 3092 seq_buf_putc(&s, ',');
3093 - expr_field_str(key_field, &s);
3093 + seq_buf_puts(&s, key_field->field->name);
3094 3094 first = false;
3095 3095 }
3096 3096
modified kernel/trace/undefsyms_base.c
@@ -9,7 +9,6 @@
9 9
10 10 #include <linux/atomic.h>
11 11 #include <linux/string.h>
12 - #include <asm/local.h>
13 12 #include <asm/page.h>
14 13
15 14 void undefsyms_base(void *p, int n);
@@ -19,13 +18,11 @@ static char page[PAGE_SIZE] __aligned(PAGE_SIZE);
19 18 void undefsyms_base(void *p, int n)
20 19 {
21 20 char buffer[256] = { 0 };
22 - u32 u = 0;
23 21
22 + u32 u = 0;
24 23 memset((char * volatile)page, 8, PAGE_SIZE);
25 24 memset((char * volatile)buffer, 8, sizeof(buffer));
26 25 memcpy((void * volatile)p, buffer, sizeof(buffer));
27 26 cmpxchg((u32 * volatile)&u, 0, 8);
28 - cmpxchg((unsigned long *)p, 0, 8);
29 - local_set((local_t *)p, 8);
30 27 WARN_ON(n == 0xdeadbeef);
31 28 }