nvdimm/namespace: Nest mapping locks under the bus lock
Dave Jiang
maintainerPATCH 1/1 nvdimm/namespace: Nest mapping locks under the bus lock
create_namespaces() takes every mapping lock in a region with mutex_lock_nested() and uses the mapping index as the subclass. Lockdep supports only 8 subclasses. A region with more mappings, such as a 12-way CXL pmem region, triggers:
DEBUG_LOCKS_WARN_ON(subclass >= MAX_LOCKDEP_SUBCLASSES)
and lockdep turns itself off.
The caller holds the nvdimm bus reconfig_mutex. Take the mapping locks with mutex_lock_nest_lock() under that lock instead. Lockdep then tracks any number of mapping locks.
--- drivers/nvdimm/namespace_devs.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
drivers/nvdimm/namespace_devs.c +9 -2
@@ -2031,6 +2031,7 @@ static struct device **scan_labels(struct nd_region *nd_region)static struct device **create_namespaces (struct nd_region *nd_region) { +struct nvdimm_bus *nvdimm_bus =walk_to_nvdimm_bus (&nd_region->dev);struct nd_mapping *nd_mapping;struct device **devs;int i;
@@ -2038,10 +2039,16 @@ static struct device **create_namespaces(struct nd_region *nd_region)if (nd_region->ndr_mappings ==0 )return NULL ; -/* lock down all mappings while we scan labels */ +/* +* Lock down all mappings while we scan labels. A region can have +* more mappings than lockdep has subclasses, so nest the mapping +* locks under the bus lock rather than giving each its own subclass. +*/ +lockdep_assert_held (&nvdimm_bus->reconfig_mutex);for (i =0 ; i < nd_region->ndr_mappings; i++) { nd_mapping = &nd_region->mapping[i]; -mutex_lock_nested (&nd_mapping->lock, i); +mutex_lock_nest_lock (&nd_mapping->lock, + &nvdimm_bus->reconfig_mutex); } devs =scan_labels (nd_region);
base-commit: aebb7274476403040d582c61478df8f4a7bf9cd3
Signature · 1 line
2.54.0
Quoted 7 lines · Dave Jiang
create_namespaces()takes every mapping lock in a region withmutex_lock_nested()and uses the mapping index as the subclass. Lockdep supports only 8 subclasses. A region with more mappings, such as a 12-way CXL pmem region, triggers:
DEBUG_LOCKS_WARN_ON(subclass >= MAX_LOCKDEP_SUBCLASSES)
Quoted 8 lines
and lockdep turns itself off.
The caller holds the nvdimm bus
reconfig_mutex. Take the mapping locks withmutex_lock_nest_lock()under that lock instead. Lockdep then tracks any number of mapping locks.Fixes ae8219f186d8 ("libnvdimm, label: convert label tracking to a linked list")Signed-off-by Dave Jiang <[email protected]>
This was testable (and indeed DaveJ found it) with the unit test that adds a 12-way CXL pmem region, cxl-region-finer-gran.sh. Thanks Dave!
Quoted 41 lines
--- drivers/nvdimm/namespace_devs.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)drivers/nvdimm/namespace_devs.c +9
-2@@ -2031,6 +2031,7 @@ static struct device **scan_labels(struct nd_region *nd_region)static struct device **create_namespaces (struct nd_region *nd_region) { +struct nvdimm_bus *nvdimm_bus =walk_to_nvdimm_bus (&nd_region->dev);struct nd_mapping *nd_mapping;struct device **devs;int i;@@ -2038,10 +2039,16 @@ static struct device **create_namespaces(struct nd_region *nd_region)if (nd_region->ndr_mappings ==0 )return NULL ; -/* lock down all mappings while we scan labels */ +/* +* Lock down all mappings while we scan labels. A region can have +* more mappings than lockdep has subclasses, so nest the mapping +* locks under the bus lock rather than giving each its own subclass. +*/ +lockdep_assert_held (&nvdimm_bus->reconfig_mutex);for (i =0 ; i < nd_region->ndr_mappings; i++) { nd_mapping = &nd_region->mapping[i]; -mutex_lock_nested (&nd_mapping->lock, i); +mutex_lock_nest_lock (&nd_mapping->lock, + &nvdimm_bus->reconfig_mutex); } devs =scan_labels (nd_region);base-commit:
aebb7274476403040d582c61478df8f4a7bf9cd3Signature · 1 line
2.54.0
Quoted 5 lines · Dave Jiang
create_namespaces()takes every mapping lock in a region withmutex_lock_nested()and uses the mapping index as the subclass. Lockdep supports only 8 subclasses. A region with more mappings, such as a 12-way CXL pmem region, triggers:
Quoted 7 lines
DEBUG_LOCKS_WARN_ON(subclass >= MAX_LOCKDEP_SUBCLASSES)and lockdep turns itself off.
The caller holds the nvdimm bus
reconfig_mutex. Take the mapping locks withmutex_lock_nest_lock()under that lock instead. Lockdep then tracks any number of mapping locks.
Applied to libnvdimm-fixes:
https://git.kernel.org//nvdimm/nvdimm/c/5a277aa7eb4d
Quoted 44 lines
Fixes ae8219f186d8 ("libnvdimm, label: convert label tracking to a linked list")Signed-off-by Dave Jiang <[email protected]>--- drivers/nvdimm/namespace_devs.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)drivers/nvdimm/namespace_devs.c +9
-2@@ -2031,6 +2031,7 @@ static struct device **scan_labels(struct nd_region *nd_region)static struct device **create_namespaces (struct nd_region *nd_region) { +struct nvdimm_bus *nvdimm_bus =walk_to_nvdimm_bus (&nd_region->dev);struct nd_mapping *nd_mapping;struct device **devs;int i;@@ -2038,10 +2039,16 @@ static struct device **create_namespaces(struct nd_region *nd_region)if (nd_region->ndr_mappings ==0 )return NULL ; -/* lock down all mappings while we scan labels */ +/* +* Lock down all mappings while we scan labels. A region can have +* more mappings than lockdep has subclasses, so nest the mapping +* locks under the bus lock rather than giving each its own subclass. +*/ +lockdep_assert_held (&nvdimm_bus->reconfig_mutex);for (i =0 ; i < nd_region->ndr_mappings; i++) { nd_mapping = &nd_region->mapping[i]; -mutex_lock_nested (&nd_mapping->lock, i); +mutex_lock_nest_lock (&nd_mapping->lock, + &nvdimm_bus->reconfig_mutex); } devs =scan_labels (nd_region);base-commit:
aebb7274476403040d582c61478df8f4a7bf9cd3Signature · 1 line
2.54.0