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 43AF4C433F5 for ; Mon, 31 Jan 2022 17:29:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C5A176B00D8; Mon, 31 Jan 2022 12:29:04 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C0A886B00D9; Mon, 31 Jan 2022 12:29:04 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id AAAB86B00DA; Mon, 31 Jan 2022 12:29:04 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0169.hostedemail.com [216.40.44.169]) by kanga.kvack.org (Postfix) with ESMTP id 97E5D6B00D8 for ; Mon, 31 Jan 2022 12:29:04 -0500 (EST) Received: from smtpin16.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 5D97098C0B for ; Mon, 31 Jan 2022 17:29:04 +0000 (UTC) X-FDA: 79091267808.16.3C2D2F2 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by imf07.hostedemail.com (Postfix) with ESMTP id 4B54440002 for ; Mon, 31 Jan 2022 17:29:03 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1643650142; 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=6YssarRgfVxnSWEnvlnQzSqHFZFIik/ezEmYYEXQuqA=; b=eUahWFOG4TgU5r92zeTB1+59svA1KjVRucRcAuK8CNDeIfsBi5hpOfniRqC2Dunqgb24VG wVQhwWHVgEz6+pCfHQXWWks+S3G7b2SjWDBKvxFi0as4c2A4vEFLFOGfj6kCRl7kLqe2G2 WE4m7jsyol7cfxZISJmE9NpBs2Pnxys= Received: from mail-ej1-f69.google.com (mail-ej1-f69.google.com [209.85.218.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-436-uWvCRMBfOCWDlWPPucvxbg-1; Mon, 31 Jan 2022 12:29:01 -0500 X-MC-Unique: uWvCRMBfOCWDlWPPucvxbg-1 Received: by mail-ej1-f69.google.com with SMTP id x16-20020a170906135000b006b5b4787023so5331052ejb.12 for ; Mon, 31 Jan 2022 09:29:01 -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=6YssarRgfVxnSWEnvlnQzSqHFZFIik/ezEmYYEXQuqA=; b=HSJXPY/GDU0H04DB2+/ZkxtDo6G7BRWtalvnpFSgT10thW7zUI2i1v8tm0yWPwBhxl CgxKc8riBvlC9cjrsPkuhIg7pHQ1ECa1GzEupd84/Cncf/9/F+GfypihHXB6lFaXTd1G Tk/SmIXMlvoglWU39S/ih96j/q295F80DEkb3j7t7zXLq4iKwhzX3OeQViixz79mp/yB bDauYFxE/J1gekJmdBKIOrDPhRam8nkHcYX1VHvL5VRn0wX6mCU8PkEQb48iZ26YpfVt nN10F0SI2MZ97oEfO2dXDS9yFiUPBFmQ4sygP5BJylgIZWxkz4auPev0bkYa79kO62jp hgjg== X-Gm-Message-State: AOAM530cfS6f4Z0UDa3HoHxaEYpIj/wm1Bqe6QQhtVlzNTEchXzX9gLu Kn319qKGPeuhxUn+tuGabq5RWtiP2sJuMSLoSTHnhi17cZmjNEEJb9hQtzqX3wvKGw5MfZVZN3n pqkEXezTlL/I= X-Received: by 2002:a05:6402:2792:: with SMTP id b18mr21240759ede.344.1643650140105; Mon, 31 Jan 2022 09:29:00 -0800 (PST) X-Google-Smtp-Source: ABdhPJzi94WA2rS/qDB0W0YsfdD1WddtX+9lkk6sfJvSyIjvPlxzWeqvF5AEGF0afGHWy3SDj8xJrw== X-Received: by 2002:a05:6402:2792:: with SMTP id b18mr21240741ede.344.1643650139822; Mon, 31 Jan 2022 09:28:59 -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 i12sm13339486ejy.43.2022.01.31.09.28.59 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 31 Jan 2022 09:28:59 -0800 (PST) Message-ID: <9d4ecc6f-f885-5cc9-9786-bcc42b979585@redhat.com> Date: Mon, 31 Jan 2022 18:28:58 +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: Nadav Amit , Mike Rapoport Cc: Mike Rapoport , Andrea Arcangeli , Peter Xu , Linux-MM References: <18B50289-223E-4C78-B2D6-8E9F0B9E2387@gmail.com> From: David Hildenbrand Organization: Red Hat In-Reply-To: <18B50289-223E-4C78-B2D6-8E9F0B9E2387@gmail.com> X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 X-Rspam-User: nil X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 4B54440002 X-Stat-Signature: yaarxn779mxgr7tz8ey5h4d9584xg183 Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=redhat.com header.s=mimecast20190719 header.b=eUahWFOG; spf=none (imf07.hostedemail.com: domain of david@redhat.com has no SPF policy when checking 170.10.133.124) smtp.mailfrom=david@redhat.com; dmarc=pass (policy=none) header.from=redhat.com X-HE-Tag: 1643650143-544110 Content-Transfer-Encoding: quoted-printable 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 18:23, Nadav Amit wrote: >=20 >> On Jan 31, 2022, at 2:42 AM, 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 usa= bility >>> issue that complicates userspace UFFD-monitor implementation. I obvio= suly >>> 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 migh= t not >>> reflect the actual order in which events took place. >>> >>> In more detail, UFFD events (e.g., unmap, fork) are not ordered again= st >>> themselves [*]. The mm-lock is dropped before notifying the userspace >>> UFFD-monitor, and therefore there is no guarantee as to whether the o= rder 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. I= n >>> this case the UFFD-monitor cannot figure from the events whether the >>> child process has the unmapped memory range still mapped (because for= k >>> 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! >=20 > 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-fault= s > are reported before events. Of course, for the issue I brought up (if it's a real issue), the question is if we could "adjust the documentation" to state that there are no ordering guarantees. IMHO at least the fork()+munmap() needs a proper fix, because otherwise, we might really end up with an API that's partially useless -- as you correctly state. >=20 > 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. >=20 > 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. >=20 My first thought was to include a timestamp. But requiring user space to restore the order based on a timestamp might be really ... weird. --=20 Thanks, David / dhildenb