Devicetree
 help / color / mirror / Atom feed
From: Debjit Ghosh <dghosh@juniper.net>
To: <broonie@kernel.org>, <robh@kernel.org>, <saravanak@kernel.org>
Cc: <linux-spi@vger.kernel.org>, <devicetree@vger.kernel.org>,
	Debjit Ghosh <debjit.ghosh@hpe.com>
Subject: [PATCH v2] spi: serialize OF notifier device removal with controller removal
Date: Sun, 29 Nov 2026 12:50:08 -0800	[thread overview]
Message-ID: <20261129205008.25212-1-dghosh@juniper.net> (raw)

From: Debjit Ghosh <debjit.ghosh@hpe.com>

The OF reconfig notifier and spi_unregister_controller() can both
unregister the same SPI device concurrently. However,
spi_unregister_controller() unregisters children holding ctlr->add_lock,
but of_spi_notify() does not. So both contexts, sometimes executing
concurrently, would call device_del() on the same device potentially
corrupting the controller's child klist.

  Oops: general protection fault, probably for non-canonical
	address 0xdead0000000000f8
  CPU: 13 UID: 0 PID: 173 Comm: irq/38-pciehp
  RIP: 0010:klist_next+0x6e/0x140
   device_for_each_child+0x5d/0xb0
   spi_unregister_controller+0x3a/0x140
   ...
   mfd_remove_devices+0x36/0x50
   pci_device_remove+0x3d/0xb0
   ...
   pciehp_ist+0x13c/0x180

while an overlay unload mechanism was concurrently removing the same
device through of_overlay_remove() -> of_spi_notify().

Serialize the notifier's removal path with the controller's child
unregistration using ctlr->add_lock, and recheck OF_POPULATED under the
lock before calling spi_unregister_device().

Fixes: ce79d54ae447 ("spi/of: Add OF notifier handler")
Signed-off-by: Debjit Ghosh <debjit.ghosh@hpe.com>
---
Changes since v1:
- resolve the controller from device node's parent (as done in ADD case)
  instead of resolving through spi device and take add_lock prior to
  checking OF_POPULATED or looking up the device. So now the notifier
  waits out a concurrent controller teardown before returing NOTIFY_OK
  to OF subsystem.
  https://lore.kernel.org/all/20261128024830.57367-1-dghosh@juniper.net/
- add fixes trailer.

 drivers/spi/spi.c | 21 +++++++++++++++++++--
 1 file changed, 19 insertions(+), 2 deletions(-)

diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index a6a9aaa47fde..b54bd3a86426 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -5311,20 +5311,37 @@ static int of_spi_notify(struct notifier_block *nb, unsigned long action,
 		break;
 
 	case OF_RECONFIG_CHANGE_REMOVE:
+		ctlr = of_find_spi_controller_by_node(rd->dn->parent);
+		if (ctlr == NULL)
+			return NOTIFY_OK;	/* Not for us */
+
+		/*
+		 * Serialize against spi_unregister_controller. The device
+		 * can be unregistered concurrently by its controller being
+		 * torn down (e.g. on surprise hot removal of the underlying
+		 * bus hosting the controller).
+		 */
+		if (IS_ENABLED(CONFIG_SPI_DYNAMIC))
+			mutex_lock(&ctlr->add_lock);
+
 		/* Already depopulated? */
 		if (!of_node_check_flag(rd->dn, OF_POPULATED))
-			return NOTIFY_OK;
+			goto notify_ok;
 
 		/* Find our device by node */
 		spi = of_find_spi_device_by_node(rd->dn);
 		if (spi == NULL)
-			return NOTIFY_OK;	/* No? not meant for us */
+			goto notify_ok;		/* No? not meant for us */
 
 		/* Unregister takes one ref away */
 		spi_unregister_device(spi);
 
 		/* And put the reference of the find */
 		put_device(&spi->dev);
+notify_ok:
+		if (IS_ENABLED(CONFIG_SPI_DYNAMIC))
+			mutex_unlock(&ctlr->add_lock);
+		put_device(&ctlr->dev);
 		break;
 	}
 
-- 
2.25.1


                 reply	other threads:[~2026-11-29 20:50 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261129205008.25212-1-dghosh@juniper.net \
    --to=dghosh@juniper.net \
    --cc=broonie@kernel.org \
    --cc=debjit.ghosh@hpe.com \
    --cc=devicetree@vger.kernel.org \
    --cc=linux-spi@vger.kernel.org \
    --cc=robh@kernel.org \
    --cc=saravanak@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox