Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

selftests/seccomp: Pin benchmark to single CPU

The seccomp benchmark test (for validating the benefit of bitmaps) can
be sensitive to scheduling speed, so pin the process to a single CPU,
which appears to significantly improve reliability, and loosen the
"close enough" checking to allow up to 10% variance instead of 1%.

Reported-by: kernel test robot <oliver.sang@intel.com>
Closes: https://lore.kernel.org/oe-lkp/202402061002.3a8722fd-oliver.sang@intel.com
Cc: Andy Lutomirski <luto@amacapital.net>
Cc: Will Drewry <wad@chromium.org>
Reviewed-by: Mark Brown <broonie@kernel.org>
Signed-off-by: Kees Cook <keescook@chromium.org>

+36 -2
+36 -2
tools/testing/selftests/seccomp/seccomp_benchmark.c
··· 4 4 */ 5 5 #define _GNU_SOURCE 6 6 #include <assert.h> 7 + #include <err.h> 7 8 #include <limits.h> 9 + #include <sched.h> 8 10 #include <stdbool.h> 9 11 #include <stddef.h> 10 12 #include <stdio.h> ··· 78 76 79 77 bool approx(int i_one, int i_two) 80 78 { 81 - double one = i_one, one_bump = one * 0.01; 82 - double two = i_two, two_bump = two * 0.01; 79 + /* 80 + * This continues to be a noisy test. Instead of a 1% comparison 81 + * go with 10%. 82 + */ 83 + double one = i_one, one_bump = one * 0.1; 84 + double two = i_two, two_bump = two * 0.1; 83 85 84 86 one_bump = one + MAX(one_bump, 2.0); 85 87 two_bump = two + MAX(two_bump, 2.0); ··· 125 119 return good ? 0 : 1; 126 120 } 127 121 122 + /* Pin to a single CPU so the benchmark won't bounce around the system. */ 123 + void affinity(void) 124 + { 125 + long cpu; 126 + ulong ncores = sysconf(_SC_NPROCESSORS_CONF); 127 + cpu_set_t *setp = CPU_ALLOC(ncores); 128 + ulong setsz = CPU_ALLOC_SIZE(ncores); 129 + 130 + /* 131 + * Totally unscientific way to avoid CPUs that might be busier: 132 + * choose the highest CPU instead of the lowest. 133 + */ 134 + for (cpu = ncores - 1; cpu >= 0; cpu--) { 135 + CPU_ZERO_S(setsz, setp); 136 + CPU_SET_S(cpu, setsz, setp); 137 + if (sched_setaffinity(getpid(), setsz, setp) == -1) 138 + continue; 139 + printf("Pinned to CPU %lu of %lu\n", cpu + 1, ncores); 140 + goto out; 141 + } 142 + fprintf(stderr, "Could not set CPU affinity -- calibration may not work well"); 143 + 144 + out: 145 + CPU_FREE(setp); 146 + } 147 + 128 148 int main(int argc, char *argv[]) 129 149 { 130 150 struct sock_filter bitmap_filter[] = { ··· 184 152 /* Avoid using "sysctl" which may not be installed. */ 185 153 system("grep -H . /proc/sys/net/core/bpf_jit_enable"); 186 154 system("grep -H . /proc/sys/net/core/bpf_jit_harden"); 155 + 156 + affinity(); 187 157 188 158 if (argc > 1) 189 159 samples = strtoull(argv[1], NULL, 0);