Skip to contentSkip to conversation

x86: Reduce duplicated code in the x86_64 context switch path.

PATCH Superseded Joe Damato 4 participants 6 messages May 13, 2013 x86

lore
Share & tools
lore

Joe Damato

author
#0 lore raw

PATCH 1/1 x86: Reduce duplicated code in the x86_64 context switch path.

---
 arch/x86/include/asm/switch_to.h |   30 ++++++++++++++++++++++++++++++
 arch/x86/kernel/process_64.c     |   29 ++---------------------------
 2 files changed, 32 insertions(+), 27 deletions(-)
arch/x86/include/asm/switch_to.h +30 -0
index 4ec45b3..a322cc6 100644
@@ -124,6 +124,36 @@ do {									\
 	       __switch_canary_iparam					  \
 	     : "memory", "cc" __EXTRA_CLOBBER)
 
+#define loadsegment_fs(fs, index)					  \
+	loadsegment(fs, index)
+
+#define loadsegment_gs(gs, index)					  \
+	load_gs_index(index)
+
+#define switch_segment(prev, next, index, seg, msr)			       \
+	do {								       \
+		/*							       \
+		 * Segment register != 0 always requires a reload.  Also       \
+		 * reload when it has changed.  When prev process used 64bit   \
+		 * base always reload to avoid an information leak.	       \
+		 */							       \
+		if (unlikely(index | next->index | prev->seg)) {	       \
+			loadsegment_##seg(seg, next->index);		       \
+			/*						       \
+			 * Check if the user used a selector != 0; if yes      \
+			 *  clear 64bit base, since overloaded base is always  \
+			 *  mapped to the Null selector			       \
+			 */						       \
+			if (index)					       \
+			  prev->seg = 0;				       \
+		}							       \
+									       \
+		/* when next process has a 64bit base use it */		       \
+		if (next->seg)						       \
+		  wrmsrl(msr, next->seg);				       \
+		prev->index = index;					       \
+	} while (0)
+
 #endif /* CONFIG_X86_32 */
 
 #endif /* _ASM_X86_SWITCH_TO_H */
arch/x86/kernel/process_64.c +2 -27
index 355ae06..f41d026 100644
@@ -318,34 +318,9 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
 
 	/*
 	 * Switch FS and GS.
-	 *
-	 * Segment register != 0 always requires a reload.  Also
-	 * reload when it has changed.  When prev process used 64bit
-	 * base always reload to avoid an information leak.
 	 */
-	if (unlikely(fsindex | next->fsindex | prev->fs)) {
-		loadsegment(fs, next->fsindex);
-		/*
-		 * Check if the user used a selector != 0; if yes
-		 *  clear 64bit base, since overloaded base is always
-		 *  mapped to the Null selector
-		 */
-		if (fsindex)
-			prev->fs = 0;
-	}
-	/* when next process has a 64bit base use it */
-	if (next->fs)
-		wrmsrl(MSR_FS_BASE, next->fs);
-	prev->fsindex = fsindex;
-
-	if (unlikely(gsindex | next->gsindex | prev->gs)) {
-		load_gs_index(next->gsindex);
-		if (gsindex)
-			prev->gs = 0;
-	}
-	if (next->gs)
-		wrmsrl(MSR_KERNEL_GS_BASE, next->gs);
-	prev->gsindex = gsindex;
+	switch_segment(prev, next, fsindex, fs, MSR_FS_BASE);
+	switch_segment(prev, next, gsindex, gs, MSR_KERNEL_GS_BASE);
 
 	switch_fpu_finish(next_p, fpu);
 
Signature · 1 line
1.7.9.5
#1 lore raw

This is a net addition in code, and send to only make it harder to understand what is happening. As such I don't think this is a good idea.

Quoted 91 lines · Joe Damato

---

arch/x86/include/asm/switch_to.h |   30 ++++++++++++++++++++++++++++++
arch/x86/kernel/process_64.c     |   29 ++---------------------------
2 files changed, 32 insertions(+), 27 deletions(-)
arch/x86/include/asm/switch_to.h +30 -0
index 4ec45b3..a322cc6 100644
@@ -124,6 +124,36 @@ do {									\
 	       __switch_canary_iparam					  \
 	     : "memory", "cc" __EXTRA_CLOBBER)
 
