Commit ee6d3dd4 authored by Wedson Almeida Filho's avatar Wedson Almeida Filho Committed by Greg Kroah-Hartman

driver core: make kobj_type constant.

This way instances of kobj_type (which contain function pointers) can be
stored in .rodata, which means that they cannot be [easily/accidentally]
modified at runtime.
Signed-off-by: default avatarWedson Almeida Filho <wedsonaf@google.com>
Link: https://lore.kernel.org/r/20211224231345.777370-1-wedsonaf@google.comSigned-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 67e532a4
...@@ -118,7 +118,7 @@ Initialization of kobjects ...@@ -118,7 +118,7 @@ Initialization of kobjects
Code which creates a kobject must, of course, initialize that object. Some Code which creates a kobject must, of course, initialize that object. Some
of the internal fields are setup with a (mandatory) call to kobject_init():: of the internal fields are setup with a (mandatory) call to kobject_init()::
void kobject_init(struct kobject *kobj, struct kobj_type *ktype); void kobject_init(struct kobject *kobj, const struct kobj_type *ktype);
The ktype is required for a kobject to be created properly, as every kobject The ktype is required for a kobject to be created properly, as every kobject
must have an associated kobj_type. After calling kobject_init(), to must have an associated kobj_type. After calling kobject_init(), to
...@@ -156,7 +156,7 @@ kobject_name():: ...@@ -156,7 +156,7 @@ kobject_name()::
There is a helper function to both initialize and add the kobject to the There is a helper function to both initialize and add the kobject to the
kernel at the same time, called surprisingly enough kobject_init_and_add():: kernel at the same time, called surprisingly enough kobject_init_and_add()::
int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype, int kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
struct kobject *parent, const char *fmt, ...); struct kobject *parent, const char *fmt, ...);
The arguments are the same as the individual kobject_init() and The arguments are the same as the individual kobject_init() and
......
...@@ -165,7 +165,7 @@ static struct kobj_type bus_ktype = { ...@@ -165,7 +165,7 @@ static struct kobj_type bus_ktype = {
static int bus_uevent_filter(struct kset *kset, struct kobject *kobj) static int bus_uevent_filter(struct kset *kset, struct kobject *kobj)
{ {
struct kobj_type *ktype = get_ktype(kobj); const struct kobj_type *ktype = get_ktype(kobj);
if (ktype == &bus_ktype) if (ktype == &bus_ktype)
return 1; return 1;
......
...@@ -2263,7 +2263,7 @@ static struct kobj_type device_ktype = { ...@@ -2263,7 +2263,7 @@ static struct kobj_type device_ktype = {
static int dev_uevent_filter(struct kset *kset, struct kobject *kobj) static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
{ {
struct kobj_type *ktype = get_ktype(kobj); const struct kobj_type *ktype = get_ktype(kobj);
if (ktype == &device_ktype) { if (ktype == &device_ktype) {
struct device *dev = kobj_to_dev(kobj); struct device *dev = kobj_to_dev(kobj);
......
...@@ -66,7 +66,7 @@ struct kobject { ...@@ -66,7 +66,7 @@ struct kobject {
struct list_head entry; struct list_head entry;
struct kobject *parent; struct kobject *parent;
struct kset *kset; struct kset *kset;
struct kobj_type *ktype; const struct kobj_type *ktype;
struct kernfs_node *sd; /* sysfs directory entry */ struct kernfs_node *sd; /* sysfs directory entry */
struct kref kref; struct kref kref;
#ifdef CONFIG_DEBUG_KOBJECT_RELEASE #ifdef CONFIG_DEBUG_KOBJECT_RELEASE
...@@ -90,13 +90,13 @@ static inline const char *kobject_name(const struct kobject *kobj) ...@@ -90,13 +90,13 @@ static inline const char *kobject_name(const struct kobject *kobj)
return kobj->name; return kobj->name;
} }
extern void kobject_init(struct kobject *kobj, struct kobj_type *ktype); extern void kobject_init(struct kobject *kobj, const struct kobj_type *ktype);
extern __printf(3, 4) __must_check extern __printf(3, 4) __must_check
int kobject_add(struct kobject *kobj, struct kobject *parent, int kobject_add(struct kobject *kobj, struct kobject *parent,
const char *fmt, ...); const char *fmt, ...);
extern __printf(4, 5) __must_check extern __printf(4, 5) __must_check
int kobject_init_and_add(struct kobject *kobj, int kobject_init_and_add(struct kobject *kobj,
struct kobj_type *ktype, struct kobject *parent, const struct kobj_type *ktype, struct kobject *parent,
const char *fmt, ...); const char *fmt, ...);
extern void kobject_del(struct kobject *kobj); extern void kobject_del(struct kobject *kobj);
...@@ -217,7 +217,7 @@ static inline void kset_put(struct kset *k) ...@@ -217,7 +217,7 @@ static inline void kset_put(struct kset *k)
kobject_put(&k->kobj); kobject_put(&k->kobj);
} }
static inline struct kobj_type *get_ktype(struct kobject *kobj) static inline const struct kobj_type *get_ktype(struct kobject *kobj)
{ {
return kobj->ktype; return kobj->ktype;
} }
......
...@@ -928,7 +928,7 @@ static const struct sysfs_ops module_sysfs_ops = { ...@@ -928,7 +928,7 @@ static const struct sysfs_ops module_sysfs_ops = {
static int uevent_filter(struct kset *kset, struct kobject *kobj) static int uevent_filter(struct kset *kset, struct kobject *kobj)
{ {
struct kobj_type *ktype = get_ktype(kobj); const struct kobj_type *ktype = get_ktype(kobj);
if (ktype == &module_ktype) if (ktype == &module_ktype)
return 1; return 1;
......
...@@ -65,7 +65,7 @@ void kobject_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid) ...@@ -65,7 +65,7 @@ void kobject_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
*/ */
static int populate_dir(struct kobject *kobj) static int populate_dir(struct kobject *kobj)
{ {
struct kobj_type *t = get_ktype(kobj); const struct kobj_type *t = get_ktype(kobj);
struct attribute *attr; struct attribute *attr;
int error = 0; int error = 0;
int i; int i;
...@@ -346,7 +346,7 @@ EXPORT_SYMBOL(kobject_set_name); ...@@ -346,7 +346,7 @@ EXPORT_SYMBOL(kobject_set_name);
* to kobject_put(), not by a call to kfree directly to ensure that all of * to kobject_put(), not by a call to kfree directly to ensure that all of
* the memory is cleaned up properly. * the memory is cleaned up properly.
*/ */
void kobject_init(struct kobject *kobj, struct kobj_type *ktype) void kobject_init(struct kobject *kobj, const struct kobj_type *ktype)
{ {
char *err_str; char *err_str;
...@@ -461,7 +461,7 @@ EXPORT_SYMBOL(kobject_add); ...@@ -461,7 +461,7 @@ EXPORT_SYMBOL(kobject_add);
* same type of error handling after a call to kobject_add() and kobject * same type of error handling after a call to kobject_add() and kobject
* lifetime rules are the same here. * lifetime rules are the same here.
*/ */
int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype, int kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
struct kobject *parent, const char *fmt, ...) struct kobject *parent, const char *fmt, ...)
{ {
va_list args; va_list args;
...@@ -679,7 +679,7 @@ EXPORT_SYMBOL(kobject_get_unless_zero); ...@@ -679,7 +679,7 @@ EXPORT_SYMBOL(kobject_get_unless_zero);
static void kobject_cleanup(struct kobject *kobj) static void kobject_cleanup(struct kobject *kobj)
{ {
struct kobject *parent = kobj->parent; struct kobject *parent = kobj->parent;
struct kobj_type *t = get_ktype(kobj); const struct kobj_type *t = get_ktype(kobj);
const char *name = kobj->name; const char *name = kobj->name;
pr_debug("kobject: '%s' (%p): %s, parent %p\n", pr_debug("kobject: '%s' (%p): %s, parent %p\n",
......
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