Usage of unlikely in RCU code

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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

_______________________________________________
Kernelnewbies mailing list
Kernelnewbies@xxxxxxxxxxxxxxxxx
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux