1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) Crackerjack Project., 2007 4f08c3bdfSopenharmony_ci * Description: This case tests the sched_getaffinity() syscall 5f08c3bdfSopenharmony_ci * History: Porting from Crackerjack to LTP is done by 6f08c3bdfSopenharmony_ci * Manas Kumar Nayak maknayak@in.ibm.com> 7f08c3bdfSopenharmony_ci */ 8f08c3bdfSopenharmony_ci#define _GNU_SOURCE 9f08c3bdfSopenharmony_ci#include <errno.h> 10f08c3bdfSopenharmony_ci#include <sched.h> 11f08c3bdfSopenharmony_ci#include <stdlib.h> 12f08c3bdfSopenharmony_ci#include <string.h> 13f08c3bdfSopenharmony_ci#include "tst_test.h" 14f08c3bdfSopenharmony_ci#include "tst_safe_macros.h" 15f08c3bdfSopenharmony_ci#include "lapi/cpuset.h" 16f08c3bdfSopenharmony_ci 17f08c3bdfSopenharmony_cistatic long ncpu; 18f08c3bdfSopenharmony_ci 19f08c3bdfSopenharmony_cistatic void *bad_addr; 20f08c3bdfSopenharmony_ci 21f08c3bdfSopenharmony_cistatic void errno_test(pid_t pid, size_t cpusize, void *mask, int exp_errno) 22f08c3bdfSopenharmony_ci{ 23f08c3bdfSopenharmony_ci TEST(sched_getaffinity(pid, cpusize, mask)); 24f08c3bdfSopenharmony_ci 25f08c3bdfSopenharmony_ci if (TST_RET != -1) { 26f08c3bdfSopenharmony_ci tst_res(TFAIL, 27f08c3bdfSopenharmony_ci "sched_getaffinity() returned %ld, expected -1", 28f08c3bdfSopenharmony_ci TST_RET); 29f08c3bdfSopenharmony_ci return; 30f08c3bdfSopenharmony_ci } 31f08c3bdfSopenharmony_ci 32f08c3bdfSopenharmony_ci if (TST_ERR != exp_errno) { 33f08c3bdfSopenharmony_ci tst_res(TFAIL | TTERRNO, 34f08c3bdfSopenharmony_ci "sched_getaffinity() should fail with %s", 35f08c3bdfSopenharmony_ci tst_strerrno(exp_errno)); 36f08c3bdfSopenharmony_ci return; 37f08c3bdfSopenharmony_ci } 38f08c3bdfSopenharmony_ci 39f08c3bdfSopenharmony_ci tst_res(TPASS | TTERRNO, "sched_getaffinity() failed"); 40f08c3bdfSopenharmony_ci} 41f08c3bdfSopenharmony_ci 42f08c3bdfSopenharmony_cistatic void do_test(void) 43f08c3bdfSopenharmony_ci{ 44f08c3bdfSopenharmony_ci cpu_set_t *mask; 45f08c3bdfSopenharmony_ci int nrcpus = 1024; 46f08c3bdfSopenharmony_ci unsigned len; 47f08c3bdfSopenharmony_ci 48f08c3bdfSopenharmony_cirealloc: 49f08c3bdfSopenharmony_ci mask = CPU_ALLOC(nrcpus); 50f08c3bdfSopenharmony_ci if (!mask) 51f08c3bdfSopenharmony_ci tst_brk(TBROK | TERRNO, "CPU_ALLOC()"); 52f08c3bdfSopenharmony_ci 53f08c3bdfSopenharmony_ci len = CPU_ALLOC_SIZE(nrcpus); 54f08c3bdfSopenharmony_ci CPU_ZERO_S(len, mask); 55f08c3bdfSopenharmony_ci 56f08c3bdfSopenharmony_ci TEST(sched_getaffinity(0, len, mask)); 57f08c3bdfSopenharmony_ci if (TST_RET == -1) { 58f08c3bdfSopenharmony_ci CPU_FREE(mask); 59f08c3bdfSopenharmony_ci if (TST_ERR == EINVAL && nrcpus < (1024 << 8)) { 60f08c3bdfSopenharmony_ci nrcpus = nrcpus << 2; 61f08c3bdfSopenharmony_ci goto realloc; 62f08c3bdfSopenharmony_ci } 63f08c3bdfSopenharmony_ci tst_brk(TBROK | TTERRNO, "fail to get cpu affinity"); 64f08c3bdfSopenharmony_ci } 65f08c3bdfSopenharmony_ci 66f08c3bdfSopenharmony_ci long i, af_cpus = 0; 67f08c3bdfSopenharmony_ci 68f08c3bdfSopenharmony_ci for (i = 0; i < nrcpus; i++) 69f08c3bdfSopenharmony_ci af_cpus += !!CPU_ISSET_S(i, len, mask); 70f08c3bdfSopenharmony_ci 71f08c3bdfSopenharmony_ci if (af_cpus == 0) 72f08c3bdfSopenharmony_ci tst_res(TFAIL, "No cpus enabled in mask"); 73f08c3bdfSopenharmony_ci else if (af_cpus > ncpu) 74f08c3bdfSopenharmony_ci tst_res(TFAIL, "Enabled cpus = %li > system cpus %li", af_cpus, ncpu); 75f08c3bdfSopenharmony_ci else 76f08c3bdfSopenharmony_ci tst_res(TPASS, "cpuset size = %u, enabled cpus %ld", len, af_cpus); 77f08c3bdfSopenharmony_ci 78f08c3bdfSopenharmony_ci errno_test(0, len, bad_addr, EFAULT); 79f08c3bdfSopenharmony_ci errno_test(0, 0, mask, EINVAL); 80f08c3bdfSopenharmony_ci errno_test(tst_get_unused_pid(), len, mask, ESRCH); 81f08c3bdfSopenharmony_ci 82f08c3bdfSopenharmony_ci CPU_FREE(mask); 83f08c3bdfSopenharmony_ci} 84f08c3bdfSopenharmony_ci 85f08c3bdfSopenharmony_cistatic void setup(void) 86f08c3bdfSopenharmony_ci{ 87f08c3bdfSopenharmony_ci ncpu = SAFE_SYSCONF(_SC_NPROCESSORS_CONF); 88f08c3bdfSopenharmony_ci tst_res(TINFO, "system has %ld processor(s).", ncpu); 89f08c3bdfSopenharmony_ci 90f08c3bdfSopenharmony_ci bad_addr = tst_get_bad_addr(NULL); 91f08c3bdfSopenharmony_ci} 92f08c3bdfSopenharmony_ci 93f08c3bdfSopenharmony_cistatic struct tst_test test = { 94f08c3bdfSopenharmony_ci .setup = setup, 95f08c3bdfSopenharmony_ci .test_all = do_test, 96f08c3bdfSopenharmony_ci}; 97