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 71D1FC433F5 for ; Mon, 31 Jan 2022 14:12:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D64CD6B00A0; Mon, 31 Jan 2022 09:12:41 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id D14C86B00A1; Mon, 31 Jan 2022 09:12:41 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BB4986B00A2; Mon, 31 Jan 2022 09:12:41 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0145.hostedemail.com [216.40.44.145]) by kanga.kvack.org (Postfix) with ESMTP id ABB376B00A0 for ; Mon, 31 Jan 2022 09:12:41 -0500 (EST) Received: from smtpin21.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 649C981715E7 for ; Mon, 31 Jan 2022 14:12:41 +0000 (UTC) X-FDA: 79090772922.21.6C406C7 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by imf15.hostedemail.com (Postfix) with ESMTP id AD89EA000A for ; Mon, 31 Jan 2022 14:12:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643638360; 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=pPwyHPFWhmFw6/WWtOwNFoPFCoA0RmK1syozFEW5aaI=; b=YlyrG8Jj3znVghuxjnUWzzZYNBcEDN0zcXXmZhfREeRkD0fN7OgjNpeONtP2iSLEmQTk2A r8tAsZrq3LuQUAwo1fmNyc+hQ/yVR9VOYxcwSKGOqDzWrO8Ay8imLOdszc5S8Ghs7n9PKI w6ockRQn7egjzUe6XyfUzjSjyZNJDB8= Received: from mail-wm1-f72.google.com (mail-wm1-f72.google.com [209.85.128.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-345-054999OZMKWVb9fDdJaJsw-1; Mon, 31 Jan 2022 09:12:38 -0500 X-MC-Unique: 054999OZMKWVb9fDdJaJsw-1 Received: by mail-wm1-f72.google.com with SMTP id n22-20020a05600c3b9600b00352d3f6a850so899382wms.3 for ; Mon, 31 Jan 2022 06:12:38 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:cc:references:from:organization:in-reply-to :content-transfer-encoding; bh=pPwyHPFWhmFw6/WWtOwNFoPFCoA0RmK1syozFEW5aaI=; b=a2s3q7R0aG2PqrHzjas4DMsFL/vw4QUciBNE94sDnvFl2nnpCI8ZW0/bbaJYRvVVj9 itLpornZhNq286fPkXqROPOpPh2aDaLPmIPWxH0d7b5/OqObhYushg3SSKhf2HwrKDbX FwIGrj7RZ63R85DsT0gdf7QzTvIFWbdnD7ps+d385bZVtz9ZNbrE6LhhNwIxPE1Zhq6U 3Hzp4bGa6F4IqZoXuN+k7XyvLwVuuNdGWwoj1dd9kTygZNF0L9a4v2sSjaI/XXg2XWSL Xna3/oRkz1xFLv3fH2fRzftf2PhI+vjiaAxTEWtYOMYVTHrP7Gt3XAijqaSuf5K6rvwg On4g== X-Gm-Message-State: AOAM5332pocv4MEMNl8H6FahPCc0JP2mFhUVpqciHb6vJO8d+81BnVBl HwMeI64Mx8b25YzvwlfREE4iw5rUUdKGAq/nMpXacTmaMsTJ8JQoF/wpv+GM5Sbx8H6DDK5E6UP c/SWdhQzsdHU= X-Received: by 2002:a5d:64ef:: with SMTP id g15mr9662272wri.701.1643638357635; Mon, 31 Jan 2022 06:12:37 -0800 (PST) X-Google-Smtp-Source: ABdhPJz8RhAJIr2S9QOsg4YzYNqkoe8WoqJiBl5FqPPNimfwGGnHxr4rgxdLy8kM4PfHd9JJinETWw== X-Received: by 2002:a5d:64ef:: with SMTP id g15mr9662253wri.701.1643638357365; Mon, 31 Jan 2022 06:12:37 -0800 (PST) Received: from ?IPV6:2003:cb:c709:b200:f007:5a26:32e7:8ef5? (p200300cbc709b200f0075a2632e78ef5.dip0.t-ipconnect.de. [2003:cb:c709:b200:f007:5a26:32e7:8ef5]) by smtp.gmail.com with ESMTPSA id f14sm9452140wmq.40.2022.01.31.06.12.36 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 31 Jan 2022 06:12:36 -0800 (PST) Message-ID: <63a8a665-4431-a13c-c320-1b46e5f62005@redhat.com> Date: Mon, 31 Jan 2022 15:12:36 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.4.0 Subject: Re: userfaultfd: usability issue due to lack of UFFD events ordering To: Mike Rapoport Cc: Nadav Amit , Mike Rapoport , Andrea Arcangeli , Peter Xu , Linux-MM References: <11831b20-0b46-92df-885a-1220430f9257@redhat.com> From: David Hildenbrand Organization: Red Hat In-Reply-To: X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Rspam-User: nil X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: AD89EA000A X-Stat-Signature: b5zces1cxeoo4tatx4bkpk1xxtbdnx5f Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=YlyrG8Jj; spf=none (imf15.hostedemail.com: domain of david@redhat.com has no SPF policy when checking 170.10.129.124) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-HE-Tag: 1643638360-590380 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 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. -- Thanks, David / dhildenb