Skip to contentSkip to conversation

selftests/resctrl: Fixes to error handling logic

PATCH Superseded Ilpo Järvinen 2 participants 8 messages Feb 8, 2023 – Feb 14, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover selftests/resctrl: Fixes to error handling logic

This series fixes a few cleanup/error handling problems.

Fenghua Yu (1):
selftests/resctrl: Return error if memory is not allocated

Ilpo Järvinen (3):
selftests/resctrl: Move ->setup() call outside of test specific
branches
selftests/resctrl: Allow ->setup() to return errors selftests/resctrl: Check for return value after write_schemata()

 tools/testing/selftests/resctrl/cache.c       |  4 +++-
 tools/testing/selftests/resctrl/cat_test.c    |  2 +-
 tools/testing/selftests/resctrl/cmt_test.c    |  2 +-
 tools/testing/selftests/resctrl/fill_buf.c    |  2 ++
 tools/testing/selftests/resctrl/mba_test.c    |  9 ++++++--
 tools/testing/selftests/resctrl/mbm_test.c    |  2 +-
 tools/testing/selftests/resctrl/resctrl.h     |  2 ++
 tools/testing/selftests/resctrl/resctrl_val.c | 21 +++++++------------
 8 files changed, 25 insertions(+), 19 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH 1/4 selftests/resctrl: Return error if memory is not allocated

From: Fenghua Yu <[email protected]>

malloc_and_init_memory() in fill_buf isn't checking if memalign() successfully allocated memory or not before accessing the memory.

Check the return value of memalign() and return NULL if allocating aligned memory fails.

Fixes a2561b12fe39 ("selftests/resctrl: Add built in benchmark")
---
 tools/testing/selftests/resctrl/fill_buf.c | 2 ++
 1 file changed, 2 insertions(+)
tools/testing/selftests/resctrl/fill_buf.c +2 -0
index 56ccbeae0638..f4880c962ec4 100644
@@ -68,6 +68,8 @@ static void *malloc_and_init_memory(size_t s)
 	size_t s64;
 
 	void *p = memalign(PAGE_SIZE, s);
+	if (!p)
+		return p;
 
 	p64 = (uint64_t *)p;
 	s64 = s / sizeof(uint64_t);
Signature · 1 line
2.30.2
maintainer
#2 lore raw

Re: [PATCH 1/4] selftests/resctrl: Return error if memory is not allocated

Hi Ilpo,

I do not see a why two patch series are needed for the resctrl fixes. It may make it easier for everybody if it is handled as one patch series (with fixes first)?

Quoted 10 lines · Ilpo Järvinen

From: Fenghua Yu <[email protected]>

malloc_and_init_memory() in fill_buf isn't checking if memalign() successfully allocated memory or not before accessing the memory.

Check the return value of memalign() and return NULL if allocating aligned memory fails.

Fixes a2561b12fe39 ("selftests/resctrl: Add built in benchmark")

Missing your Signed-off-by?

tools/testing/selftests/resctrl/fill_buf.c static void *malloc_and_init_memory(size_t s) · line 70

Quoted 12 lines
---
 tools/testing/selftests/resctrl/fill_buf.c | 2 ++
 1 file changed, 2 insertions(+)
tools/testing/selftests/resctrl/fill_buf.c +0 -0
index 56ccbeae0638..f4880c962ec4 100644
@@ -68,6 +68,8 @@ static void *malloc_and_init_memory(size_t s)
 	size_t s64;
 
 	void *p = memalign(PAGE_SIZE, s);

This may also be a good time to stop using an obsolete call?

tools/testing/selftests/resctrl/fill_buf.c static void *malloc_and_init_memory(size_t s) · line 72

Quoted 2 lines
tools/testing/selftests/resctrl/fill_buf.c +2 -0
+	if (!p)
+		return p;

Could you please return NULL explicitly?

tools/testing/selftests/resctrl/fill_buf.c static void *malloc_and_init_memory(size_t s) · line 75

