History

posix-cpu-timers: Extend rcu_read_lock removing task_struct references

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.

Eric W. Biederman committed 6 years ago

Commit 9bf7c321 parent bbd40fc1 file · +23 −20Browse files

Showing the changes to kernel (1 of 1 file). Show all

Unified · Split

kernel/time/posix-cpu-timers.c+23−20
@@ -81,36 +81,36 @@ static struct task_struct *lookup_task(const pid_t pid, bool thread, · 80 unmodified lines
8181 }
8282
8383 static struct task_struct *__get_task_for_clock(const clockid_t clock,
84− bool getref, bool gettime)
84+ bool gettime)
8585 {
8686 const bool thread = !!CPUCLOCK_PERTHREAD(clock);
8787 const pid_t pid = CPUCLOCK_PID(clock);
88− struct task_struct *p;
8988
9089 if (CPUCLOCK_WHICH(clock) >= CPUCLOCK_MAX)
9190 return NULL;
9291
93− rcu_read_lock();
94− p = lookup_task(pid, thread, gettime);
95− if (p && getref)
96− get_task_struct(p);
97− rcu_read_unlock();
98− return p;
92+ return lookup_task(pid, thread, gettime);
9993 }
10094
10195 static inline struct task_struct *get_task_for_clock(const clockid_t clock)
10296 {
103− return __get_task_for_clock(clock, true, false);
97+ return __get_task_for_clock(clock, false);
10498 }
10599
106100 static inline struct task_struct *get_task_for_clock_get(const clockid_t clock)
107101 {
108− return __get_task_for_clock(clock, true, true);
102+ return __get_task_for_clock(clock, true);
109103 }
110104
111105 static inline int validate_clock_permissions(const clockid_t clock)
112106 {
113− return __get_task_for_clock(clock, false, false) ? 0 : -EINVAL;
107+ int ret;
108+
109+ rcu_read_lock();
110+ ret = __get_task_for_clock(clock, false) ? 0 : -EINVAL;
111+ rcu_read_unlock();
112+
113+ return ret;
114114 }
115115
116116 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) · 251 unmodified lines
368368 struct task_struct *tsk;
369369 u64 t;
370370
371+ rcu_read_lock();
371372 tsk = get_task_for_clock_get(clock);
372− if (!tsk)
373+ if (!tsk) {
374+ rcu_read_unlock();
373375 return -EINVAL;
376+ }
374377
375378 if (CPUCLOCK_PERTHREAD(clock))
376379 t = cpu_clock_sample(clkid, tsk);
377380 else
378381 t = cpu_clock_sample_group(clkid, tsk, false);
379− put_task_struct(tsk);
382+ rcu_read_unlock();
380383
381384 *tp = ns_to_timespec64(t);
382385 return 0;
@@ -389,19 +392,19 @@ static int posix_cpu_clock_get(const clockid_t clock, struct timespec64 *tp) · 6 unmodified lines
389392 */
390393 static int posix_cpu_timer_create(struct k_itimer *new_timer)
391394 {
392− struct task_struct *p = get_task_for_clock(new_timer->it_clock);
395+ struct task_struct *p;
393396
394− if (!p)
397+ rcu_read_lock();
398+ p = get_task_for_clock(new_timer->it_clock);
399+ if (!p) {
400+ rcu_read_unlock();
395401 return -EINVAL;
402+ }
396403
397404 new_timer->kclock = &clock_posix_cpu;
398405 timerqueue_init(&new_timer->it.cpu.node);
399406 new_timer->it.cpu.pid = get_task_pid(p, cpu_timer_pid_type(new_timer));
400− /*
401− * get_task_for_clock() took a reference on @p. Drop it as the timer
402− * holds a reference on the pid of @p.
403− */
404− put_task_struct(p);
407+ rcu_read_unlock();
405408 return 0;
406409 }
407410

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t