From mboxrd@z Thu Jan 1 00:00:00 1970 From: Peter Zijlstra Subject: Re: [PATCH -v5][RFC]: mutex: implement adaptive spinning Date: Wed, 07 Jan 2009 22:51:55 +0100 Message-ID: <1231365115.11687.361.camel@twins> References: <87r63ljzox.fsf@basil.nowhere.org> <20090103191706.GA2002@parisc-linux.org> <1231093310.27690.5.camel@twins> <20090104184103.GE2002@parisc-linux.org> <1231242031.11687.97.camel@twins> <20090106121052.GA27232@elte.hu> <4963584A.4090805@novell.com> <20090106131643.GA15228@elte.hu> <1231248041.11687.107.camel@twins> <49636799.1010109@novell.com> <20090106214229.GD6741@linux.vnet.ibm.com> <1231278275.11687.111.camel@twins> <1231279660.11687.121.camel@twins> <1231281801.11687.125.camel@twins> <1231283778.11687.136.camel@twins> <1231329783.11687.287.camel@twins> <1231347442.11687.344.camel@twins> Mime-Version: 1.0 Content-Type: text/plain Cc: Steven Rostedt , paulmck@linux.vnet.ibm.com, Gregory Haskins , Ingo Molnar , Matthew Wilcox , Andi Kleen , Chris Mason , Andrew Morton , Linux Kernel Mailing List , linux-fsdevel , linux-btrfs , Thomas Gleixner , Nick Piggin , Peter Morreale , Sven Dietrich To: Linus Torvalds Return-path: In-Reply-To: List-ID: On Wed, 2009-01-07 at 12:55 -0800, Linus Torvalds wrote: > /* > * Look out! "thread" is an entirely speculative pointer > * access and not reliable. > */ > void loop_while_oncpu(struct mutex *lock, struct thread_struct *thread) > { > for (;;) { > unsigned cpu; > struct runqueue *rq; > > if (lock->owner != thread) > break; > > /* > * Need to access the cpu field knowing that > * DEBUG_PAGEALLOC could have unmapped it if > * the mutex owner just released it and exited. > */ > if (__get_user(cpu, &thread->cpu)) > break; > > /* > * Even if the access succeeded (likely case), > * the cpu field may no longer be valid. FIXME: > * this needs to validate that we can do a > * get_cpu() and that we have the percpu area. > */ > if (cpu >= NR_CPUS) > break; > > if (!cpu_online(cpu)) > break; > > /* > * Is that thread really running on that cpu? > */ > rq = cpu_rq(cpu); > if (task_thread_info(rq->curr) != thread) > break; > > cpu_relax(); > } > } Do we really have to re-do all that code every loop? void loop_while_oncpu(struct mutex *lock, struct thread_struct *thread) { unsigned cpu; struct runqueue *rq; /* * Need to access the cpu field knowing that * DEBUG_PAGEALLOC could have unmapped it if * the mutex owner just released it and exited. */ if (__get_user(cpu, &thread->cpu)) break; /* * Even if the access succeeded (likely case), * the cpu field may no longer be valid. FIXME: * this needs to validate that we can do a * get_cpu() and that we have the percpu area. */ if (cpu >= NR_CPUS) break; if (!cpu_online(cpu)) break; rq = cpu_rq(cpu); for (;;) { if (lock->owner != thread) break; /* * Is that thread really running on that cpu? */ if (task_thread_info(rq->curr) != thread) break; cpu_relax(); } } Also, it would still need to do the funny: l_owner = ACCESS_ONCE(lock->owner) if (l_owner && l_owner != thread) break; thing, to handle the premature non-atomic lock->owner tracking.