Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp710137ybz; Wed, 15 Apr 2020 17:14:19 -0700 (PDT) X-Google-Smtp-Source: APiQypLdzrymjzie9V4wW5trKxpxOov19hkzfowpQLOl+hxqnw8eCKF7SpyvwXhhx/fASetv/GVL X-Received: by 2002:aa7:ce82:: with SMTP id y2mr27688340edv.11.1586996059112; Wed, 15 Apr 2020 17:14:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1586996059; cv=none; d=google.com; s=arc-20160816; b=noNtZb7aQyiBIZ967eie5ghKONEPqcS97mm0SEQTPPI3Fkn0THZDMkdXWavcIlPeG7 +eqwVx19Ccw6yZc+QPm3rywUpTqbDwO5cBp561EdiFDr2KXNF80gHYVKAX+2e7c1dvqH irCYcH0ro7s2K4odRDGr8wW6sLyYTLG2BRYYp/48sDYvgi1jo8ZuRnyZ+WwNRfdiP+HD z2r/UVQ5wbpTAGzY+sHBLx/NnBLi05KNSOGuviJzRKysOVJ67IAZILGEH/EjdEPWe7R3 nc/mY/huRrBHSgdTjIcEQsLjQN9Ts3XcAjFsvEy7TBrPMtWFUjPmZz9wexvi/C1ScZlG bT9g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=AznA+h/3cK3KAncERuNAqxLwQ846WsMdLs+fjVSErA0=; b=J0P4j7pPS2jcIfnrbkgwwjHap3z3kuOfrVPzc+rb2ozNVTEgLzd0p1fLvr5YcyPwO1 bHiQ5Rh7V/CH0XkqBcoJkroJPDtAtnErr+833uc8J0HUJT3vOA0sXudnGSsBdAjzLFU/ FRhieaQ2Lv19Ny2czbbf0MyVVZKJCJkkcxuo2lwpSO0WOl0fR+PFSLCppkZdkmbep+Q3 GILcGTzGUpwbFgkS5Mr1g5kg8fDeIWeFwAde17Z8SwuUTm/7brU1Iic5TEYfRsFp/11B P97wsJEZeNCdQEPUDy4j3bKDnsXo27y3WcuyluImXgM22//UtaxLhBpRRAEgBkYvl3RZ 7F6A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=GxDA6hmp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l13si7402562edi.509.2020.04.15.17.13.56; Wed, 15 Apr 2020 17:14:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=GxDA6hmp; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1414626AbgDOPRl (ORCPT + 99 others); Wed, 15 Apr 2020 11:17:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46366 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1414618AbgDOPRi (ORCPT ); Wed, 15 Apr 2020 11:17:38 -0400 Received: from mail-ed1-x544.google.com (mail-ed1-x544.google.com [IPv6:2a00:1450:4864:20::544]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 89A84C061A0C for ; Wed, 15 Apr 2020 08:17:38 -0700 (PDT) Received: by mail-ed1-x544.google.com with SMTP id i7so5364982edq.3 for ; Wed, 15 Apr 2020 08:17:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=AznA+h/3cK3KAncERuNAqxLwQ846WsMdLs+fjVSErA0=; b=GxDA6hmpKkfHchiJxVFamypNBy9Ctt62byY4TTPc1XlJ0qyqECgO+nsJJaVxTCfbzm GrhlgD0Q3Ubail7kT1oQFd+yI/ijySpO1QHMOpX0ldAJXCRMenSJgjoEWrCGq2dIv3Ue Xy4tJM1Or2hymiw/uLyEvbkbe1g2yYa+TOXtoP+Re2eLm8IdqqWXhPcnd9zyWQmtQveO LoI8Ru71oz00dJTMAH2pSZX68zD5IkDp+X/3d4bPz+tUGkIxTi9xlI1cU2iIv2M9PmHI 3fnmacwk9yrP8xAZEqxpyGuf+XVv5sh/qcj6gJNnMyk4/WU2nOzHMrD1FMA1V0JRUSdG 8fog== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=AznA+h/3cK3KAncERuNAqxLwQ846WsMdLs+fjVSErA0=; b=r0foBWjfd2NjX9kxuXC+iszrOjqbb9xSQ8zFuhXUjwaptTJxRM/YT3B0yVckxdzrns W0ofMxE899imGK87iz2vfNRPwA+rGb+YqEPz146byW5nYIGXtop1qycYyOeF4i4HJgqP 8o+ndoZU4IisY1zVGkHJ79WBjUkOB63BpzpWBwQBixiIZoDTpnASB/iRft+sqIcWAaY1 cKxLWyCEABHkXMXAeFVPzUN0ttX/OCVMBRAh7wa3TOdM20w/MfAI+vdPodD9H0SGQHAm fxFN8fU8Uw3XUQNlJltxcasIG9IodCkLBg9X+jx4nBCA0LzkqliohPAWoNsht+/GXaGH vxxg== X-Gm-Message-State: AGi0PuYN1xguWyHGZjWZlmmcgtb0AQWCCisE8jtG54fgrwtnsMqQ87+E AxOgV0DSdQF6epri7nP+h1z+lYgks7Z6y006EtbI/g== X-Received: by 2002:a17:906:f1c4:: with SMTP id gx4mr5284444ejb.171.1586963856720; Wed, 15 Apr 2020 08:17:36 -0700 (PDT) MIME-Version: 1.0 References: <20200414214516.GA182757@xz-x1> <20200415031602.22348-1-hdanton@sina.com> <20200415142546.GO5100@ziepe.ca> In-Reply-To: <20200415142546.GO5100@ziepe.ca> From: Brian Geffon Date: Wed, 15 Apr 2020 08:16:59 -0700 Message-ID: Subject: Re: Userfaultfd doesn't seem to break out of poll on fd close To: Jason Gunthorpe Cc: Hillf Danton , Peter Xu , Andrea Arcangeli , linux-mm , LKML , Sonny Rao Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Thanks everyone for the insights! On Wed, Apr 15, 2020 at 7:25 AM Jason Gunthorpe wrote: > > 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