Skip to contentSkip to conversation

Expose range-checking functions from arch-specific uaccess.h

PATCH In review Rusty Russell 2 participants 3 messages Apr 4, 2007 Generic include/asm header files

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Expose range-checking functions from arch-specific uaccess.h

lguest wants range checking, but unless there are other users I'm not sure this is worth the pain. Putting it out there in case it's useful.

Cheers,
Rusty.
==
There are some places where we want to check if a value + length is within a range. This can be tricky, so it's nice to have a single routine.

Also, this can be done more efficiently (with arch-dependent) carry bits, as shown by the __range_ok() implementation on several archs.

This patch exposes arch-specific "val_outside(val, len, limit)" and a generic "range_within(start, len, base, limit)" function. As a side-effect, it fixes the comment in i386/uaccess.h: __range_ok doesn't test "(u33)addr + (u33)size >= (u33)current->addr_limit.seg", it tests "(u33)addr + (u33)size > (u33)current->addr_limit.seg".

include/asm-generic/range.h new file +20 -0
diff -r 4272a6d4f27c include/asm-generic/range.h
@@ -0,0 +1,20 @@
+#ifndef _ASM_GENERIC_RANGE_H
+#define _ASM_GENERIC_RANGE_H
+
+/**
+ * val_outside - is a value and length past a limit?
+ * @val: the start value
+ * @len: the length from the start
+ * @limit: the first invalid value
+ *
+ * Like val + len > limit, except with overflow checking.
+ */
+static inline bool val_outside(unsigned long val, unsigned long len,
+			       unsigned long limit)
+			       
+{
+	return val + len > limit || val + len < val;
+}
+
+#endif	/* _ASM_GENERIC_RANGE_H */
+
include/asm-i386/range.h new file +14 -0
diff -r 4272a6d4f27c include/asm-i386/range.h
@@ -0,0 +1,14 @@
+#ifndef __ASM_RANGE_H
+#define __ASM_RANGE_H
+
+/* Is val + size > limit? This needs 33-bit arithmetic. We have a carry... */
+static inline bool val_outside(unsigned long val, unsigned long len,
+			       unsigned long limit)
+{
+	unsigned long flag, roksum;
+	asm("addl %3,%1 ; sbbl %0,%0; cmpl %1,%4; sbbl $0,%0"
+		:"=&r" (flag), "=r" (roksum)
+		:"1" (val), "g" (len), "rm" (limit));
+	return flag;
+}
+#endif	/* __ASM_RANGE_H */
include/asm-i386/uaccess.h +6 -11
diff -r 4272a6d4f27c include/asm-i386/uaccess.h
@@ -49,17 +49,12 @@ extern struct movsl_mask {
  * Returns 0 if the range is valid, nonzero otherwise.
  *
  * This is equivalent to the following test:
- * (u33)addr + (u33)size >= (u33)current->addr_limit.seg
- *
- * This needs 33-bit arithmetic. We have a carry...
- */
-#define __range_ok(addr,size) ({ \
-	unsigned long flag,roksum; \
-	__chk_user_ptr(addr); \
-	asm("addl %3,%1 ; sbbl %0,%0; cmpl %1,%4; sbbl $0,%0" \
-		:"=&r" (flag), "=r" (roksum) \
-		:"1" (addr),"g" ((int)(size)),"rm" (current_thread_info()->addr_limit.seg)); \
-	flag; })
+ * (u33)addr + (u33)size > (u33)current->addr_limit.seg
+ */
+#define __range_ok(addr,size) ({					      \
+	__chk_user_ptr(addr);						      \
+	val_outside((int)(addr),(size),current_thread_info()->addr_limit.seg); \
+})
 
 /**
  * access_ok: - Checks if a user space pointer is valid
include/linux/kernel.h +21 -0
diff -r 4272a6d4f27c include/linux/kernel.h
@@ -16,6 +16,7 @@
 #include <linux/log2.h>
 #include <asm/byteorder.h>
 #include <asm/bug.h>
+#include <asm/range.h>
 
 extern const char linux_banner[];
 extern const char linux_proc_banner[];
@@ -311,6 +312,26 @@ static inline int __attribute__ ((format
 	(void)__tmp; \
 })
 
+/**
+ * range_within - is one range within another?
+ * @start: the start value
+ * @len: the length from the start
+ * @base: the first valid value
+ * @limit: the first invalid value
+ *
+ * This is usually used for memory range testing.  The common cases of
+ * constant 0 start and constant 0 len cases are optimized out.
+ */
+static inline bool range_within(unsigned long start, unsigned long len,
+				unsigned long base, unsigned long limit)
+{
+	if (start < base)
+		return false;
+	if (__builtin_constant_p(len) && len == 0)
+		return start - base <= limit - base;
+	return !val_outside(limit, start, len);
+}
+
 struct sysinfo;
 extern int do_sysinfo(struct sysinfo *info);
 
