PATCH V6 03 Implement IOCTL To Get Andor The Clear Info About PTEs

* [PATCH v6 0/3] Implement IOCTL to get and/or the clear info about PTEs@ :23 Muhammad Usama Anjum :23 ` [PATCH v6 1/3] fs/proc/task_mmu: update functions to clear the soft-dirty PTE bit Muhammad Usama Anjum ` (4 more replies)
0 siblings, 5 replies; 18+ messages in thread
From: Muhammad Usama Anjum @ :23 UTC (permalink / raw)
To: Michał Mirosław, Andrei Vagin, Danylo Mocherniuk,
Alexander Viro, Andrew Morton, Suren Baghdasaryan, Greg KH,
Christian Brauner, Peter Xu, Yang Shi, Vlastimil Babka,
Zach O’Keefe, Matthew Wilcox (Oracle),
Gustavo A. R. Silva, Dan Williams, Muhammad Usama Anjum, kernel,
Gabriel Krisman Bertazi, David Hildenbrand, Peter Enderborg,
open list : KERNEL SELFTEST FRAMEWORK, Shuah Khan, open list,
open list : PROC FILESYSTEM, open list : MEMORY MANAGEMENT,
Paul Gofman
Changes in v6:
– Updated the interface and made cosmetic changes
Original 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] /lkml/54d4c322-cd6e-eefd-b161-/
[2] /all//
[3] /all/ . /
Regards,
Muhammad Usama Anjum
Muhammad Usama Anjum (3):
fs/proc/task_mmu: update functions to clear the soft-dirty PTE bit
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 | 410 +++++++++++-
include/uapi/linux/fs.h | 56 ++
tools/include/uapi/linux/fs.h | 56 ++
tools/testing/selftests/vm/.gitignore | 1 +
tools/testing/selftests/vm/Makefile | 5 +-
tools/testing/selftests/vm/pagemap_ioctl.c | 698 +++++++++++++++++++++
6 files changed, 1193 insertions(+), 33 deletions(-)
create mode tools/testing/selftests/vm/pagemap_ioctl.c .30.2
^ permalink raw reply [flat|nested] 18+ messages in thread

