From patchwork Sat Sep 23 01:31:43 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13396496 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 0CE36CE7A8A for ; Sat, 23 Sep 2023 01:31:55 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7B8736B0289; Fri, 22 Sep 2023 21:31:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 769076B028F; Fri, 22 Sep 2023 21:31:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 657EF6B02A9; Fri, 22 Sep 2023 21:31:55 -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 526C96B0289 for ; Fri, 22 Sep 2023 21:31:55 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id B7522C032D for ; Sat, 23 Sep 2023 01:31:54 +0000 (UTC) X-FDA: 81266135748.07.EE12A12 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf16.hostedemail.com (Postfix) with ESMTP id 006A8180003 for ; Sat, 23 Sep 2023 01:31:52 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Hl4oZwqj; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 3B0AOZQYKCO4ikhUdRWeeWbU.SecbYdkn-ccalQSa.ehW@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3B0AOZQYKCO4ikhUdRWeeWbU.SecbYdkn-ccalQSa.ehW@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1695432713; 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=KbsGHzMZ4WdaKCliylW7h6jhLyquTlpj2AXcxZt+2PU=; b=GVfXMdYvkREtofkmXkeLDSZwnr2ZD9UPNptFU5NLUnQuRf+Ly5ykiIMzVn54xuwGGmb9+/ vqCE6TpmgqeBB87djtCiGfwaIrASYw/9GhbC7bGevvfogRT6L04llOPgFZXtj0xKRpuklc DpbHz4lhF0gNNtM573zRDArdlAoebJs= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Hl4oZwqj; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 3B0AOZQYKCO4ikhUdRWeeWbU.SecbYdkn-ccalQSa.ehW@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3B0AOZQYKCO4ikhUdRWeeWbU.SecbYdkn-ccalQSa.ehW@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1695432713; a=rsa-sha256; cv=none; b=QJgM45KSNO+TcEtlyDnuUYe/XSL0jLoAN4u45azLp9mzxYEjmyNeryRlcDsn91lnjLizVj GAq9yPIM2jmaU9crxpdUGAoL6lOxhZ8XXukXDzYWDlDjJuPmHs4piymgV/GWtIicjXsK4J fJu/pN0uz4dWTZ5wKUtb1jiX4zr3Vno= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-597f461adc5so49169607b3.1 for ; Fri, 22 Sep 2023 18:31:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1695432712; x=1696037512; 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=KbsGHzMZ4WdaKCliylW7h6jhLyquTlpj2AXcxZt+2PU=; b=Hl4oZwqjstS0B8WrI6qTJFO/R1QhHagHxKtgVqTU9cNUjRQbixD+a5fQaUIG5E24pA Q5aYyUlgVfrJb4l76C65+5ak3mjgHZ46Iv24QsWceVaBbJ7D1Q/P6vobo68EuoKDuzLE 1/OByYfwCzrnMN0nA87pxpRstGlLknGKuv/rSs+WcbGndzWt+GJyY14WWl7QoUGrFpZ/ GRDpMi2H/iJdJllKVJuHK1E0Y5UrBqzG+pS79suryTFU2enzdjPtGvwI7b65H5zUkDpe Qus4XF+cii4GUweIk+JzBZeRraVgbgZOJ12vJ7xRd/uHfump8gQnFkBTSjfZc7rNpgNg d4Bw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1695432712; x=1696037512; 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=KbsGHzMZ4WdaKCliylW7h6jhLyquTlpj2AXcxZt+2PU=; b=o/B9TbJ80J1pWY7+XBfRkdDPorUr+pN/CeT1aF5CI1zJzBh9xpWxjuMVPtOYROJlhd itG/xVnq86LAMcNN9jruwx4NjelVbbf77FGY0Le7fOTQyLIuhAs036OnSX7qdF36cyrt JEG52ujuMIVgoJ1GwzXO4idF797FXle5yw/HVoZMS4oSFjs2xxdyUs4YKR3xE6QKIl+V lW3VtnvFuXjtKDdYsAQX5DHmdLx/EjBtayEyIhpS6xHWT7u/B2wXXBoKlk0FnQZwC+W6 TG3eoGZCogOyV48T17LvGcmJ7FPFPeaRH8EexMJqH913vtAi4Y2UzFNlRsQe50ZDFTwA aalg== X-Gm-Message-State: AOJu0YzgiOx7XBqwOyyc0O/wxtEoHPJz5gqtWT/ozdrq+itstBj6s/7n E4C4f92awZeVAOzHw2BiFgyAK77d6Pg= X-Google-Smtp-Source: AGHT+IE4ADU4C0Pp+rt39uFleka+iXZpTfhXH7ZHR9+MVLwVc0IPZg1d6xDn1jjOqOJRYkPT+nl5tp2OE6Y= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:3398:6644:ed7f:bec9]) (user=surenb job=sendgmr) by 2002:a25:48f:0:b0:d86:5644:5d12 with SMTP id 137-20020a25048f000000b00d8656445d12mr9916ybe.4.1695432711967; Fri, 22 Sep 2023 18:31:51 -0700 (PDT) Date: Fri, 22 Sep 2023 18:31:43 -0700 Mime-Version: 1.0 X-Mailer: git-send-email 2.42.0.515.g380fc7ccd1-goog Message-ID: <20230923013148.1390521-1-surenb@google.com> Subject: [PATCH v2 0/3] userfaultfd remap 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-Queue-Id: 006A8180003 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: xcg6wdrq4d5cqgw9or3qfpgtqi58kmb1 X-HE-Tag: 1695432712-826848 X-HE-Meta: U2FsdGVkX1/Cr+j/VI8T6uBGNwH5hPesUFthtleQYNpW/SHMQWzcUfQPyRDxBB6nB4xbQlCPwnjzVxNK0BXwbfA/S2RL7xj0fm6qMX/Ke1by0Ek86v6f5P8e+MQnbcirh4EMSAv08dr7EdlYHudJLrVf8dbG61nEmWw8+N1jzcaihbnwo9Ilb641hiWyQvp3iVV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z1WJPVKWrDuiPtdM1YkiR0CNbYaVJIbTf8QVAZzi5e9rGFxwg8lwMfl0cTuHZ4tNj1ZdQPHQVSqmOc/zAsTmmf1U26H8Ft8BQgS3h6BuSOkXij1n9GqF5R2t+8bxWQjHpKwfEAXsfg72sx0oGd09ya7cysKprQ4rYoQQ7OXaRIstYSXomq1dBvuDTrGEskefSBBqiX6C21bIxCRW1Dl5wtt0eQW0impc6qC3z50k8sj0l0Sjf3DWzywVg9z9Gm5ZicW7VdAEYIChGEEK9kiOE5qK6TnFi2Pm4IX+UYpwFaImcg6F/BfGjiS1PnfnEaoORIUbbGSVX3Rfw6nN8lmDoLX45fUJ6X9X2SFZVFxND7P+WP4it73EIm/vF/bCHA2G2st7BIe7tIcUPDZd4E0VddCEZeuBZhzYl/6JKxWoPGY+yKrIjfxWmzHWb/5wU+vM= 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_REMAP 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. UFFDIO_COPY performs ~20% better than UFFDIO_REMAP when the application needs pages to be allocated [2]. However, with UFFDIO_REMAP, 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_REMAP 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_REMAP enables remapping 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_REMAP 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 [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/ Andrea Arcangeli (2): userfaultfd: UFFDIO_REMAP: rmap preparation userfaultfd: UFFDIO_REMAP uABI Suren Baghdasaryan (1): selftests/mm: add UFFDIO_REMAP ioctl test fs/userfaultfd.c | 63 ++ include/linux/rmap.h | 5 + include/linux/userfaultfd_k.h | 12 + include/uapi/linux/userfaultfd.h | 22 + mm/huge_memory.c | 130 ++++ mm/khugepaged.c | 3 + mm/rmap.c | 13 + mm/userfaultfd.c | 590 +++++++++++++++++++ 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 ++ 11 files changed, 940 insertions(+), 2 deletions(-)