Skip to contentSkip to conversation

mm/mremap: avoid unneeded do_munmap call

PATCH Applied Miaohe Lin 2 participants 3 messages Apr 1, 2022 – Apr 2, 2022

Subsystems (2)
lore
Share & tools
lore

Miaohe Lin

author
#0 lore raw

PATCH 1/1 mm/mremap: avoid unneeded do_munmap call

When old_len == new_len, do_munmap will return -EINVAL due to len == 0. This errno will be simply ignored because of old_len != new_len check. So it is unnecessary to call do_munmap when old_len == new_len because nothing is actually done.

---
 mm/mremap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/mremap.c +2 -2
index e776d4c2345c..dd966621a056 100644
@@ -817,9 +817,9 @@ static unsigned long mremap_to(unsigned long addr, unsigned long old_len,
 			goto out;
 	}
 
-	if (old_len >= new_len) {
+	if (old_len > new_len) {
 		ret = do_munmap(mm, addr+new_len, old_len - new_len, uf_unmap);
-		if (ret && old_len != new_len)
+		if (ret)
 			goto out;
 		old_len = new_len;
 	}
Signature · 1 line
2.23.0
Acked-by on v1 1/1
#1 lore raw
Quoted 16 lines · Miaohe Lin

When old_len == new_len, do_munmap will return -EINVAL due to len == 0. This errno will be simply ignored because of old_len != new_len check. So it is unnecessary to call do_munmap when old_len == new_len because nothing is actually done.

---
 mm/mremap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/mremap.c +0 -0
index e776d4c2345c..dd966621a056 100644
@@ -817,9 +817,9 @@ static unsigned long mremap_to(unsigned long addr, unsigned long old_len,
 			goto out;

mm/mremap.c static unsigned long mremap_to(unsigned long addr, unsigned long old_len, · line 825

Quoted 10 lines
mm/mremap.c +2 -2
 	}
 
-	if (old_len >= new_len) {
+	if (old_len > new_len) {
 		ret = do_munmap(mm, addr+new_len, old_len - new_len, uf_unmap);
-		if (ret && old_len != new_len)
+		if (ret)
 			goto out;
 		old_len = new_len;
 	}

I remember stumbling over that myself a year ago or so but dig not deeper. But indeed, both variants (mmu, nommu) return -EINVAL in case len (old_len - new_len) == 0.

Maybe that used to be different before ecc1a8993751 ("do_mremap() untangling, part 2"), but it doesn't look like it.

Signature · 3 lines
Thanks,

David / dhildenb
author
#2 lore raw
Quoted 32 lines · David Hildenbrand
Quoted 16 lines · Miaohe Lin

When old_len == new_len, do_munmap will return -EINVAL due to len == 0. This errno will be simply ignored because of old_len != new_len check. So it is unnecessary to call do_munmap when old_len == new_len because nothing is actually done.

---
 mm/mremap.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
mm/mremap.c +0 -0
index e776d4c2345c..dd966621a056 100644
@@ -817,9 +817,9 @@ static unsigned long mremap_to(unsigned long addr, unsigned long old_len,
 			goto out;

mm/mremap.c static unsigned long mremap_to(unsigned long addr, unsigned long old_len, · line 825

Quoted 10 lines
mm/mremap.c +2 -2
 	}
 
-	if (old_len >= new_len) {
+	if (old_len > new_len) {
 		ret = do_munmap(mm, addr+new_len, old_len - new_len, uf_unmap);
-		if (ret && old_len != new_len)
+		if (ret)
 			goto out;
 		old_len = new_len;
 	}

I remember stumbling over that myself a year ago or so but dig not deeper. But indeed, both variants (mmu, nommu) return -EINVAL in case len (old_len - new_len) == 0.

Quoted 2 lines

Maybe that used to be different before ecc1a8993751 ("do_mremap() untangling, part 2"), but it doesn't look like it.

It could be really hard to trace back to the reason. But it's clear that this change does the same thing now and simplifies the code a bit.

Quoted 2 lines

Many thanks for your comment and Acked-by tag! Have a nice weekend! :)

Quoted 1 line