[PATCH v8 15/15] futex: Avoid allocating new local hash if there is something pending.

Sebastian Andrzej Siewior posted 15 patches 1 year ago
There is a newer version of this series
[PATCH v8 15/15] futex: Avoid allocating new local hash if there is something pending.
Posted by Sebastian Andrzej Siewior 1 year ago
If a new private hash has been prepared but not yet applied then
creating a new thread will lead to the allocation of another private
hash which will be freed later.

This is an optional optimisation to avoid allocating a new private hash
if the already prepared private hash matches the current requirement.

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

diff --git a/kernel/futex/core.c b/kernel/futex/core.c
index 9e32bfa7ba4ab..7f61f734ca5d9 100644
--- a/kernel/futex/core.c
+++ b/kernel/futex/core.c
@@ -1424,6 +1424,13 @@ static int futex_hash_allocate(unsigned int hash_slots)
 					&alloc_size)))
 		return -ENOMEM;
 
+	if (mm->futex_phash_new) {
+		scoped_guard(mutex, &mm->futex_hash_lock) {
+			if (mm->futex_phash_new && mm->futex_phash_new->hash_mask >= hash_slots - 1)
+				return 0;
+		}
+	}
+
 	hb_p = kvmalloc(alloc_size, GFP_KERNEL_ACCOUNT);
 	if (!hb_p)
 		return -ENOMEM;
-- 
2.47.2