Compare

3c516a5646285442e1cd71dff2584b77eb4fc421..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
8 files changed, +68 −97
modified drivers/gpu/drm/amd/amdgpu/amdgpu_gem.c
@@ -567,10 +567,8 @@ int amdgpu_gem_userptr_ioctl(struct drm_device *dev, void *data,
567 567
568 568 if (args->flags & AMDGPU_GEM_USERPTR_VALIDATE) {
569 569 range = amdgpu_hmm_range_alloc(NULL);
570 - if (unlikely(!range)) {
571 - r = -ENOMEM;
572 - goto release_object;
573 - }
570 + if (unlikely(!range))
571 + return -ENOMEM;
574 572 r = amdgpu_ttm_tt_get_user_pages(bo, range);
575 573 if (r) {
576 574 amdgpu_hmm_range_free(range);
modified drivers/gpu/drm/amd/amdgpu/amdgpu_ttm.c
@@ -2065,15 +2065,14 @@ static int amdgpu_ttm_buffer_entity_init(struct amdgpu_gtt_mgr *mgr,
2065 2065 int i, r, num_pages;
2066 2066 const u64 GTT_MAX_PAGES = (AMDGPU_GTT_MAX_TRANSFER_SIZE >> PAGE_SHIFT);
2067 2067
2068 - if (ARRAY_SIZE(entity->gart_window_offs) < num_gart_windows)
2069 - return -EINVAL;
2070 -
2071 2068 r = drm_sched_entity_init(&entity->base, prio, scheds, num_schedulers, NULL);
2072 2069 if (r)
2073 2070 return r;
2074 2071
2075 2072 mutex_init(&entity->lock);
2076 2073
2074 + if (ARRAY_SIZE(entity->gart_window_offs) < num_gart_windows)
2075 + return -EINVAL;
2077 2076 if (num_gart_windows == 0)
2078 2077 return 0;
2079 2078
@@ -2081,7 +2080,6 @@ static int amdgpu_ttm_buffer_entity_init(struct amdgpu_gtt_mgr *mgr,
2081 2080 r = amdgpu_gtt_mgr_alloc_entries(mgr, &entity->gart_node, num_pages,
2082 2081 DRM_MM_INSERT_BEST);
2083 2082 if (r) {
2084 - mutex_destroy(&entity->lock);
2085 2083 drm_sched_entity_destroy(&entity->base);
2086 2084 return r;
2087 2085 }
@@ -2099,7 +2097,6 @@ static void amdgpu_ttm_buffer_entity_fini(struct amdgpu_gtt_mgr *mgr,
2099 2097 struct amdgpu_ttm_buffer_entity *entity)
2100 2098 {
2101 2099 amdgpu_gtt_mgr_free_entries(mgr, &entity->gart_node);
2102 - mutex_destroy(&entity->lock);
2103 2100 drm_sched_entity_destroy(&entity->base);
2104 2101 }
2105 2102
modified drivers/gpu/drm/amd/amdgpu/uvd_v3_1.c
@@ -288,7 +288,7 @@ static int uvd_v3_1_fw_validate(struct amdgpu_device *adev)
288 288 int i;
289 289 uint32_t keysel = adev->uvd.keyselect;
290 290
291 - if (RREG32(mmUVD_FW_START)) {
291 + if (RREG32(mmUVD_FW_START) & UVD_FW_STATUS__PASS_MASK) {
292 292 dev_dbg(adev->dev, "UVD keyselect already set: 0x%x (on CPU: 0x%x)\n",
293 293 RREG32(mmUVD_FW_START), adev->uvd.keyselect);
294 294 return 0;
modified drivers/gpu/drm/amd/pm/swsmu/smu14/smu_v14_0_2_ppt.c
@@ -89,7 +89,6 @@ static const struct smu_feature_bits smu_v14_0_2_dpm_features = {
89 89 #define PP_OD_FEATURE_FAN_TARGET_TEMPERATURE 9
90 90 #define PP_OD_FEATURE_FAN_MINIMUM_PWM 10
91 91 #define PP_OD_FEATURE_FAN_ZERO_RPM_ENABLE 11
92 - #define PP_OD_FEATURE_FAN_ZERO_RPM_STOP_TEMP 12
93 92
94 93 static struct cmn2asic_msg_mapping smu_v14_0_2_message_map[SMU_MSG_MAX_COUNT] = {
95 94 MSG_MAP(TestMessage, PPSMC_MSG_TestMessage, 1),
@@ -1021,10 +1020,6 @@ static void smu_v14_0_2_get_od_setting_limits(struct smu_context *smu,
1021 1020 od_min_setting = overdrive_lowerlimits->FanZeroRpmEnable;
1022 1021 od_max_setting = overdrive_upperlimits->FanZeroRpmEnable;
1023 1022 break;
1024 - case PP_OD_FEATURE_FAN_ZERO_RPM_STOP_TEMP:
1025 - od_min_setting = pptable->SkuTable.MsgLimits.FanStopTempMin;
1026 - od_max_setting = pptable->SkuTable.MsgLimits.FanStopTempMax;
1027 - break;
1028 1023 default:
1029 1024 od_min_setting = od_max_setting = INT_MAX;
1030 1025 break;
@@ -1249,24 +1244,6 @@ static int smu_v14_0_2_emit_clk_levels(struct smu_context *smu,
1249 1244 min_value, max_value);
1250 1245 break;
1251 1246
1252 - case SMU_OD_FAN_ZERO_RPM_STOP_TEMP:
1253 - if (!smu_v14_0_2_is_od_feature_supported(smu,
1254 - PP_OD_FEATURE_ZERO_FAN_BIT))
1255 - break;
1256 -
1257 - size += sysfs_emit_at(buf, size, "FAN_ZERO_RPM_STOP_TEMPERATURE:\n");
1258 - size += sysfs_emit_at(buf, size, "%d\n",
1259 - (int)od_table->OverDriveTable.FanZeroRpmStopTemp);
1260 -
1261 - size += sysfs_emit_at(buf, size, "%s:\n", "OD_RANGE");
1262 - smu_v14_0_2_get_od_setting_limits(smu,
1263 - PP_OD_FEATURE_FAN_ZERO_RPM_STOP_TEMP,
1264 - &min_value,
1265 - &max_value);
1266 - size += sysfs_emit_at(buf, size, "ZERO_RPM_STOP_TEMPERATURE: %u %u\n",
1267 - min_value, max_value);
1268 - break;
1269 -
1270 1247 case SMU_OD_RANGE:
1271 1248 if (!smu_v14_0_2_is_od_feature_supported(smu, PP_OD_FEATURE_GFXCLK_BIT) &&
1272 1249 !smu_v14_0_2_is_od_feature_supported(smu, PP_OD_FEATURE_UCLK_BIT) &&
@@ -2334,9 +2311,7 @@ static void smu_v14_0_2_set_supported_od_feature_mask(struct smu_context *smu)
2334 2311 OD_OPS_SUPPORT_FAN_MINIMUM_PWM_RETRIEVE |
2335 2312 OD_OPS_SUPPORT_FAN_MINIMUM_PWM_SET |
2336 2313 OD_OPS_SUPPORT_FAN_ZERO_RPM_ENABLE_RETRIEVE |
2337 - OD_OPS_SUPPORT_FAN_ZERO_RPM_ENABLE_SET |
2338 - OD_OPS_SUPPORT_FAN_ZERO_RPM_STOP_TEMP_RETRIEVE |
2339 - OD_OPS_SUPPORT_FAN_ZERO_RPM_STOP_TEMP_SET;
2314 + OD_OPS_SUPPORT_FAN_ZERO_RPM_ENABLE_SET;
2340 2315 }
2341 2316
2342 2317 static int smu_v14_0_2_get_overdrive_table(struct smu_context *smu,
@@ -2417,8 +2392,6 @@ static int smu_v14_0_2_set_default_od_settings(struct smu_context *smu)
2417 2392 user_od_table_bak.OverDriveTable.FanMinimumPwm;
2418 2393 user_od_table->OverDriveTable.FanZeroRpmEnable =
2419 2394 user_od_table_bak.OverDriveTable.FanZeroRpmEnable;
2420 - user_od_table->OverDriveTable.FanZeroRpmStopTemp =
2421 - user_od_table_bak.OverDriveTable.FanZeroRpmStopTemp;
2422 2395 }
2423 2396
2424 2397 smu_v14_0_2_set_supported_od_feature_mask(smu);
@@ -2473,11 +2446,6 @@ static int smu_v14_0_2_od_restore_table_single(struct smu_context *smu, long inp
2473 2446 boot_overdrive_table->OverDriveTable.FanZeroRpmEnable;
2474 2447 od_table->OverDriveTable.FeatureCtrlMask |= BIT(PP_OD_FEATURE_ZERO_FAN_BIT);
2475 2448 break;
2476 - case PP_OD_EDIT_FAN_ZERO_RPM_STOP_TEMP:
2477 - od_table->OverDriveTable.FanZeroRpmStopTemp =
2478 - boot_overdrive_table->OverDriveTable.FanZeroRpmStopTemp;
2479 - od_table->OverDriveTable.FeatureCtrlMask |= BIT(PP_OD_FEATURE_ZERO_FAN_BIT);
2480 - break;
2481 2449 case PP_OD_EDIT_ACOUSTIC_LIMIT:
2482 2450 od_table->OverDriveTable.AcousticLimitRpmThreshold =
2483 2451 boot_overdrive_table->OverDriveTable.AcousticLimitRpmThreshold;
@@ -2784,27 +2752,6 @@ static int smu_v14_0_2_od_edit_dpm_table(struct smu_context *smu,
2784 2752 od_table->OverDriveTable.FeatureCtrlMask |= BIT(PP_OD_FEATURE_ZERO_FAN_BIT);
2785 2753 break;
2786 2754
2787 - case PP_OD_EDIT_FAN_ZERO_RPM_STOP_TEMP:
2788 - if (!smu_v14_0_2_is_od_feature_supported(smu, PP_OD_FEATURE_ZERO_FAN_BIT)) {
2789 - dev_warn(adev->dev, "Zero RPM setting not supported!\n");
2790 - return -ENOTSUPP;
2791 - }
2792 -
2793 - smu_v14_0_2_get_od_setting_limits(smu,
2794 - PP_OD_FEATURE_FAN_ZERO_RPM_STOP_TEMP,
2795 - &minimum,
2796 - &maximum);
2797 - if (input[0] < minimum ||
2798 - input[0] > maximum) {
2799 - dev_info(adev->dev, "zero RPM stop temperature setting(%ld) must be within [%d, %d]!\n",
2800 - input[0], minimum, maximum);
2801 - return -EINVAL;
2802 - }
2803 -
2804 - od_table->OverDriveTable.FanZeroRpmStopTemp = input[0];
2805 - od_table->OverDriveTable.FeatureCtrlMask |= BIT(PP_OD_FEATURE_ZERO_FAN_BIT);
2806 - break;
2807 -
2808 2755 case PP_OD_RESTORE_DEFAULT_TABLE:
2809 2756 if (size == 1) {
2810 2757 ret = smu_v14_0_2_od_restore_table_single(smu, input[0]);
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 {