linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Matthew Wilcox <willy@infradead.org>
To: Suren Baghdasaryan <surenb@google.com>
Cc: Peter Xu <peterx@redhat.com>,
	"Liam R. Howlett" <Liam.Howlett@oracle.com>,
	LKML <linux-kernel@vger.kernel.org>,
	linux-mm@kvack.org, Andrew Morton <akpm@linux-foundation.org>,
	Lokesh Gidra <lokeshgidra@google.com>,
	Alistair Popple <apopple@nvidia.com>
Subject: Re: [PATCH] mm: Always sanity check anon_vma first for per-vma locks
Date: Fri, 12 Apr 2024 16:31:45 +0100	[thread overview]
Message-ID: <ZhlT4eG05mUcOQQJ@casper.infradead.org> (raw)
In-Reply-To: <ZhlQ_4Ve0vYNbWbl@casper.infradead.org>

On Fri, Apr 12, 2024 at 04:19:27PM +0100, Matthew Wilcox wrote:
> On Fri, Apr 12, 2024 at 09:53:29AM -0500, Suren Baghdasaryan wrote:
> > Unless vmf_anon_prepare() already explains why vma->anon_vma poses a
> > problem for per-vma locks, we should have an explanation there. This
> > comment would serve that purpose IMO.
> 
> I'll do you one better; here's some nice kernel-doc for
> vmd_anon_prepare():

And here's a followup patch to fix some minor issues in uffd.

 - Rename lock_vma() to uffd_lock_vma() because it really is uffd
   specific.
 - Remove comment referencing unlock_vma() which doesn't exist.
 - Fix the comment about lock_vma_under_rcu() which I just made
   incorrect.

