cxl/region: Remove lock from memory notifier callback
PATCH v3 1/1 cxl/region: Remove lock from memory notifier callback
In testing Dynamic Capacity Device (DCD) support, a lockdep splat revealed an ABBA issue between the memory notifiers and the DCD extent processing code.[0] Changing the lock ordering within DCD proved difficult because regions must be stable while searching for the proper region and then the device lock must be held to properly notify the DAX region driver of memory changes.
Dan points out in the thread that notifiers should be able to trust that it is safe to access static data. Region data is static once the device is realized and until it's destruction. Thus it is better to manage the notifiers within the region driver.
Remove the need for a lock by ensuring the notifiers are active only during the region's lifetime.
Furthermore, remove cxl_region_nid() because resource can't be NULL while the region is stable.
---
Changes in v3:
- [Ying: Add comment regarding cxl_region_rwsem]
- [iweiny: pickup tags]
- Link to v2: https://patch.msgid.link/[email protected]
Changes in v2:
- [djbw: remove cxl_region_nid()]
- Link to v1: https://patch.msgid.link/[email protected]
--- drivers/cxl/core/region.c | 54 ++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 24 deletions(-)
drivers/cxl/core/region.c +30 -24
@@ -2313,8 +2313,6 @@ static void unregister_region(void *_cxlr)struct cxl_region_params *p = &cxlr->params;int i; -unregister_memory_notifier (&cxlr->memory_notifier); -unregister_mt_adistance_algorithm (&cxlr->adist_notifier);device_del (&cxlr->dev);/*
@@ -2391,18 +2389,6 @@ static bool cxl_region_update_coordinates(struct cxl_region *cxlr, int nid)return true; } -static int cxl_region_nid (struct cxl_region *cxlr) -{ -struct cxl_region_params *p = &cxlr->params; -struct resource *res; - -guard (rwsem_read)(&cxl_region_rwsem); - res = p->res; -if (!res) -return NUMA_NO_NODE ; -return phys_to_target_node (res->start); -} -static int cxl_region_perf_attrs_callback (struct notifier_block *nb,unsigned long action,void *arg) {
@@ -2415,7 +2401,11 @@ static int cxl_region_perf_attrs_callback(struct notifier_block *nb,if (nid ==NUMA_NO_NODE || action !=MEM_ONLINE )return NOTIFY_DONE ; - region_nid =cxl_region_nid (cxlr); +/* +* No need to hold cxl_region_rwsem; region parameters are stable +* within the cxl_region driver. +*/ + region_nid =phys_to_target_node (cxlr->params.res->start);if (nid != region_nid)return NOTIFY_DONE ;
@@ -2434,7 +2424,11 @@ static int cxl_region_calculate_adistance(struct notifier_block *nb,int *adist = data;int region_nid; - region_nid =cxl_region_nid (cxlr); +/* +* No need to hold cxl_region_rwsem; region parameters are stable +* within the cxl_region driver. +*/ + region_nid =phys_to_target_node (cxlr->params.res->start);if (nid != region_nid)return NOTIFY_OK ;
@@ -2484,14 +2478,6 @@ static struct cxl_region *devm_cxl_add_region(struct cxl_root_decoder *cxlrd,if (rc) goto err; - cxlr->memory_notifier.notifier_call = cxl_region_perf_attrs_callback; - cxlr->memory_notifier.priority =CXL_CALLBACK_PRI ; -register_memory_notifier (&cxlr->memory_notifier); - - cxlr->adist_notifier.notifier_call = cxl_region_calculate_adistance; - cxlr->adist_notifier.priority =100 ; -register_mt_adistance_algorithm (&cxlr->adist_notifier); - rc =devm_add_action_or_reset (port->uport_dev, unregister_region, cxlr);if (rc)return ERR_PTR (rc);
@@ -3386,6 +3372,14 @@ static int is_system_ram(struct resource *res, void *arg)return 1 ; } +static void shutdown_notifiers (void *_cxlr) +{ +struct cxl_region *cxlr = _cxlr; + +unregister_memory_notifier (&cxlr->memory_notifier); +unregister_mt_adistance_algorithm (&cxlr->adist_notifier); +} +static int cxl_region_probe (struct device *dev) {struct cxl_region *cxlr =to_cxl_region (dev);
@@ -3418,6 +3412,18 @@ static int cxl_region_probe(struct device *dev) out:up_read (&cxl_region_rwsem); +if (rc) +return rc; + + cxlr->memory_notifier.notifier_call = cxl_region_perf_attrs_callback; + cxlr->memory_notifier.priority =CXL_CALLBACK_PRI ; +register_memory_notifier (&cxlr->memory_notifier); + + cxlr->adist_notifier.notifier_call = cxl_region_calculate_adistance; + cxlr->adist_notifier.priority =100 ; +register_mt_adistance_algorithm (&cxlr->adist_notifier); + + rc =devm_add_action_or_reset (&cxlr->dev, shutdown_notifiers, cxlr);if (rc)return rc;
---
base-commit: 67784a74e258a467225f0e68335df77acd67b7ab
change-id: 20240813-fix-notifiers-99c350b044a2
Best regards,
Signature · 1 line
Ira Weiny <[email protected]>
Quoted 18 lines · On Wed, 04 Sep 2024 09:47:54 -0500 Ira Weiny <[email protected]> wrote:
In testing Dynamic Capacity Device (DCD) support, a lockdep splat revealed an ABBA issue between the memory notifiers and the DCD extent processing code.[0] Changing the lock ordering within DCD proved difficult because regions must be stable while searching for the proper region and then the device lock must be held to properly notify the DAX region driver of memory changes.
Dan points out in the thread that notifiers should be able to trust that it is safe to access static data. Region data is static once the device is realized and until it's destruction. Thus it is better to manage the notifiers within the region driver.
Remove the need for a lock by ensuring the notifiers are active only during the region's lifetime.
Furthermore, remove
cxl_region_nid()because resource can't be NULL while the region is stable.
Quoted 7 lines
Cc Ying Huang <[email protected]>Suggested-by Dan Williams <[email protected]>Reviewed-by Davidlohr Bueso <[email protected]>Reviewed-by Jonathan Cameron <[email protected]>Reviewed-by Ying Huang <[email protected]>Signed-off-by Ira Weiny <[email protected]>
A few comments on looking at this again, but all things that apply equally to old code so maybe things for another day.
Jonathan
Quoted 87 lines
---
Changes in v3:
- [Ying: Add comment regardingcxl_region_rwsem]
- [iweiny: pickup tags]
- Link to v2: https://patch.msgid.link/[email protected]Changes in v2:
- [djbw: removecxl_region_nid()]
- Link to v1: https://patch.msgid.link/[email protected]--- drivers/cxl/core/region.c | 54 ++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 24 deletions(-)drivers/cxl/core/region.c +10
-24@@ -2313,8 +2313,6 @@ static void unregister_region(void *_cxlr)struct cxl_region_params *p = &cxlr->params;int i; -unregister_memory_notifier (&cxlr->memory_notifier); -unregister_mt_adistance_algorithm (&cxlr->adist_notifier);device_del (&cxlr->dev);/* @@ -2391,18 +2389,6 @@ static bool cxl_region_update_coordinates(struct cxl_region *cxlr, int nid)return true; } -static int cxl_region_nid (struct cxl_region *cxlr) -{ -struct cxl_region_params *p = &cxlr->params; -struct resource *res; - -guard (rwsem_read)(&cxl_region_rwsem); - res = p->res; -if (!res) -return NUMA_NO_NODE ; -return phys_to_target_node (res->start); -} -static int cxl_region_perf_attrs_callback (struct notifier_block *nb,unsigned long action,void *arg) {@@ -2415,7 +2401,11 @@ static int cxl_region_perf_attrs_callback(struct notifier_block *nb,if (nid ==NUMA_NO_NODE || action !=MEM_ONLINE )return NOTIFY_DONE ; - region_nid =cxl_region_nid (cxlr); +/* +* No need to hold cxl_region_rwsem; region parameters are stable +* within the cxl_region driver. +*/ + region_nid =phys_to_target_node (cxlr->params.res->start);if (nid != region_nid)return NOTIFY_DONE ;@@ -2434,7 +2424,11 @@ static int cxl_region_calculate_adistance(struct notifier_block *nb,int *adist = data;int region_nid; - region_nid =cxl_region_nid (cxlr); +/* +* No need to hold cxl_region_rwsem; region parameters are stable +* within the cxl_region driver. +*/ + region_nid =phys_to_target_node (cxlr->params.res->start);if (nid != region_nid)return NOTIFY_OK ;@@ -2484,14 +2478,6 @@ static struct cxl_region *devm_cxl_add_region(struct cxl_root_decoder *cxlrd,if (rc) goto err; - cxlr->memory_notifier.notifier_call = cxl_region_perf_attrs_callback; - cxlr->memory_notifier.priority =CXL_CALLBACK_PRI ; -register_memory_notifier (&cxlr->memory_notifier); - - cxlr->adist_notifier.notifier_call = cxl_region_calculate_adistance; - cxlr->adist_notifier.priority =100 ; -register_mt_adistance_algorithm (&cxlr->adist_notifier); - rc =devm_add_action_or_reset (port->uport_dev, unregister_region, cxlr);if (rc)return ERR_PTR (rc);
drivers/cxl/core/region.c static int is_system_ram(struct resource *res, void *arg) · line 3380
Quoted 9 lines
drivers/cxl/core/region.c +6
-0@@ -3386,6 +3372,14 @@ static int is_system_ram(struct resource *res, void *arg)return 1 ; } +static void shutdown_notifiers (void *_cxlr) +{ +struct cxl_region *cxlr = _cxlr; + +unregister_memory_notifier (&cxlr->memory_notifier); +unregister_mt_adistance_algorithm (&cxlr->adist_notifier);
Flip order.
Makes zero real difference, but if we later end up with more to do here for some reason there may be ordering requirements that will care that this doesn't tear down in reverse of setup.
Mind you, see below.
Quoted 6 lines
drivers/cxl/core/region.c +2
-0+} +static int cxl_region_probe (struct device *dev) {struct cxl_region *cxlr =to_cxl_region (dev);
drivers/cxl/core/region.c static int cxl_region_probe(struct device *dev) · line 3420
Quoted 9 lines
drivers/cxl/core/region.c +6
-0@@ -3418,6 +3412,18 @@ static int cxl_region_probe(struct device *dev) out:up_read (&cxl_region_rwsem); +if (rc) +return rc; + + cxlr->memory_notifier.notifier_call = cxl_region_perf_attrs_callback; + cxlr->memory_notifier.priority =CXL_CALLBACK_PRI ; +register_memory_notifier (&cxlr->memory_notifier);
Can in theory fail. Today that is EEXIST only but who knows in future. I think we should handle that and do two devm_add_action_or_reset() perhaps?
Quoted 15 lines
drivers/cxl/core/region.c +6
-1+ + cxlr->adist_notifier.notifier_call = cxl_region_calculate_adistance; + cxlr->adist_notifier.priority =100 ; +register_mt_adistance_algorithm (&cxlr->adist_notifier); + + rc =devm_add_action_or_reset (&cxlr->dev, shutdown_notifiers, cxlr);if (rc)return rc; ---base-commit:
67784a74e258a467225f0e68335df77acd67b7ab
change-id: 20240813-fix-notifiers-99c350b044a2Best regards,
Quoted 3 lines · Jonathan Cameron
On Wed, 04 Sep 2024 09:47:54 -0500
Ira Weiny <[email protected]> wrote:
[snip]
Quoted 10 lines
Quoted 8 lines
Cc Ying Huang <[email protected]>Suggested-by Dan Williams <[email protected]>Reviewed-by Davidlohr Bueso <[email protected]>Reviewed-by Jonathan Cameron <[email protected]>Reviewed-by Ying Huang <[email protected]>Signed-off-by Ira Weiny <[email protected]>A few comments on looking at this again, but all things that apply equally to old code so maybe things for another day.
Yea this was solely a move of existing code to fix the locking issue. I did not evaluate the original code. However...
[snip]
Quoted 10 lines
drivers/cxl/core/region.c static int is_system_ram(struct resource *res, void *arg) · line 3380
Quoted 8 lines
}
drivers/cxl/core/region.c +6
-0+static void shutdown_notifiers (void *_cxlr) +{ +struct cxl_region *cxlr = _cxlr; + +unregister_memory_notifier (&cxlr->memory_notifier); +unregister_mt_adistance_algorithm (&cxlr->adist_notifier);Flip order.
Quoted 3 lines
Makes zero real difference, but if we later end up with more to do here for some reason there may be ordering requirements that will care that this doesn't tear down in reverse of setup.
Generally I agree with you however, the memory and adist notifiers are unrelated. So failing to unwind in reverse order is a matter of taste and is not required even if some other logic was introduced between the registrations I don't see how this backwards order would be an issue.
Quoted 12 lines
Mind you, see below.
Quoted 9 lines
drivers/cxl/core/region.c +2
-0+} +static int cxl_region_probe (struct device *dev) {struct cxl_region *cxlr =to_cxl_region (dev);@@ -3418,6 +3412,18 @@ static int cxl_region_probe(struct device *dev) out:up_read (&cxl_region_rwsem);
Quoted 9 lines
drivers/cxl/core/region.c static int cxl_region_probe(struct device *dev) · line 3420
Quoted 6 lines
drivers/cxl/core/region.c +6
-0+if (rc) +return rc; + + cxlr->memory_notifier.notifier_call = cxl_region_perf_attrs_callback; + cxlr->memory_notifier.priority =CXL_CALLBACK_PRI ; +register_memory_notifier (&cxlr->memory_notifier);Can in theory fail. Today that is EEXIST only but who knows in future. I think we should handle that and do two
devm_add_action_or_reset()perhaps?
First we should not fail the probe if this fails.
Second, nothing bad happens in unregister if the registration failed. Therefore, register failing is benign and I don't see a need for the extra action callback.
Ira
[snip]
Quoted 33 lines · On Fri, 13 Sep 2024 13:41:06 -0500 Ira Weiny <[email protected]> wrote:
Quoted 3 lines · Jonathan Cameron
On Wed, 04 Sep 2024 09:47:54 -0500
Ira Weiny <[email protected]> wrote:[snip]
Quoted 10 lines
Quoted 8 lines
Cc Ying Huang <[email protected]>Suggested-by Dan Williams <[email protected]>Reviewed-by Davidlohr Bueso <[email protected]>Reviewed-by Jonathan Cameron <[email protected]>Reviewed-by Ying Huang <[email protected]>Signed-off-by Ira Weiny <[email protected]>A few comments on looking at this again, but all things that apply equally to old code so maybe things for another day.
Yea this was solely a move of existing code to fix the locking issue. I did not evaluate the original code. However...
[snip]
Quoted 10 lines
drivers/cxl/core/region.c static int is_system_ram(struct resource *res, void *arg) · line 3380
Quoted 8 lines
}
drivers/cxl/core/region.c +6
-0+static void shutdown_notifiers (void *_cxlr) +{ +struct cxl_region *cxlr = _cxlr; + +unregister_memory_notifier (&cxlr->memory_notifier); +unregister_mt_adistance_algorithm (&cxlr->adist_notifier);Flip order.
Quoted 8 lines
Quoted 3 lines
Makes zero real difference, but if we later end up with more to do here for some reason there may be ordering requirements that will care that this doesn't tear down in reverse of setup.
Generally I agree with you however, the memory and adist notifiers are unrelated. So failing to unwind in reverse order is a matter of taste and is not required even if some other logic was introduced between the registrations I don't see how this backwards order would be an issue.
Not an issue as such, but if it requires a tiny bit of thinking that we've engaged in here then it was worth the obviously correct ordering. Not worth churn of a patch though.
Quoted 23 lines
Quoted 13 lines
Mind you, see below.
Quoted 9 lines
drivers/cxl/core/region.c +2
-0+} +static int cxl_region_probe (struct device *dev) {struct cxl_region *cxlr =to_cxl_region (dev);@@ -3418,6 +3412,18 @@ static int cxl_region_probe(struct device *dev) out:up_read (&cxl_region_rwsem);Quoted 9 lines
drivers/cxl/core/region.c static int cxl_region_probe(struct device *dev) · line 3420
Quoted 6 lines
drivers/cxl/core/region.c +6
-0+if (rc) +return rc; + + cxlr->memory_notifier.notifier_call = cxl_region_perf_attrs_callback; + cxlr->memory_notifier.priority =CXL_CALLBACK_PRI ; +register_memory_notifier (&cxlr->memory_notifier);Can in theory fail. Today that is EEXIST only but who knows in future. I think we should handle that and do two
devm_add_action_or_reset()perhaps?
Quoted 5 lines
First we should not fail the probe if this fails.
Second, nothing bad happens in unregister if the registration failed. Therefore, register failing is benign and I don't see a need for the extra action callback.
It's probably a case of print a warning only I guess.
Might be useful to know it's going wrong even though impact is not huge. Personally I'd argue in favor of always failing to probe on a 'software issue' like this where we don't expect to fail. Things get less clear if we fail on an optional hardware related part where carrying on may be the reasonable thing to do.
Jonathan
Quoted 5 lines
Ira
[snip]