Compare
28d2905d77bc2e7c20260a6bc6b4179bdf95cb06..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
9 files changed,
+67
−43
- fs/ocfs2/dlm/dlmcommon.h
- fs/ocfs2/dlm/dlmdebug.c
- fs/ocfs2/dlm/dlmrecovery.c
- include/linux/futex.h
- include/linux/futex_types.h
- include/linux/irq-entry-common.h
- include/linux/sched/signal.h
- kernel/futex/core.c
- samples/pidfd/Makefile
modified fs/ocfs2/dlm/dlmcommon.h
| @@ -435,8 +435,6 @@ enum { | |||
| 435 | 435 | DLM_DEREF_LOCKRES_DONE = 522, | |
| 436 | 436 | }; | |
| 437 | 437 | ||
| 438 | - | extern spinlock_t dlm_reco_state_lock; | |
| 439 | - | ||
| 440 | 438 | struct dlm_reco_node_data | |
| 441 | 439 | { | |
| 442 | 440 | int state; | |
modified fs/ocfs2/dlm/dlmdebug.c
| @@ -736,11 +736,8 @@ static int debug_state_print(struct dlm_ctxt *dlm, char *buf, int len) | |||
| 736 | 736 | buf + out, len - out); | |
| 737 | 737 | out += scnprintf(buf + out, len - out, "\n"); | |
| 738 | 738 | ||
| 739 | - | spin_unlock(&dlm->spinlock); | |
| 740 | - | ||
| 741 | 739 | /* Recovery Node State: */ | |
| 742 | 740 | out += scnprintf(buf + out, len - out, "Recovery Node State:\n"); | |
| 743 | - | spin_lock(&dlm_reco_state_lock); | |
| 744 | 741 | list_for_each_entry(node, &dlm->reco.node_data, list) { | |
| 745 | 742 | switch (node->state) { | |
| 746 | 743 | case DLM_RECO_NODE_DATA_INIT: | |
| @@ -772,7 +769,7 @@ static int debug_state_print(struct dlm_ctxt *dlm, char *buf, int len) | |||
| 772 | 769 | node->node_num, state); | |
| 773 | 770 | } | |
| 774 | 771 | ||
| 775 | - | spin_unlock(&dlm_reco_state_lock); | |
| 772 | + | spin_unlock(&dlm->spinlock); | |
| 776 | 773 | ||
| 777 | 774 | return out; | |
| 778 | 775 | } | |
modified fs/ocfs2/dlm/dlmrecovery.c
| @@ -80,7 +80,7 @@ static int dlm_lockres_master_requery(struct dlm_ctxt *dlm, | |||
| 80 | 80 | ||
| 81 | 81 | static u64 dlm_get_next_mig_cookie(void); | |
| 82 | 82 | ||
| 83 | - | DEFINE_SPINLOCK(dlm_reco_state_lock); | |
| 83 | + | static DEFINE_SPINLOCK(dlm_reco_state_lock); | |
| 84 | 84 | static DEFINE_SPINLOCK(dlm_mig_cookie_lock); | |
| 85 | 85 | static u64 dlm_mig_cookie = 1; | |
| 86 | 86 | ||
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 include/linux/sched/signal.h
| @@ -130,8 +130,8 @@ struct signal_struct { | |||
| 130 | 130 | * process will inherit a flag if they should look for a | |
| 131 | 131 | * child_subreaper process at exit. | |
| 132 | 132 | */ | |
| 133 | - | bool is_child_subreaper; | |
| 134 | - | bool has_child_subreaper; | |
| 133 | + | unsigned int is_child_subreaper:1; | |
| 134 | + | unsigned int has_child_subreaper:1; | |
| 135 | 135 | unsigned int autoreap:1; | |
| 136 | 136 | ||
| 137 | 137 | #ifdef CONFIG_POSIX_TIMERS | |
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 samples/pidfd/Makefile
| @@ -1,4 +1,4 @@ | |||
| 1 | 1 | # SPDX-License-Identifier: GPL-2.0 | |
| 2 | - | userprogs-always-y += pidfd-metadata | |
| 2 | + | usertprogs-always-y += pidfd-metadata | |
| 3 | 3 | ||
| 4 | 4 | userccflags += -I usr/include | |