From nobody Sun Feb 8 15:28:45 2026 Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) (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 35A2019342F for ; Sat, 11 Jan 2025 04:26:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.214.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736569601; cv=none; b=WE7eE7hgh3FgZyHsU1wTx+F0iV3YQ7lBaKr67KvSrghPMIvdC7gaz8dOJd3Hu94d3zAo6hwK2NyPW86lANnsjxmk0tL629h/xv6+H048zu+pPLc0wHUI8Ruz2AlAu7rYGwMBO4Fcm/HdLA/en8zylQ1W24S/ZRya8BqTY56UJxI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1736569601; c=relaxed/simple; bh=bMFnoPr0vvMm61gosasQGe/t4yH8uJ1rFqZuK9xLtds=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=IAU58HfBC66Sq934khLKsnPhr16CIiLxzKeuIuZ+6or3XFE9388wWQT2vKWW3E05eMu0ATa6AgHA7dvC8MVsgOFafIl2suIavI7c12wBfmiLw/whc4PGlxpjq+8bwNXk6Wjoqm30s1V17PuE57f1sXpWrX978yXKMTp4NmZOhpY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--surenb.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=loBJwYJ5; arc=none smtp.client-ip=209.85.214.201 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=flex--surenb.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="loBJwYJ5" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-21638389f63so33524495ad.1 for ; Fri, 10 Jan 2025 20:26:40 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1736569599; x=1737174399; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=sqweZn90u3nkindBXw7O0ID50FUbfbOsDRIP3ctel0c=; b=loBJwYJ5r1BVvdRFPgm2kNlRt+qIHwFKEb/+03kmH+cDFiWkVUBGNQYgsROcRek3+r AfFW0gGRfudq7VhqSkWG01zliJNOANJM8COtlhqEge2hYuo0COA3KneQvZUaNUWjWG/f 1ob2VtdkAbGCmFzCzka0UVZxHPEoSDQ3Kx4dUnFYOOuoPeypHi6jWJNYk2M3C4OKWe1S crRYhYgnQieLGd5GtC7Gn3uFKwPTV90uBx8LcxGGUJ60/+gFh6wK5NpXCcufVk0R2ZvF L4GVIdF+QeP17YhBQ+IooGAKYFq5naAdGsWRF7E4MrmLSkJgL9u8KYWPRAhO44SPHcNp 1oKw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1736569599; x=1737174399; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=sqweZn90u3nkindBXw7O0ID50FUbfbOsDRIP3ctel0c=; b=gewv6yqKZd75xBIHxWLtRLNxX8wRhPnn1EuRs3C8kkYQTBPVCFJcUanrei6RVN3BZj B6NKx4ToVaMCUY8fN4/vzyygGFDRLzIvYuXDQ+0f1bVXf8BOVHTYzVjlHS4sYKaLBgzU XXqhy0sDhOuF133HumXtT4V+SApyde1bWSbFU6lZBBpm8u/QsNUV5y/A7clLsxh4RvWk fC46U+kVD8581/31VN586Ufv2MnFFMF4drVtD5WnFeZnwYuFhfD/6mGi62KQNYcD0b3R /kVhJWHgtC0IzSyI+bllT0S1zbjvofwnGZRtK/G5zeMiCzXpsDp8Kl6UZID3lyvi2caG ErYg== X-Forwarded-Encrypted: i=1; AJvYcCXyc4aeHPsATzIuABQ2qbViknrgWvm009BFAcxMS6qBAprpSttOu2uOZmcFNLlRfs+S4NRCRH+Z6hVdr+Y=@vger.kernel.org X-Gm-Message-State: AOJu0YzsLzA+oZpZE8kTzBGyD2pC3yG1i4jAWDmuZBjiD2KzmZrXHX8U GwAtygJeaRBgg6oExH38IpTLe5VW8cQbHBTvXblctt0Q39yVbUlAB1VK5UgiP1yrkEi+rYuCHXs 7yA== X-Google-Smtp-Source: AGHT+IEDi9vLxcvUpqCWBPMXXF0Y8lrMfIatGu39C3DhS1ZkGtjS5dUciQm8YtZJQiW79lEU0cSQ1XI24iE= X-Received: from pgg14.prod.google.com ([2002:a05:6a02:4d8e:b0:7ff:d6:4f07]) (user=surenb job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6a20:c996:b0:1e3:e680:8c91 with SMTP id adf61e73a8af0-1e88d2d5ea9mr24704039637.31.1736569599645; Fri, 10 Jan 2025 20:26:39 -0800 (PST) Date: Fri, 10 Jan 2025 20:26:02 -0800 In-Reply-To: <20250111042604.3230628-1-surenb@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20250111042604.3230628-1-surenb@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20250111042604.3230628-16-surenb@google.com> Subject: [PATCH v9 15/17] mm: prepare lock_vma_under_rcu() for vma reuse possibility From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: peterz@infradead.org, willy@infradead.org, liam.howlett@oracle.com, lorenzo.stoakes@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, surenb@google.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Once we make vma cache SLAB_TYPESAFE_BY_RCU, it will be possible for a vma to be reused and attached to another mm after lock_vma_under_rcu() locks the vma. lock_vma_under_rcu() should ensure that vma_start_read() is using the original mm and after locking the vma it should ensure that vma->vm_mm has not changed from under us. Signed-off-by: Suren Baghdasaryan Reviewed-by: Vlastimil Babka --- include/linux/mm.h | 10 ++++++---- mm/memory.c | 7 ++++--- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index c8da64b114d1..cb29eb7360c5 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -739,8 +739,10 @@ static inline void vma_refcount_put(struct vm_area_str= uct *vma) * Try to read-lock a vma. The function is allowed to occasionally yield f= alse * locked result to avoid performance overhead, in which case we fall back= to * using mmap_lock. The function should never yield false unlocked result. + * False locked result is possible if mm_lock_seq overflows or if vma gets + * reused and attached to a different mm before we lock it. */ -static inline bool vma_start_read(struct vm_area_struct *vma) +static inline bool vma_start_read(struct mm_struct *mm, struct vm_area_str= uct *vma) { int oldcnt; =20 @@ -751,7 +753,7 @@ static inline bool vma_start_read(struct vm_area_struct= *vma) * we don't rely on for anything - the mm_lock_seq read against which we * need ordering is below. */ - if (READ_ONCE(vma->vm_lock_seq) =3D=3D READ_ONCE(vma->vm_mm->mm_lock_seq.= sequence)) + if (READ_ONCE(vma->vm_lock_seq) =3D=3D READ_ONCE(mm->mm_lock_seq.sequence= )) return false; =20 /* @@ -774,7 +776,7 @@ static inline bool vma_start_read(struct vm_area_struct= *vma) * after it has been unlocked. * This pairs with RELEASE semantics in vma_end_write_all(). */ - if (unlikely(vma->vm_lock_seq =3D=3D raw_read_seqcount(&vma->vm_mm->mm_lo= ck_seq))) { + if (unlikely(vma->vm_lock_seq =3D=3D raw_read_seqcount(&mm->mm_lock_seq))= ) { vma_refcount_put(vma); return false; } @@ -906,7 +908,7 @@ struct vm_area_struct *lock_vma_under_rcu(struct mm_str= uct *mm, #else /* CONFIG_PER_VMA_LOCK */ =20 static inline void vma_lock_init(struct vm_area_struct *vma, bool reset_re= fcnt) {} -static inline bool vma_start_read(struct vm_area_struct *vma) +static inline bool vma_start_read(struct mm_struct *mm, struct vm_area_str= uct *vma) { return false; } static inline void vma_end_read(struct vm_area_struct *vma) {} static inline void vma_start_write(struct vm_area_struct *vma) {} diff --git a/mm/memory.c b/mm/memory.c index dc16b67beefa..67cfcebb0f94 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -6426,7 +6426,7 @@ struct vm_area_struct *lock_vma_under_rcu(struct mm_s= truct *mm, if (!vma) goto inval; =20 - if (!vma_start_read(vma)) + if (!vma_start_read(mm, vma)) goto inval; =20 /* @@ -6436,8 +6436,9 @@ struct vm_area_struct *lock_vma_under_rcu(struct mm_s= truct *mm, * fields are accessible for RCU readers. */ =20 - /* Check since vm_start/vm_end might change before we lock the VMA */ - if (unlikely(address < vma->vm_start || address >=3D vma->vm_end)) + /* Check if the vma we locked is the right one. */ + if (unlikely(vma->vm_mm !=3D mm || + address < vma->vm_start || address >=3D vma->vm_end)) goto inval_end_read; =20 rcu_read_unlock(); --=20 2.47.1.613.gc27f4b7a9f-goog