Skip to content

Commit 3aff514

Browse files
ij-intelshuahkh
authored andcommitted
selftests/resctrl: Extend signal handler coverage to unmount on receiving signal
Unmounting resctrl FS has been moved into the per test functions in resctrl_tests.c by commit caddc0f ("selftests/resctrl: Move resctrl FS mount/umount to higher level"). In case a signal (SIGINT, SIGTERM, or SIGHUP) is received, the running selftest is aborted by ctrlc_handler() which then unmounts resctrl fs before exiting. The current section between signal_handler_register() and signal_handler_unregister(), however, does not cover the entire duration when resctrl FS is mounted. Move signal_handler_register() and signal_handler_unregister() calls from per test files into resctrl_tests.c to properly unmount resctrl fs. In order to not add signal_handler_register()/unregister() n times, create helpers test_prepare() and test_cleanup(). Do not call ksft_exit_fail_msg() in test_prepare() but only in the per test function to keep the control flow cleaner without adding calls to exit() deep into the call chain. Adjust child process kill() call in ctrlc_handler() to only be invoked if the child was already forked. Fixes: caddc0f ("selftests/resctrl: Move resctrl FS mount/umount to higher level") Signed-off-by: Ilpo Järvinen <ilpo.jarvinen@linux.intel.com> Tested-by: Shaopeng Tan <tan.shaopeng@jp.fujitsu.com> Reviewed-by: Shaopeng Tan <tan.shaopeng@jp.fujitsu.com> Reviewed-by: Reinette Chatre <reinette.chatre@intel.com> Cc: <stable@vger.kernel.org> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
1 parent beb7f47 commit 3aff514

File tree

3 files changed

+55
-44
lines changed

3 files changed

+55
-44
lines changed

tools/testing/selftests/resctrl/cat_test.c

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,6 @@ int cat_perf_miss_val(int cpu_no, int n, char *cache_type)
167167
strcpy(param.filename, RESULT_FILE_NAME1);
168168
param.num_of_runs = 0;
169169
param.cpu_no = sibling_cpu_no;
170-
} else {
171-
ret = signal_handler_register();
172-
if (ret) {
173-
kill(bm_pid, SIGKILL);
174-
goto out;
175-
}
176170
}
177171

178172
remove(param.filename);
@@ -209,10 +203,8 @@ int cat_perf_miss_val(int cpu_no, int n, char *cache_type)
209203
}
210204
close(pipefd[0]);
211205
kill(bm_pid, SIGKILL);
212-
signal_handler_unregister();
213206
}
214207

215-
out:
216208
cat_test_cleanup();
217209

218210
return ret;

tools/testing/selftests/resctrl/resctrl_tests.c

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,30 +67,54 @@ void tests_cleanup(void)
6767
cat_test_cleanup();
6868
}
6969

70-
static void run_mbm_test(const char * const *benchmark_cmd, int cpu_no)
70+
static int test_prepare(void)
7171
{
7272
int res;
7373

74-
ksft_print_msg("Starting MBM BW change ...\n");
74+
res = signal_handler_register();
75+
if (res) {
76+
ksft_print_msg("Failed to register signal handler\n");
77+
return res;
78+
}
7579

7680
res = mount_resctrlfs();
7781
if (res) {
78-
ksft_exit_fail_msg("Failed to mount resctrl FS\n");
82+
signal_handler_unregister();
83+
ksft_print_msg("Failed to mount resctrl FS\n");
84+
return res;
85+
}
86+
return 0;
87+
}
88+
89+
static void test_cleanup(void)
90+
{
91+
umount_resctrlfs();
92+
signal_handler_unregister();
93+
}
94+
95+
static void run_mbm_test(const char * const *benchmark_cmd, int cpu_no)
96+
{
97+
int res;
98+
99+
ksft_print_msg("Starting MBM BW change ...\n");
100+
101+
if (test_prepare()) {
102+
ksft_exit_fail_msg("Abnormal failure when preparing for the test\n");
79103
return;
80104
}
81105

82106
if (!validate_resctrl_feature_request(MBM_STR) || (get_vendor() != ARCH_INTEL)) {
83107
ksft_test_result_skip("Hardware does not support MBM or MBM is disabled\n");
84-
goto umount;
108+
goto cleanup;
85109
}
86110

87111
res = mbm_bw_change(cpu_no, benchmark_cmd);
88112
ksft_test_result(!res, "MBM: bw change\n");
89113
if ((get_vendor() == ARCH_INTEL) && res)
90114
ksft_print_msg("Intel MBM may be inaccurate when Sub-NUMA Clustering is enabled. Check BIOS configuration.\n");
91115

92-
umount:
93-
umount_resctrlfs();
116+
cleanup:
117+
test_cleanup();
94118
}
95119

