Skip to content

Commit

Permalink
Avoid possibility of division by zero
Browse files Browse the repository at this point in the history
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 <mav@FreeBSD.org>
Reviewed-by: Brian Behlendorf <behlendorf1@llnl.gov>
Signed-off-by: Ryan Moeller <freqlabs@FreeBSD.org>
Closes #10894
  • Loading branch information
Ryan Moeller authored Sep 8, 2020
1 parent 189272f commit ebc4b52
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion include/os/freebsd/zfs/sys/zfs_context_os.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions include/sys/zfs_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit ebc4b52

Please sign in to comment.