Skip to contentSkip to conversation

kdev_t: mask mi with MINORMASK in MKDEV macro

PATCH In review Zhiqiang Liu 2 participants 3 messages Jan 10, 2020 – Jan 12, 2020 Block layer

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 kdev_t: mask mi with MINORMASK in MKDEV macro

In MKDEV macro, if mi is larger than MINORMASK, the major will be affected by mi. For example, set dev = MKDEV(2, (1U << MINORBITS)), then MAJOR(dev) will be equal to 3, incorrectly.

Here, we mask mi with MINORMASK in MKDEV macro.

---
 include/linux/kdev_t.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/kdev_t.h +1 -1
index 85b5151911cf..40a9423720b2 100644
@@ -9,7 +9,7 @@
 
 #define MAJOR(dev)	((unsigned int) ((dev) >> MINORBITS))
 #define MINOR(dev)	((unsigned int) ((dev) & MINORMASK))
-#define MKDEV(ma,mi)	(((ma) << MINORBITS) | (mi))
+#define MKDEV(ma, mi)	(((ma) << MINORBITS) | ((mi) & MINORMASK))
 
 #define print_dev_t(buffer, dev)					\
 	sprintf((buffer), "%u:%u\n", MAJOR(dev), MINOR(dev))
Signature · 1 line
2.19.1
#1 lore raw
Quoted 17 lines · Zhiqiang Liu

In MKDEV macro, if mi is larger than MINORMASK, the major will be affected by mi. For example, set dev = MKDEV(2, (1U << MINORBITS)), then MAJOR(dev) will be equal to 3, incorrectly.

Here, we mask mi with MINORMASK in MKDEV macro.

---
 include/linux/kdev_t.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/kdev_t.h +0 -0
index 85b5151911cf..40a9423720b2 100644

include/linux/kdev_t.h line 15

Quoted 8 lines
include/linux/kdev_t.h +1 -1
@@ -9,7 +9,7 @@
 
 #define MAJOR(dev)	((unsigned int) ((dev) >> MINORBITS))
 #define MINOR(dev)	((unsigned int) ((dev) & MINORMASK))
-#define MKDEV(ma,mi)	(((ma) << MINORBITS) | (mi))
+#define MKDEV(ma, mi)	(((ma) << MINORBITS) | ((mi) & MINORMASK))
 
 #define print_dev_t(buffer, dev)					\
 	sprintf((buffer), "%u:%u\n", MAJOR(dev), MINOR(dev))

Shouldn't the users of MKDEV() be fixed instead of changing the MKDEV() definition?

Thanks,

Bart.

#2 lore raw
Quoted 30 lines · Bart Van Assche
Quoted 17 lines · Zhiqiang Liu

In MKDEV macro, if mi is larger than MINORMASK, the major will be affected by mi. For example, set dev = MKDEV(2, (1U << MINORBITS)), then MAJOR(dev) will be equal to 3, incorrectly.

Here, we mask mi with MINORMASK in MKDEV macro.

---
 include/linux/kdev_t.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/kdev_t.h +0 -0
index 85b5151911cf..40a9423720b2 100644

include/linux/kdev_t.h line 15

Quoted 8 lines
include/linux/kdev_t.h +1 -1
@@ -9,7 +9,7 @@
 
 #define MAJOR(dev)	((unsigned int) ((dev) >> MINORBITS))
 #define MINOR(dev)	((unsigned int) ((dev) & MINORMASK))
-#define MKDEV(ma,mi)	(((ma) << MINORBITS) | (mi))
+#define MKDEV(ma, mi)	(((ma) << MINORBITS) | ((mi) & MINORMASK))
 
 #define print_dev_t(buffer, dev)					\
 	sprintf((buffer), "%u:%u\n", MAJOR(dev), MINOR(dev))

Shouldn't the users of MKDEV() be fixed instead of changing the MKDEV() definition?

Quoted 3 lines

Thanks,

Bart.

Thanks for your reply.
I think that your opinion is much better. Users of MKDEV() should make sure that the mi is not larger than MINORMASK. If we mask mi with MINORMASK in MKDEV(), ma will be not affected by mi. But, the result may be not the expected value of users.

So, please ignore the patch.

Quoted 3 lines