[<prev] [next>] [day] [month] [year] [list]
Message-Id: <20240822-class_fix-v1-1-2a6d38ba913a@quicinc.com>
Date: Thu, 22 Aug 2024 20:38:35 +0800
From: Zijun Hu <zijun_hu@...oud.com>
To: Greg Kroah-Hartman <gregkh@...uxfoundation.org>,
"Rafael J. Wysocki" <rafael@...nel.org>
Cc: Zijun Hu <zijun_hu@...oud.com>, linux-kernel@...r.kernel.org,
Zijun Hu <quic_zijuhu@...cinc.com>
Subject: [PATCH] driver core: class: Check namespace relevant parameters in
class_register()
From: Zijun Hu <quic_zijuhu@...cinc.com>
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@...cinc.com>
---
drivers/base/class.c | 11 +++++++++++
drivers/base/core.c | 2 +-
2 files changed, 12 insertions(+), 1 deletion(-)
diff --git a/drivers/base/class.c b/drivers/base/class.c
index 7b38fdf8e1d7..49612609de80 100644
--- a/drivers/base/class.c
+++ b/drivers/base/class.c
@@ -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;
diff --git a/drivers/base/core.c b/drivers/base/core.c
index 6d3897492285..2f616423959f 100644
--- a/drivers/base/core.c
+++ b/drivers/base/core.c
@@ -2595,7 +2595,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;
---
base-commit: 18ec12c97b39ff6aa15beb8d2b25d15cd44b87d8
change-id: 20240819-class_fix-3a3ad277f4d2
Best regards,
--
Zijun Hu <quic_zijuhu@...cinc.com>
Powered by blists - more mailing lists