Re: [PATCH 0/3] Optimize code generation during context

Xie Yuanbin
2025-10-27T15:21:30Z

I conducted a more detailed performance test on this series of patches.
https://lore.kernel.org/lkml/20251024182628.68921-1-qq570070308@gmail.com/t/#u

The data is as follows:
1. Time spent on calling finish_task_switch (unit: rdtsc):
| compiler && appended cmdline | without patches | with patches |
| clang + NA | 14.11 - 14.16 | 12.73 - 12.74 |
| clang + "spectre_v2_user=on" | 30.04 - 30.18 | 17.64 - 17.73 |
| gcc + NA | 16.73 - 16.83 | 15.35 - 15.44 |
| gcc + "spectre_v2_user=on" | 40.91 - 40.96 | 30.61 - 30.66 |

Note: I use x86 for testing here. Different architectures have different
cmdlines for configuring mitigations. For example, on arm64, spectre v2
mitigation is enabled by default, and it should be disabled by adding
"nospectre_v2" to the cmdline.

2. bzImage size:
| compiler | without patches | with patches |
| clang | 13173760 | 13173760 |
| gcc | 12166144 | 12166144 |

No size changes were found on bzImage.

Test info:
1. kernel source:
latest linux-next branch:
commit id 72fb0170ef1f45addf726319c52a0562b6913707
2. test machine:
cpu: intel i5-8300h@4Ghz
mem: DDR4 2666MHz
Bare-metal boot, non-virtualized environment
3. compiler:
gcc: gcc version 15.2.0 (Debian 15.2.0-7)
clang: Debian clang version 22.0.0 (++20250731080150+be449d6b6587-1~exp1+b1)
4. config:
base on default x86_64_defconfig, and setting:
CONFIG_PREEMPT=y
CONFIG_PREEMPT_DYNAMIC=n
CONFIG_CC_OPTIMIZE_FOR_SIZE=y
CONFIG_HZ=100
CONFIG_DEBUG_ENTRY=n
CONFIG_X86_DEBUG_FPU=n
CONFIG_EXPERT=y
CONFIG_MODIFY_LDT_SYSCALL=n
CONFIG_CGROUPS=n
CONFIG_BUG=n
CONFIG_BLK_DEV_NVME=y
5. test method:
Use rdtsc (cntvct_el0 can be use on arm64/arm) to obtain timestamps
before and after finish_task_switch calling point, and created multiple
processes to trigger context switches, then calculated the average
duration of the finish_task_switch call.
Note that using multiple processes rather than threads is recommended for
testing, because this will trigger switch_mm (where spectre v2 mitigations
may be performed) during context switching.

I put my test code here:
kernel(just for testing, not a commit):
```
diff --git a/arch/x86/entry/syscalls/syscall_64.tbl b/arch/x86/entry/syscalls/syscall_64.tbl
index ced2a1dee..9e72a4a1a 100644
--- a/arch/x86/entry/syscalls/syscall_64.tbl
+++ b/arch/x86/entry/syscalls/syscall_64.tbl
@@ -394,6 +394,7 @@
467 common open_tree_attr sys_open_tree_attr
468 common file_getattr sys_file_getattr
469 common file_setattr sys_file_setattr
+470 common mysyscall sys_mysyscall

#
# Due to a historical design error, certain syscalls are numbered differently
diff --git a/kernel/sched/core.c b/kernel/sched/core.c
index 1842285ea..bcbfea69d 100644
--- a/kernel/sched/core.c
+++ b/kernel/sched/core.c
@@ -5191,6 +5191,40 @@ asmlinkage __visible void schedule_tail(struct task_struct *prev)
calculate_sigpending();
}

+static DEFINE_PER_CPU(uint64_t, mytime);
+static DEFINE_PER_CPU(uint64_t, total_time);
+static DEFINE_PER_CPU(uint64_t, last_total_time);
+static DEFINE_PER_CPU(uint64_t, total_count);
+
+static __always_inline uint64_t myrdtsc(void)
+{
+ register uint64_t rax __asm__("rax");
+ register uint64_t rdx __asm__("rdx");
+
+ __asm__ __volatile__ ("rdtsc" : "=a"(rax), "=d"(rdx));
+ return rax | (rdx << 32);
+}
+
+static __always_inline void start_time(void)
+{
+ raw_cpu_write(mytime, myrdtsc());
+}
+
+static __always_inline void end_time(void)
+{
+ const uint64_t end_time = myrdtsc();
+ const uint64_t cost_time = end_time - raw_cpu_read(mytime);
+
+ raw_cpu_add(total_time, cost_time);
+ if (raw_cpu_inc_return(total_count) % (1 << 20) == 0) {
+ const uint64_t t = raw_cpu_read(total_time);
+ const uint64_t lt = raw_cpu_read(last_total_time);
+
+ pr_emerg("cpu %d total_time %llu, last_total_time %llu, cha : %llu\n", raw_smp_processor_id(), t, lt, t - lt);
+ raw_cpu_write(last_total_time, t);
+ }
+}
+
/*
* context_switch - switch to the new MM and the new thread's register state.
*/
@@ -5254,7 +5288,10 @@ context_switch(struct rq *rq, struct task_struct *prev,
switch_to(prev, next, prev);
barrier();

- return finish_task_switch(prev);
+ start_time();
+ rq = finish_task_switch(prev);
+ end_time();
+ return rq;
}

/*
@@ -10854,3 +10891,19 @@ void sched_change_end(struct sched_change_ctx *ctx)
p->sched_class->prio_changed(rq, p, ctx->prio);
}
}
+
+
+static struct task_struct *my_task;
+
+SYSCALL_DEFINE0(mysyscall)
+{
+ preempt_disable();
+ while (1) {
+ if (my_task)
+ wake_up_process(my_task);
+ my_task = current;
+ set_current_state(TASK_UNINTERRUPTIBLE);
+ __schedule(0);
+ }
+ return 0;
+}
```

User program:
```c
int main()
{
cpu_set_t mask;
if (fork())
sleep(1);

CPU_ZERO(&mask);
CPU_SET(5, &mask); // Assume that cpu5 exists
assert(sched_setaffinity(0, sizeof(mask), &mask) == 0);
syscall(470);
// unreachable
return 0;
}
```

Usage:
1. set core5 as isolated cpu: add "isolcpus=5" to cmdline
2. run user programe
3. wait for kernel print

Everyone is welcome to test it.

Xie Yuanbin