Skip to content

Commit 9649877

Browse files
committed
posix-cpu-timers: Replace __get_task_for_clock with pid_for_clock
Now that the codes store references to pids instead of referendes to tasks. Looking up a task for a clock instead of looking up a struct pid makes the code more difficult to verify it is correct than necessary. In posix_cpu_timers_create get_task_pid can race with release_task for threads and return a NULL pid. As put_pid and cpu_timer_task_rcu handle NULL pids just fine the code works without problems but it is an extra case to consider and keep in mind while verifying and modifying the code. There are races with de_thread to consider that only don't apply because thread clocks are only allowed for threads in the same thread_group. So instead of leaving a burden for people making modification to the code in the future return a rcu protected struct pid for the clock instead. The logic for __get_task_for_pid and lookup_task has been folded into the new function pid_for_clock with the only change being the logic has been modified from working on a task to working on a pid that will be returned. In posix_cpu_clock_get instead of calling pid_for_clock checking the result and then calling pid_task to get the task. The result of pid_for_clock is fed directly into pid_task. This is safe because pid_task handles NULL pids. As such an extra error check was unnecessary. Instead of hiding the flag that enables the special clock_gettime handling, I have made the 3 callers just pass the flag in themselves. That is less code and seems just as simple to work with as the wrapper functions. Historically the clock_gettime special case of allowing a process clock to be found by the thread id did not even exist [33ab0fe] but Thomas Gleixner reports that he has found code that uses that functionality [55e8c8e]. Link: https://lkml.kernel.org/r/87zhaxqkwa.fsf@nanos.tec.linutronix.de/ Ref: 33ab0fe ("posix-timers: Consolidate posix_cpu_clock_get()") Ref: 55e8c8e ("posix-cpu-timers: Store a reference to a pid not a task") Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
1 parent fece982 commit 9649877

1 file changed

Lines changed: 30 additions & 45 deletions

File tree

kernel/time/posix-cpu-timers.c

Lines changed: 30 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -47,67 +47,52 @@ void update_rlimit_cpu(struct task_struct *task, unsigned long rlim_new)
4747
/*
4848
* Functions for validating access to tasks.
4949
*/
50-
static struct task_struct *lookup_task(const pid_t pid, bool thread,
51-
bool gettime)
50+
static struct pid *pid_for_clock(const clockid_t clock, bool gettime)
5251
{
53-
struct task_struct *p;
52+
const bool thread = !!CPUCLOCK_PERTHREAD(clock);
53+
const pid_t upid = CPUCLOCK_PID(clock);
54+
struct pid *pid;
55+
56+
if (CPUCLOCK_WHICH(clock) >= CPUCLOCK_MAX)
57+
return NULL;
5458

5559
/*
5660
* If the encoded PID is 0, then the timer is targeted at current
5761
* or the process to which current belongs.
5862
*/
59-
if (!pid)
60-
return thread ? current : current->group_leader;
63+
if (upid == 0)
64+
return thread ? task_pid(current) : task_tgid(current);
6165

62-
p = find_task_by_vpid(pid);
63-
if (!p)
64-
return p;
66+
pid = find_vpid(upid);
67+
if (!pid)
68+
return NULL;
6569

66-
if (thread)
67-
return same_thread_group(p, current) ? p : NULL;
70+
if (thread) {
71+
struct task_struct *tsk = pid_task(pid, PIDTYPE_PID);
72+
return (tsk && same_thread_group(tsk, current)) ? pid : NULL;
73+
}
6874

6975
/*
70-
* For clock_gettime(PROCESS) the task does not need to be
71-
* the actual group leader. task->signal gives
72-
* access to the group's clock.
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.
7380
*/
74-
if (gettime && (p == current))
75-
return p;
81+
if (gettime && (pid == task_pid(current)))
82+
return task_tgid(current);
7683

7784
/*
78-
* For processes require that p is group leader.
85+
* For processes require that pid identifies a process.
7986
*/
80-
return thread_group_leader(p) ? p : NULL;
81-
}
82-
83-
static struct task_struct *__get_task_for_clock(const clockid_t clock,
84-
bool gettime)
85-
{
86-
const bool thread = !!CPUCLOCK_PERTHREAD(clock);
87-
const pid_t pid = CPUCLOCK_PID(clock);
88-
89-
if (CPUCLOCK_WHICH(clock) >= CPUCLOCK_MAX)
90-
return NULL;
91-
92-
return lookup_task(pid, thread, gettime);
93-
}
94-
95-
static inline struct task_struct *get_task_for_clock(const clockid_t clock)
96-
{
97-
return __get_task_for_clock(clock, false);
98-
}
99-
100-
static inline struct task_struct *get_task_for_clock_get(const clockid_t clock)
101-
{
102-
return __get_task_for_clock(clock, true);
87+
return pid_has_task(pid, PIDTYPE_TGID) ? pid : NULL;
10388
}
10489

10590
static inline int validate_clock_permissions(const clockid_t clock)
10691
{
10792
int ret;
10893

10994
rcu_read_lock();
110-
ret = __get_task_for_clock(clock, false) ? 0 : -EINVAL;
95+
ret = pid_for_clock(clock, false) ? 0 : -EINVAL;
11196
rcu_read_unlock();
11297

11398
return ret;
@@ -369,7 +354,7 @@ static int posix_cpu_clock_get(const clockid_t clock, struct timespec64 *tp)
369354
u64 t;
370355

371356
rcu_read_lock();
372-
tsk = get_task_for_clock_get(clock);
357+
tsk = pid_task(pid_for_clock(clock, true), clock_pid_type(clock));
373358
if (!tsk) {
374359
rcu_read_unlock();
375360
return -EINVAL;
@@ -392,18 +377,18 @@ static int posix_cpu_clock_get(const clockid_t clock, struct timespec64 *tp)
392377
*/
393378
static int posix_cpu_timer_create(struct k_itimer *new_timer)
394379
{
395-
struct task_struct *p;
380+
struct pid *pid;
396381

397382
rcu_read_lock();
398-
p = get_task_for_clock(new_timer->it_clock);
399-
if (!p) {
383+
pid = pid_for_clock(new_timer->it_clock, false);
384+
if (!pid) {
400385
rcu_read_unlock();
401386
return -EINVAL;
402387
}
403388

404389
new_timer->kclock = &clock_posix_cpu;
405390
timerqueue_init(&new_timer->it.cpu.node);
406-
new_timer->it.cpu.pid = get_task_pid(p, clock_pid_type(new_timer->it_clock));
391+
new_timer->it.cpu.pid = get_pid(pid);
407392
rcu_read_unlock();
408393
return 0;
409394
}

0 commit comments

Comments
 (0)