Commit 55e8c8eb authored by Eric W. Biederman's avatar Eric W. Biederman Committed by Thomas Gleixner

posix-cpu-timers: Store a reference to a pid not a task

posix cpu timers do not handle the death of a process well.

This is most clearly seen when a multi-threaded process calls exec from a
thread that is not the leader of the thread group.  The posix cpu timer code
continues to pin the old thread group leader and is unable to find the
siglock from there.

This results in posix_cpu_timer_del being unable to delete a timer,
posix_cpu_timer_set being unable to set a timer.  Further to compensate for
the problems in posix_cpu_timer_del on a multi-threaded exec all timers
that point at the multi-threaded task are stopped.

The code for the timers fundamentally needs to check if the target
process/thread is alive.  This needs an extra level of indirection. This
level of indirection is already available in struct pid.

So replace cpu.task with cpu.pid to get the needed extra layer of
indirection.

In addition to handling things more cleanly this reduces the amount of
memory a timer can pin when a process exits and then is reaped from
a task_struct to the vastly smaller struct pid.

Fixes: e0a70217 ("posix-cpu-timers: workaround to suppress the problems with mt exec")
Signed-off-by: default avatar"Eric W. Biederman" <ebiederm@xmission.com>
Signed-off-by: default avatarThomas Gleixner <tglx@linutronix.de>
Link: https://lkml.kernel.org/r/87wo86tz6d.fsf@x220.int.ebiederm.org
parent beb41d9c
...@@ -69,7 +69,7 @@ static inline int clockid_to_fd(const clockid_t clk) ...@@ -69,7 +69,7 @@ static inline int clockid_to_fd(const clockid_t clk)
struct cpu_timer { struct cpu_timer {
struct timerqueue_node node; struct timerqueue_node node;
struct timerqueue_head *head; struct timerqueue_head *head;
struct task_struct *task; struct pid *pid;
struct list_head elist; struct list_head elist;
int firing; int firing;
}; };
......
...@@ -118,6 +118,16 @@ static inline int validate_clock_permissions(const clockid_t clock) ...@@ -118,6 +118,16 @@ static inline int validate_clock_permissions(const clockid_t clock)
return __get_task_for_clock(clock, false, false) ? 0 : -EINVAL; return __get_task_for_clock(clock, false, false) ? 0 : -EINVAL;
} }
static inline enum pid_type cpu_timer_pid_type(struct k_itimer *timer)
{
return CPUCLOCK_PERTHREAD(timer->it_clock) ? PIDTYPE_PID : PIDTYPE_TGID;
}
static inline struct task_struct *cpu_timer_task_rcu(struct k_itimer *timer)
{
return pid_task(timer->it.cpu.pid, cpu_timer_pid_type(timer));
}
/* /*
* Update expiry time from increment, and increase overrun count, * Update expiry time from increment, and increase overrun count,
* given the current clock sample. * given the current clock sample.
...@@ -391,7 +401,12 @@ static int posix_cpu_timer_create(struct k_itimer *new_timer) ...@@ -391,7 +401,12 @@ static int posix_cpu_timer_create(struct k_itimer *new_timer)
new_timer->kclock = &clock_posix_cpu; new_timer->kclock = &clock_posix_cpu;
timerqueue_init(&new_timer->it.cpu.node); timerqueue_init(&new_timer->it.cpu.node);
new_timer->it.cpu.task = p; 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);
return 0; return 0;
} }
...@@ -404,13 +419,15 @@ static int posix_cpu_timer_create(struct k_itimer *new_timer) ...@@ -404,13 +419,15 @@ static int posix_cpu_timer_create(struct k_itimer *new_timer)
static int posix_cpu_timer_del(struct k_itimer *timer) static int posix_cpu_timer_del(struct k_itimer *timer)
{ {
struct cpu_timer *ctmr = &timer->it.cpu; struct cpu_timer *ctmr = &timer->it.cpu;
struct task_struct *p = ctmr->task;
struct sighand_struct *sighand; struct sighand_struct *sighand;
struct task_struct *p;
unsigned long flags; unsigned long flags;
int ret = 0; int ret = 0;
if (WARN_ON_ONCE(!p)) rcu_read_lock();
return -EINVAL; p = cpu_timer_task_rcu(timer);
if (!p)
goto out;
/* /*
* Protect against sighand release/switch in exit/exec and process/ * Protect against sighand release/switch in exit/exec and process/
...@@ -432,8 +449,10 @@ static int posix_cpu_timer_del(struct k_itimer *timer) ...@@ -432,8 +449,10 @@ static int posix_cpu_timer_del(struct k_itimer *timer)
unlock_task_sighand(p, &flags); unlock_task_sighand(p, &flags);
} }
out:
rcu_read_unlock();
if (!ret) if (!ret)
put_task_struct(p); put_pid(ctmr->pid);
return ret; return ret;
} }
...@@ -561,13 +580,21 @@ static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags, ...@@ -561,13 +580,21 @@ static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags,
clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock); clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock);
u64 old_expires, new_expires, old_incr, val; u64 old_expires, new_expires, old_incr, val;
struct cpu_timer *ctmr = &timer->it.cpu; struct cpu_timer *ctmr = &timer->it.cpu;
struct task_struct *p = ctmr->task;
struct sighand_struct *sighand; struct sighand_struct *sighand;
struct task_struct *p;
unsigned long flags; unsigned long flags;
int ret = 0; int ret = 0;
if (WARN_ON_ONCE(!p)) rcu_read_lock();
return -EINVAL; p = cpu_timer_task_rcu(timer);
if (!p) {
/*
* If p has just been reaped, we can no
* longer get any information about it at all.
*/
rcu_read_unlock();
return -ESRCH;
}
/* /*
* Use the to_ktime conversion because that clamps the maximum * Use the to_ktime conversion because that clamps the maximum
...@@ -584,8 +611,10 @@ static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags, ...@@ -584,8 +611,10 @@ static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags,
* If p has just been reaped, we can no * If p has just been reaped, we can no
* longer get any information about it at all. * longer get any information about it at all.
*/ */
if (unlikely(sighand == NULL)) if (unlikely(sighand == NULL)) {
rcu_read_unlock();
return -ESRCH; return -ESRCH;
}
/* /*
* Disarm any old timer after extracting its expiry time. * Disarm any old timer after extracting its expiry time.
...@@ -690,6 +719,7 @@ static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags, ...@@ -690,6 +719,7 @@ static int posix_cpu_timer_set(struct k_itimer *timer, int timer_flags,
ret = 0; ret = 0;
out: out:
rcu_read_unlock();
if (old) if (old)
old->it_interval = ns_to_timespec64(old_incr); old->it_interval = ns_to_timespec64(old_incr);
...@@ -701,10 +731,12 @@ static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec64 *itp ...@@ -701,10 +731,12 @@ static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec64 *itp
clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock); clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock);
struct cpu_timer *ctmr = &timer->it.cpu; struct cpu_timer *ctmr = &timer->it.cpu;
u64 now, expires = cpu_timer_getexpires(ctmr); u64 now, expires = cpu_timer_getexpires(ctmr);
struct task_struct *p = ctmr->task; struct task_struct *p;
if (WARN_ON_ONCE(!p)) rcu_read_lock();
return; p = cpu_timer_task_rcu(timer);
if (!p)
goto out;
/* /*
* Easy part: convert the reload time. * Easy part: convert the reload time.
...@@ -712,7 +744,7 @@ static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec64 *itp ...@@ -712,7 +744,7 @@ static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec64 *itp
itp->it_interval = ktime_to_timespec64(timer->it_interval); itp->it_interval = ktime_to_timespec64(timer->it_interval);
if (!expires) if (!expires)
return; goto out;
/* /*
* Sample the clock to take the difference with the expiry time. * Sample the clock to take the difference with the expiry time.
...@@ -732,6 +764,8 @@ static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec64 *itp ...@@ -732,6 +764,8 @@ static void posix_cpu_timer_get(struct k_itimer *timer, struct itimerspec64 *itp
itp->it_value.tv_nsec = 1; itp->it_value.tv_nsec = 1;
itp->it_value.tv_sec = 0; itp->it_value.tv_sec = 0;
} }
out:
rcu_read_unlock();
} }
#define MAX_COLLECTED 20 #define MAX_COLLECTED 20
...@@ -952,14 +986,15 @@ static void check_process_timers(struct task_struct *tsk, ...@@ -952,14 +986,15 @@ static void check_process_timers(struct task_struct *tsk,
static void posix_cpu_timer_rearm(struct k_itimer *timer) static void posix_cpu_timer_rearm(struct k_itimer *timer)
{ {
clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock); clockid_t clkid = CPUCLOCK_WHICH(timer->it_clock);
struct cpu_timer *ctmr = &timer->it.cpu; struct task_struct *p;
struct task_struct *p = ctmr->task;
struct sighand_struct *sighand; struct sighand_struct *sighand;
unsigned long flags; unsigned long flags;
u64 now; u64 now;
if (WARN_ON_ONCE(!p)) rcu_read_lock();
return; p = cpu_timer_task_rcu(timer);
if (!p)
goto out;
/* /*
* Fetch the current sample and update the timer's expiry time. * Fetch the current sample and update the timer's expiry time.
...@@ -974,13 +1009,15 @@ static void posix_cpu_timer_rearm(struct k_itimer *timer) ...@@ -974,13 +1009,15 @@ static void posix_cpu_timer_rearm(struct k_itimer *timer)
/* Protect timer list r/w in arm_timer() */ /* Protect timer list r/w in arm_timer() */
sighand = lock_task_sighand(p, &flags); sighand = lock_task_sighand(p, &flags);
if (unlikely(sighand == NULL)) if (unlikely(sighand == NULL))
return; goto out;
/* /*
* Now re-arm for the new expiry time. * Now re-arm for the new expiry time.
*/ */
arm_timer(timer, p); arm_timer(timer, p);
unlock_task_sighand(p, &flags); unlock_task_sighand(p, &flags);
out:
rcu_read_unlock();
} }
/** /**
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment