Skip to content

Commit f06d0ca

Browse files
yaminfdavem330
authored andcommitted
linux/dim: Fix overflow in dim calculation
While using net_dim, a dim_sample was used without ever initializing the comps value. Added use of DIV_ROUND_DOWN_ULL() to prevent potential overflow, it should not be a problem to save the final result in an int because after the division by epms the value should not be larger than a few thousand. [ 1040.127124] UBSAN: Undefined behaviour in lib/dim/dim.c:78:23 [ 1040.130118] signed integer overflow: [ 1040.131643] 134718714 * 100 cannot be represented in type 'int' Fixes: 398c2b0 ("linux/dim: Add completions count to dim_sample") Signed-off-by: Yamin Friedman <[email protected]> Signed-off-by: Leon Romanovsky <[email protected]> Acked-by: Saeed Mahameed <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent c8ec463 commit f06d0ca

File tree

5 files changed

+7
-7
lines changed

5 files changed

+7
-7
lines changed

drivers/net/ethernet/broadcom/bcmsysport.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,7 @@ static int bcm_sysport_poll(struct napi_struct *napi, int budget)
992992
{
993993
struct bcm_sysport_priv *priv =
994994
container_of(napi, struct bcm_sysport_priv, napi);
995-
struct dim_sample dim_sample;
995+
struct dim_sample dim_sample = {};
996996
unsigned int work_done = 0;
997997

998998
work_done = bcm_sysport_desc_rx(priv, budget);

drivers/net/ethernet/broadcom/bnxt/bnxt.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2136,7 +2136,7 @@ static int bnxt_poll(struct napi_struct *napi, int budget)
21362136
}
21372137
}
21382138
if (bp->flags & BNXT_FLAG_DIM) {
2139-
struct dim_sample dim_sample;
2139+
struct dim_sample dim_sample = {};
21402140

21412141
dim_update_sample(cpr->event_ctr,
21422142
cpr->rx_packets,

drivers/net/ethernet/broadcom/genet/bcmgenet.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1895,7 +1895,7 @@ static int bcmgenet_rx_poll(struct napi_struct *napi, int budget)
18951895
{
18961896
struct bcmgenet_rx_ring *ring = container_of(napi,
18971897
struct bcmgenet_rx_ring, napi);
1898-
struct dim_sample dim_sample;
1898+
struct dim_sample dim_sample = {};
18991899
unsigned int work_done;
19001900

19011901
work_done = bcmgenet_desc_rx(ring, budget);

drivers/net/ethernet/mellanox/mlx5/core/en_txrx.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ static inline bool mlx5e_channel_no_affinity_change(struct mlx5e_channel *c)
4949
static void mlx5e_handle_tx_dim(struct mlx5e_txqsq *sq)
5050
{
5151
struct mlx5e_sq_stats *stats = sq->stats;
52-
struct dim_sample dim_sample;
52+
struct dim_sample dim_sample = {};
5353

5454
if (unlikely(!test_bit(MLX5E_SQ_STATE_AM, &sq->state)))
5555
return;
@@ -61,7 +61,7 @@ static void mlx5e_handle_tx_dim(struct mlx5e_txqsq *sq)
6161
static void mlx5e_handle_rx_dim(struct mlx5e_rq *rq)
6262
{
6363
struct mlx5e_rq_stats *stats = rq->stats;
64-
struct dim_sample dim_sample;
64+
struct dim_sample dim_sample = {};
6565

6666
if (unlikely(!test_bit(MLX5E_RQ_STATE_AM, &rq->state)))
6767
return;

lib/dim/dim.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,8 @@ void dim_calc_stats(struct dim_sample *start, struct dim_sample *end,
7474
delta_us);
7575
curr_stats->cpms = DIV_ROUND_UP(ncomps * USEC_PER_MSEC, delta_us);
7676
if (curr_stats->epms != 0)
77-
curr_stats->cpe_ratio =
78-
(curr_stats->cpms * 100) / curr_stats->epms;
77+
curr_stats->cpe_ratio = DIV_ROUND_DOWN_ULL(
78+
curr_stats->cpms * 100, curr_stats->epms);
7979
else
8080
curr_stats->cpe_ratio = 0;
8181

0 commit comments

Comments
 (0)