linuxppc-dev.lists.ozlabs.org archive mirror
 help / color / mirror / Atom feed
* RE: atomic operations in user space
@ 2006-08-29  0:43 Xupei Liang
  2006-08-29  6:43 ` Li Yang-r58472
  0 siblings, 1 reply; 23+ messages in thread
From: Xupei Liang @ 2006-08-29  0:43 UTC (permalink / raw)
  To: linuxppc-embedded

I think it is less expensive using atomic operation
sometimes in the user space, e.g. when updating a
counter. If this counter is to be updated by a lot of
processes, using semaphore can potentially cause a lot
of task switching. 

Regards,

Terry Liang


> -----Original Message-----
> From: Brent Cook [mailto:bcook at bpointsys.com]
> Sent: Thursday, August 24, 2006 10:18 PM
> To: linuxppc-embedded at ozlabs.org
> Cc: Li Yang-r58472; Terry Liang
> Subject: Re: atomic operations in user space
> 
> On Thursday 24 August 2006 05:39, Li Yang-r58472
wrote:
> 
> > Why do you need atomic operations in user land?
IPC will be
sufficient
> 
> > to deal with race conditions between processes.
> 
> >
> 
> > Best Regards,
> 
> > Leo
> 
> What about multiple threads within a process
updating a counter?

Is there anything preventing semaphore to be used in
threads?
> 
> Of course, if you look at these functions in the
kernel header,
they're just 2 or
> 3 inline assembly calls - you could easily rewrite
them. Google for
'PowerPC atomic
> increment' and grab one of the unencumbered
implementations if you
need to use it
> in a non-GPL program.
> 
> On the other hand, I see no license at the top of my
/usr/include/asm-i386/atomic.h
> file at all, same for PowerPC - are Linux header
files actually GPL or
are they
> more like the glibc headers, with exceptions made
for userspace
programs?
> 
> The atomic operations on x86 were accidentally
exported early on, so
they have to
> hang around apparently for compatibility (there are
some mailing list
threads out
> there to this effect.) Currently, you just have to
assume in Linux
that if you
> include something from /usr/include/linux or asm
that it will not
necessarily be
> cross-version or cross-architecture compatible. Not
every arch in
Linux even has
> atomic operations of this nature, which I guess is
the main reason why
they are
> not exported in general.
> 
> - Brent



__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

^ permalink raw reply	[flat|nested] 23+ messages in thread
* RE: atomic operations in user space
@ 2006-08-30  2:17 Liu Dave-r63238
  2006-08-30  2:27 ` Liu Dave-r63238
  0 siblings, 1 reply; 23+ messages in thread
From: Liu Dave-r63238 @ 2006-08-30  2:17 UTC (permalink / raw)
  To: Li Yang-r58472, linuxppc-embedded, linuxppc-dev

[snip]
> I surely know all the theories you mentioned clearly.  But=20
> please do look at the case I gave.  Correct me if I missed=20
> anything.  Thanks
>=20
> All the lwarx and stwcx operate on the same address.
>=20
> > Task A		Task B
> > lwarx			=09
> // Get RESERVATION
> > 			......
> > 			lwarx
> > 			stwcx
>=20
> // RESERVATION cleared
> >
> > 			.....
> > 			.....
> > 			lwarx
>=20
> // Get RESERVATION again
> > stwcx			=09
>=20
> //Note here: RESERVATION is valid, address is the same.
> So the result is commited, no retry for task A
>=20
> > .....
> > 			stwcx
> //RESERVATION is cleared, retry atomic op for task B
>=20
> Please be noted that reservation is only identified by=20
> reservation bit and address operated on.  So different=20
> lwarx's on the same address, may be considered as the same=20
> reservation.

Is this your reason we cannot do atomic operation in user space?

How about the kernel space? You can image it.
The context switching as above also happen in kernel space,
Why we can do atomic operation in kernel space, not do in user space?

You are assuming the context switching cause the reservation broken.
but we can do atomic operation in kernel space.  The context switching
really is the execption of processor, If we can clear the wrong
RESERVATION
before exception return, I think we can solve this problem. We can dummy
stwcx. before exception return or the processor automaticly clear the
reservation in exception.=20

Are you missing these important things?

-DAve

