Skip to content

Commit ebad30a

Browse files
Stanislaw Gruszkagregkh
authored andcommitted
sched: fix divide by zero at {thread_group,task}_times
commit bea6832cc8c4a0a9a65dd17da6aaa657fe27bc3e upstream. On architectures where cputime_t is 64 bit type, is possible to trigger divide by zero on do_div(temp, (__force u32) total) line, if total is a non zero number but has lower 32 bit's zeroed. Removing casting is not a good solution since some do_div() implementations do cast to u32 internally. This problem can be triggered in practice on very long lived processes: PID: 2331 TASK: ffff880472814b00 CPU: 2 COMMAND: "oraagent.bin" #0 [ffff880472a51b70] machine_kexec at ffffffff8103214b detule#1 [ffff880472a51bd0] crash_kexec at ffffffff810b91c2 detule#2 [ffff880472a51ca0] oops_end at ffffffff814f0b00 detule#3 [ffff880472a51cd0] die at ffffffff8100f26b #4 [ffff880472a51d00] do_trap at ffffffff814f03f4 #5 [ffff880472a51d60] do_divide_error at ffffffff8100cfff #6 [ffff880472a51e00] divide_error at ffffffff8100be7b [exception RIP: thread_group_times+0x56] RIP: ffffffff81056a16 RSP: ffff880472a51eb8 RFLAGS: 00010046 RAX: bc3572c9fe12d194 RBX: ffff880874150800 RCX: 0000000110266fad RDX: 0000000000000000 RSI: ffff880472a51eb8 RDI: 001038ae7d9633dc RBP: ffff880472a51ef8 R8: 00000000b10a3a64 R9: ffff880874150800 R10: 00007fcba27ab680 R11: 0000000000000202 R12: ffff880472a51f08 R13: ffff880472a51f10 R14: 0000000000000000 R15: 0000000000000007 ORIG_RAX: ffffffffffffffff CS: 0010 SS: 0018 #7 [ffff880472a51f00] do_sys_times at ffffffff8108845d #8 [ffff880472a51f40] sys_times at ffffffff81088524 #9 [ffff880472a51f80] system_call_fastpath at ffffffff8100b0f2 RIP: 0000003808caac3a RSP: 00007fcba27ab6d8 RFLAGS: 00000202 RAX: 0000000000000064 RBX: ffffffff8100b0f2 RCX: 0000000000000000 RDX: 00007fcba27ab6e0 RSI: 000000000076d58e RDI: 00007fcba27ab6e0 RBP: 00007fcba27ab700 R8: 0000000000000020 R9: 000000000000091b R10: 00007fcba27ab680 R11: 0000000000000202 R12: 00007fff9ca41940 R13: 0000000000000000 R14: 00007fcba27ac9c0 R15: 00007fff9ca41940 ORIG_RAX: 0000000000000064 CS: 0033 SS: 002b Signed-off-by: Stanislaw Gruszka <[email protected]> Signed-off-by: Peter Zijlstra <[email protected]> Link: http://lkml.kernel.org/r/[email protected] Signed-off-by: Thomas Gleixner <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 0f342b9 commit ebad30a

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

kernel/sched/core.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3084,6 +3084,20 @@ void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
30843084
# define nsecs_to_cputime(__nsecs) nsecs_to_jiffies(__nsecs)
30853085
#endif
30863086

3087+
static cputime_t scale_utime(cputime_t utime, cputime_t rtime, cputime_t total)
3088+
{
3089+
u64 temp = (__force u64) rtime;
3090+
3091+
temp *= (__force u64) utime;
3092+
3093+
if (sizeof(cputime_t) == 4)
3094+
temp = div_u64(temp, (__force u32) total);
3095+
else
3096+
temp = div64_u64(temp, (__force u64) total);
3097+
3098+
return (__force cputime_t) temp;
3099+
}
3100+
30873101
void task_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
30883102
{
30893103
cputime_t rtime, utime = p->utime, total = utime + p->stime;
@@ -3093,13 +3107,9 @@ void task_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
30933107
*/
30943108
rtime = nsecs_to_cputime(p->se.sum_exec_runtime);
30953109

3096-
if (total) {
3097-
u64 temp = (__force u64) rtime;
3098-
3099-
temp *= (__force u64) utime;
3100-
do_div(temp, (__force u32) total);
3101-
utime = (__force cputime_t) temp;
3102-
} else
3110+
if (total)
3111+
utime = scale_utime(utime, rtime, total);
3112+
else
31033113
utime = rtime;
31043114

31053115
/*
@@ -3126,13 +3136,9 @@ void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
31263136
total = cputime.utime + cputime.stime;
31273137
rtime = nsecs_to_cputime(cputime.sum_exec_runtime);
31283138

3129-
if (total) {
3130-
u64 temp = (__force u64) rtime;
3131-
3132-
temp *= (__force u64) cputime.utime;
3133-
do_div(temp, (__force u32) total);
3134-
utime = (__force cputime_t) temp;
3135-
} else
3139+
if (total)
3140+
utime = scale_utime(cputime.utime, rtime, total);
3141+
else
31363142
utime = rtime;
31373143

31383144
sig->prev_utime = max(sig->prev_utime, utime);

0 commit comments

Comments
 (0)