From patchwork Fri Aug 4 15:27:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13342031 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 54193C001DB for ; Fri, 4 Aug 2023 15:27:33 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 6C0328D0008; Fri, 4 Aug 2023 11:27:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 670ED8D0006; Fri, 4 Aug 2023 11:27:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 539458D0008; Fri, 4 Aug 2023 11:27:32 -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 46D048D0006 for ; Fri, 4 Aug 2023 11:27:32 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 0562C140983 for ; Fri, 4 Aug 2023 15:27:31 +0000 (UTC) X-FDA: 81086801544.28.6EBE57F Received: from mail-yb1-f201.google.com (mail-yb1-f201.google.com [209.85.219.201]) by imf21.hostedemail.com (Postfix) with ESMTP id 368141C001F for ; Fri, 4 Aug 2023 15:27:28 +0000 (UTC) Authentication-Results: imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=rl5bqp6t; spf=pass (imf21.hostedemail.com: domain of 34BjNZAYKCHMjliVeSXffXcV.TfdcZelo-ddbmRTb.fiX@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=34BjNZAYKCHMjliVeSXffXcV.TfdcZelo-ddbmRTb.fiX@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=1691162849; 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=aKhfr50CytyPbKf+4uDwSSo7M3ThccfnnQRTLUrmmdg=; b=Ejoup+k6DvAPRNk07BxFgDuT3xzMcVhLqxs1Flkp7+BO5hLPaIGJ9do4/H/6zhjPau48fH 817HGa5bkYd4tswb7TU/AglDyLGkV8OXaeeeqwPWEMTa8ohJvYxqrNtwg0ZhtYbuDWMCmI YVYIGrXFReNm0QyqJnvTSMy2wndHNhA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691162849; a=rsa-sha256; cv=none; b=rHJRBLQSfgZIKuM0uM8GG/YY9ZqxlS4Uav0ENPTAPNVieBoVOLbelgfjuzyYcB5eZr6bf2 AqWvaEOYckAlXhzucuLSpbWuS/wSpvb0UdDsn9scChi4HB947vT+U18o28pIlRut91HkIC fyt9zlOvfor6IOzYxVrME7RM+jTF3ig= ARC-Authentication-Results: i=1; imf21.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=rl5bqp6t; spf=pass (imf21.hostedemail.com: domain of 34BjNZAYKCHMjliVeSXffXcV.TfdcZelo-ddbmRTb.fiX@flex--surenb.bounces.google.com designates 209.85.219.201 as permitted sender) smtp.mailfrom=34BjNZAYKCHMjliVeSXffXcV.TfdcZelo-ddbmRTb.fiX@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-yb1-f201.google.com with SMTP id 3f1490d57ef6-c6dd0e46a52so2347978276.2 for ; Fri, 04 Aug 2023 08:27:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691162848; x=1691767648; h=cc:to:from:subject:message-id:mime-version:date:from:to:cc:subject :date:message-id:reply-to; bh=aKhfr50CytyPbKf+4uDwSSo7M3ThccfnnQRTLUrmmdg=; b=rl5bqp6tQA+TvGmYH1sXrLGzVDzye+UxvtNuwcEIDmzJGGlknWXhoFyPeEQGbc4XD+ 7fM9lJOnJSiZSATLTJv3lekc5ku8HZo/nf4jfPtN3Fe5WBitNR8zGCbswnEaYIUzD+7O Un4sHHA5u0qnfGbDg3ILOXQ9MXQFmEoBp9lKyPtUgd0i3DALjgoR4XmoewAydqS8KwlJ iTaE4JCJ61Pyd3AwuTgsGPqPMPxOvJvOeVLYm6+ZtoMhi2WJkTS6sYe7btl4ERrgaLlb 2a/NnBQcYIpOtEOaDknfe4XqbeBloJxtgUbI2ZhlK9wThhAOg4P/sTKROcpLCrp+NoCv kLQg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691162848; x=1691767648; h=cc:to:from:subject:message-id:mime-version:date:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=aKhfr50CytyPbKf+4uDwSSo7M3ThccfnnQRTLUrmmdg=; b=Vqn4XwjsQ7ZIrNTyUW+7B/Li1+9VGujri0ArSA+hZXOJwL74ZEK7M6S9VZBQlQ/3hU cJQ5iKn+2AVhVAQCFp3GwcHi/Owo1jbfiHRo42AC6yQDXWmozWmEhP/X1ByGsKP+oddq 6BUQHpfVJrD03vMtOdSgZLfJfXNf+Q5PUEsudaEx6/0M8GBJo9YPedNbqwTrJE9BpRgt s1OKsjDTw6h5JItmtNA4cWEavQ1y6+m7fhvrFs+kKZJL3959BeK4m6ooACPoV1f1Jq0d sbJ+7buaXzWP6LXLPKhXzCGhMpFYQXyIyYZZhRAqvDMyKFm8hPrTjPVnFvF5WlEfnqmS qMRA== X-Gm-Message-State: AOJu0YwmxpMHFAICIOrXbT4R/FMfwAk6IQqg/XjUGsyPLYxBUMCQk7s1 c6hB4X/YufN1k+WiwSYzy0V0Jq4nIak= X-Google-Smtp-Source: AGHT+IG4f9A7dcMT8UcSpgTJBpsi+cGM1Bu1NXzOWKN5ONFOXPvmLmwOt9qFc68A9Gb/dy1voP9Rr8Da5Cw= X-Received: from surenb-desktop.mtv.corp.google.com ([2620:15c:211:201:43a7:a50f:b0fd:a068]) (user=surenb job=sendgmr) by 2002:a5b:94d:0:b0:d09:17f2:d3b0 with SMTP id x13-20020a5b094d000000b00d0917f2d3b0mr12917ybq.9.1691162848092; Fri, 04 Aug 2023 08:27:28 -0700 (PDT) Date: Fri, 4 Aug 2023 08:27:18 -0700 Mime-Version: 1.0 X-Mailer: git-send-email 2.41.0.585.gd2178a4bd4-goog Message-ID: <20230804152724.3090321-1-surenb@google.com> Subject: [PATCH v4 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, kernel-team@android.com, Suren Baghdasaryan X-Rspamd-Queue-Id: 368141C001F X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 8o84c8wkftk75581m5i6zdf59d1hmwqx X-HE-Tag: 1691162848-578657 X-HE-Meta: U2FsdGVkX19zyq/+YXjLaz2TlQh7jfZ5/AwHNg7m200h+gQCzQUeePEZM3RF1OirftapAcTB9mlEp8wwhLdZYw07b9yEu7B9MM/+hRTYpG8xyNDfg0b0XGDrm8VU0Hrx+BD5WL/TslaEhJ8dKqWqO+XY7UBoidGFI5XsMoYuCx8ByJKE/hkNdMoQ89SE/68sFRAyTv5OjnA4MlqXtRDT3QwL7Kz2R6qDv44LjGsujNc1OKFeHVJRDfYILxBgnceCr0Vj2Kk+3MWBs+LXK0eM9RbP+2yG7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hu9zTWKWlS3SmG5XO29qkqXHz5OyFScesbCwkvpq/zSGLHHt6hwQDMxvdvB/qnK/vCcGVIJcxxYTARNQGlNGNh8maQhTynLFhOtJlwUUOfeuTYMWa0ogMW6XT/tXkaJj4KAN4xbaLiWAezw1bUA+d8Fnf6wYDUNHI2599KNthrKMGORD9xxcmmKDGxvZR3FvL/d2foWyWgjvYjjQwSH+4Fk0lUbcKP5BO75EEg== 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 v3: - changed vma locking in vma_merge to avoid locking prev when not necessary, per Liam 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 | 33 +++++++++++++++-------- mm/mprotect.c | 2 ++ mm/pagewalk.c | 36 ++++++++++++++++++++++--- mm/vmscan.c | 1 + 25 files changed, 148 insertions(+), 43 deletions(-)