spinlock variable protection
Hi,
I have the following code
int ret = 0;
unsigned long irqflags;
spin_lock_irqsave(&lock, irqflags);
//...
ret = hdl->count;
//...
spin_unlock_irqrestore(&lock, irqflags);
return ret;I would like to be sure, that ret will not be optimized out. I think compiler can convert the code to equivalent:
unsigned long irqflags;
spin_lock_irqsave(&lock, irqflags);
//...
//...
spin_unlock_irqrestore(&lock, irqflags);
return hdl->count;But this is not what I want, because I use lock to protect hdl and want to return hdl->count value as it was in protected section.
Quoted 33 lines
? 2015?1?30??20:43?"Matwey V. Kornilov" <[email protected]> ???
Hi,
I have the following code
int ret = 0; unsigned long irqflags; spin_lock_irqsave(&lock, irqflags); //... ret = hdl->count; //... spin_unlock_irqrestore(&lock, irqflags); return ret;I would like to be sure, that ret will not be optimized out. I think compiler can convert the code to equivalent:
unsigned long irqflags; spin_lock_irqsave(&lock, irqflags); //... //... spin_unlock_irqrestore(&lock, irqflags); return hdl->count;
Quoted 4 lines
But this is not what I want, because I use lock to protect hdl and want to return
hdl->countvalue as it was in protected section.
Please check the assembly code to double confirm the GCC behavior.
Why will GCC change the order as what you mentioned? Only assembly code can tell you.
Quoted 4 lines
_______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
2015-01-30 16:52 GMT+03:00 buyitian <[email protected]>:
Quoted 3 lines
Quoted 1 line
Please check the assembly code to double confirm the GCC behavior.
Why will GCC change the order as what you mentioned? Only assembly code can tell you.
It does not change at the moment. I think it can change it.
Because from line
ret = hdl->count;
until line
return ret;
there is no access to either ret or hdl->count. So it is reasonable to optimizer to think that their values are the same and eliminate unneeded variable.
Signature · 4 lines
With best regards, Matwey V. Kornilov http://blog.matwey.name xmpp://0x2207 at jabber.ru
Spinlocks imply memory barriers as far as I am aware...
Read here:
http://lxr.free-electrons.com/source/Documentation/memory-barriers.txt#L1634
Quoted 13 lines · Matwey V. Kornilov
2015-01-30 16:52 GMT+03:00 buyitian <[email protected]>:
Quoted 2 lines
Please check the assembly code to double confirm the GCC behavior.
Why will GCC change the order as what you mentioned? Only assembly code can tell you.It does not change at the moment. I think it can change it.
Because from line
ret =hdl->count;
until line
return ret;
there is no access to either ret orhdl->count. So it is reasonable to optimizer to think that their values are the same and eliminate unneeded variable.
Nice, thank you
30.01.2015 18:24 ???????????? "Malte Vesper" <
[email protected]> ???????:
Quoted 25 lines
Spinlocks imply memory barriers as far as I am aware...
Read here: http://lxr.free-electrons.com/source/Documentation/memory-
barriers.txt#L1634Quoted 17 lines · Matwey V. Kornilov
2015-01-30 16:52 GMT+03:00 buyitian <[email protected]>:
Quoted 4 lines
Please check the assembly code to double confirm the GCC behavior. Why will GCC change the order as what you mentioned? Only assembly code can tell you.
It does not change at the moment. I think it can change it.
Because from line
ret =hdl->count;
until line
return ret;
there is no access to either ret orhdl->count. So it is reasonable to optimizer to think that their values are the same and eliminate unneeded variable.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20150130/d9af5564/attachment.html
Hi,
Quoted 4 lines · Malte Vesper
Spinlocks imply memory barriers as far as I am aware...
Read here:
http://lxr.free-electrons.com/source/Documentation/memory-barriers.txt#L1634
And may be here aswell,
http://lxr.free-electrons.com/source/Documentation/memory-barriers.txt#L1233
Thanks,
Arun
Quoted 21 lines · Matwey V. Kornilov
Quoted 5 lines
2015-01-30 16:52 GMT+03:00 buyitian <[email protected]>:
Quoted 2 lines
Please check the assembly code to double confirm the GCC behavior.
Why will GCC change the order as what you mentioned? Only assembly code can tell you.It does not change at the moment. I think it can change it.
Quoted 8 lines
Because from line
ret =hdl->count;
until line
return ret;
there is no access to either ret orhdl->count. So it is reasonable to optimizer to think that their values are the same and eliminate unneeded variable._______________________________________________
Kernelnewbies mailing list
Kernelnewbies at kernelnewbies.org
http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
Quoted 7 lines · Matwey V. Kornilov
2015-01-30 16:52 GMT+03:00 buyitian <[email protected]>:
Quoted 3 lines
Quoted 1 line
Please check the assembly code to double confirm the GCC behavior.
Why will GCC change the order as what you mentioned? Only assembly code can tell you.It does not change at the moment. I think it can change it.
Quoted 8 lines
Because from line
ret =hdl->count;
until line
return ret;
there is no access to either ret orhdl->count. So it is reasonable to optimizer to think that their values are the same and eliminate unneeded variable.
If your worry is that it will optimize it out then pack it into an ACCESS_ONCE and that should prevent GCC from doing so. for your case I think ret = ACCESS_ONCE(hdl->count); would be sufficient.
thx!
hofrat