Skip to contentSkip to conversation

fork: mark memcpy() in dup_mm() with data_race()

PATCH In review Muhammad Haris Awan 4 participants 8 messages Nov 17

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

PATCH 1/1 fork: mark memcpy() in dup_mm() with data_race()

KCSAN reported a data race between dup_mm() copying oldmm via memcpy(mm, oldmm, sizeof(*mm)) and lru_gen_del_mm() modifying oldmm->lru_gen.list when an adjacent mm_struct on the shared mm_list->fifo is removed under mm_list->lock.

oldmm->lru_gen and oldmm->mmlist are protected by mm_list->lock / mmlist_lock (or updated locklessly via lru_gen_use_mm()), not oldmm->mmap_lock. Because struct mm_struct uses __randomize_layout and mm_init() immediately reinitializes these fields on the new mm, wrap the memcpy() in data_race(), matching dup_vma() in mm/vma_init.c.

Fixes bd74fdaea146 ("mm: multi-gen LRU: support page table walks")
Reported-by [email protected]
---
 kernel/fork.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/fork.c +6 -1
index 5efceb8d5..14ac9a070 100644
@@ -1539,7 +1539,12 @@ static struct mm_struct *dup_mm(struct task_struct *tsk,
 		goto fail_uprobe;
 	}
 
-	memcpy(mm, oldmm, sizeof(*mm));
+	/*
+	 * oldmm->lru_gen and oldmm->mmlist may be modified concurrently under
+	 * mm_list->lock / mmlist_lock or locklessly via lru_gen_use_mm(),
+	 * and mm_init() will immediately reinitialize them.
+	 */
+	data_race(memcpy(mm, oldmm, sizeof(*mm)));
 
 	if (!mm_init(mm, tsk)) {
 		mmap_write_unlock(oldmm);
Signature · 1 line
2.56.0
#1 lore raw
Quoted 27 lines · Muhammad Haris Awan

KCSAN reported a data race between dup_mm() copying oldmm via memcpy(mm, oldmm, sizeof(*mm)) and lru_gen_del_mm() modifying oldmm->lru_gen.list when an adjacent mm_struct on the shared mm_list->fifo is removed under mm_list->lock.

oldmm->lru_gen and oldmm->mmlist are protected by mm_list->lock / mmlist_lock (or updated locklessly via lru_gen_use_mm()), not oldmm->mmap_lock. Because struct mm_struct uses __randomize_layout and mm_init() immediately reinitializes these fields on the new mm, wrap the memcpy() in data_race(), matching dup_vma() in mm/vma_init.c.

Fixes bd74fdaea146 ("mm: multi-gen LRU: support page table walks")
Reported-by [email protected]
---
 kernel/fork.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/fork.c +0 -0
index 5efceb8d5..14ac9a070 100644
@@ -1539,7 +1539,12 @@ static struct mm_struct *dup_mm(struct task_struct *tsk,
 		goto fail_uprobe;
 	}
 

kernel/fork.c static struct mm_struct *dup_mm(struct task_struct *tsk, · line 1550

Quoted 10 lines
kernel/fork.c +6 -1
-	memcpy(mm, oldmm, sizeof(*mm));
+	/*
+	 * oldmm->lru_gen and oldmm->mmlist may be modified concurrently under
+	 * mm_list->lock / mmlist_lock or locklessly via lru_gen_use_mm(),
+	 * and mm_init() will immediately reinitialize them.
+	 */
+	data_race(memcpy(mm, oldmm, sizeof(*mm)));
 
 	if (!mm_init(mm, tsk)) {
 		mmap_write_unlock(oldmm);

I hate that lru_gen_del_mm() would have to mess with that.

Is there no way we can teach reclaim code to not mess with MMs that are being initialized?

Signature · 3 lines
Cheers,

David
#2 lore raw

Hi David,

Reclaim isn't touching the new mm being initialized (which isn't added to mm_list->fifo until lru_gen_add_mm() at the end of copy_process()).

The KCSAN read is on oldmm inside dup_mm()'s memcpy(mm, oldmm, sizeof(*mm)):
oldmm is an already-live mm sitting on mm_list->fifo, so when an adjacent exiting mm calls lru_gen_del_mm(), list_del_init() updates oldmm->lru_gen.list.next/.prev under mm_list->lock.

Since oldmm is live, lru_gen_del_mm() has to update those list pointers. The alternatives in dup_mm() would be taking mm_list->lock (plus mmlist_lock) during fork, or avoiding copying oldmm->lru_gen / oldmm->mmlist (awkward because struct mm_struct uses __randomize_layout). Wrapping the memcpy() in data_race() avoids locking on fork and matches dup_vma() in mm/vma_init.c.

Thanks,
Muhammad Haris Awan

#3 lore raw
Quoted 4 lines · Muhammad Haris Awan

Hi David,

Reclaim isn't touching the new mm being initialized (which isn't added to mm_list->fifo until lru_gen_add_mm() at the end of copy_process()).

Oh, that makes sense.

Quoted 11 lines

The KCSAN read is on oldmm inside dup_mm()'s memcpy(mm, oldmm, sizeof(*mm)):
oldmm is an already-live mm sitting on mm_list->fifo, so when an adjacent exiting mm calls lru_gen_del_mm(), list_del_init() updates oldmm->lru_gen.list.next/.prev under mm_list->lock.

Quoted 1 line

Since oldmm is live, lru_gen_del_mm() has to update those list pointers.

The alternatives in dup_mm() would be taking mm_list->lock (plus mmlist_lock) during fork, or avoiding copying oldmm->lru_gen / oldmm->mmlist (awkward because struct mm_struct uses __randomize_layout). Wrapping the memcpy() in data_race() avoids locking on fork and matches dup_vma() in mm/vma_init.c.

Yeah, the only concern I have is that this is the big hammer for any members, not just the ones where we expect races.

We hold the mmap lock in write mode, so one would assume that nothing can rally modify it. Well, besides these list exceptions.

Let's see what others have to say, but I guess this is fine then, although a bit suboptimal.

Signature · 3 lines
Cheers,

David
#4 lore raw
Quoted 17 lines · David Hildenbrand (Arm)
Quoted 4 lines · Muhammad Haris Awan

Hi David,

Reclaim isn't touching the new mm being initialized (which isn't added to mm_list->fifo until lru_gen_add_mm() at the end of copy_process()).

