linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Suren Baghdasaryan <surenb@google.com>
To: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>
Cc: Andrew Morton <akpm@linux-foundation.org>,
	David Hildenbrand <david@kernel.org>,
	 "Liam R . Howlett" <Liam.Howlett@oracle.com>,
	Vlastimil Babka <vbabka@suse.cz>,
	 Mike Rapoport <rppt@kernel.org>, Michal Hocko <mhocko@suse.com>,
	Shakeel Butt <shakeel.butt@linux.dev>,
	 Jann Horn <jannh@google.com>,
	linux-mm@kvack.org, linux-kernel@vger.kernel.org,
	 linux-rt-devel@lists.linux.dev,
	Peter Zijlstra <peterz@infradead.org>,
	 Ingo Molnar <mingo@redhat.com>, Will Deacon <will@kernel.org>,
	Boqun Feng <boqun.feng@gmail.com>,
	 Waiman Long <longman@redhat.com>,
	Sebastian Andrzej Siewior <bigeasy@linutronix.de>,
	 Clark Williams <clrkwllms@kernel.org>,
	Steven Rostedt <rostedt@goodmis.org>
Subject: Re: [PATCH RESEND v3 04/10] mm/vma: add+use vma lockdep acquire/release defines
Date: Thu, 22 Jan 2026 11:32:17 -0800	[thread overview]
Message-ID: <CAJuCfpG+H=gRSWuhLL=NHQAQzrbF8Jku2qgRHv1moLPRibk5TA@mail.gmail.com> (raw)
In-Reply-To: <4703b7320796908b9a0827c3d42d74d11b703714.1769086312.git.lorenzo.stoakes@oracle.com>

On Thu, Jan 22, 2026 at 5:02 AM Lorenzo Stoakes
<lorenzo.stoakes@oracle.com> wrote:
>
> The code is littered with inscrutable and duplicative lockdep incantations,
> replace these with defines which explain what is going on and add
> commentary to explain what we're doing.
>
> If lockdep is disabled these become no-ops. We must use defines so _RET_IP_
> remains meaningful.
>
> These are self-documenting and aid readability of the code.
>
> Additionally, instead of using the confusing rwsem_*() form for something
> that is emphatically not an rwsem, we instead explicitly use
> lock_[acquired, release]_shared/exclusive() lockdep invocations since we
> are doing something rather custom here and these make more sense to use.
>
> No functional change intended.
>
> Signed-off-by: Lorenzo Stoakes <lorenzo.stoakes@oracle.com>

Very nice! Thank you.

Reviewed-by: Suren Baghdasaryan <surenb@google.com>

