mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "tip-bot for H. Peter Anvin" <hpa@linux.intel.com>
To: linux-tip-commits@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, hpa@zytor.com, mingo@kernel.org,
	torvalds@linux-foundation.org, jamie@shareable.org,
	ville.syrjala@linux.intel.com, bp@alien8.de,
	linux@arm.linux.org.uk, tglx@linutronix.de, hpa@linux.intel.com,
	hjl.tools@gmail.com
Subject: [tip:x86/mm] x86, mm: Redesign get_user with a __builtin_choose_expr hack
Date: Tue, 12 Feb 2013 12:55:54 -0800	[thread overview]
Message-ID: <tip-3578baaed4613a9fc09bab9f79f6ce2ac682e8a3@git.kernel.org> (raw)
In-Reply-To: <511A8922.6050908@zytor.com>

Commit-ID:  3578baaed4613a9fc09bab9f79f6ce2ac682e8a3
Gitweb:     http://git.kernel.org/tip/3578baaed4613a9fc09bab9f79f6ce2ac682e8a3
Author:     H. Peter Anvin <hpa@linux.intel.com>
AuthorDate: Tue, 12 Feb 2013 11:47:31 -0800
Committer:  H. Peter Anvin <hpa@linux.intel.com>
CommitDate: Tue, 12 Feb 2013 12:46:40 -0800

x86, mm: Redesign get_user with a __builtin_choose_expr hack

Instead of using a bitfield, use an odd little trick using typeof,
__builtin_choose_expr, and sizeof.  __builtin_choose_expr is
explicitly defined to not convert its type (its argument is required
to be a constant expression) so this should be well-defined.

The code is still not 100% preturbation-free versus the baseline
before 64-bit get_user(), but the differences seem to be very small,
mostly related to padding and to gcc deciding when to spill registers.

Cc: Jamie Lokier <jamie@shareable.org>
Cc: Ville Syrjälä <ville.syrjala@linux.intel.com>
Cc: Borislav Petkov <bp@alien8.de>
Cc: Russell King <linux@arm.linux.org.uk>
Cc: Linus Torvalds <torvalds@linux-foundation.org>
Cc: H. J. Lu <hjl.tools@gmail.com>
Link: http://lkml.kernel.org/r/511A8922.6050908@zytor.com
Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
---
 arch/x86/include/asm/uaccess.h | 57 +++++++++++-------------------------------
 1 file changed, 14 insertions(+), 43 deletions(-)

diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h
index a8d1265..d710a25 100644
--- a/arch/x86/include/asm/uaccess.h
+++ b/arch/x86/include/asm/uaccess.h
@@ -125,13 +125,12 @@ extern int __get_user_4(void);
 extern int __get_user_8(void);
 extern int __get_user_bad(void);
 
-#define __get_user_x(size, ret, x, ptr)		      \
-	asm volatile("call __get_user_" #size	      \
-		     : "=a" (ret), "=d" (x)	      \
-		     : "0" (ptr))		      \
-
-/* Careful: we have to cast the result to the type of the pointer
- * for sign reasons */
+/*
+ * This is a type: either unsigned long, if the argument fits into
+ * that type, or otherwise unsigned long long.
+ */
+#define __inttype(x) \
+__typeof__(__builtin_choose_expr(sizeof(x) > sizeof(0UL), 0ULL, 0UL))
 
 /**
  * get_user: - Get a simple variable from user space.
@@ -149,48 +148,20 @@ extern int __get_user_bad(void);
  *
  * Returns zero on success, or -EFAULT on error.
  * On error, the variable @x is set to zero.
+ *
+ * Careful: we have to cast the result to the type of the pointer
+ * for sign reasons.
  */
