time_namespace.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _LINUX_TIMENS_H
  3. #define _LINUX_TIMENS_H
  4. #include <linux/sched.h>
  5. #include <linux/nsproxy.h>
  6. #include <linux/ns_common.h>
  7. #include <linux/err.h>
  8. struct user_namespace;
  9. extern struct user_namespace init_user_ns;
  10. struct timens_offsets {
  11. struct timespec64 monotonic;
  12. struct timespec64 boottime;
  13. };
  14. struct time_namespace {
  15. struct user_namespace *user_ns;
  16. struct ucounts *ucounts;
  17. struct ns_common ns;
  18. struct timens_offsets offsets;
  19. struct page *vvar_page;
  20. /* If set prevents changing offsets after any task joined namespace. */
  21. bool frozen_offsets;
  22. } __randomize_layout;
  23. extern struct time_namespace init_time_ns;
  24. #ifdef CONFIG_TIME_NS
  25. extern int vdso_join_timens(struct task_struct *task,
  26. struct time_namespace *ns);
  27. extern void timens_commit(struct task_struct *tsk, struct time_namespace *ns);
  28. static inline struct time_namespace *get_time_ns(struct time_namespace *ns)
  29. {
  30. refcount_inc(&ns->ns.count);
  31. return ns;
  32. }
  33. struct time_namespace *copy_time_ns(unsigned long flags,
  34. struct user_namespace *user_ns,
  35. struct time_namespace *old_ns);
  36. void free_time_ns(struct time_namespace *ns);
  37. void timens_on_fork(struct nsproxy *nsproxy, struct task_struct *tsk);
  38. struct vdso_data *arch_get_vdso_data(void *vvar_page);
  39. static inline void put_time_ns(struct time_namespace *ns)
  40. {
  41. if (refcount_dec_and_test(&ns->ns.count))
  42. free_time_ns(ns);
  43. }
  44. void proc_timens_show_offsets(struct task_struct *p, struct seq_file *m);
  45. struct proc_timens_offset {
  46. int clockid;
  47. struct timespec64 val;
  48. };
  49. int proc_timens_set_offset(struct file *file, struct task_struct *p,
  50. struct proc_timens_offset *offsets, int n);
  51. static inline void timens_add_monotonic(struct timespec64 *ts)
  52. {
  53. struct timens_offsets *ns_offsets = &current->nsproxy->time_ns->offsets;
  54. *ts = timespec64_add(*ts, ns_offsets->monotonic);
  55. }
  56. static inline void timens_add_boottime(struct timespec64 *ts)
  57. {
  58. struct timens_offsets *ns_offsets = &current->nsproxy->time_ns->offsets;
  59. *ts = timespec64_add(*ts, ns_offsets->boottime);
  60. }
  61. static inline u64 timens_add_boottime_ns(u64 nsec)
  62. {
  63. struct timens_offsets *ns_offsets = &current->nsproxy->time_ns->offsets;
  64. return nsec + timespec64_to_ns(&ns_offsets->boottime);
  65. }
  66. static inline void timens_sub_boottime(struct timespec64 *ts)
  67. {
  68. struct timens_offsets *ns_offsets = &current->nsproxy->time_ns->offsets;
  69. *ts = timespec64_sub(*ts, ns_offsets->boottime);
  70. }
  71. ktime_t do_timens_ktime_to_host(clockid_t clockid, ktime_t tim,
  72. struct timens_offsets *offsets);
  73. static inline ktime_t timens_ktime_to_host(clockid_t clockid, ktime_t tim)
  74. {
  75. struct time_namespace *ns = current->nsproxy->time_ns;
  76. if (likely(ns == &init_time_ns))
  77. return tim;
  78. return do_timens_ktime_to_host(clockid, tim, &ns->offsets);
  79. }
  80. #else
  81. static inline int vdso_join_timens(struct task_struct *task,
  82. struct time_namespace *ns)
  83. {
  84. return 0;
  85. }
  86. static inline void timens_commit(struct task_struct *tsk,
  87. struct time_namespace *ns)
  88. {
  89. }
  90. static inline struct time_namespace *get_time_ns(struct time_namespace *ns)
  91. {
  92. return NULL;
  93. }
  94. static inline void put_time_ns(struct time_namespace *ns)
  95. {
  96. }
  97. static inline
  98. struct time_namespace *copy_time_ns(unsigned long flags,
  99. struct user_namespace *user_ns,
  100. struct time_namespace *old_ns)
  101. {
  102. if (flags & CLONE_NEWTIME)
  103. return ERR_PTR(-EINVAL);
  104. return old_ns;
  105. }
  106. static inline void timens_on_fork(struct nsproxy *nsproxy,
  107. struct task_struct *tsk)
  108. {
  109. return;
  110. }
  111. static inline void timens_add_monotonic(struct timespec64 *ts) { }
  112. static inline void timens_add_boottime(struct timespec64 *ts) { }
  113. static inline u64 timens_add_boottime_ns(u64 nsec)
  114. {
  115. return nsec;
  116. }
  117. static inline void timens_sub_boottime(struct timespec64 *ts) { }
  118. static inline ktime_t timens_ktime_to_host(clockid_t clockid, ktime_t tim)
  119. {
  120. return tim;
  121. }
  122. #endif
  123. #endif /* _LINUX_TIMENS_H */