^ permalink raw reply	[flat|nested] 23+ messages in thread
* RE: atomic operations in user space
@ 2006-08-29 13:37 Li Yang
  2006-08-29 16:05 ` Esben Nielsen
  0 siblings, 1 reply; 23+ messages in thread
From: Li Yang @ 2006-08-29 13:37 UTC (permalink / raw)
  To: linuxppc-embedded

> This is exactly how it is supposed to work! That's why there is a loop
> in the atomic increment - you check if you still had the reservation
> after the transaction by checking the result from the stwcx, and if not,
> retry.

I surely know all the theories you mentioned clearly.  But please do
look at the case I gave.  Correct me if I missed anything.  Thanks

All the lwarx and stwcx operate on the same address.

> Task A		Task B
> lwarx				
// Get RESERVATION
> 			......
> 			lwarx
> 			stwcx

// RESERVATION cleared
>
> 			.....
> 			.....
> 			lwarx

// Get RESERVATION again
> stwcx				

//Note here: RESERVATION is valid, address is the same.
So the result is commited, no retry for task A

> .....
> 			stwcx
//RESERVATION is cleared, retry atomic op for task B

Please be noted that reservation is only identified by reservation bit
and address operated on.  So different lwarx's on the same address,
may be considered as the same reservation.

^ permalink raw reply	[flat|nested] 23+ messages in thread
* atomic operations in user space
@ 2006-08-23 19:03 Terry Liang
  2006-08-24 10:39 ` Li Yang-r58472
  0 siblings, 1 reply; 23+ messages in thread
From: Terry Liang @ 2006-08-23 19:03 UTC (permalink / raw)
  To: linuxppc-embedded

[-- Attachment #1: Type: text/plain, Size: 350 bytes --]

Thanks. Arnd. My main concern is whether the operations are really atomic as they are in the kernel space. I have read some discussion in another forum that on other platforms, even if you are able to compile the atomic_add(), atomic_set(), etc. from an user space application,  they don't guarantee to be atomic. Thanks.

Regards,

Terry Liang

[-- Attachment #2: Type: text/html, Size: 820 bytes --]

^ permalink raw reply	[flat|nested] 23+ messages in thread
* atomic operations in user space
@ 2006-08-22 20:50 Xupei Liang
  2006-08-22 22:23 ` Arnd Bergmann
  0 siblings, 1 reply; 23+ messages in thread
From: Xupei Liang @ 2006-08-22 20:50 UTC (permalink / raw)
  To: linuxppc-embedded

Hi,

On the PPC platform, can I use the atomic operations
defined in the kernel, e.g. atomic_add(),
atomic_dec(), etc. in the user space programs? 

I compiled the following program:

#include <asm-ppc/atomic.h>

int main()
{
	atomic_t atom;

	atomic_add(1, &atom);
	return (0);
}

I am able to compile it and the atomic_add seems to be
translated into the correct instructions. Can anybody
please confirm? Thanks.

Regards,

Terry Liang

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 

^ permalink raw reply	[flat|nested] 23+ messages in thread

end of thread, other threads:[~2006-08-30  2:40 UTC | newest]

Thread overview: 23+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2006-08-29  0:43 atomic operations in user space Xupei Liang
2006-08-29  6:43 ` Li Yang-r58472
2006-08-29  8:33   ` Esben Nielsen
2006-08-29  8:54     ` Li Yang-r58472
2006-08-29  9:20       ` Liu Dave-r63238
2006-08-29  9:56         ` Esben Nielsen
2006-08-29 10:05           ` Liu Dave-r63238
2006-08-29 10:52           ` Li Yang-r58472
2006-08-29 11:26             ` Esben Nielsen
2006-08-29 11:30               ` Esben Nielsen
2006-08-29 12:36             ` Brent Cook
  -- strict thread matches above, loose matches on Subject: below --
2006-08-30  2:17 Liu Dave-r63238
2006-08-30  2:27 ` Liu Dave-r63238
2006-08-30  2:40   ` Li Yang-r58472
2006-08-29 13:37 Li Yang
2006-08-29 16:05 ` Esben Nielsen
2006-08-29 17:00   ` Li Yang
2006-08-23 19:03 Terry Liang
2006-08-24 10:39 ` Li Yang-r58472
2006-08-24 14:18   ` Brent Cook
2006-08-25  2:33     ` Li Yang-r58472
2006-08-22 20:50 Xupei Liang
2006-08-22 22:23 ` Arnd Bergmann

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).