All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] fs: O_* bit numbers uniqueness check
@ 2010-01-06  6:55 Wu Fengguang
  2010-01-06  7:07   ` Eric Dumazet
                   ` (2 more replies)
  0 siblings, 3 replies; 15+ messages in thread
From: Wu Fengguang @ 2010-01-06  6:55 UTC (permalink / raw)
  To: Andrew Morton
  Cc: David Miller, Stephen Rothwell, Al Viro, Christoph Hellwig,
	Eric Paris, LKML, linux-fsdevel

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.

CC: David Miller <davem@davemloft.net>
CC: Stephen Rothwell <sfr@canb.auug.org.au>
CC: Al Viro <viro@zeniv.linux.org.uk>
CC: Christoph Hellwig <hch@infradead.org>
CC: Eric Paris <eparis@redhat.com>
Signed-off-by: Wu Fengguang <fengguang.wu@intel.com>
---
 fs/fcntl.c                  |   14 ++++++++++++--
 include/asm-generic/fcntl.h |    2 ++
 2 files changed, 14 insertions(+), 2 deletions(-)

--- linux.orig/fs/fcntl.c	2010-01-06 14:41:26.000000000 +0800
+++ linux/fs/fcntl.c	2010-01-06 14:46:57.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 */
+	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

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH] fs: O_* bit numbers uniqueness check
  2010-01-06  6:55 [PATCH] fs: O_* bit numbers uniqueness check Wu Fengguang
@ 2010-01-06  7:07   ` Eric Dumazet
  2010-01-06  7:08 ` Roland Dreier
  2010-01-06 15:48 ` Christoph Hellwig
  2 siblings, 0 replies; 15+ messages in thread
From: Eric Dumazet @ 2010-01-06  7:07 UTC (permalink / raw)
  To: Wu Fengguang
  Cc: Andrew Morton, David Miller, Stephen Rothwell, Al Viro,
	Christoph Hellwig, Eric Paris, LKML, linux-fsdevel

Le 06/01/2010 07:55, Wu Fengguang a écrit :
> 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.
> 
> CC: David Miller <davem@davemloft.net>
> CC: Stephen Rothwell <sfr@canb.auug.org.au>
> CC: Al Viro <viro@zeniv.linux.org.uk>
> CC: Christoph Hellwig <hch@infradead.org>
> CC: Eric Paris <eparis@redhat.com>
> Signed-off-by: Wu Fengguang <fengguang.wu@intel.com>
> ---
>  {
> +	/* please add new bits here to ensure allocation uniqueness */
> +	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));
> +

I cannot test it, but given O_RDONLY is 0, are you sure 20 bits are actually set ?

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH] fs: O_* bit numbers uniqueness check
@ 2010-01-06  7:07   ` Eric Dumazet
  0 siblings, 0 replies; 15+ messages in thread
From: Eric Dumazet @ 2010-01-06  7:07 UTC (permalink / raw)
  To: Wu Fengguang
  Cc: Andrew Morton, David Miller, Stephen Rothwell, Al Viro,
	Christoph Hellwig, Eric Paris, LKML, linux-fsdevel

Le 06/01/2010 07:55, Wu Fengguang a écrit :
> 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.
> 
> CC: David Miller <davem@davemloft.net>
> CC: Stephen Rothwell <sfr@canb.auug.org.au>
> CC: Al Viro <viro@zeniv.linux.org.uk>
> CC: Christoph Hellwig <hch@infradead.org>
> CC: Eric Paris <eparis@redhat.com>
> Signed-off-by: Wu Fengguang <fengguang.wu@intel.com>
> ---
>  {
> +	/* please add new bits here to ensure allocation uniqueness */
> +	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));
> +

