From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qt1-f174.google.com (mail-qt1-f174.google.com [209.85.160.174]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 091983242DE for ; Thu, 22 Jan 2026 17:28:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=pass smtp.client-ip=209.85.160.174 ARC-Seal:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769102918; cv=pass; b=b0Ze8kObeWI28tSTpinYrp3K55X42Xh8BRu6wBTVeZOSPqop9ik6uuLwVgiWZyHjoB8POMy/Ygy08Rc3eeerPcysqCVYHL6iadCatEsHPdB8MebWLbVEtJI1lbGR6dwrS4LlixNe0VrmdX3RuWZmE/4PaxjnEBhjqiCtQbofuYY= ARC-Message-Signature:i=2; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1769102918; c=relaxed/simple; bh=+ksA8N8k2n5D3/kzgahd6VDNKfGPT8sA40xU8oXqSPk=; h=MIME-Version:References:In-Reply-To:From:Date:Message-ID:Subject: To:Cc:Content-Type; b=tmrvc5bTTHN28PdQBr4pM2bc/zxakJgv0iyDKyl7uWeenfShA049hwYYkRFWgO/9lVh+JrFBYH69A6ei+Se4cq/MgumFBdlh59INiTnZ19eSay3CzOTsGhUirKN37glNaj2F8vofM8SLfuHKju0Y+wpnOz6gAT29gTFGkmFwyAQ= ARC-Authentication-Results:i=2; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=Ay9307DP; arc=pass smtp.client-ip=209.85.160.174 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="Ay9307DP" Received: by mail-qt1-f174.google.com with SMTP id d75a77b69052e-5014b5d8551so543411cf.0 for ; Thu, 22 Jan 2026 09:28:21 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1769102897; cv=none; d=google.com; s=arc-20240605; b=YE1EYyNR897eQnW6yj4mC8vP04i5+gt5h7gUcPxFH3gifXFrmZ5KjUaJZMLlC1D11x hDErn6GMrikQmpA12sCW8ESK0+/vD4gcQqN8m2eC0DkqIU2W2xeflGKR7tmOFfwT2S5k /0rsa4l4epVBA/cXDD7D4bKrx2jPsB9am1jOjXt8ceDNMk5f1XRldtSP5L27qqtvb+PA cogKJprIQxG7FAXsI7ILNAOnUqRFgjNhVO1IqWsnX/MnBoaovxZucRQYWKMjKPNCQ4Rz cPRCfcgx4jgtVVrCkSFfmE/2tBRRQd1Gkg8gsbkoi7gaTWXCLSW2qQLtaprfa4SFOCJI WnnQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20240605; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=vKR+9mNOqKRizGQmFeK0caxIbjw2qX9Nz0/t9kP4530=; fh=jNkjA70fI+8vdnWXThTZL8Y1tczXBhuqN4iF6K8LK+0=; b=ErMEL8oAtz7khAuBmirFfKyeBioYjbcXWLQGQE+KsCi8tBCYzF78Nt+MqUlwg6+RCz +TZyPU2x/3bv+5uNL5WLb4Ni8lH57yKYK9hiuNwxsAM8mNIQV76NwwqBv0zcQU3zKWHP ew1Xyt7RLTfBW8VGYnHCz7iym1zz6+oxDbyOJ0ICmYAALcWlkjP2kOXNiKYbpB4+f/iu oKPDCTemfkJUh7xZQ8l8Ob2+lUvmZdNwnJ0hVI7LnguGycE6WQzqeiXTCY6cx9UR0nQC G8nSYL3vAgv5A2J3GLGPlcLZ6iEZeMp1QCSj4yCZKkVKGjDQPHtyLZJqhHrGAPLc91rk GOUA==; darn=vger.kernel.org ARC-Authentication-Results: i=1; mx.google.com; arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1769102897; x=1769707697; darn=vger.kernel.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=vKR+9mNOqKRizGQmFeK0caxIbjw2qX9Nz0/t9kP4530=; b=Ay9307DPhnKVkMVSGu9rolAD2f0MSZR5hQig1jBK5rsj+LzrUYj3lBU6qgvlJmiYDK oCuLvfm8raUtwlFO7WiI/f62a9Hh1OQLeyC1oE4Ouca2p6qMbsKgrdTGe3m/+lSz6dY2 Zt6GYmPTz/qAutijloy6Hm70gGPZPM7/PsToAK2sKd2d/SXPiSyxgxFVDNFalqFaKpGR y7V8pZPe7PiZJ40iJsmePaW4aNt7dgNvU52ajUtUJWWqq+e53aEzkejssSlewoY7Upjw mRWP3Pj84dU34cRbVHms5PCLA0481gUe8TmzfklJrmhaNY8RuKxyZQzqmyb8d2rT0/mL Bf6Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1769102897; x=1769707697; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=vKR+9mNOqKRizGQmFeK0caxIbjw2qX9Nz0/t9kP4530=; b=GNeQByl3yt+G11sv0SylqNfh3j8WTwp7pfhwgQEOj/ReapkOr+LXhPcrjuNzkj+FtY pBL8UPwrlDgZ7d9Flyp4M7h83wGkRuJ8XNWPtb4FB694WJjLr3NU0zTp4XayPXjPUUVq m1O68bnAJ9ltcc+hsHCHc69j5uzxeJsFa5ly2KHYw+0PJWF9jfIiesYTKbXpfIbGUTkB LxGh76J0/znY8jQZlSsuO0BT3RjUIB91H84ZPxcsrHZf/Dijekc1Vix7UsNmPJ5+qrD1 nfG6TKcg1UXR4qk23n9yBars/ijGLJ4wUea66JhYzioE8Ri/QpvS7oZpRBtdkUNOKJry 90Kg== X-Forwarded-Encrypted: i=1; AJvYcCVmUhtRhihYlj5tu5wRCYGYhQRJxYnDVCKdqXa2sdBsxFyBwFnx18s3XIKLn/3AwE2MQU7oEIhwZkCQz+A=@vger.kernel.org X-Gm-Message-State: AOJu0YwIxGWy2qyBajjiEZsDxVsh0elJVq5EurdpX2s1cySmVln6E6Rl QYN4AC86K2gj3n63A5dY6XysK6gw+3F5RgXU2/0M1fYkAGWBh56s97LS9eSBsHPralBOv2YqEI6 o7luyG51iOzd3oyZMfGOQjd+j4jvNiGh5wqE8fwKj X-Gm-Gg: AZuq6aKNnL8tMg2xOPA/UubDhBet/CNLZhvquiHpiF5rPsofBNc5/qga+eZXzA9Husx eNIbEUFXHrGBCncNRh7NK4ODTLYQdPauJssymE5r3JS7CxOlhu+6zSbgWS9R7FpN9OUZTi94hlm TrzcB773SUc2ySoDavoPYpR4ryO42VkCG7rpKQnTWerCw5zEntGEU3SNxd3K8O9U+s25Oz7vIFq jwoYoX74747M6QlQ/RHy6I3S5dobOjQ7+4xANNXUXk3YryyH1NWezxr77T9qv59mW4bdoDuDDIT YY6kJm/vsl22nSxRFd5YJbq/ X-Received: by 2002:a05:622a:5f05:b0:502:f58b:49bb with SMTP id d75a77b69052e-502f58b4cc6mr3220861cf.9.1769102896508; Thu, 22 Jan 2026 09:28:16 -0800 (PST) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 References: <7876e97c-9afb-418a-8ef7-5f4f306b7eea@suse.cz> In-Reply-To: <7876e97c-9afb-418a-8ef7-5f4f306b7eea@suse.cz> From: Suren Baghdasaryan Date: Thu, 22 Jan 2026 09:28:05 -0800 X-Gm-Features: AZwV_Qj5mha76ke8ifuGRZO6bD7iXP8oHrXlIUbFTB1-LzCBB2mAqlQ9bOo7PfQ Message-ID: Subject: Re: [PATCH RESEND v3 02/10] mm/vma: document possible vma->vm_refcnt values and reference comment To: Vlastimil Babka Cc: Lorenzo Stoakes , Andrew Morton , David Hildenbrand , "Liam R . Howlett" , Mike Rapoport , Michal Hocko , Shakeel Butt , Jann Horn , linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-rt-devel@lists.linux.dev, Peter Zijlstra , Ingo Molnar , Will Deacon , Boqun Feng , Waiman Long , Sebastian Andrzej Siewior , Clark Williams , Steven Rostedt Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Thu, Jan 22, 2026 at 8:48=E2=80=AFAM Vlastimil Babka wr= ote: > > On 1/22/26 14:01, Lorenzo Stoakes wrote: > > The possible vma->vm_refcnt values are confusing and vague, explain in > > detail what these can be in a comment describing the vma->vm_refcnt fie= ld > > and reference this comment in various places that read/write this field= . > > > > No functional change intended. > > > > Signed-off-by: Lorenzo Stoakes > > Thanks, very useful. Forgive my nitpicks :) It's because it's tricky so b= est > try to be as precise as possible, I believe. Another thanks from me. > > > --- > > include/linux/mm_types.h | 39 +++++++++++++++++++++++++++++++++++++-- > > include/linux/mmap_lock.h | 7 +++++++ > > mm/mmap_lock.c | 6 ++++++ > > 3 files changed, 50 insertions(+), 2 deletions(-) > > > > diff --git a/include/linux/mm_types.h b/include/linux/mm_types.h > > index 94de392ed3c5..e5ee66f84d9a 100644 > > --- a/include/linux/mm_types.h > > +++ b/include/linux/mm_types.h > > @@ -758,7 +758,8 @@ static inline struct anon_vma_name *anon_vma_name_a= lloc(const char *name) > > * set the VM_REFCNT_EXCLUDE_READERS_FLAG in vma->vm_refcnt to indicia= te to > > * vma_start_read() that the reference count should be left alone. > > * > > - * Once the operation is complete, this value is subtracted from vma->= vm_refcnt. > > + * See the comment describing vm_refcnt in vm_area_struct for details = as to > > + * which values the VMA reference count can be. > > */ > > #define VM_REFCNT_EXCLUDE_READERS_BIT (30) > > #define VM_REFCNT_EXCLUDE_READERS_FLAG (1U << VM_REFCNT_EXCLUDE_= READERS_BIT) > > @@ -989,7 +990,41 @@ struct vm_area_struct { > > struct vma_numab_state *numab_state; /* NUMA Balancing state *= / > > #endif > > #ifdef CONFIG_PER_VMA_LOCK > > - /* Unstable RCU readers are allowed to read this. */ > > + /* > > + * Used to keep track of the number of references taken by VMA re= ad or > > + * write locks. May have the VM_REFCNT_EXCLUDE_READERS_FLAG set > > I wonder about the "or write locks" part. The process of acquiring it use= s > VM_REFCNT_EXCLUDE_READERS_FLAG but then the writer doesn't hold a 1 > refcount? (the sentence could be read it way IMHO) It's vma being attache= d > that does, AFAIK? Yes, since there can be only one write-locker it only has to set VM_REFCNT_EXCLUDE_READERS_FLAG bit to announce its presence, without incrementing the refcount. > > > + * indicating that a thread has entered __vma_enter_locked() and = is > > + * waiting on any outstanding read locks to exit. > > + * > > + * This value can be equal to: > > + * > > + * 0 - Detached. > > Is it worth saying that readers can't increment the refcount? Yes, you mention that for VM_REFCNT_EXCLUDE_READERS_FLAG value. The same IMPORTANT notice applies here. > > > + * 1 - Unlocked or write-locked. > > "Attached and either unlocked or write-locked." ? Agree. That's more specific. Should we also mention here that unlocked vs write-locked distinction is determined using the vm_lock_seq member? > > (see how "write-locked" isn't reflected, I argued above) > > > + * > > + * >1, < VM_REFCNT_EXCLUDE_READERS_FLAG - Read-locked or (unlikel= y) > > + * write-locked with other threads having temporarily incremented= the > > + * reference count prior to determining it is write-locked and > > + * decrementing it again. > > Ack. > > > + * VM_REFCNT_EXCLUDE_READERS_FLAG - Detached, pending > > + * __vma_exit_locked() completion which will decrement the refere= nce > > + * count to zero. IMPORTANT - at this stage no further readers ca= n > > + * increment the reference count. It can only be reduced. > > + * > > + * VM_REFCNT_EXCLUDE_READERS_FLAG + 1 - Either an attached VMA pe= nding > > + * __vma_exit_locked() completion which will decrement the refere= nce > > + * count to one, OR a detached VMA waiting on a single spurious r= eader > > + * to decrement reference count. IMPORTANT - as above, no further > > + * readers can increment the reference count. > > + * > > + * > VM_REFCNT_EXCLUDE_READERS_FLAG + 1 - VMA is waiting on reade= rs, > > "VMA is waiting" sounds weird? a thread might be, but VMA itself? > (similarly in the previous paragraph) Maybe "VMA in the process of being write-locked or detached, which got blocked due to the spurious readers that temporarily raised the refcount"? > > > + * whether it is attempting to acquire a write lock or attempting= to > > + * detach. IMPORTANT - as above, no ruther readers can increment = the > > + * reference count. > > + * > > + * NOTE: Unstable RCU readers are allowed to read this. > > + */ > > refcount_t vm_refcnt ____cacheline_aligned_in_smp; > > #ifdef CONFIG_DEBUG_LOCK_ALLOC > > struct lockdep_map vmlock_dep_map; > > diff --git a/include/linux/mmap_lock.h b/include/linux/mmap_lock.h > > index 5acbd4ba1b52..a764439d0276 100644 > > --- a/include/linux/mmap_lock.h > > +++ b/include/linux/mmap_lock.h > > @@ -130,6 +130,9 @@ static inline bool is_vma_writer_only(int refcnt) > > * 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. > > + * > > + * See the comment describing the vm_area_struct->vm_refcnt field= for > > + * details of possible refcnt values. > > */ > > return (refcnt & VM_REFCNT_EXCLUDE_READERS_FLAG) && > > refcnt <=3D VM_REFCNT_EXCLUDE_READERS_FLAG + 1; > > @@ -249,6 +252,10 @@ static inline void vma_assert_locked(struct vm_are= a_struct *vma) > > { > > unsigned int mm_lock_seq; > > > > + /* > > + * See the comment describing the vm_area_struct->vm_refcnt field= for > > + * details of possible refcnt values. > > + */ > > VM_BUG_ON_VMA(refcount_read(&vma->vm_refcnt) <=3D 1 && > > !__is_vma_write_locked(vma, &mm_lock_seq), vma); > > } > > diff --git a/mm/mmap_lock.c b/mm/mmap_lock.c > > index 1d23b48552e9..75dc098aea14 100644 > > --- a/mm/mmap_lock.c > > +++ b/mm/mmap_lock.c > > @@ -65,6 +65,9 @@ static inline int __vma_enter_locked(struct vm_area_s= truct *vma, > > /* > > * If vma is detached then only vma_mark_attached() can raise the > > * vm_refcnt. mmap_write_lock prevents racing with vma_mark_attac= hed(). > > + * > > + * See the comment describing the vm_area_struct->vm_refcnt field= for > > + * details of possible refcnt values. > > */ > > if (!refcount_add_not_zero(VM_REFCNT_EXCLUDE_READERS_FLAG, &vma->= vm_refcnt)) > > return 0; > > @@ -137,6 +140,9 @@ void vma_mark_detached(struct vm_area_struct *vma) > > * before they check vm_lock_seq, realize the vma is locked and d= rop > > * back the vm_refcnt. That is a narrow window for observing a ra= ised > > * vm_refcnt. > > + * > > + * See the comment describing the vm_area_struct->vm_refcnt field= for > > + * details of possible refcnt values. > > */ > > if (unlikely(!refcount_dec_and_test(&vma->vm_refcnt))) { > > /* Wait until vma is detached with no readers. */ > > -- > > 2.52.0 >