[PATCH v3 0/3+1] fanotify accounting for fs/splice.c

Ahelenia Ziemiańska posted 1 patch 2 years, 7 months ago
There is a newer version of this series
fs/splice.c | 43 +++++++++++++++++++++++++------------------
1 file changed, 25 insertions(+), 18 deletions(-)
[PATCH v3 0/3+1] fanotify accounting for fs/splice.c
Posted by Ahelenia Ziemiańska 2 years, 7 months ago
In 1/3 I've applied if/else if/else tree like you said,
and expounded a bit in the message.

This is less pretty now, however, since it turns out that
iter_file_splice_write() already marks the out fd as written because it
writes to it via vfs_iter_write(), and that sent a double notification.

$ git grep -F .splice_write | grep -v iter_file_splice_write
drivers/char/mem.c:     .splice_write   = splice_write_null,
drivers/char/virtio_console.c:  .splice_write = port_fops_splice_write,
fs/fuse/dev.c:  .splice_write   = fuse_dev_splice_write,
fs/gfs2/file.c: .splice_write   = gfs2_file_splice_write,
fs/gfs2/file.c: .splice_write   = gfs2_file_splice_write,
fs/overlayfs/file.c:    .splice_write   = ovl_splice_write,
net/socket.c:   .splice_write = generic_splice_sendpage,
scripts/coccinelle/api/stream_open.cocci:    .splice_write = splice_write_f,

