[PATCH v2] drivers/hid: Check result of debugfs_create_dir() and debugfs_create_file()

Subsystems: hid core layer, the rest

STALE4000d

3 messages, 2 authors, 2015-08-20 · open the first message on its own page

[PATCH v2] drivers/hid: Check result of debugfs_create_dir() and debugfs_create_file()

From: Alexander Kuleshov <hidden>
Date: 2015-08-18 18:35:47

The debugfs_create_dir() and debugfs_create_file() functions may return -errno
if an error occurs. This patch adds a couple of checks of the result of the
debufs_create_dir() and debugfs_create_file() functions execution in the
hid_debug_register() and othre places.

Changelog:

v2:

  * add check for the result of the debugfs_create_file() calls
  * call hid_debug_init() and hid_debug_exit() only if hid_debug
  * add check for the hid_debug_root in the hid_debug_register()

Signed-off-by: Alexander Kuleshov <redacted>
---
 drivers/hid/hid-core.c  | 15 ++++++++++-----
 drivers/hid/hid-debug.c | 38 ++++++++++++++++++++++++++++++++++----
 2 files changed, 44 insertions(+), 9 deletions(-)
diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c
index e6fce23..8aa2c20 100644
--- a/drivers/hid/hid-core.c
+++ b/drivers/hid/hid-core.c
@@ -2599,8 +2599,10 @@ int hid_add_device(struct hid_device *hdev)
 	ret = device_add(&hdev->dev);
 	if (!ret)
 		hdev->status |= HID_STAT_ADDED;
-	else
-		hid_debug_unregister(hdev);
+	else {
+		if (hdev->debug)
+			hid_debug_unregister(hdev);
+	}
 
 	return ret;
 }
