Re: [PATCH 2/2] locking/rwsem: Use a return variable in rwsem_spin_on_owner()

From: Paul E. McKenney
Date: Thu Apr 09 2015 - 14:39:41 EST

On Thu, Apr 09, 2015 at 11:16:24AM -0700, Linus Torvalds wrote:
> On Thu, Apr 9, 2015 at 11:08 AM, Linus Torvalds
> <torvalds@xxxxxxxxxxxxxxxxxxxx> wrote:
> >
> > The pointer is a known-safe kernel pointer - it's just that it was
> > "known safe" a few instructions ago, and might be rcu-free'd at any
> > time.
> Actually, we could even do something like this:
> static inline int sem_owner_on_cpu(struct semaphore *sem, struct
> task_struct *owner)
> {
> int on_cpu;
> rcu_read_lock();
> #endif
> on_cpu = sem->owner == owner && owner->on_cpu;
> rcu_read_unlock();
> #endif
> return on_cpu;
> }
> because we really don't need to hold the RCU lock over the whole loop,
> we just need to validate that the semaphore owner still matches, and
> if so, check that it's on_cpu.

Much better from an RCU grace-period-latency perspective.

> And if CONFIG_DEBUG_PAGEALLOC is set, we don't care about performance
> *at*all*. We will have worse performance problems than doing some RCU
> read-locking inside the loop.
> And if CONFIG_DEBUG_PAGEALLOC isn't set, we don't really care about
> locking, since at worst we just access stale memory for one iteration.

But if we are running on a hypervisor, mightn't our VCPU be preempted
just before accessing ->on_cpu, the task exit and its structures be
freed and unmapped? Or is the task structure in memory that is never
unmapped? (If the latter, clearly not a problem.)

Thanx, Paul

> Hmm. It's not pretty, but neither is the current "let's just take a
> rcu lock that we don't really need over a loop that doesn't have very
> strict bounding".
> Comments?
> Linus

To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at
Please read the FAQ at