From patchwork Thu Aug 3 17:26:46 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13340368 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 2A092EB64DD for ; Thu, 3 Aug 2023 17:27:02 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 59DD0280280; Thu, 3 Aug 2023 13:27:01 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 573B428022C; Thu, 3 Aug 2023 13:27:01 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 379E3280280; Thu, 3 Aug 2023 13:27:01 -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 2559728022C for ; Thu, 3 Aug 2023 13:27:01 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id E7E0880545 for ; Thu, 3 Aug 2023 17:27:00 +0000 (UTC) X-FDA: 81083473800.17.4EA315B Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf30.hostedemail.com (Postfix) with ESMTP id E13C980019 for ; Thu, 3 Aug 2023 17:26:58 +0000 (UTC) Authentication-Results: imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=z+dQhQo+; spf=pass (imf30.hostedemail.com: domain of 3YePLZAYKCIQ02zmvjowwotm.kwutqv25-uus3iks.wzo@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3YePLZAYKCIQ02zmvjowwotm.kwutqv25-uus3iks.wzo@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691083619; a=rsa-sha256; cv=none; b=uwpVy5f1ZEgCq73F/IIfqmJRsKmyvUFaxrOVx/lFo8bCFziqlE3cYZxetNIuvKWJJZGib+ I5/OQM9eHKPZ6rKnkIRRGNHjf7iaAO2HBeEU7tEJMPdRjuRk0rMrNTmjqj/2QC7QJOwoV2 g/5IIMohNFosmmbPglu7lb5MdH35Bw8= ARC-Authentication-Results: i=1; imf30.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=z+dQhQo+; spf=pass (imf30.hostedemail.com: domain of 3YePLZAYKCIQ02zmvjowwotm.kwutqv25-uus3iks.wzo@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3YePLZAYKCIQ02zmvjowwotm.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=1691083619; 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=DrHTbr1YuXxuyv469kZXDVB9Dn/3HZWhgPajObnT5y2Ps2ZPphp5wGVbpjLVwS8lOia6Uz P2Z5EnvkBZ0Al8jV6L9bQrBR6C/h4S+O4ouEEdUXGubEvTaSft1EMN7twZC4NzEOlT86Ok FdOLwi/JEVkKCKkwumc1iI3vd3CFFkI= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-d1efa597303so3974297276.0 for ; Thu, 03 Aug 2023 10:26:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691083618; x=1691688418; 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=z+dQhQo+Kt6Eb1Ib+dHdcc5V/AMlevUGc3YlsFbmFxB8Ja4tQL0sH583LPCWHFEKjr LCJT0zSGk242YQimvb9fWrReMP3dYgoxBDMB5aSvFbBas9Qi+mtKIyhJ5sOA6H93jSPA R+X5vWxIjdvpEuwSIeyZt4s4fP03JBTeA8GITi/vGy+0azOReq95xpDC5gosxo4P7L+f qORD71ozdIKCy1hZFUlq7/ogEyre/zziudpgQzDONH/lXc9z3s8VuH3xg5QPQ8rKfCS5 Q5g6DHxNksBOYdDKBZ5rLMB641Cm5MK9gkaR7CAnuxbqNZ64rc/TRUA/dgOJMrYH2nl6 /KbQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691083618; x=1691688418; 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=do3zfG1APMzt5pmKH8PqmDXE0uWmld86HHOkP8NQz3dHJAxQwUQ2pvbt2uCbplVpul Cff1+u8j57NFY0EN/7wquhjYGJ1gQmyLN2PqUOaT+8vDjz+9IZZEpWPNNRYCIsPZqeGo av8KLBicFyynPmDMmAoWM4JPlNpfzDM11ZYnICzSjhCgIiW7yTJVvFMro+s9LxhZfVSE 6cv+d66p2jQ8dYeg6ObRKKBl1afGYjtfEHCnPe9d2grAhi9EIHouB55RUvipUOHgv55L W/9Nub+NupV2Yzq2EqCI81zGPFU0CmuhscR2POSvfnrDAa+ZRkbSJ3zqNp+a8j2i86wG R8BA== X-Gm-Message-State: ABy/qLb/7EikUKA6I4K5bvDEKssI71YfFujNoLeNqey5C71AB9RNnFi3 MFzB6Cq8Xdjkik9jhYV1n2c955kdpek= X-Google-Smtp-Source: APBJJlHG20H85H4lsY8faC8hU9eTd9+dNHHyvDgi8rmCO+g2myKSnUTG+z8MfGkV4XoNO+8b/dUqf5AuJJw= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:3dec:ef9e:7bf4:7a6]) (user=surenb job=sendgmr) by 2002:a05:6902:11cb:b0:d16:7ccc:b406 with SMTP id n11-20020a05690211cb00b00d167cccb406mr243662ybu.5.1691083617987; Thu, 03 Aug 2023 10:26:57 -0700 (PDT) Date: Thu, 3 Aug 2023 10:26:46 -0700 In-Reply-To: <20230803172652.2849981-1-surenb@google.com> Mime-Version: 1.0 References: <20230803172652.2849981-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.585.gd2178a4bd4-goog Message-ID: <20230803172652.2849981-2-surenb@google.com> Subject: [PATCH v3 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-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: E13C980019 X-Stat-Signature: ejkqk4agcq6sik3ji96o1tdpmqn5ohr7 X-Rspam-User: X-HE-Tag: 1691083618-379962 X-HE-Meta: U2FsdGVkX18z9WrtmcZVP4O22BTdCxmSTUagylPqxpNzzXTr3UjwVUCEzTm7BpJ04YjzhsCQ4czckvrvptZSb1SKiEblNOcHyd13GrNCVzOs3+WzqeRcVTMJlv/Ks44ERQ/ay4Y535KCDaXNO2A3YXw2GhFjd7W8hOTf+W20HwsHT3BAa6uFxok6g/KEdpKCAkG601nvqqcnmRoKtZqPePTEJtCapTuaQrf2mOTTwJn7QqURcTy+ps/arlV4T8UXLsIe9sl0rQPzzh1gk/zGzI6qex0PjB/j3jW9zQStQai1vYzAR8MKRvE947UhQOx4747TMnbYvnngpyF9jo6iBRj4H3bMyFM2mYIA9Uv95/8AtYEsQmRYk0hsEbFg13FKDlgXQixpXpZFc6mcUn9ibz3nCJPsMkACTsZJwfdsP4Z/buElxhPlhX7ROk2LsrrlskwBTFP8GDtDCF7npe2STrLAOW/5bTSnuZlN7riDhMDvQCJ0eLvZkyeDGQVMjm5zBfKL08sDXnjzFjZkOSBZ1b7r8olX0xfcE6uaKx2Y85Nc1MQhrqHxCSc9P783qzqSBExo4imuyjNdOIjtgqcWV7tt3Q3xTnI40pG0ucVMAoLrLVu9jwWD0WBtU+tXbpR0k/vBkJ3S0BQuBzfMplpjMt+OZb1LIYGCodrhslhEbaJ6uS3HKed6koQ5hEBExFGuffDv+u/epIL9fJOMcPn4IHMEvyGgy07wHNjQFsirXylvBECSzOerOwrzjoOLyS9YAUTPTBT4H38OxkZAzKYNx1J2TXxmxq+ZxnvOFx5LqGNxPeCbnnECpAMlGuS/BEjFNSHUAztP1IPodp69BDNiUzZoQcvCqdX+22aWxNsi5+6Ov/GJ+xhHflR2UXSkCNajkmxbm1oaY4Og8u8EA1IsK9Odrk+3s2QZTfU2UNQKFBsD9gsi9o6ljQ1W4Uf9Zh51+7T4HABzw2yuu7Hl8Xb EFK0gHEq omPEPpJ5pE7DBgy4aHtda/wViMKmfyzXSlmIGPmls8RlHOBH+ewJJT7+PPg2Jlzjyd2P5/xdgaR1ZDAIuQvqm3ObuDT0eDF5YbLUC+NgKTjTkI+OyUmE1EugiZ0gI67oaOE4G1pS5mZSRUT+26ecPBYFsCyK2lUxjlgwHzn39VXDQGFHDpfhVeohifXbGvMl/ecFui+m4zczWA0Qn42KnQIi/Ye6rABWS1CuYDj6gwRuX8I+WXW2q3U0gYj7a/Q3YVueks7TdA3KjMWOQ/r7TkWE7udyX/RE33Jq/FhsAqf/ur4YzcMFMQaJaKKqTzIwoE4gW7gGJY82nqE3ftzLd5wxL1fPZsKsVjZV0sHGJRd9Yeb+k2FTkk0AW/pHk6k/GfU9H+KWK5edg48kgy/Yo02n7szvRUxqAtsY5pgRvWUEr3bfW/OycAGxRm1g0YI/mmSARmQ+OuJGZ010ZmKeuhDoCgJ67lUUIMIge6B1kZvRHyQlyzPITm5eMmjs9EWDBdsUm8vSApYpSt3ghC+xJPapcsNb9W5cLLJ0aPt3gHvQ6qdCQbB/ncfXsaqq5XwxLkOQkSvX23g1VkpJEp0MEN7XqjmvS0B8irabe+4+4xa+XMY4YyZZCnV22/+mPM/5dlM6bfC4WmV4BoczQbFn+CSDadIug0oaIayUOeA4GMCxAGwg76piqxnD/VJiJLTA++6kJd/+xJH2XCB/P3+DY0ekFXw== 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 Thu Aug 3 17:26:47 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13340369 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 32E5BC001DF for ; Thu, 3 Aug 2023 17:27:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A5910280281; Thu, 3 Aug 2023 13:27:03 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9E34228022C; Thu, 3 Aug 2023 13:27:03 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 80D85280281; Thu, 3 Aug 2023 13:27:03 -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 6C01E28022C for ; Thu, 3 Aug 2023 13:27:03 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 2D02F1A0479 for ; Thu, 3 Aug 2023 17:27:03 +0000 (UTC) X-FDA: 81083473926.21.AD05BC9 Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf25.hostedemail.com (Postfix) with ESMTP id 4BE71A001F for ; Thu, 3 Aug 2023 17:27:01 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=qxsXQyRb; spf=pass (imf25.hostedemail.com: domain of 3ZOPLZAYKCIc352pymrzzrwp.nzxwty58-xxv6lnv.z2r@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3ZOPLZAYKCIc352pymrzzrwp.nzxwty58-xxv6lnv.z2r@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691083621; a=rsa-sha256; cv=none; b=eHPXfUjV6RRabg3Epd1yb/HjauT4a+P50ig93vOWM3yljPs4j1nmkhEHhr3QEZQGUqKXa+ MZl9QyuA0wlctOnuZCqMevs/X8bbJh3bsRt9QAMUnVhgisPa9r4Dc8OV7ML2hEmMd6kAAM vPmfjJtAoiZjUDVIPGfJoy75p3XAD34= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=qxsXQyRb; spf=pass (imf25.hostedemail.com: domain of 3ZOPLZAYKCIc352pymrzzrwp.nzxwty58-xxv6lnv.z2r@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3ZOPLZAYKCIc352pymrzzrwp.nzxwty58-xxv6lnv.z2r@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=1691083621; 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=EHpUmzrQnDN6BdHRPVO0KcfDXP0OZ5FbBAepEsSX9Ig=; b=GvSJaYIJvb8li9o8gEVBxsPqBS3VEkgWZZxLZITBkoj0IArpPmgSgfKw1/HZt54CSox0p6 oRWmSGXzirw64C3TOS9tbyr326NADl039hwNdzUxbrk/xVlEaLri22GoIvHgA27nR1y3am wzOwY9Q8Q7PJ5YxJNtZGYqnoNtpjJlU= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-5844e92ee6bso13003357b3.3 for ; Thu, 03 Aug 2023 10:27:00 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691083620; x=1691688420; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=EHpUmzrQnDN6BdHRPVO0KcfDXP0OZ5FbBAepEsSX9Ig=; b=qxsXQyRbVAeRELuUUtKrCwywC6QQTi8GHNRwTQUgqwaMaE06n28e21MqPJCBZOSeOT Qdy3T1xm+nWsar3PElAyHH4XRbnbh/juQv6pT24VIx/hmG68SRWkDtgsWgv0Vbw+Xq4b +9kLmoit9sN1/gZfKklOc8XhcuoYMFvLaSeXIo/wdsXcRNSNRVLSqWMC8fCCM8aUcWcB aDSUJ5MX9993h31MPorj7ATaoVZTKC0TIpSNDyVZs/SHr6qPtsnYQt8mS3zlcdzRn4PP n6WROk+TAqngChxc1yRkKMEU0NvNbIWVZIRUByrfXNiHMt2U5V4plrf6i9OLozdZrkBK n96Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691083620; x=1691688420; 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=EHpUmzrQnDN6BdHRPVO0KcfDXP0OZ5FbBAepEsSX9Ig=; b=CkgZC4v55ioor6yLKLJIEZfbkK3h5dRRaYx3azrn9eqQLqHoYU49LzQinEL0VOWz3V LlnZyoAAMKphFGP3VwwfVJzJiXRz6wFjOFFS3myJNYcN9rRZLzUftG+l3nL9fiTg/sTQ 0CO9oNnVBlgcw95UwL6jeDoAH8Xn0VXtI0beEcB/JB67AoXr9eV8hwfPKQrMeFc3mQi4 d0gJUoF7n0nuQTLGNtOHzRdEHFa0xUGDTTranL8Ui1ODuhhYbKerxuKcNccn7lQXj1uz 4P7C+bbMHWKmfQ6+w9E1Wuunz5GK4Q9pD1NJoYhpgMGREh+1jakvd5pwDq8bFHOsOXdC qchg== X-Gm-Message-State: ABy/qLacf2zPNNVv1sRKNXSAJMOwkYPzLe7SZU90luE1c6Baq3PSrglH TcTU6uHx+2ntHQNAov6TpAT6MlRyhPg= X-Google-Smtp-Source: APBJJlFJcCahjdAYX9Zx+DEl8b9H6rEwuNU2P9eGDfvhDJtLw9lJ/qnx3rNt/z46v/MM98tnx4xoFSOUp3s= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:3dec:ef9e:7bf4:7a6]) (user=surenb job=sendgmr) by 2002:a81:c509:0:b0:573:87b9:7ee9 with SMTP id k9-20020a81c509000000b0057387b97ee9mr191557ywi.4.1691083620320; Thu, 03 Aug 2023 10:27:00 -0700 (PDT) Date: Thu, 3 Aug 2023 10:26:47 -0700 In-Reply-To: <20230803172652.2849981-1-surenb@google.com> Mime-Version: 1.0 References: <20230803172652.2849981-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.585.gd2178a4bd4-goog Message-ID: <20230803172652.2849981-3-surenb@google.com> Subject: [PATCH v3 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 , "Liam R . Howlett" X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 4BE71A001F X-Stat-Signature: t4bi4ue9197bwu11x8o74uex9eapmgkq X-Rspam-User: X-HE-Tag: 1691083621-282561 X-HE-Meta: U2FsdGVkX19GhsfQmCm0h/55DfROCBDUgKcbEJ69+bIlZMoTsm2e286raydYvRM6anMewExaYa63Q6lew34256pHzJmT1Jj86aenKzHpRnxrSIXGsvrHvKCVcH4V14uy/R+drOGVw+J75aHdyTKNuzZ+OUG0V6U/Bq50L1uB0TRmNaja3PI6fzrC3KkplKHoju0XA5PwDh6NM3GqDcxxd9c/ZLTolXgVZ+MT+Ug5q7GjPcWa/seKflNK7oqU7CH406q+6Kvam37kTACaRhzl9AOSgjd2oDnfbf8nFwMfZjZyIkC8WVw+UjjkOrcrgOhV8v2Msv7ZNu6dlh7apvXGLzWPlJq1yxuxB6vP/vCpJSxoCPZKtCCfGk8S+y4xTMrMBLQkKouPrQ+7NMJLrRwNDtRg2ZNW/jR1dOLC5vdWIvhCy7fQMiU9VffouziRb3Z4Y8zdHbBmq+Kxku/0PzkiasBuBR2hWJy8AW49InXOdLNb/hsNbLUAzeSCBKQok7gR5jAxfLZOshLSJbwIGVywH00Y+3rYJSpN5IffQKaTHrG4robI2Si+IsxgtDbphYBnQNDY0ajrWLI+Y/kRMjidaRh1UveRB/PXIkcKyT86fxw4GLLB5lIaKd7p5S5m4Fkmh5hAhMMUlLZ6fw8lngcHE01k8hMhjkc3d5HojBbUMXCgl0sP49ajtfPbbC1QhDbr3X811qmrxh+JpNA492wNveGTOfAWPn77xB72OEXXRoglkZeq9c4Yxh7TNf+BIqymlN8IPGDxApfExJ7hvTqS2XTZZi7nanSqrjca1OkkFWTSjKeBlMjetpUNW9gvPTD9b1kmXgSjX7AteTEjWwHxsbQ06soy51DJRBWtoePyaJKrCWHNz6qQ5V+X8997rBHC7/fzb0aNfsIQC4iy2d0zjQUmlCkMeYLeg0zBaYEXeoQR07SjQ4xqKjCUBOfWJBXFQ+7Q68bdCZPGSvT0ExW DtsvYiUT tGgZ62tP4Ui75+B+FklgJp08EbJ9vWTkOFLWHr2h/KzgdgGPkvmENpQ5WFZNs131aJD27DGpD80YfPa+pQO048KM3GamqKZEUCMZaUwdCd4T6TBTZeozHa1JhzeZvIpqZmsuKNOCrRdgju2pzcEwpFMbHjFJGm4DW7gG+/vgevcxHUZMWIYkRvFYrb9rz+oYwH4RYNSUhsjiVOUKXdDQ/DF4Vz84ObmX7PNAg84mv89jgS6LEckfIWcAv6lrjoGrqusObiIseEHdXr2YA1azbvY/kaEv1yOVx/0rzHA5H2k3UyCu/TsMM+3Q1C8W3/9kuGxM3uCSFSp4LyIMOa1eRZO8B/jwGksvB9UxMV3vUwNszA465evJfmMAW9wB7JAJ4YmuA6LLfOnDzzOaMk9fdui+30x9AbEP0FWhsWa/WhNYT/RzIouzZs3YmZG63IjJAvX55ju2kOr2XG70eEnXVMyd16MBr9DHv2Rjffy3HCqUCZSKVLgGyjDo08Eq4PTcHjh0MD66/wXvt5nqvnJxLt6LXkX8WV/e42thHIeO7Lh4v7pZ5RRPvBWUtsI5OQAOCkdDYfxreopfrSSBElasnGcOUbxPjOHbMU9EpFaBYGDXJQnpnEEygTineA8ER814LZwKBPCUSkZuRLxUm/XlPiBq7BYE7hJDGp5VHpan5QfgHLXs9qcPfxD6mkJwKE6/UyHHbl19OXHfjgj35yHZBdXb0Bw== 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: 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 Thu Aug 3 17:26:48 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13340370 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 1B0C0C04FE0 for ; Thu, 3 Aug 2023 17:27:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A7F7B280282; Thu, 3 Aug 2023 13:27:05 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A2F7E28022C; Thu, 3 Aug 2023 13:27:05 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 834C6280282; Thu, 3 Aug 2023 13:27:05 -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 69C0928022C for ; Thu, 3 Aug 2023 13:27:05 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 2AE80120811 for ; Thu, 3 Aug 2023 17:27:05 +0000 (UTC) X-FDA: 81083474010.06.AAB084C Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf11.hostedemail.com (Postfix) with ESMTP id 47DB84002D for ; Thu, 3 Aug 2023 17:27:03 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b="HHki6L/b"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3ZuPLZAYKCIk574r0ot11tyr.p1zyv07A-zzx8npx.14t@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3ZuPLZAYKCIk574r0ot11tyr.p1zyv07A-zzx8npx.14t@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1691083623; 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=LfajP5IIbFpg2Y7BQRxvk1sHiLCqjf/rsCkC3jBCtoI=; b=g3GT/1NE9RvGOgGhrq8GHhfZfkdTsc6rgNQz13x+buTiPx78TzwXm+tXSOAnzqaL1DQ0ff tOPCbAclMvlNsMyKZ4uayplHP7T1xkRMFNhu7u5q8IxWMiXGn6Tpte5ey7Bu5ucWFD29u9 jkUf4qmW/qb93AxH4v6jc02nosflTqM= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b="HHki6L/b"; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3ZuPLZAYKCIk574r0ot11tyr.p1zyv07A-zzx8npx.14t@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3ZuPLZAYKCIk574r0ot11tyr.p1zyv07A-zzx8npx.14t@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691083623; a=rsa-sha256; cv=none; b=IXf17ET56sA/1vy/VVRIwbPk8eEk8pzoFg3XNStJ9ZUc3uNBXXd93gxKVGpON4YMHLgHfL R1fFJzhLnvjZfU2JvYmM55o9wFViWnNuNh+t/ILXyyja4skRjQ00mgVMmejcln2CLl75WI aqXtdaIYcDk0IE8FLPEMPc79I1h/L0U= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-cf4cb742715so1285617276.2 for ; Thu, 03 Aug 2023 10:27:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691083622; x=1691688422; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=LfajP5IIbFpg2Y7BQRxvk1sHiLCqjf/rsCkC3jBCtoI=; b=HHki6L/bmVrbbgbTtB9WdMtyro9jT73+cCAxKEfbQcmR3qH9v6ec6CcbLdBDBiHtbD DgkXEJ1VQUd+cMDxmtIXTtXdoPNmy73uD9LZlwIoe2mdEcYq/BSd/gAhOgP0fAm/mz7/ 8v51YuDLWoZ2H6bS1nhWVnhqzAMn395dK93Vn63kHAx/qo6JPUNuF05lCgUdd7cwcCXg Le/Ctgs2wSzsf6ywyFYUPRCg/ETixmQpFAd/6SNe06GNe12jbStG/hoYgMj4KbT0T2lp njpni/PXfMsrjLG8JE43HZ3PBUxm8AfwfKRLkTOdqi08qFkN+w6s2O9SAGVXJ8RtvGZy fOVw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691083622; x=1691688422; 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=LfajP5IIbFpg2Y7BQRxvk1sHiLCqjf/rsCkC3jBCtoI=; b=FoD8bRXtjuztfim7fJguSZhbfBGdFmVqvKeKMVmUYkJwp79VhL32CxGw8RlAf3/+NQ 6ewleoCH4fA6ZuOPY5NGCGFDlQRyyThkQ6+9tyUIDrIKg7WbSKymIzeomfY3PAKnYkeR AbD7VsqBjUZ2JmOra7CrYfQKud9pvzPRPtc4XwKZibaJkpV+qV8zWutrR2x34ZmeOfT3 rn/eMsjBw+H89zs3BdtboThs2Prlfk5EnEvGyhbqc80qTSumLL0iCAGeQDO7rE7Q6w1U XV9pFCYe3WNtUbMc16UVJKlIK3YzFsfpvTv8G924GGGqSJdzmi8XNIitj2qCeMbVMqZ5 1VwQ== X-Gm-Message-State: ABy/qLZ5NLVdFAr+cpi0zUSnSaD7D50DyPzQpd47kx0E8+8z9QPYFkH8 x4Oed8XgLvahx5VB2dP2fsOCBGNIWX0= X-Google-Smtp-Source: APBJJlERuzjun8O5+88x8UQezAzWCkSvC+SxUAPYIz16CZMGZKaq/Tmi2dNZnAJHx5Bu+iggd6tbtkFAIsk= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:3dec:ef9e:7bf4:7a6]) (user=surenb job=sendgmr) by 2002:a05:6902:569:b0:d11:3c58:2068 with SMTP id a9-20020a056902056900b00d113c582068mr132529ybt.2.1691083622391; Thu, 03 Aug 2023 10:27:02 -0700 (PDT) Date: Thu, 3 Aug 2023 10:26:48 -0700 In-Reply-To: <20230803172652.2849981-1-surenb@google.com> Mime-Version: 1.0 References: <20230803172652.2849981-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.585.gd2178a4bd4-goog Message-ID: <20230803172652.2849981-4-surenb@google.com> Subject: [PATCH v3 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 , "Liam R . Howlett" X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 47DB84002D X-Stat-Signature: h3g7bui3r7rqpyqy3gezoo9j53ji5e9g X-HE-Tag: 1691083623-295162 X-HE-Meta: U2FsdGVkX188CTnCQdrYPpjGnomeM6NHsP+MGH/A1zs/OWRNsjS2bwcHuhqO0cNqNFuwKV7lAexaDGyzuFSniXriSzsIOeX29wpKoqXGffL6qjNR4HOyVQ705TJct9gjsmQaoIi91LjEy/pzoVY09ClDloVlmMbnphutcoNcMPTf4HJUssRP0HznYc3KWaq+Ds0Eat8KVzyqMlFfLjJW9nSt5j9pcZkKfn4LSWdzdV/bOtMPAwcyvGaFF1eJWUvgI5foVLiKJ/o1K4I1GCHNJfly5H4MKa2ttKjbwL1lIHQ6jE6guU7GQ00EABjPeUQLwFb2thZzBtzridxYp7LL45iDBMZcGSNME8pI1DKNc2wvTFy1qzKmrXgf92EH5pcTsvu0DYdA4s1q5u0Gij+Vv/mGVnZpBS/NUqtWxElboPPos3SnmDaOdsS6cm9G9I+QyCcVvjIwzVvoo7UfLZbFjMZzRQ2wyYwqtUmr9i73+xHZeFA+O388FvM7XrqzQ7o81FVsG/a1vQKoj8dEHoyFre81GvvI727sbaRq62Xnm0hSOnOScTukJiKTxIoVXu1EWs7wnpQCKTiOMUccYlJvs7wmRaYc5h5tm5zIfenl+Hh6YZ13jcAPn2lrU/yRL7hE9Oby12tdcz1S2vHjitLeFyDR13gVlZuYLgPu6jx4hSyWD8fpNG+8ByoGFJ5Co6hW1/aMHFNB2SAIsQFHGAcizc68kEF++AtYi+jStT1Qr5mFwe6x6Rn8dEb4fG5EDAWeDoNabLdnfe1JCmykIc7Z5TjyQQJa+aFRxj0plDLxSqZi+UAp6AoW0Mn9U1ZHnqeA509S8ruk05tzfi0JV+ypNIGnKxb3qpqDjQM+Q6sGwa8Yklgx3gRhrFqfdu/qedQ2KDKqfB2HzyqxsIXu9/Oxl5DpDxamBi/BYl7BBX0Js9z196hgDeb0pdXydB2YaV/rt5Ig603d5aED9y7ZHeB +53ETauR 0o4qJHtdSHVlFWh9RVKTzKDbMZgFWXXUaEEIq1fJYXjRYMLAM/48P1PM4uwl6mSJa2orrTgKj7Ng7Z2R0OXzne/ygZJo/M48x3idbSNMmABM7zNi7MKF12YTRSDnEdHeEIfD7kWaywpmE1r/FPLK1yu3M46yUEvayel6EVjhmuydDOYLgLqWGTfR9WV2upTcBRMrhR/HqVTK8CXPOIffnbdqchq/Y/2w8izzc9VTLXz5ZgO8Fq0Rj2kJM5NaoV0OsKK75Jkt4HKh1krWj/hbpKH5kZASXP2+glcu3W/1oXoPLSK3w0W8qADfsfzWycLHYZaDqpwlC6Uda0WTmDZEK3uCCsi7Gsiycx5bL4UV+ZLBvT6o9cJ2CfthP8cDeMwwmKeTx5yz7HoqxEtBhXlJ6gubfxWXJntwEgpOFyUF+ZFi50KJePVtWdoLpa8Y1FNniAVrffJ/I3pFNTrTOhjrAwKOjw+wU1yxs/88rU5by5N0SnjibvlboJYH60VnCXfiLRdTBVkBaji26Fl/vVQ5bolDxA5sE5DljyXDfewyXg7CD7w06cfSVZIOxMTnCejGqhxP/OE0GFDSr9V/3mw9dm092uu4eH9RNRcPRmQyJSATawaVihz8hr6WW0Fcx4aw3g0Xq 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 Thu Aug 3 17:26:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13340371 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 B2A38EB64DD for ; Thu, 3 Aug 2023 17:27:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 43D06280283; Thu, 3 Aug 2023 13:27:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3ED3228022C; Thu, 3 Aug 2023 13:27:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2402A280283; Thu, 3 Aug 2023 13:27:08 -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 0EC3F28022C for ; Thu, 3 Aug 2023 13:27:08 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id A23C21412EF for ; Thu, 3 Aug 2023 17:27:07 +0000 (UTC) X-FDA: 81083474094.11.37A150C Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf20.hostedemail.com (Postfix) with ESMTP id A0A871C0017 for ; Thu, 3 Aug 2023 17:27:05 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=3B64s3BV; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf20.hostedemail.com: domain of 3aOPLZAYKCIs796t2qv33v0t.r310x29C-11zAprz.36v@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3aOPLZAYKCIs796t2qv33v0t.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=1691083625; 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=520TPAR42nWNvH3iwlw29nIZA75JEpTNGHc6Ua8ufv4=; b=wlHpt7mdhPUU4F9R0dx71o+7lfecK08rfMGdRcQTIDErMEJY/2Dpb3N8xW+kb9vLRbn9qW jc9MyS7ATfgVgHbmVnsBElhEgfWSHpn8GAmYLTGAJfcnnT+7RQFyX6bFSmiTKAhGCWvHF7 mKZZ733ok7lXDBinGiIu9KcLvJluFuI= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=3B64s3BV; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf20.hostedemail.com: domain of 3aOPLZAYKCIs796t2qv33v0t.r310x29C-11zAprz.36v@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3aOPLZAYKCIs796t2qv33v0t.r310x29C-11zAprz.36v@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691083625; a=rsa-sha256; cv=none; b=31aqMlqClJlFTjfsP8FOwJyOl40f7bK5jNd6yXrcCJoo2k/rI/z32P5/wUGfqN6oXRks5u ITSJK8sSUvFT1/CF42w4FMasTQBLCvhC46DabEIwW+Q9r7Zw8gamOpW2fNJRaUP0W74muy FYnNIcbEVJTVRblAjoTtDDYBYzQ615Q= Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-d07cb52a768so1367251276.1 for ; Thu, 03 Aug 2023 10:27:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691083624; x=1691688424; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=520TPAR42nWNvH3iwlw29nIZA75JEpTNGHc6Ua8ufv4=; b=3B64s3BVubEniqIBf5qTeAuU3p0nbnmnuBYsB+RpN6AZa3i3GGRn+/I8B0TAlv2Nks vcgJasJ0d6hq72DZeM0Lcm55BQ6bd/hb6ifugNbYwHXuziylKqPLbEduqiIbOnQiwIfT g0u3Le+LiGigCWodBqgYIs5hzDhaFXFQSJpVsN0EpnORpmgSB4VWx1SGQLrVpR398SXM jj5YOFr/dtJESoBiTGX4JBxqkjM8c4oYCcIgTjq3iohH0dJbfezpHUzECnQsmKYwCHk1 6xSsPFv1N8/zqJ0kI7aEpFrdp9jeZJ3j7FBpEWsso+eK/G/Snk1NGW/K3h+S2znshr9F Fcxw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691083624; x=1691688424; 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=520TPAR42nWNvH3iwlw29nIZA75JEpTNGHc6Ua8ufv4=; b=RsT15kgyAzGz1kXfA7e69rEyEUtslXw7CqjvzQIaApjg4EsM1lM+Po5wKm/vICqo3q SoIYMr5M1/ncvELvS0pOhDHpzaRnM6gxd7GRksgTOJbG5hPbzGexfJfsgweHmPtSg+r2 aikhtdmVmexvJCmIgNyhNClbtPb5ONf9khRrWNui08z/FNZRzYkMh2Lg/+KRwZz2OfaB bocAZGmNYKM70Y9p3N4iFo01Mjz0PimbNIjr/E+ktzIyLc4YO3I5VA6PwFAOEKuDmBDi +JtCGYoQpkeWMh2a+cRYLR+hle8D95dLpOubPdaozDoKFXPgyUnAE7SZWxxMw8A0fCCa iPeA== X-Gm-Message-State: ABy/qLbaBlO5tK0Tjvv15YTSzoHc0AmO4KqU/Zet4qU3v6J8I7OL4yJd ZI3CJDa/N6Q6enVRZRqHjLEt0nJFDds= X-Google-Smtp-Source: APBJJlERLM1xcqSCyBewM6WMczSZdvf6/mfoOsnnM0je8oG7l6a+OZ1VwOuI0MHYyPuiy7m+zNnSmK0xJcM= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:3dec:ef9e:7bf4:7a6]) (user=surenb job=sendgmr) by 2002:a25:250a:0:b0:d0a:86fc:536c with SMTP id l10-20020a25250a000000b00d0a86fc536cmr131344ybl.2.1691083624748; Thu, 03 Aug 2023 10:27:04 -0700 (PDT) Date: Thu, 3 Aug 2023 10:26:49 -0700 In-Reply-To: <20230803172652.2849981-1-surenb@google.com> Mime-Version: 1.0 References: <20230803172652.2849981-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.585.gd2178a4bd4-goog Message-ID: <20230803172652.2849981-5-surenb@google.com> Subject: [PATCH v3 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-Rspamd-Queue-Id: A0A871C0017 X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: ykme8xknh96hc1jd3tjz574w1wbmh5hs X-HE-Tag: 1691083625-87607 X-HE-Meta: U2FsdGVkX1/Tpis0pknUkoRzzE+5BWuAnOm7OWSJ5ipHwgCP/RQXauUwa9mX1J9OZE1XFEj/STFvs5phYEB7djG4/MjiJI1JOqyTwNCJl8pO8jsywQCUyK/nQ5Crks063Yszlj8ky08GGQZZj2Js+KdDhHT9i58jnUHvJcLYu9wfuQUR9ZSghcNpn6x3SqcqxUT+Ytx7i84oh7NvOYxmGlGPg2fjPvL4G6kozUjGIsZdLfOC7ixesM7KmoX34QlwTL0rkG3kKMmQiY8VY27efHaMpNQlqOzdB8nJsKaKl1oAgd6QYcm/8OY61IGBP6MWVZer2vAm2Jt36aRLMY5npTwHMGnREU64IU9qKU27gr839Ok3fcj2jbT1G+lfTfPFM4dH7lY1UZUFCM83U7bglRG6MtADPq0TPf8jhpO5iRTwhT/UQtpvFycxiFiqpHazq13qyv0BlyUkREcJxZlQ4oBj89dbdq/k0VkMaMmtJ/ERaISoPTnuqPEIthkbPBgbJntsr6D6GwSyW7zyzTPRB0ZL0QNJYJSPWaxF39s7YYX+DUJ42B7tr1EefujLBXDH6dpCyIC213xUgbCviLVncQSOFmmy+a/Gk1foMrqo+g47oaa8bDOAimx5bMvqe7ERmhWDraX57R/N1nzYRwAJBJdrMzmFem9Z+ZAcLJKbNANkWkEbR5ZQVobG5e228wnEAxM4mQQMPe8Rsnz4kWptOH6YcyIMXXGBZYg2VGgC80lortOnNzJ65jVQWlcc6+m6/kVnKcvWfCBeBM3q2na20RkQos0m6spzSpcpgdUQlzMq7Fgx0IH1R70XXuNWSZNaeWvOIu9xfBtJ6n6edMptwO/6nCgCF5Rxzxzf3Euvyg5YJAeZc6P/4Z6cT8TzcldXa3Xyi4yXb7r8ODinLGVqnLPprzstwOVQWkriTrbD05rpHmSSReFbrme2TbTTdkureGRwTaNl+5cCXkK+Ysd ue4mlNbO nuwKCL3VCNuKiRqYwBDmb6avP4CVr/+HvGeYx9NDzQG0fZTbl0V9vxKMV3fttYDv8TIAxfwodb/KNiapbtwxapnjT6RlEIXdRYjgov3MA2u7Slj58EvogGChWslhwjzxdTHYQ2hawP/d1Dyvq/VxAsctvLdAm6pXP+UNCzy6Bc7RJ1FE+b4pFFVTKYyHhA2OymdGj1RxjHRdSpQDPELABiZyHLWFSr9eg/ypcVTTh1imVjq9xCNAS8qip/NZY/JYFVQ1+I/Pho67aYJADC1q3bowhKSDGKthcoJ5W0BJv7VILw+9VsQ+9ncxfvDb9hN4AwqwnLVWDnyjSOgEcBjDuSgUXWiZwJEb6EkjRoHU9i8JA9fC2xx01TygUftJnNTu5aNig1OHEbQePLlarAoiH/WRh6dzIgP5U5eFLB75FdioA8s2WgFU/+ekZbqQmNnFcjHpeKUH2GZrU7AIcPGNwGzPWbsJSlrJRWqVC7GVBdDInndnNWrqYNKb0Y7QsEKj4+J/Jcz/8aYR0v43C7fiLcI7CS9OHnwO3MAJFYllxeR6+mQcymfmpasulYTp66tnSxEIzaB5rf/I3wqGTaQasjcsYcp3S7YzsoX8owM4l1eBqBhUvuYDI0Bl3duB1fXG8zMGhhZVTeQJWyDynPf0F62cmqY7ouJ5O6SdyDDA8CX1MS+xa6/8Wxzj3eMJj3rHx8kXqEMS4zpakg+XVlCDMPo7iHA== 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 --- arch/powerpc/kvm/book3s_hv_uvmem.c | 1 + fs/userfaultfd.c | 6 ++++++ include/linux/mm.h | 10 +++++++--- mm/madvise.c | 5 ++--- mm/mlock.c | 3 ++- mm/mprotect.c | 1 + 6 files changed, 19 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/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 Thu Aug 3 17:26:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13340372 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 9F560C001DF for ; Thu, 3 Aug 2023 17:27:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 379B1280284; Thu, 3 Aug 2023 13:27:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 32A9F28022C; Thu, 3 Aug 2023 13:27:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1A697280284; Thu, 3 Aug 2023 13:27:10 -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 0A82128022C for ; Thu, 3 Aug 2023 13:27:10 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id AA090B2D58 for ; Thu, 3 Aug 2023 17:27:09 +0000 (UTC) X-FDA: 81083474178.22.4116189 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf18.hostedemail.com (Postfix) with ESMTP id D811A1C0010 for ; Thu, 3 Aug 2023 17:27:07 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b="iMlvdhm/"; spf=pass (imf18.hostedemail.com: domain of 3a-PLZAYKCI4AC9w5ty66y3w.u64305CF-442Dsu2.69y@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3a-PLZAYKCI4AC9w5ty66y3w.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=1691083627; 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=GZEu7FQxJMwmMQ2ZbaGaDze4HsujzOay4eQMS+qpos8=; b=pbpC0W4mzjLbq6eD0c6Qv79gieoA86Qbkn3h62W+Nuo5h77hTQXcS1MN7JaR+nxXY5pA5y u9y9MfrUi1CpF1nsd6YDyp1XS+wv0hn0PIuG0i5D1nqbry/192x3Bn9av2dEaoPJZxiKhS mr27ZJPE72ve9r46XOFbm/JEmwm5aOk= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691083627; a=rsa-sha256; cv=none; b=m5DeCXHwiEhMta6O/Uiyzih9IUiEEO6ffPEW8FtQXaCVOkcEfUNwEVkOCy61xVeHTE71IY Syt4e5iaaSaaNfWxfIbFrRDXDVlv2Px3BGxTE5XDPusjmNNqW/hRJ7nA+t5u+RqYbOQVo7 QyaxuzKdp8wfambjTCc/iHnyrPlxlVY= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b="iMlvdhm/"; spf=pass (imf18.hostedemail.com: domain of 3a-PLZAYKCI4AC9w5ty66y3w.u64305CF-442Dsu2.69y@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3a-PLZAYKCI4AC9w5ty66y3w.u64305CF-442Dsu2.69y@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-d06d36b49f9so1381463276.1 for ; Thu, 03 Aug 2023 10:27:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691083627; x=1691688427; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=GZEu7FQxJMwmMQ2ZbaGaDze4HsujzOay4eQMS+qpos8=; b=iMlvdhm/bxVV7kv0V79kx1hxAyb+1GfcPnJNgAo6LGdR0bN5ykKWLzgwRkr0IsKULR 2o34cI0Yf3y9b3S2Dr1CwRyv6f01fPNaOoXOCMRAQbhpK1DPGFgCjTrhlWjoMsqAvyL0 8VUIztQRc1OQyCbz5DagmCU/mu3dLq6uVQlfq3eZNISYdHHkvNHZJzuMW6YuKo90+kyC hjZ+CUef9CzPm33relwkpyOY3ShX8wwK6g3JLpiI+W0wl5DrWoPM2vnAEuwmowfvDdjR SwLvk9fJF/d9WaXsmtAEQnK/vJCJQgoZOjJbm+glRWLSwwbs7FTj9oR+NIkDHwsI3k9/ MBMA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691083627; x=1691688427; 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=GZEu7FQxJMwmMQ2ZbaGaDze4HsujzOay4eQMS+qpos8=; b=YRv+jRABkRBEg7FWheIHuZW0SpyH/0w4ELFyzdicz/OTbpePIIDRT1G00sAvOZaI+L PLeigqfcDJWUeGIy335xAY6QuLy/8F5j6zT+j3IQC599ZwP5mUL5TxuioiIB4/2aA6lA AwEiXJ923R/q2aJfFOqy6/uDaOH+hahRH++CLLNOaoTyUaRKJmRY+LzQE2CSfwPEk4R+ 4M9Qpi+YoZYU0cys8bki1Ytnj5nNtepd2rK0SqXPPeuROfNHhe4dvKFsmSzANb4ndCwf VVjX6GhDBhoStxiKW4PWWmo9Z2bopyxgl3n2VViMzUbVW8hwI4mX8mclLOixeV0XcS+z QzuA== X-Gm-Message-State: ABy/qLaZf5G2MRrLzRJANRWERSHdeGG/8Q1pOgS8/SDWzOq2KMKjALx1 qZmwyvNxFWhxDWap9dOUohcu6UQDXQw= X-Google-Smtp-Source: APBJJlEm+TAFgwKeycGXNkE9sZYz1YS9giHD/D1paY/qC/rrcXYAkNTs0f2pZWBHAJYKXSdQl1MdNfKRQzw= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:3dec:ef9e:7bf4:7a6]) (user=surenb job=sendgmr) by 2002:a25:19c2:0:b0:d0d:1563:58f2 with SMTP id 185-20020a2519c2000000b00d0d156358f2mr129793ybz.2.1691083627024; Thu, 03 Aug 2023 10:27:07 -0700 (PDT) Date: Thu, 3 Aug 2023 10:26:50 -0700 In-Reply-To: <20230803172652.2849981-1-surenb@google.com> Mime-Version: 1.0 References: <20230803172652.2849981-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.585.gd2178a4bd4-goog Message-ID: <20230803172652.2849981-6-surenb@google.com> Subject: [PATCH v3 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 , "Liam R . Howlett" X-Rspamd-Queue-Id: D811A1C0010 X-Rspam-User: X-Stat-Signature: h46z795kw6xb4qq4peeztdfgsc89k5hz X-Rspamd-Server: rspam03 X-HE-Tag: 1691083627-840619 X-HE-Meta: U2FsdGVkX18+5dAAqtPC0yROTifHxlWqicM+Zz1SJE0/LERN+JcsFjAprBGbMZZx2/rNPwir1APn67cWcs2Vzw+VWKwFMSpgtgCH7d0w7X7VN9oCCt/woHqsne8C9VktPjFXChzKwJl1ScCP5+WdhUl1CeDubwnzzRaM7EZo8ji9uoWmGmV/slBC+5FZLfZ2cAMVkIniUdTOkH4rLf1X5fs2frLMLEC+oTAIEf2BUb6fRLnqO2imY/FK01EQ/mOTvNTV9LkC7MAER3+9JvM65PP8uuSit3Qb7e7RyZMIG1BfQoi0200vP0Fnj7C5FSRZc82S05Dqj9bv9/AKQKA3x3HOZP+b94wGFV0sgLFrtM0BB5+2/EJcMVEh7cdMeU6kyBAJ/PVPBZNplhzKodaB4eWVWffb6rOgFtEGxVBlxMLU2F6/rnV5CVs/+blURIM9K8nK+RYX5/X3JPrXkfFKXAFA0NaHmwylRx3jxyHp5Vc5j4m2X27QSVz/jUzwhuH3L5/YRc3DilfWSdsXA2MpFxeWVtJyk3+3uc0YL2tSJ+vtKWs1Yb3VZkjyyy1nC9Q1sh3NoiJXXg7cWhNesOPnk4NsgrtOcsvt3Z/7HNFniYKzSFtqy0nCI4cVzZY0BnL9wngRRq1CaESmoOcsYqYLmUCbYKHAvZ7qrRQ4V9ijJ4X73lmLUVSrS7jv6DxwRgU+yzXmnwSaIdyjoCMxiDn1VwGK73Hn1u8XgUFGW2GXkKKKPqOr0fmlsdvfvDNm8RhAJVwNrMXosf0wktleHWpxe84Q16fKkkc8H7wLc3T5SgA8nKplrEIp3qV01TTppdlbuLBeRdoRDAcFHhyCRPzBzTYXNGYlpQ7KCQaWoAj6GZpYqwySaYu2IgJrId9dJjBH5tAM0o/p+DzFZHaHImckpvSzVNlBumJSdk7pRrPhQkAZuQLvG3gBZBYzaac7z+4sIui153L7LFcwDoUWeK9 TOckfiaN HUSxpDKzwbX7i3WFiOWcjuOg3mwRq/U9DmRuLslC1BvV3QNvro2+0bRsWbhXGmy69cVGLGZArPKN3+3e926Q0WQ1N/3m4/Weie9WDF99nu6jSltOd5+8zWH+tr5eSO8jd0moEpDHK6xvsOxWLQ+1jJy2g8lVPN2Gdj/h+4lB+VtcwbwaWvHOdOwdbm3sXDylpK+cS60haoxe+7QzYVkrP5t8tDouiGJASipTi462CoswTxg5sXSYqmCLiJtYOyC2io7ZkZk51XXD1yik90zUEUQHh/DDa5UZu2b7UssEONtB2TgljK7vVwbFmHH9qbrb7YxxIxxlPTpUJERWxSHEhLXO1uoxNy34puX/Mzeh6JXGFjVSRLC/VZCUX5qBT6wlCxJt+DFMCVH5oBeOb3Z9FC/Epa8BwVrvlxgkgAhtHjLRbHGOuFlprJli/aGubLmjmBhWxulKVj7/u7bdIu85AxJA7EVatuT0Tf9jik5UwKbnUWacBxVFNpwpIh6xegKw8ux8yPy7F/BDDF7yuXysFOhVP5QaBVx01vqh+DhgROWt+Fx4rbukFtjHFYJb/1WxGNFk+ZgJAg9OnM6KuNTpRNcXoZd3/dAXts7nxWJ5Hn/GGIXP+gRf0Akn12/PFKpj0MUUl 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: 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 Thu Aug 3 17:26:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13340373 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 26016EB64DD for ; Thu, 3 Aug 2023 17:27:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B42DD280285; Thu, 3 Aug 2023 13:27:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AFE9E28022C; Thu, 3 Aug 2023 13:27:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 96BD6280285; Thu, 3 Aug 2023 13:27:12 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 803F728022C for ; Thu, 3 Aug 2023 13:27:12 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id 2B0301A03FF for ; Thu, 3 Aug 2023 17:27:12 +0000 (UTC) X-FDA: 81083474304.22.E6CB0AC Received: from mail-yw1-f202.google.com (mail-yw1-f202.google.com [209.85.128.202]) by imf05.hostedemail.com (Postfix) with ESMTP id 42D2F10001F for ; Thu, 3 Aug 2023 17:27:10 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=zMcctUOh; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf05.hostedemail.com: domain of 3bePLZAYKCJACEBy7v08805y.w86527EH-664Fuw4.8B0@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3bePLZAYKCJACEBy7v08805y.w86527EH-664Fuw4.8B0@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1691083630; 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=enNugCeOmhfazHyzEjQZvfU7Wp5uGQcTSmtYdGh09Fo=; b=5IUjcS8VGIIIOFUOknCei1XCxyw631pHr2JvhBEsabqfxmy3L33nPcYQd+eqIuFQ5eXFO1 mrtwXejTMA2rEdxBtH6ATYwFp3oX3CssOF7i6itDJpHyx/edjpRvS9/8kXZNYEcaWjaf2n 74hf79YeDP0NW+1FebEJwro+CyDmzJ8= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=zMcctUOh; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf05.hostedemail.com: domain of 3bePLZAYKCJACEBy7v08805y.w86527EH-664Fuw4.8B0@flex--surenb.bounces.google.com designates 209.85.128.202 as permitted sender) smtp.mailfrom=3bePLZAYKCJACEBy7v08805y.w86527EH-664Fuw4.8B0@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691083630; a=rsa-sha256; cv=none; b=V1Op1ok0urecGYnuWfNhuFxbxufYuaPxL3wcZefKUDXMiR/5djciSR49bJetL7XEKQ2b+x eoAJll0hbtdniyJIVnTXziiixV1bal/TQ2kQ+dlthEeDSpgww9tIRadTAIN42y2f9kLsBC heBIBambLDFm701fI94r86FayqYewhA= Received: by mail-yw1-f202.google.com with SMTP id 00721157ae682-585f254c41aso13568427b3.1 for ; Thu, 03 Aug 2023 10:27:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691083629; x=1691688429; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=enNugCeOmhfazHyzEjQZvfU7Wp5uGQcTSmtYdGh09Fo=; b=zMcctUOh7tYayLvckt9uz/YiCskGpP+YntI3h+0uVE/02dHvg03tF8YSHbRMyNGfZB vRYvs+C9OZgyahs0ZA60vqFKBKDcsU68jI9QF2xZbnvK1zTGlMYfnt4cUcYYUYYQEZgm QcZToqsbu8sycVp7w2dB3p8Tho375Ac9GC/kb1P9fL5XmVLzXVE4tmVCtIfbtq++EekQ sXwQggrredRKZtR876ti7Xpr8W/4VwmfvILxh9XXU6UIOZhAVePq4SmwRGAaJR1zzKb1 NYBjYG4UWWlZvCyBazMus+wGvZXN0rUGhkIT38Std3TmC/giO7p092GnBqUklLiC2uAW CiUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691083629; x=1691688429; 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=enNugCeOmhfazHyzEjQZvfU7Wp5uGQcTSmtYdGh09Fo=; b=NMBBnBykvyqgnByQSWtbDKn8F3lKbzdwUpy4iHkDemnGa0TdW1gR1bgwrzhViQaHTL 5H7LHcxfC+dfqSLwh7Z87K9+7AsVFQtuGjOdvII5O7LR4Hs5ukmc6M2G1wHaXi94am/j nslC0h+ghRF8+N3Wv17bczBJYnJx3/8xlIqfNkMDAANgEzWFrj6Kok6BfPxgXZteQcrS OI6ScZd/SmBEwYy9FoSCCUml0YiTMZPWQgbskOpHV16WH/7DxszWGQTg2+qTMkStYT1R iW0RV1fDLUKRrEPu73nFcbgyJzZ8Wpdks2amBdPVPcva8FzDzI9pEy3YRH4B32sJ74hX oBIQ== X-Gm-Message-State: ABy/qLZfiqjMHN6ZiAMX2dc6grzFSeP5rshMi/9km5rhav3ytK6zMn5W n3KnIjx/BHcyD4brNCO0Ntw5REpyOB8= X-Google-Smtp-Source: APBJJlFs+p1+IFx0/LyJPwchu48cnzJ5YCTLzpQdjYobzqCAPy/6bN4ZVrBDwToPCue3BOGsJ6NlQ5kk+iE= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:3dec:ef9e:7bf4:7a6]) (user=surenb job=sendgmr) by 2002:a81:ac58:0:b0:579:e07c:2798 with SMTP id z24-20020a81ac58000000b00579e07c2798mr160834ywj.2.1691083629316; Thu, 03 Aug 2023 10:27:09 -0700 (PDT) Date: Thu, 3 Aug 2023 10:26:51 -0700 In-Reply-To: <20230803172652.2849981-1-surenb@google.com> Mime-Version: 1.0 References: <20230803172652.2849981-1-surenb@google.com> X-Mailer: git-send-email 2.41.0.585.gd2178a4bd4-goog Message-ID: <20230803172652.2849981-7-surenb@google.com> Subject: [PATCH v3 6/6] mm: move vma locking out of vma_prepare and dup_anon_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 , Linus Torvalds , "Liam R . Howlett" X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: 42D2F10001F X-Stat-Signature: z9sq3g4rst3q6eq3bakhab5r3q1u37fw X-HE-Tag: 1691083630-900314 X-HE-Meta: U2FsdGVkX19nYvEe2pu4MY89/9Lx0NWhcTwCQkxfDYq+lDkgrSGNicXkyTrCUIWxZe5UlILwUA+6VZFCGWPFleZs8XddI0Dk+hKKDzL2gL+zkZc0w6UzA0bGTzDEn6w1sz8hWXR/JoqdwMA51beK2bZmOzLsONKyHJuP7na1me8l+HxIv6YAzCo2s55ojVCYQx08RD42fJfR44wOLF7AxtPh+o+9n9jZt4dTtl4uVWMbLzSQB21dEbFPHUWnR44UAxgZn7ua3xfTSz/Vyi6TRV3MGKKv/PSL9LQejsmCrARVX8c73y6lDDrgobmqcIgLtx0TRaGUm5Rc5i6BccmMyLZaT3tF15M8/1SeIcx0JBMvEtnePgcddYh3kod5bfolFLxSPT0BC8xIbTFrE/bXgxEYq/jli5N4FfrBXuFV4j5sBddfZOfu4Om5BZByUFXZ7ZxRdknzopH2z3Rl73I0DbHHJFZG32NkBq7BBfcCvrpDhhMJQ6N7p5pvNQtXmBWgjeYvX8RtMm/5evynE2+KddMNtgqhSmdl/Cd/fqiRwqccK5ruq1FIBsBE+LCKTR8V/+UCsAvR97X4g6xUQXrUVzFUR5wpkEgiCbORh3w6I2uvqRVJse075gb9L/Ykvm8LQgG6LRMwRZ/n+0xVs0POBZbhdNpVmCNCHqAxrQnzXersaClAM6WzblQTnymDAnEDaYsZMd9B8vxlNrjVuZpZyb5lSm+xncq1xLVK+iVokEXD+qcqbnpi/Zt3fQoH8+722Hdt8LWKTE54MyEWAEaZ5u/4GBZJuJs1SpDKBbMVZQXcuJhrtWhrIdQmzSwbBfmQvBQg+ihbBDd+Fp+5jYURAx8iNMvK4e7j5FmPAR5d5JBwOZqbKpiKm65h0ttj4EnOW5G2nqiczJXGUGYYxX8/vKExus3JbzMO/9BJj1qNxc/RyG6sswBuYR97BeBtT2/BvbEjbvseLYAlcd+sBS8 Pgm3edL1 aSg/ayrzaZuJsJvIsWmptcp31xqjsZtKOibolqPr32/eLjINBkFrRMajhfmFONWmtYs/FAkdZTsidg12aYNNJniUixFhckjhXp9O1+BZY0enkXpRzev+9F5NDXZvYxKBtrPyxNDjjvkY4fJ00u5detmtx3AyQ9InpeCvTJcYhjjx0fcZ8mXUNb35iGVRnZrE05MOxD/IGp5FSmx3BeJokD4CgjWWsdX25FzewIUIW1nvVCC61lv+rDqQ0fmoPSXxD2dfMYknjdE+dfNWQjdejdd05drQt/A91iMwl2I79SDKPxvMduxzIqF/HvdWopWaB1QYcKf0xdXiESKCzlT5INUrymuxLIzyR6fD+hnXQtDISq/y4ScGJ6LsMfZJVDkMM+HFPN1FSR5GZzi8Q59W8PiYc/IW/wQhDFbmfKY9z9i72a3mrrwFi4dR41G3mXREAXJwp4rekkux/wjBZJqZ1QetsyueY+7XW2qNykWaigc2AHUsDEBw1ekSoaQ8Xjf8QeNP5fA8S3RbEKfgFYq365QS0jaDsuX1aBtfL69LMF5X3lh9A/Tpp5p0mEKwsYvqIy3jFp+SP3ja90XrH9DkcoRchqvuzffi/3MQ4uFM8vJCNgjA6fWdJW21+mICTMpwup+e1JlgstlC7xCa5kgyeY/gNVgmmDExqPfwD1D2GjKq980OcdbrK6Ceznrrv5crs9dgalJfO9tAFLn4YxuWXOqS+4Y0c48CRwvZ47gAFzm9HbyU= 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 harder 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. Move vma locking and replace it with an assertion inside dup_anon_vma() to further clarify the locking pattern inside vma_merge(). Suggested-by: Linus Torvalds Suggested-by: Liam R. Howlett Signed-off-by: Suren Baghdasaryan --- mm/mmap.c | 29 ++++++++++++++++++----------- 1 file changed, 18 insertions(+), 11 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 850a39dee075..ae28d6f94c34 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); @@ -618,7 +608,7 @@ static inline int dup_anon_vma(struct vm_area_struct *dst, * anon pages imported. */ if (src->anon_vma && !dst->anon_vma) { - vma_start_write(dst); + vma_assert_write_locked(dst); dst->anon_vma = src->anon_vma; return anon_vma_clone(dst, src); } @@ -650,10 +640,12 @@ 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; remove_next = true; + vma_start_write(next); ret = dup_anon_vma(vma, next); if (ret) return ret; @@ -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); @@ -940,16 +934,21 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, if (!merge_prev && !merge_next) return NULL; /* Not mergeable. */ + if (prev) + vma_start_write(prev); + res = vma = prev; remove = remove2 = adjust = NULL; /* 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) @@ -957,6 +956,7 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, } } else if (merge_prev) { /* case 2 */ if (curr) { + vma_start_write(curr); err = dup_anon_vma(prev, curr); if (end == curr->vm_end) { /* case 7 */ remove = curr; @@ -966,6 +966,7 @@ struct vm_area_struct *vma_merge(struct vma_iterator *vmi, struct mm_struct *mm, } } } else { /* merge_next */ + vma_start_write(next); res = next; if (prev && addr < prev->vm_end) { /* case 4 */ vma_end = addr; @@ -983,6 +984,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); } @@ -2373,6 +2375,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 +3083,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);