* [PATCH] mm: pagemap: restrict pagewalk to the requested range
@ 2023-05-15 17:26 Yuanchu Xie
2023-05-15 22:05 ` Peter Xu
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Yuanchu Xie @ 2023-05-15 17:26 UTC (permalink / raw)
To: Andrew Morton
Cc: Liam R . Howlett, Yang Shi, Zach O'Keefe, Peter Xu,
Kirill A . Shutemov, Matthew Wilcox, Pasha Tatashin, Yuanchu Xie,
linux-kernel, linux-fsdevel, linux-mm
The pagewalk in pagemap_read reads one PTE past the end of the requested
range, and stops when the buffer runs out of space. While it produces
the right result, the extra read is unnecessary and less performant.
I timed the following command before and after this patch:
dd count=100000 if=/proc/self/pagemap of=/dev/null
The results are consistently within 0.001s across 5 runs.
Before:
100000+0 records in
100000+0 records out
51200000 bytes (51 MB) copied, 0.0763159 s, 671 MB/s
real 0m0.078s
user 0m0.012s
sys 0m0.065s
After:
100000+0 records in
100000+0 records out
51200000 bytes (51 MB) copied, 0.0487928 s, 1.0 GB/s
real 0m0.050s
user 0m0.011s
sys 0m0.039s
Signed-off-by: Yuanchu Xie <yuanchu@google.com>
---
fs/proc/task_mmu.c | 12 ++++++------
1 file changed, 6 insertions(+), 6 deletions(-)
diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
index 420510f6a545..6259dd432eeb 100644
--- a/fs/proc/task_mmu.c
+++ b/fs/proc/task_mmu.c
@@ -1689,23 +1689,23 @@ static ssize_t pagemap_read(struct file *file, char __user *buf,
/* watch out for wraparound */
start_vaddr = end_vaddr;
if (svpfn <= (ULONG_MAX >> PAGE_SHIFT)) {
+ unsigned long end;
+
ret = mmap_read_lock_killable(mm);
if (ret)
goto out_free;
start_vaddr = untagged_addr_remote(mm, svpfn << PAGE_SHIFT);
mmap_read_unlock(mm);
+
+ end = start_vaddr + ((count / PM_ENTRY_BYTES) << PAGE_SHIFT);
+ if (end >= start_vaddr && end < mm->task_size)
+ end_vaddr = end;
}
/* Ensure the address is inside the task */
if (start_vaddr > mm->task_size)
start_vaddr = end_vaddr;
- /*
- * The odds are that this will stop walking way
- * before end_vaddr, because the length of the
- * user buffer is tracked in "pm", and the walk
- * will stop when we hit the end of the buffer.
- */
ret = 0;
while (count && (start_vaddr < end_vaddr)) {
int len;
--
2.40.1.606.ga4b1b128d6-goog
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] mm: pagemap: restrict pagewalk to the requested range
2023-05-15 17:26 [PATCH] mm: pagemap: restrict pagewalk to the requested range Yuanchu Xie
@ 2023-05-15 22:05 ` Peter Xu
2023-05-16 7:45 ` Mike Rapoport
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Peter Xu @ 2023-05-15 22:05 UTC (permalink / raw)
To: Yuanchu Xie
Cc: Andrew Morton, Liam R . Howlett, Yang Shi, Zach O'Keefe,
Kirill A . Shutemov, Matthew Wilcox, Pasha Tatashin,
linux-kernel, linux-fsdevel, linux-mm
On Tue, May 16, 2023 at 01:26:08AM +0800, Yuanchu Xie wrote:
> The pagewalk in pagemap_read reads one PTE past the end of the requested
> range, and stops when the buffer runs out of space. While it produces
> the right result, the extra read is unnecessary and less performant.
>
> I timed the following command before and after this patch:
> dd count=100000 if=/proc/self/pagemap of=/dev/null
> The results are consistently within 0.001s across 5 runs.
>
> Before:
> 100000+0 records in
> 100000+0 records out
> 51200000 bytes (51 MB) copied, 0.0763159 s, 671 MB/s
>
> real 0m0.078s
> user 0m0.012s
> sys 0m0.065s
>
> After:
> 100000+0 records in
> 100000+0 records out
> 51200000 bytes (51 MB) copied, 0.0487928 s, 1.0 GB/s
>
> real 0m0.050s
> user 0m0.011s
> sys 0m0.039s
>
> Signed-off-by: Yuanchu Xie <yuanchu@google.com>
Acked-by: Peter Xu <peterx@redhat.com>
--
Peter Xu
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] mm: pagemap: restrict pagewalk to the requested range
2023-05-15 17:26 [PATCH] mm: pagemap: restrict pagewalk to the requested range Yuanchu Xie
2023-05-15 22:05 ` Peter Xu
@ 2023-05-16 7:45 ` Mike Rapoport
2023-05-16 17:10 ` Yang Shi
2023-05-21 23:55 ` David Rientjes
3 siblings, 0 replies; 5+ messages in thread
From: Mike Rapoport @ 2023-05-16 7:45 UTC (permalink / raw)
To: Yuanchu Xie
Cc: Andrew Morton, Liam R . Howlett, Yang Shi, Zach O'Keefe,
Peter Xu, Kirill A . Shutemov, Matthew Wilcox, Pasha Tatashin,
linux-kernel, linux-fsdevel, linux-mm
On Tue, May 16, 2023 at 01:26:08AM +0800, Yuanchu Xie wrote:
> The pagewalk in pagemap_read reads one PTE past the end of the requested
> range, and stops when the buffer runs out of space. While it produces
> the right result, the extra read is unnecessary and less performant.
>
> I timed the following command before and after this patch:
> dd count=100000 if=/proc/self/pagemap of=/dev/null
> The results are consistently within 0.001s across 5 runs.
>
> Before:
> 100000+0 records in
> 100000+0 records out
> 51200000 bytes (51 MB) copied, 0.0763159 s, 671 MB/s
>
> real 0m0.078s
> user 0m0.012s
> sys 0m0.065s
>
> After:
> 100000+0 records in
> 100000+0 records out
> 51200000 bytes (51 MB) copied, 0.0487928 s, 1.0 GB/s
>
> real 0m0.050s
> user 0m0.011s
> sys 0m0.039s
>
> Signed-off-by: Yuanchu Xie <yuanchu@google.com>
Acked-by: Mike Rapoport (IBM) <rppt@kernel.org>
> ---
> fs/proc/task_mmu.c | 12 ++++++------
> 1 file changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
> index 420510f6a545..6259dd432eeb 100644
> --- a/fs/proc/task_mmu.c
> +++ b/fs/proc/task_mmu.c
> @@ -1689,23 +1689,23 @@ static ssize_t pagemap_read(struct file *file, char __user *buf,
> /* watch out for wraparound */
> start_vaddr = end_vaddr;
> if (svpfn <= (ULONG_MAX >> PAGE_SHIFT)) {
> + unsigned long end;
> +
> ret = mmap_read_lock_killable(mm);
> if (ret)
> goto out_free;
> start_vaddr = untagged_addr_remote(mm, svpfn << PAGE_SHIFT);
> mmap_read_unlock(mm);
> +
> + end = start_vaddr + ((count / PM_ENTRY_BYTES) << PAGE_SHIFT);
> + if (end >= start_vaddr && end < mm->task_size)
> + end_vaddr = end;
> }
>
> /* Ensure the address is inside the task */
> if (start_vaddr > mm->task_size)
> start_vaddr = end_vaddr;
>
> - /*
> - * The odds are that this will stop walking way
> - * before end_vaddr, because the length of the
> - * user buffer is tracked in "pm", and the walk
> - * will stop when we hit the end of the buffer.
> - */
> ret = 0;
> while (count && (start_vaddr < end_vaddr)) {
> int len;
> --
> 2.40.1.606.ga4b1b128d6-goog
>
>
--
Sincerely yours,
Mike.
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] mm: pagemap: restrict pagewalk to the requested range
2023-05-15 17:26 [PATCH] mm: pagemap: restrict pagewalk to the requested range Yuanchu Xie
2023-05-15 22:05 ` Peter Xu
2023-05-16 7:45 ` Mike Rapoport
@ 2023-05-16 17:10 ` Yang Shi
2023-05-21 23:55 ` David Rientjes
3 siblings, 0 replies; 5+ messages in thread
From: Yang Shi @ 2023-05-16 17:10 UTC (permalink / raw)
To: Yuanchu Xie
Cc: Andrew Morton, Liam R . Howlett, Zach O'Keefe, Peter Xu,
Kirill A . Shutemov, Matthew Wilcox, Pasha Tatashin,
linux-kernel, linux-fsdevel, linux-mm
On Mon, May 15, 2023 at 10:26 AM Yuanchu Xie <yuanchu@google.com> wrote:
>
> The pagewalk in pagemap_read reads one PTE past the end of the requested
> range, and stops when the buffer runs out of space. While it produces
> the right result, the extra read is unnecessary and less performant.
>
> I timed the following command before and after this patch:
> dd count=100000 if=/proc/self/pagemap of=/dev/null
> The results are consistently within 0.001s across 5 runs.
>
> Before:
> 100000+0 records in
> 100000+0 records out
> 51200000 bytes (51 MB) copied, 0.0763159 s, 671 MB/s
>
> real 0m0.078s
> user 0m0.012s
> sys 0m0.065s
>
> After:
> 100000+0 records in
> 100000+0 records out
> 51200000 bytes (51 MB) copied, 0.0487928 s, 1.0 GB/s
>
> real 0m0.050s
> user 0m0.011s
> sys 0m0.039s
>
> Signed-off-by: Yuanchu Xie <yuanchu@google.com>
Reviewed-by: Yang Shi <shy828301@gmail.com>
> ---
> fs/proc/task_mmu.c | 12 ++++++------
> 1 file changed, 6 insertions(+), 6 deletions(-)
>
> diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
> index 420510f6a545..6259dd432eeb 100644
> --- a/fs/proc/task_mmu.c
> +++ b/fs/proc/task_mmu.c
> @@ -1689,23 +1689,23 @@ static ssize_t pagemap_read(struct file *file, char __user *buf,
> /* watch out for wraparound */
> start_vaddr = end_vaddr;
> if (svpfn <= (ULONG_MAX >> PAGE_SHIFT)) {
> + unsigned long end;
> +
> ret = mmap_read_lock_killable(mm);
> if (ret)
> goto out_free;
> start_vaddr = untagged_addr_remote(mm, svpfn << PAGE_SHIFT);
> mmap_read_unlock(mm);
> +
> + end = start_vaddr + ((count / PM_ENTRY_BYTES) << PAGE_SHIFT);
> + if (end >= start_vaddr && end < mm->task_size)
> + end_vaddr = end;
> }
>
> /* Ensure the address is inside the task */
> if (start_vaddr > mm->task_size)
> start_vaddr = end_vaddr;
>
> - /*
> - * The odds are that this will stop walking way
> - * before end_vaddr, because the length of the
> - * user buffer is tracked in "pm", and the walk
> - * will stop when we hit the end of the buffer.
> - */
> ret = 0;
> while (count && (start_vaddr < end_vaddr)) {
> int len;
> --
> 2.40.1.606.ga4b1b128d6-goog
>
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] mm: pagemap: restrict pagewalk to the requested range
2023-05-15 17:26 [PATCH] mm: pagemap: restrict pagewalk to the requested range Yuanchu Xie
` (2 preceding siblings ...)
2023-05-16 17:10 ` Yang Shi
@ 2023-05-21 23:55 ` David Rientjes
3 siblings, 0 replies; 5+ messages in thread
From: David Rientjes @ 2023-05-21 23:55 UTC (permalink / raw)
To: Yuanchu Xie
Cc: Andrew Morton, Liam R . Howlett, Yang Shi, Zach O'Keefe,
Peter Xu, Kirill A . Shutemov, Matthew Wilcox, Pasha Tatashin,
linux-kernel, linux-fsdevel, linux-mm
On Tue, 16 May 2023, Yuanchu Xie wrote:
> The pagewalk in pagemap_read reads one PTE past the end of the requested
> range, and stops when the buffer runs out of space. While it produces
> the right result, the extra read is unnecessary and less performant.
>
> I timed the following command before and after this patch:
> dd count=100000 if=/proc/self/pagemap of=/dev/null
> The results are consistently within 0.001s across 5 runs.
>
> Before:
> 100000+0 records in
> 100000+0 records out
> 51200000 bytes (51 MB) copied, 0.0763159 s, 671 MB/s
>
> real 0m0.078s
> user 0m0.012s
> sys 0m0.065s
>
> After:
> 100000+0 records in
> 100000+0 records out
> 51200000 bytes (51 MB) copied, 0.0487928 s, 1.0 GB/s
>
> real 0m0.050s
> user 0m0.011s
> sys 0m0.039s
>
> Signed-off-by: Yuanchu Xie <yuanchu@google.com>
Acked-by: David Rientjes <rientjes@google.com>
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2023-05-21 23:55 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-15 17:26 [PATCH] mm: pagemap: restrict pagewalk to the requested range Yuanchu Xie
2023-05-15 22:05 ` Peter Xu
2023-05-16 7:45 ` Mike Rapoport
2023-05-16 17:10 ` Yang Shi
2023-05-21 23:55 ` David Rientjes
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox