sched, signals: fix the racy usage of ->signal in account_group_xxx/run_posix_cpu_timers

Impact: fix potential NULL dereference

Contrary to ad474caca3e2a0550b7ce0706527ad5ab389a4d4 changelog, other
acct_group_xxx() helpers can be called after exit_notify() by timer tick.
Thanks to Roland for pointing out this. Somehow I missed this simple fact
when I read the original patch, and I am afraid I confused Frank during
the discussion. Sorry.

Fortunately, these helpers work with current, we can check ->exit_state
to ensure that ->signal can't go away under us.

Also, add the comment and compiler barrier to account_group_exec_runtime(),
to make sure we load ->signal only once.

Signed-off-by: Oleg Nesterov <oleg@redhat.com>
Signed-off-by: Ingo Molnar <mingo@elte.hu>

authored by Oleg Nesterov and committed by Ingo Molnar ad133ba3 29d7b90c

+16 -6
+5 -2
kernel/posix-cpu-timers.c
··· 1308 1308 */ 1309 1309 static inline int fastpath_timer_check(struct task_struct *tsk) 1310 1310 { 1311 - struct signal_struct *sig = tsk->signal; 1311 + struct signal_struct *sig; 1312 1312 1313 - if (unlikely(!sig)) 1313 + /* tsk == current, ensure it is safe to use ->signal/sighand */ 1314 + if (unlikely(tsk->exit_state)) 1314 1315 return 0; 1315 1316 1316 1317 if (!task_cputime_zero(&tsk->cputime_expires)) { ··· 1324 1323 if (task_cputime_expired(&task_sample, &tsk->cputime_expires)) 1325 1324 return 1; 1326 1325 } 1326 + 1327 + sig = tsk->signal; 1327 1328 if (!task_cputime_zero(&sig->cputime_expires)) { 1328 1329 struct task_cputime group_sample; 1329 1330
+11 -4
kernel/sched_stats.h
··· 298 298 { 299 299 struct signal_struct *sig; 300 300 301 - sig = tsk->signal; 302 - if (unlikely(!sig)) 301 + /* tsk == current, ensure it is safe to use ->signal */ 302 + if (unlikely(tsk->exit_state)) 303 303 return; 304 + 305 + sig = tsk->signal; 304 306 if (sig->cputime.totals) { 305 307 struct task_cputime *times; 306 308 ··· 327 325 { 328 326 struct signal_struct *sig; 329 327 330 - sig = tsk->signal; 331 - if (unlikely(!sig)) 328 + /* tsk == current, ensure it is safe to use ->signal */ 329 + if (unlikely(tsk->exit_state)) 332 330 return; 331 + 332 + sig = tsk->signal; 333 333 if (sig->cputime.totals) { 334 334 struct task_cputime *times; 335 335 ··· 357 353 struct signal_struct *sig; 358 354 359 355 sig = tsk->signal; 356 + /* see __exit_signal()->task_rq_unlock_wait() */ 357 + barrier(); 360 358 if (unlikely(!sig)) 361 359 return; 360 + 362 361 if (sig->cputime.totals) { 363 362 struct task_cputime *times; 364 363