[PATCH v1 1/3] posix-cpu-timers: Extend rcu_read_lock removing task_struct references

From: Eric W. Biederman
Date: Thu Apr 30 2020 - 07:59:14 EST



Now that the code stores of pid references it is no longer necessary
or desirable to take a reference on task_struct in __get_task_for_clock.

Instead extend the scope of rcu_read_lock and remove the reference
counting on struct task_struct entirely.

Signed-off-by: "Eric W. Biederman" <ebiederm@xxxxxxxxxxxx>
---
kernel/time/posix-cpu-timers.c | 43 ++++++++++++++++++----------------
1 file changed, 23 insertions(+), 20 deletions(-)

diff --git a/kernel/time/posix-cpu-timers.c b/kernel/time/posix-cpu-timers.c
index b7f972fb115e..91996dd089a4 100644
--- a/kernel/time/posix-cpu-timers.c
+++ b/kernel/time/posix-cpu-timers.c
@@ -81,36 +81,36 @@ static struct task_struct *lookup_task(const pid_t pid, bool thread,
}

static struct task_struct *__get_task_for_clock(const clockid_t clock,
- bool getref, bool gettime)
+ bool gettime)
{
const bool thread = !!CPUCLOCK_PERTHREAD(clock);
const pid_t pid = CPUCLOCK_PID(clock);
- struct task_struct *p;

if (CPUCLOCK_WHICH(clock) >= CPUCLOCK_MAX)
return NULL;

- rcu_read_lock();
- p = lookup_task(pid, thread, gettime);
- if (p && getref)
- get_task_struct(p);
- rcu_read_unlock();
- return p;
+ return lookup_task(pid, thread, gettime);
}

static inline struct task_struct *get_task_for_clock(const clockid_t clock)
{
- return __get_task_for_clock(clock, true, false);
+ return __get_task_for_clock(clock, false);
}

static inline struct task_struct *get_task_for_clock_get(const clockid_t clock)
{
- return __get_task_for_clock(clock, true, true);
+ return __get_task_for_clock(clock, true);
}

static inline int validate_clock_permissions(const clockid_t clock)
{
- return __get_task_for_clock(clock, false, false) ? 0 : -EINVAL;
+ int ret;
+
+ rcu_read_lock();
+ ret = __get_task_for_clock(clock, false) ? 0 : -EINVAL;
+ rcu_read_unlock();
+
+ return ret;
}

static inline enum pid_type cpu_timer_pid_type(struct k_itimer *timer)
@@ -368,15 +368,18 @@ static int posix_cpu_clock_get(const clockid_t clock, struct timespec64 *tp)
struct task_struct *tsk;
u64 t;

+ rcu_read_lock();
tsk = get_task_for_clock_get(clock);
- if (!tsk)
+ if (!tsk) {
+ rcu_read_unlock();
return -EINVAL;
+ }

if (CPUCLOCK_PERTHREAD(clock))
t = cpu_clock_sample(clkid, tsk);
else
t = cpu_clock_sample_group(clkid, tsk, false);
- put_task_struct(tsk);
+ rcu_read_unlock();

*tp = ns_to_timespec64(t);
return 0;
@@ -389,19 +392,19 @@ static int posix_cpu_clock_get(const clockid_t clock, struct timespec64 *tp)
*/
static int posix_cpu_timer_create(struct k_itimer *new_timer)
{
- struct task_struct *p = get_task_for_clock(new_timer->it_clock);
+ struct task_struct *p;

- if (!p)
+ rcu_read_lock();
+ p = get_task_for_clock(new_timer->it_clock);
+ if (!p) {
+ rcu_read_unlock();
return -EINVAL;
+ }

new_timer->kclock = &clock_posix_cpu;
timerqueue_init(&new_timer->it.cpu.node);
new_timer->it.cpu.pid = get_task_pid(p, cpu_timer_pid_type(new_timer));
- /*
- * get_task_for_clock() took a reference on @p. Drop it as the timer
- * holds a reference on the pid of @p.
- */
- put_task_struct(p);
+ rcu_read_unlock();
return 0;
}

--
2.25.0