Skip to content

Commit a169a66

Browse files
zijun-hugregkh
authored andcommitted
driver core: class: Check namespace relevant parameters in class_register()
Device class has two namespace relevant fields which are usually associated by the following usage: struct class { ... const struct kobj_ns_type_operations *ns_type; const void *(*namespace)(const struct device *dev); ... } if (dev->class && dev->class->ns_type) dev->class->namespace(dev); (1) The usage looks weird since it checks @ns_type but calls namespace() (2) The usage implies both fields have dependency but their dependency is not currently enforced yet. It is found for all existing class definitions that the other filed is also assigned once one is assigned in current kernel tree. Fixed by enforcing above existing dependency that both fields are required for a device class to support namespace via parameter checks. Signed-off-by: Zijun Hu <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 4a74f22 commit a169a66

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

drivers/base/class.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,17 @@ int class_register(const struct class *cls)
183183

184184
pr_debug("device class '%s': registering\n", cls->name);
185185

186+
if (cls->ns_type && !cls->namespace) {
187+
pr_err("%s: class '%s' does not have namespace\n",
188+
__func__, cls->name);
189+
return -EINVAL;
190+
}
191+
if (!cls->ns_type && cls->namespace) {
192+
pr_err("%s: class '%s' does not have ns_type\n",
193+
__func__, cls->name);
194+
return -EINVAL;
195+
}
196+
186197
cp = kzalloc(sizeof(*cp), GFP_KERNEL);
187198
if (!cp)
188199
return -ENOMEM;

drivers/base/core.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2584,7 +2584,7 @@ static const void *device_namespace(const struct kobject *kobj)
25842584
const struct device *dev = kobj_to_dev(kobj);
25852585
const void *ns = NULL;
25862586

2587-
if (dev->class && dev->class->ns_type)
2587+
if (dev->class && dev->class->namespace)
25882588
ns = dev->class->namespace(dev);
25892589

25902590
return ns;

0 commit comments

Comments
 (0)