Skip to content

Commit 2bbdbda

Browse files
committed
posix-cpu-timers: Get rid of zero checks
Deactivation of the expiry cache is done by setting all clock caches to 0. That requires to have a check for zero in all places which update the expiry cache: if (cache == 0 || new < cache) cache = new; Use U64_MAX as the deactivated value, which allows to remove the zero checks when updating the cache and reduces it to the obvious check: if (new < cache) cache = new; This also removes the weird workaround in do_prlimit() which was required to convert a RLIMIT_CPU value of 0 (immediate expiry) to 1 because handing in 0 to the posix CPU timer code would have effectively disarmed it. Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Reviewed-by: Frederic Weisbecker <frederic@kernel.org> Link: https://lkml.kernel.org/r/20190821192922.275086128@linutronix.de
1 parent 24db4dd commit 2bbdbda

3 files changed

Lines changed: 20 additions & 34 deletions

File tree

‎include/linux/posix-timers.h‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ struct posix_cputimers {
8686

8787
static inline void posix_cputimers_init(struct posix_cputimers *pct)
8888
{
89-
memset(pct->bases, 0, sizeof(pct->bases));
89+
pct->bases[0].nextevt = U64_MAX;
90+
pct->bases[1].nextevt = U64_MAX;
91+
pct->bases[2].nextevt = U64_MAX;
9092
INIT_LIST_HEAD(&pct->bases[0].cpu_timers);
9193
INIT_LIST_HEAD(&pct->bases[1].cpu_timers);
9294
INIT_LIST_HEAD(&pct->bases[2].cpu_timers);
@@ -102,7 +104,8 @@ static inline void posix_cputimers_rt_watchdog(struct posix_cputimers *pct,
102104

103105
/* Init task static initializer */
104106
#define INIT_CPU_TIMERBASE(b) { \
105-
.cpu_timers = LIST_HEAD_INIT(b.cpu_timers), \
107+
.nextevt = U64_MAX, \
108+
.cpu_timers = LIST_HEAD_INIT(b.cpu_timers), \
106109
}
107110

108111
#define INIT_CPU_TIMERBASES(b) { \

‎kernel/sys.c‎

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1557,15 +1557,6 @@ int do_prlimit(struct task_struct *tsk, unsigned int resource,
15571557
retval = -EPERM;
15581558
if (!retval)
15591559
retval = security_task_setrlimit(tsk, resource, new_rlim);
1560-
if (resource == RLIMIT_CPU && new_rlim->rlim_cur == 0) {
1561-
/*
1562-
* The caller is asking for an immediate RLIMIT_CPU
1563-
* expiry. But we use the zero value to mean "it was
1564-
* never set". So let's cheat and make it one second
1565-
* instead
1566-
*/
1567-
new_rlim->rlim_cur = 1;
1568-
}
15691560
}
15701561
if (!retval) {
15711562
if (old_rlim)

‎kernel/time/posix-cpu-timers.c‎

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,12 @@ static void bump_cpu_timer(struct k_itimer *timer, u64 now)
122122
}
123123
}
124124

125-
static inline bool expiry_cache_is_zero(const struct posix_cputimers *pct)
125+
/* Check whether all cache entries contain U64_MAX, i.e. eternal expiry time */
126+
static inline bool expiry_cache_is_inactive(const struct posix_cputimers *pct)
126127
{
127-
return !(pct->bases[CPUCLOCK_PROF].nextevt |
128-
pct->bases[CPUCLOCK_VIRT].nextevt |
129-
pct->bases[CPUCLOCK_SCHED].nextevt);
128+
return !(~pct->bases[CPUCLOCK_PROF].nextevt |
129+
~pct->bases[CPUCLOCK_VIRT].nextevt |
130+
~pct->bases[CPUCLOCK_SCHED].nextevt);
130131
}
131132

132133
static int
@@ -442,11 +443,6 @@ void posix_cpu_timers_exit_group(struct task_struct *tsk)
442443
cleanup_timers(&tsk->signal->posix_cputimers);
443444
}
444445

445-
static inline int expires_gt(u64 expires, u64 new_exp)
446-
{
447-
return expires == 0 || expires > new_exp;
448-
}
449-
450446
/*
451447
* Insert the timer on the appropriate list before any timers that
452448
* expire later. This must be called with the sighand lock held.
@@ -483,7 +479,7 @@ static void arm_timer(struct k_itimer *timer)
483479
* for process timers we share expiration cache with itimers
484480
* and RLIMIT_CPU and for thread timers with RLIMIT_RTTIME.
485481
*/
486-
if (expires_gt(base->nextevt, newexp))
482+
if (newexp < base->nextevt)
487483
base->nextevt = newexp;
488484

