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

Wei Yang
2025-01-13T09:36:14Z

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.

>+}
>+
>+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