Skip to contentSkip to conversation

mm/vma: the pgoff is correct if can_merge_right

PATCH Superseded Wei Yang 2 participants 5 messages Oct 24, 2024

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Wei Yang

author
#0 lore raw

PATCH 1/1 mm/vma: the pgoff is correct if can_merge_right

can_merge_right implies can_vma_merge_right() has checked the pgoff.

Don't need to assign it again.

CC Lorenzo Stoakes <[email protected]>
---
 mm/vma.c | 2 --
 1 file changed, 2 deletions(-)
mm/vma.c +0 -2
index 4737afcb064c..fb4f1863f88e 100644
@@ -915,7 +915,6 @@ struct vm_area_struct *vma_merge_new_range(struct vma_merge_struct *vmg)
 	unsigned long start = vmg->start;
 	unsigned long end = vmg->end;
 	pgoff_t pgoff = vmg->pgoff;
-	pgoff_t pglen = PHYS_PFN(end - start);
 	bool can_merge_left, can_merge_right;
 
 	mmap_assert_write_locked(vmg->mm);
@@ -936,7 +935,6 @@ struct vm_area_struct *vma_merge_new_range(struct vma_merge_struct *vmg)
 	if (can_merge_right) {
 		vmg->end = next->vm_end;
 		vmg->vma = next;
-		vmg->pgoff = next->vm_pgoff - pglen;
 	}
 
 	/* If we can merge with the previous VMA, adjust vmg accordingly. */
Signature · 1 line
2.34.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 3 lines · Wei Yang

can_merge_right implies can_vma_merge_right() has checked the pgoff.

Don't need to assign it again.

Would prefer a bigger commit message something like:

By this point can_vma_merge_right() must have returned true, which implies can_vma_merge_before() also returned true, which already asserts that the pgoff is as expected for a merge with the following VMA, thus this assignment is redundant.

Quoted 2 lines
Quoted 26 lines
CC Lorenzo Stoakes <[email protected]>
---
 mm/vma.c | 2 --
 1 file changed, 2 deletions(-)
mm/vma.c +0 -3
index 4737afcb064c..fb4f1863f88e 100644
@@ -915,7 +915,6 @@ struct vm_area_struct *vma_merge_new_range(struct vma_merge_struct *vmg)
 	unsigned long start = vmg->start;
 	unsigned long end = vmg->end;
 	pgoff_t pgoff = vmg->pgoff;
-	pgoff_t pglen = PHYS_PFN(end - start);
 	bool can_merge_left, can_merge_right;
 
 	mmap_assert_write_locked(vmg->mm);
@@ -936,7 +935,6 @@ struct vm_area_struct *vma_merge_new_range(struct vma_merge_struct *vmg)
 	if (can_merge_right) {
 		vmg->end = next->vm_end;
 		vmg->vma = next;
-		vmg->pgoff = next->vm_pgoff - pglen;
 	}
 
 	/* If we can merge with the previous VMA, adjust vmg accordingly. */
--
Quoted 3 lines

2.34.1

Thanks, nice spot!

For the purposes of explaining it on-list this is because:

static bool can_vma_merge_right(struct vma_merge_struct *vmg,
bool can_merge_left)

{
	if (!vmg->next || vmg->end != vmg->next->vm_start ||
	    !can_vma_merge_before(vmg))
		return false;
	...
}

And:

static bool can_vma_merge_before(struct vma_merge_struct *vmg)

{
	pgoff_t pglen = PHYS_PFN(vmg->end - vmg->start);
...
		if (vmg->next->vm_pgoff == vmg->pgoff + pglen)
			return true;

...
}

Which implies vmg->pgoff == vmg->next->vm_pgoff - pglen.

None of these values are changed between the check and prior assignment, so this was an entirely redundant assignment.

author
#2 lore raw
Quoted 12 lines · Lorenzo Stoakes
Quoted 3 lines · Wei Yang

can_merge_right implies can_vma_merge_right() has checked the pgoff.

Don't need to assign it again.

Would prefer a bigger commit message something like:

