xref: /linux/include/linux/u64_stats_sync.h (revision 1e13f27e0675552161ab1778be9a23a636dde8a7)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef _LINUX_U64_STATS_SYNC_H
3 #define _LINUX_U64_STATS_SYNC_H
4 
5 /*
6  * Protect against 64-bit values tearing on 32-bit architectures. This is
7  * typically used for statistics read/update in different subsystems.
8  *
9  * Key points :
10  *
11  * -  Use a seqcount on 32-bit
12  * -  The whole thing is a no-op on 64-bit architectures.
13  *
14  * Usage constraints:
15  *
16  * 1) Write side must ensure mutual exclusion, or one seqcount update could
17  *    be lost, thus blocking readers forever.
18  *
19  * 2) Write side must disable preemption, or a seqcount reader can preempt the
20  *    writer and also spin forever.
21  *
22  * 3) Write side must use the _irqsave() variant if other writers, or a reader,
23  *    can be invoked from an IRQ context. On 64bit systems this variant does not
24  *    disable interrupts.
25  *
26  * 4) If reader fetches several counters, there is no guarantee the whole values
27  *    are consistent w.r.t. each other (remember point #2: seqcounts are not
28  *    used for 64bit architectures).
29  *
30  * 5) Readers are allowed to sleep or be preempted/interrupted: they perform
31  *    pure reads.
32  *
33  * Usage :
34  *
35  * Stats producer (writer) should use following template granted it already got
36  * an exclusive access to counters (a lock is already taken, or per cpu
37  * data is used [in a non preemptable context])
38  *
39  *   spin_lock_bh(...) or other synchronization to get exclusive access
40  *   ...
41  *   u64_stats_update_begin(&stats->syncp);
42  *   u64_stats_add(&stats->bytes64, len); // non atomic operation
43  *   u64_stats_inc(&stats->packets64);    // non atomic operation
44  *   u64_stats_update_end(&stats->syncp);
45  *
46  * While a consumer (reader) should use following template to get consistent
47  * snapshot for each variable (but no guarantee on several ones)
48  *
49  * u64 tbytes, tpackets;
50  * unsigned int start;
51  *
52  * do {
53  *         start = u64_stats_fetch_begin(&stats->syncp);
54  *         tbytes = u64_stats_read(&stats->bytes64); // non atomic operation
55  *         tpackets = u64_stats_read(&stats->packets64); // non atomic operation
56  * } while (u64_stats_fetch_retry(&stats->syncp, start));
57  *
58  *
59  * Example of use in drivers/net/loopback.c, using per_cpu containers,
60  * in BH disabled context.
61  */
62 #include <linux/seqlock.h>
63 
64 struct u64_stats_sync {
65 #if BITS_PER_LONG == 32
66 	seqcount_t	seq;
67 #endif
68 };
69 
70 #if BITS_PER_LONG == 64
71 #include <asm/local64.h>
72 
73 typedef struct {
74 	local64_t	v;
75 } u64_stats_t ;
76 
77 static inline u64 u64_stats_read(const u64_stats_t *p)
78 {
79 	return local64_read(&p->v);
80 }
81 
82 static inline void *u64_stats_copy(void *dst, const void *src, size_t len)
83 {
84 	BUILD_BUG_ON(len % sizeof(u64_stats_t));
85 	for (size_t i = 0; i < len / sizeof(u64_stats_t); i++)
86 		((u64 *)dst)[i] = local64_read(&((local64_t *)src)[i]);
87 	return dst;
88 }
89 
90 static inline void u64_stats_set(u64_stats_t *p, u64 val)
91 {
92 	local64_set(&p->v, val);
93 }
94 
95 static inline void u64_stats_add(u64_stats_t *p, unsigned long val)
96 {
97 	local64_add(val, &p->v);
98 }
99 
100 static inline void u64_stats_sub(u64_stats_t *p, s64 val)
101 {
102 	local64_sub(val, &p->v);
103 }
104 
105 static inline void u64_stats_inc(u64_stats_t *p)
106 {
107 	local64_inc(&p->v);
108 }
109 
110 static inline void u64_stats_init(struct u64_stats_sync *syncp) { }
111 static inline void __u64_stats_update_begin(struct u64_stats_sync *syncp) { }
112 static inline void __u64_stats_update_end(struct u64_stats_sync *syncp) { }
113 static inline unsigned long __u64_stats_irqsave(void) { return 0; }
114 static inline void __u64_stats_irqrestore(unsigned long flags) { }
115 static inline unsigned int __u64_stats_fetch_begin(const struct u64_stats_sync *syncp)
116 {
117 	return 0;
118 }
119 static inline bool __u64_stats_fetch_retry(const struct u64_stats_sync *syncp,
120 					   unsigned int start)
121 {
122 	return false;
123 }
124 
125 #else /* 64 bit */
126 #include <linux/string.h>
127 
128 typedef struct {
129 	u64		v;
130 } u64_stats_t;
131 
132 static inline u64 u64_stats_read(const u64_stats_t *p)
133 {
134 	return p->v;
135 }
136 
137 static inline void *u64_stats_copy(void *dst, const void *src, size_t len)
138 {
139 	BUILD_BUG_ON(len % sizeof(u64_stats_t));
140 	return memcpy(dst, src, len);
141 }
142 
143 static inline void u64_stats_set(u64_stats_t *p, u64 val)
144 {
145 	p->v = val;
146 }
147 
148 static inline void u64_stats_add(u64_stats_t *p, unsigned long val)
149 {
150 	p->v += val;
151 }
152 
153 static inline void u64_stats_sub(u64_stats_t *p, s64 val)
154 {
155 	p->v -= val;
156 }
157 
158 static inline void u64_stats_inc(u64_stats_t *p)
159 {
160 	p->v++;
161 }
162 
163 #define u64_stats_init(syncp)				\
164 	do {						\
165 		struct u64_stats_sync *__s = (syncp);	\
166 		seqcount_init(&__s->seq);		\
167 	} while (0)
168 
169 static inline void __u64_stats_update_begin(struct u64_stats_sync *syncp)
170 {
171 	preempt_disable_nested();
172 	write_seqcount_begin(&syncp->seq);
173 }
174 
175 static inline void __u64_stats_update_end(struct u64_stats_sync *syncp)
176 {
177 	write_seqcount_end(&syncp->seq);
178 	preempt_enable_nested();
179 }
180 
181 static inline unsigned long __u64_stats_irqsave(void)
182 {
183 	unsigned long flags;
184 
185 	local_irq_save(flags);
186 	return flags;
187 }
188 
189 static inline void __u64_stats_irqrestore(unsigned long flags)
190 {
191 	local_irq_restore(flags);
192 }
193 
194 static inline unsigned int __u64_stats_fetch_begin(const struct u64_stats_sync *syncp)
195 {
196 	return read_seqcount_begin(&syncp->seq);
197 }
198 
199 static inline bool __u64_stats_fetch_retry(const struct u64_stats_sync *syncp,
200 					   unsigned int start)
201 {
202 	return read_seqcount_retry(&syncp->seq, start);
203 }
204 #endif /* !64 bit */
205 
206 static inline void u64_stats_update_begin(struct u64_stats_sync *syncp)
207 {
208 	__u64_stats_update_begin(syncp);
209 }
210 
211 static inline void u64_stats_update_end(struct u64_stats_sync *syncp)
212 {
213 	__u64_stats_update_end(syncp);
214 }
215 
216 static inline unsigned long u64_stats_update_begin_irqsave(struct u64_stats_sync *syncp)
217 {
218 	unsigned long flags = __u64_stats_irqsave();
219 
220 	__u64_stats_update_begin(syncp);
221 	return flags;
222 }
223 
224 static inline void u64_stats_update_end_irqrestore(struct u64_stats_sync *syncp,
225 						   unsigned long flags)
226 {
227 	__u64_stats_update_end(syncp);
228 	__u64_stats_irqrestore(flags);
229 }
230 
231 static inline unsigned int u64_stats_fetch_begin(const struct u64_stats_sync *syncp)
232 {
233 	return __u64_stats_fetch_begin(syncp);
234 }
235 
236 static inline bool u64_stats_fetch_retry(const struct u64_stats_sync *syncp,
237 					 unsigned int start)
238 {
239 	return __u64_stats_fetch_retry(syncp, start);
240 }
241 
242 #endif /* _LINUX_U64_STATS_SYNC_H */
243