Skip to content

Commit b18b6a9

Browse files
Nicolas Pitrejohnstultz-work
authored andcommitted
timers: Omit POSIX timer stuff from task_struct when disabled
When CONFIG_POSIX_TIMERS is disabled, it is preferable to remove related structures from struct task_struct and struct signal_struct as they won't contain anything useful and shouldn't be relied upon by mistake. Code still referencing those structures is also disabled here. Signed-off-by: Nicolas Pitre <nico@linaro.org> Signed-off-by: John Stultz <john.stultz@linaro.org>
1 parent 9f81979 commit b18b6a9

6 files changed

Lines changed: 70 additions & 33 deletions

File tree

fs/proc/base.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2179,7 +2179,7 @@ static const struct file_operations proc_map_files_operations = {
21792179
.llseek = generic_file_llseek,
21802180
};
21812181

2182-
#ifdef CONFIG_CHECKPOINT_RESTORE
2182+
#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
21832183
struct timers_private {
21842184
struct pid *pid;
21852185
struct task_struct *task;
@@ -2936,7 +2936,7 @@ static const struct pid_entry tgid_base_stuff[] = {
29362936
REG("projid_map", S_IRUGO|S_IWUSR, proc_projid_map_operations),
29372937
REG("setgroups", S_IRUGO|S_IWUSR, proc_setgroups_operations),
29382938
#endif
2939-
#ifdef CONFIG_CHECKPOINT_RESTORE
2939+
#if defined(CONFIG_CHECKPOINT_RESTORE) && defined(CONFIG_POSIX_TIMERS)
29402940
REG("timers", S_IRUGO, proc_timers_operations),
29412941
#endif
29422942
REG("timerslack_ns", S_IRUGO|S_IWUGO, proc_pid_set_timerslack_ns_operations),

include/linux/init_task.h

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,21 +42,38 @@ extern struct fs_struct init_fs;
4242
#define INIT_PREV_CPUTIME(x)
4343
#endif
4444

45+
#ifdef CONFIG_POSIX_TIMERS
46+
#define INIT_POSIX_TIMERS(s) \
47+
.posix_timers = LIST_HEAD_INIT(s.posix_timers),
48+
#define INIT_CPU_TIMERS(s) \
49+
.cpu_timers = { \
50+
LIST_HEAD_INIT(s.cpu_timers[0]), \
51+
LIST_HEAD_INIT(s.cpu_timers[1]), \
52+
LIST_HEAD_INIT(s.cpu_timers[2]), \
53+
},
54+
#define INIT_CPUTIMER(s) \
55+
.cputimer = { \
56+
.cputime_atomic = INIT_CPUTIME_ATOMIC, \
57+
.running = false, \
58+
.checking_timer = false, \
59+
},
60+
#else
61+
#define INIT_POSIX_TIMERS(s)
62+
#define INIT_CPU_TIMERS(s)
63+
#define INIT_CPUTIMER(s)
64+
#endif
65+
4566
#define INIT_SIGNALS(sig) { \
4667
.nr_threads = 1, \
4768
.thread_head = LIST_HEAD_INIT(init_task.thread_node), \
4869
.wait_chldexit = __WAIT_QUEUE_HEAD_INITIALIZER(sig.wait_chldexit),\
4970
.shared_pending = { \
5071
.list = LIST_HEAD_INIT(sig.shared_pending.list), \
5172
.signal = {{0}}}, \
52-
.posix_timers = LIST_HEAD_INIT(sig.posix_timers), \
53-
.cpu_timers = INIT_CPU_TIMERS(sig.cpu_timers), \
73+
INIT_POSIX_TIMERS(sig) \
74+
INIT_CPU_TIMERS(sig) \
5475
.rlim = INIT_RLIMITS, \
55-
.cputimer = { \
56-
.cputime_atomic = INIT_CPUTIME_ATOMIC, \
57-
.running = false, \
58-
.checking_timer = false, \
59-
}, \
76+
INIT_CPUTIMER(sig) \
6077
INIT_PREV_CPUTIME(sig) \
6178
.cred_guard_mutex = \
6279
__MUTEX_INITIALIZER(sig.cred_guard_mutex), \
@@ -247,7 +264,7 @@ extern struct task_group root_task_group;
247264
.blocked = {{0}}, \
248265
.alloc_lock = __SPIN_LOCK_UNLOCKED(tsk.alloc_lock), \
249266
.journal_info = NULL, \
250-
.cpu_timers = INIT_CPU_TIMERS(tsk.cpu_timers), \
267+
INIT_CPU_TIMERS(tsk) \
251268
.pi_lock = __RAW_SPIN_LOCK_UNLOCKED(tsk.pi_lock), \
252269
.timer_slack_ns = 50000, /* 50 usec default slack */ \
253270
.pids = { \
@@ -274,13 +291,6 @@ extern struct task_group root_task_group;
274291
}
275292

276293

277-
#define INIT_CPU_TIMERS(cpu_timers) \
278-
{ \
279-
LIST_HEAD_INIT(cpu_timers[0]), \
280-
LIST_HEAD_INIT(cpu_timers[1]), \
281-
LIST_HEAD_INIT(cpu_timers[2]), \
282-
}
283-
284294
/* Attach to the init_task data structure for proper alignment */
285295
#define __init_task_data __attribute__((__section__(".data..init_task")))
286296

include/linux/sched.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -734,13 +734,14 @@ struct signal_struct {
734734
unsigned int is_child_subreaper:1;
735735
unsigned int has_child_subreaper:1;
736736

737+
#ifdef CONFIG_POSIX_TIMERS
738+
737739
/* POSIX.1b Interval Timers */
738740
int posix_timer_id;
739741
struct list_head posix_timers;
740742

741743
/* ITIMER_REAL timer for the process */
742744
struct hrtimer real_timer;
743-
struct pid *leader_pid;
744745
ktime_t it_real_incr;
745746

746747
/*
@@ -759,12 +760,16 @@ struct signal_struct {
759760
/* Earliest-expiration cache. */
760761
struct task_cputime cputime_expires;
761762

763+
struct list_head cpu_timers[3];
764+
765+
#endif
766+
767+
struct pid *leader_pid;
768+
762769
#ifdef CONFIG_NO_HZ_FULL
763770
atomic_t tick_dep_mask;
764771
#endif
765772

766-
struct list_head cpu_timers[3];
767-
768773
struct pid *tty_old_pgrp;
769774

770775
/* boolean value for session group leader */
@@ -1681,8 +1686,10 @@ struct task_struct {
16811686
/* mm fault and swap info: this can arguably be seen as either mm-specific or thread-specific */
16821687
unsigned long min_flt, maj_flt;
16831688

1689+
#ifdef CONFIG_POSIX_TIMERS
16841690
struct task_cputime cputime_expires;
16851691
struct list_head cpu_timers[3];
1692+
#endif
16861693

16871694
/* process credentials */
16881695
const struct cred __rcu *ptracer_cred; /* Tracer's credentials at attach */

kernel/fork.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1304,6 +1304,7 @@ void __cleanup_sighand(struct sighand_struct *sighand)
13041304
}
13051305
}
13061306

1307+
#ifdef CONFIG_POSIX_TIMERS
13071308
/*
13081309
* Initialize POSIX timer handling for a thread group.
13091310
*/
@@ -1322,6 +1323,9 @@ static void posix_cpu_timers_init_group(struct signal_struct *sig)
13221323
INIT_LIST_HEAD(&sig->cpu_timers[1]);
13231324
INIT_LIST_HEAD(&sig->cpu_timers[2]);
13241325
}
1326+
#else
1327+
static inline void posix_cpu_timers_init_group(struct signal_struct *sig) { }
1328+
#endif
13251329

13261330
static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
13271331
{
@@ -1346,11 +1350,11 @@ static int copy_signal(unsigned long clone_flags, struct task_struct *tsk)
13461350
init_waitqueue_head(&sig->wait_chldexit);
13471351
sig->curr_target = tsk;
13481352
init_sigpending(&sig->shared_pending);
1349-
INIT_LIST_HEAD(&sig->posix_timers);
13501353
seqlock_init(&sig->stats_lock);
13511354
prev_cputime_init(&sig->prev_cputime);
13521355

13531356
#ifdef CONFIG_POSIX_TIMERS
1357+
INIT_LIST_HEAD(&sig->posix_timers);
13541358
hrtimer_init(&sig->real_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL);
13551359
sig->real_timer.function = it_real_fn;
13561360
#endif
@@ -1425,6 +1429,7 @@ static void rt_mutex_init_task(struct task_struct *p)
14251429
#endif
14261430
}
14271431

1432+
#ifdef CONFIG_POSIX_TIMERS
14281433
/*
14291434
* Initialize POSIX timer handling for a single task.
14301435
*/
@@ -1437,6 +1442,9 @@ static void posix_cpu_timers_init(struct task_struct *tsk)
14371442
INIT_LIST_HEAD(&tsk->cpu_timers[1]);
14381443
INIT_LIST_HEAD(&tsk->cpu_timers[2]);
14391444
}
1445+
#else
1446+
static inline void posix_cpu_timers_init(struct task_struct *tsk) { }
1447+
#endif
14401448

