DPDK patches and discussions
 help / color / mirror / Atom feed
From: Reshma Pattan <reshma.pattan@intel.com>
To: dev@dpdk.org
Cc: Reshma Pattan <reshma.pattan@intel.com>,
	david.hunt@intel.com, stable@dpdk.org
Subject: [dpdk-dev] [PATCH] examples/vm_power_manager: fix overflowed value
Date: Thu, 11 Apr 2019 12:13:36 +0100	[thread overview]
Message-ID: <20190411111336.2790-1-reshma.pattan@intel.com> (raw)
Message-ID: <20190411111336._Wnueag3xN6kyDu0wty9dOSykIoBc6gbkvXFW8PUe-I@z> (raw)

Fix the data type of last_branches, last_branch_misses
from unit32_t to uint64_t, and for hits_diff, miss_diff
from int to int64_t respectively to fix possible
overflow or truncation.

Coverity issue: 337677
Fixes: 4b1a631b8a ("examples/vm_power: add oob monitoring functions")
CC: david.hunt@intel.com
CC: stable@dpdk.org

Signed-off-by: Reshma Pattan <reshma.pattan@intel.com>
---
 examples/vm_power_manager/oob_monitor_x86.c | 23 ++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/examples/vm_power_manager/oob_monitor_x86.c b/examples/vm_power_manager/oob_monitor_x86.c
index 589c604e5..ebd96b205 100644
--- a/examples/vm_power_manager/oob_monitor_x86.c
+++ b/examples/vm_power_manager/oob_monitor_x86.c
@@ -33,10 +33,10 @@ static float
 apply_policy(int core)
 {
 	struct core_info *ci;
-	uint64_t counter;
+	uint64_t counter = 0;
 	uint64_t branches, branch_misses;
-	uint32_t last_branches, last_branch_misses;
-	int hits_diff, miss_diff;
+	uint64_t last_branches, last_branch_misses;
+	int64_t hits_diff, miss_diff;
 	float ratio;
 	int ret;
 
@@ -54,6 +54,7 @@ apply_policy(int core)
 				core);
 	branches = counter;
 
+	counter = 0;
 	ret = pread(ci->cd[core].msr_fd, &counter,
 			sizeof(counter), IA32_PERFCTR1);
 	if (ret < 0)
@@ -66,13 +67,25 @@ apply_policy(int core)
 	ci->cd[core].last_branches = branches;
 	ci->cd[core].last_branch_misses = branch_misses;
 
-	hits_diff = (int)branches - (int)last_branches;
+	/*
+	 * Intentional right shift to make MSB 0 to avoid
+	 * possible signed overflow or truncation.
+	 */
+	branches >>= 1;
+	last_branches >>= 1;
+	hits_diff = (int64_t)branches - (int64_t)last_branches;
 	if (hits_diff <= 0) {
 		/* Likely a counter overflow condition, skip this round */
 		return -1.0;
 	}
 
-	miss_diff = (int)branch_misses - (int)last_branch_misses;
+	/*
+	 * Intentional right shift to make MSB 0 to avoid
+	 * possible signed overflow or truncation.
+	 */
+	branch_misses >>= 1;
+	last_branch_misses >>= 1;
+	miss_diff = (int64_t)branch_misses - (int64_t)last_branch_misses;
 	if (miss_diff <= 0) {
 		/* Likely a counter overflow condition, skip this round */
 		return -1.0;
-- 
2.17.1


             reply	other threads:[~2019-04-11 11:13 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-04-11 11:13 Reshma Pattan [this message]
2019-04-11 11:13 ` Reshma Pattan
2019-04-11 12:07 ` Burakov, Anatoly
2019-04-11 12:07   ` Burakov, Anatoly
2019-04-22 21:17   ` Thomas Monjalon
2019-04-22 21:17     ` Thomas Monjalon

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20190411111336.2790-1-reshma.pattan@intel.com \
    --to=reshma.pattan@intel.com \
    --cc=david.hunt@intel.com \
    --cc=dev@dpdk.org \
    --cc=stable@dpdk.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).