95d380e5d2467553fa9a47532685f1e5d444e543
[cascardo/linux.git] / include / linux / percpu.h
1 #ifndef __LINUX_PERCPU_H
2 #define __LINUX_PERCPU_H
3
4 #include <linux/mmdebug.h>
5 #include <linux/preempt.h>
6 #include <linux/smp.h>
7 #include <linux/cpumask.h>
8 #include <linux/pfn.h>
9 #include <linux/init.h>
10
11 #include <asm/percpu.h>
12
13 /* enough to cover all DEFINE_PER_CPUs in modules */
14 #ifdef CONFIG_MODULES
15 #define PERCPU_MODULE_RESERVE           (8 << 10)
16 #else
17 #define PERCPU_MODULE_RESERVE           0
18 #endif
19
20 #ifndef PERCPU_ENOUGH_ROOM
21 #define PERCPU_ENOUGH_ROOM                                              \
22         (ALIGN(__per_cpu_end - __per_cpu_start, SMP_CACHE_BYTES) +      \
23          PERCPU_MODULE_RESERVE)
24 #endif
25
26 /* minimum unit size, also is the maximum supported allocation size */
27 #define PCPU_MIN_UNIT_SIZE              PFN_ALIGN(32 << 10)
28
29 /*
30  * Percpu allocator can serve percpu allocations before slab is
31  * initialized which allows slab to depend on the percpu allocator.
32  * The following two parameters decide how much resource to
33  * preallocate for this.  Keep PERCPU_DYNAMIC_RESERVE equal to or
34  * larger than PERCPU_DYNAMIC_EARLY_SIZE.
35  */
36 #define PERCPU_DYNAMIC_EARLY_SLOTS      128
37 #define PERCPU_DYNAMIC_EARLY_SIZE       (12 << 10)
38
39 /*
40  * PERCPU_DYNAMIC_RESERVE indicates the amount of free area to piggy
41  * back on the first chunk for dynamic percpu allocation if arch is
42  * manually allocating and mapping it for faster access (as a part of
43  * large page mapping for example).
44  *
45  * The following values give between one and two pages of free space
46  * after typical minimal boot (2-way SMP, single disk and NIC) with
47  * both defconfig and a distro config on x86_64 and 32.  More
48  * intelligent way to determine this would be nice.
49  */
50 #if BITS_PER_LONG > 32
51 #define PERCPU_DYNAMIC_RESERVE          (20 << 10)
52 #else
53 #define PERCPU_DYNAMIC_RESERVE          (12 << 10)
54 #endif
55
56 extern void *pcpu_base_addr;
57 extern const unsigned long *pcpu_unit_offsets;
58
59 struct pcpu_group_info {
60         int                     nr_units;       /* aligned # of units */
61         unsigned long           base_offset;    /* base address offset */
62         unsigned int            *cpu_map;       /* unit->cpu map, empty
63                                                  * entries contain NR_CPUS */
64 };
65
66 struct pcpu_alloc_info {
67         size_t                  static_size;
68         size_t                  reserved_size;
69         size_t                  dyn_size;
70         size_t                  unit_size;
71         size_t                  atom_size;
72         size_t                  alloc_size;
73         size_t                  __ai_size;      /* internal, don't use */
74         int                     nr_groups;      /* 0 if grouping unnecessary */
75         struct pcpu_group_info  groups[];
76 };
77
78 enum pcpu_fc {
79         PCPU_FC_AUTO,
80         PCPU_FC_EMBED,
81         PCPU_FC_PAGE,
82
83         PCPU_FC_NR,
84 };
85 extern const char * const pcpu_fc_names[PCPU_FC_NR];
86
87 extern enum pcpu_fc pcpu_chosen_fc;
88
89 typedef void * (*pcpu_fc_alloc_fn_t)(unsigned int cpu, size_t size,
90                                      size_t align);
91 typedef void (*pcpu_fc_free_fn_t)(void *ptr, size_t size);
92 typedef void (*pcpu_fc_populate_pte_fn_t)(unsigned long addr);
93 typedef int (pcpu_fc_cpu_distance_fn_t)(unsigned int from, unsigned int to);
94
95 extern struct pcpu_alloc_info * __init pcpu_alloc_alloc_info(int nr_groups,
96                                                              int nr_units);
97 extern void __init pcpu_free_alloc_info(struct pcpu_alloc_info *ai);
98
99 extern int __init pcpu_setup_first_chunk(const struct pcpu_alloc_info *ai,
100                                          void *base_addr);
101
102 #ifdef CONFIG_NEED_PER_CPU_EMBED_FIRST_CHUNK
103 extern int __init pcpu_embed_first_chunk(size_t reserved_size, size_t dyn_size,
104                                 size_t atom_size,
105                                 pcpu_fc_cpu_distance_fn_t cpu_distance_fn,
106                                 pcpu_fc_alloc_fn_t alloc_fn,
107                                 pcpu_fc_free_fn_t free_fn);
108 #endif
109
110 #ifdef CONFIG_NEED_PER_CPU_PAGE_FIRST_CHUNK
111 extern int __init pcpu_page_first_chunk(size_t reserved_size,
112                                 pcpu_fc_alloc_fn_t alloc_fn,
113                                 pcpu_fc_free_fn_t free_fn,
114                                 pcpu_fc_populate_pte_fn_t populate_pte_fn);
115 #endif
116
117 extern void __percpu *__alloc_reserved_percpu(size_t size, size_t align);
118 extern bool is_kernel_percpu_address(unsigned long addr);
119
120 #if !defined(CONFIG_SMP) || !defined(CONFIG_HAVE_SETUP_PER_CPU_AREA)
121 extern void __init setup_per_cpu_areas(void);
122 #endif
123 extern void __init percpu_init_late(void);
124
125 extern void __percpu *__alloc_percpu(size_t size, size_t align);
126 extern void free_percpu(void __percpu *__pdata);
127 extern phys_addr_t per_cpu_ptr_to_phys(void *addr);
128
129 #define alloc_percpu(type)      \
130         (typeof(type) __percpu *)__alloc_percpu(sizeof(type), __alignof__(type))
131
132 /*
133  * Branching function to split up a function into a set of functions that
134  * are called for different scalar sizes of the objects handled.
135  */
136
137 extern void __bad_size_call_parameter(void);
138
139 #ifdef CONFIG_DEBUG_PREEMPT
140 extern void __this_cpu_preempt_check(const char *op);
141 #else
142 static inline void __this_cpu_preempt_check(const char *op) { }
143 #endif
144
145 #define __pcpu_size_call_return(stem, variable)                         \
146 ({      typeof(variable) pscr_ret__;                                    \
147         __verify_pcpu_ptr(&(variable));                                 \
148         switch(sizeof(variable)) {                                      \
149         case 1: pscr_ret__ = stem##1(variable);break;                   \
150         case 2: pscr_ret__ = stem##2(variable);break;                   \
151         case 4: pscr_ret__ = stem##4(variable);break;                   \
152         case 8: pscr_ret__ = stem##8(variable);break;                   \
153         default:                                                        \
154                 __bad_size_call_parameter();break;                      \
155         }                                                               \
156         pscr_ret__;                                                     \
157 })
158
159 #define __pcpu_size_call_return2(stem, variable, ...)                   \
160 ({                                                                      \
161         typeof(variable) pscr2_ret__;                                   \
162         __verify_pcpu_ptr(&(variable));                                 \
163         switch(sizeof(variable)) {                                      \
164         case 1: pscr2_ret__ = stem##1(variable, __VA_ARGS__); break;    \
165         case 2: pscr2_ret__ = stem##2(variable, __VA_ARGS__); break;    \
166         case 4: pscr2_ret__ = stem##4(variable, __VA_ARGS__); break;    \
167         case 8: pscr2_ret__ = stem##8(variable, __VA_ARGS__); break;    \
168         default:                                                        \
169                 __bad_size_call_parameter(); break;                     \
170         }                                                               \
171         pscr2_ret__;                                                    \
172 })
173
174 /*
175  * Special handling for cmpxchg_double.  cmpxchg_double is passed two
176  * percpu variables.  The first has to be aligned to a double word
177  * boundary and the second has to follow directly thereafter.
178  * We enforce this on all architectures even if they don't support
179  * a double cmpxchg instruction, since it's a cheap requirement, and it
180  * avoids breaking the requirement for architectures with the instruction.
181  */
182 #define __pcpu_double_call_return_bool(stem, pcp1, pcp2, ...)           \
183 ({                                                                      \
184         bool pdcrb_ret__;                                               \
185         __verify_pcpu_ptr(&pcp1);                                       \
186         BUILD_BUG_ON(sizeof(pcp1) != sizeof(pcp2));                     \
187         VM_BUG_ON((unsigned long)(&pcp1) % (2 * sizeof(pcp1)));         \
188         VM_BUG_ON((unsigned long)(&pcp2) !=                             \
189                   (unsigned long)(&pcp1) + sizeof(pcp1));               \
190         switch(sizeof(pcp1)) {                                          \
191         case 1: pdcrb_ret__ = stem##1(pcp1, pcp2, __VA_ARGS__); break;  \
192         case 2: pdcrb_ret__ = stem##2(pcp1, pcp2, __VA_ARGS__); break;  \
193         case 4: pdcrb_ret__ = stem##4(pcp1, pcp2, __VA_ARGS__); break;  \
194         case 8: pdcrb_ret__ = stem##8(pcp1, pcp2, __VA_ARGS__); break;  \
195         default:                                                        \
196                 __bad_size_call_parameter(); break;                     \
197         }                                                               \
198         pdcrb_ret__;                                                    \
199 })
200
201 #define __pcpu_size_call(stem, variable, ...)                           \
202 do {                                                                    \
203         __verify_pcpu_ptr(&(variable));                                 \
204         switch(sizeof(variable)) {                                      \
205                 case 1: stem##1(variable, __VA_ARGS__);break;           \
206                 case 2: stem##2(variable, __VA_ARGS__);break;           \
207                 case 4: stem##4(variable, __VA_ARGS__);break;           \
208                 case 8: stem##8(variable, __VA_ARGS__);break;           \
209                 default:                                                \
210                         __bad_size_call_parameter();break;              \
211         }                                                               \
212 } while (0)
213
214 /*
215  * this_cpu operations (C) 2008-2013 Christoph Lameter <cl@linux.com>
216  *
217  * Optimized manipulation for memory allocated through the per cpu
218  * allocator or for addresses of per cpu variables.
219  *
220  * These operation guarantee exclusivity of access for other operations
221  * on the *same* processor. The assumption is that per cpu data is only
222  * accessed by a single processor instance (the current one).
223  *
224  * The first group is used for accesses that must be done in a
225  * preemption safe way since we know that the context is not preempt
226  * safe. Interrupts may occur. If the interrupt modifies the variable
227  * too then RMW actions will not be reliable.
228  *
229  * The arch code can provide optimized implementation by defining macros
230  * for certain scalar sizes. F.e. provide this_cpu_add_2() to provide per
231  * cpu atomic operations for 2 byte sized RMW actions. If arch code does
232  * not provide operations for a scalar size then the fallback in the
233  * generic code will be used.
234  */
235
236 #define _this_cpu_generic_read(pcp)                                     \
237 ({      typeof(pcp) ret__;                                              \
238         preempt_disable();                                              \
239         ret__ = *this_cpu_ptr(&(pcp));                                  \
240         preempt_enable();                                               \
241         ret__;                                                          \
242 })
243
244 # ifndef this_cpu_read_1
245 #  define this_cpu_read_1(pcp)  _this_cpu_generic_read(pcp)
246 # endif
247 # ifndef this_cpu_read_2
248 #  define this_cpu_read_2(pcp)  _this_cpu_generic_read(pcp)
249 # endif
250 # ifndef this_cpu_read_4
251 #  define this_cpu_read_4(pcp)  _this_cpu_generic_read(pcp)
252 # endif
253 # ifndef this_cpu_read_8
254 #  define this_cpu_read_8(pcp)  _this_cpu_generic_read(pcp)
255 # endif
256 # define this_cpu_read(pcp)     __pcpu_size_call_return(this_cpu_read_, (pcp))
257
258 #define _this_cpu_generic_to_op(pcp, val, op)                           \
259 do {                                                                    \
260         unsigned long flags;                                            \
261         raw_local_irq_save(flags);                                      \
262         *raw_cpu_ptr(&(pcp)) op val;                                    \
263         raw_local_irq_restore(flags);                                   \
264 } while (0)
265
266 # ifndef this_cpu_write_1
267 #  define this_cpu_write_1(pcp, val)    _this_cpu_generic_to_op((pcp), (val), =)
268 # endif
269 # ifndef this_cpu_write_2
270 #  define this_cpu_write_2(pcp, val)    _this_cpu_generic_to_op((pcp), (val), =)
271 # endif
272 # ifndef this_cpu_write_4
273 #  define this_cpu_write_4(pcp, val)    _this_cpu_generic_to_op((pcp), (val), =)
274 # endif
275 # ifndef this_cpu_write_8
276 #  define this_cpu_write_8(pcp, val)    _this_cpu_generic_to_op((pcp), (val), =)
277 # endif
278 # define this_cpu_write(pcp, val)       __pcpu_size_call(this_cpu_write_, (pcp), (val))
279
280 # ifndef this_cpu_add_1
281 #  define this_cpu_add_1(pcp, val)      _this_cpu_generic_to_op((pcp), (val), +=)
282 # endif
283 # ifndef this_cpu_add_2
284 #  define this_cpu_add_2(pcp, val)      _this_cpu_generic_to_op((pcp), (val), +=)
285 # endif
286 # ifndef this_cpu_add_4
287 #  define this_cpu_add_4(pcp, val)      _this_cpu_generic_to_op((pcp), (val), +=)
288 # endif
289 # ifndef this_cpu_add_8
290 #  define this_cpu_add_8(pcp, val)      _this_cpu_generic_to_op((pcp), (val), +=)
291 # endif
292 # define this_cpu_add(pcp, val)         __pcpu_size_call(this_cpu_add_, (pcp), (val))
293
294 # define this_cpu_sub(pcp, val)         this_cpu_add((pcp), -(typeof(pcp))(val))
295 # define this_cpu_inc(pcp)              this_cpu_add((pcp), 1)
296 # define this_cpu_dec(pcp)              this_cpu_sub((pcp), 1)
297
298 # ifndef this_cpu_and_1
299 #  define this_cpu_and_1(pcp, val)      _this_cpu_generic_to_op((pcp), (val), &=)
300 # endif
301 # ifndef this_cpu_and_2
302 #  define this_cpu_and_2(pcp, val)      _this_cpu_generic_to_op((pcp), (val), &=)
303 # endif
304 # ifndef this_cpu_and_4
305 #  define this_cpu_and_4(pcp, val)      _this_cpu_generic_to_op((pcp), (val), &=)
306 # endif
307 # ifndef this_cpu_and_8
308 #  define this_cpu_and_8(pcp, val)      _this_cpu_generic_to_op((pcp), (val), &=)
309 # endif
310 # define this_cpu_and(pcp, val)         __pcpu_size_call(this_cpu_and_, (pcp), (val))
311
312 # ifndef this_cpu_or_1
313 #  define this_cpu_or_1(pcp, val)       _this_cpu_generic_to_op((pcp), (val), |=)
314 # endif
315 # ifndef this_cpu_or_2
316 #  define this_cpu_or_2(pcp, val)       _this_cpu_generic_to_op((pcp), (val), |=)
317 # endif
318 # ifndef this_cpu_or_4
319 #  define this_cpu_or_4(pcp, val)       _this_cpu_generic_to_op((pcp), (val), |=)
320 # endif
321 # ifndef this_cpu_or_8
322 #  define this_cpu_or_8(pcp, val)       _this_cpu_generic_to_op((pcp), (val), |=)
323 # endif
324 # define this_cpu_or(pcp, val)          __pcpu_size_call(this_cpu_or_, (pcp), (val))
325
326 #define _this_cpu_generic_add_return(pcp, val)                          \
327 ({                                                                      \
328         typeof(pcp) ret__;                                              \
329         unsigned long flags;                                            \
330         raw_local_irq_save(flags);                                      \
331         raw_cpu_add(pcp, val);                                  \
332         ret__ = raw_cpu_read(pcp);                                      \
333         raw_local_irq_restore(flags);                                   \
334         ret__;                                                          \
335 })
336
337 # ifndef this_cpu_add_return_1
338 #  define this_cpu_add_return_1(pcp, val)       _this_cpu_generic_add_return(pcp, val)
339 # endif
340 # ifndef this_cpu_add_return_2
341 #  define this_cpu_add_return_2(pcp, val)       _this_cpu_generic_add_return(pcp, val)
342 # endif
343 # ifndef this_cpu_add_return_4
344 #  define this_cpu_add_return_4(pcp, val)       _this_cpu_generic_add_return(pcp, val)
345 # endif
346 # ifndef this_cpu_add_return_8
347 #  define this_cpu_add_return_8(pcp, val)       _this_cpu_generic_add_return(pcp, val)
348 # endif
349 # define this_cpu_add_return(pcp, val)  __pcpu_size_call_return2(this_cpu_add_return_, pcp, val)
350
351 #define this_cpu_sub_return(pcp, val)   this_cpu_add_return(pcp, -(typeof(pcp))(val))
352 #define this_cpu_inc_return(pcp)        this_cpu_add_return(pcp, 1)
353 #define this_cpu_dec_return(pcp)        this_cpu_add_return(pcp, -1)
354
355 #define _this_cpu_generic_xchg(pcp, nval)                               \
356 ({      typeof(pcp) ret__;                                              \
357         unsigned long flags;                                            \
358         raw_local_irq_save(flags);                                      \
359         ret__ = raw_cpu_read(pcp);                                      \
360         raw_cpu_write(pcp, nval);                                       \
361         raw_local_irq_restore(flags);                                   \
362         ret__;                                                          \
363 })
364
365 # ifndef this_cpu_xchg_1
366 #  define this_cpu_xchg_1(pcp, nval)    _this_cpu_generic_xchg(pcp, nval)
367 # endif
368 # ifndef this_cpu_xchg_2
369 #  define this_cpu_xchg_2(pcp, nval)    _this_cpu_generic_xchg(pcp, nval)
370 # endif
371 # ifndef this_cpu_xchg_4
372 #  define this_cpu_xchg_4(pcp, nval)    _this_cpu_generic_xchg(pcp, nval)
373 # endif
374 # ifndef this_cpu_xchg_8
375 #  define this_cpu_xchg_8(pcp, nval)    _this_cpu_generic_xchg(pcp, nval)
376 # endif
377 # define this_cpu_xchg(pcp, nval)       \
378         __pcpu_size_call_return2(this_cpu_xchg_, (pcp), nval)
379
380 #define _this_cpu_generic_cmpxchg(pcp, oval, nval)                      \
381 ({                                                                      \
382         typeof(pcp) ret__;                                              \
383         unsigned long flags;                                            \
384         raw_local_irq_save(flags);                                      \
385         ret__ = raw_cpu_read(pcp);                                      \
386         if (ret__ == (oval))                                            \
387                 raw_cpu_write(pcp, nval);                               \
388         raw_local_irq_restore(flags);                                   \
389         ret__;                                                          \
390 })
391
392 # ifndef this_cpu_cmpxchg_1
393 #  define this_cpu_cmpxchg_1(pcp, oval, nval)   _this_cpu_generic_cmpxchg(pcp, oval, nval)
394 # endif
395 # ifndef this_cpu_cmpxchg_2
396 #  define this_cpu_cmpxchg_2(pcp, oval, nval)   _this_cpu_generic_cmpxchg(pcp, oval, nval)
397 # endif
398 # ifndef this_cpu_cmpxchg_4
399 #  define this_cpu_cmpxchg_4(pcp, oval, nval)   _this_cpu_generic_cmpxchg(pcp, oval, nval)
400 # endif
401 # ifndef this_cpu_cmpxchg_8
402 #  define this_cpu_cmpxchg_8(pcp, oval, nval)   _this_cpu_generic_cmpxchg(pcp, oval, nval)
403 # endif
404 # define this_cpu_cmpxchg(pcp, oval, nval)      \
405         __pcpu_size_call_return2(this_cpu_cmpxchg_, pcp, oval, nval)
406
407 /*
408  * cmpxchg_double replaces two adjacent scalars at once.  The first
409  * two parameters are per cpu variables which have to be of the same
410  * size.  A truth value is returned to indicate success or failure
411  * (since a double register result is difficult to handle).  There is
412  * very limited hardware support for these operations, so only certain
413  * sizes may work.
414  */
415 #define _this_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)        \
416 ({                                                                      \
417         int ret__;                                                      \
418         unsigned long flags;                                            \
419         raw_local_irq_save(flags);                                      \
420         ret__ = raw_cpu_generic_cmpxchg_double(pcp1, pcp2,              \
421                         oval1, oval2, nval1, nval2);                    \
422         raw_local_irq_restore(flags);                                   \
423         ret__;                                                          \
424 })
425
426 # ifndef this_cpu_cmpxchg_double_1
427 #  define this_cpu_cmpxchg_double_1(pcp1, pcp2, oval1, oval2, nval1, nval2)     \
428         _this_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
429 # endif
430 # ifndef this_cpu_cmpxchg_double_2
431 #  define this_cpu_cmpxchg_double_2(pcp1, pcp2, oval1, oval2, nval1, nval2)     \
432         _this_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
433 # endif
434 # ifndef this_cpu_cmpxchg_double_4
435 #  define this_cpu_cmpxchg_double_4(pcp1, pcp2, oval1, oval2, nval1, nval2)     \
436         _this_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
437 # endif
438 # ifndef this_cpu_cmpxchg_double_8
439 #  define this_cpu_cmpxchg_double_8(pcp1, pcp2, oval1, oval2, nval1, nval2)     \
440         _this_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
441 # endif
442 # define this_cpu_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)        \
443         __pcpu_double_call_return_bool(this_cpu_cmpxchg_double_, (pcp1), (pcp2), (oval1), (oval2), (nval1), (nval2))
444
445 /*
446  * Generic percpu operations for contexts where we do not want to do
447  * any checks for preemptiosn.
448  *
449  * If there is no other protection through preempt disable and/or
450  * disabling interupts then one of these RMW operations can show unexpected
451  * behavior because the execution thread was rescheduled on another processor
452  * or an interrupt occurred and the same percpu variable was modified from
453  * the interrupt context.
454  */
455 # ifndef raw_cpu_read_1
456 #  define raw_cpu_read_1(pcp)   (*raw_cpu_ptr(&(pcp)))
457 # endif
458 # ifndef raw_cpu_read_2
459 #  define raw_cpu_read_2(pcp)   (*raw_cpu_ptr(&(pcp)))
460 # endif
461 # ifndef raw_cpu_read_4
462 #  define raw_cpu_read_4(pcp)   (*raw_cpu_ptr(&(pcp)))
463 # endif
464 # ifndef raw_cpu_read_8
465 #  define raw_cpu_read_8(pcp)   (*raw_cpu_ptr(&(pcp)))
466 # endif
467 # define raw_cpu_read(pcp)      __pcpu_size_call_return(raw_cpu_read_, (pcp))
468
469 #define raw_cpu_generic_to_op(pcp, val, op)                             \
470 do {                                                                    \
471         *raw_cpu_ptr(&(pcp)) op val;                                    \
472 } while (0)
473
474 # ifndef raw_cpu_write_1
475 #  define raw_cpu_write_1(pcp, val)     raw_cpu_generic_to_op((pcp), (val), =)
476 # endif
477 # ifndef raw_cpu_write_2
478 #  define raw_cpu_write_2(pcp, val)     raw_cpu_generic_to_op((pcp), (val), =)
479 # endif
480 # ifndef raw_cpu_write_4
481 #  define raw_cpu_write_4(pcp, val)     raw_cpu_generic_to_op((pcp), (val), =)
482 # endif
483 # ifndef raw_cpu_write_8
484 #  define raw_cpu_write_8(pcp, val)     raw_cpu_generic_to_op((pcp), (val), =)
485 # endif
486 # define raw_cpu_write(pcp, val)        __pcpu_size_call(raw_cpu_write_, (pcp), (val))
487
488 # ifndef raw_cpu_add_1
489 #  define raw_cpu_add_1(pcp, val)       raw_cpu_generic_to_op((pcp), (val), +=)
490 # endif
491 # ifndef raw_cpu_add_2
492 #  define raw_cpu_add_2(pcp, val)       raw_cpu_generic_to_op((pcp), (val), +=)
493 # endif
494 # ifndef raw_cpu_add_4
495 #  define raw_cpu_add_4(pcp, val)       raw_cpu_generic_to_op((pcp), (val), +=)
496 # endif
497 # ifndef raw_cpu_add_8
498 #  define raw_cpu_add_8(pcp, val)       raw_cpu_generic_to_op((pcp), (val), +=)
499 # endif
500 # define raw_cpu_add(pcp, val)  __pcpu_size_call(raw_cpu_add_, (pcp), (val))
501
502 # define raw_cpu_sub(pcp, val)  raw_cpu_add((pcp), -(val))
503
504 # define raw_cpu_inc(pcp)               raw_cpu_add((pcp), 1)
505
506 # define raw_cpu_dec(pcp)               raw_cpu_sub((pcp), 1)
507
508 # ifndef raw_cpu_and_1
509 #  define raw_cpu_and_1(pcp, val)       raw_cpu_generic_to_op((pcp), (val), &=)
510 # endif
511 # ifndef raw_cpu_and_2
512 #  define raw_cpu_and_2(pcp, val)       raw_cpu_generic_to_op((pcp), (val), &=)
513 # endif
514 # ifndef raw_cpu_and_4
515 #  define raw_cpu_and_4(pcp, val)       raw_cpu_generic_to_op((pcp), (val), &=)
516 # endif
517 # ifndef raw_cpu_and_8
518 #  define raw_cpu_and_8(pcp, val)       raw_cpu_generic_to_op((pcp), (val), &=)
519 # endif
520 # define raw_cpu_and(pcp, val)  __pcpu_size_call(raw_cpu_and_, (pcp), (val))
521
522 # ifndef raw_cpu_or_1
523 #  define raw_cpu_or_1(pcp, val)        raw_cpu_generic_to_op((pcp), (val), |=)
524 # endif
525 # ifndef raw_cpu_or_2
526 #  define raw_cpu_or_2(pcp, val)        raw_cpu_generic_to_op((pcp), (val), |=)
527 # endif
528 # ifndef raw_cpu_or_4
529 #  define raw_cpu_or_4(pcp, val)        raw_cpu_generic_to_op((pcp), (val), |=)
530 # endif
531 # ifndef raw_cpu_or_8
532 #  define raw_cpu_or_8(pcp, val)        raw_cpu_generic_to_op((pcp), (val), |=)
533 # endif
534 # define raw_cpu_or(pcp, val)   __pcpu_size_call(raw_cpu_or_, (pcp), (val))
535
536 #define raw_cpu_generic_add_return(pcp, val)                            \
537 ({                                                                      \
538         raw_cpu_add(pcp, val);                                          \
539         raw_cpu_read(pcp);                                              \
540 })
541
542 # ifndef raw_cpu_add_return_1
543 #  define raw_cpu_add_return_1(pcp, val)        raw_cpu_generic_add_return(pcp, val)
544 # endif
545 # ifndef raw_cpu_add_return_2
546 #  define raw_cpu_add_return_2(pcp, val)        raw_cpu_generic_add_return(pcp, val)
547 # endif
548 # ifndef raw_cpu_add_return_4
549 #  define raw_cpu_add_return_4(pcp, val)        raw_cpu_generic_add_return(pcp, val)
550 # endif
551 # ifndef raw_cpu_add_return_8
552 #  define raw_cpu_add_return_8(pcp, val)        raw_cpu_generic_add_return(pcp, val)
553 # endif
554 # define raw_cpu_add_return(pcp, val)   \
555         __pcpu_size_call_return2(raw_cpu_add_return_, pcp, val)
556
557 #define raw_cpu_sub_return(pcp, val)    raw_cpu_add_return(pcp, -(typeof(pcp))(val))
558 #define raw_cpu_inc_return(pcp) raw_cpu_add_return(pcp, 1)
559 #define raw_cpu_dec_return(pcp) raw_cpu_add_return(pcp, -1)
560
561 #define raw_cpu_generic_xchg(pcp, nval)                                 \
562 ({      typeof(pcp) ret__;                                              \
563         ret__ = raw_cpu_read(pcp);                                      \
564         raw_cpu_write(pcp, nval);                                       \
565         ret__;                                                          \
566 })
567
568 # ifndef raw_cpu_xchg_1
569 #  define raw_cpu_xchg_1(pcp, nval)     raw_cpu_generic_xchg(pcp, nval)
570 # endif
571 # ifndef raw_cpu_xchg_2
572 #  define raw_cpu_xchg_2(pcp, nval)     raw_cpu_generic_xchg(pcp, nval)
573 # endif
574 # ifndef raw_cpu_xchg_4
575 #  define raw_cpu_xchg_4(pcp, nval)     raw_cpu_generic_xchg(pcp, nval)
576 # endif
577 # ifndef raw_cpu_xchg_8
578 #  define raw_cpu_xchg_8(pcp, nval)     raw_cpu_generic_xchg(pcp, nval)
579 # endif
580 # define raw_cpu_xchg(pcp, nval)        \
581         __pcpu_size_call_return2(raw_cpu_xchg_, (pcp), nval)
582
583 #define raw_cpu_generic_cmpxchg(pcp, oval, nval)                        \
584 ({                                                                      \
585         typeof(pcp) ret__;                                              \
586         ret__ = raw_cpu_read(pcp);                                      \
587         if (ret__ == (oval))                                            \
588                 raw_cpu_write(pcp, nval);                               \
589         ret__;                                                          \
590 })
591
592 # ifndef raw_cpu_cmpxchg_1
593 #  define raw_cpu_cmpxchg_1(pcp, oval, nval)    raw_cpu_generic_cmpxchg(pcp, oval, nval)
594 # endif
595 # ifndef raw_cpu_cmpxchg_2
596 #  define raw_cpu_cmpxchg_2(pcp, oval, nval)    raw_cpu_generic_cmpxchg(pcp, oval, nval)
597 # endif
598 # ifndef raw_cpu_cmpxchg_4
599 #  define raw_cpu_cmpxchg_4(pcp, oval, nval)    raw_cpu_generic_cmpxchg(pcp, oval, nval)
600 # endif
601 # ifndef raw_cpu_cmpxchg_8
602 #  define raw_cpu_cmpxchg_8(pcp, oval, nval)    raw_cpu_generic_cmpxchg(pcp, oval, nval)
603 # endif
604 # define raw_cpu_cmpxchg(pcp, oval, nval)       \
605         __pcpu_size_call_return2(raw_cpu_cmpxchg_, pcp, oval, nval)
606
607 #define raw_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)  \
608 ({                                                                      \
609         int __ret = 0;                                                  \
610         if (raw_cpu_read(pcp1) == (oval1) &&                            \
611                          raw_cpu_read(pcp2)  == (oval2)) {              \
612                 raw_cpu_write(pcp1, (nval1));                           \
613                 raw_cpu_write(pcp2, (nval2));                           \
614                 __ret = 1;                                              \
615         }                                                               \
616         (__ret);                                                        \
617 })
618
619 # ifndef raw_cpu_cmpxchg_double_1
620 #  define raw_cpu_cmpxchg_double_1(pcp1, pcp2, oval1, oval2, nval1, nval2)      \
621         raw_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
622 # endif
623 # ifndef raw_cpu_cmpxchg_double_2
624 #  define raw_cpu_cmpxchg_double_2(pcp1, pcp2, oval1, oval2, nval1, nval2)      \
625         raw_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
626 # endif
627 # ifndef raw_cpu_cmpxchg_double_4
628 #  define raw_cpu_cmpxchg_double_4(pcp1, pcp2, oval1, oval2, nval1, nval2)      \
629         raw_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
630 # endif
631 # ifndef raw_cpu_cmpxchg_double_8
632 #  define raw_cpu_cmpxchg_double_8(pcp1, pcp2, oval1, oval2, nval1, nval2)      \
633         raw_cpu_generic_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)
634 # endif
635 # define raw_cpu_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2) \
636         __pcpu_double_call_return_bool(raw_cpu_cmpxchg_double_, (pcp1), (pcp2), (oval1), (oval2), (nval1), (nval2))
637
638 /*
639  * Generic percpu operations for context that are safe from preemption/interrupts.
640  */
641 # define __this_cpu_read(pcp) \
642         (__this_cpu_preempt_check("read"),__pcpu_size_call_return(raw_cpu_read_, (pcp)))
643
644 # define __this_cpu_write(pcp, val)                                     \
645 do { __this_cpu_preempt_check("write");                                 \
646      __pcpu_size_call(raw_cpu_write_, (pcp), (val));                    \
647 } while (0)
648
649 # define __this_cpu_add(pcp, val)                                        \
650 do { __this_cpu_preempt_check("add");                                   \
651         __pcpu_size_call(raw_cpu_add_, (pcp), (val));                   \
652 } while (0)
653
654 # define __this_cpu_sub(pcp, val)       __this_cpu_add((pcp), -(typeof(pcp))(val))
655 # define __this_cpu_inc(pcp)            __this_cpu_add((pcp), 1)
656 # define __this_cpu_dec(pcp)            __this_cpu_sub((pcp), 1)
657
658 # define __this_cpu_and(pcp, val)                                       \
659 do { __this_cpu_preempt_check("and");                                   \
660         __pcpu_size_call(raw_cpu_and_, (pcp), (val));                   \
661 } while (0)
662
663 # define __this_cpu_or(pcp, val)                                        \
664 do { __this_cpu_preempt_check("or");                                    \
665         __pcpu_size_call(raw_cpu_or_, (pcp), (val));                    \
666 } while (0)
667
668 # define __this_cpu_add_return(pcp, val)        \
669         (__this_cpu_preempt_check("add_return"),__pcpu_size_call_return2(raw_cpu_add_return_, pcp, val))
670
671 #define __this_cpu_sub_return(pcp, val) __this_cpu_add_return(pcp, -(typeof(pcp))(val))
672 #define __this_cpu_inc_return(pcp)      __this_cpu_add_return(pcp, 1)
673 #define __this_cpu_dec_return(pcp)      __this_cpu_add_return(pcp, -1)
674
675 # define __this_cpu_xchg(pcp, nval)     \
676         (__this_cpu_preempt_check("xchg"),__pcpu_size_call_return2(raw_cpu_xchg_, (pcp), nval))
677
678 # define __this_cpu_cmpxchg(pcp, oval, nval)    \
679         (__this_cpu_preempt_check("cmpxchg"),__pcpu_size_call_return2(raw_cpu_cmpxchg_, pcp, oval, nval))
680
681 # define __this_cpu_cmpxchg_double(pcp1, pcp2, oval1, oval2, nval1, nval2)      \
682         (__this_cpu_preempt_check("cmpxchg_double"),__pcpu_double_call_return_bool(raw_cpu_cmpxchg_double_, (pcp1), (pcp2), (oval1), (oval2), (nval1), (nval2)))
683
684 #endif /* __LINUX_PERCPU_H */