[PATCH v2 1/2] extcon: synchronize all-connector notifier removal

From: Hongyan Xu

Date: Sun Nov 15 2026 - 06:48:10 EST


extcon_sync() invokes the all-connector raw notifier chain after
dropping edev->lock. Unregister unlinks a block under that lock but
does not wait for an in-flight callback, so managed consumers can
release their state while a callback still publishes work.

Use an atomic notifier chain for the all-connector notifications.
Its RCU read section and synchronized unregister provide the
required callback quiescence. Use the notifier implementation's
own writer lock and perform its grace-period wait outside edev's
spinlock. Document callback and unregister context requirements.

The current all-connector consumers defer their operations to work
items and their callbacks do not sleep. Per-ID raw chains retain
their existing behavior: some OMAP callbacks perform regulator
operations and need a separate context-compatible repair.

Found by the author's in-house static analysis tool.

Fixes: 815429b39d94 ("extcon: Add new extcon_register_notifier_all() to monitor all external connectors")
Cc: stable@xxxxxxxxxxxxxxx
Link: https://lore.kernel.org/all/2ef4ad43-497f-c40f-b18a-f5b75799d95d@xxxxxxxxxxxxxxx/
Signed-off-by: Hongyan Xu <getshell@xxxxxxxxxx>
Signed-off-by: Zehao Gao <gaozehao@xxxxxxxxx>
---

Discovery: found by the author's in-house static analysis tool.

Documentation/driver-api/extcon.rst | 2 +-
drivers/extcon/extcon.c | 28 +++++++++-------------------
drivers/extcon/extcon.h | 2 +-
include/linux/extcon.h | 4 ++++
4 files changed, 15 insertions(+), 21 deletions(-)

diff --git a/Documentation/driver-api/extcon.rst b/Documentation/driver-api/extcon.rst
index d3217b9..384d743 100644
--- a/Documentation/driver-api/extcon.rst
+++ b/Documentation/driver-api/extcon.rst
@@ -70,7 +70,7 @@ The core structure representing an Extcon device::
/* Internal data */
struct device dev;
unsigned int id;
- struct raw_notifier_head nh_all;
+ struct atomic_notifier_head nh_all;
struct raw_notifier_head *nh;
struct list_head entry;
int max_supported;
diff --git a/drivers/extcon/extcon.c b/drivers/extcon/extcon.c
index 98d85cc..b8d8c2a 100644
--- a/drivers/extcon/extcon.c
+++ b/drivers/extcon/extcon.c
@@ -456,10 +456,10 @@ int extcon_sync(struct extcon_dev *edev, unsigned int id)
raw_notifier_call_chain(&edev->nh[index], state, edev);

/*
- * Call functions in a raw notifier chain for the all supported
+ * Call functions in the atomic notifier chain for all supported
* external connectors.
*/
- raw_notifier_call_chain(&edev->nh_all, state, edev);
+ atomic_notifier_call_chain(&edev->nh_all, state, edev);

spin_lock_irqsave(&edev->lock, flags);
/* This could be in interrupt handler */
@@ -967,6 +967,7 @@ EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
*
* Note that this function registers a notifier block in order to receive
* the state change of all supported external connectors from extcon device.
+ * Callbacks run under the RCU read-side critical section and must not sleep.
* And the second parameter given to the callback of nb (val) is
* the current state and the third pameter is the pointer of extcon device.
*
@@ -975,17 +976,10 @@ EXPORT_SYMBOL_GPL(extcon_unregister_notifier);
int extcon_register_notifier_all(struct extcon_dev *edev,
struct notifier_block *nb)
{
- unsigned long flags;
- int ret;
-
if (!edev || !nb)
return -EINVAL;

- spin_lock_irqsave(&edev->lock, flags);
- ret = raw_notifier_chain_register(&edev->nh_all, nb);
- spin_unlock_irqrestore(&edev->lock, flags);
-
- return ret;
+ return atomic_notifier_chain_register(&edev->nh_all, nb);
}
EXPORT_SYMBOL_GPL(extcon_register_notifier_all);

@@ -995,21 +989,17 @@ EXPORT_SYMBOL_GPL(extcon_register_notifier_all);
* @nb: a notifier block to be registered
*
* Returns 0 if success or error number if fail.
+ *
+ * Waits for in-flight callbacks. Must be called from process context and
+ * outside a notifier callback.
*/
int extcon_unregister_notifier_all(struct extcon_dev *edev,
struct notifier_block *nb)
{
- unsigned long flags;
- int ret;
-
if (!edev || !nb)
return -EINVAL;

- spin_lock_irqsave(&edev->lock, flags);
- ret = raw_notifier_chain_unregister(&edev->nh_all, nb);
- spin_unlock_irqrestore(&edev->lock, flags);
-
- return ret;
+ return atomic_notifier_chain_unregister(&edev->nh_all, nb);
}
EXPORT_SYMBOL_GPL(extcon_unregister_notifier_all);

@@ -1300,7 +1290,7 @@ int extcon_dev_register(struct extcon_dev *edev)
for (index = 0; index < edev->max_supported; index++)
RAW_INIT_NOTIFIER_HEAD(&edev->nh[index]);

- RAW_INIT_NOTIFIER_HEAD(&edev->nh_all);
+ ATOMIC_INIT_NOTIFIER_HEAD(&edev->nh_all);

dev_set_drvdata(&edev->dev, edev);
dev_set_name(&edev->dev, "extcon%d", edev->id);
diff --git a/drivers/extcon/extcon.h b/drivers/extcon/extcon.h
index 9461826..5a3f0c4 100644
--- a/drivers/extcon/extcon.h
+++ b/drivers/extcon/extcon.h
@@ -48,7 +48,7 @@ struct extcon_dev {
/* Internal data. Please do not set. */
struct device dev;
unsigned int id;
- struct raw_notifier_head nh_all;
+ struct atomic_notifier_head nh_all;
struct raw_notifier_head *nh;
struct list_head entry;
int max_supported;
diff --git a/include/linux/extcon.h b/include/linux/extcon.h
index e596a0a..869bac9 100644
--- a/include/linux/extcon.h
+++ b/include/linux/extcon.h
@@ -209,6 +209,10 @@ void devm_extcon_unregister_notifier(struct device *dev,
struct extcon_dev *edev, unsigned int id,
struct notifier_block *nb);

+/*
+ * All-connector callbacks must not sleep. Unregister waits for callbacks and
+ * must be called from process context, outside a notifier callback.
+ */
int extcon_register_notifier_all(struct extcon_dev *edev,
struct notifier_block *nb);
int extcon_unregister_notifier_all(struct extcon_dev *edev,
--
2.50.1.windows.1