-#ifdef CONFIG_X86_32
-#define __get_user_8(ret, x, ptr)		      \
-do {						      \
-	register unsigned long long __xx asm("%edx"); \
-	asm volatile("call __get_user_8"	      \
-		     : "=a" (ret), "=r" (__xx)	      \
-		     : "0" (ptr));		      \
-	(x) = __xx;				      \
-} while (0)
-
-#else
-#define __get_user_8(__ret_gu, __val_gu, ptr)				\
-		__get_user_x(8, __ret_gu, __val_gu, ptr)
-#endif
-
 #define get_user(x, ptr)						\
 ({									\
 	int __ret_gu;							\
-	struct {							\
-		unsigned long long __val_n : 8*sizeof(*(ptr));		\
-	} __val_gu;							\
+	register __inttype(*(ptr)) __val_gu asm("%edx");		\
 	__chk_user_ptr(ptr);						\
 	might_fault();							\
-	switch (sizeof(*(ptr))) {					\
-	case 1:								\
-		__get_user_x(1, __ret_gu, __val_gu.__val_n, ptr);	\
-		break;							\
-	case 2:								\
-		__get_user_x(2, __ret_gu, __val_gu.__val_n, ptr);	\
-		break;							\
-	case 4:								\
-		__get_user_x(4, __ret_gu, __val_gu.__val_n, ptr);	\
-		break;							\
-	case 8:								\
-		__get_user_8(__ret_gu, __val_gu.__val_n, ptr);		\
-		break;							\
-	default:							\
-		__get_user_x(X, __ret_gu, __val_gu.__val_n, ptr);	\
-		break;							\
-	}								\
-	(x) = (__typeof__(*(ptr)))__val_gu.__val_n;			\
+	asm volatile("call __get_user_%P3"				\
+		     : "=a" (__ret_gu), "=r" (__val_gu)			\
+		     : "0" (ptr), "i" (sizeof(*(ptr))));		\
+	(x) = (__typeof__(*(ptr))) __val_gu;				\
 	__ret_gu;							\
 })
 

  parent reply	other threads:[~2013-02-12 20:56 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-12-12 11:34 [PATCH] x86: Add support for 64bit get_user() on x86-32 ville.syrjala
2012-12-12 16:15 ` H. Peter Anvin
2012-12-12 16:32   ` Ville Syrjälä
2012-12-12 16:45     ` H. Peter Anvin
2013-02-07 16:53 ` Ville Syrjälä
2013-02-07 17:59   ` H. Peter Anvin
2013-02-08 16:24     ` Ville Syrjälä
2013-02-08 17:30       ` H. Peter Anvin
2013-02-08 18:23         ` Ville Syrjälä
2013-02-08 19:08           ` H. Peter Anvin
2013-02-09 10:41             ` Borislav Petkov
2013-02-09 11:00               ` Russell King - ARM Linux
2013-02-12  1:37                 ` [tip:x86/mm] x86, mm: Use a bitfield to mask nuisance get_user() warnings tip-bot for H. Peter Anvin
2013-02-12  3:33                   ` Linus Torvalds
2013-02-12  4:21                     ` H. Peter Anvin
2013-02-12  4:42                       ` Linus Torvalds
2013-02-12  4:47                         ` Linus Torvalds
2013-02-12  4:51                           ` H. Peter Anvin
2013-02-12  7:12                           ` H. Peter Anvin
2013-02-12  8:10                       ` Ingo Molnar
2013-02-12 16:38                       ` H.J. Lu
2013-02-12 17:00                         ` Linus Torvalds
2013-02-12 17:14                           ` H. Peter Anvin
2013-02-12 17:30                             ` H.J. Lu
2013-02-12 18:25                               ` H. Peter Anvin
2013-02-12 18:29                                 ` H.J. Lu
2013-02-12 18:46                                 ` Linus Torvalds
2013-02-12 18:58                                   ` H. Peter Anvin
2013-02-12 20:55                                 ` tip-bot for H. Peter Anvin [this message]
2013-02-12 23:06                                   ` [tip:x86/mm] x86, mm: Redesign get_user with a __builtin_choose_expr hack Linus Torvalds
2013-02-12 23:19                                     ` H. Peter Anvin
2013-02-12 23:49                                       ` Linus Torvalds
2013-02-12 23:52                                         ` H. Peter Anvin
2013-02-13  0:01                                       ` [tip:x86/mm] x86, doc: Clarify the use of asm("%edx") in uaccess.h tip-bot for H. Peter Anvin
2013-02-12 23:21                                     ` [tip:x86/mm] x86, mm: Redesign get_user with a __builtin_choose_expr hack Russell King - ARM Linux
2013-02-12 17:32                             ` [tip:x86/mm] x86, mm: Use a bitfield to mask nuisance get_user() warnings Linus Torvalds
2013-02-12 17:35                               ` H. Peter Anvin
2013-02-12 17:49                                 ` Linus Torvalds
2013-02-12 17:57                               ` Russell King - ARM Linux

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=tip-3578baaed4613a9fc09bab9f79f6ce2ac682e8a3@git.kernel.org \
    --to=hpa@linux.intel.com \
    --cc=bp@alien8.de \
    --cc=hjl.tools@gmail.com \
    --cc=hpa@zytor.com \
    --cc=jamie@shareable.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-tip-commits@vger.kernel.org \
    --cc=linux@arm.linux.org.uk \
    --cc=mingo@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.org \
    --cc=ville.syrjala@linux.intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

Powered by JetHome