--- v2
+++ v1
@@ -1,119 +1,85 @@
From: Arnd Bergmann <arnd@arndb.de>
-sparc64 is one of the architectures that uses separate address
-spaces for kernel and user addresses, so __get_kernel_nofault()
-can not just call into the normal __get_user() without the
-access_ok() check.
+The way that access_ok() is defined on x86 is slightly different from
+most other architectures, and a bit more complex.
-Instead duplicate __get_user() and __put_user() into their
-in-kernel versions, with minor changes for the calling conventions
-and leaving out the address space modifier on the assembler
-instruction.
+The generic version tends to result in the best output on all
+architectures, as it results in single comparison against a constant
+limit for calls with a known size.
-This could surely be written more elegantly, but duplicating it
-gets the job done.
+There are a few callers of __range_not_ok(), all of which use TASK_SIZE
+as the limit rather than TASK_SIZE_MAX, but I could not see any reason
+for picking this. Changing these to call __access_ok() instead uses the
+default limit, but keeps the behavior otherwise.
+
+x86 is the only architecture with a WARN_ON_IN_IRQ() checking
+access_ok(), but it's probably best to leave that in place.
Signed-off-by: Arnd Bergmann <arnd@arndb.de>
---
- arch/sparc/include/asm/uaccess_64.h | 78 +++++++++++++++++++++++++++++
- 1 file changed, 78 insertions(+)
+ arch/x86/include/asm/uaccess.h | 38 +++++++++++-----------------------
+ 1 file changed, 12 insertions(+), 26 deletions(-)
-diff --git a/arch/sparc/include/asm/uaccess_64.h b/arch/sparc/include/asm/uaccess_64.h
-index 30eb4c6414d1..b283798315b1 100644
---- a/arch/sparc/include/asm/uaccess_64.h
-+++ b/arch/sparc/include/asm/uaccess_64.h
-@@ -100,6 +100,42 @@ void __retl_efault(void);
- struct __large_struct { unsigned long buf[100]; };
- #define __m(x) ((struct __large_struct *)(x))
+diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h
+index ac96f9b2d64b..6956a63291b6 100644
+--- a/arch/x86/include/asm/uaccess.h
++++ b/arch/x86/include/asm/uaccess.h
+@@ -16,30 +16,13 @@
+ * Test whether a block of memory is a valid user space address.
+ * Returns 0 if the range is valid, nonzero otherwise.
+ */
+-static inline bool __chk_range_not_ok(unsigned long addr, unsigned long size, unsigned long limit)
++static inline bool __access_ok(void __user *ptr, unsigned long size)
+ {
+- /*
+- * If we have used "sizeof()" for the size,
+- * we know it won't overflow the limit (but
+- * it might overflow the 'addr', so it's
+- * important to subtract the size from the
+- * limit, not add it to the address).
+- */
+- if (__builtin_constant_p(size))
+- return unlikely(addr > limit - size);
+-
+- /* Arbitrary sizes? Be careful about overflow */
+- addr += size;
+- if (unlikely(addr < size))
+- return true;
+- return unlikely(addr > limit);
+-}
++ unsigned long limit = TASK_SIZE_MAX;
++ unsigned long addr = ptr;
-+#define __put_kernel_nofault(dst, src, type, label) \
-+do { \
-+ type *addr = (type __force *)(dst); \
-+ type data = *(type *)src; \
-+ register int __pu_ret; \
-+ switch (sizeof(type)) { \
-+ case 1: __put_kernel_asm(data, b, addr, __pu_ret); break; \
-+ case 2: __put_kernel_asm(data, h, addr, __pu_ret); break; \
-+ case 4: __put_kernel_asm(data, w, addr, __pu_ret); break; \
-+ case 8: __put_kernel_asm(data, x, addr, __pu_ret); break; \
-+ default: __pu_ret = __put_user_bad(); break; \
-+ } \
-+ if (__pu_ret) \
-+ goto label; \
-+} while (0)
+-#define __range_not_ok(addr, size, limit) \
+-({ \
+- __chk_user_ptr(addr); \
+- __chk_range_not_ok((unsigned long __force)(addr), size, limit); \
+-})
++ return (size <= limit) && (addr <= (limit - size));
++}
+
+ #ifdef CONFIG_DEBUG_ATOMIC_SLEEP
+ static inline bool pagefault_disabled(void);
+@@ -66,12 +49,15 @@ static inline bool pagefault_disabled(void);
+ * Return: true (nonzero) if the memory block may be valid, false (zero)
+ * if it is definitely invalid.
+ */
+-#define access_ok(addr, size) \
+-({ \
+- WARN_ON_IN_IRQ(); \
+- likely(!__range_not_ok(addr, size, TASK_SIZE_MAX)); \
++#define access_ok(addr, size) \
++({ \
++ WARN_ON_IN_IRQ(); \
++ likely(__access_ok(addr, size));\
+ })
+
++#define __range_not_ok(addr, size, limit) (!__access_ok(addr, size))
++#define __chk_range_not_ok(addr, size, limit) (!__access_ok((void __user *)addr, size))
+
-+#define __put_kernel_asm(x, size, addr, ret) \
-+__asm__ __volatile__( \
-+ "/* Put kernel asm, inline. */\n" \
-+ "1:\t" "st"#size " %1, [%2]\n\t" \
-+ "clr %0\n" \
-+ "2:\n\n\t" \
-+ ".section .fixup,#alloc,#execinstr\n\t" \
-+ ".align 4\n" \
-+ "3:\n\t" \
-+ "sethi %%hi(2b), %0\n\t" \
-+ "jmpl %0 + %%lo(2b), %%g0\n\t" \
-+ " mov %3, %0\n\n\t" \
-+ ".previous\n\t" \
-+ ".section __ex_table,\"a\"\n\t" \
-+ ".align 4\n\t" \
-+ ".word 1b, 3b\n\t" \
-+ ".previous\n\n\t" \
-+ : "=r" (ret) : "r" (x), "r" (__m(addr)), \
-+ "i" (-EFAULT))
-+
- #define __put_user_nocheck(data, addr, size) ({ \
- register int __pu_ret; \
- switch (size) { \
-@@ -134,6 +170,48 @@ __asm__ __volatile__( \
-
- int __put_user_bad(void);
-
-+#define __get_kernel_nofault(dst, src, type, label) \
-+do { \
-+ type *addr = (type __force *)(src); \
-+ register int __gu_ret; \
-+ register unsigned long __gu_val; \
-+ switch (sizeof(type)) { \
-+ case 1: __get_kernel_asm(__gu_val, ub, addr, __gu_ret); break; \
-+ case 2: __get_kernel_asm(__gu_val, uh, addr, __gu_ret); break; \
-+ case 4: __get_kernel_asm(__gu_val, uw, addr, __gu_ret); break; \
-+ case 8: __get_kernel_asm(__gu_val, x, addr, __gu_ret); break; \
-+ default: \
-+ __gu_val = 0; \
-+ __gu_ret = __get_user_bad(); \
-+ break; \
-+ } \
-+ if (__gu_ret) \
-+ goto label; \
-+ *(type *)dst = (__force type) __gu_val; \
-+} while (0)
-+#define __get_kernel_asm(x, size, addr, ret) \
-+__asm__ __volatile__( \
-+ "/* Get kernel asm, inline. */\n" \
-+ "1:\t" "ld"#size " [%2], %1\n\t" \
-+ "clr %0\n" \
-+ "2:\n\n\t" \
-+ ".section .fixup,#alloc,#execinstr\n\t" \
-+ ".align 4\n" \
-+ "3:\n\t" \
-+ "sethi %%hi(2b), %0\n\t" \
-+ "clr %1\n\t" \
-+ "jmpl %0 + %%lo(2b), %%g0\n\t" \
-+ " mov %3, %0\n\n\t" \
-+ ".previous\n\t" \
-+ ".section __ex_table,\"a\"\n\t" \
-+ ".align 4\n\t" \
-+ ".word 1b, 3b\n\n\t" \
-+ ".previous\n\t" \
-+ : "=r" (ret), "=r" (x) : "r" (__m(addr)), \
-+ "i" (-EFAULT))
-+
-+#define HAVE_GET_KERNEL_NOFAULT
-+
- #define __get_user_nocheck(data, addr, size, type) ({ \
- register int __gu_ret; \
- register unsigned long __gu_val; \
+ extern int __get_user_1(void);
+ extern int __get_user_2(void);
+ extern int __get_user_4(void);
--
2.29.2