From nobody Tue Dec 2 02:31:07 2025 Received: from out-170.mta1.migadu.com (out-170.mta1.migadu.com [95.215.58.170]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 57483363C6D for ; Tue, 18 Nov 2025 15:41:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=95.215.58.170 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763480514; cv=none; b=lI3dIKERblXZD0g2wpuU+WHEfP2Gsfiph++sTZwxqJg/vboqVr7AmkBJMtzFJzBXswD68W8OvxkSPfSA0fpVgUR2s2zChQFLWDytTsxTgUKyY8sptBhFAkZAeHN9jjEr9Qot8/fZeN62BQYl2KlSquKyaEg7RD4nI2S7GCJYGrI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763480514; c=relaxed/simple; bh=Y278PL0GzifJP03C/f/vcVWAXyroB47GiJuBr0lOs8E=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ErdlSYmxpoatOACBUccjvRUGzoCF20++Pnu3MUtFl/U7MM15ydetJlDQ7Ot3VfxXWMYsA4rK6Aa80hJYJ8X7q6Suh+kWyFHwQ7tbwVEvdBFFyrxm1Sqe9rztqcaPqg/KYexfZq5gwGayTXzTHp9qmqMZnc0aopU267DAu/y7JUQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=rwwdaZ7P; arc=none smtp.client-ip=95.215.58.170 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="rwwdaZ7P" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1763480510; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=cK/IupZxgDnZb2kaJtKj6GNDzCS1xiBqTw6TICCFNyY=; b=rwwdaZ7PazOftx5rvw5V0tCCEtg011z715cHHTSJRNaYv4YPQSAmQxddJ6b89BdTNanLsc lz9UoGyJgxfchVK2O9UYV6iwNOomgGJ1C9PcuE5QP5Jq8eldkQx55xxGoqCTh55lkS0AS3 mmy/8A3lyOL3sMnlyo5WK8HMNU6XGto= From: Dawei Li To: andersson@kernel.org, mathieu.poirier@linaro.org Cc: linux-remoteproc@vger.kernel.org, linux-kernel@vger.kernel.org, dawei.li@linux.dev, set_pte_at@outlook.com Subject: [PATCH v4 1/3] rpmsg: char: Fix WARN() in error path of rpmsg_eptdev_add() Date: Tue, 18 Nov 2025 23:41:05 +0800 Message-Id: <20251118154107.3100-2-dawei.li@linux.dev> In-Reply-To: <20251118154107.3100-1-dawei.li@linux.dev> References: <20251118154107.3100-1-dawei.li@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" put_device() is called on error path of rpmsg_eptdev_add() to cleanup resource attached to eptdev->dev, unfortunately it's bogus cause dev->release() is not set yet. When a struct device instance is destroyed, driver core framework checks the possible release() callback from candidates below: - struct device::release() - dev->type->release() - dev->class->dev_release() Rpmsg eptdev owns none of them so WARN() will complain the absence of release(). Fix it by: - Pre-assign dev->release() before potential error path. - Check before ida_free() in dev->release(). By fixing error path of rpmsg_eptdev_add() and fixing potential memory leak in rpmsg_anonymous_eptdev_create(), this work paves the way of rework of rpmsg_eptdev_add() and its callers. Fixes: c0cdc19f84a4 ("rpmsg: Driver for user space endpoint interface") Signed-off-by: Dawei Li --- drivers/rpmsg/rpmsg_char.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/drivers/rpmsg/rpmsg_char.c b/drivers/rpmsg/rpmsg_char.c index 34b35ea74aab..373b627581e8 100644 --- a/drivers/rpmsg/rpmsg_char.c +++ b/drivers/rpmsg/rpmsg_char.c @@ -408,8 +408,13 @@ static void rpmsg_eptdev_release_device(struct device = *dev) { struct rpmsg_eptdev *eptdev =3D dev_to_eptdev(dev); =20 - ida_free(&rpmsg_ept_ida, dev->id); - if (eptdev->dev.devt) + /* + * release() can be invoked from error path of rpmsg_eptdev_add(), + * WARN() will be fired if ida_free() is feed with invalid ID. + */ + if (likely(ida_exists(&rpmsg_ept_ida, dev->id))) + ida_free(&rpmsg_ept_ida, dev->id); + if (eptdev->dev.devt && likely(ida_exists(&rpmsg_minor_ida, MINOR(eptdev-= >dev.devt)))) ida_free(&rpmsg_minor_ida, MINOR(eptdev->dev.devt)); kfree(eptdev); } @@ -458,6 +463,8 @@ static int rpmsg_eptdev_add(struct rpmsg_eptdev *eptdev, struct device *dev =3D &eptdev->dev; int ret; =20 + dev->release =3D rpmsg_eptdev_release_device; + eptdev->chinfo =3D chinfo; =20 if (cdev) { @@ -471,7 +478,7 @@ static int rpmsg_eptdev_add(struct rpmsg_eptdev *eptdev, /* Anonymous inode device still need device name for dev_err() and friend= s */ ret =3D ida_alloc(&rpmsg_ept_ida, GFP_KERNEL); if (ret < 0) - goto free_minor_ida; + goto free_eptdev; dev->id =3D ret; dev_set_name(dev, "rpmsg%d", ret); =20 @@ -480,22 +487,13 @@ static int rpmsg_eptdev_add(struct rpmsg_eptdev *eptd= ev, if (cdev) { ret =3D cdev_device_add(&eptdev->cdev, &eptdev->dev); if (ret) - goto free_ept_ida; + goto free_eptdev; } =20 - /* We can now rely on the release function for cleanup */ - dev->release =3D rpmsg_eptdev_release_device; - return ret; =20 -free_ept_ida: - ida_free(&rpmsg_ept_ida, dev->id); -free_minor_ida: - if (cdev) - ida_free(&rpmsg_minor_ida, MINOR(dev->devt)); free_eptdev: put_device(dev); - kfree(eptdev); =20 return ret; } @@ -561,6 +559,8 @@ int rpmsg_anonymous_eptdev_create(struct rpmsg_device *= rpdev, struct device *par =20 if (!ret) *pfd =3D fd; + else + put_device(&eptdev->dev); =20 return ret; } --=20 2.25.1 From nobody Tue Dec 2 02:31:07 2025 Received: from out-181.mta0.migadu.com (out-181.mta0.migadu.com [91.218.175.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 021843659FE for ; Tue, 18 Nov 2025 15:42:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763480532; cv=none; b=ijyi2QDuZNeGKGgsGyaXUxwOG3oDPkdjFBLTfvB7o9aBYBHRFq+/PqG/K1ZrmheZZHe6KSkB59ibQVLUfmX8cA5OgBNpBfm0Ls5wilyp/67o9SlkFs37KxKGDgrJd2OESgsS1/NPgxAqpqa7/OIHObGZvCzQZC3ssGIvyNWFbUk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763480532; c=relaxed/simple; bh=WrExdfTS1uYA5jds6j3EehTade2/N0jdNymGodDarWI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=TRNFhu+rz2BN03WNf++cWkfYlEc98E8y4mLpLQn8YmYtxTqIT2TfcmQeOET4Ia3Yv9l6x4xYNOiL+HyrrFKlikgTUj6PzcOvgVab9rP3fHEJhScDDt+31uf2F12P0fSS8C51ncIO3EUvy8kCU6ycFu67qRx6OJ5tJd+m4xvVzV4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=HaVG2iIC; arc=none smtp.client-ip=91.218.175.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="HaVG2iIC" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1763480514; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=iFZThHKI9Tj3tjxqX+zEJQllSfC3S1umxABqldrWupE=; b=HaVG2iICuR3zdRs13GNYtC9ww9DojGNrfbK2w5WYG0Wrln7mX/4IZpQfYKEaCFWw45WbgL y6pGgp/P4VCO7fSSB5UWvdmxhvSTCi0b2a+W4PBEFgcg+3YORFzWgy9DjmUtW8Q1N3cjNx yyQh5TIZ5wYpAE8QGGSH7+kVsc+yzRs= From: Dawei Li To: andersson@kernel.org, mathieu.poirier@linaro.org Cc: linux-remoteproc@vger.kernel.org, linux-kernel@vger.kernel.org, dawei.li@linux.dev, set_pte_at@outlook.com Subject: [PATCH v4 2/3] rpmsg: char: Rework of rpmsg_eptdev_add() and its callers Date: Tue, 18 Nov 2025 23:41:06 +0800 Message-Id: <20251118154107.3100-3-dawei.li@linux.dev> In-Reply-To: <20251118154107.3100-1-dawei.li@linux.dev> References: <20251118154107.3100-1-dawei.li@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" For every caller of rpmsg_eptdev_add(), eptdev is allocated in rpmsg_eptdev_alloc(). Resource should be freed where it's allocated, In this case, eptdev should be freed in caller of rpmsg_eptdev_add() rather than rpmsg_eptdev_add() itself. Move the error handling from rpmsg_eptdev_add() to its callers. Fixes: 2410558f5f11 ("rpmsg: char: Implement eptdev based on anonymous inod= e") Signed-off-by: Dawei Li --- drivers/rpmsg/rpmsg_char.c | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/drivers/rpmsg/rpmsg_char.c b/drivers/rpmsg/rpmsg_char.c index 373b627581e8..56371899212f 100644 --- a/drivers/rpmsg/rpmsg_char.c +++ b/drivers/rpmsg/rpmsg_char.c @@ -470,7 +470,7 @@ static int rpmsg_eptdev_add(struct rpmsg_eptdev *eptdev, if (cdev) { ret =3D ida_alloc_max(&rpmsg_minor_ida, RPMSG_DEV_MAX - 1, GFP_KERNEL); if (ret < 0) - goto free_eptdev; + return ret; =20 dev->devt =3D MKDEV(MAJOR(rpmsg_major), ret); } @@ -478,7 +478,7 @@ static int rpmsg_eptdev_add(struct rpmsg_eptdev *eptdev, /* Anonymous inode device still need device name for dev_err() and friend= s */ ret =3D ida_alloc(&rpmsg_ept_ida, GFP_KERNEL); if (ret < 0) - goto free_eptdev; + return ret; dev->id =3D ret; dev_set_name(dev, "rpmsg%d", ret); =20 @@ -486,15 +486,8 @@ static int rpmsg_eptdev_add(struct rpmsg_eptdev *eptde= v, =20 if (cdev) { ret =3D cdev_device_add(&eptdev->cdev, &eptdev->dev); - if (ret) - goto free_eptdev; } =20 - return ret; - -free_eptdev: - put_device(dev); - return ret; } =20 @@ -507,12 +500,17 @@ int rpmsg_chrdev_eptdev_create(struct rpmsg_device *r= pdev, struct device *parent struct rpmsg_channel_info chinfo) { struct rpmsg_eptdev *eptdev; + int ret; =20 eptdev =3D rpmsg_chrdev_eptdev_alloc(rpdev, parent); if (IS_ERR(eptdev)) return PTR_ERR(eptdev); =20 - return rpmsg_chrdev_eptdev_add(eptdev, chinfo); + ret =3D rpmsg_chrdev_eptdev_add(eptdev, chinfo); + if (ret) + put_device(&eptdev->dev); + + return ret; } EXPORT_SYMBOL(rpmsg_chrdev_eptdev_create); =20 @@ -544,6 +542,7 @@ int rpmsg_anonymous_eptdev_create(struct rpmsg_device *= rpdev, struct device *par ret =3D rpmsg_eptdev_add(eptdev, chinfo, false); if (ret) { dev_err(&eptdev->dev, "failed to add %s\n", eptdev->chinfo.name); + put_device(&eptdev->dev); return ret; } =20 @@ -571,6 +570,7 @@ static int rpmsg_chrdev_probe(struct rpmsg_device *rpde= v) struct rpmsg_channel_info chinfo; struct rpmsg_eptdev *eptdev; struct device *dev =3D &rpdev->dev; + int ret; =20 memcpy(chinfo.name, rpdev->id.name, RPMSG_NAME_SIZE); chinfo.src =3D rpdev->src; @@ -589,7 +589,11 @@ static int rpmsg_chrdev_probe(struct rpmsg_device *rpd= ev) */ eptdev->default_ept->priv =3D eptdev; =20 - return rpmsg_chrdev_eptdev_add(eptdev, chinfo); + ret =3D rpmsg_chrdev_eptdev_add(eptdev, chinfo); + if (ret) + put_device(&eptdev->dev); + + return ret; } =20 static void rpmsg_chrdev_remove(struct rpmsg_device *rpdev) --=20 2.25.1 From nobody Tue Dec 2 02:31:07 2025 Received: from out-172.mta0.migadu.com (out-172.mta0.migadu.com [91.218.175.172]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B5A3F35CB9F for ; Tue, 18 Nov 2025 15:42:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.172 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763480526; cv=none; b=nOBs8OJFn5vcaFdfYEBsSm3WIl0Luqj+krVqacVEssyUug//CCjV8qlVR0j/4pe7suGlYxR9/M7HhRT+rqOiGGB5AK4+iaVJpLZ1y1ENR5/k1c8bIqHS6p4TR/KALimxzR898Y1Mmc1FoXZzoJsfYhSb8mClV5OLVinrq3gbULo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1763480526; c=relaxed/simple; bh=p7JmlZu7aX2oza+mhHmm837atoJM0dG3uJyoPzvTEUo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=NWwgpJ0f2voKdDUS2tQzbjZTKq2wi4s8H/K7V8XCBPbaFxA18h33QM1FR4azrX/YYotXOn5bBIEqtNnwMKTEe132gwBhWl+/lLAjQVpRVQpLj/9g+/1uVRjOj891ncT21biMRK2ZPgTbml39SSyvQWXDt/D86J0FGcrRW2rgyS8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=qfxmlxwJ; arc=none smtp.client-ip=91.218.175.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="qfxmlxwJ" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1763480520; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=JMBn6kpg/KtrTcaUg3wwySbqhmE37Z6eHvYm9uflh8s=; b=qfxmlxwJchfijDosgAxIMu1GLpjHS0tXWsShr2fsy5DlZLEBfjn6Kb+15H6VMlZwin1TFI 6cZboYNBhCUeKaM9k5fKvWhTQ/7pV+ndvAN/URlH28/HWm+8xLhmMG+dLiZVMzA9VI0UJi uxTx3N/YsZFYfZDgOJlrh2sxszq3LSM= From: Dawei Li To: andersson@kernel.org, mathieu.poirier@linaro.org Cc: linux-remoteproc@vger.kernel.org, linux-kernel@vger.kernel.org, dawei.li@linux.dev, set_pte_at@outlook.com Subject: [PATCH v4 3/3] rpmsg: char: Merge cdev branches in rpmsg_eptdev_add() Date: Tue, 18 Nov 2025 23:41:07 +0800 Message-Id: <20251118154107.3100-4-dawei.li@linux.dev> In-Reply-To: <20251118154107.3100-1-dawei.li@linux.dev> References: <20251118154107.3100-1-dawei.li@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" There are two if(cdev) branches in rpmsg_eptdev_add(), merge them. Note that cdev_device_add() still must be last one since dev->release() doesn't reclaim cdev related resource. Signed-off-by: Dawei Li --- drivers/rpmsg/rpmsg_char.c | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/drivers/rpmsg/rpmsg_char.c b/drivers/rpmsg/rpmsg_char.c index 56371899212f..505411029fe0 100644 --- a/drivers/rpmsg/rpmsg_char.c +++ b/drivers/rpmsg/rpmsg_char.c @@ -467,14 +467,6 @@ static int rpmsg_eptdev_add(struct rpmsg_eptdev *eptde= v, =20 eptdev->chinfo =3D chinfo; =20 - if (cdev) { - ret =3D ida_alloc_max(&rpmsg_minor_ida, RPMSG_DEV_MAX - 1, GFP_KERNEL); - if (ret < 0) - return ret; - - dev->devt =3D MKDEV(MAJOR(rpmsg_major), ret); - } - /* Anonymous inode device still need device name for dev_err() and friend= s */ ret =3D ida_alloc(&rpmsg_ept_ida, GFP_KERNEL); if (ret < 0) @@ -485,6 +477,12 @@ static int rpmsg_eptdev_add(struct rpmsg_eptdev *eptde= v, ret =3D 0; =20 if (cdev) { + ret =3D ida_alloc_max(&rpmsg_minor_ida, RPMSG_DEV_MAX - 1, GFP_KERNEL); + if (ret < 0) + return ret; + + dev->devt =3D MKDEV(MAJOR(rpmsg_major), ret); + ret =3D cdev_device_add(&eptdev->cdev, &eptdev->dev); } =20 --=20 2.25.1