Re: [PATCH v9 11/17] mm: replace vm_lock and detached flag with a reference count

Suren Baghdasaryan
2025-01-13T21:19:01Z

On Mon, Jan 13, 2025 at 1:36 AM Wei Yang <richard.weiyang@gmail.com> wrote:
>
> On Fri, Jan 10, 2025 at 08:25:58PM -0800, Suren Baghdasaryan wrote:
> [...]
> >
> >+static inline bool is_vma_writer_only(int refcnt)
> >+{
> >+ /*
> >+ * With a writer and no readers, refcnt is VMA_LOCK_OFFSET if the vma
> >+ * is detached and (VMA_LOCK_OFFSET + 1) if it is attached. Waiting on
> >+ * a detached vma happens only in vma_mark_detached() and is a rare
> >+ * case, therefore most of the time there will be no unnecessary wakeup.
> >+ */
> >+ return refcnt & VMA_LOCK_OFFSET && refcnt <= VMA_LOCK_OFFSET + 1;
>
> It looks equivalent to
>
> return (refcnt == VMA_LOCK_OFFSET) || (refcnt == VMA_LOCK_OFFSET + 1);
>
> And its generated code looks a little simpler.

Yeah, but I think the original version is a bit more descriptive,
checking that (1) there is a writer and (2) there are no readers.

>
> >+}
> >+
> >+static inline void vma_refcount_put(struct vm_area_struct *vma)
> >+{
> >+ /* Use a copy of vm_mm in case vma is freed after we drop vm_refcnt */
> >+ struct mm_struct *mm = vma->vm_mm;
> >+ int oldcnt;
> >+
> >+ rwsem_release(&vma->vmlock_dep_map, _RET_IP_);
> >+ if (!__refcount_dec_and_test(&vma->vm_refcnt, &oldcnt)) {
> >+
> >+ if (is_vma_writer_only(oldcnt - 1))
> >+ rcuwait_wake_up(&mm->vma_writer_wait);
> >+ }
> >+}
> >+
>
> --
> Wei Yang
> Help you, Help me