linux-fbdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Tomi Valkeinen <tomi.valkeinen@ti.com>
To: linux-fbdev@vger.kernel.org
Subject: Re: [PATCH 3/5] fbdev: omap2: panel-dpi: drop assignment to local variable
Date: Tue, 22 Dec 2015 08:56:07 +0000	[thread overview]
Message-ID: <56791027.2040305@ti.com> (raw)
In-Reply-To: <1449753107-11410-3-git-send-email-uwe@kleine-koenig.org>

[-- Attachment #1: Type: text/plain, Size: 5905 bytes --]


On 20/12/15 12:31, Uwe Kleine-König wrote:
> Hello,
> 
> On Wed, Dec 16, 2015 at 07:29:17PM +0200, Tomi Valkeinen wrote:
>> On 10/12/15 15:11, Uwe Kleine-König wrote:
>>> From: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
>>>
>>> The variable gpio is only used to store the return value of
>>> devm_gpiod_get_optional just to assign it to a member of the driver
>>> data.
>>>
>>> Get rid of this local variable and assign to driver data directly.
>>>
>>> Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
>>> ---
>>>  drivers/video/fbdev/omap2/displays-new/panel-dpi.c | 10 ++++------
>>>  1 file changed, 4 insertions(+), 6 deletions(-)
>>>
>>> diff --git a/drivers/video/fbdev/omap2/displays-new/panel-dpi.c b/drivers/video/fbdev/omap2/displays-new/panel-dpi.c
>>> index e780fd4f8b46..1216341a0d19 100644
>>> --- a/drivers/video/fbdev/omap2/displays-new/panel-dpi.c
>>> +++ b/drivers/video/fbdev/omap2/displays-new/panel-dpi.c
>>> @@ -205,13 +205,11 @@ static int panel_dpi_probe_of(struct platform_device *pdev)
>>>  	int r;
>>>  	struct display_timing timing;
>>>  	struct videomode vm;
>>> -	struct gpio_desc *gpio;
>>>  
>>> -	gpio = devm_gpiod_get_optional(&pdev->dev, "enable", GPIOD_OUT_LOW);
>>> -	if (IS_ERR(gpio))
>>> -		return PTR_ERR(gpio);
>>> -
>>> -	ddata->enable_gpio = gpio;
>>> +	ddata->enable_gpio = devm_gpiod_get_optional(&pdev->dev,
>>> +						     "enable", GPIOD_OUT_LOW);
>>> +	if (IS_ERR(ddata->enable_gpio))
>>> +		return PTR_ERR(ddata->enable_gpio);
>>>  
>>>  	ddata->backlight_gpio = -ENOENT;
>>
>> I usually try to avoid writing bad values to fields. Here
>> ddata->enable_gpio may get an error ptr. It probably doesn't matter as
>> we bail out right away, but still. If devm_gpiod_get_optional's return
>> value would be NULL or valid gpio_desc*, then it'd be fine.
> 
> this is probably a matter of taste but still I don't see why people
> don't like writing to structs immediately.

I don't have a problem with writing to struct immediately. My point was
that I don't like writing invalid values to "long term storage".

What's "invalid" is of course up to the case, but here I think it's
quite clear that 'ddata->enable_gpio' should either be a valid gpiod or
NULL.

Generally, I also like to work on a resource/object in local variables
until it's "ready", and only then push it to the public storage. In my
experience that style results in less possibilities for bugs and confusion.

> With the local variable you might have
> 
> 	gpio = -ESOMETHING
> 
> and
> 
> 	ddata->enable_gpio = NULL;
> 
> In the case that the error is handled correctly it doesn't matter if the
> value was written to the struct or not (if you accept a little
> performance penalty for writing the value actually to memory maybe). So
> the motivation is the consideration that the error might not be handled
> correctly after a later patch, right? But when ddata->enable_gpio is a
> negative error code this probably results in a crash already during
> development of the faulty patch, while when the struct's member isn't
> assigned it probably doesn't.

Yes, my motivation is that later patches may change the behavior, and
keeping a valid value in 'data->enable_gpio' may make future patches
simpler and prevent problems.

I think it's quite common to do cleanups later, using the values in the
long term storage. Here it might mean setting enable-gpio to 0, or if
devm_* was not used, freeing the gpio, and that would result in using an
error ptr as a pointer to gpiod.

And it's quite common to later change error handling slightly so that
instead of failing, the driver continues if it doesn't actually require
the resource that it failed to get.

Of course, patches for both can and should be written correctly so that
both local and direct write to struct work correctly.

> This convinces me that writing to the struct is actually a good thing.
> 
> Additionally even though the line length of 
> 
> 	gpio = devm_gpiod_get_optional(&pdev->dev, "enable", GPIOD_OUT_LOW);
> 	if (IS_ERR(gpio))
> 		return PTR_ERR(gpio);
> 
> 	ddata->enable_gpio = gpio;
> 
> is shorter (which is good), with my approach of doing:
> 
> 	ddata->enable_gpio = devm_gpiod_get_optional(&pdev->dev,
> 						     "enable", GPIOD_OUT_LOW);
> 	if (IS_ERR(ddata->enable_gpio))
> 		return PTR_ERR(ddata->enable_gpio);

No big diff here, but I can easily imagine this turning in some future
patch into:

ddata->enable_gpio = devm_gpiod_get_optional(&pdev->dev,
					     "enable", GPIOD_OUT_LOW);
if (IS_ERR(ddata->enable_gpio)) {
	r = PTR_ERR(data->enable_gpio);
	ddata->enable_gpio = NULL;
	return r;
}

> apart from saving an assignment also "enable_gpio" and "enable" are
> nearer to each other which IMHO makes it easier to see that the
> assignment is correct which outweighs the longer lines. This argument
> even gets more important when reset_gpio is added in patch 4 when the
> situation looks as follows:
> 
> 	gpio = devm_gpiod_get_optional(... "enable" ...);
> 	if (IS_ERR(gpio))
> 		...
> 	ddata->enable_gpio = gpio;
> 
> 	gpio = devm_gpiod_get_optional(... "reset" ...);
> 	if (IS_ERR(gpio))
> 		...
> 	ddata->reset_gpio = gpio;

It is, of course, possible to use more exactly named locals instead of a
single 'gpio'.

> vs.
> 
> 	ddata->enable_gpio = devm_gpiod_get_optional(... "enable" ...);
> 	if (IS_ERR(ddata->enable_gpio))
> 		...
> 
> 	ddata->reset_gpio = devm_gpiod_get_optional(... "reset" ...);
> 	if (IS_ERR(ddata->reset_gpio))
> 		...
> 
> I like my approach better, but if you don't agree, I don't care enough
> to argue (more).

I don't agree, but I don't care too much either =). Both ways are valid,
and they can always be changed later if the driver changes enough to
require that change.

 Tomi


[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 819 bytes --]

      parent reply	other threads:[~2015-12-22  8:56 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-12-10 13:11 [PATCH 3/5] fbdev: omap2: panel-dpi: drop assignment to local variable Uwe Kleine-König
2015-12-16 17:29 ` Tomi Valkeinen
2015-12-20 10:31 ` Uwe Kleine-König
2015-12-22  8:56 ` Tomi Valkeinen [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=56791027.2040305@ti.com \
    --to=tomi.valkeinen@ti.com \
    --cc=linux-fbdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).