2024-04-29 11:40:44

by Ryan Roberts

[permalink] [raw]
Subject: [PATCH v1] fs/proc/task_mmu: Fix loss of young/dirty bits during pagemap scan

make_uffd_wp_pte() was previously doing:

pte = ptep_get(ptep);
ptep_modify_prot_start(ptep);
pte = pte_mkuffd_wp(pte);
ptep_modify_prot_commit(ptep, pte);

But if another thread accessed or dirtied the pte between the first 2
calls, this could lead to loss of that information. Since
ptep_modify_prot_start() gets and clears atomically, the following is
the correct pattern and prevents any possible race. Any access after the
first call would see an invalid pte and cause a fault:

pte = ptep_modify_prot_start(ptep);
pte = pte_mkuffd_wp(pte);
ptep_modify_prot_commit(ptep, pte);

Fixes: 52526ca7fdb9 ("fs/proc/task_mmu: implement IOCTL to get and optionally clear info about PTEs")
Signed-off-by: Ryan Roberts <[email protected]>
---
fs/proc/task_mmu.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index 23fbab954c20..af4bc1da0c01 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -1825,7 +1825,7 @@ static void make_uffd_wp_pte(struct vm_area_struct *vma,
pte_t old_pte;

old_pte = ptep_modify_prot_start(vma, addr, pte);
- ptent = pte_mkuffd_wp(ptent);
+ ptent = pte_mkuffd_wp(old_pte);
ptep_modify_prot_commit(vma, addr, pte, old_pte, ptent);
} else if (is_swap_pte(ptent)) {
ptent = pte_swp_mkuffd_wp(ptent);
--
2.25.1



2024-04-29 12:25:06

by David Hildenbrand

[permalink] [raw]
Subject: Re: [PATCH v1] fs/proc/task_mmu: Fix loss of young/dirty bits during pagemap scan

On 29.04.24 13:40, Ryan Roberts wrote:
> make_uffd_wp_pte() was previously doing:
>
> pte = ptep_get(ptep);
> ptep_modify_prot_start(ptep);
> pte = pte_mkuffd_wp(pte);
> ptep_modify_prot_commit(ptep, pte);
>
> But if another thread accessed or dirtied the pte between the first 2
> calls, this could lead to loss of that information. Since
> ptep_modify_prot_start() gets and clears atomically, the following is
> the correct pattern and prevents any possible race. Any access after the
> first call would see an invalid pte and cause a fault:
>
> pte = ptep_modify_prot_start(ptep);
> pte = pte_mkuffd_wp(pte);
> ptep_modify_prot_commit(ptep, pte);
>
> Fixes: 52526ca7fdb9 ("fs/proc/task_mmu: implement IOCTL to get and optionally clear info about PTEs")
> Signed-off-by: Ryan Roberts <[email protected]>
> ---
> fs/proc/task_mmu.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
> index 23fbab954c20..af4bc1da0c01 100644
> --- a/fs/proc/task_mmu.c
> +++ b/fs/proc/task_mmu.c
> @@ -1825,7 +1825,7 @@ static void make_uffd_wp_pte(struct vm_area_struct *vma,
> pte_t old_pte;
>
> old_pte = ptep_modify_prot_start(vma, addr, pte);
> - ptent = pte_mkuffd_wp(ptent);
> + ptent = pte_mkuffd_wp(old_pte);
> ptep_modify_prot_commit(vma, addr, pte, old_pte, ptent);

Acked-by: David Hildenbrand <[email protected]>

--
Cheers,

David / dhildenb


2024-04-29 13:37:26

by Muhammad Usama Anjum

[permalink] [raw]
Subject: Re: [PATCH v1] fs/proc/task_mmu: Fix loss of young/dirty bits during pagemap scan

Thanks!

On 4/29/24 4:40 PM, Ryan Roberts wrote:
> make_uffd_wp_pte() was previously doing:
>
> pte = ptep_get(ptep);
> ptep_modify_prot_start(ptep);
> pte = pte_mkuffd_wp(pte);
> ptep_modify_prot_commit(ptep, pte);
>
> But if another thread accessed or dirtied the pte between the first 2
> calls, this could lead to loss of that information. Since
> ptep_modify_prot_start() gets and clears atomically, the following is
> the correct pattern and prevents any possible race. Any access after the
> first call would see an invalid pte and cause a fault:
>
> pte = ptep_modify_prot_start(ptep);
> pte = pte_mkuffd_wp(pte);
> ptep_modify_prot_commit(ptep, pte);
>
> Fixes: 52526ca7fdb9 ("fs/proc/task_mmu: implement IOCTL to get and optionally clear info about PTEs")
> Signed-off-by: Ryan Roberts <[email protected]>
Reviewed-by: Muhammad Usama Anjum <[email protected]>
Tested-by: Muhammad Usama Anjum <[email protected]>

> ---
> fs/proc/task_mmu.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
> index 23fbab954c20..af4bc1da0c01 100644
> --- a/fs/proc/task_mmu.c
> +++ b/fs/proc/task_mmu.c
> @@ -1825,7 +1825,7 @@ static void make_uffd_wp_pte(struct vm_area_struct *vma,
> pte_t old_pte;
>
> old_pte = ptep_modify_prot_start(vma, addr, pte);
> - ptent = pte_mkuffd_wp(ptent);
> + ptent = pte_mkuffd_wp(old_pte);
> ptep_modify_prot_commit(vma, addr, pte, old_pte, ptent);
> } else if (is_swap_pte(ptent)) {
> ptent = pte_swp_mkuffd_wp(ptent);
> --
> 2.25.1
>

--
BR,
Muhammad Usama Anjum

2024-04-29 15:55:22

by Ryan Roberts

[permalink] [raw]
Subject: Re: [PATCH v1] fs/proc/task_mmu: Fix loss of young/dirty bits during pagemap scan

On 29/04/2024 12:40, Ryan Roberts wrote:
> make_uffd_wp_pte() was previously doing:
>
> pte = ptep_get(ptep);
> ptep_modify_prot_start(ptep);
> pte = pte_mkuffd_wp(pte);
> ptep_modify_prot_commit(ptep, pte);
>
> But if another thread accessed or dirtied the pte between the first 2
> calls, this could lead to loss of that information. Since
> ptep_modify_prot_start() gets and clears atomically, the following is
> the correct pattern and prevents any possible race. Any access after the
> first call would see an invalid pte and cause a fault:
>
> pte = ptep_modify_prot_start(ptep);
> pte = pte_mkuffd_wp(pte);
> ptep_modify_prot_commit(ptep, pte);
>
> Fixes: 52526ca7fdb9 ("fs/proc/task_mmu: implement IOCTL to get and optionally clear info about PTEs")
> Signed-off-by: Ryan Roberts <[email protected]>

I guess this should have cc'ed stable but I forgot to add it. Are you able to
fix this up when you take it, Andrew, or do I need to repost?

> ---
> fs/proc/task_mmu.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
> index 23fbab954c20..af4bc1da0c01 100644
> --- a/fs/proc/task_mmu.c
> +++ b/fs/proc/task_mmu.c
> @@ -1825,7 +1825,7 @@ static void make_uffd_wp_pte(struct vm_area_struct *vma,
> pte_t old_pte;
>
> old_pte = ptep_modify_prot_start(vma, addr, pte);
> - ptent = pte_mkuffd_wp(ptent);
> + ptent = pte_mkuffd_wp(old_pte);
> ptep_modify_prot_commit(vma, addr, pte, old_pte, ptent);
> } else if (is_swap_pte(ptent)) {
> ptent = pte_swp_mkuffd_wp(ptent);
> --
> 2.25.1
>


2024-04-29 17:23:41

by Andrew Morton

[permalink] [raw]
Subject: Re: [PATCH v1] fs/proc/task_mmu: Fix loss of young/dirty bits during pagemap scan

On Mon, 29 Apr 2024 16:55:07 +0100 Ryan Roberts <[email protected]> wrote:

> > Fixes: 52526ca7fdb9 ("fs/proc/task_mmu: implement IOCTL to get and optionally clear info about PTEs")
> > Signed-off-by: Ryan Roberts <[email protected]>
>
> I guess this should have cc'ed stable but I forgot to add it. Are you able to
> fix this up when you take it, Andrew, or do I need to repost?

I have made that change.