Skip to content

Commit

Permalink
char_dev: order /proc/devices by major number
Browse files Browse the repository at this point in the history
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.

Signed-off-by: Logan Gunthorpe <[email protected]>
Cc: Linus Torvalds <[email protected]>
Cc: Greg Kroah-Hartman <[email protected]>
Cc: Alan Cox <[email protected]>
Cc: Arnd Bergmann <[email protected]>
Cc: Linus Walleij <[email protected]>
Signed-off-by: Greg Kroah-Hartman <[email protected]>
  • Loading branch information
lsgunth authored and gregkh committed Jul 17, 2017
1 parent a5d31a3 commit 8a932f7
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
17 changes: 13 additions & 4 deletions fs/char_dev.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 */
Expand Down Expand Up @@ -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;
Expand Down
8 changes: 4 additions & 4 deletions fs/proc/devices.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -25,15 +25,15 @@ 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;
}

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;
}
Expand Down
2 changes: 1 addition & 1 deletion include/linux/fs.h
Original file line number Diff line number Diff line change
Expand Up @@ -2470,7 +2470,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 */
Expand Down

0 comments on commit 8a932f7

Please sign in to comment.