Declaring it volatile
Declaring it volatile
Posted Aug 2, 2012 21:14 UTC (Thu) by corbet (editor, #1)In reply to: ACCESS_ONCE() by thedevil
Parent article: ACCESS_ONCE()
Because that would pessimize all accesses to that field. In general, use of volatile is strongly discouraged in the kernel; there's really only a few places where it is really needed.
Posted Aug 3, 2012 17:01 UTC (Fri)
by daglwn (guest, #65432)
[Link] (17 responses)
Lacking that, it should be sufficient to load the volatile-qualified name into a temporary and use the temporary to allow optimization. This way code is safe by default (no need to remember to use ACCESS_ONCE) and can be optimized explicitly where developers know it is safe.
Posted Aug 6, 2012 17:04 UTC (Mon)
by PaulMcKenney (✭ supporter ✭, #9624)
[Link] (16 responses)
Posted Aug 6, 2012 17:23 UTC (Mon)
by daglwn (guest, #65432)
[Link] (15 responses)
The use of a cast is fine.
I'm suggesting the variable be declared volatile for safety reasons and temporaries be used to explicitly avoid pessimizing code when developers know it is safe. That way things are safe by default and explicitly and obviously optimized when possible.
Posted Aug 6, 2012 17:24 UTC (Mon)
by daglwn (guest, #65432)
[Link] (14 responses)
Posted Aug 6, 2012 18:16 UTC (Mon)
by PaulMcKenney (✭ supporter ✭, #9624)
[Link] (13 responses)
Yes, you can in principle use manually coded temporaries to enable compiler optimizations in cases where a lock is held that prevents changes to the variable being loaded, but it is not at all clear to me why this would be a better solution than using ACCESS_ONCE(), especially in the Linux kernel.
Posted Aug 6, 2012 20:49 UTC (Mon)
by daglwn (guest, #65432)
[Link] (5 responses)
Safety and explicitness.
Posted Aug 6, 2012 23:12 UTC (Mon)
by PaulMcKenney (✭ supporter ✭, #9624)
[Link] (4 responses)
Posted Aug 7, 2012 3:42 UTC (Tue)
by daglwn (guest, #65432)
[Link] (3 responses)
Posted Aug 7, 2012 14:37 UTC (Tue)
by PaulMcKenney (✭ supporter ✭, #9624)
[Link] (2 responses)
Posted Aug 7, 2012 19:44 UTC (Tue)
by daglwn (guest, #65432)
[Link] (1 responses)
I'm not trying to convince you, simply stating what I believe is good programming practice. Maybe I'm wrong. But it's worked for me so far. :)
Posted Aug 8, 2012 15:26 UTC (Wed)
by PaulMcKenney (✭ supporter ✭, #9624)
[Link]
Posted Aug 7, 2012 5:52 UTC (Tue)
by viro (subscriber, #7872)
[Link] (6 responses)
Posted Aug 7, 2012 15:24 UTC (Tue)
by PaulMcKenney (✭ supporter ✭, #9624)
[Link] (5 responses)
And yes, gcc also seems quite happy to leak types from "({...})":
Yow!!!
Fortunately for me, ACCESS_ONCE() does not depend on the semantics of typeof() applied to either "({...})"; or bitfields, although only because it is illegal to take the address of either of them. ;-)
Posted Aug 7, 2012 15:35 UTC (Tue)
by nybble41 (subscriber, #55106)
[Link] (4 responses)
Are you sure about the first case? It seemed to work for me in GCC 4.5.1, at least in the case where the result of the "({...})" is a non-local l-value:
int x;
Posted Aug 7, 2012 15:57 UTC (Tue)
by PaulMcKenney (✭ supporter ✭, #9624)
[Link] (3 responses)
Posted Aug 7, 2012 18:35 UTC (Tue)
by nybble41 (subscriber, #55106)
[Link] (2 responses)
Is there any particular reason the statement expression can't be an l-value, provided the final form is an l-value, or was that case simply considered too error-prone?
Posted Aug 7, 2012 21:12 UTC (Tue)
by paulj (subscriber, #341)
[Link] (1 responses)
Posted Aug 7, 2012 21:26 UTC (Tue)
by PaulMcKenney (✭ supporter ✭, #9624)
[Link]
Posted Aug 3, 2012 18:04 UTC (Fri)
by BenHutchings (subscriber, #37955)
[Link] (3 responses)
Posted Jan 30, 2014 22:39 UTC (Thu)
by timur (guest, #30718)
[Link] (2 responses)
Posted Jan 30, 2014 22:55 UTC (Thu)
by timur (guest, #30718)
[Link]
Sneaky.
Posted Jan 31, 2014 4:07 UTC (Fri)
by mathstuf (subscriber, #69389)
[Link]
Declaring it volatile
Declaring it volatile
Declaring it volatile
Declaring it volatile
It does sound like it just might be well past time for typeof() in the C/C++ standards. ;-) That said, both gcc and llvm seem to support typeof(), and I bet a lot of other compilers do as well, either directly or indirectly. But perhaps the Linux kernel will eventually move from typeof() to C++11 decltype(), once the commonly-used compiler versions support it.
Declaring it volatile
Declaring it volatile
> compiler optimizations in cases where a lock is held that prevents changes
> to the variable being loaded, but it is not at all clear to me why this
> would be a better solution than using ACCESS_ONCE()
Declaring it volatile
Declaring it volatile
Declaring it volatile
Declaring it volatile
Declaring it volatile
Declaring it volatile
IIRC, there had been other places where it didn't play well with very unexpected parts of standard (e.g. bitfield signedness; does special treatment of signed int in there extend to e.g. typeof(signed int)? Taken a few steps further it can, unless you are very careful, extend the distinction between int and signed int through all the type system...)
I must admit that I have never used "typeof(({ printf("From typeof()\n"); j; })) k = 1;" to declare a variable "k" of the same type as "j", but gcc appears quite happy with it.
Declaring it volatile
#include <stdio.h>
int main(int argc, char *argv[])
{
int i = argc;
signed int j = i;
typeof(({ struct foo { int a; } q = { 1 }; printf("From typeof()\n"); q; })) k = { 2 };
printf("i = %d, j = %d, k.a = %d\n", i, j, k.a);
return 0;
}
Declaring it volatile
int *f(void)
{
return &({ x; });
}
I am using gcc 4.6.1, and the following emits a compiler error complaining that an lvalue is needed:
Declaring it volatile
#include <stdio.h>
#define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
int q = 3;
int main(int argc, char *argv[])
{
int i = argc;
i = ACCESS_ONCE(({ printk("foo"); q; }));
printk("i = %d\n", i);
}
Declaring it volatile
Declaring it volatile
Declaring it volatile
Also, implicit access-once behaviour can make it harder to understand how the code works.
Declaring it volatile
The functionality of this macro is actually well described by its name
Declaring it volatile
I disagree. I was completely confused by the article until I saw the implementation of ACCESS_ONCE, because the behavior being described is the opposite of what I think "access once" means. ACCESS_ONCE makes me believe that that it operates like pr_info_once(), which literally calls pr_info only once, no matter how many times loop repeats.
I also don't see how declaring it volatile forces the compiler to reload the variable only at the top of the loop. Since it's declared volatile, the compiler can reload 'owner' any time it wants.
Declaring it volatile
Declaring it volatile