include/asm-alpha/range.h new file +6 -0
diff -r 4272a6d4f27c include/asm-alpha/range.h
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */
include/asm-arm/range.h new file +6 -0
diff -r 4272a6d4f27c include/asm-arm/range.h
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */
include/asm-arm26/range.h new file +6 -0
diff -r 4272a6d4f27c include/asm-arm26/range.h
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */
include/asm-avr32/range.h new file +6 -0
diff -r 4272a6d4f27c include/asm-avr32/range.h
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */
include/asm-blackfin/range.h new file +6 -0
diff -r 4272a6d4f27c include/asm-blackfin/range.h
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */
include/asm-cris/range.h new file +6 -0
diff -r 4272a6d4f27c include/asm-cris/range.h
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */
include/asm-frv/range.h new file +6 -0
diff -r 4272a6d4f27c include/asm-frv/range.h
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */
include/asm-h8300/range.h new file +6 -0
diff -r 4272a6d4f27c include/asm-h8300/range.h
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */
include/asm-ia64/range.h new file +6 -0
diff -r 4272a6d4f27c include/asm-ia64/range.h
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */
include/asm-m32r/range.h new file +6 -0
diff -r 4272a6d4f27c include/asm-m32r/range.h
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */
include/asm-m68k/range.h new file +6 -0
diff -r 4272a6d4f27c include/asm-m68k/range.h
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */
include/asm-m68knommu/range.h new file +6 -0
diff -r 4272a6d4f27c include/asm-m68knommu/range.h
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */
include/asm-mips/range.h new file +6 -0
diff -r 4272a6d4f27c include/asm-mips/range.h
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */
include/asm-parisc/range.h new file +6 -0
diff -r 4272a6d4f27c include/asm-parisc/range.h
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */
include/asm-powerpc/range.h new file +6 -0
diff -r 4272a6d4f27c include/asm-powerpc/range.h
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */
include/asm-ppc/range.h new file +6 -0
diff -r 4272a6d4f27c include/asm-ppc/range.h
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */
include/asm-s390/range.h new file +6 -0
diff -r 4272a6d4f27c include/asm-s390/range.h
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */
include/asm-sh/range.h new file +6 -0
diff -r 4272a6d4f27c include/asm-sh/range.h
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */
include/asm-sh64/range.h new file +6 -0
diff -r 4272a6d4f27c include/asm-sh64/range.h
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */
include/asm-sparc/range.h new file +6 -0
diff -r 4272a6d4f27c include/asm-sparc/range.h
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */
include/asm-sparc64/range.h new file +6 -0
diff -r 4272a6d4f27c include/asm-sparc64/range.h
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */
include/asm-um/range.h new file +6 -0
diff -r 4272a6d4f27c include/asm-um/range.h
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */
include/asm-v850/range.h new file +6 -0
diff -r 4272a6d4f27c include/asm-v850/range.h
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */
include/asm-x86_64/range.h new file +6 -0
diff -r 4272a6d4f27c include/asm-x86_64/range.h
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */
include/asm-xtensa/range.h new file +6 -0
diff -r 4272a6d4f27c include/asm-xtensa/range.h
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */
#1 lore raw
Quoted 31 lines · On Wed, 04 Apr 2007 12:28:38 +1000 Rusty Russell wrote:

