linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Andrei Vagin <avagin@gmail.com>
To: Muhammad Usama Anjum <usama.anjum@collabora.com>
Cc: "Michał Mirosław" <emmir@google.com>,
	"Paul Gofman" <pgofman@codeweavers.com>,
	"Peter Xu" <peterx@redhat.com>,
	"David Hildenbrand" <david@redhat.com>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Danylo Mocherniuk" <mdanylo@google.com>,
	"Cyrill Gorcunov" <gorcunov@gmail.com>,
	"Mike Rapoport" <rppt@kernel.org>,
	"Nadav Amit" <namit@vmware.com>,
	"Alexander Viro" <viro@zeniv.linux.org.uk>,
	"Shuah Khan" <shuah@kernel.org>,
	"Christian Brauner" <brauner@kernel.org>,
	"Yang Shi" <shy828301@gmail.com>,
	"Vlastimil Babka" <vbabka@suse.cz>,
	"Liam R . Howlett" <Liam.Howlett@oracle.com>,
	"Yun Zhou" <yun.zhou@windriver.com>,
	"Suren Baghdasaryan" <surenb@google.com>,
	"Alex Sierra" <alex.sierra@amd.com>,
	"Matthew Wilcox" <willy@infradead.org>,
	"Pasha Tatashin" <pasha.tatashin@soleen.com>,
	"Axel Rasmussen" <axelrasmussen@google.com>,
	"Gustavo A . R . Silva" <gustavoars@kernel.org>,
	"Dan Williams" <dan.j.williams@intel.com>,
	linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org,
	linux-mm@kvack.org, linux-kselftest@vger.kernel.org,
	"Greg KH" <gregkh@linuxfoundation.org>,
	kernel@collabora.com, "Michał Mirosław" <mirq-linux@rere.qmqm.pl>
Subject: Re: WIP: Performance improvements
Date: Sun, 6 Aug 2023 20:32:53 -0700	[thread overview]
Message-ID: <CANaxB-wf6OeEAk0VtZoo6gJOBY9QAJHpO4=ctKHz1Nf8O1uw1g@mail.gmail.com> (raw)
In-Reply-To: <6b6a4e1c-a9e9-9592-d5b4-3c9210c8b650@collabora.com>

