From: Zijun Hu zijun.hu@oss.qualcomm.com
[ Upstream commit 0ef1fe4bc38673db72e39b700b29c50dfcc5a415 ]
misc_init() returns -EIO for __register_chrdev() invocation failure, but:
- -EIO is for I/O error normally, but __register_chrdev() does not do I/O. - -EIO can not cover various error codes returned by __register_chrdev().
Fix by returning error code of __register_chrdev().
Signed-off-by: Zijun Hu zijun.hu@oss.qualcomm.com Link: https://lore.kernel.org/r/20250620-fix_mischar-v1-3-6c2716bbf1fa@oss.qualcom... Signed-off-by: Greg Kroah-Hartman gregkh@linuxfoundation.org Signed-off-by: Sasha Levin sashal@kernel.org ---
LLM Generated explanations, may be completely bogus:
**Backport Status: YES**
This commit should be backported to stable kernel trees for the following reasons:
1. **Bug Fix Nature**: This is a clear bug fix that corrects improper error handling in `misc_init()`. The original code hardcoded `-EIO` regardless of the actual error returned by `__register_chrdev()`, which could mask the real failure reason.
2. **Small and Contained Change**: The fix is minimal - just 4 lines changed: - Removed the hardcoded `err = -EIO;` assignment - Changed `__register_chrdev()` from being used in an if condition to storing its return value - Updated the error check to properly test for negative error codes (`err < 0`)
3. **Improves Diagnostic Accuracy**: By returning the actual error code from `__register_chrdev()`, system administrators and developers can get accurate information about why the misc device initialization failed. Looking at the `__register_chrdev()` implementation, it can return various error codes like `-ENOMEM`, `-EBUSY`, or error codes from `__register_chrdev_region()`.
4. **No Architectural Changes**: This commit doesn't introduce any new features or change the architecture. It simply fixes error propagation in the initialization path.
5. **Low Risk of Regression**: The change is straightforward error handling correction. The behavior remains the same for successful cases, and only the error code changes for failure cases. This cannot break any working systems.
6. **Affects Core Subsystem**: The misc device subsystem is fundamental to many kernel drivers and userspace applications. Having accurate error codes during initialization failures is important for debugging boot issues.
7. **Follows Stable Rules**: This meets the stable kernel criteria - it's a real bug fix (incorrect error codes), it's small (4 lines), and it has minimal risk while providing clear benefit.
The commit properly preserves the error path behavior while ensuring the actual error from `__register_chrdev()` is propagated up, making it an ideal candidate for stable backporting.
drivers/char/misc.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/drivers/char/misc.c b/drivers/char/misc.c index dda466f9181a..30178e20d962 100644 --- a/drivers/char/misc.c +++ b/drivers/char/misc.c @@ -314,8 +314,8 @@ static int __init misc_init(void) if (err) goto fail_remove;
- err = -EIO; - if (__register_chrdev(MISC_MAJOR, 0, MINORMASK + 1, "misc", &misc_fops)) + err = __register_chrdev(MISC_MAJOR, 0, MINORMASK + 1, "misc", &misc_fops); + if (err < 0) goto fail_printk; return 0;