Skip to contentSkip to conversation

mm: mincore: use per-vma lock during page table walk

PATCH RESEND In review Kefeng Wang 3 participants 4 messages Sep 15 – Sep 16

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH RESEND 1/1 mm: mincore: use per-vma lock during page table walk

do_mincore() performs a read-only, per-VMA residency query, making it a good candidate for per-VMA locking. Convert it to acquire the per-VMA lock, thereby reducing contention on the global mmap_lock.

---

RESEND:
- using new vma_start_read_unlocked() API, suggestted by Pedro Falcato

v1: https://lore.kernel.org/linux-mm/[email protected]/

 mm/mincore.c | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)
mm/mincore.c +15 -13
index c086836bc4bc..f989e6d1ec18 100644
@@ -235,24 +235,19 @@ static const struct mm_walk_ops mincore_walk_ops = {
 	.pmd_entry		= mincore_pte_range,
 	.pte_hole		= mincore_unmapped_range,
 	.hugetlb_entry		= mincore_hugetlb,
-	.walk_lock		= PGWALK_RDLOCK,
+	.walk_lock		= PGWALK_VMA_RDLOCK_VERIFY,
 };
 
 /*
  * Do a chunk of "sys_mincore()". We've already checked
- * all the arguments, we hold the mmap semaphore: we should
- * just return the amount of info we're asked for.
+ * all the arguments, just return the amount of info we're asked for.
  */
