]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blame - include/linux/u64_stats_sync.h
mm: memcontrol: convert NR_ANON_THPS account to pages
[mirror_ubuntu-jammy-kernel.git] / include / linux / u64_stats_sync.h
CommitLineData
b2441318 1/* SPDX-License-Identifier: GPL-2.0 */
16b8a476
ED
2#ifndef _LINUX_U64_STATS_SYNC_H
3#define _LINUX_U64_STATS_SYNC_H
4
5/*
6501bf87
AD
6 * Protect against 64-bit values tearing on 32-bit architectures. This is
7 * typically used for statistics read/update in different subsystems.
16b8a476
ED
8 *
9 * Key points :
6501bf87
AD
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
16b8a476 17 * be lost, thus blocking readers forever.
16b8a476 18 *
6501bf87
AD
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.
16b8a476 24 *
6501bf87
AD
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).
16b8a476 28 *
6501bf87
AD
29 * 5) Readers are allowed to sleep or be preempted/interrupted: they perform
30 * pure reads.
b6b3ecc7 31 *
6501bf87
AD
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.
33d91f00 36 *
16b8a476
ED
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);
316580b6
ED
46 * u64_stats_add(&stats->bytes64, len); // non atomic operation
47 * u64_stats_inc(&stats->packets64); // non atomic operation
16b8a476
ED
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);
316580b6
ED
58 * tbytes = u64_stats_read(&stats->bytes64); // non atomic operation
59 * tpackets = u64_stats_read(&stats->packets64); // non atomic operation
b6b3ecc7 60 * } while (u64_stats_fetch_retry(&stats->syncp, start));
16b8a476
ED
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
16b8a476 68struct u64_stats_sync {
33d91f00 69#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
16b8a476 70 seqcount_t seq;
33d91f00 71#endif
16b8a476
ED
72};
73
316580b6
ED
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_add(u64_stats_t *p, unsigned long val)
87{
88 local64_add(val, &p->v);
89}
90
91static inline void u64_stats_inc(u64_stats_t *p)
92{
93 local64_inc(&p->v);
94}
95
96#else
97
98typedef struct {
99 u64 v;
100} u64_stats_t;
101
102static inline u64 u64_stats_read(const u64_stats_t *p)
103{
104 return p->v;
105}
106
107static inline void u64_stats_add(u64_stats_t *p, unsigned long val)
108{
109 p->v += val;
110}
111
112static inline void u64_stats_inc(u64_stats_t *p)
113{
114 p->v++;
115}
116#endif
827da44c 117
9464ca65
ED
118static inline void u64_stats_init(struct u64_stats_sync *syncp)
119{
827da44c 120#if BITS_PER_LONG == 32 && defined(CONFIG_SMP)
9464ca65 121 seqcount_init(&syncp->seq);
827da44c 122#endif
9464ca65 123}
827da44c 124
fa9f90be 125static inline void u64_stats_update_begin(struct u64_stats_sync *syncp)
16b8a476 126{
33d91f00 127#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
16b8a476 128 write_seqcount_begin(&syncp->seq);
33d91f00 129#endif
16b8a476
ED
130}
131
fa9f90be 132static inline void u64_stats_update_end(struct u64_stats_sync *syncp)
16b8a476 133{
33d91f00 134#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
16b8a476 135 write_seqcount_end(&syncp->seq);
33d91f00 136#endif
16b8a476
ED
137}
138
2695578b
ED
139static inline unsigned long
140u64_stats_update_begin_irqsave(struct u64_stats_sync *syncp)
141{
142 unsigned long flags = 0;
143
144#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
145 local_irq_save(flags);
146 write_seqcount_begin(&syncp->seq);
147#endif
148 return flags;
149}
150
151static inline void
152u64_stats_update_end_irqrestore(struct u64_stats_sync *syncp,
153 unsigned long flags)
154{
155#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
156 write_seqcount_end(&syncp->seq);
157 local_irq_restore(flags);
158#endif
159}
160
68107df5 161static inline unsigned int __u64_stats_fetch_begin(const struct u64_stats_sync *syncp)
16b8a476 162{
33d91f00 163#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
16b8a476 164 return read_seqcount_begin(&syncp->seq);
33d91f00 165#else
33d91f00
ED
166 return 0;
167#endif
16b8a476
ED
168}
169
68107df5
FW
170static inline unsigned int u64_stats_fetch_begin(const struct u64_stats_sync *syncp)
171{
172#if BITS_PER_LONG==32 && !defined(CONFIG_SMP)
173 preempt_disable();
174#endif
175 return __u64_stats_fetch_begin(syncp);
176}
177
178static inline bool __u64_stats_fetch_retry(const struct u64_stats_sync *syncp,
16b8a476
ED
179 unsigned int start)
180{
33d91f00 181#if BITS_PER_LONG==32 && defined(CONFIG_SMP)
16b8a476 182 return read_seqcount_retry(&syncp->seq, start);
16b8a476 183#else
33d91f00
ED
184 return false;
185#endif
16b8a476
ED
186}
187
68107df5
FW
188static inline bool u64_stats_fetch_retry(const struct u64_stats_sync *syncp,
189 unsigned int start)
190{
191#if BITS_PER_LONG==32 && !defined(CONFIG_SMP)
192 preempt_enable();
193#endif
194 return __u64_stats_fetch_retry(syncp, start);
195}
196
33d91f00 197/*
57a7744e 198 * In case irq handlers can update u64 counters, readers can use following helpers
33d91f00 199 * - SMP 32bit arches use seqcount protection, irq safe.
57a7744e 200 * - UP 32bit must disable irqs.
33d91f00
ED
201 * - 64bit have no problem atomically reading u64 values, irq safe.
202 */
57a7744e 203static inline unsigned int u64_stats_fetch_begin_irq(const struct u64_stats_sync *syncp)
16b8a476 204{
68107df5 205#if BITS_PER_LONG==32 && !defined(CONFIG_SMP)
57a7744e 206 local_irq_disable();
33d91f00 207#endif
68107df5 208 return __u64_stats_fetch_begin(syncp);
16b8a476
ED
209}
210
57a7744e 211static inline bool u64_stats_fetch_retry_irq(const struct u64_stats_sync *syncp,
68107df5 212 unsigned int start)
16b8a476 213{
68107df5 214#if BITS_PER_LONG==32 && !defined(CONFIG_SMP)
57a7744e 215 local_irq_enable();
33d91f00 216#endif
68107df5 217 return __u64_stats_fetch_retry(syncp, start);
33d91f00 218}
16b8a476
ED
219
220#endif /* _LINUX_U64_STATS_SYNC_H */