From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 018CD1C7015; Wed, 16 Apr 2025 15:26:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744817168; cv=none; b=BeHLO/RdVrVQwRX3vSMYJCK0HE1YrtoByd7nuGKS9DdvfYk0HOHAn4XA7on/r+NQURkbAF4PNujbgqKtjLflJ4eqIhRUHSPDal/4qjHAZ/v1nz+rTsL++ORMLAvHvMHF9AdiBzVWiWAEfG2W1kO3eKlwWfw8Eb0ye70j2cRt6MQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1744817168; c=relaxed/simple; bh=xuApqJIpg/A6yzMAM3bP2JnbdsNFyPu87tU3HFOJVPw=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=k3xKm7R5IO4+lRfEufFI5LJbxphnshFIP4JuYj3ToKlEfyzPZeAv2EkZFg1vqNKszODgtHsLwn7fcU/4U8nAu2mc4yIJvrjittz5Z6v6VigyzD6OSreNuPEkHdzkcYPyUSe26keQrPfrbnSe6L4P/oRo1iHNDuSo36ZiHgbySW0= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 1347A1595; Wed, 16 Apr 2025 08:26:04 -0700 (PDT) Received: from [10.57.71.18] (unknown [10.57.71.18]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 3C57C3F66E; Wed, 16 Apr 2025 08:26:05 -0700 (PDT) Message-ID: Date: Wed, 16 Apr 2025 16:26:03 +0100 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v3] PM: EM: Fix potential division-by-zero error in em_compute_costs() To: "Rafael J. Wysocki" Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Yaxiong Tian , Yaxiong Tian References: Content-Language: en-US From: Lukasz Luba In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 4/16/25 12:58, Rafael J. Wysocki wrote: > On Wed, Apr 16, 2025 at 4:57 AM Yaxiong Tian wrote: >> >> 在 2025/4/16 01:17, Rafael J. Wysocki 写道: >>> On Mon, Apr 14, 2025 at 11:09 AM Yaxiong Tian wrote: >>>> >>>> From: Yaxiong Tian >>>> >>>> When the device is of a non-CPU type, table[i].performance won't be >>>> initialized in the previous em_init_performance(), resulting in division >>>> by zero when calculating costs in em_compute_costs(). >>>> >>>> Since the 'cost' algorithm is only used for EAS energy efficiency >>>> calculations and is currently not utilized by other device drivers, we >>>> should add the _is_cpu_device(dev) check to prevent this division-by-zero >>>> issue. >>>> >>>> Fixes: <1b600da51073> ("PM: EM: Optimize em_cpu_energy() and remove division") >>> >>> Please look at the Fixes: tags in the kernel git history. They don't >>> look like the one above. >>> >> Yes, there's an extra '<>' here. >> >>>> Signed-off-by: Yaxiong Tian >>>> --- >>>> kernel/power/energy_model.c | 4 ++-- >>>> 1 file changed, 2 insertions(+), 2 deletions(-) >>>> >>>> diff --git a/kernel/power/energy_model.c b/kernel/power/energy_model.c >>>> index d9b7e2b38c7a..fc972cc1fc12 100644 >>>> --- a/kernel/power/energy_model.c >>>> +++ b/kernel/power/energy_model.c >>>> @@ -235,7 +235,7 @@ static int em_compute_costs(struct device *dev, struct em_perf_state *table, >>>> >>>> /* Compute the cost of each performance state. */ >>>> for (i = nr_states - 1; i >= 0; i--) { >>>> - unsigned long power_res, cost; >>>> + unsigned long power_res, cost = 0; >>>> >>>> if ((flags & EM_PERF_DOMAIN_ARTIFICIAL) && cb->get_cost) { >>>> ret = cb->get_cost(dev, table[i].frequency, &cost); >>>> @@ -244,7 +244,7 @@ static int em_compute_costs(struct device *dev, struct em_perf_state *table, >>>> cost, ret); >>>> return -EINVAL; >>>> } >>>> - } else { >>>> + } else if (_is_cpu_device(dev)) { >>> >>> Can't you just check this upfront at the beginning of the function and >>> make it bail out if dev is not a CPU device? >>> >> Sure, But the current implementation applies em_compute_costs() to both >> non-CPU devices and CPU devices. > > Maybe it shouldn't do that for non-CPU ones? It shouldn't call this cost computation for non-CPU devices. Let me check that.