Skip to contentSkip to conversation

64-bit futexes: generic code

PATCH In review Ulrich Drepper 1 participant 1 message May 31, 2008

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/3 64-bit futexes: generic code

This patch adds the architecture independent changes. For this it has to adjust the futex_atomic_op_inuser function in arch-specific code but this in no real code change. That function is inlined for most architectures.

The real changes depend on the definition of a single type: futex_val_t. If the arcitecture defines this type to something at least as large as u64 then all the 64-bit futex code is automagically enabled. If the type is not defined sufficiently using the FUTEX_64_FLAG flag triggers an ENOSYS error.

We already have a flag supported for futexes, the private flag. When it was introduced a pointer to a semaphore for the memory handling was passed around. I changed that. Now the flags are passed in a word and the semaphore is located in every function. This might have added a minute fraction of code but

- there is no runtime difference for this since the semaphore address is still only computed once

- the compiler now has more insight into when no semaphore is used and it can perform more optimization (see the futex_lock_mm definition).

- the restart handling is cleaned up a bit as well, as a consequence

The overall code size increase is minute, as can be guessed from the diffstat numbers below. Must of the changes are changes to existing code and not new code.

 arch/frv/kernel/futex.c     |    2 
 include/asm-frv/futex.h     |    2 
 include/asm-generic/futex.h |    2 
 include/asm-ia64/futex.h    |    2 
 include/asm-mips/futex.h    |    2 
 include/asm-parisc/futex.h  |    2 
 include/asm-powerpc/futex.h |    3 
 include/asm-s390/futex.h    |    3 
 include/asm-sh/futex.h      |    3 
 include/asm-sparc64/futex.h |    3 
 include/asm-x86/futex.h     |    3 
 include/linux/futex.h       |   16 +++
 include/linux/syscalls.h    |    6 -
 include/linux/thread_info.h |    6 -
 include/linux/types.h       |    4 
 kernel/futex.c              |  180 +++++++++++++++++++++++++-------------------
 kernel/futex_compat.c       |    5 +
 17 files changed, 149 insertions(+), 95 deletions(-)