Oh, that makes sense.

Quoted 9 lines

The KCSAN read is on oldmm inside dup_mm()'s memcpy(mm, oldmm, sizeof(*mm)):
oldmm is an already-live mm sitting on mm_list->fifo, so when an adjacent exiting mm calls lru_gen_del_mm(), list_del_init() updates oldmm->lru_gen.list.next/.prev under mm_list->lock.

Quoted 1 line

Since oldmm is live, lru_gen_del_mm() has to update those list pointers.

The alternatives in dup_mm() would be taking mm_list->lock (plus mmlist_lock) during fork, or avoiding copying oldmm->lru_gen / oldmm->mmlist (awkward because struct mm_struct uses __randomize_layout). Wrapping the

The focus on __randomize_layout is really weird. What, were you assuming you could avoid it by using offsetof() or something? That'd be an awful solution anyway?

Quoted 2 lines
Quoted 2 lines

memcpy() in data_race() avoids locking on fork and matches dup_vma() in mm/vma_init.c.

(BTW this reads like it's an AI-generated reply... sigh.)

_My_ LLM says that lock thing wouldn't work without really fiddly changes as it relies on memcg and you'd have to double-check a concurrent change didn't take place and etc.

All to silence a once-in-4-year KCSAN report...

Quoted 9 lines

Yeah, the only concern I have is that this is the big hammer for any members, not just the ones where we expect races.

We hold the mmap lock in write mode, so one would assume that nothing can rally modify it. Well, besides these list exceptions.

Let's see what others have to say, but I guess this is fine then, although a bit suboptimal.

Replied, but this for me is far from fine, since it means that we basically no longer have any KCSAN protection against most of what fork does.

Since it's 1 report in 4 years, I think we can just live without it. And I don't really trust the submitter to not try to slop some broken alternative solution.

Quoted 5 lines

--
Cheers,

David

--
Cheers, Lorenzo

#5 lore raw

I accept the technical feedback. Silencing KCSAN on the whole dup_mm() memcpy is too broad so I'm dropping this patch.

To be honest, I used AI to help draft the messages and I should have mentioned that. I picked up this open syzbot bug because I genuinely want to contribute to Linux and open source, and I chose a rare edge case because I thought no one else would pick it.

A technical NACK is totally fine and I appreciate the review, but calling a new contributor's attempt "slop" isn't really needed to say the fix isn't good.

With respect,
Haris

#6 lore raw
In reply to Muhammad Haris Awan
Quoted 8 lines · Muhammad Haris Awan

KCSAN reported a data race between dup_mm() copying oldmm via memcpy(mm, oldmm, sizeof(*mm)) and lru_gen_del_mm() modifying oldmm->lru_gen.list when an adjacent mm_struct on the shared mm_list->fifo is removed under mm_list->lock.

oldmm->lru_gen and oldmm->mmlist are protected by mm_list->lock / mmlist_lock (or updated locklessly via lru_gen_use_mm()), not oldmm->mmap_lock. Because struct mm_struct uses __randomize_layout and

Firstly what on earth does __randomize_layout have to do with anything?

Secondly, ef694e2d2101 takes the oldmm mmap_lock so what you're saying here is wrong.

The problem is that you end up manipulating a neighbouring mm in the list.

Quoted 7 lines

mm_init() immediately reinitializes these fields on the new mm, wrap the memcpy() in data_race(), matching dup_vma() in mm/vma_init.c.

Fixes bd74fdaea146 ("mm: multi-gen LRU: support page table walks")
Reported-by [email protected]

Given you're a complete newcomer to mm I do have to ask if you used an LLM, you should mention that somewhere in the commit message or at least below the '---' by convention.

And it's vital that you understand everything you submit as per
https://docs.kernel.org/process/generated-content.html

So what work are you doing with regard to KCSAN, data races and mm lifetime that led you to this? And what expertise are you coming to it with?

Quoted 9 lines
---
 kernel/fork.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
kernel/fork.c +0 -0
index 5efceb8d5..14ac9a070 100644

kernel/fork.c static struct mm_struct *dup_mm(struct task_struct *tsk, · line 1546

Quoted 9 lines
kernel/fork.c +5 -1
@@ -1539,7 +1539,12 @@ static struct mm_struct *dup_mm(struct task_struct *tsk,
 		goto fail_uprobe;
 	}
 
-	memcpy(mm, oldmm, sizeof(*mm));
+	/*
+	 * oldmm->lru_gen and oldmm->mmlist may be modified concurrently under
+	 * mm_list->lock / mmlist_lock or locklessly via lru_gen_use_mm(),
+	 * and mm_init() will immediately reinitialize them.
+	 */

I really don't like this comment that is begging to bit rot.

kernel/fork.c static struct mm_struct *dup_mm(struct task_struct *tsk, · line 1547

Quoted 1 line
kernel/fork.c +1 -0
+	data_race(memcpy(mm, oldmm, sizeof(*mm)));

That's _really_ horrible.

That's now making it so any race that matters is ignored in a _super_ sensitive part of mm, because MGLRU decided it was a special snowflake.

But I'm confused as to why in 4 years nobody reported a KCSAN issue with this? Surely something increased the likelihood of this race?

In any case, this is MGLRU's fault. It's abusing the mm data structure and doing something it shouldn't.

So any fix should be there. Similarly for other mmlist users.

Or really, given this seems to be a one-off, spurious, KCSAN report, do nothing until it becomes clear this will be reported more than once in 4 years? :)

Quoted 6 lines
kernel/fork.c +0 -1
 	if (!mm_init(mm, tsk)) {
 		mmap_write_unlock(oldmm);
--

2.56.0

--
Cheers, Lorenzo

#7 lore raw
In reply to Muhammad Haris Awan
Quoted 24 lines · Muhammad Haris Awan

KCSAN reported a data race between dup_mm() copying oldmm via memcpy(mm, oldmm, sizeof(*mm)) and lru_gen_del_mm() modifying oldmm->lru_gen.list when an adjacent mm_struct on the shared mm_list->fifo is removed under mm_list->lock.

oldmm->lru_gen and oldmm->mmlist are protected by mm_list->lock / mmlist_lock (or updated locklessly via lru_gen_use_mm()), not oldmm->mmap_lock. Because struct mm_struct uses __randomize_layout and mm_init() immediately reinitializes these fields on the new mm, wrap the memcpy() in data_race(), matching dup_vma() in mm/vma_init.c.

Fixes bd74fdaea146 ("mm: multi-gen LRU: support page table walks")
Reported-by [email protected]

---
kernel/fork.c | 7 ++++++-

1 file changed, 6 insertions(+), 1 deletion(-)
kernel/fork.c +0 -0
index 5efceb8d5..14ac9a070 100644

kernel/fork.c static struct mm_struct *dup_mm(struct task_struct *tsk, · line 1547

Quoted 10 lines
kernel/fork.c +6 -1
@@ -1539,7 +1539,12 @@ static struct mm_struct *dup_mm(struct task_struct *tsk,
 		goto fail_uprobe;
 	}
 
-	memcpy(mm, oldmm, sizeof(*mm));
+	/*
+	 * oldmm->lru_gen and oldmm->mmlist may be modified concurrently under
+	 * mm_list->lock / mmlist_lock or locklessly via lru_gen_use_mm(),
+	 * and mm_init() will immediately reinitialize them.
+	 */
+	data_race(memcpy(mm, oldmm, sizeof(*mm)));

NAK. Your basically just ignoring the race, in fork, like Lorenzo said.

Let's fix this somewhere else?

kernel/fork.c static struct mm_struct *dup_mm(struct task_struct *tsk,

Quoted 4 lines
kernel/fork.c +0 -0
 	if (!mm_init(mm, tsk)) {
 		mmap_write_unlock(oldmm);
 

--- Thanks!