Skip to content

Commit f51d7bf

Browse files
yangbolu1991davem330
authored andcommitted
ptp_qoriq: fix overflow in ptp_qoriq_adjfine() u64 calcalation
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> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 6ab4c31 commit f51d7bf

1 file changed

Lines changed: 7 additions & 6 deletions

File tree

‎drivers/ptp/ptp_qoriq.c‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,15 +189,16 @@ int ptp_qoriq_adjfine(struct ptp_clock_info *ptp, long scaled_ppm)
189189
tmr_add = ptp_qoriq->tmr_add;
190190
adj = tmr_add;
191191

192-
/* calculate diff as adj*(scaled_ppm/65536)/1000000
193-
* and round() to the nearest integer
192+
/*
193+
* Calculate diff and round() to the nearest integer
194+
*
195+
* diff = adj * (ppb / 1000000000)
196+
* = adj * scaled_ppm / 65536000000
194197
*/
195-
adj *= scaled_ppm;
196-
diff = div_u64(adj, 8000000);
197-
diff = (diff >> 13) + ((diff >> 12) & 1);
198+
diff = mul_u64_u64_div_u64(adj, scaled_ppm, 32768000000);
199+
diff = DIV64_U64_ROUND_UP(diff, 2);
198200

199201
tmr_add = neg_adj ? tmr_add - diff : tmr_add + diff;
200-
201202
ptp_qoriq->write(&regs->ctrl_regs->tmr_add, tmr_add);
202203

203204
return 0;

0 commit comments

Comments
 (0)