From: Zijun Hu <zijun_hu@icloud.com>
To: Arnd Bergmann <arnd@arndb.de>,
Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
"James E.J. Bottomley" <James.Bottomley@HansenPartnership.com>,
Helge Deller <deller@gmx.de>,
"David S. Miller" <davem@davemloft.net>,
Andreas Larsson <andreas@gaisler.com>
Cc: Thadeu Lima de Souza Cascardo <cascardo@igalia.com>,
Zijun Hu <zijun_hu@icloud.com>,
linux-kernel@vger.kernel.org, linux-parisc@vger.kernel.org,
sparclinux@vger.kernel.org, Zijun Hu <zijun.hu@oss.qualcomm.com>
Subject: [PATCH v5 3/8] char: misc: Disallow registering miscdevice whose minor > MISC_DYNAMIC_MINOR
Date: Thu, 10 Jul 2025 19:56:46 +0800 [thread overview]
Message-ID: <20250710-rfc_miscdev-v5-3-b3940297db16@oss.qualcomm.com> (raw)
In-Reply-To: <20250710-rfc_miscdev-v5-0-b3940297db16@oss.qualcomm.com>
From: Zijun Hu <zijun.hu@oss.qualcomm.com>
Currently, It is allowed to register miscdevice with minor > 255
which is defined by macro MISC_DYNAMIC_MINOR, and cause:
- Chaos regarding division and management of minor codes.
- Registering failure if the minor was allocated to other dynamic request.
Fortunately, in-kernel users have not had such usage yet.
Fix by refusing to register miscdevice whose minor > 255.
Also bring in a very simple minor code space division and management:
< 255 : Fixed minor code
== 255 : Indicator to request dynamic minor code
> 255 : Dynamic minor code requested, 1048320 minor codes totally
And all fixed minors allocated should be registered in 'linux/miscdevice.h'
Signed-off-by: Zijun Hu <zijun.hu@oss.qualcomm.com>
---
drivers/char/misc.c | 6 ++++++
include/linux/miscdevice.h | 8 ++++++++
2 files changed, 14 insertions(+)
diff --git a/drivers/char/misc.c b/drivers/char/misc.c
index 558302a64dd908aee30341547a5413df1af71459..b8e66466184fa21fb66d968db7950e0b5669ac43 100644
--- a/drivers/char/misc.c
+++ b/drivers/char/misc.c
@@ -210,6 +210,12 @@ int misc_register(struct miscdevice *misc)
int err = 0;
bool is_dynamic = (misc->minor == MISC_DYNAMIC_MINOR);
+ if (misc->minor > MISC_DYNAMIC_MINOR) {
+ pr_err("Invalid fixed minor %d for miscdevice '%s'\n",
+ misc->minor, misc->name);
+ return -EINVAL;
+ }
+
INIT_LIST_HEAD(&misc->list);
mutex_lock(&misc_mtx);
diff --git a/include/linux/miscdevice.h b/include/linux/miscdevice.h
index 3e6deb00fc8535a7571f85489c74979e18385bad..565b88efeb23d02b7d91df1cd7df4bdcf2898224 100644
--- a/include/linux/miscdevice.h
+++ b/include/linux/miscdevice.h
@@ -71,6 +71,14 @@
#define USERIO_MINOR 240
#define VHOST_VSOCK_MINOR 241
#define RFKILL_MINOR 242
+
+/*
+ * Misc char device minor code space division related to below macro:
+ *
+ * < 255 : Fixed minor code
+ * == 255 : Indicator to request dynamic minor code
+ * > 255 : Dynamic minor code requested, 1048320 minor codes totally.
+ */
#define MISC_DYNAMIC_MINOR 255
struct miscdevice {
--
2.34.1
next prev parent reply other threads:[~2025-07-10 11:57 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-10 11:56 [PATCH v5 0/8] char: misc: Various cleanup for miscdevice Zijun Hu
2025-07-10 11:56 ` [PATCH v5 1/8] char: misc: Move drivers/misc/misc_minor_kunit.c to drivers/char/ Zijun Hu
2025-07-10 11:56 ` [PATCH v5 2/8] char: misc: Adapt and add test cases for simple minor space division Zijun Hu
2025-07-10 14:34 ` Thadeu Lima de Souza Cascardo
2025-07-14 0:42 ` Zijun Hu
2025-07-10 11:56 ` Zijun Hu [this message]
2025-07-10 11:56 ` [PATCH v5 4/8] char: misc: Add a case to test registering miscdevice again without reinitialization Zijun Hu
2025-07-10 11:56 ` [PATCH v5 5/8] char: misc: Fix kunit test case miscdev_test_dynamic_reentry() failure Zijun Hu
2025-07-10 18:15 ` Thadeu Lima de Souza Cascardo
2025-07-14 1:02 ` Zijun Hu
2025-07-14 11:26 ` Thadeu Lima de Souza Cascardo
2025-07-10 11:56 ` [PATCH v5 6/8] char: misc: Does not request module for miscdevice with dynamic minor Zijun Hu
2025-07-10 18:22 ` Thadeu Lima de Souza Cascardo
2025-07-10 11:56 ` [PATCH v5 7/8] char: misc: Register fixed minor EISA_EEPROM_MINOR in linux/miscdevice.h Zijun Hu
2025-07-10 11:56 ` [PATCH v5 8/8] sparc: kernel: apc: Remove macro APC_MINOR definition Zijun Hu
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=20250710-rfc_miscdev-v5-3-b3940297db16@oss.qualcomm.com \
--to=zijun_hu@icloud.com \
--cc=James.Bottomley@HansenPartnership.com \
--cc=andreas@gaisler.com \
--cc=arnd@arndb.de \
--cc=cascardo@igalia.com \
--cc=davem@davemloft.net \
--cc=deller@gmx.de \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-parisc@vger.kernel.org \
--cc=sparclinux@vger.kernel.org \
--cc=zijun.hu@oss.qualcomm.com \
/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).