[tip: timers/core] posix-cpu-timers: Use common permission check in posix_cpu_clock_get()

From: tip-bot2 for Thomas Gleixner
Date: Wed Aug 28 2019 - 06:18:48 EST


The following commit has been merged into the timers/core branch of tip:

Commit-ID: bfcf3e92c6c07cd1084624bad5622f3dad96328c
Gitweb: https://git.kernel.org/tip/bfcf3e92c6c07cd1084624bad5622f3dad96328c
Author: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
AuthorDate: Wed, 21 Aug 2019 21:08:49 +02:00
Committer: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
CommitterDate: Wed, 28 Aug 2019 11:50:25 +02:00

posix-cpu-timers: Use common permission check in posix_cpu_clock_get()

Replace the next slightly different copy of permission checks. That also
removes the necessarity to check the return value of the sample functions
because the clock id is already validated.

Signed-off-by: Thomas Gleixner <tglx@xxxxxxxxxxxxx>
Reviewed-by: Frederic Weisbecker <frederic@xxxxxxxxxx>
Link: https://lkml.kernel.org/r/20190821192919.414813172@xxxxxxxxxxxxx

---
kernel/time/posix-cpu-timers.c | 57 ++++++++-------------------------
1 file changed, 14 insertions(+), 43 deletions(-)

diff --git a/kernel/time/posix-cpu-timers.c b/kernel/time/posix-cpu-timers.c
index b06ed8b..eb11117 100644
--- a/kernel/time/posix-cpu-timers.c
+++ b/kernel/time/posix-cpu-timers.c
@@ -289,53 +289,24 @@ static int cpu_clock_sample_group(const clockid_t which_clock,
return 0;
}

-static int posix_cpu_clock_get_task(struct task_struct *tsk,
- const clockid_t which_clock,
- struct timespec64 *tp)
+static int posix_cpu_clock_get(const clockid_t clock, struct timespec64 *tp)
{
- int err = -EINVAL;
- u64 rtn;
-
- if (CPUCLOCK_PERTHREAD(which_clock)) {
- if (same_thread_group(tsk, current))
- err = cpu_clock_sample(which_clock, tsk, &rtn);
- } else {
- if (tsk == current || thread_group_leader(tsk))
- err = cpu_clock_sample_group(which_clock, tsk, &rtn);
- }
-
- if (!err)
- *tp = ns_to_timespec64(rtn);
+ const clockid_t clkid = CPUCLOCK_WHICH(clock);
+ struct task_struct *tsk;
+ u64 t;

- return err;
-}
-
-
-static int posix_cpu_clock_get(const clockid_t which_clock, struct timespec64 *tp)
-{
- const pid_t pid = CPUCLOCK_PID(which_clock);
- int err = -EINVAL;
+ tsk = get_task_for_clock(clock);
+ if (!tsk)
+ return -EINVAL;

- if (pid == 0) {
- /*
- * Special case constant value for our own clocks.
- * We don't have to do any lookup to find ourselves.
- */
- err = posix_cpu_clock_get_task(current, which_clock, tp);
- } else {
- /*
- * Find the given PID, and validate that the caller
- * should be able to see it.
- */
- struct task_struct *p;
- rcu_read_lock();
- p = find_task_by_vpid(pid);
- if (p)
- err = posix_cpu_clock_get_task(p, which_clock, tp);
- rcu_read_unlock();
- }
+ if (CPUCLOCK_PERTHREAD(clock))
+ cpu_clock_sample(clkid, tsk, &t);
+ else
+ cpu_clock_sample_group(clkid, tsk, &t);
+ put_task_struct(tsk);

- return err;
+ *tp = ns_to_timespec64(t);
+ return 0;
}

/*