I cannot test it, but given O_RDONLY is 0, are you sure 20 bits are actually set ?
--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH] fs: O_* bit numbers uniqueness check
  2010-01-06  6:55 [PATCH] fs: O_* bit numbers uniqueness check Wu Fengguang
  2010-01-06  7:07   ` Eric Dumazet
@ 2010-01-06  7:08 ` Roland Dreier
  2010-01-06  7:18   ` [PATCH v2] " Wu Fengguang
  2010-01-06 16:20   ` [PATCH] " Jamie Lokier
  2010-01-06 15:48 ` Christoph Hellwig
  2 siblings, 2 replies; 15+ messages in thread
From: Roland Dreier @ 2010-01-06  7:08 UTC (permalink / raw)
  To: Wu Fengguang
  Cc: Andrew Morton, David Miller, Stephen Rothwell, Al Viro,
	Christoph Hellwig, Eric Paris, LKML, linux-fsdevel


 > +	/* please add new bits here to ensure allocation uniqueness */
 > +	BUG_ON(20 != hweight32(
 > +		O_RDONLY	| O_WRONLY	| O_RDWR	|

I wonder if there's a way to make this BUILD_BUG_ON(), so the problem is
caught at compile time (a change like adding an O_ flag is often compile
tested only for obscure archs).  One could create a compile-time
macro-ized version of hweight32() and use that, I guess, although I'm
not sure it's worth the ugliness.

Failing that maybe this should be WARN_ON()?  I'd be annoyed if my arch
wouldn't boot because some strange new O_ flag happened to collide.

 - R.

^ permalink raw reply	[flat|nested] 15+ messages in thread

* [PATCH v2] fs: O_* bit numbers uniqueness check
  2010-01-06  7:08 ` Roland Dreier
@ 2010-01-06  7:18   ` Wu Fengguang
  2010-01-06  7:30     ` Roland Dreier
  2010-01-06 16:20   ` [PATCH] " Jamie Lokier
  1 sibling, 1 reply; 15+ messages in thread
From: Wu Fengguang @ 2010-01-06  7:18 UTC (permalink / raw)
  To: Roland Dreier
  Cc: Andrew Morton, David Miller, Stephen Rothwell, Al Viro,
	Christoph Hellwig, Eric Paris, LKML, linux-fsdevel

On Wed, Jan 06, 2010 at 03:08:08PM +0800, Roland Dreier wrote:
> 
>  > +	/* please add new bits here to ensure allocation uniqueness */
>  > +	BUG_ON(20 != hweight32(
>  > +		O_RDONLY	| O_WRONLY	| O_RDWR	|
> 
> I wonder if there's a way to make this BUILD_BUG_ON(), so the problem is
> caught at compile time (a change like adding an O_ flag is often compile
> tested only for obscure archs).  One could create a compile-time
> macro-ized version of hweight32() and use that, I guess, although I'm
> not sure it's worth the ugliness.

Yeah, I chose the boot time check because of hweight32()..

> Failing that maybe this should be WARN_ON()?  I'd be annoyed if my arch
> wouldn't boot because some strange new O_ flag happened to collide.

Good suggestion! Here is the updated patch.

Andrew, this patch relies on the newly introduced O_RANDOM and Eric's
FMODE_NONOTIFY. 

Thanks,
Fengguang
---
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.

v2: change to WARN_ON() as suggested by Roland

CC: David Miller <davem@davemloft.net>
CC: Stephen Rothwell <sfr@canb.auug.org.au>
CC: Al Viro <viro@zeniv.linux.org.uk>
CC: Christoph Hellwig <hch@infradead.org>
CC: Eric Paris <eparis@redhat.com>
CC: Roland Dreier <rdreier@cisco.com>
Signed-off-by: Wu Fengguang <fengguang.wu@intel.com>
---
 fs/fcntl.c                  |   14 ++++++++++++--
 include/asm-generic/fcntl.h |    2 ++
 2 files changed, 14 insertions(+), 2 deletions(-)

--- linux.orig/fs/fcntl.c	2010-01-06 14:41:26.000000000 +0800
+++ linux/fs/fcntl.c	2010-01-06 15:15:11.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 */
+	WARN_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

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH] fs: O_* bit numbers uniqueness check
  2010-01-06  7:07   ` Eric Dumazet
  (?)
@ 2010-01-06  7:20   ` Wu Fengguang
  2010-01-06  9:13     ` Andreas Schwab
  -1 siblings, 1 reply; 15+ messages in thread
From: Wu Fengguang @ 2010-01-06  7:20 UTC (permalink / raw)
  To: Eric Dumazet
  Cc: Andrew Morton, David Miller, Stephen Rothwell, Al Viro,
	Christoph Hellwig, Eric Paris, LKML, linux-fsdevel

