Whamcloud - gitweb
LU-17744 ldiskfs: mballoc stats fixes
[fs/lustre-release.git] / libcfs / include / libcfs / linux / processor.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Misc low level processor primitives */
3 #ifndef _LINUX_PROCESSOR_H
4 #define _LINUX_PROCESSOR_H
5
6 #include <asm/processor.h>
7
8 /*
9  * spin_begin is used before beginning a busy-wait loop, and must be paired
10  * with spin_end when the loop is exited. spin_cpu_relax must be called
11  * within the loop.
12  *
13  * The loop body should be as small and fast as possible, on the order of
14  * tens of instructions/cycles as a guide. It should and avoid calling
15  * cpu_relax, or any "spin" or sleep type of primitive including nested uses
16  * of these primitives. It should not lock or take any other resource.
17  * Violations of these guidelies will not cause a bug, but may cause sub
18  * optimal performance.
19  *
20  * These loops are optimized to be used where wait times are expected to be
21  * less than the cost of a context switch (and associated overhead).
22  *
23  * Detection of resource owner and decision to spin or sleep or guest-yield
24  * (e.g., spin lock holder vcpu preempted, or mutex owner not on CPU) can be
25  * tested within the loop body.
26  */
27 #ifndef spin_begin
28 #ifdef CONFIG_PPC64
29 #define spin_begin()    HMT_low()
30 #else
31 #define spin_begin()
32 #endif /* CONFIG_PPC64 */
33 #endif /* spin_begin */
34
35 #ifndef spin_cpu_relax
36 #define spin_cpu_relax() cpu_relax()
37 #endif
38
39 /*
40  * spin_cpu_yield may be called to yield (undirected) to the hypervisor if
41  * necessary. This should be used if the wait is expected to take longer
42  * than context switch overhead, but we can't sleep or do a directed yield.
43  */
44 #ifndef spin_cpu_yield
45 #define spin_cpu_yield() cpu_relax_yield()
46 #endif
47
48 #ifndef spin_end
49 #ifdef CONFIG_PPC64
50 #define spin_end()      HMT_medium()
51 #else
52 #define spin_end()
53 #endif /* CONFIG_PPC64 */
54 #endif /* spin_end */
55
56 /*
57  * spin_until_cond can be used to wait for a condition to become true. It
58  * may be expected that the first iteration will true in the common case
59  * (no spinning), so that callers should not require a first "likely" test
60  * for the uncontended case before using this primitive.
61  *
62  * Usage and implementation guidelines are the same as for the spin_begin
63  * primitives, above.
64  */
65 #ifndef spin_until_cond
66 #define spin_until_cond(cond)                                   \
67 do {                                                            \
68         if (unlikely(!(cond))) {                                \
69                 spin_begin();                                   \
70                 do {                                            \
71                         spin_cpu_relax();                       \
72                 } while (!(cond));                              \
73                 spin_end();                                     \
74         }                                                       \
75 } while (0)
76
77 #endif
78
79 #endif /* _LINUX_PROCESSOR_H */