LKML Archive on lore.kernel.org
 help / color / Atom feed
From: Peter Zijlstra <peterz@infradead.org>
To: Waiman Long <longman@redhat.com>
Cc: Ingo Molnar <mingo@redhat.com>, Will Deacon <will.deacon@arm.com>,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH] locking/osq: Use more optimized spinning for arm64
Date: Fri, 10 Jan 2020 11:06:12 +0100
Message-ID: <20200110100612.GC2827@hirez.programming.kicks-ass.net> (raw)
In-Reply-To: <20200109153831.29993-1-longman@redhat.com>

On Thu, Jan 09, 2020 at 10:38:31AM -0500, Waiman Long wrote:

> --- a/kernel/locking/osq_lock.c
> +++ b/kernel/locking/osq_lock.c
> @@ -134,6 +134,27 @@ bool osq_lock(struct optimistic_spin_queue *lock)
>  	 * cmpxchg in an attempt to undo our queueing.
>  	 */
>  
> +	/*
> +	 * If vcpu_is_preempted is not defined, we can skip the check
> +	 * and use smp_cond_load_relaxed() instead. For arm64, this
> +	 * could lead to the use of the more optimized wfe instruction.
> +	 * As need_sched() is set by interrupt handler, it will break
> +	 * out and do the unqueue in a timely manner.
> +	 *
> +	 * TODO: We may need to add a static_key like vcpu_is_preemptible
> +	 *	 as vcpu_is_preempted() will always return false with
> +	 *	 bare metal even if it is defined.
> +	 */
> +#ifndef vcpu_is_preempted
> +	{
> +		int locked = smp_cond_load_relaxed(&node->locked,
> +						   VAL || need_resched());
> +		if (!locked)
> +			goto unqueue;
> +		return true;
> +	}
> +#endif

Much yuck :-/

With ARM64 being the only arch that currently makes use of this; another
approach is doing something like:

That is also rather yuck, and definitely needs a few comments sprinked
on it, but it should just work for everyone.

It basically relies on an arch having a spinning *cond_load*()
implementation if it has vcpu_is_preempted(), which is true today.

---
diff --git a/kernel/locking/osq_lock.c b/kernel/locking/osq_lock.c
index 6ef600aa0f47..6e00d7c077ba 100644
--- a/kernel/locking/osq_lock.c
+++ b/kernel/locking/osq_lock.c
@@ -133,18 +133,10 @@ bool osq_lock(struct optimistic_spin_queue *lock)
 	 * guaranteed their existence -- this allows us to apply
 	 * cmpxchg in an attempt to undo our queueing.
 	 */
+	if (!smp_cond_load_relaxed(&node->locked, VAL || need_resched() ||
+						  vcpu_is_preempetd(node_cpu(node->prev))))
+		goto unqueue;
 
-	while (!READ_ONCE(node->locked)) {
-		/*
-		 * If we need to reschedule bail... so we can block.
-		 * Use vcpu_is_preempted() to avoid waiting for a preempted
-		 * lock holder:
-		 */
-		if (need_resched() || vcpu_is_preempted(node_cpu(node->prev)))
-			goto unqueue;
-
-		cpu_relax();
-	}
 	return true;
 
 unqueue:

  reply index

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-09 15:38 Waiman Long
2020-01-10 10:06 ` Peter Zijlstra [this message]
2020-01-10 14:13   ` 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=20200110100612.GC2827@hirez.programming.kicks-ass.net \
    --to=peterz@infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=longman@redhat.com \
    --cc=mingo@redhat.com \
    --cc=will.deacon@arm.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

LKML Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/lkml/0 lkml/git/0.git
	git clone --mirror https://lore.kernel.org/lkml/1 lkml/git/1.git
	git clone --mirror https://lore.kernel.org/lkml/2 lkml/git/2.git
	git clone --mirror https://lore.kernel.org/lkml/3 lkml/git/3.git
	git clone --mirror https://lore.kernel.org/lkml/4 lkml/git/4.git
	git clone --mirror https://lore.kernel.org/lkml/5 lkml/git/5.git
	git clone --mirror https://lore.kernel.org/lkml/6 lkml/git/6.git
	git clone --mirror https://lore.kernel.org/lkml/7 lkml/git/7.git
	git clone --mirror https://lore.kernel.org/lkml/8 lkml/git/8.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 lkml lkml/ https://lore.kernel.org/lkml \
		linux-kernel@vger.kernel.org
	public-inbox-index lkml

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-kernel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git