* [PATCH v4 1/2] misc: misc_minor_alloc to use ida for all dynamic/misc dynamic minors
[not found] <2024101722-uncharted-wages-5759@gregkh>
@ 2024-10-17 13:35 ` Vimal Agrawal
2024-10-17 13:39 ` Greg KH
0 siblings, 1 reply; 3+ messages in thread
From: Vimal Agrawal @ 2024-10-17 13:35 UTC (permalink / raw)
To: linux-kernel, gregkh, arnd, quic_jjohnson, dan.carpenter
Cc: avimalin, vimal.agrawal, stable
misc_minor_alloc was allocating id using ida for minor only in case of
MISC_DYNAMIC_MINOR but misc_minor_free was always freeing ids
using ida_free causing a mismatch and following warn:
> > WARNING: CPU: 0 PID: 159 at lib/idr.c:525 ida_free+0x3e0/0x41f
> > ida_free called for id=127 which is not allocated.
> > <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
...
> > [<60941eb4>] ida_free+0x3e0/0x41f
> > [<605ac993>] misc_minor_free+0x3e/0xbc
> > [<605acb82>] misc_deregister+0x171/0x1b3
misc_minor_alloc is changed to allocate id from ida for all minors
falling in the range of dynamic/ misc dynamic minors
Fixes: ab760791c0cf ("char: misc: Increase the maximum number of dynamic misc devices to 1048448")
Signed-off-by: Vimal Agrawal <avimalin@gmail.com>
Cc: stable@vger.kernel.org
---
v2: Added Fixes:
added missed case for static minor in misc_minor_alloc
v3: Removed kunit changes as that will be added as second patch in this two patch series
v4: Updated Signed-off-by: to match from:
drivers/char/misc.c | 39 ++++++++++++++++++++++++++++++---------
1 file changed, 30 insertions(+), 9 deletions(-)
diff --git a/drivers/char/misc.c b/drivers/char/misc.c
index 541edc26ec89..2cf595d2e10b 100644
--- a/drivers/char/misc.c
+++ b/drivers/char/misc.c
@@ -63,16 +63,30 @@ static DEFINE_MUTEX(misc_mtx);
#define DYNAMIC_MINORS 128 /* like dynamic majors */
static DEFINE_IDA(misc_minors_ida);
-static int misc_minor_alloc(void)
+static int misc_minor_alloc(int minor)
{
- int ret;
-
- ret = ida_alloc_max(&misc_minors_ida, DYNAMIC_MINORS - 1, GFP_KERNEL);
- if (ret >= 0) {
- ret = DYNAMIC_MINORS - ret - 1;
+ int ret = 0;
+
+ if (minor == MISC_DYNAMIC_MINOR) {
+ /* allocate free id */
+ ret = ida_alloc_max(&misc_minors_ida, DYNAMIC_MINORS - 1, GFP_KERNEL);
+ if (ret >= 0) {
+ ret = DYNAMIC_MINORS - ret - 1;
+ } else {
+ ret = ida_alloc_range(&misc_minors_ida, MISC_DYNAMIC_MINOR + 1,
+ MINORMASK, GFP_KERNEL);
+ }
} else {
- ret = ida_alloc_range(&misc_minors_ida, MISC_DYNAMIC_MINOR + 1,
- MINORMASK, GFP_KERNEL);
+ /* specific minor, check if it is in dynamic or misc dynamic range */
+ if (minor < DYNAMIC_MINORS) {
+ minor = DYNAMIC_MINORS - minor - 1;
+ ret = ida_alloc_range(&misc_minors_ida, minor, minor, GFP_KERNEL);
+ } else if (minor > MISC_DYNAMIC_MINOR) {
+ ret = ida_alloc_range(&misc_minors_ida, minor, minor, GFP_KERNEL);
+ } else {
+ /* case of non-dynamic minors, no need to allocate id */
+ ret = 0;
+ }
}
return ret;
}
@@ -219,7 +233,7 @@ int misc_register(struct miscdevice *misc)
mutex_lock(&misc_mtx);
if (is_dynamic) {
- int i = misc_minor_alloc();
+ int i = misc_minor_alloc(misc->minor);
if (i < 0) {
err = -EBUSY;
@@ -228,6 +242,7 @@ int misc_register(struct miscdevice *misc)
misc->minor = i;
} else {
struct miscdevice *c;
+ int i;
list_for_each_entry(c, &misc_list, list) {
if (c->minor == misc->minor) {
@@ -235,6 +250,12 @@ int misc_register(struct miscdevice *misc)
goto out;
}
}
+
+ i = misc_minor_alloc(misc->minor);
+ if (i < 0) {
+ err = -EBUSY;
+ goto out;
+ }
}
dev = MKDEV(MISC_MAJOR, misc->minor);
--
2.17.1
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH v4 1/2] misc: misc_minor_alloc to use ida for all dynamic/misc dynamic minors
2024-10-17 13:35 ` [PATCH v4 1/2] misc: misc_minor_alloc to use ida for all dynamic/misc dynamic minors Vimal Agrawal
@ 2024-10-17 13:39 ` Greg KH
2024-10-17 14:06 ` Greg KH
0 siblings, 1 reply; 3+ messages in thread
From: Greg KH @ 2024-10-17 13:39 UTC (permalink / raw)
To: Vimal Agrawal
Cc: linux-kernel, arnd, quic_jjohnson, dan.carpenter, vimal.agrawal,
stable
On Thu, Oct 17, 2024 at 01:35:32PM +0000, Vimal Agrawal wrote:
> misc_minor_alloc was allocating id using ida for minor only in case of
> MISC_DYNAMIC_MINOR but misc_minor_free was always freeing ids
> using ida_free causing a mismatch and following warn:
> > > WARNING: CPU: 0 PID: 159 at lib/idr.c:525 ida_free+0x3e0/0x41f
> > > ida_free called for id=127 which is not allocated.
> > > <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
> ...
> > > [<60941eb4>] ida_free+0x3e0/0x41f
> > > [<605ac993>] misc_minor_free+0x3e/0xbc
> > > [<605acb82>] misc_deregister+0x171/0x1b3
>
> misc_minor_alloc is changed to allocate id from ida for all minors
> falling in the range of dynamic/ misc dynamic minors
>
> Fixes: ab760791c0cf ("char: misc: Increase the maximum number of dynamic misc devices to 1048448")
> Signed-off-by: Vimal Agrawal <avimalin@gmail.com>
Sorry, but no, do not hide behind a gmail.com address. Either fix your
corporate email system to be able to send patches out, or use the other
method of sending from a different address as documented in the kernel
documentation.
As it is, I can't take this, sorry.
greg k-h
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH v4 1/2] misc: misc_minor_alloc to use ida for all dynamic/misc dynamic minors
2024-10-17 13:39 ` Greg KH
@ 2024-10-17 14:06 ` Greg KH
0 siblings, 0 replies; 3+ messages in thread
From: Greg KH @ 2024-10-17 14:06 UTC (permalink / raw)
To: Vimal Agrawal
Cc: linux-kernel, arnd, quic_jjohnson, dan.carpenter, vimal.agrawal,
stable
On Thu, Oct 17, 2024 at 03:39:06PM +0200, Greg KH wrote:
> On Thu, Oct 17, 2024 at 01:35:32PM +0000, Vimal Agrawal wrote:
> > misc_minor_alloc was allocating id using ida for minor only in case of
> > MISC_DYNAMIC_MINOR but misc_minor_free was always freeing ids
> > using ida_free causing a mismatch and following warn:
> > > > WARNING: CPU: 0 PID: 159 at lib/idr.c:525 ida_free+0x3e0/0x41f
> > > > ida_free called for id=127 which is not allocated.
> > > > <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
> > ...
> > > > [<60941eb4>] ida_free+0x3e0/0x41f
> > > > [<605ac993>] misc_minor_free+0x3e/0xbc
> > > > [<605acb82>] misc_deregister+0x171/0x1b3
> >
> > misc_minor_alloc is changed to allocate id from ida for all minors
> > falling in the range of dynamic/ misc dynamic minors
> >
> > Fixes: ab760791c0cf ("char: misc: Increase the maximum number of dynamic misc devices to 1048448")
> > Signed-off-by: Vimal Agrawal <avimalin@gmail.com>
>
> Sorry, but no, do not hide behind a gmail.com address. Either fix your
> corporate email system to be able to send patches out, or use the other
> method of sending from a different address as documented in the kernel
> documentation.
>
> As it is, I can't take this, sorry.
Also, only patch 1/2 showed up, what happened to patch 2/2?
thanks,
greg k-h
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-10-17 14:06 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
[not found] <2024101722-uncharted-wages-5759@gregkh>
2024-10-17 13:35 ` [PATCH v4 1/2] misc: misc_minor_alloc to use ida for all dynamic/misc dynamic minors Vimal Agrawal
2024-10-17 13:39 ` Greg KH
2024-10-17 14:06 ` Greg KH
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox