Skip to contentSkip to conversation

x86/ipipe: Protect TLB flushing against context switch by head domain

RFC PATCH In review Jan Kiszka 2 participants 5 messages Mar 12, 2020 – Mar 17, 2020

Subsystems (2)
lore
Share & tools
lore

Jan Kiszka

author
#0 lore raw

RFC PATCH 1/1 x86/ipipe: Protect TLB flushing against context switch by head domain

From: Jan Kiszka <[email protected]>

A Xenomai application is very rarely triggering

WARNING: CPU: 0 PID: 1997 at arch/x86/mm/tlb.c:560 [...]

(local_tlb_gen > mm_tlb_gen)

This could be triggered by loaded_mm and loaded_mm_asid becoming out of sync when flush_tlb_func_common is interrupted by the head domain to switch a real-time task right between the retrieval of both values, or maybe even after that but before writing mm_tlb_gen back to cpu_tlbstate.ctxs[loaded_mm_asid].tlb_gen.

Avoid that case by making the retrieval atomic while keeping the TLB flush interruptible. Now, there could still be interrupt during the flush. To avoid writing back to the wrong context, we first atomically check after the flush if nothing changed and only write if that is the case. That may mean another TLB flush is triggered needlessly, but that's rare and acceptable.

---

Due to the rare nature of this issue, we are not yet confident to have truly fixed it this way.

Philippe, I'm seeing some similar attempt in dovetail but it appears to me it's missing some cases. Too bad that development was forking here and information isn't flowing smoothly yet.

 arch/x86/mm/tlb.c | 33 +++++++++++++++++++++++++--------
 1 file changed, 25 insertions(+), 8 deletions(-)
