From patchwork Thu Jan 9 02:30:25 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Suren Baghdasaryan X-Patchwork-Id: 13931823 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 D8883E77199 for ; Thu, 9 Jan 2025 02:31:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A92846B00AA; Wed, 8 Jan 2025 21:31:06 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A19096B00AB; Wed, 8 Jan 2025 21:31:06 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 7F4466B00AC; Wed, 8 Jan 2025 21:31:06 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 569DD6B00AA for ; Wed, 8 Jan 2025 21:31:06 -0500 (EST) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 100B31C83DD for ; Thu, 9 Jan 2025 02:31:06 +0000 (UTC) X-FDA: 82986336132.03.19242E9 Received: from mail-pl1-f202.google.com (mail-pl1-f202.google.com [209.85.214.202]) by imf05.hostedemail.com (Postfix) with ESMTP id 2E86110000C for ; Thu, 9 Jan 2025 02:31:04 +0000 (UTC) Authentication-Results: imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=apac9xdp; spf=pass (imf05.hostedemail.com: domain of 35jR_ZwYKCI8BDAx6uz77z4x.v75416DG-553Etv3.7Az@flex--surenb.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=35jR_ZwYKCI8BDAx6uz77z4x.v75416DG-553Etv3.7Az@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=1736389864; 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=jMJU/3gn80Bp5VROzRR3pKDE/HRHDjLKawYxTpkzfvE=; b=TMJb6VW1oIT/3zHhbF8UYlzInUYiJu2CcBvYZNKKPnyvs+9cuFMIfQ1aT4fuSkBSOU4tNo 0n1t+rp6K7Ex9jxBZ+PPg5zs46aZBqNiSRu7DSfVqivEZYdyvP6OVdqR9JqFJ8B3T/qFo9 SewCPYYNW/m+SnHQTa/RuuiFNlNBTe0= ARC-Authentication-Results: i=1; imf05.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=apac9xdp; spf=pass (imf05.hostedemail.com: domain of 35jR_ZwYKCI8BDAx6uz77z4x.v75416DG-553Etv3.7Az@flex--surenb.bounces.google.com designates 209.85.214.202 as permitted sender) smtp.mailfrom=35jR_ZwYKCI8BDAx6uz77z4x.v75416DG-553Etv3.7Az@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736389864; a=rsa-sha256; cv=none; b=L5gFHfV+C/AIR3sMeiJ3rk+Qv005P0i5d3c+Ljgc9Ya5i/FT1VtQkOj83ZYfEUAiZ3N11q db+9hBcDh1Mtd/0FCAMQPiALsxoboOuMTzGSP8V7ZmtFLeKPbtfWytXXrnWXenZIBzzjjv X5pdk7E+MkVhp4q5MLLeuYNsAaSIzCo= Received: by mail-pl1-f202.google.com with SMTP id d9443c01a7336-216430a88b0so7198385ad.0 for ; Wed, 08 Jan 2025 18:31:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736389863; x=1736994663; 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=jMJU/3gn80Bp5VROzRR3pKDE/HRHDjLKawYxTpkzfvE=; b=apac9xdp17dy68uAhIwDYlJPuLeSVM7rMkat458yxgK3ya+0fbONyRqX9E3hLvgfKE 1rkmk9RLZtdU7hixcVds9x3cXh9t0Ny4haclzXS3jFPDBLBf4FurLVfN8SR1C8cT4UBc jE8ul07h8ayBBoiKlYD187A/i0io7UX7M8vOthw8Y9ZWO7SDMIAC4uKdTRjHgsAdsZO4 +p6uabdy7tE2KApkVYc+Nat+IB1Q70j/eLyHv6+7o8yItQ7TYn+MdIaFSR7FQwouNYi5 KffUyaxilg/yps/KWAS9z6MDchWlKcvvUZDzXqVtiC1OkM1oo2IrhGorz9GGvb8cmUFL q0gA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736389863; x=1736994663; 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=jMJU/3gn80Bp5VROzRR3pKDE/HRHDjLKawYxTpkzfvE=; b=Bxvs0hrVcQcwmghO4JVI1NRgmmqEl074fEH1CSannUXdx6cnkE3Z4KBp6bswumoAFA GhnvOs7a4k17ygQMnLZcIn+/tfI5ApsyBbzEIq7oyYrSGe0D6Jp3KyRIHc2Wp1u2a4ub tu8ye4beXLtRl0fARJ2/zpVfzFeMy/H4FOM7vTUssKWDImLeJsLkMawunXXefQxHewlw UaoWjUjMw0tcnjXWSS1vF3ulSTAOugl2BE/ZLamYVBjRJDBpd1dx7c9sLIqvXQXC4ShW RZOVAO65vMs/MW7fc/J45x3Lxz+mxpBVZ7bXk084mSg5lZKFfuG9+p3Yq3ohDwd6fzGG Z/uA== X-Forwarded-Encrypted: i=1; AJvYcCU8jtjFs4JUubpMynJviw4v6VgJHtccbw1RLZmGtQfNze+ig5OgvvlX+9S2tPW62fSvtHgG1yDIQA==@kvack.org X-Gm-Message-State: AOJu0YyqNUp5ynREwr9V/Y92eWU7bjYVXORRl0ZEp08/mkACl6WzOi5I E3TYHUoWK735a7A3ci45d+hc8ZEC0JPJxGjmJ1S/+vUmTFSc1+SBPMGrwcFmwt33JAKQua0rpUm X3w== X-Google-Smtp-Source: AGHT+IGIq2BAYBw5AVU5HpNLwVIjRPdRRVq4DaaH9pXI9zGCBX20QcwARo1md41gddC/qz9xS8MYPY6uF8o= X-Received: from pgcz21.prod.google.com ([2002:a63:7e15:0:b0:7fd:50e9:aabf]) (user=surenb job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:f64f:b0:215:5240:bb3d with SMTP id d9443c01a7336-21a83fe4915mr80523945ad.42.1736389862977; Wed, 08 Jan 2025 18:31:02 -0800 (PST) Date: Wed, 8 Jan 2025 18:30:25 -0800 In-Reply-To: <20250109023025.2242447-1-surenb@google.com> Mime-Version: 1.0 References: <20250109023025.2242447-1-surenb@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250109023025.2242447-17-surenb@google.com> Subject: [PATCH v8 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, richard.weiyang@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, "Liam R. Howlett"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L/Huvd84BxrjCSZTSI8dz 3MFfW0W/ JHHQrAzthZq3LArvATyGgleEbTpV0F5xKoytPh3M3TafgTeKAmYtZWOzroZLar8+M83kYxGnuL/qKyr16i6BNucO9odPTbGB3FI2fJhHBM++AnNLFaLZo19p8HyLd5hzUb7Wf6VQ6vDpUCmfteS/4N+IU96PSFNS7xe1cJZAwXEDP9rV7f/irwFvaHjXVrsVfQdfSROD1dnv0e6ufruJEDur59aRbL1w34vV0S21+FSx9g/ARahvP3aB58ceT2sfrUeEF5fRUAqNYhal2douuRrSsjo9VThFhHzmHSSEqxM4Hw9KlDSK6Ba2CPP/4YQix8E+V482g3MAMhCm53HB94Jgrvdw7wN+etm89H4DJy1rLMHkAtuwVStHogE1ucbRKDGo+JLmbqOD0egnBWCGOrBGRz6Bo4gnWMFdN7iuL5MWU8+criSeJ8YmaQ+PWbANvz8SCI6R85rE9y+6pBcYIaqUKZE/cIknOpSOsts6W9F1osZZ466IDsYh6ewYuRRr6QFVm5fa4QvPCS7LCAjFutFnIt+NaadprQQhN7vw6of8gRiVH9VNPpBb/KNxa1RNBIGF6CMv5OFIAgUA+YoPBOr0rwQCGt8qkSlHybJ/duSkDJjz8Jb1EydXYPuvoMfGIS9koEGxNShGBLTfXWQxQrEkEewKi4H3pSV9nuqlusNjGJlWoX27X7/YEWQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.001859, 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 Reviewed-by: Liam R. Howlett --- 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.