+#define loadsegment_fs(fs, index)					  \
+	loadsegment(fs, index)
+
+#define loadsegment_gs(gs, index)					  \
+	load_gs_index(index)
+
+#define switch_segment(prev, next, index, seg, msr)			       \
+	do {								       \
+		/*							       \
+		 * Segment register != 0 always requires a reload.  Also       \
+		 * reload when it has changed.  When prev process used 64bit   \
+		 * base always reload to avoid an information leak.	       \
+		 */							       \
+		if (unlikely(index | next->index | prev->seg)) {	       \
+			loadsegment_##seg(seg, next->index);		       \
+			/*						       \
+			 * Check if the user used a selector != 0; if yes      \
+			 *  clear 64bit base, since overloaded base is always  \
+			 *  mapped to the Null selector			       \
+			 */						       \
+			if (index)					       \
+			  prev->seg = 0;				       \
+		}							       \
+									       \
+		/* when next process has a 64bit base use it */		       \
+		if (next->seg)						       \
+		  wrmsrl(msr, next->seg);				       \
+		prev->index = index;					       \
+	} while (0)
+
 #endif /* CONFIG_X86_32 */
 
 #endif /* _ASM_X86_SWITCH_TO_H */
arch/x86/kernel/process_64.c +2 -27
index 355ae06..f41d026 100644
@@ -318,34 +318,9 @@ __switch_to(struct task_struct *prev_p, struct
 task_struct *next_p)
 
 	/*
 	 * Switch FS and GS.
-	 *
-	 * Segment register != 0 always requires a reload.  Also
-	 * reload when it has changed.  When prev process used 64bit
-	 * base always reload to avoid an information leak.
 	 */
-	if (unlikely(fsindex | next->fsindex | prev->fs)) {
-		loadsegment(fs, next->fsindex);
-		/*
-		 * Check if the user used a selector != 0; if yes
-		 *  clear 64bit base, since overloaded base is always
-		 *  mapped to the Null selector
-		 */
-		if (fsindex)
-			prev->fs = 0;
-	}
-	/* when next process has a 64bit base use it */
-	if (next->fs)
-		wrmsrl(MSR_FS_BASE, next->fs);
-	prev->fsindex = fsindex;
-
-	if (unlikely(gsindex | next->gsindex | prev->gs)) {
-		load_gs_index(next->gsindex);
-		if (gsindex)
-			prev->gs = 0;
-	}
-	if (next->gs)
-		wrmsrl(MSR_KERNEL_GS_BASE, next->gs);
-	prev->gsindex = gsindex;
+	switch_segment(prev, next, fsindex, fs, MSR_FS_BASE);
+	switch_segment(prev, next, gsindex, gs, MSR_KERNEL_GS_BASE);
 
 	switch_fpu_finish(next_p, fpu);
Signature · 1 line
Sent from my mobile phone. Please excuse brevity and lack of formatting.
author
#3 lore raw

PATCH v2 1/1 x86: Reduce duplicated code in the x86_64 context switch path.

In reply to Joe Damato
---
 arch/x86/include/asm/switch_to.h |   25 +++++++++++++++++++++++++
 arch/x86/kernel/process_64.c     |   29 ++---------------------------
 2 files changed, 27 insertions(+), 27 deletions(-)
arch/x86/include/asm/switch_to.h +25 -0
index 4ec45b3..5fd0267 100644
@@ -124,6 +124,31 @@ do {									\
 	       __switch_canary_iparam					  \
 	     : "memory", "cc" __EXTRA_CLOBBER)
 
+#define load_fs_index(index)						       \
+	loadsegment(fs, index)
+
+#define switch_segment(prev, next, index, seg, msr)			       \
+	do {								       \
+		/*							       \
+		 * Segment register != 0 always requires a reload.  Also       \
+		 * reload when it has changed.  When prev process used 64bit   \
+		 * base always reload to avoid an information leak.	       \
+		 */							       \
+		if (unlikely(index | next->index | prev->seg)) {	       \
+			load_##seg##_index(next->index);		       \
+			/*						       \
+			 * Check if the user used a selector != 0; if yes      \
+			 *  clear 64bit base, since overloaded base is always  \
+			 *  mapped to the Null selector			       \
+			 */						       \
+			if (index)					       \
+			  prev->seg = 0;				       \
+		}							       \
+		/* when next process has a 64bit base use it */		       \
+		if (next->seg)						       \
+		  wrmsrl(msr, next->seg);				       \
+		prev->index = index;					       \
+	} while (0)
 #endif /* CONFIG_X86_32 */
 
 #endif /* _ASM_X86_SWITCH_TO_H */
arch/x86/kernel/process_64.c +2 -27
index 355ae06..f41d026 100644
@@ -318,34 +318,9 @@ __switch_to(struct task_struct *prev_p, struct task_struct *next_p)
 
 	/*
 	 * Switch FS and GS.
-	 *
-	 * Segment register != 0 always requires a reload.  Also
-	 * reload when it has changed.  When prev process used 64bit
-	 * base always reload to avoid an information leak.
 	 */
-	if (unlikely(fsindex | next->fsindex | prev->fs)) {
-		loadsegment(fs, next->fsindex);
-		/*
-		 * Check if the user used a selector != 0; if yes
-		 *  clear 64bit base, since overloaded base is always
-		 *  mapped to the Null selector
-		 */
-		if (fsindex)
-			prev->fs = 0;
-	}
-	/* when next process has a 64bit base use it */
-	if (next->fs)
-		wrmsrl(MSR_FS_BASE, next->fs);
-	prev->fsindex = fsindex;
-
-	if (unlikely(gsindex | next->gsindex | prev->gs)) {
-		load_gs_index(next->gsindex);
-		if (gsindex)
-			prev->gs = 0;
-	}
-	if (next->gs)
-		wrmsrl(MSR_KERNEL_GS_BASE, next->gs);
-	prev->gsindex = gsindex;
+	switch_segment(prev, next, fsindex, fs, MSR_FS_BASE);
+	switch_segment(prev, next, gsindex, gs, MSR_KERNEL_GS_BASE);
 
 	switch_fpu_finish(next_p, fpu);
 
Signature · 1 line
1.7.9.5
author
#2 lore raw
In reply to H. Peter Anvin
Quoted 1 line · H. Peter Anvin

This is a net addition in code, and send to only make it harder to understand what is happening. As such I don't think this is a good idea.

OK, I tweaked it slightly to break even on lines of code sent a v2. I do think it would be nice to remove the duplicated code and (IMHO) it makes __switch_to easier to read.

Quoted 96 lines
Quoted 81 lines · Joe Damato

---

arch/x86/include/asm/switch_to.h |   30 ++++++++++++++++++++++++++++++
arch/x86/kernel/process_64.c     |   29 ++---------------------------
2 files changed, 32 insertions(+), 27 deletions(-)
arch/x86/include/asm/switch_to.h +30 -0
index 4ec45b3..a322cc6 100644
@@ -124,6 +124,36 @@ do {                                                                      \
             __switch_canary_iparam                                     \
           : "memory", "cc" __EXTRA_CLOBBER)
 
+#define loadsegment_fs(fs, index)                                       \
+      loadsegment(fs, index)
+
+#define loadsegment_gs(gs, index)                                       \
+      load_gs_index(index)
+
+#define switch_segment(prev, next, index, seg, msr)                          \
+      do {                                                                   \
+              /*                                                             \
+               * Segment register != 0 always requires a reload.  Also       \
+               * reload when it has changed.  When prev process used 64bit   \
+               * base always reload to avoid an information leak.            \
+               */                                                            \
+              if (unlikely(index | next->index | prev->seg)) {               \
+                      loadsegment_##seg(seg, next->index);                   \
+                      /*                                                     \
+                       * Check if the user used a selector != 0; if yes      \
+                       *  clear 64bit base, since overloaded base is always  \
+                       *  mapped to the Null selector                        \
+                       */                                                    \
+                      if (index)                                             \
+                        prev->seg = 0;                                       \
+              }                                                              \
+                                                                             \
+              /* when next process has a 64bit base use it */                \
+              if (next->seg)                                                 \
+                wrmsrl(msr, next->seg);                                      \
+              prev->index = index;                                           \
+      } while (0)
+
 #endif /* CONFIG_X86_32 */
 
 #endif /* _ASM_X86_SWITCH_TO_H */
arch/x86/kernel/process_64.c +0 -21
index 355ae06..f41d026 100644
@@ -318,34 +318,9 @@ __switch_to(struct task_struct *prev_p, struct
 task_struct *next_p)
 
      /*
       * Switch FS and GS.
-       *
-       * Segment register != 0 always requires a reload.  Also
-       * reload when it has changed.  When prev process used 64bit
-       * base always reload to avoid an information leak.
       */
