From mboxrd@z Thu Jan 1 00:00:00 1970 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 BA97FE77188 for ; Thu, 26 Dec 2024 17:07:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CA19C6B00A9; Thu, 26 Dec 2024 12:07:51 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id C28AB6B00AA; Thu, 26 Dec 2024 12:07:51 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A2D6A6B00AB; Thu, 26 Dec 2024 12:07:51 -0500 (EST) 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 80CA46B00A9 for ; Thu, 26 Dec 2024 12:07:51 -0500 (EST) Received: from smtpin25.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 39439C103B for ; Thu, 26 Dec 2024 17:07:51 +0000 (UTC) X-FDA: 82937740662.25.F4660F5 Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) by imf09.hostedemail.com (Postfix) with ESMTP id 857FC14000E for ; Thu, 26 Dec 2024 17:07:20 +0000 (UTC) Authentication-Results: imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=fY7MblCN; spf=pass (imf09.hostedemail.com: domain of 3Y41tZwYKCHYmolYhVaiiafY.Wigfchor-ggepUWe.ila@flex--surenb.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=3Y41tZwYKCHYmolYhVaiiafY.Wigfchor-ggepUWe.ila@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=1735232820; 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=n0bYxFZhcDHg7gKjgV3lNbcRW7Y3pA8DAyN8Hyy1GpFXtVYUSm3QbkIuuZ+CCpIKNBMit2 1dI44LBloc17iWKI59cl0pWPYp3D7e3OvToEqF5doRsf4dJFyZJ0wf0DYR1/caBm/7zzzW t6jC4m1USaVTz+IJAxYA/Ud7o3/OIss= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1735232820; a=rsa-sha256; cv=none; b=PG0CIDRHVdEEuitn6yYCxWpKKnSuiwIlmr8GZYOtWVDhSv+qtIURzSQZYfWLf1UPojkzf3 64QonOeUd6Fupi0Osg0iCMdHAlKS6E56TggHPveQOdprpCsr1SKEPofc5+Hp4ZY6b+o3Hc p5PF/hexfjpMLzJZ6lInNlEMfI0GZEU= ARC-Authentication-Results: i=1; imf09.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=fY7MblCN; spf=pass (imf09.hostedemail.com: domain of 3Y41tZwYKCHYmolYhVaiiafY.Wigfchor-ggepUWe.ila@flex--surenb.bounces.google.com designates 209.85.216.74 as permitted sender) smtp.mailfrom=3Y41tZwYKCHYmolYhVaiiafY.Wigfchor-ggepUWe.ila@flex--surenb.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-pj1-f74.google.com with SMTP id 98e67ed59e1d1-2ef9e4c5343so12323502a91.0 for ; Thu, 26 Dec 2024 09:07:48 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1735232868; x=1735837668; 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=fY7MblCNbdDqi0khH8qtMEB/DS/OtO+fxdclXr3o0k5rvnbxRvRrVg/SKcDmu8cu/I TseWxdHI3j+QHHE2f45uNV4yj2cXClnsVpm/4bCn7ITsiDlKmQLrKbKrZDr1Sfk1p9Ej X2tBtTrRTaupsAbzwZsB7UoiJNA3tKs3xHbarTjT/qcVSRSY0ikKvJlLCxnTs57wRawh MKb6WbZLSU9BVHS8JWRo6Q3ESA8mRk1klcchDOA0skCIaYPm8GsAcEvskTszVGQTtJDM iSj6/0tq3ymKqUGG9CWvBygGhcgdV/KMGiLtcOjA11HRNyBSNmXo4KMGdI8Hd4iP9ZyK 1H/w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735232868; x=1735837668; 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=uxjL3GJcGlcHJBHYmE75Kf5ey81xebQ169O5OAmjWT8G3qCLZwfkGDtpemOjyXH5Ai h1k055yOKYYHW02U/a8xiM6ga/w/AeSm1mahBcmhThKlMKv7DqnNiAhgBfJb+8b9StF1 kVAukgWDQYhlT31b7x7l8K7SPLWKLWBgrXEkUkJJ8TqMzEA7fQUyKDhLGVvVEYbKn089 cZv5FXv6srbRZpvJDZhOoTDPGMJk89JPAbpgxT6xYoZnc4bBW95579TGjFLjpyM9k12c Rj9X2RzRQ5s5/+zKfvG3DmXpeR7SfnXYT8wAFL3w3g7pdynHLlAAns9a86t4k32xoNE5 yEeg== X-Forwarded-Encrypted: i=1; AJvYcCXbwrOdT2/kzMrDKeiNTjKtTPNQtll+04JVXsir747/gIN9RFhJQuGDPapjHeQX+oxMeWKueftp1w==@kvack.org X-Gm-Message-State: AOJu0YzOvoRAFFE8DSE6ENX7PavGIdpvkIkOhvcs79H7zFL4VfIe09Vw Ftja3Muou+mQjhp+8oR0910sFdkry6zw2TvZMoQPbHlA+3aiA8XqYohkNE1e1aVoFrOGoBxeVyb NnA== X-Google-Smtp-Source: AGHT+IF7dKgo6NOta05WIlCiTUUdE+0C61M7hMlod7dRoqWPYR+1EhCFaw3EWtQn1lXePDQDqo/d6R5TwFE= X-Received: from pfbcw3.prod.google.com ([2002:a05:6a00:4503:b0:725:f1d9:f706]) (user=surenb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a00:4399:b0:725:8b00:167e with SMTP id d2e1a72fcca58-72abdec65e2mr31237080b3a.16.1735232867927; Thu, 26 Dec 2024 09:07:47 -0800 (PST) Date: Thu, 26 Dec 2024 09:07:09 -0800 In-Reply-To: <20241226170710.1159679-1-surenb@google.com> Mime-Version: 1.0 References: <20241226170710.1159679-1-surenb@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241226170710.1159679-18-surenb@google.com> Subject: [PATCH v7 17/17] 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 Content-Type: text/plain; charset="UTF-8" X-Rspamd-Queue-Id: 857FC14000E X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: dry1qq41udb1mggas44moa368dr9y85z X-HE-Tag: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DAS6CicEgFMQzdnKs0Bx+fBWaPFd0HB7LF5D5LuhbgRe3tDz4C/rZaoL6qvApHjq7T7JOeYG93dgyZ4lhB/n15rJqEWG+ph1bSYUXBGDJHOw2KoIxF7s5accc28LTCEkN8CjGUQlmApAzlqTICW+SbN6kDPa6R3miAMduzlDOo2xfg25fMCK+nZM29rZTpm9WGa7YI0tUJrpU8vwZKD8y57EC2tGvbiTwTImUfh1Tw/+lxKO1BB6P8/5rtKeQYV266h9Sl/bwqD8Hu7ikqxSvkXvXl5uk9KhNHCS6YgiY7FtaOGKyyfdDez3mxlglfj+tofFROqbCFS15wKUerh/x+FT4NlapOERUuZRI4nvtKUINJ5jC8Zpq7lw/xi3lE7sWoa54DLDPMewXAUBZO+o7xpUwxopF3R7BJwAUVT1mqcCRA9bBpqyiVCNwzeA0ViOIAM9WWu03yT4L/WHEmoiDQsq1Wg4jTbYiDkX2n+NlRV3CKS9u8XFGtaoAYx2SCnj48uTUDQ== X-Bogosity: Ham, tests=bogofilter, spamicity=0.003709, 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. -- 2.47.1.613.gc27f4b7a9f-goog