Re: [PATCH][RFC]: mutex: adaptive spin

Peter Zijlstra
2009-01-06T23:17:25Z

On Tue, 2009-01-06 at 23:43 +0100, Peter Zijlstra wrote:

> @@ -115,6 +117,7 @@ void __sched mutex_unlock(struct mutex *
> * The unlocking fastpath is the 0->1 transition from 'locked'
> * into 'unlocked' state:
> */
> + lock->owner = NULL;
> __mutex_fastpath_unlock(&lock->count, __mutex_unlock_slowpath);
> }
>

> +void mutex_spin_or_schedule(struct mutex_waiter *waiter, long state, unsigned long *flags)
> +{
> + struct mutex *lock = waiter->lock;
> + struct task_struct *task = waiter->task;
> + struct task_struct *owner = lock->owner;
> + struct rq *rq;
> +
> + if (!owner)
> + goto do_schedule;
> +
> + rq = task_rq(owner);
> +
> + if (rq->curr != owner) {
> +do_schedule:
> + __set_task_state(task, state);
> + spin_unlock_mutex(&lock->wait_lock, *flags);
> + schedule();
> + } else {
> + spin_unlock_mutex(&lock->wait_lock, *flags);
> + for (;;) {
> + /* Stop spinning when there's a pending signal. */
> + if (signal_pending_state(state, task))
> + break;
> +
> + /* Owner changed, bail to revalidate state */
> + if (lock->owner != owner)
> + break;
> +
> + /* Owner stopped running, bail to revalidate state */
> + if (rq->curr != owner)
> + break;
> +
> + cpu_relax();
> + }
> + }
> + spin_lock_mutex(&lock->wait_lock, *flags);
> +}

That's not going to work, we set owner to NULL, which means pending
spinners get schedule()ed out instead of racing to acquire.

I suppose the below would fix that... really sleep time now

Index: linux-2.6/kernel/sched.c
===================================================================
--- linux-2.6.orig/kernel/sched.c
+++ linux-2.6/kernel/sched.c
@@ -4626,8 +4626,8 @@ do_schedule:
if (signal_pending_state(state, task))
break;

- /* Owner changed, bail to revalidate state */
- if (lock->owner != owner)
+ /* Mutex got unlocked, race to acquire. */
+ if (!mutex_is_locked(lock))
break;

/* Owner stopped running, bail to revalidate state */