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 X-Spam-Level: X-Spam-Status: No, score=-8.8 required=3.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 623E4C433B4 for ; Wed, 21 Apr 2021 13:48:35 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 330ED61439 for ; Wed, 21 Apr 2021 13:48:35 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S235199AbhDUNtH (ORCPT ); Wed, 21 Apr 2021 09:49:07 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48336 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234768AbhDUNtD (ORCPT ); Wed, 21 Apr 2021 09:49:03 -0400 Received: from metis.ext.pengutronix.de (metis.ext.pengutronix.de [IPv6:2001:67c:670:201:290:27ff:fe1d:cc33]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0AA6EC06174A for ; Wed, 21 Apr 2021 06:48:30 -0700 (PDT) Received: from ptx.hi.pengutronix.de ([2001:67c:670:100:1d::c0]) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lZDDP-0002bJ-SZ; Wed, 21 Apr 2021 15:48:27 +0200 Received: from ukl by ptx.hi.pengutronix.de with local (Exim 4.92) (envelope-from ) id 1lZDDO-00018U-EK; Wed, 21 Apr 2021 15:48:26 +0200 Date: Wed, 21 Apr 2021 15:48:25 +0200 From: Uwe =?utf-8?Q?Kleine-K=C3=B6nig?= To: Claudiu Beznea , Thierry Reding , Lee Jones Cc: linux-pwm@vger.kernel.org, Alexandre Belloni , Nicolas Ferre , Ludovic Desroches , kernel@pengutronix.de, linux-arm-kernel@lists.infradead.org Subject: Re: overflow and wrong timeout errors in pwm-atmel Message-ID: <20210421134825.powx5tvqvce32fho@pengutronix.de> References: <20210420095118.1571344-1-u.kleine-koenig@pengutronix.de> <20210421092606.1634092-1-u.kleine-koenig@pengutronix.de> <20210421110336.bd5s6e2kjxqilddi@pengutronix.de> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="hunmctkm5vrpq4ne" Content-Disposition: inline In-Reply-To: <20210421110336.bd5s6e2kjxqilddi@pengutronix.de> X-SA-Exim-Connect-IP: 2001:67c:670:100:1d::c0 X-SA-Exim-Mail-From: ukl@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-pwm@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-pwm@vger.kernel.org --hunmctkm5vrpq4ne Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Apr 21, 2021 at 01:03:36PM +0200, Uwe Kleine-K=F6nig wrote: > On Wed, Apr 21, 2021 at 11:26:08AM +0200, Uwe Kleine-K=F6nig wrote: > > With these three patches PWM_DEBUG is now happy. (At least I couldn't > > trigger a warning any more. I think there are still a few problems with > > integer overflows.) >=20 > BTW, setting the period to 138350580899 (with a clock rate of 133333333 > Hz) results in setting period=3D0 because >=20 > state->period * clkrate =3D > 138350580899 * 133333333 =3D > 40254751 (discarded from 18446744073749806367). As a first remedy the following could be done: diff --git a/drivers/pwm/pwm-atmel.c b/drivers/pwm/pwm-atmel.c index 38d86340201c..02d69fa5f7d2 100644 --- a/drivers/pwm/pwm-atmel.c +++ b/drivers/pwm/pwm-atmel.c @@ -199,6 +199,11 @@ static int atmel_pwm_calculate_cprd_and_pres(struct pw= m_chip *chip, unsigned long long cycles =3D state->period; int shift; =20 + if (fls(cycles) + fls(clkrate) > 64) { + dev_err(chip->dev, "period to big to calculate HW parameters\n"); + return -EINVAL; + } + /* Calculate the period cycles and prescale value */ cycles *=3D clkrate; do_div(cycles, NSEC_PER_SEC); Is this sensible? (Actually I'd prefer to just continue with period =3D (ULL(1) << (64 - fls(clkrate))) - 1 according to the motto to yield the highest possible period, but this function has another error path that returns -EINVAL so this would be inconsistent.) Best regards Uwe --=20 Pengutronix e.K. | Uwe Kleine-K=F6nig | Industrial Linux Solutions | https://www.pengutronix.de/ | --hunmctkm5vrpq4ne Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAABCgAdFiEEfnIqFpAYrP8+dKQLwfwUeK3K7AkFAmCALSYACgkQwfwUeK3K 7Almrwf/c+tV70NNg925X+gBlg69oaELR8U7TwSV0FF2Pfxk2qyPGcDnxRFBiBrH WDmcQ/mfk/qO7Jn2Y7Jmf1S4ZMX80ELFfHLEP1nMYk2jIB/77lmBWkAlNvSnc5Ak +NoINHyO735sVHaHQlEBUBDeWdElsmzDD/dxJTbGpDNWQnQb+xzR5yAYsieLC1o9 JjyQ/Y9NM7471W54jzF3xHwo9tSMIUaPhm2aM66XDsQCGzgzn+h/Ss4LV1lTAPqg gMU907QtjzkL5/fRBqud80wmVOvkXs++x7GgDX5QCR5qRo1sI0IyXJvUU+AemRBa fjRg0uBCeu1g7CJOrT8RtMRf9GGvxQ== =45Q3 -----END PGP SIGNATURE----- --hunmctkm5vrpq4ne-- 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 X-Spam-Level: X-Spam-Status: No, score=-8.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1C3B3C433ED for ; Wed, 21 Apr 2021 13:50:35 +0000 (UTC) Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 7B1FA61439 for ; Wed, 21 Apr 2021 13:50:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 7B1FA61439 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=pengutronix.de Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=desiato.20200630; h=Sender:Content-Type:Cc: List-Subscribe:List-Help:List-Post:List-Archive:List-Unsubscribe:List-Id: In-Reply-To:MIME-Version:References:Message-ID:Subject:To:From:Date:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=qVDWvSh4/VZJ3VX+ZPLEtWUzKdvWaQTw1h1Zzw6VsbY=; b=JLMcm0CXwmhEpvsybfgDKpe2v y4F/EfL/pZIvIDSFEtQsdYDGUxSK9q2gaDsEpEUUect/yIPGc1KTeb147vuMWSAO6F85pcN5zxqk4 wF6zCUu3HiycUw8wYQ3Ea/8XWtr2HaWs2v5W4LVcnt5JqgdhmnG4omK6XwfRoZlUrv3F4N8FVGdqB iSmJEeqDCedyVjP9d+Imlsw01WfXK/f3Tg2pA0lDJVqIypC2kfvKI2+pMO0/p0PqQqjxublX257tX EHPcHIBVh5SBm/N9KLKESQFrBInBJ/dmuAzmCcw+bmNTVGMyLCTNu2BWGrB9xPttxmk4ymUMJKmZD 7JpJ770MQ==; Received: from localhost ([::1] helo=desiato.infradead.org) by desiato.infradead.org with esmtp (Exim 4.94 #2 (Red Hat Linux)) id 1lZDDd-00EUyn-LF; Wed, 21 Apr 2021 13:48:41 +0000 Received: from bombadil.infradead.org ([2607:7c80:54:e::133]) by desiato.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lZDDa-00EUyW-Fj for linux-arm-kernel@desiato.infradead.org; Wed, 21 Apr 2021 13:48:38 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=bombadil.20210309; h=In-Reply-To:Content-Type:MIME-Version :References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=iizqSR0jcuLB9fScvI0Stv13bNxQ05VyIGFQICHaVYU=; b=hYuXjJdk5huv7D2dKIqJ5b5QP2 0Br5OqcYacxNoxmuDPVRIT4GYFZsMvkb9ngnb+XteMIs8NypfpN9ZiBupjkACvJbMJsBg+Dshs45B xqlSaOMKUdUDgY49K+uo2uG820AuAL1jPRW8jhgVNNmA9+VYnS0bzjag7xRsB1z8+MWOFY7Ydo/8p Y/QkgtYf8ajAd6s2zl7kItXnNuvG8wLy89D5t0pBjYwbsB7VIIcM4DSBWuLu3Z4aMcveT9z4MUGy9 MSxketQhQ5pXWdIjbohUalbjKZvrBVbWE95hAf1SPTcJfTLgotMXrZwCDKiuH3mRQbNfQFVB3Pcp+ 1PfzXOGA==; Received: from metis.ext.pengutronix.de ([2001:67c:670:201:290:27ff:fe1d:cc33]) by bombadil.infradead.org with esmtps (Exim 4.94 #2 (Red Hat Linux)) id 1lZDDX-00Cvfh-U0 for linux-arm-kernel@lists.infradead.org; Wed, 21 Apr 2021 13:48:37 +0000 Received: from ptx.hi.pengutronix.de ([2001:67c:670:100:1d::c0]) by metis.ext.pengutronix.de with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1lZDDP-0002bJ-SZ; Wed, 21 Apr 2021 15:48:27 +0200 Received: from ukl by ptx.hi.pengutronix.de with local (Exim 4.92) (envelope-from ) id 1lZDDO-00018U-EK; Wed, 21 Apr 2021 15:48:26 +0200 Date: Wed, 21 Apr 2021 15:48:25 +0200 From: Uwe =?utf-8?Q?Kleine-K=C3=B6nig?= To: Claudiu Beznea , Thierry Reding , Lee Jones Subject: Re: overflow and wrong timeout errors in pwm-atmel Message-ID: <20210421134825.powx5tvqvce32fho@pengutronix.de> References: <20210420095118.1571344-1-u.kleine-koenig@pengutronix.de> <20210421092606.1634092-1-u.kleine-koenig@pengutronix.de> <20210421110336.bd5s6e2kjxqilddi@pengutronix.de> MIME-Version: 1.0 In-Reply-To: <20210421110336.bd5s6e2kjxqilddi@pengutronix.de> X-SA-Exim-Connect-IP: 2001:67c:670:100:1d::c0 X-SA-Exim-Mail-From: ukl@pengutronix.de X-SA-Exim-Scanned: No (on metis.ext.pengutronix.de); SAEximRunCond expanded to false X-PTX-Original-Recipient: linux-arm-kernel@lists.infradead.org X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210421_064835_991066_89D0D63C X-CRM114-Status: GOOD ( 19.69 ) 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: , Cc: linux-pwm@vger.kernel.org, Alexandre Belloni , Ludovic Desroches , kernel@pengutronix.de, linux-arm-kernel@lists.infradead.org Content-Type: multipart/mixed; boundary="===============7730308542081137058==" Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org --===============7730308542081137058== Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="hunmctkm5vrpq4ne" Content-Disposition: inline --hunmctkm5vrpq4ne Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Wed, Apr 21, 2021 at 01:03:36PM +0200, Uwe Kleine-K=F6nig wrote: > On Wed, Apr 21, 2021 at 11:26:08AM +0200, Uwe Kleine-K=F6nig wrote: > > With these three patches PWM_DEBUG is now happy. (At least I couldn't > > trigger a warning any more. I think there are still a few problems with > > integer overflows.) >=20 > BTW, setting the period to 138350580899 (with a clock rate of 133333333 > Hz) results in setting period=3D0 because >=20 > state->period * clkrate =3D > 138350580899 * 133333333 =3D > 40254751 (discarded from 18446744073749806367). As a first remedy the following could be done: diff --git a/drivers/pwm/pwm-atmel.c b/drivers/pwm/pwm-atmel.c index 38d86340201c..02d69fa5f7d2 100644 --- a/drivers/pwm/pwm-atmel.c +++ b/drivers/pwm/pwm-atmel.c @@ -199,6 +199,11 @@ static int atmel_pwm_calculate_cprd_and_pres(struct pw= m_chip *chip, unsigned long long cycles =3D state->period; int shift; =20 + if (fls(cycles) + fls(clkrate) > 64) { + dev_err(chip->dev, "period to big to calculate HW parameters\n"); + return -EINVAL; + } + /* Calculate the period cycles and prescale value */ cycles *=3D clkrate; do_div(cycles, NSEC_PER_SEC); Is this sensible? (Actually I'd prefer to just continue with period =3D (ULL(1) << (64 - fls(clkrate))) - 1 according to the motto to yield the highest possible period, but this function has another error path that returns -EINVAL so this would be inconsistent.) Best regards Uwe --=20 Pengutronix e.K. | Uwe Kleine-K=F6nig | Industrial Linux Solutions | https://www.pengutronix.de/ | --hunmctkm5vrpq4ne Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQEzBAABCgAdFiEEfnIqFpAYrP8+dKQLwfwUeK3K7AkFAmCALSYACgkQwfwUeK3K 7Almrwf/c+tV70NNg925X+gBlg69oaELR8U7TwSV0FF2Pfxk2qyPGcDnxRFBiBrH WDmcQ/mfk/qO7Jn2Y7Jmf1S4ZMX80ELFfHLEP1nMYk2jIB/77lmBWkAlNvSnc5Ak +NoINHyO735sVHaHQlEBUBDeWdElsmzDD/dxJTbGpDNWQnQb+xzR5yAYsieLC1o9 JjyQ/Y9NM7471W54jzF3xHwo9tSMIUaPhm2aM66XDsQCGzgzn+h/Ss4LV1lTAPqg gMU907QtjzkL5/fRBqud80wmVOvkXs++x7GgDX5QCR5qRo1sI0IyXJvUU+AemRBa fjRg0uBCeu1g7CJOrT8RtMRf9GGvxQ== =45Q3 -----END PGP SIGNATURE----- --hunmctkm5vrpq4ne-- --===============7730308542081137058== Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel --===============7730308542081137058==--