summaryrefslogtreecommitdiff
path: root/kernel
diff options
context:
space:
mode:
authorSebastian Andrzej Siewior <bigeasy@linutronix.de>2026-07-01 18:17:36 +0200
committerPeter Zijlstra <peterz@infradead.org>2026-07-29 13:17:52 +0200
commit5e601ab3615c86be7c4068ce992f94654693a032 (patch)
tree215e3ecf7ed2eea102786690229bcaf97f2ab1ff /kernel
parentb78b0b65825275f58336a43611a700de174be8c3 (diff)
futex: Optimise the size check get_futex_key()
The futex address must be naturally aligned and this is checked via "address % size" where `address' is the supplied address and `size' is the expected size of futex. It is guaranteed that `size' is power of two but the compiler does not see it and creates here a `div' operation (x86, arm, gcc-15). We can take advantage of the pow2 property and rewrite it as "address & (size-1)". As per testing, the command |perf bench futex hash -f 1 -b 16384 -t 1 -r 30 improved from | [thread 0] futex: 0x5619f931f740 [ 7001583 ops/sec ] to | [thread 0] futex: 0x55da173e5740 [ 7376137 ops/sec ] or by 5.3% Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Link: https://patch.msgid.link/20260701161736.xYYizA0e@linutronix.de
Diffstat (limited to 'kernel')
-rw-r--r--kernel/futex/core.c2
1 files changed, 1 insertions, 1 deletions
diff --git a/kernel/futex/core.c b/kernel/futex/core.c
index 3bf6cb79f71a..0864c6e8cde7 100644
--- a/kernel/futex/core.c
+++ b/kernel/futex/core.c
@@ -516,7 +516,7 @@ int get_futex_key(u32 __user *uaddr, unsigned int flags, union futex_key *key,
* The futex address must be "naturally" aligned.
*/
key->both.offset = address % PAGE_SIZE;
- if (unlikely((address % size) != 0))
+ if (unlikely((address & (size-1)) != 0))
return -EINVAL;
address -= key->both.offset;