From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm1-f50.google.com (mail-wm1-f50.google.com [209.85.128.50]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5F4AC2F3B for ; Thu, 23 Jan 2025 13:04:19 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.50 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737637461; cv=none; b=ZoumZT4kqjFOnfsQmALHbQpVrPY1LmsVzSPmly+4sXi1dcbg/l/xHxMg/9ejiMLrjs2NjAQpCDvIk0AJEIsKXMFCKyhfeR26mmE3kp0t0Sn2b/AvPsrPreBcPOthiTBAEpi6Bp8EKosU53a+vRfKY0zEdhfa7diJsvw3p1HA+fc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1737637461; c=relaxed/simple; bh=Vdzu+Fwt06yiMZfjE+22PPXLLqoNKYmCMWOlFKYGS6I=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=eLB8hXMCS7ALb5forutDTzH6akJuoPCHEJ00W+6x04kx3MblnQ7N8QYRVUzJ5qF53IREAjMmvSze4uM990NMyF6xuSHrbfOtydJVI4zcJlaW3ufhQ/IscoQ6Ab846lXhSZvjV/78UuR1dgpGodl1QN6GkEqGGw8L/sPNxVdZbU8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org; spf=pass smtp.mailfrom=linaro.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b=FdEYTTYD; arc=none smtp.client-ip=209.85.128.50 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linaro.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linaro.org header.i=@linaro.org header.b="FdEYTTYD" Received: by mail-wm1-f50.google.com with SMTP id 5b1f17b1804b1-436249df846so5772535e9.3 for ; Thu, 23 Jan 2025 05:04:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1737637457; x=1738242257; darn=vger.kernel.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=cl9194DFZdwJ8whugEX9MZlsEcPw/nky6ZU9/dikYFM=; b=FdEYTTYDEvniTcg2CeNDFuMCP8sA5IQQm4JVQyPUHDf0Rn49AmAORdaJKWnDRexbit s9FmLHg2XPaY6dRoWLY1fuewXkOHQuKCrzL7DM8ziTZHVVpmu+t3i1XQ6dLujnY4pDSv VbPQ3FrnBCF+PtYXTdzasMk/bR2Hhbv1X5j92SQxrfhhXLTZ4xgVaAJ+NsDZfY1iFAW1 ebfsvmg+r5awbAPdhoNHTaPK9pq1YwUmH5JfJHgrF31/x/NEXIUu0vLid8brb6xrPCum jowWsZt2AcbRb5YcFd33e12i5Gm4poGVk2CDaGRCDWRhndVbbk+csA2PtGvYnk7D1WDJ bHVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737637458; x=1738242258; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=cl9194DFZdwJ8whugEX9MZlsEcPw/nky6ZU9/dikYFM=; b=K4QI/tDbisrrAUl7TMwgR1Cnf2q0cR0LAw7KGhGmIoc11GOfUiSkq/jn9vvcddDdO9 25Bn66+H6w23Unw+hmBL4zEUlblRbnbZHTC87byQyRZbne1Ud6v4Gx/YOtE6725RsLYu Hbr6xTILtlbYYnXKe0RSwaVa0jyJ7a8Y8V/b1jLrO+piH5dbP7Kr0aOWB2RfNrgyqXxc xsrO+sdOMSPG2NOOqefHTc9Ewm6UyjJwBe29O4eZM4ZSjIZux5V1jxkyR28f5dAiOmPY HrOBbAFRzhsCm/huqjABSx7phCgiMAyj67M+bpzh0HBOwzoR66iyZIuniBY6pelaZ4eR Y3FQ== X-Forwarded-Encrypted: i=1; AJvYcCXkezWYA3UWF7BAvdCW/0ds2Evp4TNOvxlbZWWvmMbE2ZJFEJo2vVyWgbGacD5tR0YZYC2DZG2tORNyvoU=@vger.kernel.org X-Gm-Message-State: AOJu0YzBURflP0WI0DPvMxzYv1CmYy5ihXOBkUVwQJ6ygfiAxFrtDWff 78aO0Yc36r63X85c7M3fsZcFX+gI8HgP11V7DyyPq7X99OBBqetCFbnsP8+ta0Y= X-Gm-Gg: ASbGncs69//rS1V1yHMbIk8YDNAUGys02kWES8rVeSw73V0+nUATdMC/XEr3V2FvwmQ JVpoFahp397lxbCcxEWBnJyjrb0KSZUCEk37rllA2RWHF0ovuPLbNwAGbInPZNNEgOvPGmfrLnv utcTw8P9UByAcrej3WnmN70bOq6UtkpoLBWWH3k020h5IjIozMXoxQ2maPfJ9jq9JO8rPOxku5K 7i46gPZiH49cPSNDS1C9kxV2VOok51jNDZNaU2DDsOESF1xImrIXw6CBdC3I/M8RX2zONoz35I/ gCgHu327BTTNyN/HDcrE X-Google-Smtp-Source: AGHT+IFfMaV0Ij3T3tldLauIZsdbGiEdyirSW5JG2BohSSJNY2hQK3fqNX1SRxOVtDQ4hJ9zH+XqLQ== X-Received: by 2002:a05:600c:3d06:b0:434:eb73:b0c0 with SMTP id 5b1f17b1804b1-438913c7e31mr266934345e9.5.1737637457469; Thu, 23 Jan 2025 05:04:17 -0800 (PST) Received: from localhost ([196.207.164.177]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-438b31afa30sm64090025e9.20.2025.01.23.05.04.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jan 2025 05:04:16 -0800 (PST) Date: Thu, 23 Jan 2025 16:04:13 +0300 From: Dan Carpenter To: Sudeep Holla Cc: zuoqian , Ionela Voinescu , rafael@kernel.org, viresh.kumar@linaro.org, cristian.marussi@arm.com, arm-scmi@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] cpufreq: scpi: compare against frequency instead of rate Message-ID: <8ebf8f26-c3d9-43c0-b417-ce3131a84eb4@stanley.mountain> References: <20250123075321.4442-1-zuoqian113@gmail.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: On Thu, Jan 23, 2025 at 12:16:50PM +0000, Sudeep Holla wrote: > (for some reason I don't have the original email) > > On Thu, Jan 23, 2025 at 02:12:14PM +0300, Dan Carpenter wrote: > > On Thu, Jan 23, 2025 at 07:53:20AM +0000, zuoqian wrote: > > > The CPU rate from clk_get_rate() may not be divisible by 1000 > > > (e.g., 133333333). But the rate calculated from frequency is always > > > divisible by 1000 (e.g., 133333000). > > > Comparing the rate causes a warning during CPU scaling: > > > "cpufreq: __target_index: Failed to change cpu frequency: -5". > > > When we choose to compare frequency here, the issue does not occur. > > > > > > Signed-off-by: zuoqian > > > --- > > > drivers/cpufreq/scpi-cpufreq.c | 5 +++-- > > > 1 file changed, 3 insertions(+), 2 deletions(-) > > > > > > diff --git a/drivers/cpufreq/scpi-cpufreq.c b/drivers/cpufreq/scpi-cpufreq.c > > > index cd89c1b9832c..3bff4bb5ab4a 100644 > > > --- a/drivers/cpufreq/scpi-cpufreq.c > > > +++ b/drivers/cpufreq/scpi-cpufreq.c > > > @@ -39,8 +39,9 @@ static unsigned int scpi_cpufreq_get_rate(unsigned int cpu) > > > static int > > > scpi_cpufreq_set_target(struct cpufreq_policy *policy, unsigned int index) > > > { > > > - u64 rate = policy->freq_table[index].frequency * 1000; > > > > policy->freq_table[index].frequency is a u32 so in this original > > calculation, even though "rate" is declared as a u64, it can't actually > > be more than UINT_MAX. > > > > Agreed and understood. > > > > + unsigned long freq = policy->freq_table[index].frequency; > > > struct scpi_data *priv = policy->driver_data; > > > + u64 rate = freq * 1000; > > > > So you've fixed this by casting policy->freq_table[index].frequency > > to unsigned long, which fixes the problem on 64bit systems but it still > > remains on 32bit systems. It would be better to declare freq as a u64. > > > > Just trying to understand if that matters. freq is in kHz as copied > from policy->freq_table[index].frequency and we compare it with > kHZ below as the obtained clock rate is divided by 1000. What am I > missing ? If it helps, it can be renamed as freq_in_khz and even keep > it as "unsigned int" as in struct cpufreq_frequency_table. > I misunderstood the integer overflow bug because I read too much into the fact that "rate" was declared as a u64. It would have been fine to declare it as a unsigned long. The cpufreq internals don't support anything more than ULONG_MAX. I have heard someone say that new systems are bumping up against the 4GHz limit but presumably that would only be high end 64bit systems, not old 32bit system. The ->freq_table[] frequency is in kHz so a u32 is fine. I guess if we get frequencies of a THz then we'll have to update that. But when we convert to Hz then we need a cast to avoid an integer overflow for systems which are over the 4GHz boundary. unsigned long rate = (unsigned long)khz * 1000; The second bug is that we need to compare kHz instead of Hz and that's straight forward. regards, dan carpenter