u64_stats_sync.h 5.1 KB

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