Skip to contentSkip to conversation

ppc64 serialization problem

OT Greg Smith 5 participants 9 messages Mar 29, 2006

lore
Share & tools
lore

Greg Smith

author
#0 lore raw

We have a multi-threaded app running on a p520 in 64 bit mode.

Thread A does

pthread_mutex_lock(&lock);
u32 &= ~bitA;
pthread_mutex_unlock(&lock);

and Thread B does

pthread_mutex_lock(&lock);
u32 |= bitB;
A = u32;
B = u32;
pthread_mutex_unlock(&lock);

On rare occasions, values A and B will differ! In the examples that I have seen, there is contention with `lock'. This phenomenon does not occur on ppc32 or a number of other architectures that we support.

I confess I do not know the linux version nor the glibc version nor what pthreads implementation is being used. I'll find that out shortly.

What I am curious about is where the problem might lie (kernel/lib/pthreads/app) so I can ask the right people.

Thank you for your patience,
Greg Smith

#1 lore raw
Quoted 16 lines · Greg Smith

We have a multi-threaded app running on a p520 in 64 bit mode.

Thread A does

pthread_mutex_lock(&lock);
u32 &= ~bitA;
pthread_mutex_unlock(&lock);

and Thread B does

pthread_mutex_lock(&lock);
u32 |= bitB;
A = u32;
B = u32;
pthread_mutex_unlock(&lock);

Quoted 3 lines

On rare occasions, values A and B will differ! In the examples that I have seen, there is contention with `lock'. This phenomenon does not occur on ppc32 or a number of other architectures that we support.

How did you actually "look" at A and B ? is that also protected by the lock ?

Quoted 2 lines

I confess I do not know the linux version nor the glibc version nor what pthreads implementation is being used. I'll find that out shortly.

That's fairly important to know those yes.

Quoted 12 lines

What I am curious about is where the problem might lie (kernel/lib/pthreads/app) so I can ask the right people.

Thank you for your patience,
Greg Smith

_______________________________________________
Linuxppc-dev mailing list
[email protected]
https://ozlabs.org/mailman/listinfo/linuxppc-dev

#4 lore raw
In reply to Greg Smith
Quoted 3 lines · Greg Smith

On rare occasions, values A and B will differ! In the examples that I have seen, there is contention with `lock'. This phenomenon does not occur on ppc32 or a number of other architectures that we support.

I would be interested to see the assembler code being generated for your code snippets.

Paul.

author
#2 lore raw
In reply to Benjamin Herrenschmidt

Very fair questions!!

Actually the code was

    pthread_mutex_lock(&lock);
    u32 |= bitB;
    TRACE("A", u32, ...);
    TRACE("B", u32, ...);
    pthread_mutex_unlock(&lock);

where TRACE is a function call (entering a trace entry to an in-storage wrap-around table). So for the "A" call, u32 could have come directly from a register and for "B" from the storage location. I'll have the user (actually a fellow developer) send me the assembly listing to make sure.

He has tested SLES9 (kernel 2.6.5, glibc 2.3.3, gcc 3.3.3) and Debian (kernel 2.6.16, glibc 2.3.6, gcc 4.0.3).

The TRACE occurs while the lock is held.

Now the interesting part.

I suggested he try u64 instead of u32. That works!!

He is suspecting a recent firmware upgrade may have something to do with the problem.

Thank you,
Greg Smith

Quoted 34 lines · Benjamin Herrenschmidt
Quoted 16 lines · Greg Smith

We have a multi-threaded app running on a p520 in 64 bit mode.

Thread A does

pthread_mutex_lock(&lock);
u32 &= ~bitA;
pthread_mutex_unlock(&lock);

and Thread B does

pthread_mutex_lock(&lock);
u32 |= bitB;
A = u32;
B = u32;
pthread_mutex_unlock(&lock);

Quoted 3 lines

On rare occasions, values A and B will differ! In the examples that I have seen, there is contention with `lock'. This phenomenon does not occur on ppc32 or a number of other architectures that we support.

How did you actually "look" at A and B ? is that also protected by the lock ?

Quoted 2 lines

I confess I do not know the linux version nor the glibc version nor what pthreads implementation is being used. I'll find that out shortly.

That's fairly important to know those yes.

Quoted 5 lines

What I am curious about is where the problem might lie (kernel/lib/pthreads/app) so I can ask the right people.

Thank you for your patience,
Greg Smith

#3 lore raw
Quoted 10 lines · Greg Smith

Very fair questions!!

Actually the code was

    pthread_mutex_lock(&lock);
    u32 |= bitB;
    TRACE("A", u32, ...);
    TRACE("B", u32, ...);
    pthread_mutex_unlock(&lock);
Quoted 5 lines

where TRACE is a function call (entering a trace entry to an in-storage wrap-around table). So for the "A" call, u32 could have come directly from a register and for "B" from the storage location. I'll have the user (actually a fellow developer) send me the assembly listing to make sure.

