~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/Documentation/process/volatile-considered-harmful.rst

Version: ~ [ linux-6.12-rc7 ] ~ [ linux-6.11.7 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.60 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.116 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.171 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.229 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.285 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.323 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.12 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

Diff markup

Differences between /Documentation/process/volatile-considered-harmful.rst (Version linux-6.12-rc7) and /Documentation/process/volatile-considered-harmful.rst (Version ccs-tools-1.8.12)


  1                                                   
  2 .. _volatile_considered_harmful:                  
  3                                                   
  4 Why the "volatile" type class should not be us    
  5 ----------------------------------------------    
  6                                                   
  7 C programmers have often taken volatile to mea    
  8 changed outside of the current thread of execu    
  9 sometimes tempted to use it in kernel code whe    
 10 being used.  In other words, they have been kn    
 11 as a sort of easy atomic variable, which they     
 12 kernel code is almost never correct; this docu    
 13                                                   
 14 The key point to understand with regard to vol    
 15 to suppress optimization, which is almost neve    
 16 do.  In the kernel, one must protect shared da    
 17 unwanted concurrent access, which is very much    
 18 process of protecting against unwanted concurr    
 19 all optimization-related problems in a more ef    
 20                                                   
 21 Like volatile, the kernel primitives which mak    
 22 safe (spinlocks, mutexes, memory barriers, etc    
 23 unwanted optimization.  If they are being used    
 24 need to use volatile as well.  If volatile is     
 25 almost certainly a bug in the code somewhere.     
 26 code, volatile can only serve to slow things d    
 27                                                   
 28 Consider a typical block of kernel code::         
 29                                                   
 30     spin_lock(&the_lock);                         
 31     do_something_on(&shared_data);                
 32     do_something_else_with(&shared_data);         
 33     spin_unlock(&the_lock);                       
 34                                                   
 35 If all the code follows the locking rules, the    
 36 change unexpectedly while the_lock is held.  A    
 37 want to play with that data will be waiting on    
 38 primitives act as memory barriers - they are e    
 39 meaning that data accesses will not be optimiz    
 40 compiler might think it knows what will be in     
 41 spin_lock() call, since it acts as a memory ba    
 42 forget anything it knows.  There will be no op    
 43 accesses to that data.                            
 44                                                   
 45 If shared_data were declared volatile, the loc    
 46 necessary.  But the compiler would also be pre    
 47 to shared_data _within_ the critical section,     
 48 can be working with it.  While the lock is hel    
 49 volatile.  When dealing with shared data, prop    
 50 unnecessary - and potentially harmful.            
 51                                                   
 52 The volatile storage class was originally mean    
 53 registers.  Within the kernel, register access    
 54 by locks, but one also does not want the compi    
 55 accesses within a critical section.  But, with    
 56 accesses are always done through accessor func    
 57 directly through pointers is frowned upon and     
 58 architectures.  Those accessors are written to    
 59 optimization, so, once again, volatile is unne    
 60                                                   
 61 Another situation where one might be tempted t    
 62 when the processor is busy-waiting on the valu    
 63 way to perform a busy wait is::                   
 64                                                   
 65     while (my_variable != what_i_want)            
 66         cpu_relax();                              
 67                                                   
 68 The cpu_relax() call can lower CPU power consu    
 69 hyperthreaded twin processor; it also happens     
 70 barrier, so, once again, volatile is unnecessa    
 71 waiting is generally an anti-social act to beg    
 72                                                   
 73 There are still a few rare situations where vo    
 74 kernel:                                           
 75                                                   
 76   - The above-mentioned accessor functions mig    
 77     architectures where direct I/O memory acce    
 78     each accessor call becomes a little critic    
 79     ensures that the access happens as expecte    
 80                                                   
 81   - Inline assembly code which changes memory,    
 82     visible side effects, risks being deleted     
 83     keyword to asm statements will prevent thi    
 84                                                   
 85   - The jiffies variable is special in that it    
 86     every time it is referenced, but it can be    
 87     locking.  So jiffies can be volatile, but     
 88     variables of this type is strongly frowned    
 89     to be a "stupid legacy" issue (Linus's wor    
 90     would be more trouble than it is worth.       
 91                                                   
 92   - Pointers to data structures in coherent me    
 93     by I/O devices can, sometimes, legitimatel    
 94     used by a network adapter, where that adap    
 95     indicate which descriptors have been proce    
 96     type of situation.                            
 97                                                   
 98 For most code, none of the above justification    
 99 result, the use of volatile is likely to be se    
100 additional scrutiny to the code.  Developers w    
101 volatile should take a step back and think abo    
102 to accomplish.                                    
103                                                   
104 Patches to remove volatile variables are gener    
105 they come with a justification which shows tha    
106 been properly thought through.                    
107                                                   
108                                                   
109 References                                        
110 ==========                                        
111                                                   
112 [1] https://lwn.net/Articles/233481/              
113                                                   
114 [2] https://lwn.net/Articles/233482/              
115                                                   
116 Credits                                           
117 =======                                           
118                                                   
119 Original impetus and research by Randy Dunlap     
120                                                   
121 Written by Jonathan Corbet                        
122                                                   
123 Improvements via comments from Satyam Sharma,     
124 Juhl, Heikki Orsila, H. Peter Anvin, Philipp H    
125 Richter.                                          
                                                      

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php