1
2#ifndef _LINUX_U64_STATS_SYNC_H
3#define _LINUX_U64_STATS_SYNC_H
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66#include <linux/seqlock.h>
67
68struct u64_stats_sync {
69#if BITS_PER_LONG == 32 && (defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT))
70 seqcount_t seq;
71#endif
72};
73
74#if BITS_PER_LONG == 64
75#include <asm/local64.h>
76
77typedef struct {
78 local64_t v;
79} u64_stats_t ;
80
81static inline u64 u64_stats_read(const u64_stats_t *p)
82{
83 return local64_read(&p->v);
84}
85
86static inline void u64_stats_set(u64_stats_t *p, u64 val)
87{
88 local64_set(&p->v, val);
89}
90
91static inline void u64_stats_add(u64_stats_t *p, unsigned long val)
92{
93 local64_add(val, &p->v);
94}
95
96static inline void u64_stats_inc(u64_stats_t *p)
97{
98 local64_inc(&p->v);
99}
100
101#else
102
103typedef struct {
104 u64 v;
105} u64_stats_t;
106
107static inline u64 u64_stats_read(const u64_stats_t *p)
108{
109 return p->v;
110}
111
112static inline void u64_stats_set(u64_stats_t *p, u64 val)
113{
114 p->v = val;
115}
116
117static inline void u64_stats_add(u64_stats_t *p, unsigned long val)
118{
119 p->v += val;
120}
121
122static 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) || defined(CONFIG_PREEMPT_RT))
129#define u64_stats_init(syncp) seqcount_init(&(syncp)->seq)
130#else
131static inline void u64_stats_init(struct u64_stats_sync *syncp)
132{
133}
134#endif
135
136static inline void u64_stats_update_begin(struct u64_stats_sync *syncp)
137{
138#if BITS_PER_LONG == 32 && (defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT))
139 if (IS_ENABLED(CONFIG_PREEMPT_RT))
140 preempt_disable();
141 write_seqcount_begin(&syncp->seq);
142#endif
143}
144
145static inline void u64_stats_update_end(struct u64_stats_sync *syncp)
146{
147#if BITS_PER_LONG == 32 && (defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT))
148 write_seqcount_end(&syncp->seq);
149 if (IS_ENABLED(CONFIG_PREEMPT_RT))
150 preempt_enable();
151#endif
152}
153
154static inline unsigned long
155u64_stats_update_begin_irqsave(struct u64_stats_sync *syncp)
156{
157 unsigned long flags = 0;
158
159#if BITS_PER_LONG == 32 && (defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT))
160 if (IS_ENABLED(CONFIG_PREEMPT_RT))
161 preempt_disable();
162 else
163 local_irq_save(flags);
164 write_seqcount_begin(&syncp->seq);
165#endif
166 return flags;
167}
168
169static inline void
170u64_stats_update_end_irqrestore(struct u64_stats_sync *syncp,
171 unsigned long flags)
172{
173#if BITS_PER_LONG == 32 && (defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT))
174 write_seqcount_end(&syncp->seq);
175 if (IS_ENABLED(CONFIG_PREEMPT_RT))
176 preempt_enable();
177 else
178 local_irq_restore(flags);
179#endif
180}
181
182static inline unsigned int __u64_stats_fetch_begin(const struct u64_stats_sync *syncp)
183{
184#if BITS_PER_LONG == 32 && (defined(CONFIG_SMP) || defined(CONFIG_PREEMPT_RT))
185 return read_seqcount_begin(&syncp->seq);
186#else
187 return 0;
188#endif
189}
190
191static inline unsigned int u64_stats_fetch_begin(const struct u64_stats_sync *syncp)
192{
193#if BITS_PER_LONG == 32 && (!defined(CONFIG_SMP) && !defined(CONFIG_PREEMPT_RT))
194 preempt_disable();
195#endif
196 return __u64_stats_fetch_begin(syncp);
197}
198
199static 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) || defined(CONFIG_PREEMPT_RT))
203 return read_seqcount_retry(&syncp->seq, start);
204#else
205 return false;
206#endif
207}
208
209static inline bool u64_stats_fetch_retry(const struct u64_stats_sync *syncp,
210 unsigned int start)
211{
212#if BITS_PER_LONG == 32 && (!defined(CONFIG_SMP) && !defined(CONFIG_PREEMPT_RT))
213 preempt_enable();
214#endif
215 return __u64_stats_fetch_retry(syncp, start);
216}
217
218
219
220
221
222
223
224static inline unsigned int u64_stats_fetch_begin_irq(const struct u64_stats_sync *syncp)
225{
226#if BITS_PER_LONG == 32 && defined(CONFIG_PREEMPT_RT)
227 preempt_disable();
228#elif BITS_PER_LONG == 32 && !defined(CONFIG_SMP)
229 local_irq_disable();
230#endif
231 return __u64_stats_fetch_begin(syncp);
232}
233
234static inline bool u64_stats_fetch_retry_irq(const struct u64_stats_sync *syncp,
235 unsigned int start)
236{
237#if BITS_PER_LONG == 32 && defined(CONFIG_PREEMPT_RT)
238 preempt_enable();
239#elif BITS_PER_LONG == 32 && !defined(CONFIG_SMP)
240 local_irq_enable();
241#endif
242 return __u64_stats_fetch_retry(syncp, start);
243}
244
245#endif
246