]> www.infradead.org Git - linux.git/commitdiff
driver core: class: Check namespace relevant parameters in class_register()
authorZijun Hu <quic_zijuhu@quicinc.com>
Thu, 22 Aug 2024 12:38:35 +0000 (20:38 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Tue, 3 Sep 2024 11:00:20 +0000 (13:00 +0200)
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>
drivers/base/class.c
drivers/base/core.c

index ae22fa992c049d38fd0a279321b71a38213fa9f5..cb5359235c7020cd6b79c4d64c2f894f92352fca 100644 (file)
@@ -183,6 +183,17 @@ int class_register(const struct class *cls)
 
        pr_debug("device class '%s': registering\n", cls->name);
 
+       if (cls->ns_type && !cls->namespace) {
+               pr_err("%s: class '%s' does not have namespace\n",
+                      __func__, cls->name);
+               return -EINVAL;
+       }
+       if (!cls->ns_type && cls->namespace) {
+               pr_err("%s: class '%s' does not have ns_type\n",
+                      __func__, cls->name);
+               return -EINVAL;
+       }
+
        cp = kzalloc(sizeof(*cp), GFP_KERNEL);
        if (!cp)
                return -ENOMEM;
index ec2197aec0b7228aa7efc8816c120142ab0d94fe..30408a4a4927aa75158075a2729ae07304ff6361 100644 (file)
@@ -2584,7 +2584,7 @@ static const void *device_namespace(const struct kobject *kobj)
        const struct device *dev = kobj_to_dev(kobj);
        const void *ns = NULL;
 
-       if (dev->class && dev->class->ns_type)
+       if (dev->class && dev->class->namespace)
                ns = dev->class->namespace(dev);
 
        return ns;