Could you try to make a small program that reproduces the problem instead ?

Quoted 11 lines

He has tested SLES9 (kernel 2.6.5, glibc 2.3.3, gcc 3.3.3) and Debian (kernel 2.6.16, glibc 2.3.6, gcc 4.0.3).

The TRACE occurs while the lock is held.

Now the interesting part.

I suggested he try u64 instead of u32. That works!!

He is suspecting a recent firmware upgrade may have something to do with the problem.

I doubt it, but I need more informations.

Quoted 39 lines

Thank you,
Greg Smith

Quoted 34 lines · Benjamin Herrenschmidt
Quoted 16 lines · Greg Smith

We have a multi-threaded app running on a p520 in 64 bit mode.

Thread A does

pthread_mutex_lock(&lock);
u32 &= ~bitA;
pthread_mutex_unlock(&lock);

and Thread B does

pthread_mutex_lock(&lock);
u32 |= bitB;
A = u32;
B = u32;
pthread_mutex_unlock(&lock);

Quoted 3 lines

On rare occasions, values A and B will differ! In the examples that I have seen, there is contention with `lock'. This phenomenon does not occur on ppc32 or a number of other architectures that we support.

How did you actually "look" at A and B ? is that also protected by the lock ?

Quoted 2 lines

I confess I do not know the linux version nor the glibc version nor what pthreads implementation is being used. I'll find that out shortly.

That's fairly important to know those yes.

Quoted 5 lines

What I am curious about is where the problem might lie (kernel/lib/pthreads/app) so I can ask the right people.

Thank you for your patience,
Greg Smith

author
#5 lore raw
In reply to Paul Mackerras
Quoted 10 lines · Paul Mackerras
Quoted 3 lines · Greg Smith

On rare occasions, values A and B will differ! In the examples that I have seen, there is contention with `lock'. This phenomenon does not occur on ppc32 or a number of other architectures that we support.

I would be interested to see the assembler code being generated for your code snippets.

Paul.

This is looking like a compiler optimization issue. We have two fields

_u32 x,y;

Field y is what we are trying to serialize with the lock. However, while we are doing that it appears another thread is updating x. When x is updated and with optimization, the code is doing an 8 byte load of x and y then doing an 8 byte store, wiping out what was stored in y. Which explains why changing the type to _u64 seemed to fix the problem.

Below are some of the notes the other developer sent me.

Thanks,
Greg Smith

There are OTHER field neighbouring the ints_state field.. (and this is probably some of the flags)..

gcc, when optimizing, gcc load and stores 64 bit values, *INCLUDING* the ints_state field effectivelly breaking serialization.

Here is a sample program :

struct _test1

{
         unsigned long long z;
         unsigned char c;
         unsigned int a:1;
         const volatile unsigned int b;
};

int main()
{
}

unsigned int toto(struct _test1 *data)

{
         data->a=1;
         return data->b;
}

And here is the relevant asm snippet :

.A

         ld 11,8(9) - get 64 bits ->a
         lis 0,0x80 *
         sldi 0,0,32 *
         or 0,11,0 *
.B
         std 0,8(9) * Store 64 bits -> a
         ld 9,112(31) *
         lwz 0,12(9) * get 32 bit b

So it is clear between .A and .B -> value of b was read then written. If anything happens between .A and .B to ->b, then it is lost.

#6 lore raw
Quoted 2 lines · Greg Smith

So it is clear between .A and .B -> value of b was read then written. If anything happens between .A and .B to ->b, then it is lost.

Even gcc4.1 does it that way with -O0.

#7 lore raw
Quoted 8 lines · Olaf Hering
Quoted 3 lines · Greg Smith

So it is clear between .A and .B -> value of b was read then written. If anything happens between .A and .B to ->b, then it is lost.

Even gcc4.1 does it that way with -O0

Yeah.. Noticed that..gcc 3.3.3 and gcc 4.0.3 also do this

Even declaring the field volatile has no effect..

Question :
Is this *NORMAL* behavior ? I discussed this with some C folks and they seem to think (at least some of them) that it's broken per standards.. (volatile fields should be only read/written IFF read/written explicitly - pretty much what section 6.3.7 of the C std says).

Anyway I can produce perfectly valid sample code (to my understanding anyway) that produces incorrect result.. YUCK !

This is also possibly a problem with the powerpc64 back end of gcc..

Anyway.. It's OT.. Sorry to have bothered you guys ! (And thanks Greg for pointing me here).

--Ivan

#8 lore raw
Quoted 3 lines · Ivan Warren

Question :
Is this *NORMAL* behavior ? I discussed this with some C folks and they seem to think (at least some of them) that it's broken per standards.. (volatile fields should be only read/written IFF read/written explicitly - pretty much what section 6.3.7 of the C std says).

...For Info...

I've submitted a gcc PR :

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26933

Anyway.. My apologies since this was a bit off topic..

--Ivan