mm: gup: pack has_pinned in MMF_HAS_PINNED
authorAndrea Arcangeli <aarcange@redhat.com>
Tue, 29 Jun 2021 02:36:40 +0000 (19:36 -0700)
committerLinus Torvalds <torvalds@linux-foundation.org>
Tue, 29 Jun 2021 17:53:48 +0000 (10:53 -0700)
has_pinned 32bit can be packed in the MMF_HAS_PINNED bit as a noop
cleanup.

Any atomic_inc/dec to the mm cacheline shared by all threads in pin-fast
would reintroduce a loss of SMP scalability to pin-fast, so there's no
future potential usefulness to keep an atomic in the mm for this.

set_bit(MMF_HAS_PINNED) will be theoretically a bit slower than WRITE_ONCE
(atomic_set is equivalent to WRITE_ONCE), but the set_bit (just like
atomic_set after this commit) has to be still issued only once per "mm",
so the difference between the two will be lost in the noise.

will-it-scale "mmap2" shows no change in performance with enterprise
config as expected.

will-it-scale "pin_fast" retains the > 4000% SMP scalability performance
improvement against upstream as expected.

This is a noop as far as overall performance and SMP scalability are
concerned.

[peterx@redhat.com: pack has_pinned in MMF_HAS_PINNED]
Link: https://lkml.kernel.org/r/YJqWESqyxa8OZA+2@t490s
[akpm@linux-foundation.org: coding style fixes]
[peterx@redhat.com: fix build for task_mmu.c, introduce mm_set_has_pinned_flag, fix comments]

Link: https://lkml.kernel.org/r/20210507150553.208763-4-peterx@redhat.com
Signed-off-by: Andrea Arcangeli <aarcange@redhat.com>
Signed-off-by: Peter Xu <peterx@redhat.com>
Reviewed-by: John Hubbard <jhubbard@nvidia.com>
Cc: Hugh Dickins <hughd@google.com>
Cc: Jan Kara <jack@suse.cz>
Cc: Jann Horn <jannh@google.com>
Cc: Jason Gunthorpe <jgg@nvidia.com>
Cc: Kirill Shutemov <kirill@shutemov.name>
Cc: Kirill Tkhai <ktkhai@virtuozzo.com>
Cc: Matthew Wilcox <willy@infradead.org>
Cc: Michal Hocko <mhocko@suse.com>
Cc: Oleg Nesterov <oleg@redhat.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
fs/proc/task_mmu.c
include/linux/mm.h
include/linux/mm_types.h
include/linux/sched/coredump.h
kernel/fork.c
mm/gup.c