-static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *vec)
+static long do_mincore(struct vm_area_struct *vma, unsigned long addr,
+		unsigned long pages, unsigned char *vec)
 {
-	struct vm_area_struct *vma;
-	unsigned long end;
+	unsigned long end = min(vma->vm_end, addr + (pages << PAGE_SHIFT));
 	int err;
 
-	vma = vma_lookup(current->mm, addr);
-	if (!vma)
-		return -ENOMEM;
-	end = min(vma->vm_end, addr + (pages << PAGE_SHIFT));
 	if (!can_do_mincore(vma)) {
 		unsigned long pages = DIV_ROUND_UP(end - addr, PAGE_SIZE);
 		memset(vec, 1, pages);
@@ -319,13 +314,20 @@ SYSCALL_DEFINE3(mincore, unsigned long, start, size_t, len,
 
 	retval = 0;
 	while (pages) {
+		struct vm_area_struct *vma;
+
+		vma = vma_start_read_unlocked(current->mm, start);
+		if (!vma) {
+			retval = -ENOMEM;
+			break;
+		}
+
 		/*
 		 * Do at most PAGE_SIZE entries per iteration, due to
 		 * the temporary buffer size.
 		 */
-		mmap_read_lock(current->mm);
-		retval = do_mincore(start, min(pages, PAGE_SIZE), tmp);
-		mmap_read_unlock(current->mm);
+		retval = do_mincore(vma, start, min(pages, PAGE_SIZE), tmp);
+		vma_end_read(vma);
 
 		if (retval <= 0)
 			break;
Signature · 1 line
2.55.0
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 6 lines · Kefeng Wang

do_mincore() performs a read-only, per-VMA residency query, making it a good candidate for per-VMA locking. Convert it to acquire the per-VMA lock, thereby reducing contention on the global mmap_lock.

Looks, to my knowledge, correct. Thanks!

Signature · 1 line
Pedro
#2 lore raw
In reply to Kefeng Wang
Quoted 4 lines · Kefeng Wang

do_mincore() performs a read-only, per-VMA residency query, making it a good candidate for per-VMA locking. Convert it to acquire the per-VMA lock, thereby reducing contention on the global mmap_lock.

NIT: The mmap_lock is not global, it's per-mm.

Quoted 6 lines

---

RESEND:
- using new vma_start_read_unlocked() API, suggestted by Pedro Falcato

That's not how resends work :)

If there's a change, then it's a v2. A 'resend' is identical to its original (or at most - rebased).

This should be labelled a v2.

Quoted 14 lines

v1: https://lore.kernel.org/linux-mm/[email protected]/

 mm/mincore.c | 28 +++++++++++++++-------------
 1 file changed, 15 insertions(+), 13 deletions(-)
mm/mincore.c +0 -0
index c086836bc4bc..f989e6d1ec18 100644
@@ -235,24 +235,19 @@ static const struct mm_walk_ops mincore_walk_ops = {
 	.pmd_entry		= mincore_pte_range,
 	.pte_hole		= mincore_unmapped_range,

mm/mincore.c static const struct mm_walk_ops mincore_walk_ops = { · line 243

Quoted 10 lines
mm/mincore.c +2 -3
 	.hugetlb_entry		= mincore_hugetlb,
-	.walk_lock		= PGWALK_RDLOCK,
+	.walk_lock		= PGWALK_VMA_RDLOCK_VERIFY,
 };
 
 /*
  * Do a chunk of "sys_mincore()". We've already checked
- * all the arguments, we hold the mmap semaphore: we should
- * just return the amount of info we're asked for.
+ * all the arguments, just return the amount of info we're asked for.

The VMA read lock needs to be held here so you should say so in the comment.

mm/mincore.c static const struct mm_walk_ops mincore_walk_ops = { · line 250

Quoted 10 lines
mm/mincore.c +3 -3
  */
-static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *vec)
+static long do_mincore(struct vm_area_struct *vma, unsigned long addr,
+		unsigned long pages, unsigned char *vec)
 {
-	struct vm_area_struct *vma;
-	unsigned long end;
+	unsigned long end = min(vma->vm_end, addr + (pages << PAGE_SHIFT));
 	int err;
 

Maybe worth a:

	vma_assert_locked(vma);

?

Quoted 18 lines
mm/mincore.c +7 -4
-	vma = vma_lookup(current->mm, addr);
-	if (!vma)
-		return -ENOMEM;
-	end = min(vma->vm_end, addr + (pages << PAGE_SHIFT));
 	if (!can_do_mincore(vma)) {
 		unsigned long pages = DIV_ROUND_UP(end - addr, PAGE_SIZE);
 		memset(vec, 1, pages);
@@ -319,13 +314,20 @@ SYSCALL_DEFINE3(mincore, unsigned long, start, size_t, len,
 
 	retval = 0;
 	while (pages) {
+		struct vm_area_struct *vma;
+
+		vma = vma_start_read_unlocked(current->mm, start);
+		if (!vma) {
+			retval = -ENOMEM;
+			break;
+		}

mm/mincore.c SYSCALL_DEFINE3(mincore, unsigned long, start, size_t, len, · line 330

Quoted 10 lines
mm/mincore.c +3 -3
+
 		/*
 		 * Do at most PAGE_SIZE entries per iteration, due to
 		 * the temporary buffer size.
 		 */
-		mmap_read_lock(current->mm);
-		retval = do_mincore(start, min(pages, PAGE_SIZE), tmp);
-		mmap_read_unlock(current->mm);
+		retval = do_mincore(vma, start, min(pages, PAGE_SIZE), tmp);
+		vma_end_read(vma);

The logic looks generally right, just need to address the various bits + pieces I raised :)

Quoted 6 lines
mm/mincore.c +0 -1
 		if (retval <= 0)
 			break;
--

2.55.0

--
Cheers, Lorenzo

#3 lore raw
Quoted 15 lines · Lorenzo Stoakes (ARM)
Quoted 4 lines · Kefeng Wang

do_mincore() performs a read-only, per-VMA residency query, making it a good candidate for per-VMA locking. Convert it to acquire the per-VMA lock, thereby reducing contention on the global mmap_lock.

NIT: The mmap_lock is not global, it's per-mm.

Quoted 6 lines

---

RESEND:
- using new vma_start_read_unlocked() API, suggestted by Pedro Falcato

Quoted 6 lines

That's not how resends work :)

If there's a change, then it's a v2. A 'resend' is identical to its original (or at most - rebased).

This should be labelled a v2.

Get it, thanks for the clarification.

Quoted 20 lines
Quoted 20 lines

v1: https://lore.kernel.org/linux-mm/[email protected]/

  mm/mincore.c | 28 +++++++++++++++-------------
  1 file changed, 15 insertions(+), 13 deletions(-)
mm/mincore.c +1 -1
index c086836bc4bc..f989e6d1ec18 100644
@@ -235,24 +235,19 @@ static const struct mm_walk_ops mincore_walk_ops = {
  	.pmd_entry		= mincore_pte_range,
  	.pte_hole		= mincore_unmapped_range,
  	.hugetlb_entry		= mincore_hugetlb,
-	.walk_lock		= PGWALK_RDLOCK,
+	.walk_lock		= PGWALK_VMA_RDLOCK_VERIFY,
  };
 
Quoted 7 lines

mm/mincore.c static const struct mm_walk_ops mincore_walk_ops = { · line 243

Quoted 5 lines
mm/mincore.c +1 -2
  /*
   * Do a chunk of "sys_mincore()". We've already checked
- * all the arguments, we hold the mmap semaphore: we should
- * just return the amount of info we're asked for.
+ * all the arguments, just return the amount of info we're asked for.

The VMA read lock needs to be held here so you should say so in the comment.

ACK.

Quoted 14 lines

mm/mincore.c static const struct mm_walk_ops mincore_walk_ops = { · line 250

Quoted 11 lines
mm/mincore.c +3 -3
   */
-static long do_mincore(unsigned long addr, unsigned long pages, unsigned char *vec)
+static long do_mincore(struct vm_area_struct *vma, unsigned long addr,
+		unsigned long pages, unsigned char *vec)
  {
-	struct vm_area_struct *vma;
-	unsigned long end;
+	unsigned long end = min(vma->vm_end, addr + (pages << PAGE_SHIFT));
  	int err;
 

Maybe worth a:

Quoted 1 line
	vma_assert_locked(vma);

ACK.

Quoted 26 lines

?

Quoted 23 lines
mm/mincore.c +8 -4
-	vma = vma_lookup(current->mm, addr);
-	if (!vma)
-		return -ENOMEM;
-	end = min(vma->vm_end, addr + (pages << PAGE_SHIFT));
  	if (!can_do_mincore(vma)) {
  		unsigned long pages = DIV_ROUND_UP(end - addr, PAGE_SIZE);
  		memset(vec, 1, pages);
@@ -319,13 +314,20 @@ SYSCALL_DEFINE3(mincore, unsigned long, start, size_t, len,
 
  	retval = 0;
  	while (pages) {
+		struct vm_area_struct *vma;
+
+		vma = vma_start_read_unlocked(current->mm, start);
+		if (!vma) {
+			retval = -ENOMEM;
+			break;
+		}
+
  		/*
  		 * Do at most PAGE_SIZE entries per iteration, due to
  		 * the temporary buffer size.
  		 */
Quoted 9 lines

mm/mincore.c SYSCALL_DEFINE3(mincore, unsigned long, start, size_t, len, · line 330

Quoted 5 lines
mm/mincore.c +2 -3
-		mmap_read_lock(current->mm);
-		retval = do_mincore(start, min(pages, PAGE_SIZE), tmp);
-		mmap_read_unlock(current->mm);
+		retval = do_mincore(vma, start, min(pages, PAGE_SIZE), tmp);
+		vma_end_read(vma);

The logic looks generally right, just need to address the various bits + pieces I raised :)

Thanks for your review, will resend a v3.

Quoted 9 lines
Quoted 6 lines
mm/mincore.c +0 -1
  		if (retval <= 0)
  			break;
--

2.55.0

--
Cheers, Lorenzo