posix-timers.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef _linux_POSIX_TIMERS_H
  3. #define _linux_POSIX_TIMERS_H
  4. #include <linux/spinlock.h>
  5. #include <linux/list.h>
  6. #include <linux/sched.h>
  7. #include <linux/timex.h>
  8. #include <linux/alarmtimer.h>
  9. struct siginfo;
  10. struct cpu_timer_list {
  11. struct list_head entry;
  12. u64 expires, incr;
  13. struct task_struct *task;
  14. int firing;
  15. };
  16. /*
  17. * Bit fields within a clockid:
  18. *
  19. * The most significant 29 bits hold either a pid or a file descriptor.
  20. *
  21. * Bit 2 indicates whether a cpu clock refers to a thread or a process.
  22. *
  23. * Bits 1 and 0 give the type: PROF=0, VIRT=1, SCHED=2, or FD=3.
  24. *
  25. * A clockid is invalid if bits 2, 1, and 0 are all set.
  26. */
  27. #define CPUCLOCK_PID(clock) ((pid_t) ~((clock) >> 3))
  28. #define CPUCLOCK_PERTHREAD(clock) \
  29. (((clock) & (clockid_t) CPUCLOCK_PERTHREAD_MASK) != 0)
  30. #define CPUCLOCK_PERTHREAD_MASK 4
  31. #define CPUCLOCK_WHICH(clock) ((clock) & (clockid_t) CPUCLOCK_CLOCK_MASK)
  32. #define CPUCLOCK_CLOCK_MASK 3
  33. #define CPUCLOCK_PROF 0
  34. #define CPUCLOCK_VIRT 1
  35. #define CPUCLOCK_SCHED 2
  36. #define CPUCLOCK_MAX 3
  37. #define CLOCKFD CPUCLOCK_MAX
  38. #define CLOCKFD_MASK (CPUCLOCK_PERTHREAD_MASK|CPUCLOCK_CLOCK_MASK)
  39. static inline clockid_t make_process_cpuclock(const unsigned int pid,
  40. const clockid_t clock)
  41. {
  42. return ((~pid) << 3) | clock;
  43. }
  44. static inline clockid_t make_thread_cpuclock(const unsigned int tid,
  45. const clockid_t clock)
  46. {
  47. return make_process_cpuclock(tid, clock | CPUCLOCK_PERTHREAD_MASK);
  48. }
  49. static inline clockid_t fd_to_clockid(const int fd)
  50. {
  51. return make_process_cpuclock((unsigned int) fd, CLOCKFD);
  52. }
  53. static inline int clockid_to_fd(const clockid_t clk)
  54. {
  55. return ~(clk >> 3);
  56. }
  57. #define REQUEUE_PENDING 1
  58. /**
  59. * struct k_itimer - POSIX.1b interval timer structure.
  60. * @list: List head for binding the timer to signals->posix_timers
  61. * @t_hash: Entry in the posix timer hash table
  62. * @it_lock: Lock protecting the timer
  63. * @kclock: Pointer to the k_clock struct handling this timer
  64. * @it_clock: The posix timer clock id
  65. * @it_id: The posix timer id for identifying the timer
  66. * @it_active: Marker that timer is active
  67. * @it_overrun: The overrun counter for pending signals
  68. * @it_overrun_last: The overrun at the time of the last delivered signal
  69. * @it_requeue_pending: Indicator that timer waits for being requeued on
  70. * signal delivery
  71. * @it_sigev_notify: The notify word of sigevent struct for signal delivery
  72. * @it_interval: The interval for periodic timers
  73. * @it_signal: Pointer to the creators signal struct
  74. * @it_pid: The pid of the process/task targeted by the signal
  75. * @it_process: The task to wakeup on clock_nanosleep (CPU timers)
  76. * @sigq: Pointer to preallocated sigqueue
  77. * @it: Union representing the various posix timer type
  78. * internals. Also used for rcu freeing the timer.
  79. */
  80. struct k_itimer {
  81. struct list_head list;
  82. struct hlist_node t_hash;
  83. spinlock_t it_lock;
  84. const struct k_clock *kclock;
  85. clockid_t it_clock;
  86. timer_t it_id;
  87. int it_active;
  88. s64 it_overrun;
  89. s64 it_overrun_last;
  90. int it_requeue_pending;
  91. int it_sigev_notify;
  92. ktime_t it_interval;
  93. struct signal_struct *it_signal;
  94. union {
  95. struct pid *it_pid;
  96. struct task_struct *it_process;
  97. };
  98. struct sigqueue *sigq;
  99. union {
  100. struct {
  101. struct hrtimer timer;
  102. } real;
  103. struct cpu_timer_list cpu;
  104. struct {
  105. struct alarm alarmtimer;
  106. } alarm;
  107. struct rcu_head rcu;
  108. } it;
  109. };
  110. void run_posix_cpu_timers(struct task_struct *task);
  111. void posix_cpu_timers_exit(struct task_struct *task);
  112. void posix_cpu_timers_exit_group(struct task_struct *task);
  113. void set_process_cpu_timer(struct task_struct *task, unsigned int clock_idx,
  114. u64 *newval, u64 *oldval);
  115. void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new);
  116. void posixtimer_rearm(struct siginfo *info);
  117. #endif