mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Ahmad Fatoum <a.fatoum@pengutronix.de>
Cc: barebox@lists.infradead.org
Subject: Re: [PATCH v2 1/7] driver: introduce less error-prone dev_get_drvdata alternative
Date: Fri, 9 Oct 2020 08:48:20 +0200	[thread overview]
Message-ID: <20201009064820.GT11648@pengutronix.de> (raw)
In-Reply-To: <20201007095058.22950-1-a.fatoum@pengutronix.de>

On Wed, Oct 07, 2020 at 11:50:52AM +0200, Ahmad Fatoum wrote:
> We use dev_get_drvdata to get the driver match data associated with a
> device. This has two shortcomings:
> 
>  - Linux has dev_get_drvdata too, which returns a private pointer for
>    driver specific info to associate with a device. We use dev->priv
>    (or more often container_of) for that in barebox instead
> 
>  - It nearly always involves a cast to a double pointer, which is
>    error-prone as size and alignment match need to be ensured
>    on the programmer's part and can easily be gotten wrong:
> 	enum dev_type type;
> 	dev_get_drvdata(dev, (const void **)&type); // UB!
> 
> Add a new function that instead of using a double pointer argument,
> returns the pointer directly:
> 
>  - For normal pointer driver data, no cast is necessary
>  - For integer driver data casted to a pointer for storage,
>    the cast is still necessary, but it's only a single pointer
>    this way
> 
> Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>

Applied, thanks

Sascha

> ---
> v1 -> v2:
>   - Changed second patch to use pointer instead of enum in match data
>     (Sascha)
>   - Changed third and fourth patch to return error on match with
>     driver name instead of assuming to be compatible with a default
>     type.
> ---
>  drivers/base/driver.c | 11 +++++++++++
>  include/driver.h      | 18 ++++++++++++++++++
>  2 files changed, 29 insertions(+)
> 
> diff --git a/drivers/base/driver.c b/drivers/base/driver.c
> index 412db6c40699..3205bbc3c33b 100644
> --- a/drivers/base/driver.c
> +++ b/drivers/base/driver.c
> @@ -500,3 +500,14 @@ int dev_get_drvdata(struct device_d *dev, const void **data)
>  
>  	return -ENODEV;
>  }
> +
> +const void *device_get_match_data(struct device_d *dev)
> +{
> +	if (dev->of_id_entry)
> +		return dev->of_id_entry->data;
> +
> +	if (dev->id_entry)
> +		return (void *)dev->id_entry->driver_data;
> +
> +	return NULL;
> +}
> diff --git a/include/driver.h b/include/driver.h
> index 1961ab6ed9ea..e2886d051d4f 100644
> --- a/include/driver.h
> +++ b/include/driver.h
> @@ -540,8 +540,26 @@ int devfs_create_partitions(const char *devname,
>  #define DRV_OF_COMPAT(compat) \
>  	IS_ENABLED(CONFIG_OFDEVICE) ? (compat) : NULL
>  
> +/**
> + * dev_get_drvdata - get driver match data associated with device
> + * @dev: device instance
> + * @data: pointer to void *, where match data is stored
> + *
> + * Returns 0 on success and error code otherwise.
> + *
> + * DEPRECATED: use device_get_match_data instead, which avoids
> + * common pitfalls due to explicit pointer casts
> + */
>  int dev_get_drvdata(struct device_d *dev, const void **data);
>  
> +/**
> + * device_get_match_data - get driver match data associated with device
> + * @dev: device instance
> + *
> + * Returns match data on success and NULL otherwise
> + */
> +const void *device_get_match_data(struct device_d *dev);
> +
>  int device_match_of_modalias(struct device_d *dev, struct driver_d *drv);
>  
>  #endif /* DRIVER_H */
> -- 
> 2.28.0
> 
> 
> _______________________________________________
> barebox mailing list
> barebox@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/barebox
> 

-- 
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

      parent reply	other threads:[~2020-10-09  6:48 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-07  9:50 Ahmad Fatoum
2020-10-07  9:50 ` [PATCH v2 2/7] led: pca955x: fix probing from device tree Ahmad Fatoum
2020-10-07  9:50 ` [PATCH v2 3/7] dma: apbh: fix out-of-bounds write on 64-bit SoCs Ahmad Fatoum
2020-10-07  9:50 ` [PATCH v2 4/7] aiodev: lm75: " Ahmad Fatoum
2020-10-07  9:50 ` [PATCH v2 5/7] mtd: nand-mxs: " Ahmad Fatoum
2020-10-07  9:50 ` [PATCH v2 6/7] video: imx-hdmi: fix dev_get_drvdata misuse Ahmad Fatoum
2020-10-07  9:50 ` [PATCH v2 7/7] driver: migrate some from dev_get_drvdata to device_get_match_data Ahmad Fatoum
2020-10-09  6:48 ` Sascha Hauer [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=20201009064820.GT11648@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --subject='Re: [PATCH v2 1/7] driver: introduce less error-prone dev_get_drvdata alternative' \
    /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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox