OSDev.org

The Place to Start for Operating System Developers
It is currently Fri Apr 19, 2024 2:12 pm

All times are UTC - 6 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Post subject: GCC asm volatile reorder
PostPosted: Tue Mar 03, 2015 8:58 pm 
Offline

Joined: Tue Dec 16, 2014 8:02 pm
Posts: 15
Location: China
Hi all,

I am reading xv6(http://pdos.csail.mit.edu/6.828/2011/xv6.html) source code and something confuses me.

It is the implementation of spinlock. The lock release function is implemented as:
Code:
// Release the lock.
void
release(struct spinlock *lk)
{
  if(!holding(lk))
    panic("release");

  lk->pcs[0] = 0;
  lk->cpu = 0;

  // The xchg serializes, so that reads before release are
  // not reordered after it.  The 1996 PentiumPro manual (Volume 3,
  // 7.2) says reads can be carried out speculatively and in
  // any order, which implies we need to serialize here.
  // But the 2007 Intel 64 Architecture Memory Ordering White
  // Paper says that Intel 64 and IA-32 will not move a load
  // after a store. So lock->locked = 0 would work here.
  // The xchg being asm volatile ensures gcc emits it after
  // the above assignments (and after the critical section).
  xchg(&lk->locked, 0);

  popcli();
}

And the xchg is implemented as:
Code:
static inline uint
xchg(volatile uint *addr, uint newval)
{
  uint result;
 
  // The + in "+m" denotes a read-modify-write operand.
  asm volatile("lock; xchgl %0, %1" :
               "+m" (*addr), "=a" (result) :
               "1" (newval) :
               "cc");
  return result;
}

What confuses me is that the comment of release tells that:
Quote:
The xchg being asm volatile ensures gcc emits it after the above assignments (and after the critical section).

But from the gcc manual https://gcc.gnu.org/onlinedocs/gcc/Extended-Asm.html I find:
Quote:
Note that the compiler can move even volatile asm instructions relative to other code, including across jump instructions.

I think these two conflicts with each other. I prefer the manual so I think maybe this spinlock release function is wrong if gcc reorders the instructions. Is this a reasonable worry or just redundant? If I am right, does this mean I have to add a memory barrier here?


Top
 Profile  
 
 Post subject: Re: GCC asm volatile reorder
PostPosted: Tue Mar 03, 2015 10:29 pm 
Offline
Member
Member
User avatar

Joined: Wed Dec 01, 2010 3:41 am
Posts: 1761
Location: Hong Kong
Your concern is reasonable, and without seeing actual code, there is chance that popcli() get moved around as well.


According to http://preshing.com/20120625/memory-ordering-at-compile-time/

You may want to add a hint to compiler to prevent re-ordering and it seems adds no overhead (except it flush all memory dependency).

Code:
  lk->pcs[0] = 0;
  lk->cpu = 0;
  asm volatile("" ::: "memory");
  xchg(&lk->locked, 0);


Top
 Profile  
 
 Post subject: Re: GCC asm volatile reorder
PostPosted: Tue Mar 03, 2015 10:59 pm 
Offline

Joined: Tue Dec 16, 2014 8:02 pm
Posts: 15
Location: China
Thanks Bluemoon. I will email the maintainer and hope he will give me a reply for conformation.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 84 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group