Skip to contentSkip to conversation

char_dev: replace cdev_map with an xarray

PATCH In review Christoph Hellwig 4 participants 11 messages Jan 11, 2021 – Jan 12, 2021

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 char_dev: replace cdev_map with an xarray

None of the complicated overlapping regions bits of the kobj_map are required for the character device lookup, so just a trivial xarray instead.

---
 drivers/base/Makefile    |   5 +-
 drivers/base/map.c       | 154 ---------------------------------------
 fs/char_dev.c            |  93 ++++++++++++-----------
 fs/dcache.c              |   1 -
 fs/internal.h            |   5 --
 include/linux/kobj_map.h |  20 -----
 6 files changed, 48 insertions(+), 230 deletions(-)
 delete mode 100644 drivers/base/map.c
 delete mode 100644 include/linux/kobj_map.h
drivers/base/Makefile +2 -3
index 5e7bf9669a81f8..ff0be4d875cfaf 100644
@@ -1,9 +1,8 @@
 # SPDX-License-Identifier: GPL-2.0
 # Makefile for the Linux device tree
 
-obj-y			:= component.o core.o bus.o dd.o syscore.o \
-			   driver.o class.o platform.o \
-			   cpu.o firmware.o init.o map.o devres.o \
+obj-y			:= component.o core.o bus.o dd.o syscore.o driver.o \
+			   class.o platform.o cpu.o firmware.o init.o devres.o \
 			   attribute_container.o transport_class.o \
 			   topology.o container.o property.o cacheinfo.o \
 			   swnode.o
drivers/base/map.c deleted +0 -154

154 lines not on this page · raw message

fs/char_dev.c +46 -47

161 lines not on this page · raw message

fs/dcache.c +0 -1
index 97e81a844a966c..3e0329b3de0a2d 100644
@@ -3240,5 +3240,4 @@ void __init vfs_caches_init(void)
 	files_maxfiles_init();
 	mnt_init();
 	bdev_cache_init();
-	chrdev_init();
 }
fs/internal.h +0 -5
index 77c50befbfbe96..a2985ee25c3b15 100644
@@ -50,11 +50,6 @@ static inline int emergency_thaw_bdev(struct super_block *sb)
 extern int __block_write_begin_int(struct page *page, loff_t pos, unsigned len,
 		get_block_t *get_block, struct iomap *iomap);
 
-/*
- * char_dev.c
- */
-extern void __init chrdev_init(void);
-
 /*
  * fs_context.c
  */
include/linux/kobj_map.h deleted +0 -20
deleted file mode 100644
index c9919f8b22932c..00000000000000
@@ -1,20 +0,0 @@
-/* SPDX-License-Identifier: GPL-2.0 */
-/*
- * kobj_map.h
- */
-
-#ifndef _KOBJ_MAP_H_
-#define _KOBJ_MAP_H_
-
-#include <linux/mutex.h>
-
-typedef struct kobject *kobj_probe_t(dev_t, int *, void *);
-struct kobj_map;
-
-int kobj_map(struct kobj_map *, dev_t, unsigned long, struct module *,
-	     kobj_probe_t *, int (*)(dev_t, void *), void *);
-void kobj_unmap(struct kobj_map *, dev_t, unsigned long);
-struct kobject *kobj_lookup(struct kobj_map *, dev_t, int *);
-struct kobj_map *kobj_map_init(kobj_probe_t *, struct mutex *);
-
-#endif /* _KOBJ_MAP_H_ */
Signature · 1 line
2.29.2
Reviewed-by on v1 1/1
#1 lore raw
Quoted 3 lines · Christoph Hellwig

None of the complicated overlapping regions bits of the kobj_map are required for the character device lookup, so just a trivial xarray instead.

Thanks for doing this. We could make it more efficient for chardevs that occupy 64 or more consecutive/aligned devices -- is it worth doing?