> ---
>  include/linux/mmap_lock.h | 35 ++++++++++++++++++++++++++++++++---
>  mm/mmap_lock.c            | 10 +++++-----
>  2 files changed, 37 insertions(+), 8 deletions(-)
>
> diff --git a/include/linux/mmap_lock.h b/include/linux/mmap_lock.h
> index 0b3614aadbb4..da63b1be6ec0 100644
> --- a/include/linux/mmap_lock.h
> +++ b/include/linux/mmap_lock.h
> @@ -78,6 +78,36 @@ static inline void mmap_assert_write_locked(const struct mm_struct *mm)
>
>  #ifdef CONFIG_PER_VMA_LOCK
>
> +/*
> + * VMA locks do not behave like most ordinary locks found in the kernel, so we
> + * cannot quite have full lockdep tracking in the way we would ideally prefer.
> + *
> + * Read locks act as shared locks which exclude an exclusive lock being
> + * taken. We therefore mark these accordingly on read lock acquire/release.
> + *
> + * Write locks are acquired exclusively per-VMA, but released in a shared
> + * fashion, that is upon vma_end_write_all(), we update the mmap's seqcount such
> + * that write lock is de-acquired.
> + *
> + * We therefore cannot track write locks per-VMA, nor do we try. Mitigating this
> + * is the fact that, of course, we do lockdep-track the mmap lock rwsem.
> + *
> + * We do, however, want to indicate that during either acquisition of a VMA
> + * write lock or detachment of a VMA that we require the lock held be exclusive,
> + * so we utilise lockdep to do so.
> + */
> +#define __vma_lockdep_acquire_read(vma) \
> +       lock_acquire_shared(&vma->vmlock_dep_map, 0, 1, NULL, _RET_IP_)
> +#define __vma_lockdep_release_read(vma) \
> +       lock_release(&vma->vmlock_dep_map, _RET_IP_)
> +#define __vma_lockdep_acquire_exclusive(vma) \
> +       lock_acquire_exclusive(&vma->vmlock_dep_map, 0, 0, NULL, _RET_IP_)
> +#define __vma_lockdep_release_exclusive(vma) \
> +       lock_release(&vma->vmlock_dep_map, _RET_IP_)
> +/* Only meaningful if CONFIG_LOCK_STAT is defined. */
> +#define __vma_lockdep_stat_mark_acquired(vma) \
> +       lock_acquired(&vma->vmlock_dep_map, _RET_IP_)
> +
>  static inline void mm_lock_seqcount_init(struct mm_struct *mm)
>  {
>         seqcount_init(&mm->mm_lock_seq);
> @@ -176,8 +206,7 @@ static inline void vma_refcount_put(struct vm_area_struct *vma)
>         int refcnt;
>         bool detached;
>
> -       rwsem_release(&vma->vmlock_dep_map, _RET_IP_);
> -
> +       __vma_lockdep_release_read(vma);
>         detached = __vma_refcount_put(vma, &refcnt);
>         /*
>          * __vma_enter_locked() may be sleeping waiting for readers to drop
> @@ -203,7 +232,7 @@ static inline bool vma_start_read_locked_nested(struct vm_area_struct *vma, int
>                                                               VM_REFCNT_LIMIT)))
>                 return false;
>
> -       rwsem_acquire_read(&vma->vmlock_dep_map, 0, 1, _RET_IP_);
> +       __vma_lockdep_acquire_read(vma);
>         return true;
>  }
>
> diff --git a/mm/mmap_lock.c b/mm/mmap_lock.c
> index ebacb57e5f16..9563bfb051f4 100644
> --- a/mm/mmap_lock.c
> +++ b/mm/mmap_lock.c
> @@ -72,7 +72,7 @@ static inline int __vma_enter_locked(struct vm_area_struct *vma,
>         if (!refcount_add_not_zero(VM_REFCNT_EXCLUDE_READERS_FLAG, &vma->vm_refcnt))
>                 return 0;
>
> -       rwsem_acquire(&vma->vmlock_dep_map, 0, 0, _RET_IP_);
> +       __vma_lockdep_acquire_exclusive(vma);
>         err = rcuwait_wait_event(&vma->vm_mm->vma_writer_wait,
>                    refcount_read(&vma->vm_refcnt) == tgt_refcnt,
>                    state);
> @@ -85,10 +85,10 @@ static inline int __vma_enter_locked(struct vm_area_struct *vma,
>                         WARN_ON_ONCE(!detaching);
>                         err = 0;
>                 }
> -               rwsem_release(&vma->vmlock_dep_map, _RET_IP_);
> +               __vma_lockdep_release_exclusive(vma);
>                 return err;
>         }
> -       lock_acquired(&vma->vmlock_dep_map, _RET_IP_);
> +       __vma_lockdep_stat_mark_acquired(vma);
>
>         return 1;
>  }
> @@ -97,7 +97,7 @@ static inline void __vma_exit_locked(struct vm_area_struct *vma, bool *detached)
>  {
>         *detached = refcount_sub_and_test(VM_REFCNT_EXCLUDE_READERS_FLAG,
>                                           &vma->vm_refcnt);
> -       rwsem_release(&vma->vmlock_dep_map, _RET_IP_);
> +       __vma_lockdep_release_exclusive(vma);
>  }
>
>  int __vma_start_write(struct vm_area_struct *vma, unsigned int mm_lock_seq,
> @@ -199,7 +199,7 @@ static inline struct vm_area_struct *vma_start_read(struct mm_struct *mm,
>                 goto err;
>         }
>
> -       rwsem_acquire_read(&vma->vmlock_dep_map, 0, 1, _RET_IP_);
> +       __vma_lockdep_acquire_read(vma);
>
>         if (unlikely(vma->vm_mm != mm))
>                 goto err_unstable;
> --
> 2.52.0


  reply	other threads:[~2026-01-22 19:32 UTC|newest]

Thread overview: 73+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-01-22 13:01 [PATCH RESEND v3 00/10] mm: add and use vma_assert_stabilised() helper Lorenzo Stoakes
2026-01-22 13:01 ` [PATCH RESEND v3 01/10] mm/vma: rename VMA_LOCK_OFFSET to VM_REFCNT_EXCLUDE_READERS_FLAG Lorenzo Stoakes
2026-01-22 16:26   ` Vlastimil Babka
2026-01-22 16:29     ` Lorenzo Stoakes
2026-01-23 13:52       ` Lorenzo Stoakes
2026-01-22 16:37   ` Suren Baghdasaryan
2026-01-23 13:26     ` Lorenzo Stoakes
2026-01-22 13:01 ` [PATCH RESEND v3 02/10] mm/vma: document possible vma->vm_refcnt values and reference comment Lorenzo Stoakes
2026-01-22 16:48   ` Vlastimil Babka
2026-01-22 17:28     ` Suren Baghdasaryan
2026-01-23 15:06       ` Lorenzo Stoakes
2026-01-23 13:45     ` Lorenzo Stoakes
2026-01-22 13:01 ` [PATCH RESEND v3 03/10] mm/vma: rename is_vma_write_only(), separate out shared refcount put Lorenzo Stoakes
2026-01-22 17:36   ` Vlastimil Babka
2026-01-22 19:31     ` Suren Baghdasaryan
2026-01-23  8:24       ` Vlastimil Babka
2026-01-23 14:52         ` Lorenzo Stoakes
2026-01-23 15:05           ` Vlastimil Babka
2026-01-23 15:07             ` Lorenzo Stoakes
2026-01-23 14:41       ` Lorenzo Stoakes
2026-01-26 10:04         ` Lorenzo Stoakes
2026-01-23 14:02     ` Lorenzo Stoakes
2026-01-22 13:01 ` [PATCH RESEND v3 04/10] mm/vma: add+use vma lockdep acquire/release defines Lorenzo Stoakes
2026-01-22 19:32   ` Suren Baghdasaryan [this message]
2026-01-22 19:41     ` Suren Baghdasaryan
2026-01-23  8:41       ` Vlastimil Babka
2026-01-23 15:08         ` Lorenzo Stoakes
2026-01-23 15:00     ` Lorenzo Stoakes
2026-01-23  8:48   ` Vlastimil Babka
2026-01-23 15:10     ` Lorenzo Stoakes
2026-01-22 13:01 ` [PATCH RESEND v3 05/10] mm/vma: de-duplicate __vma_enter_locked() error path Lorenzo Stoakes
2026-01-22 19:39   ` Suren Baghdasaryan
2026-01-23 15:11     ` Lorenzo Stoakes
2026-01-23  8:54   ` Vlastimil Babka
2026-01-23 15:10     ` Lorenzo Stoakes
2026-01-22 13:01 ` [PATCH v3 06/10] mm/vma: clean up __vma_enter/exit_locked() Lorenzo Stoakes
2026-01-22 13:08   ` Lorenzo Stoakes
2026-01-22 20:15   ` Suren Baghdasaryan
2026-01-22 20:55     ` Andrew Morton
2026-01-23 16:15       ` Lorenzo Stoakes
2026-01-23 16:33     ` Lorenzo Stoakes
2026-01-23  9:16   ` Vlastimil Babka
2026-01-23 16:17     ` Lorenzo Stoakes
2026-01-23 16:28       ` Lorenzo Stoakes
2026-01-22 13:01 ` [PATCH RESEND v3 07/10] mm/vma: introduce helper struct + thread through exclusive lock fns Lorenzo Stoakes
2026-01-22 21:41   ` Suren Baghdasaryan
2026-01-23 17:59     ` Lorenzo Stoakes
2026-01-23 19:34       ` Suren Baghdasaryan
2026-01-23 20:04         ` Lorenzo Stoakes
2026-01-23 22:07           ` Suren Baghdasaryan
2026-01-24  8:54             ` Lorenzo Stoakes
2026-01-26  6:09               ` Suren Baghdasaryan
2026-01-23 10:02   ` Vlastimil Babka
2026-01-23 18:18     ` Lorenzo Stoakes
2026-01-22 13:02 ` [PATCH RESEND v3 08/10] mm/vma: improve and document __is_vma_write_locked() Lorenzo Stoakes
2026-01-22 21:55   ` Suren Baghdasaryan
2026-01-23 16:21     ` Vlastimil Babka
2026-01-23 17:42       ` Suren Baghdasaryan
2026-01-23 18:44       ` Lorenzo Stoakes
2026-01-22 13:02 ` [PATCH RESEND v3 09/10] mm/vma: update vma_assert_locked() to use lockdep Lorenzo Stoakes
2026-01-22 22:02   ` Suren Baghdasaryan
2026-01-23 18:45     ` Lorenzo Stoakes
2026-01-23 16:55   ` Vlastimil Babka
2026-01-23 18:49     ` Lorenzo Stoakes
2026-01-22 13:02 ` [PATCH RESEND v3 10/10] mm/vma: add and use vma_assert_stabilised() Lorenzo Stoakes
2026-01-22 22:12   ` Suren Baghdasaryan
2026-01-23 18:54     ` Lorenzo Stoakes
2026-01-23 17:10   ` Vlastimil Babka
2026-01-23 18:51     ` Lorenzo Stoakes
2026-01-23 23:35   ` Hillf Danton
2026-01-22 15:48 ` [PATCH RESEND v3 00/10] mm: add and use vma_assert_stabilised() helper Andrew Morton
2026-01-22 15:57   ` Lorenzo Stoakes
2026-01-22 16:01     ` Lorenzo Stoakes

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAJuCfpG+H=gRSWuhLL=NHQAQzrbF8Jku2qgRHv1moLPRibk5TA@mail.gmail.com' \
    --to=surenb@google.com \
    --cc=Liam.Howlett@oracle.com \
    --cc=akpm@linux-foundation.org \
    --cc=bigeasy@linutronix.de \
    --cc=boqun.feng@gmail.com \
    --cc=clrkwllms@kernel.org \
    --cc=david@kernel.org \
    --cc=jannh@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-rt-devel@lists.linux.dev \
    --cc=longman@redhat.com \
    --cc=lorenzo.stoakes@oracle.com \
    --cc=mhocko@suse.com \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --cc=rppt@kernel.org \
    --cc=shakeel.butt@linux.dev \
    --cc=vbabka@suse.cz \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox