Skip to content

Commit 1f2f38d

Browse files
Thadeu Lima de Souza Cascardotorvalds
authored andcommitted
drivers/char/misc.c: use bitmap/bitops functions for dynamic minor number allocation
Use DECLARE_BITMAP(), find_first_zero_bit(), set_bit() and clear_bit() instead of rewriting code to do it with the minor number dynamic allocation bitmap. We need to invert the bit position to keep the code behaviour of using the last minor numbers first, since we don't have a find_last_zero_bit. Signed-off-by: Thadeu Lima de Souza Cascardo <cascardo@holoscopio.com> Cc: "H. Peter Anvin" <hpa@zytor.com> Cc: Alan Cox <alan@lxorguk.ukuu.org.uk> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 4ae717d commit 1f2f38d

1 file changed

Lines changed: 9 additions & 13 deletions

File tree

‎drivers/char/misc.c‎

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ static DEFINE_MUTEX(misc_mtx);
6060
* Assigned numbers, used for dynamic minors
6161
*/
6262
#define DYNAMIC_MINORS 64 /* like dynamic majors */
63-
static unsigned char misc_minors[DYNAMIC_MINORS / 8];
63+
static DECLARE_BITMAP(misc_minors, DYNAMIC_MINORS);
6464

6565
#ifdef CONFIG_PROC_FS
6666
static void *misc_seq_start(struct seq_file *seq, loff_t *pos)
@@ -196,27 +196,23 @@ int misc_register(struct miscdevice * misc)
196196
}
197197

198198
if (misc->minor == MISC_DYNAMIC_MINOR) {
199-
int i = DYNAMIC_MINORS;
200-
while (--i >= 0)
201-
if ( (misc_minors[i>>3] & (1 << (i&7))) == 0)
202-
break;
203-
if (i<0) {
199+
int i = find_first_zero_bit(misc_minors, DYNAMIC_MINORS);
200+
if (i >= DYNAMIC_MINORS) {
204201
mutex_unlock(&misc_mtx);
205202
return -EBUSY;
206203
}
207-
misc->minor = i;
204+
misc->minor = DYNAMIC_MINORS - i - 1;
205+
set_bit(i, misc_minors);
208206
}
209207

210-
if (misc->minor < DYNAMIC_MINORS)
211-
misc_minors[misc->minor >> 3] |= 1 << (misc->minor & 7);
212208
dev = MKDEV(MISC_MAJOR, misc->minor);
213209

214210
misc->this_device = device_create(misc_class, misc->parent, dev,
215211
misc, "%s", misc->name);
216212
if (IS_ERR(misc->this_device)) {
217-
int i = misc->minor;
213+
int i = DYNAMIC_MINORS - misc->minor - 1;
218214
if (i < DYNAMIC_MINORS && i >= 0)
219-
misc_minors[i>>3] &= ~(1 << (i & 7));
215+
clear_bit(i, misc_minors);
220216
err = PTR_ERR(misc->this_device);
221217
goto out;
222218
}
@@ -243,7 +239,7 @@ int misc_register(struct miscdevice * misc)
243239

244240
int misc_deregister(struct miscdevice *misc)
245241
{
246-
int i = misc->minor;
242+
int i = DYNAMIC_MINORS - misc->minor - 1;
247243

248244
if (list_empty(&misc->list))
249245
return -EINVAL;
@@ -252,7 +248,7 @@ int misc_deregister(struct miscdevice *misc)
252248
list_del(&misc->list);
253249
device_destroy(misc_class, MKDEV(MISC_MAJOR, misc->minor));
254250
if (i < DYNAMIC_MINORS && i >= 0)
255-
misc_minors[i>>3] &= ~(1 << (i & 7));
251+
clear_bit(i, misc_minors);
256252
mutex_unlock(&misc_mtx);
257253
return 0;
258254
}

0 commit comments

Comments
 (0)