By this point can_vma_merge_right() must have returned true, which implies can_vma_merge_before() also returned true, which already asserts that the pgoff is as expected for a merge with the following VMA, thus this assignment is redundant.

Will change to this in next version.

Quoted 56 lines
Quoted 2 lines
Quoted 26 lines
CC Lorenzo Stoakes <[email protected]>
---
 mm/vma.c | 2 --
 1 file changed, 2 deletions(-)
mm/vma.c +0 -3
index 4737afcb064c..fb4f1863f88e 100644
@@ -915,7 +915,6 @@ struct vm_area_struct *vma_merge_new_range(struct vma_merge_struct *vmg)
 	unsigned long start = vmg->start;
 	unsigned long end = vmg->end;
 	pgoff_t pgoff = vmg->pgoff;
-	pgoff_t pglen = PHYS_PFN(end - start);
 	bool can_merge_left, can_merge_right;
 
 	mmap_assert_write_locked(vmg->mm);
@@ -936,7 +935,6 @@ struct vm_area_struct *vma_merge_new_range(struct vma_merge_struct *vmg)
 	if (can_merge_right) {
 		vmg->end = next->vm_end;
 		vmg->vma = next;
-		vmg->pgoff = next->vm_pgoff - pglen;
 	}
 
 	/* If we can merge with the previous VMA, adjust vmg accordingly. */
--
Quoted 3 lines

2.34.1

Thanks, nice spot!

For the purposes of explaining it on-list this is because:

static bool can_vma_merge_right(struct vma_merge_struct *vmg,
bool can_merge_left)

{
	if (!vmg->next || vmg->end != vmg->next->vm_start ||
	    !can_vma_merge_before(vmg))
		return false;
	...
}

And:

static bool can_vma_merge_before(struct vma_merge_struct *vmg)

{
	pgoff_t pglen = PHYS_PFN(vmg->end - vmg->start);
...
		if (vmg->next->vm_pgoff == vmg->pgoff + pglen)
			return true;
Quoted 7 lines

...
}

Which implies vmg->pgoff == vmg->next->vm_pgoff - pglen.

None of these values are changed between the check and prior assignment, so this was an entirely redundant assignment.

Do you suggest me to add this in change log?

Signature · 2 lines
Wei Yang
Help you, Help me
#3 lore raw
Quoted 8 lines · Wei Yang
Quoted 7 lines · Lorenzo Stoakes
Quoted 3 lines · Wei Yang

can_merge_right implies can_vma_merge_right() has checked the pgoff.

Don't need to assign it again.

Would prefer a bigger commit message something like:

Quoted 7 lines
Quoted 5 lines

By this point can_vma_merge_right() must have returned true, which implies can_vma_merge_before() also returned true, which already asserts that the pgoff is as expected for a merge with the following VMA, thus this assignment is redundant.

Will change to this in next version.

Thanks. The actual change itself looks good!

Quoted 60 lines
Quoted 60 lines
Quoted 3 lines
Quoted 26 lines
CC Lorenzo Stoakes <[email protected]>
---
 mm/vma.c | 2 --
 1 file changed, 2 deletions(-)
mm/vma.c +0 -3
index 4737afcb064c..fb4f1863f88e 100644
@@ -915,7 +915,6 @@ struct vm_area_struct *vma_merge_new_range(struct vma_merge_struct *vmg)
 	unsigned long start = vmg->start;
 	unsigned long end = vmg->end;
 	pgoff_t pgoff = vmg->pgoff;
-	pgoff_t pglen = PHYS_PFN(end - start);
 	bool can_merge_left, can_merge_right;
 
 	mmap_assert_write_locked(vmg->mm);
@@ -936,7 +935,6 @@ struct vm_area_struct *vma_merge_new_range(struct vma_merge_struct *vmg)
 	if (can_merge_right) {
 		vmg->end = next->vm_end;
 		vmg->vma = next;
-		vmg->pgoff = next->vm_pgoff - pglen;
 	}
 
 	/* If we can merge with the previous VMA, adjust vmg accordingly. */
--
Quoted 3 lines

2.34.1

Thanks, nice spot!