diff --git a/mm/userfaultfd.c b/mm/userfaultfd.c
index f6267afe65d1..a32171158c38 100644
--- a/mm/userfaultfd.c
+++ b/mm/userfaultfd.c
@@ -56,17 +56,16 @@ struct vm_area_struct *find_vma_and_prepare_anon(struct mm_struct *mm,
 
 #ifdef CONFIG_PER_VMA_LOCK
 /*
- * lock_vma() - Lookup and lock vma corresponding to @address.
+ * uffd_lock_vma() - Lookup and lock vma corresponding to @address.
  * @mm: mm to search vma in.
  * @address: address that the vma should contain.
  *
- * Should be called without holding mmap_lock. vma should be unlocked after use
- * with unlock_vma().
+ * Should be called without holding mmap_lock.
  *
  * Return: A locked vma containing @address, -ENOENT if no vma is found, or
  * -ENOMEM if anon_vma couldn't be allocated.
  */
-static struct vm_area_struct *lock_vma(struct mm_struct *mm,
+static struct vm_area_struct *uffd_lock_vma(struct mm_struct *mm,
 				       unsigned long address)
 {
 	struct vm_area_struct *vma;
@@ -74,9 +73,8 @@ static struct vm_area_struct *lock_vma(struct mm_struct *mm,
 	vma = lock_vma_under_rcu(mm, address);
 	if (vma) {
 		/*
-		 * lock_vma_under_rcu() only checks anon_vma for private
-		 * anonymous mappings. But we need to ensure it is assigned in
-		 * private file-backed vmas as well.
+		 * We know we're going to need to use anon_vma, so check
+		 * that early.
 		 */
 		if (!(vma->vm_flags & VM_SHARED) && unlikely(!vma->anon_vma))
 			vma_end_read(vma);
@@ -107,7 +105,7 @@ static struct vm_area_struct *uffd_mfill_lock(struct mm_struct *dst_mm,
 {
 	struct vm_area_struct *dst_vma;
 
-	dst_vma = lock_vma(dst_mm, dst_start);
+	dst_vma = uffd_lock_vma(dst_mm, dst_start);
 	if (IS_ERR(dst_vma) || validate_dst_vma(dst_vma, dst_start + len))
 		return dst_vma;
 
@@ -1437,7 +1435,7 @@ static int uffd_move_lock(struct mm_struct *mm,
 	struct vm_area_struct *vma;
 	int err;
 
-	vma = lock_vma(mm, dst_start);
+	vma = uffd_lock_vma(mm, dst_start);
 	if (IS_ERR(vma))
 		return PTR_ERR(vma);
 
@@ -1452,7 +1450,7 @@ static int uffd_move_lock(struct mm_struct *mm,
 	}
 
 	/*
-	 * Using lock_vma() to get src_vma can lead to following deadlock:
+	 * Using uffd_lock_vma() to get src_vma can lead to following deadlock:
 	 *
 	 * Thread1				Thread2
 	 * -------				-------
@@ -1474,7 +1472,7 @@ static int uffd_move_lock(struct mm_struct *mm,
 	err = find_vmas_mm_locked(mm, dst_start, src_start, dst_vmap, src_vmap);
 	if (!err) {
 		/*
-		 * See comment in lock_vma() as to why not using
+		 * See comment in uffd_lock_vma() as to why not using
 		 * vma_start_read() here.
 		 */
 		down_read(&(*dst_vmap)->vm_lock->lock);


  reply	other threads:[~2024-04-12 15:31 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-04-10 17:06 Peter Xu
2024-04-10 20:26 ` Matthew Wilcox
     [not found]   ` <Zhb6B8UsidEEbFu3@x1n>
2024-04-10 21:10     ` Matthew Wilcox
2024-04-10 21:23       ` Peter Xu
2024-04-10 23:59         ` Matthew Wilcox
2024-04-11  0:20           ` Peter Xu
2024-04-11 14:50             ` Matthew Wilcox
2024-04-11 15:34               ` Peter Xu
2024-04-11 17:14                 ` Matthew Wilcox
2024-04-11 15:42   ` Suren Baghdasaryan
2024-04-11 17:13 ` Liam R. Howlett
     [not found]   ` <ZhhSItiyLYBEdAX3@x1n>
2024-04-11 21:27     ` Matthew Wilcox
2024-04-11 21:46       ` Peter Xu
2024-04-11 22:02         ` Matthew Wilcox
2024-04-12  3:14           ` Matthew Wilcox
2024-04-12 12:38             ` Peter Xu
2024-04-12 13:06               ` Suren Baghdasaryan
2024-04-12 14:16                 ` Matthew Wilcox
2024-04-12 14:53                   ` Suren Baghdasaryan
2024-04-12 15:19                     ` Matthew Wilcox
2024-04-12 15:31                       ` Matthew Wilcox [this message]
2024-04-13 21:46                         ` Suren Baghdasaryan
2024-04-13 22:52                           ` Matthew Wilcox
2024-04-13 23:11                             ` Suren Baghdasaryan
2024-04-13 21:41                       ` Suren Baghdasaryan
2024-04-13 22:46                         ` Matthew Wilcox
2024-04-15 15:58                       ` Suren Baghdasaryan
2024-04-15 16:13                         ` Matthew Wilcox
2024-04-15 16:19                           ` Suren Baghdasaryan
2024-04-15 16:26                             ` Matthew Wilcox
2024-04-12 12:46             ` Suren Baghdasaryan
2024-04-12 13:32               ` Matthew Wilcox
2024-04-12 13:46                 ` Suren Baghdasaryan
2024-04-26 14:00             ` Matthew Wilcox
2024-04-26 15:07               ` Suren Baghdasaryan
2024-04-26 15:28                 ` Matthew Wilcox
2024-04-26 15:32                   ` Suren Baghdasaryan
2024-04-26 15:50                     ` Matthew Wilcox
2024-04-26 15:32                 ` Liam R. Howlett

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=ZhlT4eG05mUcOQQJ@casper.infradead.org \
    --to=willy@infradead.org \
    --cc=Liam.Howlett@oracle.com \
    --cc=akpm@linux-foundation.org \
    --cc=apopple@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=lokeshgidra@google.com \
    --cc=peterx@redhat.com \
    --cc=surenb@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox