Skip to contentSkip to conversation

lkdtm: no need to check return value of debugfs_create functions

PATCH In review Greg Kroah-Hartman 2 participants 5 messages Jun 11, 2019 – Jun 12, 2019

Subsystems (2)
lore
Share & tools
lore

Greg Kroah-Hartman

author maintainer
#0 lore raw

PATCH 1/1 lkdtm: no need to check return value of debugfs_create functions

When calling debugfs functions, there is no need to ever check the return value. The function can work or not, but the code logic should never do something different based on this.

---
 drivers/misc/lkdtm/core.c | 13 ++-----------
 1 file changed, 2 insertions(+), 11 deletions(-)
drivers/misc/lkdtm/core.c +2 -11
index 1972dad966f5..bae3b3763f3e 100644
@@ -429,22 +429,13 @@ static int __init lkdtm_module_init(void)
 
 	/* Register debugfs interface */
 	lkdtm_debugfs_root = debugfs_create_dir("provoke-crash", NULL);
-	if (!lkdtm_debugfs_root) {
-		pr_err("creating root dir failed\n");
-		return -ENODEV;
-	}
 
 	/* Install debugfs trigger files. */
 	for (i = 0; i < ARRAY_SIZE(crashpoints); i++) {
 		struct crashpoint *cur = &crashpoints[i];
-		struct dentry *de;
 
-		de = debugfs_create_file(cur->name, 0644, lkdtm_debugfs_root,
-					 cur, &cur->fops);
-		if (de == NULL) {
-			pr_err("could not create crashpoint %s\n", cur->name);
-			goto out_err;
-		}
+		debugfs_create_file(cur->name, 0644, lkdtm_debugfs_root, cur,
+				    &cur->fops);
 	}
 
 	/* Install crashpoint if one was selected. */
Signature · 1 line
2.22.0
#1 lore raw
Quoted 3 lines · Greg Kroah-Hartman

When calling debugfs functions, there is no need to ever check the return value. The function can work or not, but the code logic should never do something different based on this.

What is the user-visible feedback when, say, debugfs_create_file() fails? And what happens when debugfs_create_file() passes in a NULL root?

-Kees

Quoted 41 lines
---
 drivers/misc/lkdtm/core.c | 13 ++-----------
 1 file changed, 2 insertions(+), 11 deletions(-)
drivers/misc/lkdtm/core.c +2 -11
index 1972dad966f5..bae3b3763f3e 100644
@@ -429,22 +429,13 @@ static int __init lkdtm_module_init(void)
 
 	/* Register debugfs interface */
 	lkdtm_debugfs_root = debugfs_create_dir("provoke-crash", NULL);
-	if (!lkdtm_debugfs_root) {
-		pr_err("creating root dir failed\n");
-		return -ENODEV;
-	}
 
 	/* Install debugfs trigger files. */
 	for (i = 0; i < ARRAY_SIZE(crashpoints); i++) {
 		struct crashpoint *cur = &crashpoints[i];
-		struct dentry *de;
 
-		de = debugfs_create_file(cur->name, 0644, lkdtm_debugfs_root,
-					 cur, &cur->fops);
-		if (de == NULL) {
-			pr_err("could not create crashpoint %s\n", cur->name);
-			goto out_err;
-		}
+		debugfs_create_file(cur->name, 0644, lkdtm_debugfs_root, cur,
+				    &cur->fops);
 	}
 
 	/* Install crashpoint if one was selected. */
Signature · 1 line
2.22.0
Signature · 1 line
Kees Cook
author maintainer
#2 lore raw
Quoted 7 lines · Kees Cook
Quoted 3 lines · Greg Kroah-Hartman

When calling debugfs functions, there is no need to ever check the return value. The function can work or not, but the code logic should never do something different based on this.

What is the user-visible feedback when, say, debugfs_create_file() fails?

All of the memory in your system is now gone and it would have long locked up a while before this call ever happened :)

And no user functionality should ever change if a debugfs call fails, or succeeds, this is debugging only.

Quoted 1 line

And what happens when debugfs_create_file() passes in a NULL root?

The file ends up in the root of debugfs. But as this can only happen if the system is dead, I wouldn't worry about it.

thanks,

greg k-h

Acked-by on v1 1/1
#3 lore raw
In reply to Greg Kroah-Hartman
Quoted 8 lines · Greg Kroah-Hartman

When calling debugfs functions, there is no need to ever check the return value. The function can work or not, but the code logic should never do something different based on this.

-Kees

Quoted 36 lines
---
 drivers/misc/lkdtm/core.c | 13 ++-----------
 1 file changed, 2 insertions(+), 11 deletions(-)
drivers/misc/lkdtm/core.c +2 -11
index 1972dad966f5..bae3b3763f3e 100644
@@ -429,22 +429,13 @@ static int __init lkdtm_module_init(void)
 
 	/* Register debugfs interface */
 	lkdtm_debugfs_root = debugfs_create_dir("provoke-crash", NULL);
-	if (!lkdtm_debugfs_root) {
-		pr_err("creating root dir failed\n");
-		return -ENODEV;
-	}
 
 	/* Install debugfs trigger files. */
 	for (i = 0; i < ARRAY_SIZE(crashpoints); i++) {
 		struct crashpoint *cur = &crashpoints[i];
-		struct dentry *de;
 
-		de = debugfs_create_file(cur->name, 0644, lkdtm_debugfs_root,
-					 cur, &cur->fops);
-		if (de == NULL) {
-			pr_err("could not create crashpoint %s\n", cur->name);
-			goto out_err;
-		}
+		debugfs_create_file(cur->name, 0644, lkdtm_debugfs_root, cur,
+				    &cur->fops);
 	}
 
 	/* Install crashpoint if one was selected. */
Signature · 1 line
2.22.0
Signature · 1 line
Kees Cook
author maintainer
#4 lore raw
Quoted 11 lines · Kees Cook
Quoted 8 lines · Greg Kroah-Hartman

When calling debugfs functions, there is no need to ever check the return value. The function can work or not, but the code logic should never do something different based on this.

Thanks for the review!

greg k-h