Received: by 2002:a05:7412:a9a2:b0:e2:908c:2ebd with SMTP id o34csp1508655rdh; Fri, 27 Oct 2023 17:38:53 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEyKIbkakDz/J+n3d+Nkfz3pEQD4YI8yMG+iLhqv/KYR0NEyC8BKb/r553PCOU8efzM6cX7 X-Received: by 2002:a05:6358:903:b0:168:dc03:5b90 with SMTP id r3-20020a056358090300b00168dc035b90mr5721251rwi.23.1698453533411; Fri, 27 Oct 2023 17:38:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698453533; cv=none; d=google.com; s=arc-20160816; b=kY3TDhUyLxuHDdLATmrntwPxprBnhyQwdLFX3+k1Neomnklu1oX0LHSr99tqW6rby+ WmHwbNeBCVFXdaWAIe21g/KHCCy9fKVXp2H/vt1kEe5dOiUCYgrjvwZjEUpx8lh//zZG zETSh4Cwi0ofSjW9FZxFyW7X4CQQ7i/qGHd9DcvZozbP+nnrVgNKvUus9PiWslU2Wp7y FBKQEKlnXIcighdN34IvbkDYbuGtET9+OpdUVlNZvnESI2SldsnvRD41HjvtvYtKayOP 1YmxKSivIx4narAFF3tp23ruRuCR8CsG8IgRxG6Th5JO8C2zAH+vD6RSpeUJKCCsUQw/ Iqjw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:from:subject :message-id:mime-version:date:dkim-signature; bh=K0U9f0GPnYV6HySM//pU94NgpKMw+FvA1LiNkKj/lR8=; fh=vBLx0KH+vftfPuCKolEKY+MKbAkCpwC5WBbaBlSuTXE=; b=yAw5gLeXjL/04PwYRYgdnojqFraeYfQQD0vrxnyA1Ehb7G5btNMXmcfemX6d7PJdBD evCbQAhujTpB8g41XmXQ70Tp0XtEOSYvnYwVlzGb6+fi/3no1IDrtVAghkliT8ax/c2E dokJxxx3j/KcHK573FoCyQUMkaguSz1pcs8xLMfkXR0a6lU1784cOVJeGBjJO4R2Wwfx AiXlBKqVaNkNLtSxElKilqvtKNo9DIl5qysDHqVfgBqlzjN2/U5x0XozrJSi7H1+PhqF xNvSlg/vG8hkPcYnB9sQ/mxFBcl6PbxvkqqzEkczPAZwbbWOgLrGCTrsnEnc/E1GDbJL hiBQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=EHIYjWzb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [2620:137:e000::3:3]) by mx.google.com with ESMTPS id j12-20020a63e74c000000b00578666614f1si1754759pgk.63.2023.10.27.17.38.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Oct 2023 17:38:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) client-ip=2620:137:e000::3:3; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20230601 header.b=EHIYjWzb; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::3:3 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id 0231082A8019; Fri, 27 Oct 2023 17:38:48 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232896AbjJ1Aib (ORCPT + 99 others); Fri, 27 Oct 2023 20:38:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42408 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231444AbjJ1Ai2 (ORCPT ); Fri, 27 Oct 2023 20:38:28 -0400 Received: from mail-yw1-x114a.google.com (mail-yw1-x114a.google.com [IPv6:2607:f8b0:4864:20::114a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id CE1041B9 for ; Fri, 27 Oct 2023 17:38:23 -0700 (PDT) Received: by mail-yw1-x114a.google.com with SMTP id 00721157ae682-5ae5b12227fso23577857b3.0 for ; Fri, 27 Oct 2023 17:38:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1698453503; x=1699058303; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:from:subject:message-id :mime-version:date:from:to:cc:subject:date:message-id:reply-to; bh=K0U9f0GPnYV6HySM//pU94NgpKMw+FvA1LiNkKj/lR8=; b=EHIYjWzbYSU6ctz50ItDaCybvJ62FKZojKJZn85HTwmmEp3tpW/+rGRo2MNXNC98Pu hh/PD33x9nftZq/lk9RwnsPsdwN+TeG8ggPrWxzSMPAXF1+SxcgmHWKXEBIP6H0n28Wf t4AqOIFAE6ABBu0wGscy1DC4JK4tokB0ryUivqmSYTjMc0h248buSyDbGB4WwbtttyBC /1vE6HpWUMQedBfwp7wYBybcDp6/vvCSEhMa7Cp+QTrarWBql+uzocklE7SOI6DuCqNQ ganZC/gOcyocj6f01LqvgXSPrXOyP9kqoVQo4wS0gZ4kq9aCmyofAu/84IQvxgvsumqN Hkig== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698453503; x=1699058303; 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=K0U9f0GPnYV6HySM//pU94NgpKMw+FvA1LiNkKj/lR8=; b=roAnckowdDAs8VmzcHaS7Lr6z5/8PQoxuKMK81pWWtUbAPtPN76MF19M9qD0oQSqIj frqGJqbal3EAsb8hKozfRidx3KrNKPP+joWeeIaXDGMlwcGW9mKPIz2mD+uYI3bko+9T Vr8jFW3+9QMwARPo4vHMj8DaUNHBhlhV5dJwZepfpczXo2jPgwBY00lF46MwzB8DoBgk Fnl4K5yvI0rER9bAoyCj5qUEu3Dqi+dbSzwY7wpeZLAsg8H7Ony2In2LcJObioYjbsop YtAdjL60/0Us42PJGrkLl5mXxLFKcjQEOw8NA3VDUZ2oLH87fx80dAr29JCDI3x1QnVC go1A== X-Gm-Message-State: AOJu0Yw2fJ1cxO3tnIlYOF5vEbecZGYWlN3Xb/9bDxPBabzM9DAn9P3s NMYnWmBkVgb2KGZoicU8loCrbIShVoY= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:cba3:7e7f:79e4:fa57]) (user=surenb job=sendgmr) by 2002:a81:92c5:0:b0:5a7:af69:a279 with SMTP id j188-20020a8192c5000000b005a7af69a279mr98223ywg.9.1698453503001; Fri, 27 Oct 2023 17:38:23 -0700 (PDT) Date: Fri, 27 Oct 2023 17:38:10 -0700 Mime-Version: 1.0 X-Mailer: git-send-email 2.42.0.820.g83a721a137-goog Message-ID: <20231028003819.652322-1-surenb@google.com> Subject: [PATCH v4 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-8.4 required=5.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Fri, 27 Oct 2023 17:38:48 -0700 (PDT) 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=E2=80=99s 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 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=3Dn, 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/2aec7aea56b10438a3881a20a411aa4= b1fc19e92 [2] https://lore.kernel.org/all/1425575884-2574-1-git-send-email-aarcange@r= edhat.com/ [3] https://lore.kernel.org/linux-mm/CA+EESO4uO84SSnBhArH4HvLNhaUQ5nZKNKXqx= RCyjniNVjp0Aw@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@colorad= o.edu/ [8] https://lore.kernel.org/all/20231009064230.2952396-1-surenb@google.com/ The patchset applies over mm-unstable. 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 | 596 +++++++++++++++++++ tools/testing/selftests/mm/uffd-common.c | 51 +- tools/testing/selftests/mm/uffd-common.h | 11 + tools/testing/selftests/mm/uffd-stress.c | 5 +- tools/testing/selftests/mm/uffd-unit-tests.c | 144 +++++ 13 files changed, 1078 insertions(+), 4 deletions(-) --=20 2.42.0.820.g83a721a137-goog