96120
static void run_mba_test(const char * const *benchmark_cmd, int cpu_no)
@@ -99,22 +123,21 @@ static void run_mba_test(const char * const *benchmark_cmd, int cpu_no)
99123

100124
ksft_print_msg("Starting MBA Schemata change ...\n");
101125

102-
res = mount_resctrlfs();
103-
if (res) {
104-
ksft_exit_fail_msg("Failed to mount resctrl FS\n");
126+
if (test_prepare()) {
127+
ksft_exit_fail_msg("Abnormal failure when preparing for the test\n");
105128
return;
106129
}
107130

108131
if (!validate_resctrl_feature_request(MBA_STR) || (get_vendor() != ARCH_INTEL)) {
109132
ksft_test_result_skip("Hardware does not support MBA or MBA is disabled\n");
110-
goto umount;
133+
goto cleanup;
111134
}
112135

113136
res = mba_schemata_change(cpu_no, benchmark_cmd);
114137
ksft_test_result(!res, "MBA: schemata change\n");
115138

116-
umount:
117-
umount_resctrlfs();
139+
cleanup:
140+
test_cleanup();
118141
}
119142

120143
static void run_cmt_test(const char * const *benchmark_cmd, int cpu_no)
@@ -123,24 +146,23 @@ static void run_cmt_test(const char * const *benchmark_cmd, int cpu_no)
123146

124147
ksft_print_msg("Starting CMT test ...\n");
125148

126-
res = mount_resctrlfs();
127-
if (res) {
128-
ksft_exit_fail_msg("Failed to mount resctrl FS\n");
149+
if (test_prepare()) {
150+
ksft_exit_fail_msg("Abnormal failure when preparing for the test\n");
129151
return;
130152
}
131153

132154
if (!validate_resctrl_feature_request(CMT_STR)) {
133155
ksft_test_result_skip("Hardware does not support CMT or CMT is disabled\n");
134-
goto umount;
156+
goto cleanup;
135157
}
136158

137159
res = cmt_resctrl_val(cpu_no, 5, benchmark_cmd);
138160
ksft_test_result(!res, "CMT: test\n");
139161
if ((get_vendor() == ARCH_INTEL) && res)
140162
ksft_print_msg("Intel CMT may be inaccurate when Sub-NUMA Clustering is enabled. Check BIOS configuration.\n");
141163

142-
umount:
143-
umount_resctrlfs();
164+
cleanup:
165+
test_cleanup();
144166
}
145167

146168
static void run_cat_test(int cpu_no, int no_of_bits)
@@ -149,22 +171,21 @@ static void run_cat_test(int cpu_no, int no_of_bits)
149171

150172
ksft_print_msg("Starting CAT test ...\n");
151173

