Message ID | 20210506232537.165788-4-peterx@redhat.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | mm/gup: Fix pin page write cache bouncing on has_pinned | expand |
On 5/6/21 4:25 PM, Peter Xu wrote: > From: Andrea Arcangeli <aarcange@redhat.com> > > 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. It's nice that you spelled that out. I don't see any technical problems with the diffs. There are a couple of tiny suggestions, below. > > Signed-off-by: Andrea Arcangeli <aarcange@redhat.com> > [peterx: Fix build for task_mmu.c] > Signed-off-by: Peter Xu <peterx@redhat.com> > --- > fs/proc/task_mmu.c | 2 +- > include/linux/mm.h | 2 +- > include/linux/mm_types.h | 10 ---------- > include/linux/sched/coredump.h | 1 + > kernel/fork.c | 1 - > mm/gup.c | 9 +++++---- > 6 files changed, 8 insertions(+), 17 deletions(-) > > diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c > index 4c95cc57a66a8..6144571942db9 100644 > --- a/fs/proc/task_mmu.c > +++ b/fs/proc/task_mmu.c > @@ -1049,7 +1049,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) > diff --git a/include/linux/mm.h b/include/linux/mm.h > index d6790ab0cf575..94dc84f6d8658 100644 > --- a/include/linux/mm.h > +++ b/include/linux/mm.h > @@ -1331,7 +1331,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); > diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h > index 6613b26a88946..15d79858fadbd 100644 > --- a/include/linux/mm_types.h > +++ b/include/linux/mm_types.h > @@ -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; > - > /** > * @write_protect_seq: Locked when any thread is write > * protecting pages mapped by this mm to enforce a later COW, > diff --git a/include/linux/sched/coredump.h b/include/linux/sched/coredump.h > index dfd82eab29025..bf45badd63e6d 100644 > --- a/include/linux/sched/coredump.h > +++ b/include/linux/sched/coredump.h > @@ -73,6 +73,7 @@ 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 */ > +#define MMF_HAS_PINNED 28 /* FOLL_PIN has run, never cleared */ How about this instead, so that we effectively retain the comment block that is otherwise being deleted from mm.h: /* * 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 ran. Never cleared. */ > #define MMF_DISABLE_THP_MASK (1 << MMF_DISABLE_THP) > > #define MMF_INIT_MASK (MMF_DUMPABLE_MASK | MMF_DUMP_FILTER_MASK |\ > diff --git a/kernel/fork.c b/kernel/fork.c > index 502dc046fbc62..a71e73707ef59 100644 > --- a/kernel/fork.c > +++ b/kernel/fork.c > @@ -1026,7 +1026,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); > diff --git a/mm/gup.c b/mm/gup.c > index 8b513e1723b45..78416b0909873 100644 > --- a/mm/gup.c > +++ b/mm/gup.c > @@ -1292,8 +1292,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 && !test_bit(MMF_HAS_PINNED, &mm->flags)) > + set_bit(MMF_HAS_PINNED, &mm->flags); I expect this suggestion to be controversial, but I'm going to float it anyway. The above is a little less clear than it used to be, *and* it is in two places so far, so how about factoring out a tiny subroutine, like this: diff --git a/mm/gup.c b/mm/gup.c index 036ab0de9457..2dc001a7c850 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1270,6 +1270,16 @@ int fixup_user_fault(struct mm_struct *mm, } EXPORT_SYMBOL_GPL(fixup_user_fault); +static void set_mm_has_pinned_flag(unsigned long *mm_flags) +{ + /* + * Avoid setting the bit unless necessary. This matters a lot with + * large SMP machines. + */ + if (!test_bit(MMF_HAS_PINNED, mm_flags)) + set_bit(MMF_HAS_PINNED, mm_flags); +} + /* * Please note that this function, unlike __get_user_pages will not * return 0 for nr_pages > 0 without FOLL_NOWAIT @@ -1292,8 +1302,8 @@ static __always_inline long __get_user_pages_locked(struct mm_struct *mm, BUG_ON(*locked != 1); } - if (flags & FOLL_PIN && !test_bit(MMF_HAS_PINNED, &mm->flags)) - set_bit(MMF_HAS_PINNED, &mm->flags); + if (flags & FOLL_PIN) + set_mm_has_pinned_flag(&mm->flags); ...which is now very readable, once again. > > /* > * FOLL_PIN and FOLL_GET are mutually exclusive. Traditional behavior > @@ -2617,8 +2617,9 @@ static int internal_get_user_pages_fast(unsigned long start, > FOLL_FAST_ONLY))) > return -EINVAL; > > - if (gup_flags & FOLL_PIN && !atomic_read(¤t->mm->has_pinned)) > - atomic_set(¤t->mm->has_pinned, 1); > + if (gup_flags & FOLL_PIN && > + !test_bit(MMF_HAS_PINNED, ¤t->mm->flags)) > + set_bit(MMF_HAS_PINNED, ¤t->mm->flags); > > if (!(gup_flags & FOLL_FAST_ONLY)) > might_lock_read(¤t->mm->mmap_lock); > thanks,
On Thu, May 6, 2021 at 11:43 PM John Hubbard <jhubbard@nvidia.com> wrote: > > +static void set_mm_has_pinned_flag(unsigned long *mm_flags) > +{ > + /* > + * Avoid setting the bit unless necessary. This matters a lot with > + * large SMP machines. > + */ > + if (!test_bit(MMF_HAS_PINNED, mm_flags)) > + set_bit(MMF_HAS_PINNED, mm_flags); > +} Yes, please do split it up like this. But please make it explicitly inline, and move the comment to above the function. And add the important key part to it: that the bit is never cleared. That idempotent behavior of the "set_bit()" is what makes it safe to do this non-atomic test-and-set (yes, the "set_bit()" itself is atomic, but the sequence above is not). Side note: we do have a few other places where this kind of thing happens, so it *might* make sense to even make this a generic pattern in case somebody can come up with a good descriptive name for that ("set_bit_if_not_set()" sounds descriptive, but the subtle non-atomicity should probably be part of it). > + if (flags & FOLL_PIN) > + set_mm_has_pinned_flag(&mm->flags); > > ...which is now very readable, once again. Yes, that does look much better. Thanks, Linus
On Thu, May 06, 2021 at 11:42:59PM -0700, John Hubbard wrote:
> +static void set_mm_has_pinned_flag(unsigned long *mm_flags)
mm_set_has_pinned_flag(struct mm_struct *mm), please.
On Thu, May 06, 2021 at 11:42:59PM -0700, John Hubbard wrote: > > +#define MMF_HAS_PINNED 28 /* FOLL_PIN has run, never cleared */ > > How about this instead, so that we effectively retain the comment block > that is otherwise being deleted from mm.h: > > /* > * 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 ran. Never cleared. */ Sure, good to know the comment is still valid! > > @@ -1292,8 +1292,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 && !test_bit(MMF_HAS_PINNED, &mm->flags)) > > + set_bit(MMF_HAS_PINNED, &mm->flags); > > I expect this suggestion to be controversial, but I'm going to float it > anyway. The above is a little less clear than it used to be, *and* it is > in two places so far, so how about factoring out a tiny subroutine, like this: Definitely less "controversial" than expected, isn't it? ;) Thanks for the suggestion, it looks much better indeed. Also I'll rename the helper to mm_set_has_pinned_flag() as suggested by Matthew.
diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 4c95cc57a66a8..6144571942db9 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -1049,7 +1049,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) diff --git a/include/linux/mm.h b/include/linux/mm.h index d6790ab0cf575..94dc84f6d8658 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -1331,7 +1331,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); diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h index 6613b26a88946..15d79858fadbd 100644 --- a/include/linux/mm_types.h +++ b/include/linux/mm_types.h @@ -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; - /** * @write_protect_seq: Locked when any thread is write * protecting pages mapped by this mm to enforce a later COW, diff --git a/include/linux/sched/coredump.h b/include/linux/sched/coredump.h index dfd82eab29025..bf45badd63e6d 100644 --- a/include/linux/sched/coredump.h +++ b/include/linux/sched/coredump.h @@ -73,6 +73,7 @@ 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 */ +#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 |\ diff --git a/kernel/fork.c b/kernel/fork.c index 502dc046fbc62..a71e73707ef59 100644 --- a/kernel/fork.c +++ b/kernel/fork.c @@ -1026,7 +1026,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); diff --git a/mm/gup.c b/mm/gup.c index 8b513e1723b45..78416b0909873 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1292,8 +1292,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 && !test_bit(MMF_HAS_PINNED, &mm->flags)) + set_bit(MMF_HAS_PINNED, &mm->flags); /* * FOLL_PIN and FOLL_GET are mutually exclusive. Traditional behavior @@ -2617,8 +2617,9 @@ static int internal_get_user_pages_fast(unsigned long start, FOLL_FAST_ONLY))) return -EINVAL; - if (gup_flags & FOLL_PIN && !atomic_read(¤t->mm->has_pinned)) - atomic_set(¤t->mm->has_pinned, 1); + if (gup_flags & FOLL_PIN && + !test_bit(MMF_HAS_PINNED, ¤t->mm->flags)) + set_bit(MMF_HAS_PINNED, ¤t->mm->flags); if (!(gup_flags & FOLL_FAST_ONLY)) might_lock_read(¤t->mm->mmap_lock);