Received: by 2002:a05:6358:1087:b0:cb:c9d3:cd90 with SMTP id j7csp381992rwi; Thu, 27 Oct 2022 02:33:47 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6DAP2iacJG17BQOi57vJuUPWkBluCeuJ3H3Drjp37K4ry/Xtkkt7mvLZSukjdNDhiWfeRM X-Received: by 2002:a63:d314:0:b0:452:598a:cc5a with SMTP id b20-20020a63d314000000b00452598acc5amr42455897pgg.299.1666863226835; Thu, 27 Oct 2022 02:33:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666863226; cv=none; d=google.com; s=arc-20160816; b=TVCQv7SPDQCaSDL6wrdvjTjO72LlJCfEjR0vfzZTZ8o45GkQ6gWXBcyECLnALK4UcN H9PG+/JUnWN6JZ7s8jSmmZRDMfgHPajHgJ36m1ru85SYcfyuIVzHxLUASkj0F5L5iVqN 9hVBOkQY9ycJD7OSWvFaOG5UW0IBlmc1NspLviCc0SCgu/aL66dJl8H4Xz73cpi3R9KQ zKJJSes3aZynQn+frSjgHtnIn3lfP8Hd0+/H1q/yrKiw6umALq2mnmcimC1/q+Cm+ER6 2LiaWhOyVa76cg6zHzQiSz+cMhrYQMHyBPMfZfsX903ByFZTbcoGDnoSkF/tFHhUjG8n atQA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=/dxMhaiQkbELR5cRCF3auNLCWo02kh5lcqfROaPMYpg=; b=WYUvnZVoIiBQdcwBP07H0qCT+Fe810XipFXRW5Qw5+b9TggZXWBBYGqYfdw097GrED D9gzjabpXVV4gdIkAF0xleMGTpOI03GNHUdPtDDyC7HfS34a5jVdYUlyJOzctJZS0GqU 0WnhS4ZJKjyjfKhUcIx5qxvglQVf0f591ynkZNwJIGqG5LClFXOK9g4m0oq2gQTC2QMJ Olhu4gQU2hH8rxQciik5dL/bcYb5jA+m5LkSjjAnzbg4PEbmgIo7L7QBSPqC9d2BmsZa JrKi6JdqNvgrxYNC1AGSmzpes7Ux3ilTrc6ZeV2LXOo2+iKtX4Vx/ZXWldmOGf7KaCCD dZqQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JtBEdbYs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id jk3-20020a170903330300b001869ef16477si905176plb.356.2022.10.27.02.33.35; Thu, 27 Oct 2022 02:33:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=JtBEdbYs; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235334AbiJ0JKv (ORCPT + 99 others); Thu, 27 Oct 2022 05:10:51 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40892 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235246AbiJ0JKZ (ORCPT ); Thu, 27 Oct 2022 05:10:25 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 12BB41FF92 for ; Thu, 27 Oct 2022 02:10:20 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id A402362243 for ; Thu, 27 Oct 2022 09:10:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 44A54C433C1; Thu, 27 Oct 2022 09:10:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1666861819; bh=VpRfkhtCImVDOH7LH3f1cCEnxnIxnNzXwZHVtePJVkg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JtBEdbYsaEkCHFIKvmPIqw1pCTXO75n0J8VNPzct1Q2Sc2EqxH7SDoCzOORdqqylb lDM6Lhp4aJkgwZil+JwhL4p7L+z6hhf0ixGwT/1QP1TgspUjBXHlcNoRG5ZFZwfmm+ dDi2lH56mekF5pd9XyNuOFc5O1H/VQQyRQFSkq/0RgUBf6vswqMsUTvok/KXDN3+Ig fu0fMghUtHxvtoRzAonHHYDstBo+h6HpFJLeEAvQi9xYD/0jPjwcdSfKvYCtLquD0B XN6tbA0w794qlmGTPO/AEScCMJ2xTSO/1Y4gjjkfBxHtJaFNNYGKEnT+AzrNK5I4yI nI3/DmFzpNOQQ== From: Oded Gabbay To: linux-kernel@vger.kernel.org Cc: Ohad Sharabi Subject: [PATCH 05/15] habanalabs: avoid divide by zero in device utilization Date: Thu, 27 Oct 2022 12:09:57 +0300 Message-Id: <20221027091007.664797-5-ogabbay@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221027091007.664797-1-ogabbay@kernel.org> References: <20221027091007.664797-1-ogabbay@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Ohad Sharabi Currently there is no verification whether the divisor is legal. Signed-off-by: Ohad Sharabi Reviewed-by: Oded Gabbay Signed-off-by: Oded Gabbay --- drivers/misc/habanalabs/common/device.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/drivers/misc/habanalabs/common/device.c b/drivers/misc/habanalabs/common/device.c index 0e88396744a1..b71303ba11d0 100644 --- a/drivers/misc/habanalabs/common/device.c +++ b/drivers/misc/habanalabs/common/device.c @@ -987,11 +987,16 @@ static void device_late_fini(struct hl_device *hdev) int hl_device_utilization(struct hl_device *hdev, u32 *utilization) { - u64 max_power, curr_power, dc_power, dividend; + u64 max_power, curr_power, dc_power, dividend, divisor; int rc; max_power = hdev->max_power; dc_power = hdev->asic_prop.dc_power_default; + divisor = max_power - dc_power; + if (!divisor) { + dev_warn(hdev->dev, "device utilization is not supported\n"); + return -EOPNOTSUPP; + } rc = hl_fw_cpucp_power_get(hdev, &curr_power); if (rc) @@ -1000,7 +1005,7 @@ int hl_device_utilization(struct hl_device *hdev, u32 *utilization) curr_power = clamp(curr_power, dc_power, max_power); dividend = (curr_power - dc_power) * 100; - *utilization = (u32) div_u64(dividend, (max_power - dc_power)); + *utilization = (u32) div_u64(dividend, divisor); return 0; } -- 2.25.1