Skip to content

Commit 18d1ce6

Browse files
Stanislaw Gruszkajustin0406
authored andcommitted
sched: fix divide by zero at {thread_group,task}_times
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 brothaedhung#1 [ffff880472a51bd0] crash_kexec at ffffffff810b91c2 brothaedhung#2 [ffff880472a51ca0] oops_end at ffffffff814f0b00 brothaedhung#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 <sgruszka@redhat.com> Signed-off-by: Peter Zijlstra <a.p.zijlstra@chello.nl> Link: http://lkml.kernel.org/r/20120808092714.GA3580@redhat.com Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Pranav Vashi <neobuddy89@gmail.com>
1 parent dc17c6e commit 18d1ce6

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
@@ -3231,6 +3231,20 @@ void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
32313231
# define nsecs_to_cputime(__nsecs) nsecs_to_jiffies(__nsecs)
32323232
#endif
32333233

3234+
static cputime_t scale_utime(cputime_t utime, cputime_t rtime, cputime_t total)
3235+
{
3236+
u64 temp = (__force u64) rtime;
3237+
3238+
temp *= (__force u64) utime;
3239+
3240+
if (sizeof(cputime_t) == 4)
3241+
temp = div_u64(temp, (__force u32) total);
3242+
else
3243+
temp = div64_u64(temp, (__force u64) total);
3244+
3245+
return (__force cputime_t) temp;
3246+
}
3247+
32343248
void task_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
32353249
{
32363250
cputime_t rtime, utime = p->utime, total = utime + p->stime;
@@ -3240,13 +3254,9 @@ void task_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
32403254
*/
32413255
rtime = nsecs_to_cputime(p->se.sum_exec_runtime);
32423256

3243-
if (total) {
3244-
u64 temp = (__force u64) rtime;
3245-
3246-
temp *= (__force u64) utime;
3247-
do_div(temp, (__force u32) total);
3248-
utime = (__force cputime_t) temp;
3249-
} else
3257+
if (total)
3258+
utime = scale_utime(utime, rtime, total);
3259+
else
32503260
utime = rtime;
32513261

32523262
/*
@@ -3273,13 +3283,9 @@ void thread_group_times(struct task_struct *p, cputime_t *ut, cputime_t *st)
32733283
total = cputime.utime + cputime.stime;
32743284
rtime = nsecs_to_cputime(cputime.sum_exec_runtime);
32753285

3276-
if (total) {
3277-
u64 temp = (__force u64) rtime;
3278-
3279-
temp *= (__force u64) cputime.utime;
3280-
do_div(temp, (__force u32) total);
3281-
utime = (__force cputime_t) temp;
3282-
} else
3286+
if (total)
3287+
utime = scale_utime(cputime.utime, rtime, total);
3288+
else
32833289
utime = rtime;
32843290

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

0 commit comments

Comments
 (0)