Re: [PATCH v2 1/5] device property: Keep dev_fwnode() and dev_fwnode_const() separate

Greg Kroah-Hartman
2022-10-04T08:24:24Z

On Tue, Oct 04, 2022 at 11:14:14AM +0300, Andy Shevchenko wrote:
> On Tue, Oct 04, 2022 at 09:55:21AM +0200, Greg Kroah-Hartman wrote:
> > On Mon, Oct 03, 2022 at 08:08:58PM +0000, Sakari Ailus wrote:
> > > On Mon, Oct 03, 2022 at 06:17:17PM +0200, Greg Kroah-Hartman wrote:
>
> ...
>
> > > #define kobj_to_dev(kobj) \
> > > (_Generic((kobj), \
> > > const struct kobject *: __kobj_to_dev_const, \
> > > struct kobject *: __kobj_to_dev)(kobj))
> >
> > Ah, doh! I had the (kobj) part in the wrong place, thanks for that
> > fix...
> >
> > Ok, this looks better, let me see how well the build breaks with some of
> > these changes
>
> I believe I can rewrite my patch like this and then it will be much nicer since
> we may constify all the rest without calling __dev_fwnode_const() directly.
>
> Are you agree?

Yes, I think this is a much better option, try it and see. Below is the
patch I'm running through my build systems at the moment, feel free to
copy the style for your change as well.

thanks,

greg k-h

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