helpers.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __VDSO_HELPERS_H
  3. #define __VDSO_HELPERS_H
  4. #ifndef __ASSEMBLY__
  5. #include <asm/barrier.h>
  6. #include <vdso/datapage.h>
  7. static __always_inline u32 vdso_read_begin(const struct vdso_clock *vc)
  8. {
  9. u32 seq;
  10. while (unlikely((seq = READ_ONCE(vc->seq)) & 1))
  11. cpu_relax();
  12. smp_rmb();
  13. return seq;
  14. }
  15. static __always_inline u32 vdso_read_retry(const struct vdso_clock *vc,
  16. u32 start)
  17. {
  18. u32 seq;
  19. smp_rmb();
  20. seq = READ_ONCE(vc->seq);
  21. return seq != start;
  22. }
  23. static __always_inline void vdso_write_seq_begin(struct vdso_clock *vc)
  24. {
  25. /*
  26. * WRITE_ONCE() is required otherwise the compiler can validly tear
  27. * updates to vc->seq and it is possible that the value seen by the
  28. * reader is inconsistent.
  29. */
  30. WRITE_ONCE(vc->seq, vc->seq + 1);
  31. }
  32. static __always_inline void vdso_write_seq_end(struct vdso_clock *vc)
  33. {
  34. /*
  35. * WRITE_ONCE() is required otherwise the compiler can validly tear
  36. * updates to vc->seq and it is possible that the value seen by the
  37. * reader is inconsistent.
  38. */
  39. WRITE_ONCE(vc->seq, vc->seq + 1);
  40. }
  41. static __always_inline void vdso_write_begin_clock(struct vdso_clock *vc)
  42. {
  43. vdso_write_seq_begin(vc);
  44. /* Ensure the sequence invalidation is visible before data is modified */
  45. smp_wmb();
  46. }
  47. static __always_inline void vdso_write_end_clock(struct vdso_clock *vc)
  48. {
  49. /* Ensure the data update is visible before the sequence is set valid again */
  50. smp_wmb();
  51. vdso_write_seq_end(vc);
  52. }
  53. static __always_inline void vdso_write_begin(struct vdso_time_data *vd)
  54. {
  55. struct vdso_clock *vc = vd->clock_data;
  56. vdso_write_seq_begin(&vc[CS_HRES_COARSE]);
  57. vdso_write_seq_begin(&vc[CS_RAW]);
  58. /* Ensure the sequence invalidation is visible before data is modified */
  59. smp_wmb();
  60. }
  61. static __always_inline void vdso_write_end(struct vdso_time_data *vd)
  62. {
  63. struct vdso_clock *vc = vd->clock_data;
  64. /* Ensure the data update is visible before the sequence is set valid again */
  65. smp_wmb();
  66. vdso_write_seq_end(&vc[CS_HRES_COARSE]);
  67. vdso_write_seq_end(&vc[CS_RAW]);
  68. }
  69. #endif /* !__ASSEMBLY__ */
  70. #endif /* __VDSO_HELPERS_H */