@@ -2644,7 +2646,8 @@ static void hid_remove_device(struct hid_device *hdev)
 {
 	if (hdev->status & HID_STAT_ADDED) {
 		device_del(&hdev->dev);
-		hid_debug_unregister(hdev);
+		if (hdev->debug)
+			hid_debug_unregister(hdev);
 		hdev->status &= ~HID_STAT_ADDED;
 	}
 	kfree(hdev->dev_rdesc);
@@ -2737,7 +2740,8 @@ static int __init hid_init(void)
 	if (ret)
 		goto err_bus;
 
-	hid_debug_init();
+	if (hid_debug)
+		hid_debug_init();
 
 	return 0;
 err_bus:
@@ -2748,7 +2752,8 @@ err:
 
 static void __exit hid_exit(void)
 {
-	hid_debug_exit();
+	if (hid_debug)
+		hid_debug_exit();
 	hidraw_exit();
 	bus_unregister(&hid_bus_type);
 }
diff --git a/drivers/hid/hid-debug.c b/drivers/hid/hid-debug.c
index 2886b64..5db1e6e 100644
--- a/drivers/hid/hid-debug.c
+++ b/drivers/hid/hid-debug.c
@@ -1222,30 +1222,60 @@ static const struct file_operations hid_debug_events_fops = {
 
 void hid_debug_register(struct hid_device *hdev, const char *name)
 {
+	if (!hid_debug_root)
+		goto debugfs_root_error
+
 	hdev->debug_dir = debugfs_create_dir(name, hid_debug_root);
+
+	if (!hdev->debug_dir) {
+		printk(KERN_WARNING "Could not create '%s' debugfs directory\n",
+			name);
+		return;
+	}
+
 	hdev->debug_rdesc = debugfs_create_file("rdesc", 0400,
 			hdev->debug_dir, hdev, &hid_debug_rdesc_fops);
+	if (!hdev->debug_rdesc) {
+		printk(KERN_WARNING "Could not create rdesc debugfs file\n");
+		goto error
+	}
+
 	hdev->debug_events = debugfs_create_file("events", 0400,
 			hdev->debug_dir, hdev, &hid_debug_events_fops);
+	if (!hdev->debug_events) {
+		printk(KERN_WARNING "Could not create events debugfs file\n");
+		goto error
+	}
+
 	hdev->debug = 1;
+
+	return;
+
+error:
+	debugfs_remove_recursive(hdev->debug_dir);
+	return;
+
+debugfs_root_error:
+	return;
 }
 
 void hid_debug_unregister(struct hid_device *hdev)
 {
 	hdev->debug = 0;
 	wake_up_interruptible(&hdev->debug_wait);
-	debugfs_remove(hdev->debug_rdesc);
-	debugfs_remove(hdev->debug_events);
-	debugfs_remove(hdev->debug_dir);
+	debugfs_remove_recursive(hdev->debug_dir);
 }
 
 void hid_debug_init(void)
 {
 	hid_debug_root = debugfs_create_dir("hid", NULL);
+	if (!hid_debug_root)
+		printk(KERN_WARNING "Could not create root 'hid' debugfs directory\n");
 }
 
 void hid_debug_exit(void)
 {
-	debugfs_remove_recursive(hid_debug_root);
+	if (hid_debug_root)
+		debugfs_remove_recursive(hid_debug_root);
 }
 
-- 
2.5.0

Re: [PATCH v2] drivers/hid: Check result of debugfs_create_dir() and debugfs_create_file()

From: Jiri Kosina <jikos@kernel.org>
Date: 2015-08-19 20:41:57

On Wed, 19 Aug 2015, Alexander Kuleshov wrote:
quoted hunk
The debugfs_create_dir() and debugfs_create_file() functions may return -errno
if an error occurs. This patch adds a couple of checks of the result of the
debufs_create_dir() and debugfs_create_file() functions execution in the
hid_debug_register() and othre places.

Changelog:

v2:

  * add check for the result of the debugfs_create_file() calls
  * call hid_debug_init() and hid_debug_exit() only if hid_debug
  * add check for the hid_debug_root in the hid_debug_register()

Signed-off-by: Alexander Kuleshov <redacted>
---
 drivers/hid/hid-core.c  | 15 ++++++++++-----
 drivers/hid/hid-debug.c | 38 ++++++++++++++++++++++++++++++++++----
 2 files changed, 44 insertions(+), 9 deletions(-)
diff --git a/drivers/hid/hid-core.c b/drivers/hid/hid-core.c
index e6fce23..8aa2c20 100644
--- a/drivers/hid/hid-core.c
+++ b/drivers/hid/hid-core.c
@@ -2599,8 +2599,10 @@ int hid_add_device(struct hid_device *hdev)
 	ret = device_add(&hdev->dev);
 	if (!ret)
 		hdev->status |= HID_STAT_ADDED;
-	else
-		hid_debug_unregister(hdev);
+	else {
+		if (hdev->debug)
+			hid_debug_unregister(hdev);
+	}
 
 	return ret;
 }
@@ -2644,7 +2646,8 @@ static void hid_remove_device(struct hid_device *hdev)
 {
 	if (hdev->status & HID_STAT_ADDED) {
 		device_del(&hdev->dev);
-		hid_debug_unregister(hdev);
+		if (hdev->debug)
+			hid_debug_unregister(hdev);
 		hdev->status &= ~HID_STAT_ADDED;
 	}
 	kfree(hdev->dev_rdesc);
@@ -2737,7 +2740,8 @@ static int __init hid_init(void)
 	if (ret)
 		goto err_bus;
 
-	hid_debug_init();
+	if (hid_debug)
+		hid_debug_init();
How is this change related to the rest of the patch?

It's wrong anyway, because hid_debug is independent of the debugfs HID 
debugging facility. Please see for example the printk() a few lines above 
in this very function, which is emited in case hid_debug is enabled.

-- 
Jiri Kosina
SUSE Labs

Re: [PATCH v2] drivers/hid: Check result of debugfs_create_dir() and debugfs_create_file()

From: Alexander Kuleshov <hidden>
Date: 2015-08-20 17:39:19

Hello Jiri,

On 08-19-15, Jiri Kosina wrote:
On Wed, 19 Aug 2015, Alexander Kuleshov wrote:
quoted
 
-	hid_debug_init();
+	if (hid_debug)
+		hid_debug_init();
How is this change related to the rest of the patch?

It's wrong anyway, because hid_debug is independent of the debugfs HID 
debugging facility. Please see for example the printk() a few lines above 
in this very function, which is emited in case hid_debug is enabled.
hmm, I thought that these things are related. I've remove all hid_debug check
and sent v3 of the patch.
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help