Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2488156pxj; Mon, 10 May 2021 04:18:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx/EvaXerE0u0CWn5YcOnQSdzYEvb2eQ5i07sGrazT0XHtw3LPtoE1bIpiWWArrDWfUXqVn X-Received: by 2002:aa7:d4d5:: with SMTP id t21mr15510166edr.95.1620645518547; Mon, 10 May 2021 04:18:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620645518; cv=none; d=google.com; s=arc-20160816; b=XwStGZwJBf2EaiwFmCwjun0UImsMwh1zfA6NQ7qJqYPkLMIBj55POhncR/D3AjK5pR CaqTWOmB6+NUui1pXjXorMwdhkJiItumh12s5Tl9fmannspQblpoV7/tCaSSFSTssPBs pkaHAej3pIcQmEjQlwBOJqg02kyl0zfZYiXU2airlgba0znvIo+yICyXTVwDTFvFKODG rgfS6C6tp/HdZrGD/Yi58W13INWUesVngRQpuVjTMYCFCK9of3d8t25c7LHPXBVvzCuI H39lerDDmUrmD1aJV4Jeo5DrnLd3k0oPZvUtqSWQnH9j/L3uPAbpL/o+6kW0llIIBMEg 0O+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=mEmc8tfTmsSKNOkMw11iX17l2zxPncDaYvuEjxlQEz0=; b=q2jCwyqgQh3AL0YXVpxBxe474/qCKPlCccWtcAZilliNhEHIvR/bu977yLWOychAaD U8LdrM3Nb49JI3i1yVWcULI93hBAoT9SCQQTKSGzwk55ev7ycGQ1nm7LHiLvEFriXqF6 rKCdFb95LKUD41/UVIdDdCv4flpmpjkeWmDg7r7jfk+RRZqYEpEHS+XTc7A3nDYVHlAg H/YPf+yQGQZKrScVlCYT62RLbvmFf1FBKoKKX8QC/2VE0vn4uJAhfHtSTT/J49NmaxHy sQXXn+22VFM4lfR6688IjdYAyosT5f9fGsxGYpC+Ziet6nqES6SwYmNLYWPJbwUDyf7i 1Vgw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=XJVCqrMh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id b15si12653353eds.133.2021.05.10.04.18.15; Mon, 10 May 2021 04:18:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=XJVCqrMh; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236965AbhEJLLG (ORCPT + 99 others); Mon, 10 May 2021 07:11:06 -0400 Received: from mail.kernel.org ([198.145.29.99]:41486 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233521AbhEJKuL (ORCPT ); Mon, 10 May 2021 06:50:11 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id DA085619EE; Mon, 10 May 2021 10:39:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620643153; bh=ac/ECm5ssDNwfZVOirucWzBGrSsQR/PGlfv7yDn6bOs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XJVCqrMhsMTjTnxonD3ne2dRv7NLpD0gEaryw69a97dR/Udls/ki7oJTe4lvogAxR aFHjX0TMXrorIKkwS68SOptUTXSXi0x5kZ3NNU01pfl55X3xRX9AxSqVkjVSkfwKf4 tkdQPgsGCTORaKWcmK4icvFsJ84VDuUngdESY5Is= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dan Carpenter , Babu Moger , Fenghua Yu , Shuah Khan , Sasha Levin Subject: [PATCH 5.10 164/299] selftests/resctrl: Fix checking for < 0 for unsigned values Date: Mon, 10 May 2021 12:19:21 +0200 Message-Id: <20210510102010.371693121@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210510102004.821838356@linuxfoundation.org> References: <20210510102004.821838356@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Fenghua Yu [ Upstream commit 1205b688c92558a04d8dd4cbc2b213e0fceba5db ] Dan reported following static checker warnings tools/testing/selftests/resctrl/resctrl_val.c:545 measure_vals() warn: 'bw_imc' unsigned <= 0 tools/testing/selftests/resctrl/resctrl_val.c:549 measure_vals() warn: 'bw_resc_end' unsigned <= 0 These warnings are reported because 1. measure_vals() declares 'bw_imc' and 'bw_resc_end' as unsigned long variables 2. Return value of get_mem_bw_imc() and get_mem_bw_resctrl() are assigned to 'bw_imc' and 'bw_resc_end' respectively 3. The returned values are checked for <= 0 to see if the calls failed Checking for < 0 for an unsigned value doesn't make any sense. Fix this issue by changing the implementation of get_mem_bw_imc() and get_mem_bw_resctrl() such that they now accept reference to a variable and set the variable appropriately upon success and return 0, else return < 0 on error. Reported-by: Dan Carpenter Tested-by: Babu Moger Signed-off-by: Fenghua Yu Signed-off-by: Shuah Khan Signed-off-by: Sasha Levin --- tools/testing/selftests/resctrl/resctrl_val.c | 41 +++++++++++-------- 1 file changed, 23 insertions(+), 18 deletions(-) diff --git a/tools/testing/selftests/resctrl/resctrl_val.c b/tools/testing/selftests/resctrl/resctrl_val.c index 5478c23c62ba..8df557894059 100644 --- a/tools/testing/selftests/resctrl/resctrl_val.c +++ b/tools/testing/selftests/resctrl/resctrl_val.c @@ -300,9 +300,9 @@ static int initialize_mem_bw_imc(void) * Memory B/W utilized by a process on a socket can be calculated using * iMC counters. Perf events are used to read these counters. * - * Return: >= 0 on success. < 0 on failure. + * Return: = 0 on success. < 0 on failure. */ -static float get_mem_bw_imc(int cpu_no, char *bw_report) +static int get_mem_bw_imc(int cpu_no, char *bw_report, float *bw_imc) { float reads, writes, of_mul_read, of_mul_write; int imc, j, ret; @@ -373,13 +373,18 @@ static float get_mem_bw_imc(int cpu_no, char *bw_report) close(imc_counters_config[imc][WRITE].fd); } - if (strcmp(bw_report, "reads") == 0) - return reads; + if (strcmp(bw_report, "reads") == 0) { + *bw_imc = reads; + return 0; + } - if (strcmp(bw_report, "writes") == 0) - return writes; + if (strcmp(bw_report, "writes") == 0) { + *bw_imc = writes; + return 0; + } - return (reads + writes); + *bw_imc = reads + writes; + return 0; } void set_mbm_path(const char *ctrlgrp, const char *mongrp, int resource_id) @@ -438,9 +443,8 @@ static void initialize_mem_bw_resctrl(const char *ctrlgrp, const char *mongrp, * 1. If con_mon grp is given, then read from it * 2. If con_mon grp is not given, then read from root con_mon grp */ -static unsigned long get_mem_bw_resctrl(void) +static int get_mem_bw_resctrl(unsigned long *mbm_total) { - unsigned long mbm_total = 0; FILE *fp; fp = fopen(mbm_total_path, "r"); @@ -449,7 +453,7 @@ static unsigned long get_mem_bw_resctrl(void) return -1; } - if (fscanf(fp, "%lu", &mbm_total) <= 0) { + if (fscanf(fp, "%lu", mbm_total) <= 0) { perror("Could not get mbm local bytes"); fclose(fp); @@ -457,7 +461,7 @@ static unsigned long get_mem_bw_resctrl(void) } fclose(fp); - return mbm_total; + return 0; } pid_t bm_pid, ppid; @@ -549,7 +553,8 @@ static void initialize_llc_occu_resctrl(const char *ctrlgrp, const char *mongrp, static int measure_vals(struct resctrl_val_param *param, unsigned long *bw_resc_start) { - unsigned long bw_imc, bw_resc, bw_resc_end; + unsigned long bw_resc, bw_resc_end; + float bw_imc; int ret; /* @@ -559,13 +564,13 @@ measure_vals(struct resctrl_val_param *param, unsigned long *bw_resc_start) * Compare the two values to validate resctrl value. * It takes 1sec to measure the data. */ - bw_imc = get_mem_bw_imc(param->cpu_no, param->bw_report); - if (bw_imc <= 0) - return bw_imc; + ret = get_mem_bw_imc(param->cpu_no, param->bw_report, &bw_imc); + if (ret < 0) + return ret; - bw_resc_end = get_mem_bw_resctrl(); - if (bw_resc_end <= 0) - return bw_resc_end; + ret = get_mem_bw_resctrl(&bw_resc_end); + if (ret < 0) + return ret; bw_resc = (bw_resc_end - *bw_resc_start) / MB; ret = print_results_bw(param->filename, bm_pid, bw_imc, bw_resc); -- 2.30.2