Re: [PATCH v2] fs: O_* bit numbers uniqueness check

From: Wu Fengguang
Date: Wed Jan 06 2010 - 02:42:22 EST


On Wed, Jan 06, 2010 at 03:30:31PM +0800, Roland Dreier wrote:
>
> > Yeah, I chose the boot time check because of hweight32()..
>
> One could do something like
>
> #define HWEIGHT32(x) (!!((x) & (1u << 0)) +
> !!((x) & (1u << 1)) +
> //...
> !!((x) & (1u << 31)));
>
> that would probably work with BUILD_BUG_ON().
>
> But as I said maybe it's too ugly.

Yes, it'll feel like this. Then you'll have to fix the possible error
in order to compile ;)

---
fs: O_* bit numbers uniqueness check

The O_* bit numbers are defined in 20+ arch/*, and hence can silently
overlap. Add a boot time check to ensure the uniqueness as suggested
by David Miller.

v3: change to BUILD_BUG_ON() as suggested by Roland

CC: David Miller <davem@xxxxxxxxxxxxx>
CC: Stephen Rothwell <sfr@xxxxxxxxxxxxxxxx>
CC: Al Viro <viro@xxxxxxxxxxxxxxxxxx>
CC: Christoph Hellwig <hch@xxxxxxxxxxxxx>
CC: Eric Paris <eparis@xxxxxxxxxx>
CC: Roland Dreier <rdreier@xxxxxxxxx>
Signed-off-by: Wu Fengguang <fengguang.wu@xxxxxxxxx>
---
fs/fcntl.c | 14 ++++++++++++--
include/asm-generic/fcntl.h | 2 ++
include/linux/bitops.h | 33 +++++++++++++++++++++++++++++++++
3 files changed, 47 insertions(+), 2 deletions(-)

--- linux.orig/fs/fcntl.c 2010-01-06 14:41:26.000000000 +0800
+++ linux/fs/fcntl.c 2010-01-06 15:37:14.000000000 +0800
@@ -709,11 +709,21 @@ void kill_fasync(struct fasync_struct **
}
EXPORT_SYMBOL(kill_fasync);

-static int __init fasync_init(void)
+static int __init fcntl_init(void)
{
+ /* please add new bits here to ensure allocation uniqueness */
+ BUILD_BUG_ON(20 != HWEIGHT32(
+ O_RDONLY | O_WRONLY | O_RDWR |
+ O_CREAT | O_EXCL | O_NOCTTY |
+ O_TRUNC | O_APPEND | O_NONBLOCK |
+ O_SYNC | FASYNC | O_DIRECT |
+ O_LARGEFILE | O_DIRECTORY | O_NOFOLLOW |
+ O_NOATIME | O_CLOEXEC | O_RANDOM |
+ FMODE_EXEC | FMODE_NONOTIFY));
+
fasync_cache = kmem_cache_create("fasync_cache",
sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
return 0;
}

-module_init(fasync_init)
+module_init(fcntl_init)
--- linux.orig/include/asm-generic/fcntl.h 2010-01-06 14:45:34.000000000 +0800
+++ linux/include/asm-generic/fcntl.h 2010-01-06 14:50:57.000000000 +0800
@@ -4,6 +4,8 @@
#include <linux/types.h>

/*
+ * When introducing new O_* bits, please check its uniqueness in fcntl_init().
+ *
* FMODE_EXEC is 0x20
* FMODE_NONOTIFY is 0x1000000
* These cannot be used by userspace O_* until internal and external open
--- linux.orig/include/linux/bitops.h 2010-01-06 15:33:39.000000000 +0800
+++ linux/include/linux/bitops.h 2010-01-06 15:38:57.000000000 +0800
@@ -8,6 +8,39 @@
#define BIT_WORD(nr) ((nr) / BITS_PER_LONG)
#define BITS_PER_BYTE 8
#define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_BYTE * sizeof(long))
+
+#define HWEIGHT32(x) (!!((x) & (1u << 0)) + \
+ !!((x) & (1u << 1)) + \
+ !!((x) & (1u << 2)) + \
+ !!((x) & (1u << 3)) + \
+ !!((x) & (1u << 4)) + \
+ !!((x) & (1u << 5)) + \
+ !!((x) & (1u << 6)) + \
+ !!((x) & (1u << 7)) + \
+ !!((x) & (1u << 8)) + \
+ !!((x) & (1u << 9)) + \
+ !!((x) & (1u << 10)) + \
+ !!((x) & (1u << 11)) + \
+ !!((x) & (1u << 12)) + \
+ !!((x) & (1u << 13)) + \
+ !!((x) & (1u << 14)) + \
+ !!((x) & (1u << 15)) + \
+ !!((x) & (1u << 16)) + \
+ !!((x) & (1u << 17)) + \
+ !!((x) & (1u << 18)) + \
+ !!((x) & (1u << 19)) + \
+ !!((x) & (1u << 20)) + \
+ !!((x) & (1u << 21)) + \
+ !!((x) & (1u << 22)) + \
+ !!((x) & (1u << 23)) + \
+ !!((x) & (1u << 24)) + \
+ !!((x) & (1u << 25)) + \
+ !!((x) & (1u << 26)) + \
+ !!((x) & (1u << 27)) + \
+ !!((x) & (1u << 28)) + \
+ !!((x) & (1u << 29)) + \
+ !!((x) & (1u << 30)) + \
+ !!((x) & (1u << 31)))
#endif

/*
--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/