From patchwork Mon Jan 9 06:45:15 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Muhammad Usama Anjum X-Patchwork-Id: 13093087 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 780D6C5479D for ; Mon, 9 Jan 2023 06:46:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7F7988E0002; Mon, 9 Jan 2023 01:46:13 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7A6728E0001; Mon, 9 Jan 2023 01:46:13 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6473D8E0002; Mon, 9 Jan 2023 01:46:13 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 4E1A08E0001 for ; Mon, 9 Jan 2023 01:46:13 -0500 (EST) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 15AE3AA9EC for ; Mon, 9 Jan 2023 06:46:13 +0000 (UTC) X-FDA: 80334326226.20.17D733D Received: from madras.collabora.co.uk (madras.collabora.co.uk [46.235.227.172]) by imf18.hostedemail.com (Postfix) with ESMTP id 6AA421C0008 for ; Mon, 9 Jan 2023 06:46:11 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=collabora.com header.s=mail header.b=aJm28bNw; spf=pass (imf18.hostedemail.com: domain of usama.anjum@collabora.com designates 46.235.227.172 as permitted sender) smtp.mailfrom=usama.anjum@collabora.com; dmarc=pass (policy=none) header.from=collabora.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1673246771; 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-transfer-encoding:content-transfer-encoding: in-reply-to:references:dkim-signature; bh=ObPT7ZXlrmnMFcboQOHaT292Oqz3Q07Xr+91EMqNDxg=; b=5V3wafa9/BqFm3LTkUcvkvFpx7rmV6+Xa85uqzQINL1NUCxo0MKn00wK6qkSOaMmFkpKWg e0Wj+v4EfxjUY6cGVoDOiAq4xzxQwJ6ZwJndkY4MQzO5/rP7vlTVNfz23TrtTvEUzd3HdU RL8XwT/FBJYbGaYzEy3BPYwkc2SPPMk= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=collabora.com header.s=mail header.b=aJm28bNw; spf=pass (imf18.hostedemail.com: domain of usama.anjum@collabora.com designates 46.235.227.172 as permitted sender) smtp.mailfrom=usama.anjum@collabora.com; dmarc=pass (policy=none) header.from=collabora.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1673246771; a=rsa-sha256; cv=none; b=abnGjEhU7CbZhxTtc170bmPPaEC1wBMUuEpPEAdu2mD0YPr+iuulN7T6SQrjcr1RdNd7gH XmGGVBzmSErk6mkL1S5FZjHEF0puIfx4XPH/DyhQVXn71kifJiurCTzzWhra6O3QQdZvd4 eD7ytVi9BqNgPeVGa1MKFkRbz/ctwv0= Received: from localhost.localdomain (unknown [39.45.24.179]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) (Authenticated sender: usama.anjum) by madras.collabora.co.uk (Postfix) with ESMTPSA id DF6946602CFC; Mon, 9 Jan 2023 06:46:01 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1673246769; bh=jdpH5EMyLy7iSiO7dY+lCisLDuNYuznIAiYPmlec300=; h=From:To:Cc:Subject:Date:From; b=aJm28bNwVM3F7NMdXvTi0MTx8P7Y4vbn5z4grLCmJWy6a8oRuFWtzK8CgjkaOQ6+R u7BqCsWyxRXYmEDXwMnlVnr2bQMi3BptwoD7bTfeRD6RJi9rkMzzL8rY9z57Ax8wES LS+nwK7Cnyx5NHrVbq4oATiyE54dJ9vw4bMw9uL79wYNmZTWHuFE/yXw0BffsuAHch Su5+rva2C69Yx4+dlZx6jRssheZHTfyzYoZRWnU3ow5AxmBBYEsvHmlSQ6WS7doVhD nsIGosB3eXyS1lKo8b8jyIGpe9RshO2emRN1taZ6OVZlIYiC9hDpEgDUkULpM+JlnH jFeUz0LJsLkvA== From: Muhammad Usama Anjum To: Peter Xu , David Hildenbrand , Andrew Morton , =?utf-8?b?TWljaGHFgiBNaXJvc8WC?= =?utf-8?b?YXc=?= , Andrei Vagin , Danylo Mocherniuk , Paul Gofman , Cyrill Gorcunov Cc: Alexander Viro , Shuah Khan , Christian Brauner , Yang Shi , Vlastimil Babka , "Liam R . Howlett" , Yun Zhou , Suren Baghdasaryan , Alex Sierra , Muhammad Usama Anjum , Matthew Wilcox , Pasha Tatashin , Mike Rapoport , Nadav Amit , Axel Rasmussen , "Gustavo A . R . Silva" , Dan Williams , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-kselftest@vger.kernel.org, Greg KH , kernel@collabora.com Subject: [PATCH v7 0/4] Implement IOCTL to get and/or the clear info about PTEs Date: Mon, 9 Jan 2023 11:45:15 +0500 Message-Id: <20230109064519.3555250-1-usama.anjum@collabora.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 6AA421C0008 X-Stat-Signature: poxoyfuj1z1bhfxerfi47xtbsgnb8znn X-Rspam-User: X-HE-Tag: 1673246771-820309 X-HE-Meta: U2FsdGVkX19PRkWnHRd8XaDsLqwWdkzuj3oBzEbho6WqMI8UwGgq7huPGn2rnQxJQqdbB8VJt1FUwnsim66oqHAzL51oXn49hvkFXfgId/iT7Ig/p8qTk1HhpYkQ5CKWKZpT6ef4VckTBAi9ud/KQs7rOzHjc4/bq2yKaE4wCZfieuk0VNv5vykaOlJD0CmZVKCkoJKRYwKtAkscXqYK+jgbMAyIXNfDreqxUbTzTABMAMAXS9uCJM57KgKpt9LkuvyLSELdF7zCmG6RQ2svbfS2UbHkpgInVbBSf3fEneKcDgj/qqgM7THHW1xfhcTkQZ7AT3Fenkus/ZhvmJ8t2oyaIA/vMDsL7dXgZFMs//rAO2fLSj3u4YA1t6p5ogHt0c/WTUJWSF+eeufTvItgC67K/cF1R70eVn9xuR9ZvjgZ4aeePy+TZ/lPZE+9mnvMl46xmE15lCocaI6tIDZcIJlfAqrguIqTxWuvXZWOXe23x1esJ9A47gcSXnpwQcd9Gk/gw12bFOZp2fCWDZzxJznXz7H86TAzKuxGlHcK/srNwsf0aKdDDT+uflP4qSqQogL+IwlsdHCYPTQR2JgXNEhcXHGc1t3irKowpQqDVL/LcGgBCVOSe42Nu3fIKy5L0oKPcsbHAzVvWkVhCXJJecDNEdekTX23p/8SsQLLD1lWd5qROafmaliksLNYfjSEAfh8cwAZVs7U/DyIebVTKblAvS6leDepPZ/fqibdFu/nyHYoZwUy2FuNzlM8orIylA/VNp0WGL/ENi1VuUQkjC11iP2yz5BgCKsfCtz16lFy4wryES3piePgk6OP922DeZhajMpcby7lqYzpwbPOBkCI5D0lSc/6yEfSpBeFN4qKUi5XxRemxUBojtSDX2v+iEthiRgwE9HUFM1QOjio1w2GwoK8b9sgLn9ZLrdvPXjm3SD5Nt9DklAFDNcdaPNQT3fll8HM9BE= 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: *Changes in v7:* - Add uffd wp async - Update the IOCTL to use uffd under the hood instead of soft-dirty flags Stop using the soft-dirty flags for finding which pages have been written to. It is too delicate and wrong as it shows more soft-dirty pages than the actual soft-dirty pages. There is no interest in correcting it [A][B] as this is how the feature was written years ago. It shouldn't be updated to changed behaviour. Peter Xu has suggested using the async version of the UFFD WP [C] as it is based inherently on the PTEs. So in this patch series, I've added a new mode to the UFFD which is asynchronous version of the write protect. When this variant of the UFFD WP is used, the page faults are resolved automatically by the kernel. The pages which have been written-to can be found by reading pagemap file (!PM_UFFD_WP). This feature can be used successfully to find which pages have been written to from the time the pages were write protected. This works just like the soft-dirty flag without showing any extra pages which aren't soft-dirty in reality. [A] https://lore.kernel.org/all/20221220162606.1595355-1-usama.anjum@collabora.com [B] https://lore.kernel.org/all/20221122115007.2787017-1-usama.anjum@collabora.com [C] https://lore.kernel.org/all/Y6Hc2d+7eTKs7AiH@x1n *Changes in v6:* - Updated the interface and made cosmetic changes *Cover Letter in v5:* Hello, This patch series implements IOCTL on the pagemap procfs file to get the information about the page table entries (PTEs). The following operations are supported in this ioctl: - Get the information if the pages are soft-dirty, file mapped, present or swapped. - Clear the soft-dirty PTE bit of the pages. - Get and clear the soft-dirty PTE bit of the pages atomically. Soft-dirty PTE bit of the memory pages can be read by using the pagemap procfs file. The soft-dirty PTE bit for the whole memory range of the process can be cleared by writing to the clear_refs file. There are other methods to mimic this information entirely in userspace with poor performance: - The mprotect syscall and SIGSEGV handler for bookkeeping - The userfaultfd syscall with the handler for bookkeeping Some benchmarks can be seen here[1]. This series adds features that weren't present earlier: - There is no atomic get soft-dirty PTE bit status and clear operation possible. - The soft-dirty PTE bit of only a part of memory cannot be cleared. Historically, soft-dirty PTE bit tracking has been used in the CRIU project. The procfs interface is enough for finding the soft-dirty bit status and clearing the soft-dirty bit of all the pages of a process. We have the use case where we need to track the soft-dirty PTE bit for only specific pages on demand. We need this tracking and clear mechanism of a region of memory while the process is running to emulate the getWriteWatch() syscall of Windows. This syscall is used by games to keep track of dirty pages to process only the dirty pages. The information related to pages if the page is file mapped, present and swapped is required for the CRIU project[2][3]. The addition of the required mask, any mask, excluded mask and return masks are also required for the CRIU project[2]. The IOCTL returns the addresses of the pages which match the specific masks. The page addresses are returned in struct page_region in a compact form. The max_pages is needed to support a use case where user only wants to get a specific number of pages. So there is no need to find all the pages of interest in the range when max_pages is specified. The IOCTL returns when the maximum number of the pages are found. The max_pages is optional. If max_pages is specified, it must be equal or greater than the vec_size. This restriction is needed to handle worse case when one page_region only contains info of one page and it cannot be compacted. This is needed to emulate the Windows getWriteWatch() syscall. Some non-dirty pages get marked as dirty because of the kernel's internal activity (such as VMA merging as soft-dirty bit difference isn't considered while deciding to merge VMAs). The dirty bit of the pages is stored in the VMA flags and in the per page flags. If any of these two bits are set, the page is considered to be soft dirty. Suppose you have cleared the soft dirty bit of half of VMA which will be done by splitting the VMA and clearing soft dirty bit flag in the half VMA and the pages in it. Now kernel may decide to merge the VMAs again. So the half VMA becomes dirty again. This splitting/merging costs performance. The application receives a lot of pages which aren't dirty in reality but marked as dirty. Performance is lost again here. Also sometimes user doesn't want the newly allocated memory to be marked as dirty. PAGEMAP_NO_REUSED_REGIONS flag solves both the problems. It is used to not depend on the soft dirty flag in the VMA flags. So VMA splitting and merging doesn't happen. It only depends on the soft dirty bit of the individual pages. Thus by using this flag, there may be a scenerio such that the new memory regions which are just created, doesn't look dirty when seen with the IOCTL, but look dirty when seen from procfs. This seems okay as the user of this flag know the implication of using it. [1] https://lore.kernel.org/lkml/54d4c322-cd6e-eefd-b161-2af2b56aae24@collabora.com/ [2] https://lore.kernel.org/all/YyiDg79flhWoMDZB@gmail.com/ [3] https://lore.kernel.org/all/20221014134802.1361436-1-mdanylo@google.com/ Regards, Muhammad Usama Anjum Muhammad Usama Anjum (4): userfaultfd: Add UFFD WP Async support userfaultfd: split mwriteprotect_range() fs/proc/task_mmu: Implement IOCTL to get and/or the clear info about PTEs selftests: vm: add pagemap ioctl tests fs/proc/task_mmu.c | 300 +++++++ fs/userfaultfd.c | 161 ++-- include/linux/userfaultfd_k.h | 10 + include/uapi/linux/fs.h | 50 ++ include/uapi/linux/userfaultfd.h | 6 + mm/userfaultfd.c | 40 +- tools/include/uapi/linux/fs.h | 50 ++ tools/testing/selftests/vm/.gitignore | 1 + tools/testing/selftests/vm/Makefile | 5 +- tools/testing/selftests/vm/pagemap_ioctl.c | 884 +++++++++++++++++++++ 10 files changed, 1424 insertions(+), 83 deletions(-) create mode 100644 tools/testing/selftests/vm/pagemap_ioctl.c