From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 00B1427462 for ; Tue, 10 Oct 2023 16:17:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="fqWOE/O9" Received: by smtp.kernel.org (Postfix) with ESMTPSA id D5A8FC433C7; Tue, 10 Oct 2023 16:17:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1696954647; bh=56o7CLyPH8HD1DpSyQ1z6xhaWF6KpUVyNqwHhHWJOaE=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=fqWOE/O98IhVs7xSEUl4RAkZhz0pzXDMVhEEExYhYg6zz7NCc5LW0184tnrquHHQV Zky3zc3CjghGJhx16jIhJG32EWrnEZRR5o0oP+pLvQw+u0Jwp9BMbx1jbUcvyv62ID E5cp/ECw03M7LZ21cS1712uxPqENZeX+pTiClhOgMqn07ik2Y9r5Q5eR/sTWfdakqy Sn7D2/Gc4OMzenlWPwCb/qbMdG+yEyVZd7bZ/cBfS0Vv9f0WgaBf3FyvKQE9bTyokb lJEX8OnHQyqZpvh1saaUIhg8bu/lHuod4r4Auv7clUV87sCOkXacUVPRoO7/fAs7qX dmQx0od/ii8Ng== Date: Tue, 10 Oct 2023 17:17:38 +0100 From: Jonathan Cameron To: David Lechner Cc: linux-iio@vger.kernel.org, linux-staging@lists.linux.dev, Michael Hennerich , Nuno =?UTF-8?B?U8Oh?= , Axel Haslam , Philip Molloy , linux-kernel@vger.kernel.org Subject: Re: [PATCH v4 17/17] staging: iio: resolver: ad2s1210: simplify code with guard(mutex) Message-ID: <20231010171738.5a23e66e@jic23-huawei> In-Reply-To: <20231005-ad2s1210-mainline-v4-17-ec00746840fc@baylibre.com> References: <20231005-ad2s1210-mainline-v4-0-ec00746840fc@baylibre.com> <20231005-ad2s1210-mainline-v4-17-ec00746840fc@baylibre.com> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-staging@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Thu, 5 Oct 2023 19:50:34 -0500 David Lechner wrote: > We can simplify the code and get rid of most of the gotos by using > guard(mutex) from cleanup.h. You could consider scoped_guard() for a few cases in here, but perhaps it's better to be consistent and always use the guard() version. There is a small timing question wrt to the gpio manipulation inline. > > Signed-off-by: David Lechner > --- > > v4 changes: New patch in v4. > > drivers/staging/iio/resolver/ad2s1210.c | 157 ++++++++++---------------------- > 1 file changed, 50 insertions(+), 107 deletions(-) > > diff --git a/drivers/staging/iio/resolver/ad2s1210.c b/drivers/staging/iio/resolver/ad2s1210.c > index c4e1bc22e8b0..c4e0ffa92dc2 100644 > --- a/drivers/staging/iio/resolver/ad2s1210.c > +++ b/drivers/staging/iio/resolver/ad2s1210.c > @@ -47,6 +47,7 @@ > > #include > #include > +#include > #include > #include > #include > @@ -404,11 +405,13 @@ static int ad2s1210_single_conversion(struct iio_dev *indio_dev, > s64 timestamp; > int ret; > > - mutex_lock(&st->lock); > + guard(mutex)(&st->lock); > + > gpiod_set_value(st->sample_gpio, 1); > timestamp = iio_get_time_ns(indio_dev); > /* delay (6 * tck + 20) nano seconds */ > udelay(1); > + gpiod_set_value(st->sample_gpio, 0); > > switch (chan->type) { > case IIO_ANGL: > @@ -418,14 +421,13 @@ static int ad2s1210_single_conversion(struct iio_dev *indio_dev, > ret = ad2s1210_set_mode(st, MOD_VEL); > break; > default: > - ret = -EINVAL; > - break; > + return -EINVAL; > } > if (ret < 0) > - goto error_ret; > + return ret; > ret = spi_read(st->sdev, &st->sample, 3); > if (ret < 0) > - goto error_ret; > + return ret; > > switch (chan->type) { > case IIO_ANGL: > @@ -437,17 +439,11 @@ static int ad2s1210_single_conversion(struct iio_dev *indio_dev, > ret = IIO_VAL_INT; > break; > default: > - ret = -EINVAL; > - break; > + return -EINVAL; > } > > ad2s1210_push_events(indio_dev, st->sample.fault, timestamp); > > -error_ret: > - gpiod_set_value(st->sample_gpio, 0); > - /* delay (2 * tck + 20) nano seconds */ > - udelay(1); Dropping this delay isn't obviously safe (though it probably is given stuff done before we exit). I assume there are no rules on holding the gpio down for the register read. If nothing else I think the patch description needs to made an argument for why it is fine. > - mutex_unlock(&st->lock); > return ret; > } >