Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 75D92C87FCF for ; Sat, 9 Aug 2025 17:08:35 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.1076121.1437861 (Exim 4.92) (envelope-from ) id 1ukn3S-0007EP-Eu; Sat, 09 Aug 2025 17:08:26 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 1076121.1437861; Sat, 09 Aug 2025 17:08:26 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ukn3S-0007EG-Bg; Sat, 09 Aug 2025 17:08:26 +0000 Received: by outflank-mailman (input) for mailman id 1076121; Sat, 09 Aug 2025 17:08:24 +0000 Received: from se1-gles-flk1-in.inumbo.com ([94.247.172.50] helo=se1-gles-flk1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1ukn3Q-00067a-GP for xen-devel@lists.xenproject.org; Sat, 09 Aug 2025 17:08:24 +0000 Received: from mail-4316.protonmail.ch (mail-4316.protonmail.ch [185.70.43.16]) by se1-gles-flk1.inumbo.com (Halon) with ESMTPS id 729b6eaf-7543-11f0-b898-0df219b8e170; Sat, 09 Aug 2025 19:08:19 +0200 (CEST) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 729b6eaf-7543-11f0-b898-0df219b8e170 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proton.me; s=kuzy6lodpvdb7aowi7jungvymu.protonmail; t=1754759298; x=1755018498; bh=QIoDWNQvjB0vbubD3Q49dV/LoY+Oz8PxC/SZyX5a+h8=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector; b=UuwnSDOwq4q36HjWOKuLmgYllt37Gny8YB4Oi+7y/P5Bl+R52Q0AIxQSWZ9hw5dvp sG070horqC6MLd7FKdiqGg3FUXxiuCQ2SsbuXf4Li7u+D9iBxldUbLL6XyYf2xSczZ YnqWO0cYlk+V8D6w6ALzOWoyxVI2ZDtz+CoukEaARL0R3il6JcE747HtwTn9CHP37B 4t7N1cSgx6Y/vwDp5N71qHl94YAVQ/CoYWN0Zl0FhPGcpCwFMnODbf3RG3rfUeraIF M8APCPmfZz+gMVTbLTyWylzcVo0Tpz8pxoS6Rw+PB8SUOEqsGQu2yHM+j/UByynIHA ncUu2qIwvYyGg== Date: Sat, 09 Aug 2025 17:08:13 +0000 To: xen-devel@lists.xenproject.org From: dmkhn@proton.me Cc: andrew.cooper3@citrix.com, anthony.perard@vates.tech, jbeulich@suse.com, julien@xen.org, michal.orzel@amd.com, roger.pau@citrix.com, sstabellini@kernel.org, dmukhin@ford.com, Julien Grall Subject: [PATCH v15 3/4] tools/tests: introduce unit tests for domain ID allocator Message-ID: <20250809170747.1836880-4-dmukhin@ford.com> In-Reply-To: <20250809170747.1836880-1-dmukhin@ford.com> References: <20250809170747.1836880-1-dmukhin@ford.com> Feedback-ID: 123220910:user:proton X-Pm-Message-ID: 4686383e3422d33880488c4f47c0d279d9d8ee00 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable From: Denis Mukhin =20 Introduce some basic infrastructure for doing domain ID allocation unit tes= ts, and add a few tests that ensure correctness of the domain ID allocator. Use and xen/lib/find-next-bit.c in test hardness code. Adjust find-next-bit.c to be compiled with __XEN_TOOLS__. Signed-off-by: Denis Mukhin Reviewed-by: Julien Grall --- Changes since v14: - addressed NITs from v14's review - extended the test for exact IDs allocation (trivial change) - added Julien's R-b=20 --- tools/include/xen-tools/bitops.h | 10 +++ tools/tests/Makefile | 1 + tools/tests/domid/.gitignore | 2 + tools/tests/domid/Makefile | 56 ++++++++++++++++ tools/tests/domid/harness.h | 54 +++++++++++++++ tools/tests/domid/include/xen/domain.h | 1 + tools/tests/domid/test-domid.c | 93 ++++++++++++++++++++++++++ xen/lib/find-next-bit.c | 5 ++ 8 files changed, 222 insertions(+) create mode 100644 tools/tests/domid/.gitignore create mode 100644 tools/tests/domid/Makefile create mode 100644 tools/tests/domid/harness.h create mode 120000 tools/tests/domid/include/xen/domain.h create mode 100644 tools/tests/domid/test-domid.c diff --git a/tools/include/xen-tools/bitops.h b/tools/include/xen-tools/bit= ops.h index 681482f6759f..3b98fba6d74c 100644 --- a/tools/include/xen-tools/bitops.h +++ b/tools/include/xen-tools/bitops.h @@ -12,6 +12,16 @@ #define BITS_PER_LONG 32 #endif =20 +#define ffsl(x) __builtin_ffsl(x) + +#define BIT_WORD(nr) ((nr) / BITS_PER_LONG) + +#define BITS_TO_LONGS(bits) \ + (((bits) + BITS_PER_LONG - 1) / BITS_PER_LONG) + +#define DECLARE_BITMAP(name, bits) \ + unsigned long name[BITS_TO_LONGS(bits)] + #define BITMAP_ENTRY(_nr,_bmap) ((_bmap))[(_nr) / 8] #define BITMAP_SHIFT(_nr) ((_nr) % 8) =20 diff --git a/tools/tests/Makefile b/tools/tests/Makefile index 36928676a666..94fa80c16880 100644 --- a/tools/tests/Makefile +++ b/tools/tests/Makefile @@ -2,6 +2,7 @@ XEN_ROOT =3D $(CURDIR)/../.. include $(XEN_ROOT)/tools/Rules.mk =20 SUBDIRS-y :=3D +SUBDIRS-y +=3D domid SUBDIRS-y +=3D resource SUBDIRS-$(CONFIG_X86) +=3D cpu-policy SUBDIRS-$(CONFIG_X86) +=3D tsx diff --git a/tools/tests/domid/.gitignore b/tools/tests/domid/.gitignore new file mode 100644 index 000000000000..70e306b3c074 --- /dev/null +++ b/tools/tests/domid/.gitignore @@ -0,0 +1,2 @@ +*.o +test-domid diff --git a/tools/tests/domid/Makefile b/tools/tests/domid/Makefile new file mode 100644 index 000000000000..d96ceca6d954 --- /dev/null +++ b/tools/tests/domid/Makefile @@ -0,0 +1,56 @@ +# SPDX-License-Identifier: GPL-2.0-only +# +# Unit tests for domain ID allocator. +# +# Copyright 2025 Ford Motor Company + +XEN_ROOT=3D$(CURDIR)/../../.. +include $(XEN_ROOT)/tools/Rules.mk + +TESTS :=3D test-domid + +vpath domid.c $(XEN_ROOT)/xen/common/ +vpath find-next-bit.c $(XEN_ROOT)/xen/lib/ + +.PHONY: all +all: $(TESTS) + +.PHONY: run +run: $(TESTS) +=09$(foreach t,$(TESTS),./$(t);) + +.PHONY: clean +clean: +=09$(RM) -- *.o $(TESTS) $(DEPS_RM) + +.PHONY: distclean +distclean: clean +=09$(RM) -- *~ + +.PHONY: install +install: all +=09$(INSTALL_DIR) $(DESTDIR)$(LIBEXEC)/tests +=09$(INSTALL_PROG) test-domid $(DESTDIR)$(LIBEXEC)/tests + +.PHONY: uninstall +uninstall: +=09$(RM) -- $(DESTDIR)$(LIBEXEC)/tests/test-domid + +CFLAGS +=3D -D__XEN_TOOLS__ +# find-next-bit.c +CFLAGS +=3D '-DEXPORT_SYMBOL(x)=3D' \ + -Dfind_first_bit \ + -Dfind_first_zero_bit \ + -Dfind_next_bit \ + -Dfind_next_bit_le \ + -Dfind_next_zero_bit_le +CFLAGS +=3D $(APPEND_CFLAGS) +CFLAGS +=3D $(CFLAGS_xeninclude) +CFLAGS +=3D -I./include/ + +LDFLAGS +=3D $(APPEND_LDFLAGS) + +test-domid: domid.o find-next-bit.o test-domid.o +=09$(CC) $^ -o $@ $(LDFLAGS) + +-include $(DEPS_INCLUDE) diff --git a/tools/tests/domid/harness.h b/tools/tests/domid/harness.h new file mode 100644 index 000000000000..17eb22a9a854 --- /dev/null +++ b/tools/tests/domid/harness.h @@ -0,0 +1,54 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Unit test harness for domain ID allocator. + * + * Copyright 2025 Ford Motor Company + */ + +#ifndef _TEST_HARNESS_ +#define _TEST_HARNESS_ + +#include +#include +#include +#include + +#include +#include + +typedef bool spinlock_t; +typedef uint16_t domid_t; + +extern domid_t domid_alloc(domid_t domid); +extern void domid_free(domid_t domid); + +extern unsigned long find_next_zero_bit(const unsigned long *addr, + unsigned long size, + unsigned long offset); + +#define __test_and_set_bit(nr, addr) test_and_set_bit(nr, addr) +#define __test_and_clear_bit(nr, addr) test_and_clear_bit(nr, addr) +#define __set_bit(nr, addr) set_bit(nr, addr) + +#define BUG_ON(x) assert(!(x)) +#define ASSERT(x) assert(x) + +#define DEFINE_SPINLOCK(l) spinlock_t l +#define spin_lock(l) (assert(!*(l)), *(l) =3D true) +#define spin_unlock(l) (assert(*(l)), *(l) =3D false) + +#define printk printf + +#define DOMID_FIRST_RESERVED (100) +#define DOMID_INVALID (101) + +#endif /* _TEST_HARNESS_ */ + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/tools/tests/domid/include/xen/domain.h b/tools/tests/domid/inc= lude/xen/domain.h new file mode 120000 index 000000000000..2eda9aed088e --- /dev/null +++ b/tools/tests/domid/include/xen/domain.h @@ -0,0 +1 @@ +../../harness.h \ No newline at end of file diff --git a/tools/tests/domid/test-domid.c b/tools/tests/domid/test-domid.= c new file mode 100644 index 000000000000..51a88a6a9550 --- /dev/null +++ b/tools/tests/domid/test-domid.c @@ -0,0 +1,93 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* + * Unit tests for domain ID allocator. + * + * Copyright 2025 Ford Motor Company + */ + +#include "harness.h" + +#define verify(exp, fmt, args...) do { \ + if ( !(exp) ) \ + printf(fmt, ## args); \ + assert(exp); \ +} while (0); + +/* + * Fail on the first error, since tests are dependent on each other. + */ +int main(int argc, char **argv) +{ + domid_t expected, allocated; + + /* Test ID cannot be allocated twice. */ + for ( expected =3D 0; expected < DOMID_FIRST_RESERVED; expected++ ) + { + allocated =3D domid_alloc(expected); + verify(allocated =3D=3D expected, + "TEST 1: expected %u allocated %u\n", expected, allocated); + } + for ( expected =3D 0; expected < DOMID_FIRST_RESERVED; expected++ ) + { + allocated =3D domid_alloc(expected); + verify(allocated =3D=3D DOMID_INVALID, + "TEST 2: expected %u allocated %u\n", DOMID_INVALID, alloca= ted); + } + + /* Ensure all IDs, including ID#0 are not allocated. */ + for ( expected =3D 0; expected < DOMID_FIRST_RESERVED; expected++ ) + domid_free(expected); + + /* + * Test that that two consecutive calls of domid_alloc(DOMID_INVALID) + * will never return the same ID. + * NB: ID#0 is reserved and shall not be allocated by + * domid_alloc(DOMID_INVALID). + */ + for ( expected =3D 1; expected < DOMID_FIRST_RESERVED; expected++ ) + { + allocated =3D domid_alloc(DOMID_INVALID); + verify(allocated =3D=3D expected, + "TEST 3: expected %u allocated %u\n", expected, allocated); + } + for ( expected =3D 1; expected < DOMID_FIRST_RESERVED; expected++ ) + { + allocated =3D domid_alloc(DOMID_INVALID); + verify(allocated =3D=3D DOMID_INVALID, + "TEST 4: expected %u allocated %u\n", DOMID_INVALID, alloca= ted); + } + + /* Re-allocate first ID from [1..DOMID_FIRST_RESERVED/2]. */ + for ( expected =3D 1; expected < DOMID_FIRST_RESERVED / 2; expected++ = ) + domid_free(expected); + for ( expected =3D 1; expected < DOMID_FIRST_RESERVED / 2; expected++ = ) + { + allocated =3D domid_alloc(DOMID_INVALID); + verify(allocated =3D=3D expected, + "TEST 5: expected %u allocated %u\n", expected, allocated); + } + + /* Re-allocate last ID from [1..DOMID_FIRST_RESERVED - 1]. */ + expected =3D DOMID_FIRST_RESERVED - 1; + domid_free(DOMID_FIRST_RESERVED - 1); + allocated =3D domid_alloc(DOMID_INVALID); + verify(allocated =3D=3D expected, + "TEST 6: expected %u allocated %u\n", expected, allocated); + + /* Allocate an invalid ID. */ + expected =3D DOMID_INVALID; + allocated =3D domid_alloc(DOMID_FIRST_RESERVED); + verify(allocated =3D=3D expected, + "TEST 7: expected %u allocated %u\n", expected, allocated); + + return 0; +} + +/* + * Local variables: + * mode: C + * c-file-style: "BSD" + * c-basic-offset: 4 + * indent-tabs-mode: nil + * End: + */ diff --git a/xen/lib/find-next-bit.c b/xen/lib/find-next-bit.c index 9b8d7814f20c..539c7f2022b0 100644 --- a/xen/lib/find-next-bit.c +++ b/xen/lib/find-next-bit.c @@ -8,8 +8,13 @@ * as published by the Free Software Foundation; either version * 2 of the License, or (at your option) any later version. */ + +#ifdef __XEN_TOOLS__ +#include +#else #include #include +#endif =20 #define __ffs(x) (ffsl(x) - 1) #define ffz(x) __ffs(~(x)) --=20 2.34.1