Of these, splice_write_null() doesn't mark out as written
(but it's for /dev/null so I think this is expected),
and I haven't been able to visually confirm whether
port_fops_splice_write() and generic_splice_sendpage() do.

All the others delegate to iter_file_splice_write().

In 2/3 I fixed the vmsplice notification placement
(access from pipe, modify to pipe).

I'm following this up with an LTP patch, where only sendfile_file_to_pipe
passes on 6.1.27-1 and all tests pass on v6.4 + this patchset.

Ahelenia Ziemiańska (3):
  splice: always fsnotify_access(in), fsnotify_modify(out) on success
  splice: fsnotify_access(fd)/fsnotify_modify(fd) in vmsplice
  splice: fsnotify_access(in), fsnotify_modify(out) on success in tee

 fs/splice.c | 43 +++++++++++++++++++++++++------------------
 1 file changed, 25 insertions(+), 18 deletions(-)


Interdiff against v2:
diff --git a/fs/splice.c b/fs/splice.c
index 3234aaa6e957..0427f0a91c7d 100644
--- a/fs/splice.c
+++ b/fs/splice.c
@@ -1155,10 +1155,7 @@ long do_splice(struct file *in, loff_t *off_in, struct file *out,
 			flags |= SPLICE_F_NONBLOCK;
 
 		ret = splice_pipe_to_pipe(ipipe, opipe, len, flags);
-		goto notify;
-	}
-
-	if (ipipe) {
+	} else if (ipipe) {
 		if (off_in)
 			return -ESPIPE;
 		if (off_out) {
@@ -1188,10 +1185,10 @@ long do_splice(struct file *in, loff_t *off_in, struct file *out,
 		else
 			*off_out = offset;
 
-		goto notify;
-	}
-
-	if (opipe) {
+		// ->splice_write already marked out
+		// as modified via vfs_iter_write()
+		goto noaccessout;
+	} else if (opipe) {
 		if (off_out)
 			return -ESPIPE;
 		if (off_in) {
@@ -1211,17 +1208,14 @@ long do_splice(struct file *in, loff_t *off_in, struct file *out,
 			in->f_pos = offset;
 		else
 			*off_in = offset;
+	} else
+		return -EINVAL;
 
-		goto notify;
-	}
-
-	return -EINVAL;
-
-notify:
-	if (ret > 0) {
-		fsnotify_access(in);
+	if (ret > 0)
 		fsnotify_modify(out);
-	}
+noaccessout:
+	if (ret > 0)
+		fsnotify_access(in);
 
 	return ret;
 }
@@ -1352,6 +1346,9 @@ static long vmsplice_to_user(struct file *file, struct iov_iter *iter,
 		pipe_unlock(pipe);
 	}
 
+	if (ret > 0)
+		fsnotify_access(file);
+
 	return ret;
 }
 
@@ -1381,8 +1378,10 @@ static long vmsplice_to_pipe(struct file *file, struct iov_iter *iter,
 	if (!ret)
 		ret = iter_to_pipe(iter, pipe, buf_flag);
 	pipe_unlock(pipe);
-	if (ret > 0)
+	if (ret > 0) {
 		wakeup_pipe_readers(pipe);
+		fsnotify_modify(file);
+	}
 	return ret;
 }
 
@@ -1447,9 +1446,6 @@ SYSCALL_DEFINE4(vmsplice, int, fd, const struct iovec __user *, uiov,
 	else
 		error = vmsplice_to_user(f.file, &iter, flags);
 
-	if (error > 0)
-		fsnotify_modify(f.file);
-
 	kfree(iov);
 out_fdput:
 	fdput(f);
-- 
2.39.2
Re: [PATCH v3 0/3+1] fanotify accounting for fs/splice.c
Posted by Christoph Hellwig 2 years, 7 months ago
Can you please resend this outside this thread?  I really cant't see
what's new or old here if you have a reply-to in the old thread.

On Tue, Jun 27, 2023 at 06:55:22PM +0200, Ahelenia Ziemiańska wrote:
> In 1/3 I've applied if/else if/else tree like you said,
> and expounded a bit in the message.
> 
> This is less pretty now, however, since it turns out that
> iter_file_splice_write() already marks the out fd as written because it
> writes to it via vfs_iter_write(), and that sent a double notification.

It seems like vfs_iter_write is the wrong level to implement
->splice_write given that the the ->splice_write caller has already
checked f_mode, done the equivalent of rw_verify_area and
should do the fsnotify_modify.  I'd suggest to just open code the
relevant parts of vfs_iocb_iter_write in iter_file_splice_write.
Re: [PATCH v3 0/3+1] fanotify accounting for fs/splice.c
Posted by Jan Kara 2 years, 7 months ago
On Tue 27-06-23 21:51:05, Christoph Hellwig wrote:
> Can you please resend this outside this thread?  I really cant't see
> what's new or old here if you have a reply-to in the old thread.
> 
> On Tue, Jun 27, 2023 at 06:55:22PM +0200, Ahelenia Ziemiańska wrote:
> > In 1/3 I've applied if/else if/else tree like you said,
> > and expounded a bit in the message.
> > 
> > This is less pretty now, however, since it turns out that
> > iter_file_splice_write() already marks the out fd as written because it
> > writes to it via vfs_iter_write(), and that sent a double notification.
> 
> It seems like vfs_iter_write is the wrong level to implement
> ->splice_write given that the the ->splice_write caller has already
> checked f_mode, done the equivalent of rw_verify_area and
> should do the fsnotify_modify.  I'd suggest to just open code the
> relevant parts of vfs_iocb_iter_write in iter_file_splice_write.

Yeah, looking into the code I agree (with a small remark that unlike
vfs_iocb_iter_write() this particular variant also needs to work with files
providing only ->write and not ->write_iter). But we can live with
duplicate events for now and this seems like a rather separate cleanup to
do.

								Honza
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR
Re: [PATCH v3 0/3+1] fanotify accounting for fs/splice.c
Posted by Amir Goldstein 2 years, 7 months ago
On Tue, Jun 27, 2023 at 7:55 PM Ahelenia Ziemiańska
<nabijaczleweli@nabijaczleweli.xyz> wrote:
>
> In 1/3 I've applied if/else if/else tree like you said,
> and expounded a bit in the message.
>
> This is less pretty now, however, since it turns out that

If my advice turns out to be bad, then please drop it.

> iter_file_splice_write() already marks the out fd as written because it
> writes to it via vfs_iter_write(), and that sent a double notification.
>
> $ git grep -F .splice_write | grep -v iter_file_splice_write
> drivers/char/mem.c:     .splice_write   = splice_write_null,
> drivers/char/virtio_console.c:  .splice_write = port_fops_splice_write,
> fs/fuse/dev.c:  .splice_write   = fuse_dev_splice_write,
> fs/gfs2/file.c: .splice_write   = gfs2_file_splice_write,
> fs/gfs2/file.c: .splice_write   = gfs2_file_splice_write,
> fs/overlayfs/file.c:    .splice_write   = ovl_splice_write,
> net/socket.c:   .splice_write = generic_splice_sendpage,
> scripts/coccinelle/api/stream_open.cocci:    .splice_write = splice_write_f,
>
> Of these, splice_write_null() doesn't mark out as written
> (but it's for /dev/null so I think this is expected),
> and I haven't been able to visually confirm whether
> port_fops_splice_write() and generic_splice_sendpage() do.
>
> All the others delegate to iter_file_splice_write().
>

All this is very troubling to me.
It translates to a mental model that I cannot remember and
cannot maintain for fixes whose value are still questionable.

IIUC, the only thing you need to change in do_splice() for
making your use case work is to add fsnotify_modify()
for the splice_pipe_to_pipe() case. Right?

So either make the change that you need, or all the changes
that are simple to follow without trying to make the world
consistent - these pipe iterators business is really messy.
I don't know if avoiding a double event (which is likely not visible)
is worth the complicated code that is hard to understand.

> In 2/3 I fixed the vmsplice notification placement
> (access from pipe, modify to pipe).
>
> I'm following this up with an LTP patch, where only sendfile_file_to_pipe
> passes on 6.1.27-1 and all tests pass on v6.4 + this patchset.
>

Were these tests able to detect the double event?
Maybe it's not visible because double consequent events get merged.

> Ahelenia Ziemiańska (3):
>   splice: always fsnotify_access(in), fsnotify_modify(out) on success
>   splice: fsnotify_access(fd)/fsnotify_modify(fd) in vmsplice
>   splice: fsnotify_access(in), fsnotify_modify(out) on success in tee
>
>  fs/splice.c | 43 +++++++++++++++++++++++++------------------
>  1 file changed, 25 insertions(+), 18 deletions(-)
>
>
> Interdiff against v2:
> diff --git a/fs/splice.c b/fs/splice.c
> index 3234aaa6e957..0427f0a91c7d 100644
> --- a/fs/splice.c
> +++ b/fs/splice.c
> @@ -1155,10 +1155,7 @@ long do_splice(struct file *in, loff_t *off_in, struct file *out,
>                         flags |= SPLICE_F_NONBLOCK;
>
>                 ret = splice_pipe_to_pipe(ipipe, opipe, len, flags);
> -               goto notify;
> -       }
> -
> -       if (ipipe) {
> +       } else if (ipipe) {
>                 if (off_in)
>                         return -ESPIPE;
>                 if (off_out) {
> @@ -1188,10 +1185,10 @@ long do_splice(struct file *in, loff_t *off_in, struct file *out,
>                 else
>                         *off_out = offset;
>
> -               goto notify;
> -       }
> -
> -       if (opipe) {
> +               // ->splice_write already marked out
> +               // as modified via vfs_iter_write()
> +               goto noaccessout;

That's too ugly IMO.
Are you claiming that the code in master is wrong?
Because in master there is fsnotify_modify(out) for (ipipe) case.

> +       } else if (opipe) {
>                 if (off_out)
>                         return -ESPIPE;
>                 if (off_in) {
> @@ -1211,17 +1208,14 @@ long do_splice(struct file *in, loff_t *off_in, struct file *out,
>                         in->f_pos = offset;
>                 else
>                         *off_in = offset;
> +       } else
> +               return -EINVAL;
>
> -               goto notify;
> -       }
> -
> -       return -EINVAL;
> -
> -notify:
> -       if (ret > 0) {
> -               fsnotify_access(in);
> +       if (ret > 0)
>                 fsnotify_modify(out);
> -       }
> +noaccessout:
> +       if (ret > 0)
> +               fsnotify_access(in);
>

Not to mention that it should be nomodifyout, but I dislike this
"common" code that it not common at all, so either just handle
the pipe_to_pipe case to fix your use case or leave this code
completely common ignoring the possible double events.

Thanks,
Amir.
Re: [PATCH v3 0/3+1] fanotify accounting for fs/splice.c
Posted by Ahelenia Ziemiańska 2 years, 7 months ago
On Tue, Jun 27, 2023 at 09:03:17PM +0300, Amir Goldstein wrote:
> On Tue, Jun 27, 2023 at 7:55 PM Ahelenia Ziemiańska
> <nabijaczleweli@nabijaczleweli.xyz> wrote:
> >
> > In 1/3 I've applied if/else if/else tree like you said,
> > and expounded a bit in the message.
> >
> > This is less pretty now, however, since it turns out that
> If my advice turns out to be bad, then please drop it.
The if/else if/else with no goto is better than before;
it was made ugly by the special-casing below.

> > iter_file_splice_write() already marks the out fd as written because it
> > writes to it via vfs_iter_write(), and that sent a double notification.
> >
> > $ git grep -F .splice_write | grep -v iter_file_splice_write
> > drivers/char/mem.c:     .splice_write   = splice_write_null,
> > drivers/char/virtio_console.c:  .splice_write = port_fops_splice_write,
> > fs/fuse/dev.c:  .splice_write   = fuse_dev_splice_write,
> > fs/gfs2/file.c: .splice_write   = gfs2_file_splice_write,
> > fs/gfs2/file.c: .splice_write   = gfs2_file_splice_write,
> > fs/overlayfs/file.c:    .splice_write   = ovl_splice_write,
> > net/socket.c:   .splice_write = generic_splice_sendpage,
> > scripts/coccinelle/api/stream_open.cocci:    .splice_write = splice_write_f,
> >
> > Of these, splice_write_null() doesn't mark out as written
> > (but it's for /dev/null so I think this is expected),
> > and I haven't been able to visually confirm whether
> > port_fops_splice_write() and generic_splice_sendpage() do.
> >
> > All the others delegate to iter_file_splice_write().
> All this is very troubling to me.
> It translates to a mental model that I cannot remember and
> cannot maintain for fixes whose value are still questionable.
> 
> IIUC, the only thing you need to change in do_splice() for
> making your use case work is to add fsnotify_modify()
> for the splice_pipe_to_pipe() case. Right?
No, all splice/tee/vmsplice cases need to generate modify events for the
output fd. Really, all I/O syscalls do, but those are for today.

> So either make the change that you need, or all the changes
> that are simple to follow without trying to make the world
> consistent
Thus I also originally had all the aforementioned generate access/modify
for in/out.

> - these pipe iterators business is really messy.
> I don't know if avoiding a double event (which is likely not visible)
> is worth the complicated code that is hard to understand.
> 
> > In 2/3 I fixed the vmsplice notification placement
> > (access from pipe, modify to pipe).
> >
> > I'm following this up with an LTP patch, where only sendfile_file_to_pipe
> > passes on 6.1.27-1 and all tests pass on v6.4 + this patchset.
> Were these tests able to detect the double event?
> Maybe it's not visible because double consequent events get merged.
That's how I discovered it, yes. They aren't merged because we'd generate
  modify out  <- from the VFS callback
  access in   <- from do_splice
  modify out  <- ibid.

I agree this got very ugly very fast for a weird edge case ‒
maybe I did get a little over-zealous on having a consistent
"one syscall↔one event for each affected file" model.

OTOH: I've found that just using
	if (ret > 0) {
		fsnotify_modify(out);
		fsnotify_access(in);
	}
does get the events merged from
  modify out  <- from the VFS callback
  modify out  <- from do_splice
  access in   <- ibid.
into
  modify out
  access in
which solves all issues
(reliable wake-up regardless of backing file, no spurious wake-ups)
at no cost. I would've done this originally, but I hadn't known
inotify events get merged :v
[PATCH v4 0/3] fanotify accounting for fs/splice.c
Posted by Ahelenia Ziemiańska 2 years, 7 months ago
Always generate modify out, access in for splice;
this gets automatically merged with no ugly special cases.

No changes to 2/3 or 3/3.

Ahelenia Ziemiańska (3):
  splice: always fsnotify_access(in), fsnotify_modify(out) on success
  splice: fsnotify_access(fd)/fsnotify_modify(fd) in vmsplice
  splice: fsnotify_access(in), fsnotify_modify(out) on success in tee

 fs/splice.c | 38 ++++++++++++++++++++------------------
 1 file changed, 20 insertions(+), 18 deletions(-)

Interdiff against v3:
diff --git a/fs/splice.c b/fs/splice.c
index 2ecfccbda956..bdbabc2ebfff 100644
--- a/fs/splice.c
+++ b/fs/splice.c
@@ -1184,10 +1184,6 @@ long do_splice(struct file *in, loff_t *off_in, struct file *out,
 			out->f_pos = offset;
 		else
 			*off_out = offset;
-
-		// splice_write-> already marked out
-		// as modified via vfs_iter_write()
-		goto noaccessout;
 	} else if (opipe) {
 		if (off_out)
 			return -ESPIPE;
@@ -1211,11 +1207,10 @@ long do_splice(struct file *in, loff_t *off_in, struct file *out,
 	} else
 		return -EINVAL;
 
-	if (ret > 0)
+	if (ret > 0) {
 		fsnotify_modify(out);
-noaccessout:
-	if (ret > 0)
 		fsnotify_access(in);
+	}
 
 	return ret;
 }
-- 
2.39.2
Re: [PATCH v4 0/3] fanotify accounting for fs/splice.c
Posted by Jan Kara 2 years, 7 months ago
Hello!

On Tue 27-06-23 22:50:46, Ahelenia Ziemiańska wrote:
> Always generate modify out, access in for splice;
> this gets automatically merged with no ugly special cases.
> 
> No changes to 2/3 or 3/3.

Thanks for the patches Ahelena! The code looks fine to me but to be honest
I still have one unresolved question so let me think about it loud here for
documentation purposes :). Do we want fsnotify (any filesystem
notification framework like inotify or fanotify) to actually generate
events on FIFOs? FIFOs are virtual objects and are not part of the
filesystem as such (well, the inode itself and the name is), hence
*filesystem* notification framework does not seem like a great fit to watch
for changes or accesses there. And if we say "yes" for FIFOs, then why not
AF_UNIX sockets? Where do we draw the line? And is it all worth the
trouble?

I understand the convenience of inotify working on FIFOs for the "tail -f"
usecase but then wouldn't this better be fixed in tail(1) itself by using
epoll(7) for FIFOs which, as I've noted in my other reply, does not have
the problem that poll(2) has when there are no writers?

Another issue with FIFOs is that they do not have a concept of file
position. For hierarchical storage usecase we are introducing events that
will report file ranges being modified / accessed and officially supporting
FIFOs is one more special case to deal with.

What is supporting your changes is that fsnotify mostly works for FIFOs
already now (normal reads & writes generate notification) so splice not
working could be viewed as an inconsistency. Sockets (although they are
visible in the filesystem) cannot be open so for them the illusion of being
a file is even weaker.

So overall I guess I'm slightly in favor of making fsnotify generate events
on FIFOs even with splice, provided Amir does not see a big trouble in
supporting this with his upcoming HSM changes.

								Honza

> Ahelenia Ziemiańska (3):
>   splice: always fsnotify_access(in), fsnotify_modify(out) on success
>   splice: fsnotify_access(fd)/fsnotify_modify(fd) in vmsplice
>   splice: fsnotify_access(in), fsnotify_modify(out) on success in tee
> 
>  fs/splice.c | 38 ++++++++++++++++++++------------------
>  1 file changed, 20 insertions(+), 18 deletions(-)
> 
> Interdiff against v3:
> diff --git a/fs/splice.c b/fs/splice.c
> index 2ecfccbda956..bdbabc2ebfff 100644
> --- a/fs/splice.c
> +++ b/fs/splice.c
> @@ -1184,10 +1184,6 @@ long do_splice(struct file *in, loff_t *off_in, struct file *out,
>  			out->f_pos = offset;
>  		else
>  			*off_out = offset;
> -
> -		// splice_write-> already marked out
> -		// as modified via vfs_iter_write()
> -		goto noaccessout;
>  	} else if (opipe) {
>  		if (off_out)
>  			return -ESPIPE;
> @@ -1211,11 +1207,10 @@ long do_splice(struct file *in, loff_t *off_in, struct file *out,
>  	} else
>  		return -EINVAL;
>  
> -	if (ret > 0)
> +	if (ret > 0) {
>  		fsnotify_modify(out);
> -noaccessout:
> -	if (ret > 0)
>  		fsnotify_access(in);
> +	}
>  
>  	return ret;
>  }
> -- 
> 2.39.2


-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR
Re: [PATCH v4 0/3] fanotify accounting for fs/splice.c
Posted by Amir Goldstein 2 years, 7 months ago
On Wed, Jun 28, 2023 at 2:38 PM Jan Kara <jack@suse.cz> wrote:
>
> Hello!
>
> On Tue 27-06-23 22:50:46, Ahelenia Ziemiańska wrote:
> > Always generate modify out, access in for splice;
> > this gets automatically merged with no ugly special cases.
> >
> > No changes to 2/3 or 3/3.
>
> Thanks for the patches Ahelena! The code looks fine to me but to be honest
> I still have one unresolved question so let me think about it loud here for
> documentation purposes :). Do we want fsnotify (any filesystem
> notification framework like inotify or fanotify) to actually generate
> events on FIFOs? FIFOs are virtual objects and are not part of the
> filesystem as such (well, the inode itself and the name is), hence
> *filesystem* notification framework does not seem like a great fit to watch
> for changes or accesses there. And if we say "yes" for FIFOs, then why not
> AF_UNIX sockets? Where do we draw the line? And is it all worth the
> trouble?
>
> I understand the convenience of inotify working on FIFOs for the "tail -f"
> usecase but then wouldn't this better be fixed in tail(1) itself by using
> epoll(7) for FIFOs which, as I've noted in my other reply, does not have
> the problem that poll(2) has when there are no writers?
>
> Another issue with FIFOs is that they do not have a concept of file
> position. For hierarchical storage usecase we are introducing events that
> will report file ranges being modified / accessed and officially supporting
> FIFOs is one more special case to deal with.
>
> What is supporting your changes is that fsnotify mostly works for FIFOs
> already now (normal reads & writes generate notification) so splice not
> working could be viewed as an inconsistency. Sockets (although they are
> visible in the filesystem) cannot be open so for them the illusion of being
> a file is even weaker.
>
> So overall I guess I'm slightly in favor of making fsnotify generate events
> on FIFOs even with splice, provided Amir does not see a big trouble in
> supporting this with his upcoming HSM changes.
>

I've also thought about this.

The thing about the HSM events is that they are permission events
and just like FAN_ACCESS_PERM, they originate from the common
access control helpers {rw,remap}_verify_area(), which also happen
to have the file range info (with ppos NULL for pipes).

Ahelenia's patches do not add any new rw_verify_area() to pipes
so no new FAN_ACCESS_PERM events were added.

If we could go back to the design of fanotify we would have probably
made it explicit that permission events are only allowed on regular
files and dirs. For the new HSM events we can (and will) do that.

In any case, the new events are supposed to be delivered with
file access range records, so delivering HSM events on pipes
wouldn't make any sense.

So I do not see any problem with these patches wrt upcomping
HSM events.

However, note that these patches create more inconsistencies
between IN_ACCESS and FAN_ACCESS_PERM on pipes.

We can leave it at that if we want, but fixing the inconsistencies
by adding more FAN_ACCESS_PERM events on pipes - this
is not something that I wouldn't be comfortable with.

If anything, we can remove FAN_ACCESS_PERM events from
special files and see if anybody complains.

I don't know of any users of FAN_ACCESS_PERM and even for
FAN_OPEN_PERM, I don't think that AV-vendors have anything
useful to do with open permission events on special files.

Thanks,
Amir.
Re: [PATCH v4 0/3] fanotify accounting for fs/splice.c
Posted by Ahelenia Ziemiańska 2 years, 7 months ago
Hi!

On Wed, Jun 28, 2023 at 01:38:53PM +0200, Jan Kara wrote:
> On Tue 27-06-23 22:50:46, Ahelenia Ziemiańska wrote:
> > Always generate modify out, access in for splice;
> > this gets automatically merged with no ugly special cases.
> > 
> > No changes to 2/3 or 3/3.
> Thanks for the patches Ahelena! The code looks fine to me but to be honest
> I still have one unresolved question so let me think about it loud here for
> documentation purposes :). Do we want fsnotify (any filesystem
> notification framework like inotify or fanotify) to actually generate
> events on FIFOs? FIFOs are virtual objects and are not part of the
> filesystem as such (well, the inode itself and the name is), hence
> *filesystem* notification framework does not seem like a great fit to watch
> for changes or accesses there. And if we say "yes" for FIFOs, then why not
> AF_UNIX sockets? Where do we draw the line? And is it all worth the
> trouble?
As a relative outsider (I haven't used inotify before this, and have not
 been subjected to it or its peripheries before),
I interpreted inotify as being the Correct solution for:
  1. stuff you can find in a normal
     (non-/dev, you don't want to touch devices)
     filesystem traversal
  2. stuff you can open
where, going down the list in inode(7):
  S_IFSOCK   can't open
  S_IFLNK    can't open
  S_IFREG    yes!
  S_IFBLK    it's a device
  S_IFDIR    yes!
  S_IFCHR    it's a device
  S_IFIFO    yes!

It appears that I'm not the only one who's interpreted it that way,
especially since neither regular files nor pipes are pollable.
(Though, under that same categorisation, I wouldn't be surprised
 if anonymous pipes had been refused, for example, since those are
 conventionally unnameable.)

To this end, I'd say we're leaving the line precisely where it was drawn
before, even if by accident.

> I understand the convenience of inotify working on FIFOs for the "tail -f"
> usecase but then wouldn't this better be fixed in tail(1) itself by using
> epoll(7) for FIFOs which, as I've noted in my other reply, does not have
> the problem that poll(2) has when there are no writers?
Yes, epoll in ET mode returns POLLHUP only once, but you /also/ need the
inotify anyway for regular files, which epoll refuses
(and, with -F, you may want both epoll for a pipe and inotify for the
 directory it's contained in).
Is it possible to do? yes. Is it more annoying than just having pipes
report when they were written to? very much so.

inotify actually working(*) is presumably why coreutils tail doesn't use
epoll ‒ inotify already provides all required events(*), you can use the
same code for regular files and fifos, and with one fewer level of
indirection: there's just no need(*).

(*: except with a magic syscall only I use apparently)
Re: [PATCH v4 0/3] fanotify accounting for fs/splice.c
Posted by Jan Kara 2 years, 7 months ago
Hi!

On Wed 28-06-23 20:54:28, Ahelenia Ziemiańska wrote:
> On Wed, Jun 28, 2023 at 01:38:53PM +0200, Jan Kara wrote:
> > On Tue 27-06-23 22:50:46, Ahelenia Ziemiańska wrote:
> > > Always generate modify out, access in for splice;
> > > this gets automatically merged with no ugly special cases.
> > > 
> > > No changes to 2/3 or 3/3.
> > Thanks for the patches Ahelena! The code looks fine to me but to be honest
> > I still have one unresolved question so let me think about it loud here for
> > documentation purposes :). Do we want fsnotify (any filesystem
> > notification framework like inotify or fanotify) to actually generate
> > events on FIFOs? FIFOs are virtual objects and are not part of the
> > filesystem as such (well, the inode itself and the name is), hence
> > *filesystem* notification framework does not seem like a great fit to watch
> > for changes or accesses there. And if we say "yes" for FIFOs, then why not
> > AF_UNIX sockets? Where do we draw the line? And is it all worth the
> > trouble?
> As a relative outsider (I haven't used inotify before this, and have not
>  been subjected to it or its peripheries before),
> I interpreted inotify as being the Correct solution for:
>   1. stuff you can find in a normal
>      (non-/dev, you don't want to touch devices)
>      filesystem traversal
>   2. stuff you can open
> where, going down the list in inode(7):
>   S_IFSOCK   can't open
>   S_IFLNK    can't open
>   S_IFREG    yes!
>   S_IFBLK    it's a device
>   S_IFDIR    yes!
>   S_IFCHR    it's a device
>   S_IFIFO    yes!
> 
> It appears that I'm not the only one who's interpreted it that way,
> especially since neither regular files nor pipes are pollable.
> (Though, under that same categorisation, I wouldn't be surprised
>  if anonymous pipes had been refused, for example, since those are
>  conventionally unnameable.)
> 
> To this end, I'd say we're leaving the line precisely where it was drawn
> before, even if by accident.

I agree, although I'd note that there are S_IFREG inodes under /sys or
/proc where it would be too difficult to provide fsnotify events (exactly
because the file contents is not "data stored somewhere" but rather
something "generated on the fly") so the illusion is not perfect already.

> > I understand the convenience of inotify working on FIFOs for the "tail -f"
> > usecase but then wouldn't this better be fixed in tail(1) itself by using
> > epoll(7) for FIFOs which, as I've noted in my other reply, does not have
> > the problem that poll(2) has when there are no writers?
> Yes, epoll in ET mode returns POLLHUP only once, but you /also/ need the
> inotify anyway for regular files, which epoll refuses
> (and, with -F, you may want both epoll for a pipe and inotify for the
>  directory it's contained in).
> Is it possible to do? yes. Is it more annoying than just having pipes
> report when they were written to? very much so.
> 
> inotify actually working(*) is presumably why coreutils tail doesn't use
> epoll ‒ inotify already provides all required events(*), you can use the
> same code for regular files and fifos, and with one fewer level of
> indirection: there's just no need(*).
> 
> (*: except with a magic syscall only I use apparently)

Yeah, I've slept to this and I still think adding fsnotify events to splice
is a nicer option so feel free to add:

Acked-by: Jan Kara <jack@suse.cz>

to all kernel patches in your series. Since the changes are in splice code,
Christian or Al Viro (who you already have on CC list) should be merging
this so please make sure to also include them in the v5 submission.

								Honza
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR
Re: [PATCH v4 0/3] fanotify accounting for fs/splice.c
Posted by Amir Goldstein 2 years, 7 months ago
On Tue, Jun 27, 2023 at 11:50 PM Ahelenia Ziemiańska
<nabijaczleweli@nabijaczleweli.xyz> wrote:
>
> Always generate modify out, access in for splice;
> this gets automatically merged with no ugly special cases.

Ahelenia,

Obviously, you are new to sending patches to the kernel
and you appear to be a very enthusiastic and fast learner,
so I assume you won't mind getting some tips that you won't
find in any document.

a) CC LTP only on tests, not on the kernel patches

b) Please don't post these "diff" patches.
Developers (and bots) should be able to understand which
upstream commit a patch is based on (git format-patch provides that info).

I mean you can send those diff patches as part of a conversation to
explain yourself, that's fine, just don't post them as if they are patches
for review.

c) When there are prospect reviewers that have not reviewed v1
(especially inotify maintainer), it is better to wait at least one day posting
v2 and v3 and v4 ;), because:
1. It is better to accumulate review comments from several reviewers
2. Different reviewers may disagree, so if you are just following my
    advice you may need to go back and forth until everyone is happy
3. It's racy - reviewers may be in the middle of review of v1 without
    realizing that v2,v3,v4 is already in their inbox, so that's creating
    extra work for them - not a good outcome

Going to review v4....

Thanks,
Amir.
[PATCH v4 1/3] splice: always fsnotify_access(in), fsnotify_modify(out) on success
Posted by Ahelenia Ziemiańska 2 years, 7 months ago
The current behaviour caused an asymmetry where some write APIs
(write, sendfile) would notify the written-to/read-from objects,
but splice wouldn't.

This affected userspace which uses inotify, most notably coreutils
tail -f, to monitor pipes.
If the pipe buffer had been filled by a splice-family function:
  * tail wouldn't know and thus wouldn't service the pipe, and
  * all writes to the pipe would block because it's full,
thus service was denied.
(For the particular case of tail -f this could be worked around
 with ---disable-inotify.)

Generate modify out before access in to let inotify merge the
modify out events in thr ipipe case.

Fixes: 983652c69199 ("splice: report related fsnotify events")
Link: https://lore.kernel.org/linux-fsdevel/jbyihkyk5dtaohdwjyivambb2gffyjs3dodpofafnkkunxq7bu@jngkdxx65pux/t/#u
Link: https://bugs.debian.org/1039488
Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
Reviewed-by: Amir Goldstein <amir73il@gmail.com>
---
 fs/splice.c | 26 +++++++++-----------------
 1 file changed, 9 insertions(+), 17 deletions(-)

diff --git a/fs/splice.c b/fs/splice.c
index 3e06611d19ae..b5c7a5ae0e94 100644
--- a/fs/splice.c
+++ b/fs/splice.c
@@ -1154,10 +1154,8 @@ long do_splice(struct file *in, loff_t *off_in, struct file *out,
 		if ((in->f_flags | out->f_flags) & O_NONBLOCK)
 			flags |= SPLICE_F_NONBLOCK;
 
-		return splice_pipe_to_pipe(ipipe, opipe, len, flags);
-	}
-
-	if (ipipe) {
+		ret = splice_pipe_to_pipe(ipipe, opipe, len, flags);
+	} else if (ipipe) {
 		if (off_in)
 			return -ESPIPE;
 		if (off_out) {
@@ -1182,18 +1180,11 @@ long do_splice(struct file *in, loff_t *off_in, struct file *out,
 		ret = do_splice_from(ipipe, out, &offset, len, flags);
 		file_end_write(out);
 
-		if (ret > 0)
-			fsnotify_modify(out);
-
 		if (!off_out)
 			out->f_pos = offset;
 		else
 			*off_out = offset;
-
-		return ret;
-	}
-
-	if (opipe) {
+	} else if (opipe) {
 		if (off_out)
 			return -ESPIPE;
 		if (off_in) {
@@ -1209,18 +1200,19 @@ long do_splice(struct file *in, loff_t *off_in, struct file *out,
 
 		ret = splice_file_to_pipe(in, opipe, &offset, len, flags);
 
-		if (ret > 0)
-			fsnotify_access(in);
-
 		if (!off_in)
 			in->f_pos = offset;
 		else
 			*off_in = offset;
+	} else
+		return -EINVAL;
 
-		return ret;
+	if (ret > 0) {
+		fsnotify_modify(out);
+		fsnotify_access(in);
 	}
 
-	return -EINVAL;
+	return ret;
 }
 
 static long __do_splice(struct file *in, loff_t __user *off_in,
-- 
2.39.2

Re: [PATCH v4 1/3] splice: always fsnotify_access(in), fsnotify_modify(out) on success
Posted by Amir Goldstein 2 years, 7 months ago
On Tue, Jun 27, 2023 at 11:50 PM Ahelenia Ziemiańska
<nabijaczleweli@nabijaczleweli.xyz> wrote:
>
> The current behaviour caused an asymmetry where some write APIs
> (write, sendfile) would notify the written-to/read-from objects,
> but splice wouldn't.
>
> This affected userspace which uses inotify, most notably coreutils
> tail -f, to monitor pipes.
> If the pipe buffer had been filled by a splice-family function:
>   * tail wouldn't know and thus wouldn't service the pipe, and
>   * all writes to the pipe would block because it's full,
> thus service was denied.
> (For the particular case of tail -f this could be worked around
>  with ---disable-inotify.)
>

Is my understanding of the tail code wrong?
My understanding was that tail_forever_inotify() is not called for
pipes, or is it being called when tailing a mixed collection of pipes
and regular files? If there are subtleties like those you need to
mention them , otherwise people will not be able to reproduce the
problem that you are describing.

I need to warn you about something regarding this patch -
often there are colliding interests among different kernel users -
fsnotify use cases quite often collide with the interest of users tracking
performance regressions and IN_ACCESS/IN_MODIFY on anonymous pipes
specifically have been the source of several performance regression reports
in the past and have driven optimizations like:

71d734103edf ("fsnotify: Rearrange fast path to minimise overhead
when there is no watcher")
e43de7f0862b ("fsnotify: optimize the case of no marks of any type")

The moral of this story is: even if your patches are accepted by fsnotify
reviewers, once they are staged for merging they will be subject to
performance regression tests and I can tell you with certainty that
performance regression will not be tolerated for the tail -f use case.
I will push your v4 patches to a branch in my github, to let the kernel
test bots run the performance regressions on it whenever they get to it.

Moreover, if coreutils will change tail -f to start setting inotify watches
on anonymous pipes (my understanding is that currently does not?),
then any tail -f on anonymous pipe can cripple the "no marks on sb"
performance optimization for all anonymous pipes and that would be
a *very* unfortunate outcome.

I think we need to add a rule to fanotify_events_supported() to ban
sb/mount marks on SB_KERNMOUNT and backport this
fix to LTS kernels (I will look into it) and then we can fine tune
the s_fsnotify_connectors optimization in fsnotify_parent() for
the SB_KERNMOUNT special case.
This may be able to save your patch for the faith of NACKed
for performance regression.

> Generate modify out before access in to let inotify merge the
> modify out events in thr ipipe case.

This comment is not clear and does not belong in this context,
but it very much belongs near the code in question.

Please wait to collect more feedback and specifically
to hear what Jan has to say about this hack before posting v5!

FYI, we are now in the beginning of the 6.5 "merge window",
which means that maintainers may be less responsive for the
next two weeks to non-critical patches as this one, which are
not targeted for the 6.5 kernel release.

Thanks,
Amir.
Re: [PATCH v4 1/3] splice: always fsnotify_access(in), fsnotify_modify(out) on success
Posted by Ahelenia Ziemiańska 2 years, 7 months ago
On Wed, Jun 28, 2023 at 09:33:43AM +0300, Amir Goldstein wrote:
> On Tue, Jun 27, 2023 at 11:50 PM Ahelenia Ziemiańska
> <nabijaczleweli@nabijaczleweli.xyz> wrote:
> > The current behaviour caused an asymmetry where some write APIs
> > (write, sendfile) would notify the written-to/read-from objects,
> > but splice wouldn't.
> >
> > This affected userspace which uses inotify, most notably coreutils
> > tail -f, to monitor pipes.
> > If the pipe buffer had been filled by a splice-family function:
> >   * tail wouldn't know and thus wouldn't service the pipe, and
> >   * all writes to the pipe would block because it's full,
> > thus service was denied.
> > (For the particular case of tail -f this could be worked around
> >  with ---disable-inotify.)
> Is my understanding of the tail code wrong?
> My understanding was that tail_forever_inotify() is not called for
> pipes, or is it being called when tailing a mixed collection of pipes
> and regular files? If there are subtleties like those you need to
> mention them , otherwise people will not be able to reproduce the
> problem that you are describing.
I can't squeak to the code itself, but it's trivial to check:
  $ tail -f  fifo &
  [1] 3213996
  $ echo zupa > fifo
  zupa
  $ echo zupa > fifo
  zupa
  $ echo zupa > fifo
  zupa
  $ cat /bin/tail > fifo
  # ...
  $ cat /bin/tail > fifo
hangs: the fifo is being watched with inotify.

This happens regardless of other files being specified.

tail -f doesn't follow FIFOs or pipes if they're fd 0
(guaranteed by POSIX, coreutils conforms).

OTOH, you could theoretically do
  $ cat | tail -f /dev/fd/3 3<&0
which first reads from the pipe until completion (⇔ hangup, cat died),
then hangs, because it's waiting for more data on the pipe.

This can never happen under a normal scenario, but doing
  $ echo zupa > /proc/3238590/fd/3
a few times reveals it's using classic 1/s polling
(and splicing to /proc/3238590/fd/3 actually yields that data being
 output from tail).

> I need to warn you about something regarding this patch -
> often there are colliding interests among different kernel users -
> fsnotify use cases quite often collide with the interest of users tracking
> performance regressions and IN_ACCESS/IN_MODIFY on anonymous pipes
> specifically have been the source of several performance regression reports
> in the past and have driven optimizations like:
> 
> 71d734103edf ("fsnotify: Rearrange fast path to minimise overhead
> when there is no watcher")
> e43de7f0862b ("fsnotify: optimize the case of no marks of any type")
> 
> The moral of this story is: even if your patches are accepted by fsnotify
> reviewers, once they are staged for merging they will be subject to
> performance regression tests and I can tell you with certainty that
> performance regression will not be tolerated for the tail -f use case.
> I will push your v4 patches to a branch in my github, to let the kernel
> test bots run the performance regressions on it whenever they get to it.
> 
> Moreover, if coreutils will change tail -f to start setting inotify watches
> on anonymous pipes (my understanding is that currently does not?),
> then any tail -f on anonymous pipe can cripple the "no marks on sb"
> performance optimization for all anonymous pipes and that would be
> a *very* unfortunate outcome.
As seen above, it doesn't set inotify watches on anon pipes, and
(since it manages to distinguish "| /dev/fd/3 3<&0" from "fifo",
 so it must be going further than S_ISFIFO(fstat()))
this is an explicit design decision.

If you refuse setting inotifies on anon pipes then that likely won't
impact any userspace program (it's pathological, and for tail-like cases
 it'd only be meaningful for magic /proc/$pid/fd/* symlinks),
and if it's in the name of performance then no-one'll likely complain,
or even notice.

> I think we need to add a rule to fanotify_events_supported() to ban
> sb/mount marks on SB_KERNMOUNT and backport this
> fix to LTS kernels (I will look into it) and then we can fine tune
> the s_fsnotify_connectors optimization in fsnotify_parent() for
> the SB_KERNMOUNT special case.
> This may be able to save your patch for the faith of NACKed
> for performance regression.
This goes over my head, but if Jan says it makes sense
then it must do.

> > Generate modify out before access in to let inotify merge the
> > modify out events in thr ipipe case.
> This comment is not clear and does not belong in this context,
> but it very much belongs near the code in question.
Turned it into
		/*
		 * Generate modify out before access in:
		 * do_splice_from() may've already sent modify out,
		 * and this ensures the events get merged.
		 */
for v5.
Re: [PATCH v4 1/3] splice: always fsnotify_access(in), fsnotify_modify(out) on success
Posted by Amir Goldstein 2 years, 7 months ago
On Wed, Jun 28, 2023 at 8:09 PM Ahelenia Ziemiańska
<nabijaczleweli@nabijaczleweli.xyz> wrote:
>
> On Wed, Jun 28, 2023 at 09:33:43AM +0300, Amir Goldstein wrote:
> > On Tue, Jun 27, 2023 at 11:50 PM Ahelenia Ziemiańska
> > <nabijaczleweli@nabijaczleweli.xyz> wrote:
> > > The current behaviour caused an asymmetry where some write APIs
> > > (write, sendfile) would notify the written-to/read-from objects,
> > > but splice wouldn't.
> > >
> > > This affected userspace which uses inotify, most notably coreutils
> > > tail -f, to monitor pipes.
> > > If the pipe buffer had been filled by a splice-family function:
> > >   * tail wouldn't know and thus wouldn't service the pipe, and
> > >   * all writes to the pipe would block because it's full,
> > > thus service was denied.
> > > (For the particular case of tail -f this could be worked around
> > >  with ---disable-inotify.)
> > Is my understanding of the tail code wrong?
> > My understanding was that tail_forever_inotify() is not called for
> > pipes, or is it being called when tailing a mixed collection of pipes
> > and regular files? If there are subtleties like those you need to
> > mention them , otherwise people will not be able to reproduce the
> > problem that you are describing.
> I can't squeak to the code itself, but it's trivial to check:
>   $ tail -f  fifo &
>   [1] 3213996
>   $ echo zupa > fifo
>   zupa
>   $ echo zupa > fifo
>   zupa
>   $ echo zupa > fifo
>   zupa
>   $ cat /bin/tail > fifo
>   # ...
>   $ cat /bin/tail > fifo
> hangs: the fifo is being watched with inotify.
>
> This happens regardless of other files being specified.
>
> tail -f doesn't follow FIFOs or pipes if they're fd 0
> (guaranteed by POSIX, coreutils conforms).
>
> OTOH, you could theoretically do
>   $ cat | tail -f /dev/fd/3 3<&0
> which first reads from the pipe until completion (⇔ hangup, cat died),
> then hangs, because it's waiting for more data on the pipe.
>
> This can never happen under a normal scenario, but doing
>   $ echo zupa > /proc/3238590/fd/3
> a few times reveals it's using classic 1/s polling
> (and splicing to /proc/3238590/fd/3 actually yields that data being
>  output from tail).
>
> > I need to warn you about something regarding this patch -
> > often there are colliding interests among different kernel users -
> > fsnotify use cases quite often collide with the interest of users tracking
> > performance regressions and IN_ACCESS/IN_MODIFY on anonymous pipes
> > specifically have been the source of several performance regression reports
> > in the past and have driven optimizations like:
> >
> > 71d734103edf ("fsnotify: Rearrange fast path to minimise overhead
> > when there is no watcher")
> > e43de7f0862b ("fsnotify: optimize the case of no marks of any type")
> >
> > The moral of this story is: even if your patches are accepted by fsnotify
> > reviewers, once they are staged for merging they will be subject to
> > performance regression tests and I can tell you with certainty that
> > performance regression will not be tolerated for the tail -f use case.
> > I will push your v4 patches to a branch in my github, to let the kernel
> > test bots run the performance regressions on it whenever they get to it.
> >
> > Moreover, if coreutils will change tail -f to start setting inotify watches
> > on anonymous pipes (my understanding is that currently does not?),
> > then any tail -f on anonymous pipe can cripple the "no marks on sb"
> > performance optimization for all anonymous pipes and that would be
> > a *very* unfortunate outcome.
> As seen above, it doesn't set inotify watches on anon pipes, and
> (since it manages to distinguish "| /dev/fd/3 3<&0" from "fifo",
>  so it must be going further than S_ISFIFO(fstat()))
> this is an explicit design decision.
>
> If you refuse setting inotifies on anon pipes then that likely won't
> impact any userspace program (it's pathological, and for tail-like cases
>  it'd only be meaningful for magic /proc/$pid/fd/* symlinks),
> and if it's in the name of performance then no-one'll likely complain,
> or even notice.
>

Unfortunately, it doesn't work this way - most of the time we are not
supposed to break existing applications and I have no way of knowing if
those applications exist...

> > I think we need to add a rule to fanotify_events_supported() to ban
> > sb/mount marks on SB_KERNMOUNT and backport this
> > fix to LTS kernels (I will look into it) and then we can fine tune
> > the s_fsnotify_connectors optimization in fsnotify_parent() for
> > the SB_KERNMOUNT special case.
> > This may be able to save your patch for the faith of NACKed
> > for performance regression.
> This goes over my head, but if Jan says it makes sense
> then it must do.
>

Here you go:
https://github.com/amir73il/linux/commits/fsnotify_pipe

I ended up using SB_NOUSER which is narrower than
SB_KERNMOUNT.

Care to test?
1) Functionally - that I did not break your tests.
2) Optimization - that when one anon pipe has an inotify watch
write to another anon pipe stops at fsnotify_inode_has_watchers()
and does not get to fsnotify().

> > > Generate modify out before access in to let inotify merge the
> > > modify out events in thr ipipe case.
> > This comment is not clear and does not belong in this context,
> > but it very much belongs near the code in question.
> Turned it into
>                 /*
>                  * Generate modify out before access in:
>                  * do_splice_from() may've already sent modify out,
>                  * and this ensures the events get merged.
>                  */
> for v5.

OK.

Thanks,
Amir.
Re: [PATCH v4 1/3] splice: always fsnotify_access(in), fsnotify_modify(out) on success
Posted by Ahelenia Ziemiańska 2 years, 7 months ago
On Wed, Jun 28, 2023 at 09:38:03PM +0300, Amir Goldstein wrote:
> On Wed, Jun 28, 2023 at 8:09 PM Ahelenia Ziemiańska
> <nabijaczleweli@nabijaczleweli.xyz> wrote:
> > On Wed, Jun 28, 2023 at 09:33:43AM +0300, Amir Goldstein wrote:
> > > I think we need to add a rule to fanotify_events_supported() to ban
> > > sb/mount marks on SB_KERNMOUNT and backport this
> > > fix to LTS kernels (I will look into it) and then we can fine tune
> > > the s_fsnotify_connectors optimization in fsnotify_parent() for
> > > the SB_KERNMOUNT special case.
> > > This may be able to save your patch for the faith of NACKed
> > > for performance regression.
> > This goes over my head, but if Jan says it makes sense
> > then it must do.
> Here you go:
> https://github.com/amir73il/linux/commits/fsnotify_pipe
> 
> I ended up using SB_NOUSER which is narrower than
> SB_KERNMOUNT.
> 
> Care to test?
> 1) Functionally - that I did not break your tests.
) | gzip -d > inotify13; chmod +x inotify13; exec ./inotify13
tst_test.c:1560: TINFO: Timeout per run is 0h 00m 30s
inotify13.c:260: TINFO: file_to_pipe
inotify13.c:269: TPASS: ок
inotify13.c:260: TINFO: file_to_pipe
inotify13.c:269: TPASS: ок
inotify13.c:260: TINFO: splice_pipe_to_file
inotify13.c:269: TPASS: ок
inotify13.c:260: TINFO: pipe_to_pipe
inotify13.c:269: TPASS: ок
inotify13.c:260: TINFO: pipe_to_pipe
inotify13.c:269: TPASS: ок
inotify13.c:260: TINFO: vmsplice_pipe_to_mem
inotify13.c:269: TPASS: ок
inotify13.c:260: TINFO: vmsplice_mem_to_pipe
inotify13.c:269: TPASS: ок

Summary:
passed   7
failed   0
broken   0
skipped  0
warnings 0

The discrete tests from before also work as expected,
both to a fifo and an anon pipe.

> 2) Optimization - that when one anon pipe has an inotify watch
> write to another anon pipe stops at fsnotify_inode_has_watchers()
> and does not get to fsnotify().
Yes, I can confirm this as well: fsnotify_parent() only continues to
fsnotify() for the watched pipe; writes to other pipes early-exit.

To validate the counterfactual, I reverted "fsnotify: optimize the case
of anonymous pipe with no watches" and fsnotify() was being called
for each anon pipe write, so long as any anon pipe watches were registered.
Re: [PATCH v4 1/3] splice: always fsnotify_access(in), fsnotify_modify(out) on success
Posted by Amir Goldstein 2 years, 7 months ago
On Wed, Jun 28, 2023 at 11:18 PM Ahelenia Ziemiańska
<nabijaczleweli@nabijaczleweli.xyz> wrote:
>
> On Wed, Jun 28, 2023 at 09:38:03PM +0300, Amir Goldstein wrote:
> > On Wed, Jun 28, 2023 at 8:09 PM Ahelenia Ziemiańska
> > <nabijaczleweli@nabijaczleweli.xyz> wrote:
> > > On Wed, Jun 28, 2023 at 09:33:43AM +0300, Amir Goldstein wrote:
> > > > I think we need to add a rule to fanotify_events_supported() to ban
> > > > sb/mount marks on SB_KERNMOUNT and backport this
> > > > fix to LTS kernels (I will look into it) and then we can fine tune
> > > > the s_fsnotify_connectors optimization in fsnotify_parent() for
> > > > the SB_KERNMOUNT special case.
> > > > This may be able to save your patch for the faith of NACKed
> > > > for performance regression.
> > > This goes over my head, but if Jan says it makes sense
> > > then it must do.
> > Here you go:
> > https://github.com/amir73il/linux/commits/fsnotify_pipe
> >
> > I ended up using SB_NOUSER which is narrower than
> > SB_KERNMOUNT.
> >
> > Care to test?
> > 1) Functionally - that I did not break your tests.
> ) | gzip -d > inotify13; chmod +x inotify13; exec ./inotify13
> tst_test.c:1560: TINFO: Timeout per run is 0h 00m 30s
> inotify13.c:260: TINFO: file_to_pipe
> inotify13.c:269: TPASS: ок
> inotify13.c:260: TINFO: file_to_pipe
> inotify13.c:269: TPASS: ок
> inotify13.c:260: TINFO: splice_pipe_to_file
> inotify13.c:269: TPASS: ок
> inotify13.c:260: TINFO: pipe_to_pipe
> inotify13.c:269: TPASS: ок
> inotify13.c:260: TINFO: pipe_to_pipe
> inotify13.c:269: TPASS: ок
> inotify13.c:260: TINFO: vmsplice_pipe_to_mem
> inotify13.c:269: TPASS: ок
> inotify13.c:260: TINFO: vmsplice_mem_to_pipe
> inotify13.c:269: TPASS: ок
>
> Summary:
> passed   7
> failed   0
> broken   0
> skipped  0
> warnings 0
>
> The discrete tests from before also work as expected,
> both to a fifo and an anon pipe.
>
> > 2) Optimization - that when one anon pipe has an inotify watch
> > write to another anon pipe stops at fsnotify_inode_has_watchers()
> > and does not get to fsnotify().
> Yes, I can confirm this as well: fsnotify_parent() only continues to
> fsnotify() for the watched pipe; writes to other pipes early-exit.
>
> To validate the counterfactual, I reverted "fsnotify: optimize the case
> of anonymous pipe with no watches" and fsnotify() was being called
> for each anon pipe write, so long as any anon pipe watches were registered.

Thank you for testing!

As Jan suggested, when you post v5, with my Reviewed-by and Jan's
Acked-by, please ask Christian to review and consider taking these
patches through the vfs tree for the 6.6 release.

Please include a link to your LTP test in the cover letter and a link to
my performance optimization patches.

Unless the kernel test bots detect a performance regression due to
your patches, I am not sure whether or not or when we will apply the
optimization patches.

Thanks,
Amir.
Re: [PATCH v4 1/3] splice: always fsnotify_access(in), fsnotify_modify(out) on success
Posted by Jan Kara 2 years, 7 months ago
On Wed 28-06-23 09:33:43, Amir Goldstein wrote:
> On Tue, Jun 27, 2023 at 11:50 PM Ahelenia Ziemiańska
> <nabijaczleweli@nabijaczleweli.xyz> wrote:
> >
> > The current behaviour caused an asymmetry where some write APIs
> > (write, sendfile) would notify the written-to/read-from objects,
> > but splice wouldn't.
> >
> > This affected userspace which uses inotify, most notably coreutils
> > tail -f, to monitor pipes.
> > If the pipe buffer had been filled by a splice-family function:
> >   * tail wouldn't know and thus wouldn't service the pipe, and
> >   * all writes to the pipe would block because it's full,
> > thus service was denied.
> > (For the particular case of tail -f this could be worked around
> >  with ---disable-inotify.)
> >
> 
> Is my understanding of the tail code wrong?
> My understanding was that tail_forever_inotify() is not called for
> pipes, or is it being called when tailing a mixed collection of pipes
> and regular files? If there are subtleties like those you need to
> mention them , otherwise people will not be able to reproduce the
> problem that you are describing.

Well, on my openSUSE 15.4 at least, tail -f does use inotify on FIFOs and
indeed when data is spliced to the FIFO, tail doesn't notice.

> I need to warn you about something regarding this patch -
> often there are colliding interests among different kernel users -
> fsnotify use cases quite often collide with the interest of users tracking
> performance regressions and IN_ACCESS/IN_MODIFY on anonymous pipes
> specifically have been the source of several performance regression reports
> in the past and have driven optimizations like:
> 
> 71d734103edf ("fsnotify: Rearrange fast path to minimise overhead
> when there is no watcher")
> e43de7f0862b ("fsnotify: optimize the case of no marks of any type")
> 
> The moral of this story is: even if your patches are accepted by fsnotify
> reviewers, once they are staged for merging they will be subject to
> performance regression tests and I can tell you with certainty that
> performance regression will not be tolerated for the tail -f use case.
> I will push your v4 patches to a branch in my github, to let the kernel
> test bots run the performance regressions on it whenever they get to it.
> 
> Moreover, if coreutils will change tail -f to start setting inotify watches
> on anonymous pipes (my understanding is that currently does not?),
> then any tail -f on anonymous pipe can cripple the "no marks on sb"
> performance optimization for all anonymous pipes and that would be
> a *very* unfortunate outcome.

Do you mean the "s_fsnotify_connectors" check? Yeah, a fsnotify watch on
any pipe inode is going to somewhat slow down the fsnotify calls for any
pipe. OTOH I don't expect inotify watches on pipe inodes to be common and
it is not like the overhead is huge. Also nobody really prevents you from
placing watch on pipe inode now with similar consequences, this patch only
makes it actually working with splice. So I'm not worried about the
performance impact. At least until somebody comes with a realistic
complaint ;-).

> I think we need to add a rule to fanotify_events_supported() to ban
> sb/mount marks on SB_KERNMOUNT and backport this
> fix to LTS kernels (I will look into it) and then we can fine tune
> the s_fsnotify_connectors optimization in fsnotify_parent() for
> the SB_KERNMOUNT special case.

Yeah, probably makes sense.

								Honza
-- 
Jan Kara <jack@suse.com>
SUSE Labs, CR
[PATCH v4 2/3] splice: fsnotify_access(fd)/fsnotify_modify(fd) in vmsplice
Posted by Ahelenia Ziemiańska 2 years, 7 months ago
Same logic applies here: this can fill up the pipe and pollers that rely
on getting IN_MODIFY notifications never wake up.

Fixes: 983652c69199 ("splice: report related fsnotify events")
Link: https://lore.kernel.org/linux-fsdevel/jbyihkyk5dtaohdwjyivambb2gffyjs3dodpofafnkkunxq7bu@jngkdxx65pux/t/#u
Link: https://bugs.debian.org/1039488
Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
Reviewed-by: Amir Goldstein <amir73il@gmail.com>
---
 fs/splice.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/fs/splice.c b/fs/splice.c
index b5c7a5ae0e94..d3a7f4d5c078 100644
--- a/fs/splice.c
+++ b/fs/splice.c
@@ -1341,6 +1341,9 @@ static long vmsplice_to_user(struct file *file, struct iov_iter *iter,
 		pipe_unlock(pipe);
 	}
 
+	if (ret > 0)
+		fsnotify_access(file);
+
 	return ret;
 }
 
@@ -1370,8 +1373,10 @@ static long vmsplice_to_pipe(struct file *file, struct iov_iter *iter,
 	if (!ret)
 		ret = iter_to_pipe(iter, pipe, buf_flag);
 	pipe_unlock(pipe);
-	if (ret > 0)
+	if (ret > 0) {
 		wakeup_pipe_readers(pipe);
+		fsnotify_modify(file);
+	}
 	return ret;
 }
 
-- 
2.39.2

[PATCH v4 3/3] splice: fsnotify_access(in), fsnotify_modify(out) on success in tee
Posted by Ahelenia Ziemiańska 2 years, 7 months ago
Same logic applies here: this can fill up the pipe, and pollers that rely
on getting IN_MODIFY notifications never wake up.

Fixes: 983652c69199 ("splice: report related fsnotify events")
Link: https://lore.kernel.org/linux-fsdevel/jbyihkyk5dtaohdwjyivambb2gffyjs3dodpofafnkkunxq7bu@jngkdxx65pux/t/#u
Link: https://bugs.debian.org/1039488
Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
Reviewed-by: Amir Goldstein <amir73il@gmail.com>
---
 fs/splice.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/fs/splice.c b/fs/splice.c
index d3a7f4d5c078..bdbabc2ebfff 100644
--- a/fs/splice.c
+++ b/fs/splice.c
@@ -1810,6 +1810,11 @@ long do_tee(struct file *in, struct file *out, size_t len, unsigned int flags)
 		}
 	}
 
+	if (ret > 0) {
+		fsnotify_access(in);
+		fsnotify_modify(out);
+	}
+
 	return ret;
 }
 
-- 
2.39.2
[PATCH v3 1/3] splice: always fsnotify_access(in), fsnotify_modify(out) on success
Posted by Ahelenia Ziemiańska 2 years, 7 months ago
The current behaviour caused an asymmetry where some write APIs
(write, sendfile) would notify the written-to/read-from objects,
but splice wouldn't.

This affected userspace which uses inotify, most notably coreutils
tail -f, to monitor pipes.
If the pipe buffer had been filled by a splice-family function:
  * tail wouldn't know and thus wouldn't service the pipe, and
  * all writes to the pipe would block because it's full,
thus service was denied.
(For the particular case of tail -f this could be worked around
 with ---disable-inotify.)

Fixes: 983652c69199 ("splice: report related fsnotify events")
Link: https://lore.kernel.org/linux-fsdevel/jbyihkyk5dtaohdwjyivambb2gffyjs3dodpofafnkkunxq7bu@jngkdxx65pux/t/#u
Link: https://bugs.debian.org/1039488
Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
Reviewed-by: Amir Goldstein <amir73il@gmail.com>
---
 fs/splice.c | 31 ++++++++++++++-----------------
 1 file changed, 14 insertions(+), 17 deletions(-)

diff --git a/fs/splice.c b/fs/splice.c
index 3e06611d19ae..e16f4f032d2f 100644
--- a/fs/splice.c
+++ b/fs/splice.c
@@ -1154,10 +1154,8 @@ long do_splice(struct file *in, loff_t *off_in, struct file *out,
 		if ((in->f_flags | out->f_flags) & O_NONBLOCK)
 			flags |= SPLICE_F_NONBLOCK;
 
-		return splice_pipe_to_pipe(ipipe, opipe, len, flags);
-	}
-
-	if (ipipe) {
+		ret = splice_pipe_to_pipe(ipipe, opipe, len, flags);
+	} else if (ipipe) {
 		if (off_in)
 			return -ESPIPE;
 		if (off_out) {
@@ -1182,18 +1180,15 @@ long do_splice(struct file *in, loff_t *off_in, struct file *out,
 		ret = do_splice_from(ipipe, out, &offset, len, flags);
 		file_end_write(out);
 
-		if (ret > 0)
-			fsnotify_modify(out);
-
 		if (!off_out)
 			out->f_pos = offset;
 		else
 			*off_out = offset;
 
-		return ret;
-	}
-
-	if (opipe) {
+		// splice_write-> already marked out
+		// as modified via vfs_iter_write()
+		goto noaccessout;
+	} else if (opipe) {
 		if (off_out)
 			return -ESPIPE;
 		if (off_in) {
@@ -1209,18 +1204,20 @@ long do_splice(struct file *in, loff_t *off_in, struct file *out,
 
 		ret = splice_file_to_pipe(in, opipe, &offset, len, flags);
 
-		if (ret > 0)
-			fsnotify_access(in);
-
 		if (!off_in)
 			in->f_pos = offset;
 		else
 			*off_in = offset;
+	} else
+		return -EINVAL;
 
-		return ret;
-	}
+	if (ret > 0)
+		fsnotify_modify(out);
+noaccessout:
+	if (ret > 0)
+		fsnotify_access(in);
 
-	return -EINVAL;
+	return ret;
 }
 
 static long __do_splice(struct file *in, loff_t __user *off_in,
-- 
2.39.2

Re: [PATCH v3 1/3] splice: always fsnotify_access(in), fsnotify_modify(out) on success
Posted by Amir Goldstein 2 years, 7 months ago
On Tue, Jun 27, 2023 at 7:55 PM Ahelenia Ziemiańska
<nabijaczleweli@nabijaczleweli.xyz> wrote:
>
> The current behaviour caused an asymmetry where some write APIs
> (write, sendfile) would notify the written-to/read-from objects,
> but splice wouldn't.
>
> This affected userspace which uses inotify, most notably coreutils
> tail -f, to monitor pipes.
> If the pipe buffer had been filled by a splice-family function:
>   * tail wouldn't know and thus wouldn't service the pipe, and
>   * all writes to the pipe would block because it's full,
> thus service was denied.
> (For the particular case of tail -f this could be worked around
>  with ---disable-inotify.)
>
> Fixes: 983652c69199 ("splice: report related fsnotify events")
> Link: https://lore.kernel.org/linux-fsdevel/jbyihkyk5dtaohdwjyivambb2gffyjs3dodpofafnkkunxq7bu@jngkdxx65pux/t/#u
> Link: https://bugs.debian.org/1039488
> Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
> Reviewed-by: Amir Goldstein <amir73il@gmail.com>
> ---
>  fs/splice.c | 31 ++++++++++++++-----------------
>  1 file changed, 14 insertions(+), 17 deletions(-)
>
> diff --git a/fs/splice.c b/fs/splice.c
> index 3e06611d19ae..e16f4f032d2f 100644
> --- a/fs/splice.c
> +++ b/fs/splice.c
> @@ -1154,10 +1154,8 @@ long do_splice(struct file *in, loff_t *off_in, struct file *out,
>                 if ((in->f_flags | out->f_flags) & O_NONBLOCK)
>                         flags |= SPLICE_F_NONBLOCK;
>
> -               return splice_pipe_to_pipe(ipipe, opipe, len, flags);
> -       }
> -
> -       if (ipipe) {
> +               ret = splice_pipe_to_pipe(ipipe, opipe, len, flags);
> +       } else if (ipipe) {
>                 if (off_in)
>                         return -ESPIPE;
>                 if (off_out) {
> @@ -1182,18 +1180,15 @@ long do_splice(struct file *in, loff_t *off_in, struct file *out,
>                 ret = do_splice_from(ipipe, out, &offset, len, flags);
>                 file_end_write(out);
>
> -               if (ret > 0)
> -                       fsnotify_modify(out);
> -
>                 if (!off_out)
>                         out->f_pos = offset;
>                 else
>                         *off_out = offset;
>
> -               return ret;
> -       }
> -
> -       if (opipe) {
> +               // splice_write-> already marked out
> +               // as modified via vfs_iter_write()
> +               goto noaccessout;
> +       } else if (opipe) {
>                 if (off_out)
>                         return -ESPIPE;
>                 if (off_in) {
> @@ -1209,18 +1204,20 @@ long do_splice(struct file *in, loff_t *off_in, struct file *out,
>
>                 ret = splice_file_to_pipe(in, opipe, &offset, len, flags);
>
> -               if (ret > 0)
> -                       fsnotify_access(in);
> -
>                 if (!off_in)
>                         in->f_pos = offset;
>                 else
>                         *off_in = offset;
> +       } else
> +               return -EINVAL;
>
> -               return ret;
> -       }
> +       if (ret > 0)
> +               fsnotify_modify(out);
> +noaccessout:
> +       if (ret > 0)
> +               fsnotify_access(in);
>

As I wrote, I don't like this special case.
I prefer that we generate double IN_MODIFY than
having to maintain unreadable code.

Let's see what Jan has to say about this.

Thanks,
Amir.
Re: [PATCH v3 1/3] splice: always fsnotify_access(in), fsnotify_modify(out) on success
Posted by Ahelenia Ziemiańska 2 years, 7 months ago
On Tue, Jun 27, 2023 at 09:10:09PM +0300, Amir Goldstein wrote:
> On Tue, Jun 27, 2023 at 7:55 PM Ahelenia Ziemiańska
> <nabijaczleweli@nabijaczleweli.xyz> wrote:
> > @@ -1209,18 +1204,20 @@ long do_splice(struct file *in, loff_t *off_in, struct file *out,
> >
> >                 ret = splice_file_to_pipe(in, opipe, &offset, len, flags);
> >
> > -               if (ret > 0)
> > -                       fsnotify_access(in);
> > -
> >                 if (!off_in)
> >                         in->f_pos = offset;
> >                 else
> >                         *off_in = offset;
> > +       } else
> > +               return -EINVAL;
> >
> > -               return ret;
> > -       }
> > +       if (ret > 0)
> > +               fsnotify_modify(out);
> > +noaccessout:
> > +       if (ret > 0)
> > +               fsnotify_access(in);
> >
> As I wrote, I don't like this special case.
> I prefer that we generate double IN_MODIFY than
> having to maintain unreadable code.
> 
> Let's see what Jan has to say about this.
Yes, in principle I definitely agree, but I don't know what the official
policy is on (effectively-)spurious/duplicate events; neither the kernel
documentation nor the manual speak to the reliability of the signal,
so I defaulted to the variant I thought to be correcter, if filthy.
[PATCH v3 2/3] splice: fsnotify_access(fd)/fsnotify_modify(fd) in vmsplice
Posted by Ahelenia Ziemiańska 2 years, 7 months ago
Same logic applies here: this can fill up the pipe and pollers that rely
on getting IN_MODIFY notifications never wake up.

Fixes: 983652c69199 ("splice: report related fsnotify events")
Link: https://lore.kernel.org/linux-fsdevel/jbyihkyk5dtaohdwjyivambb2gffyjs3dodpofafnkkunxq7bu@jngkdxx65pux/t/#u
Link: https://bugs.debian.org/1039488
Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
---
 fs/splice.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)

diff --git a/fs/splice.c b/fs/splice.c
index e16f4f032d2f..0eb36e93c030 100644
--- a/fs/splice.c
+++ b/fs/splice.c
@@ -1346,6 +1346,9 @@ static long vmsplice_to_user(struct file *file, struct iov_iter *iter,
 		pipe_unlock(pipe);
 	}
 
+	if (ret > 0)
+		fsnotify_access(file);
+
 	return ret;
 }
 
@@ -1375,8 +1378,10 @@ static long vmsplice_to_pipe(struct file *file, struct iov_iter *iter,
 	if (!ret)
 		ret = iter_to_pipe(iter, pipe, buf_flag);
 	pipe_unlock(pipe);
-	if (ret > 0)
+	if (ret > 0) {
 		wakeup_pipe_readers(pipe);
+		fsnotify_modify(file);
+	}
 	return ret;
 }
 
-- 
2.39.2

Re: [PATCH v3 2/3] splice: fsnotify_access(fd)/fsnotify_modify(fd) in vmsplice
Posted by Amir Goldstein 2 years, 7 months ago
On Tue, Jun 27, 2023 at 7:55 PM Ahelenia Ziemiańska
<nabijaczleweli@nabijaczleweli.xyz> wrote:
>
> Same logic applies here: this can fill up the pipe and pollers that rely
> on getting IN_MODIFY notifications never wake up.
>
> Fixes: 983652c69199 ("splice: report related fsnotify events")
> Link: https://lore.kernel.org/linux-fsdevel/jbyihkyk5dtaohdwjyivambb2gffyjs3dodpofafnkkunxq7bu@jngkdxx65pux/t/#u
> Link: https://bugs.debian.org/1039488
> Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>

Reviewed-by: Amir Goldstein <amir73il@gmail.com>

> ---
>  fs/splice.c | 7 ++++++-
>  1 file changed, 6 insertions(+), 1 deletion(-)
>
> diff --git a/fs/splice.c b/fs/splice.c
> index e16f4f032d2f..0eb36e93c030 100644
> --- a/fs/splice.c
> +++ b/fs/splice.c
> @@ -1346,6 +1346,9 @@ static long vmsplice_to_user(struct file *file, struct iov_iter *iter,
>                 pipe_unlock(pipe);
>         }
>
> +       if (ret > 0)
> +               fsnotify_access(file);
> +
>         return ret;
>  }
>
> @@ -1375,8 +1378,10 @@ static long vmsplice_to_pipe(struct file *file, struct iov_iter *iter,
>         if (!ret)
>                 ret = iter_to_pipe(iter, pipe, buf_flag);
>         pipe_unlock(pipe);
> -       if (ret > 0)
> +       if (ret > 0) {
>                 wakeup_pipe_readers(pipe);
> +               fsnotify_modify(file);
> +       }
>         return ret;
>  }
>
> --
> 2.39.2
>
[PATCH v3 3/3] splice: fsnotify_access(in), fsnotify_modify(out) on success in tee
Posted by Ahelenia Ziemiańska 2 years, 7 months ago
Same logic applies here: this can fill up the pipe, and pollers that rely
on getting IN_MODIFY notifications never wake up.

Fixes: 983652c69199 ("splice: report related fsnotify events")
Link: https://lore.kernel.org/linux-fsdevel/jbyihkyk5dtaohdwjyivambb2gffyjs3dodpofafnkkunxq7bu@jngkdxx65pux/t/#u
Link: https://bugs.debian.org/1039488
Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
Reviewed-by: Amir Goldstein <amir73il@gmail.com>
---
 fs/splice.c | 5 +++++
 1 file changed, 5 insertions(+)

diff --git a/fs/splice.c b/fs/splice.c
index 0eb36e93c030..2ecfccbda956 100644
--- a/fs/splice.c
+++ b/fs/splice.c
@@ -1815,6 +1815,11 @@ long do_tee(struct file *in, struct file *out, size_t len, unsigned int flags)
 		}
 	}
 
+	if (ret > 0) {
+		fsnotify_access(in);
+		fsnotify_modify(out);
+	}
+
 	return ret;
 }
 