* [PATCH v6 1/3] fs/proc/task_mmu: update functions to clear the soft-dirty PTE bit :23 [PATCH v6 0/3] Implement IOCTL to get and/or the clear info about PTEs Muhammad Usama Anjum
@ :23 ` Muhammad Usama Anjum :23 ` [PATCH v6 2/3] fs/proc/task_mmu: Implement IOCTL to get and/or the clear info about PTEs Muhammad Usama Anjum ` (3 subsequent siblings)
4 siblings, 0 replies; 18+ messages in thread
From: Muhammad Usama Anjum @ :23 UTC (permalink / raw)
To: Michał Mirosław, Andrei Vagin, Danylo Mocherniuk,
Alexander Viro, Andrew Morton, Suren Baghdasaryan, Greg KH,
Christian Brauner, Peter Xu, Yang Shi, Vlastimil Babka,
Zach O’Keefe, Matthew Wilcox (Oracle),
Gustavo A. R. Silva, Dan Williams, Muhammad Usama Anjum, kernel,
Gabriel Krisman Bertazi, David Hildenbrand, Peter Enderborg,
open list : KERNEL SELFTEST FRAMEWORK, Shuah Khan, open list,
open list : PROC FILESYSTEM, open list : MEMORY MANAGEMENT,
Paul Gofman
Update the clear_soft_dirty() and clear_soft_dirty_pmd() to optionally
clear and return the status if page is dirty.
Signed-off-by: Muhammad Usama Anjum Changes in v2:
– Move back the functions back to their original file fs/proc/task_mmu.c | 82 ++++++++++++++++++++++++++++ file changed, 51 insertions(+), 31 deletions(-)
diff –git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index 8a74cdcc9af0..8235c536ac a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -1095,8 +1095,8 @@ static inline bool pte_is_pinned(struct vm_area_struct *vma, unsigned long addr,
return page_maybe_dma_pinned(page);
}
-static inline void clear_soft_dirty(struct vm_area_struct *vma,
– unsigned long addr, pte_t *pte)
+static inline bool check_soft_dirty(struct vm_area_struct *vma,
+ unsigned long addr, pte_t *pte, bool clear)
{
/*
* The soft-dirty tracker uses #PF-s to catch writes
@@ -1105,55 +1105,75 @@ static inline void clear_soft_dirty(struct vm_area_struct *vma,
* of how soft-dirty works.
*/
pte_t ptent = *pte;
+ int dirty = 0;
if (pte_present(ptent)) {
pte_t old_pte;
– if (pte_is_pinned(vma, addr, ptent))
– return;
– old_pte = ptep_modify_prot_start(vma, addr, pte);
– ptent = pte_wrprotect(old_pte);
– ptent = pte_clear_soft_dirty(ptent);
– ptep_modify_prot_commit(vma, addr, pte, old_pte, ptent);
+ dirty = pte_soft_dirty(ptent);
+
+ if (dirty && clear && !pte_is_pinned(vma, addr, ptent)) {
+ old_pte = ptep_modify_prot_start(vma, addr, pte);
+ ptent = pte_wrprotect(old_pte);
+ ptent = pte_clear_soft_dirty(ptent);
+ ptep_modify_prot_commit(vma, addr, pte, old_pte, ptent);
+ }
} else if (is_swap_pte(ptent)) {
– ptent = pte_swp_clear_soft_dirty(ptent);
– set_pte_at(vma->vm_mm, addr, pte, ptent);
+ dirty = pte_swp_soft_dirty(ptent);
+
+ if (dirty && clear) {
+ ptent = pte_swp_clear_soft_dirty(ptent);
+ set_pte_at(vma->vm_mm, addr, pte, ptent);
+ }
}
+
+ return !!dirty;
}
#else
-static inline void clear_soft_dirty(struct vm_area_struct *vma,
– unsigned long addr, pte_t *pte)
+static inline bool check_soft_dirty(struct vm_area_struct *vma,
+ unsigned long addr, pte_t *pte, bool clear)
{
+ return false;
}
#endif
#if defined(CONFIG_MEM_SOFT_DIRTY) && defined(CONFIG_TRANSPARENT_HUGEPAGE)
-static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
– unsigned long addr, pmd_t *pmdp)
+static inline bool check_soft_dirty_pmd(struct vm_area_struct *vma,
+ unsigned long addr, pmd_t *pmdp, bool clear)
{
pmd_t old, pmd = *pmdp;
+ int dirty = 0;
if (pmd_present(pmd)) {
– /* See comment in change_huge_pmd() */
– old = pmdp_invalidate(vma, addr, pmdp);
– if (pmd_dirty(old))
– pmd = pmd_mkdirty(pmd);
– if (pmd_young(old))
– pmd = pmd_mkyoung(pmd); pmd = pmd_wrprotect(pmd);
– pmd = pmd_clear_soft_dirty(pmd); set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
+ dirty = pmd_soft_dirty(pmd);
+ if (dirty && clear) {
+ /* See comment in change_huge_pmd() */
+ old = pmdp_invalidate(vma, addr, pmdp);
+ if (pmd_dirty(old))
+ pmd = pmd_mkdirty(pmd);
+ if (pmd_young(old))
+ pmd = pmd_mkyoung(pmd);
+
+ pmd = pmd_wrprotect(pmd);
+ pmd = pmd_clear_soft_dirty(pmd);
+
+ set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
+ }
} else if (is_migration_entry(pmd_to_swp_entry(pmd))) {
– pmd = pmd_swp_clear_soft_dirty(pmd);
– set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
+ dirty = pmd_swp_soft_dirty(pmd);
+
+ if (dirty && clear) {
+ pmd = pmd_swp_clear_soft_dirty(pmd);
+ set_pmd_at(vma->vm_mm, addr, pmdp, pmd);
+ }
}
+ return !!dirty;
}
#else
-static inline void clear_soft_dirty_pmd(struct vm_area_struct *vma,
– unsigned long addr, pmd_t *pmdp)
+static inline bool check_soft_dirty_pmd(struct vm_area_struct *vma,
+ unsigned long addr, pmd_t *pmdp, bool clear)
{
+ return false;
}
#endif
@@ -1169,7 +1189,7 @@ static int clear_refs_pte_range(pmd_t *pmd, unsigned long addr,
ptl = pmd_trans_huge_lock(pmd, vma);
if (ptl) 56 ++++++
tools/include/uapi/linux/fs.h
+#define PAGEMAP_OP_MASK (PAGE_IS_SOFTDIRTY | PAGE_IS_FILE | \
+ PAGE_IS_PRESENT | PAGE_IS_SWAPPED)
+#define PAGEMAP_NONSD_OP_MASK (PAGE_IS_FILE | PAGE_IS_PRESENT | PAGE_IS_SWAPPED)
+#define PAGEMAP_SD_FLAGS (PAGEMAP_SOFTDIRTY_CLEAR | PAGEMAP_NO_REUSED_REGIONS)
+#define IS_CLEAR_OP(a) (a->flags & PAGEMAP_SOFTDIRTY_CLEAR)
+#define IS_GET_OP(a) (a->vec)
+#define IS_SD_OP(a) (a->flags & PAGEMAP_SD_FLAGS)
+
+struct pagemap_scan_private {
+ struct page_region *vec;
+ unsigned long vec_len;
+ unsigned long vec_index;
+ unsigned int max_pages;
+ unsigned int found_pages;
+ unsigned int flags;
+ unsigned long required_mask;
+ unsigned long anyof_mask;
+ unsigned long excluded_mask;
+ unsigned long return_mask;
+};
+
+static int pagemap_scan_pmd_test_walk(unsigned long start, unsigned long end, struct mm_walk *walk)
+{
+ struct pagemap_scan_private *p = walk->private;
+ struct vm_area_struct *vma = walk->vma;
+
+ if (IS_GET_OP(p) && p->max_pages && (p->found_pages == p->max_pages))
+ return -1;
+
+ if (vma->vm_flags & VM_PFNMAP)
+ return 1;
+
+ return 0;
+}
+
+static int add_to_out(bool sd, bool file, bool pres, bool swap, struct pagemap_scan_private *p,
+ unsigned long addr, unsigned int len)
+ file <<>required_mask)
+ cpy = ((p->required_mask & cur) == p->required_mask);
+ if (cpy && p->anyof_mask)
+ cpy = (p->anyof_mask & cur);
+ if (cpy && p->excluded_mask)
+ cpy = !(p->excluded_mask & cur);
+
+ bitmap = cur & p->return_mask;
+
+ if (cpy && bitmap) {
+ if ((p->vec_index) && (p->vec[p->vec_index – 1].bitmap == bitmap) &&
+ (p->vec[p->vec_index – 1].start + p->vec[p->vec_index – 1].len * PAGE_SIZE ==
+ addr)) {
+ p->vec[p->vec_index – 1].len += len;
+ p->found_pages += len;
+ } else if (p->vec_index vec_len) {
+ p->vec[p->vec_index].start = addr;
+ p->vec[p->vec_index].len = len;
+ p->found_pages += len;
+ p->vec[p->vec_index].bitmap = bitmap;
+ p->vec_index++;
+ } else {
+ return -ENOMEM;
+ }
+ }
+
+ return 0;
+
+
+static int pagemap_scan_pmd_entry(pmd_t *pmd, unsigned long addr,
+ unsigned long end, struct mm_walk *walk)
+
+ struct pagemap_scan_private *p = walk->private;
+ struct vm_area_struct *vma = walk->vma;
+ unsigned int len;
+ spinlock_t *ptl;
+ int ret = 0;
+ pte_t *pte;
+ bool dirty_vma = (p->flags & PAGEMAP_NO_REUSED_REGIONS) ?
+ (false) : (vma->vm_flags & VM_SOFTDIRTY);
+
+ if ((walk->vma->vm_end max_pages && p->found_pages == p->max_pages))
+ return 0;
+
+ end = min(end, walk->vma->vm_end);
+
+ ptl = pmd_trans_huge_lock(pmd, vma);
+ if (ptl) check_soft_dirty_pmd(vma, addr, pmd, false))
+ check_soft_dirty_pmd(vma, addr, pmd, false),
+ vma->vm_file, pmd_present(*pmd), is_swap_pmd(*pmd),
+ p, addr, len);
+
+ if (!ret && IS_CLEAR_OP(p))
+ check_soft_dirty_pmd(vma, addr, pmd, true);
+
+ spin_unlock(ptl);
+ return 0;
+
+
+process_smaller_pages:
+ if (pmd_trans_unstable(pmd))
+ return 0;
+
+ pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
+ for (; addr max_pages || (p->found_pages max_pages))
+ ; pte++, addr += PAGE_SIZE) check_soft_dirty(vma, addr, pte, false),
+ vma->vm_file, pte_present(*pte),
+ is_swap_pte(*pte), p, addr, 1);
+ if (!ret && IS_CLEAR_OP(p))
+ check_soft_dirty(vma, addr, pte, true);
+
+ pte_unmap_unlock(pte – 1, ptl);
+ cond_resched();
+
+ return 0;
+}
+
+static int pagemap_scan_pte_hole(unsigned long addr, unsigned long end, int depth,
+ struct mm_walk *walk)
+{
+ struct pagemap_scan_private *p = walk->private;
+ struct vm_area_struct *vma = walk->vma;
+ unsigned int len;
+ bool sd;
+
+ if (vma) {
+ /* Individual pages haven’t been allocated and written */
+ sd = (p->flags & PAGEMAP_NO_REUSED_REGIONS) ? (false) :
+ (vma->vm_flags & VM_SOFTDIRTY);
+
+ len = (end – addr)/PAGE_SIZE;
+ if (p->max_pages && p->found_pages + len > p->max_pages)
+ len = p->max_pages – p->found_pages;
+
+ add_to_out(sd, vma->vm_file, false, false, p, addr, len);
+ }
+
+ return 0;
+}
+
+#ifdef CONFIG_MEM_SOFT_DIRTY
+static int pagemap_scan_pre_vma(unsigned long start, unsigned long end, struct mm_walk *walk)
+{
+ struct pagemap_scan_private *p = walk->private;
+ struct vm_area_struct *vma = walk->vma;
+ unsigned long end_cut = end;
+ int ret;
+
+ if (!(p->flags & PAGEMAP_NO_REUSED_REGIONS) && IS_CLEAR_OP(p) &&
+ (vma->vm_flags & VM_SOFTDIRTY)) {
+ if (vma->vm_start vm_mm, vma, start, 1);
+ if (ret)
+ return ret;
+ }
+ /* Calculate end_cut because of max_pages */
+ if (IS_GET_OP(p) && p->max_pages)
+ end_cut = min(start + (p->max_pages – p->found_pages) * PAGE_SIZE, end);
+
+ if (vma->vm_end > end_cut) {
+ ret = split_vma(vma->vm_mm, vma, end_cut, 0);
+ if (ret)
+ return ret;
+ }
+ }
+
+ return 0;
+}
+
+static void pagemap_scan_post_vma(struct mm_walk *walk)
+{
+ struct pagemap_scan_private *p = walk->private;
+ struct vm_area_struct *vma = walk->vma;
+
+ if (!(p->flags & PAGEMAP_NO_REUSED_REGIONS) && IS_CLEAR_OP(p) &&
+ (vma->vm_flags & VM_SOFTDIRTY)) {
+ vma->vm_flags &= ~VM_SOFTDIRTY;
+ vma_set_page_prot(vma);
+ }
+}
+#endif /* CONFIG_MEM_SOFT_DIRTY */
+
+static const struct mm_walk_ops pagemap_scan_ops = {
+ .test_walk = pagemap_scan_pmd_test_walk,
+ .pmd_entry = pagemap_scan_pmd_entry,
+ .pte_hole = pagemap_scan_pte_hole,
+
+#ifdef CONFIG_MEM_SOFT_DIRTY
+ /* Only for clearing SD bit over VMAs */
+ .pre_vma = pagemap_scan_pre_vma,
+ .post_vma = pagemap_scan_post_vma,
+#endif /* CONFIG_MEM_SOFT_DIRTY */
+};
+
+static long do_pagemap_sd_cmd(struct mm_struct *mm, struct pagemap_scan_arg *arg)
+ else {
+ ret = 0;
+ }
+
+free_data:
+ if (IS_GET_OP(arg))
+ vfree(p.vec);
+
+ return ret;
+}
+
+static long pagemap_sd_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
+{
+ struct pagemap_scan_arg __user *uarg = (struct pagemap_scan_arg __user *)arg;
+ struct mm_struct *mm = file->private_data;
+ struct pagemap_scan_arg argument;
+
+ if (cmd == PAGEMAP_SCAN) {
+ if (copy_from_user(&argument, uarg, sizeof(struct pagemap_scan_arg)))
+ return -EFAULT;
+ return do_pagemap_sd_cmd(mm, &argument);
+ }
+ return -EINVAL;
+}
+
const struct file_operations proc_pagemap_operations = {
.llseek = mem_lseek, /* borrow this */
.read = pagemap_read,
.open = pagemap_open,
.release = pagemap_release,
+ .unlocked_ioctl = pagemap_sd_ioctl,
+ .compat_ioctl = pagemap_sd_ioctl,
};
#endif /* CONFIG_PROC_PAGE_MONITOR */
diff –git a/include/uapi/linux/fs.h b/include/uapi/linux/fs.h
index b7b c..11d232cfc9b a/include/uapi/linux/fs.h
+++ b/include/uapi/linux/fs.h
@@ -305,4 +305,60 @@ typedef int __bitwise __kernel_rwf_t;
#define RWF_SUPPORTED (RWF_HIPRI | RWF_DSYNC | RWF_SYNC | RWF_NOWAIT |\ RWF_APPEND)
+/* Pagemap ioctl */
+#define PAGEMAP_SCAN _IOWR(‘f’, 16, struct pagemap_scan_arg)
+
+/* Bits are set in the bitmap of the page_region and masks in pagemap_sd_args */
+#define PAGE_IS_SOFTDIRTY (1 <<> #endif /* _UAPI_LINUX_FS_H */
diff –git a/tools/include/uapi/linux/fs.h b/tools/include/uapi/linux/fs.h
index b7b c..11d232cfc9b a/tools/include/uapi/linux/fs.h
+++ b/tools/include/uapi/linux/fs.h
@@ -305,4 +305,60 @@ typedef int __bitwise __kernel_rwf_t;
#define RWF_SUPPORTED (RWF_HIPRI | RWF_DSYNC | RWF_SYNC | RWF_NOWAIT |\ RWF_APPEND)
+/* Pagemap ioctl */
+#define PAGEMAP_SCAN _IOWR(‘f’, 16, struct pagemap_scan_arg)
+
+/* Bits are set in the bitmap of the page_region and masks in pagemap_sd_args */
+#define PAGE_IS_SOFTDIRTY (1 <<> #endif /* _UAPI_LINUX_FS_H */ .30.2
^ permalink raw reply related [flat|nested] 18+ messages in thread

* [PATCH v6 3/3] selftests: vm: add pagemap ioctl tests :23 [PATCH v6 0/3] Implement IOCTL to get and/or the clear info about PTEs Muhammad Usama Anjum :23 ` [PATCH v6 1/3] fs/proc/task_mmu: update functions to clear the soft-dirty PTE bit Muhammad Usama Anjum :23 ` [PATCH v6 2/3] fs/proc/task_mmu: Implement IOCTL to get and/or the clear info about PTEs Muhammad Usama Anjum
@ :23 ` Muhammad Usama Anjum :34 ` [PATCH v6 0/3] Implement IOCTL to get and/or the clear info about PTEs David Hildenbrand :11 ` Peter Xu
4 siblings, 0 replies; 18+ messages in thread
From: Muhammad Usama Anjum @ :23 UTC (permalink / raw)
To: Michał Mirosław, Andrei Vagin, Danylo Mocherniuk,
Alexander Viro, Andrew Morton, Suren Baghdasaryan, Greg KH,
Christian Brauner, Peter Xu, Yang Shi, Vlastimil Babka,
Zach O’Keefe, Matthew Wilcox (Oracle),
Gustavo A. R. Silva, Dan Williams, Muhammad Usama Anjum, kernel,
Gabriel Krisman Bertazi, David Hildenbrand, Peter Enderborg,
open list : KERNEL SELFTEST FRAMEWORK, Shuah Khan, open list,
open list : PROC FILESYSTEM, open list : MEMORY MANAGEMENT,
Paul Gofman
Add pagemap ioctl tests. Add several different types of tests to judge
the correction of the interface.
Signed-off-by: Muhammad Usama Anjum Changes in v6:
– Rename variables
Changes in v4:
– Updated all the tests to conform to new IOCTL
Changes in v3:
– Add another test to do sanity of flags
Changes in v2:
– Update the tests to use the ioctl interface instead of syscall
TAP version 13
1..59
ok 1 sanity_tests_sd wrong flag specified
ok 2 sanity_tests_sd wrong mask specified
ok 3 sanity_tests_sd wrong return mask specified
ok 4 sanity_tests_sd mixture of correct and wrong flag
ok 5 sanity_tests_sd Clear area with larger vec size
ok 6 sanity_tests_sd Repeated pattern of dirty and non-dirty pages
ok 7 sanity_tests_sd Repeated pattern of dirty and non-dirty pages in parts
ok 8 sanity_tests_sd Two regions
ok 9 Page testing: all new pages must be soft dirty
ok 10 Page testing: all pages must not be soft dirty
ok 11 Page testing: all pages dirty other than first and the last one
ok 12 Page testing: only middle page dirty
ok 13 Page testing: only two middle pages dirty
ok 14 Page testing: only get 2 dirty pages and clear them as well
ok 15 Page testing: Range clear only
ok 16 Large Page testing: all new pages must be soft dirty
ok 17 Large Page testing: all pages must not be soft dirty
ok 18 Large Page testing: all pages dirty other than first and the last one
ok 19 Large Page testing: only middle page dirty
ok 20 Large Page testing: only two middle pages dirty
ok 21 Large Page testing: only get 2 dirty pages and clear them as well
ok 22 Large Page testing: Range clear only
ok 23 Huge page testing: all new pages must be soft dirty
ok 24 Huge page testing: all pages must not be soft dirty
ok 25 Huge page testing: all pages dirty other than first and the last one
ok 26 Huge page testing: only middle page dirty
ok 27 Huge page testing: only two middle pages dirty
ok 28 Huge page testing: only get 2 dirty pages and clear them as well
ok 29 Huge page testing: Range clear only
ok 30 Performance Page testing: all new pages must be soft dirty
ok 31 Performance Page testing: all pages must not be soft dirty
ok 32 Performance Page testing: all pages dirty other than first and the last one
ok 33 Performance Page testing: only middle page dirty
ok 34 Performance Page testing: only two middle pages dirty
ok 35 Performance Page testing: only get 2 dirty pages and clear them as well
ok 36 Performance Page testing: Range clear only
ok 37 hpage_unit_tests all new huge page must be dirty
ok 38 hpage_unit_tests all the huge page must not be dirty
ok 39 hpage_unit_tests all the huge page must be dirty and clear
ok 40 hpage_unit_tests only middle page dirty
ok 41 hpage_unit_tests clear first half of huge page
ok 42 hpage_unit_tests clear first half of huge page with limited buffer
ok 43 hpage_unit_tests clear second half huge page
ok 44 unmapped_region_tests Get dirty pages
ok 45 unmapped_region_tests Get dirty pages
ok 46 Test test_simple
ok 47 sanity_tests clear op can only be specified with PAGE_IS_DIRTY
ok 48 sanity_tests rmask specified
ok 49 sanity_tests amask specified
ok 50 sanity_tests emask specified
ok 51 sanity_tests rmask and amask specified
ok 52 sanity_tests rmask and amask specified
ok 53 sanity_tests Get sd and present pages with amask
ok 54 sanity_tests Get all the pages with rmask
ok 55 sanity_tests Get sd and present pages with rmask and amask
ok 56 sanity_tests Don’t get sd pages
ok 57 sanity_tests Don’t get present pages
ok 58 sanity_tests Find dirty present pages with return mask
ok 59 sanity_tests Memory mapped file
# Totals: pass:59 fail:0 xfail:0 xpass:0 skip:0 error: | 1 +
tools/testing/selftests/vm/Makefile | 5 +-
tools/testing/selftests/vm/pagemap_ioctl.c | 698 +++++++++++++++++++++
3 files changed, 702 insertions(+), 2 deletions(-)
create mode tools/testing/selftests/vm/pagemap_ioctl.c
–git a/tools/testing/selftests/vm/.gitignore b/tools/testing/selftests/vm/.gitignore
index 8a536c731e3c..4a73983e3e a/tools/testing/selftests/vm/.gitignore
+++ b/tools/testing/selftests/vm/.gitignore
@@ -17,6 +17,7 @@ mremap_dontunmap
mremap_test
on-fault-limit
transhuge-stress
+pagemap_ioctl
protection_keys
protection_keys_32
protection_keys_64
diff –git a/tools/testing/selftests/vm/Makefile b/tools/testing/selftests/vm/Makefile
index 0986bd60c19f..2325bcdb9fae a/tools/testing/selftests/vm/Makefile
+++ b/tools/testing/selftests/vm/Makefile
@@ -24,9 +24,8 @@ MACHINE ?= $(shell echo $(uname_M) | sed -e ‘s/aarch64.*/arm64/’ -e ‘s/ppc64.*/p
# things despite using incorrect values such as an *occasionally* incomplete
# LDLIBS.
MAKEFLAGS += –no-builtin-rules

CFLAGS = -Wall -I $(top_srcdir) -I $(top_srcdir)/usr/include $(EXTRA_CFLAGS) $(KHDR_INCLUDES)
-LDLIBS = -lrt -lpthread
+LDLIBS = -lrt -lpthread -lm
TEST_GEN_FILES = anon_cow
TEST_GEN_FILES += compaction_test
TEST_GEN_FILES += gup_test
@@ -52,6 +51,7 @@ TEST_GEN_FILES += on-fault-limit
TEST_GEN_FILES += thuge-gen
TEST_GEN_FILES += transhuge-stress
TEST_GEN_FILES += userfaultfd
+TEST_GEN_PROGS += pagemap_ioctl
TEST_GEN_PROGS += soft-dirty
TEST_GEN_PROGS += split_huge_page_test
TEST_GEN_FILES += ksm_tests
@@ -103,6 +103,7 @@ $(OUTPUT)/anon_cow: vm_util.c
$(OUTPUT)/khugepaged: vm_util.c
$(OUTPUT)/ksm_functional_tests: vm_util.c
$(OUTPUT)/madv_populate: vm_util.c
+$(OUTPUT)/pagemap_ioctl: vm_util.c
$(OUTPUT)/soft-dirty: vm_util.c
$(OUTPUT)/split_huge_page_test: vm_util.c
$(OUTPUT)/userfaultfd: vm_util.c
diff –git a/tools/testing/selftests/vm/pagemap_ioctl.c b/tools/testing/selftests/vm/pagemap_ioctl.c
new file mode index ..1a55b2c3b7fc
— /dev/null
+++ b/tools/testing/selftests/vm/pagemap_ioctl.c
@@ -0,0 +1,698 @@
+// SPDX-License-Identifier: GPL-2.0
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+#include “vm_util.h”
+#include “../kselftest.h”
+#include
+#include
+#include
+#include
+#include
+
+#define PAGEMAP_OP_MASK (PAGE_IS_SOFTDIRTY | PAGE_IS_FILE | \
+ PAGE_IS_PRESENT | PAGE_IS_SWAPPED)
+#define TEST_ITERATIONS 10
+#define PAGEMAP “/proc/self/pagemap”
+int pagemap_fd;
+
+static long pagemap_ioctl(void *start, int len, void *vec, int vec_len, int flag,
+ int max_pages, long required_mask, long anyof_mask, long excluded_mask,
+ long return_mask)
+{
+ struct pagemap_scan_arg arg;
+ int ret;
+
+ arg.start = (uintptr_t)start;
+ arg.len = len;
+ arg.vec = (uintptr_t)vec;
+ arg.vec_len = vec_len;
+ arg.flags = flag;
+ arg.max_pages = max_pages;
+ arg.required_mask = required_mask;
+ arg.anyof_mask = anyof_mask;
+ arg.excluded_mask = excluded_mask;
+ arg.return_mask = return_mask;
+
+ ret = ioctl(pagemap_fd, PAGEMAP_SCAN, &arg);
+
+ return ret;
+}
+
+int sanity_tests_sd(int page_size)
+ MAP_ANON, -1, 0);
+ if (!mem)
+ ksft_exit_fail_msg(“error nomem\n”);
+
+ /* 1. wrong operation */
+ ksft_test_result(pagemap_ioctl(mem, mem_size, vec, vec_size, -1,
+ , PAGE_IS_SOFTDIRTY, 0, 0, PAGE_IS_SOFTDIRTY) = 0, “%s Clear area with larger vec size\n”, __func__);
+
+ /* 3. Repeated pattern of dirty and non-dirty pages */
+ for (i = 0; i = vec_size – 2 && vec[0].len 0,
+ “%s only middle page dirty\n”, __func__);
+
+ free(map);
+ else {
+ ksft_test_result_skip(“all new huge page must be dirty\n”);
+ ksft_test_result_skip(“all the huge page must not be dirty\n”);
+ ksft_test_result_skip(“all the huge page must be dirty and clear\n”);
+ ksft_test_result_skip(“only middle page dirty\n”);
+ }
+
+ // 5. clear first half of huge page
+ map = gethugepage(map_size);
+ if (map) MAP_ANON, -1, 0);
+ if (!mem)
+ ksft_exit_fail_msg(“error nomem\n”);
+ memset(mem, 0, mem_size);
+
+ ret = pagemap_ioctl(mem, mem_size, vec, vec_size, 0, 0,
+ , PAGEMAP_OP_MASK, 0, PAGE_IS_SOFTDIRTY);
+ ksft_test_result(ret >= 0 && vec[0].start == (uintptr_t)mem && vec[0].len == vec_size &&
+ vec[0].bitmap == PAGE_IS_SOFTDIRTY,
+ “%s Find dirty present pages with return mask\n”, __func__);
+
+ /* 9. Memory mapped file */
+ int fd;
+ struct stat sbuf;
+
+ fd = open(“run_vmtests.sh”, O_RDONLY);
+ if (fd = 0 && vec[0].start == (uintptr_t)fmem &&
+ vec[0].len == ceilf((float)sbuf.st_size/page_size) &&
+ vec[0].bitmap == (PAGE_IS_SOFTDIRTY
+
+int main(void)
+ 56 ++++++
> 3 files changed, 440 insertions(+)
>
> diff –git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
> index 8235c536ac70..8d6a84ec5ef > — a/fs/proc/task_mmu.c
> +++ b/fs/proc/task_mmu.c
> @@ -19,6 +19,9 @@
> #include
> #include
> #include
> +#include
> +#include
> +#include
>
> #include
> #include
> @@ -1775,11 +1778,336 @@ static int pagemap_release(struct inode *inode, struct file *file)
> return 0;
>
>
> +#define PAGEMAP_OP_MASK (PAGE_IS_SOFTDIRTY | PAGE_IS_FILE | \
> + PAGE_IS_PRESENT | PAGE_IS_SWAPPED)
> +#define PAGEMAP_NONSD_OP_MASK (PAGE_IS_FILE | PAGE_IS_PRESENT | PAGE_IS_SWAPPED)
> +#define PAGEMAP_SD_FLAGS (PAGEMAP_SOFTDIRTY_CLEAR | PAGEMAP_NO_REUSED_REGIONS)
> +#define IS_CLEAR_OP(a) (a->flags & PAGEMAP_SOFTDIRTY_CLEAR)
> +#define IS_GET_OP(a) (a->vec)
> +#define IS_SD_OP(a) (a->flags & PAGEMAP_SD_FLAGS)
> +
> +struct pagemap_scan_private {
> + struct page_region *vec;
> + unsigned long vec_len;
> + unsigned long vec_index;
> + unsigned int max_pages;
> + unsigned int found_pages;
> + unsigned int flags;
> + unsigned long required_mask;
> + unsigned long anyof_mask;
> + unsigned long excluded_mask;
> + unsigned long return_mask;
> +};
> +
> +static int pagemap_scan_pmd_test_walk(unsigned long start, unsigned long end, struct mm_walk *walk)
> +{
> + struct pagemap_scan_private *p = walk->private;
> + struct vm_area_struct *vma = walk->vma;
> +
> + if (IS_GET_OP(p) && p->max_pages && (p->found_pages == p->max_pages))
> + return -1;
> +
> + if (vma->vm_flags & VM_PFNMAP)
> + return 1;
> +
> + return 0;
> +}
> +
> +static int add_to_out(bool sd, bool file, bool pres, bool swap, struct pagemap_scan_private *p,
> + unsigned long addr, unsigned int len)
> + file <<>
Should we define contants for each of these bits?
> + bool cpy = true;
> +
> + if (p->required_mask)
> + cpy = ((p->required_mask & cur) == p->required_mask);
> + if (cpy && p->anyof_mask)
> + cpy = (p->anyof_mask & cur);
> + if (cpy && p->excluded_mask)
> + cpy = !(p->excluded_mask & cur);
> +
> + bitmap = cur & p->return_mask;
> +
> + if (cpy && bitmap) {
> + if ((p->vec_index) && (p->vec[p->vec_index – 1].bitmap == bitmap) &&
> + (p->vec[p->vec_index – 1].start + p->vec[p->vec_index – 1].len * PAGE_SIZE ==
> + addr)) {
I think it is better to define a variable for p->vec_index – 1.
nit: len can be in bytes rather than pages.
> + p->vec[p->vec_index – 1].len += len;
> + p->found_pages += len;
> + } else if (p->vec_index vec_len) {
> + p->vec[p->vec_index].start = addr;
> + p->vec[p->vec_index].len = len;
> + p->found_pages += len;
> + p->vec[p->vec_index].bitmap = bitmap;
> + p->vec_index++;
> + } else {
> + return -ENOMEM;
> + }
> + }
> +
> + return 0;
> +
> +
> +static int pagemap_scan_pmd_entry(pmd_t *pmd, unsigned long addr,
> + unsigned long end, struct mm_walk *walk)
> +
> + struct pagemap_scan_private *p = walk->private;
> + struct vm_area_struct *vma = walk->vma;
> + unsigned int len;
> + spinlock_t *ptl;
> + int ret = 0;
> + pte_t *pte;
> + bool dirty_vma = (p->flags & PAGEMAP_NO_REUSED_REGIONS) ?
> + (false) : (vma->vm_flags & VM_SOFTDIRTY);
> +
> + if ((walk->vma->vm_end max_pages && p->found_pages == p->max_pages))
> + return 0;
> +
> + end = min(end, walk->vma->vm_end);
> +
> + ptl = pmd_trans_huge_lock(pmd, vma);
> + if (ptl)
> + spin_unlock(ptl);
> + return 0;
> +
> +
> +process_smaller_pages:
> + if (pmd_trans_unstable(pmd))
> + return 0;
> +
> + pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
> + for (; addr max_pages || (p->found_pages max_pages))
> + ; pte++, addr += PAGE_SIZE) check_soft_dirty(vma, addr, pte, false),
> + vma->vm_file, pte_present(*pte),
> + is_swap_pte(*pte), p, addr, 1);
> + if (!ret && IS_CLEAR_OP(p))
> + check_soft_dirty(vma, addr, pte, true);
> +
> + pte_unmap_unlock(pte – 1, ptl);
> + cond_resched();
> +
> + return 0;
> +}
> +
> +static int pagemap_scan_pte_hole(unsigned long addr, unsigned long end, int depth,
> + struct mm_walk *walk)
> +{
> + struct pagemap_scan_private *p = walk->private;
> + struct vm_area_struct *vma = walk->vma;
> + unsigned int len;
> + bool sd;
> +
> + if (vma) {
> + /* Individual pages haven’t been allocated and written */
> + sd = (p->flags & PAGEMAP_NO_REUSED_REGIONS) ? (false) :
> + (vma->vm_flags & VM_SOFTDIRTY);
> +
> + len = (end – addr)/PAGE_SIZE;
> + if (p->max_pages && p->found_pages + len > p->max_pages)
> + len = p->max_pages – p->found_pages;
> +
> + add_to_out(sd, vma->vm_file, false, false, p, addr, len);
> + }
> +
> + return 0;
> +}
> +
> +#ifdef CONFIG_MEM_SOFT_DIRTY
> +static int pagemap_scan_pre_vma(unsigned long start, unsigned long end, struct mm_walk *walk)
> +{
> + struct pagemap_scan_private *p = walk->private;
> + struct vm_area_struct *vma = walk->vma;
> + unsigned long end_cut = end;
> + int ret;
> +
> + if (!(p->flags & PAGEMAP_NO_REUSED_REGIONS) && IS_CLEAR_OP(p) &&
> + (vma->vm_flags & VM_SOFTDIRTY)) {
> + if (vma->vm_start + ret = split_vma(vma->vm_mm, vma, start, 1);
> + if (ret)
> + return ret;
> + }
> + /* Calculate end_cut because of max_pages */
> + if (IS_GET_OP(p) && p->max_pages)
> + end_cut = min(start + (p->max_pages – p->found_pages) * PAGE_SIZE, end);
> +
> + if (vma->vm_end > end_cut) {
> + ret = split_vma(vma->vm_mm, vma, end_cut, 0);
> + if (ret)
> + return ret;
> + }
> + }
> +
> + return 0;
> +}
> +
> +static void pagemap_scan_post_vma(struct mm_walk *walk)
> +{
> + struct pagemap_scan_private *p = walk->private;
> + struct vm_area_struct *vma = walk->vma;
> +
> + if (!(p->flags & PAGEMAP_NO_REUSED_REGIONS) && IS_CLEAR_OP(p) &&
> + (vma->vm_flags & VM_SOFTDIRTY)) {
> + vma->vm_flags &= ~VM_SOFTDIRTY;
> + vma_set_page_prot(vma);
> + }
> +}
> +#endif /* CONFIG_MEM_SOFT_DIRTY */
> +
> +static const struct mm_walk_ops pagemap_scan_ops = {
> + .test_walk = pagemap_scan_pmd_test_walk,
> + .pmd_entry = pagemap_scan_pmd_entry,
> + .pte_hole = pagemap_scan_pte_hole,
> +
> +#ifdef CONFIG_MEM_SOFT_DIRTY
> + /* Only for clearing SD bit over VMAs */
> + .pre_vma = pagemap_scan_pre_vma,
> + .post_vma = pagemap_scan_post_vma,
> +#endif /* CONFIG_MEM_SOFT_DIRTY */
> +};
> +
> +static long do_pagemap_sd_cmd(struct mm_struct *mm, struct pagemap_scan_arg *arg)
> + static void pagemap_scan_post_vma(struct mm_walk *walk) { struct pagemap_scan_private *p = walk->private; struct vm_area_struct *vma = walk->vma; if (!(p->flags & PAGEMAP_NO_REUSED_REGIONS) && IS_CLEAR_OP(p) && (vma->vm_flags & VM_SOFTDIRTY)) { vma->vm_flags &= ~VM_SOFTDIRTY; vma_set_page_prot(vma); } } #endif /* CONFIG_MEM_SOFT_DIRTY */ static const struct mm_walk_ops pagemap_scan_ops = { .test_walk = pagemap_scan_pmd_test_walk, .pmd_entry = pagemap_scan_pmd_entry, .pte_hole = pagemap_scan_pte_hole, #ifdef CONFIG_MEM_SOFT_DIRTY /* Only for clearing SD bit over VMAs */ .pre_vma = pagemap_scan_pre_vma, .post_vma = pagemap_scan_post_vma, #endif /* CONFIG_MEM_SOFT_DIRTY */ }; static long do_pagemap_sd_cmd(struct mm_struct *mm, struct pagemap_scan_arg *arg) nested] 18+ messages in thread

