Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

hwmon: (occ) Fix division by zero issue

The code in occ_get_powr_avg() invokes div64_u64() without checking the
divisor. In case the divisor is zero, kernel gets an "Division by zero
in kernel" error.

Check the divisor and make it return 0 if the divisor is 0.

Fixes: c10e753d43eb ("hwmon (occ): Add sensor types and versions")
Signed-off-by: Lei YU <mine260309@gmail.com>
Reviewed-by: Eddie James <eajames@linux.ibm.com>
Link: https://lore.kernel.org/r/1562813088-23708-1-git-send-email-mine260309@gmail.com
Signed-off-by: Guenter Roeck <linux@roeck-us.net>

authored by

Lei YU and committed by
Guenter Roeck
211186ca 5f9e832c

+4 -2
+4 -2
drivers/hwmon/occ/common.c
··· 408 408 409 409 static u64 occ_get_powr_avg(u64 *accum, u32 *samples) 410 410 { 411 - return div64_u64(get_unaligned_be64(accum) * 1000000ULL, 412 - get_unaligned_be32(samples)); 411 + u64 divisor = get_unaligned_be32(samples); 412 + 413 + return (divisor == 0) ? 0 : 414 + div64_u64(get_unaligned_be64(accum) * 1000000ULL, divisor); 413 415 } 414 416 415 417 static ssize_t occ_show_power_2(struct device *dev,