[PATCH] ksmbd: Use common error handling code in ksmbd_vfs_path_lookup()

Markus Elfring posted 1 patch 4 months, 1 week ago
fs/smb/server/vfs.c | 32 ++++++++++++++------------------
1 file changed, 14 insertions(+), 18 deletions(-)
[PATCH] ksmbd: Use common error handling code in ksmbd_vfs_path_lookup()
Posted by Markus Elfring 4 months, 1 week ago
From: Markus Elfring <elfring@users.sourceforge.net>
Date: Fri, 3 Oct 2025 20:26:56 +0200

Add a jump target so that a bit of exception handling can be better reused
at the end of this function implementation.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 fs/smb/server/vfs.c | 32 ++++++++++++++------------------
 1 file changed, 14 insertions(+), 18 deletions(-)

diff --git a/fs/smb/server/vfs.c b/fs/smb/server/vfs.c
index 891ed2dc2b73..3535655b4d86 100644
--- a/fs/smb/server/vfs.c
+++ b/fs/smb/server/vfs.c
@@ -94,17 +94,13 @@ static int ksmbd_vfs_path_lookup(struct ksmbd_share_config *share_conf,
 	if (err)
 		return err;
 
-	if (unlikely(type != LAST_NORM)) {
-		path_put(path);
-		return -ENOENT;
-	}
+	if (unlikely(type != LAST_NORM))
+		goto put_path;
 
 	if (do_lock) {
 		err = mnt_want_write(path->mnt);
-		if (err) {
-			path_put(path);
-			return -ENOENT;
-		}
+		if (err)
+			goto put_path;
 
 		inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
 		d = lookup_one_qstr_excl(&last, path->dentry, 0);
@@ -116,8 +112,7 @@ static int ksmbd_vfs_path_lookup(struct ksmbd_share_config *share_conf,
 		}
 		inode_unlock(path->dentry->d_inode);
 		mnt_drop_write(path->mnt);
-		path_put(path);
-		return -ENOENT;
+		goto put_path;
 	}
 
 	d = lookup_noperm_unlocked(&last, path->dentry);
@@ -125,21 +120,22 @@ static int ksmbd_vfs_path_lookup(struct ksmbd_share_config *share_conf,
 		dput(d);
 		d = ERR_PTR(-ENOENT);
 	}
-	if (IS_ERR(d)) {
-		path_put(path);
-		return -ENOENT;
-	}
+	if (IS_ERR(d))
+		goto put_path;
+
 	dput(path->dentry);
 	path->dentry = d;
 
 	if (test_share_config_flag(share_conf, KSMBD_SHARE_FLAG_CROSSMNT)) {
 		err = follow_down(path, 0);
-		if (err < 0) {
-			path_put(path);
-			return -ENOENT;
-		}
+		if (err < 0)
+			goto put_path;
 	}
 	return 0;
+
+put_path:
+	path_put(path);
+	return -ENOENT;
 }
 
 void ksmbd_vfs_query_maximal_access(struct mnt_idmap *idmap,
-- 
2.51.0
Re: [PATCH] ksmbd: Use common error handling code in ksmbd_vfs_path_lookup()
Posted by NeilBrown 4 months ago
On Sat, 04 Oct 2025, Markus Elfring wrote:
> From: Markus Elfring <elfring@users.sourceforge.net>
> Date: Fri, 3 Oct 2025 20:26:56 +0200
> 
> Add a jump target so that a bit of exception handling can be better reused
> at the end of this function implementation.

I think this is a good cleanup - thanks.  But I think it could be even
better.

- rename the "path" parameter to "return_path" or similar.
- declare  struct path path __free(path_-put) = {};
- change all "path->" instances to "path."
- remove all those path_put() calls, but leave the "return xxx"
- at the point of successful return, use

   return_path->dentry = no_free_ptr(path.dentry);
   return_path->mnt = no_free_ptr(path.mnt);
   return 0;

This is based on the pattern in kern_path_parent() and
__start_removing_path().

Thanks,
NeilBrown


> 
> Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
> ---
>  fs/smb/server/vfs.c | 32 ++++++++++++++------------------
>  1 file changed, 14 insertions(+), 18 deletions(-)
> 
> diff --git a/fs/smb/server/vfs.c b/fs/smb/server/vfs.c
> index 891ed2dc2b73..3535655b4d86 100644
> --- a/fs/smb/server/vfs.c
> +++ b/fs/smb/server/vfs.c
> @@ -94,17 +94,13 @@ static int ksmbd_vfs_path_lookup(struct ksmbd_share_config *share_conf,
>  	if (err)
>  		return err;
>  
> -	if (unlikely(type != LAST_NORM)) {
> -		path_put(path);
> -		return -ENOENT;
> -	}
> +	if (unlikely(type != LAST_NORM))
> +		goto put_path;
>  
>  	if (do_lock) {
>  		err = mnt_want_write(path->mnt);
> -		if (err) {
> -			path_put(path);
> -			return -ENOENT;
> -		}
> +		if (err)
> +			goto put_path;
>  
>  		inode_lock_nested(path->dentry->d_inode, I_MUTEX_PARENT);
>  		d = lookup_one_qstr_excl(&last, path->dentry, 0);
> @@ -116,8 +112,7 @@ static int ksmbd_vfs_path_lookup(struct ksmbd_share_config *share_conf,
>  		}
>  		inode_unlock(path->dentry->d_inode);
>  		mnt_drop_write(path->mnt);
> -		path_put(path);
> -		return -ENOENT;
> +		goto put_path;
>  	}
>  
>  	d = lookup_noperm_unlocked(&last, path->dentry);
> @@ -125,21 +120,22 @@ static int ksmbd_vfs_path_lookup(struct ksmbd_share_config *share_conf,
>  		dput(d);
>  		d = ERR_PTR(-ENOENT);
>  	}
> -	if (IS_ERR(d)) {
> -		path_put(path);
> -		return -ENOENT;
> -	}
> +	if (IS_ERR(d))
> +		goto put_path;
> +
>  	dput(path->dentry);
>  	path->dentry = d;
>  
>  	if (test_share_config_flag(share_conf, KSMBD_SHARE_FLAG_CROSSMNT)) {
>  		err = follow_down(path, 0);
> -		if (err < 0) {
> -			path_put(path);
> -			return -ENOENT;
> -		}
> +		if (err < 0)
> +			goto put_path;
>  	}
>  	return 0;
> +
> +put_path:
> +	path_put(path);
> +	return -ENOENT;
>  }
>  
>  void ksmbd_vfs_query_maximal_access(struct mnt_idmap *idmap,
> -- 
> 2.51.0
> 
> 
> 
Re: ksmbd: Use common error handling code in ksmbd_vfs_path_lookup()
Posted by Markus Elfring 4 months ago
…> - declare  struct path path __free(path_-put) = {};
…>    return_path->dentry = no_free_ptr(path.dentry);
>    return_path->mnt = no_free_ptr(path.mnt);
>    return 0;
> 
> This is based on the pattern in kern_path_parent() and
> __start_removing_path().

Do you propose that affected software components may benefit more from
the application of scope-based resource management?
https://elixir.bootlin.com/linux/v6.17/source/include/linux/path.h#L22-L28

Regards,
Markus
Re: ksmbd: Use common error handling code in ksmbd_vfs_path_lookup()
Posted by NeilBrown 4 months ago
On Sat, 04 Oct 2025, Markus Elfring wrote:
> …> - declare  struct path path __free(path_-put) = {};
> …>    return_path->dentry = no_free_ptr(path.dentry);
> >    return_path->mnt = no_free_ptr(path.mnt);
> >    return 0;
> > 
> > This is based on the pattern in kern_path_parent() and
> > __start_removing_path().
> 
> Do you propose that affected software components may benefit more from
> the application of scope-based resource management?
> https://elixir.bootlin.com/linux/v6.17/source/include/linux/path.h#L22-L28

Exactly.  It doesn't suit every case, but if you are going to make
changes to the exit paths of a function, I think it is worth
considering if scope-based code will work well for the particular
function.

Since v6.17 there has already been a net increase of 167 uses of __free
(though some might be in comments....) and 1902 more uses for guard().
So at least some people think it is a good idea.

NeilBrown
Re: ksmbd: Use common error handling code in ksmbd_vfs_path_lookup()
Posted by Markus Elfring 4 months ago
>>> This is based on the pattern in kern_path_parent() and
>>> __start_removing_path().

You influenced the software evolution also according to the availability
of a function like kern_path_locked_negative().
https://elixir.bootlin.com/linux/v6.17/source/fs/namei.c#L2770-L2792

See also the following commits:
* 76a53de6f7ff0641570364234fb4489f4d4fc8e9 ("VFS/audit: introduce kern_path_parent()
  for audit") from 2025-09-23

* a681b7c17dd21d5aa0da391ceb27a2007ba970a4 ("fs: ensure that *path_locked*() helpers
  leave passed path pristine") from 2025-04-16


>> Do you propose that affected software components may benefit more from
>> the application of scope-based resource management?
>> https://elixir.bootlin.com/linux/v6.17/source/include/linux/path.h#L22-L28
> 
> Exactly.  It doesn't suit every case, but if you are going to make
> changes to the exit paths of a function, I think it is worth
> considering if scope-based code will work well for the particular
> function.

Is there a need to clarify possibilities for the usage of the macro “__free_path_put” further?

Regards,
Markus