All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jason Low <jason.low2@hp.com>
To: Davidlohr Bueso <davidlohr@hp.com>
Cc: peterz@infradead.org, mingo@kernel.org, aswin@hp.com,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH -tip/master v2] locking/mutex: Refactor optimistic spinning code
Date: Mon, 28 Jul 2014 20:41:39 -0700	[thread overview]
Message-ID: <1406605299.2411.59.camel@j-VirtualBox> (raw)
In-Reply-To: <1406602517.31161.2.camel@buesod1.americas.hpqcorp.net>

On Mon, 2014-07-28 at 19:55 -0700, Davidlohr Bueso wrote:
> +static bool mutex_optimistic_spin(struct mutex *lock,
> +				  struct ww_acquire_ctx *ww_ctx, const bool use_ww_ctx)
> +{
> +	struct task_struct *task = current;
> +
> +	if (!mutex_can_spin_on_owner(lock))
> +		return false;
> +
> +	if (!osq_lock(&lock->osq))
> +		return false;

In the !osq_lock() case, we could exit the cancellable MCS spinlock due
to need_resched(). However, this would return from the function rather
than doing the need_resched() check below. Perhaps we can add something
like "goto out" which goes to the below check?

The mutex_can_spin_on_owner() also returns false if need_resched().

> +	while (true) {
> +		struct task_struct *owner;
> +
> +		if (use_ww_ctx && ww_ctx->acquired > 0) {
> +			struct ww_mutex *ww;
> +
> +			ww = container_of(lock, struct ww_mutex, base);
> +			/*
> +			 * If ww->ctx is set the contents are undefined, only
> +			 * by acquiring wait_lock there is a guarantee that
> +			 * they are not invalid when reading.
> +			 *
> +			 * As such, when deadlock detection needs to be
> +			 * performed the optimistic spinning cannot be done.
> +			 */
> +			if (ACCESS_ONCE(ww->ctx))
> +				break;
> +		}
> +
> +		/*
> +		 * If there's an owner, wait for it to either
> +		 * release the lock or go to sleep.
> +		 */
> +		owner = ACCESS_ONCE(lock->owner);
> +		if (owner && !mutex_spin_on_owner(lock, owner))
> +			break;
> +
> +		/* Try to acquire the mutex if it is unlocked. */
> +		if (mutex_try_to_acquire(lock)) {
> +			if (use_ww_ctx) {
> +				struct ww_mutex *ww;
> +				ww = container_of(lock, struct ww_mutex, base);
> +
> +				ww_mutex_set_context_fastpath(ww, ww_ctx);
> +			}
> +
> +			mutex_set_owner(lock);
> +			osq_unlock(&lock->osq);
> +			return true;
> +		}
> +
> +		/*
> +		 * When there's no owner, we might have preempted between the
> +		 * owner acquiring the lock and setting the owner field. If
> +		 * we're an RT task that will live-lock because we won't let
> +		 * the owner complete.
> +		 */
> +		if (!owner && (need_resched() || rt_task(task)))
> +			break;
> +
> +		/*
> +		 * The cpu_relax() call is a compiler barrier which forces
> +		 * everything in this loop to be re-loaded. We don't need
> +		 * memory barriers as we'll eventually observe the right
> +		 * values at the cost of a few extra spins.
> +		 */
> +		cpu_relax_lowlatency();
> +	}
> +
> +	osq_unlock(&lock->osq);
> +
> +	/*
> +	 * If we fell out of the spin path because of need_resched(),
> +	 * reschedule now, before we try-lock the mutex. This avoids getting
> +	 * scheduled out right after we obtained the mutex.
> +	 */
> +	if (need_resched())
> +		schedule_preempt_disabled();
> +
> +	return false;
> +}



  reply	other threads:[~2014-07-29  3:41 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-28  5:18 [PATCH -tip/master 1/7] locking/mutex: Unify arguments in lock/unlock slowpaths Davidlohr Bueso
2014-07-28  5:18 ` [PATCH -tip/master 2/7] locking/mutex: Document quick lock release when unlocking Davidlohr Bueso
2014-07-30 15:10   ` Jason Low
2014-07-30 18:20     ` Davidlohr Bueso
2014-07-28  5:18 ` [PATCH -tip/master 3/7] locking/mcs: Remove obsolete comment Davidlohr Bueso
2014-07-28 16:49   ` Jason Low
2014-07-28 16:53     ` Davidlohr Bueso
2014-07-28 16:57       ` Peter Zijlstra
2014-07-28 17:19         ` Jason Low
2014-07-28 16:54     ` Peter Zijlstra
2014-07-28 17:49       ` Jason Low
2014-07-28 18:50         ` Peter Zijlstra
2014-07-28 21:02           ` Jason Low
2014-07-30 15:11   ` Jason Low
2014-07-28  5:18 ` [PATCH -tip/master 4/7] locking/mutex: Refactor optimistic spinning code Davidlohr Bueso
2014-07-28  9:08   ` Peter Zijlstra
2014-07-28 16:39     ` Jason Low
2014-07-28 16:41       ` Davidlohr Bueso
2014-07-29  2:55   ` [PATCH -tip/master v2] " Davidlohr Bueso
2014-07-29  3:41     ` Jason Low [this message]
2014-07-29  4:31       ` Davidlohr Bueso
2014-07-29  4:51   ` [PATCH -tip/master v3] " Davidlohr Bueso
2014-07-30 15:18   ` [PATCH -tip/master 4/7] " Jason Low
2014-07-28  5:18 ` [PATCH -tip/master 5/7] locking/mutex: Use MUTEX_SPIN_ON_OWNER when appropriate Davidlohr Bueso
2014-07-30 15:19   ` Jason Low
2014-07-28  5:18 ` [PATCH 6/7] locking: Move docs into Documentation/locking/ Davidlohr Bueso
2014-07-28  5:18 ` [PATCH -tip/master 7/7] Documentation: Update locking/mutex-design.txt disadvantages Davidlohr Bueso
2014-07-28 18:09   ` Jason Low

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=1406605299.2411.59.camel@j-VirtualBox \
    --to=jason.low2@hp.com \
    --cc=aswin@hp.com \
    --cc=davidlohr@hp.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.