-- 
2.39.2
[LTP PATCH] inotify13: new test for fs/splice.c functions vs pipes vs inotify
Posted by Ahelenia Ziemiańska 2 years, 7 months ago
The only one that passes on 6.1.27-1 is sendfile_file_to_pipe.

Link: https://lore.kernel.org/linux-fsdevel/jbyihkyk5dtaohdwjyivambb2gffyjs3dodpofafnkkunxq7bu@jngkdxx65pux/t/#u
Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
---
Formatted to clang-format defaults. Put the original Fixes:ed SHA in the
metadata, that's probably fine, right?

 testcases/kernel/syscalls/inotify/.gitignore  |   1 +
 testcases/kernel/syscalls/inotify/inotify13.c | 246 ++++++++++++++++++
 2 files changed, 247 insertions(+)
 create mode 100644 testcases/kernel/syscalls/inotify/inotify13.c

diff --git a/testcases/kernel/syscalls/inotify/.gitignore b/testcases/kernel/syscalls/inotify/.gitignore
index f6e5c546a..b597ea63f 100644
--- a/testcases/kernel/syscalls/inotify/.gitignore
+++ b/testcases/kernel/syscalls/inotify/.gitignore
@@ -10,3 +10,4 @@
 /inotify10
 /inotify11
 /inotify12
