diff mbox series

[v2,09/17] firmware: turris-mox-rwtm: Simplify debugfs code

Message ID 20240613161045.29606-10-kabel@kernel.org (mailing list archive)
State Superseded
Headers show
Series Updates for turris-mox-rwtm driver | expand

Commit Message

Marek Behún June 13, 2024, 4:10 p.m. UTC
Simplify debugfs code: do not check for errors, as debugfs errors should
be ignored, and use devm action for dropping the debugfs directory.

Signed-off-by: Marek Behún <kabel@kernel.org>
---
 drivers/firmware/turris-mox-rwtm.c | 44 ++++++++----------------------
 1 file changed, 11 insertions(+), 33 deletions(-)

Comments

Andy Shevchenko June 13, 2024, 8:37 p.m. UTC | #1
On Thu, Jun 13, 2024 at 6:11 PM Marek Behún <kabel@kernel.org> wrote:
>
> Simplify debugfs code: do not check for errors, as debugfs errors should
> be ignored, and use devm action for dropping the debugfs directory.

...

> -static void rwtm_unregister_debugfs(struct mox_rwtm *rwtm)
> +static void rwtm_register_debugfs(struct mox_rwtm *rwtm)
>  {
> -       debugfs_remove_recursive(rwtm->debugfs_root);
> +       struct dentry *root;
> +
> +       root = debugfs_create_dir("turris-mox-rwtm", NULL);
> +
> +       devm_add_action_or_reset(rwtm->dev, rwtm_debugfs_release, root);

This is incorrect. If devm_add_action() fails, the root will be removed...

> +       debugfs_create_file_unsafe("do_sign", 0600, root, rwtm, &do_sign_fops);

...and this most likely will use the dangling pointer.

>  }
Marek Behún June 17, 2024, 11:10 a.m. UTC | #2
On Thu, 13 Jun 2024 22:37:39 +0200
Andy Shevchenko <andy.shevchenko@gmail.com> wrote:

> On Thu, Jun 13, 2024 at 6:11 PM Marek Behún <kabel@kernel.org> wrote:
> >
> > Simplify debugfs code: do not check for errors, as debugfs errors should
> > be ignored, and use devm action for dropping the debugfs directory.  
> 
> ...
> 
> > -static void rwtm_unregister_debugfs(struct mox_rwtm *rwtm)
> > +static void rwtm_register_debugfs(struct mox_rwtm *rwtm)
> >  {
> > -       debugfs_remove_recursive(rwtm->debugfs_root);
> > +       struct dentry *root;
> > +
> > +       root = debugfs_create_dir("turris-mox-rwtm", NULL);
> > +
> > +       devm_add_action_or_reset(rwtm->dev, rwtm_debugfs_release, root);  
> 
> This is incorrect. If devm_add_action() fails, the root will be removed...
> 
> > +       debugfs_create_file_unsafe("do_sign", 0600, root, rwtm, &do_sign_fops);  
> 
> ...and this most likely will use the dangling pointer.

OK, I will first creae the file and then add the action.
diff mbox series

Patch

diff --git a/drivers/firmware/turris-mox-rwtm.c b/drivers/firmware/turris-mox-rwtm.c
index d17dc0679439..a536c9c461a7 100644
--- a/drivers/firmware/turris-mox-rwtm.c
+++ b/drivers/firmware/turris-mox-rwtm.c
@@ -90,7 +90,6 @@  struct mox_rwtm {
 	 * It should be rewritten via crypto API once akcipher API is available
 	 * from userspace.
 	 */
-	struct dentry *debugfs_root;
 	u32 last_sig[MOX_ECC_SIGNATURE_WORDS];
 	bool last_sig_done;
 #endif
@@ -405,39 +404,23 @@  static const struct file_operations do_sign_fops = {
 	.llseek	= no_llseek,
 };
 
-static int rwtm_register_debugfs(struct mox_rwtm *rwtm)
+static void rwtm_debugfs_release(void *root)
 {
-	struct dentry *root, *entry;
-
-	root = debugfs_create_dir("turris-mox-rwtm", NULL);
-
-	if (IS_ERR(root))
-		return PTR_ERR(root);
-
-	entry = debugfs_create_file_unsafe("do_sign", 0600, root, rwtm,
-					   &do_sign_fops);
-	if (IS_ERR(entry))
-		goto err_remove;
-
-	rwtm->debugfs_root = root;
-
-	return 0;
-err_remove:
 	debugfs_remove_recursive(root);
-	return PTR_ERR(entry);
 }
 
-static void rwtm_unregister_debugfs(struct mox_rwtm *rwtm)
+static void rwtm_register_debugfs(struct mox_rwtm *rwtm)
 {
-	debugfs_remove_recursive(rwtm->debugfs_root);
+	struct dentry *root;
+
+	root = debugfs_create_dir("turris-mox-rwtm", NULL);
+
+	devm_add_action_or_reset(rwtm->dev, rwtm_debugfs_release, root);
+
+	debugfs_create_file_unsafe("do_sign", 0600, root, rwtm, &do_sign_fops);
 }
 #else
-static inline int rwtm_register_debugfs(struct mox_rwtm *rwtm)
-{
-	return 0;
-}
-
-static inline void rwtm_unregister_debugfs(struct mox_rwtm *rwtm)
+static inline void rwtm_register_debugfs(struct mox_rwtm *rwtm)
 {
 }
 #endif
@@ -502,11 +485,7 @@  static int turris_mox_rwtm_probe(struct platform_device *pdev)
 		goto free_channel;
 	}
 
-	ret = rwtm_register_debugfs(rwtm);
-	if (ret < 0) {
-		dev_err(dev, "Failed creating debugfs entries: %i\n", ret);
-		goto free_channel;
-	}
+	rwtm_register_debugfs(rwtm);
 
 	dev_info(dev, "HWRNG successfully registered\n");
 
@@ -529,7 +508,6 @@  static void turris_mox_rwtm_remove(struct platform_device *pdev)
 {
 	struct mox_rwtm *rwtm = platform_get_drvdata(pdev);
 
-	rwtm_unregister_debugfs(rwtm);
 	mbox_free_channel(rwtm->mbox);
 }