[patch V2 3/7] futex: Add op for hash preallocation

From: Thomas Gleixner
Date: Thu May 05 2016 - 16:46:07 EST

From: Sebastian Siewior <bigeasy@xxxxxxxxxxxxx>

The per process hash is allocated on the fly at the first futex operation of a
process. The size of the hash is determined by a system wide default setting
controlled by the sys admin, This is suboptimal for RT applications and
applications with pathological futex abuse,

- For RT applications its important to allocate the per process hash before the
first futex operation to avoid the allocation on the first futex operation.

- For pathological applications which use gazillions of futexes its useful to
allocate a hash greater than the default hash size.

Add a futex op which allows to preallocate the hash with the requested
size. The size is limited by the systemwide maximum hash size, which can be
set by the admin. The requested size is rounded up to the next order of 2.

The function can be called several times, but ony the first call results in a
hash allocation of the requested size as there is no non-intrusive way to
reallocate/rehash in a multithreaded application.

Note, that this call must be issued before the first futex operation in the
process because that would automatically allocate the default sized hash.

The function returns the actual hash size or 0 if the global hash is used. The
latter is the case on UP and in the rare case that the allocation failed and
the global hash is used as a fallback.

Signed-off-by: Sebastian Siewior <bigeasy@xxxxxxxxxxxxx>
Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
include/uapi/linux/futex.h | 1 +
kernel/futex.c | 41 +++++++++++++++++++++++++++++++++++++++++
2 files changed, 42 insertions(+)

--- a/include/uapi/linux/futex.h
+++ b/include/uapi/linux/futex.h
@@ -20,6 +20,7 @@

--- a/kernel/futex.c
+++ b/kernel/futex.c
@@ -3307,6 +3307,45 @@ static void futex_populate_hash(unsigned
static inline void futex_populate_hash(unsigned int hash_bits) { }

+ * futex_preallocate_hash - Preallocate the process private hash
+ * @slots: Number of slots to allocate
+ *
+ * The function will allocate the process private hash with the number of
+ * requested slots. The number is rounded to the next power of two and may not
+ * exceed the current system limit.
+ *
+ * If the hash was already allocated by either an earlier call to
+ * futex_preallocate_hash() or an earlier futex op which allocated the cache
+ * on the fly, we return the size of the active hash.
+ *
+ * Returns:: Size of the hash, if 0 then the global hash is used.
+ */
+static int futex_preallocate_hash(unsigned int slots)
+ struct mm_struct *mm = current->mm;
+ struct futex_hash_bucket *hb;
+ unsigned int bits;
+ /* Try to allocate the requested nr of slots */
+ bits = order_base_2(slots);
+ if (bits < FUTEX_MIN_HASH_BITS)
+ if (bits > futex_max_hash_bits)
+ bits = futex_max_hash_bits;
+ futex_populate_hash(bits);
+ hb = mm->futex_hash.hash;
+ return hb == FUTEX_USE_GLOBAL_HASH ? 0 : 1 << mm->futex_hash.hash_bits;
+ return 0;
long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
u32 __user *uaddr2, u32 val2, u32 val3)
@@ -3362,6 +3401,8 @@ long do_futex(u32 __user *uaddr, int op,
return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 1);
+ return futex_preallocate_hash(val);
return -ENOSYS;