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 SMP, only disable preemption for 32-bit UP.
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.
24  *
25  * 4) If reader fetches several counters, there is no guarantee the whole values
26  *    are consistent w.r.t. each other (remember point #2: seqcounts are not
27  *    used for 64bit architectures).
28  *
29  * 5) Readers are allowed to sleep or be preempted/interrupted: they perform
30  *    pure reads.
31  *
32  * 6) Readers must use both u64_stats_fetch_{begin,retry}_irq() if the stats
33  *    might be updated from a hardirq or softirq context (remember point #1:
34  *    seqcounts are not used for UP kernels). 32-bit UP stat readers could read
35  *    corrupted 64-bit values otherwise.
36  *
37  * Usage :
38  *
39  * Stats producer (writer) should use following template granted it already got
40  * an exclusive access to counters (a lock is already taken, or per cpu
41  * data is used [in a non preemptable context])
42  *
43  *   spin_lock_bh(...) or other synchronization to get exclusive access
44  *   ...
45  *   u64_stats_update_begin(&stats->syncp);
46  *   u64_stats_add(&stats->bytes64, len); // non atomic operation
47  *   u64_stats_inc(&stats->packets64);    // non atomic operation
48  *   u64_stats_update_end(&stats->syncp);
49  *
50  * While a consumer (reader) should use following template to get consistent
51  * snapshot for each variable (but no guarantee on several ones)
52  *
53  * u64 tbytes, tpackets;
54  * unsigned int start;
55  *
56  * do {
57  *         start = u64_stats_fetch_begin(&stats->syncp);
58  *         tbytes = u64_stats_read(&stats->bytes64); // non atomic operation
59  *         tpackets = u64_stats_read(&stats->packets64); // non atomic operation
60  * } while (u64_stats_fetch_retry(&stats->syncp, start));
61  *
62  *
63  * Example of use in drivers/net/loopback.c, using per_cpu containers,
64  * in BH disabled context.
65  */
66 #include <linux/seqlock.h>
67 
68 struct u64_stats_sync {
69 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
70 	seqcount_t	seq;
71 #endif
72 };
73 
74 #if BITS_PER_LONG == 64
75 #include <asm/local64.h>
76 
77 typedef struct {
78 	local64_t	v;
79 } u64_stats_t ;
80 
u64_stats_read(const u64_stats_t * p)81 static inline u64 u64_stats_read(const u64_stats_t *p)
82 {
83 	return local64_read(&p->v);
84 }
85 
u64_stats_set(u64_stats_t * p,u64 val)86 static inline void u64_stats_set(u64_stats_t *p, u64 val)
87 {
88 	local64_set(&p->v, val);
89 }
90 
u64_stats_add(u64_stats_t * p,unsigned long val)91 static inline void u64_stats_add(u64_stats_t *p, unsigned long val)
92 {
93 	local64_add(val, &p->v);
94 }
95 
u64_stats_inc(u64_stats_t * p)96 static inline void u64_stats_inc(u64_stats_t *p)
97 {
98 	local64_inc(&p->v);
99 }
100 
101 #else
102 
103 typedef struct {
104 	u64		v;
105 } u64_stats_t;
106 
u64_stats_read(const u64_stats_t * p)107 static inline u64 u64_stats_read(const u64_stats_t *p)
108 {
109 	return p->v;
110 }
111 
u64_stats_set(u64_stats_t * p,u64 val)112 static inline void u64_stats_set(u64_stats_t *p, u64 val)
113 {
114 	p->v = val;
115 }
116 
u64_stats_add(u64_stats_t * p,unsigned long val)117 static inline void u64_stats_add(u64_stats_t *p, unsigned long val)
118 {
119 	p->v += val;
120 }
121 
u64_stats_inc(u64_stats_t * p)122 static inline void u64_stats_inc(u64_stats_t *p)
123 {
124 	p->v++;
125 }
126 #endif
127 
128 #if BITS_PER_LONG == 32 && defined(CONFIG_SMP)
129 #define u64_stats_init(syncp)	seqcount_init(&(syncp)->seq)
130 #else
u64_stats_init(struct u64_stats_sync * syncp)131 static inline void u64_stats_init(struct u64_stats_sync *syncp)
132 {
133 }
134 #endif
135 
u64_stats_update_begin(struct u64_stats_sync * syncp)136 static inline void u64_stats_update_begin(struct u64_stats_sync *syncp)
137 {
138 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
139 	write_seqcount_begin(&syncp->seq);
140 #endif
141 }
142 
u64_stats_update_end(struct u64_stats_sync * syncp)143 static inline void u64_stats_update_end(struct u64_stats_sync *syncp)
144 {
145 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
146 	write_seqcount_end(&syncp->seq);
147 #endif
148 }
149 
150 static inline unsigned long
u64_stats_update_begin_irqsave(struct u64_stats_sync * syncp)151 u64_stats_update_begin_irqsave(struct u64_stats_sync *syncp)
152 {
153 	unsigned long flags = 0;
154 
155 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
156 	local_irq_save(flags);
157 	write_seqcount_begin(&syncp->seq);
158 #endif
159 	return flags;
160 }
161 
162 static inline void
u64_stats_update_end_irqrestore(struct u64_stats_sync * syncp,unsigned long flags)163 u64_stats_update_end_irqrestore(struct u64_stats_sync *syncp,
164 				unsigned long flags)
165 {
166 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
167 	write_seqcount_end(&syncp->seq);
168 	local_irq_restore(flags);
169 #endif
170 }
171 
__u64_stats_fetch_begin(const struct u64_stats_sync * syncp)172 static inline unsigned int __u64_stats_fetch_begin(const struct u64_stats_sync *syncp)
173 {
174 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
175 	return read_seqcount_begin(&syncp->seq);
176 #else
177 	return 0;
178 #endif
179 }
180 
u64_stats_fetch_begin(const struct u64_stats_sync * syncp)181 static inline unsigned int u64_stats_fetch_begin(const struct u64_stats_sync *syncp)
182 {
183 #if BITS_PER_LONG==32 && !defined(CONFIG_SMP)
184 	preempt_disable();
185 #endif
186 	return __u64_stats_fetch_begin(syncp);
187 }
188 
__u64_stats_fetch_retry(const struct u64_stats_sync * syncp,unsigned int start)189 static inline bool __u64_stats_fetch_retry(const struct u64_stats_sync *syncp,
190 					 unsigned int start)
191 {
192 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
193 	return read_seqcount_retry(&syncp->seq, start);
194 #else
195 	return false;
196 #endif
197 }
198 
u64_stats_fetch_retry(const struct u64_stats_sync * syncp,unsigned int start)199 static inline bool u64_stats_fetch_retry(const struct u64_stats_sync *syncp,
200 					 unsigned int start)
201 {
202 #if BITS_PER_LONG==32 && !defined(CONFIG_SMP)
203 	preempt_enable();
204 #endif
205 	return __u64_stats_fetch_retry(syncp, start);
206 }
207 
208 /*
209  * In case irq handlers can update u64 counters, readers can use following helpers
210  * - SMP 32bit arches use seqcount protection, irq safe.
211  * - UP 32bit must disable irqs.
212  * - 64bit have no problem atomically reading u64 values, irq safe.
213  */
u64_stats_fetch_begin_irq(const struct u64_stats_sync * syncp)214 static inline unsigned int u64_stats_fetch_begin_irq(const struct u64_stats_sync *syncp)
215 {
216 #if BITS_PER_LONG==32 && !defined(CONFIG_SMP)
217 	local_irq_disable();
218 #endif
219 	return __u64_stats_fetch_begin(syncp);
220 }
221 
u64_stats_fetch_retry_irq(const struct u64_stats_sync * syncp,unsigned int start)222 static inline bool u64_stats_fetch_retry_irq(const struct u64_stats_sync *syncp,
223 					     unsigned int start)
224 {
225 #if BITS_PER_LONG==32 && !defined(CONFIG_SMP)
226 	local_irq_enable();
227 #endif
228 	return __u64_stats_fetch_retry(syncp, start);
229 }
230 
231 #endif /* _LINUX_U64_STATS_SYNC_H */
232