From patchwork Thu Aug 3 17:26:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13340367 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 8297FC04A6A for ; Thu, 3 Aug 2023 17:26:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 12FAD28027F; Thu, 3 Aug 2023 13:26:59 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 0E05128022C; Thu, 3 Aug 2023 13:26:59 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id EC2B828027F; Thu, 3 Aug 2023 13:26:58 -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 D8FC928022C for ; Thu, 3 Aug 2023 13:26:58 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 89F1141329 for ; Thu, 3 Aug 2023 17:26:58 +0000 (UTC) X-FDA: 81083473716.07.C29C044 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf07.hostedemail.com (Postfix) with ESMTP id BB45740009 for ; Thu, 3 Aug 2023 17:26:56 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=cPa0RPAC; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of 3X-PLZAYKCIIy0xkthmuumrk.iusrot03-ssq1giq.uxm@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3X-PLZAYKCIIy0xkthmuumrk.iusrot03-ssq1giq.uxm@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691083616; a=rsa-sha256; cv=none; b=LRE0XH2Pge6TJQvMNrLbZFjzXiWcgtli7xZeRT6OAXPMXki6rpHSzR/k/6YdWtaVN63Pxe YlbZ4CMtQ5jbwLXjqD2pQ7l5Lu8lrIvjpDram/S2JVJOuljb/E9f6Yr6SiEi+ANHIwY+5D c7NNzn28LDKcc8bjOW/43zKHl9LKNIU= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=cPa0RPAC; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of 3X-PLZAYKCIIy0xkthmuumrk.iusrot03-ssq1giq.uxm@flex--surenb.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3X-PLZAYKCIIy0xkthmuumrk.iusrot03-ssq1giq.uxm@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1691083616; 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=cZ2Sq6VO6+Afhr0Ggao6dyfVObiTvoIor2VOxBVDNUQ=; b=nsyx570oJFUuM0Rhd6+pANQDqDpMW8A4Ai7YDOVJ2o6BVT0MwFtDhyY2ee3Kx8rC0rGpvW X9RdK56+UGk7CIGcDzmxaihwMCZFjEuqAxPo0AmPpmqdnKKvUGM4wC16z3kVbusbxHbyZC GPGFw7sKw2auZyDBbuVVe9901tdjZ2g= Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-5844a99c268so13508907b3.2 for ; Thu, 03 Aug 2023 10:26:56 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691083615; x=1691688415; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=cZ2Sq6VO6+Afhr0Ggao6dyfVObiTvoIor2VOxBVDNUQ=; b=cPa0RPACNVX2ZeSvOhETX/ApTMwoNjLDFtBMJdLBKbqOKgAAEKVJMkmX1+OQ7m8oiI nRP9dxXeihwf7k+zwxVZYSLnzRl/3JxkUUNRlD6Gvn/x7J2A7e3AY9bMxKGHDUnYEq5N o8EuuDVdrIS8TSLSQiSFDmCQ6u+OXs1RjNjBxv66OvBImpxNx75I6pIHzHRxbqE1jHx+ MzseOT8F/Kd8sNYb6fBF3zFP8F62hl44NTK/dGbAFtvvwpjb87wyJa64+nJnDfV6zKYS +cd/NndJD0wTB7neqWgh5qF6tawh9ZhwnDWDlfOBAjPlMXZYz+dn+b76zDzzI/KCYrnF 86Tw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691083615; x=1691688415; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=cZ2Sq6VO6+Afhr0Ggao6dyfVObiTvoIor2VOxBVDNUQ=; b=JSm5NNiAqdV2z5dGwKTaDE55uj5ti0EHKbf8N2Etxd22oX4z1Ul/JR/KUYrKslur02 yROilZLPmNyWObDsX8nwcoJ2RueDsoveT68EyUkrYti4JkhvliIAV7YKEPFUytFPHycp 4yw+XzXoo3C8X8bZOA3E1wN96oWjBCpU8o7iOApFj47C/uQq2ssOUP1vr1XUPRQjxyks 3VGMm3fAFPG5ljpSSkDufVe96OmL0GLhF+Tz6P1hknzsH5Chz/y7C1Ak3GzuosK734Ng /6g3MN4okEXys4eQEIMaFcrOsijGH06nFjlMUw4DtLtVq62uz3qpdBSZ/J7gmrgsFNtY XCwQ== X-Gm-Message-State: ABy/qLYzhMb1cN8bl1jPTkGTdninJ0iLikeyOY20vWK8axvjZpBMk2yk zsWelHxkc6r1hsBX0AoA+drVDJ2QklU= X-Google-Smtp-Source: APBJJlHkaDlFGikr7jzCQXoOIxsLN4xYUczw/VwZSwj+TSdWsNmAHDKAVdQ/qIeXeWYgtRctJkUUMzW4e8E= 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:100f:b0:cf9:3564:33cc with SMTP id w15-20020a056902100f00b00cf9356433ccmr181148ybt.13.1691083615709; Thu, 03 Aug 2023 10:26:55 -0700 (PDT) Date: Thu, 3 Aug 2023 10:26:45 -0700 Mime-Version: 1.0 X-Mailer: git-send-email 2.41.0.585.gd2178a4bd4-goog Message-ID: <20230803172652.2849981-1-surenb@google.com> Subject: [PATCH v3 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-Rspam-User: X-Rspamd-Server: rspam06 X-Rspamd-Queue-Id: BB45740009 X-Stat-Signature: 1e8qsq4ny1idqca4nmbhewmwp1z4knbr X-HE-Tag: 1691083616-896633 X-HE-Meta: U2FsdGVkX1+nKCDUdd+wVv9c5BL/K5XTG5Txsh5goJaN5AdhatHEqjRA2r4GV6M41lqLpDWVE98oDCI5Tn6FW2anfxMbh0qpJ2+mrpezrCYxWlHb3KRzx478T0V78niiumjvi8K1ADu1Ta/cwfAPLwWPbwixqQUgiEnG/mTaMyV1r4TxEi+SoSnIhCcLAnvhoUVbER83RcGI586rnlkaUyZ5o06O8/qy0+zeIDsjV7OhRx/Vkg7puG8tBQeWrw1pgRecUaIKWdIN+094wva6nBkTV3WDKkI30ha/XwZYCJZMpYcOyZWa0t3LE3KKw0tRo3F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PVkvR1r6Ggrg+2GRocD/PFIc836wFHRtp5dKgDcDl7xPNfRTPO8XSxmOI3ty3qONylnSvXCZACbtIpfLP2mjml2kWQUp8+qJoAfS6BxHpMdAvq7rGlhlJltFLzkBJFQUIH3g9kdUV9+Wh9srU54qOoCiwqd1SmHdrOF1dz/UGAttt/BkU6sVYMf/qXBwGbvw== 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 v2: - removed vma locking from hfi1_file_mmap(), per Linus - moved vma locking out of dup_anon_vma(), per Liam - 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 and dup_anon_vma 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 ++++ 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 | 32 ++++++++++++++-------- mm/mprotect.c | 2 ++ mm/pagewalk.c | 36 ++++++++++++++++++++++--- mm/vmscan.c | 1 + 25 files changed, 147 insertions(+), 43 deletions(-)