@@ -23,8 +23,10 @@ static void posix_cpu_timer_rearm(struct k_itimer *timer);
2323void posix_cputimers_group_init (struct posix_cputimers * pct , u64 cpu_limit )
2424{
2525 posix_cputimers_init (pct );
26- if (cpu_limit != RLIM_INFINITY )
26+ if (cpu_limit != RLIM_INFINITY ) {
2727 pct -> bases [CPUCLOCK_PROF ].nextevt = cpu_limit * NSEC_PER_SEC ;
28+ pct -> timers_active = true;
29+ }
2830}
2931
3032/*
@@ -248,8 +250,9 @@ static void update_gt_cputime(struct task_cputime_atomic *cputime_atomic,
248250void thread_group_sample_cputime (struct task_struct * tsk , u64 * samples )
249251{
250252 struct thread_group_cputimer * cputimer = & tsk -> signal -> cputimer ;
253+ struct posix_cputimers * pct = & tsk -> signal -> posix_cputimers ;
251254
252- WARN_ON_ONCE (!cputimer -> running );
255+ WARN_ON_ONCE (!pct -> timers_active );
253256
254257 proc_sample_cputime_atomic (& cputimer -> cputime_atomic , samples );
255258}
@@ -269,9 +272,10 @@ void thread_group_sample_cputime(struct task_struct *tsk, u64 *samples)
269272static void thread_group_start_cputime (struct task_struct * tsk , u64 * samples )
270273{
271274 struct thread_group_cputimer * cputimer = & tsk -> signal -> cputimer ;
275+ struct posix_cputimers * pct = & tsk -> signal -> posix_cputimers ;
272276
273277 /* Check if cputimer isn't running. This is accessed without locking. */
274- if (!READ_ONCE (cputimer -> running )) {
278+ if (!READ_ONCE (pct -> timers_active )) {
275279 struct task_cputime sum ;
276280
277281 /*
@@ -283,13 +287,13 @@ static void thread_group_start_cputime(struct task_struct *tsk, u64 *samples)
283287 update_gt_cputime (& cputimer -> cputime_atomic , & sum );
284288
285289 /*
286- * We're setting cputimer->running without a lock. Ensure
287- * this only gets written to in one operation. We set
288- * running after update_gt_cputime() as a small optimization,
289- * but barriers are not required because update_gt_cputime()
290+ * We're setting timers_active without a lock. Ensure this
291+ * only gets written to in one operation. We set it after
292+ * update_gt_cputime() as a small optimization, but
293+ * barriers are not required because update_gt_cputime()
290294 * can handle concurrent updates.
291295 */
292- WRITE_ONCE (cputimer -> running , true);
296+ WRITE_ONCE (pct -> timers_active , true);
293297 }
294298 proc_sample_cputime_atomic (& cputimer -> cputime_atomic , samples );
295299}
@@ -313,9 +317,10 @@ static u64 cpu_clock_sample_group(const clockid_t clkid, struct task_struct *p,
313317 bool start )
314318{
315319 struct thread_group_cputimer * cputimer = & p -> signal -> cputimer ;
320+ struct posix_cputimers * pct = & p -> signal -> posix_cputimers ;
316321 u64 samples [CPUCLOCK_MAX ];
317322
318- if (!READ_ONCE (cputimer -> running )) {
323+ if (!READ_ONCE (pct -> timers_active )) {
319324 if (start )
320325 thread_group_start_cputime (p , samples );
321326 else
@@ -834,10 +839,10 @@ static void check_thread_timers(struct task_struct *tsk,
834839
835840static inline void stop_process_timers (struct signal_struct * sig )
836841{
837- struct thread_group_cputimer * cputimer = & sig -> cputimer ;
842+ struct posix_cputimers * pct = & sig -> posix_cputimers ;
838843
839- /* Turn off cputimer->running . This is done without locking. */
840- WRITE_ONCE (cputimer -> running , false);
844+ /* Turn off the active flag . This is done without locking. */
845+ WRITE_ONCE (pct -> timers_active , false);
841846 tick_dep_clear_signal (sig , TICK_DEP_BIT_POSIX_TIMER );
842847}
843848
@@ -877,17 +882,17 @@ static void check_process_timers(struct task_struct *tsk,
877882 unsigned long soft ;
878883
879884 /*
880- * If cputimer is not running, then there are no active
881- * process wide timers (POSIX 1.b, itimers, RLIMIT_CPU) .
885+ * If there are no active process wide timers (POSIX 1.b, itimers,
886+ * RLIMIT_CPU) nothing to check .
882887 */
883- if (!READ_ONCE (sig -> cputimer . running ))
888+ if (!READ_ONCE (pct -> timers_active ))
884889 return ;
885890
886891 /*
887892 * Signify that a thread is checking for process timers.
888893 * Write access to this field is protected by the sighand lock.
889894 */
890- sig -> cputimer . checking_timer = true;
895+ pct -> timers_active = true;
891896
892897 /*
893898 * Collect the current process totals. Group accounting is active
@@ -933,7 +938,7 @@ static void check_process_timers(struct task_struct *tsk,
933938 if (expiry_cache_is_inactive (pct ))
934939 stop_process_timers (sig );
935940
936- sig -> cputimer . checking_timer = false;
941+ pct -> expiry_active = false;
937942}
938943
939944/*
@@ -1027,39 +1032,41 @@ task_cputimers_expired(const u64 *sample, struct posix_cputimers *pct)
10271032 */
10281033static inline bool fastpath_timer_check (struct task_struct * tsk )
10291034{
1035+ struct posix_cputimers * pct = & tsk -> posix_cputimers ;
10301036 struct signal_struct * sig ;
10311037
1032- if (!expiry_cache_is_inactive (& tsk -> posix_cputimers )) {
1038+ if (!expiry_cache_is_inactive (pct )) {
10331039 u64 samples [CPUCLOCK_MAX ];
10341040
10351041 task_sample_cputime (tsk , samples );
1036- if (task_cputimers_expired (samples , & tsk -> posix_cputimers ))
1042+ if (task_cputimers_expired (samples , pct ))
10371043 return true;
10381044 }
10391045
10401046 sig = tsk -> signal ;
1047+ pct = & sig -> posix_cputimers ;
10411048 /*
1042- * Check if thread group timers expired when the cputimer is
1043- * running and no other thread in the group is already checking
1044- * for thread group cputimers. These fields are read without the
1045- * sighand lock. However, this is fine because this is meant to
1046- * be a fastpath heuristic to determine whether we should try to
1047- * acquire the sighand lock to check/ handle timers .
1049+ * Check if thread group timers expired when timers are active and
1050+ * no other thread in the group is already handling expiry for
1051+ * thread group cputimers. These fields are read without the
1052+ * sighand lock. However, this is fine because this is meant to be
1053+ * a fastpath heuristic to determine whether we should try to
1054+ * acquire the sighand lock to handle timer expiry .
10481055 *
1049- * In the worst case scenario, if 'running' or 'checking_timer' gets
1050- * set but the current thread doesn't see the change yet, we'll wait
1051- * until the next thread in the group gets a scheduler interrupt to
1052- * handle the timer. This isn't an issue in practice because these
1053- * types of delays with signals actually getting sent are expected.
1056+ * In the worst case scenario, if concurrently timers_active is set
1057+ * or expiry_active is cleared, but the current thread doesn't see
1058+ * the change yet, the timer checks are delayed until the next
1059+ * thread in the group gets a scheduler interrupt to handle the
1060+ * timer. This isn't an issue in practice because these types of
1061+ * delays with signals actually getting sent are expected.
10541062 */
1055- if (READ_ONCE (sig -> cputimer .running ) &&
1056- !READ_ONCE (sig -> cputimer .checking_timer )) {
1063+ if (READ_ONCE (pct -> timers_active ) && !READ_ONCE (pct -> expiry_active )) {
10571064 u64 samples [CPUCLOCK_MAX ];
10581065
10591066 proc_sample_cputime_atomic (& sig -> cputimer .cputime_atomic ,
10601067 samples );
10611068
1062- if (task_cputimers_expired (samples , & sig -> posix_cputimers ))
1069+ if (task_cputimers_expired (samples , pct ))
10631070 return true;
10641071 }
10651072
0 commit comments