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 93860C433EF for ; Mon, 31 Jan 2022 14:41:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 297386B00A9; Mon, 31 Jan 2022 09:41:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2469E6B00AB; Mon, 31 Jan 2022 09:41:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 10F096B00AC; Mon, 31 Jan 2022 09:41:10 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay039.a.hostedemail.com [64.99.140.39]) by kanga.kvack.org (Postfix) with ESMTP id CCBF46B00A9 for ; Mon, 31 Jan 2022 09:41:10 -0500 (EST) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 7503E2272B for ; Mon, 31 Jan 2022 14:41:10 +0000 (UTC) X-FDA: 79090844700.01.DB70B1D Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf21.hostedemail.com (Postfix) with ESMTP id DB1C31C000E for ; Mon, 31 Jan 2022 14:41:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643640069; 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=opgIQJEwEuXpvkQ3WQ7kstdnySKcWNZkapY8yd4dySU=; b=UzOWQ/Ha0/MhGp6/ITtDH0meh79UxnyqcrzBZ5RtANNiOW7erZRJjH0mchUE//L41xTF5K +7lAcU4p9TiJiFJycuKQc+vKWXQaXrd+hLz+1UxFQMuezifRy+tmUbp5WoQp48JRDG1kyQ lA866ljTyvNuM7LfynUHlI1V/qy0McM= Received: from mail-ed1-f71.google.com (mail-ed1-f71.google.com [209.85.208.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-12-pcqNcTEUNvCM4IruspPrwQ-1; Mon, 31 Jan 2022 09:41:07 -0500 X-MC-Unique: pcqNcTEUNvCM4IruspPrwQ-1 Received: by mail-ed1-f71.google.com with SMTP id l16-20020aa7c3d0000000b004070ea10e7fso7003409edr.3 for ; Mon, 31 Jan 2022 06:41:07 -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 :content-language:to:cc:references:from:organization:subject :in-reply-to:content-transfer-encoding; bh=opgIQJEwEuXpvkQ3WQ7kstdnySKcWNZkapY8yd4dySU=; b=Bv09C5ntAqgJsDj8q27Q5T/jlGdgMNenG0tTjYh5eo+hGSC/pqB8mGvQDizX4bBVtp TaUXH9WqRXVM4fSQeUGnh470b8N6t3KVWxBhuKBytLWneaspm757jjKxgBtikMhRGpMk vFgdOLMmkZla9jZGu6Z/lYgdekKjhAg4zBx5aWfzteQXqM+dBZ1txsYHbRBrs0ERtSIQ yHn7EWoJ6359iRqPsdxt2J9Ran/C0qX/LsxBJt46gKTOq3lzXwr2pgybdif5AC19wuPH sw7Cen+Q4STGw6UDo4cPrky6PlpbNgHW3oStRhiDiyLvaHBZTPO5PIpcXxeOfuAPcUUe tweQ== X-Gm-Message-State: AOAM530OvqOKkduj7RF4IVrpgVAFDiRYiUz/tazm4u6WjoNVVq4I4b3U SteJWAcpLOakSCmyWYPS/Vd5j+ToEbC1WA86ljJuTsk0DsTxp4HBL/d5wWJ2cIxyKBIOk1PYBfs C89l9nlGhoZo= X-Received: by 2002:a17:907:9488:: with SMTP id dm8mr17188543ejc.73.1643640066665; Mon, 31 Jan 2022 06:41:06 -0800 (PST) X-Google-Smtp-Source: ABdhPJzyvcvGP+XF0jNPQwgxLooVzfvhIZh1lLvDFH+hgJgBGGtf1/b14IRAMp4pwVBHDzxPsSAwSw== X-Received: by 2002:a17:907:9488:: with SMTP id dm8mr17188529ejc.73.1643640066482; Mon, 31 Jan 2022 06:41:06 -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 d2sm13532792ejw.70.2022.01.31.06.41.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 31 Jan 2022 06:41:06 -0800 (PST) Message-ID: Date: Mon, 31 Jan 2022 15:41:05 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.4.0 To: Mike Rapoport Cc: Nadav Amit , Mike Rapoport , Andrea Arcangeli , Peter Xu , Linux-MM References: <11831b20-0b46-92df-885a-1220430f9257@redhat.com> <63a8a665-4431-a13c-c320-1b46e5f62005@redhat.com> From: David Hildenbrand Organization: Red Hat Subject: Re: userfaultfd: usability issue due to lack of UFFD events ordering 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-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: DB1C31C000E X-Rspam-User: nil Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b="UzOWQ/Ha"; dmarc=pass (policy=none) header.from=redhat.com; spf=none (imf21.hostedemail.com: domain of david@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=david@redhat.com X-Stat-Signature: mnro85fjoc74knjjqi96kz8t6pxqd8rb X-HE-Tag: 1643640069-32644 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:28, Mike Rapoport wrote: > 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? My thinking was that the monitor might use REMOVE events to track which pages are actually populated. If you receive REMOVE after UFFDIO_COPY/ZERO the monitor would conclude that the page is not populated, just like if we'd get the MADV_DONTNEED/MADV_REMOVE immediately after placing a page. Of course, it heavily depends on the target use case in the monitor or I might just be wrong. -- Thanks, David / dhildenb