All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jason Gunthorpe <jgg@ziepe.ca>
To: Hillf Danton <hdanton@sina.com>
Cc: Peter Xu <peterx@redhat.com>, Brian Geffon <bgeffon@google.com>,
	Andrea Arcangeli <aarcange@redhat.com>,
	linux-mm <linux-mm@kvack.org>,
	LKML <linux-kernel@vger.kernel.org>,
	Sonny Rao <sonnyrao@google.com>
Subject: Re: Userfaultfd doesn't seem to break out of poll on fd close
Date: Wed, 15 Apr 2020 11:25:46 -0300	[thread overview]
Message-ID: <20200415142546.GO5100@ziepe.ca> (raw)
In-Reply-To: <20200415031602.22348-1-hdanton@sina.com>

On Wed, Apr 15, 2020 at 11:16:02AM +0800, Hillf Danton wrote:
> 
> On Tue, 14 Apr 2020 19:34:10 -0300 Jason Gunthorpe wrote:
> > 
> > On Tue, Apr 14, 2020 at 05:45:16PM -0400, Peter Xu wrote:
> > > On Sun, Apr 12, 2020 at 01:10:40PM -0700, Brian Geffon wrote:
> > > > Hi,
> > > > It seems that userfaultfd isn't woken from a poll when the file
> > > > descriptor is closed. It seems that it should be from the code in
> > > > userfault_ctx_release, but it appears that's not actually called
> > > > immediately. I have a simple standalone example that shows this
> > > > behavior. It's straight forward: one thread creates a userfaultfd and
> > > > then closes it after a second thread has entered a poll syscall, some
> > > > abbreviated strace output is below showing this and the code can be
> > > > seen here: https://gist.github.com/bgaff/9a8fbbe8af79c0e18502430d416df77e
> > > > 
> > > > Given that it's probably very common to have a dedicated thread remain
> > > > blocked indefinitely in a poll(2) waiting for faults there must be a
> > > > way to break it out early when it's closed. Am I missing something?
> > > 
> > > Hi, Brian,
> > > 
> > > I might be wrong below, just to share my understanding...
> > > 
> > > IMHO a well-behaved userspace should not close() on a file descriptor
> > > if it's still in use within another thread.  In this case, the poll()
> > > thread is still using the userfaultfd handle
> > 
> > I also don't think concurrant close() on a file descriptor that is
> > under poll() is well defined, or should be relied upon.
> > 
> > > IIUC userfaultfd_release() is only called when the file descriptor
> > > destructs itself.  But shouldn't the poll() take a refcount of that
> > > file descriptor too before waiting?  Not sure userfaultfd_release() is
> > > the place to kick then, because if so, close() will only decrease the
> > > fd refcount from 2->1, and I'm not sure userfaultfd_release() will be
> > > triggered.
> > 
> > This is most probably true.
> > 
> > eventfd, epoll and pthread_join is the robust answer to these
> > problems.
> > 
> 
> See the difference EPOLLHUP makes.

The whole idea is completely racey:

          CPU1                            CPU2                  CPU3
 fds[i]->fd = userfaultfd;
 while()
                                       close(userfaultfd)
                                       pthread_join()
                                                            someother_fd = open()
                                                            userfaultfd == someother_fd
     poll(fds)   // <- Still sleeps

The kernel should not be trying to wake poll from fd release, and
userspace should not close a FD that is currently under poll.

Besides, it really does look like poll holds the fget while doing its
work (see poll_freewait), so fops release() won't be called anyhow..

Jason


  reply	other threads:[~2020-04-15 14:25 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-12 20:10 Userfaultfd doesn't seem to break out of poll on fd close Brian Geffon
2020-04-14 21:45 ` Peter Xu
2020-04-14 22:34   ` Jason Gunthorpe
2020-04-15  3:16   ` Hillf Danton
2020-04-15 14:25     ` Jason Gunthorpe [this message]
2020-04-15 15:16       ` Brian Geffon
2020-04-16  0:02       ` Andrea Arcangeli
2020-04-16  1:15         ` Brian Geffon
2020-04-16  1:37           ` Peter Xu
2020-04-16  4:39             ` Brian Geffon
2020-04-16 14:49           ` Jason Gunthorpe

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=20200415142546.GO5100@ziepe.ca \
    --to=jgg@ziepe.ca \
    --cc=aarcange@redhat.com \
    --cc=bgeffon@google.com \
    --cc=hdanton@sina.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=peterx@redhat.com \
    --cc=sonnyrao@google.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.