On Fri, Jul 28, 2023 at 4:02 AM Muhammad Usama Anjum
<usama.anjum@collabora.com> wrote:
>
> We are optimizing for more performance. Please find the change-set below
> for easy review before next revision and post your comments:
>
> - Replace memcpy() with direct copy as it proving to be very expensive
> - Don't check if PAGE_IS_FILE if no mask needs it as it is very
>   expensive to check per pte
> - Add question in comment for discussion purpose
> - Add fast path for exclusive WP for ptes
> ---
>  fs/proc/task_mmu.c | 54 ++++++++++++++++++++++++++++++++++++----------
>  1 file changed, 43 insertions(+), 11 deletions(-)
>
> diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c
> index 7e92c33635cab..879baf896ed0b 100644
> --- a/fs/proc/task_mmu.c
> +++ b/fs/proc/task_mmu.c
> @@ -1757,37 +1757,51 @@ static int pagemap_release(struct inode *inode,
> struct file *file)
>                                  PAGE_IS_HUGE)
>  #define PM_SCAN_FLAGS          (PM_SCAN_WP_MATCHING | PM_SCAN_CHECK_WPASYNC)
>
> +#define MASKS_OF_INTEREST(a)   (a.category_inverted | a.category_mask | \
> +                                a.category_anyof_mask | a.return_mask)
> +
>  struct pagemap_scan_private {
>         struct pm_scan_arg arg;
> +       unsigned long masks_of_interest;
>         unsigned long cur_vma_category;
>         struct page_region *vec_buf, cur_buf;
>         unsigned long vec_buf_len, vec_buf_index, found_pages, end_addr;
>         struct page_region __user *vec_out;
>  };
>
> -static unsigned long pagemap_page_category(struct vm_area_struct *vma,
> +static unsigned long pagemap_page_category(struct pagemap_scan_private *p,
> +                                          struct vm_area_struct *vma,
>                                            unsigned long addr, pte_t pte)
>  {
>         unsigned long categories = 0;
>
>         if (pte_present(pte)) {
> -               struct page *page = vm_normal_page(vma, addr, pte);
> +               struct page *page;
>
>                 categories |= PAGE_IS_PRESENT;
>                 if (!pte_uffd_wp(pte))
>                         categories |= PAGE_IS_WRITTEN;
> -               if (page && !PageAnon(page))
> -                       categories |= PAGE_IS_FILE;
> +
> +               if (p->masks_of_interest & PAGE_IS_FILE) {
> +                       page = vm_normal_page(vma, addr, pte);
> +                       if (page && !PageAnon(page))
> +                               categories |= PAGE_IS_FILE;
> +               }
> +
>                 if (is_zero_pfn(pte_pfn(pte)))
>                         categories |= PAGE_IS_PFNZERO;
>         } else if (is_swap_pte(pte)) {
> -               swp_entry_t swp = pte_to_swp_entry(pte);
> +               swp_entry_t swp;
>
>                 categories |= PAGE_IS_SWAPPED;
>                 if (!pte_swp_uffd_wp_any(pte))
>                         categories |= PAGE_IS_WRITTEN;
> -               if (is_pfn_swap_entry(swp) && !PageAnon(pfn_swap_entry_to_page(swp)))
> -                       categories |= PAGE_IS_FILE;
> +
> +               if (p->masks_of_interest & PAGE_IS_FILE) {
> +                       swp = pte_to_swp_entry(pte);
> +                       if (is_pfn_swap_entry(swp) && !PageAnon(pfn_swap_entry_to_page(swp)))
> +                               categories |= PAGE_IS_FILE;
> +               }
>         }
>
>         return categories;
> @@ -1957,9 +1971,7 @@ static bool pagemap_scan_push_range(unsigned long
> categories,
>                 if (p->vec_buf_index >= p->vec_buf_len)
>                         return false;
>
> -               memcpy(&p->vec_buf[p->vec_buf_index], cur_buf,
> -                      sizeof(*p->vec_buf));
> -               ++p->vec_buf_index;
> +               p->vec_buf[p->vec_buf_index++] = *cur_buf;
>         }
>
>         cur_buf->start = addr;
> @@ -2095,9 +2107,24 @@ static int pagemap_scan_pmd_entry(pmd_t *pmd,
> unsigned long start,
>                 return 0;
>         }
>
> +       if (!p->vec_buf) {
> +               /* Fast path for performing exclusive WP */
> +               for (addr = start; addr != end; pte++, addr += PAGE_SIZE) {
> +                       if (pte_uffd_wp(ptep_get(pte)))
> +                               continue;
> +                       make_uffd_wp_pte(vma, addr, pte);
> +                       if (!flush) {
> +                               start = addr;
> +                               flush = true;
> +                       }
> +               }
> +               ret = 0;
> +               goto flush_and_return;
> +       }
> +
>         for (addr = start; addr != end; pte++, addr += PAGE_SIZE) {
>                 unsigned long categories = p->cur_vma_category |
> -                                          pagemap_page_category(vma, addr, ptep_get(pte));
> +                                          pagemap_page_category(p, vma, addr, ptep_get(pte));
>                 unsigned long next = addr + PAGE_SIZE;
>
>                 ret = pagemap_scan_output(categories, p, addr, &next);
> @@ -2119,6 +2146,7 @@ static int pagemap_scan_pmd_entry(pmd_t *pmd,
> unsigned long start,
>                 }
>         }
>
> +flush_and_return:
>         if (flush)
>                 flush_tlb_range(vma, start, addr);
>
> @@ -2284,6 +2312,9 @@ static int pagemap_scan_init_bounce_buffer(struct
> pagemap_scan_private *p)
>          * consecutive ranges that have the same categories returned across
>          * walk_page_range() calls.
>          */
> +       // Question: Increasing the vec_buf_len increases the execution speed.
> +       // But it'll increase the memory needed to run the IOCTL. Can we do
> something here?
> +       // Right now only have space for 512 entries of page_region

The main problem here is that walk_page_range is executed for 512 pages.
I think we need to execute it for the whole range and interrupt it
when we need to
drain the bounce buffer.

For a trivial program that scans its address space the time is reduced from 20
seconds to 0.001 seconds. The test program and perf data are here:
https://gist.github.com/avagin/c5a22f3c78f8cb34281602dfe9c43d10

>         p->vec_buf_len = min_t(size_t, PAGEMAP_WALK_SIZE >> PAGE_SHIFT,
>                                p->arg.vec_len - 1);
>         p->vec_buf = kmalloc_array(p->vec_buf_len, sizeof(*p->vec_buf),
> @@ -2329,6 +2360,7 @@ static long do_pagemap_scan(struct mm_struct *mm,
> unsigned long uarg)
>         if (ret)
>                 return ret;
>
> +       p.masks_of_interest = MASKS_OF_INTEREST(p.arg);
>         ret = pagemap_scan_init_bounce_buffer(&p);
>         if (ret)
>                 return ret;
> --
> 2.39.2
>


  parent reply	other threads:[~2023-08-07  3:33 UTC|newest]

Thread overview: 20+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-27  9:36 [PATCH v26 0/5] Implement IOCTL to get and optionally clear info about PTEs Muhammad Usama Anjum
2023-07-27  9:36 ` [PATCH v26 1/5] userfaultfd: UFFD_FEATURE_WP_ASYNC Muhammad Usama Anjum
2023-07-27  9:36 ` [PATCH v26 2/5] fs/proc/task_mmu: Implement IOCTL to get and optionally clear info about PTEs Muhammad Usama Anjum
2023-07-27 11:40   ` Michał Mirosław
2023-07-27 11:48     ` Muhammad Usama Anjum
2023-07-28 11:02     ` WIP: Performance improvements Muhammad Usama Anjum
2023-07-28 11:13       ` Greg KH
2023-08-07  3:32       ` Andrei Vagin [this message]
2023-07-27 11:46   ` [PATCH v26 2/5] fs/proc/task_mmu: Implement IOCTL to get and optionally clear info about PTEs Michał Mirosław
2023-07-27 11:49     ` Muhammad Usama Anjum
2023-08-03 15:08   ` Andrei Vagin
2023-08-03 15:25     ` Michał Mirosław
2023-08-04 15:59       ` Andrei Vagin
2023-08-04 17:17         ` Michał Mirosław
2023-08-04  6:08     ` Muhammad Usama Anjum
2023-08-04 21:53   ` Andrei Vagin
2023-08-07  4:28     ` Muhammad Usama Anjum
2023-07-27  9:36 ` [PATCH v26 3/5] tools headers UAPI: Update linux/fs.h with the kernel sources Muhammad Usama Anjum
2023-07-27  9:36 ` [PATCH v26 4/5] mm/pagemap: add documentation of PAGEMAP_SCAN IOCTL Muhammad Usama Anjum
2023-07-27  9:36 ` [PATCH v26 5/5] selftests: mm: add pagemap ioctl tests Muhammad Usama Anjum

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CANaxB-wf6OeEAk0VtZoo6gJOBY9QAJHpO4=ctKHz1Nf8O1uw1g@mail.gmail.com' \
    --to=avagin@gmail.com \
    --cc=Liam.Howlett@oracle.com \
    --cc=akpm@linux-foundation.org \
    --cc=alex.sierra@amd.com \
    --cc=axelrasmussen@google.com \
    --cc=brauner@kernel.org \
    --cc=dan.j.williams@intel.com \
    --cc=david@redhat.com \
    --cc=emmir@google.com \
    --cc=gorcunov@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=gustavoars@kernel.org \
    --cc=kernel@collabora.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mdanylo@google.com \
    --cc=mirq-linux@rere.qmqm.pl \
    --cc=namit@vmware.com \
    --cc=pasha.tatashin@soleen.com \
    --cc=peterx@redhat.com \
    --cc=pgofman@codeweavers.com \
    --cc=rppt@kernel.org \
    --cc=shuah@kernel.org \
    --cc=shy828301@gmail.com \
    --cc=surenb@google.com \
    --cc=usama.anjum@collabora.com \
    --cc=vbabka@suse.cz \
    --cc=viro@zeniv.linux.org.uk \
    --cc=willy@infradead.org \
    --cc=yun.zhou@windriver.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox