[LTP] [PATCH v8 1/5] sched_setattr01: Convert to new API
Andrea Cervesato
andrea.cervesato@suse.de
Fri Sep 4 09:41:19 CEST 2026
From: Andrea Cervesato <andrea.cervesato@suse.com>
Rewrite the test to use the modern LTP API (tst_test.h) with a
struct tcase array and TST_EXP_* macros.
Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
testcases/kernel/syscalls/sched_setattr/Makefile | 1 -
.../syscalls/sched_setattr/sched_setattr01.c | 231 +++++++++++++--------
2 files changed, 142 insertions(+), 90 deletions(-)
diff --git a/testcases/kernel/syscalls/sched_setattr/Makefile b/testcases/kernel/syscalls/sched_setattr/Makefile
index 8fd2bd6f2..81f9dc164 100644
--- a/testcases/kernel/syscalls/sched_setattr/Makefile
+++ b/testcases/kernel/syscalls/sched_setattr/Makefile
@@ -5,6 +5,5 @@ top_srcdir ?= ../../../..
include $(top_srcdir)/include/mk/testcases.mk
-CFLAGS += -pthread
include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/kernel/syscalls/sched_setattr/sched_setattr01.c b/testcases/kernel/syscalls/sched_setattr/sched_setattr01.c
index 13380d177..721620850 100644
--- a/testcases/kernel/syscalls/sched_setattr/sched_setattr01.c
+++ b/testcases/kernel/syscalls/sched_setattr/sched_setattr01.c
@@ -1,134 +1,187 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (c) Huawei Technologies Co., Ltd., 2015
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
- * the GNU General Public License for more details.
+ * Copyright (C) 2026 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
*/
- /* Description:
- * Verify that:
- * 1) sched_setattr succeed with correct parameters
- * 2) sched_setattr fails with unused pid
- * 3) sched_setattr fails with invalid address
- * 4) sched_setattr fails with invalid flag
+
+/*\
+ * Verify that :manpage:`sched_setattr(2)`:
+ *
+ * - succeeds with correct parameters and attributes are verified via
+ * :manpage:`sched_getattr(2)`
+ * - fails with ESRCH when pid is unused
+ * - fails with EINVAL when pid is negative
+ * - fails with EINVAL when sched_attr address is NULL
+ * - fails with EFAULT when sched_attr address is invalid
+ * - fails with E2BIG when sched_attr size is smaller than version 0
+ * - fails with EINVAL when flags are invalid
+ * - fails with EINVAL when sched_policy is invalid
+ * - fails with EINVAL when runtime exceeds deadline
+ *
+ * Root is required (:c:macro:`CAP_SYS_NICE`) to configure and validate the
+ * :c:macro:`SCHED_DEADLINE` policy.
+ *
+ * The test relies on the LTP harness process isolation and resets the
+ * scheduling policy to :c:macro:`SCHED_OTHER` after testing to prevent
+ * :c:macro:`SCHED_DEADLINE` constraints from leaking into subsequent
+ * test cases or iterations.
*/
#define _GNU_SOURCE
-#include <unistd.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <time.h>
-#include <linux/unistd.h>
-#include <linux/kernel.h>
-#include <linux/types.h>
-#include <sys/syscall.h>
-#include <pthread.h>
+
#include <errno.h>
-#include "test.h"
+#include "tst_test.h"
#include "lapi/sched.h"
-char *TCID = "sched_setattr01";
-
#define RUNTIME_VAL 10000000
#define PERIOD_VAL 30000000
#define DEADLINE_VAL 30000000
-static pid_t pid;
static pid_t unused_pid;
+static pid_t invalid_pid = -1;
+static void *bad_addr;
static struct sched_attr attr = {
.size = sizeof(struct sched_attr),
- .sched_flags = 0,
- .sched_nice = 0,
- .sched_priority = 0,
-
.sched_policy = SCHED_DEADLINE,
.sched_runtime = RUNTIME_VAL,
.sched_period = PERIOD_VAL,
.sched_deadline = DEADLINE_VAL,
};
-static struct test_case {
+static struct sched_attr attr_small = {
+ .size = SCHED_ATTR_SIZE_VER0 - 1,
+};
+
+static struct sched_attr attr_invalid_policy = {
+ .size = sizeof(struct sched_attr),
+ .sched_policy = 999,
+};
+
+static struct sched_attr attr_bad_dl = {
+ .size = sizeof(struct sched_attr),
+ .sched_policy = SCHED_DEADLINE,
+ .sched_runtime = PERIOD_VAL,
+ .sched_deadline = RUNTIME_VAL,
+ .sched_period = PERIOD_VAL,
+};
+
+static struct tcase {
pid_t *pid;
- struct sched_attr *a;
+ struct sched_attr *attr;
+ int bad_attr;
unsigned int flags;
- int exp_return;
int exp_errno;
-} test_cases[] = {
- {&pid, &attr, 0, 0, 0},
- {&unused_pid, &attr, 0, -1, ESRCH},
- {&pid, NULL, 0, -1, EINVAL},
- {&pid, &attr, 1000, -1, EINVAL}
+ const char *desc;
+} tcases[] = {
+ {
+ .attr = &attr,
+ .desc = "sched_setattr() with valid parameters",
+ },
+ {
+ .pid = &unused_pid,
+ .attr = &attr,
+ .exp_errno = ESRCH,
+ .desc = "sched_setattr() with unused pid",
+ },
+ {
+ .pid = &invalid_pid,
+ .attr = &attr,
+ .exp_errno = EINVAL,
+ .desc = "sched_setattr() with negative pid",
+ },
+ {
+ .exp_errno = EINVAL,
+ .desc = "sched_setattr() with NULL sched_attr",
+ },
+ {
+ .bad_attr = 1,
+ .exp_errno = EFAULT,
+ .desc = "sched_setattr() with invalid sched_attr address",
+ },
+ {
+ .attr = &attr_small,
+ .exp_errno = E2BIG,
+ .desc = "sched_setattr() with size smaller than version 0",
+ },
+ {
+ .attr = &attr,
+ .flags = 1000,
+ .exp_errno = EINVAL,
+ .desc = "sched_setattr() with invalid flags",
+ },
+ {
+ .attr = &attr_invalid_policy,
+ .exp_errno = EINVAL,
+ .desc = "sched_setattr() with invalid sched_policy",
+ },
+ {
+ .attr = &attr_bad_dl,
+ .exp_errno = EINVAL,
+ .desc = "sched_setattr() with runtime exceeding deadline",
+ },
};
-static void setup(void);
-static void sched_setattr_verify(const struct test_case *test);
-
-int TST_TOTAL = ARRAY_SIZE(test_cases);
-
-void *do_test(void *data LTP_ATTRIBUTE_UNUSED)
+static void reset_sched(void)
{
- int i;
-
- for (i = 0; i < TST_TOTAL; i++)
- sched_setattr_verify(&test_cases[i]);
+ struct sched_attr normal = {
+ .size = sizeof(normal),
+ .sched_policy = SCHED_OTHER,
+ };
- return NULL;
+ sched_setattr(0, &normal, 0);
}
-static void sched_setattr_verify(const struct test_case *test)
+static void verify_sched_setattr(unsigned int n)
{
- TEST(sched_setattr(*(test->pid), test->a, test->flags));
-
- if (TEST_RETURN != test->exp_return) {
- tst_resm(TFAIL | TTERRNO, "sched_setattr(%i,attr,%u) "
- "returned: %ld expected: %d",
- *(test->pid), test->flags,
- TEST_RETURN, test->exp_return);
+ struct tcase *tc = &tcases[n];
+ pid_t pid = tc->pid ? *tc->pid : 0;
+ struct sched_attr *target_attr = tc->bad_attr ? bad_addr : tc->attr;
+ struct sched_attr read_attr = { .size = sizeof(read_attr) };
+
+ /*
+ * The kernel writes sizeof(struct sched_attr) back to uattr->size
+ * on the -E2BIG error path, clobbering our test input. Refresh
+ * before each call so re-runs (e.g. -i N) still exercise the
+ * intended size.
+ */
+ attr_small.size = SCHED_ATTR_SIZE_VER0 - 1;
+
+ if (tc->exp_errno) {
+ TST_EXP_FAIL(sched_setattr(pid, target_attr, tc->flags),
+ tc->exp_errno, "%s", tc->desc);
return;
}
- if (TEST_ERRNO == test->exp_errno) {
- tst_resm(TPASS | TTERRNO,
- "sched_setattr() works as expected");
+ TST_EXP_PASS(sched_setattr(pid, target_attr, tc->flags),
+ "%s", tc->desc);
+ if (!TST_PASS)
return;
- }
-
- tst_resm(TFAIL | TTERRNO, "sched_setattr(%i,attr,%u): "
- "expected: %d - %s",
- *(test->pid), test->flags,
- test->exp_errno, tst_strerrno(test->exp_errno));
-}
-
-int main(int argc, char **argv)
-{
- pthread_t thread;
- int lc;
- tst_parse_opts(argc, argv, NULL, NULL);
-
- setup();
-
- for (lc = 0; TEST_LOOPING(lc); lc++) {
- pthread_create(&thread, NULL, do_test, NULL);
- pthread_join(thread, NULL);
+ if (sched_getattr(pid, &read_attr, sizeof(read_attr), 0) == -1) {
+ tst_res(TFAIL | TERRNO, "sched_getattr() failed");
+ return;
}
- tst_exit();
+ TST_EXP_EQ_LU(read_attr.sched_policy, SCHED_DEADLINE);
+ TST_EXP_EQ_LU(read_attr.sched_runtime, RUNTIME_VAL);
+ TST_EXP_EQ_LU(read_attr.sched_deadline, DEADLINE_VAL);
+ TST_EXP_EQ_LU(read_attr.sched_period, PERIOD_VAL);
+
+ reset_sched();
}
-void setup(void)
+static void setup(void)
{
- unused_pid = tst_get_unused_pid(setup);
-
- tst_require_root();
-
- TEST_PAUSE;
+ unused_pid = tst_get_unused_pid();
+ bad_addr = tst_get_bad_addr(NULL);
}
+
+static struct tst_test test = {
+ .test = verify_sched_setattr,
+ .tcnt = ARRAY_SIZE(tcases),
+ .setup = setup,
+ .cleanup = reset_sched,
+ .needs_root = 1,
+};
--
2.51.0
More information about the ltp
mailing list