Compare

70db8dea2aac43dd4f9a7ec2365d8c950efaa07d..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
9 files changed, +32 −105
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/i915/display/intel_cx0_phy.c
@@ -2342,14 +2342,12 @@ static void intel_c10pll_dump_hw_state(struct drm_printer *p,
2342 2342 static bool is_arrowlake_s_by_host_bridge(void)
2343 2343 {
2344 2344 struct pci_dev *pdev = NULL;
2345 - while ((pdev = pci_get_class(PCI_CLASS_BRIDGE_HOST << 8, pdev))) {
2346 - if (IS_ARROWLAKE_S_BY_HOST_BRIDGE_ID(pdev->device)) {
2347 - pci_dev_put(pdev);
2348 - return true;
2349 - }
2350 - }
2345 + u16 host_bridge_pci_dev_id;
2351 2346
2352 - return false;
2347 + while ((pdev = pci_get_class(PCI_CLASS_BRIDGE_HOST << 8, pdev)))
2348 + host_bridge_pci_dev_id = pdev->device;
2349 +
2350 + return pdev && IS_ARROWLAKE_S_BY_HOST_BRIDGE_ID(host_bridge_pci_dev_id);
2353 2351 }
2354 2352
2355 2353 static u16 intel_c20_hdmi_tmds_tx_cgf_1(struct intel_display *display)
modified drivers/gpu/drm/i915/display/intel_dmc.c
@@ -1023,55 +1023,6 @@ static void dmc_set_fw_offset(struct intel_dmc *dmc,
1023 1023 }
1024 1024 }
1025 1025
1026 - /*
1027 - * Check if the load address is within the valid range for the given DMC ID.
1028 - */
1029 - static bool dmc_load_addr_sanity_check(struct intel_dmc *dmc,
1030 - u32 start_addr, u32 payload_size,
1031 - int header_ver, enum intel_dmc_id dmc_id)
1032 - {
1033 - u32 start_range, end_range, end_addr;
1034 -
1035 - if (header_ver != 3)
1036 - return true;
1037 -
1038 - switch (dmc_id) {
1039 - case DMC_FW_MAIN:
1040 - start_range = DMC_MAIN_PROGRAM_BASE_START;
1041 - end_range = DMC_MAIN_PROGRAM_BASE_END;
1042 - break;
1043 - case DMC_FW_PIPEA:
1044 - start_range = DMC_PIPEA_PROGRAM_BASE_START;
1045 - end_range = DMC_PIPEA_PROGRAM_BASE_END;
1046 - break;
1047 - case DMC_FW_PIPEB:
1048 - start_range = DMC_PIPEB_PROGRAM_BASE_START;
1049 - end_range = DMC_PIPEB_PROGRAM_BASE_END;
1050 - break;
1051 - case DMC_FW_PIPEC:
1052 - start_range = DMC_PIPEC_PROGRAM_BASE_START;
1053 - end_range = DMC_PIPEC_PROGRAM_BASE_END;
1054 - break;
1055 - case DMC_FW_PIPED:
1056 - start_range = DMC_PIPED_PROGRAM_BASE_START;
1057 - end_range = DMC_PIPED_PROGRAM_BASE_END;
1058 - break;
1059 - default:
1060 - MISSING_CASE(dmc_id);
1061 - return false;
1062 - }
1063 -
1064 - if (payload_size == 0)
1065 - end_addr = start_addr;
1066 - else if (check_add_overflow(start_addr, payload_size - 1, &end_addr))
1067 - return false;
1068 -
1069 - if (start_addr < start_range || end_addr > end_range)
1070 - return false;
1071 -
1072 - return true;
1073 - }
1074 -
1075 1026 static bool dmc_mmio_addr_sanity_check(struct intel_dmc *dmc,
1076 1027 const u32 *mmioaddr, u32 mmio_count,
1077 1028 int header_ver, enum intel_dmc_id dmc_id)
@@ -1169,7 +1120,7 @@ static u32 parse_dmc_fw_header(struct intel_dmc *dmc,
1169 1120 }
1170 1121
1171 1122 /* Cache the dmc header info. */
1172 - if (!mmio_count || mmio_count > mmio_count_max) {
1123 + if (mmio_count > mmio_count_max) {
1173 1124 drm_err(display->drm, "DMC firmware has wrong mmio count %u\n", mmio_count);
1174 1125 return 0;
1175 1126 }
@@ -1215,21 +1166,13 @@ static u32 parse_dmc_fw_header(struct intel_dmc *dmc,
1215 1166 disable_dmc_evt(display, dmc_id, dmc_info->mmioaddr[i],
1216 1167 dmc_info->mmiodata[i]) ? " (disabling)" : "");
1217 1168 }
1169 + dmc_info->mmio_count = mmio_count;
1170 + dmc_info->start_mmioaddr = start_mmioaddr;
1218 1171
1219 1172 rem_size -= header_len_bytes;
1220 1173
1221 1174 /* fw_size is in dwords, so multiplied by 4 to convert into bytes. */
1222 - if (check_mul_overflow(dmc_header->fw_size, 4u, &payload_size)) {
1223 - drm_err(display->drm, "DMC fw_size too large (%u dwords)\n",
1224 - dmc_header->fw_size);
1225 - return 0;
1226 - }
1227 -
1228 - if (!payload_size) {
1229 - drm_err(display->drm, "DMC firmware has empty payload\n");
1230 - return 0;
1231 - }
1232 -
1175 + payload_size = dmc_header->fw_size * 4;
1233 1176 if (rem_size < payload_size)
1234 1177 goto error_truncated;
1235 1178
@@ -1237,21 +1180,12 @@ static u32 parse_dmc_fw_header(struct intel_dmc *dmc,
1237 1180 drm_err(display->drm, "DMC FW too big (%u bytes)\n", payload_size);
1238 1181 return 0;
1239 1182 }
1240 -
1241 - if (!dmc_load_addr_sanity_check(dmc, start_mmioaddr, payload_size,
1242 - dmc_header->header_ver, dmc_id)) {
1243 - drm_err(display->drm, "DMC %d: firmware has wrong load address\n", dmc_id);
1244 - return 0;
1245 - }
1183 + dmc_info->dmc_fw_size = dmc_header->fw_size;
1246 1184
1247 1185 dmc_info->payload = kmalloc(payload_size, GFP_KERNEL);
1248 1186 if (!dmc_info->payload)
1249 1187 return 0;
1250 1188
1251 - dmc_info->mmio_count = mmio_count;
1252 - dmc_info->start_mmioaddr = start_mmioaddr;
1253 - dmc_info->dmc_fw_size = dmc_header->fw_size;
1254 -
1255 1189 payload = (u8 *)(dmc_header) + header_len_bytes;
1256 1190 memcpy(dmc_info->payload, payload, payload_size);
1257 1191
modified drivers/gpu/drm/i915/display/intel_dmc_regs.h
@@ -521,18 +521,6 @@ enum pipedmc_event_id {
521 521 #define TGL_PIPE_MMIO_END(dmc_id) _PICK_EVEN(((dmc_id) - 1), _TGL_PIPEA_MMIO_END,\
522 522 _TGL_PIPEB_MMIO_END)
523 523
524 - /* For DMC header version v3*/
525 - #define DMC_MAIN_PROGRAM_BASE_START 0x80000
526 - #define DMC_MAIN_PROGRAM_BASE_END 0x86fff
527 - #define DMC_PIPEA_PROGRAM_BASE_START 0x90000
528 - #define DMC_PIPEA_PROGRAM_BASE_END 0x96fff
529 - #define DMC_PIPEB_PROGRAM_BASE_START 0x98000
530 - #define DMC_PIPEB_PROGRAM_BASE_END 0x9efff
531 - #define DMC_PIPEC_PROGRAM_BASE_START 0x52000
532 - #define DMC_PIPEC_PROGRAM_BASE_END 0x53fff
533 - #define DMC_PIPED_PROGRAM_BASE_START 0x59000
534 - #define DMC_PIPED_PROGRAM_BASE_END 0x5afff
535 -
536 524 #define SKL_DMC_DC3_DC5_COUNT _MMIO(0x80030)
537 525 #define SKL_DMC_DC5_DC6_COUNT _MMIO(0x8002C)
538 526 #define BXT_DMC_DC3_DC5_COUNT _MMIO(0x80038)
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.