mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: Sascha Hauer <s.hauer@pengutronix.de>
Cc: barebox@lists.infradead.org
Subject: Re: [PATCH 1/7] driver: introduce less error-prone dev_get_drvdata alternative
Date: Wed, 30 Sep 2020 15:13:42 +0200	[thread overview]
Message-ID: <ba127a9d-1db2-9960-5522-14a93035e62e@pengutronix.de> (raw)
In-Reply-To: <20200930074847.GH11648@pengutronix.de>

Hi,

On 9/30/20 9:48 AM, Sascha Hauer wrote:
>> If the match data is a valid pointer:
>>  -> It doesn't matter, why we have no match data either way.
>>
>> If the match data is a casted integer (e.g. enum):
>>  The driver author should either:
>>    -> place the default enum value as first one,
>>       so no match data => default
> 
> You know that, but "The driver Author" probably doesn't.

Like with all other functions that return an error code,
the author should check those errors:

enum lm75_type type = (enum lm75_type)device_get_match_data(dev);
if (type == 0)
   return -ENODEV;

I'd expect they will see that 0 shouldn't be part of the enumeration.

>>    -> should add an initial DEVICE_TYPE_UNKNOWN = 0 in the enum
>>       and handle it appropriately
>>
>> I like the function signature like that, I don't really see
>> a need to adjust it.
>>
>>> As you realize in your series some drivers cast the returned value into
>>> an integer type and use 0 as a valid possibility. These need an extra
>>> explanation why we can accept that case. I can think of different
>>> possibilies to get that straight:
>>>
>>> - Put a real pointer into matchdata. This is really preferred as it
>>>   motivates people to put flags into a (struct type) matchdata which
>>>   doesn't lead to excessive if (type == foo || type == bar || type ==
>>>   baz) we sometimes see in drivers.
>>
>> We have a real pointer there already. The problem is migrating the
>> existing drivers.
> 
> Yes, existing drivers would have to be migrated, that is exactly what I
> am proposing.
> 
>>
>>> - Return an ERR_PTR from device_get_match_data(). this is less likely
>>>   interpreted as a valid int value
>>
>> Doesn't cover all cases. Also for the normal use, it means
>> you need to have to check with IS_ERR_OR_NULL everywhere to
>> be sure you don't dereference a NULL pointer.
> 
> Why that? Just don't return NULL when there's no match data, but return
> -ESOMETHING.

For the lm75 we would have to do:

enum lm75_type { TYPE_1, TYPE_2 };

const void *match = device_get_match_data(dev);
if (IS_ERR(match))
   return PTR_ERR(match);
enum lm75_type type = (enum lm75_type)match;

The alternative being:

enum lm75_type { TYPE_UNKNOWN = 0, TYPE_1, TYPE_2 };

enum lm75_type type = (enum lm75_type)device_get_match_data(dev);
if (type == TYPE_UNKNOWN)
   return -ENODEV;

I prefer the second one very much.

> 
> Sascha
> 

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  reply	other threads:[~2020-09-30 13:13 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-28 15:42 Ahmad Fatoum
2020-09-28 15:42 ` [PATCH 2/7] led: pca955x: fix probing from device tree Ahmad Fatoum
2020-09-29  7:19   ` Sascha Hauer
2020-09-30  7:28     ` Ahmad Fatoum
2020-09-28 15:42 ` [PATCH 3/7] dma: apbh: fix out-of-bounds write on 64-bit SoCs Ahmad Fatoum
2020-09-28 15:42 ` [PATCH 4/7] aiodev: lm75: " Ahmad Fatoum
2020-09-28 15:42 ` [PATCH 5/7] mtd: nand-mxs: " Ahmad Fatoum
2020-09-28 16:01   ` Marco Felsch
2020-09-28 16:03     ` Ahmad Fatoum
2020-09-28 16:08       ` Marco Felsch
2020-09-28 15:42 ` [PATCH 6/7] video: imx-hdmi: fix dev_get_drvdata misuse Ahmad Fatoum
2020-09-28 15:42 ` [PATCH 7/7] driver: migrate some from dev_get_drvdata to device_get_match_data Ahmad Fatoum
2020-09-28 15:46 ` [PATCH 1/7] driver: introduce less error-prone dev_get_drvdata alternative Ahmad Fatoum
2020-09-29  6:45 ` Sascha Hauer
2020-09-29  7:32 ` Sascha Hauer
2020-09-29  9:42   ` Ahmad Fatoum
2020-09-30  7:48     ` Sascha Hauer
2020-09-30 13:13       ` Ahmad Fatoum [this message]
2020-10-05  8:31         ` Ahmad Fatoum
2020-10-07  8:43           ` Sascha Hauer

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=ba127a9d-1db2-9960-5522-14a93035e62e@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=s.hauer@pengutronix.de \
    /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