-      if (unlikely(fsindex | next->fsindex | prev->fs)) {
-              loadsegment(fs, next->fsindex);
-              /*
-               * Check if the user used a selector != 0; if yes
-               *  clear 64bit base, since overloaded base is always
-               *  mapped to the Null selector
-               */
-              if (fsindex)
-                      prev->fs = 0;
-      }
-      /* when next process has a 64bit base use it */
-      if (next->fs)
-              wrmsrl(MSR_FS_BASE, next->fs);
-      prev->fsindex = fsindex;
-
-      if (unlikely(gsindex | next->gsindex | prev->gs)) {
-              load_gs_index(next->gsindex);

arch/x86/kernel/process_64.c __switch_to(struct task_struct *prev_p, struct · line 326

Quoted 10 lines
arch/x86/kernel/process_64.c +2 -6
-              if (gsindex)
-                      prev->gs = 0;
-      }
-      if (next->gs)
-              wrmsrl(MSR_KERNEL_GS_BASE, next->gs);
-      prev->gsindex = gsindex;
+      switch_segment(prev, next, fsindex, fs, MSR_FS_BASE);
+      switch_segment(prev, next, gsindex, gs, MSR_KERNEL_GS_BASE);
 
      switch_fpu_finish(next_p, fpu);

--
Sent from my mobile phone. Please excuse brevity and lack of formatting.

#4 lore raw
In reply to Joe Damato
Quoted 36 lines · Joe Damato
---
 arch/x86/include/asm/switch_to.h |   30 ++++++++++++++++++++++++++++++
 arch/x86/kernel/process_64.c     |   29 ++---------------------------
 2 files changed, 32 insertions(+), 27 deletions(-)
arch/x86/include/asm/switch_to.h +22 -0
index 4ec45b3..a322cc6 100644
@@ -124,6 +124,36 @@ do {									\
 	       __switch_canary_iparam					  \
 	     : "memory", "cc" __EXTRA_CLOBBER)
 
+#define loadsegment_fs(fs, index)					  \
+	loadsegment(fs, index)
+
+#define loadsegment_gs(gs, index)					  \
+	load_gs_index(index)
+
+#define switch_segment(prev, next, index, seg, msr)			       \
+	do {								       \
+		/*							       \
+		 * Segment register != 0 always requires a reload.  Also       \
+		 * reload when it has changed.  When prev process used 64bit   \
+		 * base always reload to avoid an information leak.	       \
+		 */							       \
+		if (unlikely(index | next->index | prev->seg)) {	       \
+			loadsegment_##seg(seg, next->index);		       \
+			/*						       \
+			 * Check if the user used a selector != 0; if yes      \
+			 *  clear 64bit base, since overloaded base is always  \
+			 *  mapped to the Null selector			       \
+			 */						       \
+			if (index)					       \
+			  prev->seg = 0;				       \

arch/x86/include/asm/switch_to.h do { \ · line 158

Quoted 10 lines
arch/x86/include/asm/switch_to.h +8 -0
+		}							       \
+									       \
+		/* when next process has a 64bit base use it */		       \
+		if (next->seg)						       \
+		  wrmsrl(msr, next->seg);				       \
+		prev->index = index;					       \
+	} while (0)
+
 #endif /* CONFIG_X86_32 */
 

For my part I'll never understand how code written as macros is supposed to improve anything. I always find it confusing and risky, as it is very easy to introduce side effects. Also, while it may reduce the source code size, it often results in increased object size.

My take: If you can not write it as inline function(s), don't bother.

Guenter

#5 lore raw
Quoted 52 lines · Guenter Roeck
Quoted 36 lines · Joe Damato
---
 arch/x86/include/asm/switch_to.h |   30 ++++++++++++++++++++++++++++++
 arch/x86/kernel/process_64.c     |   29 ++---------------------------
 2 files changed, 32 insertions(+), 27 deletions(-)
arch/x86/include/asm/switch_to.h +22 -0
index 4ec45b3..a322cc6 100644
@@ -124,6 +124,36 @@ do {									\
 	       __switch_canary_iparam					  \
 	     : "memory", "cc" __EXTRA_CLOBBER)
 
+#define loadsegment_fs(fs, index)					  \
+	loadsegment(fs, index)
+
+#define loadsegment_gs(gs, index)					  \
+	load_gs_index(index)
+
+#define switch_segment(prev, next, index, seg, msr)			       \
+	do {								       \
+		/*							       \
+		 * Segment register != 0 always requires a reload.  Also       \
+		 * reload when it has changed.  When prev process used 64bit   \
+		 * base always reload to avoid an information leak.	       \
+		 */							       \
+		if (unlikely(index | next->index | prev->seg)) {	       \
+			loadsegment_##seg(seg, next->index);		       \
+			/*						       \
+			 * Check if the user used a selector != 0; if yes      \
+			 *  clear 64bit base, since overloaded base is always  \
+			 *  mapped to the Null selector			       \
+			 */						       \
+			if (index)					       \
+			  prev->seg = 0;				       \

arch/x86/include/asm/switch_to.h do { \ · line 158

Quoted 10 lines
arch/x86/include/asm/switch_to.h +8 -0
+		}							       \
+									       \
+		/* when next process has a 64bit base use it */		       \
+		if (next->seg)						       \
+		  wrmsrl(msr, next->seg);				       \
+		prev->index = index;					       \
+	} while (0)
+
 #endif /* CONFIG_X86_32 */
 

For my part I'll never understand how code written as macros is supposed to improve anything. I always find it confusing and risky, as it is very easy to introduce side effects. Also, while it may reduce the source code size, it often results in increased object size.

Quoted 1 line

My take: If you can not write it as inline function(s), don't bother.

Indeed.

Thanks,

Ingo