[PATCH] selftests: kvm: Fix transposed calloc() arguments in the uffd helper

Chaithanya Lagisetty posted 1 patch 3 weeks, 1 day ago
tools/testing/selftests/kvm/lib/userfaultfd_util.c | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
[PATCH] selftests: kvm: Fix transposed calloc() arguments in the uffd helper
Posted by Chaithanya Lagisetty 3 weeks, 1 day ago
uffd_setup_demand_paging() passes the element size as the first argument
to calloc() and the element count as the second:

	uffd_desc->pipefds = calloc(sizeof(int), num_readers);

calloc() expects the number of elements first and the size of each element
second. The product, and therefore the allocation size, is the same either
way, so this is not a functional change, but the arguments are misleading
and GCC 14 diagnoses them with -Wcalloc-transposed-args, which is enabled
by -Wextra.

Swap the arguments to match the calloc() prototype.

Signed-off-by: Chaithanya Lagisetty <nagachaithanya9911@gmail.com>
---
 tools/testing/selftests/kvm/lib/userfaultfd_util.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/tools/testing/selftests/kvm/lib/userfaultfd_util.c b/tools/testing/selftests/kvm/lib/userfaultfd_util.c
index f7ce5a6ddcc2..49eff05b7d39 100644
--- a/tools/testing/selftests/kvm/lib/userfaultfd_util.c
+++ b/tools/testing/selftests/kvm/lib/userfaultfd_util.c
@@ -119,13 +119,13 @@ struct uffd_desc *uffd_setup_demand_paging(int uffd_mode, useconds_t delay,
 	uffd_desc = malloc(sizeof(struct uffd_desc));
 	TEST_ASSERT(uffd_desc, "Failed to malloc uffd descriptor");
 
-	uffd_desc->pipefds = calloc(sizeof(int), num_readers);
+	uffd_desc->pipefds = calloc(num_readers, sizeof(int));
 	TEST_ASSERT(uffd_desc->pipefds, "Failed to alloc pipes");
 
-	uffd_desc->readers = calloc(sizeof(pthread_t), num_readers);
+	uffd_desc->readers = calloc(num_readers, sizeof(pthread_t));
 	TEST_ASSERT(uffd_desc->readers, "Failed to alloc reader threads");
 
-	uffd_desc->reader_args = calloc(sizeof(struct uffd_reader_args), num_readers);
+	uffd_desc->reader_args = calloc(num_readers, sizeof(struct uffd_reader_args));
 	TEST_ASSERT(uffd_desc->reader_args, "Failed to alloc reader_args");
 
 	uffd_desc->num_readers = num_readers;
-- 
2.43.0
Re: [PATCH] selftests: kvm: Fix transposed calloc() arguments in the uffd helper
Posted by Sean Christopherson 4 days, 15 hours ago
On Fri, 04 Sep 2026 05:20:33 +0000, Chaithanya Lagisetty wrote:
> uffd_setup_demand_paging() passes the element size as the first argument
> to calloc() and the element count as the second:
> 
> 	uffd_desc->pipefds = calloc(sizeof(int), num_readers);
> 
> calloc() expects the number of elements first and the size of each element
> second. The product, and therefore the allocation size, is the same either
> way, so this is not a functional change, but the arguments are misleading
> and GCC 14 diagnoses them with -Wcalloc-transposed-args, which is enabled
> by -Wextra.
> 
> [...]

Applied to kvm-x86 selftests, thanks!

[1/1] selftests: kvm: Fix transposed calloc() arguments in the uffd helper
      https://github.com/kvm-x86/linux/commit/202378243192

--
https://github.com/kvm-x86/linux/tree/next
Re: [PATCH] selftests: kvm: Fix transposed calloc() arguments in the uffd helper
Posted by Sean Christopherson 2 weeks ago
On Fri, Sep 04, 2026, Chaithanya Lagisetty wrote:
> uffd_setup_demand_paging() passes the element size as the first argument
> to calloc() and the element count as the second:
> 
> 	uffd_desc->pipefds = calloc(sizeof(int), num_readers);
> 
> calloc() expects the number of elements first and the size of each element
> second. The product, and therefore the allocation size, is the same either
> way, so this is not a functional change, but the arguments are misleading
> and GCC 14 diagnoses them with -Wcalloc-transposed-args, which is enabled
> by -Wextra.

Heh, the calloc() implementation provided by tools/include/nolibc/stdlib.h gets
this wrong.  But KVM selftests don't use nolibc, i.e. that's someone else's problem
to deal with.
Re: [PATCH] selftests: kvm: Fix transposed calloc() arguments in the uffd helper
Posted by Gautam Menghani 2 weeks, 2 days ago
On Fri, Sep 04, 2026 at 05:20:33AM +0000, Chaithanya Lagisetty wrote:
> uffd_setup_demand_paging() passes the element size as the first argument
> to calloc() and the element count as the second:
> 
> 	uffd_desc->pipefds = calloc(sizeof(int), num_readers);
> 
> calloc() expects the number of elements first and the size of each element
> second. The product, and therefore the allocation size, is the same either
> way, so this is not a functional change, but the arguments are misleading
> and GCC 14 diagnoses them with -Wcalloc-transposed-args, which is enabled
> by -Wextra.
> 
> Swap the arguments to match the calloc() prototype.
> 
> Signed-off-by: Chaithanya Lagisetty <nagachaithanya9911@gmail.com>
> ---
>  tools/testing/selftests/kvm/lib/userfaultfd_util.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/tools/testing/selftests/kvm/lib/userfaultfd_util.c b/tools/testing/selftests/kvm/lib/userfaultfd_util.c
> index f7ce5a6ddcc2..49eff05b7d39 100644
> --- a/tools/testing/selftests/kvm/lib/userfaultfd_util.c
> +++ b/tools/testing/selftests/kvm/lib/userfaultfd_util.c
> @@ -119,13 +119,13 @@ struct uffd_desc *uffd_setup_demand_paging(int uffd_mode, useconds_t delay,
>  	uffd_desc = malloc(sizeof(struct uffd_desc));
>  	TEST_ASSERT(uffd_desc, "Failed to malloc uffd descriptor");
>  
> -	uffd_desc->pipefds = calloc(sizeof(int), num_readers);
> +	uffd_desc->pipefds = calloc(num_readers, sizeof(int));
>  	TEST_ASSERT(uffd_desc->pipefds, "Failed to alloc pipes");
>  
> -	uffd_desc->readers = calloc(sizeof(pthread_t), num_readers);
> +	uffd_desc->readers = calloc(num_readers, sizeof(pthread_t));
>  	TEST_ASSERT(uffd_desc->readers, "Failed to alloc reader threads");
>  
> -	uffd_desc->reader_args = calloc(sizeof(struct uffd_reader_args), num_readers);
> +	uffd_desc->reader_args = calloc(num_readers, sizeof(struct uffd_reader_args));
>  	TEST_ASSERT(uffd_desc->reader_args, "Failed to alloc reader_args");
>  
>  	uffd_desc->num_readers = num_readers;
> -- 
> 2.43.0
> 

Good catch
Reviewed-by: Gautam Menghani <gautam@linux.ibm.com>