.. | .. |
---|
20 | 20 | |
---|
21 | 21 | static void posix_cpu_timer_rearm(struct k_itimer *timer); |
---|
22 | 22 | |
---|
| 23 | +void posix_cputimers_group_init(struct posix_cputimers *pct, u64 cpu_limit) |
---|
| 24 | +{ |
---|
| 25 | + posix_cputimers_init(pct); |
---|
| 26 | + if (cpu_limit != RLIM_INFINITY) { |
---|
| 27 | + pct->bases[CPUCLOCK_PROF].nextevt = cpu_limit * NSEC_PER_SEC; |
---|
| 28 | + pct->timers_active = true; |
---|
| 29 | + } |
---|
| 30 | +} |
---|
| 31 | + |
---|
23 | 32 | /* |
---|
24 | 33 | * Called after updating RLIMIT_CPU to run cpu timer and update |
---|
25 | | - * tsk->signal->cputime_expires expiration cache if necessary. Needs |
---|
26 | | - * siglock protection since other code may update expiration cache as |
---|
27 | | - * well. |
---|
| 34 | + * tsk->signal->posix_cputimers.bases[clock].nextevt expiration cache if |
---|
| 35 | + * necessary. Needs siglock protection since other code may update the |
---|
| 36 | + * expiration cache as well. |
---|
28 | 37 | */ |
---|
29 | 38 | void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new) |
---|
30 | 39 | { |
---|
.. | .. |
---|
35 | 44 | spin_unlock_irq(&task->sighand->siglock); |
---|
36 | 45 | } |
---|
37 | 46 | |
---|
38 | | -static int check_clock(const clockid_t which_clock) |
---|
| 47 | +/* |
---|
| 48 | + * Functions for validating access to tasks. |
---|
| 49 | + */ |
---|
| 50 | +static struct pid *pid_for_clock(const clockid_t clock, bool gettime) |
---|
39 | 51 | { |
---|
40 | | - int error = 0; |
---|
41 | | - struct task_struct *p; |
---|
42 | | - const pid_t pid = CPUCLOCK_PID(which_clock); |
---|
| 52 | + const bool thread = !!CPUCLOCK_PERTHREAD(clock); |
---|
| 53 | + const pid_t upid = CPUCLOCK_PID(clock); |
---|
| 54 | + struct pid *pid; |
---|
43 | 55 | |
---|
44 | | - if (CPUCLOCK_WHICH(which_clock) >= CPUCLOCK_MAX) |
---|
45 | | - return -EINVAL; |
---|
| 56 | + if (CPUCLOCK_WHICH(clock) >= CPUCLOCK_MAX) |
---|
| 57 | + return NULL; |
---|
46 | 58 | |
---|
47 | | - if (pid == 0) |
---|
48 | | - return 0; |
---|
| 59 | + /* |
---|
| 60 | + * If the encoded PID is 0, then the timer is targeted at current |
---|
| 61 | + * or the process to which current belongs. |
---|
| 62 | + */ |
---|
| 63 | + if (upid == 0) |
---|
| 64 | + return thread ? task_pid(current) : task_tgid(current); |
---|
| 65 | + |
---|
| 66 | + pid = find_vpid(upid); |
---|
| 67 | + if (!pid) |
---|
| 68 | + return NULL; |
---|
| 69 | + |
---|
| 70 | + if (thread) { |
---|
| 71 | + struct task_struct *tsk = pid_task(pid, PIDTYPE_PID); |
---|
| 72 | + return (tsk && same_thread_group(tsk, current)) ? pid : NULL; |
---|
| 73 | + } |
---|
| 74 | + |
---|
| 75 | + /* |
---|
| 76 | + * For clock_gettime(PROCESS) allow finding the process by |
---|
| 77 | + * with the pid of the current task. The code needs the tgid |
---|
| 78 | + * of the process so that pid_task(pid, PIDTYPE_TGID) can be |
---|
| 79 | + * used to find the process. |
---|
| 80 | + */ |
---|
| 81 | + if (gettime && (pid == task_pid(current))) |
---|
| 82 | + return task_tgid(current); |
---|
| 83 | + |
---|
| 84 | + /* |
---|
| 85 | + * For processes require that pid identifies a process. |
---|
| 86 | + */ |
---|
| 87 | + return pid_has_task(pid, PIDTYPE_TGID) ? pid : NULL; |
---|
| 88 | +} |
---|
| 89 | + |
---|
| 90 | +static inline int validate_clock_permissions(const clockid_t clock) |
---|
| 91 | +{ |
---|
| 92 | + int ret; |
---|
49 | 93 | |
---|
50 | 94 | rcu_read_lock(); |
---|
51 | | - p = find_task_by_vpid(pid); |
---|
52 | | - if (!p || !(CPUCLOCK_PERTHREAD(which_clock) ? |
---|
53 | | - same_thread_group(p, current) : has_group_leader_pid(p))) { |
---|
54 | | - error = -EINVAL; |
---|
55 | | - } |
---|
| 95 | + ret = pid_for_clock(clock, false) ? 0 : -EINVAL; |
---|
56 | 96 | rcu_read_unlock(); |
---|
57 | 97 | |
---|
58 | | - return error; |
---|
| 98 | + return ret; |
---|
| 99 | +} |
---|
| 100 | + |
---|
| 101 | +static inline enum pid_type clock_pid_type(const clockid_t clock) |
---|
| 102 | +{ |
---|
| 103 | + return CPUCLOCK_PERTHREAD(clock) ? PIDTYPE_PID : PIDTYPE_TGID; |
---|
| 104 | +} |
---|
| 105 | + |
---|
| 106 | +static inline struct task_struct *cpu_timer_task_rcu(struct k_itimer *timer) |
---|
| 107 | +{ |
---|
| 108 | + return pid_task(timer->it.cpu.pid, clock_pid_type(timer->it_clock)); |
---|
59 | 109 | } |
---|
60 | 110 | |
---|
61 | 111 | /* |
---|
62 | 112 | * Update expiry time from increment, and increase overrun count, |
---|
63 | 113 | * given the current clock sample. |
---|
64 | 114 | */ |
---|
65 | | -static void bump_cpu_timer(struct k_itimer *timer, u64 now) |
---|
| 115 | +static u64 bump_cpu_timer(struct k_itimer *timer, u64 now) |
---|
66 | 116 | { |
---|
| 117 | + u64 delta, incr, expires = timer->it.cpu.node.expires; |
---|
67 | 118 | int i; |
---|
68 | | - u64 delta, incr; |
---|
69 | 119 | |
---|
70 | | - if (timer->it.cpu.incr == 0) |
---|
71 | | - return; |
---|
| 120 | + if (!timer->it_interval) |
---|
| 121 | + return expires; |
---|
72 | 122 | |
---|
73 | | - if (now < timer->it.cpu.expires) |
---|
74 | | - return; |
---|
| 123 | + if (now < expires) |
---|
| 124 | + return expires; |
---|
75 | 125 | |
---|
76 | | - incr = timer->it.cpu.incr; |
---|
77 | | - delta = now + incr - timer->it.cpu.expires; |
---|
| 126 | + incr = timer->it_interval; |
---|
| 127 | + delta = now + incr - expires; |
---|
78 | 128 | |
---|
79 | 129 | /* Don't use (incr*2 < delta), incr*2 might overflow. */ |
---|
80 | 130 | for (i = 0; incr < delta - incr; i++) |
---|
.. | .. |
---|
84 | 134 | if (delta < incr) |
---|
85 | 135 | continue; |
---|
86 | 136 | |
---|
87 | | - timer->it.cpu.expires += incr; |
---|
| 137 | + timer->it.cpu.node.expires += incr; |
---|
88 | 138 | timer->it_overrun += 1LL << i; |
---|
89 | 139 | delta -= incr; |
---|
90 | 140 | } |
---|
| 141 | + return timer->it.cpu.node.expires; |
---|
91 | 142 | } |
---|
92 | 143 | |
---|
93 | | -/** |
---|
94 | | - * task_cputime_zero - Check a task_cputime struct for all zero fields. |
---|
95 | | - * |
---|
96 | | - * @cputime: The struct to compare. |
---|
97 | | - * |
---|
98 | | - * Checks @cputime to see if all fields are zero. Returns true if all fields |
---|
99 | | - * are zero, false if any field is nonzero. |
---|
100 | | - */ |
---|
101 | | -static inline int task_cputime_zero(const struct task_cputime *cputime) |
---|
| 144 | +/* Check whether all cache entries contain U64_MAX, i.e. eternal expiry time */ |
---|
| 145 | +static inline bool expiry_cache_is_inactive(const struct posix_cputimers *pct) |
---|
102 | 146 | { |
---|
103 | | - if (!cputime->utime && !cputime->stime && !cputime->sum_exec_runtime) |
---|
104 | | - return 1; |
---|
105 | | - return 0; |
---|
106 | | -} |
---|
107 | | - |
---|
108 | | -static inline u64 prof_ticks(struct task_struct *p) |
---|
109 | | -{ |
---|
110 | | - u64 utime, stime; |
---|
111 | | - |
---|
112 | | - task_cputime(p, &utime, &stime); |
---|
113 | | - |
---|
114 | | - return utime + stime; |
---|
115 | | -} |
---|
116 | | -static inline u64 virt_ticks(struct task_struct *p) |
---|
117 | | -{ |
---|
118 | | - u64 utime, stime; |
---|
119 | | - |
---|
120 | | - task_cputime(p, &utime, &stime); |
---|
121 | | - |
---|
122 | | - return utime; |
---|
| 147 | + return !(~pct->bases[CPUCLOCK_PROF].nextevt | |
---|
| 148 | + ~pct->bases[CPUCLOCK_VIRT].nextevt | |
---|
| 149 | + ~pct->bases[CPUCLOCK_SCHED].nextevt); |
---|
123 | 150 | } |
---|
124 | 151 | |
---|
125 | 152 | static int |
---|
126 | 153 | posix_cpu_clock_getres(const clockid_t which_clock, struct timespec64 *tp) |
---|
127 | 154 | { |
---|
128 | | - int error = check_clock(which_clock); |
---|
| 155 | + int error = validate_clock_permissions(which_clock); |
---|
| 156 | + |
---|
129 | 157 | if (!error) { |
---|
130 | 158 | tp->tv_sec = 0; |
---|
131 | 159 | tp->tv_nsec = ((NSEC_PER_SEC + HZ - 1) / HZ); |
---|
.. | .. |
---|
142 | 170 | } |
---|
143 | 171 | |
---|
144 | 172 | static int |
---|
145 | | -posix_cpu_clock_set(const clockid_t which_clock, const struct timespec64 *tp) |
---|
| 173 | +posix_cpu_clock_set(const clockid_t clock, const struct timespec64 *tp) |
---|
146 | 174 | { |
---|
| 175 | + int error = validate_clock_permissions(clock); |
---|
| 176 | + |
---|
147 | 177 | /* |
---|
148 | 178 | * You can never reset a CPU clock, but we check for other errors |
---|
149 | 179 | * in the call before failing with EPERM. |
---|
150 | 180 | */ |
---|
151 | | - int error = check_clock(which_clock); |
---|
152 | | - if (error == 0) { |
---|
153 | | - error = -EPERM; |
---|
154 | | - } |
---|
155 | | - return error; |
---|
| 181 | + return error ? : -EPERM; |
---|
156 | 182 | } |
---|
157 | 183 | |
---|
158 | | - |
---|
159 | 184 | /* |
---|
160 | | - * Sample a per-thread clock for the given task. |
---|
| 185 | + * Sample a per-thread clock for the given task. clkid is validated. |
---|
161 | 186 | */ |
---|
162 | | -static int cpu_clock_sample(const clockid_t which_clock, |
---|
163 | | - struct task_struct *p, u64 *sample) |
---|
| 187 | +static u64 cpu_clock_sample(const clockid_t clkid, struct task_struct *p) |
---|
164 | 188 | { |
---|
165 | | - switch (CPUCLOCK_WHICH(which_clock)) { |
---|
166 | | - default: |
---|
167 | | - return -EINVAL; |
---|
| 189 | + u64 utime, stime; |
---|
| 190 | + |
---|
| 191 | + if (clkid == CPUCLOCK_SCHED) |
---|
| 192 | + return task_sched_runtime(p); |
---|
| 193 | + |
---|
| 194 | + task_cputime(p, &utime, &stime); |
---|
| 195 | + |
---|
| 196 | + switch (clkid) { |
---|
168 | 197 | case CPUCLOCK_PROF: |
---|
169 | | - *sample = prof_ticks(p); |
---|
170 | | - break; |
---|
| 198 | + return utime + stime; |
---|
171 | 199 | case CPUCLOCK_VIRT: |
---|
172 | | - *sample = virt_ticks(p); |
---|
173 | | - break; |
---|
174 | | - case CPUCLOCK_SCHED: |
---|
175 | | - *sample = task_sched_runtime(p); |
---|
176 | | - break; |
---|
| 200 | + return utime; |
---|
| 201 | + default: |
---|
| 202 | + WARN_ON_ONCE(1); |
---|
177 | 203 | } |
---|
178 | 204 | return 0; |
---|
| 205 | +} |
---|
| 206 | + |
---|
| 207 | +static inline void store_samples(u64 *samples, u64 stime, u64 utime, u64 rtime) |
---|
| 208 | +{ |
---|
| 209 | + samples[CPUCLOCK_PROF] = stime + utime; |
---|
| 210 | + samples[CPUCLOCK_VIRT] = utime; |
---|
| 211 | + samples[CPUCLOCK_SCHED] = rtime; |
---|
| 212 | +} |
---|
| 213 | + |
---|
| 214 | +static void task_sample_cputime(struct task_struct *p, u64 *samples) |
---|
| 215 | +{ |
---|
| 216 | + u64 stime, utime; |
---|
| 217 | + |
---|
| 218 | + task_cputime(p, &utime, &stime); |
---|
| 219 | + store_samples(samples, stime, utime, p->se.sum_exec_runtime); |
---|
| 220 | +} |
---|
| 221 | + |
---|
| 222 | +static void proc_sample_cputime_atomic(struct task_cputime_atomic *at, |
---|
| 223 | + u64 *samples) |
---|
| 224 | +{ |
---|
| 225 | + u64 stime, utime, rtime; |
---|
| 226 | + |
---|
| 227 | + utime = atomic64_read(&at->utime); |
---|
| 228 | + stime = atomic64_read(&at->stime); |
---|
| 229 | + rtime = atomic64_read(&at->sum_exec_runtime); |
---|
| 230 | + store_samples(samples, stime, utime, rtime); |
---|
179 | 231 | } |
---|
180 | 232 | |
---|
181 | 233 | /* |
---|
.. | .. |
---|
193 | 245 | } |
---|
194 | 246 | } |
---|
195 | 247 | |
---|
196 | | -static void update_gt_cputime(struct task_cputime_atomic *cputime_atomic, struct task_cputime *sum) |
---|
| 248 | +static void update_gt_cputime(struct task_cputime_atomic *cputime_atomic, |
---|
| 249 | + struct task_cputime *sum) |
---|
197 | 250 | { |
---|
198 | 251 | __update_gt_cputime(&cputime_atomic->utime, sum->utime); |
---|
199 | 252 | __update_gt_cputime(&cputime_atomic->stime, sum->stime); |
---|
200 | 253 | __update_gt_cputime(&cputime_atomic->sum_exec_runtime, sum->sum_exec_runtime); |
---|
201 | 254 | } |
---|
202 | 255 | |
---|
203 | | -/* Sample task_cputime_atomic values in "atomic_timers", store results in "times". */ |
---|
204 | | -static inline void sample_cputime_atomic(struct task_cputime *times, |
---|
205 | | - struct task_cputime_atomic *atomic_times) |
---|
206 | | -{ |
---|
207 | | - times->utime = atomic64_read(&atomic_times->utime); |
---|
208 | | - times->stime = atomic64_read(&atomic_times->stime); |
---|
209 | | - times->sum_exec_runtime = atomic64_read(&atomic_times->sum_exec_runtime); |
---|
210 | | -} |
---|
211 | | - |
---|
212 | | -void thread_group_cputimer(struct task_struct *tsk, struct task_cputime *times) |
---|
| 256 | +/** |
---|
| 257 | + * thread_group_sample_cputime - Sample cputime for a given task |
---|
| 258 | + * @tsk: Task for which cputime needs to be started |
---|
| 259 | + * @samples: Storage for time samples |
---|
| 260 | + * |
---|
| 261 | + * Called from sys_getitimer() to calculate the expiry time of an active |
---|
| 262 | + * timer. That means group cputime accounting is already active. Called |
---|
| 263 | + * with task sighand lock held. |
---|
| 264 | + * |
---|
| 265 | + * Updates @times with an uptodate sample of the thread group cputimes. |
---|
| 266 | + */ |
---|
| 267 | +void thread_group_sample_cputime(struct task_struct *tsk, u64 *samples) |
---|
213 | 268 | { |
---|
214 | 269 | struct thread_group_cputimer *cputimer = &tsk->signal->cputimer; |
---|
215 | | - struct task_cputime sum; |
---|
| 270 | + struct posix_cputimers *pct = &tsk->signal->posix_cputimers; |
---|
| 271 | + |
---|
| 272 | + WARN_ON_ONCE(!pct->timers_active); |
---|
| 273 | + |
---|
| 274 | + proc_sample_cputime_atomic(&cputimer->cputime_atomic, samples); |
---|
| 275 | +} |
---|
| 276 | + |
---|
| 277 | +/** |
---|
| 278 | + * thread_group_start_cputime - Start cputime and return a sample |
---|
| 279 | + * @tsk: Task for which cputime needs to be started |
---|
| 280 | + * @samples: Storage for time samples |
---|
| 281 | + * |
---|
| 282 | + * The thread group cputime accouting is avoided when there are no posix |
---|
| 283 | + * CPU timers armed. Before starting a timer it's required to check whether |
---|
| 284 | + * the time accounting is active. If not, a full update of the atomic |
---|
| 285 | + * accounting store needs to be done and the accounting enabled. |
---|
| 286 | + * |
---|
| 287 | + * Updates @times with an uptodate sample of the thread group cputimes. |
---|
| 288 | + */ |
---|
| 289 | +static void thread_group_start_cputime(struct task_struct *tsk, u64 *samples) |
---|
| 290 | +{ |
---|
| 291 | + struct thread_group_cputimer *cputimer = &tsk->signal->cputimer; |
---|
| 292 | + struct posix_cputimers *pct = &tsk->signal->posix_cputimers; |
---|
216 | 293 | |
---|
217 | 294 | /* Check if cputimer isn't running. This is accessed without locking. */ |
---|
218 | | - if (!READ_ONCE(cputimer->running)) { |
---|
| 295 | + if (!READ_ONCE(pct->timers_active)) { |
---|
| 296 | + struct task_cputime sum; |
---|
| 297 | + |
---|
219 | 298 | /* |
---|
220 | 299 | * The POSIX timer interface allows for absolute time expiry |
---|
221 | 300 | * values through the TIMER_ABSTIME flag, therefore we have |
---|
.. | .. |
---|
225 | 304 | update_gt_cputime(&cputimer->cputime_atomic, &sum); |
---|
226 | 305 | |
---|
227 | 306 | /* |
---|
228 | | - * We're setting cputimer->running without a lock. Ensure |
---|
229 | | - * this only gets written to in one operation. We set |
---|
230 | | - * running after update_gt_cputime() as a small optimization, |
---|
231 | | - * but barriers are not required because update_gt_cputime() |
---|
| 307 | + * We're setting timers_active without a lock. Ensure this |
---|
| 308 | + * only gets written to in one operation. We set it after |
---|
| 309 | + * update_gt_cputime() as a small optimization, but |
---|
| 310 | + * barriers are not required because update_gt_cputime() |
---|
232 | 311 | * can handle concurrent updates. |
---|
233 | 312 | */ |
---|
234 | | - WRITE_ONCE(cputimer->running, true); |
---|
| 313 | + WRITE_ONCE(pct->timers_active, true); |
---|
235 | 314 | } |
---|
236 | | - sample_cputime_atomic(times, &cputimer->cputime_atomic); |
---|
| 315 | + proc_sample_cputime_atomic(&cputimer->cputime_atomic, samples); |
---|
| 316 | +} |
---|
| 317 | + |
---|
| 318 | +static void __thread_group_cputime(struct task_struct *tsk, u64 *samples) |
---|
| 319 | +{ |
---|
| 320 | + struct task_cputime ct; |
---|
| 321 | + |
---|
| 322 | + thread_group_cputime(tsk, &ct); |
---|
| 323 | + store_samples(samples, ct.stime, ct.utime, ct.sum_exec_runtime); |
---|
237 | 324 | } |
---|
238 | 325 | |
---|
239 | 326 | /* |
---|
240 | | - * Sample a process (thread group) clock for the given group_leader task. |
---|
241 | | - * Must be called with task sighand lock held for safe while_each_thread() |
---|
242 | | - * traversal. |
---|
| 327 | + * Sample a process (thread group) clock for the given task clkid. If the |
---|
| 328 | + * group's cputime accounting is already enabled, read the atomic |
---|
| 329 | + * store. Otherwise a full update is required. clkid is already validated. |
---|
243 | 330 | */ |
---|
244 | | -static int cpu_clock_sample_group(const clockid_t which_clock, |
---|
245 | | - struct task_struct *p, |
---|
246 | | - u64 *sample) |
---|
| 331 | +static u64 cpu_clock_sample_group(const clockid_t clkid, struct task_struct *p, |
---|
| 332 | + bool start) |
---|
247 | 333 | { |
---|
248 | | - struct task_cputime cputime; |
---|
| 334 | + struct thread_group_cputimer *cputimer = &p->signal->cputimer; |
---|
| 335 | + struct posix_cputimers *pct = &p->signal->posix_cputimers; |
---|
| 336 | + u64 samples[CPUCLOCK_MAX]; |
---|
249 | 337 | |
---|
250 | | - switch (CPUCLOCK_WHICH(which_clock)) { |
---|
251 | | - default: |
---|
252 | | - return -EINVAL; |
---|
253 | | - case CPUCLOCK_PROF: |
---|
254 | | - thread_group_cputime(p, &cputime); |
---|
255 | | - *sample = cputime.utime + cputime.stime; |
---|
256 | | - break; |
---|
257 | | - case CPUCLOCK_VIRT: |
---|
258 | | - thread_group_cputime(p, &cputime); |
---|
259 | | - *sample = cputime.utime; |
---|
260 | | - break; |
---|
261 | | - case CPUCLOCK_SCHED: |
---|
262 | | - thread_group_cputime(p, &cputime); |
---|
263 | | - *sample = cputime.sum_exec_runtime; |
---|
264 | | - break; |
---|
265 | | - } |
---|
266 | | - return 0; |
---|
267 | | -} |
---|
268 | | - |
---|
269 | | -static int posix_cpu_clock_get_task(struct task_struct *tsk, |
---|
270 | | - const clockid_t which_clock, |
---|
271 | | - struct timespec64 *tp) |
---|
272 | | -{ |
---|
273 | | - int err = -EINVAL; |
---|
274 | | - u64 rtn; |
---|
275 | | - |
---|
276 | | - if (CPUCLOCK_PERTHREAD(which_clock)) { |
---|
277 | | - if (same_thread_group(tsk, current)) |
---|
278 | | - err = cpu_clock_sample(which_clock, tsk, &rtn); |
---|
| 338 | + if (!READ_ONCE(pct->timers_active)) { |
---|
| 339 | + if (start) |
---|
| 340 | + thread_group_start_cputime(p, samples); |
---|
| 341 | + else |
---|
| 342 | + __thread_group_cputime(p, samples); |
---|
279 | 343 | } else { |
---|
280 | | - if (tsk == current || thread_group_leader(tsk)) |
---|
281 | | - err = cpu_clock_sample_group(which_clock, tsk, &rtn); |
---|
| 344 | + proc_sample_cputime_atomic(&cputimer->cputime_atomic, samples); |
---|
282 | 345 | } |
---|
283 | 346 | |
---|
284 | | - if (!err) |
---|
285 | | - *tp = ns_to_timespec64(rtn); |
---|
286 | | - |
---|
287 | | - return err; |
---|
| 347 | + return samples[clkid]; |
---|
288 | 348 | } |
---|
289 | 349 | |
---|
290 | | - |
---|
291 | | -static int posix_cpu_clock_get(const clockid_t which_clock, struct timespec64 *tp) |
---|
| 350 | +static int posix_cpu_clock_get(const clockid_t clock, struct timespec64 *tp) |
---|
292 | 351 | { |
---|
293 | | - const pid_t pid = CPUCLOCK_PID(which_clock); |
---|
294 | | - int err = -EINVAL; |
---|
| 352 | + const clockid_t clkid = CPUCLOCK_WHICH(clock); |
---|
| 353 | + struct task_struct *tsk; |
---|
| 354 | + u64 t; |
---|
295 | 355 | |
---|
296 | | - if (pid == 0) { |
---|
297 | | - /* |
---|
298 | | - * Special case constant value for our own clocks. |
---|
299 | | - * We don't have to do any lookup to find ourselves. |
---|
300 | | - */ |
---|
301 | | - err = posix_cpu_clock_get_task(current, which_clock, tp); |
---|
302 | | - } else { |
---|
303 | | - /* |
---|
304 | | - * Find the given PID, and validate that the caller |
---|
305 | | - * should be able to see it. |
---|
306 | | - */ |
---|
307 | | - struct task_struct *p; |
---|
308 | | - rcu_read_lock(); |
---|
309 | | - p = find_task_by_vpid(pid); |
---|
310 | | - if (p) |
---|
311 | | - err = posix_cpu_clock_get_task(p, which_clock, tp); |
---|
| 356 | + rcu_read_lock(); |
---|
| 357 | + tsk = pid_task(pid_for_clock(clock, true), clock_pid_type(clock)); |
---|
| 358 | + if (!tsk) { |
---|
312 | 359 | rcu_read_unlock(); |
---|
| 360 | + return -EINVAL; |
---|
313 | 361 | } |
---|
314 | 362 | |
---|
315 | | - return err; |
---|
| 363 | + if (CPUCLOCK_PERTHREAD(clock)) |
---|
| 364 | + t = cpu_clock_sample(clkid, tsk); |
---|
| 365 | + else |
---|
| 366 | + t = cpu_clock_sample_group(clkid, tsk, false); |
---|
| 367 | + rcu_read_unlock(); |
---|
| 368 | + |
---|
| 369 | + *tp = ns_to_timespec64(t); |
---|
| 370 | + return 0; |
---|
316 | 371 | } |
---|
317 | 372 | |
---|
318 | 373 | /* |
---|
.. | .. |
---|
322 | 377 | */ |
---|
323 | 378 | static int posix_cpu_timer_create(struct k_itimer *new_timer) |
---|
324 | 379 | { |
---|
325 | | - int ret = 0; |
---|
326 | | - const pid_t pid = CPUCLOCK_PID(new_timer->it_clock); |
---|
327 | | - struct task_struct *p; |
---|
328 | | - |
---|
329 | | - if (CPUCLOCK_WHICH(new_timer->it_clock) >= CPUCLOCK_MAX) |
---|
330 | | - return -EINVAL; |
---|
331 | | - |
---|
332 | | - new_timer->kclock = &clock_posix_cpu; |
---|
333 | | - |
---|
334 | | - INIT_LIST_HEAD(&new_timer->it.cpu.entry); |
---|
| 380 | + static struct lock_class_key posix_cpu_timers_key; |
---|
| 381 | + struct pid *pid; |
---|
335 | 382 | |
---|
336 | 383 | rcu_read_lock(); |
---|
337 | | - if (CPUCLOCK_PERTHREAD(new_timer->it_clock)) { |
---|
338 | | - if (pid == 0) { |
---|
339 | | - p = current; |
---|
340 | | - } else { |
---|
341 | | - p = find_task_by_vpid(pid); |
---|
342 | | - if (p && !same_thread_group(p, current)) |
---|
343 | | - p = NULL; |
---|
344 | | - } |
---|
345 | | - } else { |
---|
346 | | - if (pid == 0) { |
---|
347 | | - p = current->group_leader; |
---|
348 | | - } else { |
---|
349 | | - p = find_task_by_vpid(pid); |
---|
350 | | - if (p && !has_group_leader_pid(p)) |
---|
351 | | - p = NULL; |
---|
352 | | - } |
---|
| 384 | + pid = pid_for_clock(new_timer->it_clock, false); |
---|
| 385 | + if (!pid) { |
---|
| 386 | + rcu_read_unlock(); |
---|
| 387 | + return -EINVAL; |
---|
353 | 388 | } |
---|
354 | | - new_timer->it.cpu.task = p; |
---|
355 | | - if (p) { |
---|
356 | | - get_task_struct(p); |
---|
357 | | - } else { |
---|
358 | | - ret = -EINVAL; |
---|
359 | | - } |
---|
360 | | - rcu_read_unlock(); |
---|
361 | 389 | |
---|
362 | | - return ret; |
---|
| 390 | + /* |
---|
| 391 | + * If posix timer expiry is handled in task work context then |
---|
| 392 | + * timer::it_lock can be taken without disabling interrupts as all |
---|
| 393 | + * other locking happens in task context. This requires a seperate |
---|
| 394 | + * lock class key otherwise regular posix timer expiry would record |
---|
| 395 | + * the lock class being taken in interrupt context and generate a |
---|
| 396 | + * false positive warning. |
---|
| 397 | + */ |
---|
| 398 | + if (IS_ENABLED(CONFIG_POSIX_CPU_TIMERS_TASK_WORK)) |
---|
| 399 | + lockdep_set_class(&new_timer->it_lock, &posix_cpu_timers_key); |
---|
| 400 | + |
---|
| 401 | + new_timer->kclock = &clock_posix_cpu; |
---|
| 402 | + timerqueue_init(&new_timer->it.cpu.node); |
---|
| 403 | + new_timer->it.cpu.pid = get_pid(pid); |
---|
| 404 | + rcu_read_unlock(); |
---|
| 405 | + return 0; |
---|
363 | 406 | } |
---|
364 | 407 | |
---|
365 | 408 | /* |
---|
.. | .. |
---|
370 | 413 | */ |
---|
371 | 414 | static int posix_cpu_timer_del(struct k_itimer *timer) |
---|
372 | 415 | { |
---|
373 | | - int ret = 0; |
---|
374 | | - unsigned long flags; |
---|
| 416 | + struct cpu_timer *ctmr = &timer->it.cpu; |
---|
375 | 417 | struct sighand_struct *sighand; |
---|
376 | | - struct task_struct *p = timer->it.cpu.task; |
---|
| 418 | + struct task_struct *p; |
---|
| 419 | + unsigned long flags; |
---|
| 420 | + int ret = 0; |
---|
377 | 421 | |
---|
378 | | - if (WARN_ON_ONCE(!p)) |
---|
379 | | - return -EINVAL; |
---|
| 422 | + rcu_read_lock(); |
---|
| 423 | + p = cpu_timer_task_rcu(timer); |
---|
| 424 | + if (!p) |
---|
| 425 | + goto out; |
---|
380 | 426 | |
---|
381 | 427 | /* |
---|
382 | 428 | * Protect against sighand release/switch in exit/exec and process/ |
---|
.. | .. |
---|
385 | 431 | sighand = lock_task_sighand(p, &flags); |
---|
386 | 432 | if (unlikely(sighand == NULL)) { |
---|
387 | 433 | /* |
---|
388 | | - * We raced with the reaping of the task. |
---|
389 | | - * The deletion should have cleared us off the list. |
---|
| 434 | + * This raced with the reaping of the task. The exit cleanup |
---|
| 435 | + * should have removed this timer from the timer queue. |
---|
390 | 436 | */ |
---|
391 | | - WARN_ON_ONCE(!list_empty(&timer->it.cpu.entry)); |
---|
| 437 | + WARN_ON_ONCE(ctmr->head || timerqueue_node_queued(&ctmr->node)); |
---|
392 | 438 | } else { |
---|
393 | 439 | if (timer->it.cpu.firing) |
---|
394 | 440 | ret = TIMER_RETRY; |
---|
395 | 441 | else |
---|
396 | | - list_del(&timer->it.cpu.entry); |
---|
| 442 | + cpu_timer_dequeue(ctmr); |
---|
397 | 443 | |
---|
398 | 444 | unlock_task_sighand(p, &flags); |
---|
399 | 445 | } |
---|
400 | 446 | |
---|
| 447 | +out: |
---|
| 448 | + rcu_read_unlock(); |
---|
401 | 449 | if (!ret) |
---|
402 | | - put_task_struct(p); |
---|
| 450 | + put_pid(ctmr->pid); |
---|
403 | 451 | |
---|
404 | 452 | return ret; |
---|
405 | 453 | } |
---|
406 | 454 | |
---|
407 | | -static void cleanup_timers_list(struct list_head *head) |
---|
| 455 | +static void cleanup_timerqueue(struct timerqueue_head *head) |
---|
408 | 456 | { |
---|
409 | | - struct cpu_timer_list *timer, *next; |
---|
| 457 | + struct timerqueue_node *node; |
---|
| 458 | + struct cpu_timer *ctmr; |
---|
410 | 459 | |
---|
411 | | - list_for_each_entry_safe(timer, next, head, entry) |
---|
412 | | - list_del_init(&timer->entry); |
---|
| 460 | + while ((node = timerqueue_getnext(head))) { |
---|
| 461 | + timerqueue_del(head, node); |
---|
| 462 | + ctmr = container_of(node, struct cpu_timer, node); |
---|
| 463 | + ctmr->head = NULL; |
---|
| 464 | + } |
---|
413 | 465 | } |
---|
414 | 466 | |
---|
415 | 467 | /* |
---|
416 | | - * Clean out CPU timers still ticking when a thread exited. The task |
---|
417 | | - * pointer is cleared, and the expiry time is replaced with the residual |
---|
418 | | - * time for later timer_gettime calls to return. |
---|
| 468 | + * Clean out CPU timers which are still armed when a thread exits. The |
---|
| 469 | + * timers are only removed from the list. No other updates are done. The |
---|
| 470 | + * corresponding posix timers are still accessible, but cannot be rearmed. |
---|
| 471 | + * |
---|
419 | 472 | * This must be called with the siglock held. |
---|
420 | 473 | */ |
---|
421 | | -static void cleanup_timers(struct list_head *head) |
---|
| 474 | +static void cleanup_timers(struct posix_cputimers *pct) |
---|
422 | 475 | { |
---|
423 | | - cleanup_timers_list(head); |
---|
424 | | - cleanup_timers_list(++head); |
---|
425 | | - cleanup_timers_list(++head); |
---|
| 476 | + cleanup_timerqueue(&pct->bases[CPUCLOCK_PROF].tqhead); |
---|
| 477 | + cleanup_timerqueue(&pct->bases[CPUCLOCK_VIRT].tqhead); |
---|
| 478 | + cleanup_timerqueue(&pct->bases[CPUCLOCK_SCHED].tqhead); |
---|
426 | 479 | } |
---|
427 | 480 | |
---|
428 | 481 | /* |
---|
.. | .. |
---|
432 | 485 | */ |
---|
433 | 486 | void posix_cpu_timers_exit(struct task_struct *tsk) |
---|
434 | 487 | { |
---|
435 | | - cleanup_timers(tsk->cpu_timers); |
---|
| 488 | + cleanup_timers(&tsk->posix_cputimers); |
---|
436 | 489 | } |
---|
437 | 490 | void posix_cpu_timers_exit_group(struct task_struct *tsk) |
---|
438 | 491 | { |
---|
439 | | - cleanup_timers(tsk->signal->cpu_timers); |
---|
440 | | -} |
---|
441 | | - |
---|
442 | | -static inline int expires_gt(u64 expires, u64 new_exp) |
---|
443 | | -{ |
---|
444 | | - return expires == 0 || expires > new_exp; |
---|
| 492 | + cleanup_timers(&tsk->signal->posix_cputimers); |
---|
445 | 493 | } |
---|
446 | 494 | |
---|
447 | 495 | /* |
---|
448 | 496 | * Insert the timer on the appropriate list before any timers that |
---|
449 | 497 | * expire later. This must be called with the sighand lock held. |
---|
450 | 498 | */ |
---|
451 | | -static void arm_timer(struct k_itimer *timer) |
---|
| 499 | +static void arm_timer(struct k_itimer *timer, struct task_struct *p) |
---|
452 | 500 | { |
---|
453 | | - struct task_struct *p = timer->it.cpu.task; |
---|
454 | | - struct list_head *head, *listpos; |
---|
455 | | - struct task_cputime *cputime_expires; |
---|
456 | | - struct cpu_timer_list *const nt = &timer->it.cpu; |
---|
457 | | - struct cpu_timer_list *next; |
---|
| 501 | + int clkidx = CPUCLOCK_WHICH(timer->it_clock); |
---|
| 502 | + struct cpu_timer *ctmr = &timer->it.cpu; |
---|
| 503 | + u64 newexp = cpu_timer_getexpires(ctmr); |
---|
| 504 | + struct posix_cputimer_base *base; |
---|
458 | 505 | |
---|
459 | | - if (CPUCLOCK_PERTHREAD(timer->it_clock)) { |
---|
460 | | - head = p->cpu_timers; |
---|
461 | | - cputime_expires = &p->cputime_expires; |
---|
462 | | - } else { |
---|
463 | | - head = p->signal->cpu_timers; |
---|
464 | | - cputime_expires = &p->signal->cputime_expires; |
---|
465 | | - } |
---|
466 | | - head += CPUCLOCK_WHICH(timer->it_clock); |
---|
| 506 | + if (CPUCLOCK_PERTHREAD(timer->it_clock)) |
---|
| 507 | + base = p->posix_cputimers.bases + clkidx; |
---|
| 508 | + else |
---|
| 509 | + base = p->signal->posix_cputimers.bases + clkidx; |
---|
467 | 510 | |
---|
468 | | - listpos = head; |
---|
469 | | - list_for_each_entry(next, head, entry) { |
---|
470 | | - if (nt->expires < next->expires) |
---|
471 | | - break; |
---|
472 | | - listpos = &next->entry; |
---|
473 | | - } |
---|
474 | | - list_add(&nt->entry, listpos); |
---|
| 511 | + if (!cpu_timer_enqueue(&base->tqhead, ctmr)) |
---|
| 512 | + return; |
---|
475 | 513 | |
---|
476 | | - if (listpos == head) { |
---|
477 | | - u64 exp = nt->expires; |
---|
| 514 | + /* |
---|
| 515 | + * We are the new earliest-expiring POSIX 1.b timer, hence |
---|
| 516 | + * need to update expiration cache. Take into account that |
---|
| 517 | + * for process timers we share expiration cache with itimers |
---|
| 518 | + * and RLIMIT_CPU and for thread timers with RLIMIT_RTTIME. |
---|
| 519 | + */ |
---|
| 520 | + if (newexp < base->nextevt) |
---|
| 521 | + base->nextevt = newexp; |
---|
478 | 522 | |
---|
479 | | - /* |
---|
480 | | - * We are the new earliest-expiring POSIX 1.b timer, hence |
---|
481 | | - * need to update expiration cache. Take into account that |
---|
482 | | - * for process timers we share expiration cache with itimers |
---|
483 | | - * and RLIMIT_CPU and for thread timers with RLIMIT_RTTIME. |
---|
484 | | - */ |
---|
485 | | - |
---|
486 | | - switch (CPUCLOCK_WHICH(timer->it_clock)) { |
---|
487 | | - case CPUCLOCK_PROF: |
---|
488 | | - if (expires_gt(cputime_expires->prof_exp, exp)) |
---|
489 | | - cputime_expires->prof_exp = exp; |
---|
490 | | - break; |
---|
491 | | - case CPUCLOCK_VIRT: |
---|
492 | | - if (expires_gt(cputime_expires->virt_exp, exp)) |
---|
493 | | - cputime_expires->virt_exp = exp; |
---|
494 | | - break; |
---|
495 | | - case CPUCLOCK_SCHED: |
---|
496 | | - if (expires_gt(cputime_expires->sched_exp, exp)) |
---|
497 | | - cputime_expires->sched_exp = exp; |
---|
498 | | - break; |
---|
499 | | - } |
---|
500 | | - if (CPUCLOCK_PERTHREAD(timer->it_clock)) |
---|
501 | | - tick_dep_set_task(p, TICK_DEP_BIT_POSIX_TIMER); |
---|
502 | | - else |
---|
503 | | - tick_dep_set_signal(p->signal, TICK_DEP_BIT_POSIX_TIMER); |
---|
504 | | - } |
---|
| 523 | + if (CPUCLOCK_PERTHREAD(timer->it_clock)) |
---|
| 524 | + tick_dep_set_task(p, TICK_DEP_BIT_POSIX_TIMER); |
---|
| 525 | + else |
---|
| 526 | + tick_dep_set_signal(p->signal, TICK_DEP_BIT_POSIX_TIMER); |
---|
505 | 527 | } |
---|
506 | 528 | |
---|
507 | 529 | /* |
---|
.. | .. |
---|
509 | 531 | */ |
---|
510 | 532 | static void cpu_timer_fire(struct k_itimer *timer) |
---|
511 | 533 | { |
---|
| 534 | + struct cpu_timer *ctmr = &timer->it.cpu; |
---|
| 535 | + |
---|
512 | 536 | if ((timer->it_sigev_notify & ~SIGEV_THREAD_ID) == SIGEV_NONE) { |
---|
513 | 537 | /* |
---|
514 | 538 | * User don't want any signal. |
---|
515 | 539 | */ |
---|
516 | | - timer->it.cpu.expires = 0; |
---|
| 540 | + cpu_timer_setexpires(ctmr, 0); |
---|
517 | 541 | } else if (unlikely(timer->sigq == NULL)) { |
---|
518 | 542 | /* |
---|
519 | 543 | * This a special case for clock_nanosleep, |
---|
520 | 544 | * not a normal timer from sys_timer_create. |
---|
521 | 545 | */ |
---|
522 | 546 | wake_up_process(timer->it_process); |
---|
523 | | - timer->it.cpu.expires = 0; |
---|
524 | | - } else if (timer->it.cpu.incr == 0) { |
---|
| 547 | + cpu_timer_setexpires(ctmr, 0); |
---|
| 548 | + } else if (!timer->it_interval) { |
---|
525 | 549 | /* |
---|
526 | 550 | * One-shot timer. Clear it as soon as it's fired. |
---|
527 | 551 | */ |
---|
528 | 552 | posix_timer_event(timer, 0); |
---|
529 | | - timer->it.cpu.expires = 0; |
---|
| 553 | + cpu_timer_setexpires(ctmr, 0); |
---|
530 | 554 | } else if (posix_timer_event(timer, ++timer->it_requeue_pending)) { |
---|
531 | 555 | /* |
---|
532 | 556 | * The signal did not get queued because the signal |
---|
.. | .. |
---|
540 | 564 | } |
---|
541 | 565 | |
---|
542 | 566 | /* |
---|
543 | | - * Sample a process (thread group) timer for the given group_leader task. |
---|
544 | | - * Must be called with task sighand lock held for safe while_each_thread() |
---|
545 | | - * traversal. |
---|
546 | | - */ |
---|
547 | | -static int cpu_timer_sample_group(const clockid_t which_clock, |
---|
548 | | - struct task_struct *p, u64 *sample) |
---|
549 | | -{ |
---|
550 | | - struct task_cputime cputime; |
---|
551 | | - |
---|
552 | | - thread_group_cputimer(p, &cputime); |
---|
553 | | - switch (CPUCLOCK_WHICH(which_clock)) { |
---|
554 | | - default: |
---|
555 | | - return -EINVAL; |
---|
556 | | - case CPUCLOCK_PROF: |
---|
557 | | - *sample = cputime.utime + cputime.stime; |
---|
558 | | - break; |
---|
559 | | - case CPUCLOCK_VIRT: |
---|
560 | | - *sample = cputime.utime; |
---|
561 | | - break; |
---|
562 | | - case CPUCLOCK_SCHED: |
---|
563 | | - *sample = cputime.sum_exec_runtime; |
---|
564 | | - break; |
---|
565 | | - } |
---|
566 | | - return 0; |
---|
567 | | -} |
---|
568 | | - |
---|
569 | | -/* |
---|
570 | 567 | * Guts of sys_timer_settime for CPU timers. |
---|
571 | 568 | * This is called with the timer locked and interrupts disabled. |
---|
572 | 569 | * If we return TIMER_RETRY, it's necessary to release the timer's lock |
---|
.. | .. |
---|
575 | 572 | static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags, |
---|
576 | 573 | struct itimerspec64 *new, struct itimerspec64 *old) |
---|
577 | 574 | { |
---|
578 | | - unsigned long flags; |
---|
579 | | - struct sighand_struct *sighand; |
---|
580 | | - struct task_struct *p = timer->it.cpu.task; |
---|
| 575 | + clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock); |
---|
581 | 576 | u64 old_expires, new_expires, old_incr, val; |
---|
582 | | - int ret; |
---|
| 577 | + struct cpu_timer *ctmr = &timer->it.cpu; |
---|
| 578 | + struct sighand_struct *sighand; |
---|
| 579 | + struct task_struct *p; |
---|
| 580 | + unsigned long flags; |
---|
| 581 | + int ret = 0; |
---|
583 | 582 | |
---|
584 | | - if (WARN_ON_ONCE(!p)) |
---|
585 | | - return -EINVAL; |
---|
| 583 | + rcu_read_lock(); |
---|
| 584 | + p = cpu_timer_task_rcu(timer); |
---|
| 585 | + if (!p) { |
---|
| 586 | + /* |
---|
| 587 | + * If p has just been reaped, we can no |
---|
| 588 | + * longer get any information about it at all. |
---|
| 589 | + */ |
---|
| 590 | + rcu_read_unlock(); |
---|
| 591 | + return -ESRCH; |
---|
| 592 | + } |
---|
586 | 593 | |
---|
587 | 594 | /* |
---|
588 | 595 | * Use the to_ktime conversion because that clamps the maximum |
---|
.. | .. |
---|
600 | 607 | * longer get any information about it at all. |
---|
601 | 608 | */ |
---|
602 | 609 | if (unlikely(sighand == NULL)) { |
---|
| 610 | + rcu_read_unlock(); |
---|
603 | 611 | return -ESRCH; |
---|
604 | 612 | } |
---|
605 | 613 | |
---|
606 | 614 | /* |
---|
607 | 615 | * Disarm any old timer after extracting its expiry time. |
---|
608 | 616 | */ |
---|
| 617 | + old_incr = timer->it_interval; |
---|
| 618 | + old_expires = cpu_timer_getexpires(ctmr); |
---|
609 | 619 | |
---|
610 | | - ret = 0; |
---|
611 | | - old_incr = timer->it.cpu.incr; |
---|
612 | | - old_expires = timer->it.cpu.expires; |
---|
613 | 620 | if (unlikely(timer->it.cpu.firing)) { |
---|
614 | 621 | timer->it.cpu.firing = -1; |
---|
615 | 622 | ret = TIMER_RETRY; |
---|
616 | | - } else |
---|
617 | | - list_del_init(&timer->it.cpu.entry); |
---|
| 623 | + } else { |
---|
| 624 | + cpu_timer_dequeue(ctmr); |
---|
| 625 | + } |
---|
618 | 626 | |
---|
619 | 627 | /* |
---|
620 | 628 | * We need to sample the current value to convert the new |
---|
.. | .. |
---|
624 | 632 | * times (in arm_timer). With an absolute time, we must |
---|
625 | 633 | * check if it's already passed. In short, we need a sample. |
---|
626 | 634 | */ |
---|
627 | | - if (CPUCLOCK_PERTHREAD(timer->it_clock)) { |
---|
628 | | - cpu_clock_sample(timer->it_clock, p, &val); |
---|
629 | | - } else { |
---|
630 | | - cpu_timer_sample_group(timer->it_clock, p, &val); |
---|
631 | | - } |
---|
| 635 | + if (CPUCLOCK_PERTHREAD(timer->it_clock)) |
---|
| 636 | + val = cpu_clock_sample(clkid, p); |
---|
| 637 | + else |
---|
| 638 | + val = cpu_clock_sample_group(clkid, p, true); |
---|
632 | 639 | |
---|
633 | 640 | if (old) { |
---|
634 | 641 | if (old_expires == 0) { |
---|
.. | .. |
---|
636 | 643 | old->it_value.tv_nsec = 0; |
---|
637 | 644 | } else { |
---|
638 | 645 | /* |
---|
639 | | - * Update the timer in case it has |
---|
640 | | - * overrun already. If it has, |
---|
641 | | - * we'll report it as having overrun |
---|
642 | | - * and with the next reloaded timer |
---|
643 | | - * already ticking, though we are |
---|
644 | | - * swallowing that pending |
---|
645 | | - * notification here to install the |
---|
646 | | - * new setting. |
---|
| 646 | + * Update the timer in case it has overrun already. |
---|
| 647 | + * If it has, we'll report it as having overrun and |
---|
| 648 | + * with the next reloaded timer already ticking, |
---|
| 649 | + * though we are swallowing that pending |
---|
| 650 | + * notification here to install the new setting. |
---|
647 | 651 | */ |
---|
648 | | - bump_cpu_timer(timer, val); |
---|
649 | | - if (val < timer->it.cpu.expires) { |
---|
650 | | - old_expires = timer->it.cpu.expires - val; |
---|
| 652 | + u64 exp = bump_cpu_timer(timer, val); |
---|
| 653 | + |
---|
| 654 | + if (val < exp) { |
---|
| 655 | + old_expires = exp - val; |
---|
651 | 656 | old->it_value = ns_to_timespec64(old_expires); |
---|
652 | 657 | } else { |
---|
653 | 658 | old->it_value.tv_nsec = 1; |
---|
.. | .. |
---|
676 | 681 | * For a timer with no notification action, we don't actually |
---|
677 | 682 | * arm the timer (we'll just fake it for timer_gettime). |
---|
678 | 683 | */ |
---|
679 | | - timer->it.cpu.expires = new_expires; |
---|
| 684 | + cpu_timer_setexpires(ctmr, new_expires); |
---|
680 | 685 | if (new_expires != 0 && val < new_expires) { |
---|
681 | | - arm_timer(timer); |
---|
| 686 | + arm_timer(timer, p); |
---|
682 | 687 | } |
---|
683 | 688 | |
---|
684 | 689 | unlock_task_sighand(p, &flags); |
---|
.. | .. |
---|
686 | 691 | * Install the new reload setting, and |
---|
687 | 692 | * set up the signal and overrun bookkeeping. |
---|
688 | 693 | */ |
---|
689 | | - timer->it.cpu.incr = timespec64_to_ns(&new->it_interval); |
---|
690 | | - timer->it_interval = ns_to_ktime(timer->it.cpu.incr); |
---|
| 694 | + timer->it_interval = timespec64_to_ktime(new->it_interval); |
---|
691 | 695 | |
---|
692 | 696 | /* |
---|
693 | 697 | * This acts as a modification timestamp for the timer, |
---|
.. | .. |
---|
710 | 714 | |
---|
711 | 715 | ret = 0; |
---|
712 | 716 | out: |
---|
| 717 | + rcu_read_unlock(); |
---|
713 | 718 | if (old) |
---|
714 | 719 | old->it_interval = ns_to_timespec64(old_incr); |
---|
715 | 720 | |
---|
.. | .. |
---|
718 | 723 | |
---|
719 | 724 | static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec64 *itp) |
---|
720 | 725 | { |
---|
721 | | - struct task_struct *p = timer->it.cpu.task; |
---|
722 | | - u64 now; |
---|
| 726 | + clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock); |
---|
| 727 | + struct cpu_timer *ctmr = &timer->it.cpu; |
---|
| 728 | + u64 now, expires = cpu_timer_getexpires(ctmr); |
---|
| 729 | + struct task_struct *p; |
---|
723 | 730 | |
---|
724 | | - if (WARN_ON_ONCE(!p)) |
---|
725 | | - return; |
---|
| 731 | + rcu_read_lock(); |
---|
| 732 | + p = cpu_timer_task_rcu(timer); |
---|
| 733 | + if (!p) |
---|
| 734 | + goto out; |
---|
726 | 735 | |
---|
727 | 736 | /* |
---|
728 | 737 | * Easy part: convert the reload time. |
---|
729 | 738 | */ |
---|
730 | | - itp->it_interval = ns_to_timespec64(timer->it.cpu.incr); |
---|
| 739 | + itp->it_interval = ktime_to_timespec64(timer->it_interval); |
---|
731 | 740 | |
---|
732 | | - if (!timer->it.cpu.expires) |
---|
733 | | - return; |
---|
| 741 | + if (!expires) |
---|
| 742 | + goto out; |
---|
734 | 743 | |
---|
735 | 744 | /* |
---|
736 | 745 | * Sample the clock to take the difference with the expiry time. |
---|
737 | 746 | */ |
---|
738 | | - if (CPUCLOCK_PERTHREAD(timer->it_clock)) { |
---|
739 | | - cpu_clock_sample(timer->it_clock, p, &now); |
---|
740 | | - } else { |
---|
741 | | - struct sighand_struct *sighand; |
---|
742 | | - unsigned long flags; |
---|
| 747 | + if (CPUCLOCK_PERTHREAD(timer->it_clock)) |
---|
| 748 | + now = cpu_clock_sample(clkid, p); |
---|
| 749 | + else |
---|
| 750 | + now = cpu_clock_sample_group(clkid, p, false); |
---|
743 | 751 | |
---|
744 | | - /* |
---|
745 | | - * Protect against sighand release/switch in exit/exec and |
---|
746 | | - * also make timer sampling safe if it ends up calling |
---|
747 | | - * thread_group_cputime(). |
---|
748 | | - */ |
---|
749 | | - sighand = lock_task_sighand(p, &flags); |
---|
750 | | - if (unlikely(sighand == NULL)) { |
---|
751 | | - /* |
---|
752 | | - * The process has been reaped. |
---|
753 | | - * We can't even collect a sample any more. |
---|
754 | | - * Call the timer disarmed, nothing else to do. |
---|
755 | | - */ |
---|
756 | | - timer->it.cpu.expires = 0; |
---|
757 | | - return; |
---|
758 | | - } else { |
---|
759 | | - cpu_timer_sample_group(timer->it_clock, p, &now); |
---|
760 | | - unlock_task_sighand(p, &flags); |
---|
761 | | - } |
---|
762 | | - } |
---|
763 | | - |
---|
764 | | - if (now < timer->it.cpu.expires) { |
---|
765 | | - itp->it_value = ns_to_timespec64(timer->it.cpu.expires - now); |
---|
| 752 | + if (now < expires) { |
---|
| 753 | + itp->it_value = ns_to_timespec64(expires - now); |
---|
766 | 754 | } else { |
---|
767 | 755 | /* |
---|
768 | 756 | * The timer should have expired already, but the firing |
---|
.. | .. |
---|
771 | 759 | itp->it_value.tv_nsec = 1; |
---|
772 | 760 | itp->it_value.tv_sec = 0; |
---|
773 | 761 | } |
---|
| 762 | +out: |
---|
| 763 | + rcu_read_unlock(); |
---|
774 | 764 | } |
---|
775 | 765 | |
---|
776 | | -static unsigned long long |
---|
777 | | -check_timers_list(struct list_head *timers, |
---|
778 | | - struct list_head *firing, |
---|
779 | | - unsigned long long curr) |
---|
| 766 | +#define MAX_COLLECTED 20 |
---|
| 767 | + |
---|
| 768 | +static u64 collect_timerqueue(struct timerqueue_head *head, |
---|
| 769 | + struct list_head *firing, u64 now) |
---|
780 | 770 | { |
---|
781 | | - int maxfire = 20; |
---|
| 771 | + struct timerqueue_node *next; |
---|
| 772 | + int i = 0; |
---|
782 | 773 | |
---|
783 | | - while (!list_empty(timers)) { |
---|
784 | | - struct cpu_timer_list *t; |
---|
| 774 | + while ((next = timerqueue_getnext(head))) { |
---|
| 775 | + struct cpu_timer *ctmr; |
---|
| 776 | + u64 expires; |
---|
785 | 777 | |
---|
786 | | - t = list_first_entry(timers, struct cpu_timer_list, entry); |
---|
| 778 | + ctmr = container_of(next, struct cpu_timer, node); |
---|
| 779 | + expires = cpu_timer_getexpires(ctmr); |
---|
| 780 | + /* Limit the number of timers to expire at once */ |
---|
| 781 | + if (++i == MAX_COLLECTED || now < expires) |
---|
| 782 | + return expires; |
---|
787 | 783 | |
---|
788 | | - if (!--maxfire || curr < t->expires) |
---|
789 | | - return t->expires; |
---|
790 | | - |
---|
791 | | - t->firing = 1; |
---|
792 | | - list_move_tail(&t->entry, firing); |
---|
| 784 | + ctmr->firing = 1; |
---|
| 785 | + cpu_timer_dequeue(ctmr); |
---|
| 786 | + list_add_tail(&ctmr->elist, firing); |
---|
793 | 787 | } |
---|
794 | 788 | |
---|
795 | | - return 0; |
---|
| 789 | + return U64_MAX; |
---|
| 790 | +} |
---|
| 791 | + |
---|
| 792 | +static void collect_posix_cputimers(struct posix_cputimers *pct, u64 *samples, |
---|
| 793 | + struct list_head *firing) |
---|
| 794 | +{ |
---|
| 795 | + struct posix_cputimer_base *base = pct->bases; |
---|
| 796 | + int i; |
---|
| 797 | + |
---|
| 798 | + for (i = 0; i < CPUCLOCK_MAX; i++, base++) { |
---|
| 799 | + base->nextevt = collect_timerqueue(&base->tqhead, firing, |
---|
| 800 | + samples[i]); |
---|
| 801 | + } |
---|
796 | 802 | } |
---|
797 | 803 | |
---|
798 | 804 | static inline void check_dl_overrun(struct task_struct *tsk) |
---|
.. | .. |
---|
803 | 809 | } |
---|
804 | 810 | } |
---|
805 | 811 | |
---|
| 812 | +static bool check_rlimit(u64 time, u64 limit, int signo, bool rt, bool hard) |
---|
| 813 | +{ |
---|
| 814 | + if (time < limit) |
---|
| 815 | + return false; |
---|
| 816 | + |
---|
| 817 | + if (print_fatal_signals) { |
---|
| 818 | + pr_info("%s Watchdog Timeout (%s): %s[%d]\n", |
---|
| 819 | + rt ? "RT" : "CPU", hard ? "hard" : "soft", |
---|
| 820 | + current->comm, task_pid_nr(current)); |
---|
| 821 | + } |
---|
| 822 | + __group_send_sig_info(signo, SEND_SIG_PRIV, current); |
---|
| 823 | + return true; |
---|
| 824 | +} |
---|
| 825 | + |
---|
806 | 826 | /* |
---|
807 | 827 | * Check for any per-thread CPU timers that have fired and move them off |
---|
808 | 828 | * the tsk->cpu_timers[N] list onto the firing list. Here we update the |
---|
.. | .. |
---|
811 | 831 | static void check_thread_timers(struct task_struct *tsk, |
---|
812 | 832 | struct list_head *firing) |
---|
813 | 833 | { |
---|
814 | | - struct list_head *timers = tsk->cpu_timers; |
---|
815 | | - struct task_cputime *tsk_expires = &tsk->cputime_expires; |
---|
816 | | - u64 expires; |
---|
| 834 | + struct posix_cputimers *pct = &tsk->posix_cputimers; |
---|
| 835 | + u64 samples[CPUCLOCK_MAX]; |
---|
817 | 836 | unsigned long soft; |
---|
818 | 837 | |
---|
819 | 838 | if (dl_task(tsk)) |
---|
820 | 839 | check_dl_overrun(tsk); |
---|
821 | 840 | |
---|
822 | | - /* |
---|
823 | | - * If cputime_expires is zero, then there are no active |
---|
824 | | - * per thread CPU timers. |
---|
825 | | - */ |
---|
826 | | - if (task_cputime_zero(&tsk->cputime_expires)) |
---|
| 841 | + if (expiry_cache_is_inactive(pct)) |
---|
827 | 842 | return; |
---|
828 | 843 | |
---|
829 | | - expires = check_timers_list(timers, firing, prof_ticks(tsk)); |
---|
830 | | - tsk_expires->prof_exp = expires; |
---|
831 | | - |
---|
832 | | - expires = check_timers_list(++timers, firing, virt_ticks(tsk)); |
---|
833 | | - tsk_expires->virt_exp = expires; |
---|
834 | | - |
---|
835 | | - tsk_expires->sched_exp = check_timers_list(++timers, firing, |
---|
836 | | - tsk->se.sum_exec_runtime); |
---|
| 844 | + task_sample_cputime(tsk, samples); |
---|
| 845 | + collect_posix_cputimers(pct, samples, firing); |
---|
837 | 846 | |
---|
838 | 847 | /* |
---|
839 | 848 | * Check for the special case thread timers. |
---|
840 | 849 | */ |
---|
841 | 850 | soft = task_rlimit(tsk, RLIMIT_RTTIME); |
---|
842 | 851 | if (soft != RLIM_INFINITY) { |
---|
| 852 | + /* Task RT timeout is accounted in jiffies. RTTIME is usec */ |
---|
| 853 | + unsigned long rttime = tsk->rt.timeout * (USEC_PER_SEC / HZ); |
---|
843 | 854 | unsigned long hard = task_rlimit_max(tsk, RLIMIT_RTTIME); |
---|
844 | 855 | |
---|
| 856 | + /* At the hard limit, send SIGKILL. No further action. */ |
---|
845 | 857 | if (hard != RLIM_INFINITY && |
---|
846 | | - tsk->rt.timeout > DIV_ROUND_UP(hard, USEC_PER_SEC/HZ)) { |
---|
847 | | - /* |
---|
848 | | - * At the hard limit, we just die. |
---|
849 | | - * No need to calculate anything else now. |
---|
850 | | - */ |
---|
851 | | - if (print_fatal_signals) { |
---|
852 | | - pr_info("CPU Watchdog Timeout (hard): %s[%d]\n", |
---|
853 | | - tsk->comm, task_pid_nr(tsk)); |
---|
854 | | - } |
---|
855 | | - __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk); |
---|
| 858 | + check_rlimit(rttime, hard, SIGKILL, true, true)) |
---|
856 | 859 | return; |
---|
857 | | - } |
---|
858 | | - if (tsk->rt.timeout > DIV_ROUND_UP(soft, USEC_PER_SEC/HZ)) { |
---|
859 | | - /* |
---|
860 | | - * At the soft limit, send a SIGXCPU every second. |
---|
861 | | - */ |
---|
862 | | - if (soft < hard) { |
---|
863 | | - soft += USEC_PER_SEC; |
---|
864 | | - tsk->signal->rlim[RLIMIT_RTTIME].rlim_cur = |
---|
865 | | - soft; |
---|
866 | | - } |
---|
867 | | - if (print_fatal_signals) { |
---|
868 | | - pr_info("RT Watchdog Timeout (soft): %s[%d]\n", |
---|
869 | | - tsk->comm, task_pid_nr(tsk)); |
---|
870 | | - } |
---|
871 | | - __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk); |
---|
| 860 | + |
---|
| 861 | + /* At the soft limit, send a SIGXCPU every second */ |
---|
| 862 | + if (check_rlimit(rttime, soft, SIGXCPU, true, false)) { |
---|
| 863 | + soft += USEC_PER_SEC; |
---|
| 864 | + tsk->signal->rlim[RLIMIT_RTTIME].rlim_cur = soft; |
---|
872 | 865 | } |
---|
873 | 866 | } |
---|
874 | | - if (task_cputime_zero(tsk_expires)) |
---|
| 867 | + |
---|
| 868 | + if (expiry_cache_is_inactive(pct)) |
---|
875 | 869 | tick_dep_clear_task(tsk, TICK_DEP_BIT_POSIX_TIMER); |
---|
876 | 870 | } |
---|
877 | 871 | |
---|
878 | 872 | static inline void stop_process_timers(struct signal_struct *sig) |
---|
879 | 873 | { |
---|
880 | | - struct thread_group_cputimer *cputimer = &sig->cputimer; |
---|
| 874 | + struct posix_cputimers *pct = &sig->posix_cputimers; |
---|
881 | 875 | |
---|
882 | | - /* Turn off cputimer->running. This is done without locking. */ |
---|
883 | | - WRITE_ONCE(cputimer->running, false); |
---|
| 876 | + /* Turn off the active flag. This is done without locking. */ |
---|
| 877 | + WRITE_ONCE(pct->timers_active, false); |
---|
884 | 878 | tick_dep_clear_signal(sig, TICK_DEP_BIT_POSIX_TIMER); |
---|
885 | 879 | } |
---|
886 | 880 | |
---|
.. | .. |
---|
902 | 896 | __group_send_sig_info(signo, SEND_SIG_PRIV, tsk); |
---|
903 | 897 | } |
---|
904 | 898 | |
---|
905 | | - if (it->expires && (!*expires || it->expires < *expires)) |
---|
| 899 | + if (it->expires && it->expires < *expires) |
---|
906 | 900 | *expires = it->expires; |
---|
907 | 901 | } |
---|
908 | 902 | |
---|
.. | .. |
---|
915 | 909 | struct list_head *firing) |
---|
916 | 910 | { |
---|
917 | 911 | struct signal_struct *const sig = tsk->signal; |
---|
918 | | - u64 utime, ptime, virt_expires, prof_expires; |
---|
919 | | - u64 sum_sched_runtime, sched_expires; |
---|
920 | | - struct list_head *timers = sig->cpu_timers; |
---|
921 | | - struct task_cputime cputime; |
---|
| 912 | + struct posix_cputimers *pct = &sig->posix_cputimers; |
---|
| 913 | + u64 samples[CPUCLOCK_MAX]; |
---|
922 | 914 | unsigned long soft; |
---|
923 | 915 | |
---|
924 | | - if (dl_task(tsk)) |
---|
925 | | - check_dl_overrun(tsk); |
---|
926 | | - |
---|
927 | 916 | /* |
---|
928 | | - * If cputimer is not running, then there are no active |
---|
929 | | - * process wide timers (POSIX 1.b, itimers, RLIMIT_CPU). |
---|
| 917 | + * If there are no active process wide timers (POSIX 1.b, itimers, |
---|
| 918 | + * RLIMIT_CPU) nothing to check. Also skip the process wide timer |
---|
| 919 | + * processing when there is already another task handling them. |
---|
930 | 920 | */ |
---|
931 | | - if (!READ_ONCE(tsk->signal->cputimer.running)) |
---|
| 921 | + if (!READ_ONCE(pct->timers_active) || pct->expiry_active) |
---|
932 | 922 | return; |
---|
933 | 923 | |
---|
934 | | - /* |
---|
| 924 | + /* |
---|
935 | 925 | * Signify that a thread is checking for process timers. |
---|
936 | 926 | * Write access to this field is protected by the sighand lock. |
---|
937 | 927 | */ |
---|
938 | | - sig->cputimer.checking_timer = true; |
---|
| 928 | + pct->expiry_active = true; |
---|
939 | 929 | |
---|
940 | 930 | /* |
---|
941 | | - * Collect the current process totals. |
---|
| 931 | + * Collect the current process totals. Group accounting is active |
---|
| 932 | + * so the sample can be taken directly. |
---|
942 | 933 | */ |
---|
943 | | - thread_group_cputimer(tsk, &cputime); |
---|
944 | | - utime = cputime.utime; |
---|
945 | | - ptime = utime + cputime.stime; |
---|
946 | | - sum_sched_runtime = cputime.sum_exec_runtime; |
---|
947 | | - |
---|
948 | | - prof_expires = check_timers_list(timers, firing, ptime); |
---|
949 | | - virt_expires = check_timers_list(++timers, firing, utime); |
---|
950 | | - sched_expires = check_timers_list(++timers, firing, sum_sched_runtime); |
---|
| 934 | + proc_sample_cputime_atomic(&sig->cputimer.cputime_atomic, samples); |
---|
| 935 | + collect_posix_cputimers(pct, samples, firing); |
---|
951 | 936 | |
---|
952 | 937 | /* |
---|
953 | 938 | * Check for the special case process timers. |
---|
954 | 939 | */ |
---|
955 | | - check_cpu_itimer(tsk, &sig->it[CPUCLOCK_PROF], &prof_expires, ptime, |
---|
956 | | - SIGPROF); |
---|
957 | | - check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT], &virt_expires, utime, |
---|
958 | | - SIGVTALRM); |
---|
| 940 | + check_cpu_itimer(tsk, &sig->it[CPUCLOCK_PROF], |
---|
| 941 | + &pct->bases[CPUCLOCK_PROF].nextevt, |
---|
| 942 | + samples[CPUCLOCK_PROF], SIGPROF); |
---|
| 943 | + check_cpu_itimer(tsk, &sig->it[CPUCLOCK_VIRT], |
---|
| 944 | + &pct->bases[CPUCLOCK_VIRT].nextevt, |
---|
| 945 | + samples[CPUCLOCK_VIRT], SIGVTALRM); |
---|
| 946 | + |
---|
959 | 947 | soft = task_rlimit(tsk, RLIMIT_CPU); |
---|
960 | 948 | if (soft != RLIM_INFINITY) { |
---|
961 | | - unsigned long psecs = div_u64(ptime, NSEC_PER_SEC); |
---|
| 949 | + /* RLIMIT_CPU is in seconds. Samples are nanoseconds */ |
---|
962 | 950 | unsigned long hard = task_rlimit_max(tsk, RLIMIT_CPU); |
---|
963 | | - u64 x; |
---|
964 | | - if (psecs >= hard) { |
---|
965 | | - /* |
---|
966 | | - * At the hard limit, we just die. |
---|
967 | | - * No need to calculate anything else now. |
---|
968 | | - */ |
---|
969 | | - if (print_fatal_signals) { |
---|
970 | | - pr_info("RT Watchdog Timeout (hard): %s[%d]\n", |
---|
971 | | - tsk->comm, task_pid_nr(tsk)); |
---|
972 | | - } |
---|
973 | | - __group_send_sig_info(SIGKILL, SEND_SIG_PRIV, tsk); |
---|
| 951 | + u64 ptime = samples[CPUCLOCK_PROF]; |
---|
| 952 | + u64 softns = (u64)soft * NSEC_PER_SEC; |
---|
| 953 | + u64 hardns = (u64)hard * NSEC_PER_SEC; |
---|
| 954 | + |
---|
| 955 | + /* At the hard limit, send SIGKILL. No further action. */ |
---|
| 956 | + if (hard != RLIM_INFINITY && |
---|
| 957 | + check_rlimit(ptime, hardns, SIGKILL, false, true)) |
---|
974 | 958 | return; |
---|
| 959 | + |
---|
| 960 | + /* At the soft limit, send a SIGXCPU every second */ |
---|
| 961 | + if (check_rlimit(ptime, softns, SIGXCPU, false, false)) { |
---|
| 962 | + sig->rlim[RLIMIT_CPU].rlim_cur = soft + 1; |
---|
| 963 | + softns += NSEC_PER_SEC; |
---|
975 | 964 | } |
---|
976 | | - if (psecs >= soft) { |
---|
977 | | - /* |
---|
978 | | - * At the soft limit, send a SIGXCPU every second. |
---|
979 | | - */ |
---|
980 | | - if (print_fatal_signals) { |
---|
981 | | - pr_info("CPU Watchdog Timeout (soft): %s[%d]\n", |
---|
982 | | - tsk->comm, task_pid_nr(tsk)); |
---|
983 | | - } |
---|
984 | | - __group_send_sig_info(SIGXCPU, SEND_SIG_PRIV, tsk); |
---|
985 | | - if (soft < hard) { |
---|
986 | | - soft++; |
---|
987 | | - sig->rlim[RLIMIT_CPU].rlim_cur = soft; |
---|
988 | | - } |
---|
989 | | - } |
---|
990 | | - x = soft * NSEC_PER_SEC; |
---|
991 | | - if (!prof_expires || x < prof_expires) |
---|
992 | | - prof_expires = x; |
---|
| 965 | + |
---|
| 966 | + /* Update the expiry cache */ |
---|
| 967 | + if (softns < pct->bases[CPUCLOCK_PROF].nextevt) |
---|
| 968 | + pct->bases[CPUCLOCK_PROF].nextevt = softns; |
---|
993 | 969 | } |
---|
994 | 970 | |
---|
995 | | - sig->cputime_expires.prof_exp = prof_expires; |
---|
996 | | - sig->cputime_expires.virt_exp = virt_expires; |
---|
997 | | - sig->cputime_expires.sched_exp = sched_expires; |
---|
998 | | - if (task_cputime_zero(&sig->cputime_expires)) |
---|
| 971 | + if (expiry_cache_is_inactive(pct)) |
---|
999 | 972 | stop_process_timers(sig); |
---|
1000 | 973 | |
---|
1001 | | - sig->cputimer.checking_timer = false; |
---|
| 974 | + pct->expiry_active = false; |
---|
1002 | 975 | } |
---|
1003 | 976 | |
---|
1004 | 977 | /* |
---|
.. | .. |
---|
1007 | 980 | */ |
---|
1008 | 981 | static void posix_cpu_timer_rearm(struct k_itimer *timer) |
---|
1009 | 982 | { |
---|
1010 | | - struct task_struct *p = timer->it.cpu.task; |
---|
| 983 | + clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock); |
---|
| 984 | + struct task_struct *p; |
---|
1011 | 985 | struct sighand_struct *sighand; |
---|
1012 | 986 | unsigned long flags; |
---|
1013 | 987 | u64 now; |
---|
1014 | 988 | |
---|
1015 | | - if (WARN_ON_ONCE(!p)) |
---|
1016 | | - return; |
---|
| 989 | + rcu_read_lock(); |
---|
| 990 | + p = cpu_timer_task_rcu(timer); |
---|
| 991 | + if (!p) |
---|
| 992 | + goto out; |
---|
| 993 | + |
---|
| 994 | + /* Protect timer list r/w in arm_timer() */ |
---|
| 995 | + sighand = lock_task_sighand(p, &flags); |
---|
| 996 | + if (unlikely(sighand == NULL)) |
---|
| 997 | + goto out; |
---|
1017 | 998 | |
---|
1018 | 999 | /* |
---|
1019 | 1000 | * Fetch the current sample and update the timer's expiry time. |
---|
1020 | 1001 | */ |
---|
1021 | | - if (CPUCLOCK_PERTHREAD(timer->it_clock)) { |
---|
1022 | | - cpu_clock_sample(timer->it_clock, p, &now); |
---|
1023 | | - bump_cpu_timer(timer, now); |
---|
1024 | | - if (unlikely(p->exit_state)) |
---|
1025 | | - return; |
---|
| 1002 | + if (CPUCLOCK_PERTHREAD(timer->it_clock)) |
---|
| 1003 | + now = cpu_clock_sample(clkid, p); |
---|
| 1004 | + else |
---|
| 1005 | + now = cpu_clock_sample_group(clkid, p, true); |
---|
1026 | 1006 | |
---|
1027 | | - /* Protect timer list r/w in arm_timer() */ |
---|
1028 | | - sighand = lock_task_sighand(p, &flags); |
---|
1029 | | - if (!sighand) |
---|
1030 | | - return; |
---|
1031 | | - } else { |
---|
1032 | | - /* |
---|
1033 | | - * Protect arm_timer() and timer sampling in case of call to |
---|
1034 | | - * thread_group_cputime(). |
---|
1035 | | - */ |
---|
1036 | | - sighand = lock_task_sighand(p, &flags); |
---|
1037 | | - if (unlikely(sighand == NULL)) { |
---|
1038 | | - /* |
---|
1039 | | - * The process has been reaped. |
---|
1040 | | - * We can't even collect a sample any more. |
---|
1041 | | - */ |
---|
1042 | | - timer->it.cpu.expires = 0; |
---|
1043 | | - return; |
---|
1044 | | - } else if (unlikely(p->exit_state) && thread_group_empty(p)) { |
---|
1045 | | - /* If the process is dying, no need to rearm */ |
---|
1046 | | - goto unlock; |
---|
1047 | | - } |
---|
1048 | | - cpu_timer_sample_group(timer->it_clock, p, &now); |
---|
1049 | | - bump_cpu_timer(timer, now); |
---|
1050 | | - /* Leave the sighand locked for the call below. */ |
---|
1051 | | - } |
---|
| 1007 | + bump_cpu_timer(timer, now); |
---|
1052 | 1008 | |
---|
1053 | 1009 | /* |
---|
1054 | 1010 | * Now re-arm for the new expiry time. |
---|
1055 | 1011 | */ |
---|
1056 | | - arm_timer(timer); |
---|
1057 | | -unlock: |
---|
| 1012 | + arm_timer(timer, p); |
---|
1058 | 1013 | unlock_task_sighand(p, &flags); |
---|
| 1014 | +out: |
---|
| 1015 | + rcu_read_unlock(); |
---|
1059 | 1016 | } |
---|
1060 | 1017 | |
---|
1061 | 1018 | /** |
---|
1062 | | - * task_cputime_expired - Compare two task_cputime entities. |
---|
| 1019 | + * task_cputimers_expired - Check whether posix CPU timers are expired |
---|
1063 | 1020 | * |
---|
1064 | | - * @sample: The task_cputime structure to be checked for expiration. |
---|
1065 | | - * @expires: Expiration times, against which @sample will be checked. |
---|
| 1021 | + * @samples: Array of current samples for the CPUCLOCK clocks |
---|
| 1022 | + * @pct: Pointer to a posix_cputimers container |
---|
1066 | 1023 | * |
---|
1067 | | - * Checks @sample against @expires to see if any field of @sample has expired. |
---|
1068 | | - * Returns true if any field of the former is greater than the corresponding |
---|
1069 | | - * field of the latter if the latter field is set. Otherwise returns false. |
---|
| 1024 | + * Returns true if any member of @samples is greater than the corresponding |
---|
| 1025 | + * member of @pct->bases[CLK].nextevt. False otherwise |
---|
1070 | 1026 | */ |
---|
1071 | | -static inline int task_cputime_expired(const struct task_cputime *sample, |
---|
1072 | | - const struct task_cputime *expires) |
---|
| 1027 | +static inline bool |
---|
| 1028 | +task_cputimers_expired(const u64 *samples, struct posix_cputimers *pct) |
---|
1073 | 1029 | { |
---|
1074 | | - if (expires->utime && sample->utime >= expires->utime) |
---|
1075 | | - return 1; |
---|
1076 | | - if (expires->stime && sample->utime + sample->stime >= expires->stime) |
---|
1077 | | - return 1; |
---|
1078 | | - if (expires->sum_exec_runtime != 0 && |
---|
1079 | | - sample->sum_exec_runtime >= expires->sum_exec_runtime) |
---|
1080 | | - return 1; |
---|
1081 | | - return 0; |
---|
| 1030 | + int i; |
---|
| 1031 | + |
---|
| 1032 | + for (i = 0; i < CPUCLOCK_MAX; i++) { |
---|
| 1033 | + if (samples[i] >= pct->bases[i].nextevt) |
---|
| 1034 | + return true; |
---|
| 1035 | + } |
---|
| 1036 | + return false; |
---|
1082 | 1037 | } |
---|
1083 | 1038 | |
---|
1084 | 1039 | /** |
---|
.. | .. |
---|
1091 | 1046 | * timers and compare them with the corresponding expiration times. Return |
---|
1092 | 1047 | * true if a timer has expired, else return false. |
---|
1093 | 1048 | */ |
---|
1094 | | -static inline int fastpath_timer_check(struct task_struct *tsk) |
---|
| 1049 | +static inline bool fastpath_timer_check(struct task_struct *tsk) |
---|
1095 | 1050 | { |
---|
| 1051 | + struct posix_cputimers *pct = &tsk->posix_cputimers; |
---|
1096 | 1052 | struct signal_struct *sig; |
---|
1097 | 1053 | |
---|
1098 | | - if (!task_cputime_zero(&tsk->cputime_expires)) { |
---|
1099 | | - struct task_cputime task_sample; |
---|
| 1054 | + if (!expiry_cache_is_inactive(pct)) { |
---|
| 1055 | + u64 samples[CPUCLOCK_MAX]; |
---|
1100 | 1056 | |
---|
1101 | | - task_cputime(tsk, &task_sample.utime, &task_sample.stime); |
---|
1102 | | - task_sample.sum_exec_runtime = tsk->se.sum_exec_runtime; |
---|
1103 | | - if (task_cputime_expired(&task_sample, &tsk->cputime_expires)) |
---|
1104 | | - return 1; |
---|
| 1057 | + task_sample_cputime(tsk, samples); |
---|
| 1058 | + if (task_cputimers_expired(samples, pct)) |
---|
| 1059 | + return true; |
---|
1105 | 1060 | } |
---|
1106 | 1061 | |
---|
1107 | 1062 | sig = tsk->signal; |
---|
| 1063 | + pct = &sig->posix_cputimers; |
---|
1108 | 1064 | /* |
---|
1109 | | - * Check if thread group timers expired when the cputimer is |
---|
1110 | | - * running and no other thread in the group is already checking |
---|
1111 | | - * for thread group cputimers. These fields are read without the |
---|
1112 | | - * sighand lock. However, this is fine because this is meant to |
---|
1113 | | - * be a fastpath heuristic to determine whether we should try to |
---|
1114 | | - * acquire the sighand lock to check/handle timers. |
---|
| 1065 | + * Check if thread group timers expired when timers are active and |
---|
| 1066 | + * no other thread in the group is already handling expiry for |
---|
| 1067 | + * thread group cputimers. These fields are read without the |
---|
| 1068 | + * sighand lock. However, this is fine because this is meant to be |
---|
| 1069 | + * a fastpath heuristic to determine whether we should try to |
---|
| 1070 | + * acquire the sighand lock to handle timer expiry. |
---|
1115 | 1071 | * |
---|
1116 | | - * In the worst case scenario, if 'running' or 'checking_timer' gets |
---|
1117 | | - * set but the current thread doesn't see the change yet, we'll wait |
---|
1118 | | - * until the next thread in the group gets a scheduler interrupt to |
---|
1119 | | - * handle the timer. This isn't an issue in practice because these |
---|
1120 | | - * types of delays with signals actually getting sent are expected. |
---|
| 1072 | + * In the worst case scenario, if concurrently timers_active is set |
---|
| 1073 | + * or expiry_active is cleared, but the current thread doesn't see |
---|
| 1074 | + * the change yet, the timer checks are delayed until the next |
---|
| 1075 | + * thread in the group gets a scheduler interrupt to handle the |
---|
| 1076 | + * timer. This isn't an issue in practice because these types of |
---|
| 1077 | + * delays with signals actually getting sent are expected. |
---|
1121 | 1078 | */ |
---|
1122 | | - if (READ_ONCE(sig->cputimer.running) && |
---|
1123 | | - !READ_ONCE(sig->cputimer.checking_timer)) { |
---|
1124 | | - struct task_cputime group_sample; |
---|
| 1079 | + if (READ_ONCE(pct->timers_active) && !READ_ONCE(pct->expiry_active)) { |
---|
| 1080 | + u64 samples[CPUCLOCK_MAX]; |
---|
1125 | 1081 | |
---|
1126 | | - sample_cputime_atomic(&group_sample, &sig->cputimer.cputime_atomic); |
---|
| 1082 | + proc_sample_cputime_atomic(&sig->cputimer.cputime_atomic, |
---|
| 1083 | + samples); |
---|
1127 | 1084 | |
---|
1128 | | - if (task_cputime_expired(&group_sample, &sig->cputime_expires)) |
---|
1129 | | - return 1; |
---|
| 1085 | + if (task_cputimers_expired(samples, pct)) |
---|
| 1086 | + return true; |
---|
1130 | 1087 | } |
---|
1131 | 1088 | |
---|
1132 | 1089 | if (dl_task(tsk) && tsk->dl.dl_overrun) |
---|
1133 | | - return 1; |
---|
| 1090 | + return true; |
---|
1134 | 1091 | |
---|
1135 | | - return 0; |
---|
| 1092 | + return false; |
---|
| 1093 | +} |
---|
| 1094 | + |
---|
| 1095 | +static void handle_posix_cpu_timers(struct task_struct *tsk); |
---|
| 1096 | + |
---|
| 1097 | +#ifdef CONFIG_POSIX_CPU_TIMERS_TASK_WORK |
---|
| 1098 | +static void posix_cpu_timers_work(struct callback_head *work) |
---|
| 1099 | +{ |
---|
| 1100 | + handle_posix_cpu_timers(current); |
---|
1136 | 1101 | } |
---|
1137 | 1102 | |
---|
1138 | 1103 | /* |
---|
1139 | | - * This is called from the timer interrupt handler. The irq handler has |
---|
1140 | | - * already updated our counts. We need to check if any timers fire now. |
---|
1141 | | - * Interrupts are disabled. |
---|
| 1104 | + * Clear existing posix CPU timers task work. |
---|
1142 | 1105 | */ |
---|
1143 | | -void run_posix_cpu_timers(struct task_struct *tsk) |
---|
| 1106 | +void clear_posix_cputimers_work(struct task_struct *p) |
---|
1144 | 1107 | { |
---|
1145 | | - LIST_HEAD(firing); |
---|
1146 | | - struct k_itimer *timer, *next; |
---|
1147 | | - unsigned long flags; |
---|
| 1108 | + /* |
---|
| 1109 | + * A copied work entry from the old task is not meaningful, clear it. |
---|
| 1110 | + * N.B. init_task_work will not do this. |
---|
| 1111 | + */ |
---|
| 1112 | + memset(&p->posix_cputimers_work.work, 0, |
---|
| 1113 | + sizeof(p->posix_cputimers_work.work)); |
---|
| 1114 | + init_task_work(&p->posix_cputimers_work.work, |
---|
| 1115 | + posix_cpu_timers_work); |
---|
| 1116 | + p->posix_cputimers_work.scheduled = false; |
---|
| 1117 | +} |
---|
1148 | 1118 | |
---|
1149 | | - lockdep_assert_irqs_disabled(); |
---|
| 1119 | +/* |
---|
| 1120 | + * Initialize posix CPU timers task work in init task. Out of line to |
---|
| 1121 | + * keep the callback static and to avoid header recursion hell. |
---|
| 1122 | + */ |
---|
| 1123 | +void __init posix_cputimers_init_work(void) |
---|
| 1124 | +{ |
---|
| 1125 | + clear_posix_cputimers_work(current); |
---|
| 1126 | +} |
---|
| 1127 | + |
---|
| 1128 | +/* |
---|
| 1129 | + * Note: All operations on tsk->posix_cputimer_work.scheduled happen either |
---|
| 1130 | + * in hard interrupt context or in task context with interrupts |
---|
| 1131 | + * disabled. Aside of that the writer/reader interaction is always in the |
---|
| 1132 | + * context of the current task, which means they are strict per CPU. |
---|
| 1133 | + */ |
---|
| 1134 | +static inline bool posix_cpu_timers_work_scheduled(struct task_struct *tsk) |
---|
| 1135 | +{ |
---|
| 1136 | + return tsk->posix_cputimers_work.scheduled; |
---|
| 1137 | +} |
---|
| 1138 | + |
---|
| 1139 | +static inline void __run_posix_cpu_timers(struct task_struct *tsk) |
---|
| 1140 | +{ |
---|
| 1141 | + if (WARN_ON_ONCE(tsk->posix_cputimers_work.scheduled)) |
---|
| 1142 | + return; |
---|
| 1143 | + |
---|
| 1144 | + /* Schedule task work to actually expire the timers */ |
---|
| 1145 | + tsk->posix_cputimers_work.scheduled = true; |
---|
| 1146 | + task_work_add(tsk, &tsk->posix_cputimers_work.work, TWA_RESUME); |
---|
| 1147 | +} |
---|
| 1148 | + |
---|
| 1149 | +static inline bool posix_cpu_timers_enable_work(struct task_struct *tsk, |
---|
| 1150 | + unsigned long start) |
---|
| 1151 | +{ |
---|
| 1152 | + bool ret = true; |
---|
1150 | 1153 | |
---|
1151 | 1154 | /* |
---|
1152 | | - * The fast path checks that there are no expired thread or thread |
---|
1153 | | - * group timers. If that's so, just return. |
---|
| 1155 | + * On !RT kernels interrupts are disabled while collecting expired |
---|
| 1156 | + * timers, so no tick can happen and the fast path check can be |
---|
| 1157 | + * reenabled without further checks. |
---|
1154 | 1158 | */ |
---|
1155 | | - if (!fastpath_timer_check(tsk)) |
---|
1156 | | - return; |
---|
| 1159 | + if (!IS_ENABLED(CONFIG_PREEMPT_RT)) { |
---|
| 1160 | + tsk->posix_cputimers_work.scheduled = false; |
---|
| 1161 | + return true; |
---|
| 1162 | + } |
---|
| 1163 | + |
---|
| 1164 | + /* |
---|
| 1165 | + * On RT enabled kernels ticks can happen while the expired timers |
---|
| 1166 | + * are collected under sighand lock. But any tick which observes |
---|
| 1167 | + * the CPUTIMERS_WORK_SCHEDULED bit set, does not run the fastpath |
---|
| 1168 | + * checks. So reenabling the tick work has do be done carefully: |
---|
| 1169 | + * |
---|
| 1170 | + * Disable interrupts and run the fast path check if jiffies have |
---|
| 1171 | + * advanced since the collecting of expired timers started. If |
---|
| 1172 | + * jiffies have not advanced or the fast path check did not find |
---|
| 1173 | + * newly expired timers, reenable the fast path check in the timer |
---|
| 1174 | + * interrupt. If there are newly expired timers, return false and |
---|
| 1175 | + * let the collection loop repeat. |
---|
| 1176 | + */ |
---|
| 1177 | + local_irq_disable(); |
---|
| 1178 | + if (start != jiffies && fastpath_timer_check(tsk)) |
---|
| 1179 | + ret = false; |
---|
| 1180 | + else |
---|
| 1181 | + tsk->posix_cputimers_work.scheduled = false; |
---|
| 1182 | + local_irq_enable(); |
---|
| 1183 | + |
---|
| 1184 | + return ret; |
---|
| 1185 | +} |
---|
| 1186 | +#else /* CONFIG_POSIX_CPU_TIMERS_TASK_WORK */ |
---|
| 1187 | +static inline void __run_posix_cpu_timers(struct task_struct *tsk) |
---|
| 1188 | +{ |
---|
| 1189 | + lockdep_posixtimer_enter(); |
---|
| 1190 | + handle_posix_cpu_timers(tsk); |
---|
| 1191 | + lockdep_posixtimer_exit(); |
---|
| 1192 | +} |
---|
| 1193 | + |
---|
| 1194 | +static inline bool posix_cpu_timers_work_scheduled(struct task_struct *tsk) |
---|
| 1195 | +{ |
---|
| 1196 | + return false; |
---|
| 1197 | +} |
---|
| 1198 | + |
---|
| 1199 | +static inline bool posix_cpu_timers_enable_work(struct task_struct *tsk, |
---|
| 1200 | + unsigned long start) |
---|
| 1201 | +{ |
---|
| 1202 | + return true; |
---|
| 1203 | +} |
---|
| 1204 | +#endif /* CONFIG_POSIX_CPU_TIMERS_TASK_WORK */ |
---|
| 1205 | + |
---|
| 1206 | +static void handle_posix_cpu_timers(struct task_struct *tsk) |
---|
| 1207 | +{ |
---|
| 1208 | + struct k_itimer *timer, *next; |
---|
| 1209 | + unsigned long flags, start; |
---|
| 1210 | + LIST_HEAD(firing); |
---|
1157 | 1211 | |
---|
1158 | 1212 | if (!lock_task_sighand(tsk, &flags)) |
---|
1159 | 1213 | return; |
---|
1160 | | - /* |
---|
1161 | | - * Here we take off tsk->signal->cpu_timers[N] and |
---|
1162 | | - * tsk->cpu_timers[N] all the timers that are firing, and |
---|
1163 | | - * put them on the firing list. |
---|
1164 | | - */ |
---|
1165 | | - check_thread_timers(tsk, &firing); |
---|
1166 | 1214 | |
---|
1167 | | - check_process_timers(tsk, &firing); |
---|
| 1215 | + do { |
---|
| 1216 | + /* |
---|
| 1217 | + * On RT locking sighand lock does not disable interrupts, |
---|
| 1218 | + * so this needs to be careful vs. ticks. Store the current |
---|
| 1219 | + * jiffies value. |
---|
| 1220 | + */ |
---|
| 1221 | + start = READ_ONCE(jiffies); |
---|
| 1222 | + barrier(); |
---|
| 1223 | + |
---|
| 1224 | + /* |
---|
| 1225 | + * Here we take off tsk->signal->cpu_timers[N] and |
---|
| 1226 | + * tsk->cpu_timers[N] all the timers that are firing, and |
---|
| 1227 | + * put them on the firing list. |
---|
| 1228 | + */ |
---|
| 1229 | + check_thread_timers(tsk, &firing); |
---|
| 1230 | + |
---|
| 1231 | + check_process_timers(tsk, &firing); |
---|
| 1232 | + |
---|
| 1233 | + /* |
---|
| 1234 | + * The above timer checks have updated the exipry cache and |
---|
| 1235 | + * because nothing can have queued or modified timers after |
---|
| 1236 | + * sighand lock was taken above it is guaranteed to be |
---|
| 1237 | + * consistent. So the next timer interrupt fastpath check |
---|
| 1238 | + * will find valid data. |
---|
| 1239 | + * |
---|
| 1240 | + * If timer expiry runs in the timer interrupt context then |
---|
| 1241 | + * the loop is not relevant as timers will be directly |
---|
| 1242 | + * expired in interrupt context. The stub function below |
---|
| 1243 | + * returns always true which allows the compiler to |
---|
| 1244 | + * optimize the loop out. |
---|
| 1245 | + * |
---|
| 1246 | + * If timer expiry is deferred to task work context then |
---|
| 1247 | + * the following rules apply: |
---|
| 1248 | + * |
---|
| 1249 | + * - On !RT kernels no tick can have happened on this CPU |
---|
| 1250 | + * after sighand lock was acquired because interrupts are |
---|
| 1251 | + * disabled. So reenabling task work before dropping |
---|
| 1252 | + * sighand lock and reenabling interrupts is race free. |
---|
| 1253 | + * |
---|
| 1254 | + * - On RT kernels ticks might have happened but the tick |
---|
| 1255 | + * work ignored posix CPU timer handling because the |
---|
| 1256 | + * CPUTIMERS_WORK_SCHEDULED bit is set. Reenabling work |
---|
| 1257 | + * must be done very carefully including a check whether |
---|
| 1258 | + * ticks have happened since the start of the timer |
---|
| 1259 | + * expiry checks. posix_cpu_timers_enable_work() takes |
---|
| 1260 | + * care of that and eventually lets the expiry checks |
---|
| 1261 | + * run again. |
---|
| 1262 | + */ |
---|
| 1263 | + } while (!posix_cpu_timers_enable_work(tsk, start)); |
---|
1168 | 1264 | |
---|
1169 | 1265 | /* |
---|
1170 | | - * We must release these locks before taking any timer's lock. |
---|
| 1266 | + * We must release sighand lock before taking any timer's lock. |
---|
1171 | 1267 | * There is a potential race with timer deletion here, as the |
---|
1172 | 1268 | * siglock now protects our private firing list. We have set |
---|
1173 | 1269 | * the firing flag in each timer, so that a deletion attempt |
---|
.. | .. |
---|
1182 | 1278 | * each timer's lock before clearing its firing flag, so no |
---|
1183 | 1279 | * timer call will interfere. |
---|
1184 | 1280 | */ |
---|
1185 | | - list_for_each_entry_safe(timer, next, &firing, it.cpu.entry) { |
---|
| 1281 | + list_for_each_entry_safe(timer, next, &firing, it.cpu.elist) { |
---|
1186 | 1282 | int cpu_firing; |
---|
1187 | 1283 | |
---|
| 1284 | + /* |
---|
| 1285 | + * spin_lock() is sufficient here even independent of the |
---|
| 1286 | + * expiry context. If expiry happens in hard interrupt |
---|
| 1287 | + * context it's obvious. For task work context it's safe |
---|
| 1288 | + * because all other operations on timer::it_lock happen in |
---|
| 1289 | + * task context (syscall or exit). |
---|
| 1290 | + */ |
---|
1188 | 1291 | spin_lock(&timer->it_lock); |
---|
1189 | | - list_del_init(&timer->it.cpu.entry); |
---|
| 1292 | + list_del_init(&timer->it.cpu.elist); |
---|
1190 | 1293 | cpu_firing = timer->it.cpu.firing; |
---|
1191 | 1294 | timer->it.cpu.firing = 0; |
---|
1192 | 1295 | /* |
---|
.. | .. |
---|
1201 | 1304 | } |
---|
1202 | 1305 | |
---|
1203 | 1306 | /* |
---|
| 1307 | + * This is called from the timer interrupt handler. The irq handler has |
---|
| 1308 | + * already updated our counts. We need to check if any timers fire now. |
---|
| 1309 | + * Interrupts are disabled. |
---|
| 1310 | + */ |
---|
| 1311 | +void run_posix_cpu_timers(void) |
---|
| 1312 | +{ |
---|
| 1313 | + struct task_struct *tsk = current; |
---|
| 1314 | + |
---|
| 1315 | + lockdep_assert_irqs_disabled(); |
---|
| 1316 | + |
---|
| 1317 | + /* |
---|
| 1318 | + * If the actual expiry is deferred to task work context and the |
---|
| 1319 | + * work is already scheduled there is no point to do anything here. |
---|
| 1320 | + */ |
---|
| 1321 | + if (posix_cpu_timers_work_scheduled(tsk)) |
---|
| 1322 | + return; |
---|
| 1323 | + |
---|
| 1324 | + /* |
---|
| 1325 | + * The fast path checks that there are no expired thread or thread |
---|
| 1326 | + * group timers. If that's so, just return. |
---|
| 1327 | + */ |
---|
| 1328 | + if (!fastpath_timer_check(tsk)) |
---|
| 1329 | + return; |
---|
| 1330 | + |
---|
| 1331 | + __run_posix_cpu_timers(tsk); |
---|
| 1332 | +} |
---|
| 1333 | + |
---|
| 1334 | +/* |
---|
1204 | 1335 | * Set one of the process-wide special case CPU timers or RLIMIT_CPU. |
---|
1205 | 1336 | * The tsk->sighand->siglock must be held by the caller. |
---|
1206 | 1337 | */ |
---|
1207 | | -void set_process_cpu_timer(struct task_struct *tsk, unsigned int clock_idx, |
---|
| 1338 | +void set_process_cpu_timer(struct task_struct *tsk, unsigned int clkid, |
---|
1208 | 1339 | u64 *newval, u64 *oldval) |
---|
1209 | 1340 | { |
---|
1210 | | - u64 now; |
---|
1211 | | - int ret; |
---|
| 1341 | + u64 now, *nextevt; |
---|
1212 | 1342 | |
---|
1213 | | - if (WARN_ON_ONCE(clock_idx >= CPUCLOCK_SCHED)) |
---|
| 1343 | + if (WARN_ON_ONCE(clkid >= CPUCLOCK_SCHED)) |
---|
1214 | 1344 | return; |
---|
1215 | 1345 | |
---|
1216 | | - ret = cpu_timer_sample_group(clock_idx, tsk, &now); |
---|
| 1346 | + nextevt = &tsk->signal->posix_cputimers.bases[clkid].nextevt; |
---|
| 1347 | + now = cpu_clock_sample_group(clkid, tsk, true); |
---|
1217 | 1348 | |
---|
1218 | | - if (oldval && ret != -EINVAL) { |
---|
| 1349 | + if (oldval) { |
---|
1219 | 1350 | /* |
---|
1220 | 1351 | * We are setting itimer. The *oldval is absolute and we update |
---|
1221 | 1352 | * it to be relative, *newval argument is relative and we update |
---|
.. | .. |
---|
1236 | 1367 | } |
---|
1237 | 1368 | |
---|
1238 | 1369 | /* |
---|
1239 | | - * Update expiration cache if we are the earliest timer, or eventually |
---|
1240 | | - * RLIMIT_CPU limit is earlier than prof_exp cpu timer expire. |
---|
| 1370 | + * Update expiration cache if this is the earliest timer. CPUCLOCK_PROF |
---|
| 1371 | + * expiry cache is also used by RLIMIT_CPU!. |
---|
1241 | 1372 | */ |
---|
1242 | | - switch (clock_idx) { |
---|
1243 | | - case CPUCLOCK_PROF: |
---|
1244 | | - if (expires_gt(tsk->signal->cputime_expires.prof_exp, *newval)) |
---|
1245 | | - tsk->signal->cputime_expires.prof_exp = *newval; |
---|
1246 | | - break; |
---|
1247 | | - case CPUCLOCK_VIRT: |
---|
1248 | | - if (expires_gt(tsk->signal->cputime_expires.virt_exp, *newval)) |
---|
1249 | | - tsk->signal->cputime_expires.virt_exp = *newval; |
---|
1250 | | - break; |
---|
1251 | | - } |
---|
| 1373 | + if (*newval < *nextevt) |
---|
| 1374 | + *nextevt = *newval; |
---|
1252 | 1375 | |
---|
1253 | 1376 | tick_dep_set_signal(tsk->signal, TICK_DEP_BIT_POSIX_TIMER); |
---|
1254 | 1377 | } |
---|
.. | .. |
---|
1270 | 1393 | timer.it_overrun = -1; |
---|
1271 | 1394 | error = posix_cpu_timer_create(&timer); |
---|
1272 | 1395 | timer.it_process = current; |
---|
| 1396 | + |
---|
1273 | 1397 | if (!error) { |
---|
1274 | 1398 | static struct itimerspec64 zero_it; |
---|
1275 | 1399 | struct restart_block *restart; |
---|
.. | .. |
---|
1285 | 1409 | } |
---|
1286 | 1410 | |
---|
1287 | 1411 | while (!signal_pending(current)) { |
---|
1288 | | - if (timer.it.cpu.expires == 0) { |
---|
| 1412 | + if (!cpu_timer_getexpires(&timer.it.cpu)) { |
---|
1289 | 1413 | /* |
---|
1290 | 1414 | * Our timer fired and was reset, below |
---|
1291 | 1415 | * deletion can not fail. |
---|
.. | .. |
---|
1307 | 1431 | /* |
---|
1308 | 1432 | * We were interrupted by a signal. |
---|
1309 | 1433 | */ |
---|
1310 | | - expires = timer.it.cpu.expires; |
---|
| 1434 | + expires = cpu_timer_getexpires(&timer.it.cpu); |
---|
1311 | 1435 | error = posix_cpu_timer_set(&timer, 0, &zero_it, &it); |
---|
1312 | 1436 | if (!error) { |
---|
1313 | 1437 | /* |
---|
.. | .. |
---|
1427 | 1551 | } |
---|
1428 | 1552 | |
---|
1429 | 1553 | const struct k_clock clock_posix_cpu = { |
---|
1430 | | - .clock_getres = posix_cpu_clock_getres, |
---|
1431 | | - .clock_set = posix_cpu_clock_set, |
---|
1432 | | - .clock_get = posix_cpu_clock_get, |
---|
1433 | | - .timer_create = posix_cpu_timer_create, |
---|
1434 | | - .nsleep = posix_cpu_nsleep, |
---|
1435 | | - .timer_set = posix_cpu_timer_set, |
---|
1436 | | - .timer_del = posix_cpu_timer_del, |
---|
1437 | | - .timer_get = posix_cpu_timer_get, |
---|
1438 | | - .timer_rearm = posix_cpu_timer_rearm, |
---|
| 1554 | + .clock_getres = posix_cpu_clock_getres, |
---|
| 1555 | + .clock_set = posix_cpu_clock_set, |
---|
| 1556 | + .clock_get_timespec = posix_cpu_clock_get, |
---|
| 1557 | + .timer_create = posix_cpu_timer_create, |
---|
| 1558 | + .nsleep = posix_cpu_nsleep, |
---|
| 1559 | + .timer_set = posix_cpu_timer_set, |
---|
| 1560 | + .timer_del = posix_cpu_timer_del, |
---|
| 1561 | + .timer_get = posix_cpu_timer_get, |
---|
| 1562 | + .timer_rearm = posix_cpu_timer_rearm, |
---|
1439 | 1563 | }; |
---|
1440 | 1564 | |
---|
1441 | 1565 | const struct k_clock clock_process = { |
---|
1442 | | - .clock_getres = process_cpu_clock_getres, |
---|
1443 | | - .clock_get = process_cpu_clock_get, |
---|
1444 | | - .timer_create = process_cpu_timer_create, |
---|
1445 | | - .nsleep = process_cpu_nsleep, |
---|
| 1566 | + .clock_getres = process_cpu_clock_getres, |
---|
| 1567 | + .clock_get_timespec = process_cpu_clock_get, |
---|
| 1568 | + .timer_create = process_cpu_timer_create, |
---|
| 1569 | + .nsleep = process_cpu_nsleep, |
---|
1446 | 1570 | }; |
---|
1447 | 1571 | |
---|
1448 | 1572 | const struct k_clock clock_thread = { |
---|
1449 | | - .clock_getres = thread_cpu_clock_getres, |
---|
1450 | | - .clock_get = thread_cpu_clock_get, |
---|
1451 | | - .timer_create = thread_cpu_timer_create, |
---|
| 1573 | + .clock_getres = thread_cpu_clock_getres, |
---|
| 1574 | + .clock_get_timespec = thread_cpu_clock_get, |
---|
| 1575 | + .timer_create = thread_cpu_timer_create, |
---|
1452 | 1576 | }; |
---|