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.
Showing the changes to kernel (1 of 1 file). Show all
Unified · Split
| @@ -81,36 +81,36 @@ static struct task_struct *lookup_task(const pid_t pid, bool thread, · 80 unmodified lines | ||
| 81 | 81 | } |
| 82 | 82 | |
| 83 | 83 | static struct task_struct *__get_task_for_clock(const clockid_t clock, |
| 84 | − bool getref, bool gettime) | |
| 84 | + bool gettime) | |
| 85 | 85 | { |
| 86 | 86 | const bool thread = !!CPUCLOCK_PERTHREAD(clock); |
| 87 | 87 | const pid_t pid = CPUCLOCK_PID(clock); |
| 88 | − struct task_struct *p; | |
| 89 | 88 | |
| 90 | 89 | if (CPUCLOCK_WHICH(clock) >= CPUCLOCK_MAX) |
| 91 | 90 | return NULL; |
| 92 | 91 | |
| 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); | |
| 99 | 93 | } |
| 100 | 94 | |
| 101 | 95 | static inline struct task_struct *get_task_for_clock(const clockid_t clock) |
| 102 | 96 | { |
| 103 | − return __get_task_for_clock(clock, true, false); | |
| 97 | + return __get_task_for_clock(clock, false); | |
| 104 | 98 | } |
| 105 | 99 | |
| 106 | 100 | static inline struct task_struct *get_task_for_clock_get(const clockid_t clock) |
| 107 | 101 | { |
| 108 | − return __get_task_for_clock(clock, true, true); | |
| 102 | + return __get_task_for_clock(clock, true); | |
| 109 | 103 | } |
| 110 | 104 | |
| 111 | 105 | static inline int validate_clock_permissions(const clockid_t clock) |
| 112 | 106 | { |
| 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; | |
| 114 | 114 | } |
| 115 | 115 | |
| 116 | 116 | 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 | ||
| 368 | 368 | struct task_struct *tsk; |
| 369 | 369 | u64 t; |
| 370 | 370 | |
| 371 | + rcu_read_lock(); | |
| 371 | 372 | tsk = get_task_for_clock_get(clock); |
| 372 | − if (!tsk) | |
| 373 | + if (!tsk) { | |
| 374 | + rcu_read_unlock(); | |
| 373 | 375 | return -EINVAL; |
| 376 | + } | |
| 374 | 377 | |
| 375 | 378 | if (CPUCLOCK_PERTHREAD(clock)) |
| 376 | 379 | t = cpu_clock_sample(clkid, tsk); |
| 377 | 380 | else |
| 378 | 381 | t = cpu_clock_sample_group(clkid, tsk, false); |
| 379 | − put_task_struct(tsk); | |
| 382 | + rcu_read_unlock(); | |
| 380 | 383 | |
| 381 | 384 | *tp = ns_to_timespec64(t); |
| 382 | 385 | return 0; |
| @@ -389,19 +392,19 @@ static int posix_cpu_clock_get(const clockid_t clock, struct timespec64 *tp) · 6 unmodified lines | ||
| 389 | 392 | */ |
| 390 | 393 | static int posix_cpu_timer_create(struct k_itimer *new_timer) |
| 391 | 394 | { |
| 392 | − struct task_struct *p = get_task_for_clock(new_timer->it_clock); | |
| 395 | + struct task_struct *p; | |
| 393 | 396 | |
| 394 | − if (!p) | |
| 397 | + rcu_read_lock(); | |
| 398 | + p = get_task_for_clock(new_timer->it_clock); | |
| 399 | + if (!p) { | |
| 400 | + rcu_read_unlock(); | |
| 395 | 401 | return -EINVAL; |
| 402 | + } | |
| 396 | 403 | |
| 397 | 404 | new_timer->kclock = &clock_posix_cpu; |
| 398 | 405 | timerqueue_init(&new_timer->it.cpu.node); |
| 399 | 406 | 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(); | |
| 405 | 408 | return 0; |
| 406 | 409 | } |
| 407 | 410 | |