Linux GPIO subsystem development
 help / color / mirror / Atom feed
From: Hongyan Xu <getshell@seu.edu.cn>
To: gregkh@linuxfoundation.org, sashal@kernel.org, arnd@arndb.de
Cc: linusw@kernel.org, brgl@kernel.org, stable@vger.kernel.org,
	linux-kernel@vger.kernel.org, linux-gpio@vger.kernel.org,
	jianhao.xu@seu.edu.cn, getshell@seu.edu.cn
Subject: [RFC PATCH 6.6.y 3/4] misc: ti-st: drain published users before transport removal
Date: Fri, 24 Jul 2026 13:28:41 +0800	[thread overview]
Message-ID: <20260724052842.1052-4-getshell@seu.edu.cn> (raw)
In-Reply-To: <20260724052842.1052-1-getshell@seu.edu.cn>

kim_remove() leaves the transport published while it tears down GPIO,
sysfs state and the core.  A protocol registration or write can therefore
retain the core pointer across kfree().

Under the publication mutex, mark the core as removing and clear its
global slot.  Unregister the line discipline and wait for all previously
accounted users before releasing the core and its parent data.  Keep the
GPIO and platform data valid until those users have drained.

Fixes: 53618cc1e51e ("Staging: sources for ST core")
Cc: stable@vger.kernel.org
Signed-off-by: Hongyan Xu <getshell@seu.edu.cn>
---
 drivers/misc/ti-st/st_core.c | 25 ++++++++++++++-----------
 drivers/misc/ti-st/st_kim.c  | 22 ++++++++++++----------
 2 files changed, 26 insertions(+), 21 deletions(-)

diff --git a/drivers/misc/ti-st/st_core.c b/drivers/misc/ti-st/st_core.c
index 43d2587..3aed6c3 100644
--- a/drivers/misc/ti-st/st_core.c
+++ b/drivers/misc/ti-st/st_core.c
@@ -906,20 +906,23 @@ err_unreg_ldisc:
 void st_core_exit(struct st_data_s *st_gdata)
 {
 	long err;
+
+	if (!st_gdata)
+		return;
+
+	tty_unregister_ldisc(&st_ldisc_ops);
+	wait_event(st_gdata->users_wait,
+		   !atomic_read(&st_gdata->active_users));
+
 	/* internal module cleanup */
 	err = st_ll_deinit(st_gdata);
 	if (err)
 		pr_err("error during deinit of ST LL %ld", err);
 
-	if (st_gdata != NULL) {
-		/* Free ST Tx Qs and skbs */
-		skb_queue_purge(&st_gdata->txq);
-		skb_queue_purge(&st_gdata->tx_waitq);
-		kfree_skb(st_gdata->rx_skb);
-		kfree_skb(st_gdata->tx_skb);
-		/* TTY ldisc cleanup */
-		tty_unregister_ldisc(&st_ldisc_ops);
-		/* free the global data pointer */
-		kfree(st_gdata);
-	}
+	/* Free ST Tx Qs and skbs */
+	skb_queue_purge(&st_gdata->txq);
+	skb_queue_purge(&st_gdata->tx_waitq);
+	kfree_skb(st_gdata->rx_skb);
+	kfree_skb(st_gdata->tx_skb);
+	kfree(st_gdata);
 }
diff --git a/drivers/misc/ti-st/st_kim.c b/drivers/misc/ti-st/st_kim.c
index 11eadc1..ab84fe6 100644
--- a/drivers/misc/ti-st/st_kim.c
+++ b/drivers/misc/ti-st/st_kim.c
@@ -788,28 +788,30 @@ err_core_init:
 
 static int kim_remove(struct platform_device *pdev)
 {
-	/* free the GPIOs requested */
 	struct ti_st_plat_data	*pdata = pdev->dev.platform_data;
 	struct kim_data_s	*kim_gdata;
+	int id = 0;
 
 	kim_gdata = platform_get_drvdata(pdev);
-
-	/*
-	 * Free the Bluetooth/FM/GPIO
-	 * nShutdown gpio from the system
-	 */
-	gpio_free(pdata->nshutdown_gpio);
-	pr_info("nshutdown GPIO Freed");
+	if (pdev->id >= 0 && pdev->id < MAX_ST_DEVICES)
+		id = pdev->id;
+	mutex_lock(&st_kim_mutex);
+	kim_gdata->core_data->removing = true;
+	st_kim_devices[id] = NULL;
+	mutex_unlock(&st_kim_mutex);
 
 	debugfs_remove_recursive(kim_debugfs_dir);
 	sysfs_remove_group(&pdev->dev.kobj, &uim_attr_grp);
 	pr_info("sysfs entries removed");
 
-	kim_gdata->kim_pdev = NULL;
 	st_core_exit(kim_gdata->core_data);
+	kim_gdata->core_data = NULL;
+	kim_gdata->kim_pdev = NULL;
+	gpio_free(pdata->nshutdown_gpio);
+	pr_info("nshutdown GPIO Freed");
 
+	platform_set_drvdata(pdev, NULL);
 	kfree(kim_gdata);
-	kim_gdata = NULL;
 	return 0;
 }
 
-- 
2.50.1.windows.1


  parent reply	other threads:[~2026-07-24  5:28 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-24  5:28 [RFC PATCH 6.6.y 0/4] misc: ti-st: close transport removal races Hongyan Xu
2026-07-24  5:28 ` [RFC PATCH 6.6.y 1/4] misc: ti-st: publish the transport only after probe succeeds Hongyan Xu
2026-07-24  5:28 ` [RFC PATCH 6.6.y 2/4] misc: ti-st: account published transport users Hongyan Xu
2026-07-24  5:28 ` Hongyan Xu [this message]
2026-07-24  5:28 ` [RFC PATCH 6.6.y 4/4] misc: ti-st: synchronize TTY teardown with transport removal Hongyan Xu

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=20260724052842.1052-4-getshell@seu.edu.cn \
    --to=getshell@seu.edu.cn \
    --cc=arnd@arndb.de \
    --cc=brgl@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=jianhao.xu@seu.edu.cn \
    --cc=linusw@kernel.org \
    --cc=linux-gpio@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=sashal@kernel.org \
    --cc=stable@vger.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