[PATCH] fuse: don't set file->private_data in fuse_conn_waiting_read

Jeff Layton posted 1 patch 10 months, 1 week ago
fs/fuse/control.c | 5 ++---
1 file changed, 2 insertions(+), 3 deletions(-)
[PATCH] fuse: don't set file->private_data in fuse_conn_waiting_read
Posted by Jeff Layton 10 months, 1 week ago
I see no reason to set the private_data on the file to this value. Just
grab the result of the atomic_read() and output it without setting
private_data.

Signed-off-by: Jeff Layton <jlayton@kernel.org>
---
 fs/fuse/control.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/fs/fuse/control.c b/fs/fuse/control.c
index 2a730d88cc3bdb50ea1f8a3185faad5f05fc6e74..17ef07cf0c38e44bd7eadb3450bd53a8acc5e885 100644
--- a/fs/fuse/control.c
+++ b/fs/fuse/control.c
@@ -49,18 +49,17 @@ static ssize_t fuse_conn_waiting_read(struct file *file, char __user *buf,
 {
 	char tmp[32];
 	size_t size;
+	int value;
 
 	if (!*ppos) {
-		long value;
 		struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
 		if (!fc)
 			return 0;
 
 		value = atomic_read(&fc->num_waiting);
-		file->private_data = (void *)value;
 		fuse_conn_put(fc);
 	}
-	size = sprintf(tmp, "%ld\n", (long)file->private_data);
+	size = sprintf(tmp, "%d\n", value);
 	return simple_read_from_buffer(buf, len, ppos, tmp, size);
 }
 

---
base-commit: 9afd7336f3acbe5678cca3b3bc5baefb51ce9564
change-id: 20250204-fuse-fixes-03882c05c1b1

Best regards,
-- 
Jeff Layton <jlayton@kernel.org>
Re: [PATCH] fuse: don't set file->private_data in fuse_conn_waiting_read
Posted by Miklos Szeredi 10 months ago
On Tue, 4 Feb 2025 at 16:04, Jeff Layton <jlayton@kernel.org> wrote:
>
> I see no reason to set the private_data on the file to this value. Just
> grab the result of the atomic_read() and output it without setting
> private_data.
>
> Signed-off-by: Jeff Layton <jlayton@kernel.org>
> ---
>  fs/fuse/control.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
>
> diff --git a/fs/fuse/control.c b/fs/fuse/control.c
> index 2a730d88cc3bdb50ea1f8a3185faad5f05fc6e74..17ef07cf0c38e44bd7eadb3450bd53a8acc5e885 100644
> --- a/fs/fuse/control.c
> +++ b/fs/fuse/control.c
> @@ -49,18 +49,17 @@ static ssize_t fuse_conn_waiting_read(struct file *file, char __user *buf,
>  {
>         char tmp[32];
>         size_t size;
> +       int value;
>
>         if (!*ppos) {
> -               long value;
>                 struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
>                 if (!fc)
>                         return 0;
>
>                 value = atomic_read(&fc->num_waiting);
> -               file->private_data = (void *)value;
>                 fuse_conn_put(fc);
>         }

"value" is uninitialized if *ppos is non-zero.

I also wonder why this patch is an improvement (with the bug fixed)?

Thanks,
Mikos
Re: [PATCH] fuse: don't set file->private_data in fuse_conn_waiting_read
Posted by Jeff Layton 10 months ago
On Fri, 2025-02-14 at 11:17 +0100, Miklos Szeredi wrote:
> On Tue, 4 Feb 2025 at 16:04, Jeff Layton <jlayton@kernel.org> wrote:
> > 
> > I see no reason to set the private_data on the file to this value. Just
> > grab the result of the atomic_read() and output it without setting
> > private_data.
> > 
> > Signed-off-by: Jeff Layton <jlayton@kernel.org>
> > ---
> >  fs/fuse/control.c | 5 ++---
> >  1 file changed, 2 insertions(+), 3 deletions(-)
> > 
> > diff --git a/fs/fuse/control.c b/fs/fuse/control.c
> > index 2a730d88cc3bdb50ea1f8a3185faad5f05fc6e74..17ef07cf0c38e44bd7eadb3450bd53a8acc5e885 100644
> > --- a/fs/fuse/control.c
> > +++ b/fs/fuse/control.c
> > @@ -49,18 +49,17 @@ static ssize_t fuse_conn_waiting_read(struct file *file, char __user *buf,
> >  {
> >         char tmp[32];
> >         size_t size;
> > +       int value;
> > 
> >         if (!*ppos) {
> > -               long value;
> >                 struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
> >                 if (!fc)
> >                         return 0;
> > 
> >                 value = atomic_read(&fc->num_waiting);
> > -               file->private_data = (void *)value;
> >                 fuse_conn_put(fc);
> >         }
> 
> "value" is uninitialized if *ppos is non-zero.
> 

Good catch. Thanks. Do you want me to resend?

> I also wonder why this patch is an improvement (with the bug fixed)?
> 

It's just an unnecessary assignment. Nothing will look at or use
private_data in this codepath, so there is no need to set it.
-- 
Jeff Layton <jlayton@kernel.org>
Re: [PATCH] fuse: don't set file->private_data in fuse_conn_waiting_read
Posted by Miklos Szeredi 10 months ago
On Fri, 14 Feb 2025 at 13:58, Jeff Layton <jlayton@kernel.org> wrote:

> It's just an unnecessary assignment. Nothing will look at or use
> private_data in this codepath, so there is no need to set it.

I think the reason it was done this way is to return a sane value even
in the case of small reads (i.e. char-by-char).  It's unlikely to
matter in real life, but removing it is not a big enough win to be
worth risking it.

Thanks,
Miklos
Re: [PATCH] fuse: don't set file->private_data in fuse_conn_waiting_read
Posted by Joanne Koong 10 months, 1 week ago
On Tue, Feb 4, 2025 at 7:09 AM Jeff Layton <jlayton@kernel.org> wrote:
>
> I see no reason to set the private_data on the file to this value. Just
> grab the result of the atomic_read() and output it without setting
> private_data.
>
> Signed-off-by: Jeff Layton <jlayton@kernel.org>

Reviewed-by: Joanne Koong <joannelkoong@gmail.com>

> ---
>  fs/fuse/control.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
>
> diff --git a/fs/fuse/control.c b/fs/fuse/control.c
> index 2a730d88cc3bdb50ea1f8a3185faad5f05fc6e74..17ef07cf0c38e44bd7eadb3450bd53a8acc5e885 100644
> --- a/fs/fuse/control.c
> +++ b/fs/fuse/control.c
> @@ -49,18 +49,17 @@ static ssize_t fuse_conn_waiting_read(struct file *file, char __user *buf,
>  {
>         char tmp[32];
>         size_t size;
> +       int value;
>

It might be helpful if a "if (*ppos) return -EINVAL;" check gets added here too?


>         if (!*ppos) {
> -               long value;
>                 struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
>                 if (!fc)
>                         return 0;
>
>                 value = atomic_read(&fc->num_waiting);
> -               file->private_data = (void *)value;
>                 fuse_conn_put(fc);
>         }
> -       size = sprintf(tmp, "%ld\n", (long)file->private_data);
> +       size = sprintf(tmp, "%d\n", value);
>         return simple_read_from_buffer(buf, len, ppos, tmp, size);
>  }
>
>
> ---
> base-commit: 9afd7336f3acbe5678cca3b3bc5baefb51ce9564
> change-id: 20250204-fuse-fixes-03882c05c1b1
>
> Best regards,
> --
> Jeff Layton <jlayton@kernel.org>
>
>


>         if (!*ppos) {
> -               long value;
>                 struct fuse_conn *fc = fuse_ctl_file_conn_get(file);
>                 if (!fc)
>                         return 0;
>
>                 value = atomic_read(&fc->num_waiting);
> -               file->private_data = (void *)value;
>                 fuse_conn_put(fc);
>         }
> -       size = sprintf(tmp, "%ld\n", (long)file->private_data);
> +       size = sprintf(tmp, "%d\n", value);
>         return simple_read_from_buffer(buf, len, ppos, tmp, size);
>  }
>
>
> ---
> base-commit: 9afd7336f3acbe5678cca3b3bc5baefb51ce9564
> change-id: 20250204-fuse-fixes-03882c05c1b1
>
> Best regards,
> --
> Jeff Layton <jlayton@kernel.org>
>
>