From: Pan Xinhui <xinhui.pan@linux.vnet.ibm.com>
To: linux-kernel@vger.kernel.org
Cc: peterz@infradead.org, mingo@redhat.com, Waiman.Long@hpe.com,
Pan Xinhui <xinhui.pan@linux.vnet.ibm.com>
Subject: [PATCH] pv-qspinlock: Try to re-hash the lock after spurious_wakeup
Date: Wed, 25 May 2016 14:09:35 +0800 [thread overview]
Message-ID: <1464156575-4732-1-git-send-email-xinhui.pan@linux.vnet.ibm.com> (raw)
In pv_wait_head_or_lock, if there is a spurious_wakeup, and it fails to
get the lock as there is lock stealing, then after a short spin, we need
hash the lock again and enter pv_wait to yield.
Currently after a spurious_wakeup, as l->locked is not _Q_SLOW_VAL,
pv_wait might do nothing and return directly, that is not
paravirt-friendly because pv_wait_head_or_lock will just spin on the
lock then.
Signed-off-by: Pan Xinhui <xinhui.pan@linux.vnet.ibm.com>
---
kernel/locking/qspinlock_paravirt.h | 39 +++++++++++++++++++++++++++++--------
1 file changed, 31 insertions(+), 8 deletions(-)
diff --git a/kernel/locking/qspinlock_paravirt.h b/kernel/locking/qspinlock_paravirt.h
index 2bbffe4..3482ce9 100644
--- a/kernel/locking/qspinlock_paravirt.h
+++ b/kernel/locking/qspinlock_paravirt.h
@@ -429,14 +429,15 @@ static void pv_kick_node(struct qspinlock *lock, struct mcs_spinlock *node)
return;
/*
- * Put the lock into the hash table and set the _Q_SLOW_VAL.
- *
- * As this is the same vCPU that will check the _Q_SLOW_VAL value and
- * the hash table later on at unlock time, no atomic instruction is
- * needed.
+ * Put the lock into the hash table and set the _Q_SLOW_VAL later
*/
- WRITE_ONCE(l->locked, _Q_SLOW_VAL);
(void)pv_hash(lock, pn);
+
+ /*
+ * Match the smp_load_acquire in pv_wait_head_or_lock()
+ * We mush set the _Q_SLOW_VAL after hash.
+ */
+ smp_store_release(&l->locked, _Q_SLOW_VAL);
}
/*
@@ -454,6 +455,7 @@ pv_wait_head_or_lock(struct qspinlock *lock, struct mcs_spinlock *node)
struct qspinlock **lp = NULL;
int waitcnt = 0;
int loop;
+ u8 lock_val;
/*
* If pv_kick_node() already advanced our state, we don't need to
@@ -487,7 +489,7 @@ pv_wait_head_or_lock(struct qspinlock *lock, struct mcs_spinlock *node)
clear_pending(lock);
- if (!lp) { /* ONCE */
+ if (!lp) {
lp = pv_hash(lock, pn);
/*
@@ -517,6 +519,13 @@ pv_wait_head_or_lock(struct qspinlock *lock, struct mcs_spinlock *node)
qstat_inc(qstat_pv_wait_again, waitcnt);
/*
+ * make sure pv_kick_node has hashed the lock, so after pv_wait
+ * if ->locked is not _Q_SLOW_VAL, we can hash the lock again.
+ */
+ if (lp == (struct qspinlock **)1
+ && smp_load_acquire(&l->locked) == _Q_SLOW_VAL)
+ lp = (struct qspinlock **)2;
+ /*
* Pass in the previous node vCPU nmber which is likely to be
* the lock holder vCPU. This additional information may help
* the hypervisor to give more resource to that vCPU so that
@@ -525,13 +534,27 @@ pv_wait_head_or_lock(struct qspinlock *lock, struct mcs_spinlock *node)
*/
pv_wait(&l->locked, _Q_SLOW_VAL, pn->prev_cpu);
+ lock_val = READ_ONCE(l->locked);
+
+ /* if ->locked is zero, then lock owner has unhashed the lock
+ * if ->locked is _Q_LOCKED_VAL,
+ * 1) pv_kick_node didnot hash the lock, and lp != 0x1
+ * 2) lock stealing, lock owner has unhashed the lock too
+ * 3) race with pv_kick_node, if lp == 2, we know it has hashed
+ * the lock and the lock is unhashed in unlock()
+ * if ->lock is _Q_SLOW_VAL, spurious_wakeup?
+ */
+ if (lock_val != _Q_SLOW_VAL) {
+ if (lock_val == 0 || lp != (struct qspinlock **)1)
+ lp = 0;
+ }
/*
* The unlocker should have freed the lock before kicking the
* CPU. So if the lock is still not free, it is a spurious
* wakeup or another vCPU has stolen the lock. The current
* vCPU should spin again.
*/
- qstat_inc(qstat_pv_spurious_wakeup, READ_ONCE(l->locked));
+ qstat_inc(qstat_pv_spurious_wakeup, lock_val);
}
/*
--
1.9.1
next reply other threads:[~2016-05-25 6:11 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-05-25 6:09 Pan Xinhui [this message]
2016-05-26 18:31 ` Waiman Long
2016-05-27 10:32 ` xinhui
2016-05-28 3:41 ` Waiman Long
2016-05-30 8:53 ` xinhui
[not found] ` <201605300855.u4U8sLm5005684@mx0a-001b2d01.pphosted.com>
2016-05-31 18:13 ` Waiman Long
2016-06-01 5:54 ` xinhui
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=1464156575-4732-1-git-send-email-xinhui.pan@linux.vnet.ibm.com \
--to=xinhui.pan@linux.vnet.ibm.com \
--cc=Waiman.Long@hpe.com \
--cc=linux-kernel@vger.kernel.org \
--cc=mingo@redhat.com \
--cc=peterz@infradead.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
all inboxes | Powered by JetHome®