Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp567009pxb; Sat, 6 Mar 2021 08:52:58 -0800 (PST) X-Google-Smtp-Source: ABdhPJwMCetQothBIp78sSOQ22Tqbpf1NLfZ1l2QLKGRqym39jKvV8hfFsLTB/jFuy7fU0TI4kxl X-Received: by 2002:aa7:df14:: with SMTP id c20mr14606773edy.197.1615049578205; Sat, 06 Mar 2021 08:52:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1615049578; cv=none; d=google.com; s=arc-20160816; b=sycL40P4Omeqdiy+GytuTQkY7Z22DI+QhV7YxqTBGWki2vaffZzCFAfnhpXDk0ac43 WznHcdxsQD3lvhRM4/TJLLSTavtXz+XMatI1DaPAFODQiRus6ChIdL3iblLz9i8EGQ4c tB4j+tsf3sm5mlHwdw+Nj9oeOteOJ+tucDdzxvXVhg4uCKAf953m9TO8RDUPzAkfPM0y 73UvveXSu7R+RR+a7FS7feIaVVWdH1mSv98IIkQ7GlhhZoCSr+B+UAPv9s+rZrON+JJD dOvUxRtzHO7sR8/f4FnIHOm12RZ95noT3O4zfnBGaDgLSFDv7V94Cs5pcNLFeYsueLN6 In3A== 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:subject:cc:to:from:date; bh=Jdz7BkmRBIu0va3fgu3F+STzw8jAaMMgv99oBUdqzkw=; b=Vy+gpSsUlJGkJlotRie4CzKTDJIPyC9r5GEs06ca+p2ZDxU2knurkeFrApfB9/TA83 +MPLMgeYLM55N1sMb280gZonjgqCgFJqoOsicIxGyKV2xrD3BRxCpdH+H1Bg/Iq+ppmH G0EhaMx8Qz1xS7VMvaHhKckvXbulgz6UBWi5Qe/NbTOxRsZs2m5A7RmSTgH6YSuoHZFb 7akky6Ph7BwZjf1ZAXzOo4bKE+kU8GU4SI7VGqMg7NvQj5opaoXzH9sLwNVokCJGNsit xA3lLGt+/PYo6z3nGnOigytubH8/xvIHvk3uepgFvBHDMclg6TD1uNB1Cdp37miR5Nte TFuQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id c11si3874093edv.591.2021.03.06.08.52.35; Sat, 06 Mar 2021 08:52:58 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S231159AbhCFQrp (ORCPT + 99 others); Sat, 6 Mar 2021 11:47:45 -0500 Received: from mail.kernel.org ([198.145.29.99]:53024 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230216AbhCFQrf (ORCPT ); Sat, 6 Mar 2021 11:47:35 -0500 Received: from archlinux (cpc108967-cmbg20-2-0-cust86.5-4.cable.virginm.net [81.101.6.87]) (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 D0FAB64FE4; Sat, 6 Mar 2021 16:47:33 +0000 (UTC) Date: Sat, 6 Mar 2021 16:47:30 +0000 From: Jonathan Cameron To: William Breathitt Gray Cc: Fabrice Gasnier , david@lechnology.com, alexandre.torgue@foss.st.com, mcoquelin.stm32@gmail.com, olivier.moysan@foss.st.com, linux-iio@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH] counter: stm32-timer-cnt: fix ceiling write max value Message-ID: <20210306164730.61043167@archlinux> In-Reply-To: References: <1614696235-24088-1-git-send-email-fabrice.gasnier@foss.st.com> X-Mailer: Claws Mail 3.17.8 (GTK+ 2.24.33; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, 2 Mar 2021 23:56:46 +0900 William Breathitt Gray wrote: > On Tue, Mar 02, 2021 at 03:43:55PM +0100, Fabrice Gasnier wrote: > > The ceiling value isn't checked before writing it into registers. The user > > could write a value higher than the counter resolution (e.g. 16 or 32 bits > > indicated by max_arr). This makes most significant bits to be truncated. > > Fix it by checking the max_arr to report a range error [1] to the user. > > > > Fixes: ad29937e206f ("counter: Add STM32 Timer quadrature encoder") > > > > [1] https://lkml.org/lkml/2021/2/12/358 > > > > Signed-off-by: Fabrice Gasnier > > Acked-by: William Breathitt Gray Applied to the fixes-togreg branch of iio.git and marked for stable. thanks Jonathan > > Side question: if priv->ceiling is tracking the current ceiling > configuration, would it make sense to change stm32_count_ceiling_read() > to print the value of priv->ceiling instead of doing a regmap_read() > call? > > > --- > > drivers/counter/stm32-timer-cnt.c | 5 +++++ > > 1 file changed, 5 insertions(+) > > > > diff --git a/drivers/counter/stm32-timer-cnt.c b/drivers/counter/stm32-timer-cnt.c > > index ef2a974..2cf0c05 100644 > > --- a/drivers/counter/stm32-timer-cnt.c > > +++ b/drivers/counter/stm32-timer-cnt.c > > @@ -32,6 +32,7 @@ struct stm32_timer_cnt { > > struct regmap *regmap; > > struct clk *clk; > > u32 ceiling; > > + u32 max_arr; > > bool enabled; > > struct stm32_timer_regs bak; > > }; > > @@ -185,6 +186,9 @@ static ssize_t stm32_count_ceiling_write(struct counter_device *counter, > > if (ret) > > return ret; > > > > + if (ceiling > priv->max_arr) > > + return -ERANGE; > > + > > /* TIMx_ARR register shouldn't be buffered (ARPE=0) */ > > regmap_update_bits(priv->regmap, TIM_CR1, TIM_CR1_ARPE, 0); > > regmap_write(priv->regmap, TIM_ARR, ceiling); > > @@ -360,6 +364,7 @@ static int stm32_timer_cnt_probe(struct platform_device *pdev) > > priv->regmap = ddata->regmap; > > priv->clk = ddata->clk; > > priv->ceiling = ddata->max_arr; > > + priv->max_arr = ddata->max_arr; > > > > priv->counter.name = dev_name(dev); > > priv->counter.parent = dev; > > -- > > 2.7.4 > >