2024-03-06 23:24:22

by Richard Weinberger

[permalink] [raw]
Subject: [PATCH 1/2] [RFC] proc: pagemap: Expose whether a PTE is writable

Is a PTE present and writable, bit 58 will be set.
This allows detecting CoW memory mappings and other mappings
where a write access will cause a page fault.

Signed-off-by: Richard Weinberger <[email protected]>
---
fs/proc/task_mmu.c | 8 ++++++++
1 file changed, 8 insertions(+)

diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index 3f78ebbb795f..7c7e0e954c02 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -1341,6 +1341,7 @@ struct pagemapread {
#define PM_SOFT_DIRTY BIT_ULL(55)
#define PM_MMAP_EXCLUSIVE BIT_ULL(56)
#define PM_UFFD_WP BIT_ULL(57)
+#define PM_WRITE BIT_ULL(58)
#define PM_FILE BIT_ULL(61)
#define PM_SWAP BIT_ULL(62)
#define PM_PRESENT BIT_ULL(63)
@@ -1417,6 +1418,8 @@ static pagemap_entry_t pte_to_pagemap_entry(struct pagemapread *pm,
flags |= PM_SOFT_DIRTY;
if (pte_uffd_wp(pte))
flags |= PM_UFFD_WP;
+ if (pte_write(pte))
+ flags |= PM_WRITE;
} else if (is_swap_pte(pte)) {
swp_entry_t entry;
if (pte_swp_soft_dirty(pte))
@@ -1483,6 +1486,8 @@ static int pagemap_pmd_range(pmd_t *pmdp, unsigned long addr, unsigned long end,
flags |= PM_SOFT_DIRTY;
if (pmd_uffd_wp(pmd))
flags |= PM_UFFD_WP;
+ if (pmd_write(pmd))
+ flags |= PM_WRITE;
if (pm->show_pfn)
frame = pmd_pfn(pmd) +
((addr & ~PMD_MASK) >> PAGE_SHIFT);
@@ -1586,6 +1591,9 @@ static int pagemap_hugetlb_range(pte_t *ptep, unsigned long hmask,
if (huge_pte_uffd_wp(pte))
flags |= PM_UFFD_WP;

+ if (pte_write(pte))
+ flags |= PM_WRITE;
+
flags |= PM_PRESENT;
if (pm->show_pfn)
frame = pte_pfn(pte) +
--
2.35.3



2024-03-06 23:24:34

by Richard Weinberger

[permalink] [raw]
Subject: [PATCH 2/2] [RFC] pagemap.rst: Document write bit

Bit 58 denotes that a PTE is writable.
The main use case is detecting CoW mappings.

Signed-off-by: Richard Weinberger <[email protected]>
---
Documentation/admin-guide/mm/pagemap.rst | 8 +++++++-
1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/Documentation/admin-guide/mm/pagemap.rst b/Documentation/admin-guide/mm/pagemap.rst
index f5f065c67615..81ffe3601b96 100644
--- a/Documentation/admin-guide/mm/pagemap.rst
+++ b/Documentation/admin-guide/mm/pagemap.rst
@@ -21,7 +21,8 @@ There are four components to pagemap:
* Bit 56 page exclusively mapped (since 4.2)
* Bit 57 pte is uffd-wp write-protected (since 5.13) (see
Documentation/admin-guide/mm/userfaultfd.rst)
- * Bits 58-60 zero
+ * Bit 58 pte is writable (since 6.10)
+ * Bits 59-60 zero
* Bit 61 page is file-page or shared-anon (since 3.5)
* Bit 62 page swapped
* Bit 63 page present
@@ -37,6 +38,11 @@ There are four components to pagemap:
precisely which pages are mapped (or in swap) and comparing mapped
pages between processes.

+ Bit 58 is useful to detect CoW mappings; however, it does not indicate
+ whether the page mapping is writable or not. If an anonymous mapping is
+ writable but the write bit is not set, it means that the next write access
+ will cause a page fault, and copy-on-write will happen.
+
Efficient users of this interface will use ``/proc/pid/maps`` to
determine which areas of memory are actually mapped and llseek to
skip over unmapped regions.
--
2.35.3