arch/frv/kernel/futex.c +1 -1
index 14f64b0..272cbd5 100644
@@ -186,7 +186,7 @@ static inline int atomic_futex_op_xchg_xor(int oparg, int __user *uaddr, int *_o
 /*
  * do the futex operations
  */
-int futex_atomic_op_inuser(int encoded_op, int __user *uaddr)
+int futex_atomic_op_inuser(int encoded_op, int __user *uaddr, int flags)
 {
 	int op = (encoded_op >> 28) & 7;
 	int cmp = (encoded_op >> 24) & 15;
include/asm-frv/futex.h +1 -1
index 08b3d1d..4b4579e 100644
@@ -7,7 +7,7 @@
 #include <asm/errno.h>
 #include <asm/uaccess.h>
 
-extern int futex_atomic_op_inuser(int encoded_op, int __user *uaddr);
+extern int futex_atomic_op_inuser(int encoded_op, int __user *uaddr, int flags);
 
 static inline int
 futex_atomic_cmpxchg_inatomic(int __user *uaddr, int oldval, int newval)
include/asm-generic/futex.h +1 -1
index 3c2344f..3bc5c6a 100644
@@ -6,7 +6,7 @@
 #include <asm/errno.h>
 
 static inline int
-futex_atomic_op_inuser (int encoded_op, int __user *uaddr)
+futex_atomic_op_inuser (int encoded_op, int __user *uaddr, int flags)
 {
 	int op = (encoded_op >> 28) & 7;
 	int cmp = (encoded_op >> 24) & 15;
include/asm-ia64/futex.h +1 -1
index c7f0f06..aedc609 100644
@@ -46,7 +46,7 @@ do {									\
 } while (0)
 
 static inline int
-futex_atomic_op_inuser (int encoded_op, int __user *uaddr)
+futex_atomic_op_inuser (int encoded_op, int __user *uaddr, int flags)
 {
 	int op = (encoded_op >> 28) & 7;
 	int cmp = (encoded_op >> 24) & 15;
include/asm-mips/futex.h +1 -1
index b9cce90..9fc8ab2 100644
@@ -75,7 +75,7 @@
 }
 
 static inline int
-futex_atomic_op_inuser(int encoded_op, int __user *uaddr)
+futex_atomic_op_inuser(int encoded_op, int __user *uaddr, int flags)
 {
 	int op = (encoded_op >> 28) & 7;
 	int cmp = (encoded_op >> 24) & 15;
include/asm-parisc/futex.h +1 -1
index 0c705c3..b485a89 100644
@@ -8,7 +8,7 @@
 #include <asm/errno.h>
 
 static inline int
-futex_atomic_op_inuser (int encoded_op, int __user *uaddr)
+futex_atomic_op_inuser (int encoded_op, int __user *uaddr, int flags)
 {
 	int op = (encoded_op >> 28) & 7;
 	int cmp = (encoded_op >> 24) & 15;
include/asm-powerpc/futex.h +2 -1
index 6d406c5..b830055 100644
@@ -30,7 +30,8 @@
 	: "b" (uaddr), "i" (-EFAULT), "1" (oparg) \
 	: "cr0", "memory")
 
-static inline int futex_atomic_op_inuser (int encoded_op, int __user *uaddr)
+static inline int futex_atomic_op_inuser (int encoded_op, int __user *uaddr,
+					  int flags)
 {
 	int op = (encoded_op >> 28) & 7;
 	int cmp = (encoded_op >> 24) & 15;
include/asm-s390/futex.h +2 -1
index 5c5d02d..81cb5cf 100644
@@ -7,7 +7,8 @@
 #include <linux/uaccess.h>
 #include <asm/errno.h>
 
-static inline int futex_atomic_op_inuser (int encoded_op, int __user *uaddr)
+static inline int futex_atomic_op_inuser (int encoded_op, int __user *uaddr,
+					  int flags)
 {
 	int op = (encoded_op >> 28) & 7;
 	int cmp = (encoded_op >> 24) & 15;
include/asm-sh/futex.h +2 -1
index 68256ec..70656d0 100644
@@ -10,7 +10,8 @@
 /* XXX: UP variants, fix for SH-4A and SMP.. */
 #include <asm/futex-irq.h>
 
-static inline int futex_atomic_op_inuser(int encoded_op, int __user *uaddr)
+static inline int futex_atomic_op_inuser(int encoded_op, int __user *uaddr,
+					 int flags)
 {
 	int op = (encoded_op >> 28) & 7;
 	int cmp = (encoded_op >> 24) & 15;
include/asm-sparc64/futex.h +2 -1
index d837893..235dd57 100644
@@ -30,7 +30,8 @@
 	: "r" (uaddr), "r" (oparg), "i" (-EFAULT)	\
 	: "memory")
 
-static inline int futex_atomic_op_inuser(int encoded_op, int __user *uaddr)
+static inline int futex_atomic_op_inuser(int encoded_op, int __user *uaddr,
+					 int flags)
 {
 	int op = (encoded_op >> 28) & 7;
 	int cmp = (encoded_op >> 24) & 15;
include/asm-x86/futex.h +2 -1
index e7a76b3..7c53d4c 100644
@@ -37,7 +37,8 @@
 		       "+m" (*uaddr), "=&r" (tem)		\
 		     : "r" (oparg), "i" (-EFAULT), "1" (0))
 
-static inline int futex_atomic_op_inuser(int encoded_op, int __user *uaddr)
+static inline int futex_atomic_op_inuser(int encoded_op, int __user *uaddr,
+					 int flags)
 {
 	int op = (encoded_op >> 28) & 7;
 	int cmp = (encoded_op >> 24) & 15;
include/linux/futex.h +13 -3
index 586ab56..1658df4 100644
@@ -25,7 +25,8 @@ union ktime;
 #define FUTEX_WAKE_BITSET	10
 
 #define FUTEX_PRIVATE_FLAG	128
-#define FUTEX_CMD_MASK		~FUTEX_PRIVATE_FLAG
+#define FUTEX_64_FLAG		256
+#define FUTEX_CMD_MASK		~(FUTEX_PRIVATE_FLAG | FUTEX_64_FLAG)
 
 #define FUTEX_WAIT_PRIVATE	(FUTEX_WAIT | FUTEX_PRIVATE_FLAG)
 #define FUTEX_WAKE_PRIVATE	(FUTEX_WAKE | FUTEX_PRIVATE_FLAG)
@@ -38,6 +39,9 @@ union ktime;
 #define FUTEX_WAIT_BITSET_PRIVATE	(FUTEX_WAIT_BITS | FUTEX_PRIVATE_FLAG)
 #define FUTEX_WAKE_BITSET_PRIVATE	(FUTEX_WAKE_BITS | FUTEX_PRIVATE_FLAG)
 
+#define FUTEX_64_P(op) \
+	(sizeof(futex_val_t) >= sizeof(u64) && (op & FUTEX_64_FLAG))
+
 /*
  * Support for robust futexes: the kernel cleans up held futexes at
  * thread exit time.
@@ -122,8 +126,8 @@ struct robust_list_head {
 #define FUTEX_BITSET_MATCH_ANY	0xffffffff
 
 #ifdef __KERNEL__
-long do_futex(u32 __user *uaddr, int op, u32 val, union ktime *timeout,
-	      u32 __user *uaddr2, u32 val2, u32 val3);
+long do_futex(void __user *uaddr, int op, futex_val_t val, ktime_t *timeout,
+	      void __user *uaddr2, u32 val2, futex_val_t val3);
 
 extern int
 handle_futex_death(u32 __user *uaddr, struct task_struct *curr, int pi);
@@ -183,6 +187,12 @@ static inline void exit_pi_state_list(struct task_struct *curr)
 #define FUTEX_OP_OR		2	/* *(int *)UADDR2 |= OPARG; */
 #define FUTEX_OP_ANDN		3	/* *(int *)UADDR2 &= ~OPARG; */
 #define FUTEX_OP_XOR		4	/* *(int *)UADDR2 ^= OPARG; */
+/* Similar for 64-bit futexes (if supported).  */
+#define FUTEX_OP64_SET		(FUTEX_OP_SET | 8)
+#define FUTEX_OP64_ADD		(FUTEX_OP_ADD | 8)
+#define FUTEX_OP64_OR		(FUTEX_OP_OR | 8)
+#define FUTEX_OP64_ANDN		(FUTEX_OP_ANDN | 8)
+#define FUTEX_OP64_XOR		(FUTEX_OP_XOR | 8)
 
 #define FUTEX_OP_OPARG_SHIFT	8	/* Use (1 << OPARG) instead of OPARG.  */
 
include/linux/syscalls.h +3 -3
index 0522f36..9744533 100644
@@ -174,9 +174,9 @@ asmlinkage long sys_waitid(int which, pid_t pid,
 			   int options, struct rusage __user *ru);
 asmlinkage long sys_waitpid(pid_t pid, int __user *stat_addr, int options);
 asmlinkage long sys_set_tid_address(int __user *tidptr);
-asmlinkage long sys_futex(u32 __user *uaddr, int op, u32 val,
-			struct timespec __user *utime, u32 __user *uaddr2,
-			u32 val3);
+asmlinkage long sys_futex(void __user *uaddr, int op, unsigned long val,
+			struct timespec __user *utime, void __user *uaddr2,
+			unsigned long val3);
 
 asmlinkage long sys_init_module(void __user *umod, unsigned long len,
 				const char __user *uargs);
include/linux/thread_info.h +3 -3
index 38a5647..02bfd14 100644
@@ -23,9 +23,9 @@ struct restart_block {
 		};
 		/* For futex_wait */
 		struct {
-			u32 *uaddr;
-			u32 val;
-			u32 flags;
+			void __user *uaddr;
+			futex_val_t val;
+			int flags;
 			u32 bitset;
 			u64 time;
 		} futex;
include/linux/types.h +4 -0
index d4a9ce6..32681fb 100644
@@ -206,4 +206,8 @@ struct ustat {
 
 #endif	/* __KERNEL__ */
 
+#ifndef __have_futex_val_t
+typedef u32 futex_val_t;
+#endif
+
 #endif /* _LINUX_TYPES_H */
kernel/futex.c +105 -75

458 lines not on this page · raw message

kernel/futex_compat.c +5 -0
index 04ac3a9..cc36d3b 100644
@@ -176,6 +176,11 @@ asmlinkage long compat_sys_futex(u32 __user *uaddr, int op, u32 val,
 	int val2 = 0;
 	int cmd = op & FUTEX_CMD_MASK;
 
+	/* The compatibility interface is used for 32-bit entry points.
+	   Therefore it is not possible to support 64-bit futexes here.  */
+	if (op & FUTEX_64_FLAG)
+		return -ENOSYS;
+
 	if (utime && (cmd == FUTEX_WAIT || cmd == FUTEX_LOCK_PI ||
 		      cmd == FUTEX_WAIT_BITSET)) {
 		if (get_compat_timespec(&ts, utime))