Received: from picard.linux.it (picard.linux.it [213.254.12.146]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 441D3606A327 for ; Mon, 30 Nov 2026 12:00:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=JWpmwNG6 Received: from picard.linux.it (localhost [IPv6:::1]) by picard.linux.it (Postfix) with ESMTP id E756A3EA6AB for ; Mon, 30 Nov 2026 13:00:30 +0100 (CET) Received: from in-3.smtp.seeweb.it (in-3.smtp.seeweb.it [IPv6:2001:4b78:1:20::3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1)) (No client certificate requested) by picard.linux.it (Postfix) with ESMTPS id 7017E3E917F for ; Mon, 30 Nov 2026 12:59:31 +0100 (CET) Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.10]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by in-3.smtp.seeweb.it (Postfix) with ESMTPS id 858C41A009A9 for ; Mon, 30 Nov 2026 12:59:30 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796039971; x=1827575971; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=RRURq8bxiRMihsKxxGZ/OHZV2wxAXDYbaBnMPrlw+kA=; b=JWpmwNG66cnOFsLx40A+lHpKZhsXL8eDPLf8Y7uQARc/Ypw+lEmu2lj4 x3VcjzU5XYKJdH2YGCcMU8vm/rAIjOLFV4RtErux+dD2TLm1AD+XlJZBr MOWpxe1bFPO7HwQvnTj87hWMcOHov1CVmELnpVCRtAIlD6OCJo3KLybPT kCmR7+PfTgdE7OAreLxR9fCPRhV9H8HlISv8wbXa4Qre/8Xp2DM2A8dD1 tCquc+mtYp/AYeArYykp7f86aEKR3oFc8epWAoYQowTqQt1WJDqnOIA+Q 6XndyAXtTtPpf5PLZc1RhlTGJLquNRKXtDhz/vcPTr9hu2wBt4dpa/j3j w==; X-CSE-ConnectionGUID: sn/qDAuCQriWD4nb+tDuwg== X-CSE-MsgGUID: UI9rWs5NSZ6R4gw0A+HIAQ== X-IronPort-AV: E=McAfee;i="6800,10657,11982"; a="4757543" X-IronPort-AV: E=Sophos;i="6.27,242,1787036400"; d="scan'208";a="4757543" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by fmvoesa104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Nov 2026 03:59:26 -0800 X-CSE-ConnectionGUID: dIP2A4FuQHmEelKJn714Cg== X-CSE-MsgGUID: wjBGE5ciT9SrRu0jquKkdw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,242,1787036400"; d="scan'208";a="12790183" Received: from pkubaj-mobl.igk.intel.com (HELO intel.com) ([10.237.142.91]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Nov 2026 03:59:25 -0800 From: Piotr Kubaj To: ltp@lists.linux.it Date: Mon, 30 Nov 2026 12:59:00 +0100 Message-ID: <20261130115901.32486-3-piotr.kubaj@intel.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20261130115901.32486-1-piotr.kubaj@intel.com> References: <20261130115901.32486-1-piotr.kubaj@intel.com> X-Archived-By: MIME-Version: 1.0 X-Spam-Status: No, score=-16.6 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 X-Virus-Scanned: clamav-milter 1.0.9 at in-3.smtp.seeweb.it X-Virus-Status: Clean Subject: [LTP] [PATCH v6 2/3] lib: add SAFE_SCHED_GETAFFINITY() and SAFE_SCHED_SETAFFINITY() X-BeenThere: ltp@lists.linux.it X-Mailman-Version: 2.1.29 Precedence: list List-Id: Linux Test Project List-Archive: List-Post: List-Help: List-Subscribe: , Cc: helena.anna.dubel@intel.com, tomasz.ossowski@intel.com, rafael.j.wysocki@intel.com, "Claude Fable 5.2" , daniel.niestepski@intel.com Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "ltp" 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 Co-Authored-By: Claude Fable 5.2 --- 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 + */ + +#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 +#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 ` 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 ` 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. -- Mailing list info: https://lists.linux.it/listinfo/ltp