14411449
static inline void
14421450
init_task_pid(struct task_struct *task, enum pid_type type, struct pid *pid)

kernel/sched/rt.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2246,6 +2246,7 @@ prio_changed_rt(struct rq *rq, struct task_struct *p, int oldprio)
22462246
}
22472247
}
22482248

2249+
#ifdef CONFIG_POSIX_TIMERS
22492250
static void watchdog(struct rq *rq, struct task_struct *p)
22502251
{
22512252
unsigned long soft, hard;
@@ -2267,6 +2268,9 @@ static void watchdog(struct rq *rq, struct task_struct *p)
22672268
p->cputime_expires.sched_exp = p->se.sum_exec_runtime;
22682269
}
22692270
}
2271+
#else
2272+
static inline void watchdog(struct rq *rq, struct task_struct *p) { }
2273+
#endif
22702274

22712275
static void task_tick_rt(struct rq *rq, struct task_struct *p, int queued)
22722276
{

kernel/sched/stats.h

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -172,18 +172,19 @@ sched_info_switch(struct rq *rq,
172172
*/
173173

174174
/**
175-
* cputimer_running - return true if cputimer is running
175+
* get_running_cputimer - return &tsk->signal->cputimer if cputimer is running
176176
*
177177
* @tsk: Pointer to target task.
178178
*/
179-
static inline bool cputimer_running(struct task_struct *tsk)
180-
179+
#ifdef CONFIG_POSIX_TIMERS
180+
static inline
181+
struct thread_group_cputimer *get_running_cputimer(struct task_struct *tsk)
181182
{
182183
struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
183184

184185
/* Check if cputimer isn't running. This is accessed without locking. */
185186
if (!READ_ONCE(cputimer->running))
186-
return false;
187+
return NULL;
187188

188189
/*
189190
* After we flush the task's sum_exec_runtime to sig->sum_sched_runtime
@@ -200,10 +201,17 @@ static inline bool cputimer_running(struct task_struct *tsk)
200201
* clock delta is behind the expiring timer value.
201202
*/
202203
if (unlikely(!tsk->sighand))
203-
return false;
204+
return NULL;
204205

205-
return true;
206+
return cputimer;
207+
}
208+
#else
209+
static inline
210+
struct thread_group_cputimer *get_running_cputimer(struct task_struct *tsk)
211+
{
212+
return NULL;
206213
}
214+
#endif
207215

208216
/**
209217
* account_group_user_time - Maintain utime for a thread group.
@@ -218,9 +226,9 @@ static inline bool cputimer_running(struct task_struct *tsk)
218226
static inline void account_group_user_time(struct task_struct *tsk,
219227
cputime_t cputime)
220228
{
221-
struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
229+
struct thread_group_cputimer *cputimer = get_running_cputimer(tsk);
222230

223-
if (!cputimer_running(tsk))
231+
if (!cputimer)
224232
return;
225233

226234
atomic64_add(cputime, &cputimer->cputime_atomic.utime);
@@ -239,9 +247,9 @@ static inline void account_group_user_time(struct task_struct *tsk,
239247
static inline void account_group_system_time(struct task_struct *tsk,
240248
cputime_t cputime)
241249
{
242-
struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
250+
struct thread_group_cputimer *cputimer = get_running_cputimer(tsk);
243251

244-
if (!cputimer_running(tsk))
252+
if (!cputimer)
245253
return;
246254

247255
atomic64_add(cputime, &cputimer->cputime_atomic.stime);
@@ -260,9 +268,9 @@ static inline void account_group_system_time(struct task_struct *tsk,
260268
static inline void account_group_exec_runtime(struct task_struct *tsk,
261269
unsigned long long ns)
262270
{
263-
struct thread_group_cputimer *cputimer = &tsk->signal->cputimer;
271+
struct thread_group_cputimer *cputimer = get_running_cputimer(tsk);
264272

265-
if (!cputimer_running(tsk))
273+
if (!cputimer)
266274
return;
267275

268276
atomic64_add(ns, &cputimer->cputime_atomic.sum_exec_runtime);

0 commit comments

Comments
 (0)