[PATCH 3.16 71/76] x86: reorganize SMAP handling in user space accesses

2018-03-11 Thread Ben Hutchings
3.16.56-rc1 review patch.  If anyone has any objections, please let me know.

--

From: Linus Torvalds 

commit 11f1a4b9755f5dbc3e822a96502ebe9b044b14d8 upstream.

This reorganizes how we do the stac/clac instructions in the user access
code.  Instead of adding the instructions directly to the same inline
asm that does the actual user level access and exception handling, add
them at a higher level.

This is mainly preparation for the next step, where we will expose an
interface to allow users to mark several accesses together as being user
space accesses, but it does already clean up some code:

 - the inlined trivial cases of copy_in_user() now do stac/clac just
   once over the accesses: they used to do one pair around the user
   space read, and another pair around the write-back.

 - the {get,put}_user_ex() macros that are used with the catch/try
   handling don't do any stac/clac at all, because that happens in the
   try/catch surrounding them.

Other than those two cleanups that happened naturally from the
re-organization, this should not make any difference. Yet.

Signed-off-by: Linus Torvalds 
[bwh: Backported to 3.16: adjust context]
Signed-off-by: Ben Hutchings 
---
 arch/x86/include/asm/uaccess.h| 53 ++
 arch/x86/include/asm/uaccess_64.h | 94 +++
 2 files changed, 101 insertions(+), 46 deletions(-)

--- a/arch/x86/include/asm/uaccess.h
+++ b/arch/x86/include/asm/uaccess.h
@@ -143,6 +143,9 @@ extern int __get_user_4(void);
 extern int __get_user_8(void);
 extern int __get_user_bad(void);
 
