From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.180 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796318693; b=VOgr8FkrhWVxJ5uFSTwBYwUfGnbLWxk/SkeORgI8Bio2oOvat6sJnd3hCmHBYy7/kHwo BL/UubUHmpWEsXzykaZRi2zGPj0/XLJR69DgqVA5dAli9/G9hDClNaRcoMTmhrtp2bE1U 52INzzSq2HA740Wy/+PEuAFLz46ONl2wZzARF8y2ydhIjTSRAQWxMTtTqBE6tA/kdUL3n fUUx162oQ8Xfcx48tepR22RHMM7eGeDEJaXYC86WNjly7doY8GCEDbN+UsmOY6yKrPDC0 RoLN3iJMB4dGheoiQFen3kA3wwAFSorqJhWglKp4ex6W6b6pjaFiHMPOvdYpn8nVsuA== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796318693; h=DMARC-Filter:Received:DKIM-Signature:X-Google-DKIM-Signature: X-Forwarded-Encrypted:X-Gm-Message-State:X-Gm-Gg:X-Received:Received: Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; bh=ipvEXfQUAFiDBV+IouYhTmqWTaY0DZtmlkBoSGs6O5k=; b=GEjAxXSSktDz9srywDEgvTocUTKHricCiD1I0JAOpkLyyWHxRgTVVwzoue/0tiPsToGk cTQU46+t87v5/G8+sgeNb3z0oqsULKPtS4z9vvYiRKDMFmbd1n0nZ7VAsm+S0oTJMRxkA DULJJaoqiim35+PAOtPdfRN0jg1eRD69kYJrI+jWJkmc0idgqjp54CX0NuFl3VfT4TWru lgtUDeunF1CxxV9bDuIp3W4rSXMlLE6HGU63/ZXqhObDNZG6V/1FkjYg/pKRC/zjoNbNz mNNxLutSyHwZtbgclzbtfkW5izW2LzdoM+BQbVi6Iiv5JZ8mgeWJTgbD3JNLuWpMeJA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=JJipXZA9; arc=none smtp.remote-ip=209.85.214.180 Received: from mail-pl1-f180.google.com (mail-pl1-f180.google.com [209.85.214.180]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 345C4606A32F for ; Thu, 3 Dec 2026 17:24:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 345C4606A32F Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=JJipXZA9 Received: by mail-pl1-f180.google.com with SMTP id d9443c01a7336-2f1a4443407so799135ad.3 for ; Thu, 03 Dec 2026 09:24:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1796318692; x=1796923492; darn=vger.kernel.org; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:from:to:cc:subject :date:message-id:reply-to:content-type; bh=ipvEXfQUAFiDBV+IouYhTmqWTaY0DZtmlkBoSGs6O5k=; b=JJipXZA9LOwGoYrkM6CLUGTNyODm0a9hQQWgRU/VVE2xO/7YBktI4LWi0To0DY8cbU 9QU/D69YtsJVT47R7Tf3Tvc03EJ30WzQvM0zRvFJnk5pWHNUaaDRd84X6A0000gxHP1q 130xgxJJBN6yjJ2tc8/PbMEwD82RNhhKlD5pvvgR64S+nF6Ia1C1uENyOLGA6iF0aRA2 JjvBB0B8RqiFISoVW33J1yhLPYoNzwwZ7510t2bbiRzEvsEEKsD3yX8n5iGyanRAIxVJ pOrHk8yIyF066MetGtfm0DXsaFynpJ5i6b0Kc7MxUDVxajgZdwzcAP5Up/jYAKm6KZ+8 1J6g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1796318692; x=1796923492; h=in-reply-to:content-disposition:content-type:mime-version :references:message-id:subject:cc:to:from:date:x-gm-gg :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to :content-type; bh=ipvEXfQUAFiDBV+IouYhTmqWTaY0DZtmlkBoSGs6O5k=; b=QLEdlcl1lzbidp0gPU7SvwU5T1hNnTrVqzdRSkUBikO3fo8em5+ZNGodz/HLVHqJ0/ 52GroFlfF7TAQJEtzd6ajlDbY3j4IcDQs3GeqSp3KeioUt8c6RxN3tqYHsfc93NMUsKn O+fO+wBpAUC8idT46wx+Gv3eTN1sCq4SEX0G/vMPR9ZysnzJxjgj51hnBIbJJN2DMlLp 1EbjqV4h/b4a6is5rLdP/jbgQDszyHMgN3RHLBnJNuM7Q2b5Ch61ky8i7I5x1iZ4ce7r Ioy17VIfpfd+ReH/og0aZKTd7fQsImWApkpdWRGY6eMcWsCSqwBYCROw8acfW07lOVOf ztmw== X-Forwarded-Encrypted: i=1; AL+hErGd5nmdu72ctB951ypnT1XanqfvxPyVJRrrQXaBpXoPJqaRHmzAiEhcUEZszYY7XUwaYSWsAasxZp3bs6s=@vger.kernel.org X-Gm-Message-State: APuU4Z+uUfE6gn7vQeuZTHRZszJKYSdDe61Umoz5ddD/9lvW0MY7g23F z/ToA/L8CNioIBVzIq3ILxNU3X5P6PDD9FZGBETcUumcWzC9VXaV1gscTpo83ES9lDE= X-Gm-Gg: AZFdTZdldQbYWhQ/WbF0rtnnuXK3q5D9DqHqlv6BG9PQX2uqiyekrRTrZUXZyQN1jwd OZ75gUQeVdwncJz5GNLEazP9efOdG1N8uD+Mt3LwgFTVqaGngcIAQNbqdEBiO3jCtihpQWR6TzM zLk5SNlq+A9yj/GaXRC7cqcwv9jQnfDYc09Ffdk83KkgUYW45rK+RJbLu4raJ2jLAiseNJ2NaPr r06N9P/EKXq4LjNvN3ZrU4rn/JAfy/f/Sza1w7fLWBYpp8U/gveMjpuI/11bst1dOJ2JwY//ZIT mjX1kQCNXyy/UgCj+pKOlqaaegusIWAxNH6B1BDZyfV3Si69qLQSd50ccqvOrMQc8Ay5mwyON6h qqoGDWVW/NJSmUbX1gHipYQJcW15uaFY2V0PVYo+gX4tqRCOIb6Y5tcQmw+BBKRhSnICbQTxCyj T/jEP2do4geVqWLzwhfM6MKM5sOfYWjnE1jPHtIBLqeusC2hKxbCrTYFZB5D30zheCLRUFhi6i7 5PbF2OmBFvJ4XE4umvQMEyP847EsmBl1/inSELBQxg0rLm3CyU/5L4x1jtRXUuvQyYcPTqZrTY9 nI2BpHoYGUVdFybulsYCuT8zLMv0twAgS5eiPC2WgihRQ4Pcv+ZFiQ== X-Received: by 2002:a17:903:32ce:b0:2e9:5971:122 with SMTP id d9443c01a7336-2f03f1767ddmr488475705ad.12.1796318692214; Thu, 03 Dec 2026 09:24:52 -0800 (PST) Received: from p14s ([2604:3d09:148c:c800:e521:9bd1:ba48:689b]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-2f07877d6afsm195538165ad.42.2026.12.03.09.24.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 03 Dec 2026 09:24:50 -0800 (PST) Date: Thu, 3 Dec 2026 11:24:48 -0600 From: Mathieu Poirier To: Chrispine Tinega Cc: Bjorn Andersson , 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 Message-ID: References: <20261123191453.1172967-1-dev@chrispinetinega.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 > --- > 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 >