Skip to contentSkip to conversation

mm, mmap: remove vma_merge()

PATCH In review Vlastimil Babka 4 participants 4 messages Apr 1, 2024 – Apr 2, 2024

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

PATCH 1/1 mm, mmap: remove vma_merge()

With SLAB gone I was looking for what I could delete next, and then it dawned on me - it should be vma_merge(). It's simply an unmaintainable mess.

The code is extremely hard to follow. There has been a number of recent attempts to made it more readable, by Liam, Lorenzo and myself. Almost every time this only resulted in subtle embarassing bugs [1] [2] [3] [4] [5]. For many of the bugs the only reliable testcase we have is the rpm process running in the Open Build Service. Which is worrying, as that clearly makes vma_merge() a potential supply chain attack vector for planting backdoors.

Thus simply remove vma_merge() and all the related code, and adjust all callers to take the path as if merging was attempted and failed. The only downside is that there might now be more vmas and maple tree nodes due to no merging. To extensively validate that, I've booted the kernel with virtme-ng before and after the change and checked slabinfo for the number of active objects:

before:
maple_node 1159
vm_area_struct 1538

after:
maple_node 1113
vm_area_struct 1861

Yeah there's more vma's but interestingly, less maple tree nodes. I believe it's worth it. The larger number of smaller vmas could even help with better parallelism thanks to per-vma locking. So the conclusion is obvious - we should have never tried merging vmas in the first place, hence the tags on this patch.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=d014cd7c1c35
[2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=4c6759967826
[3] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7e7757876f25
[4] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=1419430c8abb
[5] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=fc0c8f9089c2

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc <[email protected]> # CVE mandatory
Cc Lorenzo Stoakes <[email protected]>
Cc Liam R. Howlett <[email protected]>

Singed-off-by-one: Vlastimil Babka <[email protected]>

---
 mm/internal.h |   7 -
 mm/mmap.c     | 368 +++-----------------------------------------------
 mm/mremap.c   |  60 --------
 3 files changed, 17 insertions(+), 418 deletions(-)
mm/internal.h +0 -7
index 7e486f2c502c..a95bf9f33403 100644
@@ -1107,13 +1107,6 @@ struct page *follow_trans_huge_pmd(struct vm_area_struct *vma,
 				   unsigned long addr, pmd_t *pmd,
 				   unsigned int flags);
 
-/*
- * mm/mmap.c
- */
-struct vm_area_struct *vma_merge_extend(struct vma_iterator *vmi,
-					struct vm_area_struct *vma,
-					unsigned long delta);
-
 enum {
 	/* mark page accessed */
 	FOLL_TOUCH = 1 << 16,
mm/mmap.c +17 -351

423 lines not on this page · raw message

mm/mremap.c +0 -60

72 lines not on this page · raw message

Signature · 1 line
2.44.0
#1 lore raw
Quoted 39 lines · Vlastimil Babka

With SLAB gone I was looking for what I could delete next, and then it dawned on me - it should be vma_merge(). It's simply an unmaintainable mess.

The code is extremely hard to follow. There has been a number of recent attempts to made it more readable, by Liam, Lorenzo and myself. Almost every time this only resulted in subtle embarassing bugs [1] [2] [3] [4] [5]. For many of the bugs the only reliable testcase we have is the rpm process running in the Open Build Service. Which is worrying, as that clearly makes vma_merge() a potential supply chain attack vector for planting backdoors.

Thus simply remove vma_merge() and all the related code, and adjust all callers to take the path as if merging was attempted and failed. The only downside is that there might now be more vmas and maple tree nodes due to no merging. To extensively validate that, I've booted the kernel with virtme-ng before and after the change and checked slabinfo for the number of active objects:

before:
maple_node 1159
vm_area_struct 1538

after:
maple_node 1113
vm_area_struct 1861

Yeah there's more vma's but interestingly, less maple tree nodes. I believe it's worth it. The larger number of smaller vmas could even help with better parallelism thanks to per-vma locking. So the conclusion is obvious - we should have never tried merging vmas in the first place, hence the tags on this patch.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=d014cd7c1c35
[2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=4c6759967826
[3] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7e7757876f25
[4] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=1419430c8abb
[5] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=fc0c8f9089c2

Quoted 6 lines
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc <[email protected]> # CVE mandatory
Cc Lorenzo Stoakes <[email protected]>
Cc Liam R. Howlett <[email protected]>

Singed-off-by-one: Vlastimil Babka <[email protected]>
---

[snip]

This is a beautiful patch and about as perfect a clean up as one could imagine (I mean #thebestcodeisnocode right?), but as we have now eradicated VMA merging, I can't be a hypocrite and advocate for patch merging.

As a result, sadly I must:

#2 lore raw
In reply to Vlastimil Babka

Hello.

Quoted 39 lines · CEST Vlastimil Babka

With SLAB gone I was looking for what I could delete next, and then it dawned on me - it should be vma_merge(). It's simply an unmaintainable mess.

The code is extremely hard to follow. There has been a number of recent attempts to made it more readable, by Liam, Lorenzo and myself. Almost every time this only resulted in subtle embarassing bugs [1] [2] [3] [4] [5]. For many of the bugs the only reliable testcase we have is the rpm process running in the Open Build Service. Which is worrying, as that clearly makes vma_merge() a potential supply chain attack vector for planting backdoors.

Thus simply remove vma_merge() and all the related code, and adjust all callers to take the path as if merging was attempted and failed. The only downside is that there might now be more vmas and maple tree nodes due to no merging. To extensively validate that, I've booted the kernel with virtme-ng before and after the change and checked slabinfo for the number of active objects:

before:
maple_node 1159
vm_area_struct 1538

after:
maple_node 1113
vm_area_struct 1861

Yeah there's more vma's but interestingly, less maple tree nodes. I believe it's worth it. The larger number of smaller vmas could even help with better parallelism thanks to per-vma locking. So the conclusion is obvious - we should have never tried merging vmas in the first place, hence the tags on this patch.

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=d014cd7c1c35
[2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=4c6759967826
[3] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7e7757876f25
[4] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=1419430c8abb
[5] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=fc0c8f9089c2

Quoted 5 lines
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc <[email protected]> # CVE mandatory
Cc Lorenzo Stoakes <[email protected]>
Cc Liam R. Howlett <[email protected]>

Singed-off-by-one: Vlastimil Babka <[email protected]>

TYVM!

Quoted 5 lines
---
 mm/internal.h |   7 -
 mm/mmap.c     | 368 +++-----------------------------------------------
 mm/mremap.c   |  60 --------
 3 files changed, 17 insertions(+), 418 deletions(-)

/* SNIP */

Signature · 1 line
Oleksandr Natalenko (post-factum)
reviewer
#3 lore raw
In reply to Vlastimil Babka
Quoted 33 lines · Vlastimil Babka

With SLAB gone I was looking for what I could delete next, and then it dawned on me - it should be vma_merge(). It's simply an unmaintainable mess.

The code is extremely hard to follow. There has been a number of recent attempts to made it more readable, by Liam, Lorenzo and myself. Almost every time this only resulted in subtle embarassing bugs [1] [2] [3] [4] [5]. For many of the bugs the only reliable testcase we have is the rpm process running in the Open Build Service. Which is worrying, as that clearly makes vma_merge() a potential supply chain attack vector for planting backdoors.

Thus simply remove vma_merge() and all the related code, and adjust all callers to take the path as if merging was attempted and failed. The only downside is that there might now be more vmas and maple tree nodes due to no merging. To extensively validate that, I've booted the kernel with virtme-ng before and after the change and checked slabinfo for the number of active objects:

before:
maple_node 1159
vm_area_struct 1538

after:
maple_node 1113
vm_area_struct 1861

Yeah there's more vma's but interestingly, less maple tree nodes. I believe it's worth it. The larger number of smaller vmas could even help with better parallelism thanks to per-vma locking. So the conclusion is obvious - we should have never tried merging vmas in the first place, hence the tags on this patch.

Quoted 7 lines

[1] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=d014cd7c1c35
[2] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=4c6759967826
[3] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=7e7757876f25
[4] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=1419430c8abb
[5] https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=fc0c8f9089c2

Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")

This is hilarious, stable folks will be excited :)

Quoted 10 lines

Cc: <[email protected]> # CVE mandatory
Cc: Lorenzo Stoakes <[email protected]>
Cc: Liam R. Howlett <[email protected]>
Singed-off-by-one: Vlastimil Babka <[email protected]>

---
 mm/internal.h |   7 -
 mm/mmap.c     | 368 +++-----------------------------------------------
 mm/mremap.c   |  60 --------
 3 files changed, 17 insertions(+), 418 deletions(-)
Signature · 2 lines
Sincerely yours,
Mike.