From patchwork Mon Dec 16 19:24:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13910249 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 64A06E77183 for ; Mon, 16 Dec 2024 19:25:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1DA8C6B00C8; Mon, 16 Dec 2024 14:25:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 1992F6B00C9; Mon, 16 Dec 2024 14:25:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F1D166B00CA; Mon, 16 Dec 2024 14:24:59 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id CCD016B00C8 for ; Mon, 16 Dec 2024 14:24:59 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 556701C7E3C for ; Mon, 16 Dec 2024 19:24:59 +0000 (UTC) X-FDA: 82901797818.17.2AE60BB Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) by imf25.hostedemail.com (Postfix) with ESMTP id 0635DA000D for ; Mon, 16 Dec 2024 19:24:35 +0000 (UTC) Authentication-Results: imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=3HyDeN3m; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf25.hostedemail.com: domain of 3h35gZwYKCEg241oxlqyyqvo.mywvsx47-wwu5kmu.y1q@flex--surenb.bounces.google.com designates 209.85.210.201 as permitted sender) smtp.mailfrom=3h35gZwYKCEg241oxlqyyqvo.mywvsx47-wwu5kmu.y1q@flex--surenb.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1734377083; 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=4HhxNlm5l3oc+a05Upn5SwGTABo4qTWEjrE4UxpsHII=; b=LA9OuAFpCd2xYL5Jf71qHF9h1zQvXa+teBXIWhV7kUUCgXqWHJvPH57H7WzB4t0+p7IEbn Q7Ha2kNxTTm/5mJ0BsKpIOQR2cc8eZ5BVU0usOFUcVdG5SefUeWPG6KV+kZyi1Mj/jd+60 OFXa2U0pY3T2tu+GjlhPdvvAlkwc7QA= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1734377083; a=rsa-sha256; cv=none; b=lgvVcXfTB9XUcTsjcQl60R6KWUIc1Ar+38wFco+PmXsDBUDprjjlCS4m6v7dUDwqTsqBME TgdXqQsdbElqBWRFy8CJMNQ1yprplGvqBvLlWuQF7TJezYtRdBBRY0v7aO0pdhr9JsVFfC F2/GWQN+bwO//Kob27Qr6XsZxuItrj8= ARC-Authentication-Results: i=1; imf25.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=3HyDeN3m; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf25.hostedemail.com: domain of 3h35gZwYKCEg241oxlqyyqvo.mywvsx47-wwu5kmu.y1q@flex--surenb.bounces.google.com designates 209.85.210.201 as permitted sender) smtp.mailfrom=3h35gZwYKCEg241oxlqyyqvo.mywvsx47-wwu5kmu.y1q@flex--surenb.bounces.google.com Received: by mail-pf1-f201.google.com with SMTP id d2e1a72fcca58-725e2413114so3422810b3a.1 for ; Mon, 16 Dec 2024 11:24:56 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1734377096; x=1734981896; darn=kvack.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=4HhxNlm5l3oc+a05Upn5SwGTABo4qTWEjrE4UxpsHII=; b=3HyDeN3mg0bDyGoONICe188+zK3XeFdf14rf8gquL7L7tL4qFia3YHNgfQ/ZYLU/BV qO0+K2MudsleykrLs9f6QgvtsJYOiWcJKe3u6FtD62ipgEppY3ByPXHKuY6ZSfBuI0/1 JYPBy5lJ3INXL/c21xEoDZayunbSgDMU4RnuStJiLN+t5ewMMFhBz4vK4VNbqty+HwXl zhi2N7REuwFpsMnShwtLLxnSwZqqE21/tBwcLJ71QgwExtTs04pDJtDGX8uU+ldpuYRs lPH7Zov0onuKzW5pwh3Ux/Xh9Sepo8BYc4Us4pH/xsoPCgedWXYkwSNjbo6IGjFt6UFb Ws7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1734377096; x=1734981896; 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=4HhxNlm5l3oc+a05Upn5SwGTABo4qTWEjrE4UxpsHII=; b=NhhNLVJFnvFbddEkrhoYACmK3KYATEzIfefw2B5VP/NPCh48F472EWlAEms3SDTaO0 kWoZraGG1F+o7kEFSg8wqvUPm7OvJY66KtWvetzjzimDMM4ecXk7++/2c2428xS+0uRs H4hzV0e1GPF1UaIw+tRifye0rIc2MhJMVGDnk5/KqIFfUoxX/MPVoMKm2h6UK8kRVcmt 4x13cPWBGfiuxBJEWCLi3NqsOmHxotblcKoq7C6ynmTF9thFkW0CQNA7XX2XmFQ3xuLg d3C1arP6BiQs6b7wVnDEctTBhQM6fpzQk0Ye6RZ/nW41NtMIgryDUQJyDNieRLVQQhjE 1reg== X-Forwarded-Encrypted: i=1; AJvYcCVjaBkrSM3ufXfW+GHWN7agyjuLwN0HAXSg5+l+T9uc8YlVaOgS340K7HF/8myTYEHWjreXM1OBKA==@kvack.org X-Gm-Message-State: AOJu0YxX+fbYVH40N20hH5dY/Ca5JC1xMOIThyz2b6OhcXwJ97uHEY3i FiCv9AKXm7QQt+Ljfpzla9uMvT3bx9XmBuknVIKpdf9HlCnhSIiG4kchsOqy3tfbutU3y6TxubR EuQ== X-Google-Smtp-Source: AGHT+IFCRcSRmnDxCEXNS618C+s8AbCbrJ3sqm2+B/Vq6ZMmO1in0EJV2noWuvjJbPv4qSgauLdf86FSJXo= X-Received: from pfbfb35.prod.google.com ([2002:a05:6a00:2da3:b0:728:b8e3:9934]) (user=surenb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:4b54:b0:729:49a:2db0 with SMTP id d2e1a72fcca58-7290c273bfemr20412405b3a.25.1734377095966; Mon, 16 Dec 2024 11:24:55 -0800 (PST) Date: Mon, 16 Dec 2024 11:24:19 -0800 In-Reply-To: <20241216192419.2970941-1-surenb@google.com> Mime-Version: 1.0 References: <20241216192419.2970941-1-surenb@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241216192419.2970941-17-surenb@google.com> Subject: [PATCH v6 16/16] docs/mm: document latest changes to vm_lock From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: peterz@infradead.org, willy@infradead.org, liam.howlett@oracle.com, lorenzo.stoakes@oracle.com, mhocko@suse.com, vbabka@suse.cz, hannes@cmpxchg.org, mjguzik@gmail.com, oliver.sang@intel.com, mgorman@techsingularity.net, david@redhat.com, peterx@redhat.com, oleg@redhat.com, dave@stgolabs.net, paulmck@kernel.org, brauner@kernel.org, dhowells@redhat.com, hdanton@sina.com, hughd@google.com, lokeshgidra@google.com, minchan@google.com, jannh@google.com, shakeel.butt@linux.dev, souravpanda@google.com, pasha.tatashin@soleen.com, klarasmodin@gmail.com, corbet@lwn.net, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, kernel-team@android.com, surenb@google.com X-Stat-Signature: qi8whxntz7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NaxbM2 gAZg1fJSdDGDThi4uAkCh+GxPhwHME0MQ7iG53Jyb7IkcFtiB/a2u5b5OkJCKuitoZlL+ZVVVTKV4bj5Kp34WGZzoeLg7mTlQyIhB07h6SAHWdYXA6YpLZ374cS0lH8pzjlAzkD4uuG2Qhp8GexkW86EFO9b7l0lsZW7odrRNmSxOOguz9J5QguslIYKfGb1mTnKAXVdHJyrS5x/gI6P43WX5GulAJL4XDo0iOlS1ChiRlD6wP84hc7JULeUt+yOxJuJG/pg/zoRl5GhL/luyw5BmflDOA0f5FAu6U47iq11CY8j0lN94PJKoyyAPLkhO2PtZjU8mrgf4EZEsTsePiYDgFDsHBsXtou0eb0Al3TUIHnaS9NC4wdbEin3QzW8k8MbDtsn5eNztz/FEZsfwzGzKw1s000ni2j3OqEWZV2mP9MQl++i3Sgojvduy0Ijrdfla5JUoo0AC5ROFDaPowxY6Zw1tHc5betAut/PQoNZrOUIlG6+lM5+AsySxU/6fls6NQSepBpwim02Xa86+ThFMxsvmus/eUWEMCaxGpC+UfAQjyPQufzm2xG8v7Os7mDs+0nf3j8uuM8BojpV17WDoUQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.127449, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: Change the documentation to reflect that vm_lock is integrated into vma and replaced with vm_refcnt. Document newly introduced vma_start_read_locked{_nested} functions. Signed-off-by: Suren Baghdasaryan --- Documentation/mm/process_addrs.rst | 44 ++++++++++++++++++------------ 1 file changed, 26 insertions(+), 18 deletions(-) diff --git a/Documentation/mm/process_addrs.rst b/Documentation/mm/process_addrs.rst index 81417fa2ed20..f573de936b5d 100644 --- a/Documentation/mm/process_addrs.rst +++ b/Documentation/mm/process_addrs.rst @@ -716,9 +716,14 @@ calls :c:func:`!rcu_read_lock` to ensure that the VMA is looked up in an RCU critical section, then attempts to VMA lock it via :c:func:`!vma_start_read`, before releasing the RCU lock via :c:func:`!rcu_read_unlock`. -VMA read locks hold the read lock on the :c:member:`!vma->vm_lock` semaphore for -their duration and the caller of :c:func:`!lock_vma_under_rcu` must release it -via :c:func:`!vma_end_read`. +In cases when the user already holds mmap read lock, :c:func:`!vma_start_read_locked` +and :c:func:`!vma_start_read_locked_nested` can be used. These functions do not +fail due to lock contention but the caller should still check their return values +in case they fail for other reasons. + +VMA read locks increment :c:member:`!vma.vm_refcnt` reference counter for their +duration and the caller of :c:func:`!lock_vma_under_rcu` must drop it via +:c:func:`!vma_end_read`. VMA **write** locks are acquired via :c:func:`!vma_start_write` in instances where a VMA is about to be modified, unlike :c:func:`!vma_start_read` the lock is always @@ -726,9 +731,9 @@ acquired. An mmap write lock **must** be held for the duration of the VMA write lock, releasing or downgrading the mmap write lock also releases the VMA write lock so there is no :c:func:`!vma_end_write` function. -Note that a semaphore write lock is not held across a VMA lock. Rather, a -sequence number is used for serialisation, and the write semaphore is only -acquired at the point of write lock to update this. +Note that when write-locking a VMA lock, the :c:member:`!vma.vm_refcnt` is temporarily +modified so that readers can detect the presense of a writer. The reference counter is +restored once the vma sequence number used for serialisation is updated. This ensures the semantics we require - VMA write locks provide exclusive write access to the VMA. @@ -738,7 +743,7 @@ Implementation details The VMA lock mechanism is designed to be a lightweight means of avoiding the use of the heavily contended mmap lock. It is implemented using a combination of a -read/write semaphore and sequence numbers belonging to the containing +reference counter and sequence numbers belonging to the containing :c:struct:`!struct mm_struct` and the VMA. Read locks are acquired via :c:func:`!vma_start_read`, which is an optimistic @@ -779,28 +784,31 @@ release of any VMA locks on its release makes sense, as you would never want to keep VMAs locked across entirely separate write operations. It also maintains correct lock ordering. -Each time a VMA read lock is acquired, we acquire a read lock on the -:c:member:`!vma->vm_lock` read/write semaphore and hold it, while checking that -the sequence count of the VMA does not match that of the mm. +Each time a VMA read lock is acquired, we increment :c:member:`!vma.vm_refcnt` +reference counter and check that the sequence count of the VMA does not match +that of the mm. -If it does, the read lock fails. If it does not, we hold the lock, excluding -writers, but permitting other readers, who will also obtain this lock under RCU. +If it does, the read lock fails and :c:member:`!vma.vm_refcnt` is dropped. +If it does not, we keep the reference counter raised, excluding writers, but +permitting other readers, who can also obtain this lock under RCU. Importantly, maple tree operations performed in :c:func:`!lock_vma_under_rcu` are also RCU safe, so the whole read lock operation is guaranteed to function correctly. -On the write side, we acquire a write lock on the :c:member:`!vma->vm_lock` -read/write semaphore, before setting the VMA's sequence number under this lock, -also simultaneously holding the mmap write lock. +On the write side, we set a bit in :c:member:`!vma.vm_refcnt` which can't be +modified by readers and wait for all readers to drop their reference count. +Once there are no readers, VMA's sequence number is set to match that of the +mm. During this entire operation mmap write lock is held. This way, if any read locks are in effect, :c:func:`!vma_start_write` will sleep until these are finished and mutual exclusion is achieved. -After setting the VMA's sequence number, the lock is released, avoiding -complexity with a long-term held write lock. +After setting the VMA's sequence number, the bit in :c:member:`!vma.vm_refcnt` +indicating a writer is cleared. From this point on, VMA's sequence number will +indicate VMA's write-locked state until mmap write lock is dropped or downgraded. -This clever combination of a read/write semaphore and sequence count allows for +This clever combination of a reference counter and sequence count allows for fast RCU-based per-VMA lock acquisition (especially on page fault, though utilised elsewhere) with minimal complexity around lock ordering.