Received: by 2002:ab2:7b86:0:b0:1f7:5705:b850 with SMTP id q6csp1240164lqh; Mon, 6 May 2024 01:05:09 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXSn+i1E3sLQKeNueQxb1bz1b8zfSjWX7ZCEOwykR7GLdxg9UOEVkM8QictvnDWE/AxgcNt1iPCLFNEB9x1e9dH8IY6CQiDHTMtnn49iQ== X-Google-Smtp-Source: AGHT+IGVIA9mx7zKQELzKAIyIw7OC04UhnDz0IAi31K8nH82CMgU7j6D/NMH1ajb74C+xV/Nzjd8 X-Received: by 2002:a05:6a00:2391:b0:6ea:f392:bce9 with SMTP id f17-20020a056a00239100b006eaf392bce9mr10792749pfc.15.1714982709436; Mon, 06 May 2024 01:05:09 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714982709; cv=pass; d=google.com; s=arc-20160816; b=LUWGKqivcLMy4c8rviDNNLGZDZCk34m6/0X+DNRu/LKSgIxr7lclEibShnscnZ+j55 bNHnCfo+/xEXhXlcLQ6TYyJRlh3Es+UngR7RrajS8gdE9sOgP9AUblnJjEj5mFszfLJR 7a5S6RqTzhuxAY3rFYaQHonFS3AncReI2at0onR+5ef8+ylXO2n/oYlhPeD2ppXk+bDt yON2wukoSHLBITUq9O2pk3I0O4gUauGxHDSgYuvktlC22tqZrb5eqN22XRWG4R6U31X6 Nyz7U4gqyUKyl0geHwUtom3vg8wZOKq9TenzwQXbmO3cTmezPkEmW8h185y9nHucf9OQ Zf9A== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:message-id:date:subject:cc:to :from; bh=7OG7codY84O+66IML4wq0+38qfD7hxyfnaVzuxx4b5o=; fh=52ust5CmU8kYywdu6avG83e5xW4T/M3LfKkfWmnUT4o=; b=a5jQ3TH/a+vRvh/AKHNR+lNnZd4bGHW6kKT2BG78QMIHB/XQ+FjtWC2zsL87U/fJ3S C7FZUiagmp4MXXuCZo2cGZwYXa9LdDVWzg0iDK9C9E3mj8Jp63Lohh9s0KqIls9qgYKy 0X4LRete2zsvJeFxfaAjM8WGGNF54vFn2lfPOj1jCKdyi+fh0OEO1QsR/bzPK6UlcdQ/ UNtIP07TL6HRR2omp5Pu+z+6m5siKT8lzSzaYxEovcXf1ela2+4WsF4xl41/lr9ixY03 aVhdqOKE4Cfgik+AQLx8f4IQyjtWbrz2+3Gz+YtRdHdQcLNuPJWCACNldwxn3IU6Cwoj 5HdQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-169461-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-169461-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id ln17-20020a056a003cd100b006f46209b1d4si3541980pfb.291.2024.05.06.01.05.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 May 2024 01:05:09 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-169461-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=huawei.com dmarc=pass fromdomain=huawei.com); spf=pass (google.com: domain of linux-kernel+bounces-169461-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-169461-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by sy.mirrors.kernel.org (Postfix) with ESMTPS id 2C38BB2107D for ; Mon, 6 May 2024 08:03:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id C9E061422AC; Mon, 6 May 2024 07:59:44 +0000 (UTC) Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7B141140E23; Mon, 6 May 2024 07:59:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=45.249.212.189 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714982384; cv=none; b=airYsZZMiYy60v02OyaA7FZAYtTbqqeznnBwS1VxgFekyaFFIl90/+LxAMU7k2OE+C+wDBCbsyZP2e9McG+Op7zUG6b0p/rFbJYXQ3dDv+Y83X+u4PYO6gKl3JV5VyIqdLjauGPpBBl9R5EfBg38b0v9TdVFiAvBa+ZKTsOq48Q= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714982384; c=relaxed/simple; bh=mrMc4UwV2GXg/vdxlNdq7LIgtP98hWJIuB7PNLcVgqo=; h=From:To:CC:Subject:Date:Message-ID:MIME-Version:Content-Type; b=l6DI4Wtpb8a1U6L279TqQx2R7ojBdko233953HtxboC7qenhgrxUGrEfs0Cx/NdSMgCrZhsjVbd0tplErDKg700bh/KCCrSoB41kVQFYXgd13Xx+nHFBgh3e5P4ySY2Z9cvG1ncEka0hLdbwVc2wjkGOQfQJ6IlMAIudqaBK7R8= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com; spf=pass smtp.mailfrom=huawei.com; arc=none smtp.client-ip=45.249.212.189 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=huawei.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=huawei.com Received: from mail.maildlp.com (unknown [172.19.162.254]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4VXtzB4FXSzNw35; Mon, 6 May 2024 15:56:54 +0800 (CST) Received: from dggpemm500018.china.huawei.com (unknown [7.185.36.111]) by mail.maildlp.com (Postfix) with ESMTPS id 06A7118007C; Mon, 6 May 2024 15:59:39 +0800 (CST) Received: from huawei.com (10.175.103.91) by dggpemm500018.china.huawei.com (7.185.36.111) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.1.2507.35; Mon, 6 May 2024 15:59:38 +0800 From: liwei To: , , , CC: , , , Subject: [PATCH v2] cpufreq/cppc: fix perf_to_khz/khz_to_perf conversion exception Date: Mon, 6 May 2024 15:58:16 +0800 Message-ID: <20240506075816.1325303-1-liwei728@huawei.com> X-Mailer: git-send-email 2.25.1 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemm500018.china.huawei.com (7.185.36.111) When the nominal_freq recorded by the kernel is equal to the lowest_freq, and the frequency adjustment operation is triggered externally, there is a logic error in cppc_perf_to_khz()/cppc_khz_to_perf(), resulting in perf and khz conversion errors. Fix this by adding the branch processing logic when nominal_freq is equal to lowest_freq. Fixes: ec1c7ad47664 ("cpufreq: CPPC: Fix performance/frequency conversion") Signed-off-by: liwei --- v2: - Fix similar issue in cppc_khz_to_perf() drivers/acpi/cppc_acpi.c | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/drivers/acpi/cppc_acpi.c b/drivers/acpi/cppc_acpi.c index a40b6f3946ef..e3a0d6b46b24 100644 --- a/drivers/acpi/cppc_acpi.c +++ b/drivers/acpi/cppc_acpi.c @@ -1869,9 +1869,15 @@ unsigned int cppc_perf_to_khz(struct cppc_perf_caps *caps, unsigned int perf) u64 mul, div; if (caps->lowest_freq && caps->nominal_freq) { - mul = caps->nominal_freq - caps->lowest_freq; + /* Avoid special case when nominal_freq is equal to lowest_freq */ + if (caps->lowest_freq == caps->nominal_freq) { + mul = caps->nominal_freq; + div = caps->nominal_perf; + } else { + mul = caps->nominal_freq - caps->lowest_freq; + div = caps->nominal_perf - caps->lowest_perf; + } mul *= KHZ_PER_MHZ; - div = caps->nominal_perf - caps->lowest_perf; offset = caps->nominal_freq * KHZ_PER_MHZ - div64_u64(caps->nominal_perf * mul, div); } else { @@ -1892,11 +1898,17 @@ unsigned int cppc_khz_to_perf(struct cppc_perf_caps *caps, unsigned int freq) { s64 retval, offset = 0; static u64 max_khz; - u64 mul, div; + u64 mul, div; if (caps->lowest_freq && caps->nominal_freq) { - mul = caps->nominal_perf - caps->lowest_perf; - div = caps->nominal_freq - caps->lowest_freq; + /* Avoid special case when nominal_freq is equal to lowest_freq */ + if (caps->lowest_freq == caps->nominal_freq) { + mul = caps->nominal_perf; + div = caps->nominal_freq; + } else { + mul = caps->nominal_perf - caps->lowest_perf; + div = caps->nominal_freq - caps->lowest_freq; + } /* * We don't need to convert to kHz for computing offset and can * directly use nominal_freq and lowest_freq as the div64_u64 -- 2.25.1