From patchwork Mon Jul 31 17:12: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: 13335314 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 C361DC001DE for ; Mon, 31 Jul 2023 17:12:40 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 375D1280082; Mon, 31 Jul 2023 13:12:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3249F28007A; Mon, 31 Jul 2023 13:12:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 21D99280082; Mon, 31 Jul 2023 13:12:40 -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 10FB428007A for ; Mon, 31 Jul 2023 13:12:40 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id D22CEB20BE for ; Mon, 31 Jul 2023 17:12:39 +0000 (UTC) X-FDA: 81072551238.14.9DAEF77 Received: from mail-yb1-f202.google.com (mail-yb1-f202.google.com [209.85.219.202]) by imf16.hostedemail.com (Postfix) with ESMTP id F28B718001E for ; Mon, 31 Jul 2023 17:12:37 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=6FV+nnfj; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 3hOvHZAYKCKcZbYLUINVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3hOvHZAYKCKcZbYLUINVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1690823558; 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=c3PYDZ1RN/B3pu1J25lpMbrKPoFg0ktyB6SwecJ4DzU=; b=zcK/jf14/+OvM97/nTyBw6EtTClO0/nMUU0oXzCfwZvXz6P460mAlOkSCi30ssew064lTM hpbkC6FW0owdosBljr3ZqoqtnVUiJRywN/j41YhGs+72Q6ppGkrGIw0DIETFecHvtfi73h jKZ1cRKCqyeqGSI+4b9FNA+BXTbLpMA= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=6FV+nnfj; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf16.hostedemail.com: domain of 3hOvHZAYKCKcZbYLUINVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--surenb.bounces.google.com designates 209.85.219.202 as permitted sender) smtp.mailfrom=3hOvHZAYKCKcZbYLUINVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--surenb.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1690823558; a=rsa-sha256; cv=none; b=i51whOPZ4+0jjqucAizeh8ii1pSDblev2aiGcYR+k9UV4B2UFQo9Jj/LRR6WpfjnKHEgCw 1TPRULPzi3B2JXdYOUtcl9BmNjlaGwKz4sBPXo5YqehiXYqwZOfdOIYRSeIyLFWG0K53XF Hq1aoK53llFMUhFonvHlN5VcAIzcGgQ= Received: by mail-yb1-f202.google.com with SMTP id 3f1490d57ef6-d1efa597303so9846522276.0 for ; Mon, 31 Jul 2023 10:12:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1690823557; x=1691428357; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=c3PYDZ1RN/B3pu1J25lpMbrKPoFg0ktyB6SwecJ4DzU=; b=6FV+nnfj59v0O8qa3GSfbEYQhq8YmdgJWLU8iPRuV+6j61UppVaAn5rOGOl+8YHMdt 5zb59tU4zOXjjNhOid4WniiNCPgSltUe6NgoO/pX6+qFqRqYlePiUlahDQXFWxHbwf7U 5JRbk/t2axk1kdap2kYPs2NLmGWi6QwrywA09T4kw5cif/b+/jnjeTHz4ALKCCdCRMun OfBd07Zdyr5thNsiyTKJzJGNXUd12BPg0pSFSz+ZdMgFAejegH2kK9khBYiJ5O2FuNPa X7b6E6HKN6OD8odj1FJxZrLHyXxOw3Yun7LkiSKkR3ofC+PTL6zZ8gYhvyXgVUdmB4tx AR0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1690823557; x=1691428357; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=c3PYDZ1RN/B3pu1J25lpMbrKPoFg0ktyB6SwecJ4DzU=; b=MWKaTxvuO2okAXcFXzLET5f+TB0cMPKoOA0lbIGVI107EOMwkII1oLjdbtYi30+xTQ J2SgQlH+muXzAIPO95ju/6LxFZrpv2GZPctWwjgvezvosfrFEX1Kcu5iHg3FjNhradN4 XLyKC/PqRqqrqjQ49K6cVjsiNccN1qzj/hkGimRUn9rFSqlT+k66Hbk1fZ1c7/tk8U/t KauH9KjT7fVrNDfYeO9ZuasHS8cKEFvzlLMDZAkzLO45oreuLYwHbfCJrlsbfZ9CKT8p XCJjLb0dpn7unmOK2z21spOG0lnknFVWEs9r7lo3zcWkEze1WPR9wVvtjrLW1Ws0YDzM tDFA== X-Gm-Message-State: ABy/qLam16aaQRpwvG5a0wynNOPAIsNe0H/uKZ+/IzciY30WrKQ8HbLY MhROJ+HDexx/ZOlVmVAbgpPAyf4skR4= X-Google-Smtp-Source: APBJJlEKfgHW3Pt+D+HW+X08sGkNnzE7rrwZeKVkKbn6HinHN7XInD85rNYlUT2BXDYNkx/+RNlDUje3uTI= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:d4d3:7e41:cb80:21ea]) (user=surenb job=sendgmr) by 2002:a25:305:0:b0:d0c:44d5:4530 with SMTP id 5-20020a250305000000b00d0c44d54530mr115944ybd.3.1690823556992; Mon, 31 Jul 2023 10:12:36 -0700 (PDT) Date: Mon, 31 Jul 2023 10:12:26 -0700 Mime-Version: 1.0 X-Mailer: git-send-email 2.41.0.487.g6d72f3e995-goog Message-ID: <20230731171233.1098105-1-surenb@google.com> Subject: [PATCH 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: F28B718001E X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: gsffoo1jfbr4jxoez5uyrcid73pjaoc7 X-HE-Tag: 1690823557-788589 X-HE-Meta: U2FsdGVkX1+vbN0azLQGGJHqSTKzQnSO8wzZjPr6W+vara0BIhwlEX6nLUZOW8I0L0jnPvltXWa4Sd++Ud69IbVwFjgJmllQ152i7QkyfxfMtE+xfutkLBOaq0L5sHfI3elYpWfBnRRa8mk05K9P3KiRELdULmi6L9nm6YtVtm0kZAwW/dZFk9fE73fbLETzPywF9ZekdPOPcg7HbeHQSLbdaVQ7caA/wS7ZF5uPo/xg8LLwUZk7ttu4pLE08lR7TUar51PvT31kyBo6anQkl2QYu+syuXlrfEJSUGJesVclSXM5SNBItwYU2sZ5fm9gSz6zSb8OH2MjE/bi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6ur0iNdOLfCb1valgfsqXflIVpdLWsGFo0nk8aV1iYCgzg5nSZ7zcOZy95h09z3Ini3eSfMGxCTpb01y06W6C36YrnksxUk0l+yA6HvtGyD3K2FP/6JdxixeORHpuqsoDGdHaUMITdSo+SHG54YAPatVFvSskiVua8ys7Xyf1XCbezKvfSx0ByLIdHa7E7g== 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") 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 | 2 +- arch/riscv/mm/pageattr.c | 4 ++-- arch/s390/mm/gmap.c | 10 ++++----- drivers/infiniband/hw/hfi1/file_ops.c | 1 + fs/proc/task_mmu.c | 10 ++++----- fs/userfaultfd.c | 6 +++++ include/linux/mm.h | 13 +++++++---- include/linux/pagewalk.h | 6 ++--- mm/damon/vaddr.c | 4 ++-- mm/hmm.c | 2 +- mm/hugetlb.c | 2 +- mm/khugepaged.c | 5 +++-- mm/ksm.c | 16 +++++++------- mm/madvise.c | 13 +++++------ mm/memcontrol.c | 4 ++-- mm/memory-failure.c | 2 +- mm/memory.c | 2 +- mm/mempolicy.c | 12 ++++------ mm/migrate_device.c | 2 +- mm/mincore.c | 2 +- mm/mlock.c | 5 +++-- mm/mmap.c | 29 ++++++++++++++++--------- mm/mprotect.c | 3 ++- mm/pagewalk.c | 13 ++++++++--- mm/vmscan.c | 3 ++- 26 files changed, 100 insertions(+), 72 deletions(-)