Quoted 10 lines
fs/char_dev.c +10 -0
+static struct cdev *cdev_lookup(dev_t dev)
+{
+	struct cdev *cdev;
+
+	mutex_lock(&chrdevs_lock);
+	cdev = xa_load(&cdev_map, dev);
+	if (!cdev) {
+		mutex_unlock(&chrdevs_lock);
+		if (request_module("char-major-%d-%d",
+				   MAJOR(dev), MINOR(dev)) > 0)

fs/char_dev.c void cdev_put(struct cdev *p) · line 387

Quoted 10 lines
fs/char_dev.c +10 -0
+			/* Make old-style 2.4 aliases work */
+			request_module("char-major-%d", MAJOR(dev));
+		mutex_lock(&chrdevs_lock);
+
+		cdev = xa_load(&cdev_map, dev);
+	}
+	if (cdev && !cdev_get(cdev))
+		cdev = NULL;
+	mutex_unlock(&chrdevs_lock);
+	return cdev;

What does the mutex protect here? Is it cdev being freed?

fs/char_dev.c static void cdev_unmap(dev_t dev, unsigned count) · line 609

Quoted 11 lines
fs/char_dev.c +6 -1
@@ -593,11 +601,16 @@ static void cdev_unmap(dev_t dev, unsigned count)
  */
 void cdev_del(struct cdev *p)
 {
-	cdev_unmap(p->dev, p->count);
+	int i;
+
+	mutex_lock(&chrdevs_lock);
+	for (i = 0; i < p->count; i++)
+		xa_erase(&cdev_map, p->dev + i);
+	mutex_unlock(&chrdevs_lock);

I don't understand what it's protecting here. It's clearly not cdev_get as that could happen before we acquire the mutex. This also suggests I should add an xa_erase_range() to the API.

But there's nothing wrong here, just some places that maybe could be better, so:

maintainer
#2 lore raw
Quoted 7 lines · Matthew Wilcox
Quoted 3 lines · Christoph Hellwig

None of the complicated overlapping regions bits of the kobj_map are required for the character device lookup, so just a trivial xarray instead.

Thanks for doing this. We could make it more efficient for chardevs that occupy 64 or more consecutive/aligned devices -- is it worth doing?

efficient in what way? Space or faster lookup?

THis shouldn't be on a "fast" lookup path, so I doubt that's worth optimizing for. Space, maybe, for systems with thousands of scsi devices, but usually they just stick to the block device, not a char device from what I remember.

thanks,

greg k-h

#3 lore raw
Quoted 10 lines · Greg KH
Quoted 7 lines · Matthew Wilcox
Quoted 3 lines · Christoph Hellwig

None of the complicated overlapping regions bits of the kobj_map are required for the character device lookup, so just a trivial xarray instead.

Thanks for doing this. We could make it more efficient for chardevs that occupy 64 or more consecutive/aligned devices -- is it worth doing?

efficient in what way? Space or faster lookup?

Both, but primarily space.

The radix tree underlying the xarray allows N consecutive entries with the same value to be represented as a single entry; if there are at least 64 entries then we get to skip an entire level of the tree (saving 1/7 of a page). Of course, we'd need to go from the 'head' pointer to the correct pointer, something like p += rdev - p->rdev.

Quoted 4 lines

THis shouldn't be on a "fast" lookup path, so I doubt that's worth optimizing for. Space, maybe, for systems with thousands of scsi devices, but usually they just stick to the block device, not a char device from what I remember.

/dev/sgX is a chardev?

maintainer
#4 lore raw
Quoted 12 lines · Matthew Wilcox
Quoted 10 lines · Greg KH
Quoted 7 lines · Matthew Wilcox
Quoted 3 lines · Christoph Hellwig

None of the complicated overlapping regions bits of the kobj_map are required for the character device lookup, so just a trivial xarray instead.

Thanks for doing this. We could make it more efficient for chardevs that occupy 64 or more consecutive/aligned devices -- is it worth doing?

efficient in what way? Space or faster lookup?

Quoted 7 lines

Both, but primarily space.

The radix tree underlying the xarray allows N consecutive entries with the same value to be represented as a single entry; if there are at least 64 entries then we get to skip an entire level of the tree (saving 1/7 of a page). Of course, we'd need to go from the 'head' pointer to the correct pointer, something like p += rdev - p->rdev.

How much "space" are you talking about here?

A "normal" machine has about 100-200 char devices. Servers, maybe more, but probably not.

The kobject being used previously wasn't really "small" at all, so odds are any conversion to not use it like this will be better overall.

Quoted 6 lines
Quoted 4 lines

THis shouldn't be on a "fast" lookup path, so I doubt that's worth optimizing for. Space, maybe, for systems with thousands of scsi devices, but usually they just stick to the block device, not a char device from what I remember.

/dev/sgX is a chardev?

I sure hope no one is using /dev/sgX for tens of thousands of block device accesses, if so, they have bigger problems than this :)

thanks,

greg k-h

#5 lore raw
Quoted 5 lines · Greg KH
Quoted 4 lines · Matthew Wilcox
Quoted 1 line

efficient in what way? Space or faster lookup?

Both, but primarily space.

Quoted 7 lines
Quoted 5 lines

The radix tree underlying the xarray allows N consecutive entries with the same value to be represented as a single entry; if there are at least 64 entries then we get to skip an entire level of the tree (saving 1/7 of a page). Of course, we'd need to go from the 'head' pointer to the correct pointer, something like p += rdev - p->rdev.

How much "space" are you talking about here?

576 bytes -- 1/7 of a page.

Quoted 5 lines

A "normal" machine has about 100-200 char devices. Servers, maybe more, but probably not.

The kobject being used previously wasn't really "small" at all, so odds are any conversion to not use it like this will be better overall.

Yes.

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

THis shouldn't be on a "fast" lookup path, so I doubt that's worth optimizing for. Space, maybe, for systems with thousands of scsi devices, but usually they just stick to the block device, not a char device from what I remember.

/dev/sgX is a chardev?

I sure hope no one is using /dev/sgX for tens of thousands of block device accesses, if so, they have bigger problems than this :)