Quoted 3 lines
tools/testing/selftests/resctrl/fill_buf.c +0 -0
 	p64 = (uint64_t *)p;
 	s64 = s / sizeof(uint64_t);

Reinette

#3 lore raw

Re: [PATCH 1/4] selftests/resctrl: Return error if memory is not allocated

Quoted 3 lines · Reinette Chatre

I do not see a why two patch series are needed for the resctrl fixes. It may make it easier for everybody if it is handled as one patch series (with fixes first)?

Ok, I can put the fixes and cleanups into one series.

Quoted 6 lines · Ilpo Järvinen
Quoted 5 lines

From: Fenghua Yu <[email protected]>

malloc_and_init_memory() in fill_buf isn't checking if memalign() successfully allocated memory or not before accessing the memory.

Quoted 7 lines
Quoted 5 lines

Check the return value of memalign() and return NULL if allocating aligned memory fails.

Fixes a2561b12fe39 ("selftests/resctrl: Add built in benchmark")

Missing your Signed-off-by?

These were intentionally. When I didn't modify the original patch at all during forward porting it, I just kept the original From and SoB as is. But from the doc you pointed me to, I see now x86 wants also handlers sobs.

Quoted 11 lines
Quoted 11 lines
---
 tools/testing/selftests/resctrl/fill_buf.c | 2 ++
 1 file changed, 2 insertions(+)
tools/testing/selftests/resctrl/fill_buf.c +0 -0
index 56ccbeae0638..f4880c962ec4 100644
@@ -68,6 +68,8 @@ static void *malloc_and_init_memory(size_t s)
 	size_t s64;
 
Quoted 3 lines

tools/testing/selftests/resctrl/fill_buf.c static void *malloc_and_init_memory(size_t s) · line 70

Quoted 1 line
tools/testing/selftests/resctrl/fill_buf.c +0 -0
 	void *p = memalign(PAGE_SIZE, s);

This may also be a good time to stop using an obsolete call?

Sure, I can add another patch to change that to posix_memalign().

Quoted 4 lines

tools/testing/selftests/resctrl/fill_buf.c static void *malloc_and_init_memory(size_t s) · line 72

Quoted 2 lines
tools/testing/selftests/resctrl/fill_buf.c +2 -0
+	if (!p)
+		return p;

Could you please return NULL explicitly?

I'll change it.

Thanks for you comments.

Signature · 1 line
 i.
maintainer
#4 lore raw

Re: [PATCH 1/4] selftests/resctrl: Return error if memory is not allocated

Hi Ilpo,

Quoted 8 lines · Ilpo Järvinen
Quoted 1 line · Reinette Chatre

Missing your Signed-off-by?

These were intentionally. When I didn't modify the original patch at all during forward porting it, I just kept the original From and SoB as is. But from the doc you pointed me to, I see now x86 wants also handlers sobs.

I do not think this is x86 specific. Documentation/process/submitting-patches.rst states:
"Any further SoBs (Signed-off-by:'s) following the author's SoB are from people handling and transporting the patch, but were not involved in its development. SoB chains should reflect the **real** route a patch took as it was propagated to the maintainers and ultimately to Linus, with the first SoB entry signalling primary authorship of a single author."

Quoted 12 lines
Quoted 12 lines
Quoted 12 lines
---
 tools/testing/selftests/resctrl/fill_buf.c | 2 ++
 1 file changed, 2 insertions(+)
tools/testing/selftests/resctrl/fill_buf.c +0 -0
index 56ccbeae0638..f4880c962ec4 100644
@@ -68,6 +68,8 @@ static void *malloc_and_init_memory(size_t s)
 	size_t s64;
 
Quoted 5 lines
Quoted 3 lines

tools/testing/selftests/resctrl/fill_buf.c static void *malloc_and_init_memory(size_t s) · line 70

Quoted 1 line
tools/testing/selftests/resctrl/fill_buf.c +0 -0
 	void *p = memalign(PAGE_SIZE, s);

This may also be a good time to stop using an obsolete call?

