From: Zhangfei Gao <zhangfei.gao@linaro.org>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
Alexander Viro <viro@zeniv.linux.org.uk>,
Christian Brauner <brauner@kernel.org>,
Wenkai Lin <linwenkai6@hisilicon.com>,
Chenghai Huang <huangchenghai2@huawei.com>
Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
Zhangfei Gao <zhangfei.gao@linaro.org>
Subject: [PATCH] chardev: fix consistent error handling in cdev_device_add
Date: Wed, 19 Nov 2025 10:15:40 +0000 [thread overview]
Message-ID: <20251119101540.106441-1-zhangfei.gao@linaro.org> (raw)
Currently cdev_device_add has inconsistent error handling:
- If device_add fails, it calls cdev_del(cdev)
- If cdev_add fails, it only returns error without cleanup
This creates a problem because cdev_set_parent(cdev, &dev->kobj)
establishes a parent-child relationship.
When callers use cdev_del(cdev) to clean up after cdev_add failure,
it also decrements the dev's refcount due to the parent relationship,
causing refcount mismatch.
To unify error handling:
- Set cdev->kobj.parent = NULL first to break the parent relationship
- Then call cdev_del(cdev) for cleanup
This ensures that in both error paths,
the dev's refcount remains consistent and callers don't need
special handling for different failure scenarios.
Signed-off-by: Zhangfei Gao <zhangfei.gao@linaro.org>
---
fs/char_dev.c | 5 ++++-
1 file changed, 4 insertions(+), 1 deletion(-)
diff --git a/fs/char_dev.c b/fs/char_dev.c
index c2ddb998f3c9..fef6ee1aba66 100644
--- a/fs/char_dev.c
+++ b/fs/char_dev.c
@@ -549,8 +549,11 @@ int cdev_device_add(struct cdev *cdev, struct device *dev)
cdev_set_parent(cdev, &dev->kobj);
rc = cdev_add(cdev, dev->devt, 1);
- if (rc)
+ if (rc) {
+ cdev->kobj.parent = NULL;
+ cdev_del(cdev);
return rc;
+ }
}
rc = device_add(dev);
--
2.25.1
next reply other threads:[~2025-11-19 10:15 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-11-19 10:15 Zhangfei Gao [this message]
2025-11-25 15:32 ` [PATCH] chardev: fix consistent error handling in cdev_device_add Zhangfei Gao
2025-11-26 10:27 ` Christian Brauner
2025-11-26 11:01 ` Zhangfei Gao
2025-11-28 2:08 ` Zhangfei Gao
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=20251119101540.106441-1-zhangfei.gao@linaro.org \
--to=zhangfei.gao@linaro.org \
--cc=brauner@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=huangchenghai2@huawei.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linwenkai6@hisilicon.com \
--cc=viro@zeniv.linux.org.uk \
/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;
as well as URLs for NNTP newsgroup(s).