All of lore.kernel.org
 help / color / mirror / Atom feed
From: Greg Kurz <groug@kaod.org>
To: Christian Schoenebeck <qemu_oss@crudebyte.com>
Cc: <qemu-devel@nongnu.org>, <qemu-stable@nongnu.org>
Subject: Re: [PATCH 5/6] 9pfs: fix 'Tgetattr' after unlink
Date: Tue, 26 Nov 2024 17:58:35 +0100	[thread overview]
Message-ID: <20241126175835.5442c7d5@bahia> (raw)
In-Reply-To: <5608682.ghPI0kNXTk@silver>

On Tue, 26 Nov 2024 17:03:45 +0100
Christian Schoenebeck <qemu_oss@crudebyte.com> wrote:

> On Sunday, November 24, 2024 4:50:03 PM CET Christian Schoenebeck wrote:
> > With a valid file ID (FID) of an open file, it should be possible to send
> > a 'Tgettattr' 9p request and successfully receive a 'Rgetattr' response,
> > even if the file has been removed in the meantime. Currently this would
> > fail with ENOENT.
> > 
> > I.e. this fixes the following misbehaviour with a 9p Linux client:
> > 
> >   open("/home/tst/filename", O_RDWR|O_CREAT|O_EXCL, 0600) = 3
> >   unlink("/home/tst/filename") = 0
> >   fstat(3, 0x23aa1a8) = -1 ENOENT (No such file or directory)
> > 
> > Expected results:
> > 
> >   open("/home/tst/filename", O_RDWR|O_CREAT|O_EXCL, 0600) = 3
> >   unlink("/home/tst/filename") = 0
> >   fstat(3, {st_mode=S_IFREG|0600, st_size=0, ...}) = 0
> > 
> > This is because 9p server is always using a path name based stat() call
> > which fails as soon as the file got removed. So to fix this, use fstat()
> > whenever we have an open file descriptor already.
> > 
> > Resolves: https://gitlab.com/qemu-project/qemu/-/issues/103
> > Signed-off-by: Christian Schoenebeck <qemu_oss@crudebyte.com>
> > ---
> >  hw/9pfs/9p.c | 8 +++++++-
> >  1 file changed, 7 insertions(+), 1 deletion(-)
> > 
> > diff --git a/hw/9pfs/9p.c b/hw/9pfs/9p.c
> > index 851e36b9a1..578517739a 100644
> > --- a/hw/9pfs/9p.c
> > +++ b/hw/9pfs/9p.c
> > @@ -1596,7 +1596,13 @@ static void coroutine_fn v9fs_getattr(void *opaque)
> >          retval = -ENOENT;
> >          goto out_nofid;
> >      }
> > -    retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
> > +    if ((fidp->fid_type == P9_FID_FILE && fidp->fs.fd != -1) ||
> > +        (fidp->fid_type == P9_FID_DIR && fidp->fs.dir.stream))
> > +    {
> > +        retval = v9fs_co_fstat(pdu, fidp, &stbuf);
> > +    } else {
> > +        retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf);
> > +    }
> 
> As for performance fstat() vs. lstat(): with glibc >= 2.39 and/or Linux
> kernel >= 6.6, fstat() is Theta(1) whereas lstat() is O(log n). So fstat() is
> faster than lstat() and hence prioritizing fstat() over lstat() does make
> sense here IMO.
> 
> That's because on Linux kernel side fstat() is implemented by a simple
> constant time linear array access via file descriptor number, whereas lstat()
> needs to lookup the path and hence walk a tree.
> 
> There is a caveat though: Both on glibc and Linux kernel side there was a
> performance bug each, which were both fixed in September 2023 by glibc 2.39
> and Linux kernel 6.6 respectively:
> 
> kernel fix: https://github.com/torvalds/linux/commit/9013c51
> 
> glibc fix: https://github.com/bminor/glibc/commit/551101e
> 
> So on glibc side, due to a misconception, they inappropriately translated
> fstat(fd, buf) -> fstatat(fd, "", buf, AT_EMPTY_PATH) for a long time, instead
> of just calling fstat() directly as ought to be and done now.
> 
> And on kernel side, the negative performance impact of case AT_EMPTY_PATH +
> empty string wasn't considered in fstatat() implementation. This case is now
> short-circuited right at the beginning of the function.
> 
> /Christian
> 
> 

Great explanation Christian !

Reviewed-by: Greg Kurz <groug@kaod.org>

Cheers,

-- 
Greg


  reply	other threads:[~2024-11-26 17:00 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-24 16:28 [PATCH 0/6] 9pfs: fix fstat() after unlink() (with a Linux guest) Christian Schoenebeck
2024-02-21 14:13 ` [PATCH 1/6] tests/9p: add 'use-after-unlink' test Christian Schoenebeck
2024-11-25  8:47   ` Greg Kurz
2024-11-25  9:34     ` Christian Schoenebeck
2024-11-24 13:34 ` [PATCH 2/6] tests/9p: fix Rreaddir response name Christian Schoenebeck
2024-11-24 19:41   ` Christian Schoenebeck
2024-11-25  8:48   ` Greg Kurz
2024-11-24 14:49 ` [PATCH 3/6] tests/9p: add missing Rgetattr " Christian Schoenebeck
2024-11-24 19:42   ` Christian Schoenebeck
2024-11-25  8:51   ` Greg Kurz
2024-11-24 15:06 ` [PATCH 4/6] 9pfs: remove obsolete comment in v9fs_getattr() Christian Schoenebeck
2024-11-24 19:43   ` Christian Schoenebeck
2024-11-25  8:54   ` Greg Kurz
2024-11-24 15:50 ` [PATCH 5/6] 9pfs: fix 'Tgetattr' after unlink Christian Schoenebeck
2024-11-24 19:44   ` Christian Schoenebeck
2024-11-26 16:03   ` Christian Schoenebeck
2024-11-26 16:58     ` Greg Kurz [this message]
2024-11-24 16:05 ` [PATCH 6/6] tests/9p: also check 'Tgetattr' in 'use-after-unlink' test Christian Schoenebeck
2024-11-26 17:02   ` Greg Kurz
2024-11-25  8:45 ` [PATCH 0/6] 9pfs: fix fstat() after unlink() (with a Linux guest) Greg Kurz
2024-11-25  9:05   ` Greg Kurz
2024-11-25 10:23   ` Christian Schoenebeck
2024-11-25 11:35     ` Greg Kurz
2024-11-25 14:11       ` Christian Schoenebeck
2024-11-27  9:58 ` Christian Schoenebeck

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=20241126175835.5442c7d5@bahia \
    --to=groug@kaod.org \
    --cc=qemu-devel@nongnu.org \
    --cc=qemu-stable@nongnu.org \
    --cc=qemu_oss@crudebyte.com \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.