From: Thomas Gleixner <tglx@linutronix.de>
To: LKML <linux-kernel@vger.kernel.org>
Cc: Sebastian Siewior <bigeasy@linutronix.de>
Subject: [patch V2 3/7] futex: Add op for hash preallocation
Date: Thu, 05 May 2016 19:03:38 -0000 [thread overview]
Message-ID: <20160505170920.131816538@linutronix.de> (raw)
In-Reply-To: 20160505170339.026555108@linutronix.de
[-- Attachment #1: futex-Add-op-for-hash-preallocation.patch --]
[-- Type: text/plain, Size: 3659 bytes --]
From: Sebastian Siewior <bigeasy@linutronix.de>
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@linutronix.de>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
---
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 @@
#define FUTEX_WAKE_BITSET 10
#define FUTEX_WAIT_REQUEUE_PI 11
#define FUTEX_CMP_REQUEUE_PI 12
+#define FUTEX_PREALLOC_HASH 13
#define FUTEX_PRIVATE_FLAG 128
#define FUTEX_CLOCK_REALTIME 256
--- 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) { }
#endif
+/**
+ * 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)
+{
+#ifdef CONFIG_FUTEX_PRIVATE_HASH
+ 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)
+ 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;
+#else
+ return 0;
+#endif
+}
+
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,
uaddr2);
case FUTEX_CMP_REQUEUE_PI:
return futex_requeue(uaddr, flags, uaddr2, val, val2, &val3, 1);
+ case FUTEX_PREALLOC_HASH:
+ return futex_preallocate_hash(val);
}
return -ENOSYS;
}
next prev parent reply other threads:[~2016-05-05 19:06 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-05-05 19:03 [patch V2 0/7] Sebastian Andrzej Siewior <bigeasy@linutronix.de>, Linus Torvalds <torvalds@linux-foundation.org>, Darren Hart <darren@dvhart.com>, Peter Zijlstra <peterz@infradead.org>, Ingo Molnar <mingo@kernel.org>, Michael Kerrisk <mtk.manpages@googlemail.com>, Davidlohr Bueso <dave@stgolabs.net>, Chris Mason <clm@fb.com>, Carlos O'Donell <carlos@redhat.com>, Torvald Riegel <triegel@redhat.com>, Eric Dumazet <edumazet@google.com> Thomas Gleixner
2016-05-05 19:03 ` [patch V2 2/7] futex: Hash private futexes per process Thomas Gleixner
2016-05-05 19:03 ` [patch V2 1/7] futex: Add some more function commentry Thomas Gleixner
2016-05-05 19:03 ` Thomas Gleixner [this message]
2016-05-05 19:03 ` [patch V2 4/7] futex: Add sysctl knobs for process private hash Thomas Gleixner
2016-05-05 19:03 ` [patch V2 6/7] perf/bench/futex-hash: Support preallocate hash table Thomas Gleixner
2016-05-05 19:03 ` [patch V2 5/7] perf/bench/futex-hash: Support NUMA Thomas Gleixner
2016-05-05 19:03 ` [patch V2 7/7] futex.2: Document hash preallocation opcode Thomas Gleixner
-- strict thread matches above, loose matches on Subject: below --
2016-05-05 20:44 [patch V2 0/7] futex: Add support for process private hashing Thomas Gleixner
2016-05-05 20:44 ` [patch V2 3/7] futex: Add op for hash preallocation Thomas Gleixner
2016-05-06 18:18 ` Darren Hart
2016-05-07 8:47 ` Thomas Gleixner
2016-05-07 11:40 ` Thomas Gleixner
2016-05-19 12:28 ` Peter Zijlstra
2016-05-19 19:36 ` Darren Hart
2016-05-19 12:24 ` Peter Zijlstra
2016-05-19 19:38 ` Darren Hart
2016-05-20 4:50 ` Peter Zijlstra
2016-05-19 12:25 ` Peter Zijlstra
2016-05-27 17:27 ` Sebastian Andrzej Siewior
2016-05-30 8:59 ` Peter Zijlstra
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20160505170920.131816538@linutronix.de \
--to=tglx@linutronix.de \
--cc=bigeasy@linutronix.de \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).