From patchwork Tue Aug 1 22:07:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13337302 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 D6887C04A6A for ; Tue, 1 Aug 2023 22:07:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 46E462800FE; Tue, 1 Aug 2023 18:07:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4010F2800C8; Tue, 1 Aug 2023 18:07:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2980C2800FE; Tue, 1 Aug 2023 18:07:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 169142800C8 for ; Tue, 1 Aug 2023 18:07:39 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id AB0C18061C for ; Tue, 1 Aug 2023 22:07:38 +0000 (UTC) X-FDA: 81076923396.04.B02B498 Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf10.hostedemail.com (Postfix) with ESMTP id E35E2C0015 for ; Tue, 1 Aug 2023 22:07:36 +0000 (UTC) Authentication-Results: imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=q+9OzccP; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf10.hostedemail.com: domain of 3J4LJZAYKCH4uwtgpdiqqing.eqonkpwz-oomxcem.qti@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3J4LJZAYKCH4uwtgpdiqqing.eqonkpwz-oomxcem.qti@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690927656; 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: references:dkim-signature; bh=zPqUPKbdP+iIzl/rUw5yUEFwwtlQE8s7qBJDuJeMKGo=; b=fo61eN+ITk0gZ24GRn9vaaoY8pVe7jY8QqYZE2sVdYH/NuLD8UqOdGRlatIXU/0+D7exqN dJHnMHVodo1W+q1g5z9qHsxl1koBP0+Fh2EXN1MS57rKDMSkjAL5zkOv8/ONuSUZFWTxJ6 AEnnXI9Gnij/R1xJ3WIvqFIIS/B4cec= ARC-Authentication-Results: i=1; imf10.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=q+9OzccP; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf10.hostedemail.com: domain of 3J4LJZAYKCH4uwtgpdiqqing.eqonkpwz-oomxcem.qti@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=3J4LJZAYKCH4uwtgpdiqqing.eqonkpwz-oomxcem.qti@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690927656; a=rsa-sha256; cv=none; b=mUzam7Y1uAY23NI78xxYNTo17C8dhdXnssPiL01BmxpRB660jShT0YjlIXawFkCadzpzgV F4+vbwXz2z9nij9octSieAbo2lq10qX4O74WwvfxArXxt+m+mGcFixg3sgVCvXFUef1p89 sumnJml/fSi3zprgdyDBVDVwG+JJMDg= Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-d0d27cd9db9so522695276.0 for ; Tue, 01 Aug 2023 15:07:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1690927656; x=1691532456; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=zPqUPKbdP+iIzl/rUw5yUEFwwtlQE8s7qBJDuJeMKGo=; b=q+9OzccPgu9rBNj9tTxxF496DJEUfuPzUq4NJySwfPgbdDOoCMU555Hl/qdgpDWovQ 7P3zZ2+VclqiHfT8jA4l3nOEOoF0zeP3xfx+eBZo8NuKWI2ZlGst5RVijFL/SYmXNX/c EBGSWByJHdwURfZYTxEjjL+Gk5CcvbvFODRPLV4eb+3Hdcu6jAz1S/kBv/TmRram3Lho 5D6Ft49iUVjSDYwIZScsTg7jzmD27EWtWIFAjuv16Hq0nXtyiO4YzEP8YH3AjuH1xlwu TugHF9nnoRBAvRJEC95ny2FaSbtI6beld/hDOO7xdUTap77spYX6TCXHiNvth+9pWjuR OyXw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690927656; x=1691532456; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=zPqUPKbdP+iIzl/rUw5yUEFwwtlQE8s7qBJDuJeMKGo=; b=FGHmPF95ejwDzfPEA+bDbDUl8sNvdWdKeffB1zR2TE7+HgOyDrFAWZTtkc9m4/7IcB /sd1JNCieVuu0IsbrQK8STi9UAm7UFRGOcJsLKFlw4812eAsCaB3hGbFLzcSXaMP93io Z9ClfQknPVfupo4+2BzmrDXCgiBGHfFKBex/J+5AqeTlEjqBQG3dS5eAR6Q3Pyn1wcXl D5+TIsE/ZxTLgMzIU5/EwND1cZjAVPLmc53fxZ+dX4TcV5sBvGsHJVDRhyYYpmiIEAjq VIlxyCgkUdIo1BsQvWIzXEMLl2zMZoOpO/5xi0vwrwSg0CrzKrwREzlp8LekdUYEgXiU dgIQ== X-Gm-Message-State: ABy/qLbEdwB4Qh+JzAGwPHx7bHDeyH+TPmIDsrT+aCSA5Y437CbFZaF4 LlfWTYf2P9O1C+xK0VbMMUr9H+3p/e4= X-Google-Smtp-Source: APBJJlH688tL11JqgwaFAglSA8dGEv9/GHDNslVjpwq0rCCbRaezorHBoZHbvJTwlN1g7j/8quBF3BFvjSU= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:211c:a2ff:f17b:c5e9]) (user=surenb job=sendgmr) by 2002:a05:6902:11cb:b0:d16:7ccc:b406 with SMTP id n11-20020a05690211cb00b00d167cccb406mr160911ybu.5.1690927655996; Tue, 01 Aug 2023 15:07:35 -0700 (PDT) Date: Tue, 1 Aug 2023 15:07:26 -0700 Mime-Version: 1.0 X-Mailer: git-send-email 2.41.0.585.gd2178a4bd4-goog Message-ID: <20230801220733.1987762-1-surenb@google.com> Subject: [PATCH v2 0/6] make vma locking more obvious From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: torvalds@linux-foundation.org, jannh@google.com, willy@infradead.org, liam.howlett@oracle.com, david@redhat.com, peterx@redhat.com, ldufour@linux.ibm.com, vbabka@suse.cz, michel@lespinasse.org, jglisse@google.com, mhocko@suse.com, hannes@cmpxchg.org, dave@stgolabs.net, hughd@google.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, stable@vger.kernel.org, Suren Baghdasaryan X-Rspamd-Queue-Id: E35E2C0015 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: fzhm836d7oq9tfiuxofmdseu5g69bnjq X-HE-Tag: 1690927656-925627 X-HE-Meta: U2FsdGVkX18nDQdnxgyHKvwmhVGDpjmy3EP484lKMgqmVeylaLSwBwwlZOoWrxdRajoiDGJnT+TRvpxJ85z3GVkRVS2N2QMSf2LYGeyApyUpC0DGA6Fbzd7VCUzeZCCSs6oKhOL1vMX7NE2m0GesKSSjzlct8u7pu9GWD5nrKfRlkZtvLkBCZrq+n/uLUjxQpsLW0gZeFZ9whFy/nwnpnGOz71iqdLayxi2ar+zwsr77S813y9fdiiNfCpLcY/P8PtZvdXLAtKvsgn4yGYgSpuoyZPO9GiwupjNn9/+EOw2/nQvGWBwMAGKzo1UoVoy5HPNG6/6y7WyT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bkjRkdXdOo5WJSzlFfztjNpctJsCBZfQT0eD3sxnc8/XBipTwwfR0J9cjJxiNaWkumRB3tnVmPdqsF0b74hZWJ87cAEK9qRyg9A7eatz2ZWPiDNdD0uC+7nsf5YjduKaqE1EnNreOaaBppEnCb99V7tAGeuhOAP8pRebmkn4mi1/ZcqIYqzQwrvgEv/A== 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: During recent vma locking patch reviews Linus and Jann Horn noted a number of issues with vma locking and suggested improvements: 1. walk_page_range() does not have ability to write-lock a vma during the walk when it's done under mmap_write_lock. For example s390_reset_cmma(). 2. Vma locking is hidden inside vm_flags modifiers and is hard to follow. Suggestion is to change vm_flags_reset{_once} to assert that vma is write-locked and require an explicit locking. 3. Same issue with vma_prepare() hiding vma locking. 4. In userfaultfd vm_flags are modified after vma->vm_userfaultfd_ctx and page faults can operate on a context while it's changed. 5. do_brk_flags() and __install_special_mapping() not locking a newly created vma before adding it into the mm. While not strictly a problem, this is fragile if vma is modified after insertion, as in the mmap_region() case which was recently fixed. Suggestion is to always lock a new vma before inserting it and making it visible to page faults. 6. vma_assert_write_locked() for CONFIG_PER_VMA_LOCK=n would benefit from being mmap_assert_write_locked() instead of no-op and then any place which operates on a vma and calls mmap_assert_write_locked() can be converted into vma_assert_write_locked(). I CC'ed stable only on the first patch because others are cleanups and the bug in userfaultfd does not affect stable (lock_vma_under_rcu prevents uffds from being handled under vma lock protection). However I would be happy if the whole series is merged into stable 6.4 since it makes vma locking more maintainable. The patches apply cleanly over Linus' ToT and will conflict when applied over mm-unstable due to missing [1]. The conflict can be easily resolved by ignoring conflicting deletions but probably simpler to take [1] into mm-unstable and avoid later conflict. [1] commit 6c21e066f925 ("mm/mempolicy: Take VMA lock before replacing policy") Changes since v1: - replace walk_page_range() parameter with mm_walk_ops.walk_lock, per Linus - introduced page_walk_lock enum to allow different locking modes during a walk, per Linus - added Liam's Reviewed-by Suren Baghdasaryan (6): mm: enable page walking API to lock vmas during the walk mm: for !CONFIG_PER_VMA_LOCK equate write lock assertion for vma and mmap mm: replace mmap with vma write lock assertions when operating on a vma mm: lock vma explicitly before doing vm_flags_reset and vm_flags_reset_once mm: always lock new vma before inserting into vma tree mm: move vma locking out of vma_prepare arch/powerpc/kvm/book3s_hv_uvmem.c | 1 + arch/powerpc/mm/book3s64/subpage_prot.c | 1 + arch/riscv/mm/pageattr.c | 1 + arch/s390/mm/gmap.c | 5 ++++ drivers/infiniband/hw/hfi1/file_ops.c | 1 + fs/proc/task_mmu.c | 5 ++++ fs/userfaultfd.c | 6 +++++ include/linux/mm.h | 13 ++++++--- include/linux/pagewalk.h | 11 ++++++++ mm/damon/vaddr.c | 2 ++ mm/hmm.c | 1 + mm/hugetlb.c | 2 +- mm/khugepaged.c | 5 ++-- mm/ksm.c | 25 ++++++++++------- mm/madvise.c | 8 +++--- mm/memcontrol.c | 2 ++ mm/memory-failure.c | 1 + mm/memory.c | 2 +- mm/mempolicy.c | 22 +++++++++------ mm/migrate_device.c | 1 + mm/mincore.c | 1 + mm/mlock.c | 4 ++- mm/mmap.c | 29 +++++++++++++------- mm/mprotect.c | 2 ++ mm/pagewalk.c | 36 ++++++++++++++++++++++--- mm/vmscan.c | 1 + 26 files changed, 146 insertions(+), 42 deletions(-)