Skip to content

Commit 33ab0fe

Browse files
committed
posix-timers: Consolidate posix_cpu_clock_get()
Consolidate the clock sampling common code used for both local and remote targets. Note that this introduces a tiny user ABI change: if a PID is passed to clock_gettime() along the clockid, we used to forbid a process wide clock sample when that PID doesn't belong to a group leader. Now after this patch we allow process wide clock samples if that PID belongs to the current task, even if the current task is not the group leader. But local process wide clock samples are allowed if PID == 0 (current task) even if the current task is not the group leader. So in the end this should be no big deal as this actually harmonize the behaviour when the remote sample is actually a local one. Signed-off-by: Frederic Weisbecker <fweisbec@gmail.com> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Ingo Molnar <mingo@kernel.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Oleg Nesterov <oleg@redhat.com> Cc: Kosaki Motohiro <kosaki.motohiro@jp.fujitsu.com> Cc: Andrew Morton <akpm@linux-foundation.org>
1 parent af82eb3 commit 33ab0fe

1 file changed

Lines changed: 30 additions & 34 deletions

File tree

‎kernel/posix-cpu-timers.c‎

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -260,30 +260,43 @@ static int cpu_clock_sample_group(const clockid_t which_clock,
260260
return 0;
261261
}
262262

263+
static int posix_cpu_clock_get_task(struct task_struct *tsk,
264+
const clockid_t which_clock,
265+
struct timespec *tp)
266+
{
267+
int err = -EINVAL;
268+
unsigned long long rtn;
269+
270+
if (CPUCLOCK_PERTHREAD(which_clock)) {
271+
if (same_thread_group(tsk, current))
272+
err = cpu_clock_sample(which_clock, tsk, &rtn);
273+
} else {
274+
read_lock(&tasklist_lock);
275+
276+
if (tsk->sighand && (tsk == current || thread_group_leader(tsk)))
277+
err = cpu_clock_sample_group(which_clock, tsk, &rtn);
278+
279+
read_unlock(&tasklist_lock);
280+
}
281+
282+
if (!err)
283+
sample_to_timespec(which_clock, rtn, tp);
284+
285+
return err;
286+
}
287+
263288

264289
static int posix_cpu_clock_get(const clockid_t which_clock, struct timespec *tp)
265290
{
266291
const pid_t pid = CPUCLOCK_PID(which_clock);
267-
int error = -EINVAL;
268-
unsigned long long rtn;
292+
int err = -EINVAL;
269293

270294
if (pid == 0) {
271295
/*
272296
* Special case constant value for our own clocks.
273297
* We don't have to do any lookup to find ourselves.
274298
*/
275-
if (CPUCLOCK_PERTHREAD(which_clock)) {
276-
/*
277-
* Sampling just ourselves we can do with no locking.
278-
*/
279-
error = cpu_clock_sample(which_clock,
280-
current, &rtn);
281-
} else {
282-
read_lock(&tasklist_lock);
283-
error = cpu_clock_sample_group(which_clock,
284-
current, &rtn);
285-
read_unlock(&tasklist_lock);
286-
}
299+
err = posix_cpu_clock_get_task(current, which_clock, tp);
287300
} else {
288301
/*
289302
* Find the given PID, and validate that the caller
@@ -292,29 +305,12 @@ static int posix_cpu_clock_get(const clockid_t which_clock, struct timespec *tp)
292305
struct task_struct *p;
293306
rcu_read_lock();
294307
p = find_task_by_vpid(pid);
295-
if (p) {
296-
if (CPUCLOCK_PERTHREAD(which_clock)) {
297-
if (same_thread_group(p, current)) {
298-
error = cpu_clock_sample(which_clock,
299-
p, &rtn);
300-
}
301-
} else {
302-
read_lock(&tasklist_lock);
303-
if (thread_group_leader(p) && p->sighand) {
304-
error =
305-
cpu_clock_sample_group(which_clock,
306-
p, &rtn);
307-
}
308-
read_unlock(&tasklist_lock);
309-
}
310-
}
308+
if (p)
309+
err = posix_cpu_clock_get_task(p, which_clock, tp);
311310
rcu_read_unlock();
312311
}
313312

314-
if (error)
315-
return error;
316-
sample_to_timespec(which_clock, rtn, tp);
317-
return 0;
313+
return err;
318314
}
319315

320316

0 commit comments

Comments
 (0)