lguest wants range checking, but unless there are other users I'm not sure this is worth the pain. Putting it out there in case it's useful.

Cheers,
Rusty.
==
There are some places where we want to check if a value + length is within a range. This can be tricky, so it's nice to have a single routine.

Also, this can be done more efficiently (with arch-dependent) carry bits, as shown by the __range_ok() implementation on several archs.

This patch exposes arch-specific "val_outside(val, len, limit)" and a generic "range_within(start, len, base, limit)" function. As a side-effect, it fixes the comment in i386/uaccess.h: __range_ok doesn't test "(u33)addr + (u33)size >= (u33)current->addr_limit.seg", it tests "(u33)addr + (u33)size > (u33)current->addr_limit.seg".

diff -r 4272a6d4f27c include/asm-generic/range.h

include/asm-generic/range.h new file +6 -0
@@ -0,0 +1,20 @@
+#ifndef _ASM_GENERIC_RANGE_H
+#define _ASM_GENERIC_RANGE_H
+
+/**
+ * val_outside - is a value and length past a limit?
+ * @val: the start value

include/asm-generic/range.h line 16

Quoted 10 lines
include/asm-generic/range.h new file +10 -0
+ * @len: the length from the start
+ * @limit: the first invalid value
+ *
+ * Like val + len > limit, except with overflow checking.
+ */
+static inline bool val_outside(unsigned long val, unsigned long len,
+			       unsigned long limit)
+			       
+{
+	return val + len > limit || val + len < val;

why isn't that

	return val + len >= limit || val + len < val;
Quoted 35 lines
include/asm-generic/range.h new file +4 -0
+}
+
+#endif	/* _ASM_GENERIC_RANGE_H */
+

diff -r 4272a6d4f27c include/asm-i386/range.h

include/asm-i386/range.h new file +14 -0
@@ -0,0 +1,14 @@
+#ifndef __ASM_RANGE_H
+#define __ASM_RANGE_H
+
+/* Is val + size > limit? This needs 33-bit arithmetic. We have a carry... */
+static inline bool val_outside(unsigned long val, unsigned long len,
+			       unsigned long limit)
+{
+	unsigned long flag, roksum;
+	asm("addl %3,%1 ; sbbl %0,%0; cmpl %1,%4; sbbl $0,%0"
+		:"=&r" (flag), "=r" (roksum)
+		:"1" (val), "g" (len), "rm" (limit));
+	return flag;
+}
+#endif	/* __ASM_RANGE_H */

diff -r 4272a6d4f27c include/asm-i386/uaccess.h

include/asm-i386/uaccess.h +0 -6
@@ -49,17 +49,12 @@ extern struct movsl_mask {
  * Returns 0 if the range is valid, nonzero otherwise.
  *
  * This is equivalent to the following test:
- * (u33)addr + (u33)size >= (u33)current->addr_limit.seg
- *
- * This needs 33-bit arithmetic. We have a carry...
- */
-#define __range_ok(addr,size) ({ \
-	unsigned long flag,roksum; \

include/asm-i386/uaccess.h extern struct movsl_mask { · line 56

Quoted 10 lines
include/asm-i386/uaccess.h +5 -5
-	__chk_user_ptr(addr); \
-	asm("addl %3,%1 ; sbbl %0,%0; cmpl %1,%4; sbbl $0,%0" \
-		:"=&r" (flag), "=r" (roksum) \
-		:"1" (addr),"g" ((int)(size)),"rm" (current_thread_info()->addr_limit.seg)); \
-	flag; })
+ * (u33)addr + (u33)size > (u33)current->addr_limit.seg
+ */
+#define __range_ok(addr,size) ({					      \
+	__chk_user_ptr(addr);						      \
+	val_outside((int)(addr),(size),current_thread_info()->addr_limit.seg); \