Sure, I can add another patch to change that to posix_memalign().

You can also consider aligned_alloc().

Reinette

#5 lore raw

PATCH 2/4 selftests/resctrl: Move ->setup() call outside of test specific branches

In reply to Ilpo Järvinen

resctrl_val() function is called only by mbm, mba, and cmt tests which means the else branch is never used and can be removed.

Presently, both of the the test branches call param->setup(). Thus, the call can be placed outside of the test specific branches reducing code duplication.

---
 tools/testing/selftests/resctrl/resctrl_val.c | 19 ++++++-------------
 1 file changed, 6 insertions(+), 13 deletions(-)
tools/testing/selftests/resctrl/resctrl_val.c +6 -13
index b32b96356ec7..787546a52849 100644
@@ -734,29 +734,22 @@ int resctrl_val(char **benchmark_cmd, struct resctrl_val_param *param)
 
 	/* Test runs until the callback setup() tells the test to stop. */
 	while (1) {
+		ret = param->setup(1, param);
+		if (ret) {
+			ret = 0;
+			break;
+		}
+
 		if (!strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)) ||
 		    !strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR))) {
-			ret = param->setup(1, param);
-			if (ret) {
-				ret = 0;
-				break;
-			}
-
 			ret = measure_vals(param, &bw_resc_start);
 			if (ret)
 				break;
 		} else if (!strncmp(resctrl_val, CMT_STR, sizeof(CMT_STR))) {
-			ret = param->setup(1, param);
-			if (ret) {
-				ret = 0;
-				break;
-			}
 			sleep(1);
 			ret = measure_cache_vals(param, bm_pid);
 			if (ret)
 				break;
-		} else {
-			break;
 		}
 	}
 
Signature · 1 line
2.30.2
#6 lore raw

PATCH 3/4 selftests/resctrl: Allow ->setup() to return errors

In reply to Ilpo Järvinen

Currently resctrl_val() assumes ->setup() always returns either 0 to continue tests or < 0 in case of the normal termination of tests after x runs. The latter overlaps with normal error returns.

Define END_OF_TESTS (=1) to differentiate the normal termination of tests and return errors as negative values. Alter callers of ->setup() to handle errors properly.

Fixes 790bf585b0ee ("selftests/resctrl: Add Cache Allocation Technology (CAT) selftest")
Fixes ecdbb911f22d ("selftests/resctrl: Add MBM test")
---
 tools/testing/selftests/resctrl/cache.c       | 4 +++-
 tools/testing/selftests/resctrl/cat_test.c    | 2 +-
 tools/testing/selftests/resctrl/cmt_test.c    | 2 +-
 tools/testing/selftests/resctrl/mba_test.c    | 2 +-
 tools/testing/selftests/resctrl/mbm_test.c    | 2 +-
 tools/testing/selftests/resctrl/resctrl.h     | 2 ++
 tools/testing/selftests/resctrl/resctrl_val.c | 4 +++-
 7 files changed, 12 insertions(+), 6 deletions(-)
