Skip to content

Commit 31b636d

Browse files
Thadeu Lima de Souza Cascardogregkh
authored andcommitted
char: misc: restrict the dynamic range to exclude reserved minors
When this was first reported [1], the possibility of having sufficient number of dynamic misc devices was theoretical, in the case of dlm driver. In practice, its userspace never created more than one device. What we know from commit ab76079 ("char: misc: Increase the maximum number of dynamic misc devices to 1048448"), is that the miscdevice interface has been used for allocating more than the single-shot devices it was designed for. And it is not only coresight_tmc, but many other drivers are able to create multiple devices. On systems like the ones described in the above commit, it is certain that the dynamic allocation will allocate certain reserved minor numbers, leading to failures when a later driver tries to claim its reserved number. Instead of excluding the historically statically allocated range from dynamic allocation, restrict the latter to minors above 255. That also removes the need for DYNAMIC_MINORS and the convolution in allocating minor numbers, simplifying the code. Since commit ab76079 ("char: misc: Increase the maximum number of dynamic misc devices to 1048448") has been applied, such range is already possible. And given such devices already need to be dynamically created, there should be no systems where this might become a problem. [1] https://lore.kernel.org/all/1257813017-28598-3-git-send-email-cascardo@holoscopio.com/ Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@igalia.com> Link: https://lore.kernel.org/r/20250423-misc-dynrange-v4-1-133b5ae4ca18@igalia.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent d0fd663 commit 31b636d

File tree

1 file changed

+5
-23
lines changed

1 file changed

+5
-23
lines changed

drivers/char/misc.c

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,8 @@ static LIST_HEAD(misc_list);
5858
static DEFINE_MUTEX(misc_mtx);
5959

6060
/*
61-
* Assigned numbers, used for dynamic minors
61+
* Assigned numbers.
6262
*/
63-
#define DYNAMIC_MINORS 128 /* like dynamic majors */
6463
static DEFINE_IDA(misc_minors_ida);
6564

6665
static int misc_minor_alloc(int minor)
@@ -69,34 +68,17 @@ static int misc_minor_alloc(int minor)
6968

7069
if (minor == MISC_DYNAMIC_MINOR) {
7170
/* allocate free id */
72-
ret = ida_alloc_max(&misc_minors_ida, DYNAMIC_MINORS - 1, GFP_KERNEL);
73-
if (ret >= 0) {
74-
ret = DYNAMIC_MINORS - ret - 1;
75-
} else {
76-
ret = ida_alloc_range(&misc_minors_ida, MISC_DYNAMIC_MINOR + 1,
77-
MINORMASK, GFP_KERNEL);
78-
}
71+
ret = ida_alloc_range(&misc_minors_ida, MISC_DYNAMIC_MINOR + 1,
72+
MINORMASK, GFP_KERNEL);
7973
} else {
80-
/* specific minor, check if it is in dynamic or misc dynamic range */
81-
if (minor < DYNAMIC_MINORS) {
82-
minor = DYNAMIC_MINORS - minor - 1;
83-
ret = ida_alloc_range(&misc_minors_ida, minor, minor, GFP_KERNEL);
84-
} else if (minor > MISC_DYNAMIC_MINOR) {
85-
ret = ida_alloc_range(&misc_minors_ida, minor, minor, GFP_KERNEL);
86-
} else {
87-
/* case of non-dynamic minors, no need to allocate id */
88-
ret = 0;
89-
}
74+
ret = ida_alloc_range(&misc_minors_ida, minor, minor, GFP_KERNEL);
9075
}
9176
return ret;
9277
}
9378

9479
static void misc_minor_free(int minor)
9580
{
96-
if (minor < DYNAMIC_MINORS)
97-
ida_free(&misc_minors_ida, DYNAMIC_MINORS - minor - 1);
98-
else if (minor > MISC_DYNAMIC_MINOR)
99-
ida_free(&misc_minors_ida, minor);
81+
ida_free(&misc_minors_ida, minor);
10082
}
10183

10284
#ifdef CONFIG_PROC_FS

0 commit comments

Comments
 (0)