On Wed, Jan 06, 2010 at 03:07:01PM +0800, Eric Dumazet wrote:
> Le 06/01/2010 07:55, Wu Fengguang a écrit :
> > 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.
> > 
> > CC: David Miller <davem@davemloft.net>
> > CC: Stephen Rothwell <sfr@canb.auug.org.au>
> > CC: Al Viro <viro@zeniv.linux.org.uk>
> > CC: Christoph Hellwig <hch@infradead.org>
> > CC: Eric Paris <eparis@redhat.com>
> > Signed-off-by: Wu Fengguang <fengguang.wu@intel.com>
> > ---
> >  {
> > +	/* please add new bits here to ensure allocation uniqueness */
> > +	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));
> > +
> 
> I cannot test it, but given O_RDONLY is 0, are you sure 20 bits are actually set ?

Yes, I tested it. The tricky one is O_SYNC, which actually has two bits..

Thanks,
Fengguang


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2] fs: O_* bit numbers uniqueness check
  2010-01-06  7:18   ` [PATCH v2] " Wu Fengguang
@ 2010-01-06  7:30     ` Roland Dreier
  2010-01-06  7:42       ` Wu Fengguang
  0 siblings, 1 reply; 15+ messages in thread
From: Roland Dreier @ 2010-01-06  7:30 UTC (permalink / raw)
  To: Wu Fengguang
  Cc: Andrew Morton, David Miller, Stephen Rothwell, Al Viro,
	Christoph Hellwig, Eric Paris, LKML, linux-fsdevel


 > 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.

 - R.

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH v2] fs: O_* bit numbers uniqueness check
  2010-01-06  7:30     ` Roland Dreier
@ 2010-01-06  7:42       ` Wu Fengguang
  0 siblings, 0 replies; 15+ messages in thread
From: Wu Fengguang @ 2010-01-06  7:42 UTC (permalink / raw)
  To: Roland Dreier
  Cc: Andrew Morton, David Miller, Stephen Rothwell, Al Viro,
	Christoph Hellwig, Eric Paris, LKML, linux-fsdevel

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@davemloft.net>
CC: Stephen Rothwell <sfr@canb.auug.org.au>
CC: Al Viro <viro@zeniv.linux.org.uk>
CC: Christoph Hellwig <hch@infradead.org>
CC: Eric Paris <eparis@redhat.com>
CC: Roland Dreier <rdreier@cisco.com>
Signed-off-by: Wu Fengguang <fengguang.wu@intel.com>
---
 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
 
 /*

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH] fs: O_* bit numbers uniqueness check
  2010-01-06  7:20   ` Wu Fengguang
@ 2010-01-06  9:13     ` Andreas Schwab
  2010-01-09 13:33         ` Wu Fengguang
  0 siblings, 1 reply; 15+ messages in thread
From: Andreas Schwab @ 2010-01-06  9:13 UTC (permalink / raw)
  To: Wu Fengguang
  Cc: Eric Dumazet, Andrew Morton, David Miller, Stephen Rothwell,
	Al Viro, Christoph Hellwig, Eric Paris, LKML, linux-fsdevel

Wu Fengguang <fengguang.wu@intel.com> writes:

> On Wed, Jan 06, 2010 at 03:07:01PM +0800, Eric Dumazet wrote:
>> Le 06/01/2010 07:55, Wu Fengguang a écrit :
>> > 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.
>> > 
>> > CC: David Miller <davem@davemloft.net>
>> > CC: Stephen Rothwell <sfr@canb.auug.org.au>
>> > CC: Al Viro <viro@zeniv.linux.org.uk>
>> > CC: Christoph Hellwig <hch@infradead.org>
>> > CC: Eric Paris <eparis@redhat.com>
>> > Signed-off-by: Wu Fengguang <fengguang.wu@intel.com>
>> > ---
>> >  {
>> > +	/* please add new bits here to ensure allocation uniqueness */
>> > +	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));
>> > +
>> 
>> I cannot test it, but given O_RDONLY is 0, are you sure 20 bits are actually set ?
>
> Yes, I tested it. The tricky one is O_SYNC, which actually has two bits..

What if a new architecture wants to use a single bit value (since it
does not need backwards compatibility)?

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH] fs: O_* bit numbers uniqueness check
  2010-01-06  6:55 [PATCH] fs: O_* bit numbers uniqueness check Wu Fengguang
  2010-01-06  7:07   ` Eric Dumazet
  2010-01-06  7:08 ` Roland Dreier
@ 2010-01-06 15:48 ` Christoph Hellwig
  2010-01-09 13:52   ` Wu Fengguang
  2 siblings, 1 reply; 15+ messages in thread