tools/testing/selftests/resctrl/cache.c +3 -1
index 68ff856d36f0..0485863a169f 100644
@@ -244,10 +244,12 @@ int cat_val(struct resctrl_val_param *param)
 	while (1) {
 		if (!strncmp(resctrl_val, CAT_STR, sizeof(CAT_STR))) {
 			ret = param->setup(1, param);
-			if (ret) {
+			if (ret == END_OF_TESTS) {
 				ret = 0;
 				break;
 			}
+			if (ret < 0)
+				break;
 			ret = reset_enable_llc_perf(bm_pid, param->cpu_no);
 			if (ret)
 				break;
tools/testing/selftests/resctrl/cat_test.c +1 -1
index 1c5e90c63254..2d3c7c77ab6c 100644
@@ -40,7 +40,7 @@ static int cat_setup(int num, ...)
 
 	/* Run NUM_OF_RUNS times */
 	if (p->num_of_runs >= NUM_OF_RUNS)
-		return -1;
+		return END_OF_TESTS;
 
 	if (p->num_of_runs == 0) {
 		sprintf(schemata, "%lx", p->mask);
tools/testing/selftests/resctrl/cmt_test.c +1 -1
index 8968e36db99d..3b0454e7fc82 100644
@@ -32,7 +32,7 @@ static int cmt_setup(int num, ...)
 
 	/* Run NUM_OF_RUNS times */
 	if (p->num_of_runs >= NUM_OF_RUNS)
-		return -1;
+		return END_OF_TESTS;
 
 	p->num_of_runs++;
 
tools/testing/selftests/resctrl/mba_test.c +1 -1
index 1a1bdb6180cf..f32289ae17ae 100644
@@ -41,7 +41,7 @@ static int mba_setup(int num, ...)
 		return 0;
 
 	if (allocation < ALLOCATION_MIN || allocation > ALLOCATION_MAX)
-		return -1;
+		return END_OF_TESTS;
 
 	sprintf(allocation_str, "%d", allocation);
 
tools/testing/selftests/resctrl/mbm_test.c +1 -1
index 8392e5c55ed0..280187628054 100644
@@ -95,7 +95,7 @@ static int mbm_setup(int num, ...)
 
 	/* Run NUM_OF_RUNS times */
 	if (num_of_runs++ >= NUM_OF_RUNS)
-		return -1;
+		return END_OF_TESTS;
 
 	va_start(param, num);
 	p = va_arg(param, struct resctrl_val_param *);
tools/testing/selftests/resctrl/resctrl.h +2 -0
index f0ded31fb3c7..f44fa2de4d98 100644
@@ -37,6 +37,8 @@
 #define ARCH_INTEL     1
 #define ARCH_AMD       2
 
+#define END_OF_TESTS	1
+
 #define PARENT_EXIT(err_msg)			\
 	do {					\
 		perror(err_msg);		\
tools/testing/selftests/resctrl/resctrl_val.c +3 -1
index 787546a52849..00864242d76c 100644
@@ -735,10 +735,12 @@ int resctrl_val(char **benchmark_cmd, struct resctrl_val_param *param)
 	/* Test runs until the callback setup() tells the test to stop. */
 	while (1) {
 		ret = param->setup(1, param);
-		if (ret) {
+		if (ret == END_OF_TESTS) {
 			ret = 0;
 			break;
 		}
+		if (ret < 0)
+			break;
 
 		if (!strncmp(resctrl_val, MBM_STR, sizeof(MBM_STR)) ||
 		    !strncmp(resctrl_val, MBA_STR, sizeof(MBA_STR))) {
Signature · 1 line
2.30.2
#7 lore raw

PATCH 4/4 selftests/resctrl: Check for return value after write_schemata()

In reply to Ilpo Järvinen

MBA test case writes schemata but it forgets to check if the write is successful or not. Hence, add the check and return error properly.

Fixes 01fee6b4d1f9 ("selftests/resctrl: Add MBA test")
---
 tools/testing/selftests/resctrl/mba_test.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
tools/testing/selftests/resctrl/mba_test.c +6 -1
index f32289ae17ae..97dc98c0c949 100644
@@ -28,6 +28,7 @@ static int mba_setup(int num, ...)
 	struct resctrl_val_param *p;
 	char allocation_str[64];
 	va_list param;
+	int ret;
 
 	va_start(param, num);
 	p = va_arg(param, struct resctrl_val_param *);
@@ -45,7 +46,11 @@ static int mba_setup(int num, ...)
 
 	sprintf(allocation_str, "%d", allocation);
 
-	write_schemata(p->ctrlgrp, allocation_str, p->cpu_no, p->resctrl_val);
+	ret = write_schemata(p->ctrlgrp, allocation_str, p->cpu_no,
+			     p->resctrl_val);
+	if (ret < 0)
+		return ret;
+
 	allocation -= ALLOCATION_STEP;
 
 	return 0;
Signature · 1 line
2.30.2