mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH 1/2] selftests: iommu: suppress -Warray-bounds in _test_cmd_get_hw_info
@ 2026-09-08 21:48 Eva Kurchatova
  2026-09-08 21:48 ` [PATCH 2/2] selftests: iommu: skip when the mock device is not there Eva Kurchatova
  2026-09-09 16:31 ` [PATCH 1/2] selftests: iommu: suppress -Warray-bounds in _test_cmd_get_hw_info Jason Gunthorpe
  0 siblings, 2 replies; 4+ messages in thread
From: Eva Kurchatova @ 2026-09-08 21:48 UTC (permalink / raw)
  To: Jason Gunthorpe, Kevin Tian, Shuah Khan
  Cc: linux-kernel, Eva Kurchatova, iommu, linux-kselftest

  In function '_test_cmd_get_hw_info',
      inlined from 'iommufd_ioas_get_hw_info' at iommufd.c:614:3,
      inlined from 'wrapper_iommufd_ioas_get_hw_info' at iommufd.c:589:1:
  iommufd_utils.h:683:37: warning: array subscript 'struct iommu_test_hw_info[0]'
  is partly outside array bounds of 'struct iommu_test_hw_info_buffer_smaller[1]'
  [-Warray-bounds=]
    683 |                         assert(!info->flags);
        |                                 ~~~~^~~~~~~

The test intentionally passes a smaller buffer to exercise the kernel's
handling of undersized user buffers. Runtime data_len checks make the
access safe, but GCC cannot prove this statically. Suppress the warning
locally with a pragma.

Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
 tools/testing/selftests/iommu/iommufd_utils.h | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/tools/testing/selftests/iommu/iommufd_utils.h b/tools/testing/selftests/iommu/iommufd_utils.h
index b4928cbd4d9c..c4dfa05e0612 100644
--- a/tools/testing/selftests/iommu/iommufd_utils.h
+++ b/tools/testing/selftests/iommu/iommufd_utils.h
@@ -866,12 +866,19 @@ static int _test_cmd_get_hw_info(int fd, __u32 device_id, __u32 data_type,
 		}
 	}
 
+	/*
+	 * When data_len is smaller than sizeof(*info), GCC warns about
+	 * out-of-bounds access.  The runtime data_len checks make this safe.
+	 */
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Warray-bounds"
 	if (info) {
 		if (data_len >= offsetofend(struct iommu_test_hw_info, test_reg))
 			assert(info->test_reg == IOMMU_HW_INFO_SELFTEST_REGVAL);
 		if (data_len >= offsetofend(struct iommu_test_hw_info, flags))
 			assert(!info->flags);
 	}
+#pragma GCC diagnostic pop
 
 	if (max_pasid)
 		*max_pasid = cmd.out_max_pasid_log2;
-- 
2.55.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH 2/2] selftests: iommu: skip when the mock device is not there
  2026-09-08 21:48 [PATCH 1/2] selftests: iommu: suppress -Warray-bounds in _test_cmd_get_hw_info Eva Kurchatova
@ 2026-09-08 21:48 ` Eva Kurchatova
  2026-09-09 16:33   ` Jason Gunthorpe
  2026-09-09 16:31 ` [PATCH 1/2] selftests: iommu: suppress -Warray-bounds in _test_cmd_get_hw_info Jason Gunthorpe
  1 sibling, 1 reply; 4+ messages in thread
From: Eva Kurchatova @ 2026-09-08 21:48 UTC (permalink / raw)
  To: Jason Gunthorpe, Kevin Tian, Shuah Khan
  Cc: linux-kernel, Eva Kurchatova, iommu, linux-kselftest

Both iommufd tests need the mock device the iommufd module registers
under CONFIG_IOMMUFD_TEST, and they open /dev/iommu in every fixture
without checking, so on a kernel that does not provide it every case
fails on the open.

Checking the open is not enough on its own. /dev/iommu appears as soon
as the iommufd module is loaded, which says nothing about the mock
device: the release config here is CONFIG_IOMMUFD=m with
CONFIG_IOMMUFD_TEST unset, so the open succeeds and the tests fail later
in the ioctls that drive the mock device instead. Only the debug config
sets CONFIG_IOMMUFD_TEST.

/sys/bus/iommufd_mock covers both, it exists only once the module is
loaded and the kernel was built with CONFIG_IOMMUFD_TEST. Check for it
once before handing over to the harness, rather than in each of the
fixtures, and report a skip. test_harness_run() is what TEST_HARNESS_MAIN
calls anyway, so the tests still run exactly as they did.

Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
---
 tools/testing/selftests/iommu/iommufd.c          | 15 ++++++++++++++-
 tools/testing/selftests/iommu/iommufd_fail_nth.c | 15 ++++++++++++++-
 2 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c
index d44b34b05757..446306badcdc 100644
--- a/tools/testing/selftests/iommu/iommufd.c
+++ b/tools/testing/selftests/iommu/iommufd.c
@@ -3613,4 +3613,17 @@ TEST_F(iommufd_device_pasid, pasid_attach)
 	test_cmd_mock_domain_replace(self->stdev_id, self->ioas_id);
 }
 
-TEST_HARNESS_MAIN
+static bool iommufd_mock_available(void)
+{
+	return access("/sys/bus/iommufd_mock", F_OK) == 0;
+}
+
+int main(int argc, char **argv)
+{
+	if (!iommufd_mock_available())
+		ksft_exit_skip("no iommufd mock device, the tests need "
+			       "CONFIG_IOMMUFD_TEST=y and the iommufd "
+			       "module loaded\n");
+
+	return test_harness_run(argc, argv);
+}
diff --git a/tools/testing/selftests/iommu/iommufd_fail_nth.c b/tools/testing/selftests/iommu/iommufd_fail_nth.c
index 25495d8dceb3..c516e00f7316 100644
--- a/tools/testing/selftests/iommu/iommufd_fail_nth.c
+++ b/tools/testing/selftests/iommu/iommufd_fail_nth.c
@@ -745,4 +745,17 @@ TEST_FAIL_NTH(basic_fail_nth, device)
 	return 0;
 }
 
-TEST_HARNESS_MAIN
+static bool iommufd_mock_available(void)
+{
+	return access("/sys/bus/iommufd_mock", F_OK) == 0;
+}
+
+int main(int argc, char **argv)
+{
+	if (!iommufd_mock_available())
+		ksft_exit_skip("no iommufd mock device, the tests need "
+			       "CONFIG_IOMMUFD_TEST=y and the iommufd "
+			       "module loaded\n");
+
+	return test_harness_run(argc, argv);
+}
-- 
2.55.0


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 1/2] selftests: iommu: suppress -Warray-bounds in _test_cmd_get_hw_info
  2026-09-08 21:48 [PATCH 1/2] selftests: iommu: suppress -Warray-bounds in _test_cmd_get_hw_info Eva Kurchatova
  2026-09-08 21:48 ` [PATCH 2/2] selftests: iommu: skip when the mock device is not there Eva Kurchatova