From: Christoph Hellwig @ 2010-01-06 15:48 UTC (permalink / raw)
  To: Wu Fengguang
  Cc: Andrew Morton, David Miller, Stephen Rothwell, Al Viro,
	Christoph Hellwig, Eric Paris, LKML, linux-fsdevel

On Wed, Jan 06, 2010 at 02:55:26PM +0800, Wu Fengguang wrote:
> 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.

The right fix is to stop overlyaing the FMODE_ flags into the O_
namespace.  Al has been working towards this.


^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH] fs: O_* bit numbers uniqueness check
  2010-01-06  7:08 ` Roland Dreier
  2010-01-06  7:18   ` [PATCH v2] " Wu Fengguang
@ 2010-01-06 16:20   ` Jamie Lokier
  2010-01-09 13:44     ` Wu Fengguang
  1 sibling, 1 reply; 15+ messages in thread
From: Jamie Lokier @ 2010-01-06 16:20 UTC (permalink / raw)
  To: Roland Dreier
  Cc: Wu Fengguang, Andrew Morton, David Miller, Stephen Rothwell,
	Al Viro, Christoph Hellwig, Eric Paris, LKML, linux-fsdevel

Roland Dreier wrote:
> 
>  > +	/* please add new bits here to ensure allocation uniqueness */
>  > +	BUG_ON(20 != hweight32(
>  > +		O_RDONLY	| O_WRONLY	| O_RDWR	|
> 
> I wonder if there's a way to make this BUILD_BUG_ON(), so the problem is
> caught at compile time (a change like adding an O_ flag is often compile
> tested only for obscure archs).  One could create a compile-time
> macro-ized version of hweight32() and use that, I guess, although I'm
> not sure it's worth the ugliness.

Not ugly at all:

#define hweight32(x) __builtin_popcount(x)

Checked GCC 3.4.3 / 4.1 / 4.4: It expands as a compile-time constant
if the argument is a compile-time constant, so can be used in
BUILD_BUG_ON() and even for array sizes etc.

If GCC's __builtin_popcount() isn't good enough for non-constant
values, the macro would be more involved:

#define hweight32(x) (__builtin_constant_p(x) ? __builtin_popcount(x) \
                      : hweight32_nonconstant(x))

-- Jamie

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH] fs: O_* bit numbers uniqueness check
  2010-01-06  9:13     ` Andreas Schwab
@ 2010-01-09 13:33         ` Wu Fengguang
  0 siblings, 0 replies; 15+ messages in thread
From: Wu Fengguang @ 2010-01-09 13:33 UTC (permalink / raw)
  To: Andreas Schwab
  Cc: Eric Dumazet, Andrew Morton, David Miller, Stephen Rothwell,
	Al Viro, Christoph Hellwig, Eric Paris, LKML, linux-fsdevel

On Wed, Jan 06, 2010 at 05:13:54PM +0800, Andreas Schwab wrote:
> Wu Fengguang <fengguang.wu@intel.com> writes:
> 
> > On Wed, Jan 06, 2010 at 03:07:01PM +0800, Eric Dumazet wrote:
> >> Le 06/01/2010 07:55, Wu Fengguang a écrit :
> >> > 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.
> >> > 
> >> > CC: David Miller <davem@davemloft.net>
> >> > CC: Stephen Rothwell <sfr@canb.auug.org.au>
> >> > CC: Al Viro <viro@zeniv.linux.org.uk>
> >> > CC: Christoph Hellwig <hch@infradead.org>
> >> > CC: Eric Paris <eparis@redhat.com>
> >> > Signed-off-by: Wu Fengguang <fengguang.wu@intel.com>
> >> > ---
> >> >  {
> >> > +	/* please add new bits here to ensure allocation uniqueness */
> >> > +	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));
> >> > +
> >> 
> >> I cannot test it, but given O_RDONLY is 0, are you sure 20 bits are actually set ?
> >
> > Yes, I tested it. The tricky one is O_SYNC, which actually has two bits..
> 
> What if a new architecture wants to use a single bit value (since it
> does not need backwards compatibility)?

