Usage of unlikely in RCU code

Julie Sullivan kernelmail.jms at gmail.com
Mon Aug 1 17:28:11 EDT 2011


Hi list,

I'm looking at RCU at the moment and (on reading a recent article by
Paul McKenney on LWN) I've spotted something I'm a bit confused by in
this code, a function which is defined in kernel/rcutree_plugin.h:

void __rcu_read_unlock(void)
{
        struct task_struct *t = current;

        barrier();  /* needed if we ever invoke rcu_read_unlock in rcutree.c */
        --t->rcu_read_lock_nesting;
        barrier();  /* decrement before load of ->rcu_read_unlock_special */
        if (t->rcu_read_lock_nesting == 0 &&
            unlikely(ACCESS_ONCE(t->rcu_read_unlock_special)))
                rcu_read_unlock_special(t);
#ifdef CONFIG_PROVE_LOCKING
        WARN_ON_ONCE(ACCESS_ONCE(t->rcu_read_lock_nesting) < 0);
#endif /* #ifdef CONFIG_PROVE_LOCKING */
}



Specifically, the conditional

  if (t->rcu_read_lock_nesting == 0 &&
            unlikely(ACCESS_ONCE(t->rcu_read_unlock_special)))
                rcu_read_unlock_special(t);

I've googled for 'likely/unlikely' and i've seen

  if (unlikely(...

before in kernel code, and I might understand if the logical operator
here were || not &&, but why is the 'unlikely' attribute only applied
to the second operand here? Because both must be evaluated, with
left-to-right associativity, and then both must be true for the branch
to be taken, right? If it's necessary for branch
prediction/optimization purposes shouldn't it be applied to both or
the first one? Or might the operands also be re-ordered by the
compiler or processor so that the second is evaluated before the
first?

What am I misunderstanding about this attribute?

Cheers
Julie



More information about the Kernelnewbies mailing list