There is one sgX char dev for every /dev/sdN, so anyone with a thousand SCSI devices also has a thousand char devices. On the other hand, they're added one at a time, so there is no chance to optimise here:

        cdev = cdev_alloc();
...
        error = cdev_add(cdev, MKDEV(SCSI_GENERIC_MAJOR, sdp->index), 1);
#7 lore raw
In reply to Christoph Hellwig
Quoted 5 lines · Christoph Hellwig
fs/char_dev.c +0 -1
@@ -486,14 +491,22 @@ int cdev_add(struct cdev *p, dev_t dev, unsigned count)
 	if (WARN_ON(dev == WHITEOUT_DEV))
 		return -EBUSY;
 
-	error = kobj_map(cdev_map, dev, count, NULL,

fs/char_dev.c int cdev_add(struct cdev *p, dev_t dev, unsigned count) · line 500

Quoted 10 lines
fs/char_dev.c +7 -3
-			 exact_match, exact_lock, p);
-	if (error)
-		return error;
+	mutex_lock(&chrdevs_lock);
+	for (i = 0; i < count; i++) {
+		error = xa_insert(&cdev_map, dev + i, p, GFP_KERNEL);
+		if (error)
+			goto out_unwind;
+	}
+	mutex_unlock(&chrdevs_lock);

Looking at some of the users ...

#define BSG_MAX_DEVS 32768
...

        ret = cdev_add(&bsg_cdev, MKDEV(bsg_major, 0), BSG_MAX_DEVS);

So this is going to allocate 32768 entries; at 8 bytes each, that's 256kB. With XArray overhead, it works out to 73 pages or 292kB. While I don't have bsg loaded on my laptop, I imagine a lot of machines do.

drivers/net/tap.c:#define TAP_NUM_DEVS (1U << MINORBITS)
include/linux/kdev_t.h:#define MINORBITS 20
drivers/net/tap.c: err = cdev_add(tap_cdev, *tap_major, TAP_NUM_DEVS);

That's going to be even worse -- 8MB plus the overhead to be closer to 9MB.

