1 #ifndef _LINUX_U64_STATS_SYNC_WRAPPER_H
2 #define _LINUX_U64_STATS_SYNC_WRAPPER_H
4 #include <linux/version.h>
6 #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,36)
7 #include_next <linux/u64_stats_sync.h>
11 * To properly implement 64bits network statistics on 32bit and 64bit hosts,
12 * we provide a synchronization point, that is a noop on 64bit or UP kernels.
15 * 1) Use a seqcount on SMP 32bits, with low overhead.
16 * 2) Whole thing is a noop on 64bit arches or UP kernels.
17 * 3) Write side must ensure mutual exclusion or one seqcount update could
18 * be lost, thus blocking readers forever.
19 * If this synchronization point is not a mutex, but a spinlock or
20 * spinlock_bh() or disable_bh() :
21 * 3.1) Write side should not sleep.
22 * 3.2) Write side should not allow preemption.
23 * 3.3) If applicable, interrupts should be disabled.
25 * 4) If reader fetches several counters, there is no guarantee the whole values
26 * are consistent (remember point 1) : this is a noop on 64bit arches anyway)
28 * 5) readers are allowed to sleep or be preempted/interrupted : They perform
29 * pure reads. But if they have to fetch many values, it's better to not allow
30 * preemptions/interruptions to avoid many retries.
32 * 6) If counter might be written by an interrupt, readers should block interrupts.
33 * (On UP, there is no seqcount_t protection, a reader allowing interrupts could
34 * read partial values)
36 * 7) For softirq uses, readers can use u64_stats_fetch_begin_bh() and
37 * u64_stats_fetch_retry_bh() helpers
41 * Stats producer (writer) should use following template granted it already got
42 * an exclusive access to counters (a lock is already taken, or per cpu
43 * data is used [in a non preemptable context])
45 * spin_lock_bh(...) or other synchronization to get exclusive access
47 * u64_stats_update_begin(&stats->syncp);
48 * stats->bytes64 += len; // non atomic operation
49 * stats->packets64++; // non atomic operation
50 * u64_stats_update_end(&stats->syncp);
52 * While a consumer (reader) should use following template to get consistent
53 * snapshot for each variable (but no guarantee on several ones)
55 * u64 tbytes, tpackets;
59 * start = u64_stats_fetch_begin(&stats->syncp);
60 * tbytes = stats->bytes64; // non atomic operation
61 * tpackets = stats->packets64; // non atomic operation
62 * } while (u64_stats_fetch_retry(&stats->syncp, start));
65 * Example of use in drivers/net/loopback.c, using per_cpu containers,
66 * in BH disabled context.
68 #include <linux/seqlock.h>
70 struct u64_stats_sync {
71 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
76 static inline void u64_stats_update_begin(struct u64_stats_sync *syncp)
78 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
79 write_seqcount_begin(&syncp->seq);
83 static inline void u64_stats_update_end(struct u64_stats_sync *syncp)
85 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
86 write_seqcount_end(&syncp->seq);
90 static inline unsigned int u64_stats_fetch_begin(const struct u64_stats_sync *syncp)
92 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
93 return read_seqcount_begin(&syncp->seq);
102 static inline bool u64_stats_fetch_retry(const struct u64_stats_sync *syncp,
105 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
106 return read_seqcount_retry(&syncp->seq, start);
108 #if BITS_PER_LONG==32
116 * In case softirq handlers can update u64 counters, readers can use following helpers
117 * - SMP 32bit arches use seqcount protection, irq safe.
118 * - UP 32bit must disable BH.
119 * - 64bit have no problem atomically reading u64 values, irq safe.
121 static inline unsigned int u64_stats_fetch_begin_bh(const struct u64_stats_sync *syncp)
123 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
124 return read_seqcount_begin(&syncp->seq);
126 #if BITS_PER_LONG==32
133 static inline bool u64_stats_fetch_retry_bh(const struct u64_stats_sync *syncp,
136 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
137 return read_seqcount_retry(&syncp->seq, start);
139 #if BITS_PER_LONG==32
146 #endif /* Linux kernel < 2.6.36 */
147 #endif /* _LINUX_U64_STATS_SYNC_WRAPPER_H */