From: Waiman Long <longman@redhat.com>
To: Thomas Gleixner <tglx@linutronix.de>,
Ingo Molnar <mingo@kernel.org>,
Peter Zijlstra <peterz@infradead.org>,
Jonathan Corbet <corbet@lwn.net>
Cc: linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org,
Arnaldo Carvalho de Melo <acme@kernel.org>,
Davidlohr Bueso <dave@stgolabs.net>,
Mike Galbraith <umgwanakikbuti@gmail.com>,
Scott J Norton <scott.norton@hpe.com>,
Waiman Long <longman@redhat.com>
Subject: [PATCH-tip v6 17/22] TP-futex: Enable kernel reader lock stealing
Date: Wed, 22 Mar 2017 13:38:53 -0400 [thread overview]
Message-ID: <1490204338-1856-18-git-send-email-longman@redhat.com> (raw)
In-Reply-To: <1490204338-1856-1-git-send-email-longman@redhat.com>
By default, the TP futexes do not have preference for either readers
or writers. Most reader-writer locks allows users to decide if they
want to prefer readers or writers more.
This patch allows the setting of the prefer-reader mode in the val
argument of the futex system call. If that flag is set, it will
enable kernel reader to steal the lock when the futex is currently
reader-owned and the lock handoff mechanism hasn't been enabled yet.
Signed-off-by: Waiman Long <longman@redhat.com>
---
kernel/futex.c | 20 ++++++++++++++++++--
1 file changed, 18 insertions(+), 2 deletions(-)
diff --git a/kernel/futex.c b/kernel/futex.c
index 4139843..cacaaf1 100644
--- a/kernel/futex.c
+++ b/kernel/futex.c
@@ -197,6 +197,7 @@
#define FLAGS_CLOCKRT 0x02
#define FLAGS_HAS_TIMEOUT 0x04
#define FLAGS_TP_USLOCK 0x08 /* Do the locking in userspace */
+#define FLAGS_TP_PREADER 0x08 /* Prefer readers */
enum futex_type {
TYPE_PI = 0,
@@ -4004,6 +4005,20 @@ static noinline int futex_lock(u32 __user *uaddr, unsigned int flags,
goto out_put_state_key;
}
+ /*
+ * For reader, we will try to steal the lock here as if it is the
+ * top waiter without taking the serialization mutex if the handoff
+ * PID hasn't been set and is in prefer-reader mode.
+ */
+ if (shared && (flags & FLAGS_TP_PREADER) && !state->handoff_pid) {
+ ret = futex_trylock(uaddr, vpid, &uval, true);
+ if (ret) {
+ if (ret > 0)
+ ret = TP_LOCK_STOLEN;
+ goto out_put_state_key;
+ }
+ }
+
if (to)
hrtimer_start_expires(&to->timer, HRTIMER_MODE_ABS);
@@ -4219,8 +4234,9 @@ long do_futex(u32 __user *uaddr, int op, u32 val, ktime_t *timeout,
#ifdef CONFIG_SMP
case FUTEX_LOCK:
case FUTEX_LOCK_SHARED:
- if (val && (cmd == FUTEX_LOCK))
- flags |= FLAGS_TP_USLOCK;
+ if (val)
+ flags |= (cmd == FUTEX_LOCK) ? FLAGS_TP_USLOCK
+ : FLAGS_TP_PREADER;
return futex_lock(uaddr, flags, timeout,
(cmd == FUTEX_LOCK) ? false : true);
case FUTEX_UNLOCK:
--
1.8.3.1
next prev parent reply other threads:[~2017-03-22 17:43 UTC|newest]
Thread overview: 25+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-03-22 17:38 [PATCH-tip v6 00/22] futex: Introducing throughput-optimized (TP) futexes Waiman Long
2017-03-22 17:38 ` [PATCH-tip v6 01/22] perf bench: New microbenchmark for userspace mutex performance Waiman Long
2017-03-22 17:38 ` [PATCH-tip v6 02/22] perf bench: New microbenchmark for userspace rwlock performance Waiman Long
2017-03-22 17:38 ` [PATCH-tip v6 03/22] futex: Consolidate duplicated timer setup code Waiman Long
2017-03-22 17:38 ` [PATCH-tip v6 04/22] futex: Rename futex_pi_state to futex_state Waiman Long
2017-03-22 17:38 ` [PATCH-tip v6 05/22] futex: Add helpers to get & cmpxchg futex value without lock Waiman Long
2017-03-22 17:38 ` [PATCH-tip v6 06/22] futex: Consolidate pure pi_state_list add & delete codes to helpers Waiman Long
2017-03-22 17:38 ` [PATCH-tip v6 07/22] futex: Add a new futex type field into futex_state Waiman Long
2017-03-22 17:38 ` [PATCH-tip v6 08/22] futex: Allow direct attachment of futex_state objects to hash bucket Waiman Long
2017-03-22 17:38 ` [PATCH-tip v6 09/22] futex: Introduce throughput-optimized (TP) futexes Waiman Long
2017-03-22 17:38 ` [PATCH-tip v6 10/22] TP-futex: Enable robust handling Waiman Long
2017-03-22 17:38 ` [PATCH-tip v6 11/22] TP-futex: Implement lock handoff to prevent lock starvation Waiman Long
2017-03-22 17:38 ` [PATCH-tip v6 12/22] TP-futex: Return status code on FUTEX_LOCK calls Waiman Long
2017-03-22 17:38 ` [PATCH-tip v6 13/22] TP-futex: Add timeout support Waiman Long
2017-03-22 17:38 ` [PATCH-tip v6 14/22] TP-futex: Optionally return EAGAIN for userspace locking Waiman Long
2017-03-22 17:38 ` [PATCH-tip v6 15/22] TP-futex, doc: Add TP futexes documentation Waiman Long
2017-03-22 17:38 ` [PATCH-tip v6 16/22] TP-futex: Support userspace reader/writer locks Waiman Long
2017-03-22 17:38 ` Waiman Long [this message]
2017-03-22 17:38 ` [PATCH-tip v6 18/22] TP-futex: Group readers together in wait queue Waiman Long
2017-03-24 8:19 ` kbuild test robot
2017-03-24 8:28 ` kbuild test robot
2017-03-22 17:38 ` [PATCH-tip v6 19/22] TP-futex, doc: Update TP futexes document on shared locking Waiman Long
2017-03-22 17:38 ` [PATCH-tip v6 20/22] perf bench: Extend mutex/rwlock futex suite to test TP futexes Waiman Long
2017-03-22 17:38 ` [PATCH-tip v6 21/22] sched, TP-futex: Make wake_up_q() return wakeup count Waiman Long
2017-03-22 17:38 ` [PATCH-tip v6 22/22] futex: Dump internal futex state via debugfs Waiman Long
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=1490204338-1856-18-git-send-email-longman@redhat.com \
--to=longman@redhat.com \
--cc=acme@kernel.org \
--cc=corbet@lwn.net \
--cc=dave@stgolabs.net \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@kernel.org \
--cc=peterz@infradead.org \
--cc=scott.norton@hpe.com \
--cc=tglx@linutronix.de \
--cc=umgwanakikbuti@gmail.com \
/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®