152-
res = mount_resctrlfs();
153-
if (res) {
154-
ksft_exit_fail_msg("Failed to mount resctrl FS\n");
174+
if (test_prepare()) {
175+
ksft_exit_fail_msg("Abnormal failure when preparing for the test\n");
155176
return;
156177
}
157178

158179
if (!validate_resctrl_feature_request(CAT_STR)) {
159180
ksft_test_result_skip("Hardware does not support CAT or CAT is disabled\n");
160-
goto umount;
181+
goto cleanup;
161182
}
162183

163184
res = cat_perf_miss_val(cpu_no, no_of_bits, "L3");
164185
ksft_test_result(!res, "CAT: test\n");
165186

166-
umount:
167-
umount_resctrlfs();
187+
cleanup:
188+
test_cleanup();
168189
}
169190

170191
int main(int argc, char **argv)

tools/testing/selftests/resctrl/resctrl_val.c

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -468,7 +468,9 @@ pid_t bm_pid, ppid;
468468

469469
void ctrlc_handler(int signum, siginfo_t *info, void *ptr)
470470
{
471-
kill(bm_pid, SIGKILL);
471+
/* Only kill child after bm_pid is set after fork() */
472+
if (bm_pid)
473+
kill(bm_pid, SIGKILL);
472474
umount_resctrlfs();
473475
tests_cleanup();
474476
ksft_print_msg("Ending\n\n");
@@ -485,6 +487,8 @@ int signal_handler_register(void)
485487
struct sigaction sigact = {};
486488
int ret = 0;
487489

490+
bm_pid = 0;
491+
488492
sigact.sa_sigaction = ctrlc_handler;
489493
sigemptyset(&sigact.sa_mask);
490494
sigact.sa_flags = SA_SIGINFO;
@@ -706,10 +710,6 @@ int resctrl_val(const char * const *benchmark_cmd, struct resctrl_val_param *par
706710

707711
ksft_print_msg("Benchmark PID: %d\n", bm_pid);
708712

709-
ret = signal_handler_register();
710-
if (ret)
711-
goto out;
712-
713713
/*
714714
* The cast removes constness but nothing mutates benchmark_cmd within
715715
* the context of this process. At the receiving process, it becomes
@@ -721,19 +721,19 @@ int resctrl_val(const char * const *benchmark_cmd, struct resctrl_val_param *par
721721
/* Taskset benchmark to specified cpu */
722722
ret = taskset_benchmark(bm_pid, param->cpu_no);
723723
if (ret)
724-
goto unregister;
724+
goto out;
725725

726726
/* Write benchmark to specified control&monitoring grp in resctrl FS */
727727
ret = write_bm_pid_to_resctrl(bm_pid, param->ctrlgrp, param->mongrp,
728728
resctrl_val);
729729
if (ret)
730-
goto unregister;
730+
goto out;
731731

732732
if (!strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)) ||
733733
!strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR))) {
734734
ret = initialize_mem_bw_imc();
735735
if (ret)
736-
goto unregister;
736+
goto out;
737737

738738
initialize_mem_bw_resctrl(param->ctrlgrp, param->mongrp,
739739
param->cpu_no, resctrl_val);
@@ -748,7 +748,7 @@ int resctrl_val(const char * const *benchmark_cmd, struct resctrl_val_param *par
748748
sizeof(pipe_message)) {
749749
perror("# failed reading message from child process");
750750
close(pipefd[0]);
751-
goto unregister;
751+
goto out;
752752
}
753753
}
754754
close(pipefd[0]);
@@ -757,7 +757,7 @@ int resctrl_val(const char * const *benchmark_cmd, struct resctrl_val_param *par
757757
if (sigqueue(bm_pid, SIGUSR1, value) == -1) {
758758
perror("# sigqueue SIGUSR1 to child");
759759
ret = errno;
760-
goto unregister;
760+
goto out;
761761
}
762762

763763
/* Give benchmark enough time to fully run */
@@ -786,8 +786,6 @@ int resctrl_val(const char * const *benchmark_cmd, struct resctrl_val_param *par
786786
}
787787
}
788788

789-
unregister:
790-
signal_handler_unregister();
791789
out:
792790
kill(bm_pid, SIGKILL);
793791

0 commit comments

Comments
 (0)