From: Rasmus Villemoes <linux@rasmusvillemoes.dk>
To: Thomas Gleixner <tglx@linutronix.de>,
Davidlohr Bueso <dave@stgolabs.net>,
kbuild test robot <fengguang.wu@intel.com>,
Sebastian Andrzej Siewior <bigeasy@linutronix.de>
Cc: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@kernel.org>,
Rasmus Villemoes <linux@rasmusvillemoes.dk>,
linux-kernel@vger.kernel.org
Subject: [PATCH] futex: eliminate cache miss from futex_hash()
Date: Wed, 9 Sep 2015 23:36:40 +0200 [thread overview]
Message-ID: <1441834601-13633-1-git-send-email-linux@rasmusvillemoes.dk> (raw)
futex_hash() references two global variables: the base pointer
futex_queues and the size of the array futex_hashsize. The latter is
marked __read_mostly, while the former is not, so they are likely to
end up very far from each other. This means that futex_hash() is
likely to encounter two cache misses.
We could mark futex_queues as __read_mostly as well, but that doesn't
guarantee they'll end up next to each other (and even if they do, they
may still end up in different cache lines). So put the two variables
in a small singleton struct with sufficient alignment and mark that as
__read_mostly.
A diff of the disassembly shows what I'd expect:
: 31 d1 xor %edx,%ecx
: c1 ca 12 ror $0x12,%edx
: 29 d1 sub %edx,%ecx
-: 48 8b 15 25 c8 e5 00 mov 0xe5c825(%rip),%rdx # ffffffff81f149c8 <futex_hashsize>
+: 48 8b 15 35 c8 e5 00 mov 0xe5c835(%rip),%rdx # ffffffff81f149d8 <__futex_data+0x8>
: 31 c8 xor %ecx,%eax
: c1 c9 08 ror $0x8,%ecx
: 29 c8 sub %ecx,%eax
: 48 83 ea 01 sub $0x1,%rdx
: 48 21 d0 and %rdx,%rax
: 48 c1 e0 06 shl $0x6,%rax
-: 48 03 05 e4 5e 02 01 add 0x1025ee4(%rip),%rax # ffffffff820de0a0 <futex_queues>
+: 48 03 05 14 c8 e5 00 add 0xe5c814(%rip),%rax # ffffffff81f149d0 <__futex_data>
: c3 retq
: 0f 1f 00 nopl (%rax)
Signed-off-by: Rasmus Villemoes <linux@rasmusvillemoes.dk>
---
Resending since this was never picked up - and I assume it's actually
ok. Also, this time the alignment is spelled 2*sizeof(long) to avoid
wasting 8 bytes on 32bit.
kernel/futex.c | 13 +++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)
diff --git a/kernel/futex.c b/kernel/futex.c
index 6e443efc65f4..dfc86e93c31d 100644
--- a/kernel/futex.c
+++ b/kernel/futex.c
@@ -255,9 +255,18 @@ struct futex_hash_bucket {
struct plist_head chain;
} ____cacheline_aligned_in_smp;
-static unsigned long __read_mostly futex_hashsize;
+/*
+ * The base of the bucket array and its size are always used together
+ * (after initialization only in hash_futex()), so ensure that they
+ * reside in the same cacheline.
+ */
+static struct {
+ struct futex_hash_bucket *queues;
+ unsigned long hashsize;
+} __futex_data __read_mostly __aligned(2*sizeof(long));
+#define futex_queues (__futex_data.queues)
+#define futex_hashsize (__futex_data.hashsize)
-static struct futex_hash_bucket *futex_queues;
/*
* Fault injections for futexes.
--
2.1.3
next reply other threads:[~2015-09-09 21:36 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-09-09 21:36 Rasmus Villemoes [this message]
2015-09-10 10:22 ` Davidlohr Bueso
2015-09-12 9:59 ` Ingo Molnar
2015-10-26 15:22 ` Sebastian Andrzej Siewior
2015-09-22 14:27 ` [tip:locking/core] futex: Force hot variables into a single cache line tip-bot for Rasmus Villemoes
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=1441834601-13633-1-git-send-email-linux@rasmusvillemoes.dk \
--to=linux@rasmusvillemoes.dk \
--cc=bigeasy@linutronix.de \
--cc=dave@stgolabs.net \
--cc=fengguang.wu@intel.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=peterz@infradead.org \
--cc=tglx@linutronix.de \
/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
all inboxes | Powered by JetHome®