[LTP] [PATCH v6 2/3] lib: add SAFE_SCHED_GETAFFINITY() and SAFE_SCHED_SETAFFINITY()

Piotr Kubaj piotr.kubaj@intel.com
Mon Nov 30 12:59:00 CET 2026


Add safe wrappers for sched_getaffinity() and sched_setaffinity() in a
new tst_safe_sched.h header and list it in the C API documentation.
Both calls are GNU extensions, so the header is separate from
tst_safe_macros.h and tests using it have to define _GNU_SOURCE.

Signed-off-by: Piotr Kubaj <piotr.kubaj@intel.com>
Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
---
 doc/developers/api_c_tests.rst |  4 ++
 include/tst_safe_sched.h       | 83 ++++++++++++++++++++++++++++++++++
 2 files changed, 87 insertions(+)
 create mode 100644 include/tst_safe_sched.h

diff --git a/doc/developers/api_c_tests.rst b/doc/developers/api_c_tests.rst
index af7d84ce1..53ff5429f 100644
--- a/doc/developers/api_c_tests.rst
+++ b/doc/developers/api_c_tests.rst
@@ -95,6 +95,10 @@ Safe file operations
 --------------------
 .. kernel-doc:: ../../include/tst_safe_file_ops.h
 
+Safe scheduler operations
+-------------------------
+.. kernel-doc:: ../../include/tst_safe_sched.h
+
 Saving and restoring /proc|sys values
 -------------------------------------
 .. kernel-doc:: ../../include/tst_sys_conf.h
diff --git a/include/tst_safe_sched.h b/include/tst_safe_sched.h
new file mode 100644
index 000000000..e1125d2e5
--- /dev/null
+++ b/include/tst_safe_sched.h
@@ -0,0 +1,83 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2026 Piotr Kubaj <piotr.kubaj@intel.com>
+ */
+
+#ifndef TST_SAFE_SCHED_H__
+#define TST_SAFE_SCHED_H__
+
+/*
+ * sched_getaffinity() and sched_setaffinity() are GNU extensions, tests have
+ * to define _GNU_SOURCE before including any header to use these.
+ */
+
+#include <sched.h>
+#include "tst_test.h"
+
+static inline int safe_sched_getaffinity(const char *file, const int lineno,
+	pid_t pid, size_t cpusetsize, cpu_set_t *mask)
+{
+	int rval;
+
+	rval = sched_getaffinity(pid, cpusetsize, mask);
+
+	if (rval == -1) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			"sched_getaffinity(%d, %zu, %p) failed",
+			pid, cpusetsize, mask);
+	} else if (rval) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			"Invalid sched_getaffinity(%d, %zu, %p) return value %d",
+			pid, cpusetsize, mask, rval);
+	}
+
+	return rval;
+}
+
+/**
+ * SAFE_SCHED_GETAFFINITY() - Reads the CPU affinity mask of a thread.
+ *
+ * @pid: Thread ID, 0 for the calling thread.
+ * @cpusetsize: Size of the mask in bytes.
+ * @mask: Pointer to the CPU set to store the affinity mask into.
+ *
+ * Wrapper for :manpage:`sched_getaffinity(2)`, exits with
+ * :c:enum:`TBROK <tst_res_flags>` on failure.
+ */
+#define SAFE_SCHED_GETAFFINITY(pid, cpusetsize, mask) \
+	safe_sched_getaffinity(__FILE__, __LINE__, (pid), (cpusetsize), (mask))
+
+static inline int safe_sched_setaffinity(const char *file, const int lineno,
+	pid_t pid, size_t cpusetsize, const cpu_set_t *mask)
+{
+	int rval;
+
+	rval = sched_setaffinity(pid, cpusetsize, mask);
+
+	if (rval == -1) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			"sched_setaffinity(%d, %zu, %p) failed",
+			pid, cpusetsize, mask);
+	} else if (rval) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			"Invalid sched_setaffinity(%d, %zu, %p) return value %d",
+			pid, cpusetsize, mask, rval);
+	}
+
+	return rval;
+}
+
+/**
+ * SAFE_SCHED_SETAFFINITY() - Sets the CPU affinity mask of a thread.
+ *
+ * @pid: Thread ID, 0 for the calling thread.
+ * @cpusetsize: Size of the mask in bytes.
+ * @mask: Pointer to the CPU set with the new affinity mask.
+ *
+ * Wrapper for :manpage:`sched_setaffinity(2)`, exits with
+ * :c:enum:`TBROK <tst_res_flags>` on failure.
+ */
+#define SAFE_SCHED_SETAFFINITY(pid, cpusetsize, mask) \
+	safe_sched_setaffinity(__FILE__, __LINE__, (pid), (cpusetsize), (mask))
+
+#endif /* TST_SAFE_SCHED_H__ */
-- 
2.47.3

---------------------------------------------------------------------
Intel Technology Poland sp. z o.o.
ul. Slowackiego 173 | 80-298 Gdansk | Sad Rejonowy Gdansk Polnoc | VII Wydzial Gospodarczy Krajowego Rejestru Sadowego - KRS 101882 | NIP 957-07-52-316 | Kapital zakladowy 200.000 PLN.
Spolka oswiadcza, ze posiada status duzego przedsiebiorcy w rozumieniu ustawy z dnia 8 marca 2013 r. o przeciwdzialaniu nadmiernym opoznieniom w transakcjach handlowych.

Ta wiadomosc wraz z zalacznikami jest przeznaczona dla okreslonego adresata i moze zawierac informacje poufne. W razie przypadkowego otrzymania tej wiadomosci, prosimy o powiadomienie nadawcy oraz trwale jej usuniecie; jakiekolwiek przegladanie lub rozpowszechnianie jest zabronione.
This e-mail and any attachments may contain confidential material for the sole use of the intended recipient(s). If you are not the intended recipient, please contact the sender and delete all copies; any review or distribution by others is strictly prohibited.



More information about the ltp mailing list