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=-5.9 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, MENTIONS_GIT_HOSTING,SPF_HELO_NONE,SPF_PASS 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 5B354C2BB1D for ; Tue, 14 Apr 2020 21:45:26 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 173C020767 for ; Tue, 14 Apr 2020 21:45:25 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="SqVyiwsP" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 173C020767 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=redhat.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id A07F58E0003; Tue, 14 Apr 2020 17:45:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 992048E0001; Tue, 14 Apr 2020 17:45:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 833E88E0003; Tue, 14 Apr 2020 17:45:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0185.hostedemail.com [216.40.44.185]) by kanga.kvack.org (Postfix) with ESMTP id 68C528E0001 for ; Tue, 14 Apr 2020 17:45:25 -0400 (EDT) Received: from smtpin05.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 0E5E18245578 for ; Tue, 14 Apr 2020 21:45:25 +0000 (UTC) X-FDA: 76707792210.05.brick98_697376f273b30 X-HE-Tag: brick98_697376f273b30 X-Filterd-Recvd-Size: 5343 Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by imf13.hostedemail.com (Postfix) with ESMTP for ; Tue, 14 Apr 2020 21:45:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1586900724; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=uPCsJU7vrHpuSIuLldYHffM8CGWRa2zEgIM1fcW6l6M=; b=SqVyiwsPIJOcLRreKroeIKU0Fs7upvN+EjYSNdgHRuVPBgTgNxA0pXWQZeEf4254M9zjTC Hj0V2Taiyof7Uf8mOBksJ6KQlDLNezB8PGY7giNIuaJOJItljtQDHqDGJLDqz31yN3nIkZ Mlf2M1paLLrGPeXBE8MW0xMY00/K8nM= Received: from mail-qv1-f70.google.com (mail-qv1-f70.google.com [209.85.219.70]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-336-aehZyU52Mp2SutqrpMpjYA-1; Tue, 14 Apr 2020 17:45:19 -0400 X-MC-Unique: aehZyU52Mp2SutqrpMpjYA-1 Received: by mail-qv1-f70.google.com with SMTP id f4so1128047qvu.19 for ; Tue, 14 Apr 2020 14:45:19 -0700 (PDT) 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; bh=ZeGGFPup1XJHwyIEzNEDendxGJK59EpZeVXypI0W67g=; b=cpU4gn12I8LPXAg5hJ+u6zTGVJ7+k6mDQsxp+9FJAiaCF0dEjxYOh1TBr6CsSKzBkD xysN24Ui62nRvMzJ4UTR2JhxLFCfW7ZLXLXmHiG+Q44h6IsXg1P4xjIxCGq3/xB73EDr k/0ZvTT0M3DMrmoJFvgiDlKLScCPC/YV5uUXaaQFfW+acOUu/yw2q6syvNHToCCyJeW/ SBpka3p0SCNcr2uB7KngRnQI5ANqFWMLIqWjF3u5uYXzmTgNzMKRrkV8FQLEe79Ls/7x fDl9YAI5WKvTonrZWMOT1tlL+27BlVfgHQNE8fdRpM0dqvUGCeXDDebYYuyOYDzNyuq4 3Ptw== X-Gm-Message-State: AGi0PuahjenL9KGQslKb37vWpkDR+darHRG8UkbojR7xnqkqNAEUeEwm pT+SD59QrT9mqm2W6+buxmNuTx9yqUHS/+cnEz0tvPzrZubPieAG2fcTushJDLeh60YmmuUj9f4 ESuFcF7uXHgw= X-Received: by 2002:ac8:19fd:: with SMTP id s58mr13025715qtk.222.1586900719303; Tue, 14 Apr 2020 14:45:19 -0700 (PDT) X-Google-Smtp-Source: APiQypLnIUCv2QyX3WPwS8EK5rktDGhPYL53gRHXyaxrMeQgwGmL+C8SmB9EGuhtZKwhRNIFRi4DHA== X-Received: by 2002:ac8:19fd:: with SMTP id s58mr13025690qtk.222.1586900719028; Tue, 14 Apr 2020 14:45:19 -0700 (PDT) Received: from xz-x1 ([2607:9880:19c0:32::2]) by smtp.gmail.com with ESMTPSA id v5sm11448631qka.106.2020.04.14.14.45.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 14 Apr 2020 14:45:18 -0700 (PDT) Date: Tue, 14 Apr 2020 17:45:16 -0400 From: Peter Xu To: Brian Geffon Cc: Andrea Arcangeli , linux-mm , LKML , Sonny Rao Subject: Re: Userfaultfd doesn't seem to break out of poll on fd close Message-ID: <20200414214516.GA182757@xz-x1> References: MIME-Version: 1.0 In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline 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 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 >=20 > 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, so imo it's cleaner that the main thread should pthread_join() on the poll() thread before it closes the handle. It can be easily achieved by attaching another eventfd to the struct pollfds array, and write to the eventfd when the main thread wants to quit so that the poll() will return on the write to the eventfd. On the other hand I'm thinking whether we can achieve what you said. 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. Thanks, --=20 Peter Xu