Skip to content

Commit 2c7ad9a

Browse files
Ryan Robertsakpm00
authored andcommitted
fs/proc/task_mmu: fix uffd-wp confusion in pagemap_scan_pmd_entry()
pagemap_scan_pmd_entry() checks if uffd-wp is set on each pte to avoid unnecessary if set. However it was previously checking with `pte_uffd_wp(ptep_get(pte))` without first confirming that the pte was present. It is only valid to call pte_uffd_wp() for present ptes. For swap ptes, pte_swp_uffd_wp() must be called because the uffd-wp bit may be kept in a different position, depending on the arch. This was leading to test failures in the pagemap_ioctl mm selftest, when bringing up uffd-wp support on arm64 due to incorrectly interpretting the uffd-wp status of migration entries. Let's fix this by using the correct check based on pte_present(). While we are at it, let's pass the pte to make_uffd_wp_pte() to avoid the pointless extra ptep_get() which can't be optimized out due to READ_ONCE() on many arches. Link: https://lkml.kernel.org/r/20240429114104.182890-1-ryan.roberts@arm.com Fixes: 12f6b01 ("fs/proc/task_mmu: add fast paths to get/clear PAGE_IS_WRITTEN flag") Closes: https://lore.kernel.org/linux-arm-kernel/ZiuyGXt0XWwRgFh9@x1n/ Signed-off-by: Ryan Roberts <ryan.roberts@arm.com> Acked-by: David Hildenbrand <david@redhat.com> Reviewed-by: Muhammad Usama Anjum <usama.anjum@collabora.com> Tested-by: Muhammad Usama Anjum <usama.anjum@collabora.com> Cc: Peter Xu <peterx@redhat.com> Cc: <stable@vger.kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
1 parent c70dce4 commit 2c7ad9a

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

fs/proc/task_mmu.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1817,10 +1817,8 @@ static unsigned long pagemap_page_category(struct pagemap_scan_private *p,
18171817
}
18181818

18191819
static void make_uffd_wp_pte(struct vm_area_struct *vma,
1820-
unsigned long addr, pte_t *pte)
1820+
unsigned long addr, pte_t *pte, pte_t ptent)
18211821
{
1822-
pte_t ptent = ptep_get(pte);
1823-
18241822
if (pte_present(ptent)) {
18251823
pte_t old_pte;
18261824

@@ -2175,9 +2173,12 @@ static int pagemap_scan_pmd_entry(pmd_t *pmd, unsigned long start,
21752173
if ((p->arg.flags & PM_SCAN_WP_MATCHING) && !p->vec_out) {
21762174
/* Fast path for performing exclusive WP */
21772175
for (addr = start; addr != end; pte++, addr += PAGE_SIZE) {
2178-
if (pte_uffd_wp(ptep_get(pte)))
2176+
pte_t ptent = ptep_get(pte);
2177+
2178+
if ((pte_present(ptent) && pte_uffd_wp(ptent)) ||
2179+
pte_swp_uffd_wp_any(ptent))
21792180
continue;
2180-
make_uffd_wp_pte(vma, addr, pte);
2181+
make_uffd_wp_pte(vma, addr, pte, ptent);
21812182
if (!flush_end)
21822183
start = addr;
21832184
flush_end = addr + PAGE_SIZE;
@@ -2190,16 +2191,18 @@ static int pagemap_scan_pmd_entry(pmd_t *pmd, unsigned long start,
21902191
p->arg.return_mask == PAGE_IS_WRITTEN) {
21912192
for (addr = start; addr < end; pte++, addr += PAGE_SIZE) {
21922193
unsigned long next = addr + PAGE_SIZE;
2194+
pte_t ptent = ptep_get(pte);
21932195

2194-
if (pte_uffd_wp(ptep_get(pte)))
2196+
if ((pte_present(ptent) && pte_uffd_wp(ptent)) ||
2197+
pte_swp_uffd_wp_any(ptent))
21952198
continue;
21962199
ret = pagemap_scan_output(p->cur_vma_category | PAGE_IS_WRITTEN,
21972200
p, addr, &next);
21982201
if (next == addr)
21992202
break;
22002203
if (~p->arg.flags & PM_SCAN_WP_MATCHING)
22012204
continue;
2202-
make_uffd_wp_pte(vma, addr, pte);
2205+
make_uffd_wp_pte(vma, addr, pte, ptent);
22032206
if (!flush_end)
22042207
start = addr;
22052208
flush_end = next;
@@ -2208,8 +2211,9 @@ static int pagemap_scan_pmd_entry(pmd_t *pmd, unsigned long start,
22082211
}
22092212

22102213
for (addr = start; addr != end; pte++, addr += PAGE_SIZE) {
2214+
pte_t ptent = ptep_get(pte);
22112215
unsigned long categories = p->cur_vma_category |
2212-
pagemap_page_category(p, vma, addr, ptep_get(pte));
2216+
pagemap_page_category(p, vma, addr, ptent);
22132217
unsigned long next = addr + PAGE_SIZE;
22142218

22152219
if (!pagemap_scan_is_interesting_page(categories, p))
@@ -2224,7 +2228,7 @@ static int pagemap_scan_pmd_entry(pmd_t *pmd, unsigned long start,
22242228
if (~categories & PAGE_IS_WRITTEN)
22252229
continue;
22262230

2227-
make_uffd_wp_pte(vma, addr, pte);
2231+
make_uffd_wp_pte(vma, addr, pte, ptent);
22282232
if (!flush_end)
22292233
start = addr;
22302234
flush_end = next;

0 commit comments

Comments
 (0)