diff mbox series

[v1,1/2] virtio_fs: introduce virtio_fs_put_locked helper

Message ID 20240825130716.9506-1-mgurtovoy@nvidia.com (mailing list archive)
State New
Headers show
Series [v1,1/2] virtio_fs: introduce virtio_fs_put_locked helper | expand

Commit Message

Max Gurtovoy Aug. 25, 2024, 1:07 p.m. UTC
Introduce a new helper function virtio_fs_put_locked to encapsulate the
common pattern of releasing a virtio_fs reference while holding a lock.
The existing virtio_fs_put helper will be used to release a virtio_fs
reference while not holding a lock.

Also add an assertion in case the lock is not taken when it should.

Reviewed-by: Idan Zach <izach@nvidia.com>
Reviewed-by: Shai Malin <smalin@nvidia.com>
Signed-off-by: Max Gurtovoy <mgurtovoy@nvidia.com>
---
 fs/fuse/virtio_fs.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)

Comments

Michael S. Tsirkin Sept. 5, 2024, 12:11 p.m. UTC | #1
Cc: "Eugenio PĂ©rez" <eperezma@redhat.com>


On Sun, Aug 25, 2024 at 04:07:15PM +0300, Max Gurtovoy wrote:
> Introduce a new helper function virtio_fs_put_locked to encapsulate the
> common pattern of releasing a virtio_fs reference while holding a lock.
> The existing virtio_fs_put helper will be used to release a virtio_fs
> reference while not holding a lock.
> 
> Also add an assertion in case the lock is not taken when it should.
> 
> Reviewed-by: Idan Zach <izach@nvidia.com>
> Reviewed-by: Shai Malin <smalin@nvidia.com>
> Signed-off-by: Max Gurtovoy <mgurtovoy@nvidia.com>
> ---
>  fs/fuse/virtio_fs.c | 17 +++++++++++------
>  1 file changed, 11 insertions(+), 6 deletions(-)
> 
> diff --git a/fs/fuse/virtio_fs.c b/fs/fuse/virtio_fs.c
> index dd5260141615..43f7be1d7887 100644
> --- a/fs/fuse/virtio_fs.c
> +++ b/fs/fuse/virtio_fs.c
> @@ -201,18 +201,25 @@ static const struct kobj_type virtio_fs_ktype = {
>  };
>  
>  /* Make sure virtiofs_mutex is held */
> -static void virtio_fs_put(struct virtio_fs *fs)
> +static void virtio_fs_put_locked(struct virtio_fs *fs)
>  {
> +	lockdep_assert_held(&virtio_fs_mutex);
> +
>  	kobject_put(&fs->kobj);
>  }
>  
> +static void virtio_fs_put(struct virtio_fs *fs)
> +{
> +	mutex_lock(&virtio_fs_mutex);
> +	virtio_fs_put_locked(fs);
> +	mutex_unlock(&virtio_fs_mutex);
> +}
> +
>  static void virtio_fs_fiq_release(struct fuse_iqueue *fiq)
>  {
>  	struct virtio_fs *vfs = fiq->priv;
>  
> -	mutex_lock(&virtio_fs_mutex);
>  	virtio_fs_put(vfs);
> -	mutex_unlock(&virtio_fs_mutex);
>  }
>  
>  static void virtio_fs_drain_queue(struct virtio_fs_vq *fsvq)
> @@ -1052,7 +1059,7 @@ static void virtio_fs_remove(struct virtio_device *vdev)
>  
>  	vdev->priv = NULL;
>  	/* Put device reference on virtio_fs object */
> -	virtio_fs_put(fs);
> +	virtio_fs_put_locked(fs);
>  	mutex_unlock(&virtio_fs_mutex);
>  }
>  
> @@ -1596,9 +1603,7 @@ static int virtio_fs_get_tree(struct fs_context *fsc)
>  
>  out_err:
>  	kfree(fc);
> -	mutex_lock(&virtio_fs_mutex);
>  	virtio_fs_put(fs);
> -	mutex_unlock(&virtio_fs_mutex);
>  	return err;
>  }
>  
> -- 
> 2.18.1
Stefan Hajnoczi Sept. 5, 2024, 2:17 p.m. UTC | #2
On Sun, Aug 25, 2024 at 04:07:15PM +0300, Max Gurtovoy wrote:
> Introduce a new helper function virtio_fs_put_locked to encapsulate the
> common pattern of releasing a virtio_fs reference while holding a lock.
> The existing virtio_fs_put helper will be used to release a virtio_fs
> reference while not holding a lock.
> 
> Also add an assertion in case the lock is not taken when it should.
> 
> Reviewed-by: Idan Zach <izach@nvidia.com>
> Reviewed-by: Shai Malin <smalin@nvidia.com>
> Signed-off-by: Max Gurtovoy <mgurtovoy@nvidia.com>
> ---
>  fs/fuse/virtio_fs.c | 17 +++++++++++------
>  1 file changed, 11 insertions(+), 6 deletions(-)

Reviewed-by: Stefan Hajnoczi <stefanha@redhat.com>
diff mbox series

Patch

diff --git a/fs/fuse/virtio_fs.c b/fs/fuse/virtio_fs.c
index dd5260141615..43f7be1d7887 100644
--- a/fs/fuse/virtio_fs.c
+++ b/fs/fuse/virtio_fs.c
@@ -201,18 +201,25 @@  static const struct kobj_type virtio_fs_ktype = {
 };
 
 /* Make sure virtiofs_mutex is held */
-static void virtio_fs_put(struct virtio_fs *fs)
+static void virtio_fs_put_locked(struct virtio_fs *fs)
 {
+	lockdep_assert_held(&virtio_fs_mutex);
+
 	kobject_put(&fs->kobj);
 }
 
+static void virtio_fs_put(struct virtio_fs *fs)
+{
+	mutex_lock(&virtio_fs_mutex);
+	virtio_fs_put_locked(fs);
+	mutex_unlock(&virtio_fs_mutex);
+}
+
 static void virtio_fs_fiq_release(struct fuse_iqueue *fiq)
 {
 	struct virtio_fs *vfs = fiq->priv;
 
-	mutex_lock(&virtio_fs_mutex);
 	virtio_fs_put(vfs);
-	mutex_unlock(&virtio_fs_mutex);
 }
 
 static void virtio_fs_drain_queue(struct virtio_fs_vq *fsvq)
@@ -1052,7 +1059,7 @@  static void virtio_fs_remove(struct virtio_device *vdev)
 
 	vdev->priv = NULL;
 	/* Put device reference on virtio_fs object */
-	virtio_fs_put(fs);
+	virtio_fs_put_locked(fs);
 	mutex_unlock(&virtio_fs_mutex);
 }
 
@@ -1596,9 +1603,7 @@  static int virtio_fs_get_tree(struct fs_context *fsc)
 
 out_err:
 	kfree(fc);
-	mutex_lock(&virtio_fs_mutex);
 	virtio_fs_put(fs);
-	mutex_unlock(&virtio_fs_mutex);
 	return err;
 }