From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=209.85.214.179 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796318693; b=gZ99OleAqDinPBk9gsF7ZgmFra0wRl68C90JvhiZxefUk7ZeVUrarss47iNVHi/eLvpv 2pBxhLkmoEeE1zbgEe4+jHoC+mBshZHvAnHj8jNxkIK9ZFszGOFXFm1p86n86RrFIDxlU tHuWjg/xfW1lgVx8CfOFHWpJLo7buN53RNLaPoUBrJaFhYk1GrZh2J/rIMQrejJ4N+wG9 oo+9AZDcd/XpuYYRi14Plw5t7+EUcWmH8BRbw6oy7zziN6aHnT1ocDUDfS6rZgupyig6w i6+HTX44EU1HZz572qk1/mid/jjkjo0X+O8Spj6KdzVo3A8oahpsi1JTTdLwN+QvIgQ== 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=ZKjYNyHrzN8FwFO8HICMzAK6sz7MPBXS3bI0uxd5tUcBfSunpsq2cyScDJWVvWinxOA8 2JOyC0EP4vkBDsSSYINamr4yG0MRLLGZMwwMNwEDUVsrYj5yYixUNML0Cyechs0bn3kBo XxLyA2eSX5fCXsh5QuvfXp8zTkxfq/V8nzkpaspP0KDtN1nmJYYB2e+ysdm7MSimCjYYV U90+S5mRGybvz//0cZpEgWM6Z+oWqrezD0er63qFNHbX2CK4Pg8ypNmcX7TjH00M5PmuF DNjsELxDCgMR8uGUkhfJKMKGzZDijrARPecrSLMLquI1HFdEKAZQhNl9FiAJMTFgepw== 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.179 Received: from mail-pl1-f179.google.com (mail-pl1-f179.google.com [209.85.214.179]) (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 324F0606A32D for ; Thu, 3 Dec 2026 17:24:53 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 324F0606A32D 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, unprotected) header.d=linaro.org header.i=@linaro.org header.a=rsa-sha256 header.s=google header.b=JJipXZA9 Received: by mail-pl1-f179.google.com with SMTP id d9443c01a7336-2f05bc71e01so962965ad.0 for ; Thu, 03 Dec 2026 09:24:53 -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=D76Cj3MTG+Ukt7iwuM9gwdEqWFiNfp12MeDvRQDr+aeOnuBGXdfA/Dv0AeV6zXPQU+ NRcNz4GtYrWWy6bVvkgjI7CU0oMv2mvJ+u7Wv0rrCtC0HzSYjNMZVCjmCsLQfeCau3Qi 95Cc4Xo20uOPeIFwuBDmSIoCEUahuZyH4Tbl0jOhdoW0jP1H0uhaIo8FpoZn4Bhm1KMd 2l1jTAS/mYa17C5Ne5K9FdIJSM/mQQLjnIox9+LDdxqs31nCWer5oQeYrg3MiarbKrg7 iBAsOhRYD1/QwnUtbo2ydW19eqmD8naVMO6XV3vasZZ/Mj1s8OGT78CHFETDBF1+J2HU oKYg== X-Forwarded-Encrypted: i=1; AL+hErGW+y1LSQweCyzNc4NcdhTHhAofo+uwhuvW7zckIMpzf3S4o2CjG1mbNWWjG63IooCYbUNlr66jYT7guWXYoZSo@vger.kernel.org X-Gm-Message-State: APuU4Z+vzewBTfJX9sTZbmddfBue0BmH4TCpd9eTsUNicOpM8zoU6tYp Zcj5WS7tWjzpj0S2Woi9wNk9owvK3cDr1ocdM37s0UOXmEnQ7AgiiiZ0CTA5eVLVR65pdvDt2Qs LUyuu X-Gm-Gg: AZFdTZfDIg2SAU6MMsYDX2Lju6CSQrEAULGuEkYz2XqBZJD978+e4cg4t5J87aDRxOC 5mNrij7xsd2ZcbHp58+n+fqtagPsOYqVvh5jbkN9L4R7vgHfXPHzWYi+7rxX7QmdNi7M+Rq/Mm8 UWLz0fm/kmt3NdowEh5RG/9qd4GmsdJeos2+x8MsH4PQAXrM0+Pl3L33PaN0REqutPn1iHznmF+ 1G3ltg05PCv233VOH25AvXS3C8P8hMDf6epEbl9x39SILgV9V6XggEDHcSB0UlQwnxT5p5tWqhE Nn5CYEtW/yaL1L3FURxk1M0NqxdIjH4B4VPdzVIwcrYOpjj83hLd5F5cXQjHsxW7hFWBcMgGywL MorA9AAoz9GR/UAs/Z5JyoUk5cO5u/cI6BK5f0TjcYP9rPN4AGNPgH21737m5/97o5a8rwTsJjr fanXDTx6T/xEW7rf6WsMeZ1qKnsiIzBfvvF+U6T9m+6HxuUdtsxS8qAwjSBVsWe6NCPzWqHAid+ itUDddWtinaMGDo/BwD8ExjOyH0nJb1VZ2GtQUwaeB7e0VzM+7Bi+D2VRqRNOt31o2mylxE4CmU NSdd5uAvZymPFDbK8s94ayVtE5Al1JhQ+tXd7qtph9PXIldLt6b7hQ== 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-remoteproc@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 >