Received: by 10.223.185.116 with SMTP id b49csp300218wrg; Tue, 20 Feb 2018 21:41:17 -0800 (PST) X-Google-Smtp-Source: AH8x2254tTjEIUxLZGMKUjniWdrJMEhjT2JV+Ag2bey2vQ5a6Ez4bFO0wm8w87jqM63EIW5mAuia X-Received: by 2002:a17:902:595d:: with SMTP id e29-v6mr2038014plj.189.1519191677304; Tue, 20 Feb 2018 21:41:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1519191677; cv=none; d=google.com; s=arc-20160816; b=mI8pfiNW7rkwjWDSTvxLY5OVhV1arBjRH+L6MgrLgP0plvEFM5cH2ecKMtJfgWirt2 6Tib0InjeCBXpSmCvYMhQbzVrC2pDFec82qxLiL/PvF4OcjkZePovGnArdBCZpA2m8Eo 7yyl+nA5S9SdYaY3y00uaWOCog9sIaCj+rJLbaSdLUg9HzltNvwHlpQ/zQ5SHqHKzIvp bDng+va+gT/cNPw0nHtS1MLngvVOznHpdDwTjVc0IPDYo1nIl1CIJF6IGQcXPPuwzbPw YghyL6IdVvP51dLNV39DUARWjazl+U2JN9e03qBQJ7A/etxFcuLMe+Mb5Caeq+cCM8Vw sIGQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:references :in-reply-to:subject:cc:to:from:arc-authentication-results; bh=bPZUCMMBM03jJGtlLLqoR9OfrRKPNzwHnhETVnsujKk=; b=D4rruy+V1BYpyL27kCABG5u9wodiQjKqqIBHBtEGTvyrQYMRhcWSYPi8XcYJpoW+NX ItTsF/l0o17XiJVqrflO87CAb3j3pLz2DGejV7aJ8trPNlI5ugOMqASOyzfsiV2qtl4G FPOvoggGGiTjWJaUY2ewDHalISnG/n2Gd+RXSXdTKy9BQCdtHhe8tbLAF+r/SRIm0+Uq L5bVYhhopclSiIjvdxss6Wdc2Ro2XoSk7h7OnPPEoi71S5F1xjyIxQOTnkfwsZEbO62U 2r1iUrSzj+xDh8lcfOLTSwjdIE5JLeOKQUeCu8xij7xobSUnkPwVNWltRmM9t0mggc9K IhzA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id r13-v6si1116816plj.141.2018.02.20.21.41.01; Tue, 20 Feb 2018 21:41:17 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751534AbeBUFkA (ORCPT + 99 others); Wed, 21 Feb 2018 00:40:00 -0500 Received: from ozlabs.org ([103.22.144.67]:33837 "EHLO ozlabs.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750811AbeBUFj6 (ORCPT ); Wed, 21 Feb 2018 00:39:58 -0500 Received: from authenticated.ozlabs.org (localhost [127.0.0.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPSA id 3zmRC11Hqbz9ryC; Wed, 21 Feb 2018 16:39:57 +1100 (AEDT) From: Michael Ellerman To: Viresh Kumar , Shilpasri G Bhat Cc: rjw@rjwysocki.net, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org Subject: Re: [PATCH] cpufreq: powernv: Check negative value returned by cpufreq_table_find_index_dl() In-Reply-To: <20180212102900.GU28462@vireshk-i7> References: <1518430876-24464-1-git-send-email-shilpa.bhat@linux.vnet.ibm.com> <20180212102900.GU28462@vireshk-i7> Date: Wed, 21 Feb 2018 16:39:54 +1100 Message-ID: <874lmasxxx.fsf@concordia.ellerman.id.au> MIME-Version: 1.0 Content-Type: text/plain Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Viresh Kumar writes: > On 12-02-18, 15:51, Shilpasri G Bhat wrote: >> This patch fixes the below Coverity warning: >> >> *** CID 182816: Memory - illegal accesses (NEGATIVE_RETURNS) >> /drivers/cpufreq/powernv-cpufreq.c: 1008 in powernv_fast_switch() >> 1002 unsigned int target_freq) >> 1003 { >> 1004 int index; >> 1005 struct powernv_smp_call_data freq_data; >> 1006 >> 1007 index = cpufreq_table_find_index_dl(policy, target_freq); >> >>> CID 182816: Memory - illegal accesses (NEGATIVE_RETURNS) >> >>> Using variable "index" as an index to array "powernv_freqs". >> 1008 freq_data.pstate_id = powernv_freqs[index].driver_data; >> 1009 freq_data.gpstate_id = powernv_freqs[index].driver_data; >> 1010 set_pstate(&freq_data); >> 1011 >> 1012 return powernv_freqs[index].frequency; >> 1013 } >> >> Signed-off-by: Shilpasri G Bhat >> --- >> drivers/cpufreq/powernv-cpufreq.c | 3 +++ >> 1 file changed, 3 insertions(+) >> >> diff --git a/drivers/cpufreq/powernv-cpufreq.c b/drivers/cpufreq/powernv-cpufreq.c >> index 29cdec1..69edfe9 100644 >> --- a/drivers/cpufreq/powernv-cpufreq.c >> +++ b/drivers/cpufreq/powernv-cpufreq.c >> @@ -1005,6 +1005,9 @@ static unsigned int powernv_fast_switch(struct cpufreq_policy *policy, >> struct powernv_smp_call_data freq_data; >> >> index = cpufreq_table_find_index_dl(policy, target_freq); >> + if (unlikely(index < 0)) >> + index = get_nominal_index(); >> + > > AFAICT, you will get -1 here only if the freq table had no valid > frequencies (or the freq table is empty). Why would that happen ? Bugs? Or if you ask for a target_freq that is higher than anything in the table. Or the API changes, and we forget to update this call site. If you're saying that cpufreq_table_find_index_dl() can NEVER fail, then write it so that it can never fail and change it to return unsigned int. Having it potentially return -1, which is then used to index an array and not handling that is just asking for bugs to happen. cheers