Skip to contentSkip to conversation

rpmsg: char: Drop messages that arrive while the device is closed

PATCH New Chrispine Tinega 1 participant 1 message Nov 23 Remote processor messaging (rpmsg)

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 rpmsg: char: Drop messages that arrive while the device is closed

A char device bound to an rpmsg channel reuses the channel's default endpoint, and that endpoint stays bound when the last file is closed. rpmsg_ept_cb() keeps running and appends an skb to eptdev->queue for every message, with no limit. The queue is purged only on release, so nothing frees it while the device stays closed.

An endpoint created through RPMSG_CREATE_EPT_IOCTL does not have this
problem: it is destroyed on release, and messages sent to it while the device is closed are dropped by the bus.

On a TI AM62x whose M4F core sends about four 64-byte messages a second, kernel memory that cannot be reclaimed grew by 2.2 MB in the 10 minutes the reading process was stopped, about 850 bytes a message. At that rate a reader that is down for a day holds about 320 MB.

Drop messages for a default endpoint while eptdev->ept is NULL, as the bus already does for an endpoint that does not exist. Non-default endpoints are not checked here because a transport may invoke rpmsg_ept_cb() from rpmsg_create_ept() before rpmsg_eptdev_open() sets eptdev->ept.

Hold eptdev->queue_lock (irqsave) around the check and enqueue in rpmsg_ept_cb() and around updates to eptdev->ept, and purge the queue in rpmsg_eptdev_release() before releasing eptdev->ept_lock. This prevents an in-flight callback from enqueuing an skb after release has purged the queue, and prevents a concurrent open from racing with the purge. Release already discards whatever the last reader left unread, so delivery across a close is not something a reader can depend on today.

Fixes bc69d1066569 ("rpmsg: char: Introduce the "rpmsg-raw" channel")

---
Changes in v2:
- Scope the drop check to default endpoints (eptdev->default_ept), so initial messages arriving during rpmsg_create_ept() for a non-default endpoint are not dropped before eptdev->ept is set.
- Synchronize eptdev->ept updates and the rpmsg_ept_cb() check/enqueue under eptdev->queue_lock (irqsave), and purge eptdev->queue in rpmsg_eptdev_release() while still holding eptdev->ept_lock, so an in-flight callback cannot queue a stale or leaked skb after purge.
- Rebased onto rpmsg-next (965089d20).

v1: https://lore.kernel.org/all/[email protected]/

 drivers/rpmsg/rpmsg_char.c | 31 ++++++++++++++++++++++++++-----
 1 file changed, 26 insertions(+), 5 deletions(-)
drivers/rpmsg/rpmsg_char.c +26 -5
index d8868faac..c03b7a424 100644
@@ -77,6 +77,7 @@ struct rpmsg_eptdev {
 int rpmsg_chrdev_eptdev_destroy(struct device *dev, void *data)
 {
 	struct rpmsg_eptdev *eptdev = dev_to_eptdev(dev);
+	unsigned long flags;
 
 	mutex_lock(&eptdev->ept_lock);
 	if (!eptdev->rpdev) {
@@ -89,7 +90,9 @@ int rpmsg_chrdev_eptdev_destroy(struct device *dev, void *data)
 		/* The default endpoint is released by the rpmsg core */
 		if (!eptdev->default_ept)
 			rpmsg_destroy_ept(eptdev->ept);
+		spin_lock_irqsave(&eptdev->queue_lock, flags);
 		eptdev->ept = NULL;
+		spin_unlock_irqrestore(&eptdev->queue_lock, flags);
 	}
 	mutex_unlock(&eptdev->ept_lock);
 
@@ -108,19 +111,31 @@ static int rpmsg_ept_cb(struct rpmsg_device *rpdev, void *buf, int len,
 {
 	struct rpmsg_eptdev *eptdev = priv;
 	struct sk_buff *skb;
+	unsigned long flags;
 
 	if (!eptdev)
 		return 0;
 
+	spin_lock_irqsave(&eptdev->queue_lock, flags);
+	/*
+	 * A channel's default endpoint stays bound while the device is closed,
+	 * so this callback keeps running. Queuing now would grow the queue
+	 * without limit until the device is opened again. Drop the message.
+	 */
+	if (eptdev->default_ept && !eptdev->ept) {
+		spin_unlock_irqrestore(&eptdev->queue_lock, flags);
+		return 0;
+	}
+
 	skb = alloc_skb(len, GFP_ATOMIC);
-	if (!skb)
+	if (!skb) {
+		spin_unlock_irqrestore(&eptdev->queue_lock, flags);
 		return -ENOMEM;
+	}
 
 	skb_put_data(skb, buf, len);
-
-	spin_lock(&eptdev->queue_lock);
 	skb_queue_tail(&eptdev->queue, skb);
-	spin_unlock(&eptdev->queue_lock);
+	spin_unlock_irqrestore(&eptdev->queue_lock, flags);
 
 	/* wake up any blocking processes, waiting for new data */
 	wake_up_interruptible(&eptdev->readq);
@@ -149,6 +164,7 @@ static int rpmsg_eptdev_open(struct inode *inode, struct file *filp)
 	struct rpmsg_endpoint *ept;
 	struct rpmsg_device *rpdev = eptdev->rpdev;
 	struct device *dev = &eptdev->dev;
+	unsigned long flags;
 
 	mutex_lock(&eptdev->ept_lock);
 	if (eptdev->ept) {
@@ -180,7 +196,9 @@ static int rpmsg_eptdev_open(struct inode *inode, struct file *filp)
 	}
 
 	ept->flow_cb = rpmsg_ept_flow_cb;
+	spin_lock_irqsave(&eptdev->queue_lock, flags);
 	eptdev->ept = ept;
+	spin_unlock_irqrestore(&eptdev->queue_lock, flags);
 	filp->private_data = eptdev;
 	mutex_unlock(&eptdev->ept_lock);
 
@@ -191,19 +209,22 @@ static int rpmsg_eptdev_release(struct inode *inode, struct file *filp)
 {
 	struct rpmsg_eptdev *eptdev = cdev_to_eptdev(inode->i_cdev);
 	struct device *dev = &eptdev->dev;
+	unsigned long flags;
 
 	/* Close the endpoint, if it's not already destroyed by the parent */
 	mutex_lock(&eptdev->ept_lock);
 	if (eptdev->ept) {
 		if (!eptdev->default_ept)
 			rpmsg_destroy_ept(eptdev->ept);
+		spin_lock_irqsave(&eptdev->queue_lock, flags);
 		eptdev->ept = NULL;
+		spin_unlock_irqrestore(&eptdev->queue_lock, flags);
 	}
-	mutex_unlock(&eptdev->ept_lock);
 	eptdev->remote_flow_updated = false;
 
 	/* Discard all SKBs */
 	skb_queue_purge(&eptdev->queue);
+	mutex_unlock(&eptdev->ept_lock);
 
 	put_device(dev);
 
Signature · 1 line
2.53.0