Re: [PATCH 2/2] virtiofs: Improve error handling in virtio_fs_get_tree()

From: Vivek Goyal
Date: Tue Jan 02 2024 - 15:21:52 EST


On Fri, Dec 29, 2023 at 09:38:47AM +0100, Markus Elfring wrote:
> From: Markus Elfring <elfring@xxxxxxxxxxxxxxxxxxxxx>
> Date: Fri, 29 Dec 2023 09:15:07 +0100
>
> The kfree() function was called in two cases by
> the virtio_fs_get_tree() function during error handling
> even if the passed variable contained a null pointer.
> This issue was detected by using the Coccinelle software.
>
> * Thus use another label.
>
> * Move an error code assignment into an if branch.
>
> * Delete an initialisation (for the variable “fc”)
> which became unnecessary with this refactoring.
>
> Signed-off-by: Markus Elfring <elfring@xxxxxxxxxxxxxxxxxxxxx>

As Matthew said that kfree(NULL) is perfectly acceptable usage in kernel,
so I really don't feel that this patch is required. Current code looks
good as it is.

Thanks
Vivek

> ---
> fs/fuse/virtio_fs.c | 13 ++++++++-----
> 1 file changed, 8 insertions(+), 5 deletions(-)
>
> diff --git a/fs/fuse/virtio_fs.c b/fs/fuse/virtio_fs.c
> index 2f8ba9254c1e..0746f54ec743 100644
> --- a/fs/fuse/virtio_fs.c
> +++ b/fs/fuse/virtio_fs.c
> @@ -1415,10 +1415,10 @@ static int virtio_fs_get_tree(struct fs_context *fsc)
> {
> struct virtio_fs *fs;
> struct super_block *sb;
> - struct fuse_conn *fc = NULL;
> + struct fuse_conn *fc;
> struct fuse_mount *fm;
> unsigned int virtqueue_size;
> - int err = -EIO;
> + int err;
>
> /* This gets a reference on virtio_fs object. This ptr gets installed
> * in fc->iq->priv. Once fuse_conn is going away, it calls ->put()
> @@ -1431,13 +1431,15 @@ static int virtio_fs_get_tree(struct fs_context *fsc)
> }
>
> virtqueue_size = virtqueue_get_vring_size(fs->vqs[VQ_REQUEST].vq);
> - if (WARN_ON(virtqueue_size <= FUSE_HEADER_OVERHEAD))
> - goto out_err;
> + if (WARN_ON(virtqueue_size <= FUSE_HEADER_OVERHEAD)) {
> + err = -EIO;
> + goto lock_mutex;
> + }
>
> err = -ENOMEM;
> fc = kzalloc(sizeof(*fc), GFP_KERNEL);
> if (!fc)
> - goto out_err;
> + goto lock_mutex;
>
> fm = kzalloc(sizeof(*fm), GFP_KERNEL);
> if (!fm)
> @@ -1476,6 +1478,7 @@ static int virtio_fs_get_tree(struct fs_context *fsc)
>
> out_err:
> kfree(fc);
> +lock_mutex:
> mutex_lock(&virtio_fs_mutex);
> virtio_fs_put(fs);
> mutex_unlock(&virtio_fs_mutex);
> --
> 2.43.0
>