mirror of
				https://github.com/torvalds/linux.git
				synced 2025-11-04 02:30:34 +02:00 
			
		
		
		
	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: 398c2b05bb ("linux/dim: Add completions count to dim_sample")
Signed-off-by: Yamin Friedman <yaminf@mellanox.com>
Signed-off-by: Leon Romanovsky <leonro@mellanox.com>
Acked-by: Saeed Mahameed <saeedm@mellanox.com>
Signed-off-by: David S. Miller <davem@davemloft.net>
		
	
			
		
			
				
	
	
		
			83 lines
		
	
	
	
		
			2.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			83 lines
		
	
	
	
		
			2.1 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
// SPDX-License-Identifier: GPL-2.0 OR Linux-OpenIB
 | 
						|
/*
 | 
						|
 * Copyright (c) 2019, Mellanox Technologies inc.  All rights reserved.
 | 
						|
 */
 | 
						|
 | 
						|
#include <linux/dim.h>
 | 
						|
 | 
						|
bool dim_on_top(struct dim *dim)
 | 
						|
{
 | 
						|
	switch (dim->tune_state) {
 | 
						|
	case DIM_PARKING_ON_TOP:
 | 
						|
	case DIM_PARKING_TIRED:
 | 
						|
		return true;
 | 
						|
	case DIM_GOING_RIGHT:
 | 
						|
		return (dim->steps_left > 1) && (dim->steps_right == 1);
 | 
						|
	default: /* DIM_GOING_LEFT */
 | 
						|
		return (dim->steps_right > 1) && (dim->steps_left == 1);
 | 
						|
	}
 | 
						|
}
 | 
						|
EXPORT_SYMBOL(dim_on_top);
 | 
						|
 | 
						|
void dim_turn(struct dim *dim)
 | 
						|
{
 | 
						|
	switch (dim->tune_state) {
 | 
						|
	case DIM_PARKING_ON_TOP:
 | 
						|
	case DIM_PARKING_TIRED:
 | 
						|
		break;
 | 
						|
	case DIM_GOING_RIGHT:
 | 
						|
		dim->tune_state = DIM_GOING_LEFT;
 | 
						|
		dim->steps_left = 0;
 | 
						|
		break;
 | 
						|
	case DIM_GOING_LEFT:
 | 
						|
		dim->tune_state = DIM_GOING_RIGHT;
 | 
						|
		dim->steps_right = 0;
 | 
						|
		break;
 | 
						|
	}
 | 
						|
}
 | 
						|
EXPORT_SYMBOL(dim_turn);
 | 
						|
 | 
						|
void dim_park_on_top(struct dim *dim)
 | 
						|
{
 | 
						|
	dim->steps_right  = 0;
 | 
						|
	dim->steps_left   = 0;
 | 
						|
	dim->tired        = 0;
 | 
						|
	dim->tune_state   = DIM_PARKING_ON_TOP;
 | 
						|
}
 | 
						|
EXPORT_SYMBOL(dim_park_on_top);
 | 
						|
 | 
						|
void dim_park_tired(struct dim *dim)
 | 
						|
{
 | 
						|
	dim->steps_right  = 0;
 | 
						|
	dim->steps_left   = 0;
 | 
						|
	dim->tune_state   = DIM_PARKING_TIRED;
 | 
						|
}
 | 
						|
EXPORT_SYMBOL(dim_park_tired);
 | 
						|
 | 
						|
void dim_calc_stats(struct dim_sample *start, struct dim_sample *end,
 | 
						|
		    struct dim_stats *curr_stats)
 | 
						|
{
 | 
						|
	/* u32 holds up to 71 minutes, should be enough */
 | 
						|
	u32 delta_us = ktime_us_delta(end->time, start->time);
 | 
						|
	u32 npkts = BIT_GAP(BITS_PER_TYPE(u32), end->pkt_ctr, start->pkt_ctr);
 | 
						|
	u32 nbytes = BIT_GAP(BITS_PER_TYPE(u32), end->byte_ctr,
 | 
						|
			     start->byte_ctr);
 | 
						|
	u32 ncomps = BIT_GAP(BITS_PER_TYPE(u32), end->comp_ctr,
 | 
						|
			     start->comp_ctr);
 | 
						|
 | 
						|
	if (!delta_us)
 | 
						|
		return;
 | 
						|
 | 
						|
	curr_stats->ppms = DIV_ROUND_UP(npkts * USEC_PER_MSEC, delta_us);
 | 
						|
	curr_stats->bpms = DIV_ROUND_UP(nbytes * USEC_PER_MSEC, delta_us);
 | 
						|
	curr_stats->epms = DIV_ROUND_UP(DIM_NEVENTS * USEC_PER_MSEC,
 | 
						|
					delta_us);
 | 
						|
	curr_stats->cpms = DIV_ROUND_UP(ncomps * USEC_PER_MSEC, delta_us);
 | 
						|
	if (curr_stats->epms != 0)
 | 
						|
		curr_stats->cpe_ratio = DIV_ROUND_DOWN_ULL(
 | 
						|
			curr_stats->cpms * 100, curr_stats->epms);
 | 
						|
	else
 | 
						|
		curr_stats->cpe_ratio = 0;
 | 
						|
 | 
						|
}
 | 
						|
EXPORT_SYMBOL(dim_calc_stats);
 |