From patchwork Tue Nov 21 17:16:33 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13463392 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 1D47FC61D90 for ; Tue, 21 Nov 2023 17:16:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B00296B04C1; Tue, 21 Nov 2023 12:16:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id AAEB46B04C3; Tue, 21 Nov 2023 12:16:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 951706B04C4; Tue, 21 Nov 2023 12:16:51 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 81C276B04C1 for ; Tue, 21 Nov 2023 12:16:51 -0500 (EST) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 53BCF1203AA for ; Tue, 21 Nov 2023 17:16:51 +0000 (UTC) X-FDA: 81482616222.06.94CC5A9 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf16.hostedemail.com (Postfix) with ESMTP id 4752F180024 for ; Tue, 21 Nov 2023 17:16:48 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=kAMw1Hip; spf=pass (imf16.hostedemail.com: domain of 3_-VcZQYKCHAgifSbPUccUZS.QcaZWbil-aaYjOQY.cfU@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3_-VcZQYKCHAgifSbPUccUZS.QcaZWbil-aaYjOQY.cfU@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700587008; a=rsa-sha256; cv=none; b=f5ZDan+nwP0Tz6CmjmTQZFO6lsoDhX/KcnmAuDaCxPvOwohl3mrbho4W1ke+o1PeDP7Bzj vi76l2JwbwqIdv3/CxjztLd6AvnkhGIHi/akkNT6Bc9/7yPo2dShfv6gBRRss9G9bAIvhy Lrsryy7CaH6yyXOAhQh+6GUh+3EQEks= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=kAMw1Hip; spf=pass (imf16.hostedemail.com: domain of 3_-VcZQYKCHAgifSbPUccUZS.QcaZWbil-aaYjOQY.cfU@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3_-VcZQYKCHAgifSbPUccUZS.QcaZWbil-aaYjOQY.cfU@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1700587008; h=from:from:sender: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: references:dkim-signature; bh=jM7XBxZIdIQFnI3Em7K4YQrm7oT5I43ipyduUgFQ+MA=; b=xmkumkeEP7aww2aFvaKe4HMZlUxT6b37ZVppHwdJyh7dBgsi3wow2aD/PxYYYEu2xGA5xY 8Qp1XeeWZu098R4d8JebVEIs9B74RvUMV5DG53a6I72IZ6HNV6wJ55KvlzWq5TbbUGnWvf nEltuEMeAqmQBJlMYoroUWUICruz7Ig= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-da31ec03186so4533672276.1 for ; Tue, 21 Nov 2023 09:16:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1700587007; x=1701191807; darn=kvack.org; h=content-transfer-encoding:cc:to:from:subject:message-id :mime-version:date:from:to:cc:subject:date:message-id:reply-to; bh=jM7XBxZIdIQFnI3Em7K4YQrm7oT5I43ipyduUgFQ+MA=; b=kAMw1HipsE4Wsxqz+g2wm2Ui41eKRSpaFE/ygd1lHjY9H82ieefBBp27BFY71jLdTy 8fEah+MUNf/wnCT0HDl0B5IcaJIGOuEpJBQb9ZCvF8jRx7DgKVLT4aOvAjr1mq5R23Hi EN0wTdOsUhP/MXIhbaYh7OI2lo7tmKGv9Fe4PTfp/1bKVVc3UfdjbZPmbvCCDImzjR96 9KFrVjEsILDAhku2OUCMOFOdV675QW0anUB0MGdtY9slzGw5TSRSnkB84KkRaKM4mDFt yPgLsTqeo2sm/OiWXg+UNgjC7g7FpXYTxcqByRf89/+POW9s1I+sd8PbfW3u8Sc/c6mx vg+w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700587007; x=1701191807; h=content-transfer-encoding:cc:to:from:subject:message-id :mime-version:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=jM7XBxZIdIQFnI3Em7K4YQrm7oT5I43ipyduUgFQ+MA=; b=IyzbBkj7M6+l9Fdc7kv2UlVl/dls1MqJn4HCAp0sTbSPkhJwO0UkvS2KQZIaPyqiTc zhjgfb+H1CnJ9z3fklRGI5gva92fP/W8fnFMc78CJbBEIkQ6LT/w0wteKtnH5/qaQEb5 lXzUyjNR6SNFiDEiATS2jyv+aqVffu3OEH4WGr7hqtqNd8NRBWA2TRO/WWsS5rXYUAIe MWS08JENYLtRXwh4oXzNPE7j6wYl4IkBSRiJ+/K7t7FA5k3t0FAfUqKjuhPcvwKvp3Rs of9wo/CCazSlbNilJ37z+wOy2e8DlBfMV/+JiuP+8mN0vRyJX6MOq4Kw9pQbLejITE7I Eslw== X-Gm-Message-State: AOJu0YzPrl2iSlLiIFbvKRRt22ZiewEIvpEySkup89VRzApx+gvaIDjs Kw994TOUkAsr5QNvgUpFMuKpYJKA8/c= X-Google-Smtp-Source: AGHT+IFqboCvwPHQtX7cCJPOYR6+OPn7Jj/tG7no88TpQkFNSfK4t9ha8o0yTwTy7v2M2vtmHrTy83rrpPo= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:2045:f6d2:f01d:3fff]) (user=surenb job=sendgmr) by 2002:a25:4a44:0:b0:d89:42d7:e72d with SMTP id x65-20020a254a44000000b00d8942d7e72dmr88654yba.3.1700587007306; Tue, 21 Nov 2023 09:16:47 -0800 (PST) Date: Tue, 21 Nov 2023 09:16:33 -0800 Mime-Version: 1.0 X-Mailer: git-send-email 2.43.0.rc1.413.gea7ed67945-goog Message-ID: <20231121171643.3719880-1-surenb@google.com> Subject: [PATCH v5 0/5] userfaultfd move option From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: viro@zeniv.linux.org.uk, brauner@kernel.org, shuah@kernel.org, aarcange@redhat.com, lokeshgidra@google.com, peterx@redhat.com, david@redhat.com, hughd@google.com, mhocko@suse.com, axelrasmussen@google.com, rppt@kernel.org, willy@infradead.org, Liam.Howlett@oracle.com, jannh@google.com, zhangpeng362@huawei.com, bgeffon@google.com, kaleshsingh@google.com, ngeoffray@google.com, jdduke@google.com, surenb@google.com, linux-mm@kvack.org, linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-kselftest@vger.kernel.org, kernel-team@android.com X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 4752F180024 X-Stat-Signature: 1em7jsbueht996u4gkjignjfau7hbbyn X-Rspam-User: X-HE-Tag: 1700587008-228135 X-HE-Meta: U2FsdGVkX18CnXZVAGWewFq4plkHTdpsCcPCFSqg9VNFJgSYjUR/9dRskOOCjp1l8P03xC3k4frPhA8YGZPtMDCuAXzAZHkZNd/0fjX1Gcmp78HyDm0+tXl8e6HNYLNp4MNUT+nGqf05BjnyPaKLj9Scx6AQMK9vTLww6moAQf4++NfvZm/plgSTnq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lcGPZZHpqFdpW5qELGgAPZYG5GlS92T4CqjHZB8gIn447OBAIbStvC/YltvIFPn26k6PRSA7ARsKh7DQLoqf6N+P4grfCW9Q1JwWBrS6L3N6gMVey4b7U/EwXM6trqg4OFh/pM9oqQQUkJI14Z7s4zGy6UW1v/qxp4EAQTk4hieoN5cwJ3AqGPxBT1fCASLhgq0prxdRNj0y+UWVznYBpwcaSCQFfAYaL8mkYR9NTT+vmHlQ08xZWUonk3n7X75Tc61OIFialw6Wnew9mX106eD4k4ISZzYMy4APO8PLHQ7uN9E5gNjT0kdGUqSgDINsCB2GGswiqLBhJXYXN7hn3Fbm4Z95FccBbDWxd8Pk9fSyDd+X/8gnJsKYlGt5Hpx0chQK+JhTOA6ogGZHSk65ZFD+uv1fMV7GtQ6khzs31T3zHSahARJJwCatg43r+VNgZEENOtpVx5rkpTkJwRqzBTy6hLR/4Frgte4Z8g= 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: List-Subscribe: List-Unsubscribe: This patch series introduces UFFDIO_MOVE feature to userfaultfd, which has long been implemented and maintained by Andrea in his local tree [1], but was not upstreamed due to lack of use cases where this approach would be better than allocating a new page and copying the contents. Previous upstraming attempts could be found at [6] and [7]. UFFDIO_COPY performs ~20% better than UFFDIO_MOVE when the application needs pages to be allocated [2]. However, with UFFDIO_MOVE, if pages are available (in userspace) for recycling, as is usually the case in heap compaction algorithms, then we can avoid the page allocation and memcpy (done by UFFDIO_COPY). Also, since the pages are recycled in the userspace, we avoid the need to release (via madvise) the pages back to the kernel [3]. We see over 40% reduction (on a Google pixel 6 device) in the compacting thread’s completion time by using UFFDIO_MOVE vs. UFFDIO_COPY. This was measured using a benchmark that emulates a heap compaction implementation using userfaultfd (to allow concurrent accesses by application threads). More details of the usecase are explained in [3]. Furthermore, UFFDIO_MOVE enables moving swapped-out pages without touching them within the same vma. Today, it can only be done by mremap, however it forces splitting the vma. TODOs for follow-up improvements: - cross-mm support. Known differences from single-mm and missing pieces: - memcg recharging (might need to isolate pages in the process) - mm counters - cross-mm deposit table moves - cross-mm test - document the address space where src and dest reside in struct uffdio_move - TLB flush batching. Will require extensive changes to PTL locking in move_pages_pte(). OTOH that might let us reuse parts of mremap code. Changes since v4 [9]: - added Acked-by in patch 1, per Peter Xu - added description for ctx, mm and mode parameters of move_pages(), per kernel test robot - added Reviewed-by's, per Peter Xu and Axel Rasmussen - removed unused operations in uffd_test_case_ops - refactored uffd-unit-test changes to avoid using global variables and handle pmd moves without page size overrides, per Peter Xu Changes since v3 [8]: - changed retry path in folio_lock_anon_vma_read() to unlock and then relock RCU, per Peter Xu - removed cross-mm support from initial patchset, per David Hildenbrand - replaced BUG_ONs with VM_WARN_ON or WARN_ON_ONCE, per David Hildenbrand - added missing cache flushing, per Lokesh Gidra and Peter Xu - updated manpage text in the patch description, per Peter Xu - renamed internal functions from "remap" to "move", per Peter Xu - added mmap_changing check after taking mmap_lock, per Peter Xu - changed uffd context check to ensure dst_mm is registered onto uffd we are operating on, Peter Xu and David Hildenbrand - changed to non-maybe variants of maybe*_mkwrite(), per David Hildenbrand - fixed warning for CONFIG_TRANSPARENT_HUGEPAGE=n, per kernel test robot - comments cleanup, per David Hildenbrand and Peter Xu - checks for VM_IO,VM_PFNMAP,VM_HUGETLB,..., per David Hildenbrand - prevent moving pinned pages, per Peter Xu - changed uffd tests to call move uffd_test_ctx_clear() at the end of the test run instead of in the beginning of the next run - added support for testcase-specific ops - added test for moving PMD-aligned blocks Changes since v2 [5]: - renamed UFFDIO_REMAP to UFFDIO_MOVE, per David Hildenbrand - rebase over mm-unstable to use folio_move_anon_rmap(), per David Hildenbrand - added text for manpage explaining DONTFORK and KSM requirements for this feature, per David Hildenbrand - check for anon_vma changes in the fast path of folio_lock_anon_vma_read, per Peter Xu - updated the title and description of the first patch, per David Hildenbrand - updating comments in folio_lock_anon_vma_read() explaining the need for anon_vma checks, per David Hildenbrand - changed all mapcount checks to PageAnonExclusive, per Jann Horn and David Hildenbrand - changed counters in remap_swap_pte() from MM_ANONPAGES to MM_SWAPENTS, per Jann Horn - added a check for PTE change after folio is locked in remap_pages_pte(), per Jann Horn - added handling of PMD migration entries and bailout when pmd_devmap(), per Jann Horn - added checks to ensure both src and dst VMAs are writable, per Peter Xu - added UFFD_FEATURE_MOVE, per Peter Xu - removed obsolete comments, per Peter Xu - renamed remap_anon_pte to remap_present_pte, per Peter Xu - added a comment for folio_get_anon_vma() explaining the need for anon_vma checks, per Peter Xu - changed error handling in remap_pages() to make it more clear, per Peter Xu - changed EFAULT to EAGAIN to retry when a hugepage appears or disappears from under us, per Peter Xu - added links to previous upstreaming attempts, per David Hildenbrand Changes since v1 [4]: - add mmget_not_zero in userfaultfd_remap, per Jann Horn - removed extern from function definitions, per Matthew Wilcox - converted to folios in remap_pages_huge_pmd, per Matthew Wilcox - use PageAnonExclusive in remap_pages_huge_pmd, per David Hildenbrand - handle pgtable transfers between MMs, per Jann Horn - ignore concurrent A/D pte bit changes, per Jann Horn - split functions into smaller units, per David Hildenbrand - test for folio_test_large in remap_anon_pte, per Matthew Wilcox - use pte_swp_exclusive for swapcount check, per David Hildenbrand - eliminated use of mmu_notifier_invalidate_range_start_nonblock, per Jann Horn - simplified THP alignment checks, per Jann Horn - refactored the loop inside remap_pages, per Jann Horn - additional clarifying comments, per Jann Horn Main changes since Andrea's last version [1]: - Trivial translations from page to folio, mmap_sem to mmap_lock - Replace pmd_trans_unstable() with pte_offset_map_nolock() and handle its possible failure - Move pte mapping into remap_pages_pte to allow for retries when source page or anon_vma is contended. Since pte_offset_map_nolock() start RCU read section, we can't block anymore after mapping a pte, so have to unmap the ptesm do the locking and retry. - Add and use anon_vma_trylock_write() to avoid blocking while in RCU read section. - Accommodate changes in mmu_notifier_range_init() API, switch to mmu_notifier_invalidate_range_start_nonblock() to avoid blocking while in RCU read section. - Open-code now removed __swp_swapcount() - Replace pmd_read_atomic() with pmdp_get_lockless() - Add new selftest for UFFDIO_MOVE [1] https://gitlab.com/aarcange/aa/-/commit/2aec7aea56b10438a3881a20a411aa4b1fc19e92 [2] https://lore.kernel.org/all/1425575884-2574-1-git-send-email-aarcange@redhat.com/ [3] https://lore.kernel.org/linux-mm/CA+EESO4uO84SSnBhArH4HvLNhaUQ5nZKNKXqxRCyjniNVjp0Aw@mail.gmail.com/ [4] https://lore.kernel.org/all/20230914152620.2743033-1-surenb@google.com/ [5] https://lore.kernel.org/all/20230923013148.1390521-1-surenb@google.com/ [6] https://lore.kernel.org/all/1425575884-2574-21-git-send-email-aarcange@redhat.com/ [7] https://lore.kernel.org/all/cover.1547251023.git.blake.caldwell@colorado.edu/ [8] https://lore.kernel.org/all/20231009064230.2952396-1-surenb@google.com/ [9] https://lore.kernel.org/all/20231028003819.652322-1-surenb@google.com/ Andrea Arcangeli (2): mm/rmap: support move to different root anon_vma in folio_move_anon_rmap() userfaultfd: UFFDIO_MOVE uABI Suren Baghdasaryan (3): selftests/mm: call uffd_test_ctx_clear at the end of the test selftests/mm: add uffd_test_case_ops to allow test case-specific operations selftests/mm: add UFFDIO_MOVE ioctl test Documentation/admin-guide/mm/userfaultfd.rst | 3 + fs/userfaultfd.c | 72 +++ include/linux/rmap.h | 5 + include/linux/userfaultfd_k.h | 11 + include/uapi/linux/userfaultfd.h | 29 +- mm/huge_memory.c | 122 ++++ mm/khugepaged.c | 3 + mm/rmap.c | 30 + mm/userfaultfd.c | 599 +++++++++++++++++++ tools/testing/selftests/mm/uffd-common.c | 39 +- tools/testing/selftests/mm/uffd-common.h | 9 + tools/testing/selftests/mm/uffd-stress.c | 5 +- tools/testing/selftests/mm/uffd-unit-tests.c | 192 ++++++ 13 files changed, 1115 insertions(+), 4 deletions(-)