From: Yaohan Li <shunsheng1218@gmail.com>
To: andersson@kernel.org, mathieu.poirier@linaro.org
Cc: linux-remoteproc@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [RFC PATCH] rpmsg: stop the inbound callbacks before the driver is removed
Date: Thu, 19 Nov 2026 10:42:48 +0800 [thread overview]
Message-ID: <20261119024248.1497433-1-shunsheng1218@gmail.com> (raw)
rpmsg_dev_remove() calls rpdrv->remove() and destroys the default
endpoint only afterwards. Until the endpoint is destroyed it keeps
delivering inbound messages to rpmsg_ept_cb() and flow control
notifications to rpmsg_ept_flow_cb(), both with ept->priv pointing to the
per-endpoint data that the driver releases during its remove. For the
default endpoint of rpmsg_chrdev that data is the rpmsg_eptdev that is
freed as soon as the channel goes away.
Add an optional quiesce_ept operation that stops the inbound callbacks
without releasing the endpoint, and call it from rpmsg_dev_remove()
before rpdrv->remove(), and from the probe failure path which calls the
driver remove callback as well. The endpoint itself is still destroyed
after the driver remove() returns, so drivers that want to send a last
message keep working. No in-tree RPMsg driver sends on the endpoint
from its remove callback.
The operation runs in process context; it is not an atomic one. Virtio
takes the mutex it already holds around the callbacks, mtk_rpmsg waits
for an RCU grace period, and qcom_smd and qcom_glink take their receive
spinlock. Callback execution context is transport specific: a callback
invoked under a spinlock or an RCU read side section must not sleep, and
the operation preserves the context each transport already used. Glink
also takes recv_lock around the flow control callback, whose only
in-tree implementation, rpmsg_ept_flow_cb(), sets two flags and wakes
the read queue; glink is also the only transport that delivers that
callback today.
qcom_smd skips the callback once it is gone and mtk_rpmsg switches the
callback pointer to RCU because its IPI handler runs without a lock. The
warning that mtk_rpmsg_ipi_handler() prints for a failing callback has to
be inside the RCU read side section: quiesce_ept() waits for the readers
with synchronize_rcu(), and afterwards the endpoint and the device it
names may be gone.
quiesce_ept is optional, so the guarantee is per transport: only a
transport that implements it can promise that no inbound callback is
left when the driver remove callback returns. All four in-tree
transports do.
Signed-off-by: Yaohan Li <shunsheng1218@gmail.com>
---
drivers/rpmsg/mtk_rpmsg.c | 28 +++++++++++++++++++++++-----
drivers/rpmsg/qcom_glink_native.c | 17 ++++++++++++++---
drivers/rpmsg/qcom_smd.c | 20 +++++++++++++++++---
drivers/rpmsg/rpmsg_core.c | 11 +++++++++++
drivers/rpmsg/rpmsg_internal.h | 18 ++++++++++++++++++
drivers/rpmsg/virtio_rpmsg_bus.c | 16 ++++++++++++----
6 files changed, 95 insertions(+), 15 deletions(-)
diff --git a/drivers/rpmsg/mtk_rpmsg.c b/drivers/rpmsg/mtk_rpmsg.c
index 430a9b2aa7bb..37d573287ced 100644
--- a/drivers/rpmsg/mtk_rpmsg.c
+++ b/drivers/rpmsg/mtk_rpmsg.c
@@ -73,12 +73,23 @@ static void mtk_rpmsg_ipi_handler(void *data, unsigned int len, void *priv)
{
struct mtk_rpmsg_endpoint *mept = priv;
struct rpmsg_endpoint *ept = &mept->ept;
+ rpmsg_rx_cb_t cb;
int ret;
- ret = (*ept->cb)(ept->rpdev, data, len, ept->priv, ept->addr);
- if (ret)
- dev_warn(&ept->rpdev->dev, "rpmsg handler return error = %d",
- ret);
+ /*
+ * The warning has to be inside the read side section as well: @ept and
+ * the device it points to are released once quiesce_ept() has waited
+ * for the readers, and that wait would be over right after the unlock.
+ */
+ rcu_read_lock();
+ cb = rcu_dereference(ept->cb);
+ if (cb) {
+ ret = cb(ept->rpdev, data, len, ept->priv, ept->addr);
+ if (ret)
+ dev_warn(&ept->rpdev->dev,
+ "rpmsg handler return error = %d", ret);
+ }
+ rcu_read_unlock();
}
static struct rpmsg_endpoint *
@@ -100,7 +111,7 @@ __mtk_create_ept(struct mtk_rpmsg_rproc_subdev *mtk_subdev,
kref_init(&ept->refcount);
ept->rpdev = rpdev;
- ept->cb = cb;
+ RCU_INIT_POINTER(ept->cb, cb);
ept->priv = priv;
ept->ops = &mtk_rpmsg_endpoint_ops;
ept->addr = id;
@@ -126,6 +137,12 @@ mtk_rpmsg_create_ept(struct rpmsg_device *rpdev, rpmsg_rx_cb_t cb, void *priv,
return __mtk_create_ept(mtk_subdev, rpdev, cb, priv, chinfo.src);
}
+static void mtk_rpmsg_quiesce_ept(struct rpmsg_endpoint *ept)
+{
+ rcu_assign_pointer(ept->cb, NULL);
+ synchronize_rcu();
+}
+
static void mtk_rpmsg_destroy_ept(struct rpmsg_endpoint *ept)
{
struct mtk_rpmsg_rproc_subdev *mtk_subdev =
@@ -158,6 +175,7 @@ static int mtk_rpmsg_trysend(struct rpmsg_endpoint *ept, const void *data, int l
}
static const struct rpmsg_endpoint_ops mtk_rpmsg_endpoint_ops = {
+ .quiesce_ept = mtk_rpmsg_quiesce_ept,
.destroy_ept = mtk_rpmsg_destroy_ept,
.send = mtk_rpmsg_send,
.trysend = mtk_rpmsg_trysend,
diff --git a/drivers/rpmsg/qcom_glink_native.c b/drivers/rpmsg/qcom_glink_native.c
index f848ab46161d..5b88a70ed08c 100644
--- a/drivers/rpmsg/qcom_glink_native.c
+++ b/drivers/rpmsg/qcom_glink_native.c
@@ -1165,8 +1165,11 @@ static void qcom_glink_handle_signals(struct qcom_glink *glink,
enable = sigs & NATIVE_DSR_SIG || sigs & NATIVE_CTS_SIG;
+ spin_lock_irqsave(&channel->recv_lock, flags);
if (channel->ept.flow_cb)
- channel->ept.flow_cb(channel->ept.rpdev, channel->ept.priv, enable);
+ channel->ept.flow_cb(channel->ept.rpdev, channel->ept.priv,
+ enable);
+ spin_unlock_irqrestore(&channel->recv_lock, flags);
}
void qcom_glink_native_rx(struct qcom_glink *glink)
@@ -1408,16 +1411,23 @@ static void qcom_glink_remove_rpmsg_device(struct qcom_glink *glink, struct glin
channel->rpdev = NULL;
}
-static void qcom_glink_destroy_ept(struct rpmsg_endpoint *ept)
+static void qcom_glink_quiesce_ept(struct rpmsg_endpoint *ept)
{
struct glink_channel *channel = to_glink_channel(ept);
- struct qcom_glink *glink = channel->glink;
unsigned long flags;
spin_lock_irqsave(&channel->recv_lock, flags);
channel->ept.cb = NULL;
+ channel->ept.flow_cb = NULL;
spin_unlock_irqrestore(&channel->recv_lock, flags);
+}
+
+static void qcom_glink_destroy_ept(struct rpmsg_endpoint *ept)
+{
+ struct glink_channel *channel = to_glink_channel(ept);
+ struct qcom_glink *glink = channel->glink;
+ qcom_glink_quiesce_ept(ept);
qcom_glink_send_close_req(glink, channel);
}
@@ -1611,6 +1621,7 @@ static const struct rpmsg_device_ops glink_device_ops = {
};
static const struct rpmsg_endpoint_ops glink_endpoint_ops = {
+ .quiesce_ept = qcom_glink_quiesce_ept,
.destroy_ept = qcom_glink_destroy_ept,
.send = qcom_glink_send,
.sendto = qcom_glink_sendto,
diff --git a/drivers/rpmsg/qcom_smd.c b/drivers/rpmsg/qcom_smd.c
index 7dbe1c6efe41..a27964c757ca 100644
--- a/drivers/rpmsg/qcom_smd.c
+++ b/drivers/rpmsg/qcom_smd.c
@@ -558,9 +558,11 @@ static int qcom_smd_channel_recv_single(struct qcom_smd_channel *channel)
len = channel->pkt_size;
}
- ret = ept->cb(ept->rpdev, ptr, len, ept->priv, RPMSG_ADDR_ANY);
- if (ret < 0)
- return ret;
+ if (ept->cb) {
+ ret = ept->cb(ept->rpdev, ptr, len, ept->priv, RPMSG_ADDR_ANY);
+ if (ret < 0)
+ return ret;
+ }
/* Only forward the tail if the client consumed the data */
qcom_smd_channel_advance(channel, len);
@@ -950,6 +952,17 @@ static struct rpmsg_endpoint *qcom_smd_create_ept(struct rpmsg_device *rpdev,
return NULL;
}
+static void qcom_smd_quiesce_ept(struct rpmsg_endpoint *ept)
+{
+ struct qcom_smd_endpoint *qsept = to_smd_endpoint(ept);
+ struct qcom_smd_channel *channel = qsept->qsch;
+ unsigned long flags;
+
+ spin_lock_irqsave(&channel->recv_lock, flags);
+ ept->cb = NULL;
+ spin_unlock_irqrestore(&channel->recv_lock, flags);
+}
+
static void qcom_smd_destroy_ept(struct rpmsg_endpoint *ept)
{
struct qcom_smd_endpoint *qsept = to_smd_endpoint(ept);
@@ -1052,6 +1065,7 @@ static const struct rpmsg_device_ops qcom_smd_device_ops = {
};
static const struct rpmsg_endpoint_ops qcom_smd_endpoint_ops = {
+ .quiesce_ept = qcom_smd_quiesce_ept,
.destroy_ept = qcom_smd_destroy_ept,
.send = qcom_smd_send,
.sendto = qcom_smd_sendto,
diff --git a/drivers/rpmsg/rpmsg_core.c b/drivers/rpmsg/rpmsg_core.c
index 04bfcc75f05b..b832345ca262 100644
--- a/drivers/rpmsg/rpmsg_core.c
+++ b/drivers/rpmsg/rpmsg_core.c
@@ -481,6 +481,9 @@ static int rpmsg_dev_probe(struct device *dev)
return 0;
remove_rpdev:
+ if (ept && ept->ops->quiesce_ept)
+ ept->ops->quiesce_ept(ept);
+
if (rpdrv->remove)
rpdrv->remove(rpdev);
destroy_ept:
@@ -498,6 +501,14 @@ static void rpmsg_dev_remove(struct device *dev)
if (rpdev->ops->announce_destroy)
rpdev->ops->announce_destroy(rpdev);
+ /*
+ * Stop the inbound callbacks before the driver releases the data
+ * reachable through @ept->priv. The endpoint itself is destroyed
+ * below, once the driver does not need it anymore.
+ */
+ if (rpdev->ept && rpdev->ept->ops->quiesce_ept)
+ rpdev->ept->ops->quiesce_ept(rpdev->ept);
+
if (rpdrv->remove)
rpdrv->remove(rpdev);
diff --git a/drivers/rpmsg/rpmsg_internal.h b/drivers/rpmsg/rpmsg_internal.h
index a8b7065fd165..02dda4b6809b 100644
--- a/drivers/rpmsg/rpmsg_internal.h
+++ b/drivers/rpmsg/rpmsg_internal.h
@@ -61,6 +61,24 @@ struct rpmsg_device_ops {
* @trysend, while the variants sending data off-channel are optional.
*/
struct rpmsg_endpoint_ops {
+ /*
+ * Stop the inbound callbacks of the endpoint without releasing it.
+ * Optional: a transport that does not implement it keeps delivering
+ * messages until the endpoint is destroyed, so only the transports
+ * that do can promise that no callback is left when the driver
+ * remove callback returns.
+ *
+ * Called from a sleepable context. An implementation must wait for
+ * the endpoint callbacks that are in flight before it returns, with
+ * whatever lock already serializes them with its receive path.
+ *
+ * The context the callbacks themselves run in is transport
+ * specific: a callback invoked under a spinlock or an RCU read side
+ * section must not sleep. A transport keeps the context it already
+ * used for its callbacks.
+ */
+ void (*quiesce_ept)(struct rpmsg_endpoint *ept);
+
void (*destroy_ept)(struct rpmsg_endpoint *ept);
int (*send)(struct rpmsg_endpoint *ept, const void *data, int len);
diff --git a/drivers/rpmsg/virtio_rpmsg_bus.c b/drivers/rpmsg/virtio_rpmsg_bus.c
index 23ff6c8b7a20..c8a15bb63841 100644
--- a/drivers/rpmsg/virtio_rpmsg_bus.c
+++ b/drivers/rpmsg/virtio_rpmsg_bus.c
@@ -137,6 +137,7 @@ struct virtio_rpmsg_channel {
*/
#define RPMSG_RESERVED_ADDRESSES (1024)
+static void virtio_rpmsg_quiesce_ept(struct rpmsg_endpoint *ept);
static void virtio_rpmsg_destroy_ept(struct rpmsg_endpoint *ept);
static int virtio_rpmsg_send(struct rpmsg_endpoint *ept, const void *data, int len);
static int virtio_rpmsg_sendto(struct rpmsg_endpoint *ept, const void *data,
@@ -152,6 +153,7 @@ static struct rpmsg_device *__rpmsg_create_channel(struct virtproc_info *vrp,
struct rpmsg_channel_info *chinfo);
static const struct rpmsg_endpoint_ops virtio_endpoint_ops = {
+ .quiesce_ept = virtio_rpmsg_quiesce_ept,
.destroy_ept = virtio_rpmsg_destroy_ept,
.send = virtio_rpmsg_send,
.sendto = virtio_rpmsg_sendto,
@@ -292,6 +294,15 @@ static struct rpmsg_endpoint *virtio_rpmsg_create_ept(struct rpmsg_device *rpdev
* name service endpoint, which isn't bound to an rpmsg channel.
* See also __rpmsg_create_ept().
*/
+static void virtio_rpmsg_quiesce_ept(struct rpmsg_endpoint *ept)
+{
+ /* make sure in-flight inbound messages won't invoke cb anymore */
+ mutex_lock(&ept->cb_lock);
+ ept->cb = NULL;
+ ept->flow_cb = NULL;
+ mutex_unlock(&ept->cb_lock);
+}
+
static void
__rpmsg_destroy_ept(struct virtproc_info *vrp, struct rpmsg_endpoint *ept)
{
@@ -300,10 +311,7 @@ __rpmsg_destroy_ept(struct virtproc_info *vrp, struct rpmsg_endpoint *ept)
idr_remove(&vrp->endpoints, ept->addr);
mutex_unlock(&vrp->endpoints_lock);
- /* make sure in-flight inbound messages won't invoke cb anymore */
- mutex_lock(&ept->cb_lock);
- ept->cb = NULL;
- mutex_unlock(&ept->cb_lock);
+ virtio_rpmsg_quiesce_ept(ept);
kref_put(&ept->refcount, __ept_release);
}
--
2.34.1
reply other threads:[~2026-11-19 2:42 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=20261119024248.1497433-1-shunsheng1218@gmail.com \
--to=shunsheng1218@gmail.com \
--cc=andersson@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-remoteproc@vger.kernel.org \
--cc=mathieu.poirier@linaro.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
all inboxes | Powered by JetHome®