]>
git.proxmox.com Git - mirror_ovs.git/blob - datapath/linux/compat/include/linux/u64_stats_sync.h
1 #ifndef _LINUX_U64_STATS_SYNC_WRAPPER_H
2 #define _LINUX_U64_STATS_SYNC_WRAPPER_H
4 #include <linux/version.h>
6 #if defined(HAVE_U64_STATS_FETCH_BEGIN_IRQ) && \
7 LINUX_VERSION_CODE >= KERNEL_VERSION(3,13,0)
8 #include_next <linux/u64_stats_sync.h>
12 * To properly implement 64bits network statistics on 32bit and 64bit hosts,
13 * we provide a synchronization point, that is a noop on 64bit or UP kernels.
16 * 1) Use a seqcount on SMP 32bits, with low overhead.
17 * 2) Whole thing is a noop on 64bit arches or UP kernels.
18 * 3) Write side must ensure mutual exclusion or one seqcount update could
19 * be lost, thus blocking readers forever.
20 * If this synchronization point is not a mutex, but a spinlock or
21 * spinlock_bh() or disable_bh() :
22 * 3.1) Write side should not sleep.
23 * 3.2) Write side should not allow preemption.
24 * 3.3) If applicable, interrupts should be disabled.
26 * 4) If reader fetches several counters, there is no guarantee the whole values
27 * are consistent (remember point 1) : this is a noop on 64bit arches anyway)
29 * 5) readers are allowed to sleep or be preempted/interrupted : They perform
30 * pure reads. But if they have to fetch many values, it's better to not allow
31 * preemptions/interruptions to avoid many retries.
33 * 6) If counter might be written by an interrupt, readers should block interrupts.
34 * (On UP, there is no seqcount_t protection, a reader allowing interrupts could
35 * read partial values)
37 * 7) For irq or softirq uses, readers can use u64_stats_fetch_begin_irq() and
38 * u64_stats_fetch_retry_irq() helpers
42 * Stats producer (writer) should use following template granted it already got
43 * an exclusive access to counters (a lock is already taken, or per cpu
44 * data is used [in a non preemptable context])
46 * spin_lock_bh(...) or other synchronization to get exclusive access
48 * u64_stats_update_begin(&stats->syncp);
49 * stats->bytes64 += len; // non atomic operation
50 * stats->packets64++; // non atomic operation
51 * u64_stats_update_end(&stats->syncp);
53 * While a consumer (reader) should use following template to get consistent
54 * snapshot for each variable (but no guarantee on several ones)
56 * u64 tbytes, tpackets;
60 * start = u64_stats_fetch_begin(&stats->syncp);
61 * tbytes = stats->bytes64; // non atomic operation
62 * tpackets = stats->packets64; // non atomic operation
63 * } while (u64_stats_fetch_retry(&stats->syncp, start));
66 * Example of use in drivers/net/loopback.c, using per_cpu containers,
67 * in BH disabled context.
69 #include <linux/seqlock.h>
71 struct u64_stats_sync
{
72 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
77 #if BITS_PER_LONG == 32 && defined(CONFIG_SMP)
78 # define u64_stats_init(syncp) seqcount_init(syncp.seq)
80 # define u64_stats_init(syncp) do { } while (0)
83 static inline void u64_stats_update_begin(struct u64_stats_sync
*syncp
)
85 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
86 write_seqcount_begin(&syncp
->seq
);
90 static inline void u64_stats_update_end(struct u64_stats_sync
*syncp
)
92 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
93 write_seqcount_end(&syncp
->seq
);
97 static inline unsigned int u64_stats_fetch_begin(const struct u64_stats_sync
*syncp
)
99 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
100 return read_seqcount_begin(&syncp
->seq
);
102 #if BITS_PER_LONG==32
109 static inline bool u64_stats_fetch_retry(const struct u64_stats_sync
*syncp
,
112 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
113 return read_seqcount_retry(&syncp
->seq
, start
);
115 #if BITS_PER_LONG==32
123 * In case irq handlers can update u64 counters, readers can use following helpers
124 * - SMP 32bit arches use seqcount protection, irq safe.
125 * - UP 32bit must disable irqs.
126 * - 64bit have no problem atomically reading u64 values, irq safe.
128 static inline unsigned int u64_stats_fetch_begin_irq(const struct u64_stats_sync
*syncp
)
130 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
131 return read_seqcount_begin(&syncp
->seq
);
133 #if BITS_PER_LONG==32
140 static inline bool u64_stats_fetch_retry_irq(const struct u64_stats_sync
*syncp
,
143 #if BITS_PER_LONG==32 && defined(CONFIG_SMP)
144 return read_seqcount_retry(&syncp
->seq
, start
);
146 #if BITS_PER_LONG==32
153 #endif /* !HAVE_U64_STATS_FETCH_BEGIN_IRQ || kernel < 3.13 */
155 #endif /* _LINUX_U64_STATS_SYNC_WRAPPER_H */