You mean to test __O_SYNC | O_DSYNC instead of O_SYNC?

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH] fs: O_* bit numbers uniqueness check
@ 2010-01-09 13:33         ` Wu Fengguang
  0 siblings, 0 replies; 15+ messages in thread
From: Wu Fengguang @ 2010-01-09 13:33 UTC (permalink / raw)
  To: Andreas Schwab
  Cc: Eric Dumazet, Andrew Morton, David Miller, Stephen Rothwell,
	Al Viro, Christoph Hellwig, Eric Paris, LKML, linux-fsdevel

On Wed, Jan 06, 2010 at 05:13:54PM +0800, Andreas Schwab wrote:
> Wu Fengguang <fengguang.wu@intel.com> writes:
> 
> > On Wed, Jan 06, 2010 at 03:07:01PM +0800, Eric Dumazet wrote:
> >> Le 06/01/2010 07:55, Wu Fengguang a écrit :
> >> > 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.
> >> > 
> >> > CC: David Miller <davem@davemloft.net>
> >> > CC: Stephen Rothwell <sfr@canb.auug.org.au>
> >> > CC: Al Viro <viro@zeniv.linux.org.uk>
> >> > CC: Christoph Hellwig <hch@infradead.org>
> >> > CC: Eric Paris <eparis@redhat.com>
> >> > Signed-off-by: Wu Fengguang <fengguang.wu@intel.com>
> >> > ---
> >> >  {
> >> > +	/* please add new bits here to ensure allocation uniqueness */
> >> > +	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));
> >> > +
> >> 
> >> I cannot test it, but given O_RDONLY is 0, are you sure 20 bits are actually set ?
> >
> > Yes, I tested it. The tricky one is O_SYNC, which actually has two bits..
> 
> What if a new architecture wants to use a single bit value (since it
> does not need backwards compatibility)?

You mean to test __O_SYNC | O_DSYNC instead of O_SYNC?
--
To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH] fs: O_* bit numbers uniqueness check
  2010-01-06 16:20   ` [PATCH] " Jamie Lokier
@ 2010-01-09 13:44     ` Wu Fengguang
  0 siblings, 0 replies; 15+ messages in thread
From: Wu Fengguang @ 2010-01-09 13:44 UTC (permalink / raw)
  To: Jamie Lokier
  Cc: Roland Dreier, Andrew Morton, David Miller, Stephen Rothwell,
	Al Viro, Christoph Hellwig, Eric Paris, LKML, linux-fsdevel

On Thu, Jan 07, 2010 at 12:20:04AM +0800, Jamie Lokier wrote:
> Roland Dreier wrote:
> > 
> >  > +	/* please add new bits here to ensure allocation uniqueness */
> >  > +	BUG_ON(20 != hweight32(
> >  > +		O_RDONLY	| O_WRONLY	| O_RDWR	|
> > 
> > I wonder if there's a way to make this BUILD_BUG_ON(), so the problem is
> > caught at compile time (a change like adding an O_ flag is often compile
> > tested only for obscure archs).  One could create a compile-time
> > macro-ized version of hweight32() and use that, I guess, although I'm
> > not sure it's worth the ugliness.
> 
> Not ugly at all:
> 
> #define hweight32(x) __builtin_popcount(x)
> 
> Checked GCC 3.4.3 / 4.1 / 4.4: It expands as a compile-time constant
> if the argument is a compile-time constant, so can be used in
> BUILD_BUG_ON() and even for array sizes etc.
> 
> If GCC's __builtin_popcount() isn't good enough for non-constant
> values, the macro would be more involved:
> 
> #define hweight32(x) (__builtin_constant_p(x) ? __builtin_popcount(x) \
>                       : hweight32_nonconstant(x))

Jamie, Thanks for the hint!  Would you sign this patch?

---
bitops: compile time optimization for hweight_long(CONSTANT)

This allows use of hweight_long() in BUILD_BUG_ON().
Suggested by Jamie.

CC: Jamie Lokier <jamie@shareable.org>
CC: Roland Dreier <rdreier@cisco.com>
Signed-off-by: Wu Fengguang <fengguang.wu@intel.com>
---
 include/linux/bitops.h |   12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

--- linux.orig/include/linux/bitops.h	2010-01-09 12:13:00.000000000 +0800
+++ linux/include/linux/bitops.h	2010-01-09 12:21:50.000000000 +0800
@@ -40,10 +40,14 @@ static __inline__ int get_count_order(un
 	return order;
 }
 
-static inline unsigned long hweight_long(unsigned long w)
-{
-	return sizeof(w) == 4 ? hweight32(w) : hweight64(w);
-}
+#define hweight_long(x)				\
+(						\
+	__builtin_constant_p(x) ?		\
+		__builtin_popcountl(x) :	\
+		(sizeof(x) <= 4 ?		\
+			 hweight32(x) :		\
+			 hweight64(x))		\
+)
 
 /**
  * rol32 - rotate a 32-bit value left

^ permalink raw reply	[flat|nested] 15+ messages in thread

* Re: [PATCH] fs: O_* bit numbers uniqueness check
  2010-01-06 15:48 ` Christoph Hellwig