I think we do need to implement the 'store a range' option ;-(

maintainer
#8 lore raw
Quoted 27 lines · Matthew Wilcox
Quoted 5 lines · Christoph Hellwig
fs/char_dev.c +0 -1
@@ -486,14 +491,22 @@ int cdev_add(struct cdev *p, dev_t dev, unsigned count)
 	if (WARN_ON(dev == WHITEOUT_DEV))
 		return -EBUSY;
 
-	error = kobj_map(cdev_map, dev, count, NULL,

fs/char_dev.c int cdev_add(struct cdev *p, dev_t dev, unsigned count) · line 500

Quoted 10 lines
fs/char_dev.c +7 -3
-			 exact_match, exact_lock, p);
-	if (error)
-		return error;
+	mutex_lock(&chrdevs_lock);
+	for (i = 0; i < count; i++) {
+		error = xa_insert(&cdev_map, dev + i, p, GFP_KERNEL);
+		if (error)
+			goto out_unwind;
+	}
+	mutex_unlock(&chrdevs_lock);

Looking at some of the users ...

#define BSG_MAX_DEVS 32768
...

        ret = cdev_add(&bsg_cdev, MKDEV(bsg_major, 0), BSG_MAX_DEVS);

So this is going to allocate 32768 entries; at 8 bytes each, that's 256kB. With XArray overhead, it works out to 73 pages or 292kB. While I don't have bsg loaded on my laptop, I imagine a lot of machines do.

Quoted 7 lines

drivers/net/tap.c:#define TAP_NUM_DEVS (1U << MINORBITS)
include/linux/kdev_t.h:#define MINORBITS 20
drivers/net/tap.c: err = cdev_add(tap_cdev, *tap_major, TAP_NUM_DEVS);

That's going to be even worse -- 8MB plus the overhead to be closer to 9MB.

I think we do need to implement the 'store a range' option ;-(

Looks like it will be needed here.

Wow that's a lot of tap devices allocated all at once, odd...

Note, we will get some additional savings when this goes in as I can delete the kobject from the cdev (after cleaning up the misguided drivers that tried to set it thinking it was used), which will help out a lot for the individual structures being created, but not for these ranges.

thanks,

greg k-h

#9 lore raw

From: Greg KH

Quoted 34 lines

Sent: 12 January 2021 09:35

Quoted 31 lines · Matthew Wilcox
Quoted 5 lines · Christoph Hellwig
fs/char_dev.c +0 -1
@@ -486,14 +491,22 @@ int cdev_add(struct cdev *p, dev_t dev, unsigned count)
 	if (WARN_ON(dev == WHITEOUT_DEV))
 		return -EBUSY;
 
-	error = kobj_map(cdev_map, dev, count, NULL,

fs/char_dev.c int cdev_add(struct cdev *p, dev_t dev, unsigned count) · line 500

Quoted 10 lines
fs/char_dev.c +7 -3
-			 exact_match, exact_lock, p);
-	if (error)
-		return error;
+	mutex_lock(&chrdevs_lock);
+	for (i = 0; i < count; i++) {
+		error = xa_insert(&cdev_map, dev + i, p, GFP_KERNEL);
+		if (error)
+			goto out_unwind;
+	}
+	mutex_unlock(&chrdevs_lock);

Looking at some of the users ...

#define BSG_MAX_DEVS 32768
...

        ret = cdev_add(&bsg_cdev, MKDEV(bsg_major, 0), BSG_MAX_DEVS);

So this is going to allocate 32768 entries; at 8 bytes each, that's 256kB. With XArray overhead, it works out to 73 pages or 292kB. While I don't have bsg loaded on my laptop, I imagine a lot of machines do.

drivers/net/tap.c:#define TAP_NUM_DEVS (1U << MINORBITS)
include/linux/kdev_t.h:#define MINORBITS 20
drivers/net/tap.c: err = cdev_add(tap_cdev, *tap_major, TAP_NUM_DEVS);

Quoted 7 lines
Quoted 3 lines

That's going to be even worse -- 8MB plus the overhead to be closer to 9MB.

I think we do need to implement the 'store a range' option ;-(

Looks like it will be needed here.

Wow that's a lot of tap devices allocated all at once, odd...

Aren't there two distinct use cases that probably need treating separately?

1) A driver that uses it's own major (private) major number.
2) Drivers that share a major number (eg serial ports).

In the first case you just want all minors to come to your driver. In the second case different (ranges of) minors need to go to different drivers.

Until the major number is actually shared only the upper limit is an any way relevant.

David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK Registration No: 1397386 (Wales)

maintainer
#10 lore raw
Quoted 49 lines · David Laight

From: Greg KH

Quoted 34 lines

Sent: 12 January 2021 09:35

Quoted 31 lines · Matthew Wilcox
Quoted 5 lines · Christoph Hellwig
fs/char_dev.c +0 -1
@@ -486,14 +491,22 @@ int cdev_add(struct cdev *p, dev_t dev, unsigned count)
 	if (WARN_ON(dev == WHITEOUT_DEV))
 		return -EBUSY;
 
-	error = kobj_map(cdev_map, dev, count, NULL,

fs/char_dev.c int cdev_add(struct cdev *p, dev_t dev, unsigned count) · line 500

Quoted 10 lines
fs/char_dev.c +7 -3
-			 exact_match, exact_lock, p);
-	if (error)
-		return error;
+	mutex_lock(&chrdevs_lock);
+	for (i = 0; i < count; i++) {
+		error = xa_insert(&cdev_map, dev + i, p, GFP_KERNEL);
+		if (error)
+			goto out_unwind;
+	}
+	mutex_unlock(&chrdevs_lock);

Looking at some of the users ...

#define BSG_MAX_DEVS 32768
...

        ret = cdev_add(&bsg_cdev, MKDEV(bsg_major, 0), BSG_MAX_DEVS);

So this is going to allocate 32768 entries; at 8 bytes each, that's 256kB. With XArray overhead, it works out to 73 pages or 292kB. While I don't have bsg loaded on my laptop, I imagine a lot of machines do.

drivers/net/tap.c:#define TAP_NUM_DEVS (1U << MINORBITS)
include/linux/kdev_t.h:#define MINORBITS 20
drivers/net/tap.c: err = cdev_add(tap_cdev, *tap_major, TAP_NUM_DEVS);

Quoted 7 lines
Quoted 3 lines

That's going to be even worse -- 8MB plus the overhead to be closer to 9MB.

I think we do need to implement the 'store a range' option ;-(

Looks like it will be needed here.

Wow that's a lot of tap devices allocated all at once, odd...

Aren't there two distinct use cases that probably need treating separately?

1) A driver that uses it's own major (private) major number.
2) Drivers that share a major number (eg serial ports).

Quoted 6 lines

In the first case you just want all minors to come to your driver. In the second case different (ranges of) minors need to go to different drivers.

Until the major number is actually shared only the upper limit is an any way relevant.

Patches gladly reviewed.

thanks,

greg k-h