Compare

55a67aa89342545b89f469dea6f29bfb6fac6f3d..d5a821f66047c2effd06147b84775f9b2ce0f4aa

regmap: Fix cache lifecycle locking and teardown

Michael Reeves <michael.reeves077@gmail.com> says:

Fix an "Invalid wait context" lockdep warning during Apple GPIO probe and
correct debugfs ordering during cache teardown.

Tested on Apple M3 MacBook Air (T8122):

- All 4 GPIO controllers probe successfully with lockdep enabled.
- The "Invalid wait context" warning no longer occurs.
- The regmap KUnit suite passes and lockdep remains enabled.

Link: https://patch.msgid.link/20261006-regcache-lifecycle-submit-v3-0-37bdd82fd47c@gmail.com

Mark Brown <broonie@kernel.org> · 2026-10-12 23:11 UTC

Browse files
14 files changed, +59 −114
modified drivers/base/regmap/regcache-maple.c
@@ -312,16 +312,21 @@ static void regcache_maple_exit(struct regmap *map)
312 312 struct maple_tree *mt = map->cache;
313 313 MA_STATE(mas, mt, 0, UINT_MAX);
314 314 unsigned int *entry;
315 + unsigned long flags;
315 316
316 317 /* if we've already been called then just return */
317 318 if (!mt)
318 319 return;
319 320
320 - mas_lock(&mas);
321 + /*
322 + * Register access may take the Maple lock from hard IRQ context, so
323 + * preserve its IRQ-safe lockdep state during teardown.
324 + */
325 + mas_lock_irqsave(&mas, flags);
321 326 mas_for_each(&mas, entry, UINT_MAX)
322 327 kfree(entry);
323 328 __mt_destroy(mt);
324 - mas_unlock(&mas);
329 + mas_unlock_irqrestore(&mas, flags);
325 330
326 331 kfree(mt);
327 332 map->cache = NULL;
modified drivers/base/regmap/regcache.c
@@ -225,9 +225,7 @@ int regcache_init(struct regmap *map, const struct regmap_config *config)
225 225 if (map->cache_ops->init) {
226 226 dev_dbg(map->dev, "Initializing %s cache\n",
227 227 map->cache_ops->name);
228 - map->lock(map->lock_arg);
229 228 ret = map->cache_ops->init(map);
230 - map->unlock(map->lock_arg);
231 229 if (ret)
232 230 goto err_free_reg_defaults;
233 231 }
@@ -259,9 +257,7 @@ err_free:
259 257 err_exit:
260 258 if (map->cache_ops->exit) {
261 259 dev_dbg(map->dev, "Destroying %s cache\n", map->cache_ops->name);
262 - map->lock(map->lock_arg);
263 260 map->cache_ops->exit(map);
264 - map->unlock(map->lock_arg);
265 261 }
266 262 err_free_reg_defaults:
267 263 kfree(map->reg_defaults);
@@ -281,9 +277,7 @@ void regcache_exit(struct regmap *map)
281 277 if (map->cache_ops->exit) {
282 278 dev_dbg(map->dev, "Destroying %s cache\n",
283 279 map->cache_ops->name);
284 - map->lock(map->lock_arg);
285 280 map->cache_ops->exit(map);
286 - map->unlock(map->lock_arg);
287 281 }
288 282
289 283 kfree(map->reg_defaults);
modified drivers/base/regmap/regmap-debugfs.c
@@ -664,6 +664,7 @@ void regmap_debugfs_exit(struct regmap *map)
664 664 {
665 665 if (map->debugfs) {
666 666 debugfs_remove_recursive(map->debugfs);
667 + map->debugfs = NULL;
667 668 mutex_lock(&map->cache_lock);
668 669 regmap_debugfs_free_dump_cache(map);
669 670 mutex_unlock(&map->cache_lock);
modified drivers/base/regmap/regmap-irq.c
@@ -963,7 +963,7 @@ int regmap_add_irq_chip_fwnode(struct fwnode_handle *fwnode,
963 963 return 0;
964 964
965 965 err_domain:
966 - /* Should really dispose of the domain but... */
966 + irq_domain_remove(d->domain);
967 967 err_mutex:
968 968 mutex_destroy(&d->lock);
969 969 lockdep_unregister_key(&d->lock_key);
modified drivers/base/regmap/regmap.c
@@ -1422,8 +1422,8 @@ int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
1422 1422 {
1423 1423 int ret;
1424 1424
1425 - regcache_exit(map);
1426 1425 regmap_debugfs_exit(map);
1426 + regcache_exit(map);
1427 1427
1428 1428 map->max_register = config->max_register;
1429 1429 map->max_register_is_set = map->max_register ?: config->max_register_is_0;
@@ -1440,12 +1440,16 @@ int regmap_reinit_cache(struct regmap *map, const struct regmap_config *config)
1440 1440 if (ret)
1441 1441 return ret;
1442 1442
1443 - regmap_debugfs_init(map);
1444 -
1445 1443 map->cache_bypass = false;
1446 1444 map->cache_only = false;
1447 1445
1448 - return regcache_init(map, config);
1446 + ret = regcache_init(map, config);
1447 + if (ret)
1448 + return ret;
1449 +
1450 + regmap_debugfs_init(map);
1451 +
1452 + return 0;
1449 1453 }
1450 1454 EXPORT_SYMBOL_GPL(regmap_reinit_cache);
1451 1455
@@ -1459,9 +1463,8 @@ void regmap_exit(struct regmap *map)
1459 1463 struct regmap_async *async;
1460 1464
1461 1465 regmap_detach_dev(map->dev, map);
1462 - regcache_exit(map);
1463 -
1464 1466 regmap_debugfs_exit(map);
1467 + regcache_exit(map);
1465 1468 regmap_range_exit(map);
1466 1469 if (map->bus && map->bus->free_context)
1467 1470 map->bus->free_context(map->bus_context);
modified drivers/gpu/drm/amd/amdgpu/amdgpu_cs.c
@@ -1431,7 +1431,6 @@ static void amdgpu_cs_parser_fini(struct amdgpu_cs_parser *parser)
1431 1431 int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
1432 1432 {
1433 1433 struct amdgpu_device *adev = drm_to_adev(dev);
1434 - struct amdgpu_bo_list_entry *e;
1435 1434 struct amdgpu_cs_parser parser;
1436 1435 int r;
1437 1436
@@ -1466,31 +1465,25 @@ int amdgpu_cs_ioctl(struct drm_device *dev, void *data, struct drm_file *filp)
1466 1465
1467 1466 r = amdgpu_cs_patch_jobs(&parser);
1468 1467 if (r)
1469 - goto error_free_user_pages;
1468 + goto error_fini;
1470 1469
1471 1470 r = amdgpu_cs_vm_handling(&parser);
1472 1471 if (r)
1473 - goto error_free_user_pages;
1472 + goto error_fini;
1474 1473
1475 1474 r = amdgpu_cs_sync_rings(&parser);
1476 1475 if (r)
1477 - goto error_free_user_pages;
1476 + goto error_fini;
1478 1477
1479 1478 trace_amdgpu_cs_ibs(&parser);
1480 1479
1481 1480 r = amdgpu_cs_submit(&parser, data);
1482 1481 if (r)
1483 - goto error_free_user_pages;
1482 + goto error_fini;
1484 1483
1485 1484 amdgpu_cs_parser_fini(&parser);
1486 1485 return 0;
1487 1486
1488 - error_free_user_pages:
1489 - /* amdgpu_cs_submit() only drops the ranges once it got that far. */
1490 - amdgpu_bo_list_for_each_userptr_entry(e, parser.bo_list) {
1491 - amdgpu_hmm_range_free(e->range);
1492 - e->range = NULL;
1493 - }
1494 1487 error_fini:
1495 1488 amdgpu_cs_parser_fini(&parser);
1496 1489 return r;
modified drivers/gpu/drm/amd/amdgpu/amdgpu_debugfs.c
@@ -1266,6 +1266,7 @@ static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
1266 1266 {
1267 1267 struct amdgpu_device *adev = f->f_inode->i_private;
1268 1268 int r;
1269 + ssize_t result = 0;
1269 1270 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
1270 1271
1271 1272 if (size > 4096 || size & 3 || *pos & 3)
@@ -1309,11 +1310,25 @@ static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
1309 1310 mutex_unlock(&adev->grbm_idx_mutex);
1310 1311
1311 1312 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
1312 - amdgpu_virt_disable_access_debugfs(adev);
1313 1313
1314 - r = copy_to_user(buf, data, size) ? -EFAULT : size;
1314 + while (size) {
1315 + uint32_t value;
1316 +
1317 + value = data[result >> 2];
1318 + r = put_user(value, (uint32_t *)buf);
1319 + if (r) {
1320 + amdgpu_virt_disable_access_debugfs(adev);
1321 + goto err;
1322 + }
1323 +
1324 + result += 4;
1325 + buf += 4;
1326 + size -= 4;
1327 + }
1328 +
1315 1329 kfree(data);
1316 - return r;
1330 + amdgpu_virt_disable_access_debugfs(adev);
1331 + return result;
1317 1332
1318 1333 err:
1319 1334 pm_runtime_put_autosuspend(adev_to_drm(adev)->dev);
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);
@@ -1238,7 +1236,7 @@ int amdgpu_gem_list_handles_ioctl(struct drm_device *dev, void *data,
1238 1236 bo_entry->alloc_flags = bo->flags & AMDGPU_GEM_CREATE_SETTABLE_MASK;
1239 1237 bo_entry->preferred_domains = bo->preferred_domains;
1240 1238 bo_entry->gem_handle = id;
1241 - bo_entry->alignment = (u64)bo->tbo.page_alignment << PAGE_SHIFT;
1239 + bo_entry->alignment = bo->tbo.page_alignment;
1242 1240
1243 1241 if (bo->tbo.base.import_attach)
1244 1242 bo_entry->flags |= AMDGPU_GEM_LIST_HANDLES_FLAG_IS_IMPORT;
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/amdgpu_userq.c
@@ -980,21 +980,19 @@ amdgpu_userq_bo_validate(struct amdgpu_device *adev, struct drm_exec *exec,
980 980 {
981 981 struct ttm_operation_ctx ctx = { false, false };
982 982 struct amdgpu_bo_va *bo_va;
983 - struct amdgpu_bo *bo, *tmp;
983 + struct amdgpu_bo *bo;
984 984 int ret;
985 985
986 986 spin_lock(&vm->individual_lock);
987 - while (!list_empty(&vm->individual.evicted)) {
988 - bo_va = list_first_entry(&vm->individual.evicted,
987 + while (!list_empty(&vm->always_valid.evicted)) {
988 + bo_va = list_first_entry(&vm->always_valid.evicted,
989 989 struct amdgpu_bo_va,
990 990 base.vm_status);
991 - bo = bo_va->base.bo;
992 - tmp = amdgpu_bo_ref(bo);
993 991 spin_unlock(&vm->individual_lock);
994 992
993 + bo = bo_va->base.bo;
995 994 ret = drm_exec_prepare_obj(exec, &bo->tbo.base,
996 995 TTM_NUM_MOVE_FENCES + 1);
997 - amdgpu_bo_unref(&tmp);
998 996 if (unlikely(ret))
999 997 return ret;
1000 998
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/maple_tree.h
@@ -507,9 +507,13 @@ struct ma_wr_state {
507 507 };
508 508
509 509 #define mas_lock(mas) spin_lock(&((mas)->tree->ma_lock))
510 + #define mas_lock_irqsave(mas, flags) \
511 + spin_lock_irqsave(&((mas)->tree->ma_lock), flags)
510 512 #define mas_lock_nested(mas, subclass) \
511 513 spin_lock_nested(&((mas)->tree->ma_lock), subclass)
512 514 #define mas_unlock(mas) spin_unlock(&((mas)->tree->ma_lock))
515 + #define mas_unlock_irqrestore(mas, flags) \
516 + spin_unlock_irqrestore(&((mas)->tree->ma_lock), flags)
513 517
514 518 /*
515 519 * Special values for ma_state.node.