Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mm: gup: pack has_pinned in MMF_HAS_PINNED

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>

authored by

Andrea Arcangeli and committed by
Linus Torvalds
a458b76a 292648ac

+25 -17
+1 -1
fs/proc/task_mmu.c
··· 1047 1047 return false; 1048 1048 if (!is_cow_mapping(vma->vm_flags)) 1049 1049 return false; 1050 - if (likely(!atomic_read(&vma->vm_mm->has_pinned))) 1050 + if (likely(!test_bit(MMF_HAS_PINNED, &vma->vm_mm->flags))) 1051 1051 return false; 1052 1052 page = vm_normal_page(vma, addr, pte); 1053 1053 if (!page)
+1 -1
include/linux/mm.h
··· 1341 1341 if (!is_cow_mapping(vma->vm_flags)) 1342 1342 return false; 1343 1343 1344 - if (!atomic_read(&vma->vm_mm->has_pinned)) 1344 + if (!test_bit(MMF_HAS_PINNED, &vma->vm_mm->flags)) 1345 1345 return false; 1346 1346 1347 1347 return page_maybe_dma_pinned(page);
-10
include/linux/mm_types.h
··· 435 435 */ 436 436 atomic_t mm_count; 437 437 438 - /** 439 - * @has_pinned: Whether this mm has pinned any pages. This can 440 - * be either replaced in the future by @pinned_vm when it 441 - * becomes stable, or grow into a counter on its own. We're 442 - * aggresive on this bit now - even if the pinned pages were 443 - * unpinned later on, we'll still keep this bit set for the 444 - * lifecycle of this mm just for simplicity. 445 - */ 446 - atomic_t has_pinned; 447 - 448 438 #ifdef CONFIG_MMU 449 439 atomic_long_t pgtables_bytes; /* PTE page table pages */ 450 440 #endif
+8
include/linux/sched/coredump.h
··· 73 73 #define MMF_OOM_VICTIM 25 /* mm is the oom victim */ 74 74 #define MMF_OOM_REAP_QUEUED 26 /* mm was queued for oom_reaper */ 75 75 #define MMF_MULTIPROCESS 27 /* mm is shared between processes */ 76 + /* 77 + * MMF_HAS_PINNED: Whether this mm has pinned any pages. This can be either 78 + * replaced in the future by mm.pinned_vm when it becomes stable, or grow into 79 + * a counter on its own. We're aggresive on this bit for now: even if the 80 + * pinned pages were unpinned later on, we'll still keep this bit set for the 81 + * lifecycle of this mm, just for simplicity. 82 + */ 83 + #define MMF_HAS_PINNED 28 /* FOLL_PIN has run, never cleared */ 76 84 #define MMF_DISABLE_THP_MASK (1 << MMF_DISABLE_THP) 77 85 78 86 #define MMF_INIT_MASK (MMF_DUMPABLE_MASK | MMF_DUMP_FILTER_MASK |\
-1
kernel/fork.c
··· 1029 1029 mm_pgtables_bytes_init(mm); 1030 1030 mm->map_count = 0; 1031 1031 mm->locked_vm = 0; 1032 - atomic_set(&mm->has_pinned, 0); 1033 1032 atomic64_set(&mm->pinned_vm, 0); 1034 1033 memset(&mm->rss_stat, 0, sizeof(mm->rss_stat)); 1035 1034 spin_lock_init(&mm->page_table_lock);
+15 -4
mm/gup.c
··· 420 420 } 421 421 EXPORT_SYMBOL(unpin_user_pages); 422 422 423 + /* 424 + * Set the MMF_HAS_PINNED if not set yet; after set it'll be there for the mm's 425 + * lifecycle. Avoid setting the bit unless necessary, or it might cause write 426 + * cache bouncing on large SMP machines for concurrent pinned gups. 427 + */ 428 + static inline void mm_set_has_pinned_flag(unsigned long *mm_flags) 429 + { 430 + if (!test_bit(MMF_HAS_PINNED, mm_flags)) 431 + set_bit(MMF_HAS_PINNED, mm_flags); 432 + } 433 + 423 434 #ifdef CONFIG_MMU 424 435 static struct page *no_page_table(struct vm_area_struct *vma, 425 436 unsigned int flags) ··· 1331 1320 BUG_ON(*locked != 1); 1332 1321 } 1333 1322 1334 - if ((flags & FOLL_PIN) && !atomic_read(&mm->has_pinned)) 1335 - atomic_set(&mm->has_pinned, 1); 1323 + if (flags & FOLL_PIN) 1324 + mm_set_has_pinned_flag(&mm->flags); 1336 1325 1337 1326 /* 1338 1327 * FOLL_PIN and FOLL_GET are mutually exclusive. Traditional behavior ··· 2652 2641 FOLL_FAST_ONLY))) 2653 2642 return -EINVAL; 2654 2643 2655 - if ((gup_flags & FOLL_PIN) && !atomic_read(&current->mm->has_pinned)) 2656 - atomic_set(&current->mm->has_pinned, 1); 2644 + if (gup_flags & FOLL_PIN) 2645 + mm_set_has_pinned_flag(&current->mm->flags); 2657 2646 2658 2647 if (!(gup_flags & FOLL_FAST_ONLY)) 2659 2648 might_lock_read(&current->mm->mmap_lock);