Skip to content

Commit ee6d3dd

Browse files
wedsonafgregkh
authored andcommitted
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: Wedson Almeida Filho <wedsonaf@google.com> Link: https://lore.kernel.org/r/20211224231345.777370-1-wedsonaf@google.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 67e532a commit ee6d3dd

File tree

6 files changed

+13
-13
lines changed

6 files changed

+13
-13
lines changed

Documentation/core-api/kobject.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Initialization of kobjects
118118
Code which creates a kobject must, of course, initialize that object. Some
119119
of the internal fields are setup with a (mandatory) call to kobject_init()::
120120

121-
void kobject_init(struct kobject *kobj, struct kobj_type *ktype);
121+
void kobject_init(struct kobject *kobj, const struct kobj_type *ktype);
122122

123123
The ktype is required for a kobject to be created properly, as every kobject
124124
must have an associated kobj_type. After calling kobject_init(), to
@@ -156,7 +156,7 @@ kobject_name()::
156156
There is a helper function to both initialize and add the kobject to the
157157
kernel at the same time, called surprisingly enough kobject_init_and_add()::
158158

159-
int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
159+
int kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
160160
struct kobject *parent, const char *fmt, ...);
161161

162162
The arguments are the same as the individual kobject_init() and

drivers/base/bus.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ static struct kobj_type bus_ktype = {
165165

166166
static int bus_uevent_filter(struct kset *kset, struct kobject *kobj)
167167
{
168-
struct kobj_type *ktype = get_ktype(kobj);
168+
const struct kobj_type *ktype = get_ktype(kobj);
169169

170170
if (ktype == &bus_ktype)
171171
return 1;

drivers/base/core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2263,7 +2263,7 @@ static struct kobj_type device_ktype = {
22632263

22642264
static int dev_uevent_filter(struct kset *kset, struct kobject *kobj)
22652265
{
2266-
struct kobj_type *ktype = get_ktype(kobj);
2266+
const struct kobj_type *ktype = get_ktype(kobj);
22672267

22682268
if (ktype == &device_ktype) {
22692269
struct device *dev = kobj_to_dev(kobj);

include/linux/kobject.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ struct kobject {
6666
struct list_head entry;
6767
struct kobject *parent;
6868
struct kset *kset;
69-
struct kobj_type *ktype;
69+
const struct kobj_type *ktype;
7070
struct kernfs_node *sd; /* sysfs directory entry */
7171
struct kref kref;
7272
#ifdef CONFIG_DEBUG_KOBJECT_RELEASE
@@ -90,13 +90,13 @@ static inline const char *kobject_name(const struct kobject *kobj)
9090
return kobj->name;
9191
}
9292

93-
extern void kobject_init(struct kobject *kobj, struct kobj_type *ktype);
93+
extern void kobject_init(struct kobject *kobj, const struct kobj_type *ktype);
9494
extern __printf(3, 4) __must_check
9595
int kobject_add(struct kobject *kobj, struct kobject *parent,
9696
const char *fmt, ...);
9797
extern __printf(4, 5) __must_check
9898
int kobject_init_and_add(struct kobject *kobj,
99-
struct kobj_type *ktype, struct kobject *parent,
99+
const struct kobj_type *ktype, struct kobject *parent,
100100
const char *fmt, ...);
101101

102102
extern void kobject_del(struct kobject *kobj);
@@ -217,7 +217,7 @@ static inline void kset_put(struct kset *k)
217217
kobject_put(&k->kobj);
218218
}
219219

220-
static inline struct kobj_type *get_ktype(struct kobject *kobj)
220+
static inline const struct kobj_type *get_ktype(struct kobject *kobj)
221221
{
222222
return kobj->ktype;
223223
}

kernel/params.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ static const struct sysfs_ops module_sysfs_ops = {
928928

929929
static int uevent_filter(struct kset *kset, struct kobject *kobj)
930930
{
931-
struct kobj_type *ktype = get_ktype(kobj);
931+
const struct kobj_type *ktype = get_ktype(kobj);
932932

933933
if (ktype == &module_ktype)
934934
return 1;

lib/kobject.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ void kobject_get_ownership(struct kobject *kobj, kuid_t *uid, kgid_t *gid)
6565
*/
6666
static int populate_dir(struct kobject *kobj)
6767
{
68-
struct kobj_type *t = get_ktype(kobj);
68+
const struct kobj_type *t = get_ktype(kobj);
6969
struct attribute *attr;
7070
int error = 0;
7171
int i;
@@ -346,7 +346,7 @@ EXPORT_SYMBOL(kobject_set_name);
346346
* to kobject_put(), not by a call to kfree directly to ensure that all of
347347
* the memory is cleaned up properly.
348348
*/
349-
void kobject_init(struct kobject *kobj, struct kobj_type *ktype)
349+
void kobject_init(struct kobject *kobj, const struct kobj_type *ktype)
350350
{
351351
char *err_str;
352352

@@ -461,7 +461,7 @@ EXPORT_SYMBOL(kobject_add);
461461
* same type of error handling after a call to kobject_add() and kobject
462462
* lifetime rules are the same here.
463463
*/
464-
int kobject_init_and_add(struct kobject *kobj, struct kobj_type *ktype,
464+
int kobject_init_and_add(struct kobject *kobj, const struct kobj_type *ktype,
465465
struct kobject *parent, const char *fmt, ...)
466466
{
467467
va_list args;
@@ -679,7 +679,7 @@ EXPORT_SYMBOL(kobject_get_unless_zero);
679679
static void kobject_cleanup(struct kobject *kobj)
680680
{
681681
struct kobject *parent = kobj->parent;
682-
struct kobj_type *t = get_ktype(kobj);
682+
const struct kobj_type *t = get_ktype(kobj);
683683
const char *name = kobj->name;
684684

685685
pr_debug("kobject: '%s' (%p): %s, parent %p\n",

0 commit comments

Comments
 (0)