Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0 */ |
Eric Dumazet | 16b8a47 | 2010-06-22 10:22:17 -0700 | [diff] [blame] | 2 | #ifndef _LINUX_U64_STATS_SYNC_H |
| 3 | #define _LINUX_U64_STATS_SYNC_H |
| 4 | |
| 5 | /* |
| 6 | * To properly implement 64bits network statistics on 32bit and 64bit hosts, |
| 7 | * we provide a synchronization point, that is a noop on 64bit or UP kernels. |
| 8 | * |
| 9 | * Key points : |
| 10 | * 1) Use a seqcount on SMP 32bits, with low overhead. |
| 11 | * 2) Whole thing is a noop on 64bit arches or UP kernels. |
| 12 | * 3) Write side must ensure mutual exclusion or one seqcount update could |
| 13 | * be lost, thus blocking readers forever. |
| 14 | * If this synchronization point is not a mutex, but a spinlock or |
| 15 | * spinlock_bh() or disable_bh() : |
| 16 | * 3.1) Write side should not sleep. |
| 17 | * 3.2) Write side should not allow preemption. |
| 18 | * 3.3) If applicable, interrupts should be disabled. |
| 19 | * |
| 20 | * 4) If reader fetches several counters, there is no guarantee the whole values |
| 21 | * are consistent (remember point 1) : this is a noop on 64bit arches anyway) |
| 22 | * |
| 23 | * 5) readers are allowed to sleep or be preempted/interrupted : They perform |
| 24 | * pure reads. But if they have to fetch many values, it's better to not allow |
| 25 | * preemptions/interruptions to avoid many retries. |
| 26 | * |
Eric Dumazet | b6b3ecc | 2010-06-24 00:04:38 +0000 | [diff] [blame] | 27 | * 6) If counter might be written by an interrupt, readers should block interrupts. |
| 28 | * (On UP, there is no seqcount_t protection, a reader allowing interrupts could |
| 29 | * read partial values) |
| 30 | * |
Eric W. Biederman | 57a7744 | 2014-03-13 21:26:42 -0700 | [diff] [blame] | 31 | * 7) For irq and softirq uses, readers can use u64_stats_fetch_begin_irq() and |
| 32 | * u64_stats_fetch_retry_irq() helpers |
Eric Dumazet | 33d91f0 | 2010-06-24 00:54:06 +0000 | [diff] [blame] | 33 | * |
Eric Dumazet | 16b8a47 | 2010-06-22 10:22:17 -0700 | [diff] [blame] | 34 | * Usage : |
| 35 | * |
| 36 | * Stats producer (writer) should use following template granted it already got |
| 37 | * an exclusive access to counters (a lock is already taken, or per cpu |
| 38 | * data is used [in a non preemptable context]) |
| 39 | * |
| 40 | * spin_lock_bh(...) or other synchronization to get exclusive access |
| 41 | * ... |
| 42 | * u64_stats_update_begin(&stats->syncp); |
| 43 | * stats->bytes64 += len; // non atomic operation |
| 44 | * stats->packets64++; // non atomic operation |
| 45 | * u64_stats_update_end(&stats->syncp); |
| 46 | * |
| 47 | * While a consumer (reader) should use following template to get consistent |
| 48 | * snapshot for each variable (but no guarantee on several ones) |
| 49 | * |
| 50 | * u64 tbytes, tpackets; |
| 51 | * unsigned int start; |
| 52 | * |
| 53 | * do { |
| 54 | * start = u64_stats_fetch_begin(&stats->syncp); |
| 55 | * tbytes = stats->bytes64; // non atomic operation |
| 56 | * tpackets = stats->packets64; // non atomic operation |
Eric Dumazet | b6b3ecc | 2010-06-24 00:04:38 +0000 | [diff] [blame] | 57 | * } while (u64_stats_fetch_retry(&stats->syncp, start)); |
Eric Dumazet | 16b8a47 | 2010-06-22 10:22:17 -0700 | [diff] [blame] | 58 | * |
| 59 | * |
| 60 | * Example of use in drivers/net/loopback.c, using per_cpu containers, |
| 61 | * in BH disabled context. |
| 62 | */ |
| 63 | #include <linux/seqlock.h> |
| 64 | |
Eric Dumazet | 33d91f0 | 2010-06-24 00:54:06 +0000 | [diff] [blame] | 65 | struct u64_stats_sync { |
Eric Dumazet | 16b8a47 | 2010-06-22 10:22:17 -0700 | [diff] [blame] | 66 | #if BITS_PER_LONG==32 && defined(CONFIG_SMP) |
Eric Dumazet | 16b8a47 | 2010-06-22 10:22:17 -0700 | [diff] [blame] | 67 | seqcount_t seq; |
Eric Dumazet | 16b8a47 | 2010-06-22 10:22:17 -0700 | [diff] [blame] | 68 | #endif |
Eric Dumazet | 33d91f0 | 2010-06-24 00:54:06 +0000 | [diff] [blame] | 69 | }; |
| 70 | |
John Stultz | 827da44 | 2013-10-07 15:51:58 -0700 | [diff] [blame] | 71 | |
Eric Dumazet | 9464ca6 | 2015-06-12 19:44:48 -0700 | [diff] [blame] | 72 | static inline void u64_stats_init(struct u64_stats_sync *syncp) |
| 73 | { |
John Stultz | 827da44 | 2013-10-07 15:51:58 -0700 | [diff] [blame] | 74 | #if BITS_PER_LONG == 32 && defined(CONFIG_SMP) |
Eric Dumazet | 9464ca6 | 2015-06-12 19:44:48 -0700 | [diff] [blame] | 75 | seqcount_init(&syncp->seq); |
John Stultz | 827da44 | 2013-10-07 15:51:58 -0700 | [diff] [blame] | 76 | #endif |
Eric Dumazet | 9464ca6 | 2015-06-12 19:44:48 -0700 | [diff] [blame] | 77 | } |
John Stultz | 827da44 | 2013-10-07 15:51:58 -0700 | [diff] [blame] | 78 | |
Jesper Juhl | fa9f90b | 2010-11-28 21:39:34 +0100 | [diff] [blame] | 79 | static inline void u64_stats_update_begin(struct u64_stats_sync *syncp) |
Eric Dumazet | 33d91f0 | 2010-06-24 00:54:06 +0000 | [diff] [blame] | 80 | { |
| 81 | #if BITS_PER_LONG==32 && defined(CONFIG_SMP) |
| 82 | write_seqcount_begin(&syncp->seq); |
| 83 | #endif |
| 84 | } |
| 85 | |
Jesper Juhl | fa9f90b | 2010-11-28 21:39:34 +0100 | [diff] [blame] | 86 | static inline void u64_stats_update_end(struct u64_stats_sync *syncp) |
Eric Dumazet | 33d91f0 | 2010-06-24 00:54:06 +0000 | [diff] [blame] | 87 | { |
| 88 | #if BITS_PER_LONG==32 && defined(CONFIG_SMP) |
| 89 | write_seqcount_end(&syncp->seq); |
| 90 | #endif |
| 91 | } |
| 92 | |
Eric Dumazet | 46cc6e4 | 2016-05-03 16:56:03 -0700 | [diff] [blame] | 93 | static inline void u64_stats_update_begin_raw(struct u64_stats_sync *syncp) |
| 94 | { |
| 95 | #if BITS_PER_LONG==32 && defined(CONFIG_SMP) |
| 96 | raw_write_seqcount_begin(&syncp->seq); |
| 97 | #endif |
| 98 | } |
| 99 | |
| 100 | static inline void u64_stats_update_end_raw(struct u64_stats_sync *syncp) |
| 101 | { |
| 102 | #if BITS_PER_LONG==32 && defined(CONFIG_SMP) |
| 103 | raw_write_seqcount_end(&syncp->seq); |
| 104 | #endif |
| 105 | } |
| 106 | |
Frederic Weisbecker | 68107df | 2016-09-26 02:29:19 +0200 | [diff] [blame] | 107 | static inline unsigned int __u64_stats_fetch_begin(const struct u64_stats_sync *syncp) |
Eric Dumazet | 33d91f0 | 2010-06-24 00:54:06 +0000 | [diff] [blame] | 108 | { |
| 109 | #if BITS_PER_LONG==32 && defined(CONFIG_SMP) |
| 110 | return read_seqcount_begin(&syncp->seq); |
| 111 | #else |
Frederic Weisbecker | 68107df | 2016-09-26 02:29:19 +0200 | [diff] [blame] | 112 | return 0; |
| 113 | #endif |
| 114 | } |
| 115 | |
| 116 | static inline unsigned int u64_stats_fetch_begin(const struct u64_stats_sync *syncp) |
| 117 | { |
| 118 | #if BITS_PER_LONG==32 && !defined(CONFIG_SMP) |
Eric Dumazet | 33d91f0 | 2010-06-24 00:54:06 +0000 | [diff] [blame] | 119 | preempt_disable(); |
| 120 | #endif |
Frederic Weisbecker | 68107df | 2016-09-26 02:29:19 +0200 | [diff] [blame] | 121 | return __u64_stats_fetch_begin(syncp); |
| 122 | } |
| 123 | |
| 124 | static inline bool __u64_stats_fetch_retry(const struct u64_stats_sync *syncp, |
| 125 | unsigned int start) |
| 126 | { |
| 127 | #if BITS_PER_LONG==32 && defined(CONFIG_SMP) |
| 128 | return read_seqcount_retry(&syncp->seq, start); |
| 129 | #else |
| 130 | return false; |
Eric Dumazet | 33d91f0 | 2010-06-24 00:54:06 +0000 | [diff] [blame] | 131 | #endif |
| 132 | } |
| 133 | |
Jesper Juhl | fa9f90b | 2010-11-28 21:39:34 +0100 | [diff] [blame] | 134 | static inline bool u64_stats_fetch_retry(const struct u64_stats_sync *syncp, |
Eric Dumazet | 33d91f0 | 2010-06-24 00:54:06 +0000 | [diff] [blame] | 135 | unsigned int start) |
| 136 | { |
Frederic Weisbecker | 68107df | 2016-09-26 02:29:19 +0200 | [diff] [blame] | 137 | #if BITS_PER_LONG==32 && !defined(CONFIG_SMP) |
Eric Dumazet | 33d91f0 | 2010-06-24 00:54:06 +0000 | [diff] [blame] | 138 | preempt_enable(); |
| 139 | #endif |
Frederic Weisbecker | 68107df | 2016-09-26 02:29:19 +0200 | [diff] [blame] | 140 | return __u64_stats_fetch_retry(syncp, start); |
Eric Dumazet | 33d91f0 | 2010-06-24 00:54:06 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | /* |
Eric W. Biederman | 57a7744 | 2014-03-13 21:26:42 -0700 | [diff] [blame] | 144 | * In case irq handlers can update u64 counters, readers can use following helpers |
Eric Dumazet | 33d91f0 | 2010-06-24 00:54:06 +0000 | [diff] [blame] | 145 | * - SMP 32bit arches use seqcount protection, irq safe. |
Eric W. Biederman | 57a7744 | 2014-03-13 21:26:42 -0700 | [diff] [blame] | 146 | * - UP 32bit must disable irqs. |
Eric Dumazet | 33d91f0 | 2010-06-24 00:54:06 +0000 | [diff] [blame] | 147 | * - 64bit have no problem atomically reading u64 values, irq safe. |
| 148 | */ |
Eric W. Biederman | 57a7744 | 2014-03-13 21:26:42 -0700 | [diff] [blame] | 149 | static inline unsigned int u64_stats_fetch_begin_irq(const struct u64_stats_sync *syncp) |
Eric Dumazet | 33d91f0 | 2010-06-24 00:54:06 +0000 | [diff] [blame] | 150 | { |
Frederic Weisbecker | 68107df | 2016-09-26 02:29:19 +0200 | [diff] [blame] | 151 | #if BITS_PER_LONG==32 && !defined(CONFIG_SMP) |
Eric W. Biederman | 57a7744 | 2014-03-13 21:26:42 -0700 | [diff] [blame] | 152 | local_irq_disable(); |
Eric Dumazet | 33d91f0 | 2010-06-24 00:54:06 +0000 | [diff] [blame] | 153 | #endif |
Frederic Weisbecker | 68107df | 2016-09-26 02:29:19 +0200 | [diff] [blame] | 154 | return __u64_stats_fetch_begin(syncp); |
Eric Dumazet | 33d91f0 | 2010-06-24 00:54:06 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Eric W. Biederman | 57a7744 | 2014-03-13 21:26:42 -0700 | [diff] [blame] | 157 | static inline bool u64_stats_fetch_retry_irq(const struct u64_stats_sync *syncp, |
Frederic Weisbecker | 68107df | 2016-09-26 02:29:19 +0200 | [diff] [blame] | 158 | unsigned int start) |
Eric Dumazet | 33d91f0 | 2010-06-24 00:54:06 +0000 | [diff] [blame] | 159 | { |
Frederic Weisbecker | 68107df | 2016-09-26 02:29:19 +0200 | [diff] [blame] | 160 | #if BITS_PER_LONG==32 && !defined(CONFIG_SMP) |
Eric W. Biederman | 57a7744 | 2014-03-13 21:26:42 -0700 | [diff] [blame] | 161 | local_irq_enable(); |
Eric Dumazet | 33d91f0 | 2010-06-24 00:54:06 +0000 | [diff] [blame] | 162 | #endif |
Frederic Weisbecker | 68107df | 2016-09-26 02:29:19 +0200 | [diff] [blame] | 163 | return __u64_stats_fetch_retry(syncp, start); |
Eric Dumazet | 33d91f0 | 2010-06-24 00:54:06 +0000 | [diff] [blame] | 164 | } |
Eric Dumazet | 16b8a47 | 2010-06-22 10:22:17 -0700 | [diff] [blame] | 165 | |
| 166 | #endif /* _LINUX_U64_STATS_SYNC_H */ |