driver core: device: mark struct class in struct device as constant
authorGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Mon, 13 Mar 2023 18:18:42 +0000 (19:18 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 17 Mar 2023 14:16:48 +0000 (15:16 +0100)
The pointer to a struct class in a struct device should never be used to
change anything in that class.  So mark it as constant to enforce this
requirement.

This requires a few minor changes to some internal driver core functions
to enforce the const * being used here now.

Cc: "Rafael J. Wysocki" <rafael@kernel.org>
Link: https://lore.kernel.org/r/20230313181843.1207845-11-gregkh@linuxfoundation.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/base/core.c
include/linux/device.h

index f3b7040..0970db6 100644 (file)
@@ -2820,7 +2820,7 @@ EXPORT_SYMBOL_GPL(devm_device_add_groups);
 
 static int device_add_attrs(struct device *dev)
 {
-       struct class *class = dev->class;
+       const struct class *class = dev->class;
        const struct device_type *type = dev->type;
        int error;
 
@@ -2887,7 +2887,7 @@ static int device_add_attrs(struct device *dev)
 
 static void device_remove_attrs(struct device *dev)
 {
-       struct class *class = dev->class;
+       const struct class *class = dev->class;
        const struct device_type *type = dev->type;
 
        if (dev->physical_location) {
@@ -3120,7 +3120,7 @@ struct kobject *virtual_device_parent(struct device *dev)
 
 struct class_dir {
        struct kobject kobj;
-       struct class *class;
+       const struct class *class;
 };
 
 #define to_class_dir(obj) container_of(obj, struct class_dir, kobj)
@@ -3145,7 +3145,7 @@ static const struct kobj_type class_dir_ktype = {
 };
 
 static struct kobject *
-class_dir_create_and_add(struct class *class, struct kobject *parent_kobj)
+class_dir_create_and_add(const struct class *class, struct kobject *parent_kobj)
 {
        struct class_dir *dir;
        int retval;
@@ -4580,7 +4580,7 @@ static int device_attrs_change_owner(struct device *dev, kuid_t kuid,
                                     kgid_t kgid)
 {
        struct kobject *kobj = &dev->kobj;
-       struct class *class = dev->class;
+       const struct class *class = dev->class;
        const struct device_type *type = dev->type;
        int error;
 
index 2562451..ea07b95 100644 (file)
@@ -631,7 +631,7 @@ struct device {
        spinlock_t              devres_lock;
        struct list_head        devres_head;
 
-       struct class            *class;
+       const struct class      *class;
        const struct attribute_group **groups;  /* optional groups */
 
        void    (*release)(struct device *dev);