Commit f06d0ca4 authored by Yamin Friedman's avatar Yamin Friedman Committed by David S. Miller

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: 398c2b05 ("linux/dim: Add completions count to dim_sample")
Signed-off-by: default avatarYamin Friedman <yaminf@mellanox.com>
Signed-off-by: default avatarLeon Romanovsky <leonro@mellanox.com>
Acked-by: default avatarSaeed Mahameed <saeedm@mellanox.com>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent c8ec4632
...@@ -992,7 +992,7 @@ static int bcm_sysport_poll(struct napi_struct *napi, int budget) ...@@ -992,7 +992,7 @@ static int bcm_sysport_poll(struct napi_struct *napi, int budget)
{ {
struct bcm_sysport_priv *priv = struct bcm_sysport_priv *priv =
container_of(napi, struct bcm_sysport_priv, napi); container_of(napi, struct bcm_sysport_priv, napi);
struct dim_sample dim_sample; struct dim_sample dim_sample = {};
unsigned int work_done = 0; unsigned int work_done = 0;
work_done = bcm_sysport_desc_rx(priv, budget); work_done = bcm_sysport_desc_rx(priv, budget);
......
...@@ -2136,7 +2136,7 @@ static int bnxt_poll(struct napi_struct *napi, int budget) ...@@ -2136,7 +2136,7 @@ static int bnxt_poll(struct napi_struct *napi, int budget)
} }
} }
if (bp->flags & BNXT_FLAG_DIM) { if (bp->flags & BNXT_FLAG_DIM) {
struct dim_sample dim_sample; struct dim_sample dim_sample = {};
dim_update_sample(cpr->event_ctr, dim_update_sample(cpr->event_ctr,
cpr->rx_packets, cpr->rx_packets,
......
...@@ -1895,7 +1895,7 @@ static int bcmgenet_rx_poll(struct napi_struct *napi, int budget) ...@@ -1895,7 +1895,7 @@ static int bcmgenet_rx_poll(struct napi_struct *napi, int budget)
{ {
struct bcmgenet_rx_ring *ring = container_of(napi, struct bcmgenet_rx_ring *ring = container_of(napi,
struct bcmgenet_rx_ring, napi); struct bcmgenet_rx_ring, napi);
struct dim_sample dim_sample; struct dim_sample dim_sample = {};
unsigned int work_done; unsigned int work_done;
work_done = bcmgenet_desc_rx(ring, budget); work_done = bcmgenet_desc_rx(ring, budget);
......
...@@ -49,7 +49,7 @@ static inline bool mlx5e_channel_no_affinity_change(struct mlx5e_channel *c) ...@@ -49,7 +49,7 @@ static inline bool mlx5e_channel_no_affinity_change(struct mlx5e_channel *c)
static void mlx5e_handle_tx_dim(struct mlx5e_txqsq *sq) static void mlx5e_handle_tx_dim(struct mlx5e_txqsq *sq)
{ {
struct mlx5e_sq_stats *stats = sq->stats; struct mlx5e_sq_stats *stats = sq->stats;
struct dim_sample dim_sample; struct dim_sample dim_sample = {};
if (unlikely(!test_bit(MLX5E_SQ_STATE_AM, &sq->state))) if (unlikely(!test_bit(MLX5E_SQ_STATE_AM, &sq->state)))
return; return;
...@@ -61,7 +61,7 @@ static void mlx5e_handle_tx_dim(struct mlx5e_txqsq *sq) ...@@ -61,7 +61,7 @@ static void mlx5e_handle_tx_dim(struct mlx5e_txqsq *sq)
static void mlx5e_handle_rx_dim(struct mlx5e_rq *rq) static void mlx5e_handle_rx_dim(struct mlx5e_rq *rq)
{ {
struct mlx5e_rq_stats *stats = rq->stats; struct mlx5e_rq_stats *stats = rq->stats;
struct dim_sample dim_sample; struct dim_sample dim_sample = {};
if (unlikely(!test_bit(MLX5E_RQ_STATE_AM, &rq->state))) if (unlikely(!test_bit(MLX5E_RQ_STATE_AM, &rq->state)))
return; return;
......
...@@ -74,8 +74,8 @@ void dim_calc_stats(struct dim_sample *start, struct dim_sample *end, ...@@ -74,8 +74,8 @@ void dim_calc_stats(struct dim_sample *start, struct dim_sample *end,
delta_us); delta_us);
curr_stats->cpms = DIV_ROUND_UP(ncomps * USEC_PER_MSEC, delta_us); curr_stats->cpms = DIV_ROUND_UP(ncomps * USEC_PER_MSEC, delta_us);
if (curr_stats->epms != 0) if (curr_stats->epms != 0)
curr_stats->cpe_ratio = curr_stats->cpe_ratio = DIV_ROUND_DOWN_ULL(
(curr_stats->cpms * 100) / curr_stats->epms; curr_stats->cpms * 100, curr_stats->epms);
else else
curr_stats->cpe_ratio = 0; curr_stats->cpe_ratio = 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment