Skip to contentSkip to conversation

spinlock variable protection

DISCUSSION Matwey V. Kornilov 5 participants 7 messages Jan 30, 2015 – Jan 31, 2015

lore
Share & tools
lore
#0 lore raw

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.

#1 lore raw
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->count value 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

#2 lore raw

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
#3 lore raw

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 or hdl->count. So it is reasonable to optimizer to think that their values are the same and eliminate unneeded variable.

#4 lore raw

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#L1634

Quoted 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 or hdl->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

#5 lore raw
In reply to Malte Vesper

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 or hdl->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

#6 lore raw
In reply to Matwey V. Kornilov
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 or hdl->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