Linux Remote Processor Subsystem development
 help / color / mirror / Atom feed
From: Mathieu Poirier <mathieu.poirier@linaro.org>
To: Chrispine Tinega <dev@chrispinetinega.com>
Cc: Bjorn Andersson <andersson@kernel.org>,
	linux-remoteproc@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] rpmsg: char: Drop messages that arrive while the device is closed
Date: Thu, 3 Dec 2026 11:24:48 -0600	[thread overview]
Message-ID: <axGl4BY14sMMPzqY@p14s> (raw)
In-Reply-To: <20261123191453.1172967-1-dev@chrispinetinega.com>

Sorry for the late reply - I had to refresh my mind on the inner-working of the
RPMSG char driver.

On Mon, Nov 23, 2026 at 10:14:53PM +0300, Chrispine Tinega wrote:
> 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.
> 

This is indeed a valid usecase.

> 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")
> Signed-off-by: Chrispine Tinega <dev@chrispinetinega.com>
> ---
> 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/20261115110040.363306-1-dev@chrispinetinega.com/
> 
>  drivers/rpmsg/rpmsg_char.c | 31 ++++++++++++++++++++++++++-----
>  1 file changed, 26 insertions(+), 5 deletions(-)
> 
> diff --git a/drivers/rpmsg/rpmsg_char.c b/drivers/rpmsg/rpmsg_char.c
> index d8868faac..c03b7a424 100644
> --- a/drivers/rpmsg/rpmsg_char.c
> +++ b/drivers/rpmsg/rpmsg_char.c
> @@ -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) {

Is there a point in checking @eptdev->default at all?  From what I understand,
checking @eptdev->ept should be sufficient.

Otherwise I'm good with your patch.

Thanks,
Mathieu


> +		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);
>  
> -- 
> 2.53.0
> 

      reply	other threads:[~2026-12-03 17:24 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-23 19:14 [PATCH v2] rpmsg: char: Drop messages that arrive while the device is closed Chrispine Tinega
2026-12-03 17:24 ` Mathieu Poirier [this message]

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=axGl4BY14sMMPzqY@p14s \
    --to=mathieu.poirier@linaro.org \
    --cc=andersson@kernel.org \
    --cc=dev@chrispinetinega.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-remoteproc@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