@ 2026-09-09 16:31 ` Jason Gunthorpe
  1 sibling, 0 replies; 4+ messages in thread
From: Jason Gunthorpe @ 2026-09-09 16:31 UTC (permalink / raw)
  To: Eva Kurchatova
  Cc: Jason Gunthorpe, Kevin Tian, Shuah Khan, linux-kernel, iommu,
	linux-kselftest

>   In function '_test_cmd_get_hw_info',
>       inlined from 'iommufd_ioas_get_hw_info' at iommufd.c:614:3,
>       inlined from 'wrapper_iommufd_ioas_get_hw_info' at iommufd.c:589:1:
>   iommufd_utils.h:683:37: warning: array subscript 'struct iommu_test_hw_info[0]'
>   is partly outside array bounds of 'struct iommu_test_hw_info_buffer_smaller[1]'
>   [-Warray-bounds=]

?? My tree doesn't have anything like
iommu_test_hw_info_buffer_smaller ? And all these line numbers don't
match mine.

How are you triggering the warning? The struct memory is properly
sized to do this:

>     683 |                         assert(!info->flags);

I prefer not to have the #pragma things, I don't think there should be
a reason for them here.

-- 
Jason

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH 2/2] selftests: iommu: skip when the mock device is not there
  2026-09-08 21:48 ` [PATCH 2/2] selftests: iommu: skip when the mock device is not there Eva Kurchatova
@ 2026-09-09 16:33   ` Jason Gunthorpe
  0 siblings, 0 replies; 4+ messages in thread
From: Jason Gunthorpe @ 2026-09-09 16:33 UTC (permalink / raw)
  To: Eva Kurchatova
  Cc: Kevin Tian, Shuah Khan, linux-kernel, iommu, linux-kselftest

On Wed, Sep 09, 2026 at 12:48:12AM +0300, Eva Kurchatova wrote:
> Both iommufd tests need the mock device the iommufd module registers
> under CONFIG_IOMMUFD_TEST, and they open /dev/iommu in every fixture
> without checking, so on a kernel that does not provide it every case
> fails on the open.
> 
> Checking the open is not enough on its own. /dev/iommu appears as soon
> as the iommufd module is loaded, which says nothing about the mock
> device: the release config here is CONFIG_IOMMUFD=m with
> CONFIG_IOMMUFD_TEST unset, so the open succeeds and the tests fail later
> in the ioctls that drive the mock device instead. Only the debug config
> sets CONFIG_IOMMUFD_TEST.
> 
> /sys/bus/iommufd_mock covers both, it exists only once the module is
> loaded and the kernel was built with CONFIG_IOMMUFD_TEST. Check for it
> once before handing over to the harness, rather than in each of the
> fixtures, and report a skip. test_harness_run() is what TEST_HARNESS_MAIN
> calls anyway, so the tests still run exactly as they did.
> 
> Signed-off-by: Eva Kurchatova <eva.kurchatova@virtuozzo.com>
> ---
>  tools/testing/selftests/iommu/iommufd.c          | 15 ++++++++++++++-
>  tools/testing/selftests/iommu/iommufd_fail_nth.c | 15 ++++++++++++++-
>  2 files changed, 28 insertions(+), 2 deletions(-)

Applied thanks

Jason

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2026-09-09 16:33 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-09-08 21:48 [PATCH 1/2] selftests: iommu: suppress -Warray-bounds in _test_cmd_get_hw_info Eva Kurchatova
2026-09-08 21:48 ` [PATCH 2/2] selftests: iommu: skip when the mock device is not there Eva Kurchatova
2026-09-09 16:33   ` Jason Gunthorpe
2026-09-09 16:31 ` [PATCH 1/2] selftests: iommu: suppress -Warray-bounds in _test_cmd_get_hw_info Jason Gunthorpe

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®