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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 91B9BC433EF for ; Mon, 31 Jan 2022 14:28:22 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 16C646B00A3; Mon, 31 Jan 2022 09:28:22 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 11C856B00A5; Mon, 31 Jan 2022 09:28:22 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 00BE06B00A6; Mon, 31 Jan 2022 09:28:21 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0055.hostedemail.com [216.40.44.55]) by kanga.kvack.org (Postfix) with ESMTP id E5D946B00A3 for ; Mon, 31 Jan 2022 09:28:21 -0500 (EST) Received: from smtpin29.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 97AFC181C9B8B for ; Mon, 31 Jan 2022 14:28:21 +0000 (UTC) X-FDA: 79090812402.29.4FBB3C2 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf28.hostedemail.com (Postfix) with ESMTP id 27FB7C0002 for ; Mon, 31 Jan 2022 14:28:21 +0000 (UTC) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 1E0336132D; Mon, 31 Jan 2022 14:28:20 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6E599C340E8; Mon, 31 Jan 2022 14:28:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1643639299; bh=wgb0fIayWLIdgdYvgRN5Pv6E7yS3YmPSNxpN9xW9IXM=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=XDUH8gIxwHS4ze9OcEm3Pgo5Xw/Q3U7JSnk4mQGqGtoaB3BfmrXfZJA585N4USq/n zWBpKaAoxS4Fo45eH5m2pwmLq8NeQuFPOEwvyoT2TnsFwPyrdBOMZ7tLhTAxOaj5NF v9eE+X0ojAswyRUAjqpMVXG66+h+ZR8XMuqFaJuk3M3Wx4dvu7LDB2B6arsqLgUH4v yjjKNSwkgL4O4/0+Ro6BIJzoBE9c8Wm1FqE08qbBQM/II60ihBo2mxVCBQS+WEz2JM S9cVvgniIPl+AxFDph7VTXibim/+Bj1Ql2sgbfADdizf26pnOEuwBIWuYBbutvsFjm Ce847TBY0aFnw== Date: Mon, 31 Jan 2022 16:28:12 +0200 From: Mike Rapoport To: David Hildenbrand Cc: Nadav Amit , Mike Rapoport , Andrea Arcangeli , Peter Xu , Linux-MM Subject: Re: userfaultfd: usability issue due to lack of UFFD events ordering Message-ID: References: <11831b20-0b46-92df-885a-1220430f9257@redhat.com> <63a8a665-4431-a13c-c320-1b46e5f62005@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <63a8a665-4431-a13c-c320-1b46e5f62005@redhat.com> X-Stat-Signature: 463s58jft61qdnh4wgqe4tzeregm5qod X-Rspam-User: nil Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b=XDUH8gIx; spf=pass (imf28.hostedemail.com: domain of rppt@kernel.org designates 139.178.84.217 as permitted sender) smtp.mailfrom=rppt@kernel.org; dmarc=pass (policy=none) header.from=kernel.org X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 27FB7C0002 X-HE-Tag: 1643639301-66628 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 Mon, Jan 31, 2022 at 03:12:36PM +0100, David Hildenbrand wrote: > On 31.01.22 15:05, Mike Rapoport wrote: > > On Mon, Jan 31, 2022 at 11:48:27AM +0100, David Hildenbrand wrote: > >> On 31.01.22 11:42, Mike Rapoport wrote: > >>> Hi Nadav, > >>> > >>> On Sat, Jan 29, 2022 at 10:23:55PM -0800, Nadav Amit wrote: > >>>> Using userfautlfd and looking at the kernel code, I encountered a usability > >>>> issue that complicates userspace UFFD-monitor implementation. I obviosuly > >>>> might be wrong, so I would appreciate a (polite?) feedback. I do have a > >>>> userspace workaround, but I thought it is worthy to share and to hear your > >>>> opinion, as well as feedback from other UFFD users. > >>>> > >>>> The issue I encountered regards the ordering of UFFD events tbat might not > >>>> reflect the actual order in which events took place. > >>>> > >>>> In more detail, UFFD events (e.g., unmap, fork) are not ordered against > >>>> themselves [*]. The mm-lock is dropped before notifying the userspace > >>>> UFFD-monitor, and therefore there is no guarantee as to whether the order of > >>>> the events actually reflects the order in which the events took place. > >>>> This can prevent a UFFD-monitor from using the events to track which > >>>> ranges are mapped. Specifically, UFFD_EVENT_FORK message and a > >>>> UFFD_EVENT_UNMAP message (which reflects unmap in the parent process) can > >>>> be reordered, if the events are triggered by two different threads. In > >>>> this case the UFFD-monitor cannot figure from the events whether the > >>>> child process has the unmapped memory range still mapped (because fork > >>>> happened first) or not. > >>> > >>> Yeah, it seems that something like this is possible: > >>> > >>> > >>> fork() munmap() > >>> mmap_write_unlock(); > >>> mmap_write_lock_killable(); > >>> do_things(); > >>> mmap_{read,write}_unlock(); > >>> userfaultfd_unmap_complete(); > >>> dup_userfaultfd_complete(); > >>> > >> > >> I was thinking about other possible races, e.g., MADV_DONTNEED/MADV_FREE > >> racing with UFFD_EVENT_PAGEFAULT -- where we only hold the mmap_lock in > >> read mode. But not sure if they apply. > > > > The userspace can live with these, at least for uffd missing page faults. > > If the monitor will try to resolve a page fault for a removed area, the > > errno from UFFDIO_COPY/ZERO can be used to detect such races. > > I was wondering if the monitor could get confused if he just resolved a > page fault via UFFDIO_COPY/ZERO and then receives a REMOVE event. And why would it be confused? -- Sincerely yours, Mike.