489485
if (CPUCLOCK_PERTHREAD(timer->it_clock))
@@ -753,7 +749,7 @@ check_timers_list(struct list_head *timers,
753749
list_move_tail(&t->entry, firing);
754750
}
755751

756-
return 0;
752+
return U64_MAX;
757753
}
758754

759755
static inline void check_dl_overrun(struct task_struct *tsk)
@@ -779,11 +775,7 @@ static void check_thread_timers(struct task_struct *tsk,
779775
if (dl_task(tsk))
780776
check_dl_overrun(tsk);
781777

782-
/*
783-
* If the expiry cache is zero, then there are no active per thread
784-
* CPU timers.
785-
*/
786-
if (expiry_cache_is_zero(&tsk->posix_cputimers))
778+
if (expiry_cache_is_inactive(&tsk->posix_cputimers))
787779
return;
788780

789781
task_cputime(tsk, &utime, &stime);
@@ -833,7 +825,7 @@ static void check_thread_timers(struct task_struct *tsk,
833825
}
834826
}
835827

836-
if (expiry_cache_is_zero(&tsk->posix_cputimers))
828+
if (expiry_cache_is_inactive(&tsk->posix_cputimers))
837829
tick_dep_clear_task(tsk, TICK_DEP_BIT_POSIX_TIMER);
838830
}
839831

@@ -864,7 +856,7 @@ static void check_cpu_itimer(struct task_struct *tsk, struct cpu_itimer *it,
864856
__group_send_sig_info(signo, SEND_SIG_PRIV, tsk);
865857
}
866858

867-
if (it->expires && (!*expires || it->expires < *expires))
859+
if (it->expires && it->expires < *expires)
868860
*expires = it->expires;
869861
}
870862

@@ -948,15 +940,15 @@ static void check_process_timers(struct task_struct *tsk,
948940
}
949941
}
950942
softns = soft * NSEC_PER_SEC;
951-
if (!prof_exp || softns < prof_exp)
943+
if (softns < prof_exp)
952944
prof_exp = softns;
953945
}
954946

955947
base[CPUCLOCK_PROF].nextevt = prof_exp;
956948
base[CPUCLOCK_VIRT].nextevt = virt_exp;
957949
base[CPUCLOCK_SCHED].nextevt = sched_exp;
958950

959-
if (expiry_cache_is_zero(&sig->posix_cputimers))
951+
if (expiry_cache_is_inactive(&sig->posix_cputimers))
960952
stop_process_timers(sig);
961953

962954
sig->cputimer.checking_timer = false;
@@ -1035,7 +1027,7 @@ task_cputimers_expired(const u64 *sample, struct posix_cputimers *pct)
10351027
int i;
10361028

10371029
for (i = 0; i < CPUCLOCK_MAX; i++) {
1038-
if (pct->bases[i].nextevt && sample[i] >= pct->bases[i].nextevt)
1030+
if (sample[i] >= pct->bases[i].nextevt)
10391031
return true;
10401032
}
10411033
return false;
@@ -1055,7 +1047,7 @@ static inline bool fastpath_timer_check(struct task_struct *tsk)
10551047
{
10561048
struct signal_struct *sig;
10571049

1058-
if (!expiry_cache_is_zero(&tsk->posix_cputimers)) {
1050+
if (!expiry_cache_is_inactive(&tsk->posix_cputimers)) {
10591051
u64 samples[CPUCLOCK_MAX];
10601052

10611053
task_sample_cputime(tsk, samples);
@@ -1200,7 +1192,7 @@ void set_process_cpu_timer(struct task_struct *tsk, unsigned int clkid,
12001192
* Update expiration cache if this is the earliest timer. CPUCLOCK_PROF
12011193
* expiry cache is also used by RLIMIT_CPU!.
12021194
*/
1203-
if (expires_gt(*nextevt, *newval))
1195+
if (*newval < *nextevt)
12041196
*nextevt = *newval;
12051197

12061198
tick_dep_set_signal(tsk->signal, TICK_DEP_BIT_POSIX_TIMER);

0 commit comments

Comments
 (0)