For the purposes of explaining it on-list this is because:

static bool can_vma_merge_right(struct vma_merge_struct *vmg,
bool can_merge_left)

{
	if (!vmg->next || vmg->end != vmg->next->vm_start ||
	    !can_vma_merge_before(vmg))
		return false;
	...
}

And:

static bool can_vma_merge_before(struct vma_merge_struct *vmg)

{
	pgoff_t pglen = PHYS_PFN(vmg->end - vmg->start);
...
		if (vmg->next->vm_pgoff == vmg->pgoff + pglen)
			return true;

...
}

Quoted 6 lines
Quoted 4 lines

Which implies vmg->pgoff == vmg->next->vm_pgoff - pglen.

None of these values are changed between the check and prior assignment, so this was an entirely redundant assignment.

Do you suggest me to add this in change log?

Sure, I am a big believer in putting as much detail as possible in commit messages, we definitely need to explain why we're doing this to future observers (including me... ;)

Quoted 5 lines

--
Wei Yang
Help you, Help me

author
#4 lore raw
Quoted 81 lines · Lorenzo Stoakes
Quoted 8 lines · Wei Yang
Quoted 7 lines · Lorenzo Stoakes
Quoted 3 lines · Wei Yang

can_merge_right implies can_vma_merge_right() has checked the pgoff.

Don't need to assign it again.

Would prefer a bigger commit message something like:

Quoted 7 lines
Quoted 5 lines

By this point can_vma_merge_right() must have returned true, which implies can_vma_merge_before() also returned true, which already asserts that the pgoff is as expected for a merge with the following VMA, thus this assignment is redundant.

Will change to this in next version.

Thanks. The actual change itself looks good!

Quoted 62 lines
Quoted 62 lines
Quoted 3 lines
Quoted 26 lines
CC Lorenzo Stoakes <[email protected]>
---
 mm/vma.c | 2 --
 1 file changed, 2 deletions(-)
mm/vma.c +0 -3
index 4737afcb064c..fb4f1863f88e 100644
@@ -915,7 +915,6 @@ struct vm_area_struct *vma_merge_new_range(struct vma_merge_struct *vmg)
 	unsigned long start = vmg->start;
 	unsigned long end = vmg->end;
 	pgoff_t pgoff = vmg->pgoff;
-	pgoff_t pglen = PHYS_PFN(end - start);
 	bool can_merge_left, can_merge_right;
 
 	mmap_assert_write_locked(vmg->mm);
@@ -936,7 +935,6 @@ struct vm_area_struct *vma_merge_new_range(struct vma_merge_struct *vmg)
 	if (can_merge_right) {
 		vmg->end = next->vm_end;
 		vmg->vma = next;
-		vmg->pgoff = next->vm_pgoff - pglen;
 	}
 
 	/* If we can merge with the previous VMA, adjust vmg accordingly. */
--
Quoted 3 lines

2.34.1

Thanks, nice spot!

For the purposes of explaining it on-list this is because:

static bool can_vma_merge_right(struct vma_merge_struct *vmg,
bool can_merge_left)

{
	if (!vmg->next || vmg->end != vmg->next->vm_start ||
	    !can_vma_merge_before(vmg))
		return false;
	...
}

And:

static bool can_vma_merge_before(struct vma_merge_struct *vmg)

{
	pgoff_t pglen = PHYS_PFN(vmg->end - vmg->start);
...
		if (vmg->next->vm_pgoff == vmg->pgoff + pglen)
			return true;

...
}

Which implies vmg->pgoff == vmg->next->vm_pgoff - pglen.

Quoted 9 lines
Quoted 4 lines
Quoted 2 lines

None of these values are changed between the check and prior assignment, so this was an entirely redundant assignment.

Do you suggest me to add this in change log?

Sure, I am a big believer in putting as much detail as possible in commit messages, we definitely need to explain why we're doing this to future observers (including me... ;)

Sure, will arrange to put these in change log too.

Quoted 5 lines
Quoted 5 lines

--
Wei Yang
Help you, Help me

Signature · 2 lines
Wei Yang
Help you, Help me