[PATCH v8 05/15] futex: Hash only the address for private futexes.

Sebastian Andrzej Siewior posted 15 patches 1 year ago
There is a newer version of this series
[PATCH v8 05/15] futex: Hash only the address for private futexes.
Posted by Sebastian Andrzej Siewior 1 year ago
futex_hash() passes the whole futex_key to jhash2. The first two member
are passed as the first argument and the offset as the "initial value".

For private futexes, the mm-part is always the same and it is used only
within the process. By excluding the mm part from the hash, we reduce
the length passed to jhash2 from 4 (16 / 4) to 2 (8 / 2). This avoids
the __jhash_mix() part of jhash.

The resulting code is smaller and based on testing this variant performs
as good as the original or slightly better.

Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
---
 kernel/futex/core.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/kernel/futex/core.c b/kernel/futex/core.c
index 26328d8072fee..f608cd6ccc032 100644
--- a/kernel/futex/core.c
+++ b/kernel/futex/core.c
@@ -134,8 +134,8 @@ struct futex_hash_bucket *futex_hash(union futex_key *key)
 	if (fhb && futex_key_is_private(key)) {
 		u32 hash_mask = current->mm->futex_hash_mask;
 
-		hash = jhash2((u32 *)key,
-			      offsetof(typeof(*key), both.offset) / 4,
+		hash = jhash2((void *)&key->private.address,
+			      sizeof(key->private.address) / 4,
 			      key->both.offset);
 		return &fhb[hash & hash_mask];
 	}
-- 
2.47.2
Re: [PATCH v8 05/15] futex: Hash only the address for private futexes.
Posted by Peter Zijlstra 1 year ago
On Mon, Feb 03, 2025 at 02:59:25PM +0100, Sebastian Andrzej Siewior wrote:
> futex_hash() passes the whole futex_key to jhash2. The first two member
> are passed as the first argument and the offset as the "initial value".
> 
> For private futexes, the mm-part is always the same and it is used only
> within the process. By excluding the mm part from the hash, we reduce
> the length passed to jhash2 from 4 (16 / 4) to 2 (8 / 2). This avoids
> the __jhash_mix() part of jhash.
> 
> The resulting code is smaller and based on testing this variant performs
> as good as the original or slightly better.
> 
> Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
> ---
>  kernel/futex/core.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/kernel/futex/core.c b/kernel/futex/core.c
> index 26328d8072fee..f608cd6ccc032 100644
> --- a/kernel/futex/core.c
> +++ b/kernel/futex/core.c
> @@ -134,8 +134,8 @@ struct futex_hash_bucket *futex_hash(union futex_key *key)
>  	if (fhb && futex_key_is_private(key)) {
>  		u32 hash_mask = current->mm->futex_hash_mask;
>  
> -		hash = jhash2((u32 *)key,
> -			      offsetof(typeof(*key), both.offset) / 4,
> +		hash = jhash2((void *)&key->private.address,
> +			      sizeof(key->private.address) / 4,
>  			      key->both.offset);
>  		return &fhb[hash & hash_mask];
>  	}

Like just replied to that other email, this should probably be moved
earlier and be independent of fhb.