Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

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 <quic_zijuhu@quicinc.com>
Link: https://lore.kernel.org/r/20240822-class_fix-v1-1-2a6d38ba913a@quicinc.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Zijun Hu and committed by
Greg Kroah-Hartman
a169a663 4a74f223

+12 -1
+11
drivers/base/class.c
··· 183 183 184 184 pr_debug("device class '%s': registering\n", cls->name); 185 185 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 + 186 197 cp = kzalloc(sizeof(*cp), GFP_KERNEL); 187 198 if (!cp) 188 199 return -ENOMEM;
+1 -1
drivers/base/core.c
··· 2584 2584 const struct device *dev = kobj_to_dev(kobj); 2585 2585 const void *ns = NULL; 2586 2586 2587 - if (dev->class && dev->class->ns_type) 2587 + if (dev->class && dev->class->namespace) 2588 2588 ns = dev->class->namespace(dev); 2589 2589 2590 2590 return ns;