Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

clarify usage expectations for cnt32_to_63()

Currently, all existing users of cnt32_to_63() are fine since the CPU
architectures where it is used don't do read access reordering, and user
mode preemption is disabled already. It is nevertheless a good idea to
better elaborate usage requirements wrt preemption, and use an explicit
memory barrier on SMP to avoid different CPUs accessing the counter
value in the wrong order. On UP a simple compiler barrier is
sufficient.

Signed-off-by: Nicolas Pitre <nico@marvell.com>
Acked-by: Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

authored by

Nicolas Pitre and committed by
Linus Torvalds
058e3739 02cabab4

+16 -6
+16 -6
include/linux/cnt32_to_63.h
··· 16 16 #include <linux/compiler.h> 17 17 #include <linux/types.h> 18 18 #include <asm/byteorder.h> 19 + #include <asm/system.h> 19 20 20 21 /* this is used only to give gcc a clue about good code generation */ 21 22 union cnt32_to_63 { ··· 54 53 * needed increment. And any race in updating the value in memory is harmless 55 54 * as the same value would simply be stored more than once. 56 55 * 57 - * The only restriction for the algorithm to work properly is that this 58 - * code must be executed at least once per each half period of the 32-bit 59 - * counter to properly update the state bit in memory. This is usually not a 60 - * problem in practice, but if it is then a kernel timer could be scheduled 61 - * to manage for this code to be executed often enough. 56 + * The restrictions for the algorithm to work properly are: 57 + * 58 + * 1) this code must be called at least once per each half period of the 59 + * 32-bit counter; 60 + * 61 + * 2) this code must not be preempted for a duration longer than the 62 + * 32-bit counter half period minus the longest period between two 63 + * calls to this code. 64 + * 65 + * Those requirements ensure proper update to the state bit in memory. 66 + * This is usually not a problem in practice, but if it is then a kernel 67 + * timer should be scheduled to manage for this code to be executed often 68 + * enough. 62 69 * 63 70 * Note that the top bit (bit 63) in the returned value should be considered 64 71 * as garbage. It is not cleared here because callers are likely to use a ··· 77 68 */ 78 69 #define cnt32_to_63(cnt_lo) \ 79 70 ({ \ 80 - static volatile u32 __m_cnt_hi; \ 71 + static u32 __m_cnt_hi; \ 81 72 union cnt32_to_63 __x; \ 82 73 __x.hi = __m_cnt_hi; \ 74 + smp_rmb(); \ 83 75 __x.lo = (cnt_lo); \ 84 76 if (unlikely((s32)(__x.hi ^ __x.lo) < 0)) \ 85 77 __m_cnt_hi = __x.hi = (__x.hi ^ 0x80000000) + (__x.hi >> 31); \