Commit 7ebe49b7 authored by Greg Kroah-Hartman's avatar Greg Kroah-Hartman

driver core: allow kobj_to_dev() to take a const pointer

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.

Cc: "Rafael J. Wysocki" <rafael@kernel.org>
Reviewed-by: default avatarSakari Ailus <sakari.ailus@linux.intel.com>
Reviewed-by: default avatarAndy Shevchenko <andriy.shevchenko@linux.intel.com>
Link: https://lore.kernel.org/r/20221016104126.1259809-1-gregkh@linuxfoundation.orgSigned-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 9abf2313
......@@ -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
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment