This is an automated email from the ASF dual-hosted git repository. xiaoxiang781216 pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/nuttx.git
commit 6377a5a9bf27445ccb7ef156da1c6e754bb3cc5d Author: Bowen Wang <[email protected]> AuthorDate: Tue Nov 24 17:04:35 2026 +0800 drivers/rpmsg/rpmsg_port: hold the endpoint across the ns bound callbacks rpmsg_port_ns_callback() drops rdev->lock before it calls the user callbacks, so a concurrent close() can drop the last reference and free the endpoint while a callback still runs on it. NS_DESTROY takes a reference around ns_unbind_cb(), but NS_CREATE and NS_CREATE_ACK call ns_bound_cb() without one. Take the reference on all three paths, as rpmsg_virtio does, and test release_cb once before the branches. The reference is dropped under rdev->lock, as rpmsg_ept_decref() requires. Signed-off-by: Bowen Wang <[email protected]> --- drivers/rpmsg/rpmsg_port.c | 45 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/drivers/rpmsg/rpmsg_port.c b/drivers/rpmsg/rpmsg_port.c index 8e2cf264c06..550edabd584 100644 --- a/drivers/rpmsg/rpmsg_port.c +++ b/drivers/rpmsg/rpmsg_port.c @@ -496,7 +496,7 @@ static int rpmsg_port_ns_callback(FAR struct rpmsg_endpoint *ept, struct rpmsg_ns_msg msg; FAR const char *name; uint32_t dest; - bool decref = false; + bool ept_to_release; if (len != sizeof(msg)) { @@ -516,16 +516,23 @@ static int rpmsg_port_ns_callback(FAR struct rpmsg_endpoint *ept, metal_mutex_acquire(&rdev->lock); ept = rpmsg_get_endpoint(rdev, name, RPMSG_ADDR_ANY, dest); + /* If the ept release callback is not implemented, the callbacks below + * can free the endpoint. Test ept->release_cb before relying on the + * endpoint staying alive across them. + */ + + ept_to_release = ept != NULL && ept->release_cb != NULL; + if (msg.flags == RPMSG_NS_DESTROY) { if (ept != NULL) { ept->dest_addr = RPMSG_ADDR_ANY; - if (ept->release_cb != NULL) - { - rpmsg_ept_incref(ept); - decref = true; - } + } + + if (ept_to_release) + { + rpmsg_ept_incref(ept); } metal_mutex_release(&rdev->lock); @@ -539,7 +546,7 @@ static int rpmsg_port_ns_callback(FAR struct rpmsg_endpoint *ept, rdev->ns_unbind_cb(rdev, name, dest); } - if (decref) + if (ept_to_release) { metal_mutex_acquire(&rdev->lock); rpmsg_ept_decref(ept); @@ -559,6 +566,11 @@ static int rpmsg_port_ns_callback(FAR struct rpmsg_endpoint *ept, else if (ept->dest_addr == RPMSG_ADDR_ANY) { ept->dest_addr = dest; + if (ept_to_release) + { + rpmsg_ept_incref(ept); + } + metal_mutex_release(&rdev->lock); if (ept->name[0] && rdev->support_ack) { @@ -571,6 +583,13 @@ static int rpmsg_port_ns_callback(FAR struct rpmsg_endpoint *ept, { ept->ns_bound_cb(ept); } + + if (ept_to_release) + { + metal_mutex_acquire(&rdev->lock); + rpmsg_ept_decref(ept); + metal_mutex_release(&rdev->lock); + } } else { @@ -584,12 +603,24 @@ static int rpmsg_port_ns_callback(FAR struct rpmsg_endpoint *ept, if (ept != NULL && ept->dest_addr == RPMSG_ADDR_ANY) { ept->dest_addr = dest; + if (ept_to_release) + { + rpmsg_ept_incref(ept); + } + metal_mutex_release(&rdev->lock); if (ept->ns_bound_cb != NULL) { ept->ns_bound_cb(ept); } + + if (ept_to_release) + { + metal_mutex_acquire(&rdev->lock); + rpmsg_ept_decref(ept); + metal_mutex_release(&rdev->lock); + } } else {
