Is it harmful to use volatiles in C?

I am very new to cutaway. And while running sparse I see this warning:

warning: wrong type in argument 2 (different address spaces)

expected void volatile [noderef] <asn:2>*addr
got void *

      

This is mainly due to the following:

struct context{
    void __iomem *base;
};

readl(const volatile void __iomem* add){
....
....
}

function: foo(){
    struct  context *var;
    readl(var->base);   //---> here i got the above mentioned warning
}

      

To fix this, I did the following:

struct context{
      - void __iomem *base;
      + volatile void __iomem *base;
};

      

And the warning is removed.

My question:          - In this case, it is harmful to use "unstable". and if so, WHY? - I think that I shouldn't turn the member of the structure as "mutable". But how can we get rid of the Csparse warning.

As stated in the documentation @ https://www.kernel.org/doc/Documentation/volatile-considered-harmful.txt @ http://lwn.net/Articles/233482/ . we should always avoid using volatiles.

+3


source to share


2 answers


No, this is not harmful. I do not know why it should or may be harmful for what?



If the code that you are calling expects a pointer volatile

, then it is incorrect to pass more than volatile

one to it, since the code in the calling context may be incorrectly adapted to the requirements of the volatile

value in this case.

+1


source


volatile instructs the compiler not to do any optimization on this variable. Thus, it would provide a guarantee that the last value of the variable will be used. This can be changed by an external event. volatile is commonly used when dealing with external events such as hardware-related pins interrupts. I don't think this is harmful. But why should it be used when it is not needed, because optimization helps in improving efficiency, so if you are sure that even if a variable is optimized it cannot be changed by an external event and then subtle then unstable.



+1


source







All Articles