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 2E6E1C02180 for ; Mon, 13 Jan 2025 17:57:03 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id BAA6B6B008A; Mon, 13 Jan 2025 12:57:02 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B5B646B008C; Mon, 13 Jan 2025 12:57:02 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9FB1B6B0092; Mon, 13 Jan 2025 12:57:02 -0500 (EST) 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 80CA06B008A for ; Mon, 13 Jan 2025 12:57:02 -0500 (EST) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 31501A0254 for ; Mon, 13 Jan 2025 17:57:02 +0000 (UTC) X-FDA: 83003184684.12.50D9EEF Received: from mail-qt1-f182.google.com (mail-qt1-f182.google.com [209.85.160.182]) by imf01.hostedemail.com (Postfix) with ESMTP id 4E1204000E for ; Mon, 13 Jan 2025 17:57:00 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=vDcCmXTC; spf=pass (imf01.hostedemail.com: domain of surenb@google.com designates 209.85.160.182 as permitted sender) smtp.mailfrom=surenb@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=1736791020; 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:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=dsnKCzZTFBXoelPsVIOpgU5ec5YIwNewGQBegIpuFsU=; b=jSeP+kfGjdu3GylBdOacIFfnxCsmFSaB62Gov03Vf7WEZSsg80jTcl59SWCCO5jjOF0Vjb 7yj3Fv3GvmgIyGB8mlOz/LUSmkecYLU1ADZJSa643HSW5c3JIdxn8dALgkUwh474Sb2d2n Yf0I7HroXd9YY8U8i1Y3+gtD05qYyDM= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1736791020; a=rsa-sha256; cv=none; b=SkEbC+LR01WKleKWsPc5HhHqrhzO/fqIkF5jgzU4mp4NfggiidbkPgDY9CygzZ+ao4qE6Q wLb1fd8D31lJypPpU1UbjzPn+W1ZME7CAqQ1NFqEUrdU7siVaYY77VMlShM/C0uEQM8hq2 LJ4vh0DGj2X1QnCwSiffPEItpFv3wPY= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=vDcCmXTC; spf=pass (imf01.hostedemail.com: domain of surenb@google.com designates 209.85.160.182 as permitted sender) smtp.mailfrom=surenb@google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-qt1-f182.google.com with SMTP id d75a77b69052e-467abce2ef9so4701cf.0 for ; Mon, 13 Jan 2025 09:57:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736791019; x=1737395819; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=dsnKCzZTFBXoelPsVIOpgU5ec5YIwNewGQBegIpuFsU=; b=vDcCmXTCr0jbnagJFm6LkLDD/kATEoOVvzR3daYspnJucu8H2KjuVTfeTaFV+RXIRB PvPmKfPYkJbUY/VggrnwGMimnBzfnt2PiFcdgYamR08N7nLSKm/LwDpPXyTK4ebJTZA2 BqOgBFq3IP8lmRI+WbwLZ1ivSd2Wrbmrt9TrhMIIh2NIQ4/YDFIWGTXxImDeSeH/QEAE BMySuzY6bdBypQBDT5uuU76fetU4wobo+nMiKbnRKfRRFWI8iZTiwsbJZyDimzVsyWst r+bN2gkSFzefYHDFn0yapTP0jwd5h2Z49GEx1gubEee2Cp7ESZVNnTQAx1rvnsAucXsO g8bQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736791019; x=1737395819; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=dsnKCzZTFBXoelPsVIOpgU5ec5YIwNewGQBegIpuFsU=; b=jpaPuhWAAYboxJvDPGCBwjPRGdm2JOJKgWs/UxDwrpvI7X08AYg8Y939AwIrNUMRcC JxjucMK0virn4my1JHq+xwrE7FNjmKqiaUTXSSrle35+aEuOvGdJdjJIMkc/4piGx/Sx 98kZHujE/WgWtJWC9S8wWRd6YuRhtw3DjIauUe/EXaT3ecxb+dqNrHdOIWDESwBOqBoC eeq/j5dE9PAZFChCun9BpKmd//tR7AQh+WI2sQ+X8CeErbBKNk11gU3ijoZ4UEXbZUCf j4ojDiOUlqoaPM4WKtAgDtZV8ReYMb7rnnCzJOAOWef2ZsYkd042LZ2Xn8gw4DTwJ4vB fQug== X-Forwarded-Encrypted: i=1; AJvYcCUeWXO+ZHP4keIHmAxsAgFeaH4BjaIPeOYfgKC33XZKeGH6uGq4Oln8nBuO6JYMuEwuiK+h7cx8HA==@kvack.org X-Gm-Message-State: AOJu0Yx9fVCQxX9cPdXmt3LUiabSZJ1/hnT+wc47JzL7OGPUqEkTAh2P 1lvW4DBp9nTMZudh+5J0WsHNln0CMJRmxE95yZH6UUijpkwSJwO23/vcaWvUInrEGj6uu7liHi0 pdXBidsjfbGSgocgolLmhIMtz20qVaoKLQiRs X-Gm-Gg: ASbGncsNhJmHALchPnpnB2vxs7LZ52goDjMDPA0UqnVTtEnXQ0y9fonmwxR+mZcqgA1 hGWXNxh+AwSQA3fVpHFN4jlh/dFYLdFbz0DAYCw== X-Google-Smtp-Source: AGHT+IGzcJo1I1YVox7rWqfGYoNZYNvup0scNLk1JR/UjsMbtJu6gVOf+7JEyklWdJNDczaONo32EGH6Yhgo5fbYmf0= X-Received: by 2002:ac8:574a:0:b0:466:7926:c69 with SMTP id d75a77b69052e-46c87f3c47bmr12873421cf.20.1736791017719; Mon, 13 Jan 2025 09:56:57 -0800 (PST) MIME-Version: 1.0 References: <20250111042604.3230628-1-surenb@google.com> <20250111042604.3230628-18-surenb@google.com> <096e5619-a717-4263-983c-e55c8c26f7e9@lucifer.local> In-Reply-To: <096e5619-a717-4263-983c-e55c8c26f7e9@lucifer.local> From: Suren Baghdasaryan Date: Mon, 13 Jan 2025 09:56:46 -0800 X-Gm-Features: AbW1kvZlyO4Ui26K7o5PUCTV3eMe4od3BesRZkKNlUkSMs9bz2M9h5XzyggEQAg Message-ID: Subject: Re: [PATCH v9 17/17] docs/mm: document latest changes to vm_lock To: Lorenzo Stoakes Cc: akpm@linux-foundation.org, peterz@infradead.org, willy@infradead.org, liam.howlett@oracle.com, david.laight.linux@gmail.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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 4E1204000E X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature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xuAOKtQX1lZzEsGwf68lnuyjxAXYYQg0rNyo9fcfpzKAJsCWsDLvrYBEEiqsG7Ur35t6eFeevELAQtOM9s+nDhmALSMFRtk2DqxvACA78K1C4cpELnqv0AuK1BBIEyKm8KH5IamkxvDs9UeOFaWPMrcjR3TkqKThE1olVuJlf4zTLXwdMxuKdRxT/xvvHKF76s/8SdXCmxQrT0/h/h+xQ6S34U6XbYM4lzFUhSTgnVo+EVUn0E3ciADtGrvwK+ma8xC2wYcQGR/4Lrb+S4n+AFsjAkED/+UMlAc4ePqGtvc1e2SJSVqUUhKZE7fryp/4g7X3fxTedP9aBZk= X-Bogosity: Ham, tests=bogofilter, spamicity=0.001037, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: On Mon, Jan 13, 2025 at 8:33=E2=80=AFAM Lorenzo Stoakes wrote: > > On Fri, Jan 10, 2025 at 08:26:04PM -0800, Suren Baghdasaryan wrote: > > 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 > > Apart from small nit, LGTM, thanks for doing this! > > Reviewed-by: Lorenzo Stoakes > > > --- > > Documentation/mm/process_addrs.rst | 44 ++++++++++++++++++------------ > > 1 file changed, 26 insertions(+), 18 deletions(-) > > > > diff --git a/Documentation/mm/process_addrs.rst b/Documentation/mm/proc= ess_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` sem= aphore for > > -their duration and the caller of :c:func:`!lock_vma_under_rcu` must re= lease it > > -via :c:func:`!vma_end_read`. > > +In cases when the user already holds mmap read lock, :c:func:`!vma_sta= rt_read_locked` > > +and :c:func:`!vma_start_read_locked_nested` can be used. These functio= ns do not > > +fail due to lock contention but the caller should still check their re= turn 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 ins= tances 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 t= he duration of the VMA write > > lock, releasing or downgrading the mmap write lock also releases the V= MA 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 refe= rence counter is > > +restored once the vma sequence number used for serialisation is update= d. > > > > This ensures the semantics we require - VMA write locks provide exclus= ive write > > access to the VMA. > > @@ -738,7 +743,7 @@ Implementation details > > > > The VMA lock mechanism is designed to be a lightweight means of avoidi= ng the use > > of the heavily contended mmap lock. It is implemented using a combinat= ion 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 opt= imistic > > @@ -779,28 +784,31 @@ release of any VMA locks on its release makes sen= se, as you would never want to > > keep VMAs locked across entirely separate write operations. It also ma= intains > > 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 chec= king 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 no= t match > > +that of the mm. > > > > -If it does, the read lock fails. If it does not, we hold the lock, exc= luding > > -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 drop= ped. > > +If it does not, we keep the reference counter raised, excluding writer= s, but > > +permitting other readers, who can also obtain this lock under RCU. > > > > Importantly, maple tree operations performed in :c:func:`!lock_vma_und= er_rcu` > > are also RCU safe, so the whole read lock operation is guaranteed to f= unction > > 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 t= his lock, > > -also simultaneously holding the mmap write lock. > > +On the write side, we set a bit in :c:member:`!vma.vm_refcnt` which ca= n't be > > +modified by readers and wait for all readers to drop their reference c= ount. > > +Once there are no readers, VMA's sequence number is set to match that = of the > > Nit: 'the VMA's sequence number' seems to read better here. Ack. > > > +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, avoidin= g > > -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 num= ber 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 a= llows for > > +This clever combination of a reference counter and sequence count allo= ws for > > fast RCU-based per-VMA lock acquisition (especially on page fault, tho= ugh > > utilised elsewhere) with minimal complexity around lock ordering. > > > > -- > > 2.47.1.613.gc27f4b7a9f-goog > >