From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 87C12C6FD1F for ; Fri, 29 Mar 2024 12:27:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=7eQKwf0Z+/IU8P6FzXPXgNYGM69mSrUpyYay8JCX58M=; b=J919tjvQSR70LQgdUq4DYjVPqO 7QjsVn9+HTBrG71ijr9stq1Ls849fSzg5PY9Tg+qq4LwWG70AeEe5pDzQwnjZ6bGtnXNX2QBD8wcZ qGWXCIJQkZG8VaOenBLAmv5wSR/uvsrcx6YbvZnJeM1tEAT27bBSsZ6bZaINEnGKiNvzP1dmGQbXz UIGwhmsLbcne5+/1+3lNtBuzTAtgfchw8k7sqfr9Ig2hsCyyTN6EjOGMmar8S/qaG6Q9sRZeE6YhI vordtE6qKOxXN+QsMMLC2TVOEyTH1IM2z7K3HGzeyYbJWjsAFdqro5ag4SnUDROdvz8ULZj6lHlPQ i+lHCXYg==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rqBKg-00000000KXX-1p25; Fri, 29 Mar 2024 12:27:42 +0000 Received: from sin.source.kernel.org ([145.40.73.55]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rqBKc-00000000KUm-0l9n; Fri, 29 Mar 2024 12:27:39 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 98B3FCE2F86; Fri, 29 Mar 2024 12:27:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 78F23C43399; Fri, 29 Mar 2024 12:27:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711715255; bh=uRO9hNSHchqspmCb1F6je6akXTN4Hy8qBEMg95DG9jg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JTnqM1oKdNWitGhU2cnLgvRcYpHHmFam4LN5z+ywtEKTb6FOwEoheA8wwNvZ27NiB f+iNKhrt/6iEp+u7gZhiUjeLWcg/ydgICGwiYQ0keqtkCOFAJpf0dcmzkDFFwi0lJv eDIpY2G6r5iDxRBedJ8BoZu6s80zXYgrf1yz3u+IIR2++OiCriQsxMJ0iTBZPA/WZ0 tyVkJuTSVNQafg3v1yMLi0k8/IYU6uN2T0pb4WiO2DobnUujHfwLZ73PC4bItDPkcF IDQXU1/f+uutHFyTogT9VkiJ3x/7MjKK0obO1h6nQAfB76NtHD7vBhDKj+mzZmIB+l P2ob7DNFfOJaQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: C Cheng , Bo Ye , AngeloGioacchino Del Regno , "Rafael J . Wysocki" , Sasha Levin , rafael@kernel.org, daniel.lezcano@linaro.org, matthias.bgg@gmail.com, linux-pm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org Subject: [PATCH AUTOSEL 6.8 23/68] cpuidle: Avoid potential overflow in integer multiplication Date: Fri, 29 Mar 2024 08:25:19 -0400 Message-ID: <20240329122652.3082296-23-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240329122652.3082296-1-sashal@kernel.org> References: <20240329122652.3082296-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.8.2 Content-Transfer-Encoding: 8bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240329_052738_431424_CDF0627D X-CRM114-Status: GOOD ( 13.38 ) X-BeenThere: linux-mediatek@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "Linux-mediatek" Errors-To: linux-mediatek-bounces+linux-mediatek=archiver.kernel.org@lists.infradead.org From: C Cheng [ Upstream commit 88390dd788db485912ee7f9a8d3d56fc5265d52f ] In detail: In C language, when you perform a multiplication operation, if both operands are of int type, the multiplication operation is performed on the int type, and then the result is converted to the target type. This means that if the product of int type multiplication exceeds the range that int type can represent, an overflow will occur even if you store the result in a variable of int64_t type. For a multiplication of two int values, it is better to use mul_u32_u32() rather than s->exit_latency_ns = s->exit_latency * NSEC_PER_USEC to avoid potential overflow happenning. Signed-off-by: C Cheng Signed-off-by: Bo Ye Reviewed-by: AngeloGioacchino Del Regno [ rjw: New subject ] Signed-off-by: Rafael J. Wysocki Signed-off-by: Sasha Levin --- drivers/cpuidle/driver.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/cpuidle/driver.c b/drivers/cpuidle/driver.c index d9cda7f6ccb98..cf5873cc45dc8 100644 --- a/drivers/cpuidle/driver.c +++ b/drivers/cpuidle/driver.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "cpuidle.h" @@ -187,7 +188,7 @@ static void __cpuidle_driver_init(struct cpuidle_driver *drv) s->target_residency = div_u64(s->target_residency_ns, NSEC_PER_USEC); if (s->exit_latency > 0) - s->exit_latency_ns = s->exit_latency * NSEC_PER_USEC; + s->exit_latency_ns = mul_u32_u32(s->exit_latency, NSEC_PER_USEC); else if (s->exit_latency_ns < 0) s->exit_latency_ns = 0; else -- 2.43.0 From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4BE3CCD11DD for ; Fri, 29 Mar 2024 12:27:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=T6AkJWzPSdYnBgQ37LcxnW53LRpzi2FnwepwUHS6R0o=; b=d2ZiYjseQS5phg 18CpBufbGUecqrAPND7+QNHjDKUB3Df5WjzFFN7rvmkDHpGco7sg6H/QRP2MY5SE2CoRfV0vJQKAk R714B0dBn/PajWbfeQjwpQv70M+nOP77kUyYnnqWrY7jVGE/00mL0wOfMNWYci9c+ghNnS+eotfZG sJcQHCq14wvfko6AGBbCRCtGJDVf0nq8MRDl02Qau7lj7Yy2QXXphZALkRpAjC4Bnq/GNUlAM8Jp7 6P2HX/5wACkuCzt3AvPYWx54mmYC5nGkmZ7stpy6DePYMu5XIAsYQtCbG247dmdXsS3v1jsOzN/KS aar09UY+KiRLI5F9v6JQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.97.1 #2 (Red Hat Linux)) id 1rqBKe-00000000KWb-3oEI; Fri, 29 Mar 2024 12:27:40 +0000 Received: from sin.source.kernel.org ([145.40.73.55]) by bombadil.infradead.org with esmtps (Exim 4.97.1 #2 (Red Hat Linux)) id 1rqBKc-00000000KUm-0l9n; Fri, 29 Mar 2024 12:27:39 +0000 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sin.source.kernel.org (Postfix) with ESMTP id 98B3FCE2F86; Fri, 29 Mar 2024 12:27:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 78F23C43399; Fri, 29 Mar 2024 12:27:34 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711715255; bh=uRO9hNSHchqspmCb1F6je6akXTN4Hy8qBEMg95DG9jg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JTnqM1oKdNWitGhU2cnLgvRcYpHHmFam4LN5z+ywtEKTb6FOwEoheA8wwNvZ27NiB f+iNKhrt/6iEp+u7gZhiUjeLWcg/ydgICGwiYQ0keqtkCOFAJpf0dcmzkDFFwi0lJv eDIpY2G6r5iDxRBedJ8BoZu6s80zXYgrf1yz3u+IIR2++OiCriQsxMJ0iTBZPA/WZ0 tyVkJuTSVNQafg3v1yMLi0k8/IYU6uN2T0pb4WiO2DobnUujHfwLZ73PC4bItDPkcF IDQXU1/f+uutHFyTogT9VkiJ3x/7MjKK0obO1h6nQAfB76NtHD7vBhDKj+mzZmIB+l P2ob7DNFfOJaQ== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: C Cheng , Bo Ye , AngeloGioacchino Del Regno , "Rafael J . Wysocki" , Sasha Levin , rafael@kernel.org, daniel.lezcano@linaro.org, matthias.bgg@gmail.com, linux-pm@vger.kernel.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org Subject: [PATCH AUTOSEL 6.8 23/68] cpuidle: Avoid potential overflow in integer multiplication Date: Fri, 29 Mar 2024 08:25:19 -0400 Message-ID: <20240329122652.3082296-23-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240329122652.3082296-1-sashal@kernel.org> References: <20240329122652.3082296-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.8.2 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20240329_052738_431424_CDF0627D X-CRM114-Status: GOOD ( 13.38 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: C Cheng [ Upstream commit 88390dd788db485912ee7f9a8d3d56fc5265d52f ] In detail: In C language, when you perform a multiplication operation, if both operands are of int type, the multiplication operation is performed on the int type, and then the result is converted to the target type. This means that if the product of int type multiplication exceeds the range that int type can represent, an overflow will occur even if you store the result in a variable of int64_t type. For a multiplication of two int values, it is better to use mul_u32_u32() rather than s->exit_latency_ns = s->exit_latency * NSEC_PER_USEC to avoid potential overflow happenning. Signed-off-by: C Cheng Signed-off-by: Bo Ye Reviewed-by: AngeloGioacchino Del Regno [ rjw: New subject ] Signed-off-by: Rafael J. Wysocki Signed-off-by: Sasha Levin --- drivers/cpuidle/driver.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/drivers/cpuidle/driver.c b/drivers/cpuidle/driver.c index d9cda7f6ccb98..cf5873cc45dc8 100644 --- a/drivers/cpuidle/driver.c +++ b/drivers/cpuidle/driver.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "cpuidle.h" @@ -187,7 +188,7 @@ static void __cpuidle_driver_init(struct cpuidle_driver *drv) s->target_residency = div_u64(s->target_residency_ns, NSEC_PER_USEC); if (s->exit_latency > 0) - s->exit_latency_ns = s->exit_latency * NSEC_PER_USEC; + s->exit_latency_ns = mul_u32_u32(s->exit_latency, NSEC_PER_USEC); else if (s->exit_latency_ns < 0) s->exit_latency_ns = 0; else -- 2.43.0 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel