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 19946C433EF for ; Mon, 31 Jan 2022 10:42:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A58266B0073; Mon, 31 Jan 2022 05:42:56 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A077A6B0074; Mon, 31 Jan 2022 05:42:56 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8F6A46B0075; Mon, 31 Jan 2022 05:42:56 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0175.hostedemail.com [216.40.44.175]) by kanga.kvack.org (Postfix) with ESMTP id 81F256B0073 for ; Mon, 31 Jan 2022 05:42:56 -0500 (EST) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 2F91B818677C for ; Mon, 31 Jan 2022 10:42:56 +0000 (UTC) X-FDA: 79090244352.04.6CD941A Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by imf26.hostedemail.com (Postfix) with ESMTP id 9B865140004 for ; Mon, 31 Jan 2022 10:42:55 +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 99FC66136C; Mon, 31 Jan 2022 10:42:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id C405AC340E8; Mon, 31 Jan 2022 10:42:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1643625774; bh=VGf6xj/kUzvVsSuae+JtLR6KtQOV4IeFBXnEhqN9TBo=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=Y/86eKbUfHFYE05o6Y+O7snkD7rAADwCGeUpBkCepYzl/2C8zOYECl59pDxAi9xjK sB/ABmQzKowj/gDBJQwPeh2QQXzgh6I4KT7vcXwz0i+k1HC2CGBMnARRC+SMpRykth F3CYiY6AJyn5phEPadXD3cm5HzZbF762YqOSNib72iOxboj2fziLKXY7WY4ob3g2iW FzDfpANMMS0ZLPihIHgPaQb5PrmFKg9xi1OLEeMPDar+72wB60sIkZZ6dtR9I/tmyT AU1ckN6BUnOuwr152e+LkNjMYQ6QP3d045ux4Fb0kTZRItyGkcdGiX4Ac9ELYBvMuU NXQZ3lYef6HwA== Date: Mon, 31 Jan 2022 12:42:45 +0200 From: Mike Rapoport To: Nadav Amit Cc: David Hildenbrand , Mike Rapoport , Andrea Arcangeli , Peter Xu , Linux-MM Subject: Re: userfaultfd: usability issue due to lack of UFFD events ordering Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: X-Stat-Signature: 7uazwuw6gs57i3z85s6qhtdekwded59o X-Rspam-User: nil Authentication-Results: imf26.hostedemail.com; dkim=pass header.d=kernel.org header.s=k20201202 header.b="Y/86eKbU"; spf=pass (imf26.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: rspam01 X-Rspamd-Queue-Id: 9B865140004 X-HE-Tag: 1643625775-244290 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: 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(); A solution could be to split uffd_*_complete() to two parts: one that queues up the event message and the second one that waits for it to be read by the monitor. The first part then can run befor mm-lock is released. If you can think of something nicer, it'll be really great! > Obviously, it does not make sense to keep holding mm-lock while notifying the > user, as it can even lead to deadlocks. Userspace UFFD-monitors can > workaround this issue by using seccomp+ptrace instead of UFFD-events to > obtain order of the events or examine /proc/[pid]/smaps. Yet, this introduces > overheads, is complicated, and I doubt anyone does so. I wonder if the API is > reasonable, or whether I am missing something. > > Thanks, > Nadav > > [*] Note that I do not discuss UFFD-monitor issued ioctl's, but the order > between UFFD-events. > -- Sincerely yours, Mike.