From nobody Mon Feb 9 23:38:12 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 1AE4813BC39 for ; Thu, 26 Dec 2024 17:07:15 +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=1735232837; cv=none; b=APvgG/7XzqKrqh/soarydTm0to7wZo1uxzeRz3d4kcklrf4JxK12MwiYjxNubq2bNc0G/PhJDlnUjfSxjcuFe24FdvQ9UCvfEWG4Qlo2mWEmt0Rn+AgNUQFfN2uiLqZBDZW78yKrAtprYPA5aW+u066ZkXpRs513gUqLWM02Ni8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1735232837; c=relaxed/simple; bh=fvJMYV/tbE2hwCDL6WpJtnm7Rj70ZnNeVx47h6oWiYI=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=PVJYCtSTTueQWyzGTlDczodM7OMM9S3p5Qh2hJP/YWO13KgUk8nFRNy/9mDEDgjYlUwJtvyT+jBsM07YGMRpg9vhj9zmfvVRlrmmwpycyfB7y8NfEJx3yNyq95uPzc0n/bbxHqbNUp/Ywt2Ngkds9q9umfeo1eiU8IishPo45A4= 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=whD4LKVJ; 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="whD4LKVJ" Received: by mail-pl1-f201.google.com with SMTP id d9443c01a7336-2164fad3792so79221185ad.0 for ; Thu, 26 Dec 2024 09:07:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1735232835; x=1735837635; 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=lZeoQJ48TvPY6kfbB0nyOCq9qEQQ9w4ETlSFBxsMiUM=; b=whD4LKVJS8shdMcWypXrJiWLLf/e4Dab39HWUafy7thir014v/YFXpRJ4INtjTKP9W 9w6TpLefIQ3HjpimX4Ukhw5MtqWopGxdp13YhpRufIZHVEC5RzDTNT//lztgeD+TrdA2 +PIBVPYiMdd1hHMP8yrrLEKC2Ept35uZHv+YRiVGX10zg84m1UGFHjtZoehfM80OvqBQ clqTCilpt/I2DtBqJ85GVve+sxhth92nORdpG/TAMS9KjlKIKfs+no0yx7t5eNrGnQBl Ngoahn9wILBntZGbdLbtskRHzLxEmzF4rRhRKCnMLPjcEGuZCE18hRgx4dvW4o4fOtCq VKOA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1735232835; x=1735837635; 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=lZeoQJ48TvPY6kfbB0nyOCq9qEQQ9w4ETlSFBxsMiUM=; b=U7ULicUFCSODxtgh3qegUW9RdaJvQZeB6WCyW2W3QecpxDAZNl0FJfyeslY9NCSHa8 TvHzg1+FME6+E9p0XmrJddPv0MXEB1kNE8undx6rq5L+j4Ukx/ByA6qCTaH2mk0jjrbc lFEsrBvJjTo7sTvdP6lQT6KGxj5iQxe/L+rHMrRnSyk9c6oRDewlhmdvjcx2SgEe+W1z OfhQp+YPqIX5ysCmFGLMav/MsFUd8AVtJ19RfIzSILBRGjyJytQah0mVC4nIoH7aRJLr AYCJ6p2uPBIf3vVW6SGPlwBgwvLTg877StneCPIh2JUfgvcbHyprkDeOVljoUROZEDbb WHug== X-Forwarded-Encrypted: i=1; AJvYcCXYC+ERrrRjO+CH2FoWV2B645ApLHLaJvS8OOQ0/0g41o4cdjL/5PjQMIxtAOqpTTQTxukHXQwYapprVI0=@vger.kernel.org X-Gm-Message-State: AOJu0YyUG0QWEk9BocGqNabPciqmn0iqTFO/ta5l1qcxQzpGXLUM0Pbr sTOfymtfYEzKvjOUWf1THMKcBa/BoUanca6dEoiwr3lhZfiY6uGlaeNxykCbYry9ozrf8ULlq9G DiA== X-Google-Smtp-Source: AGHT+IGWdbrm7iCFwz72a6IFmBjvmmjFFdB3W+njMZ9BDPMzH23Ov9tsL+sC5puGq870a2X6Uofv9F0zU/g= X-Received: from plty1.prod.google.com ([2002:a17:902:8641:b0:216:2f96:8ba1]) (user=surenb job=prod-delivery.src-stubby-dispatcher) by 2002:a17:902:d587:b0:216:5af7:eb2a with SMTP id d9443c01a7336-219e6ebb70bmr361013645ad.33.1735232835405; Thu, 26 Dec 2024 09:07:15 -0800 (PST) Date: Thu, 26 Dec 2024 09:06:53 -0800 In-Reply-To: <20241226170710.1159679-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: <20241226170710.1159679-1-surenb@google.com> X-Mailer: git-send-email 2.47.1.613.gc27f4b7a9f-goog Message-ID: <20241226170710.1159679-2-surenb@google.com> Subject: [PATCH v7 01/17] mm: introduce vma_start_read_locked{_nested} helpers From: Suren Baghdasaryan To: akpm@linux-foundation.org Cc: peterz@infradead.org, willy@infradead.org, liam.howlett@oracle.com, lorenzo.stoakes@oracle.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, 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" Introduce helper functions which can be used to read-lock a VMA when holding mmap_lock for read. Replace direct accesses to vma->vm_lock with these new helpers. Signed-off-by: Suren Baghdasaryan Reviewed-by: Lorenzo Stoakes Reviewed-by: Davidlohr Bueso Reviewed-by: Shakeel Butt Reviewed-by: Vlastimil Babka Reviewed-by: Liam R. Howlett --- include/linux/mm.h | 24 ++++++++++++++++++++++++ mm/userfaultfd.c | 22 +++++----------------- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 406b981af881..a48e207d25f2 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -735,6 +735,30 @@ static inline bool vma_start_read(struct vm_area_struc= t *vma) return true; } =20 +/* + * Use only while holding mmap read lock which guarantees that locking wil= l not + * fail (nobody can concurrently write-lock the vma). vma_start_read() sho= uld + * not be used in such cases because it might fail due to mm_lock_seq over= flow. + * This functionality is used to obtain vma read lock and drop the mmap re= ad lock. + */ +static inline void vma_start_read_locked_nested(struct vm_area_struct *vma= , int subclass) +{ + mmap_assert_locked(vma->vm_mm); + down_read_nested(&vma->vm_lock->lock, subclass); +} + +/* + * Use only while holding mmap read lock which guarantees that locking wil= l not + * fail (nobody can concurrently write-lock the vma). vma_start_read() sho= uld + * not be used in such cases because it might fail due to mm_lock_seq over= flow. + * This functionality is used to obtain vma read lock and drop the mmap re= ad lock. + */ +static inline void vma_start_read_locked(struct vm_area_struct *vma) +{ + mmap_assert_locked(vma->vm_mm); + down_read(&vma->vm_lock->lock); +} + static inline void vma_end_read(struct vm_area_struct *vma) { rcu_read_lock(); /* keeps vma alive till the end of up_read */ diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c index af3dfc3633db..4527c385935b 100644 --- a/mm/userfaultfd.c +++ b/mm/userfaultfd.c @@ -84,16 +84,8 @@ static struct vm_area_struct *uffd_lock_vma(struct mm_st= ruct *mm, =20 mmap_read_lock(mm); vma =3D find_vma_and_prepare_anon(mm, address); - if (!IS_ERR(vma)) { - /* - * We cannot use vma_start_read() as it may fail due to - * false locked (see comment in vma_start_read()). We - * can avoid that by directly locking vm_lock under - * mmap_lock, which guarantees that nobody can lock the - * vma for write (vma_start_write()) under us. - */ - down_read(&vma->vm_lock->lock); - } + if (!IS_ERR(vma)) + vma_start_read_locked(vma); =20 mmap_read_unlock(mm); return vma; @@ -1491,14 +1483,10 @@ static int uffd_move_lock(struct mm_struct *mm, mmap_read_lock(mm); err =3D find_vmas_mm_locked(mm, dst_start, src_start, dst_vmap, src_vmap); if (!err) { - /* - * See comment in uffd_lock_vma() as to why not using - * vma_start_read() here. - */ - down_read(&(*dst_vmap)->vm_lock->lock); + vma_start_read_locked(*dst_vmap); if (*dst_vmap !=3D *src_vmap) - down_read_nested(&(*src_vmap)->vm_lock->lock, - SINGLE_DEPTH_NESTING); + vma_start_read_locked_nested(*src_vmap, + SINGLE_DEPTH_NESTING); } mmap_read_unlock(mm); return err; --=20 2.47.1.613.gc27f4b7a9f-goog