* Re: [PATCH v6 2/3] fs/proc/task_mmu: Implement IOCTL to get and/or the clear info about PTEs :54 ` Andrei Vagin@ : ` Muhammad Usama Anjum
0 siblings, 0 replies; 18+ messages in thread
From: Muhammad Usama Anjum @ :10 UTC (permalink / raw)
To: Andrei Vagin
Cc: Muhammad Usama Anjum, Michał Mirosław,
Danylo Mocherniuk, Alexander Viro, Andrew Morton,
Suren Baghdasaryan, Greg KH, Christian Brauner, Peter Xu,
Yang Shi, Vlastimil Babka, Zach O’Keefe,
Matthew Wilcox (Oracle),
Gustavo A. R. Silva, Dan Williams, kernel,
Gabriel Krisman Bertazi, David Hildenbrand, Peter Enderborg,
open list : KERNEL SELFTEST FRAMEWORK, Shuah Khan, open list,
open list : PROC FILESYSTEM, open list : MEMORY MANAGEMENT,
Paul Gofman
Hello Andrei,
Thank you for reviewing.
On 11/10/22 4:54 AM, Andrei Vagin wrote:
[…]
>> +static int add_to_out(bool sd, bool file, bool pres, bool swap, struct pagemap_scan_private *p,
>> + unsigned long addr, unsigned int len)
>> +
>> + unsigned long bitmap, cur = sd