Skip to contentSkip to conversation

selftests/iommu: prevent use of uninitialized variable

PATCH Superseded Alessandro Zanni 3 participants 4 messages Sep 23, 2025 – Sep 24, 2025

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/iommu: prevent use of uninitialized variable

Fix to avoid the usage of the res variable uninitialized in the following macro expansions.

It solves the following warning:
In function ‘iommufd_viommu_vdevice_alloc’,
inlined from ‘wrapper_iommufd_viommu_vdevice_alloc’ at
iommufd.c:2889:1:
../kselftest_harness.h:760:12: warning: ‘ret’ may be used uninitialized [-Wmaybe-uninitialized]
760 | if (!(__exp _t __seen)) { \
| ^
../kselftest_harness.h:513:9: note: in expansion of macro ‘__EXPECT’

  513 |   __EXPECT(expected, #expected, seen, #seen, ==, 1)
      |   ^~~~~~~~
iommufd_utils.h:1057:9: note: in expansion of macro ‘ASSERT_EQ’
 1057 |   ASSERT_EQ(0, _test_cmd_trigger_vevents(self->fd, dev_id,
nvevents))
      |   ^~~~~~~~~
iommufd.c:2924:17: note: in expansion of macro
‘test_cmd_trigger_vevents’
 2924 |   test_cmd_trigger_vevents(dev_id, 3);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~

The issue can be reproduced, building the tests, with the command:
make -C tools/testing/selftests TARGETS=iommu

---
 tools/testing/selftests/iommu/iommufd_utils.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/testing/selftests/iommu/iommufd_utils.h +1 -1
index 3c3e08b8c90e..4ae0fcc4f871 100644
@@ -1042,7 +1042,7 @@ static int _test_cmd_trigger_vevents(int fd, __u32 dev_id, __u32 nvevents)
 			.dev_id = dev_id,
 		},
 	};
-	int ret;
+	int ret = 0;
 
 	while (nvevents--) {
 		ret = ioctl(fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_TRIGGER_VEVENT),
Signature · 1 line
2.43.0
#1 lore raw
Quoted 23 lines · Alessandro Zanni

Fix to avoid the usage of the res variable uninitialized in the following macro expansions.

It solves the following warning:
In function ‘iommufd_viommu_vdevice_alloc’,
inlined from ‘wrapper_iommufd_viommu_vdevice_alloc’ at
iommufd.c:2889:1:
../kselftest_harness.h:760:12: warning: ‘ret’ may be used uninitialized [-Wmaybe-uninitialized]
760 | if (!(__exp _t __seen)) { \
| ^
../kselftest_harness.h:513:9: note: in expansion of macro ‘__EXPECT’

  513 |   __EXPECT(expected, #expected, seen, #seen, ==, 1)
      |   ^~~~~~~~
iommufd_utils.h:1057:9: note: in expansion of macro ‘ASSERT_EQ’
 1057 |   ASSERT_EQ(0, _test_cmd_trigger_vevents(self->fd, dev_id,
nvevents))
      |   ^~~~~~~~~
iommufd.c:2924:17: note: in expansion of macro
‘test_cmd_trigger_vevents’
 2924 |   test_cmd_trigger_vevents(dev_id, 3);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~
Quoted 7 lines

The issue can be reproduced, building the tests, with the command:
make -C tools/testing/selftests TARGETS=iommu

---
 tools/testing/selftests/iommu/iommufd_utils.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

I think it should be like this?

@@ -1042,15 +1042,12 @@ static int _test_cmd_trigger_vevents(int fd, __u32 dev_id, __u32 nvevents)

                        .dev_id = dev_id,
                },
        };

- int ret;

- while (nvevents--) {
- ret = ioctl(fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_TRIGGER_VEVENT),
- &trigger_vevent_cmd);
- if (ret < 0)
+ while (nvevents--)
+ if (ioctl(fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_TRIGGER_VEVENT),
+ &trigger_vevent_cmd))

                        return -1;
-       }
-       return ret;
+       return 0;

}

And add a fixes line?

Jason

#2 lore raw
Quoted 55 lines · Jason Gunthorpe
Quoted 23 lines · Alessandro Zanni

Fix to avoid the usage of the res variable uninitialized in the following macro expansions.

