From patchwork Tue Jan 24 08:43:19 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: 13113661 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 CED5BC25B4E for ; Tue, 24 Jan 2023 08:44:35 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0A5366B0071; Tue, 24 Jan 2023 03:44:35 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 02E556B0072; Tue, 24 Jan 2023 03:44:34 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id DEA496B0074; Tue, 24 Jan 2023 03:44:34 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id CD9956B0071 for ; Tue, 24 Jan 2023 03:44:34 -0500 (EST) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id A611BA0783 for ; Tue, 24 Jan 2023 08:44:34 +0000 (UTC) X-FDA: 80389056468.22.89D05A6 Received: from madras.collabora.co.uk (madras.collabora.co.uk [46.235.227.172]) by imf19.hostedemail.com (Postfix) with ESMTP id D4DF01A0006 for ; Tue, 24 Jan 2023 08:44:32 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=collabora.com header.s=mail header.b="F/AWXdkB"; spf=pass (imf19.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=1674549873; 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=1A5hOFZYJJxg8FgA/oBYm2GbiOhV1/IpRiFGGP06kxU=; b=Qo2aeb9RXkUBQVkZp9s7Lj3HZ9+ot7C6YxX3NwzMnIJIg8xGylPseo8FQDrghN8SPoTJQW HzJaLmFSEhoy8XSwrS5hRNrza7/mj6L93pnsPkjiHvBSqgdM7nInqHcV+RDDLPD24xv0lo hm4WZPPgPE8tYAVPIDlm5JO7bYc3J8k= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=collabora.com header.s=mail header.b="F/AWXdkB"; spf=pass (imf19.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=1674549873; a=rsa-sha256; cv=none; b=DnxlS1hppRx/cAQZlJvrqY04zBGurl3iZXgqk+q9tFizfywJwR2ybkNYjYdeK02eLxWhmx wzRfQwMnhqwrfmd9lVycKVmFONtGVNghzaRB67SbnEKtTWoSjxSv+++mZ3GCnAX6TK5mJN 2Ke4/q+T7KUpS7F2yHed75RHgsNm+Hc= Received: from localhost.localdomain (unknown [39.45.186.163]) (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 BF4DF6602DE5; Tue, 24 Jan 2023 08:44:24 +0000 (GMT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=collabora.com; s=mail; t=1674549871; bh=e4CQ5fwaejmFh2jEyV59ZLNZDU1xLhAA6U/NLInBTGs=; h=From:To:Cc:Subject:Date:From; b=F/AWXdkBfsGNPgTQSr/q+7Lnn6TcjKA1/OIvjLMwR0kj68SDFMl7NdXHcls218wKg 6n/4yTQgtDYqDZHf6zJAY/ogzjHbNwIBX3PZ0bt4jWGJMzUcxSfXKWxNNLtWf2gA3q vl1+H0rKuQbOdbhjTl00bnVXU+saRsLW6aSbIhUzZJfer6iMn8H+QekSFlspwcSfKR zSfGoCFo76EfQ8LUS+bKtquQ8Bxb7JuZPrtsUzenShHrImW1wgKrOPqk9qnED9utBb s4yf8MutpLaaJY9sL/QQ4WFlH5szs6SJNd/G470AbVBLNLEcA/Jn1rvEpXCxcs8Mbv oIxcPuo7R6mBQ== 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 v8 0/4] Implement IOCTL to get and/or the clear info about PTEs Date: Tue, 24 Jan 2023 13:43:19 +0500 Message-Id: <20230124084323.1363825-1-usama.anjum@collabora.com> X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 X-Rspamd-Queue-Id: D4DF01A0006 X-Stat-Signature: 5uimtw8wjotsjpst58of6c5huhj6e4in X-Rspam-User: X-Rspamd-Server: rspam08 X-HE-Tag: 1674549872-970878 X-HE-Meta: U2FsdGVkX18ySLIahGkJSND6iQe04B6vcTKdDyXzixAwmFtcrUjcf/U1zULMzePrDPvhR/W+bZaEQIllBrlvU3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Fznp2LnRpXFM8mJtbwJNCjXVMdBjdtKmU9Vj+nnuwzARRM2XROshrTgybP3MeOkJQCqZqrIsNI0AK1xGAMjUM/4hrD0RYxUFz/1Go2aSkbue5ZFCfyCGhKmO796MIUyhMb5l55pb1MBFl/S2NRLmYiaiKgFko1uMqciSspSvea4EavAL+Jm2MQcbPHhD6nQRTjKLpOw== 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 v8:* - Update uffd async wp implementation - Improve PAGEMAP_IOCTL implementation *Changes in v7:* - Add uffd wp async - Update the IOCTL to use uffd under the hood instead of soft-dirty flags Hello, Note: Soft-dirty pages and pages which have been written-to are synonyms. As kernel already has soft-dirty feature inside which we have given up to use, we are using written-to terminology while using UFFD async WP under the hood. This IOCTL, PAGEMAP_SCAN on pagemap file can be used to get and/or clear the info about page table entries. The following operations are supported in this ioctl: - Get the information if the pages have been written-to (PAGE_IS_WT), file mapped (PAGE_IS_FILE), present (PAGE_IS_PRESENT) or swapped (PAGE_IS_SWAPPED). - Write-protect the pages (PAGEMAP_WP_ENGAGE) to start finding which pages have been written-to. - Find pages which have been written-to and write protect the pages (atomic PAGE_IS_WT + PAGEMAP_WP_ENGAGE) It is possible to find and clear soft-dirty pages entirely in userspace. But it isn't efficient: - The mprotect and SIGSEGV handler for bookkeeping - The userfaultfd wp 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 present in the kernel. - The pages which have been written-to can not be found in accurate way. (Kernel's soft-dirty PTE bit + sof_dirty VMA bit shows more soft-dirty pages than there actually are.) 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. *(Moved to using UFFD instead of soft-dirty to find pages which have been written-to from v7 patch series)*: 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 [2][3] 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 [4] 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. The information related to pages if the page is file mapped, present and swapped is required for the CRIU project [5][6]. The addition of the required mask, any mask, excluded mask and return masks are also required for the CRIU project [5]. 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. The patch series include the detailed selftest which can be used as an example for the uffd async wp test and PAGEMAP_IOCTL. It shows the interface usages as well. [1] https://lore.kernel.org/lkml/54d4c322-cd6e-eefd-b161-2af2b56aae24@collabora.com/ [2] https://lore.kernel.org/all/20221220162606.1595355-1-usama.anjum@collabora.com [3] https://lore.kernel.org/all/20221122115007.2787017-1-usama.anjum@collabora.com [4] https://lore.kernel.org/all/Y6Hc2d+7eTKs7AiH@x1n [5] https://lore.kernel.org/all/YyiDg79flhWoMDZB@gmail.com/ [6] 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 | 294 +++++++ fs/userfaultfd.c | 21 + include/linux/userfaultfd_k.h | 16 + include/uapi/linux/fs.h | 50 ++ include/uapi/linux/userfaultfd.h | 8 +- mm/memory.c | 29 +- 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 | 880 +++++++++++++++++++++ 11 files changed, 1374 insertions(+), 20 deletions(-) create mode 100644 tools/testing/selftests/vm/pagemap_ioctl.c