[PATCH v8 06/15] futex: Move private hashing into its own function.

Sebastian Andrzej Siewior posted 15 patches 1 year ago
There is a newer version of this series
[PATCH v8 06/15] futex: Move private hashing into its own function.
Posted by Sebastian Andrzej Siewior 1 year ago
The hashing of the private is slightly different and will be needed
again while moving a futex_q entry to a different hash bucket after the
resize.

Move the private hashing into its own function.

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

diff --git a/kernel/futex/core.c b/kernel/futex/core.c
index f608cd6ccc032..fdfc3402278a1 100644
--- a/kernel/futex/core.c
+++ b/kernel/futex/core.c
@@ -117,6 +117,18 @@ static inline bool futex_key_is_private(union futex_key *key)
 	return !(key->both.offset & (FUT_OFF_INODE | FUT_OFF_MMSHARED));
 }
 
+static struct futex_hash_bucket *futex_hash_private(union futex_key *key,
+						    struct futex_hash_bucket *fhb,
+						    u32 hash_mask)
+{
+	u32 hash;
+
+	hash = jhash2((void *)&key->private.address,
+		      sizeof(key->private.address) / 4,
+		      key->both.offset);
+	return &fhb[hash & hash_mask];
+}
+
 /**
  * futex_hash - Return the hash bucket in the global or local hash
  * @key:	Pointer to the futex key for which the hash is calculated
@@ -131,14 +143,9 @@ struct futex_hash_bucket *futex_hash(union futex_key *key)
 	u32 hash;
 
 	fhb = current->mm->futex_hash_bucket;
-	if (fhb && futex_key_is_private(key)) {
-		u32 hash_mask = current->mm->futex_hash_mask;
+	if (fhb && futex_key_is_private(key))
+		return futex_hash_private(key, fhb, current->mm->futex_hash_mask);
 
-		hash = jhash2((void *)&key->private.address,
-			      sizeof(key->private.address) / 4,
-			      key->both.offset);
-		return &fhb[hash & hash_mask];
-	}
 	hash = jhash2((u32 *)key,
 		      offsetof(typeof(*key), both.offset) / 4,
 		      key->both.offset);
-- 
2.47.2
Re: [PATCH v8 06/15] futex: Move private hashing into its own function.
Posted by Peter Zijlstra 1 year ago
On Mon, Feb 03, 2025 at 02:59:26PM +0100, Sebastian Andrzej Siewior wrote:
> The hashing of the private is slightly different and will be needed
> again while moving a futex_q entry to a different hash bucket after the
> resize.
> 
> Move the private hashing into its own function.
> 
> Signed-off-by: Sebastian Andrzej Siewior <bigeasy@linutronix.de>
> ---
>  kernel/futex/core.c | 21 ++++++++++++++-------
>  1 file changed, 14 insertions(+), 7 deletions(-)
> 
> diff --git a/kernel/futex/core.c b/kernel/futex/core.c
> index f608cd6ccc032..fdfc3402278a1 100644
> --- a/kernel/futex/core.c
> +++ b/kernel/futex/core.c
> @@ -117,6 +117,18 @@ static inline bool futex_key_is_private(union futex_key *key)
>  	return !(key->both.offset & (FUT_OFF_INODE | FUT_OFF_MMSHARED));
>  }
>  
> +static struct futex_hash_bucket *futex_hash_private(union futex_key *key,
> +						    struct futex_hash_bucket *fhb,
> +						    u32 hash_mask)
> +{
> +	u32 hash;
> +
> +	hash = jhash2((void *)&key->private.address,
> +		      sizeof(key->private.address) / 4,
> +		      key->both.offset);
> +	return &fhb[hash & hash_mask];
> +}
> +
>  /**
>   * futex_hash - Return the hash bucket in the global or local hash
>   * @key:	Pointer to the futex key for which the hash is calculated
> @@ -131,14 +143,9 @@ struct futex_hash_bucket *futex_hash(union futex_key *key)
>  	u32 hash;
>  
>  	fhb = current->mm->futex_hash_bucket;
> -	if (fhb && futex_key_is_private(key)) {
> -		u32 hash_mask = current->mm->futex_hash_mask;
> +	if (fhb && futex_key_is_private(key))
> +		return futex_hash_private(key, fhb, current->mm->futex_hash_mask);
>  
> -		hash = jhash2((void *)&key->private.address,
> -			      sizeof(key->private.address) / 4,
> -			      key->both.offset);
> -		return &fhb[hash & hash_mask];
> -	}
>  	hash = jhash2((u32 *)key,
>  		      offsetof(typeof(*key), both.offset) / 4,
>  		      key->both.offset);

Humph, this is a lot of back and forth. Maybe just do it right once?

Anyway, perhaps write it like:

static struct futex_hash_bucket *futex_hash_private(union futex_key *key)
{
	struct mm_struct *mm;
	u32 hash;

	if (!futex_key_is_private(key))
		return NULL;

	mm = key->private.mm;
	if (!mm->futex_hash_bucket)
		return NULL;

	/*
	 * Since key->private.mm is the same for all keys in this hash
	 * table, it does not contribute anything, and skipping it
	 * allows jhash2() to take a shorter/faster path.
	 */
	hash = jhash2((void *)&key->private.address,
		      sizeof(key->private.address) / 4,
		      key->both.offset);

	return mm->futex_hash_bucket[hash & mm->futex_hash_mask];
}

struct futex_hash_bucket *futex_hash(union futex_key *key)
{
	struct futex_hash_bucket *fhb;
	u32 hash;

	fhb = futex_hash_private(key);
	if (fhb)
		return fhb;

	hash = jhash2((u32 *)key, offsetof(typeof(*key), both.offset) / 4,
		      key->both.offset);
	
	return &futex_queues[hash & (futex_hashsize - 1)];
}
Re: [PATCH v8 06/15] futex: Move private hashing into its own function.
Posted by Sebastian Andrzej Siewior 1 year ago
On 2025-02-04 10:34:02 [+0100], Peter Zijlstra wrote:
> > index f608cd6ccc032..fdfc3402278a1 100644
> > --- a/kernel/futex/core.c
> > +++ b/kernel/futex/core.c
> > @@ -131,14 +143,9 @@ struct futex_hash_bucket *futex_hash(union futex_key *key)
> >  	u32 hash;
> >  
> >  	fhb = current->mm->futex_hash_bucket;
> > -	if (fhb && futex_key_is_private(key)) {
> > -		u32 hash_mask = current->mm->futex_hash_mask;
> > +	if (fhb && futex_key_is_private(key))
> > +		return futex_hash_private(key, fhb, current->mm->futex_hash_mask);
> >  
> > -		hash = jhash2((void *)&key->private.address,
> > -			      sizeof(key->private.address) / 4,
> > -			      key->both.offset);
> > -		return &fhb[hash & hash_mask];
> > -	}
> >  	hash = jhash2((u32 *)key,
> >  		      offsetof(typeof(*key), both.offset) / 4,
> >  		      key->both.offset);
> 
> Humph, this is a lot of back and forth. Maybe just do it right once?

I pulled it in little steps and I replaced it step by step.
I stayed with the "smaller" jhash2 because it is less code. We might do
something like hash_long() which is even less. I had numbers at
	https://lore.kernel.org/all/20241101110810.R3AnEqdu@linutronix.de/

But I can pull it in as suggested.

Sebastian