Re: [PATCH] ptp_qoriq: fix overflow in ptp_qoriq_adjfine() u64 calcalation
From: Naresh Kamboju <hidden>
Date: 2021-03-25 16:33:53
FYI, On Tue, 23 Mar 2021 at 13:26, Yangbo Lu [off-list ref] wrote:
quoted hunk ↗ jump to hunk
Current calculation for diff of TMR_ADD register value may have 64-bit overflow in this code line, when long type scaled_ppm is large. adj *= scaled_ppm; This patch is to resolve it by using mul_u64_u64_div_u64(). Signed-off-by: Yangbo Lu <yangbo.lu@nxp.com> --- drivers/ptp/ptp_qoriq.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-)diff --git a/drivers/ptp/ptp_qoriq.c b/drivers/ptp/ptp_qoriq.c index 68beb1bd07c0..f7f220700cb5 100644 --- a/drivers/ptp/ptp_qoriq.c +++ b/drivers/ptp/ptp_qoriq.c@@ -189,15 +189,16 @@ int ptp_qoriq_adjfine(struct ptp_clock_info *ptp, long scaled_ppm) tmr_add = ptp_qoriq->tmr_add; adj = tmr_add; - /* calculate diff as adj*(scaled_ppm/65536)/1000000 - * and round() to the nearest integer + /* + * Calculate diff and round() to the nearest integer + * + * diff = adj * (ppb / 1000000000) + * = adj * scaled_ppm / 65536000000 */ - adj *= scaled_ppm; - diff = div_u64(adj, 8000000); - diff = (diff >> 13) + ((diff >> 12) & 1); + diff = mul_u64_u64_div_u64(adj, scaled_ppm, 32768000000);
While building Linux next 20210325 tag for powerpc architecture ppc6xx_defconfig failed due to following warnings / errors. - powerpc (ppc6xx_defconfig) with gcc-8 - powerpc (ppc6xx_defconfig) with gcc-9 - powerpc (ppc6xx_defconfig) with gcc-10 make --silent --keep-going --jobs=8 O=/home/tuxbuild/.cache/tuxmake/builds/1/tmp ARCH=powerpc CROSS_COMPILE=powerpc64le-linux-gnu- 'CC=sccache powerpc64le-linux-gnu-gcc' 'HOSTCC=sccache gcc' INFO: Uncompressed kernel (size 0xd985ec) overlaps the address of the wrapper(0x400000) INFO: Fixing the link_address of wrapper to (0xe00000) INFO: Uncompressed kernel (size 0xd985ec) overlaps the address of the wrapper(0x400000) INFO: Fixing the link_address of wrapper to (0xe00000) INFO: Uncompressed kernel (size 0xd985ec) overlaps the address of the wrapper(0x400000) INFO: Fixing the link_address of wrapper to (0xe00000) ERROR: modpost: "mul_u64_u64_div_u64" [drivers/ptp/ptp-qoriq.ko] undefined! Reported-by: Naresh Kamboju <redacted> Steps to reproduce: ------------------- # TuxMake is a command line tool and Python library that provides # portable and repeatable Linux kernel builds across a variety of # architectures, toolchains, kernel configurations, and make targets. # # TuxMake supports the concept of runtimes. # See https://docs.tuxmake.org/runtimes/, for that to work it requires # that you install podman or docker on your system. # # To install tuxmake on your system globally: # sudo pip3 install -U tuxmake # # See https://docs.tuxmake.org/ for complete documentation. tuxmake --runtime podman --target-arch powerpc --toolchain gcc-8 --kconfig ppc6xx_defconfig -- Linaro LKFT https://lkft.linaro.org