+/inotify13
diff --git a/testcases/kernel/syscalls/inotify/inotify13.c b/testcases/kernel/syscalls/inotify/inotify13.c
new file mode 100644
index 000000000..c34f1dc9f
--- /dev/null
+++ b/testcases/kernel/syscalls/inotify/inotify13.c
@@ -0,0 +1,246 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*\
+ * Verify splice-family functions (and sendfile) generate IN_ACCESS
+ * for what they read and IN_MODIFY for what they write.
+ *
+ * Regression test for 983652c69199 and
+ * https://lore.kernel.org/linux-fsdevel/jbyihkyk5dtaohdwjyivambb2gffyjs3dodpofafnkkunxq7bu@jngkdxx65pux/t/#u
+ */
+
+#define _GNU_SOURCE
+#include "config.h"
+
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <inttypes.h>
+#include <signal.h>
+#include <sys/mman.h>
+#include <sys/sendfile.h>
+
+#include "tst_test.h"
+#include "tst_safe_macros.h"
+#include "inotify.h"
+
+#if defined(HAVE_SYS_INOTIFY_H)
+#include <sys/inotify.h>
+
+
+static int pipes[2] = {-1, -1};
+static int inotify = -1;
+static int memfd = -1;
+static int data_pipes[2] = {-1, -1};
+
+static void watch_rw(int fd) {
+  char buf[64];
+  sprintf(buf, "/proc/self/fd/%d", fd);
+  SAFE_MYINOTIFY_ADD_WATCH(inotify, buf, IN_ACCESS | IN_MODIFY);
+}
+
+static int compar(const void *l, const void *r) {
+  const struct inotify_event *lie = l;
+  const struct inotify_event *rie = r;
+  return lie->wd - rie->wd;
+}
+
+static void get_events(size_t evcnt, struct inotify_event evs[static evcnt]) {
+  struct inotify_event tail, *itr = evs;
+  for (size_t left = evcnt; left; --left)
+    SAFE_READ(true, inotify, itr++, sizeof(struct inotify_event));
+
+  TEST(read(inotify, &tail, sizeof(struct inotify_event)));
+  if (TST_RET != -1)
+    tst_brk(TFAIL, "expect %zu events", evcnt);
+  if (TST_ERR != EAGAIN)
+    tst_brk(TFAIL | TTERRNO, "expected EAGAIN");
+
+  qsort(evs, evcnt, sizeof(struct inotify_event), compar);
+}
+
+static void expect_event(struct inotify_event *ev, int wd, uint32_t mask) {
+  if (ev->wd != wd)
+    tst_brk(TFAIL, "expect event for wd %d got %d", wd, ev->wd);
+  if (ev->mask != mask)
+    tst_brk(TFAIL, "expect event with mask %" PRIu32 " got %" PRIu32 "", mask,
+            ev->mask);
+}
+
+#define F2P(splice)                                                            \
+  SAFE_WRITE(SAFE_WRITE_RETRY, memfd, __func__, sizeof(__func__));             \
+  SAFE_LSEEK(memfd, 0, SEEK_SET);                                              \
+  watch_rw(memfd);                                                             \
+  watch_rw(pipes[0]);                                                          \
+  TEST(splice);                                                                \
+  if (TST_RET == -1)                                                           \
+    tst_brk(TBROK | TERRNO, #splice);                                          \
+  if (TST_RET != sizeof(__func__))                                             \
+    tst_brk(TBROK, #splice ": %" PRId64 "", TST_RET);                          \
+                                                                               \
+  /*expecting: IN_ACCESS memfd, IN_MODIFY pipes[0]*/                           \
+  struct inotify_event events[2];                                              \
+  get_events(ARRAY_SIZE(events), events);                                      \
+  expect_event(events + 0, 1, IN_ACCESS);                                      \
+  expect_event(events + 1, 2, IN_MODIFY);                                      \
+                                                                               \
+  char buf[sizeof(__func__)];                                                  \
+  SAFE_READ(true, pipes[0], buf, sizeof(__func__));                            \
+  if (memcmp(buf, __func__, sizeof(__func__)))                                 \
+    tst_brk(TFAIL, "buf contents bad");
+static void splice_file_to_pipe(void) {
+  F2P(splice(memfd, NULL, pipes[1], NULL, 128 * 1024 * 1024, 0));
+}
+static void sendfile_file_to_pipe(void) {
+  F2P(sendfile(pipes[1], memfd, NULL, 128 * 1024 * 1024));
+}
+
+static void splice_pipe_to_file(void) {
+  SAFE_WRITE(SAFE_WRITE_RETRY, pipes[1], __func__, sizeof(__func__));
+  watch_rw(pipes[0]);
+  watch_rw(memfd);
+  TEST(splice(pipes[0], NULL, memfd, NULL, 128 * 1024 * 1024, 0));
+  if(TST_RET == -1)
+		tst_brk(TBROK | TERRNO, "splice");
+	if(TST_RET != sizeof(__func__))
+		tst_brk(TBROK, "splice: %" PRId64 "", TST_RET);
+
+	// expecting: IN_ACCESS pipes[0], IN_MODIFY memfd
+	struct inotify_event events[2];
+	get_events(ARRAY_SIZE(events), events);
+	expect_event(events + 0, 1, IN_ACCESS);
+	expect_event(events + 1, 2, IN_MODIFY);
+
+  char buf[sizeof(__func__)];
+  SAFE_LSEEK(memfd, 0, SEEK_SET);
+  SAFE_READ(true, memfd, buf, sizeof(__func__));
+  if (memcmp(buf, __func__, sizeof(__func__)))
+                tst_brk(TFAIL, "buf contents bad");
+}
+
+#define P2P(splice)                                                            \
+  SAFE_WRITE(SAFE_WRITE_RETRY, data_pipes[1], __func__, sizeof(__func__));     \
+  watch_rw(data_pipes[0]);                                                     \
+  watch_rw(pipes[1]);                                                          \
+  TEST(splice);                                                                \
+  if (TST_RET == -1)                                                           \
+                tst_brk(TBROK | TERRNO, #splice);                              \
+  if (TST_RET != sizeof(__func__))                                             \
+                tst_brk(TBROK, #splice ": %" PRId64 "", TST_RET);              \
+                                                                               \
+  /* expecting: IN_ACCESS data_pipes[0], IN_MODIFY pipes[1] */                 \
+  struct inotify_event events[2];                                              \
+  get_events(ARRAY_SIZE(events), events);                                      \
+  expect_event(events + 0, 1, IN_ACCESS);                                      \
+  expect_event(events + 1, 2, IN_MODIFY);                                      \
+                                                                               \
+  char buf[sizeof(__func__)];                                                  \
+  SAFE_READ(true, pipes[0], buf, sizeof(__func__));                            \
+  if (memcmp(buf, __func__, sizeof(__func__)))                                 \
+                tst_brk(TFAIL, "buf contents bad");
+static void splice_pipe_to_pipe(void) {
+  P2P(splice(data_pipes[0], NULL, pipes[1], NULL, 128 * 1024 * 1024, 0));
+}
+static void tee_pipe_to_pipe(void) {
+  P2P(tee(data_pipes[0], pipes[1], 128 * 1024 * 1024, 0));
+}
+
+static char vmsplice_pipe_to_mem_dt[32 * 1024];
+static void vmsplice_pipe_to_mem(void) {
+  memcpy(vmsplice_pipe_to_mem_dt, __func__, sizeof(__func__));
+  watch_rw(pipes[0]);
+  TEST(vmsplice(pipes[1],
+                &(struct iovec){.iov_base = vmsplice_pipe_to_mem_dt,
+                                .iov_len = sizeof(vmsplice_pipe_to_mem_dt)},
+                1, SPLICE_F_GIFT));
+  if (TST_RET == -1)
+    tst_brk(TBROK | TERRNO, "vmsplice");
+  if (TST_RET != sizeof(vmsplice_pipe_to_mem_dt))
+    tst_brk(TBROK, "vmsplice: %" PRId64 "", TST_RET);
+
+  // expecting: IN_MODIFY pipes[0]
+  struct inotify_event event;
+  get_events(1, &event);
+  expect_event(&event, 1, IN_MODIFY);
+
+  char buf[sizeof(__func__)];
+  SAFE_READ(true, pipes[0], buf, sizeof(__func__));
+  if (memcmp(buf, __func__, sizeof(__func__)))
+    tst_brk(TFAIL, "buf contents bad");
+}
+
+static void vmsplice_mem_to_pipe(void) {
+  char buf[sizeof(__func__)];
+  SAFE_WRITE(SAFE_WRITE_RETRY, pipes[1], __func__, sizeof(__func__));
+  watch_rw(pipes[1]);
+  TEST(vmsplice(pipes[0],
+                &(struct iovec){.iov_base = buf, .iov_len = sizeof(buf)}, 1,
+                0));
+  if (TST_RET == -1)
+    tst_brk(TBROK | TERRNO, "vmsplice");
+  if (TST_RET != sizeof(buf))
+    tst_brk(TBROK, "vmsplice: %" PRId64 "", TST_RET);
+
+  // expecting: IN_ACCESS pipes[1]
+  struct inotify_event event;
+  get_events(1, &event);
+  expect_event(&event, 1, IN_ACCESS);
+  if (memcmp(buf, __func__, sizeof(__func__)))
+    tst_brk(TFAIL, "buf contents bad");
+}
+
+typedef void (*tests_f)(void);
+#define TEST_F(f) { f, #f }
+static const struct {
+        tests_f f;
+        const char *n;
+} tests[] = {
+    TEST_F(splice_file_to_pipe),  TEST_F(sendfile_file_to_pipe),
+    TEST_F(splice_pipe_to_file),  TEST_F(splice_pipe_to_pipe),
+    TEST_F(tee_pipe_to_pipe),     TEST_F(vmsplice_pipe_to_mem),
+    TEST_F(vmsplice_mem_to_pipe),
+};
+
+static void run_test(unsigned int n)
+{
+	tst_res(TINFO, "%s", tests[n].n);
+
+	SAFE_PIPE2(pipes, O_CLOEXEC);
+	SAFE_PIPE2(data_pipes, O_CLOEXEC);
+	inotify = SAFE_MYINOTIFY_INIT1(IN_NONBLOCK | IN_CLOEXEC);
+	if((memfd = memfd_create(__func__, MFD_CLOEXEC)) == -1)
+		tst_brk(TCONF | TERRNO, "memfd");
+	tests[n].f();
+	tst_res(TPASS, "ок");
+}
+
+static void cleanup(void)
+{
+	if (memfd != -1)
+		SAFE_CLOSE(memfd);
+	if (inotify != -1)
+		SAFE_CLOSE(inotify);
+	if (pipes[0] != -1)
+		SAFE_CLOSE(pipes[0]);
+	if (pipes[1] != -1)
+		SAFE_CLOSE(pipes[1]);
+	if (data_pipes[0] != -1)
+		SAFE_CLOSE(data_pipes[0]);
+	if (data_pipes[1] != -1)
+		SAFE_CLOSE(data_pipes[1]);
+}
+
+static struct tst_test test = {
+	.max_runtime = 10,
+	.cleanup = cleanup,
+	.test = run_test,
+	.tcnt = ARRAY_SIZE(tests),
+	.tags = (const struct tst_tag[]) {
+		{"linux-git", "983652c69199"},
+		{}
+	},
+};
+
+#else
+	TST_TEST_TCONF("system doesn't have required inotify support");
+#endif
-- 
2.39.2
Re: [LTP PATCH] inotify13: new test for fs/splice.c functions vs pipes vs inotify
Posted by Amir Goldstein 2 years, 7 months ago
On Tue, Jun 27, 2023 at 7:57 PM Ahelenia Ziemiańska
<nabijaczleweli@nabijaczleweli.xyz> wrote:
>
> The only one that passes on 6.1.27-1 is sendfile_file_to_pipe.
>
> Link: https://lore.kernel.org/linux-fsdevel/jbyihkyk5dtaohdwjyivambb2gffyjs3dodpofafnkkunxq7bu@jngkdxx65pux/t/#u
> Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
> ---
> Formatted to clang-format defaults. Put the original Fixes:ed SHA in the
> metadata, that's probably fine, right?

No. The git commit is for the commits that fix the problem.
This can only be added after your fixes are merged.

I will let the LPT developers comment about style,
but I think LTP project wants tab indents.
I am personally unable to read this patch with so little indentation
and so much macroing.

>
>  testcases/kernel/syscalls/inotify/.gitignore  |   1 +
>  testcases/kernel/syscalls/inotify/inotify13.c | 246 ++++++++++++++++++
>  2 files changed, 247 insertions(+)
>  create mode 100644 testcases/kernel/syscalls/inotify/inotify13.c
>
> diff --git a/testcases/kernel/syscalls/inotify/.gitignore b/testcases/kernel/syscalls/inotify/.gitignore
> index f6e5c546a..b597ea63f 100644
> --- a/testcases/kernel/syscalls/inotify/.gitignore
> +++ b/testcases/kernel/syscalls/inotify/.gitignore
> @@ -10,3 +10,4 @@
>  /inotify10
>  /inotify11
>  /inotify12
> +/inotify13
> diff --git a/testcases/kernel/syscalls/inotify/inotify13.c b/testcases/kernel/syscalls/inotify/inotify13.c
> new file mode 100644
> index 000000000..c34f1dc9f
> --- /dev/null
> +++ b/testcases/kernel/syscalls/inotify/inotify13.c
> @@ -0,0 +1,246 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*\
> + * Verify splice-family functions (and sendfile) generate IN_ACCESS
> + * for what they read and IN_MODIFY for what they write.
> + *
> + * Regression test for 983652c69199 and
> + * https://lore.kernel.org/linux-fsdevel/jbyihkyk5dtaohdwjyivambb2gffyjs3dodpofafnkkunxq7bu@jngkdxx65pux/t/#u
> + */
> +
> +#define _GNU_SOURCE
> +#include "config.h"
> +
> +#include <stdio.h>
> +#include <unistd.h>
> +#include <stdlib.h>
> +#include <fcntl.h>
> +#include <stdbool.h>
> +#include <inttypes.h>
> +#include <signal.h>
> +#include <sys/mman.h>
> +#include <sys/sendfile.h>
> +
> +#include "tst_test.h"
> +#include "tst_safe_macros.h"
> +#include "inotify.h"
> +
> +#if defined(HAVE_SYS_INOTIFY_H)
> +#include <sys/inotify.h>
> +
> +
> +static int pipes[2] = {-1, -1};
> +static int inotify = -1;
> +static int memfd = -1;
> +static int data_pipes[2] = {-1, -1};
> +
> +static void watch_rw(int fd) {
> +  char buf[64];
> +  sprintf(buf, "/proc/self/fd/%d", fd);
> +  SAFE_MYINOTIFY_ADD_WATCH(inotify, buf, IN_ACCESS | IN_MODIFY);
> +}
> +
> +static int compar(const void *l, const void *r) {
> +  const struct inotify_event *lie = l;
> +  const struct inotify_event *rie = r;
> +  return lie->wd - rie->wd;
> +}
> +
> +static void get_events(size_t evcnt, struct inotify_event evs[static evcnt]) {
> +  struct inotify_event tail, *itr = evs;
> +  for (size_t left = evcnt; left; --left)
> +    SAFE_READ(true, inotify, itr++, sizeof(struct inotify_event));
> +
> +  TEST(read(inotify, &tail, sizeof(struct inotify_event)));
> +  if (TST_RET != -1)
> +    tst_brk(TFAIL, "expect %zu events", evcnt);
> +  if (TST_ERR != EAGAIN)
> +    tst_brk(TFAIL | TTERRNO, "expected EAGAIN");
> +
> +  qsort(evs, evcnt, sizeof(struct inotify_event), compar);
> +}
> +
> +static void expect_event(struct inotify_event *ev, int wd, uint32_t mask) {
> +  if (ev->wd != wd)
> +    tst_brk(TFAIL, "expect event for wd %d got %d", wd, ev->wd);
> +  if (ev->mask != mask)
> +    tst_brk(TFAIL, "expect event with mask %" PRIu32 " got %" PRIu32 "", mask,
> +            ev->mask);
> +}
> +
> +#define F2P(splice)                                                            \
> +  SAFE_WRITE(SAFE_WRITE_RETRY, memfd, __func__, sizeof(__func__));             \
> +  SAFE_LSEEK(memfd, 0, SEEK_SET);                                              \
> +  watch_rw(memfd);                                                             \
> +  watch_rw(pipes[0]);                                                          \
> +  TEST(splice);                                                                \
> +  if (TST_RET == -1)                                                           \
> +    tst_brk(TBROK | TERRNO, #splice);                                          \
> +  if (TST_RET != sizeof(__func__))                                             \
> +    tst_brk(TBROK, #splice ": %" PRId64 "", TST_RET);                          \
> +                                                                               \
> +  /*expecting: IN_ACCESS memfd, IN_MODIFY pipes[0]*/                           \
> +  struct inotify_event events[2];                                              \
> +  get_events(ARRAY_SIZE(events), events);                                      \
> +  expect_event(events + 0, 1, IN_ACCESS);                                      \
> +  expect_event(events + 1, 2, IN_MODIFY);                                      \
> +                                                                               \
> +  char buf[sizeof(__func__)];                                                  \
> +  SAFE_READ(true, pipes[0], buf, sizeof(__func__));                            \
> +  if (memcmp(buf, __func__, sizeof(__func__)))                                 \
> +    tst_brk(TFAIL, "buf contents bad");
> +static void splice_file_to_pipe(void) {
> +  F2P(splice(memfd, NULL, pipes[1], NULL, 128 * 1024 * 1024, 0));
> +}
> +static void sendfile_file_to_pipe(void) {
> +  F2P(sendfile(pipes[1], memfd, NULL, 128 * 1024 * 1024));
> +}
> +
> +static void splice_pipe_to_file(void) {
> +  SAFE_WRITE(SAFE_WRITE_RETRY, pipes[1], __func__, sizeof(__func__));
> +  watch_rw(pipes[0]);
> +  watch_rw(memfd);
> +  TEST(splice(pipes[0], NULL, memfd, NULL, 128 * 1024 * 1024, 0));
> +  if(TST_RET == -1)
> +               tst_brk(TBROK | TERRNO, "splice");
> +       if(TST_RET != sizeof(__func__))
> +               tst_brk(TBROK, "splice: %" PRId64 "", TST_RET);
> +
> +       // expecting: IN_ACCESS pipes[0], IN_MODIFY memfd
> +       struct inotify_event events[2];
> +       get_events(ARRAY_SIZE(events), events);
> +       expect_event(events + 0, 1, IN_ACCESS);
> +       expect_event(events + 1, 2, IN_MODIFY);
> +
> +  char buf[sizeof(__func__)];
> +  SAFE_LSEEK(memfd, 0, SEEK_SET);
> +  SAFE_READ(true, memfd, buf, sizeof(__func__));
> +  if (memcmp(buf, __func__, sizeof(__func__)))
> +                tst_brk(TFAIL, "buf contents bad");
> +}
> +
> +#define P2P(splice)                                                            \
> +  SAFE_WRITE(SAFE_WRITE_RETRY, data_pipes[1], __func__, sizeof(__func__));     \
> +  watch_rw(data_pipes[0]);                                                     \
> +  watch_rw(pipes[1]);                                                          \
> +  TEST(splice);                                                                \
> +  if (TST_RET == -1)                                                           \
> +                tst_brk(TBROK | TERRNO, #splice);                              \
> +  if (TST_RET != sizeof(__func__))                                             \
> +                tst_brk(TBROK, #splice ": %" PRId64 "", TST_RET);              \
> +                                                                               \
> +  /* expecting: IN_ACCESS data_pipes[0], IN_MODIFY pipes[1] */                 \
> +  struct inotify_event events[2];                                              \
> +  get_events(ARRAY_SIZE(events), events);                                      \
> +  expect_event(events + 0, 1, IN_ACCESS);                                      \
> +  expect_event(events + 1, 2, IN_MODIFY);                                      \
> +                                                                               \
> +  char buf[sizeof(__func__)];                                                  \
> +  SAFE_READ(true, pipes[0], buf, sizeof(__func__));                            \
> +  if (memcmp(buf, __func__, sizeof(__func__)))                                 \
> +                tst_brk(TFAIL, "buf contents bad");
> +static void splice_pipe_to_pipe(void) {
> +  P2P(splice(data_pipes[0], NULL, pipes[1], NULL, 128 * 1024 * 1024, 0));
> +}
> +static void tee_pipe_to_pipe(void) {
> +  P2P(tee(data_pipes[0], pipes[1], 128 * 1024 * 1024, 0));
> +}
> +
> +static char vmsplice_pipe_to_mem_dt[32 * 1024];
> +static void vmsplice_pipe_to_mem(void) {
> +  memcpy(vmsplice_pipe_to_mem_dt, __func__, sizeof(__func__));
> +  watch_rw(pipes[0]);
> +  TEST(vmsplice(pipes[1],
> +                &(struct iovec){.iov_base = vmsplice_pipe_to_mem_dt,
> +                                .iov_len = sizeof(vmsplice_pipe_to_mem_dt)},
> +                1, SPLICE_F_GIFT));
> +  if (TST_RET == -1)
> +    tst_brk(TBROK | TERRNO, "vmsplice");
> +  if (TST_RET != sizeof(vmsplice_pipe_to_mem_dt))
> +    tst_brk(TBROK, "vmsplice: %" PRId64 "", TST_RET);
> +
> +  // expecting: IN_MODIFY pipes[0]
> +  struct inotify_event event;
> +  get_events(1, &event);
> +  expect_event(&event, 1, IN_MODIFY);
> +
> +  char buf[sizeof(__func__)];
> +  SAFE_READ(true, pipes[0], buf, sizeof(__func__));
> +  if (memcmp(buf, __func__, sizeof(__func__)))
> +    tst_brk(TFAIL, "buf contents bad");
> +}
> +
> +static void vmsplice_mem_to_pipe(void) {
> +  char buf[sizeof(__func__)];
> +  SAFE_WRITE(SAFE_WRITE_RETRY, pipes[1], __func__, sizeof(__func__));
> +  watch_rw(pipes[1]);
> +  TEST(vmsplice(pipes[0],
> +                &(struct iovec){.iov_base = buf, .iov_len = sizeof(buf)}, 1,
> +                0));
> +  if (TST_RET == -1)
> +    tst_brk(TBROK | TERRNO, "vmsplice");
> +  if (TST_RET != sizeof(buf))
> +    tst_brk(TBROK, "vmsplice: %" PRId64 "", TST_RET);
> +
> +  // expecting: IN_ACCESS pipes[1]
> +  struct inotify_event event;
> +  get_events(1, &event);
> +  expect_event(&event, 1, IN_ACCESS);
> +  if (memcmp(buf, __func__, sizeof(__func__)))
> +    tst_brk(TFAIL, "buf contents bad");
> +}
> +
> +typedef void (*tests_f)(void);
> +#define TEST_F(f) { f, #f }
> +static const struct {
> +        tests_f f;
> +        const char *n;
> +} tests[] = {
> +    TEST_F(splice_file_to_pipe),  TEST_F(sendfile_file_to_pipe),
> +    TEST_F(splice_pipe_to_file),  TEST_F(splice_pipe_to_pipe),
> +    TEST_F(tee_pipe_to_pipe),     TEST_F(vmsplice_pipe_to_mem),
> +    TEST_F(vmsplice_mem_to_pipe),
> +};
> +
> +static void run_test(unsigned int n)
> +{
> +       tst_res(TINFO, "%s", tests[n].n);
> +
> +       SAFE_PIPE2(pipes, O_CLOEXEC);
> +       SAFE_PIPE2(data_pipes, O_CLOEXEC);
> +       inotify = SAFE_MYINOTIFY_INIT1(IN_NONBLOCK | IN_CLOEXEC);
> +       if((memfd = memfd_create(__func__, MFD_CLOEXEC)) == -1)
> +               tst_brk(TCONF | TERRNO, "memfd");
> +       tests[n].f();

Normally, a test cases table would encode things like
the number of expected events and type of events.
The idea is that the test template has parametrized code
and not just a loop for test cases subroutines, but there
are many ways to write tests, so as long as it gets the job
done and is readable to humans, I don't mind.

Right now this test may do the job, but it is not readable
for this human ;-)
mostly because of the huge macros -
LTP is known for pretty large macros, but those are
for generic utilities and you have complete test cases
written as macros (templates).

> +       tst_res(TPASS, "ок");
> +}
> +
> +static void cleanup(void)
> +{
> +       if (memfd != -1)
> +               SAFE_CLOSE(memfd);
> +       if (inotify != -1)
> +               SAFE_CLOSE(inotify);
> +       if (pipes[0] != -1)
> +               SAFE_CLOSE(pipes[0]);
> +       if (pipes[1] != -1)
> +               SAFE_CLOSE(pipes[1]);
> +       if (data_pipes[0] != -1)
> +               SAFE_CLOSE(data_pipes[0]);
> +       if (data_pipes[1] != -1)
> +               SAFE_CLOSE(data_pipes[1]);
> +}
> +

This cleanup does not happen for every test case -
it happens only at the end of all the tests IIRC.

> +static struct tst_test test = {
> +       .max_runtime = 10,
> +       .cleanup = cleanup,
> +       .test = run_test,
> +       .tcnt = ARRAY_SIZE(tests),
> +       .tags = (const struct tst_tag[]) {
> +               {"linux-git", "983652c69199"},

Leave this out for now.

Thanks,
Amir.
Re: [LTP PATCH] inotify13: new test for fs/splice.c functions vs pipes vs inotify
Posted by Petr Vorel 2 years, 7 months ago
> On Tue, Jun 27, 2023 at 7:57 PM Ahelenia Ziemiańska
> <nabijaczleweli@nabijaczleweli.xyz> wrote:

> > The only one that passes on 6.1.27-1 is sendfile_file_to_pipe.

> > Link: https://lore.kernel.org/linux-fsdevel/jbyihkyk5dtaohdwjyivambb2gffyjs3dodpofafnkkunxq7bu@jngkdxx65pux/t/#u
> > Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
> > ---
> > Formatted to clang-format defaults. Put the original Fixes:ed SHA in the
> > metadata, that's probably fine, right?

> No. The git commit is for the commits that fix the problem.
> This can only be added after your fixes are merged.

> I will let the LPT developers comment about style,
> but I think LTP project wants tab indents.
> I am personally unable to read this patch with so little indentation
> and so much macroing.

Yes, it's hard to read. Style formatting it would improve it little bit
(make check-inotify13 is your friend, it complains a lot, also some spaces above
if () would make it more readable), but there are other things, e.g. macros
F2P(splice) and P2P(splice) should be functions (readability). Please have look
at other inotify tests, they are fairly simple and easy to read.

Also, this is a patch for LTP, you're supposed to post it also to LTP mailing
list (ltp@lists.linux.it, you need to register to
https://lists.linux.it/listinfo/ltp first).

> >  testcases/kernel/syscalls/inotify/.gitignore  |   1 +
> >  testcases/kernel/syscalls/inotify/inotify13.c | 246 ++++++++++++++++++
> >  2 files changed, 247 insertions(+)
> >  create mode 100644 testcases/kernel/syscalls/inotify/inotify13.c

> > diff --git a/testcases/kernel/syscalls/inotify/.gitignore b/testcases/kernel/syscalls/inotify/.gitignore
> > index f6e5c546a..b597ea63f 100644
> > --- a/testcases/kernel/syscalls/inotify/.gitignore
> > +++ b/testcases/kernel/syscalls/inotify/.gitignore
> > @@ -10,3 +10,4 @@
> >  /inotify10
> >  /inotify11
> >  /inotify12
> > +/inotify13
> > diff --git a/testcases/kernel/syscalls/inotify/inotify13.c b/testcases/kernel/syscalls/inotify/inotify13.c
> > new file mode 100644
> > index 000000000..c34f1dc9f
> > --- /dev/null
> > +++ b/testcases/kernel/syscalls/inotify/inotify13.c
> > @@ -0,0 +1,246 @@
> > +// SPDX-License-Identifier: GPL-2.0-or-later
> > +/*\

You need to add here:
* [Description]

> > + * Verify splice-family functions (and sendfile) generate IN_ACCESS
> > + * for what they read and IN_MODIFY for what they write.
> > + *
> > + * Regression test for 983652c69199 and
I guess there would be only 983652c69199 ("splice: report related fsnotify
events").
> > + * https://lore.kernel.org/linux-fsdevel/jbyihkyk5dtaohdwjyivambb2gffyjs3dodpofafnkkunxq7bu@jngkdxx65pux/t/#u
This is some discussion, not a real patch. Not sure how much useful it is, until
it results to fix accepted in the mainline kernel.

> > + */
> > +
> > +#define _GNU_SOURCE
> > +#include "config.h"
> > +
> > +#include <stdio.h>
> > +#include <unistd.h>
> > +#include <stdlib.h>
> > +#include <fcntl.h>
> > +#include <stdbool.h>
> > +#include <inttypes.h>
> > +#include <signal.h>
> > +#include <sys/mman.h>
> > +#include <sys/sendfile.h>
> > +
> > +#include "tst_test.h"
> > +#include "tst_safe_macros.h"
> > +#include "inotify.h"
> > +
> > +#if defined(HAVE_SYS_INOTIFY_H)
> > +#include <sys/inotify.h>
> > +
> > +
> > +static int pipes[2] = {-1, -1};
> > +static int inotify = -1;
> > +static int memfd = -1;
> > +static int data_pipes[2] = {-1, -1};
> > +
> > +static void watch_rw(int fd) {
> > +  char buf[64];
> > +  sprintf(buf, "/proc/self/fd/%d", fd);
> > +  SAFE_MYINOTIFY_ADD_WATCH(inotify, buf, IN_ACCESS | IN_MODIFY);
> > +}
> > +
> > +static int compar(const void *l, const void *r) {
> > +  const struct inotify_event *lie = l;
> > +  const struct inotify_event *rie = r;
> > +  return lie->wd - rie->wd;
> > +}
> > +
> > +static void get_events(size_t evcnt, struct inotify_event evs[static evcnt]) {
> > +  struct inotify_event tail, *itr = evs;
> > +  for (size_t left = evcnt; left; --left)
> > +    SAFE_READ(true, inotify, itr++, sizeof(struct inotify_event));
> > +
> > +  TEST(read(inotify, &tail, sizeof(struct inotify_event)));
> > +  if (TST_RET != -1)
> > +    tst_brk(TFAIL, "expect %zu events", evcnt);
> > +  if (TST_ERR != EAGAIN)
> > +    tst_brk(TFAIL | TTERRNO, "expected EAGAIN");
> > +
> > +  qsort(evs, evcnt, sizeof(struct inotify_event), compar);
> > +}
> > +
> > +static void expect_event(struct inotify_event *ev, int wd, uint32_t mask) {
> > +  if (ev->wd != wd)
> > +    tst_brk(TFAIL, "expect event for wd %d got %d", wd, ev->wd);
> > +  if (ev->mask != mask)
> > +    tst_brk(TFAIL, "expect event with mask %" PRIu32 " got %" PRIu32 "", mask,
> > +            ev->mask);
> > +}
> > +
> > +#define F2P(splice)                                                            \
> > +  SAFE_WRITE(SAFE_WRITE_RETRY, memfd, __func__, sizeof(__func__));             \
> > +  SAFE_LSEEK(memfd, 0, SEEK_SET);                                              \
> > +  watch_rw(memfd);                                                             \
> > +  watch_rw(pipes[0]);                                                          \
> > +  TEST(splice);                                                                \
> > +  if (TST_RET == -1)                                                           \
> > +    tst_brk(TBROK | TERRNO, #splice);                                          \
> > +  if (TST_RET != sizeof(__func__))                                             \
> > +    tst_brk(TBROK, #splice ": %" PRId64 "", TST_RET);                          \
> > +                                                                               \
> > +  /*expecting: IN_ACCESS memfd, IN_MODIFY pipes[0]*/                           \
> > +  struct inotify_event events[2];                                              \
> > +  get_events(ARRAY_SIZE(events), events);                                      \
> > +  expect_event(events + 0, 1, IN_ACCESS);                                      \
> > +  expect_event(events + 1, 2, IN_MODIFY);                                      \
> > +                                                                               \
> > +  char buf[sizeof(__func__)];                                                  \
> > +  SAFE_READ(true, pipes[0], buf, sizeof(__func__));                            \
> > +  if (memcmp(buf, __func__, sizeof(__func__)))                                 \
> > +    tst_brk(TFAIL, "buf contents bad");
> > +static void splice_file_to_pipe(void) {
> > +  F2P(splice(memfd, NULL, pipes[1], NULL, 128 * 1024 * 1024, 0));
> > +}
> > +static void sendfile_file_to_pipe(void) {
> > +  F2P(sendfile(pipes[1], memfd, NULL, 128 * 1024 * 1024));
> > +}
> > +
> > +static void splice_pipe_to_file(void) {
> > +  SAFE_WRITE(SAFE_WRITE_RETRY, pipes[1], __func__, sizeof(__func__));
> > +  watch_rw(pipes[0]);
> > +  watch_rw(memfd);
> > +  TEST(splice(pipes[0], NULL, memfd, NULL, 128 * 1024 * 1024, 0));
> > +  if(TST_RET == -1)
> > +               tst_brk(TBROK | TERRNO, "splice");
> > +       if(TST_RET != sizeof(__func__))
> > +               tst_brk(TBROK, "splice: %" PRId64 "", TST_RET);
> > +
> > +       // expecting: IN_ACCESS pipes[0], IN_MODIFY memfd
> > +       struct inotify_event events[2];
> > +       get_events(ARRAY_SIZE(events), events);
> > +       expect_event(events + 0, 1, IN_ACCESS);
> > +       expect_event(events + 1, 2, IN_MODIFY);
> > +
> > +  char buf[sizeof(__func__)];
> > +  SAFE_LSEEK(memfd, 0, SEEK_SET);
> > +  SAFE_READ(true, memfd, buf, sizeof(__func__));
> > +  if (memcmp(buf, __func__, sizeof(__func__)))
> > +                tst_brk(TFAIL, "buf contents bad");
> > +}
> > +
> > +#define P2P(splice)                                                            \
> > +  SAFE_WRITE(SAFE_WRITE_RETRY, data_pipes[1], __func__, sizeof(__func__));     \
> > +  watch_rw(data_pipes[0]);                                                     \
> > +  watch_rw(pipes[1]);                                                          \
> > +  TEST(splice);                                                                \
> > +  if (TST_RET == -1)                                                           \
> > +                tst_brk(TBROK | TERRNO, #splice);                              \
> > +  if (TST_RET != sizeof(__func__))                                             \
> > +                tst_brk(TBROK, #splice ": %" PRId64 "", TST_RET);              \
> > +                                                                               \
> > +  /* expecting: IN_ACCESS data_pipes[0], IN_MODIFY pipes[1] */                 \
> > +  struct inotify_event events[2];                                              \
> > +  get_events(ARRAY_SIZE(events), events);                                      \
> > +  expect_event(events + 0, 1, IN_ACCESS);                                      \
> > +  expect_event(events + 1, 2, IN_MODIFY);                                      \
> > +                                                                               \
> > +  char buf[sizeof(__func__)];                                                  \
> > +  SAFE_READ(true, pipes[0], buf, sizeof(__func__));                            \
> > +  if (memcmp(buf, __func__, sizeof(__func__)))                                 \
> > +                tst_brk(TFAIL, "buf contents bad");
> > +static void splice_pipe_to_pipe(void) {
> > +  P2P(splice(data_pipes[0], NULL, pipes[1], NULL, 128 * 1024 * 1024, 0));
> > +}
> > +static void tee_pipe_to_pipe(void) {
> > +  P2P(tee(data_pipes[0], pipes[1], 128 * 1024 * 1024, 0));
> > +}
> > +
> > +static char vmsplice_pipe_to_mem_dt[32 * 1024];
> > +static void vmsplice_pipe_to_mem(void) {
> > +  memcpy(vmsplice_pipe_to_mem_dt, __func__, sizeof(__func__));
> > +  watch_rw(pipes[0]);
> > +  TEST(vmsplice(pipes[1],
> > +                &(struct iovec){.iov_base = vmsplice_pipe_to_mem_dt,
> > +                                .iov_len = sizeof(vmsplice_pipe_to_mem_dt)},
> > +                1, SPLICE_F_GIFT));
> > +  if (TST_RET == -1)
> > +    tst_brk(TBROK | TERRNO, "vmsplice");
> > +  if (TST_RET != sizeof(vmsplice_pipe_to_mem_dt))
> > +    tst_brk(TBROK, "vmsplice: %" PRId64 "", TST_RET);
> > +
> > +  // expecting: IN_MODIFY pipes[0]
> > +  struct inotify_event event;
> > +  get_events(1, &event);
> > +  expect_event(&event, 1, IN_MODIFY);
> > +
> > +  char buf[sizeof(__func__)];
> > +  SAFE_READ(true, pipes[0], buf, sizeof(__func__));
> > +  if (memcmp(buf, __func__, sizeof(__func__)))
> > +    tst_brk(TFAIL, "buf contents bad");
> > +}
> > +
> > +static void vmsplice_mem_to_pipe(void) {
> > +  char buf[sizeof(__func__)];
> > +  SAFE_WRITE(SAFE_WRITE_RETRY, pipes[1], __func__, sizeof(__func__));
> > +  watch_rw(pipes[1]);
> > +  TEST(vmsplice(pipes[0],
> > +                &(struct iovec){.iov_base = buf, .iov_len = sizeof(buf)}, 1,
> > +                0));
> > +  if (TST_RET == -1)
> > +    tst_brk(TBROK | TERRNO, "vmsplice");
> > +  if (TST_RET != sizeof(buf))
> > +    tst_brk(TBROK, "vmsplice: %" PRId64 "", TST_RET);
> > +
> > +  // expecting: IN_ACCESS pipes[1]
> > +  struct inotify_event event;
> > +  get_events(1, &event);
> > +  expect_event(&event, 1, IN_ACCESS);
> > +  if (memcmp(buf, __func__, sizeof(__func__)))
> > +    tst_brk(TFAIL, "buf contents bad");
> > +}
> > +
> > +typedef void (*tests_f)(void);
> > +#define TEST_F(f) { f, #f }
> > +static const struct {
> > +        tests_f f;
> > +        const char *n;
> > +} tests[] = {
> > +    TEST_F(splice_file_to_pipe),  TEST_F(sendfile_file_to_pipe),
> > +    TEST_F(splice_pipe_to_file),  TEST_F(splice_pipe_to_pipe),
> > +    TEST_F(tee_pipe_to_pipe),     TEST_F(vmsplice_pipe_to_mem),
> > +    TEST_F(vmsplice_mem_to_pipe),
> > +};
> > +
> > +static void run_test(unsigned int n)
> > +{
> > +       tst_res(TINFO, "%s", tests[n].n);
> > +
> > +       SAFE_PIPE2(pipes, O_CLOEXEC);
> > +       SAFE_PIPE2(data_pipes, O_CLOEXEC);
> > +       inotify = SAFE_MYINOTIFY_INIT1(IN_NONBLOCK | IN_CLOEXEC);
> > +       if((memfd = memfd_create(__func__, MFD_CLOEXEC)) == -1)
> > +               tst_brk(TCONF | TERRNO, "memfd");
> > +       tests[n].f();

> Normally, a test cases table would encode things like
> the number of expected events and type of events.
> The idea is that the test template has parametrized code
> and not just a loop for test cases subroutines, but there
> are many ways to write tests, so as long as it gets the job
> done and is readable to humans, I don't mind.

> Right now this test may do the job, but it is not readable
> for this human ;-)
> mostly because of the huge macros -
> LTP is known for pretty large macros, but those are
> for generic utilities and you have complete test cases
> written as macros (templates).

+100. We strive for simple readable code, which is not this one.

Kind regards,
Petr

> > +       tst_res(TPASS, "ок");
> > +}
> > +
> > +static void cleanup(void)
> > +{
> > +       if (memfd != -1)
> > +               SAFE_CLOSE(memfd);
> > +       if (inotify != -1)
> > +               SAFE_CLOSE(inotify);
> > +       if (pipes[0] != -1)
> > +               SAFE_CLOSE(pipes[0]);
> > +       if (pipes[1] != -1)
> > +               SAFE_CLOSE(pipes[1]);
> > +       if (data_pipes[0] != -1)
> > +               SAFE_CLOSE(data_pipes[0]);
> > +       if (data_pipes[1] != -1)
> > +               SAFE_CLOSE(data_pipes[1]);
> > +}
> > +

> This cleanup does not happen for every test case -
> it happens only at the end of all the tests IIRC.

> > +static struct tst_test test = {
> > +       .max_runtime = 10,
> > +       .cleanup = cleanup,
> > +       .test = run_test,
> > +       .tcnt = ARRAY_SIZE(tests),
> > +       .tags = (const struct tst_tag[]) {
> > +               {"linux-git", "983652c69199"},

> Leave this out for now.

> Thanks,
> Amir.
[LTP RFC PATCH v2] inotify13: new test for fs/splice.c functions vs pipes vs inotify
Posted by Ahelenia Ziemiańska 2 years, 7 months ago
The only one that passes on 6.1.27-1 is sendfile_file_to_pipe.

Link: https://lore.kernel.org/linux-fsdevel/jbyihkyk5dtaohdwjyivambb2gffyjs3dodpofafnkkunxq7bu@jngkdxx65pux/t/#u
Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
---
Whitespace-only changes against v1.
Marking as RFC since we have no commit SHA to reference.

I didn't find any style opinions in the repository, so I just let
clang-format rip. Apparently, according to a /github wiki page/:
  https://github.com/linux-test-project/ltp/wiki/Test-Writing-Guidelines#2-coding-style
which is /only referenced/ from a 2020 issue:
  https://github.com/linux-test-project/ltp/issues/631
LTP uses the kernel style.

Could've fooled me, because the other inotify tests are definitely not
in the kernel style.

Re-formatted the whole file with the linux .clang-format.

 testcases/kernel/syscalls/inotify/.gitignore  |   1 +
 testcases/kernel/syscalls/inotify/inotify13.c | 260 ++++++++++++++++++
 2 files changed, 261 insertions(+)
 create mode 100644 testcases/kernel/syscalls/inotify/inotify13.c

diff --git a/testcases/kernel/syscalls/inotify/.gitignore b/testcases/kernel/syscalls/inotify/.gitignore
index f6e5c546a..b597ea63f 100644
--- a/testcases/kernel/syscalls/inotify/.gitignore
+++ b/testcases/kernel/syscalls/inotify/.gitignore
@@ -10,3 +10,4 @@
 /inotify10
 /inotify11
 /inotify12
+/inotify13
diff --git a/testcases/kernel/syscalls/inotify/inotify13.c b/testcases/kernel/syscalls/inotify/inotify13.c
new file mode 100644
index 000000000..8c2cd4cf1
--- /dev/null
+++ b/testcases/kernel/syscalls/inotify/inotify13.c
@@ -0,0 +1,260 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*\
+ * Verify splice-family functions (and sendfile) generate IN_ACCESS
+ * for what they read and IN_MODIFY for what they write.
+ *
+ * Regression test for 983652c69199 and
+ * https://lore.kernel.org/linux-fsdevel/jbyihkyk5dtaohdwjyivambb2gffyjs3dodpofafnkkunxq7bu@jngkdxx65pux/t/#u
+ */
+
+#define _GNU_SOURCE
+#include "config.h"
+
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <inttypes.h>
+#include <signal.h>
+#include <sys/mman.h>
+#include <sys/sendfile.h>
+
+#include "tst_test.h"
+#include "tst_safe_macros.h"
+#include "inotify.h"
+
+#if defined(HAVE_SYS_INOTIFY_H)
+#include <sys/inotify.h>
+
+static int pipes[2] = { -1, -1 };
+static int inotify = -1;
+static int memfd = -1;
+static int data_pipes[2] = { -1, -1 };
+
+static void watch_rw(int fd)
+{
+	char buf[64];
+	sprintf(buf, "/proc/self/fd/%d", fd);
+	SAFE_MYINOTIFY_ADD_WATCH(inotify, buf, IN_ACCESS | IN_MODIFY);
+}
+
+static int compar(const void *l, const void *r)
+{
+	const struct inotify_event *lie = l;
+	const struct inotify_event *rie = r;
+	return lie->wd - rie->wd;
+}
+
+static void get_events(size_t evcnt, struct inotify_event evs[static evcnt])
+{
+	struct inotify_event tail, *itr = evs;
+	for (size_t left = evcnt; left; --left)
+		SAFE_READ(true, inotify, itr++, sizeof(struct inotify_event));
+
+	TEST(read(inotify, &tail, sizeof(struct inotify_event)));
+	if (TST_RET != -1)
+		tst_brk(TFAIL, ">%zu events", evcnt);
+	if (TST_ERR != EAGAIN)
+		tst_brk(TFAIL | TTERRNO, "expected EAGAIN");
+
+	qsort(evs, evcnt, sizeof(struct inotify_event), compar);
+}
+
+static void expect_event(struct inotify_event *ev, int wd, uint32_t mask)
+{
+	if (ev->wd != wd)
+		tst_brk(TFAIL, "expect event for wd %d got %d", wd, ev->wd);
+	if (ev->mask != mask)
+		tst_brk(TFAIL,
+			"expect event with mask %" PRIu32 " got %" PRIu32 "",
+			mask, ev->mask);
+}
+
+#define F2P(splice)                                                      \
+	SAFE_WRITE(SAFE_WRITE_RETRY, memfd, __func__, sizeof(__func__)); \
+	SAFE_LSEEK(memfd, 0, SEEK_SET);                                  \
+	watch_rw(memfd);                                                 \
+	watch_rw(pipes[0]);                                              \
+	TEST(splice);                                                    \
+	if (TST_RET == -1)                                               \
+		tst_brk(TBROK | TERRNO, #splice);                        \
+	if (TST_RET != sizeof(__func__))                                 \
+		tst_brk(TBROK, #splice ": %" PRId64 "", TST_RET);        \
+                                                                         \
+	/* expecting: IN_ACCESS memfd, IN_MODIFY pipes[0] */             \
+	struct inotify_event events[2];                                  \
+	get_events(ARRAY_SIZE(events), events);                          \
+	expect_event(events + 0, 1, IN_ACCESS);                          \
+	expect_event(events + 1, 2, IN_MODIFY);                          \
+                                                                         \
+	char buf[sizeof(__func__)];                                      \
+	SAFE_READ(true, pipes[0], buf, sizeof(__func__));                \
+	if (memcmp(buf, __func__, sizeof(__func__)))                     \
+		tst_brk(TFAIL, "buf contents bad");
+static void splice_file_to_pipe(void)
+{
+	F2P(splice(memfd, NULL, pipes[1], NULL, 128 * 1024 * 1024, 0));
+}
+static void sendfile_file_to_pipe(void)
+{
+	F2P(sendfile(pipes[1], memfd, NULL, 128 * 1024 * 1024));
+}
+
+static void splice_pipe_to_file(void)
+{
+	SAFE_WRITE(SAFE_WRITE_RETRY, pipes[1], __func__, sizeof(__func__));
+	watch_rw(pipes[0]);
+	watch_rw(memfd);
+	TEST(splice(pipes[0], NULL, memfd, NULL, 128 * 1024 * 1024, 0));
+	if (TST_RET == -1)
+		tst_brk(TBROK | TERRNO, "splice");
+	if (TST_RET != sizeof(__func__))
+		tst_brk(TBROK, "splice: %" PRId64 "", TST_RET);
+
+	/* expecting: IN_ACCESS pipes[0], IN_MODIFY memfd */
+	struct inotify_event events[2];
+	get_events(ARRAY_SIZE(events), events);
+	expect_event(events + 0, 1, IN_ACCESS);
+	expect_event(events + 1, 2, IN_MODIFY);
+
+	char buf[sizeof(__func__)];
+	SAFE_LSEEK(memfd, 0, SEEK_SET);
+	SAFE_READ(true, memfd, buf, sizeof(__func__));
+	if (memcmp(buf, __func__, sizeof(__func__)))
+		tst_brk(TFAIL, "buf contents bad");
+}
+
+#define P2P(splice)                                                  \
+	SAFE_WRITE(SAFE_WRITE_RETRY, data_pipes[1], __func__,        \
+		   sizeof(__func__));                                \
+	watch_rw(data_pipes[0]);                                     \
+	watch_rw(pipes[1]);                                          \
+	TEST(splice);                                                \
+	if (TST_RET == -1)                                           \
+		tst_brk(TBROK | TERRNO, #splice);                    \
+	if (TST_RET != sizeof(__func__))                             \
+		tst_brk(TBROK, #splice ": %" PRId64 "", TST_RET);    \
+                                                                     \
+	/* expecting: IN_ACCESS data_pipes[0], IN_MODIFY pipes[1] */ \
+	struct inotify_event events[2];                              \
+	get_events(ARRAY_SIZE(events), events);                      \
+	expect_event(events + 0, 1, IN_ACCESS);                      \
+	expect_event(events + 1, 2, IN_MODIFY);                      \
+                                                                     \
+	char buf[sizeof(__func__)];                                  \
+	SAFE_READ(true, pipes[0], buf, sizeof(__func__));            \
+	if (memcmp(buf, __func__, sizeof(__func__)))                 \
+		tst_brk(TFAIL, "buf contents bad");
+static void splice_pipe_to_pipe(void)
+{
+	P2P(splice(data_pipes[0], NULL, pipes[1], NULL, 128 * 1024 * 1024, 0));
+}
+static void tee_pipe_to_pipe(void)
+{
+	P2P(tee(data_pipes[0], pipes[1], 128 * 1024 * 1024, 0));
+}
+
+static char vmsplice_pipe_to_mem_dt[32 * 1024];
+static void vmsplice_pipe_to_mem(void)
+{
+	memcpy(vmsplice_pipe_to_mem_dt, __func__, sizeof(__func__));
+	watch_rw(pipes[0]);
+	TEST(vmsplice(
+		pipes[1],
+		&(struct iovec){ .iov_base = vmsplice_pipe_to_mem_dt,
+				 .iov_len = sizeof(vmsplice_pipe_to_mem_dt) },
+		1, SPLICE_F_GIFT));
+	if (TST_RET == -1)
+		tst_brk(TBROK | TERRNO, "vmsplice");
+	if (TST_RET != sizeof(vmsplice_pipe_to_mem_dt))
+		tst_brk(TBROK, "vmsplice: %" PRId64 "", TST_RET);
+
+	/* expecting: IN_MODIFY pipes[0] */
+	struct inotify_event event;
+	get_events(1, &event);
+	expect_event(&event, 1, IN_MODIFY);
+
+	char buf[sizeof(__func__)];
+	SAFE_READ(true, pipes[0], buf, sizeof(__func__));
+	if (memcmp(buf, __func__, sizeof(__func__)))
+		tst_brk(TFAIL, "buf contents bad");
+}
+
+static void vmsplice_mem_to_pipe(void)
+{
+	char buf[sizeof(__func__)];
+	SAFE_WRITE(SAFE_WRITE_RETRY, pipes[1], __func__, sizeof(__func__));
+	watch_rw(pipes[1]);
+	TEST(vmsplice(pipes[0],
+		      &(struct iovec){ .iov_base = buf,
+				       .iov_len = sizeof(buf) },
+		      1, 0));
+	if (TST_RET == -1)
+		tst_brk(TBROK | TERRNO, "vmsplice");
+	if (TST_RET != sizeof(buf))
+		tst_brk(TBROK, "vmsplice: %" PRId64 "", TST_RET);
+
+	/* expecting: IN_ACCESS pipes[1] */
+	struct inotify_event event;
+	get_events(1, &event);
+	expect_event(&event, 1, IN_ACCESS);
+	if (memcmp(buf, __func__, sizeof(__func__)))
+		tst_brk(TFAIL, "buf contents bad");
+}
+
+typedef void (*tests_f)(void);
+#define TEST_F(f)      \
+	{              \
+		f, #f, \
+	}
+static const struct {
+	tests_f f;
+	const char *n;
+} tests[] = {
+	TEST_F(splice_file_to_pipe),  TEST_F(sendfile_file_to_pipe),
+	TEST_F(splice_pipe_to_file),  TEST_F(splice_pipe_to_pipe),
+	TEST_F(tee_pipe_to_pipe),     TEST_F(vmsplice_pipe_to_mem),
+	TEST_F(vmsplice_mem_to_pipe),
+};
+
+static void run_test(unsigned int n)
+{
+	tst_res(TINFO, "%s", tests[n].n);
+
+	SAFE_PIPE2(pipes, O_CLOEXEC);
+	SAFE_PIPE2(data_pipes, O_CLOEXEC);
+	inotify = SAFE_MYINOTIFY_INIT1(IN_NONBLOCK | IN_CLOEXEC);
+	if ((memfd = memfd_create(__func__, MFD_CLOEXEC)) == -1)
+		tst_brk(TCONF | TERRNO, "memfd");
+	tests[n].f();
+	tst_res(TPASS, "ок");
+}
+
+static void cleanup(void)
+{
+	if (memfd != -1)
+		SAFE_CLOSE(memfd);
+	if (inotify != -1)
+		SAFE_CLOSE(inotify);
+	if (pipes[0] != -1)
+		SAFE_CLOSE(pipes[0]);
+	if (pipes[1] != -1)
+		SAFE_CLOSE(pipes[1]);
+	if (data_pipes[0] != -1)
+		SAFE_CLOSE(data_pipes[0]);
+	if (data_pipes[1] != -1)
+		SAFE_CLOSE(data_pipes[1]);
+}
+
+static struct tst_test test = {
+	.max_runtime = 10,
+	.cleanup = cleanup,
+	.test = run_test,
+	.tcnt = ARRAY_SIZE(tests),
+	.tags = (const struct tst_tag[]){ { "linux-git", "983652c69199" }, {} },
+};
+
+#else
+TST_TEST_TCONF("system doesn't have required inotify support");
+#endif
-- 
2.39.2
[LTP RFC PATCH v3] inotify13: new test for fs/splice.c functions vs pipes vs inotify
Posted by Ahelenia Ziemiańska 2 years, 7 months ago
The only one that passes on 6.1.27-1 is sendfile_file_to_pipe.

Link: https://lore.kernel.org/linux-fsdevel/jbyihkyk5dtaohdwjyivambb2gffyjs3dodpofafnkkunxq7bu@jngkdxx65pux/t/#u
Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
---
Sorry, I missed second part of Amir's comments somehow.
cleanup is only run at the end by default:
run it manually to not leak fds between tests.

I've parameterised the tests from the driver, instead of with macros,
and removed the tst_tag data.

Added the * [Description] tag and full commit subject to the header
comment; leaving the lore.k.o link for now, to be turned into a SHA
when the kernel behaviour this tests starts having a SHA.

Error checking has been lifted out as well.
Formatted in kernel style accd'g to clang-format and check-inotify13.

I used the wrong address for ltp@ the first time; I've since bounced the
patchset, and am sending this, to the correct address. They were all
held for moderation for now.

 testcases/kernel/syscalls/inotify/.gitignore  |   1 +
 testcases/kernel/syscalls/inotify/inotify13.c | 282 ++++++++++++++++++
 2 files changed, 283 insertions(+)
 create mode 100644 testcases/kernel/syscalls/inotify/inotify13.c

diff --git a/testcases/kernel/syscalls/inotify/.gitignore b/testcases/kernel/syscalls/inotify/.gitignore
index f6e5c546a..b597ea63f 100644
--- a/testcases/kernel/syscalls/inotify/.gitignore
+++ b/testcases/kernel/syscalls/inotify/.gitignore
@@ -10,3 +10,4 @@
 /inotify10
 /inotify11
 /inotify12
+/inotify13
diff --git a/testcases/kernel/syscalls/inotify/inotify13.c b/testcases/kernel/syscalls/inotify/inotify13.c
new file mode 100644
index 000000000..97f88053e
--- /dev/null
+++ b/testcases/kernel/syscalls/inotify/inotify13.c
@@ -0,0 +1,282 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*\
+ * [Description]
+ * Verify splice-family functions (and sendfile) generate IN_ACCESS
+ * for what they read and IN_MODIFY for what they write.
+ *
+ * Regression test for 983652c69199 ("splice: report related fsnotify events") and
+ * https://lore.kernel.org/linux-fsdevel/jbyihkyk5dtaohdwjyivambb2gffyjs3dodpofafnkkunxq7bu@jngkdxx65pux/t/#u
+ */
+
+#define _GNU_SOURCE
+#include "config.h"
+
+#include <stdio.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <fcntl.h>
+#include <stdbool.h>
+#include <inttypes.h>
+#include <signal.h>
+#include <sys/mman.h>
+#include <sys/sendfile.h>
+
+#include "tst_test.h"
+#include "tst_safe_macros.h"
+#include "inotify.h"
+
+#if defined(HAVE_SYS_INOTIFY_H)
+#include <sys/inotify.h>
+
+static int pipes[2] = { -1, -1 };
+static int inotify = -1;
+static int memfd = -1;
+static int data_pipes[2] = { -1, -1 };
+
+static void watch_rw(int fd)
+{
+	char buf[64];
+
+	sprintf(buf, "/proc/self/fd/%d", fd);
+	SAFE_MYINOTIFY_ADD_WATCH(inotify, buf, IN_ACCESS | IN_MODIFY);
+}
+
+static int compar(const void *l, const void *r)
+{
+	const struct inotify_event *lie = l;
+	const struct inotify_event *rie = r;
+
+	return lie->wd - rie->wd;
+}
+
+static void get_events(size_t evcnt, struct inotify_event evs[static evcnt])
+{
+	struct inotify_event tail, *itr = evs;
+
+	for (size_t left = evcnt; left; --left)
+		SAFE_READ(true, inotify, itr++, sizeof(struct inotify_event));
+
+	TEST(read(inotify, &tail, sizeof(struct inotify_event)));
+	if (TST_RET != -1)
+		tst_brk(TFAIL, ">%zu events", evcnt);
+	if (TST_ERR != EAGAIN)
+		tst_brk(TFAIL | TTERRNO, "expected EAGAIN");
+
+	qsort(evs, evcnt, sizeof(struct inotify_event), compar);
+}
+
+static void expect_transfer(const char *name, size_t size)
+{
+	if (TST_RET == -1)
+		tst_brk(TBROK | TERRNO, "%s", name);
+	if ((size_t)TST_RET != size)
+		tst_brk(TBROK, "%s: %ld != %zu", name, TST_RET, size);
+}
+
+static void expect_event(struct inotify_event *ev, int wd, uint32_t mask)
+{
+	if (ev->wd != wd)
+		tst_brk(TFAIL, "expect event for wd %d got %d", wd, ev->wd);
+	if (ev->mask != mask)
+		tst_brk(TFAIL,
+			"expect event with mask %" PRIu32 " got %" PRIu32 "",
+			mask, ev->mask);
+}
+
+// write to file, rewind, transfer accd'g to f2p, read from pipe
+// expecting: IN_ACCESS memfd, IN_MODIFY pipes[0]
+static void file_to_pipe(const char *name, ssize_t (*f2p)(void))
+{
+	struct inotify_event events[2];
+	char buf[strlen(name)];
+
+	SAFE_WRITE(SAFE_WRITE_RETRY, memfd, name, strlen(name));
+	SAFE_LSEEK(memfd, 0, SEEK_SET);
+	watch_rw(memfd);
+	watch_rw(pipes[0]);
+	TEST(f2p());
+	expect_transfer(name, strlen(name));
+
+	get_events(ARRAY_SIZE(events), events);
+	expect_event(events + 0, 1, IN_ACCESS);
+	expect_event(events + 1, 2, IN_MODIFY);
+
+	SAFE_READ(true, pipes[0], buf, strlen(name));
+	if (memcmp(buf, name, strlen(name)))
+		tst_brk(TFAIL, "buf contents bad");
+}
+static ssize_t splice_file_to_pipe(void)
+{
+	return splice(memfd, NULL, pipes[1], NULL, 128 * 1024 * 1024, 0);
+}
+static ssize_t sendfile_file_to_pipe(void)
+{
+	return sendfile(pipes[1], memfd, NULL, 128 * 1024 * 1024);
+}
+
+// write to pipe, transfer with splice, rewind file, read from file
+// expecting: IN_ACCESS pipes[0], IN_MODIFY memfd
+static void splice_pipe_to_file(const char *name, ssize_t (*param)(void))
+{
+	(void)name;
+	(void)param;
+	struct inotify_event events[2];
+	char buf[sizeof(__func__)];
+
+	SAFE_WRITE(SAFE_WRITE_RETRY, pipes[1], __func__, sizeof(__func__));
+	watch_rw(pipes[0]);
+	watch_rw(memfd);
+	TEST(splice(pipes[0], NULL, memfd, NULL, 128 * 1024 * 1024, 0));
+	expect_transfer(__func__, sizeof(__func__));
+
+	get_events(ARRAY_SIZE(events), events);
+	expect_event(events + 0, 1, IN_ACCESS);
+	expect_event(events + 1, 2, IN_MODIFY);
+
+	SAFE_LSEEK(memfd, 0, SEEK_SET);
+	SAFE_READ(true, memfd, buf, sizeof(__func__));
+	if (memcmp(buf, __func__, sizeof(__func__)))
+		tst_brk(TFAIL, "buf contents bad");
+}
+
+// write to data_pipe, transfer accd'g to p2p, read from pipe
+// expecting: IN_ACCESS data_pipes[0], IN_MODIFY pipes[1]
+static void pipe_to_pipe(const char *name, ssize_t (*p2p)(void))
+{
+	struct inotify_event events[2];
+	char buf[strlen(name)];
+
+	SAFE_WRITE(SAFE_WRITE_RETRY, data_pipes[1], name, strlen(name));
+	watch_rw(data_pipes[0]);
+	watch_rw(pipes[1]);
+	TEST(p2p());
+	expect_transfer(name, strlen(name));
+
+	get_events(ARRAY_SIZE(events), events);
+	expect_event(events + 0, 1, IN_ACCESS);
+	expect_event(events + 1, 2, IN_MODIFY);
+
+	SAFE_READ(true, pipes[0], buf, strlen(name));
+	if (memcmp(buf, name, strlen(name)))
+		tst_brk(TFAIL, "buf contents bad");
+}
+static ssize_t splice_pipe_to_pipe(void)
+{
+	return splice(data_pipes[0], NULL, pipes[1], NULL, 128 * 1024 * 1024,
+		      0);
+}
+static ssize_t tee_pipe_to_pipe(void)
+{
+	return tee(data_pipes[0], pipes[1], 128 * 1024 * 1024, 0);
+}
+
+// vmsplice to pipe, read from pipe
+// expecting: IN_MODIFY pipes[0]
+static char vmsplice_pipe_to_mem_dt[32 * 1024];
+static void vmsplice_pipe_to_mem(const char *name, ssize_t (*param)(void))
+{
+	(void)name;
+	(void)param;
+	struct inotify_event event;
+	char buf[sizeof(__func__)];
+
+	memcpy(vmsplice_pipe_to_mem_dt, __func__, sizeof(__func__));
+	watch_rw(pipes[0]);
+	TEST(vmsplice(
+		pipes[1],
+		&(struct iovec){ .iov_base = vmsplice_pipe_to_mem_dt,
+				 .iov_len = sizeof(vmsplice_pipe_to_mem_dt) },
+		1, SPLICE_F_GIFT));
+	expect_transfer(__func__, sizeof(vmsplice_pipe_to_mem_dt));
+
+	get_events(1, &event);
+	expect_event(&event, 1, IN_MODIFY);
+
+	SAFE_READ(true, pipes[0], buf, sizeof(__func__));
+	if (memcmp(buf, __func__, sizeof(__func__)))
+		tst_brk(TFAIL, "buf contents bad");
+}
+
+// write to pipe, vmsplice from pipe
+// expecting: IN_ACCESS pipes[1]
+static void vmsplice_mem_to_pipe(const char *name, ssize_t (*param)(void))
+{
+	(void)name;
+	(void)param;
+	char buf[sizeof(__func__)];
+	struct inotify_event event;
+
+	SAFE_WRITE(SAFE_WRITE_RETRY, pipes[1], __func__, sizeof(__func__));
+	watch_rw(pipes[1]);
+	TEST(vmsplice(pipes[0],
+		      &(struct iovec){ .iov_base = buf,
+				       .iov_len = sizeof(buf) },
+		      1, 0));
+	expect_transfer(__func__, sizeof(buf));
+
+	get_events(1, &event);
+	expect_event(&event, 1, IN_ACCESS);
+
+	if (memcmp(buf, __func__, sizeof(__func__)))
+		tst_brk(TFAIL, "buf contents bad");
+}
+
+#define TEST_F(f, param)      \
+	{                     \
+		#f, f, param, \
+	}
+static const struct {
+	const char *n;
+	void (*f)(const char *name, ssize_t (*param)(void));
+	ssize_t (*param)(void);
+} tests[] = {
+	TEST_F(file_to_pipe, splice_file_to_pipe),
+	TEST_F(file_to_pipe, sendfile_file_to_pipe),
+	TEST_F(splice_pipe_to_file, NULL),
+	TEST_F(pipe_to_pipe, splice_pipe_to_pipe),
+	TEST_F(pipe_to_pipe, tee_pipe_to_pipe),
+	TEST_F(vmsplice_pipe_to_mem, NULL),
+	TEST_F(vmsplice_mem_to_pipe, NULL),
+};
+
+static void cleanup(void)
+{
+	if (memfd != -1)
+		SAFE_CLOSE(memfd);
+	if (inotify != -1)
+		SAFE_CLOSE(inotify);
+	if (pipes[0] != -1)
+		SAFE_CLOSE(pipes[0]);
+	if (pipes[1] != -1)
+		SAFE_CLOSE(pipes[1]);
+	if (data_pipes[0] != -1)
+		SAFE_CLOSE(data_pipes[0]);
+	if (data_pipes[1] != -1)
+		SAFE_CLOSE(data_pipes[1]);
+}
+
+static void run_test(unsigned int n)
+{
+	tst_res(TINFO, "%s", tests[n].n);
+
+	SAFE_PIPE2(pipes, O_CLOEXEC);
+	SAFE_PIPE2(data_pipes, O_CLOEXEC);
+	inotify = SAFE_MYINOTIFY_INIT1(IN_NONBLOCK | IN_CLOEXEC);
+	memfd = memfd_create(__func__, MFD_CLOEXEC);
+	if (memfd == -1)
+		tst_brk(TCONF | TERRNO, "memfd");
+	tests[n].f(tests[n].n, tests[n].param);
+	tst_res(TPASS, "ок");
+	cleanup();
+}
+
+static struct tst_test test = {
+	.cleanup = cleanup,
+	.test = run_test,
+	.tcnt = ARRAY_SIZE(tests),
+	.tags = (const struct tst_tag[]){ {} },
+};
+
+#else
+TST_TEST_TCONF("system doesn't have required inotify support");
+#endif
-- 
2.39.2
Re: [LTP RFC PATCH v3] inotify13: new test for fs/splice.c functions vs pipes vs inotify
Posted by Amir Goldstein 2 years, 7 months ago
On Wed, Jun 28, 2023 at 3:21 AM Ahelenia Ziemiańska
<nabijaczleweli@nabijaczleweli.xyz> wrote:
>
> The only one that passes on 6.1.27-1 is sendfile_file_to_pipe.
>
> Link: https://lore.kernel.org/linux-fsdevel/jbyihkyk5dtaohdwjyivambb2gffyjs3dodpofafnkkunxq7bu@jngkdxx65pux/t/#u
> Signed-off-by: Ahelenia Ziemiańska <nabijaczleweli@nabijaczleweli.xyz>
> ---
> Sorry, I missed second part of Amir's comments somehow.
> cleanup is only run at the end by default:
> run it manually to not leak fds between tests.
>
> I've parameterised the tests from the driver, instead of with macros,
> and removed the tst_tag data.
>
> Added the * [Description] tag and full commit subject to the header
> comment; leaving the lore.k.o link for now, to be turned into a SHA
> when the kernel behaviour this tests starts having a SHA.
>
> Error checking has been lifted out as well.
> Formatted in kernel style accd'g to clang-format and check-inotify13.
>
> I used the wrong address for ltp@ the first time; I've since bounced the
> patchset, and am sending this, to the correct address. They were all
> held for moderation for now.
>
>  testcases/kernel/syscalls/inotify/.gitignore  |   1 +
>  testcases/kernel/syscalls/inotify/inotify13.c | 282 ++++++++++++++++++
>  2 files changed, 283 insertions(+)
>  create mode 100644 testcases/kernel/syscalls/inotify/inotify13.c
>
> diff --git a/testcases/kernel/syscalls/inotify/.gitignore b/testcases/kernel/syscalls/inotify/.gitignore
> index f6e5c546a..b597ea63f 100644
> --- a/testcases/kernel/syscalls/inotify/.gitignore
> +++ b/testcases/kernel/syscalls/inotify/.gitignore
> @@ -10,3 +10,4 @@
>  /inotify10
>  /inotify11
>  /inotify12
> +/inotify13
> diff --git a/testcases/kernel/syscalls/inotify/inotify13.c b/testcases/kernel/syscalls/inotify/inotify13.c
> new file mode 100644
> index 000000000..97f88053e
> --- /dev/null
> +++ b/testcases/kernel/syscalls/inotify/inotify13.c
> @@ -0,0 +1,282 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*\
> + * [Description]
> + * Verify splice-family functions (and sendfile) generate IN_ACCESS
> + * for what they read and IN_MODIFY for what they write.
> + *
> + * Regression test for 983652c69199 ("splice: report related fsnotify events") and
> + * https://lore.kernel.org/linux-fsdevel/jbyihkyk5dtaohdwjyivambb2gffyjs3dodpofafnkkunxq7bu@jngkdxx65pux/t/#u

The process of posting a test for the fix that was not yet merged
is indeed a chicken and egg situation.

What I usually do is post a draft test (like this) and link
to the post of the LTP test (and maybe a branch on github)
when posting the fix, to say how I tested the fix.

I would then put it in my TODO to re-post the LTP
test once the kernel fix has been merged.

> + */
> +
> +#define _GNU_SOURCE
> +#include "config.h"
> +
> +#include <stdio.h>
> +#include <unistd.h>
> +#include <stdlib.h>
> +#include <fcntl.h>
> +#include <stdbool.h>
> +#include <inttypes.h>
> +#include <signal.h>
> +#include <sys/mman.h>
> +#include <sys/sendfile.h>
> +
> +#include "tst_test.h"
> +#include "tst_safe_macros.h"
> +#include "inotify.h"
> +
> +#if defined(HAVE_SYS_INOTIFY_H)
> +#include <sys/inotify.h>
> +
> +static int pipes[2] = { -1, -1 };
> +static int inotify = -1;
> +static int memfd = -1;
> +static int data_pipes[2] = { -1, -1 };
> +
> +static void watch_rw(int fd)
> +{
> +       char buf[64];
> +
> +       sprintf(buf, "/proc/self/fd/%d", fd);
> +       SAFE_MYINOTIFY_ADD_WATCH(inotify, buf, IN_ACCESS | IN_MODIFY);
> +}
> +
> +static int compar(const void *l, const void *r)
> +{
> +       const struct inotify_event *lie = l;
> +       const struct inotify_event *rie = r;
> +
> +       return lie->wd - rie->wd;
> +}
> +
> +static void get_events(size_t evcnt, struct inotify_event evs[static evcnt])
> +{
> +       struct inotify_event tail, *itr = evs;
> +
> +       for (size_t left = evcnt; left; --left)
> +               SAFE_READ(true, inotify, itr++, sizeof(struct inotify_event));
> +
> +       TEST(read(inotify, &tail, sizeof(struct inotify_event)));
> +       if (TST_RET != -1)
> +               tst_brk(TFAIL, ">%zu events", evcnt);
> +       if (TST_ERR != EAGAIN)
> +               tst_brk(TFAIL | TTERRNO, "expected EAGAIN");
> +
> +       qsort(evs, evcnt, sizeof(struct inotify_event), compar);
> +}
> +
> +static void expect_transfer(const char *name, size_t size)
> +{
> +       if (TST_RET == -1)
> +               tst_brk(TBROK | TERRNO, "%s", name);
> +       if ((size_t)TST_RET != size)
> +               tst_brk(TBROK, "%s: %ld != %zu", name, TST_RET, size);
> +}
> +
> +static void expect_event(struct inotify_event *ev, int wd, uint32_t mask)
> +{
> +       if (ev->wd != wd)
> +               tst_brk(TFAIL, "expect event for wd %d got %d", wd, ev->wd);
> +       if (ev->mask != mask)
> +               tst_brk(TFAIL,
> +                       "expect event with mask %" PRIu32 " got %" PRIu32 "",
> +                       mask, ev->mask);
> +}
> +
> +// write to file, rewind, transfer accd'g to f2p, read from pipe
> +// expecting: IN_ACCESS memfd, IN_MODIFY pipes[0]
> +static void file_to_pipe(const char *name, ssize_t (*f2p)(void))
> +{
> +       struct inotify_event events[2];
> +       char buf[strlen(name)];
> +
> +       SAFE_WRITE(SAFE_WRITE_RETRY, memfd, name, strlen(name));
> +       SAFE_LSEEK(memfd, 0, SEEK_SET);
> +       watch_rw(memfd);
> +       watch_rw(pipes[0]);
> +       TEST(f2p());
> +       expect_transfer(name, strlen(name));
> +
> +       get_events(ARRAY_SIZE(events), events);
> +       expect_event(events + 0, 1, IN_ACCESS);
> +       expect_event(events + 1, 2, IN_MODIFY);

So what I meant to say is that if there are double events that
usually get merged (unless reader was fast enough to read the
first event), this is something that I could live with, but encoding
an expectation for a double event, that's not at all what I meant.

But anyway, I see that you've found a way to work around
this problem, so at least the test can expect and get a single event.

I think you are missing expect_no_more_events() here to
verify that you won't get double events.

See test inotify12 as an example for a test that encodes
expect_events per test case and also verifies there are no
unexpected extra events.

That's also an example of a more generic test template,
but your test cases are all a bit different from each other is
subtle ways, so I trust you will find the best balance between
putting generic parameterized code in the run_test() template
and putting code in the test case subroutine.

> +
> +       SAFE_READ(true, pipes[0], buf, strlen(name));
> +       if (memcmp(buf, name, strlen(name)))
> +               tst_brk(TFAIL, "buf contents bad");
> +}
> +static ssize_t splice_file_to_pipe(void)
> +{
> +       return splice(memfd, NULL, pipes[1], NULL, 128 * 1024 * 1024, 0);
> +}
> +static ssize_t sendfile_file_to_pipe(void)
> +{
> +       return sendfile(pipes[1], memfd, NULL, 128 * 1024 * 1024);
> +}
> +
> +// write to pipe, transfer with splice, rewind file, read from file
> +// expecting: IN_ACCESS pipes[0], IN_MODIFY memfd
> +static void splice_pipe_to_file(const char *name, ssize_t (*param)(void))
> +{
> +       (void)name;
> +       (void)param;
> +       struct inotify_event events[2];
> +       char buf[sizeof(__func__)];
> +
> +       SAFE_WRITE(SAFE_WRITE_RETRY, pipes[1], __func__, sizeof(__func__));
> +       watch_rw(pipes[0]);
> +       watch_rw(memfd);
> +       TEST(splice(pipes[0], NULL, memfd, NULL, 128 * 1024 * 1024, 0));
> +       expect_transfer(__func__, sizeof(__func__));
> +
> +       get_events(ARRAY_SIZE(events), events);
> +       expect_event(events + 0, 1, IN_ACCESS);
> +       expect_event(events + 1, 2, IN_MODIFY);
> +
> +       SAFE_LSEEK(memfd, 0, SEEK_SET);
> +       SAFE_READ(true, memfd, buf, sizeof(__func__));
> +       if (memcmp(buf, __func__, sizeof(__func__)))
> +               tst_brk(TFAIL, "buf contents bad");
> +}
> +
> +// write to data_pipe, transfer accd'g to p2p, read from pipe
> +// expecting: IN_ACCESS data_pipes[0], IN_MODIFY pipes[1]
> +static void pipe_to_pipe(const char *name, ssize_t (*p2p)(void))
> +{
> +       struct inotify_event events[2];
> +       char buf[strlen(name)];
> +
> +       SAFE_WRITE(SAFE_WRITE_RETRY, data_pipes[1], name, strlen(name));
> +       watch_rw(data_pipes[0]);
> +       watch_rw(pipes[1]);
> +       TEST(p2p());
> +       expect_transfer(name, strlen(name));
> +
> +       get_events(ARRAY_SIZE(events), events);
> +       expect_event(events + 0, 1, IN_ACCESS);
> +       expect_event(events + 1, 2, IN_MODIFY);
> +
> +       SAFE_READ(true, pipes[0], buf, strlen(name));
> +       if (memcmp(buf, name, strlen(name)))
> +               tst_brk(TFAIL, "buf contents bad");
> +}
> +static ssize_t splice_pipe_to_pipe(void)
> +{
> +       return splice(data_pipes[0], NULL, pipes[1], NULL, 128 * 1024 * 1024,
> +                     0);
> +}
> +static ssize_t tee_pipe_to_pipe(void)
> +{
> +       return tee(data_pipes[0], pipes[1], 128 * 1024 * 1024, 0);
> +}
> +
> +// vmsplice to pipe, read from pipe
> +// expecting: IN_MODIFY pipes[0]
> +static char vmsplice_pipe_to_mem_dt[32 * 1024];
> +static void vmsplice_pipe_to_mem(const char *name, ssize_t (*param)(void))
> +{
> +       (void)name;
> +       (void)param;
> +       struct inotify_event event;
> +       char buf[sizeof(__func__)];
> +
> +       memcpy(vmsplice_pipe_to_mem_dt, __func__, sizeof(__func__));
> +       watch_rw(pipes[0]);
> +       TEST(vmsplice(
> +               pipes[1],
> +               &(struct iovec){ .iov_base = vmsplice_pipe_to_mem_dt,
> +                                .iov_len = sizeof(vmsplice_pipe_to_mem_dt) },
> +               1, SPLICE_F_GIFT));
> +       expect_transfer(__func__, sizeof(vmsplice_pipe_to_mem_dt));
> +
> +       get_events(1, &event);
> +       expect_event(&event, 1, IN_MODIFY);
> +
> +       SAFE_READ(true, pipes[0], buf, sizeof(__func__));
> +       if (memcmp(buf, __func__, sizeof(__func__)))
> +               tst_brk(TFAIL, "buf contents bad");
> +}
> +
> +// write to pipe, vmsplice from pipe
> +// expecting: IN_ACCESS pipes[1]
> +static void vmsplice_mem_to_pipe(const char *name, ssize_t (*param)(void))
> +{
> +       (void)name;
> +       (void)param;
> +       char buf[sizeof(__func__)];
> +       struct inotify_event event;
> +
> +       SAFE_WRITE(SAFE_WRITE_RETRY, pipes[1], __func__, sizeof(__func__));
> +       watch_rw(pipes[1]);
> +       TEST(vmsplice(pipes[0],
> +                     &(struct iovec){ .iov_base = buf,
> +                                      .iov_len = sizeof(buf) },
> +                     1, 0));
> +       expect_transfer(__func__, sizeof(buf));
> +
> +       get_events(1, &event);
> +       expect_event(&event, 1, IN_ACCESS);
> +
> +       if (memcmp(buf, __func__, sizeof(__func__)))
> +               tst_brk(TFAIL, "buf contents bad");
> +}
> +
> +#define TEST_F(f, param)      \
> +       {                     \
> +               #f, f, param, \
> +       }
> +static const struct {
> +       const char *n;
> +       void (*f)(const char *name, ssize_t (*param)(void));
> +       ssize_t (*param)(void);
> +} tests[] = {
> +       TEST_F(file_to_pipe, splice_file_to_pipe),
> +       TEST_F(file_to_pipe, sendfile_file_to_pipe),
> +       TEST_F(splice_pipe_to_file, NULL),
> +       TEST_F(pipe_to_pipe, splice_pipe_to_pipe),
> +       TEST_F(pipe_to_pipe, tee_pipe_to_pipe),
> +       TEST_F(vmsplice_pipe_to_mem, NULL),
> +       TEST_F(vmsplice_mem_to_pipe, NULL),
> +};
> +
> +static void cleanup(void)
> +{
> +       if (memfd != -1)
> +               SAFE_CLOSE(memfd);
> +       if (inotify != -1)
> +               SAFE_CLOSE(inotify);
> +       if (pipes[0] != -1)
> +               SAFE_CLOSE(pipes[0]);
> +       if (pipes[1] != -1)
> +               SAFE_CLOSE(pipes[1]);
> +       if (data_pipes[0] != -1)
> +               SAFE_CLOSE(data_pipes[0]);
> +       if (data_pipes[1] != -1)
> +               SAFE_CLOSE(data_pipes[1]);
> +}
> +
> +static void run_test(unsigned int n)
> +{
> +       tst_res(TINFO, "%s", tests[n].n);
> +
> +       SAFE_PIPE2(pipes, O_CLOEXEC);
> +       SAFE_PIPE2(data_pipes, O_CLOEXEC);
> +       inotify = SAFE_MYINOTIFY_INIT1(IN_NONBLOCK | IN_CLOEXEC);
> +       memfd = memfd_create(__func__, MFD_CLOEXEC);
> +       if (memfd == -1)
> +               tst_brk(TCONF | TERRNO, "memfd");
> +       tests[n].f(tests[n].n, tests[n].param);
> +       tst_res(TPASS, "ок");
> +       cleanup();
> +}
> +
> +static struct tst_test test = {
> +       .cleanup = cleanup,
> +       .test = run_test,
> +       .tcnt = ARRAY_SIZE(tests),
> +       .tags = (const struct tst_tag[]){ {} },

I don't think this is needed for the draft...

Thanks,
Amir.
Re: [LTP RFC PATCH v3] inotify13: new test for fs/splice.c functions vs pipes vs inotify
Posted by Ahelenia Ziemiańska 2 years, 7 months ago
On Wed, Jun 28, 2023 at 08:30:15AM +0300, Amir Goldstein wrote:
> On Wed, Jun 28, 2023 at 3:21 AM Ahelenia Ziemiańska
> > diff --git a/testcases/kernel/syscalls/inotify/inotify13.c b/testcases/kernel/syscalls/inotify/inotify13.c
> > new file mode 100644
> > index 000000000..97f88053e
> > --- /dev/null
> > +++ b/testcases/kernel/syscalls/inotify/inotify13.c
> > @@ -0,0 +1,282 @@
> > +// SPDX-License-Identifier: GPL-2.0-or-later
> > +/*\
> > + * [Description]
> > + * Verify splice-family functions (and sendfile) generate IN_ACCESS
> > + * for what they read and IN_MODIFY for what they write.
> > + *
> > + * Regression test for 983652c69199 ("splice: report related fsnotify events") and
> > + * https://lore.kernel.org/linux-fsdevel/jbyihkyk5dtaohdwjyivambb2gffyjs3dodpofafnkkunxq7bu@jngkdxx65pux/t/#u
> The process of posting a test for the fix that was not yet merged
> is indeed a chicken and egg situation.
> 
> What I usually do is post a draft test (like this) and link
> to the post of the LTP test (and maybe a branch on github)
> when posting the fix, to say how I tested the fix.
https://git.sr.ht/~nabijaczleweli/ltp/commit/v4 for now.

> I would then put it in my TODO to re-post the LTP
> test once the kernel fix has been merged.
Yep.

> > +static int compar(const void *l, const void *r)
> > +{
> > +       const struct inotify_event *lie = l;
> > +       const struct inotify_event *rie = r;
> > +
> > +       return lie->wd - rie->wd;
> > +}
> > +
> > +static void get_events(size_t evcnt, struct inotify_event evs[static evcnt])
> > +{
> > +       struct inotify_event tail, *itr = evs;
> > +
> > +       for (size_t left = evcnt; left; --left)
> > +               SAFE_READ(true, inotify, itr++, sizeof(struct inotify_event));
> > +
> > +       TEST(read(inotify, &tail, sizeof(struct inotify_event)));
> > +       if (TST_RET != -1)
> > +               tst_brk(TFAIL, ">%zu events", evcnt);
> > +       if (TST_ERR != EAGAIN)
> > +               tst_brk(TFAIL | TTERRNO, "expected EAGAIN");
> > +
> > +       qsort(evs, evcnt, sizeof(struct inotify_event), compar);
> > +}
> > +
> > +static void expect_transfer(const char *name, size_t size)
> > +{
> > +       if (TST_RET == -1)
> > +               tst_brk(TBROK | TERRNO, "%s", name);
> > +       if ((size_t)TST_RET != size)
> > +               tst_brk(TBROK, "%s: %ld != %zu", name, TST_RET, size);
> > +}
> > +
> > +static void expect_event(struct inotify_event *ev, int wd, uint32_t mask)
> > +{
> > +       if (ev->wd != wd)
> > +               tst_brk(TFAIL, "expect event for wd %d got %d", wd, ev->wd);
> > +       if (ev->mask != mask)
> > +               tst_brk(TFAIL,
> > +                       "expect event with mask %" PRIu32 " got %" PRIu32 "",
> > +                       mask, ev->mask);
> > +}
> > +
> > +// write to file, rewind, transfer accd'g to f2p, read from pipe
> > +// expecting: IN_ACCESS memfd, IN_MODIFY pipes[0]
> > +static void file_to_pipe(const char *name, ssize_t (*f2p)(void))
> > +{
> > +       struct inotify_event events[2];
> > +       char buf[strlen(name)];
> > +
> > +       SAFE_WRITE(SAFE_WRITE_RETRY, memfd, name, strlen(name));
> > +       SAFE_LSEEK(memfd, 0, SEEK_SET);
> > +       watch_rw(memfd);
> > +       watch_rw(pipes[0]);
> > +       TEST(f2p());
> > +       expect_transfer(name, strlen(name));
> > +
> > +       get_events(ARRAY_SIZE(events), events);
> > +       expect_event(events + 0, 1, IN_ACCESS);
> > +       expect_event(events + 1, 2, IN_MODIFY);
> So what I meant to say is that if there are double events that
> usually get merged (unless reader was fast enough to read the
> first event), this is something that I could live with, but encoding
> an expectation for a double event, that's not at all what I meant.
> 
> But anyway, I see that you've found a way to work around
> this problem, so at least the test can expect and get a single event.
I've tried (admittedly, not all that hard) to read a double out modify
event in this case with the v4 kernel patchset and haven't managed it.

> I think you are missing expect_no_more_events() here to
> verify that you won't get double events.
get_events() reads precisely N events, then tries to read another,
and fails if that succeeds.

Maybe a better name would be "get_events_exact()".

> See test inotify12 as an example for a test that encodes
> expect_events per test case and also verifies there are no
> unexpected extra events.
> 
> That's also an example of a more generic test template,
> but your test cases are all a bit different from each other is
> subtle ways, so I trust you will find the best balance between
> putting generic parameterized code in the run_test() template
> and putting code in the test case subroutine.
Yes, that's indeed an optics issue: it looks like there's more, but
the only actually "common" bit of the test drivers is that they all
read events in the middle: the set-up before is different, and the
additional post-conditions are different.

We /could/ encode the expected events in the test array, but then
that would put the expected events away from the code that generates
them, which is more code, and more confusing for no good reason I
think.