1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
216b8a476SEric Dumazet #ifndef _LINUX_U64_STATS_SYNC_H
316b8a476SEric Dumazet #define _LINUX_U64_STATS_SYNC_H
416b8a476SEric Dumazet
516b8a476SEric Dumazet /*
66501bf87SAhmed S. Darwish * Protect against 64-bit values tearing on 32-bit architectures. This is
76501bf87SAhmed S. Darwish * typically used for statistics read/update in different subsystems.
816b8a476SEric Dumazet *
916b8a476SEric Dumazet * Key points :
106501bf87SAhmed S. Darwish *
1144b0c295SThomas Gleixner * - Use a seqcount on 32-bit
126501bf87SAhmed S. Darwish * - The whole thing is a no-op on 64-bit architectures.
136501bf87SAhmed S. Darwish *
146501bf87SAhmed S. Darwish * Usage constraints:
156501bf87SAhmed S. Darwish *
166501bf87SAhmed S. Darwish * 1) Write side must ensure mutual exclusion, or one seqcount update could
1716b8a476SEric Dumazet * be lost, thus blocking readers forever.
186501bf87SAhmed S. Darwish *
196501bf87SAhmed S. Darwish * 2) Write side must disable preemption, or a seqcount reader can preempt the
206501bf87SAhmed S. Darwish * writer and also spin forever.
216501bf87SAhmed S. Darwish *
226501bf87SAhmed S. Darwish * 3) Write side must use the _irqsave() variant if other writers, or a reader,
2344b0c295SThomas Gleixner * can be invoked from an IRQ context. On 64bit systems this variant does not
2444b0c295SThomas Gleixner * disable interrupts.
2516b8a476SEric Dumazet *
2616b8a476SEric Dumazet * 4) If reader fetches several counters, there is no guarantee the whole values
276501bf87SAhmed S. Darwish * are consistent w.r.t. each other (remember point #2: seqcounts are not
286501bf87SAhmed S. Darwish * used for 64bit architectures).
2916b8a476SEric Dumazet *
306501bf87SAhmed S. Darwish * 5) Readers are allowed to sleep or be preempted/interrupted: they perform
316501bf87SAhmed S. Darwish * pure reads.
3216b8a476SEric Dumazet *
3316b8a476SEric Dumazet * Usage :
3416b8a476SEric Dumazet *
3516b8a476SEric Dumazet * Stats producer (writer) should use following template granted it already got
3616b8a476SEric Dumazet * an exclusive access to counters (a lock is already taken, or per cpu
3716b8a476SEric Dumazet * data is used [in a non preemptable context])
3816b8a476SEric Dumazet *
3916b8a476SEric Dumazet * spin_lock_bh(...) or other synchronization to get exclusive access
4016b8a476SEric Dumazet * ...
4116b8a476SEric Dumazet * u64_stats_update_begin(&stats->syncp);
42316580b6SEric Dumazet * u64_stats_add(&stats->bytes64, len); // non atomic operation
43316580b6SEric Dumazet * u64_stats_inc(&stats->packets64); // non atomic operation
4416b8a476SEric Dumazet * u64_stats_update_end(&stats->syncp);
4516b8a476SEric Dumazet *
4616b8a476SEric Dumazet * While a consumer (reader) should use following template to get consistent
4716b8a476SEric Dumazet * snapshot for each variable (but no guarantee on several ones)
4816b8a476SEric Dumazet *
4916b8a476SEric Dumazet * u64 tbytes, tpackets;
5016b8a476SEric Dumazet * unsigned int start;
5116b8a476SEric Dumazet *
5216b8a476SEric Dumazet * do {
5316b8a476SEric Dumazet * start = u64_stats_fetch_begin(&stats->syncp);
54316580b6SEric Dumazet * tbytes = u64_stats_read(&stats->bytes64); // non atomic operation
55316580b6SEric Dumazet * tpackets = u64_stats_read(&stats->packets64); // non atomic operation
56b6b3ecc7SEric Dumazet * } while (u64_stats_fetch_retry(&stats->syncp, start));
5716b8a476SEric Dumazet *
5816b8a476SEric Dumazet *
5916b8a476SEric Dumazet * Example of use in drivers/net/loopback.c, using per_cpu containers,
6016b8a476SEric Dumazet * in BH disabled context.
6116b8a476SEric Dumazet */
6216b8a476SEric Dumazet #include <linux/seqlock.h>
6316b8a476SEric Dumazet
6433d91f00SEric Dumazet struct u64_stats_sync {
6544b0c295SThomas Gleixner #if BITS_PER_LONG == 32
6616b8a476SEric Dumazet seqcount_t seq;
6716b8a476SEric Dumazet #endif
6833d91f00SEric Dumazet };
6933d91f00SEric Dumazet
70316580b6SEric Dumazet #if BITS_PER_LONG == 64
71316580b6SEric Dumazet #include <asm/local64.h>
72316580b6SEric Dumazet
73316580b6SEric Dumazet typedef struct {
74316580b6SEric Dumazet local64_t v;
75316580b6SEric Dumazet } u64_stats_t ;
76316580b6SEric Dumazet
u64_stats_read(const u64_stats_t * p)77316580b6SEric Dumazet static inline u64 u64_stats_read(const u64_stats_t *p)
78316580b6SEric Dumazet {
79316580b6SEric Dumazet return local64_read(&p->v);
80316580b6SEric Dumazet }
81316580b6SEric Dumazet
u64_stats_set(u64_stats_t * p,u64 val)82f2efdb17SAhmed S. Darwish static inline void u64_stats_set(u64_stats_t *p, u64 val)
83f2efdb17SAhmed S. Darwish {
84f2efdb17SAhmed S. Darwish local64_set(&p->v, val);
85f2efdb17SAhmed S. Darwish }
86f2efdb17SAhmed S. Darwish
u64_stats_add(u64_stats_t * p,unsigned long val)87316580b6SEric Dumazet static inline void u64_stats_add(u64_stats_t *p, unsigned long val)
88316580b6SEric Dumazet {
89316580b6SEric Dumazet local64_add(val, &p->v);
90316580b6SEric Dumazet }
91316580b6SEric Dumazet
u64_stats_inc(u64_stats_t * p)92316580b6SEric Dumazet static inline void u64_stats_inc(u64_stats_t *p)
93316580b6SEric Dumazet {
94316580b6SEric Dumazet local64_inc(&p->v);
95316580b6SEric Dumazet }
96316580b6SEric Dumazet
u64_stats_init(struct u64_stats_sync * syncp)9744b0c295SThomas Gleixner static inline void u64_stats_init(struct u64_stats_sync *syncp) { }
__u64_stats_update_begin(struct u64_stats_sync * syncp)9844b0c295SThomas Gleixner static inline void __u64_stats_update_begin(struct u64_stats_sync *syncp) { }
__u64_stats_update_end(struct u64_stats_sync * syncp)9944b0c295SThomas Gleixner static inline void __u64_stats_update_end(struct u64_stats_sync *syncp) { }
__u64_stats_irqsave(void)10044b0c295SThomas Gleixner static inline unsigned long __u64_stats_irqsave(void) { return 0; }
__u64_stats_irqrestore(unsigned long flags)10144b0c295SThomas Gleixner static inline void __u64_stats_irqrestore(unsigned long flags) { }
__u64_stats_fetch_begin(const struct u64_stats_sync * syncp)10244b0c295SThomas Gleixner static inline unsigned int __u64_stats_fetch_begin(const struct u64_stats_sync *syncp)
10344b0c295SThomas Gleixner {
10444b0c295SThomas Gleixner return 0;
10544b0c295SThomas Gleixner }
__u64_stats_fetch_retry(const struct u64_stats_sync * syncp,unsigned int start)10644b0c295SThomas Gleixner static inline bool __u64_stats_fetch_retry(const struct u64_stats_sync *syncp,
10744b0c295SThomas Gleixner unsigned int start)
10844b0c295SThomas Gleixner {
10944b0c295SThomas Gleixner return false;
11044b0c295SThomas Gleixner }
11144b0c295SThomas Gleixner
11244b0c295SThomas Gleixner #else /* 64 bit */
113316580b6SEric Dumazet
114316580b6SEric Dumazet typedef struct {
115316580b6SEric Dumazet u64 v;
116316580b6SEric Dumazet } u64_stats_t;
117316580b6SEric Dumazet
u64_stats_read(const u64_stats_t * p)118316580b6SEric Dumazet static inline u64 u64_stats_read(const u64_stats_t *p)
119316580b6SEric Dumazet {
120316580b6SEric Dumazet return p->v;
121316580b6SEric Dumazet }
122316580b6SEric Dumazet
u64_stats_set(u64_stats_t * p,u64 val)123f2efdb17SAhmed S. Darwish static inline void u64_stats_set(u64_stats_t *p, u64 val)
124f2efdb17SAhmed S. Darwish {
125f2efdb17SAhmed S. Darwish p->v = val;
126f2efdb17SAhmed S. Darwish }
127f2efdb17SAhmed S. Darwish
u64_stats_add(u64_stats_t * p,unsigned long val)128316580b6SEric Dumazet static inline void u64_stats_add(u64_stats_t *p, unsigned long val)
129316580b6SEric Dumazet {
130316580b6SEric Dumazet p->v += val;
131316580b6SEric Dumazet }
132316580b6SEric Dumazet
u64_stats_inc(u64_stats_t * p)133316580b6SEric Dumazet static inline void u64_stats_inc(u64_stats_t *p)
134316580b6SEric Dumazet {
135316580b6SEric Dumazet p->v++;
136316580b6SEric Dumazet }
137827da44cSJohn Stultz
138*38a15d0aSPetr Tesarik #define u64_stats_init(syncp) \
139*38a15d0aSPetr Tesarik do { \
140*38a15d0aSPetr Tesarik struct u64_stats_sync *__s = (syncp); \
141*38a15d0aSPetr Tesarik seqcount_init(&__s->seq); \
142*38a15d0aSPetr Tesarik } while (0)
143827da44cSJohn Stultz
__u64_stats_update_begin(struct u64_stats_sync * syncp)14444b0c295SThomas Gleixner static inline void __u64_stats_update_begin(struct u64_stats_sync *syncp)
14533d91f00SEric Dumazet {
14644b0c295SThomas Gleixner preempt_disable_nested();
14733d91f00SEric Dumazet write_seqcount_begin(&syncp->seq);
14833d91f00SEric Dumazet }
14933d91f00SEric Dumazet
__u64_stats_update_end(struct u64_stats_sync * syncp)15044b0c295SThomas Gleixner static inline void __u64_stats_update_end(struct u64_stats_sync *syncp)
15133d91f00SEric Dumazet {
15233d91f00SEric Dumazet write_seqcount_end(&syncp->seq);
15344b0c295SThomas Gleixner preempt_enable_nested();
15433d91f00SEric Dumazet }
15533d91f00SEric Dumazet
__u64_stats_irqsave(void)15644b0c295SThomas Gleixner static inline unsigned long __u64_stats_irqsave(void)
1572695578bSEric Dumazet {
15844b0c295SThomas Gleixner unsigned long flags;
1592695578bSEric Dumazet
1602695578bSEric Dumazet local_irq_save(flags);
1612695578bSEric Dumazet return flags;
1622695578bSEric Dumazet }
1632695578bSEric Dumazet
__u64_stats_irqrestore(unsigned long flags)16444b0c295SThomas Gleixner static inline void __u64_stats_irqrestore(unsigned long flags)
1652695578bSEric Dumazet {
1662695578bSEric Dumazet local_irq_restore(flags);
1672695578bSEric Dumazet }
1682695578bSEric Dumazet
__u64_stats_fetch_begin(const struct u64_stats_sync * syncp)16968107df5SFrederic Weisbecker static inline unsigned int __u64_stats_fetch_begin(const struct u64_stats_sync *syncp)
17033d91f00SEric Dumazet {
17133d91f00SEric Dumazet return read_seqcount_begin(&syncp->seq);
17268107df5SFrederic Weisbecker }
17368107df5SFrederic Weisbecker
__u64_stats_fetch_retry(const struct u64_stats_sync * syncp,unsigned int start)17468107df5SFrederic Weisbecker static inline bool __u64_stats_fetch_retry(const struct u64_stats_sync *syncp,
17568107df5SFrederic Weisbecker unsigned int start)
17668107df5SFrederic Weisbecker {
17768107df5SFrederic Weisbecker return read_seqcount_retry(&syncp->seq, start);
17844b0c295SThomas Gleixner }
17944b0c295SThomas Gleixner #endif /* !64 bit */
18044b0c295SThomas Gleixner
u64_stats_update_begin(struct u64_stats_sync * syncp)18144b0c295SThomas Gleixner static inline void u64_stats_update_begin(struct u64_stats_sync *syncp)
18244b0c295SThomas Gleixner {
18344b0c295SThomas Gleixner __u64_stats_update_begin(syncp);
18444b0c295SThomas Gleixner }
18544b0c295SThomas Gleixner
u64_stats_update_end(struct u64_stats_sync * syncp)18644b0c295SThomas Gleixner static inline void u64_stats_update_end(struct u64_stats_sync *syncp)
18744b0c295SThomas Gleixner {
18844b0c295SThomas Gleixner __u64_stats_update_end(syncp);
18944b0c295SThomas Gleixner }
19044b0c295SThomas Gleixner
u64_stats_update_begin_irqsave(struct u64_stats_sync * syncp)19144b0c295SThomas Gleixner static inline unsigned long u64_stats_update_begin_irqsave(struct u64_stats_sync *syncp)
19244b0c295SThomas Gleixner {
19344b0c295SThomas Gleixner unsigned long flags = __u64_stats_irqsave();
19444b0c295SThomas Gleixner
19544b0c295SThomas Gleixner __u64_stats_update_begin(syncp);
19644b0c295SThomas Gleixner return flags;
19744b0c295SThomas Gleixner }
19844b0c295SThomas Gleixner
u64_stats_update_end_irqrestore(struct u64_stats_sync * syncp,unsigned long flags)19944b0c295SThomas Gleixner static inline void u64_stats_update_end_irqrestore(struct u64_stats_sync *syncp,
20044b0c295SThomas Gleixner unsigned long flags)
20144b0c295SThomas Gleixner {
20244b0c295SThomas Gleixner __u64_stats_update_end(syncp);
20344b0c295SThomas Gleixner __u64_stats_irqrestore(flags);
20444b0c295SThomas Gleixner }
20544b0c295SThomas Gleixner
u64_stats_fetch_begin(const struct u64_stats_sync * syncp)20644b0c295SThomas Gleixner static inline unsigned int u64_stats_fetch_begin(const struct u64_stats_sync *syncp)
20744b0c295SThomas Gleixner {
20844b0c295SThomas Gleixner return __u64_stats_fetch_begin(syncp);
20933d91f00SEric Dumazet }
21033d91f00SEric Dumazet
u64_stats_fetch_retry(const struct u64_stats_sync * syncp,unsigned int start)211fa9f90beSJesper Juhl static inline bool u64_stats_fetch_retry(const struct u64_stats_sync *syncp,
21233d91f00SEric Dumazet unsigned int start)
21333d91f00SEric Dumazet {
21468107df5SFrederic Weisbecker return __u64_stats_fetch_retry(syncp, start);
21533d91f00SEric Dumazet }
21633d91f00SEric Dumazet
21716b8a476SEric Dumazet #endif /* _LINUX_U64_STATS_SYNC_H */
218