From patchwork Thu Sep 14 15:26:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13385624 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 B974AEEAA53 for ; Thu, 14 Sep 2023 15:26:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D29CD6B02BC; Thu, 14 Sep 2023 11:26:27 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CD9DE6B02BE; Thu, 14 Sep 2023 11:26:27 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BA27F6B02C0; Thu, 14 Sep 2023 11:26:27 -0400 (EDT) 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 ABF4B6B02BC for ; Thu, 14 Sep 2023 11:26:27 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 6E4701CABDF for ; Thu, 14 Sep 2023 15:26:27 +0000 (UTC) X-FDA: 81235579614.17.376B202 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf24.hostedemail.com (Postfix) with ESMTP id 9D704180016 for ; Thu, 14 Sep 2023 15:26:25 +0000 (UTC) Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=YkpSUan3; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf24.hostedemail.com: domain of 3ICYDZQYKCKcZbYLUINVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3ICYDZQYKCKcZbYLUINVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1694705185; a=rsa-sha256; cv=none; b=WtwnVkVV9tMr7151pdOFwbxIluZKEmG+7EY4HJ0aszKhs+DB+V9XkxQXLXF73Z7s0JIyx+ LBKW68x+5P8CGpTzWIyLuB8CL1ZLcm9QAfDs//Rj6lRL3LlE47SlSH/T/C5BvrnipKX9dQ 97715Ypn6w5TxyoRdCya1mfPILbPxE0= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=YkpSUan3; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf24.hostedemail.com: domain of 3ICYDZQYKCKcZbYLUINVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3ICYDZQYKCKcZbYLUINVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1694705185; 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=RQOs579jB3ePn++NtGKTs4Zt+LBBSKWi+6qtOEbCDCw=; b=vmc2wt11hOQQFabeXYYSTAVdQcHXXsNRk1C6mfY4aARilog5iA+iZqj+xBeRssnJaz1YZ+ XYnjpPwq41/MBt7pm5BAmehMa2bs0j6HUXMcSK/cm9dFZukBL5P8xbIY6EsqGJt4vqcMkP fStk6OIFVy8n4nJqz8p3X3Mn3kLx5oU= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-59bdac026f7so15815667b3.0 for ; Thu, 14 Sep 2023 08:26:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1694705185; x=1695309985; 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=RQOs579jB3ePn++NtGKTs4Zt+LBBSKWi+6qtOEbCDCw=; b=YkpSUan3PfYlooTvZMhztwIQ6TfgSOjhoQhYmcIbldpm+fy2USSHHOvgxIuGo4m4vS iMb/vDMHYB5VG4UpRzsg/C9/PoWOlCuWkX1vCrpv1eEjSFRr9yHUyfAhbarwVN0mkvmY JX/HWINFIDFE5vTKfXlffHRW/Ue0EWxB/0U/3BYkpZthyNOds+UCJPOjrfCbaGkEW707 Q+v/8Fynj9PmK25XSiu44XQfuzn/s+aRW6WONsWLWNL7QJUk42RCNcBu9asOtwpVAcVy KO7d+ricHLd1iErToaDxZuoZh0IAVz0zilZ2aQ5y0t3wSEohj/jYjy0EvvVPoj0X1a0F 4dPw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1694705185; x=1695309985; 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=RQOs579jB3ePn++NtGKTs4Zt+LBBSKWi+6qtOEbCDCw=; b=gs8plpUxyGOeaccXoAzwelM+3yt28EwXaac8jdYeHMELgk6bgQ8twAgwGWycX9R1qi p+Jpq3d5CMwBCU26v3R9X2fDLYcAGf3nWi4SEqdIsguLb83QqQBddxLxMtOH9tm/4NTJ wou+Jx/UYkOoSHos4aXMrMfzpgSlA0/u1DAU/iPTZnn3cWibmdA5CPtG9793p/F1o9gI RsZXtZjPUGHxDDg/iiZ2KX9VudTOp7qNCjIZGDWCeSnZcvc+iw/laSn5q21g4ZjCEcs7 qg2X7AsRlJ3Z/ZSwXAIwT+6FxAKLJwdmV8fTa9XemsDESMM5ha3wp9G9j6ZkPaH3ErJy IlgQ== X-Gm-Message-State: AOJu0YxwxbtvyDe4YjdnSa8bDqY7gpuerl2s+ECLrH5lIr8/ZbMdygjr dYWHQD2RJRInHxmfcufpwD7am7lRyNo= X-Google-Smtp-Source: AGHT+IHpgG/2V5r0nUrZtDSSc5XIPL/tp2CusRPSGOiITv48aOuJ2ChFrY99OLcqYNplN5whp/meeAEFRXw= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:405c:ba64:810f:5fee]) (user=surenb job=sendgmr) by 2002:a81:fe07:0:b0:59b:e684:3c7a with SMTP id j7-20020a81fe07000000b0059be6843c7amr79006ywn.4.1694705184355; Thu, 14 Sep 2023 08:26:24 -0700 (PDT) Date: Thu, 14 Sep 2023 08:26:10 -0700 Mime-Version: 1.0 X-Mailer: git-send-email 2.42.0.283.g2d96d420d3-goog Message-ID: <20230914152620.2743033-1-surenb@google.com> Subject: [PATCH 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-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 9D704180016 X-Stat-Signature: spsfhj468ooduadgm6qz8k1z4d5e7czd X-HE-Tag: 1694705185-723682 X-HE-Meta: U2FsdGVkX19GkpY6p91W/two9vBK0HI7/ZbhR2FUUnRM29+BfMNav+wVUPoHFSqZWbRRAHM33qH2QitXUwO0FEG+5bld08fkuG0gYnsgWr9nMRgtNhxhzEiAvAWCMk+RrUUy8AWd+3djqjVri/Ea3jUoy8OdznCjEKTqFnUooK5zsr/YcoPp3lylN9r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7029ZvbNCTi4BYIRfEWSQi7PyX3cQJ+Vnib3mFgUjqpbLr5ue/2TJBE8Lj6b6i39jfugHrsPD6p8nVUADVDEmtuUYxUm2z7ooAV/ncToJC9CsSF1eRDJAWXtyogx5bj2glO84cSbyoZy/T+d/fqzSXylEyEHzNpRyNM7rup725rcukUCWaRbWNXuiPpxUpK87X6zhQpjalPNxI9P2RNc0rrg3rhRivH5AlhkYjmecgfmBTm0TfqQWwWEb5tw38FmcU30eeb+VT0f2irB+SzfTF0hqAsE4DOmc029ZGUcCljHHavE7bg3EJKvTBVghI85roA1DUw8bpeShzLGR4fXLGkIEM9jdxR0n1WoSEDKq/Y6WqmV2JrxDugzJGoC0oJUCOu0AYo7aiCDnHNlDzjM1rU5U7zubTFZF32EKs+MdNEuvxxQaHvdxuHrdojdv3n1WzOcwqX6A== 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]: 1. Trivial translations from page to folio, mmap_sem to mmap_lock 2. Replace pmd_trans_unstable() with pte_offset_map_nolock() and handle its possible failure 3. 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. 4. Add and use anon_vma_trylock_write() to avoid blocking while in RCU read section. 5. Accommodate changes in mmu_notifier_range_init() API, switch to mmu_notifier_invalidate_range_start_nonblock() to avoid blocking while in RCU read section. 6. Open-code now removed __swp_swapcount() 7. Replace pmd_read_atomic() with pmdp_get_lockless() 8. Add new selftest for UFFDIO_REMAP [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/ 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 | 49 ++ include/linux/rmap.h | 5 + include/linux/userfaultfd_k.h | 17 + include/uapi/linux/userfaultfd.h | 22 + mm/huge_memory.c | 118 ++++ mm/khugepaged.c | 3 + mm/rmap.c | 13 + mm/userfaultfd.c | 586 +++++++++++++++++++ tools/testing/selftests/mm/uffd-common.c | 34 +- tools/testing/selftests/mm/uffd-common.h | 1 + tools/testing/selftests/mm/uffd-unit-tests.c | 62 ++ 11 files changed, 908 insertions(+), 2 deletions(-)