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=-13.6 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,INCLUDES_PATCH,MAILING_LIST_MULTI, MENTIONS_GIT_HOSTING,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_2 autolearn=unavailable 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 8903EC33CB7 for ; Sat, 18 Jan 2020 11:29:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 6020324694 for ; Sat, 18 Jan 2020 11:29:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1579346952; bh=D8x+g+unQ8xBuwJgPT/oCsHR7+dsGc9bZHovonk0mNo=; h=Date:From:To:Cc:Subject:In-Reply-To:References:List-ID:From; b=MxoIwJNBBi7FxEytH39LperzLjCK7UyQojLBgSQHhRW07EFX0CcX/lFDaEuJBohnm vJ7QZxg3V6Rehm7zb0l/0JCkX3knHZW3QyDZDE5Yt63s+Ic3USfa/zdPyMfmXDKFj0 Wrg64DKtPug5uIo/OBF1e07IvOyMVor+bxb/oReE= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727123AbgARL3L (ORCPT ); Sat, 18 Jan 2020 06:29:11 -0500 Received: from mail.kernel.org ([198.145.29.99]:45556 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727033AbgARL3L (ORCPT ); Sat, 18 Jan 2020 06:29:11 -0500 Received: from archlinux (cpc149474-cmbg20-2-0-cust94.5-4.cable.virginm.net [82.4.196.95]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 37B0F24690; Sat, 18 Jan 2020 11:29:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1579346950; bh=D8x+g+unQ8xBuwJgPT/oCsHR7+dsGc9bZHovonk0mNo=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=Jxc4Izx3rLBVGIqHKSFZ/VaLeRokaFHkHuFG+PFLz/cfYs0Jk3jSnvMjYq2HgFCl2 4eFvcvqlfcrCavpKfr6mecy55lyYOH6WD+KIPq+VcG4YLCZ5wMujGOIzszeZTTFDM8 WLhaabbzboOtLcluufTtvS7Ah11zkYr3TZaNdvdM= Date: Sat, 18 Jan 2020 11:29:06 +0000 From: Jonathan Cameron To: "Hennerich, Michael" Cc: "Bia, Beniamin" , "lars@metafoo.de" , "pmeerw@pmeerw.net" , "linux-iio@vger.kernel.org" , "linux-kernel@vger.kernel.org" , "biabeniamin@outlook.com" , "knaack.h@gmx.de" Subject: Re: [PATCH] iio: frequency: adf4371: Fix divide by zero exception bug Message-ID: <20200118112906.0ad6dd6b@archlinux> In-Reply-To: References: <20200107131559.17772-1-beniamin.bia@analog.com> <20200111110848.7c45a4f3@archlinux> X-Mailer: Claws Mail 3.17.4 (GTK+ 2.24.32; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 14 Jan 2020 16:19:28 +0000 "Hennerich, Michael" wrote: > > -----Original Message----- > > From: Jonathan Cameron > > Sent: Samstag, 11. Januar 2020 12:09 > > To: Bia, Beniamin > > Cc: lars@metafoo.de; Hennerich, Michael ; > > pmeerw@pmeerw.net; linux-iio@vger.kernel.org; linux- > > kernel@vger.kernel.org; biabeniamin@outlook.com; knaack.h@gmx.de > > Subject: Re: [PATCH] iio: frequency: adf4371: Fix divide by zero exception bug > > > > On Tue, 7 Jan 2020 15:15:59 +0200 > > Beniamin Bia wrote: > > > > > From: Michael Hennerich > > > > > > During initialization adf4371_pll_fract_n_get_rate() is called on all > > > output channels to determine if the device was setup. In this case > > > mod2 is zero which can cause a divide by zero exception. > > > Return before that can happen. > > I'm confused by this description vs the code. > > > > As far as I can see fract_n_get_rate is only called on a sysfs read of the > > frequency. > > That's not the case. The failure mechanism comes via adf4371_channel_config(). > It calls adf4371_pll_fract_n_get_rate() prior adf4371_set_freq() which initializes > st->mod2 via adf4371_pll_fract_n_compute(). This only happens the first time > during probe and setup. So the solution was to return 0 if st->mod2 == 0. Not in mainline it doesn't. I took a look at the ADI git hub and I see it does there. I think we are missing a precursor patch. Probably this one from blame... https://github.com/analogdevicesinc/linux/commit/c8e6b341abf749f78e00326cb92b365d90d9de1f Jonathan > > -Michael > > > > > mod2 is set when fract_n_compute is called in the relevant set_freq calls. > > This seems to occur on a sysfs set frequency call. > > > > So the issue here is that a sysfs read before a write of the frequency will cause a > > div zero? If so, is there a sane set of initial values we can put in mod2 and > > friends before exposing them via the device register? > > > > If mod2==0 is a valid value and indicates for example that the channel is turned > > off, then the description should make that clear. > > > > Jonathan > > > > > > > > Fixes: 7f699bd149134 ("iio: frequency: adf4371: Add support for > > > ADF4371 PLL") > > > Signed-off-by: Michael Hennerich > > > Signed-off-by: Beniamin Bia > > > --- > > > drivers/iio/frequency/adf4371.c | 3 +++ > > > 1 file changed, 3 insertions(+) > > > > > > diff --git a/drivers/iio/frequency/adf4371.c > > > b/drivers/iio/frequency/adf4371.c index e2a599b912e5..c21462238314 > > > 100644 > > > --- a/drivers/iio/frequency/adf4371.c > > > +++ b/drivers/iio/frequency/adf4371.c > > > @@ -191,6 +191,9 @@ static unsigned long long > > adf4371_pll_fract_n_get_rate(struct adf4371_state *st, > > > unsigned long long val, tmp; > > > unsigned int ref_div_sel; > > > > > > + if (st->mod2 == 0) > > > + return 0; > > > + > > > val = (((u64)st->integer * ADF4371_MODULUS1) + st->fract1) * st- > > >fpfd; > > > tmp = (u64)st->fract2 * st->fpfd; > > > do_div(tmp, st->mod2); >