char_dev: extend dynamic allocation of majors into a higher range
Subsystems (3)
PATCH 1/2 char_dev: extend dynamic allocation of majors into a higher range
We've run into problems with running out of dynamicly assign char device majors particullarly on automated test systems with all-yes-configs. Roughly 40 dynamic assignments can be made with such kernels at this time while space is reserved for only 20.
Currently, the kernel only prints a warning when dynamic allocation overflows the reserved region. And when this happens drivers that have fixed assignments can randomly fail depending on the order of initialization of other drivers. Thus, adding a new char device can cause unexpected failures in completely unrelated parts of the kernel.
This patch solves the problem by extending dynamic major number allocations down from 511 once the 234-254 region fills up. Fixed majors already exist above 255 so the infrastructure to support high number majors is already in place. The patch reserves an additional 128 major numbers which should hopefully last us a while.
Kernels that don't require more than 20 dynamic majors assigned (which is pretty typical) should not be affected by this change.
--- Documentation/admin-guide/devices.txt | 5 +++++ fs/char_dev.c | 41 ++++++++++++++++++++++++----------- include/linux/fs.h | 4 ++++ 3 files changed, 37 insertions(+), 13 deletions(-)
Documentation/admin-guide/devices.txt +5 -0
@@ -3079,3 +3079,8 @@ 1 = /dev/osd1 Second OSD Device ... 255 = /dev/osd255 256th OSD Device + + 384-511 char RESERVED FOR DYNAMIC ASSIGNMENT + Character devices that request a dynamic allocation of major + number will take numbers starting from 511 and downward, + once the 234-254 range is full.
fs/char_dev.c +28 -13
56 lines not on this page · raw message
include/linux/fs.h +4 -0
@@ -2448,6 +2448,10 @@ static inline void bd_unlink_disk_holder(struct block_device *bdev,#define CHRDEV_MAJOR_HASH_SIZE 255/* Marks the bottom of the first segment of free char majors */ #define CHRDEV_MAJOR_DYN_END 234 +/* Marks the top and bottom of the second segment of free char majors */ +#define CHRDEV_MAJOR_DYN_EXT_START 511 +#define CHRDEV_MAJOR_DYN_EXT_END 384 +extern int alloc_chrdev_region (dev_t *,unsigned ,unsigned ,const char *);extern int register_chrdev_region (dev_t ,unsigned ,const char *); extern int __register_chrdev(unsigned int major, unsigned int baseminor,
Signature · 1 line
2.11.0
PATCH 2/2 char_dev: order /proc/devices by major number
Presently, the order of the char devices listed in /proc/devices is not entirely sequential. If a char device has a major number greater than CHRDEV_MAJOR_HASH_SIZE (255), it will be ordered as if its major were module 255. For example, 511 appears after 1.
This patch cleans that up and prints each major number in the correct order, regardless of where they are stored in the hash table.
In order to do this, we introduce CHRDEV_MAJOR_MAX as an artificial limit (chosen to be 511). It will then print all devices in major order number from 0 to the maximum.
--- fs/char_dev.c | 17 +++++++++++++---- fs/proc/devices.c | 8 ++++---- include/linux/fs.h | 2 +- 3 files changed, 18 insertions(+), 9 deletions(-)
fs/char_dev.c +13 -4
@@ -28,6 +28,8 @@ static struct kobj_map *cdev_map;static DEFINE_MUTEX (chrdevs_lock); +#define CHRDEV_MAJOR_HASH_SIZE 255 + static struct char_device_struct { struct char_device_struct *next; unsigned int major;
@@ -49,12 +51,12 @@ void chrdev_show(struct seq_file *f, off_t offset) {struct char_device_struct *cd; -if (offset <CHRDEV_MAJOR_HASH_SIZE ) { -mutex_lock (&chrdevs_lock); -for (cd = chrdevs[offset]; cd; cd = cd->next) +mutex_lock (&chrdevs_lock); +for (cd = chrdevs[major_to_index (offset)]; cd; cd = cd->next) { +if (cd->major == offset)seq_printf (f,"%3d %s\n" , cd->major, cd->name); -mutex_unlock (&chrdevs_lock); } +mutex_unlock (&chrdevs_lock); }#endif /* CONFIG_PROC_FS */
@@ -117,6 +119,13 @@ __register_chrdev_region(unsigned int major, unsigned int baseminor, major = ret; } +if (major >=CHRDEV_MAJOR_MAX ) { +pr_err ("CHRDEV \"%s\" major requested (%d) is greater than the maximum (%d)\n" , + name, major,CHRDEV_MAJOR_MAX ); + ret = -EINVAL ; + goto out; + } + cd->major = major; cd->baseminor = baseminor; cd->minorct = minorct;
fs/proc/devices.c +4 -4
@@ -7,14 +7,14 @@ static int devinfo_show(struct seq_file *f, void *v) {int i = *(loff_t *) v; -if (i <CHRDEV_MAJOR_HASH_SIZE ) { +if (i <CHRDEV_MAJOR_MAX ) {if (i ==0 )seq_puts (f,"Character devices:\n" );chrdev_show (f, i); }#ifdef CONFIG_BLOCK else { - i -=CHRDEV_MAJOR_HASH_SIZE ; + i -=CHRDEV_MAJOR_MAX ;if (i ==0 )seq_puts (f,"\nBlock devices:\n" );blkdev_show (f, i);
@@ -25,7 +25,7 @@ static int devinfo_show(struct seq_file *f, void *v)static void *devinfo_start (struct seq_file *f,loff_t *pos) { -if (*pos < (BLKDEV_MAJOR_HASH_SIZE +CHRDEV_MAJOR_HASH_SIZE )) +if (*pos < (BLKDEV_MAJOR_HASH_SIZE +CHRDEV_MAJOR_MAX ))return pos;return NULL ; }
@@ -33,7 +33,7 @@ static void *devinfo_start(struct seq_file *f, loff_t *pos)static void *devinfo_next (struct seq_file *f,void *v,loff_t *pos) { (*pos)++; -if (*pos >= (BLKDEV_MAJOR_HASH_SIZE +CHRDEV_MAJOR_HASH_SIZE )) +if (*pos >= (BLKDEV_MAJOR_HASH_SIZE +CHRDEV_MAJOR_MAX ))return NULL ;return pos; }
include/linux/fs.h +1 -1
@@ -2445,7 +2445,7 @@ static inline void bd_unlink_disk_holder(struct block_device *bdev,#endif /* fs/char_dev.c */ -#define CHRDEV_MAJOR_HASH_SIZE 255 +#define CHRDEV_MAJOR_MAX 512/* Marks the bottom of the first segment of free char majors */ #define CHRDEV_MAJOR_DYN_END 234/* Marks the top and bottom of the second segment of free char majors */
Signature · 1 line
2.11.0
Re: [PATCH 2/2] char_dev: order /proc/devices by major number
Quoted 4 lines · Logan Gunthorpe
Presently, the order of the char devices listed in
/proc/devicesis not entirely sequential. If a char device has a major number greater thanCHRDEV_MAJOR_HASH_SIZE(255), it will be ordered as if its major were module 255. For example, 511 appears after 1.
Hey, nice fix, that was annoying!
Also looks like block devices have the same issue in this file, care to fix that up too while you are there? :)
thanks,
greg k-h
Re: [PATCH 2/2] char_dev: order /proc/devices by major number
Quoted 2 lines · Greg Kroah-Hartman
Also looks like block devices have the same issue in this file, care to fix that up too while you are there? :)
Sure, I'll take a look at that and send another patch later.
Logan