blob: 07ee0f84a46caa9e2b1c446f96009f63b3b99f50 [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001/* SPDX-License-Identifier: GPL-2.0 */
Eric Dumazet16b8a472010-06-22 10:22:17 -07002#ifndef _LINUX_U64_STATS_SYNC_H
3#define _LINUX_U64_STATS_SYNC_H
4
5/*
6 * To properly implement 64bits network statistics on 32bit and 64bit hosts,
7 * we provide a synchronization point, that is a noop on 64bit or UP kernels.
8 *
9 * Key points :
10 * 1) Use a seqcount on SMP 32bits, with low overhead.
11 * 2) Whole thing is a noop on 64bit arches or UP kernels.
12 * 3) Write side must ensure mutual exclusion or one seqcount update could
13 * be lost, thus blocking readers forever.
14 * If this synchronization point is not a mutex, but a spinlock or
15 * spinlock_bh() or disable_bh() :
16 * 3.1) Write side should not sleep.
17 * 3.2) Write side should not allow preemption.
18 * 3.3) If applicable, interrupts should be disabled.
19 *
20 * 4) If reader fetches several counters, there is no guarantee the whole values
21 * are consistent (remember point 1) : this is a noop on 64bit arches anyway)
22 *
23 * 5) readers are allowed to sleep or be preempted/interrupted : They perform
24 * pure reads. But if they have to fetch many values, it's better to not allow
25 * preemptions/interruptions to avoid many retries.
26 *
Eric Dumazetb6b3ecc2010-06-24 00:04:38 +000027 * 6) If counter might be written by an interrupt, readers should block interrupts.
28 * (On UP, there is no seqcount_t protection, a reader allowing interrupts could
29 * read partial values)
30 *
Eric W. Biederman57a77442014-03-13 21:26:42 -070031 * 7) For irq and softirq uses, readers can use u64_stats_fetch_begin_irq() and
32 * u64_stats_fetch_retry_irq() helpers
Eric Dumazet33d91f02010-06-24 00:54:06 +000033 *
Eric Dumazet16b8a472010-06-22 10:22:17 -070034 * Usage :
35 *
36 * Stats producer (writer) should use following template granted it already got
37 * an exclusive access to counters (a lock is already taken, or per cpu
38 * data is used [in a non preemptable context])
39 *
40 * spin_lock_bh(...) or other synchronization to get exclusive access
41 * ...
42 * u64_stats_update_begin(&stats->syncp);
43 * stats->bytes64 += len; // non atomic operation
44 * stats->packets64++; // non atomic operation
45 * u64_stats_update_end(&stats->syncp);
46 *
47 * While a consumer (reader) should use following template to get consistent
48 * snapshot for each variable (but no guarantee on several ones)
49 *
50 * u64 tbytes, tpackets;
51 * unsigned int start;
52 *
53 * do {
54 * start = u64_stats_fetch_begin(&stats->syncp);
55 * tbytes = stats->bytes64; // non atomic operation
56 * tpackets = stats->packets64; // non atomic operation
Eric Dumazetb6b3ecc2010-06-24 00:04:38 +000057 * } while (u64_stats_fetch_retry(&stats->syncp, start));
Eric Dumazet16b8a472010-06-22 10:22:17 -070058 *
59 *
60 * Example of use in drivers/net/loopback.c, using per_cpu containers,
61 * in BH disabled context.
62 */
63#include <linux/seqlock.h>
64
Eric Dumazet33d91f02010-06-24 00:54:06 +000065struct u64_stats_sync {
Eric Dumazet16b8a472010-06-22 10:22:17 -070066#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
Eric Dumazet16b8a472010-06-22 10:22:17 -070067 seqcount_t seq;
Eric Dumazet16b8a472010-06-22 10:22:17 -070068#endif
Eric Dumazet33d91f02010-06-24 00:54:06 +000069};
70
John Stultz827da442013-10-07 15:51:58 -070071
Eric Dumazet9464ca62015-06-12 19:44:48 -070072static inline void u64_stats_init(struct u64_stats_sync *syncp)
73{
John Stultz827da442013-10-07 15:51:58 -070074#if BITS_PER_LONG == 32 && defined(CONFIG_SMP)
Eric Dumazet9464ca62015-06-12 19:44:48 -070075 seqcount_init(&syncp->seq);
John Stultz827da442013-10-07 15:51:58 -070076#endif
Eric Dumazet9464ca62015-06-12 19:44:48 -070077}
John Stultz827da442013-10-07 15:51:58 -070078
Jesper Juhlfa9f90b2010-11-28 21:39:34 +010079static inline void u64_stats_update_begin(struct u64_stats_sync *syncp)
Eric Dumazet33d91f02010-06-24 00:54:06 +000080{
81#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
82 write_seqcount_begin(&syncp->seq);
83#endif
84}
85
Jesper Juhlfa9f90b2010-11-28 21:39:34 +010086static inline void u64_stats_update_end(struct u64_stats_sync *syncp)
Eric Dumazet33d91f02010-06-24 00:54:06 +000087{
88#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
89 write_seqcount_end(&syncp->seq);
90#endif
91}
92
Eric Dumazet26955782018-03-05 11:41:13 -080093static inline unsigned long
94u64_stats_update_begin_irqsave(struct u64_stats_sync *syncp)
95{
96 unsigned long flags = 0;
97
98#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
99 local_irq_save(flags);
100 write_seqcount_begin(&syncp->seq);
101#endif
102 return flags;
103}
104
105static inline void
106u64_stats_update_end_irqrestore(struct u64_stats_sync *syncp,
107 unsigned long flags)
108{
109#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
110 write_seqcount_end(&syncp->seq);
111 local_irq_restore(flags);
112#endif
113}
114
Eric Dumazet46cc6e42016-05-03 16:56:03 -0700115static inline void u64_stats_update_begin_raw(struct u64_stats_sync *syncp)
116{
117#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
118 raw_write_seqcount_begin(&syncp->seq);
119#endif
120}
121
122static inline void u64_stats_update_end_raw(struct u64_stats_sync *syncp)
123{
124#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
125 raw_write_seqcount_end(&syncp->seq);
126#endif
127}
128
Frederic Weisbecker68107df2016-09-26 02:29:19 +0200129static inline unsigned int __u64_stats_fetch_begin(const struct u64_stats_sync *syncp)
Eric Dumazet33d91f02010-06-24 00:54:06 +0000130{
131#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
132 return read_seqcount_begin(&syncp->seq);
133#else
Frederic Weisbecker68107df2016-09-26 02:29:19 +0200134 return 0;
135#endif
136}
137
138static inline unsigned int u64_stats_fetch_begin(const struct u64_stats_sync *syncp)
139{
140#if BITS_PER_LONG==32 && !defined(CONFIG_SMP)
Eric Dumazet33d91f02010-06-24 00:54:06 +0000141 preempt_disable();
142#endif
Frederic Weisbecker68107df2016-09-26 02:29:19 +0200143 return __u64_stats_fetch_begin(syncp);
144}
145
146static inline bool __u64_stats_fetch_retry(const struct u64_stats_sync *syncp,
147 unsigned int start)
148{
149#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
150 return read_seqcount_retry(&syncp->seq, start);
151#else
152 return false;
Eric Dumazet33d91f02010-06-24 00:54:06 +0000153#endif
154}
155
Jesper Juhlfa9f90b2010-11-28 21:39:34 +0100156static inline bool u64_stats_fetch_retry(const struct u64_stats_sync *syncp,
Eric Dumazet33d91f02010-06-24 00:54:06 +0000157 unsigned int start)
158{
Frederic Weisbecker68107df2016-09-26 02:29:19 +0200159#if BITS_PER_LONG==32 && !defined(CONFIG_SMP)
Eric Dumazet33d91f02010-06-24 00:54:06 +0000160 preempt_enable();
161#endif
Frederic Weisbecker68107df2016-09-26 02:29:19 +0200162 return __u64_stats_fetch_retry(syncp, start);
Eric Dumazet33d91f02010-06-24 00:54:06 +0000163}
164
165/*
Eric W. Biederman57a77442014-03-13 21:26:42 -0700166 * In case irq handlers can update u64 counters, readers can use following helpers
Eric Dumazet33d91f02010-06-24 00:54:06 +0000167 * - SMP 32bit arches use seqcount protection, irq safe.
Eric W. Biederman57a77442014-03-13 21:26:42 -0700168 * - UP 32bit must disable irqs.
Eric Dumazet33d91f02010-06-24 00:54:06 +0000169 * - 64bit have no problem atomically reading u64 values, irq safe.
170 */
Eric W. Biederman57a77442014-03-13 21:26:42 -0700171static inline unsigned int u64_stats_fetch_begin_irq(const struct u64_stats_sync *syncp)
Eric Dumazet33d91f02010-06-24 00:54:06 +0000172{
Frederic Weisbecker68107df2016-09-26 02:29:19 +0200173#if BITS_PER_LONG==32 && !defined(CONFIG_SMP)
Eric W. Biederman57a77442014-03-13 21:26:42 -0700174 local_irq_disable();
Eric Dumazet33d91f02010-06-24 00:54:06 +0000175#endif
Frederic Weisbecker68107df2016-09-26 02:29:19 +0200176 return __u64_stats_fetch_begin(syncp);
Eric Dumazet33d91f02010-06-24 00:54:06 +0000177}
178
Eric W. Biederman57a77442014-03-13 21:26:42 -0700179static inline bool u64_stats_fetch_retry_irq(const struct u64_stats_sync *syncp,
Frederic Weisbecker68107df2016-09-26 02:29:19 +0200180 unsigned int start)
Eric Dumazet33d91f02010-06-24 00:54:06 +0000181{
Frederic Weisbecker68107df2016-09-26 02:29:19 +0200182#if BITS_PER_LONG==32 && !defined(CONFIG_SMP)
Eric W. Biederman57a77442014-03-13 21:26:42 -0700183 local_irq_enable();
Eric Dumazet33d91f02010-06-24 00:54:06 +0000184#endif
Frederic Weisbecker68107df2016-09-26 02:29:19 +0200185 return __u64_stats_fetch_retry(syncp, start);
Eric Dumazet33d91f02010-06-24 00:54:06 +0000186}
Eric Dumazet16b8a472010-06-22 10:22:17 -0700187
188#endif /* _LINUX_U64_STATS_SYNC_H */