From: Tejun Heo <tj@kernel.org>
To: Peter Zijlstra <peterz@infradead.org>,
Ingo Molnar <mingo@redhat.com>,
Linus Torvalds <torvalds@linux-foundation.org>,
Andrew Morton <akpm@linux-foundation.org>,
Chris Mason <chris.m
Cc: linux-kernel@vger.kernel.org, linux-btrfs@vger.kernel.org
Subject: Re: [PATCH 2/2] mutex: Apply adaptive spinning on mutex_trylock()
Date: Tue, 29 Mar 2011 19:09:49 +0200 [thread overview]
Message-ID: <20110329170949.GF29865@htj.dyndns.org> (raw)
In-Reply-To: <20110329163702.GE29865@htj.dyndns.org>
Here's the combined patch I was planning on testing but didn't get to
(yet). It implements two things - hard limit on spin duration and
early break if the owner also is spinning on a mutex.
Thanks.
Index: work1/include/linux/sched.h
===================================================================
--- work1.orig/include/linux/sched.h
+++ work1/include/linux/sched.h
@@ -359,7 +359,7 @@ extern signed long schedule_timeout_inte
extern signed long schedule_timeout_killable(signed long timeout);
extern signed long schedule_timeout_uninterruptible(signed long timeout);
asmlinkage void schedule(void);
-extern int mutex_spin_on_owner(struct mutex *lock, struct thread_info *owner);
+extern bool mutex_spin_on_owner(struct mutex *lock, struct thread_info *owner);
struct nsproxy;
struct user_namespace;
Index: work1/kernel/sched.c
===================================================================
--- work1.orig/kernel/sched.c
+++ work1/kernel/sched.c
@@ -536,6 +536,10 @@ struct rq {
struct hrtimer hrtick_timer;
#endif
+#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
+ bool spinning_on_mutex;
+#endif
+
#ifdef CONFIG_SCHEDSTATS
/* latency stats */
struct sched_info rq_sched_info;
@@ -4021,16 +4025,44 @@ EXPORT_SYMBOL(schedule);
#ifdef CONFIG_MUTEX_SPIN_ON_OWNER
/*
- * Look out! "owner" is an entirely speculative pointer
- * access and not reliable.
+ * Maximum mutex owner spin duration in nsecs. Don't spin more then
+ * DEF_TIMESLICE.
+ */
+#define MAX_MUTEX_SPIN_NS (DEF_TIMESLICE * 1000000000LLU / HZ)
+
+/**
+ * mutex_spin_on_owner - optimistic adaptive spinning on locked mutex
+ * @lock: the mutex to spin on
+ * @owner: the current owner (speculative pointer)
+ *
+ * The caller is trying to acquire @lock held by @owner. If @owner is
+ * currently running, it might get unlocked soon and spinning on it can
+ * save the overhead of sleeping and waking up.
+ *
+ * Note that @owner is completely speculative and may be completely
+ * invalid. It should be accessed very carefully.
+ *
+ * Forward progress is guaranteed regardless of locking ordering by never
+ * spinning longer than MAX_MUTEX_SPIN_NS. This is necessary because
+ * mutex_trylock(), which doesn't have to follow the usual locking
+ * ordering, also uses this function.
+ *
+ * CONTEXT:
+ * Preemption disabled.
+ *
+ * RETURNS:
+ * %true if the lock was released and the caller should retry locking.
+ * %false if the caller better go sleeping.
*/
-int mutex_spin_on_owner(struct mutex *lock, struct thread_info *owner)
+bool mutex_spin_on_owner(struct mutex *lock, struct thread_info *owner)
{
+ unsigned long start;
unsigned int cpu;
struct rq *rq;
+ bool ret = true;
if (!sched_feat(OWNER_SPIN))
- return 0;
+ return false;
#ifdef CONFIG_DEBUG_PAGEALLOC
/*
@@ -4039,7 +4071,7 @@ int mutex_spin_on_owner(struct mutex *lo
* the mutex owner just released it and exited.
*/
if (probe_kernel_address(&owner->cpu, cpu))
- return 0;
+ return false;
#else
cpu = owner->cpu;
#endif
@@ -4049,15 +4081,17 @@ int mutex_spin_on_owner(struct mutex *lo
* the cpu field may no longer be valid.
*/
if (cpu >= nr_cpumask_bits)
- return 0;
+ return false;
/*
* We need to validate that we can do a
* get_cpu() and that we have the percpu area.
*/
if (!cpu_online(cpu))
- return 0;
+ return false;
+ this_rq()->spinning_on_mutex = true;
+ start = local_clock();
rq = cpu_rq(cpu);
for (;;) {
@@ -4070,21 +4104,30 @@ int mutex_spin_on_owner(struct mutex *lo
* we likely have heavy contention. Return 0 to quit
* optimistic spinning and not contend further:
*/
- if (lock->owner)
- return 0;
+ ret = !lock->owner;
break;
}
/*
- * Is that owner really running on that cpu?
+ * Quit spinning if any of the followings is true.
+ *
+ * - The owner isn't running on that cpu.
+ * - The owner also is spinning on a mutex.
+ * - Someone else wants to use this cpu.
+ * - We've been spinning for too long.
*/
- if (task_thread_info(rq->curr) != owner || need_resched())
- return 0;
+ if (task_thread_info(rq->curr) != owner ||
+ rq->spinning_on_mutex || need_resched() ||
+ local_clock() > start + MAX_MUTEX_SPIN_NS) {
+ ret = false;
+ break;
+ }
arch_mutex_cpu_relax();
}
- return 1;
+ this_rq()->spinning_on_mutex = false;
+ return ret;
}
#endif
next prev parent reply other threads:[~2011-03-29 17:09 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-03-23 15:37 [RFC PATCH] mutex: Apply adaptive spinning on mutex_trylock() Tejun Heo
2011-03-23 15:40 ` Tejun Heo
2011-03-23 15:48 ` Linus Torvalds
2011-03-23 15:52 ` Tejun Heo
2011-03-23 19:46 ` Andrey Kuzmin
2011-03-24 8:18 ` Ingo Molnar
2011-03-25 3:24 ` Steven Rostedt
2011-03-25 10:29 ` Ingo Molnar
2011-03-24 9:41 ` [PATCH 1/2] Subject: mutex: Separate out mutex_spin() Tejun Heo
2011-03-24 9:41 ` [PATCH 2/2] mutex: Apply adaptive spinning on mutex_trylock() Tejun Heo
2011-03-25 3:39 ` Steven Rostedt
2011-03-25 4:38 ` Linus Torvalds
2011-03-25 6:53 ` Tejun Heo
2011-03-25 13:10 ` Steven Rostedt
2011-03-25 13:29 ` Steven Rostedt
2011-03-25 11:13 ` Andrey Kuzmin
2011-03-25 13:12 ` Steven Rostedt
2011-03-25 13:50 ` Andrey Kuzmin
2011-03-25 14:05 ` Steven Rostedt
2011-03-25 19:51 ` Ingo Molnar
2011-03-25 10:12 ` Tejun Heo
2011-03-25 10:31 ` Ingo Molnar
2011-03-29 16:37 ` Tejun Heo
2011-03-29 17:09 ` Tejun Heo [this message]
2011-03-29 17:37 ` Peter Zijlstra
2011-03-30 8:17 ` Tejun Heo
2011-03-30 11:29 ` Peter Zijlstra
2011-03-30 11:46 ` Chris Mason
2011-03-30 11:52 ` Peter Zijlstra
2011-03-30 11:59 ` Chris Mason
2011-03-24 9:42 ` [PATCH 1/2] Subject: mutex: Separate out mutex_spin() Tejun Heo
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=20110329170949.GF29865@htj.dyndns.org \
--to=tj@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=linux-btrfs@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=peterz@infradead.org \
--cc=torvalds@linux-foundation.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).