diff options
author | Ryan Moeller <[email protected]> | 2020-09-08 14:39:16 -0400 |
---|---|---|
committer | Brian Behlendorf <[email protected]> | 2020-09-09 10:26:03 -0700 |
commit | 127daad223fe29f04975f4f4907480b0b54ab78a (patch) | |
tree | 7ae0da047cef0776236358186a61c58b1e8ffd8b | |
parent | b155a243a6c8a73a57d64534e21ef3593f6ac5e2 (diff) |
Avoid possibility of division by zero
When hz > 1000, msec / (1000 / hz) results in division by zero.
I found somewhere in FreeBSD using howmany(msec * hz, 1000) to convert
ms to ticks, avoiding the potential for a zero in the divisor.
Reviewed-by: Alexander Motin <[email protected]>
Reviewed-by: Brian Behlendorf <[email protected]>
Signed-off-by: Ryan Moeller <[email protected]>
Closes #10894
-rw-r--r-- | include/os/freebsd/zfs/sys/zfs_context_os.h | 2 | ||||
-rw-r--r-- | include/sys/zfs_context.h | 6 |
2 files changed, 4 insertions, 4 deletions
diff --git a/include/os/freebsd/zfs/sys/zfs_context_os.h b/include/os/freebsd/zfs/sys/zfs_context_os.h index b46c4aa7c..0316f93b2 100644 --- a/include/os/freebsd/zfs/sys/zfs_context_os.h +++ b/include/os/freebsd/zfs/sys/zfs_context_os.h @@ -72,7 +72,7 @@ extern struct mtx zfs_debug_mtx; } \ } while (0) -#define MSEC_TO_TICK(msec) ((msec) / (MILLISEC / hz)) +#define MSEC_TO_TICK(msec) (howmany((hrtime_t)(msec) * hz, MILLISEC)) extern int hz; extern int tick; typedef int fstrans_cookie_t; diff --git a/include/sys/zfs_context.h b/include/sys/zfs_context.h index 8e16399e8..e33f52c17 100644 --- a/include/sys/zfs_context.h +++ b/include/sys/zfs_context.h @@ -602,9 +602,9 @@ typedef struct vsecattr { extern void delay(clock_t ticks); #define SEC_TO_TICK(sec) ((sec) * hz) -#define MSEC_TO_TICK(msec) ((msec) / (MILLISEC / hz)) -#define USEC_TO_TICK(usec) ((usec) / (MICROSEC / hz)) -#define NSEC_TO_TICK(usec) ((usec) / (NANOSEC / hz)) +#define MSEC_TO_TICK(msec) (howmany((hrtime_t)(msec) * hz, MILLISEC)) +#define USEC_TO_TICK(usec) (howmany((hrtime_t)(usec) * hz, MICROSEC)) +#define NSEC_TO_TICK(nsec) (howmany((hrtime_t)(nsec) * hz, NANOSEC)) #define max_ncpus 64 #define boot_ncpus (sysconf(_SC_NPROCESSORS_ONLN)) |