From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754791AbbCIQ22 (ORCPT ); Mon, 9 Mar 2015 12:28:28 -0400 Received: from pandora.arm.linux.org.uk ([78.32.30.218]:33146 "EHLO pandora.arm.linux.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753536AbbCIQ2Z (ORCPT ); Mon, 9 Mar 2015 12:28:25 -0400 Date: Mon, 9 Mar 2015 16:28:09 +0000 From: Russell King - ARM Linux To: "pi-cheng.chen" Cc: Viresh Kumar , Matthias Brugger , Rob Herring , "Rafael J. Wysocki" , Thomas Petazzoni , Mark Rutland , devicetree@vger.kernel.org, linaro-kernel@lists.linaro.org, Mike Turquette , Pawel Moll , Ian Campbell , Catalin Marinas , linux-pm@vger.kernel.org, Will Deacon , linux-kernel@vger.kernel.org, fan.chen@mediatek.com, Howard Chen , Ashwin Chaugule , linux-mediatek@lists.infradead.org, Kumar Gala , "Joe.C" , Eddie Huang , linux-arm-kernel@lists.infradead.org Subject: Re: [PATCH v2 3/4] cpufreq: mediatek: add Mediatek cpufreq driver Message-ID: <20150309162809.GY8656@n2100.arm.linux.org.uk> References: <1425458956-20665-1-git-send-email-pi-cheng.chen@linaro.org> <1425458956-20665-4-git-send-email-pi-cheng.chen@linaro.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1425458956-20665-4-git-send-email-pi-cheng.chen@linaro.org> User-Agent: Mutt/1.5.23 (2014-03-12) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Mar 04, 2015 at 04:49:15PM +0800, pi-cheng.chen wrote: > +static int cpu_opp_table_get_freq_index(unsigned int freq) > +{ > + struct cpu_opp_table *opp_tbl = dvfs_info->opp_tbl; > + int i; > + > + for (i = 0; opp_tbl[i].freq != 0; i++) { > + if (opp_tbl[i].freq >= freq) > + return i; > + } > + > + return -1; My "return -1" detector fired on this... > +static int cpu_opp_table_get_volt_index(unsigned int volt) > +{ > + struct cpu_opp_table *opp_tbl = dvfs_info->opp_tbl; > + int i; > + > + for (i = 0; opp_tbl[i].vproc != -1; i++) > + if (opp_tbl[i].vproc >= volt) > + return i; > + > + return -1; And this. > +static int mtk_cpufreq_notify(struct notifier_block *nb, > + unsigned long action, void *data) > +{ > + struct cpufreq_freqs *freqs = data; > + struct cpu_opp_table *opp_tbl = dvfs_info->opp_tbl; > + int old_vproc, new_vproc, old_index, new_index; > + > + if (!cpumask_test_cpu(freqs->cpu, &dvfs_info->cpus)) > + return NOTIFY_DONE; > + > + old_vproc = regulator_get_voltage(dvfs_info->proc_reg); > + old_index = cpu_opp_table_get_volt_index(old_vproc); > + new_index = cpu_opp_table_get_freq_index(freqs->new * 1000); > + new_vproc = opp_tbl[new_index].vproc; Let's say that cpu_opp_table_get_freq_index() returns -1. We then do no error checking on this, and access the memory immediately preceding opp_tbl[0]. Since we'll be loading garbage from opp_tbl[-1], this probably means that mtk_cpufreq_voltage_trace() will go wrong. Your method of using the vproc values to work out which direction we should walk between old_index...new_index means that we could end up walking through almost the whole UINT_MAX range to wrap around to the new index. Yet again, "return -1" proves to be a sure sign of a bug. -- FTTC broadband for 0.8mile line: currently at 10.5Mbps down 400kbps up according to speedtest.net.