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+3RcUkZCplg5qhS8y3Vp1crPkRADkFuIwuxvWQzw1v4BOofvYyZhRyUBYXxrinOFGT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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;