It solves the following warning:
In function ‘iommufd_viommu_vdevice_alloc’,
inlined from ‘wrapper_iommufd_viommu_vdevice_alloc’ at
iommufd.c:2889:1:
../kselftest_harness.h:760:12: warning: ‘ret’ may be used uninitialized [-Wmaybe-uninitialized]
760 | if (!(__exp _t __seen)) { \
| ^
../kselftest_harness.h:513:9: note: in expansion of macro ‘__EXPECT’

  513 |   __EXPECT(expected, #expected, seen, #seen, ==, 1)
      |   ^~~~~~~~
iommufd_utils.h:1057:9: note: in expansion of macro ‘ASSERT_EQ’
 1057 |   ASSERT_EQ(0, _test_cmd_trigger_vevents(self->fd, dev_id,
nvevents))
      |   ^~~~~~~~~
iommufd.c:2924:17: note: in expansion of macro
‘test_cmd_trigger_vevents’
 2924 |   test_cmd_trigger_vevents(dev_id, 3);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~
Quoted 7 lines

The issue can be reproduced, building the tests, with the command:
make -C tools/testing/selftests TARGETS=iommu

---
 tools/testing/selftests/iommu/iommufd_utils.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

I think it should be like this?

(quoted diff) +4 -7
@@ -1042,15 +1042,12 @@ static int _test_cmd_trigger_vevents(int fd, __u32 dev_id, __u32 nvevents)
                        .dev_id = dev_id,
                },
        };
-       int ret;
 
-       while (nvevents--) {
-               ret = ioctl(fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_TRIGGER_VEVENT),
-                           &trigger_vevent_cmd);
-               if (ret < 0)
+       while (nvevents--)
+               if (ioctl(fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_TRIGGER_VEVENT),
+                         &trigger_vevent_cmd))
                        return -1;
-       }
-       return ret;
+       return 0;
 }
 

And add a fixes line?

Quoted 1 line

Jason

Thank you for the reply.
I'm not sure the right behavior the test should have:
- in the version you proposed, when ioctl() returns a positive value the loop ends and the next tests are skipped.
- in the original version, if the function ioctl() returns a positive value the loop continues with the following tests.

Which one is the desired behavior?

Thanks,
Alessandro

#3 lore raw
Quoted 65 lines · Alessandro Zanni
Quoted 55 lines · Jason Gunthorpe
Quoted 23 lines · Alessandro Zanni

Fix to avoid the usage of the res variable uninitialized in the following macro expansions.

It solves the following warning:
In function ‘iommufd_viommu_vdevice_alloc’,
inlined from ‘wrapper_iommufd_viommu_vdevice_alloc’ at
iommufd.c:2889:1:
../kselftest_harness.h:760:12: warning: ‘ret’ may be used uninitialized [-Wmaybe-uninitialized]
760 | if (!(__exp _t __seen)) { \
| ^
../kselftest_harness.h:513:9: note: in expansion of macro ‘__EXPECT’

  513 |   __EXPECT(expected, #expected, seen, #seen, ==, 1)
      |   ^~~~~~~~
iommufd_utils.h:1057:9: note: in expansion of macro ‘ASSERT_EQ’
 1057 |   ASSERT_EQ(0, _test_cmd_trigger_vevents(self->fd, dev_id,
nvevents))
      |   ^~~~~~~~~
iommufd.c:2924:17: note: in expansion of macro
‘test_cmd_trigger_vevents’
 2924 |   test_cmd_trigger_vevents(dev_id, 3);
      |   ^~~~~~~~~~~~~~~~~~~~~~~~
Quoted 7 lines

The issue can be reproduced, building the tests, with the command:
make -C tools/testing/selftests TARGETS=iommu

---
 tools/testing/selftests/iommu/iommufd_utils.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

I think it should be like this?

(quoted diff) +4 -7
@@ -1042,15 +1042,12 @@ static int _test_cmd_trigger_vevents(int fd, __u32 dev_id, __u32 nvevents)
                        .dev_id = dev_id,
                },
        };
-       int ret;
 
-       while (nvevents--) {
-               ret = ioctl(fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_TRIGGER_VEVENT),
-                           &trigger_vevent_cmd);
-               if (ret < 0)
+       while (nvevents--)
+               if (ioctl(fd, _IOMMU_TEST_CMD(IOMMU_TEST_OP_TRIGGER_VEVENT),
+                         &trigger_vevent_cmd))
                        return -1;
-       }
-       return ret;
+       return 0;
 }
 

And add a fixes line?

Quoted 1 line

Jason

Thank you for the reply.
I'm not sure the right behavior the test should have:
- in the version you proposed, when ioctl() returns a positive value the loop ends and the next tests are skipped.
- in the original version, if the function ioctl() returns a positive value the loop continues with the following tests.

Quoted 1 line

Which one is the desired behavior?

Sorry it should be !ioctl()

The ioctl will only return 0 or -errno

Jason