arch/x86/mm/tlb.c +25 -8
index 1fcf20673982..9fbf0a5f710e 100644
@@ -518,17 +518,27 @@ static void flush_tlb_func_common(const struct flush_tlb_info *f,
 	 * - f->new_tlb_gen: the generation that the requester of the flush
 	 *                   wants us to catch up to.
 	 */
-	struct mm_struct *loaded_mm = this_cpu_read(cpu_tlbstate.loaded_mm);
-	u32 loaded_mm_asid = this_cpu_read(cpu_tlbstate.loaded_mm_asid);
-	u64 mm_tlb_gen = atomic64_read(&loaded_mm->context.tlb_gen);
-	u64 local_tlb_gen = this_cpu_read(cpu_tlbstate.ctxs[loaded_mm_asid].tlb_gen);
+	struct mm_struct *loaded_mm;
+	u32 loaded_mm_asid;
+	u64 mm_tlb_gen;
+	u64 local_tlb_gen;
 	unsigned long flags;
 
 	/* This code cannot presently handle being reentered. */
 	VM_WARN_ON(!irqs_disabled());
 
-	if (unlikely(loaded_mm == &init_mm))
+	flags = hard_cond_local_irq_save();
+
+	loaded_mm = this_cpu_read(cpu_tlbstate.loaded_mm);
+	loaded_mm_asid = this_cpu_read(cpu_tlbstate.loaded_mm_asid);
+	mm_tlb_gen = atomic64_read(&loaded_mm->context.tlb_gen);
+	loaded_mm_asid = this_cpu_read(cpu_tlbstate.loaded_mm_asid);
+	local_tlb_gen = this_cpu_read(cpu_tlbstate.ctxs[loaded_mm_asid].tlb_gen);
+
+	if (unlikely(loaded_mm == &init_mm)) {
+		hard_cond_local_irq_restore(flags);
 		return;
+	}
 
 	VM_WARN_ON(this_cpu_read(cpu_tlbstate.ctxs[loaded_mm_asid].ctx_id) !=
 		   loaded_mm->context.ctx_id);
@@ -540,13 +550,13 @@ static void flush_tlb_func_common(const struct flush_tlb_info *f,
 		 * garbage into our TLB.  Since switching to init_mm is barely
 		 * slower than a minimal flush, just switch to init_mm.
 		 */
-		flags = hard_cond_local_irq_save();
 		switch_mm_irqs_off(NULL, &init_mm, NULL);
 		hard_cond_local_irq_restore(flags);
 		return;
 	}
 
 	if (unlikely(local_tlb_gen == mm_tlb_gen)) {
+		hard_cond_local_irq_restore(flags);
 		/*
 		 * There's nothing to do: we're already up to date.  This can
 		 * happen if two concurrent flushes happen -- the first flush to
@@ -560,6 +570,8 @@ static void flush_tlb_func_common(const struct flush_tlb_info *f,
 	WARN_ON_ONCE(local_tlb_gen > mm_tlb_gen);
 	WARN_ON_ONCE(f->new_tlb_gen > mm_tlb_gen);
 
+	hard_cond_local_irq_restore(flags);
+
 	/*
 	 * If we get to this point, we know that our TLB is out of date.
 	 * This does not strictly imply that we need to flush (it's
@@ -620,8 +632,13 @@ static void flush_tlb_func_common(const struct flush_tlb_info *f,
 		trace_tlb_flush(reason, TLB_FLUSH_ALL);
 	}
 
-	/* Both paths above update our state to mm_tlb_gen. */
-	this_cpu_write(cpu_tlbstate.ctxs[loaded_mm_asid].tlb_gen, mm_tlb_gen);
+	flags = hard_cond_local_irq_save();
+	if (loaded_mm == this_cpu_read(cpu_tlbstate.loaded_mm) &&
+	    loaded_mm_asid == this_cpu_read(cpu_tlbstate.loaded_mm_asid)) {
+		/* Both paths above update our state to mm_tlb_gen. */
+		this_cpu_write(cpu_tlbstate.ctxs[loaded_mm_asid].tlb_gen, mm_tlb_gen);
+	}
+	hard_cond_local_irq_restore(flags);
 }
 
 static void flush_tlb_func_local(void *info, enum tlb_flush_reason reason)
Signature · 6 lines
2.16.4


-- 
Siemens AG, Corporate Technology, CT RDA IOT SES-DE
Corporate Competence Center Embedded Linux
#1 lore raw
Quoted 20 lines · Jan Kiszka

From: Jan Kiszka <[email protected]>

A Xenomai application is very rarely triggering

WARNING: CPU: 0 PID: 1997 at arch/x86/mm/tlb.c:560 [...]

(local_tlb_gen > mm_tlb_gen)

This could be triggered by loaded_mm and loaded_mm_asid becoming out of sync when flush_tlb_func_common is interrupted by the head domain to switch a real-time task right between the retrieval of both values, or maybe even after that but before writing mm_tlb_gen back to cpu_tlbstate.ctxs[loaded_mm_asid].tlb_gen.

Avoid that case by making the retrieval atomic while keeping the TLB flush interruptible. Now, there could still be interrupt during the flush. To avoid writing back to the wrong context, we first atomically check after the flush if nothing changed and only write if that is the case. That may mean another TLB flush is triggered needlessly, but that's rare and acceptable.

Quoted 8 lines

---

Due to the rare nature of this issue, we are not yet confident to have truly fixed it this way.

Philippe, I'm seeing some similar attempt in dovetail but it appears to me it's missing some cases.

Not "some cases", but the last one in your patch specifically if I read it correctly, which I assumed was not applicable, at least not the way I read your change, when I worked on this a year ago. This explains why that particular change is not present in the commit (3aa2fc2fb4c) you seem to have cherry picked from dovetail for the 5.x kernel series. This said, these are tricky issues, so as you hinted in your commit log, there is likely room for improvement in any case, and I may have overlooked things.

Quoted 2 lines

Too bad that development was forking here
and information isn't flowing smoothly yet.

You just demonstrated that the information is there, and that anyone can access it freely by looking at the EVL development tree. I'm sorry to hear that forking my own code for the most part in order to find a better approach for others to benefit from in the long run can be a problem. I did not find any other way to go back to the drawing board as required by the technical goals I'm pursuing with EVL, which differ from Xenomai's.

Signature · 1 line
Philippe.
author
#2 lore raw
Quoted 39 lines · Philippe Gerum
Quoted 20 lines · Jan Kiszka

From: Jan Kiszka <[email protected]>

A Xenomai application is very rarely triggering

WARNING: CPU: 0 PID: 1997 at arch/x86/mm/tlb.c:560 [...]

(local_tlb_gen > mm_tlb_gen)

This could be triggered by loaded_mm and loaded_mm_asid becoming out of sync when flush_tlb_func_common is interrupted by the head domain to switch a real-time task right between the retrieval of both values, or maybe even after that but before writing mm_tlb_gen back to cpu_tlbstate.ctxs[loaded_mm_asid].tlb_gen.

Avoid that case by making the retrieval atomic while keeping the TLB flush interruptible. Now, there could still be interrupt during the flush. To avoid writing back to the wrong context, we first atomically check after the flush if nothing changed and only write if that is the case. That may mean another TLB flush is triggered needlessly, but that's rare and acceptable.

Quoted 8 lines

---

Due to the rare nature of this issue, we are not yet confident to have truly fixed it this way.

Philippe, I'm seeing some similar attempt in dovetail but it appears to me it's missing some cases.

Not "some cases", but the last one in your patch specifically if I read it correctly, which I assumed was not applicable, at least not the way I read your change, when I worked on this a year ago. This explains why that particular change is not present in the commit (3aa2fc2fb4c) you seem to have cherry picked from dovetail for the 5.x kernel series. This said, these are tricky issues, so as you hinted in your commit log, there is likely room for improvement in any case, and I may have overlooked things.

Quoted 5 lines
Quoted 2 lines

Too bad that development was forking here
and information isn't flowing smoothly yet.

You just demonstrated that the information is there, and that anyone can access it freely by looking at the EVL development tree. I'm sorry to

It's there but it now requires polling to extract it. I suspect I will find more interesting changes once reviewing the dovetail queue completely (I already found the reverse: KVM was broken in dovetail due to incomplete forward porting; will fix when I come along the code).

Quoted 5 lines

hear that forking my own code for the most part in order to find a better approach for others to benefit from in the long run can be a problem. I did not find any other way to go back to the drawing board as required by the technical goals I'm pursuing with EVL, which differ from Xenomai's.

I've seen this with other spin-offs/rewrites/etc. of the ipipe-like kernel queue a couple of times: Even if colors and edges look differently, the core concept remains the same. Thus you also share the conceptual problems - and often also the solutions. Doing this multiple times is just wasted time. That's why we really need to get Xenomai based in dovetail for upcoming kernels so that test results and fixes flow in both directions automatically again.

Jan

Signature · 2 lines
Siemens AG, Corporate Technology, CT RDA IOT SES-DE
Corporate Competence Center Embedded Linux
#3 lore raw
Quoted 40 lines · Jan Kiszka
Quoted 39 lines · Philippe Gerum
Quoted 20 lines · Jan Kiszka

From: Jan Kiszka <[email protected]>

A Xenomai application is very rarely triggering

WARNING: CPU: 0 PID: 1997 at arch/x86/mm/tlb.c:560 [...]

(local_tlb_gen > mm_tlb_gen)

This could be triggered by loaded_mm and loaded_mm_asid becoming out of sync when flush_tlb_func_common is interrupted by the head domain to switch a real-time task right between the retrieval of both values, or maybe even after that but before writing mm_tlb_gen back to cpu_tlbstate.ctxs[loaded_mm_asid].tlb_gen.

Avoid that case by making the retrieval atomic while keeping the TLB flush interruptible. Now, there could still be interrupt during the flush. To avoid writing back to the wrong context, we first atomically check after the flush if nothing changed and only write if that is the case. That may mean another TLB flush is triggered needlessly, but that's rare and acceptable.

Quoted 8 lines

---

Due to the rare nature of this issue, we are not yet confident to have truly fixed it this way.

Philippe, I'm seeing some similar attempt in dovetail but it appears to me it's missing some cases.

Not "some cases", but the last one in your patch specifically if I read it correctly, which I assumed was not applicable, at least not the way I read your change, when I worked on this a year ago. This explains why that particular change is not present in the commit (3aa2fc2fb4c) you seem to have cherry picked from dovetail for the 5.x kernel series. This said, these are tricky issues, so as you hinted in your commit log, there is likely room for improvement in any case, and I may have overlooked things.

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

Too bad that development was forking here
and information isn't flowing smoothly yet.

You just demonstrated that the information is there, and that anyone can access it freely by looking at the EVL development tree. I'm sorry to

It's there but it now requires polling to extract it.

Yes, because I'm done with maintaining the I-pipe, and reviewing things with backports to I-pipe in mind would amount to maintaining this code.

Quoted 11 lines

I suspect I will
find more interesting changes once reviewing the dovetail queue completely (I already found the reverse: KVM was broken in dovetail due to incomplete forward porting; will fix when I come along the code).

Quoted 5 lines

hear that forking my own code for the most part in order to find a better approach for others to benefit from in the long run can be a problem. I did not find any other way to go back to the drawing board as required by the technical goals I'm pursuing with EVL, which differ from Xenomai's.

Quoted 8 lines

I've seen this with other spin-offs/rewrites/etc. of the ipipe-like kernel queue a couple of times: Even if colors and edges look differently, the core concept remains the same. Thus you also share the conceptual problems - and often also the solutions. Doing this multiple times is just wasted time. That's why we really need to get Xenomai based in dovetail for upcoming kernels so that test results and fixes flow in both directions automatically again.

I understand. I'm certainly the closest illustration of a rookie engineer, I'm not as experienced as you are, hence my stubborn tendency to do wasteful things at times. Last time was around 2001 when starting Xenomai, then a year later implementing the first version of the I-pipe. I should know better, but I can't help it, always looking for fun stuff in coding while I should be doing boring things instead, like any responsible grown-up should do.

Signature · 1 line
Philippe.
author
#4 lore raw
In reply to Jan Kiszka
Quoted 20 lines · Jan Kiszka via Xenomai

From: Jan Kiszka <[email protected]>

A Xenomai application is very rarely triggering

WARNING: CPU: 0 PID: 1997 at arch/x86/mm/tlb.c:560 [...]

(local_tlb_gen > mm_tlb_gen)

This could be triggered by loaded_mm and loaded_mm_asid becoming out of sync when flush_tlb_func_common is interrupted by the head domain to switch a real-time task right between the retrieval of both values, or maybe even after that but before writing mm_tlb_gen back to cpu_tlbstate.ctxs[loaded_mm_asid].tlb_gen.

Avoid that case by making the retrieval atomic while keeping the TLB flush interruptible. Now, there could still be interrupt during the flush. To avoid writing back to the wrong context, we first atomically check after the flush if nothing changed and only write if that is the case. That may mean another TLB flush is triggered needlessly, but that's rare and acceptable.

Quoted 5 lines

---

Due to the rare nature of this issue, we are not yet confident to have truly fixed it this way.

The positive effect of the patch has been confirmed by now (with a high confidence). Merging it therefore.

Jan

Quoted 86 lines

Philippe, I'm seeing some similar attempt in dovetail but it appears to me it's missing some cases. Too bad that development was forking here and information isn't flowing smoothly yet.

  arch/x86/mm/tlb.c | 33 +++++++++++++++++++++++++--------
  1 file changed, 25 insertions(+), 8 deletions(-)
arch/x86/mm/tlb.c +25 -8
index 1fcf20673982..9fbf0a5f710e 100644
@@ -518,17 +518,27 @@ static void flush_tlb_func_common(const struct flush_tlb_info *f,
  	 * - f->new_tlb_gen: the generation that the requester of the flush
  	 *                   wants us to catch up to.
  	 */
-	struct mm_struct *loaded_mm = this_cpu_read(cpu_tlbstate.loaded_mm);
-	u32 loaded_mm_asid = this_cpu_read(cpu_tlbstate.loaded_mm_asid);
-	u64 mm_tlb_gen = atomic64_read(&loaded_mm->context.tlb_gen);
-	u64 local_tlb_gen = this_cpu_read(cpu_tlbstate.ctxs[loaded_mm_asid].tlb_gen);
+	struct mm_struct *loaded_mm;
+	u32 loaded_mm_asid;
+	u64 mm_tlb_gen;
+	u64 local_tlb_gen;
  	unsigned long flags;
 
  	/* This code cannot presently handle being reentered. */
  	VM_WARN_ON(!irqs_disabled());
 
-	if (unlikely(loaded_mm == &init_mm))
+	flags = hard_cond_local_irq_save();
+
+	loaded_mm = this_cpu_read(cpu_tlbstate.loaded_mm);
+	loaded_mm_asid = this_cpu_read(cpu_tlbstate.loaded_mm_asid);
+	mm_tlb_gen = atomic64_read(&loaded_mm->context.tlb_gen);
+	loaded_mm_asid = this_cpu_read(cpu_tlbstate.loaded_mm_asid);
+	local_tlb_gen = this_cpu_read(cpu_tlbstate.ctxs[loaded_mm_asid].tlb_gen);
+
+	if (unlikely(loaded_mm == &init_mm)) {
+		hard_cond_local_irq_restore(flags);
  		return;
+	}
 
  	VM_WARN_ON(this_cpu_read(cpu_tlbstate.ctxs[loaded_mm_asid].ctx_id) !=
  		   loaded_mm->context.ctx_id);
@@ -540,13 +550,13 @@ static void flush_tlb_func_common(const struct flush_tlb_info *f,
  		 * garbage into our TLB.  Since switching to init_mm is barely
  		 * slower than a minimal flush, just switch to init_mm.
  		 */
-		flags = hard_cond_local_irq_save();
  		switch_mm_irqs_off(NULL, &init_mm, NULL);
  		hard_cond_local_irq_restore(flags);
  		return;
  	}
 
  	if (unlikely(local_tlb_gen == mm_tlb_gen)) {
+		hard_cond_local_irq_restore(flags);
  		/*
  		 * There's nothing to do: we're already up to date.  This can
  		 * happen if two concurrent flushes happen -- the first flush to
@@ -560,6 +570,8 @@ static void flush_tlb_func_common(const struct flush_tlb_info *f,
  	WARN_ON_ONCE(local_tlb_gen > mm_tlb_gen);
  	WARN_ON_ONCE(f->new_tlb_gen > mm_tlb_gen);
 
+	hard_cond_local_irq_restore(flags);
+
  	/*
  	 * If we get to this point, we know that our TLB is out of date.
  	 * This does not strictly imply that we need to flush (it's
@@ -620,8 +632,13 @@ static void flush_tlb_func_common(const struct flush_tlb_info *f,
  		trace_tlb_flush(reason, TLB_FLUSH_ALL);
  	}
 
-	/* Both paths above update our state to mm_tlb_gen. */
-	this_cpu_write(cpu_tlbstate.ctxs[loaded_mm_asid].tlb_gen, mm_tlb_gen);
+	flags = hard_cond_local_irq_save();
+	if (loaded_mm == this_cpu_read(cpu_tlbstate.loaded_mm) &&
+	    loaded_mm_asid == this_cpu_read(cpu_tlbstate.loaded_mm_asid)) {
+		/* Both paths above update our state to mm_tlb_gen. */
+		this_cpu_write(cpu_tlbstate.ctxs[loaded_mm_asid].tlb_gen, mm_tlb_gen);
+	}
+	hard_cond_local_irq_restore(flags);
  }
 
  static void flush_tlb_func_local(void *info, enum tlb_flush_reason reason)
Signature · 2 lines
Siemens AG, Corporate Technology, CT RDA IOT SES-DE
Corporate Competence Center Embedded Linux