From: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Cc: linux-kernel@vger.kernel.org,
Sakari Ailus <sakari.ailus@linux.intel.com>,
"Rafael J. Wysocki" <rafael@kernel.org>
Subject: Re: [PATCH v2] driver core: allow kobj_to_dev() to take a const pointer
Date: Mon, 17 Oct 2022 12:24:44 +0300 [thread overview]
Message-ID: <Y00fXHoJc9mPH4/A@smile.fi.intel.com> (raw)
In-Reply-To: <20221016104126.1259809-1-gregkh@linuxfoundation.org>
On Sun, Oct 16, 2022 at 12:41:26PM +0200, Greg Kroah-Hartman wrote:
> If a const * to a kobject is passed to kobj_to_dev(), we want to return
> back a const * to a device as the driver core shouldn't be modifying a
> constant structure. But when dealing with container_of() the pointer
> const attribute is cast away, so we need to manually handle this by
> determining the type of the pointer passed in to know the type of the
> pointer to pass out.
>
> Luckily _Generic can do this type of magic, and as the kernel now
> supports C11 it is availble to us to handle this type of build-time type
> detection.
I was following this in your branch and I find it good,
Reviewed-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> Cc: Sakari Ailus <sakari.ailus@linux.intel.com>
> Cc: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
> Cc: "Rafael J. Wysocki" <rafael@kernel.org>
> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> ---
> v2 - use _Generic() to make this type safe as pointed out by Sakari
>
> include/linux/device.h | 18 +++++++++++++++++-
> 1 file changed, 17 insertions(+), 1 deletion(-)
>
> diff --git a/include/linux/device.h b/include/linux/device.h
> index 424b55df0272..023ea50b1916 100644
> --- a/include/linux/device.h
> +++ b/include/linux/device.h
> @@ -680,11 +680,27 @@ struct device_link {
> bool supplier_preactivated; /* Owned by consumer probe. */
> };
>
> -static inline struct device *kobj_to_dev(struct kobject *kobj)
> +static inline struct device *__kobj_to_dev(struct kobject *kobj)
> {
> return container_of(kobj, struct device, kobj);
> }
>
> +static inline const struct device *__kobj_to_dev_const(const struct kobject *kobj)
> +{
> + return container_of(kobj, const struct device, kobj);
> +}
> +
> +/*
> + * container_of() will happily take a const * and spit back a non-const * as it
> + * is just doing pointer math. But we want to be a bit more careful in the
> + * driver code, so manually force any const * of a kobject to also be a const *
> + * to a device.
> + */
> +#define kobj_to_dev(kobj) \
> + _Generic((kobj), \
> + const struct kobject *: __kobj_to_dev_const, \
> + struct kobject *: __kobj_to_dev)(kobj)
> +
> /**
> * device_iommu_mapped - Returns true when the device DMA is translated
> * by an IOMMU
> --
> 2.38.0
>
--
With Best Regards,
Andy Shevchenko
prev parent reply other threads:[~2022-10-17 9:25 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-10-16 10:41 [PATCH v2] driver core: allow kobj_to_dev() to take a const pointer Greg Kroah-Hartman
2022-10-17 7:54 ` Sakari Ailus
2022-10-17 8:04 ` Greg Kroah-Hartman
2022-10-17 9:39 ` Sakari Ailus
2022-10-17 11:24 ` David Laight
2022-10-17 11:32 ` 'Greg Kroah-Hartman'
2022-10-17 9:24 ` Andy Shevchenko [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=Y00fXHoJc9mPH4/A@smile.fi.intel.com \
--to=andriy.shevchenko@linux.intel.com \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rafael@kernel.org \
--cc=sakari.ailus@linux.intel.com \
/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