* [PATCH] mm/memory.c: do_numa_page(): remove a redundant page table read
@ 2024-02-28 3:41 John Hubbard
2024-02-28 9:11 ` David Hildenbrand
2024-02-29 11:35 ` Ryan Roberts
0 siblings, 2 replies; 3+ messages in thread
From: John Hubbard @ 2024-02-28 3:41 UTC (permalink / raw)
To: Andrew Morton
Cc: LKML, linux-mm, John Hubbard, Ryan Roberts, David Hildenbrand
do_numa_page() is reading from the same page table entry, twice, while
holding the page table lock: once while checking that the pte hasn't
changed, and again in order to modify the pte.
Instead, just read the pte once, and save it in the same old_pte
variable that already exists. This has no effect on behavior, other than
to provide a tiny potential improvement to performance, by avoiding the
redundant memory read (which the compiler cannot elide, due to
READ_ONCE()).
Also improve the associated comments nearby.
Cc: Ryan Roberts <ryan.roberts@arm.com>
Cc: David Hildenbrand <david@redhat.com>
Signed-off-by: John Hubbard <jhubbard@nvidia.com>
---
mm/memory.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/mm/memory.c b/mm/memory.c
index 0bfc8b007c01..df0711982901 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -4928,18 +4928,18 @@ static vm_fault_t do_numa_page(struct vm_fault *vmf)
int flags = 0;
/*
- * The "pte" at this point cannot be used safely without
- * validation through pte_unmap_same(). It's of NUMA type but
- * the pfn may be screwed if the read is non atomic.
+ * The pte cannot be used safely until we verify, while holding the page
+ * table lock, that its contents have not changed during fault handling.
*/
spin_lock(vmf->ptl);
- if (unlikely(!pte_same(ptep_get(vmf->pte), vmf->orig_pte))) {
+ /* Read the live PTE from the page tables: */
+ old_pte = ptep_get(vmf->pte);
+
+ if (unlikely(!pte_same(old_pte, vmf->orig_pte))) {
pte_unmap_unlock(vmf->pte, vmf->ptl);
goto out;
}
- /* Get the normal PTE */
- old_pte = ptep_get(vmf->pte);
pte = pte_modify(old_pte, vma->vm_page_prot);
/*
--
2.44.0
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] mm/memory.c: do_numa_page(): remove a redundant page table read
2024-02-28 3:41 [PATCH] mm/memory.c: do_numa_page(): remove a redundant page table read John Hubbard
@ 2024-02-28 9:11 ` David Hildenbrand
2024-02-29 11:35 ` Ryan Roberts
1 sibling, 0 replies; 3+ messages in thread
From: David Hildenbrand @ 2024-02-28 9:11 UTC (permalink / raw)
To: John Hubbard, Andrew Morton; +Cc: LKML, linux-mm, Ryan Roberts
On 28.02.24 04:41, John Hubbard wrote:
> do_numa_page() is reading from the same page table entry, twice, while
> holding the page table lock: once while checking that the pte hasn't
> changed, and again in order to modify the pte.
>
> Instead, just read the pte once, and save it in the same old_pte
> variable that already exists. This has no effect on behavior, other than
> to provide a tiny potential improvement to performance, by avoiding the
> redundant memory read (which the compiler cannot elide, due to
> READ_ONCE()).
>
> Also improve the associated comments nearby.
>
> Cc: Ryan Roberts <ryan.roberts@arm.com>
> Cc: David Hildenbrand <david@redhat.com>
> Signed-off-by: John Hubbard <jhubbard@nvidia.com>
> ---
> mm/memory.c | 12 ++++++------
> 1 file changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/mm/memory.c b/mm/memory.c
> index 0bfc8b007c01..df0711982901 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -4928,18 +4928,18 @@ static vm_fault_t do_numa_page(struct vm_fault *vmf)
> int flags = 0;
>
> /*
> - * The "pte" at this point cannot be used safely without
> - * validation through pte_unmap_same(). It's of NUMA type but
> - * the pfn may be screwed if the read is non atomic.
> + * The pte cannot be used safely until we verify, while holding the page
> + * table lock, that its contents have not changed during fault handling.
> */
> spin_lock(vmf->ptl);
> - if (unlikely(!pte_same(ptep_get(vmf->pte), vmf->orig_pte))) {
> + /* Read the live PTE from the page tables: */
> + old_pte = ptep_get(vmf->pte);
> +
> + if (unlikely(!pte_same(old_pte, vmf->orig_pte))) {
> pte_unmap_unlock(vmf->pte, vmf->ptl);
> goto out;
> }
>
> - /* Get the normal PTE */
> - old_pte = ptep_get(vmf->pte);
> pte = pte_modify(old_pte, vma->vm_page_prot);
>
> /*
Reviewed-by: David Hildenbrand <david@redhat.com>
--
Cheers,
David / dhildenb
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] mm/memory.c: do_numa_page(): remove a redundant page table read
2024-02-28 3:41 [PATCH] mm/memory.c: do_numa_page(): remove a redundant page table read John Hubbard
2024-02-28 9:11 ` David Hildenbrand
@ 2024-02-29 11:35 ` Ryan Roberts
1 sibling, 0 replies; 3+ messages in thread
From: Ryan Roberts @ 2024-02-29 11:35 UTC (permalink / raw)
To: John Hubbard, Andrew Morton; +Cc: LKML, linux-mm, David Hildenbrand
On 28/02/2024 03:41, John Hubbard wrote:
> do_numa_page() is reading from the same page table entry, twice, while
> holding the page table lock: once while checking that the pte hasn't
> changed, and again in order to modify the pte.
>
> Instead, just read the pte once, and save it in the same old_pte
> variable that already exists. This has no effect on behavior, other than
> to provide a tiny potential improvement to performance, by avoiding the
> redundant memory read (which the compiler cannot elide, due to
> READ_ONCE()).
>
> Also improve the associated comments nearby.
>
> Cc: Ryan Roberts <ryan.roberts@arm.com>
> Cc: David Hildenbrand <david@redhat.com>
> Signed-off-by: John Hubbard <jhubbard@nvidia.com>
Reviewed-by: Ryan Roberts <ryan.roberts@arm.com>
> ---
> mm/memory.c | 12 ++++++------
> 1 file changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/mm/memory.c b/mm/memory.c
> index 0bfc8b007c01..df0711982901 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -4928,18 +4928,18 @@ static vm_fault_t do_numa_page(struct vm_fault *vmf)
> int flags = 0;
>
> /*
> - * The "pte" at this point cannot be used safely without
> - * validation through pte_unmap_same(). It's of NUMA type but
> - * the pfn may be screwed if the read is non atomic.
> + * The pte cannot be used safely until we verify, while holding the page
> + * table lock, that its contents have not changed during fault handling.
> */
> spin_lock(vmf->ptl);
> - if (unlikely(!pte_same(ptep_get(vmf->pte), vmf->orig_pte))) {
> + /* Read the live PTE from the page tables: */
> + old_pte = ptep_get(vmf->pte);
> +
> + if (unlikely(!pte_same(old_pte, vmf->orig_pte))) {
> pte_unmap_unlock(vmf->pte, vmf->ptl);
> goto out;
> }
>
> - /* Get the normal PTE */
> - old_pte = ptep_get(vmf->pte);
> pte = pte_modify(old_pte, vma->vm_page_prot);
>
> /*
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-02-29 11:35 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-28 3:41 [PATCH] mm/memory.c: do_numa_page(): remove a redundant page table read John Hubbard
2024-02-28 9:11 ` David Hildenbrand
2024-02-29 11:35 ` Ryan Roberts
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox