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 E08ABC433EF for ; Mon, 31 Jan 2022 17:23:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 66D4B8D0015; Mon, 31 Jan 2022 12:23:58 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 61C548D0001; Mon, 31 Jan 2022 12:23:58 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4BD428D0015; Mon, 31 Jan 2022 12:23:58 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0176.hostedemail.com [216.40.44.176]) by kanga.kvack.org (Postfix) with ESMTP id 3C9B78D0001 for ; Mon, 31 Jan 2022 12:23:58 -0500 (EST) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id EFAC918215EC1 for ; Mon, 31 Jan 2022 17:23:57 +0000 (UTC) X-FDA: 79091254914.09.14F7410 Received: from mail-pg1-f171.google.com (mail-pg1-f171.google.com [209.85.215.171]) by imf10.hostedemail.com (Postfix) with ESMTP id 70FAEC0006 for ; Mon, 31 Jan 2022 17:23:57 +0000 (UTC) Received: by mail-pg1-f171.google.com with SMTP id z131so12804161pgz.12 for ; Mon, 31 Jan 2022 09:23:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=K2mAdaWq4UsOVIyV8S/yimBA579U0j14Vh3u9RFH+co=; b=XKwmk80MQeEIvWj+gsTum4zhiT9AN+pSMaMPA8ROOFAdFXbzPEpQVcIkAcUdhC35Tj /s5jEWBRVRm3QODnS5PDTCjTs9PAniV3zP2G6L9RnrygDpdiMOAgVmvEMd+Z+6G+bdD9 JSJQYNkrYDNl5k8q/W+P6JepD8JXLL12aeoj7VaDsEFrXucUNNtyXj5OFSfISM53jkGF NspfdWzA+bJ9zY0ifn/vi9s2KF3OMrWdnvVsGR1xF4JB8/GHNVFuczPdL/pBX6YaeG1j NUxO1iQ9AN2u0fZ1MIz3fXkbhmFuSV2sCnE9eWO6c/L6BFQfqnYIMBmrdnFzOKzvEB6v ll3w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=K2mAdaWq4UsOVIyV8S/yimBA579U0j14Vh3u9RFH+co=; b=codyiPF20jnlCpMM1lMcusnfdr4hENNkmUtl4SHsVGwdELu0kePXAYcZ8JwAjeN7h6 jCSHNUEGmswDuTW+p+1o9WCbyyRVuiOYqME6gYk+dKoEW/Sd/H/6UjxkkT7og8OGZ6nW IquX4/ulzB9nvos6MppQvpl764TdhE/Z191bpa1ix3RlTDj3VD6lBNJaQ+wZmisXx0P9 JggDsiSZAlwc66WscYBL6Nb8F7WA3+nA+ufJD3jnXPgrZ0JqGOVrilP9Xw/oRqUqWWfi bf78+dWsICByYazzm+PDUJDjACcmvEA+7mOD5Au5DwY+g+1xlfSR8Zv42XZh/9oaSfWr Qskw== X-Gm-Message-State: AOAM533ClCp1LDYx08AL1iNiQT5CpkrwgjH2AakNa2drBn/z+gJsHlSs Vvh2n07BoufpogFEBlPcQSo= X-Google-Smtp-Source: ABdhPJxfXSVUSJPiu+ph8cKvgNPKTSr1MOyMmEHh668zv0/Er4DUaS9fa1wI2aaK6fwpIQHIN8e8/g== X-Received: by 2002:a63:36c8:: with SMTP id d191mr17381329pga.377.1643649836255; Mon, 31 Jan 2022 09:23:56 -0800 (PST) Received: from smtpclient.apple (c-24-6-216-183.hsd1.ca.comcast.net. [24.6.216.183]) by smtp.gmail.com with ESMTPSA id x33sm21381976pfh.178.2022.01.31.09.23.54 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 31 Jan 2022 09:23:55 -0800 (PST) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 15.0 \(3693.40.0.1.81\)) Subject: Re: userfaultfd: usability issue due to lack of UFFD events ordering From: Nadav Amit In-Reply-To: Date: Mon, 31 Jan 2022 09:23:54 -0800 Cc: David Hildenbrand , Mike Rapoport , Andrea Arcangeli , Peter Xu , Linux-MM Content-Transfer-Encoding: quoted-printable Message-Id: <18B50289-223E-4C78-B2D6-8E9F0B9E2387@gmail.com> References: To: Mike Rapoport X-Mailer: Apple Mail (2.3693.40.0.1.81) X-Rspamd-Queue-Id: 70FAEC0006 X-Rspam-User: nil Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=gmail.com header.s=20210112 header.b=XKwmk80M; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf10.hostedemail.com: domain of nadav.amit@gmail.com designates 209.85.215.171 as permitted sender) smtp.mailfrom=nadav.amit@gmail.com X-Stat-Signature: p5wpihfryuzf8qgp5aqws4wfawu8ecmp X-Rspamd-Server: rspam08 X-HE-Tag: 1643649837-4667 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 Jan 31, 2022, at 2:42 AM, Mike Rapoport wrote: >=20 > Hi Nadav, >=20 > 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. >>=20 >> The issue I encountered regards the ordering of UFFD events tbat = might not >> reflect the actual order in which events took place. >>=20 >> 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. >=20 > Yeah, it seems that something like this is possible: >=20 >=20 > fork() munmap() > mmap_write_unlock(); > = mmap_write_lock_killable(); > do_things(); > = mmap_{read,write}_unlock(); > = userfaultfd_unmap_complete(); > dup_userfaultfd_complete(); >=20 > 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. >=20 > If you can think of something nicer, it'll be really great! Thanks for the quick response. Your solution is possible, but then the order between events and page-faults is certainly not kept - as David mentioned: regardless of mm-lock that is not always taken for write, events and page-faults are on two separate lists, and queued page-faults are reported before events. I am also not sure how simple/performant it is, since it would require an additional refcount for userfaultfd_wait_queue to prevent it from disappearing between the time it is enqueued to the time it blocks. Another option is to associate some =E2=80=9Cgeneration=E2=80=9D or = =E2=80=9Csequence number=E2=80=9D with every event and change the PAI to include it. It still leaves the problem of ordering MADV_DONTNEED and page-faults though.=