index fc97845..66965ad 100644 (file)
@@ -1047,7 +1047,7 @@ static inline bool pte_is_pinned(struct vm_area_struct *vma, unsigned long addr,
                return false;
        if (!is_cow_mapping(vma->vm_flags))
                return false;
-       if (likely(!atomic_read(&vma->vm_mm->has_pinned)))
+       if (likely(!test_bit(MMF_HAS_PINNED, &vma->vm_mm->flags)))
                return false;
        page = vm_normal_page(vma, addr, pte);
        if (!page)
index e39ed49..79f3296 100644 (file)
@@ -1341,7 +1341,7 @@ static inline bool page_needs_cow_for_dma(struct vm_area_struct *vma,
        if (!is_cow_mapping(vma->vm_flags))
                return false;
 
-       if (!atomic_read(&vma->vm_mm->has_pinned))
+       if (!test_bit(MMF_HAS_PINNED, &vma->vm_mm->flags))
                return false;
 
        return page_maybe_dma_pinned(page);
index 8f0fb62..b66d022 100644 (file)
@@ -435,16 +435,6 @@ struct mm_struct {
                 */
                atomic_t mm_count;
 
-               /**
-                * @has_pinned: Whether this mm has pinned any pages.  This can
-                * be either replaced in the future by @pinned_vm when it
-                * becomes stable, or grow into a counter on its own. We're
-                * aggresive on this bit now - even if the pinned pages were
-                * unpinned later on, we'll still keep this bit set for the
-                * lifecycle of this mm just for simplicity.
-                */
-               atomic_t has_pinned;
-
 #ifdef CONFIG_MMU
                atomic_long_t pgtables_bytes;   /* PTE page table pages */
 #endif
index dfd82ea..4d9e3a6 100644 (file)
@@ -73,6 +73,14 @@ static inline int get_dumpable(struct mm_struct *mm)
 #define MMF_OOM_VICTIM         25      /* mm is the oom victim */
 #define MMF_OOM_REAP_QUEUED    26      /* mm was queued for oom_reaper */
 #define MMF_MULTIPROCESS       27      /* mm is shared between processes */
+/*
+ * MMF_HAS_PINNED: Whether this mm has pinned any pages.  This can be either
+ * replaced in the future by mm.pinned_vm when it becomes stable, or grow into
+ * a counter on its own. We're aggresive on this bit for now: even if the
+ * pinned pages were unpinned later on, we'll still keep this bit set for the
+ * lifecycle of this mm, just for simplicity.
+ */
+#define MMF_HAS_PINNED         28      /* FOLL_PIN has run, never cleared */
 #define MMF_DISABLE_THP_MASK   (1 << MMF_DISABLE_THP)
 
 #define MMF_INIT_MASK          (MMF_DUMPABLE_MASK | MMF_DUMP_FILTER_MASK |\
index a070cae..c6747d5 100644 (file)
@@ -1029,7 +1029,6 @@ static struct mm_struct *mm_init(struct mm_struct *mm, struct task_struct *p,
        mm_pgtables_bytes_init(mm);
        mm->map_count = 0;
        mm->locked_vm = 0;
-       atomic_set(&mm->has_pinned, 0);
        atomic64_set(&mm->pinned_vm, 0);
        memset(&mm->rss_stat, 0, sizeof(mm->rss_stat));
        spin_lock_init(&mm->page_table_lock);
index a6c20a7..8651309 100644 (file)
--- a/mm/gup.c
+++ b/mm/gup.c
@@ -420,6 +420,17 @@ void unpin_user_pages(struct page **pages, unsigned long npages)
 }
 EXPORT_SYMBOL(unpin_user_pages);
 
+/*
+ * Set the MMF_HAS_PINNED if not set yet; after set it'll be there for the mm's
+ * lifecycle.  Avoid setting the bit unless necessary, or it might cause write
+ * cache bouncing on large SMP machines for concurrent pinned gups.
+ */
+static inline void mm_set_has_pinned_flag(unsigned long *mm_flags)
+{
+       if (!test_bit(MMF_HAS_PINNED, mm_flags))
+               set_bit(MMF_HAS_PINNED, mm_flags);
+}
+
 #ifdef CONFIG_MMU
 static struct page *no_page_table(struct vm_area_struct *vma,
                unsigned int flags)
@@ -1320,8 +1331,8 @@ static __always_inline long __get_user_pages_locked(struct mm_struct *mm,
                BUG_ON(*locked != 1);
        }
 
-       if ((flags & FOLL_PIN) && !atomic_read(&mm->has_pinned))
-               atomic_set(&mm->has_pinned, 1);
+       if (flags & FOLL_PIN)
+               mm_set_has_pinned_flag(&mm->flags);
 
        /*
         * FOLL_PIN and FOLL_GET are mutually exclusive. Traditional behavior
@@ -2641,8 +2652,8 @@ static int internal_get_user_pages_fast(unsigned long start,
                                       FOLL_FAST_ONLY)))
                return -EINVAL;
 
-       if ((gup_flags & FOLL_PIN) && !atomic_read(&current->mm->has_pinned))
-               atomic_set(&current->mm->has_pinned, 1);
+       if (gup_flags & FOLL_PIN)
+               mm_set_has_pinned_flag(&current->mm->flags);
 
        if (!(gup_flags & FOLL_FAST_ONLY))
                might_lock_read(&current->mm->mmap_lock);