From: Amir Goldstein <amir73il@gmail.com>
To: "Ahelenia Ziemiańska" <nabijaczleweli@nabijaczleweli.xyz>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>,
Christian Brauner <brauner@kernel.org>,
linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
Jan Kara <jack@suse.cz>
Subject: Re: splice(-> FIFO) never wakes up inotify IN_MODIFY?
Date: Mon, 26 Jun 2023 09:11:53 +0300 [thread overview]
Message-ID: <CAOQ4uxhut2NHc+MY-XOJay5B-OKXU2X5Fe0-6-RCMKt584ft5A@mail.gmail.com> (raw)
In-Reply-To: <jbyihkyk5dtaohdwjyivambb2gffyjs3dodpofafnkkunxq7bu@jngkdxx65pux>
On Mon, Jun 26, 2023 at 6:54 AM Ahelenia Ziemiańska
<nabijaczleweli@nabijaczleweli.xyz> wrote:
>
> Hi!
>
> Consider the following programs:
> -- >8 --
> ==> ino.c <==
> #define _GNU_SOURCE
> #include <stdio.h>
> #include <sys/inotify.h>
> #include <unistd.h>
> int main() {
> int ino = inotify_init1(IN_CLOEXEC);
> inotify_add_watch(ino, "/dev/fd/0", IN_MODIFY);
>
> char buf[64 * 1024];
> struct inotify_event ev;
> while (read(ino, &ev, sizeof(ev)) > 0) {
> fprintf(stderr, "%d: mask=%x, cook=%x, len=%x, name=%.*s\n", ev.wd, ev.mask,
> ev.cookie, ev.len, (int)ev.len, ev.name);
> fprintf(stderr, "rd=%zd\n", read(0, buf, sizeof(buf)));
> }
> }
>
That's a very odd (and wrong) way to implement poll(2).
This is not a documented way to use pipes, so it may
happen to work with sendfile(2), but there is no guarantee.
> ==> se.c <==
> #define _GNU_SOURCE
> #include <stdio.h>
> #include <sys/sendfile.h>
> int main() {
> ssize_t rd, acc = 0;
> while ((rd = sendfile(1, 0, 0, 128 * 1024 * 1024)) > 0)
> acc += rd;
> fprintf(stderr, "se=%zd: %m\n", acc);
> }
>
> ==> sp.c <==
> #define _GNU_SOURCE
> #include <fcntl.h>
> #include <stdio.h>
> int main() {
> ssize_t rd, acc = 0;
> while ((rd = splice(0, 0, 1, 0, 128 * 1024 * 1024, 0)) > 0)
> acc += rd;
> fprintf(stderr, "sp=%zd: %m\n", acc);
> }
> -- >8 --
>
> By all means, ./sp | ./ino and ./se | ./ino should be equivalent,
> right?
>
Maybe it should, but it's not.
> -- >8 --
> $ make se sp ino
> $ mkfifo fifo
> $ ./ino < fifo &
> [1] 230
> $ echo a > fifo
> $ echo a > fifo
> 1: mask=2, cook=0, len=0, name=
> rd=4
> $ echo c > fifo
> 1: mask=2, cook=0, len=0, name=
> rd=2
> $ ./se > fifo
> abcdef
> 1: mask=2, cook=0, len=0, name=
> asd
> ^D
> se=11: Success
> rd=11
> 1: mask=2, cook=0, len=0, name=
> rd=0
> $ ./sp > fifo
> abcdefg
> asd
> dsasdadadad
> sp=24: Success
> $ < sp ./sp > fifo
> sp=25856: Success
> $ < sp ./sp > fifo
> ^C
> $ echo sp > fifo
> ^C
> -- >8 --
>
> Note how in all ./sp > fifo cases, ./ino doesn't wake up!
> Note also how, thus, we've managed to fill the pipe buffer with ./sp
> (when it transferred 25856), and now we can't /ever/ write there again
> (both splicing and normal writes block, since there's no space left in
> the pipe; ./ino hasn't seen this and will never wake up or service the
> pipe):
> so we've effectively "denied service" by slickily using a different
> syscall to do the write, right?
>
Only applications that do not check for availability
of input in the pipe correctly will get "denied service".
> I consider this to be unexpected behaviour because (a) obviously and
> (b) sendfile() sends the inotify event.
>
The fact is that relying on inotify IN_MODIFY and IN_ACCESS events
for pipes is not a good idea.
splice(2) differentiates three different cases:
if (ipipe && opipe) {
...
if (ipipe) {
...
if (opipe) {
...
IN_ACCESS will only be generated for non-pipe input
IN_MODIFY will only be generated for non-pipe output
Similarly FAN_ACCESS_PERM fanotify permission events
will only be generated for non-pipe input.
sendfile(2) OTOH does not special cases the pipe input
case at all and it generates IN_MODIFY for the pipe output
case as well.
If you would insist on fixing this inconsistency, I would be
willing to consider a patch that matches sendfile(2) behavior
to that of splice(2) and not the other way around.
My general opinion about IN_ACCESS/IN_MODIFY
(as well as FAN_ACCESS_PERM) is that they are not
very practical, not well defined for pipes and anyway do
not cover all the ways that a file can be modified/accessed
(i.e. mmap). Therefore, IMO, there is no incentive to fix
something that has been broken for decades unless
you have a very real use case - not a made up one.
Incidentally, I am working on a new set of fanotify
permission events (FAN_PRE_ACCESS/MODIFY)
that will have better defined semantics - those are not
going to be applicable to pipes though.
Thanks,
Amir.
next prev parent reply other threads:[~2023-06-26 6:12 UTC|newest]
Thread overview: 55+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-06-26 3:04 splice(-> FIFO) never wakes up inotify IN_MODIFY? Ahelenia Ziemiańska
2023-06-26 6:11 ` Amir Goldstein [this message]
2023-06-26 12:19 ` Ahelenia Ziemiańska
2023-06-26 12:57 ` Ahelenia Ziemiańska
2023-06-26 13:51 ` Jan Kara
2023-06-26 14:25 ` Ahelenia Ziemiańska
2023-06-26 15:00 ` Jan Kara
2023-06-26 15:15 ` Ahelenia Ziemiańska
2023-06-26 16:52 ` Jan Kara
2023-06-26 14:53 ` Amir Goldstein
2023-06-26 15:12 ` Ahelenia Ziemiańska
2023-06-26 16:21 ` Amir Goldstein
2023-06-26 17:14 ` Ahelenia Ziemiańska
2023-06-26 18:57 ` Amir Goldstein
2023-06-26 23:08 ` [PATCH v2 0/3] fanotify accounting for fs/splice.c наб
2023-06-27 6:14 ` Amir Goldstein
2023-06-27 16:55 ` [PATCH v3 0/3+1] " Ahelenia Ziemiańska
2023-06-27 16:55 ` [PATCH v3 1/3] splice: always fsnotify_access(in), fsnotify_modify(out) on success Ahelenia Ziemiańska
2023-06-27 18:10 ` Amir Goldstein
2023-06-27 20:13 ` Ahelenia Ziemiańska
2023-06-27 16:55 ` [PATCH v3 2/3] splice: fsnotify_access(fd)/fsnotify_modify(fd) in vmsplice Ahelenia Ziemiańska
2023-06-27 18:11 ` Amir Goldstein
2023-06-27 16:55 ` [PATCH v3 3/3] splice: fsnotify_access(in), fsnotify_modify(out) on success in tee Ahelenia Ziemiańska
2023-06-27 16:57 ` [LTP PATCH] inotify13: new test for fs/splice.c functions vs pipes vs inotify Ahelenia Ziemiańska
2023-06-27 18:31 ` Amir Goldstein
2023-06-27 20:59 ` [LTP RFC PATCH v2] " Ahelenia Ziemiańska
2023-06-28 0:21 ` [LTP RFC PATCH v3] " Ahelenia Ziemiańska
2023-06-28 5:30 ` Amir Goldstein
2023-06-28 16:03 ` Ahelenia Ziemiańska
2023-06-27 22:57 ` [LTP PATCH] " Petr Vorel
2023-06-27 18:03 ` [PATCH v3 0/3+1] fanotify accounting for fs/splice.c Amir Goldstein
2023-06-27 20:34 ` Ahelenia Ziemiańska
2023-06-27 20:50 ` [PATCH v4 0/3] " Ahelenia Ziemiańska
2023-06-27 20:50 ` [PATCH v4 1/3] splice: always fsnotify_access(in), fsnotify_modify(out) on success Ahelenia Ziemiańska
2023-06-28 6:33 ` Amir Goldstein
2023-06-28 10:11 ` Jan Kara
2023-06-28 17:09 ` Ahelenia Ziemiańska
2023-06-28 18:38 ` Amir Goldstein
2023-06-28 20:18 ` Ahelenia Ziemiańska
2023-06-30 11:03 ` Amir Goldstein
2023-06-27 20:50 ` [PATCH v4 2/3] splice: fsnotify_access(fd)/fsnotify_modify(fd) in vmsplice Ahelenia Ziemiańska
2023-06-27 20:51 ` [PATCH v4 3/3] splice: fsnotify_access(in), fsnotify_modify(out) on success in tee Ahelenia Ziemiańska
2023-06-28 6:02 ` [PATCH v4 0/3] fanotify accounting for fs/splice.c Amir Goldstein
2023-06-28 11:38 ` Jan Kara
2023-06-28 13:41 ` Amir Goldstein
2023-06-28 18:54 ` Ahelenia Ziemiańska
2023-06-29 8:45 ` Jan Kara
2023-06-28 4:51 ` [PATCH v3 0/3+1] " Christoph Hellwig
2023-06-28 10:38 ` Jan Kara
2023-06-26 23:09 ` [PATCH v2 1/3] splice: always fsnotify_access(in), fsnotify_modify(out) on success Ahelenia Ziemiańska
2023-06-27 6:02 ` Amir Goldstein
2023-06-26 23:09 ` [PATCH v2 2/3] splice: fsnotify_modify(fd) in vmsplice Ahelenia Ziemiańska
2023-06-27 6:27 ` Amir Goldstein
2023-06-26 23:09 ` [PATCH v2 3/3] splice: fsnotify_access(in), fsnotify_modify(out) on success in tee Ahelenia Ziemiańska
2023-06-27 6:20 ` Amir Goldstein
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=CAOQ4uxhut2NHc+MY-XOJay5B-OKXU2X5Fe0-6-RCMKt584ft5A@mail.gmail.com \
--to=amir73il@gmail.com \
--cc=brauner@kernel.org \
--cc=jack@suse.cz \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=nabijaczleweli@nabijaczleweli.xyz \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).