datapath: Change u64_stats_* to use _irq instead of _bh().
[cascardo/ovs.git] / datapath / linux / compat / include / linux / u64_stats_sync.h
1 #ifndef _LINUX_U64_STATS_SYNC_WRAPPER_H
2 #define _LINUX_U64_STATS_SYNC_WRAPPER_H
3
4 #include <linux/version.h>
5
6 #if defined(HAVE_U64_STATS_FETCH_BEGIN_IRQ) && \
7     LINUX_VERSION_CODE >= KERNEL_VERSION(3,13,0)
8 #include_next <linux/u64_stats_sync.h>
9 #else
10
11 /*
12  * To properly implement 64bits network statistics on 32bit and 64bit hosts,
13  * we provide a synchronization point, that is a noop on 64bit or UP kernels.
14  *
15  * Key points :
16  * 1) Use a seqcount on SMP 32bits, with low overhead.
17  * 2) Whole thing is a noop on 64bit arches or UP kernels.
18  * 3) Write side must ensure mutual exclusion or one seqcount update could
19  *    be lost, thus blocking readers forever.
20  *    If this synchronization point is not a mutex, but a spinlock or
21  *    spinlock_bh() or disable_bh() :
22  * 3.1) Write side should not sleep.
23  * 3.2) Write side should not allow preemption.
24  * 3.3) If applicable, interrupts should be disabled.
25  *
26  * 4) If reader fetches several counters, there is no guarantee the whole values
27  *    are consistent (remember point 1) : this is a noop on 64bit arches anyway)
28  *
29  * 5) readers are allowed to sleep or be preempted/interrupted : They perform
30  *    pure reads. But if they have to fetch many values, it's better to not allow
31  *    preemptions/interruptions to avoid many retries.
32  *
33  * 6) If counter might be written by an interrupt, readers should block interrupts.
34  *    (On UP, there is no seqcount_t protection, a reader allowing interrupts could
35  *     read partial values)
36  *
37  * 7) For irq or softirq uses, readers can use u64_stats_fetch_begin_irq() and
38  *    u64_stats_fetch_retry_irq() helpers
39  *
40  * Usage :
41  *
42  * Stats producer (writer) should use following template granted it already got
43  * an exclusive access to counters (a lock is already taken, or per cpu
44  * data is used [in a non preemptable context])
45  *
46  *   spin_lock_bh(...) or other synchronization to get exclusive access
47  *   ...
48  *   u64_stats_update_begin(&stats->syncp);
49  *   stats->bytes64 += len; // non atomic operation
50  *   stats->packets64++;    // non atomic operation
51  *   u64_stats_update_end(&stats->syncp);
52  *
53  * While a consumer (reader) should use following template to get consistent
54  * snapshot for each variable (but no guarantee on several ones)
55  *
56  * u64 tbytes, tpackets;
57  * unsigned int start;
58  *
59  * do {
60  *         start = u64_stats_fetch_begin(&stats->syncp);
61  *         tbytes = stats->bytes64; // non atomic operation
62  *         tpackets = stats->packets64; // non atomic operation
63  * } while (u64_stats_fetch_retry(&stats->syncp, start));
64  *
65  *
66  * Example of use in drivers/net/loopback.c, using per_cpu containers,
67  * in BH disabled context.
68  */
69 #include <linux/seqlock.h>
70
71 struct u64_stats_sync {
72 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
73         seqcount_t      seq;
74 #endif
75 };
76
77 #if BITS_PER_LONG == 32 && defined(CONFIG_SMP)
78 # define u64_stats_init(syncp)  seqcount_init(syncp.seq)
79 #else
80 # define u64_stats_init(syncp)  do { } while (0)
81 #endif
82
83 static inline void u64_stats_update_begin(struct u64_stats_sync *syncp)
84 {
85 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
86         write_seqcount_begin(&syncp->seq);
87 #endif
88 }
89
90 static inline void u64_stats_update_end(struct u64_stats_sync *syncp)
91 {
92 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
93         write_seqcount_end(&syncp->seq);
94 #endif
95 }
96
97 static inline unsigned int u64_stats_fetch_begin(const struct u64_stats_sync *syncp)
98 {
99 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
100         return read_seqcount_begin(&syncp->seq);
101 #else
102 #if BITS_PER_LONG==32
103         preempt_disable();
104 #endif
105         return 0;
106 #endif
107 }
108
109 static inline bool u64_stats_fetch_retry(const struct u64_stats_sync *syncp,
110                                          unsigned int start)
111 {
112 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
113         return read_seqcount_retry(&syncp->seq, start);
114 #else
115 #if BITS_PER_LONG==32
116         preempt_enable();
117 #endif
118         return false;
119 #endif
120 }
121
122 /*
123  * In case irq handlers can update u64 counters, readers can use following helpers
124  * - SMP 32bit arches use seqcount protection, irq safe.
125  * - UP 32bit must disable irqs.
126  * - 64bit have no problem atomically reading u64 values, irq safe.
127  */
128 static inline unsigned int u64_stats_fetch_begin_irq(const struct u64_stats_sync *syncp)
129 {
130 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
131         return read_seqcount_begin(&syncp->seq);
132 #else
133 #if BITS_PER_LONG==32
134         local_irq_disable();
135 #endif
136         return 0;
137 #endif
138 }
139
140 static inline bool u64_stats_fetch_retry_irq(const struct u64_stats_sync *syncp,
141                                          unsigned int start)
142 {
143 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
144         return read_seqcount_retry(&syncp->seq, start);
145 #else
146 #if BITS_PER_LONG==32
147         local_irq_enable();
148 #endif
149         return false;
150 #endif
151 }
152
153 #endif /* !HAVE_U64_STATS_FETCH_BEGIN_IRQ || kernel < 3.13 */
154
155 #endif /* _LINUX_U64_STATS_SYNC_WRAPPER_H */