Skip to contentSkip to conversation

mm: fix vma_copy for !CONFIG_PER_VMA_LOCK

PATCH In review Suren Baghdasaryan 4 participants 5 messages Dec 9, 2024 – Dec 10, 2024

Subsystems (5)
lore
Share & tools
lore

Suren Baghdasaryan

author reviewer
#0 lore raw

PATCH 1/1 mm: fix vma_copy for !CONFIG_PER_VMA_LOCK

vma_copy() function for !CONFIG_PER_VMA_LOCK configuration copies all fields using memcpy() as opposed to CONFIG_PER_VMA_LOCK version which copies only required fields. anon_vma_chain field should not be copied and new vma should instead initialize it to an empty list. Fix this by initializing anon_vma_chain inside vma_copy() function. The version of vma_copy() for CONFIG_PER_VMA_LOCK is fine since it does not change that field and anon_vma_chain of any new vma is already initialized and empty.

---
Applies over mm-unstable

 kernel/fork.c | 1 +
 1 file changed, 1 insertion(+)
kernel/fork.c +1 -0
index fec32aa06135..d532f893e977 100644
@@ -524,6 +524,7 @@ static void vma_copy(const struct vm_area_struct *src, struct vm_area_struct *de
 	 * will be reinitialized.
 	 */
 	data_race(memcpy(dest, src, sizeof(*dest)));
+	INIT_LIST_HEAD(&dest->anon_vma_chain);
 }
 
 #endif /* CONFIG_PER_VMA_LOCK */

base-commit: 6e165f54437931f329d09dca6c19d99af08a36e1

Signature · 1 line
2.47.0.338.g60cca15819-goog
Tested-by on v1 1/1
#1 lore raw
Quoted 34 lines · Suren Baghdasaryan

vma_copy() function for !CONFIG_PER_VMA_LOCK configuration copies all fields using memcpy() as opposed to CONFIG_PER_VMA_LOCK version which copies only required fields. anon_vma_chain field should not be copied and new vma should instead initialize it to an empty list. Fix this by initializing anon_vma_chain inside vma_copy() function. The version of vma_copy() for CONFIG_PER_VMA_LOCK is fine since it does not change that field and anon_vma_chain of any new vma is already initialized and empty.

---
Applies over mm-unstable

  kernel/fork.c | 1 +
  1 file changed, 1 insertion(+)
kernel/fork.c +1 -0
index fec32aa06135..d532f893e977 100644
@@ -524,6 +524,7 @@ static void vma_copy(const struct vm_area_struct *src, struct vm_area_struct *de
  	 * will be reinitialized.
  	 */
  	data_race(memcpy(dest, src, sizeof(*dest)));
+	INIT_LIST_HEAD(&dest->anon_vma_chain);
  }
 
  #endif /* CONFIG_PER_VMA_LOCK */
Quoted 1 line

base-commit: 6e165f54437931f329d09dca6c19d99af08a36e1

This fixes the issue for me.

Thanks,

#2 lore raw
In reply to Suren Baghdasaryan
Quoted 8 lines · Suren Baghdasaryan

vma_copy() function for !CONFIG_PER_VMA_LOCK configuration copies all fields using memcpy() as opposed to CONFIG_PER_VMA_LOCK version which copies only required fields. anon_vma_chain field should not be copied and new vma should instead initialize it to an empty list. Fix this by initializing anon_vma_chain inside vma_copy() function. The version of vma_copy() for CONFIG_PER_VMA_LOCK is fine since it does not change that field and anon_vma_chain of any new vma is already initialized and empty.

I'm wondering if there is sufficient reason to have two implementations to do the copying.

How expensive would it be to simply use the CONFIG_PER_VMA_LOCK variant unconditionally? Is it even measurable in fork() micro-benchmarks?

Signature · 3 lines
Cheers,

David / dhildenb
Reviewed-by on v1 1/1
#4 lore raw
In reply to Suren Baghdasaryan
Quoted 8 lines · Suren Baghdasaryan

vma_copy() function for !CONFIG_PER_VMA_LOCK configuration copies all fields using memcpy() as opposed to CONFIG_PER_VMA_LOCK version which copies only required fields. anon_vma_chain field should not be copied and new vma should instead initialize it to an empty list. Fix this by initializing anon_vma_chain inside vma_copy() function. The version of vma_copy() for CONFIG_PER_VMA_LOCK is fine since it does not change that field and anon_vma_chain of any new vma is already initialized and empty.

Yeah ouch, good spot though.

This anon_vma stuff is landmine after landmine...

Quoted 7 lines

kernel/fork.c

Quoted 10 lines

---
Applies over mm-unstable

 kernel/fork.c | 1 +
 1 file changed, 1 insertion(+)
kernel/fork.c +0 -0
index fec32aa06135..d532f893e977 100644

Unrelated to patch but it _really_ truly sucks to have this in kernel/fork.c. Maybe unavoidable given we put a bunch of this kind of logic there, but would be good to change this at some point...

kernel/fork.c static void vma_copy(const struct vm_area_struct *src, struct vm_area_struct *de · line 527

Quoted 5 lines
kernel/fork.c +1 -0
@@ -524,6 +524,7 @@ static void vma_copy(const struct vm_area_struct *src, struct vm_area_struct *de
 	 * will be reinitialized.
 	 */
 	data_race(memcpy(dest, src, sizeof(*dest)));
+	INIT_LIST_HEAD(&dest->anon_vma_chain);

Nit, but might be worth a comment here. Though I suppose it's kind of implicit.

Quoted 8 lines
kernel/fork.c +0 -0
 }
 
 #endif /* CONFIG_PER_VMA_LOCK */

base-commit: 6e165f54437931f329d09dca6c19d99af08a36e1 --
2.47.0.338.g60cca15819-goog

author reviewer
#3 lore raw
In reply to David Hildenbrand
Quoted 11 lines · David Hildenbrand
Quoted 8 lines · Suren Baghdasaryan

vma_copy() function for !CONFIG_PER_VMA_LOCK configuration copies all fields using memcpy() as opposed to CONFIG_PER_VMA_LOCK version which copies only required fields. anon_vma_chain field should not be copied and new vma should instead initialize it to an empty list. Fix this by initializing anon_vma_chain inside vma_copy() function. The version of vma_copy() for CONFIG_PER_VMA_LOCK is fine since it does not change that field and anon_vma_chain of any new vma is already initialized and empty.

Quoted 5 lines

I'm wondering if there is sufficient reason to have two implementations to do the copying.

How expensive would it be to simply use the CONFIG_PER_VMA_LOCK variant unconditionally? Is it even measurable in fork() micro-benchmarks?

Yeah, the benefit if any would be tiny. I'll try measuring the difference and if it's not visible will remove the !CONFIG_PER_VMA_LOCK version.
Thanks!

Quoted 6 lines

--
Cheers,

David / dhildenb