+#define __uaccess_begin() stac()
+#define __uaccess_end()   clac()
+
 /*
  * This is a type: either unsigned long, if the argument fits into
  * that type, or otherwise unsigned long long.
@@ -201,10 +204,10 @@ __typeof__(__builtin_choose_expr(sizeof(
 
 #ifdef CONFIG_X86_32
 #define __put_user_asm_u64(x, addr, err, errret)   \
-   asm volatile(ASM_STAC "\n"  \
+   asm volatile("\n"   \
 "1:movl %%eax,0(%2)\n" \
 "2:movl %%edx,4(%2)\n" \
-"3: " ASM_CLAC "\n"\
+"3:"   \
 ".section .fixup,\"ax\"\n" \
 "4:movl %3,%0\n"   \
 "  jmp 3b\n"   \
@@ -215,10 +218,10 @@ __typeof__(__builtin_choose_expr(sizeof(
 : "A" (x), "r" (addr), "i" (errret), "0" (err))
 
 #define __put_user_asm_ex_u64(x, addr) \
-   asm volatile(ASM_STAC "\n"  \
+   asm volatile("\n"   \
 "1:movl %%eax,0(%1)\n" \
 "2:movl %%edx,4(%1)\n" \
-"3: " ASM_CLAC "\n"\
+"3:"   \
 _ASM_EXTABLE_EX(1b, 2b)\
 _ASM_EXTABLE_EX(2b, 3b)\
 : : "A" (x), "r" (addr))
@@ -311,6 +314,10 @@ do {   
\
}   \
 } while (0)
 
+/*
+ * This doesn't do __uaccess_begin/end - the exception handling
+ * around it must do that.
+ */
 #define __put_user_size_ex(x, ptr, size)   \
 do {   \
__chk_user_ptr(ptr);\
@@ -365,9 +372,9 @@ do {
\
 } while (0)
 
 #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret)  \
-   asm volatile(ASM_STAC "\n"  \
+   asm volatile("\n"   \
 "1:mov"itype" %2,%"rtype"1\n"  \
-"2: " ASM_CLAC "\n"\
+"2:\n" \
 ".section .fixup,\"ax\"\n" \
 "3:mov %3,%0\n"\
 "  xor"itype" %"rtype"1,%"rtype"1\n"   \
@@ -377,6 +384,10 @@ do {   
\
 : "=r" (err), ltype(x)

[PATCH 3.16 71/76] x86: reorganize SMAP handling in user space accesses

2018-03-11 Thread Ben Hutchings
3.16.56-rc1 review patch.  If anyone has any objections, please let me know.

--

From: Linus Torvalds 

commit 11f1a4b9755f5dbc3e822a96502ebe9b044b14d8 upstream.

This reorganizes how we do the stac/clac instructions in the user access
code.  Instead of adding the instructions directly to the same inline
asm that does the actual user level access and exception handling, add
them at a higher level.

This is mainly preparation for the next step, where we will expose an
interface to allow users to mark several accesses together as being user
space accesses, but it does already clean up some code:

 - the inlined trivial cases of copy_in_user() now do stac/clac just
   once over the accesses: they used to do one pair around the user
   space read, and another pair around the write-back.

 - the {get,put}_user_ex() macros that are used with the catch/try
   handling don't do any stac/clac at all, because that happens in the
   try/catch surrounding them.

Other than those two cleanups that happened naturally from the
re-organization, this should not make any difference. Yet.

Signed-off-by: Linus Torvalds 
[bwh: Backported to 3.16: adjust context]
Signed-off-by: Ben Hutchings 
---
 arch/x86/include/asm/uaccess.h| 53 ++
 arch/x86/include/asm/uaccess_64.h | 94 +++
 2 files changed, 101 insertions(+), 46 deletions(-)

--- a/arch/x86/include/asm/uaccess.h
+++ b/arch/x86/include/asm/uaccess.h
@@ -143,6 +143,9 @@ extern int __get_user_4(void);
 extern int __get_user_8(void);
 extern int __get_user_bad(void);
 
+#define __uaccess_begin() stac()
+#define __uaccess_end()   clac()
+
 /*
  * This is a type: either unsigned long, if the argument fits into
  * that type, or otherwise unsigned long long.
@@ -201,10 +204,10 @@ __typeof__(__builtin_choose_expr(sizeof(
 
 #ifdef CONFIG_X86_32
 #define __put_user_asm_u64(x, addr, err, errret)   \
-   asm volatile(ASM_STAC "\n"  \
+   asm volatile("\n"   \
 "1:movl %%eax,0(%2)\n" \
 "2:movl %%edx,4(%2)\n" \
-"3: " ASM_CLAC "\n"\
+"3:"   \
 ".section .fixup,\"ax\"\n" \
 "4:movl %3,%0\n"   \
 "  jmp 3b\n"   \
@@ -215,10 +218,10 @@ __typeof__(__builtin_choose_expr(sizeof(
 : "A" (x), "r" (addr), "i" (errret), "0" (err))
 
 #define __put_user_asm_ex_u64(x, addr) \
-   asm volatile(ASM_STAC "\n"  \
+   asm volatile("\n"   \
 "1:movl %%eax,0(%1)\n" \
 "2:movl %%edx,4(%1)\n" \
-"3: " ASM_CLAC "\n"\
+"3:"   \
 _ASM_EXTABLE_EX(1b, 2b)\
 _ASM_EXTABLE_EX(2b, 3b)\
 : : "A" (x), "r" (addr))
@@ -311,6 +314,10 @@ do {   
\
}   \
 } while (0)
 
+/*
+ * This doesn't do __uaccess_begin/end - the exception handling
+ * around it must do that.
+ */
 #define __put_user_size_ex(x, ptr, size)   \
 do {   \
__chk_user_ptr(ptr);\
@@ -365,9 +372,9 @@ do {
\
 } while (0)
 
 #define __get_user_asm(x, addr, err, itype, rtype, ltype, errret)  \
-   asm volatile(ASM_STAC "\n"  \
+   asm volatile("\n"   \
 "1:mov"itype" %2,%"rtype"1\n"  \
-"2: " ASM_CLAC "\n"\
+"2:\n" \
 ".section .fixup,\"ax\"\n" \
 "3:mov %3,%0\n"\
 "  xor"itype" %"rtype"1,%"rtype"1\n"   \
@@ -377,6 +384,10 @@ do {   
\
 : "=r" (err), ltype(x) \
 : "m" (__m(addr)), "i" (errret), "0" (err))