tty: expose new methods needed for drivers to get termios right
[linux-2.6-block.git] / Documentation / atomic_ops.txt
CommitLineData
1da177e4
LT
1 Semantics and Behavior of Atomic and
2 Bitmask Operations
3
4 David S. Miller
5
6 This document is intended to serve as a guide to Linux port
7maintainers on how to implement atomic counter, bitops, and spinlock
8interfaces properly.
9
10 The atomic_t type should be defined as a signed integer.
11Also, it should be made opaque such that any kind of cast to a normal
12C integer type will fail. Something like the following should
13suffice:
14
15 typedef struct { volatile int counter; } atomic_t;
16
1a2142b0
GG
17local_t is very similar to atomic_t. If the counter is per CPU and only
18updated by one CPU, local_t is probably more appropriate. Please see
19Documentation/local_ops.txt for the semantics of local_t.
20
1da177e4
LT
21 The first operations to implement for atomic_t's are the
22initializers and plain reads.
23
24 #define ATOMIC_INIT(i) { (i) }
25 #define atomic_set(v, i) ((v)->counter = (i))
26
27The first macro is used in definitions, such as:
28
29static atomic_t my_counter = ATOMIC_INIT(1);
30
31The second interface can be used at runtime, as in:
32
33 struct foo { atomic_t counter; };
34 ...
35
36 struct foo *k;
37
38 k = kmalloc(sizeof(*k), GFP_KERNEL);
39 if (!k)
40 return -ENOMEM;
41 atomic_set(&k->counter, 0);
42
43Next, we have:
44
45 #define atomic_read(v) ((v)->counter)
46
47which simply reads the current value of the counter.
48
49Now, we move onto the actual atomic operation interfaces.
50
51 void atomic_add(int i, atomic_t *v);
52 void atomic_sub(int i, atomic_t *v);
53 void atomic_inc(atomic_t *v);
54 void atomic_dec(atomic_t *v);
55
56These four routines add and subtract integral values to/from the given
57atomic_t value. The first two routines pass explicit integers by
58which to make the adjustment, whereas the latter two use an implicit
59adjustment value of "1".
60
61One very important aspect of these two routines is that they DO NOT
62require any explicit memory barriers. They need only perform the
63atomic_t counter update in an SMP safe manner.
64
65Next, we have:
66
67 int atomic_inc_return(atomic_t *v);
68 int atomic_dec_return(atomic_t *v);
69
70These routines add 1 and subtract 1, respectively, from the given
71atomic_t and return the new counter value after the operation is
72performed.
73
74Unlike the above routines, it is required that explicit memory
75barriers are performed before and after the operation. It must be
76done such that all memory operations before and after the atomic
77operation calls are strongly ordered with respect to the atomic
78operation itself.
79
80For example, it should behave as if a smp_mb() call existed both
81before and after the atomic operation.
82
83If the atomic instructions used in an implementation provide explicit
84memory barrier semantics which satisfy the above requirements, that is
85fine as well.
86
87Let's move on:
88
89 int atomic_add_return(int i, atomic_t *v);
90 int atomic_sub_return(int i, atomic_t *v);
91
92These behave just like atomic_{inc,dec}_return() except that an
93explicit counter adjustment is given instead of the implicit "1".
94This means that like atomic_{inc,dec}_return(), the memory barrier
95semantics are required.
96
97Next:
98
99 int atomic_inc_and_test(atomic_t *v);
100 int atomic_dec_and_test(atomic_t *v);
101
102These two routines increment and decrement by 1, respectively, the
103given atomic counter. They return a boolean indicating whether the
104resulting counter value was zero or not.
105
106It requires explicit memory barrier semantics around the operation as
107above.
108
109 int atomic_sub_and_test(int i, atomic_t *v);
110
111This is identical to atomic_dec_and_test() except that an explicit
112decrement is given instead of the implicit "1". It requires explicit
113memory barrier semantics around the operation.
114
115 int atomic_add_negative(int i, atomic_t *v);
116
117The given increment is added to the given atomic counter value. A
118boolean is return which indicates whether the resulting counter value
119is negative. It requires explicit memory barrier semantics around the
120operation.
121
8426e1f6 122Then:
4a6dae6d
NP
123
124 int atomic_cmpxchg(atomic_t *v, int old, int new);
125
126This performs an atomic compare exchange operation on the atomic value v,
127with the given old and new values. Like all atomic_xxx operations,
128atomic_cmpxchg will only satisfy its atomicity semantics as long as all
129other accesses of *v are performed through atomic_xxx operations.
130
131atomic_cmpxchg requires explicit memory barriers around the operation.
132
133The semantics for atomic_cmpxchg are the same as those defined for 'cas'
134below.
135
8426e1f6
NP
136Finally:
137
138 int atomic_add_unless(atomic_t *v, int a, int u);
139
140If the atomic value v is not equal to u, this function adds a to v, and
141returns non zero. If v is equal to u then it returns zero. This is done as
142an atomic operation.
143
144atomic_add_unless requires explicit memory barriers around the operation.
145
146atomic_inc_not_zero, equivalent to atomic_add_unless(v, 1, 0)
147
4a6dae6d 148
1da177e4
LT
149If a caller requires memory barrier semantics around an atomic_t
150operation which does not return a value, a set of interfaces are
151defined which accomplish this:
152
153 void smp_mb__before_atomic_dec(void);
154 void smp_mb__after_atomic_dec(void);
155 void smp_mb__before_atomic_inc(void);
4249e08e 156 void smp_mb__after_atomic_inc(void);
1da177e4
LT
157
158For example, smp_mb__before_atomic_dec() can be used like so:
159
160 obj->dead = 1;
161 smp_mb__before_atomic_dec();
162 atomic_dec(&obj->ref_count);
163
a0ebb3ff 164It makes sure that all memory operations preceding the atomic_dec()
1da177e4 165call are strongly ordered with respect to the atomic counter
a0ebb3ff 166operation. In the above example, it guarantees that the assignment of
1da177e4
LT
167"1" to obj->dead will be globally visible to other cpus before the
168atomic counter decrement.
169
a0ebb3ff 170Without the explicit smp_mb__before_atomic_dec() call, the
1da177e4
LT
171implementation could legally allow the atomic counter update visible
172to other cpus before the "obj->dead = 1;" assignment.
173
174The other three interfaces listed are used to provide explicit
175ordering with respect to memory operations after an atomic_dec() call
176(smp_mb__after_atomic_dec()) and around atomic_inc() calls
177(smp_mb__{before,after}_atomic_inc()).
178
179A missing memory barrier in the cases where they are required by the
a0ebb3ff
MH
180atomic_t implementation above can have disastrous results. Here is
181an example, which follows a pattern occurring frequently in the Linux
1da177e4
LT
182kernel. It is the use of atomic counters to implement reference
183counting, and it works such that once the counter falls to zero it can
a0ebb3ff 184be guaranteed that no other entity can be accessing the object:
1da177e4
LT
185
186static void obj_list_add(struct obj *obj)
187{
188 obj->active = 1;
189 list_add(&obj->list);
190}
191
192static void obj_list_del(struct obj *obj)
193{
194 list_del(&obj->list);
195 obj->active = 0;
196}
197
198static void obj_destroy(struct obj *obj)
199{
200 BUG_ON(obj->active);
201 kfree(obj);
202}
203
204struct obj *obj_list_peek(struct list_head *head)
205{
206 if (!list_empty(head)) {
207 struct obj *obj;
208
209 obj = list_entry(head->next, struct obj, list);
210 atomic_inc(&obj->refcnt);
211 return obj;
212 }
213 return NULL;
214}
215
216void obj_poke(void)
217{
218 struct obj *obj;
219
220 spin_lock(&global_list_lock);
221 obj = obj_list_peek(&global_list);
222 spin_unlock(&global_list_lock);
223
224 if (obj) {
225 obj->ops->poke(obj);
226 if (atomic_dec_and_test(&obj->refcnt))
227 obj_destroy(obj);
228 }
229}
230
231void obj_timeout(struct obj *obj)
232{
233 spin_lock(&global_list_lock);
234 obj_list_del(obj);
235 spin_unlock(&global_list_lock);
236
237 if (atomic_dec_and_test(&obj->refcnt))
238 obj_destroy(obj);
239}
240
241(This is a simplification of the ARP queue management in the
242 generic neighbour discover code of the networking. Olaf Kirch
243 found a bug wrt. memory barriers in kfree_skb() that exposed
244 the atomic_t memory barrier requirements quite clearly.)
245
246Given the above scheme, it must be the case that the obj->active
247update done by the obj list deletion be visible to other processors
248before the atomic counter decrement is performed.
249
250Otherwise, the counter could fall to zero, yet obj->active would still
251be set, thus triggering the assertion in obj_destroy(). The error
252sequence looks like this:
253
254 cpu 0 cpu 1
255 obj_poke() obj_timeout()
256 obj = obj_list_peek();
257 ... gains ref to obj, refcnt=2
258 obj_list_del(obj);
259 obj->active = 0 ...
260 ... visibility delayed ...
261 atomic_dec_and_test()
262 ... refcnt drops to 1 ...
263 atomic_dec_and_test()
264 ... refcount drops to 0 ...
265 obj_destroy()
266 BUG() triggers since obj->active
267 still seen as one
268 obj->active update visibility occurs
269
270With the memory barrier semantics required of the atomic_t operations
271which return values, the above sequence of memory visibility can never
272happen. Specifically, in the above case the atomic_dec_and_test()
273counter decrement would not become globally visible until the
274obj->active update does.
275
276As a historical note, 32-bit Sparc used to only allow usage of
27724-bits of it's atomic_t type. This was because it used 8 bits
278as a spinlock for SMP safety. Sparc32 lacked a "compare and swap"
279type instruction. However, 32-bit Sparc has since been moved over
280to a "hash table of spinlocks" scheme, that allows the full 32-bit
281counter to be realized. Essentially, an array of spinlocks are
282indexed into based upon the address of the atomic_t being operated
283on, and that lock protects the atomic operation. Parisc uses the
284same scheme.
285
286Another note is that the atomic_t operations returning values are
287extremely slow on an old 386.
288
289We will now cover the atomic bitmask operations. You will find that
290their SMP and memory barrier semantics are similar in shape and scope
291to the atomic_t ops above.
292
293Native atomic bit operations are defined to operate on objects aligned
294to the size of an "unsigned long" C data type, and are least of that
295size. The endianness of the bits within each "unsigned long" are the
296native endianness of the cpu.
297
a0ebb3ff
MH
298 void set_bit(unsigned long nr, volatile unsigned long *addr);
299 void clear_bit(unsigned long nr, volatile unsigned long *addr);
300 void change_bit(unsigned long nr, volatile unsigned long *addr);
1da177e4
LT
301
302These routines set, clear, and change, respectively, the bit number
303indicated by "nr" on the bit mask pointed to by "ADDR".
304
305They must execute atomically, yet there are no implicit memory barrier
306semantics required of these interfaces.
307
a0ebb3ff
MH
308 int test_and_set_bit(unsigned long nr, volatile unsigned long *addr);
309 int test_and_clear_bit(unsigned long nr, volatile unsigned long *addr);
310 int test_and_change_bit(unsigned long nr, volatile unsigned long *addr);
1da177e4
LT
311
312Like the above, except that these routines return a boolean which
313indicates whether the changed bit was set _BEFORE_ the atomic bit
314operation.
315
316WARNING! It is incredibly important that the value be a boolean,
317ie. "0" or "1". Do not try to be fancy and save a few instructions by
318declaring the above to return "long" and just returning something like
319"old_val & mask" because that will not work.
320
321For one thing, this return value gets truncated to int in many code
322paths using these interfaces, so on 64-bit if the bit is set in the
323upper 32-bits then testers will never see that.
324
325One great example of where this problem crops up are the thread_info
326flag operations. Routines such as test_and_set_ti_thread_flag() chop
327the return value into an int. There are other places where things
328like this occur as well.
329
330These routines, like the atomic_t counter operations returning values,
331require explicit memory barrier semantics around their execution. All
332memory operations before the atomic bit operation call must be made
333visible globally before the atomic bit operation is made visible.
334Likewise, the atomic bit operation must be visible globally before any
335subsequent memory operation is made visible. For example:
336
337 obj->dead = 1;
338 if (test_and_set_bit(0, &obj->flags))
339 /* ... */;
340 obj->killed = 1;
341
a0ebb3ff 342The implementation of test_and_set_bit() must guarantee that
1da177e4
LT
343"obj->dead = 1;" is visible to cpus before the atomic memory operation
344done by test_and_set_bit() becomes visible. Likewise, the atomic
345memory operation done by test_and_set_bit() must become visible before
346"obj->killed = 1;" is visible.
347
348Finally there is the basic operation:
349
350 int test_bit(unsigned long nr, __const__ volatile unsigned long *addr);
351
352Which returns a boolean indicating if bit "nr" is set in the bitmask
353pointed to by "addr".
354
355If explicit memory barriers are required around clear_bit() (which
356does not return a value, and thus does not need to provide memory
357barrier semantics), two interfaces are provided:
358
359 void smp_mb__before_clear_bit(void);
360 void smp_mb__after_clear_bit(void);
361
362They are used as follows, and are akin to their atomic_t operation
363brothers:
364
365 /* All memory operations before this call will
366 * be globally visible before the clear_bit().
367 */
368 smp_mb__before_clear_bit();
369 clear_bit( ... );
370
371 /* The clear_bit() will be visible before all
372 * subsequent memory operations.
373 */
374 smp_mb__after_clear_bit();
375
376Finally, there are non-atomic versions of the bitmask operations
377provided. They are used in contexts where some other higher-level SMP
378locking scheme is being used to protect the bitmask, and thus less
379expensive non-atomic operations may be used in the implementation.
380They have names similar to the above bitmask operation interfaces,
381except that two underscores are prefixed to the interface name.
382
383 void __set_bit(unsigned long nr, volatile unsigned long *addr);
384 void __clear_bit(unsigned long nr, volatile unsigned long *addr);
385 void __change_bit(unsigned long nr, volatile unsigned long *addr);
386 int __test_and_set_bit(unsigned long nr, volatile unsigned long *addr);
387 int __test_and_clear_bit(unsigned long nr, volatile unsigned long *addr);
388 int __test_and_change_bit(unsigned long nr, volatile unsigned long *addr);
389
390These non-atomic variants also do not require any special memory
391barrier semantics.
392
393The routines xchg() and cmpxchg() need the same exact memory barriers
394as the atomic and bit operations returning values.
395
396Spinlocks and rwlocks have memory barrier expectations as well.
397The rule to follow is simple:
398
3991) When acquiring a lock, the implementation must make it globally
400 visible before any subsequent memory operation.
401
4022) When releasing a lock, the implementation must make it such that
403 all previous memory operations are globally visible before the
404 lock release.
405
406Which finally brings us to _atomic_dec_and_lock(). There is an
407architecture-neutral version implemented in lib/dec_and_lock.c,
408but most platforms will wish to optimize this in assembler.
409
410 int _atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock);
411
412Atomically decrement the given counter, and if will drop to zero
413atomically acquire the given spinlock and perform the decrement
414of the counter to zero. If it does not drop to zero, do nothing
415with the spinlock.
416
417It is actually pretty simple to get the memory barrier correct.
418Simply satisfy the spinlock grab requirements, which is make
419sure the spinlock operation is globally visible before any
420subsequent memory operation.
421
422We can demonstrate this operation more clearly if we define
423an abstract atomic operation:
424
425 long cas(long *mem, long old, long new);
426
427"cas" stands for "compare and swap". It atomically:
428
4291) Compares "old" with the value currently at "mem".
4302) If they are equal, "new" is written to "mem".
4313) Regardless, the current value at "mem" is returned.
432
433As an example usage, here is what an atomic counter update
434might look like:
435
436void example_atomic_inc(long *counter)
437{
438 long old, new, ret;
439
440 while (1) {
441 old = *counter;
442 new = old + 1;
443
444 ret = cas(counter, old, new);
445 if (ret == old)
446 break;
447 }
448}
449
450Let's use cas() in order to build a pseudo-C atomic_dec_and_lock():
451
452int _atomic_dec_and_lock(atomic_t *atomic, spinlock_t *lock)
453{
454 long old, new, ret;
455 int went_to_zero;
456
457 went_to_zero = 0;
458 while (1) {
459 old = atomic_read(atomic);
460 new = old - 1;
461 if (new == 0) {
462 went_to_zero = 1;
463 spin_lock(lock);
464 }
465 ret = cas(atomic, old, new);
466 if (ret == old)
467 break;
468 if (went_to_zero) {
469 spin_unlock(lock);
470 went_to_zero = 0;
471 }
472 }
473
474 return went_to_zero;
475}
476
477Now, as far as memory barriers go, as long as spin_lock()
478strictly orders all subsequent memory operations (including
479the cas()) with respect to itself, things will be fine.
480
a0ebb3ff 481Said another way, _atomic_dec_and_lock() must guarantee that
1da177e4
LT
482a counter dropping to zero is never made visible before the
483spinlock being acquired.
484
485Note that this also means that for the case where the counter
486is not dropping to zero, there are no memory ordering
487requirements.