Re: [PATCH 1/7] driver: introduce less error-prone dev_get_drvdata alternative

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Mon, Oct 05, 2020 at 10:31:32AM +0200, Ahmad Fatoum wrote:
> Hello Sascha,
> 
> On 9/30/20 3:13 PM, Ahmad Fatoum wrote:
> > 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.

ok then, let's go for it.

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@xxxxxxxxxxxxxxxxxxx
http://lists.infradead.org/mailman/listinfo/barebox



[Index of Archives]     [Linux Embedded]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [XFree86]

  Powered by Linux