From patchwork Tue Aug 1 22:07:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13337303 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 56F22C0015E for ; Tue, 1 Aug 2023 22:07:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D6FBA2800FF; Tue, 1 Aug 2023 18:07:41 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CF98D2800C8; Tue, 1 Aug 2023 18:07:41 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B4A522800FF; Tue, 1 Aug 2023 18:07:41 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id A1B692800C8 for ; Tue, 1 Aug 2023 18:07:41 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 7749D1601B4 for ; Tue, 1 Aug 2023 22:07:41 +0000 (UTC) X-FDA: 81076923522.07.A1A5B02 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf17.hostedemail.com (Postfix) with ESMTP id 96C9B40011 for ; Tue, 1 Aug 2023 22:07:39 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=We6ajMOj; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of 3KoLJZAYKCIExzwjsglttlqj.htrqnsz2-rrp0fhp.twl@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3KoLJZAYKCIExzwjsglttlqj.htrqnsz2-rrp0fhp.twl@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690927659; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=71RLrK6Mb0aadLJnf8lNJkVJrOaKPnZ5THHnlI7+5Ec=; b=HHigOFpgwiOCOIdL+DCx9X5/MKWImRcczlF2dyOyLNfcp/IM02uXZmUIpmUsiZcKHN6NvN Dn3Lt/Cnoi/L4p+cldHtUvhI7g2j1TRsnUsIJRdYJARWItEnjut+BQcka5ITFlQ26Gf+i6 drKuX4RnDuYuJ4JETJl+NjJnRiGlRIc= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=We6ajMOj; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf17.hostedemail.com: domain of 3KoLJZAYKCIExzwjsglttlqj.htrqnsz2-rrp0fhp.twl@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3KoLJZAYKCIExzwjsglttlqj.htrqnsz2-rrp0fhp.twl@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690927659; a=rsa-sha256; cv=none; b=n3A/jXQjouBjVGKan6cZ9DbH4+S0J8jGmeVDHhCgVMV0NYvR++bulVpyKTH5vMLcsr6pO+ lFTPJpaRjP40sNEvUpuZnWDIuFyDiS+f4a6At/kdJhlJENXTn/q97Vk10GuCiR3hl1OK+t qGir/icEAfI61vmvAzGre5WbV5Mi8Q8= Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-d1ef7a6abacso6608756276.2 for ; Tue, 01 Aug 2023 15:07:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1690927658; x=1691532458; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=71RLrK6Mb0aadLJnf8lNJkVJrOaKPnZ5THHnlI7+5Ec=; b=We6ajMOjm3XiLm7Fm134AiRtQwnSaKR7S3ytCgA240I8V4Cqb5DZkxb3c4VMaCqc0w V9+bLiaYnBwtT7C9b9kFVxy1bS64OrsWrAPLxZe0GmEClT2rmBC2Z2xa+eQmQ5FWLxHC yKOrz9bPWJ1QsP/sCqkL5hEzAEJ4p0Oq7uGE6QZf2POo/0NInGTznuALO13yDaRSnc2p oZZzhAMRwlN0xh4Oa0sVS9pWPrSw8B9WQU8aZavQe2FOuTA14a9EsXtVQo3/CWEI9VAF I/xuYpBEFF/+CKYPkLJOsJbz86ql23LK4Gz+qkaslMFUpxQhmC5GMs/A1z5mZyNEbgLj 4MtA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690927658; x=1691532458; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=71RLrK6Mb0aadLJnf8lNJkVJrOaKPnZ5THHnlI7+5Ec=; b=UPPMNF4tUiDqHkqfsO9y0GHqc+zsY0K/vYerbAwlWE52dnaDfP+//mJmTT2xhUV888 RRjxiJRdH6wCkn0Tn4i2DxP/cfx5nDMI5TBRcSvjv5Sjacm5KroTtJggkwHWKP9GRE7r qnRu6feEJaeR0rb3AEVUZPgJgBDKhEbvinbXpetqLbkQM2xoaXprobBHu6rag0kz9l4T Xe5ld2BhqsaAc3h3JNlYMuBuUCxICk0GkDUZtEqIX11bMdS0xhKN82bzWZYoKTPcYDLt 0LetOUAM09We3+tEtNyThyqZdit2DdhRV/dJ9iBeRXLyGS8G8dMp2Ny9zDsPkT0QR82x EpRg== X-Gm-Message-State: ABy/qLbYaTYStWGo++kaTci2OiTcFraev9KIRktTaRTxlx/lUKDo3kwf mFoRBfLgm03YiW2tLFHZRnN0VpMcQFA= X-Google-Smtp-Source: APBJJlFGwkEKt2rXX6M4bqakEpSaHL2RCGKxP3yPF2zEGheLcWn6DqToyN/J0zTqHXgclRZUtfHfONSz1es= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:211c:a2ff:f17b:c5e9]) (user=surenb job=sendgmr) by 2002:a25:f89:0:b0:d0b:c67:de3b with SMTP id 131-20020a250f89000000b00d0b0c67de3bmr92009ybp.13.1690927658714; Tue, 01 Aug 2023 15:07:38 -0700 (PDT) Date: Tue, 1 Aug 2023 15:07:27 -0700 In-Reply-To: <20230801220733.1987762-1-surenb@google.com> Mime-Version: 1.0 References: <20230801220733.1987762-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.585.gd2178a4bd4-goog Message-ID: <20230801220733.1987762-2-surenb@google.com> Subject: [PATCH v2 1/6] mm: enable page walking API to lock vmas during the walk From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, jannh@google.com, willy@infradead.org, liam.howlett@oracle.com, david@redhat.com, peterx@redhat.com, ldufour@linux.ibm.com, vbabka@suse.cz, michel@lespinasse.org, jglisse@google.com, mhocko@suse.com, hannes@cmpxchg.org, dave@stgolabs.net, hughd@google.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, stable@vger.kernel.org, Suren Baghdasaryan , Linus Torvalds X-Rspam-User: X-Stat-Signature: 4aufc1fzfbcxcee1fx73btiyhep5zaud X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 96C9B40011 X-HE-Tag: 1690927659-421635 X-HE-Meta: U2FsdGVkX18e273+G+erpvd3E2x9JnZ/7k1nsEwgkEr72a3wSjbcp4cgDr91lJYbYeNJ6Y5ELUPzD1sYDFEGzbmYFjL3BWNmaTKueHpX18l68UHFT5boJl88fPJ0+GKLMk1AGtW/dPz6G+8XQL9wKLiCUJDQo+Wq/k7lnebSX0iEJ06433vZ+Xcepy3GYgGQSeGsM7KDjL5Jku1Y4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pjsaKG9uD+3li+cP9TIRx1bvIUW9f8EXmpC1NJ6IGF/5ZeI6XTGm/GYb1coobd2De9sDUMVQOIyYV617PArsN86jfh0OvSeKFH6C5gkZVfFhBod2Xd3q/PZNEIUJR4uykyBGDVSw3GXzVqKLzgP6f1xwwqUGGLOELlE3hjx/Lq8snN1QRkreV7fVytCiG63vIarwkRsbMvtKnfYOV1dHOmjdQUABQv0x83Wphk+uzvgdDuvmhR+bDHobquLkL/ELfplFhcybXSYo+O717/AeZWU6PgtJ9++4Pay/DMXA1GtTrW773VYOrCozKDHbqyZsSWJXsHbH8m/TtMFeeDAPyXDCGeZtlelS1MPYTHQe5WBWgzKj9RI/Tj5Tx9BfIsWev/0ZT7qdigVo13vfGGJty/2aRny7mUtoCBj6yxCuEZVSLOrbVNIsH1UWirMGj7B0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: walk_page_range() and friends often operate under write-locked mmap_lock. With introduction of vma locks, the vmas have to be locked as well during such walks to prevent concurrent page faults in these areas. Add an additional member to mm_walk_ops to indicate locking requirements for the walk. Cc: stable@vger.kernel.org # 6.4.x Suggested-by: Linus Torvalds Suggested-by: Jann Horn Signed-off-by: Suren Baghdasaryan --- arch/powerpc/mm/book3s64/subpage_prot.c | 1 + arch/riscv/mm/pageattr.c | 1 + arch/s390/mm/gmap.c | 5 ++++ fs/proc/task_mmu.c | 5 ++++ include/linux/pagewalk.h | 11 ++++++++ mm/damon/vaddr.c | 2 ++ mm/hmm.c | 1 + mm/ksm.c | 25 ++++++++++------- mm/madvise.c | 3 +++ mm/memcontrol.c | 2 ++ mm/memory-failure.c | 1 + mm/mempolicy.c | 22 +++++++++------ mm/migrate_device.c | 1 + mm/mincore.c | 1 + mm/mlock.c | 1 + mm/mprotect.c | 1 + mm/pagewalk.c | 36 ++++++++++++++++++++++--- mm/vmscan.c | 1 + 18 files changed, 100 insertions(+), 20 deletions(-) diff --git a/arch/powerpc/mm/book3s64/subpage_prot.c b/arch/powerpc/mm/book3s64/subpage_prot.c index 0dc85556dec5..ec98e526167e 100644 --- a/arch/powerpc/mm/book3s64/subpage_prot.c +++ b/arch/powerpc/mm/book3s64/subpage_prot.c @@ -145,6 +145,7 @@ static int subpage_walk_pmd_entry(pmd_t *pmd, unsigned long addr, static const struct mm_walk_ops subpage_walk_ops = { .pmd_entry = subpage_walk_pmd_entry, + .walk_lock = PGWALK_WRLOCK_VERIFY, }; static void subpage_mark_vma_nohuge(struct mm_struct *mm, unsigned long addr, diff --git a/arch/riscv/mm/pageattr.c b/arch/riscv/mm/pageattr.c index ea3d61de065b..161d0b34c2cb 100644 --- a/arch/riscv/mm/pageattr.c +++ b/arch/riscv/mm/pageattr.c @@ -102,6 +102,7 @@ static const struct mm_walk_ops pageattr_ops = { .pmd_entry = pageattr_pmd_entry, .pte_entry = pageattr_pte_entry, .pte_hole = pageattr_pte_hole, + .walk_lock = PGWALK_RDLOCK, }; static int __set_memory(unsigned long addr, int numpages, pgprot_t set_mask, diff --git a/arch/s390/mm/gmap.c b/arch/s390/mm/gmap.c index 9c8af31be970..906a7bfc2a78 100644 --- a/arch/s390/mm/gmap.c +++ b/arch/s390/mm/gmap.c @@ -2514,6 +2514,7 @@ static int thp_split_walk_pmd_entry(pmd_t *pmd, unsigned long addr, static const struct mm_walk_ops thp_split_walk_ops = { .pmd_entry = thp_split_walk_pmd_entry, + .walk_lock = PGWALK_WRLOCK_VERIFY, }; static inline void thp_split_mm(struct mm_struct *mm) @@ -2565,6 +2566,7 @@ static int __zap_zero_pages(pmd_t *pmd, unsigned long start, static const struct mm_walk_ops zap_zero_walk_ops = { .pmd_entry = __zap_zero_pages, + .walk_lock = PGWALK_WRLOCK, }; /* @@ -2655,6 +2657,7 @@ static const struct mm_walk_ops enable_skey_walk_ops = { .hugetlb_entry = __s390_enable_skey_hugetlb, .pte_entry = __s390_enable_skey_pte, .pmd_entry = __s390_enable_skey_pmd, + .walk_lock = PGWALK_WRLOCK, }; int s390_enable_skey(void) @@ -2692,6 +2695,7 @@ static int __s390_reset_cmma(pte_t *pte, unsigned long addr, static const struct mm_walk_ops reset_cmma_walk_ops = { .pte_entry = __s390_reset_cmma, + .walk_lock = PGWALK_WRLOCK, }; void s390_reset_cmma(struct mm_struct *mm) @@ -2728,6 +2732,7 @@ static int s390_gather_pages(pte_t *ptep, unsigned long addr, static const struct mm_walk_ops gather_pages_ops = { .pte_entry = s390_gather_pages, + .walk_lock = PGWALK_RDLOCK, }; /* diff --git a/fs/proc/task_mmu.c b/fs/proc/task_mmu.c index 507cd4e59d07..ef6ee330e3be 100644 --- a/fs/proc/task_mmu.c +++ b/fs/proc/task_mmu.c @@ -758,12 +758,14 @@ static int smaps_hugetlb_range(pte_t *pte, unsigned long hmask, static const struct mm_walk_ops smaps_walk_ops = { .pmd_entry = smaps_pte_range, .hugetlb_entry = smaps_hugetlb_range, + .walk_lock = PGWALK_RDLOCK, }; static const struct mm_walk_ops smaps_shmem_walk_ops = { .pmd_entry = smaps_pte_range, .hugetlb_entry = smaps_hugetlb_range, .pte_hole = smaps_pte_hole, + .walk_lock = PGWALK_RDLOCK, }; /* @@ -1245,6 +1247,7 @@ static int clear_refs_test_walk(unsigned long start, unsigned long end, static const struct mm_walk_ops clear_refs_walk_ops = { .pmd_entry = clear_refs_pte_range, .test_walk = clear_refs_test_walk, + .walk_lock = PGWALK_WRLOCK, }; static ssize_t clear_refs_write(struct file *file, const char __user *buf, @@ -1622,6 +1625,7 @@ static const struct mm_walk_ops pagemap_ops = { .pmd_entry = pagemap_pmd_range, .pte_hole = pagemap_pte_hole, .hugetlb_entry = pagemap_hugetlb_range, + .walk_lock = PGWALK_RDLOCK, }; /* @@ -1935,6 +1939,7 @@ static int gather_hugetlb_stats(pte_t *pte, unsigned long hmask, static const struct mm_walk_ops show_numa_ops = { .hugetlb_entry = gather_hugetlb_stats, .pmd_entry = gather_pte_stats, + .walk_lock = PGWALK_RDLOCK, }; /* diff --git a/include/linux/pagewalk.h b/include/linux/pagewalk.h index 27a6df448ee5..27cd1e59ccf7 100644 --- a/include/linux/pagewalk.h +++ b/include/linux/pagewalk.h @@ -6,6 +6,16 @@ struct mm_walk; +/* Locking requirement during a page walk. */ +enum page_walk_lock { + /* mmap_lock should be locked for read to stabilize the vma tree */ + PGWALK_RDLOCK = 0, + /* vma will be write-locked during the walk */ + PGWALK_WRLOCK = 1, + /* vma is expected to be already write-locked during the walk */ + PGWALK_WRLOCK_VERIFY = 2, +}; + /** * struct mm_walk_ops - callbacks for walk_page_range * @pgd_entry: if set, called for each non-empty PGD (top-level) entry @@ -66,6 +76,7 @@ struct mm_walk_ops { int (*pre_vma)(unsigned long start, unsigned long end, struct mm_walk *walk); void (*post_vma)(struct mm_walk *walk); + enum page_walk_lock walk_lock; }; /* diff --git a/mm/damon/vaddr.c b/mm/damon/vaddr.c index 2fcc9731528a..e0e59d420fca 100644 --- a/mm/damon/vaddr.c +++ b/mm/damon/vaddr.c @@ -386,6 +386,7 @@ static int damon_mkold_hugetlb_entry(pte_t *pte, unsigned long hmask, static const struct mm_walk_ops damon_mkold_ops = { .pmd_entry = damon_mkold_pmd_entry, .hugetlb_entry = damon_mkold_hugetlb_entry, + .walk_lock = PGWALK_RDLOCK, }; static void damon_va_mkold(struct mm_struct *mm, unsigned long addr) @@ -525,6 +526,7 @@ static int damon_young_hugetlb_entry(pte_t *pte, unsigned long hmask, static const struct mm_walk_ops damon_young_ops = { .pmd_entry = damon_young_pmd_entry, .hugetlb_entry = damon_young_hugetlb_entry, + .walk_lock = PGWALK_RDLOCK, }; static bool damon_va_young(struct mm_struct *mm, unsigned long addr, diff --git a/mm/hmm.c b/mm/hmm.c index 855e25e59d8f..277ddcab4947 100644 --- a/mm/hmm.c +++ b/mm/hmm.c @@ -562,6 +562,7 @@ static const struct mm_walk_ops hmm_walk_ops = { .pte_hole = hmm_vma_walk_hole, .hugetlb_entry = hmm_vma_walk_hugetlb_entry, .test_walk = hmm_vma_walk_test, + .walk_lock = PGWALK_RDLOCK, }; /** diff --git a/mm/ksm.c b/mm/ksm.c index ba266359da55..00c21fb4d94e 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -455,6 +455,12 @@ static int break_ksm_pmd_entry(pmd_t *pmd, unsigned long addr, unsigned long nex static const struct mm_walk_ops break_ksm_ops = { .pmd_entry = break_ksm_pmd_entry, + .walk_lock = PGWALK_RDLOCK, +}; + +static const struct mm_walk_ops break_ksm_lock_vma_ops = { + .pmd_entry = break_ksm_pmd_entry, + .walk_lock = PGWALK_WRLOCK, }; /* @@ -470,16 +476,17 @@ static const struct mm_walk_ops break_ksm_ops = { * of the process that owns 'vma'. We also do not want to enforce * protection keys here anyway. */ -static int break_ksm(struct vm_area_struct *vma, unsigned long addr) +static int break_ksm(struct vm_area_struct *vma, unsigned long addr, bool lock_vma) { vm_fault_t ret = 0; + const struct mm_walk_ops *ops = lock_vma ? + &break_ksm_lock_vma_ops : &break_ksm_ops; do { int ksm_page; cond_resched(); - ksm_page = walk_page_range_vma(vma, addr, addr + 1, - &break_ksm_ops, NULL); + ksm_page = walk_page_range_vma(vma, addr, addr + 1, ops, NULL); if (WARN_ON_ONCE(ksm_page < 0)) return ksm_page; if (!ksm_page) @@ -565,7 +572,7 @@ static void break_cow(struct ksm_rmap_item *rmap_item) mmap_read_lock(mm); vma = find_mergeable_vma(mm, addr); if (vma) - break_ksm(vma, addr); + break_ksm(vma, addr, false); mmap_read_unlock(mm); } @@ -871,7 +878,7 @@ static void remove_trailing_rmap_items(struct ksm_rmap_item **rmap_list) * in cmp_and_merge_page on one of the rmap_items we would be removing. */ static int unmerge_ksm_pages(struct vm_area_struct *vma, - unsigned long start, unsigned long end) + unsigned long start, unsigned long end, bool lock_vma) { unsigned long addr; int err = 0; @@ -882,7 +889,7 @@ static int unmerge_ksm_pages(struct vm_area_struct *vma, if (signal_pending(current)) err = -ERESTARTSYS; else - err = break_ksm(vma, addr); + err = break_ksm(vma, addr, lock_vma); } return err; } @@ -1029,7 +1036,7 @@ static int unmerge_and_remove_all_rmap_items(void) if (!(vma->vm_flags & VM_MERGEABLE) || !vma->anon_vma) continue; err = unmerge_ksm_pages(vma, - vma->vm_start, vma->vm_end); + vma->vm_start, vma->vm_end, false); if (err) goto error; } @@ -2530,7 +2537,7 @@ static int __ksm_del_vma(struct vm_area_struct *vma) return 0; if (vma->anon_vma) { - err = unmerge_ksm_pages(vma, vma->vm_start, vma->vm_end); + err = unmerge_ksm_pages(vma, vma->vm_start, vma->vm_end, true); if (err) return err; } @@ -2668,7 +2675,7 @@ int ksm_madvise(struct vm_area_struct *vma, unsigned long start, return 0; /* just ignore the advice */ if (vma->anon_vma) { - err = unmerge_ksm_pages(vma, start, end); + err = unmerge_ksm_pages(vma, start, end, true); if (err) return err; } diff --git a/mm/madvise.c b/mm/madvise.c index 886f06066622..bfe0e06427bd 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -233,6 +233,7 @@ static int swapin_walk_pmd_entry(pmd_t *pmd, unsigned long start, static const struct mm_walk_ops swapin_walk_ops = { .pmd_entry = swapin_walk_pmd_entry, + .walk_lock = PGWALK_RDLOCK, }; static void shmem_swapin_range(struct vm_area_struct *vma, @@ -534,6 +535,7 @@ static int madvise_cold_or_pageout_pte_range(pmd_t *pmd, static const struct mm_walk_ops cold_walk_ops = { .pmd_entry = madvise_cold_or_pageout_pte_range, + .walk_lock = PGWALK_RDLOCK, }; static void madvise_cold_page_range(struct mmu_gather *tlb, @@ -757,6 +759,7 @@ static int madvise_free_pte_range(pmd_t *pmd, unsigned long addr, static const struct mm_walk_ops madvise_free_walk_ops = { .pmd_entry = madvise_free_pte_range, + .walk_lock = PGWALK_RDLOCK, }; static int madvise_free_single_vma(struct vm_area_struct *vma, diff --git a/mm/memcontrol.c b/mm/memcontrol.c index e8ca4bdcb03c..315fd5f45e3c 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -6024,6 +6024,7 @@ static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd, static const struct mm_walk_ops precharge_walk_ops = { .pmd_entry = mem_cgroup_count_precharge_pte_range, + .walk_lock = PGWALK_RDLOCK, }; static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm) @@ -6303,6 +6304,7 @@ static int mem_cgroup_move_charge_pte_range(pmd_t *pmd, static const struct mm_walk_ops charge_walk_ops = { .pmd_entry = mem_cgroup_move_charge_pte_range, + .walk_lock = PGWALK_RDLOCK, }; static void mem_cgroup_move_charge(void) diff --git a/mm/memory-failure.c b/mm/memory-failure.c index ece5d481b5ff..6bfb762facab 100644 --- a/mm/memory-failure.c +++ b/mm/memory-failure.c @@ -831,6 +831,7 @@ static int hwpoison_hugetlb_range(pte_t *ptep, unsigned long hmask, static const struct mm_walk_ops hwp_walk_ops = { .pmd_entry = hwpoison_pte_range, .hugetlb_entry = hwpoison_hugetlb_range, + .walk_lock = PGWALK_RDLOCK, }; /* diff --git a/mm/mempolicy.c b/mm/mempolicy.c index c53f8beeb507..ec2eaceffd74 100644 --- a/mm/mempolicy.c +++ b/mm/mempolicy.c @@ -718,6 +718,14 @@ static const struct mm_walk_ops queue_pages_walk_ops = { .hugetlb_entry = queue_folios_hugetlb, .pmd_entry = queue_folios_pte_range, .test_walk = queue_pages_test_walk, + .walk_lock = PGWALK_RDLOCK, +}; + +static const struct mm_walk_ops queue_pages_lock_vma_walk_ops = { + .hugetlb_entry = queue_folios_hugetlb, + .pmd_entry = queue_folios_pte_range, + .test_walk = queue_pages_test_walk, + .walk_lock = PGWALK_WRLOCK, }; /* @@ -738,7 +746,7 @@ static const struct mm_walk_ops queue_pages_walk_ops = { static int queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end, nodemask_t *nodes, unsigned long flags, - struct list_head *pagelist) + struct list_head *pagelist, bool lock_vma) { int err; struct queue_pages qp = { @@ -749,8 +757,10 @@ queue_pages_range(struct mm_struct *mm, unsigned long start, unsigned long end, .end = end, .first = NULL, }; + const struct mm_walk_ops *ops = lock_vma ? + &queue_pages_lock_vma_walk_ops : &queue_pages_walk_ops; - err = walk_page_range(mm, start, end, &queue_pages_walk_ops, &qp); + err = walk_page_range(mm, start, end, ops, &qp); if (!qp.first) /* whole range in hole */ @@ -1078,7 +1088,7 @@ static int migrate_to_node(struct mm_struct *mm, int source, int dest, vma = find_vma(mm, 0); VM_BUG_ON(!(flags & (MPOL_MF_MOVE | MPOL_MF_MOVE_ALL))); queue_pages_range(mm, vma->vm_start, mm->task_size, &nmask, - flags | MPOL_MF_DISCONTIG_OK, &pagelist); + flags | MPOL_MF_DISCONTIG_OK, &pagelist, false); if (!list_empty(&pagelist)) { err = migrate_pages(&pagelist, alloc_migration_target, NULL, @@ -1321,12 +1331,8 @@ static long do_mbind(unsigned long start, unsigned long len, * Lock the VMAs before scanning for pages to migrate, to ensure we don't * miss a concurrently inserted page. */ - vma_iter_init(&vmi, mm, start); - for_each_vma_range(vmi, vma, end) - vma_start_write(vma); - ret = queue_pages_range(mm, start, end, nmask, - flags | MPOL_MF_INVERT, &pagelist); + flags | MPOL_MF_INVERT, &pagelist, true); if (ret < 0) { err = ret; diff --git a/mm/migrate_device.c b/mm/migrate_device.c index 8365158460ed..d5f492356e3e 100644 --- a/mm/migrate_device.c +++ b/mm/migrate_device.c @@ -279,6 +279,7 @@ static int migrate_vma_collect_pmd(pmd_t *pmdp, static const struct mm_walk_ops migrate_vma_walk_ops = { .pmd_entry = migrate_vma_collect_pmd, .pte_hole = migrate_vma_collect_hole, + .walk_lock = PGWALK_RDLOCK, }; /* diff --git a/mm/mincore.c b/mm/mincore.c index b7f7a516b26c..dad3622cc963 100644 --- a/mm/mincore.c +++ b/mm/mincore.c @@ -176,6 +176,7 @@ static const struct mm_walk_ops mincore_walk_ops = { .pmd_entry = mincore_pte_range, .pte_hole = mincore_unmapped_range, .hugetlb_entry = mincore_hugetlb, + .walk_lock = PGWALK_RDLOCK, }; /* diff --git a/mm/mlock.c b/mm/mlock.c index 0a0c996c5c21..479e09d0994c 100644 --- a/mm/mlock.c +++ b/mm/mlock.c @@ -371,6 +371,7 @@ static void mlock_vma_pages_range(struct vm_area_struct *vma, { static const struct mm_walk_ops mlock_walk_ops = { .pmd_entry = mlock_pte_range, + .walk_lock = PGWALK_WRLOCK_VERIFY, }; /* diff --git a/mm/mprotect.c b/mm/mprotect.c index 6f658d483704..3aef1340533a 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -568,6 +568,7 @@ static const struct mm_walk_ops prot_none_walk_ops = { .pte_entry = prot_none_pte_entry, .hugetlb_entry = prot_none_hugetlb_entry, .test_walk = prot_none_test, + .walk_lock = PGWALK_WRLOCK, }; int diff --git a/mm/pagewalk.c b/mm/pagewalk.c index 2022333805d3..9b2d23fbf4d3 100644 --- a/mm/pagewalk.c +++ b/mm/pagewalk.c @@ -400,6 +400,33 @@ static int __walk_page_range(unsigned long start, unsigned long end, return err; } +static inline void process_mm_walk_lock(struct mm_struct *mm, + enum page_walk_lock walk_lock) +{ + if (walk_lock == PGWALK_RDLOCK) + mmap_assert_locked(mm); + else + mmap_assert_write_locked(mm); +} + +static inline void process_vma_walk_lock(struct vm_area_struct *vma, + enum page_walk_lock walk_lock) +{ +#ifdef CONFIG_PER_VMA_LOCK + switch (walk_lock) { + case PGWALK_WRLOCK: + vma_start_write(vma); + break; + case PGWALK_WRLOCK_VERIFY: + vma_assert_write_locked(vma); + break; + case PGWALK_RDLOCK: + /* PGWALK_RDLOCK is handled by process_mm_walk_lock */ + break; + } +#endif +} + /** * walk_page_range - walk page table with caller specific callbacks * @mm: mm_struct representing the target process of page table walk @@ -459,7 +486,7 @@ int walk_page_range(struct mm_struct *mm, unsigned long start, if (!walk.mm) return -EINVAL; - mmap_assert_locked(walk.mm); + process_mm_walk_lock(walk.mm, ops->walk_lock); vma = find_vma(walk.mm, start); do { @@ -474,6 +501,7 @@ int walk_page_range(struct mm_struct *mm, unsigned long start, if (ops->pte_hole) err = ops->pte_hole(start, next, -1, &walk); } else { /* inside vma */ + process_vma_walk_lock(vma, ops->walk_lock); walk.vma = vma; next = min(end, vma->vm_end); vma = find_vma(mm, vma->vm_end); @@ -549,7 +577,8 @@ int walk_page_range_vma(struct vm_area_struct *vma, unsigned long start, if (start < vma->vm_start || end > vma->vm_end) return -EINVAL; - mmap_assert_locked(walk.mm); + process_mm_walk_lock(walk.mm, ops->walk_lock); + process_vma_walk_lock(vma, ops->walk_lock); return __walk_page_range(start, end, &walk); } @@ -566,7 +595,8 @@ int walk_page_vma(struct vm_area_struct *vma, const struct mm_walk_ops *ops, if (!walk.mm) return -EINVAL; - mmap_assert_locked(walk.mm); + process_mm_walk_lock(walk.mm, ops->walk_lock); + process_vma_walk_lock(vma, ops->walk_lock); return __walk_page_range(vma->vm_start, vma->vm_end, &walk); } diff --git a/mm/vmscan.c b/mm/vmscan.c index 1080209a568b..3555927df9b5 100644 --- a/mm/vmscan.c +++ b/mm/vmscan.c @@ -4284,6 +4284,7 @@ static void walk_mm(struct lruvec *lruvec, struct mm_struct *mm, struct lru_gen_ static const struct mm_walk_ops mm_walk_ops = { .test_walk = should_skip_vma, .p4d_entry = walk_pud_range, + .walk_lock = PGWALK_RDLOCK, }; int err; From patchwork Tue Aug 1 22:07:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13337304 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id D4C17C00528 for ; Tue, 1 Aug 2023 22:07:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 627C4280100; Tue, 1 Aug 2023 18:07:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 5A55B2800C8; Tue, 1 Aug 2023 18:07:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 4471D280100; Tue, 1 Aug 2023 18:07:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 31B252800C8 for ; Tue, 1 Aug 2023 18:07:44 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 01A051402B5 for ; Tue, 1 Aug 2023 22:07:43 +0000 (UTC) X-FDA: 81076923648.16.7AA918F Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf30.hostedemail.com (Postfix) with ESMTP id 0E96380021 for ; Tue, 1 Aug 2023 22:07:41 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=V9cpwSEL; spf=pass (imf30.hostedemail.com: domain of 3LYLJZAYKCIQ02zmvjowwotm.kwutqv25-uus3iks.wzo@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3LYLJZAYKCIQ02zmvjowwotm.kwutqv25-uus3iks.wzo@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690927662; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=i6HEpkZDiMCJl1HNBxUq/pPnwhYuhlhYWrvFQybpJWY=; b=VF5t0IX/YckN3A5/IPOzTIir0H9YW/4EIbvLTb9S0P0HGZxltwoj6d3R73xYtLI19QDPiX yenVr62t4sR1o1xPLRaLLUHf/vOI5SHPQSR19IyIGnk+g2/+aWOWhFWsTOyRxX8QK6+UT5 E3k7xvBxe8gBUWIldYH2UcUL0uXlXIE= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690927662; a=rsa-sha256; cv=none; b=pLhtXfQWcYimDlqYeqofRXLxJ/anICYs7Jdfv5pMo0WE+12X/PEiz0R33/NL+KJPYXsgYV EqT8DDxxGW+iOIqzTgnEs3U1xfy2bA+Oxe8hqxwu9LKsO1+XVpV1jd+PCNE/ZxAkf7ub5K SfSWGG2eHcxP+ZMMYCW3WdmRypz5ITY= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=V9cpwSEL; spf=pass (imf30.hostedemail.com: domain of 3LYLJZAYKCIQ02zmvjowwotm.kwutqv25-uus3iks.wzo@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3LYLJZAYKCIQ02zmvjowwotm.kwutqv25-uus3iks.wzo@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-58440eb872aso78282457b3.3 for ; Tue, 01 Aug 2023 15:07:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1690927661; x=1691532461; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=i6HEpkZDiMCJl1HNBxUq/pPnwhYuhlhYWrvFQybpJWY=; b=V9cpwSELJrkQrszXU5HyumPij806LjFOgAKqGQckrRIsTl9WyJGm7y2tP2Z2qpZNBZ 8OnG+G2+pRoWY7Gyh/uRKHckpG/wW2klLNffgsUon5TtBq05uulzu7VlS14UeHcVePc5 6rcXGltEDaVNS9+SeI1i+AP3HDVebAv3PFT1lfSUDf6ep/jTCQ/IUFd9ttrSEA9t9DPO iGN48DWEsdFNb4WBgDh2gwPyzp4LH+hHwffYSWHoJfq3HFOTa5CTWqxb2Bx96ZnBccWK 7hzPD483pGlx4dh0dSN4wMfxSLMa6kthAGUj3r6PC/2LNnNJKhb7gl7lRbdc8DmYXSji 2fsQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690927661; x=1691532461; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=i6HEpkZDiMCJl1HNBxUq/pPnwhYuhlhYWrvFQybpJWY=; b=NOSB5NwMSkJ/un78FyLkb1HqEypJmEq5niTC65SHJmG8D97+kCCpvty7Ozz6NDKjlM l8EHeVG87Nu1e5GGH55zP3dVRmK0lRUbTvSCzaIxMo72EUnB7gSbxZwwALdb1NcPRYAA ujNpYtpblJBWLF2ZOe38Oqki6LbrE95Nis6M/6hZ5o9qRDxCmN3lElFtnH0OyiaV4I3b iOC008CayYQmA+8xIHBd19ONHnthr4L3Jm4MRDfXU2XmPCIX00ekVH4aofVIRLEGKY2l h9FOJ8GPPZifbQn3IPlY5vi2h6nzdCdGkwS+zGGSfHPjob88OTrmsCbBFtrPhydFG/2Q wSYQ== X-Gm-Message-State: ABy/qLYXw+k25nq2WOL51POe/hypRTIJZAnF319QYrICLcF43BWIzuZ3 XJdxvRrmUpP0kzXSdQ/EsMBHM9gzvVk= X-Google-Smtp-Source: APBJJlFo/x04tiw33tHz2z8eqoNqUG46TlCYKQtLqzp9NxrzX4ldQZG6AFgAiSwulUiFVWDLbQZPP6VjmRI= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:211c:a2ff:f17b:c5e9]) (user=surenb job=sendgmr) by 2002:a81:b703:0:b0:584:41a6:6cd8 with SMTP id v3-20020a81b703000000b0058441a66cd8mr114995ywh.8.1690927661105; Tue, 01 Aug 2023 15:07:41 -0700 (PDT) Date: Tue, 1 Aug 2023 15:07:28 -0700 In-Reply-To: <20230801220733.1987762-1-surenb@google.com> Mime-Version: 1.0 References: <20230801220733.1987762-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.585.gd2178a4bd4-goog Message-ID: <20230801220733.1987762-3-surenb@google.com> Subject: [PATCH v2 2/6] mm: for !CONFIG_PER_VMA_LOCK equate write lock assertion for vma and mmap From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, jannh@google.com, willy@infradead.org, liam.howlett@oracle.com, david@redhat.com, peterx@redhat.com, ldufour@linux.ibm.com, vbabka@suse.cz, michel@lespinasse.org, jglisse@google.com, mhocko@suse.com, hannes@cmpxchg.org, dave@stgolabs.net, hughd@google.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, stable@vger.kernel.org, Suren Baghdasaryan X-Rspamd-Queue-Id: 0E96380021 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 1m5kew4jt491ahbpusqjpt5m9cgi4gyr X-HE-Tag: 1690927661-80100 X-HE-Meta: U2FsdGVkX1/nsvezzNP6FETaW+rMPQ4J5VbAQfT1qo30N69KjMrXsOMZ1h5ljv7g0BIYnAgSilynHyoNVs6FYEHadwqCb+ux1Bxr2xdkg93bf8y2qkm6bVOzwTZGKvhr4GVezKnLVpliHN0hcOldQmHGZPeg8zJXo5xX1qHN502UnKkp81VcCK24JxaHqFrqT1FAWNv544oJ/5IFugg1MvCWSw8U2dA8c9+EwtHLKWifMzUjv+0Lp9nlaz2xMue5SZCzPctq4gNhXnKGmXznL6+K0WaUeZ5f+JrAcqCbKjaZTx+4TvI5McdQx6HAWISFNp6qpkB90qcsMnq9PlX4SMHsPT/L4/TQ3Yuu7V6hp7IE1XFMWtmazKBIYMNiuL/MqQOqASHaULWqMWMRXv/fJlwvJH2Mq1+iukRRtX99zFZJ1rQZRvklLBXnsMeNxrIGdbKiZQtZOskdBaKG4BdtWiTjFDZrm03KKFvrRZEjWmYp3ZO8jwZbhBMCTxoC3fhcqQF4CLFojcH2mfFqHl90cm4r1VH0uUmJe+EeT9L7TSV8kjFDPegkh1OXx/r3oMpSeA+dRZF4uwJuZHENlFwlG2h5SU0oJ6V5ee6zOTRcKv05W7N9QWy3HanuETLhVWvba3mH3zJ2Fmd0OezMzjo5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ity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: When CONFIG_PER_VMA_LOCK=n, vma_assert_write_locked() should be equivalent to mmap_assert_write_locked(). Suggested-by: Jann Horn Signed-off-by: Suren Baghdasaryan Reviewed-by: Liam R. Howlett --- include/linux/mm.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 406ab9ea818f..262b5f44101d 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -750,7 +750,8 @@ static inline void vma_end_read(struct vm_area_struct *vma) {} static inline void vma_start_write(struct vm_area_struct *vma) {} static inline bool vma_try_start_write(struct vm_area_struct *vma) { return true; } -static inline void vma_assert_write_locked(struct vm_area_struct *vma) {} +static inline void vma_assert_write_locked(struct vm_area_struct *vma) + { mmap_assert_write_locked(vma->vm_mm); } static inline void vma_mark_detached(struct vm_area_struct *vma, bool detached) {} From patchwork Tue Aug 1 22:07:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13337305 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E5A59C0015E for ; Tue, 1 Aug 2023 22:07:46 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 81920280101; Tue, 1 Aug 2023 18:07:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 77B5F2800C8; Tue, 1 Aug 2023 18:07:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 53086280101; Tue, 1 Aug 2023 18:07:46 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 3DB582800C8 for ; Tue, 1 Aug 2023 18:07:46 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 13B7840CE9 for ; Tue, 1 Aug 2023 22:07:46 +0000 (UTC) X-FDA: 81076923732.24.89C4E67 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf06.hostedemail.com (Postfix) with ESMTP id 4D762180013 for ; Tue, 1 Aug 2023 22:07:44 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=yfrnQPSj; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf06.hostedemail.com: domain of 3L4LJZAYKCIY241oxlqyyqvo.mywvsx47-wwu5kmu.y1q@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3L4LJZAYKCIY241oxlqyyqvo.mywvsx47-wwu5kmu.y1q@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690927664; a=rsa-sha256; cv=none; b=LONmZWBvenmPJRDiHjTIbX56XcxQ6APnUTYakJ/619mcsNdZpsicSjA8jmxPwMd1FmDPBT w97aS/HbEgNOR8rEow/sTXvsVT4fEPn80km8o49oy0HYA3s+hRPLx9k/VVkrTXZx0aDwll NVco40mtHKf2KWmDJhdKP2Pou6lKl7I= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=yfrnQPSj; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf06.hostedemail.com: domain of 3L4LJZAYKCIY241oxlqyyqvo.mywvsx47-wwu5kmu.y1q@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3L4LJZAYKCIY241oxlqyyqvo.mywvsx47-wwu5kmu.y1q@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690927664; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=4DFBzP3PjGN+0KSVb5wBxy1Zq6atXukCZSiVwP2lCA4=; b=ILHN+WZu131Ffm7fwpswqk+lT5oDPHKqe0p9vVO3d6ADFsFc7FY9FBpFVvBon1jwA1xBHj TBtkJR+hPhIcu5v36qavnAZkcEWIr9hGxM43FCzvugpkOKb4JcYmxnxb23Ct3OJZ5l0e0r y84cwQT8gzJHdytMzQFD5DE0AYxH7rw= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-d10792c7582so4979917276.3 for ; Tue, 01 Aug 2023 15:07:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1690927663; x=1691532463; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=4DFBzP3PjGN+0KSVb5wBxy1Zq6atXukCZSiVwP2lCA4=; b=yfrnQPSjeIcEe1QiQGFJbsVHjwcAuLH5y1rpwsS9p+vhZ0UwR6PwXsX+lyRO8LZqne i0Gs4sb/UQu9JzPwvqM6DFSny+pA/wf1YWY/9cDzKMMSVuqNYknBZN4RDzqEJ+3tgA1o 9xqYOQ9YfNuN7rSMd/QL10WuKbuUb09YlooNQnxevr0KyYMiTseErJlZMTiswhp4rbGs 2EwfGVOHZ8kOe74pKMJDxDRfvODcH40dPPxzmE/4CBhsbsPSTt0MJcXPagBusQWf3TL+ 2RYrhvVai3uiHNprCu8Jx7kGArbzCMBXdmuc0K6o1xx5D3e1kBTjmNATzd4hCjxzlGtv 8JyA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690927663; x=1691532463; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=4DFBzP3PjGN+0KSVb5wBxy1Zq6atXukCZSiVwP2lCA4=; b=OLN8LHjW0bwjDpPGFWuWbygstNY/cIHWogmAbtBp9IlV/cclrvZs6WiN5A66cqb+Is siTg2+/BY5PfckEKuDq2u5IYGhq0XxDcEmKABebj5Itlr+tkBS7SaVNW0IDARjmQAMka h0Ezu3U0eGQ6fuUkTQ9BNAwACa4gMMR/cehU9DvbtBilzBcF0J4PqabuW1+h8sfdwAUL 4LN0E05M4i9+q73GHLmjODUtPHUnZs1VvjC0Ok/ZaAfj63ln4ake4aCJTOr0SyqKesGR EtIdv9/fMP1z8ZYGeOS13+8LJkrYUEcJDGekO70u3KEM1z/qbOUxFGJaUMg5PjyxrSQ/ rqHw== X-Gm-Message-State: ABy/qLbqdaXiPHFZ5kljQ7KgYzvHMFPMI7Pd6Y2536lkQw3yHbfCS8ob mInQsLtOjnTdqSHxLOHqBMjbiQOQYgs= X-Google-Smtp-Source: APBJJlFt7bPUCzTOSC2YtD5VNpVA2kL+fFs0SLmjXp2yCsxE/K0tlsB3vCbnSPBXi96wBKaf36lZ9Vrga8w= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:211c:a2ff:f17b:c5e9]) (user=surenb job=sendgmr) by 2002:a25:d246:0:b0:c61:7151:6727 with SMTP id j67-20020a25d246000000b00c6171516727mr103919ybg.10.1690927663525; Tue, 01 Aug 2023 15:07:43 -0700 (PDT) Date: Tue, 1 Aug 2023 15:07:29 -0700 In-Reply-To: <20230801220733.1987762-1-surenb@google.com> Mime-Version: 1.0 References: <20230801220733.1987762-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.585.gd2178a4bd4-goog Message-ID: <20230801220733.1987762-4-surenb@google.com> Subject: [PATCH v2 3/6] mm: replace mmap with vma write lock assertions when operating on a vma From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, jannh@google.com, willy@infradead.org, liam.howlett@oracle.com, david@redhat.com, peterx@redhat.com, ldufour@linux.ibm.com, vbabka@suse.cz, michel@lespinasse.org, jglisse@google.com, mhocko@suse.com, hannes@cmpxchg.org, dave@stgolabs.net, hughd@google.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, stable@vger.kernel.org, Suren Baghdasaryan X-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: 4D762180013 X-Stat-Signature: 3d9khq6sh54jrdgnentkgjh55nwbm5mc X-HE-Tag: 1690927664-336061 X-HE-Meta: U2FsdGVkX1+AX4WV5E0h2e9tYCC27DODZHWUAJljIqdsHT5NUVKO7wR7wmQXxrVa8Eif86VaID7SM/WGkqe0WAQftw51iWq0bgNQxR6XNxZ6qhmFySS3H51pjmBrLvn+csvSmWJKrRMiSELkcU2pDhqAj2jR8Hs/jBwTMRzNT2DMG0F3WiW10syXFjMvGLD1Vu13Vdg3G7mQHzYiep/j/ZBG/M4gLq63lVCLGEpG9MEdoAGVFr32tY+mVH0E0NNMgZw9z+BnP0CqipN+84MLOTtKnLCcmsGczTG5ToSfVnJc2x2mb/arItmhWFyo2Ve2yhtSk4rbVr6C1HWniMbuskZjN4drIYDvMIeUn/UXcXqihPqCHdlLTOde40Jp+Om2BjKXLFibYEbMKtEQB2K3G/JEFv/AGsguJirAo/9Ys8zgv8GbAhYpm5h7toK85NGz/ec2+SitN4SvgdGayjIouUN8ByD64jg58FCBDKujPbIMhHdWBHy0Lel4n5SYHt18Qdmf7U41qi1oG0I2rB1QcfkI6qQZzvXmFNmmBk8wKwQsnVkALnjtJ/vtTBOOgekxBPCxDocKi0AUKrBcf9ZozVOJcdgomsXo1I0Wbpxny3YXbbJ5UWVPJVr7b9yGImFWGPkeXKoIy7GMM6jF+pDKqSTptlPgCvhpiRpSk4jS3hcpDGxBhz3ln586CcfgUv75PKUIGdusowVspbgWlj+OMAXg8+w1JXLOIevQRO4l6m+JtATtEamnL1d1RPKIN3azJgrRWcWKl0YUvLceWcPk+Ewh4439MiMQ6tA4zruxkfclnvQ8oU1JOWGhORhCDsYtTKt7UdA6VkgEV6bqyGxOgP8ZCOenxFPim4gxIr9lpdFuoKviRkvc5hFlfi78e4nYjBq3iThTmWfZ2F/OgEhZTtOvSfkqPhAMlg5VlLMxa3Li+MkooMCoPslw/y4dtM8kVjeuTdMPivPPyC2Tvx4 btDiUqWS yshDP7wTCkm4xQPWF2FIw2Lb0fYwHiGbjZZG0mNHljVc27qYAqmYTf9+mBpeklvhDFIflichOQvsdjOx7vvwXBdKrtMfPl8BHKHBxxSP3196WQQQNlV1xmkgTeDGW5pUjE80HyvfpRCoY48Dk/PRPbOR7PAPqwo69MS/1MFfR63k5AovDy/XTbcgr3o/fHBHHz8pOQ4QmQkopr2EcBD9TtkBNPmpaTxALlAbJImx8Wha9xo6X5jTXnUwIt4Y7o4149Ikjk0rUs07DzMOhcqhMjLlPAaLNwGLKkj3oKpbX3mDCJ+pHNecsdwNigTCT2JTfbAnCuSPTZ/GRMQb0Zjitsq/jVDqS/q38YvsiKDz49nAtXvMJ4VXWdxcxTrV+Oui38WcM3y4Av2dmhnxMB4cYrayhgnNOXHOG8U8xVjtEK8tk9a0fpL6VvDnmRmmdI6oDMw4vwpUAI5H79Hkw+rqwgQighV4MeF6tskeoZkFW8jCOio8y4S/uA1SIdWmsmScPn+yAXjImBYhZfPconh6R1oREqkUhJIsEo+yOC81NNFfj6hbTvql96h4BfO1H+Ss0+77rPJvLTtwX2lIRpAt53oEratqKH2Viqi4gdE06qYTW77w= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Vma write lock assertion always includes mmap write lock assertion and additional vma lock checks when per-VMA locks are enabled. Replace weaker mmap_assert_write_locked() assertions with stronger vma_assert_write_locked() ones when we are operating on a vma which is expected to be locked. Suggested-by: Jann Horn Signed-off-by: Suren Baghdasaryan Reviewed-by: Liam R. Howlett --- mm/hugetlb.c | 2 +- mm/khugepaged.c | 5 +++-- mm/memory.c | 2 +- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/mm/hugetlb.c b/mm/hugetlb.c index 64a3239b6407..1d871a1167d8 100644 --- a/mm/hugetlb.c +++ b/mm/hugetlb.c @@ -5028,7 +5028,7 @@ int copy_hugetlb_page_range(struct mm_struct *dst, struct mm_struct *src, src_vma->vm_start, src_vma->vm_end); mmu_notifier_invalidate_range_start(&range); - mmap_assert_write_locked(src); + vma_assert_write_locked(src_vma); raw_write_seqcount_begin(&src->write_protect_seq); } else { /* diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 78c8d5d8b628..1e43a56fba31 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1495,7 +1495,7 @@ static int set_huge_pmd(struct vm_area_struct *vma, unsigned long addr, }; VM_BUG_ON(!PageTransHuge(hpage)); - mmap_assert_write_locked(vma->vm_mm); + vma_assert_write_locked(vma); if (do_set_pmd(&vmf, hpage)) return SCAN_FAIL; @@ -1525,7 +1525,7 @@ static void collapse_and_free_pmd(struct mm_struct *mm, struct vm_area_struct *v pmd_t pmd; struct mmu_notifier_range range; - mmap_assert_write_locked(mm); + vma_assert_write_locked(vma); if (vma->vm_file) lockdep_assert_held_write(&vma->vm_file->f_mapping->i_mmap_rwsem); /* @@ -1570,6 +1570,7 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, unsigned long addr, int count = 0, result = SCAN_FAIL; int i; + /* Ensure vma can't change, it will be locked below after checks */ mmap_assert_write_locked(mm); /* Fast check before locking page if already PMD-mapped */ diff --git a/mm/memory.c b/mm/memory.c index 603b2f419948..652d99b9858a 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1312,7 +1312,7 @@ copy_page_range(struct vm_area_struct *dst_vma, struct vm_area_struct *src_vma) * Use the raw variant of the seqcount_t write API to avoid * lockdep complaining about preemptibility. */ - mmap_assert_write_locked(src_mm); + vma_assert_write_locked(src_vma); raw_write_seqcount_begin(&src_mm->write_protect_seq); } From patchwork Tue Aug 1 22:07:30 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13337306 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B9142C0015E for ; Tue, 1 Aug 2023 22:07:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5A7F4280102; Tue, 1 Aug 2023 18:07:49 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 530AD2800C8; Tue, 1 Aug 2023 18:07:49 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3AA0C280102; Tue, 1 Aug 2023 18:07:49 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 251942800C8 for ; Tue, 1 Aug 2023 18:07:49 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id D44101C9CE1 for ; Tue, 1 Aug 2023 22:07:48 +0000 (UTC) X-FDA: 81076923816.27.4F97016 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf29.hostedemail.com (Postfix) with ESMTP id 1CC7412000E for ; Tue, 1 Aug 2023 22:07:46 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=ZFUINCXp; spf=pass (imf29.hostedemail.com: domain of 3MoLJZAYKCIk574r0ot11tyr.p1zyv07A-zzx8npx.14t@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3MoLJZAYKCIk574r0ot11tyr.p1zyv07A-zzx8npx.14t@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690927667; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=RKZo+1hfRepjKtnKDtyG+lpMw+Id83I1mtUQG8twV1g=; b=EBCk5M6gIWk3OzK2+bPsrLaS2/2Vz2HPHQ0D36lmmViYworlpfoA2D0mFuZF/Xb4S/WxH7 9nZWqVV1+7YN8mWohscuaIrn1OeS3dFiq+8crw58/1w88fDr2Uqe6/DnAB2HffS11FUkcf IQ4KnSR8V/R5Svb12fKp1PVYZRQB/3s= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690927667; a=rsa-sha256; cv=none; b=xrc9Wt6saBVr4N4+fOZUgtUOwljrGmEX7te/qV1zaBymBIee6V+l69smSbdtR5nPp10wYL Gr0jacds5u1+MyYNKeEc5El/qiKMP/OOsbuT2S06+zfFSX2CjiU2zxVd/Eo4Rh9kPppFM5 ywB/M7jwsyGkZoML29EwujB7q+umquw= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=ZFUINCXp; spf=pass (imf29.hostedemail.com: domain of 3MoLJZAYKCIk574r0ot11tyr.p1zyv07A-zzx8npx.14t@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3MoLJZAYKCIk574r0ot11tyr.p1zyv07A-zzx8npx.14t@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-5847479b559so70895227b3.1 for ; Tue, 01 Aug 2023 15:07:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1690927666; x=1691532466; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=RKZo+1hfRepjKtnKDtyG+lpMw+Id83I1mtUQG8twV1g=; b=ZFUINCXp69DsytCWVUi4i3FblzMVDA9BptWaaF+sKX0xKEjgkgju+DXhX3HccVVQYK XfNP/c2GWyfqQJHV5buiUdSz9eeuWGGs7bmeQuEchsb8OOL3n9wvyCurtr7tkXCQi0aN yP++75VtrNJXUbKenp0F1ykufx1TsQbFC4T42Fu0rToUR5fYyKHd1/8JmARUNH7JC7wS Lj5lgymUX89V5GFOqGp4x1br6gngtFB+xvWjpzgbM1kSIUiCB0dwywo6Jf8riCeEpuU+ fuTx3ZM3NBd+NW9LDhz7+Fp9WswDCH8sARSCiBtGMaTHvckO5JPymvcy35BZOGdWDtwz dWHA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690927666; x=1691532466; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=RKZo+1hfRepjKtnKDtyG+lpMw+Id83I1mtUQG8twV1g=; b=C+wxe9TF+CSi7WK9SHy07sC94vw/1SrRqCO1I1y2WaCJp3cUKvIuPT48fFU7rZLdov aw3hf6ylUCRWwM8Nf9voM5Jrlub6JB5IvhbBk5N56MfPRVdJ+mP2rSFXcxayyfkhvhl3 XaXcJ+5bqpVWjmumdJGC6Yq2AjRO/m9IsVHVQXdRhT0T0z5XyNhhGKePMoaTKSOy6/oJ 3I9w7fg5ulbsIzilazYaPEN7Yq6+M4/LEtMs94J7TpzxwQPvWGCoT/ILhgUlrZXXnZiw /rJyAKOwORz8r+5QJd/N4i4Diw0JuTV61MtO0YVH8WQGvlCaXe1gTk22+8hJHZXGLgRg +JhQ== X-Gm-Message-State: ABy/qLbfHcjIe8eycPgvC2lMrMkroBTSTLZJxEQg6QDzAXpMNYHQpaKw DNlcX005nhnV4SFuLOQDHujpMEIVM5E= X-Google-Smtp-Source: APBJJlEVrG0ZaZS1XJkKCyoFSCws8IlmHUWrBIIEhqF/1GbPoPNJFMVdqT5uK6xuibwesECoWBa2n5oiBr4= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:211c:a2ff:f17b:c5e9]) (user=surenb job=sendgmr) by 2002:a81:b104:0:b0:583:9913:f293 with SMTP id p4-20020a81b104000000b005839913f293mr116951ywh.1.1690927666209; Tue, 01 Aug 2023 15:07:46 -0700 (PDT) Date: Tue, 1 Aug 2023 15:07:30 -0700 In-Reply-To: <20230801220733.1987762-1-surenb@google.com> Mime-Version: 1.0 References: <20230801220733.1987762-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.585.gd2178a4bd4-goog Message-ID: <20230801220733.1987762-5-surenb@google.com> Subject: [PATCH v2 4/6] mm: lock vma explicitly before doing vm_flags_reset and vm_flags_reset_once From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, jannh@google.com, willy@infradead.org, liam.howlett@oracle.com, david@redhat.com, peterx@redhat.com, ldufour@linux.ibm.com, vbabka@suse.cz, michel@lespinasse.org, jglisse@google.com, mhocko@suse.com, hannes@cmpxchg.org, dave@stgolabs.net, hughd@google.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, stable@vger.kernel.org, Suren Baghdasaryan , Linus Torvalds X-Stat-Signature: 8s5f5jsimnnx8dxe5nrjeb6z3tmqyf1p X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 1CC7412000E X-Rspam-User: X-HE-Tag: 1690927666-166153 X-HE-Meta: U2FsdGVkX1//lTQ+c7UMnYjQz4orMMyoCdwIifh6xmzv6ZgD1C/byKVY+r6CSzR0oK34HYNAFSx60qQSIoGR6p02fTvBmQ2kTHCFwpqTr0K9T+G2q3i7Q1ZPee0nGVGZWjuMB1/kfdAz08y5lepS9WrrJ+uCUo+3RcUkZCplg5qhS8y3Vp1crPk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I2ugB8oGdxc4Gb+gcHCYEHCLHinyf1fqDGt+C/IPmHjxkNTKRB6rL8FcAkw9W7EHHNLdyKITOXpl47qQ/GntSrqFmGAhvYk/tKfQOsXSl6epHZvCXGGrOrieLeblW0CqwpYGgPMKtD562JFTZVJ1S8WQAmVCOhYop1Hef1nwoaBYiLkUo3GUATOFkPs+0nXTCQQI09o6tjGoT/+y9QhTmAy8slWqnau5tmCD0B104zybJvbtcLgqEa/Bitmhy9Ey+wp4aGSbn1Kug2hwHH0DhnuypMfNG/hYRSN/I7jRdM7uFMBKqKqqssuVGU49lSHQ2Np3a50FpPBqBxaweZzprLZ4nfkndPc2oLKG5hq7HZfQxsw2D4nYqzzlzCWI40apWFe7E4S+SUeH2DNvhgNLpUlJXNPs4Ck88OxpfhngiscwBOjWb9wHXYH2IA== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Implicit vma locking inside vm_flags_reset() and vm_flags_reset_once() is not obvious and makes it hard to understand where vma locking is happening. Also in some cases (like in dup_userfaultfd()) vma should be locked earlier than vma_flags modification. To make locking more visible, change these functions to assert that the vma write lock is taken and explicitly lock the vma beforehand. Fix userfaultfd functions which should lock the vma earlier. Suggested-by: Linus Torvalds Signed-off-by: Suren Baghdasaryan Reviewed-by: Liam R. Howlett --- arch/powerpc/kvm/book3s_hv_uvmem.c | 1 + drivers/infiniband/hw/hfi1/file_ops.c | 1 + fs/userfaultfd.c | 6 ++++++ include/linux/mm.h | 10 +++++++--- mm/madvise.c | 5 ++--- mm/mlock.c | 3 ++- mm/mprotect.c | 1 + 7 files changed, 20 insertions(+), 7 deletions(-) diff --git a/arch/powerpc/kvm/book3s_hv_uvmem.c b/arch/powerpc/kvm/book3s_hv_uvmem.c index 709ebd578394..e2d6f9327f77 100644 --- a/arch/powerpc/kvm/book3s_hv_uvmem.c +++ b/arch/powerpc/kvm/book3s_hv_uvmem.c @@ -410,6 +410,7 @@ static int kvmppc_memslot_page_merge(struct kvm *kvm, ret = H_STATE; break; } + vma_start_write(vma); /* Copy vm_flags to avoid partial modifications in ksm_madvise */ vm_flags = vma->vm_flags; ret = ksm_madvise(vma, vma->vm_start, vma->vm_end, diff --git a/drivers/infiniband/hw/hfi1/file_ops.c b/drivers/infiniband/hw/hfi1/file_ops.c index a5ab22cedd41..5920bfc1e1c5 100644 --- a/drivers/infiniband/hw/hfi1/file_ops.c +++ b/drivers/infiniband/hw/hfi1/file_ops.c @@ -344,6 +344,7 @@ static int hfi1_file_mmap(struct file *fp, struct vm_area_struct *vma) goto done; } + vma_start_write(vma); /* * vm_pgoff is used as a buffer selector cookie. Always mmap from * the beginning. diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 7cecd49e078b..6cde95533dcd 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -667,6 +667,7 @@ static void userfaultfd_event_wait_completion(struct userfaultfd_ctx *ctx, mmap_write_lock(mm); for_each_vma(vmi, vma) { if (vma->vm_userfaultfd_ctx.ctx == release_new_ctx) { + vma_start_write(vma); vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX; userfaultfd_set_vm_flags(vma, vma->vm_flags & ~__VM_UFFD_FLAGS); @@ -702,6 +703,7 @@ int dup_userfaultfd(struct vm_area_struct *vma, struct list_head *fcs) octx = vma->vm_userfaultfd_ctx.ctx; if (!octx || !(octx->features & UFFD_FEATURE_EVENT_FORK)) { + vma_start_write(vma); vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX; userfaultfd_set_vm_flags(vma, vma->vm_flags & ~__VM_UFFD_FLAGS); return 0; @@ -783,6 +785,7 @@ void mremap_userfaultfd_prep(struct vm_area_struct *vma, atomic_inc(&ctx->mmap_changing); } else { /* Drop uffd context if remap feature not enabled */ + vma_start_write(vma); vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX; userfaultfd_set_vm_flags(vma, vma->vm_flags & ~__VM_UFFD_FLAGS); } @@ -940,6 +943,7 @@ static int userfaultfd_release(struct inode *inode, struct file *file) prev = vma; } + vma_start_write(vma); userfaultfd_set_vm_flags(vma, new_flags); vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX; } @@ -1502,6 +1506,7 @@ static int userfaultfd_register(struct userfaultfd_ctx *ctx, * the next vma was merged into the current one and * the current one has not been updated yet. */ + vma_start_write(vma); userfaultfd_set_vm_flags(vma, new_flags); vma->vm_userfaultfd_ctx.ctx = ctx; @@ -1685,6 +1690,7 @@ static int userfaultfd_unregister(struct userfaultfd_ctx *ctx, * the next vma was merged into the current one and * the current one has not been updated yet. */ + vma_start_write(vma); userfaultfd_set_vm_flags(vma, new_flags); vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX; diff --git a/include/linux/mm.h b/include/linux/mm.h index 262b5f44101d..2c720c9bb1ae 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -780,18 +780,22 @@ static inline void vm_flags_init(struct vm_area_struct *vma, ACCESS_PRIVATE(vma, __vm_flags) = flags; } -/* Use when VMA is part of the VMA tree and modifications need coordination */ +/* + * Use when VMA is part of the VMA tree and modifications need coordination + * Note: vm_flags_reset and vm_flags_reset_once do not lock the vma and + * it should be locked explicitly beforehand. + */ static inline void vm_flags_reset(struct vm_area_struct *vma, vm_flags_t flags) { - vma_start_write(vma); + vma_assert_write_locked(vma); vm_flags_init(vma, flags); } static inline void vm_flags_reset_once(struct vm_area_struct *vma, vm_flags_t flags) { - vma_start_write(vma); + vma_assert_write_locked(vma); WRITE_ONCE(ACCESS_PRIVATE(vma, __vm_flags), flags); } diff --git a/mm/madvise.c b/mm/madvise.c index bfe0e06427bd..507b1d299fec 100644 --- a/mm/madvise.c +++ b/mm/madvise.c @@ -173,9 +173,8 @@ static int madvise_update_vma(struct vm_area_struct *vma, } success: - /* - * vm_flags is protected by the mmap_lock held in write mode. - */ + /* vm_flags is protected by the mmap_lock held in write mode. */ + vma_start_write(vma); vm_flags_reset(vma, new_flags); if (!vma->vm_file || vma_is_anon_shmem(vma)) { error = replace_anon_vma_name(vma, anon_name); diff --git a/mm/mlock.c b/mm/mlock.c index 479e09d0994c..06bdfab83b58 100644 --- a/mm/mlock.c +++ b/mm/mlock.c @@ -387,6 +387,7 @@ static void mlock_vma_pages_range(struct vm_area_struct *vma, */ if (newflags & VM_LOCKED) newflags |= VM_IO; + vma_start_write(vma); vm_flags_reset_once(vma, newflags); lru_add_drain(); @@ -461,9 +462,9 @@ static int mlock_fixup(struct vma_iterator *vmi, struct vm_area_struct *vma, * It's okay if try_to_unmap_one unmaps a page just after we * set VM_LOCKED, populate_vma_page_range will bring it back. */ - if ((newflags & VM_LOCKED) && (oldflags & VM_LOCKED)) { /* No work to do, and mlocking twice would be wrong */ + vma_start_write(vma); vm_flags_reset(vma, newflags); } else { mlock_vma_pages_range(vma, start, end, newflags); diff --git a/mm/mprotect.c b/mm/mprotect.c index 3aef1340533a..362e190a8f81 100644 --- a/mm/mprotect.c +++ b/mm/mprotect.c @@ -657,6 +657,7 @@ mprotect_fixup(struct vma_iterator *vmi, struct mmu_gather *tlb, * vm_flags and vm_page_prot are protected by the mmap_lock * held in write mode. */ + vma_start_write(vma); vm_flags_reset(vma, newflags); if (vma_wants_manual_pte_write_upgrade(vma)) mm_cp_flags |= MM_CP_TRY_CHANGE_WRITABLE; From patchwork Tue Aug 1 22:07:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13337307 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 06A60C04A6A for ; Tue, 1 Aug 2023 22:07:52 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9C579280103; Tue, 1 Aug 2023 18:07:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 951B12800C8; Tue, 1 Aug 2023 18:07:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7A353280103; Tue, 1 Aug 2023 18:07:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 639982800C8 for ; Tue, 1 Aug 2023 18:07:51 -0400 (EDT) Received: from smtpin09.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 3034BA0652 for ; Tue, 1 Aug 2023 22:07:51 +0000 (UTC) X-FDA: 81076923942.09.F20396F Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf06.hostedemail.com (Postfix) with ESMTP id 60FAD180014 for ; Tue, 1 Aug 2023 22:07:49 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=LGbOmtH0; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf06.hostedemail.com: domain of 3NILJZAYKCIs796t2qv33v0t.r310x29C-11zAprz.36v@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3NILJZAYKCIs796t2qv33v0t.r310x29C-11zAprz.36v@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690927669; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=QxBrE/UCHWlQTdGOylRc3uDNTY7RNXyaur9L23gJy4g=; b=xTJfQOJLawLCZ3oAGzAG4i9G7i5cg9E82yFFXwPlu5cunrHIUAWCkO0MUUhj3DCTUHRp8X ZMgrtrPx4pDtx4qi/tX2y3gUSQBinW9dgoJCS3KrUOWGZ4K68G5gIAbJEGsm0EpywzO1XS eibs/q7pKaCSubG2uu0GLnKjncioCYE= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=LGbOmtH0; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf06.hostedemail.com: domain of 3NILJZAYKCIs796t2qv33v0t.r310x29C-11zAprz.36v@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3NILJZAYKCIs796t2qv33v0t.r310x29C-11zAprz.36v@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690927669; a=rsa-sha256; cv=none; b=nQbxM4roL8gs7rAIHTAFNDQ5RpCNTTLmhg+ML7XEh7QSUiHUH5bQUSaIoYaWu4Lta/OLio Ci+e+Z5DioGWkZ8G6SKVVMEniWThAzVKfWU99fxI0gme+OL10RpSqiXyaBt7GvuKjl/pBs 4F2COyFsrfF+mu7eBA5ShWnE9FjnFhg= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5840ea40c59so75696457b3.2 for ; Tue, 01 Aug 2023 15:07:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1690927668; x=1691532468; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=QxBrE/UCHWlQTdGOylRc3uDNTY7RNXyaur9L23gJy4g=; b=LGbOmtH0lMhdDYO3+2ECExEYuxVvpb5dUlLecAiJAGrPI9PAJfTN7UrlhG2D2+sMgI A0HPsE7Dh7snipQv/ppKfZkrtywqFIDj94GgYYg1RmHEKQD/JrUFvPL4mXaNgmgR43fS rzNN/7cPcSEEUD6MphOEKlUIyUYRI9RNEW1ibPgtDMhMmPz+MaEiw+C/wPcvGj3gdm+s wmBOfyKZ9Eq/c9nMUYmgRF+ijS83GqRJwTrxNUo/kn28ipzSQ7VAbYmtkG9fCdYkXi0z Hhzh6iMenp89gYpIovNtlhgbKlD0/7Qoap5BixiG9npmNeurITeJhKlGDSUEA6RAa5bC 2mng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690927668; x=1691532468; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=QxBrE/UCHWlQTdGOylRc3uDNTY7RNXyaur9L23gJy4g=; b=E3hQVqx+s7KpBeGn3xXQCeJ+q8xs0UnvPUuU/BdnWVQ0t7JQWuXMn2szzbXTfLd/qi hTdL52lAxhKzaaVmaiTSCYosoAQIGFi0kN5RhXFMOBlT7XbASReFcRWrIhJ7vbbPo3rq fDkAb0tYSpSxE68bqIjCPcGg00m2c5GHoEVBdE8OyftY83ICfrB5DSoXeIBuTbeInSS0 P0DFh5jLiU6Zby2wudaFeTsd6UplHDTAow6DM62m66sF1fCF8X0DCXo3fU68r1ta0G4Y +iYMlWfpmkAxEfZICueWMTVNEGFee4Exo6EIi209VUf8qa1iat1OS6ThiXGgcQMBpd21 NjXw== X-Gm-Message-State: ABy/qLb5on2Lv5WE5HPNm21SW6ODOGhju6al7CTPmNjFUgUv3eS10tuL c7NOoIPvLrWNTFV2qQwxS9r1IGdvwU8= X-Google-Smtp-Source: APBJJlFGce4m/S34cz0bw8Xemucsi7OipxmEL+9L+engD7fhL2P1Th2X8JjKAj9JtV2822hEHcXYgtjOeKk= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:211c:a2ff:f17b:c5e9]) (user=surenb job=sendgmr) by 2002:a81:ae02:0:b0:576:b244:5a4e with SMTP id m2-20020a81ae02000000b00576b2445a4emr129366ywh.10.1690927668528; Tue, 01 Aug 2023 15:07:48 -0700 (PDT) Date: Tue, 1 Aug 2023 15:07:31 -0700 In-Reply-To: <20230801220733.1987762-1-surenb@google.com> Mime-Version: 1.0 References: <20230801220733.1987762-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.585.gd2178a4bd4-goog Message-ID: <20230801220733.1987762-6-surenb@google.com> Subject: [PATCH v2 5/6] mm: always lock new vma before inserting into vma tree From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, jannh@google.com, willy@infradead.org, liam.howlett@oracle.com, david@redhat.com, peterx@redhat.com, ldufour@linux.ibm.com, vbabka@suse.cz, michel@lespinasse.org, jglisse@google.com, mhocko@suse.com, hannes@cmpxchg.org, dave@stgolabs.net, hughd@google.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, stable@vger.kernel.org, Suren Baghdasaryan X-Rspamd-Queue-Id: 60FAD180014 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: ngh71s4hbxr1demm915rubatz3kgrcfq X-HE-Tag: 1690927669-459901 X-HE-Meta: U2FsdGVkX18GNxR5yVWz97guuwRG3tEoWnDHT5gOXcW0oaWlUo9wX9Wcznkvk3fBLLzsyXRGUZRBCuWzTzzrtNZ6D+SUTBZ1iYPvC3Ltl2hYfTjfHFM9xzHgvOdBv/bqXNBwYl91EVAAOiukPFQXw6MAPSEF9awgpA26eCIlexMLEZpBDJZCnVHXGAHIfE0bbjvsvlt+0ZN23YbAK+MH93gwHGhdmO9IpT2kT4isGrj3VrMqJVAspM3uVm4BjF6SzqFwhqV4mop1MUfHLsuTRYh8YR1ZJrh6CotJfU4g6Y0hBNjUAwZjCFqrMJFNIUJ28JL1ogpNbvE/s5bjgCVjN6s7bF17a3gMGo+IJrnnLbI1Zt5CPdMzTAHtHBR3xJPHYPbu3SzLLcvsfWhEftllZQGGE9bZhoTsjnOG06FVixVMcVePJTwQEBBkF92yIV6HppsvpL0xWryb8PO/ziE+iVS7Ju4QfU6q3C3jfhUmjw0hmDqWvN10199jmGeEmdWtgB9LHZeuG8cWoiorSw+M4c7jqzXnyXIcquRkzoXGZ6gnXyGf04PZvLO4uZj9r2/Q7R2bcs2j73Tat27IfqUmMRoXKFcouV1ZAd6dN29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owner-majordomo@kvack.org List-ID: While it's not strictly necessary to lock a newly created vma before adding it into the vma tree (as long as no further changes are performed to it), it seems like a good policy to lock it and prevent accidental changes after it becomes visible to the page faults. Lock the vma before adding it into the vma tree. Suggested-by: Jann Horn Signed-off-by: Suren Baghdasaryan Reviewed-by: Liam R. Howlett --- mm/mmap.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 3937479d0e07..850a39dee075 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -412,6 +412,8 @@ static int vma_link(struct mm_struct *mm, struct vm_area_struct *vma) if (vma_iter_prealloc(&vmi)) return -ENOMEM; + vma_start_write(vma); + if (vma->vm_file) { mapping = vma->vm_file->f_mapping; i_mmap_lock_write(mapping); @@ -477,7 +479,8 @@ static inline void vma_prepare(struct vma_prepare *vp) vma_start_write(vp->vma); if (vp->adj_next) vma_start_write(vp->adj_next); - /* vp->insert is always a newly created VMA, no need for locking */ + if (vp->insert) + vma_start_write(vp->insert); if (vp->remove) vma_start_write(vp->remove); if (vp->remove2) @@ -3098,6 +3101,7 @@ static int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, vma->vm_pgoff = addr >> PAGE_SHIFT; vm_flags_init(vma, flags); vma->vm_page_prot = vm_get_page_prot(flags); + vma_start_write(vma); if (vma_iter_store_gfp(vmi, vma, GFP_KERNEL)) goto mas_store_fail; @@ -3345,7 +3349,6 @@ struct vm_area_struct *copy_vma(struct vm_area_struct **vmap, get_file(new_vma->vm_file); if (new_vma->vm_ops && new_vma->vm_ops->open) new_vma->vm_ops->open(new_vma); - vma_start_write(new_vma); if (vma_link(mm, new_vma)) goto out_vma_link; *need_rmap_locks = false; From patchwork Tue Aug 1 22:07:32 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13337308 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 7C48BC00528 for ; Tue, 1 Aug 2023 22:07:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 16261280104; Tue, 1 Aug 2023 18:07:54 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0EA7C2800C8; Tue, 1 Aug 2023 18:07:54 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id ECEB9280104; Tue, 1 Aug 2023 18:07:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id DA0152800C8 for ; Tue, 1 Aug 2023 18:07:53 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 9FE631203D7 for ; Tue, 1 Aug 2023 22:07:53 +0000 (UTC) X-FDA: 81076924026.07.7436DC5 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf14.hostedemail.com (Postfix) with ESMTP id D9D2F100016 for ; Tue, 1 Aug 2023 22:07:51 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=D1O6U13I; spf=pass (imf14.hostedemail.com: domain of 3N4LJZAYKCI4AC9w5ty66y3w.u64305CF-442Dsu2.69y@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3N4LJZAYKCI4AC9w5ty66y3w.u64305CF-442Dsu2.69y@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690927671; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=FgBjF0ZO5ynPdNrndPmrtDmv0LneRZKTmqqgx6E5wg8=; b=0BtRajdLlnPjmcZBJeQ8wLcfejeMEDCxvhEb8yPZ76WGVMPF6SRNyPtFEnYguJa3GyB0tb odVTMJwxtZvdJPPA5kf/UencBn4/mFEePoKE6FOYlrR6D8lleUp3vVawuM2ahw1Wrx27DN pvSzA0+H+QY8pRxRdEATM8QTxL7NCJk= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=D1O6U13I; spf=pass (imf14.hostedemail.com: domain of 3N4LJZAYKCI4AC9w5ty66y3w.u64305CF-442Dsu2.69y@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3N4LJZAYKCI4AC9w5ty66y3w.u64305CF-442Dsu2.69y@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690927671; a=rsa-sha256; cv=none; b=TUvJ7zWid2/P1i0fabXhKCus/43qNj6F4j60PrmzTHzOqquEkpZSfm7r9LLAzvqNE2xgZg 6vh22fdQAccHbl+buhrvsv/j1zu2DLCv/ybZUyDQ0OkmO2jVMpleE+9oJ5Vj4iEb+fmR/8 85PDOH3UfMjf+cTetJzuoT05LUm+lf8= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-585fb08172bso43383617b3.2 for ; Tue, 01 Aug 2023 15:07:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1690927671; x=1691532471; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=FgBjF0ZO5ynPdNrndPmrtDmv0LneRZKTmqqgx6E5wg8=; b=D1O6U13IyQtW6sRKIgsBESsntgiHZD2NBKEqY9x4VzN/LKKhnlNfxDelhOtdMckxRn FHM+qzWy+1vIGVvjuSnC3HOPacbJwiF+FU/44s3FUSl9rg9/MlksilEUDdYILducKN5m QBspm1K1gtu0F9WcdyEIrEuwnPMiQ8okMkqevL/Euzqha1FW9VFmXK9Kd09SaJL8SIMi 4XcGgId5NKrdkKLAeWoz/iYiTpXzRmouek8VJ8B5ZERcnA/Lkgi6E3b5jwXkBi9h6gTm 7wI1MoX2O3/0bFhkSpJ1WB6U2kt7rsAJK91S/XGtkVxCLlTHZukUWI5CAAc5iREvhiOQ j9Gg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690927671; x=1691532471; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=FgBjF0ZO5ynPdNrndPmrtDmv0LneRZKTmqqgx6E5wg8=; b=GRg6P5R2jvuUa/SmOG/h4829fRvIFlAdYs9oAn3MVQh8Vkp6ppH9p6rwpavxdqieqD o0iPuxIi74wmaonyh7WszGs7B4I3tN87O3CbOv+zk4I7n984qnjy5TDQqk1JHCrzqOmd O8Z1voV89aK9c+BXU9oY2a2BOGLHrebWrLpCJDQ5AiJuObrsMmEwQVh0oJeqpUtKgq9b kMQlY/CtpF7teSiIpqzSxUqlx9qhcLgvUJS/esRMcIjRTuJ/7P/D5Vg7Jxkt4NnXrVFX CycRZxAc7w8LYqVQmfPVRt1cshaUKe1Xo5h+GYm9L8mAsiXU9g57vtCNfP5XQwFbTYkf JD2Q== X-Gm-Message-State: ABy/qLYQfW0q8uSUYiV4TRQY8NluKnsbVMoHezgsOfhdNpHVD6peI3qs NLBlt2fSpIkNa+N9ScV5cZzzxanZAXQ= X-Google-Smtp-Source: APBJJlHKCcBKjQ9mL8CtWFWy75AVvvzqEFOjz4RA/pg75yQkmZcpThORh+6C2/yUUDZy/D/HgF30pmifT2Y= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:211c:a2ff:f17b:c5e9]) (user=surenb job=sendgmr) by 2002:a81:ae13:0:b0:583:a866:3450 with SMTP id m19-20020a81ae13000000b00583a8663450mr126859ywh.7.1690927671038; Tue, 01 Aug 2023 15:07:51 -0700 (PDT) Date: Tue, 1 Aug 2023 15:07:32 -0700 In-Reply-To: <20230801220733.1987762-1-surenb@google.com> Mime-Version: 1.0 References: <20230801220733.1987762-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.585.gd2178a4bd4-goog Message-ID: <20230801220733.1987762-7-surenb@google.com> Subject: [PATCH v2 6/6] mm: move vma locking out of vma_prepare From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, jannh@google.com, willy@infradead.org, liam.howlett@oracle.com, david@redhat.com, peterx@redhat.com, ldufour@linux.ibm.com, vbabka@suse.cz, michel@lespinasse.org, jglisse@google.com, mhocko@suse.com, hannes@cmpxchg.org, dave@stgolabs.net, hughd@google.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, stable@vger.kernel.org, Suren Baghdasaryan , Linus Torvalds , "Liam R . Howlett" X-Rspamd-Queue-Id: D9D2F100016 X-Rspam-User: X-Stat-Signature: b4tommzbm1h8xt6ipagy3wfmcwbnd563 X-Rspamd-Server: rspam01 X-HE-Tag: 1690927671-358220 X-HE-Meta: U2FsdGVkX19OQDbYQxHtLTJvNkoWMXFDxI63lcgk++m4FiAoZgj7+4sWYm/d+8x51Smzy+GsIZ7X5KoZrYAF7X/LQDLcOLqXVfI6Hzj7CU9cekiZxrU1Qfv2F2aUKFopWzv3mYvf1TmsH6Kcda1yIuKuHPksw5ByK+dyRveflNuv57ntSiDKsbLrWrKYQb65IU9a9LFPFBCcczkdZjgzFc6rNfIqwrLViW+RVPh3Wull+LxZiOT/ffZ45z2w8K64NutIhh+yLYl+ONrOTZHBcTxgxW8EUqCFnDEcrPHsKEWLh5E7yrqDco+mgFEXHhVVGzW4wAPjHReIPe0muO5kriKbzxQEGsrXPzxXF1z9p3y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/UQFwKyCaM+FrGsuJ+B+Em4enK4wMHDrQ3K8Vwd/bdEm3hqpeSxjc1oR57XenV3p3uEncfjeuP2FxbJ823Zt3NzKF3T33bO+o0RnEGkQKANdYsIfDNR2kzZlsafhsDqoD9Q8b6LYwb4VVNAYb/i1i0UWOwrSXQkINXCpqM8g19ZoGxIiybS3DwK+Lu0+335YL+xAwdiXN8XsQ6t5Tu8PZIjc4EbnsXbo4O2yYrubyIK1cNvoGMbq6tu24FAa+hFxmvav0+rBc9SCFYiMfFBJ2z4QxCUnM2bHeiqMwA7JL5pCgow= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: vma_prepare() is currently the central place where vmas are being locked before vma_complete() applies changes to them. While this is convenient, it also obscures vma locking and makes it hard to follow the locking rules. Move vma locking out of vma_prepare() and take vma locks explicitly at the locations where vmas are being modified. Suggested-by: Linus Torvalds Signed-off-by: Suren Baghdasaryan Reviewed-by: Liam R. Howlett --- mm/mmap.c | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 850a39dee075..e59d83cb1d7a 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -476,16 +476,6 @@ static inline void init_vma_prep(struct vma_prepare *vp, */ static inline void vma_prepare(struct vma_prepare *vp) { - vma_start_write(vp->vma); - if (vp->adj_next) - vma_start_write(vp->adj_next); - if (vp->insert) - vma_start_write(vp->insert); - if (vp->remove) - vma_start_write(vp->remove); - if (vp->remove2) - vma_start_write(vp->remove2); - if (vp->file) { uprobe_munmap(vp->vma, vp->vma->vm_start, vp->vma->vm_end); @@ -650,6 +640,7 @@ int vma_expand(struct vma_iterator *vmi, struct vm_area_struct *vma, bool remove_next = false; struct vma_prepare vp; + vma_start_write(vma); if (next && (vma != next) && (end == next->vm_end)) { int ret; @@ -657,6 +648,7 @@ int vma_expand(struct vma_iterator *vmi, struct vm_area_struct *vma, ret = dup_anon_vma(vma, next); if (ret) return ret; + vma_start_write(next); } init_multi_vma_prep(&vp, vma, NULL, remove_next ? next : NULL, NULL); @@ -708,6 +700,8 @@ int vma_shrink(struct vma_iterator *vmi, struct vm_area_struct *vma, if (vma_iter_prealloc(vmi)) return -ENOMEM; + vma_start_write(vma); + init_vma_prep(&vp, vma); vma_prepare(&vp); vma_adjust_trans_huge(vma, start, end, 0); @@ -946,10 +940,12 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, /* Can we merge both the predecessor and the successor? */ if (merge_prev && merge_next && is_mergeable_anon_vma(prev->anon_vma, next->anon_vma, NULL)) { + vma_start_write(next); remove = next; /* case 1 */ vma_end = next->vm_end; err = dup_anon_vma(prev, next); if (curr) { /* case 6 */ + vma_start_write(curr); remove = curr; remove2 = next; if (!next->anon_vma) @@ -958,6 +954,7 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, } else if (merge_prev) { /* case 2 */ if (curr) { err = dup_anon_vma(prev, curr); + vma_start_write(curr); if (end == curr->vm_end) { /* case 7 */ remove = curr; } else { /* case 5 */ @@ -969,6 +966,7 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, res = next; if (prev && addr < prev->vm_end) { /* case 4 */ vma_end = addr; + vma_start_write(next); adjust = next; adj_start = -(prev->vm_end - addr); err = dup_anon_vma(next, prev); @@ -983,6 +981,7 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, vma_pgoff = next->vm_pgoff - pglen; if (curr) { /* case 8 */ vma_pgoff = curr->vm_pgoff; + vma_start_write(curr); remove = curr; err = dup_anon_vma(next, curr); } @@ -996,6 +995,8 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, if (vma_iter_prealloc(vmi)) return NULL; + vma_start_write(vma); + init_multi_vma_prep(&vp, vma, adjust, remove, remove2); VM_WARN_ON(vp.anon_vma && adjust && adjust->anon_vma && vp.anon_vma != adjust->anon_vma); @@ -2373,6 +2374,9 @@ int __split_vma(struct vma_iterator *vmi, struct vm_area_struct *vma, if (new->vm_ops && new->vm_ops->open) new->vm_ops->open(new); + vma_start_write(vma); + vma_start_write(new); + init_vma_prep(&vp, vma); vp.insert = new; vma_prepare(&vp); @@ -3078,6 +3082,8 @@ static int do_brk_flags(struct vma_iterator *vmi, struct vm_area_struct *vma, if (vma_iter_prealloc(vmi)) goto unacct_fail; + vma_start_write(vma); + init_vma_prep(&vp, vma); vma_prepare(&vp); vma_adjust_trans_huge(vma, vma->vm_start, addr + len, 0);