Compare

bd22f7e63509fa758cf500d9af3e88a3d3df96be..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, +27 −30
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/gpio/gpio-nct6694.c
@@ -444,7 +444,7 @@ static int nct6694_gpio_probe(struct platform_device *pdev)
444 444 data->gpio.set_config = nct6694_set_config;
445 445 data->gpio.init_valid_mask = nct6694_init_valid_mask;
446 446 data->gpio.base = -1;
447 - data->gpio.can_sleep = true;
447 + data->gpio.can_sleep = false;
448 448 data->gpio.owner = THIS_MODULE;
449 449 data->gpio.ngpio = NCT6694_NR_GPIO;
450 450
modified drivers/gpio/gpiolib-shared.c
@@ -450,6 +450,7 @@ int gpio_shared_add_proxy_lookup(struct device *consumer, struct fwnode_handle *
450 450 const char *con_id, unsigned long lflags)
451 451 {
452 452 const char *dev_id = dev_name(consumer);
453 + struct gpiod_lookup_table *lookup;
453 454 struct gpio_shared_entry *entry;
454 455 struct gpio_shared_ref *ref;
455 456
@@ -479,22 +480,18 @@ int gpio_shared_add_proxy_lookup(struct device *consumer, struct fwnode_handle *
479 480 if (!key)
480 481 return -ENOMEM;
481 482
482 - struct gpiod_lookup_table *lookup __free(kfree) =
483 - kzalloc_flex(*lookup, table, 2);
483 + lookup = kzalloc_flex(*lookup, table, 2);
484 484 if (!lookup)
485 485 return -ENOMEM;
486 486
487 487 pr_debug("Adding machine lookup entry for a shared GPIO for consumer %s, with key '%s' and con_id '%s'\n",
488 488 dev_id, key, ref->con_id ?: "none");
489 489
490 - lookup->dev_id = kstrdup(dev_id, GFP_KERNEL);
491 - if (!lookup->dev_id)
492 - return -ENOMEM;
493 -
490 + lookup->dev_id = dev_id;
494 491 lookup->table[0] = GPIO_LOOKUP(no_free_ptr(key), 0,
495 492 ref->con_id, lflags);
496 493
497 - ref->lookup = no_free_ptr(lookup);
494 + ref->lookup = lookup;
498 495 gpiod_add_lookup_table(ref->lookup);
499 496
500 497 return 0;
@@ -614,7 +611,6 @@ void gpio_device_teardown_shared(struct gpio_device *gdev)
614 611 if (ref->lookup) {
615 612 gpiod_remove_lookup_table(ref->lookup);
616 613 kfree(ref->lookup->table[0].key);
617 - kfree(ref->lookup->dev_id);
618 614 kfree(ref->lookup);
619 615 ref->lookup = NULL;
620 616 }
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.
modified tools/gpio/Makefile
@@ -7,13 +7,7 @@ bindir ?= /usr/bin
7 7 # isn't set and when invoked from selftests build, where srctree
8 8 # is set to ".". building_out_of_srctree is undefined for in srctree
9 9 # builds
10 - ifeq ($(srctree),)
11 - update_srctree := 1
12 - endif
13 10 ifndef building_out_of_srctree
14 - update_srctree := 1
15 - endif
16 - ifeq ($(update_srctree),1)
17 11 srctree := $(patsubst %/,%,$(dir $(CURDIR)))
18 12 srctree := $(patsubst %/,%,$(dir $(srctree)))
19 13 endif