space after commas, please.

Quoted 19 lines
include/asm-i386/uaccess.h +1 -0
+})
 
 /**
  * access_ok: - Checks if a user space pointer is valid

diff -r 4272a6d4f27c include/linux/kernel.h

include/linux/kernel.h +1 -0
@@ -16,6 +16,7 @@
 #include <linux/log2.h>
 #include <asm/byteorder.h>
 #include <asm/bug.h>
+#include <asm/range.h>
 
 extern const char linux_banner[];
 extern const char linux_proc_banner[];
@@ -311,6 +312,26 @@ static inline int __attribute__ ((format
 	(void)__tmp; \
 })
 

include/linux/kernel.h static inline int __attribute__ ((format · line 324

Quoted 10 lines
include/linux/kernel.h +10 -0
+/**
+ * range_within - is one range within another?
+ * @start: the start value
+ * @len: the length from the start
+ * @base: the first valid value
+ * @limit: the first invalid value
+ *
+ * This is usually used for memory range testing.  The common cases of
+ * constant 0 start and constant 0 len cases are optimized out.
+ */

Where is constant 0 start optimized out?

include/linux/kernel.h static inline int __attribute__ ((format · line 330

Quoted 6 lines
include/linux/kernel.h +6 -0
+static inline bool range_within(unsigned long start, unsigned long len,
+				unsigned long base, unsigned long limit)
+{
+	if (start < base)
+		return false;
+	if (__builtin_constant_p(len) && len == 0)

I don't see what the builtin_constant_p() adds here. If len == 0, this return <expression> is still OK, isn't it?

Quoted 21 lines
include/linux/kernel.h +4 -0
+		return start - base <= limit - base;
+	return !val_outside(limit, start, len);
+}
+
 struct sysinfo;
 extern int do_sysinfo(struct sysinfo *info);
 

diff -r 4272a6d4f27c include/asm-alpha/range.h

include/asm-alpha/range.h new file +6 -0
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */

diff -r 4272a6d4f27c include/asm-arm/range.h

include/asm-arm/range.h line 2

Quoted 2 lines
include/asm-arm/range.h new file +2 -0
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H

why ALPHA ?

include/asm-arm26/range.h line 2

Quoted 10 lines
include/asm-arm/range.h new file +4 -0
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */

diff -r 4272a6d4f27c include/asm-arm26/range.h

include/asm-arm26/range.h new file +2 -0
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H

ditto

include/asm-avr32/range.h line 2

Quoted 10 lines
include/asm-arm26/range.h new file +4 -0
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */

diff -r 4272a6d4f27c include/asm-avr32/range.h

include/asm-avr32/range.h new file +2 -0
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H

tritto etc. etc.

Quoted 208 lines
include/asm-avr32/range.h new file +4 -0
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */

diff -r 4272a6d4f27c include/asm-blackfin/range.h

include/asm-blackfin/range.h new file +6 -0
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */

diff -r 4272a6d4f27c include/asm-cris/range.h

include/asm-cris/range.h new file +6 -0
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */

diff -r 4272a6d4f27c include/asm-frv/range.h

include/asm-frv/range.h new file +6 -0
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */

diff -r 4272a6d4f27c include/asm-h8300/range.h

include/asm-h8300/range.h new file +6 -0
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */

diff -r 4272a6d4f27c include/asm-ia64/range.h

include/asm-ia64/range.h new file +6 -0
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */

diff -r 4272a6d4f27c include/asm-m32r/range.h

include/asm-m32r/range.h new file +6 -0
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */

diff -r 4272a6d4f27c include/asm-m68k/range.h

include/asm-m68k/range.h new file +6 -0
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */

diff -r 4272a6d4f27c include/asm-m68knommu/range.h

include/asm-m68knommu/range.h new file +6 -0
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */

diff -r 4272a6d4f27c include/asm-mips/range.h

include/asm-mips/range.h new file +6 -0
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */

diff -r 4272a6d4f27c include/asm-parisc/range.h

include/asm-parisc/range.h new file +6 -0
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */

diff -r 4272a6d4f27c include/asm-powerpc/range.h

include/asm-powerpc/range.h new file +6 -0
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */

diff -r 4272a6d4f27c include/asm-ppc/range.h

include/asm-ppc/range.h new file +6 -0
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */

diff -r 4272a6d4f27c include/asm-s390/range.h

include/asm-s390/range.h new file +6 -0
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */

diff -r 4272a6d4f27c include/asm-sh/range.h

include/asm-sh/range.h new file +6 -0
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */

diff -r 4272a6d4f27c include/asm-sh64/range.h

include/asm-sh64/range.h new file +6 -0
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */

diff -r 4272a6d4f27c include/asm-sparc/range.h

include/asm-sparc/range.h new file +6 -0
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */

diff -r 4272a6d4f27c include/asm-sparc64/range.h

include/asm-sparc64/range.h new file +6 -0
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */

diff -r 4272a6d4f27c include/asm-um/range.h

include/asm-um/range.h new file +6 -0
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */

diff -r 4272a6d4f27c include/asm-v850/range.h

include/asm-v850/range.h new file +6 -0
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */

diff -r 4272a6d4f27c include/asm-x86_64/range.h

include/asm-x86_64/range.h new file +6 -0
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */

diff -r 4272a6d4f27c include/asm-xtensa/range.h

include/asm-xtensa/range.h line 6

Quoted 6 lines
include/asm-xtensa/range.h new file +6 -0
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ALPHA_RANGE_H */

---
~Randy
*** Remember to use Documentation/SubmitChecklist when testing your code ***

#2 lore raw
Quoted 15 lines · Randy Dunlap

Review

Quoted 13 lines
(quoted diff) +13 -0
+/**
+ * val_outside - is a value and length past a limit?
+ * @val: the start value
+ * @len: the length from the start
+ * @limit: the first invalid value
+ *
+ * Like val + len > limit, except with overflow checking.
+ */
+static inline bool val_outside(unsigned long val, unsigned long len,
+			       unsigned long limit)
+			       
+{
+	return val + len > limit || val + len < val;

why isn't that

Quoted 1 line
	return val + len >= limit || val + len < val;

Hi Randy!

Good question! Mainly because that's what the existing __range_ok()
function does: limit is the first *invalid* value, not the last valid value.

	Say your limit is 0xC000 0000.  You want to be able to write 1 byte to

0xBFFF FFFF. So it's only outside if val + len > limit.

Quoted 5 lines

Review

Quoted 3 lines
(quoted diff) +3 -0
+#define __range_ok(addr,size) ({					      \
+	__chk_user_ptr(addr);						      \
+	val_outside((int)(addr),(size),current_thread_info()->addr_limit.seg); \

space after commas, please.

Yeah, I copied the existing code (but gladly: spaces pushes us onto another line). Fixed.

Quoted 5 lines
Quoted 3 lines

+ * This is usually used for memory range testing. The common cases of
+ * constant 0 start and constant 0 len cases are optimized out.
+ */

Where is constant 0 start optimized out?

The start < base is optimized out if base is known to be 0 (they're both unsigned): gcc realizes that can never be true so kills that whole branch.

Quoted 9 lines

Review

Quoted 6 lines
(quoted diff) +6 -0
+static inline bool range_within(unsigned long start, unsigned long len,
+				unsigned long base, unsigned long limit)
+{
+	if (start < base)
+		return false;
+	if (__builtin_constant_p(len) && len == 0)

I don't see what the builtin_constant_p() adds here. If len == 0, this return <expression> is still OK, isn't it?

We don't really want a branch in this code: __builtin_constant_p() ensures we end up only with "start - base <= limit - base" or "! val_outside(limit, start, len)" in the binary.

Quoted 8 lines

include/asm-arm/range.h line 2

Quoted 6 lines

diff -r 4272a6d4f27c include/asm-arm/range.h

include/asm-arm/range.h new file +2 -0
@@ -0,0 +1,6 @@
+#ifndef __ALPHA_RANGE_H
+#define __ALPHA_RANGE_H

why ALPHA ?

Oops. Because I coded this with a "for d in include/asm-*...".

Fixed.

Thanks!
Rusty.
==
There are some places where we want to check if a value + length is within a range. This can be tricky, so it's nice to have a single routine.

Also, this can be done more efficiently (with arch-dependent) carry bits, as shown by the __range_ok() implementation on several archs.

This patch exposes arch-specific "val_outside(val, len, limit)" and a generic "range_within(start, len, base, limit)" function. As a side-effect, it fixes the comment in i386/uaccess.h: __range_ok doesn't test "(u33)addr + (u33)size >= (u33)current->addr_limit.seg", it tests "(u33)addr + (u33)size > (u33)current->addr_limit.seg".

include/linux/kernel.h +21 -0
diff -r 4815a24d0254 include/linux/kernel.h
@@ -16,6 +16,7 @@
 #include <linux/log2.h>
 #include <asm/byteorder.h>
 #include <asm/bug.h>
+#include <asm/range.h>
 
 extern const char linux_banner[];
 extern const char linux_proc_banner[];
@@ -311,6 +312,26 @@ static inline int __attribute__ ((format
 	(void)__tmp; \
 })
 
+/**
+ * range_within - is one range within another?
+ * @start: the start value
+ * @len: the length from the start
+ * @base: the first valid value
+ * @limit: the first invalid value
+ *
+ * This is usually used for memory range testing.  The common cases of
+ * constant 0 start and constant 0 len cases are optimized out.
+ */
+static inline bool range_within(unsigned long start, unsigned long len,
+				unsigned long base, unsigned long limit)
+{
+	if (start < base)
+		return false;
+	if (__builtin_constant_p(len) && len == 0)
+		return start - base <= limit - base;
+	return !val_outside(limit, start, len);
+}
+
 struct sysinfo;
 extern int do_sysinfo(struct sysinfo *info);
 
include/asm-i386/range.h new file +14 -0
diff -r 4815a24d0254 include/asm-i386/range.h
@@ -0,0 +1,14 @@
+#ifndef __ASM_RANGE_H
+#define __ASM_RANGE_H
+
+/* Is val + size > limit? This needs 33-bit arithmetic. We have a carry... */
+static inline bool val_outside(unsigned long val, unsigned long len,
+			       unsigned long limit)
+{
+	unsigned long flag, roksum;
+	asm("addl %3,%1 ; sbbl %0,%0; cmpl %1,%4; sbbl $0,%0"
+		:"=&r" (flag), "=r" (roksum)
+		:"1" (val), "g" (len), "rm" (limit));
+	return flag;
+}
+#endif	/* __ASM_RANGE_H */
include/asm-i386/uaccess.h +7 -11
diff -r 4815a24d0254 include/asm-i386/uaccess.h
@@ -49,17 +49,13 @@ extern struct movsl_mask {
  * Returns 0 if the range is valid, nonzero otherwise.
  *
  * This is equivalent to the following test:
- * (u33)addr + (u33)size >= (u33)current->addr_limit.seg
- *
- * This needs 33-bit arithmetic. We have a carry...
- */
-#define __range_ok(addr,size) ({ \
-	unsigned long flag,roksum; \
-	__chk_user_ptr(addr); \
-	asm("addl %3,%1 ; sbbl %0,%0; cmpl %1,%4; sbbl $0,%0" \
-		:"=&r" (flag), "=r" (roksum) \
-		:"1" (addr),"g" ((int)(size)),"rm" (current_thread_info()->addr_limit.seg)); \
-	flag; })
+ * (u33)addr + (u33)size > (u33)current->addr_limit.seg
+ */
+#define __range_ok(addr, size) ({				\
+	__chk_user_ptr(addr);					\
+	val_outside((int)(addr), (size),			\
+		    current_thread_info()->addr_limit.seg);	\
+})
 
 /**
  * access_ok: - Checks if a user space pointer is valid
include/asm-generic/range.h new file +20 -0
diff -r 4815a24d0254 include/asm-generic/range.h
@@ -0,0 +1,20 @@
+#ifndef _ASM_GENERIC_RANGE_H
+#define _ASM_GENERIC_RANGE_H
+
+/**
+ * val_outside - is a value and length past a limit?
+ * @val: the start value
+ * @len: the length from the start
+ * @limit: the first invalid value
+ *
+ * Like val + len > limit, except with overflow checking.
+ */
+static inline bool val_outside(unsigned long val, unsigned long len,
+			       unsigned long limit)
+			       
+{
+	return val + len > limit || val + len < val;
+}
+
+#endif	/* _ASM_GENERIC_RANGE_H */
+
include/asm-alpha/range.h new file +6 -0
diff -r 4815a24d0254 include/asm-alpha/range.h
@@ -0,0 +1,6 @@
+#ifndef __ASM_RANGE_H
+#define __ASM_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ASM_RANGE_H */
include/asm-arm/range.h new file +6 -0
diff -r 4815a24d0254 include/asm-arm/range.h
@@ -0,0 +1,6 @@
+#ifndef __ASM_RANGE_H
+#define __ASM_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ASM_RANGE_H */
include/asm-arm26/range.h new file +6 -0
diff -r 4815a24d0254 include/asm-arm26/range.h
@@ -0,0 +1,6 @@
+#ifndef __ASM_RANGE_H
+#define __ASM_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ASM_RANGE_H */
include/asm-avr32/range.h new file +6 -0
diff -r 4815a24d0254 include/asm-avr32/range.h
@@ -0,0 +1,6 @@
+#ifndef __ASM_RANGE_H
+#define __ASM_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ASM_RANGE_H */
include/asm-blackfin/range.h new file +6 -0
diff -r 4815a24d0254 include/asm-blackfin/range.h
@@ -0,0 +1,6 @@
+#ifndef __ASM_RANGE_H
+#define __ASM_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ASM_RANGE_H */
include/asm-cris/range.h new file +6 -0
diff -r 4815a24d0254 include/asm-cris/range.h
@@ -0,0 +1,6 @@
+#ifndef __ASM_RANGE_H
+#define __ASM_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ASM_RANGE_H */
include/asm-frv/range.h new file +6 -0
diff -r 4815a24d0254 include/asm-frv/range.h
@@ -0,0 +1,6 @@
+#ifndef __ASM_RANGE_H
+#define __ASM_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ASM_RANGE_H */
include/asm-h8300/range.h new file +6 -0
diff -r 4815a24d0254 include/asm-h8300/range.h
@@ -0,0 +1,6 @@
+#ifndef __ASM_RANGE_H
+#define __ASM_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ASM_RANGE_H */
include/asm-ia64/range.h new file +6 -0
diff -r 4815a24d0254 include/asm-ia64/range.h
@@ -0,0 +1,6 @@
+#ifndef __ASM_RANGE_H
+#define __ASM_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ASM_RANGE_H */
include/asm-m32r/range.h new file +6 -0
diff -r 4815a24d0254 include/asm-m32r/range.h
@@ -0,0 +1,6 @@
+#ifndef __ASM_RANGE_H
+#define __ASM_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ASM_RANGE_H */
include/asm-m68k/range.h new file +6 -0
diff -r 4815a24d0254 include/asm-m68k/range.h
@@ -0,0 +1,6 @@
+#ifndef __ASM_RANGE_H
+#define __ASM_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ASM_RANGE_H */
include/asm-m68knommu/range.h new file +6 -0
diff -r 4815a24d0254 include/asm-m68knommu/range.h
@@ -0,0 +1,6 @@
+#ifndef __ASM_RANGE_H
+#define __ASM_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ASM_RANGE_H */
include/asm-mips/range.h new file +6 -0
diff -r 4815a24d0254 include/asm-mips/range.h
@@ -0,0 +1,6 @@
+#ifndef __ASM_RANGE_H
+#define __ASM_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ASM_RANGE_H */
include/asm-parisc/range.h new file +6 -0
diff -r 4815a24d0254 include/asm-parisc/range.h
@@ -0,0 +1,6 @@
+#ifndef __ASM_RANGE_H
+#define __ASM_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ASM_RANGE_H */
include/asm-powerpc/range.h new file +6 -0
diff -r 4815a24d0254 include/asm-powerpc/range.h
@@ -0,0 +1,6 @@
+#ifndef __ASM_RANGE_H
+#define __ASM_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ASM_RANGE_H */
include/asm-ppc/range.h new file +6 -0
diff -r 4815a24d0254 include/asm-ppc/range.h
@@ -0,0 +1,6 @@
+#ifndef __ASM_RANGE_H
+#define __ASM_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ASM_RANGE_H */
include/asm-s390/range.h new file +6 -0
diff -r 4815a24d0254 include/asm-s390/range.h
@@ -0,0 +1,6 @@
+#ifndef __ASM_RANGE_H
+#define __ASM_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ASM_RANGE_H */
include/asm-sh/range.h new file +6 -0
diff -r 4815a24d0254 include/asm-sh/range.h
@@ -0,0 +1,6 @@
+#ifndef __ASM_RANGE_H
+#define __ASM_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ASM_RANGE_H */
include/asm-sh64/range.h new file +6 -0
diff -r 4815a24d0254 include/asm-sh64/range.h
@@ -0,0 +1,6 @@
+#ifndef __ASM_RANGE_H
+#define __ASM_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ASM_RANGE_H */
include/asm-sparc/range.h new file +6 -0
diff -r 4815a24d0254 include/asm-sparc/range.h
@@ -0,0 +1,6 @@
+#ifndef __ASM_RANGE_H
+#define __ASM_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ASM_RANGE_H */
include/asm-sparc64/range.h new file +6 -0
diff -r 4815a24d0254 include/asm-sparc64/range.h
@@ -0,0 +1,6 @@
+#ifndef __ASM_RANGE_H
+#define __ASM_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ASM_RANGE_H */
include/asm-um/range.h new file +6 -0
diff -r 4815a24d0254 include/asm-um/range.h
@@ -0,0 +1,6 @@
+#ifndef __ASM_RANGE_H
+#define __ASM_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ASM_RANGE_H */
include/asm-v850/range.h new file +6 -0
diff -r 4815a24d0254 include/asm-v850/range.h
@@ -0,0 +1,6 @@
+#ifndef __ASM_RANGE_H
+#define __ASM_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ASM_RANGE_H */
include/asm-x86_64/range.h new file +6 -0
diff -r 4815a24d0254 include/asm-x86_64/range.h
@@ -0,0 +1,6 @@
+#ifndef __ASM_RANGE_H
+#define __ASM_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ASM_RANGE_H */
include/asm-xtensa/range.h new file +6 -0
diff -r 4815a24d0254 include/asm-xtensa/range.h
@@ -0,0 +1,6 @@
+#ifndef __ASM_RANGE_H
+#define __ASM_RANGE_H
+
+#include <asm-generic/range.h>
+
+#endif /* __ASM_RANGE_H */