@ 2010-01-09 13:52   ` Wu Fengguang
  0 siblings, 0 replies; 15+ messages in thread
From: Wu Fengguang @ 2010-01-09 13:52 UTC (permalink / raw)
  To: Christoph Hellwig
  Cc: Andrew Morton, David Miller, Stephen Rothwell, Al Viro,
	Eric Paris, LKML, linux-fsdevel

On Wed, Jan 06, 2010 at 11:48:14PM +0800, Christoph Hellwig wrote:
> On Wed, Jan 06, 2010 at 02:55:26PM +0800, Wu Fengguang wrote:
> > 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.
> 
> The right fix is to stop overlyaing the FMODE_ flags into the O_
> namespace.  Al has been working towards this.

I thinks they are orthogonal problems -- this patch watches for the
possible conflicting O_* in so many archs.

Anyway here is the new version without the FMODE_ and O_RANDOM.

Thanks,
Fengguang
---
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.

v4: use the nice hweight_long() as suggested by Jamie
    split O_SYNC to (__O_SYNC | O_DSYNC) as suggested by Andreas
    take away FMODE_* and O_RANDOM
v3: change to BUILD_BUG_ON() as suggested by Roland

CC: David Miller <davem@davemloft.net>
CC: Stephen Rothwell <sfr@canb.auug.org.au>
CC: Al Viro <viro@zeniv.linux.org.uk>
CC: Christoph Hellwig <hch@infradead.org>
CC: Eric Paris <eparis@redhat.com>
CC: Roland Dreier <rdreier@cisco.com>
CC: Jamie Lokier <jamie@shareable.org>
CC: Andreas Schwab <schwab@linux-m68k.org>
Signed-off-by: Wu Fengguang <fengguang.wu@intel.com>
---
 fs/fcntl.c                  |   14 ++++++++++++--
 include/asm-generic/fcntl.h |    2 ++
 2 files changed, 14 insertions(+), 2 deletions(-)

--- linux.orig/fs/fcntl.c	2010-01-09 11:02:57.000000000 +0800
+++ linux/fs/fcntl.c	2010-01-09 21:36:16.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(17 != hweight_long(
+		O_RDONLY	| O_WRONLY	| O_RDWR	|
+		O_CREAT		| O_EXCL	| O_NOCTTY	|
+		O_TRUNC		| O_APPEND	| O_NONBLOCK	|
+		__O_SYNC	| O_DSYNC	| FASYNC	|
+		O_DIRECT	| O_LARGEFILE	| O_DIRECTORY	|
+		O_NOFOLLOW	| O_NOATIME	| O_CLOEXEC
+		));
+
 	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-09 11:02:57.000000000 +0800
+++ linux/include/asm-generic/fcntl.h	2010-01-09 12:23:17.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

^ permalink raw reply	[flat|nested] 15+ messages in thread

end of thread, other threads:[~2010-01-09 13:52 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-01-06  6:55 [PATCH] fs: O_* bit numbers uniqueness check Wu Fengguang
2010-01-06  7:07 ` Eric Dumazet
2010-01-06  7:07   ` Eric Dumazet
2010-01-06  7:20   ` Wu Fengguang
2010-01-06  9:13     ` Andreas Schwab
2010-01-09 13:33       ` Wu Fengguang
2010-01-09 13:33         ` Wu Fengguang
2010-01-06  7:08 ` Roland Dreier
2010-01-06  7:18   ` [PATCH v2] " Wu Fengguang
2010-01-06  7:30     ` Roland Dreier
2010-01-06  7:42       ` Wu Fengguang
2010-01-06 16:20   ` [PATCH] " Jamie Lokier
2010-01-09 13:44     ` Wu Fengguang
2010-01-06 15:48 ` Christoph Hellwig
2010-01-09 13:52   ` Wu Fengguang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.