From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.4 required=3.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS,USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D0AB0C38A29 for ; Tue, 14 Apr 2020 22:34:16 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 888042072D for ; Tue, 14 Apr 2020 22:34:16 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=ziepe.ca header.i=@ziepe.ca header.b="mea1kmjo" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 888042072D Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=ziepe.ca Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 0BD0C8E0003; Tue, 14 Apr 2020 18:34:16 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 046868E0001; Tue, 14 Apr 2020 18:34:16 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E9E4A8E0003; Tue, 14 Apr 2020 18:34:15 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0117.hostedemail.com [216.40.44.117]) by kanga.kvack.org (Postfix) with ESMTP id D26EA8E0001 for ; Tue, 14 Apr 2020 18:34:15 -0400 (EDT) Received: from smtpin27.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 9712A40FB for ; Tue, 14 Apr 2020 22:34:15 +0000 (UTC) X-FDA: 76707915270.27.pigs62_5f5b6daa5f94f X-HE-Tag: pigs62_5f5b6daa5f94f X-Filterd-Recvd-Size: 5124 Received: from mail-qk1-f195.google.com (mail-qk1-f195.google.com [209.85.222.195]) by imf11.hostedemail.com (Postfix) with ESMTP for ; Tue, 14 Apr 2020 22:34:15 +0000 (UTC) Received: by mail-qk1-f195.google.com with SMTP id x66so15231063qkd.9 for ; Tue, 14 Apr 2020 15:34:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ziepe.ca; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=4PccVqG73oYIgQhV3w0lxzxSPUhCnt+/9GCS7XZiCHY=; b=mea1kmjo7TNJbX643hcdHOMQPqYeeALg4oXTOJEtamUQL97Nnqa9/Gizr3PPBU6KxL FvCg3rJDbt2DJJhzqyDH9+/Il5aRVeTA0pPZeaxUOWiPfe5kH4eSoTCe/SHgCfIvxjBg c2orGf2Fy5NaTQM65xmgT4Wb+oJXAq0n+LXlW3WizHK4oBn6mtK1gl0G4Kq3bmIdboEt ufu60oKRK38O4XCMW+LV2v1n4dXd7huW+GrMB8ctSTAe8bhwD/yOqKQtfQ2JFNFCZQjb joqpwDp69UXXoEZ5MisKGWSB835zacOY/xC3b/Fv0zzyOTSCXvNBGG/g9PvObggbxZCl T9Tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=4PccVqG73oYIgQhV3w0lxzxSPUhCnt+/9GCS7XZiCHY=; b=WCuQPmJolaZxrIXah9P3GHwSMx5baZKgmoWy/r8RWpT7WpVDXNAJW2hqImApdx/H/H pyFm5/dktiX8XZneT3nrozQeKsa14YCplo3EvcHAi/cqpZ08rLDeQokXze5hziESoCO2 Pv7u+uk22N2NXoMWoEOyLLzCvPtFQ+k8meCyK3dD39FGt0vQgMRX9aW80AL0t2buGiF2 Lhj4yAKghBvDrh/Zqg42woh9Y9EKFeTiP9+LUqDBoRswi/1LY+/BOwNod2tNSKvLMpJp eS0mFMvCA8FttqUZSq2hQSgCv5U23sZECCPse5fRAF7k1tnRWoxJAFHBG11KOSYoix8+ GQYA== X-Gm-Message-State: AGi0PuYsXqnL6/YyllysVPwxdt33AwskW9V25pAAdrAHYKb+NviKVzjV b0XztjqjNZPWDiRBIxCY558hwQ== X-Google-Smtp-Source: APiQypIJn69ONnTFiLaOc5B6fgwS5Hxql8vlVconUDVUEYx87Powy2n0915w3dyyUYMyNIq9r7kVRg== X-Received: by 2002:a05:620a:670:: with SMTP id a16mr22486428qkh.467.1586903654488; Tue, 14 Apr 2020 15:34:14 -0700 (PDT) Received: from ziepe.ca (hlfxns017vw-142-68-57-212.dhcp-dynamic.fibreop.ns.bellaliant.net. [142.68.57.212]) by smtp.gmail.com with ESMTPSA id h25sm4075963qto.87.2020.04.14.15.34.12 (version=TLS1_2 cipher=ECDHE-ECDSA-CHACHA20-POLY1305 bits=256/256); Tue, 14 Apr 2020 15:34:12 -0700 (PDT) Received: from jgg by mlx.ziepe.ca with local (Exim 4.90_1) (envelope-from ) id 1jOU8A-0005Jx-Oe; Tue, 14 Apr 2020 19:34:10 -0300 Date: Tue, 14 Apr 2020 19:34:10 -0300 From: Jason Gunthorpe To: Peter Xu Cc: Brian Geffon , Andrea Arcangeli , linux-mm , LKML , Sonny Rao Subject: Re: Userfaultfd doesn't seem to break out of poll on fd close Message-ID: <20200414223410.GM5100@ziepe.ca> References: <20200414214516.GA182757@xz-x1> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200414214516.GA182757@xz-x1> User-Agent: Mutt/1.9.4 (2018-02-28) X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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. Jason