From patchwork Mon Dec 5 04:55:38 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Guenter Roeck X-Patchwork-Id: 9460317 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id F27FB6022E for ; Mon, 5 Dec 2016 06:03:31 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DC80724B5B for ; Mon, 5 Dec 2016 06:03:31 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CD7B12621D; Mon, 5 Dec 2016 06:03:31 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00,DKIM_SIGNED, RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6F45C24B5B for ; Mon, 5 Dec 2016 06:03:31 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751096AbcLEGDa (ORCPT ); Mon, 5 Dec 2016 01:03:30 -0500 Received: from bh-25.webhostbox.net ([208.91.199.152]:50413 "EHLO bh-25.webhostbox.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751039AbcLEGDa (ORCPT ); Mon, 5 Dec 2016 01:03:30 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=roeck-us.net; s=default; h=References:In-Reply-To:Message-Id:Date:Subject: Cc:To:From; bh=xWG3wgWCqe/IgmkI758NnyjEa65xjZvsdvY+xqXpZj0=; b=e0ATNQ9kRyeVdN LbdacLqG8gZoISY99E4u8J11hLAvl+JntYtgcxA4eLMT6D5a66C2h4NIWtbTgyFGNxC2Z60r1OvaT SaTllgZpVwCd8U3wrN5/aUYbc4FZPQD8alv6szGFzkMpKaepT3nk3UXCfgyjZ+SxQ4RL1jmRohwS9 egGZXLsEXxxr9rerY3XvZv4MNGBO8qQQVx8Dkkc7HOJSiN7d2IkafxHkjT9GORjIYQsI26KdD0iFT 6W1R6/9Q57+G8GiyUkreKTaEXXZuVLjzlp7fx9Kt4qqez9f8vUthy9+SohHcFl5XK5i5fBXXoX78k cCoCwxAMhz4Um/fhXO0g==; Received: from 108-223-40-66.lightspeed.sntcca.sbcglobal.net ([108.223.40.66]:49780 helo=localhost) by bh-25.webhostbox.net with esmtpa (Exim 4.86_1) (envelope-from ) id 1cDlJs-0009vQ-Mu; Mon, 05 Dec 2016 04:56:05 +0000 From: Guenter Roeck To: Hardware Monitoring Cc: Jean Delvare , Guenter Roeck Subject: [PATCH 15/17] hwmln: (g762) Fix overflows and crash seen when writing limit attributes Date: Sun, 4 Dec 2016 20:55:38 -0800 Message-Id: <1480913740-5678-15-git-send-email-linux@roeck-us.net> X-Mailer: git-send-email 2.5.0 In-Reply-To: <1480913740-5678-1-git-send-email-linux@roeck-us.net> References: <1480913740-5678-1-git-send-email-linux@roeck-us.net> X-Authenticated_sender: guenter@roeck-us.net X-OutGoing-Spam-Status: No, score=-1.0 X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - bh-25.webhostbox.net X-AntiAbuse: Original Domain - vger.kernel.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - roeck-us.net X-Get-Message-Sender-Via: bh-25.webhostbox.net: authenticated_id: guenter@roeck-us.net X-Authenticated-Sender: bh-25.webhostbox.net: guenter@roeck-us.net X-Source: X-Source-Args: X-Source-Dir: Sender: linux-hwmon-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-hwmon@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Fix overflows seen when writing into fan speed limit attributes. Also fix crash due to division by zero, seen when certain very large values (such as 4611686018427387904, or 0x4000000000000000) are written into fan speed limit attributes. Fixes: 594fbe713bf60 ("Add support for GMT G762/G763 PWM fan controllers") Signed-off-by: Guenter Roeck Reviewed-by: Jean Delvare --- drivers/hwmon/g762.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/drivers/hwmon/g762.c b/drivers/hwmon/g762.c index b96a2a9e4df7..584f9f755a92 100644 --- a/drivers/hwmon/g762.c +++ b/drivers/hwmon/g762.c @@ -193,12 +193,13 @@ static inline unsigned int rpm_from_cnt(u8 cnt, u32 clk_freq, u16 p, * Convert fan RPM value from sysfs into count value for fan controller * register (FAN_SET_CNT). */ -static inline unsigned char cnt_from_rpm(u32 rpm, u32 clk_freq, u16 p, +static inline unsigned char cnt_from_rpm(unsigned long rpm, u32 clk_freq, u16 p, u8 clk_div, u8 gear_mult) { - if (!rpm) /* to stop the fan, set cnt to 255 */ + if (!rpm || !p || !clk_div) /* to stop the fan, set cnt to 255 */ return 0xff; + rpm = clamp_val(rpm, 1, ULONG_MAX / (p * clk_div)); return clamp_val(((clk_freq * 30 * gear_mult) / (rpm * p * clk_div)), 0, 255); }