From patchwork Mon Oct 9 06:42:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13412982 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 4EDA9E95A8D for ; Mon, 9 Oct 2023 06:42:38 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AFBED6B0198; Mon, 9 Oct 2023 02:42:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AABBB6B019A; Mon, 9 Oct 2023 02:42:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9C1B16B019C; Mon, 9 Oct 2023 02:42:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 8DA796B0198 for ; Mon, 9 Oct 2023 02:42:37 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 414C9A0160 for ; Mon, 9 Oct 2023 06:42:37 +0000 (UTC) X-FDA: 81324979554.07.5A99293 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf03.hostedemail.com (Postfix) with ESMTP id 77B482000E for ; Mon, 9 Oct 2023 06:42:35 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=sDkZlkbv; spf=pass (imf03.hostedemail.com: domain of 32qAjZQYKCNkNPM9I6BJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=32qAjZQYKCNkNPM9I6BJJBG9.7JHGDIPS-HHFQ57F.JMB@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=1696833755; 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=7zUUB2am9n9CKFAnT6nZ8XawbgT1VoZ44ZKp10n60qQ=; b=PZfx2uUTTRImB53vkl4TYotzc2j9t/Rc+sa4MM/G7Hhr6gcrYGnJfbWDHm7xldodlWTxt+ IXVlYBPTD/5HIIuYcZsJMZE8l79LElZ3fC/VGb6Vh/LmBe+kdzFVNHw8I++T2u+/SQ6Hpk MjO/iXD5VfYWB5zHrMUksd0GPaXDGhI= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1696833755; a=rsa-sha256; cv=none; b=eDOdKUbVvvzzFj9mWkem1uEfJcXd3j40ttci2Ht0IW2ZAAnL4x6CGXMTL99dAzLD4g7sw8 kj7DczySH8ZriIqE1J97YnTrw/dKQs0vi0F5yZhOHcIybbckan1AwvnK6vFIzpyHx4DSi7 wDMaf9jq5fk1Pcsxt5fRrGnLqF7EiNs= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=sDkZlkbv; spf=pass (imf03.hostedemail.com: domain of 32qAjZQYKCNkNPM9I6BJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=32qAjZQYKCNkNPM9I6BJJBG9.7JHGDIPS-HHFQ57F.JMB@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-d85fc108f0eso5706061276.2 for ; Sun, 08 Oct 2023 23:42:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1696833754; x=1697438554; 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=7zUUB2am9n9CKFAnT6nZ8XawbgT1VoZ44ZKp10n60qQ=; b=sDkZlkbvVScZrzrznWeqQcL1x9y+cIW/K20B5sDqdqucwDgBi2TCSFghssGAyVoUgW O4V06KHrxkVVRmU21r2/hN4fnmpgAMIMdbwRHLESDVP1tsilSfe3IRkKMauheWPOFh3o E6hLbxWbKkhmawQo+ymotIKFdzsJn71uytpnFeimVLMiIRwJwjGjGBhtbLDOQLdYqm8E gfQy+pDLgDENC+hg9+i0gLa+186NBhKByXjdMOtAiubJj21dJ2jEBq27QEfH0l/IdVSt pbYGdbxRdxm0snwA7tTwD67q5aaIWYnxAO6QjlkCNp7L7rXQxpO+Gf83h0m8ujiEKWxF 4R+A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1696833754; x=1697438554; 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=7zUUB2am9n9CKFAnT6nZ8XawbgT1VoZ44ZKp10n60qQ=; b=QxE1K60x8LizBfY2uPKv27IOeWaNdZ1UtHBDOG1zHhMIWUV2CmENE+0N5WiaK1SU9T 2tcB3m05iK/C+kMR3O6fovNef4OUDNcvm53fIVtceCD7DwIbD0B9at1KiNUKwtwMgWQw NrtgPoz1TOOh4qx2HrdOOxKJU5L8VRw1YRpyEYh15Ih6anT9U8BoVBcHvG+zJP1AM/H2 COoJrQe6REGM+Fhear5m4BFykDODRehYPfS5msTS/Pn5EhOF7WJOWZh3rp+0URHRvkfs aO3r4IEi9ZSMYDF7tJG5xlCnMYZKicMPs70+OLNT/So5LAfjezq7ykmYxzAvQukqJQgq p62w== X-Gm-Message-State: AOJu0YxYaWmcLWH9gU0Pw1hIQSIY47iumFEmwzn7h9nBaVLmVSkUMRUd 9bKWnJYV5QusLzleSvxPeRYKLJmaThA= X-Google-Smtp-Source: AGHT+IFgAg9kuGpXCr3YUWTFgYRcSR2EjJOWKwvSOMrijuF0wKr/70tmErg8XwfV2RFmODsMhMgBsAEZSjM= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:3e83:601e:dc42:a455]) (user=surenb job=sendgmr) by 2002:a25:ad50:0:b0:d78:2c3:e633 with SMTP id l16-20020a25ad50000000b00d7802c3e633mr204458ybe.2.1696833754494; Sun, 08 Oct 2023 23:42:34 -0700 (PDT) Date: Sun, 8 Oct 2023 23:42:25 -0700 Mime-Version: 1.0 X-Mailer: git-send-email 2.42.0.609.gbb76f46606-goog Message-ID: <20231009064230.2952396-1-surenb@google.com> Subject: [PATCH v3 0/3] 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-Stat-Signature: ikhrrynotnsyjs7b4otx7bo5ry4a71cq X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 77B482000E X-Rspam-User: X-HE-Tag: 1696833755-895144 X-HE-Meta: U2FsdGVkX18sO/0QP6V06iN0kTQGSFztt8xuNksaYReon03LwFGVhYPxQW2Ff7rEcu1MMWsGlClD0PyoB6grLtpQtdR80USa+ayERQ2YMax7EeGAU7SoLhmnCoFwTFJ5Fc2H6o7BOXvc2bMW8MJx8jMuajaENiRcxkhRGU/9ug5HJ7LwmwUj7U43LpieA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aknhxy47dT8TjMfj0r3E4VN8Bxyr/4HSZy22GH+XcIF+V8akAYeRrRvbq9E4/7Udae5Um7XW8kKD54LCUAd8fnh2MIIJ+wX8NdCEiMXopwKQ+aH/bt/Nn5ndtDoe4FGQT9nE9XJbNg3MdkAZCdhjR4so+HwZkSqiZ9dAcF87f+A8yV94n6VLwAnUIhemQm5inFZzWdUABtDpkujxs4Z3JOgezBanECPsPlMlLJcHT6y5kqmAZgZqjT/r97AyfJClmnOJuNjqDluCXz9vRrkqQQim95KFJgR3bLGassXKStbNbZJi0h6w19jO9Zu1sCZOZS/VWHdTs5aMUqbxDYYOmViDbkuDTKmIvwO4jA5sTlOrB/UY3mLs2oRL8W8UOFI8eGKJ43j4ZtLsr1QyOXxTsZoaNVFw0XJEWdU5BmoIXQsmzBFascjoQLlOSmXQKi5H0UsoG4Q8hzhHnHdq+PBzyISAQo= 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: 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. 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 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 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 [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/ 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 (1): selftests/mm: add UFFDIO_MOVE ioctl test Documentation/admin-guide/mm/userfaultfd.rst | 3 + fs/userfaultfd.c | 63 ++ include/linux/rmap.h | 5 + include/linux/userfaultfd_k.h | 12 + include/uapi/linux/userfaultfd.h | 29 +- mm/huge_memory.c | 138 +++++ mm/khugepaged.c | 3 + mm/rmap.c | 30 + mm/userfaultfd.c | 602 +++++++++++++++++++ tools/testing/selftests/mm/uffd-common.c | 41 +- tools/testing/selftests/mm/uffd-common.h | 1 + tools/testing/selftests/mm/uffd-unit-tests.c | 62 ++ 12 files changed, 986 insertions(+), 3 deletions(-)