[PATCH] crypto: qat - use kcalloc() in qat_uclo_map_objs_from_mof()

Qianfeng Rong posted 1 patch 1 month, 1 week ago
There is a newer version of this series
drivers/crypto/intel/qat/qat_common/qat_uclo.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] crypto: qat - use kcalloc() in qat_uclo_map_objs_from_mof()
Posted by Qianfeng Rong 1 month, 1 week ago
As noted in the kernel documentation [1], open-coded multiplication in
allocator arguments is discouraged because it can lead to integer overflow.

Use kcalloc() to gain built-in overflow protection, making memory
allocation safer when calculating allocation size compared to explicit
multiplication.

[1]: https://www.kernel.org/doc/html/next/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments

Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
---
 drivers/crypto/intel/qat/qat_common/qat_uclo.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/crypto/intel/qat/qat_common/qat_uclo.c b/drivers/crypto/intel/qat/qat_common/qat_uclo.c
index 21d652a1c8ef..23f6c2c659fc 100644
--- a/drivers/crypto/intel/qat/qat_common/qat_uclo.c
+++ b/drivers/crypto/intel/qat/qat_common/qat_uclo.c
@@ -1900,7 +1900,7 @@ static int qat_uclo_map_objs_from_mof(struct icp_qat_mof_handle *mobj_handle)
 	if (sobj_hdr)
 		sobj_chunk_num = sobj_hdr->num_chunks;
 
-	mobj_hdr = kzalloc((uobj_chunk_num + sobj_chunk_num) *
+	mobj_hdr = kcalloc(uobj_chunk_num + sobj_chunk_num,
 			   sizeof(*mobj_hdr), GFP_KERNEL);
 	if (!mobj_hdr)
 		return -ENOMEM;
-- 
2.34.1
Re: [PATCH] crypto: qat - use kcalloc() in qat_uclo_map_objs_from_mof()
Posted by Andy Shevchenko 1 month, 1 week ago
On Thu, Aug 21, 2025 at 08:35:42PM +0800, Qianfeng Rong wrote:
> As noted in the kernel documentation [1], open-coded multiplication in
> allocator arguments is discouraged because it can lead to integer overflow.
> 
> Use kcalloc() to gain built-in overflow protection, making memory
> allocation safer when calculating allocation size compared to explicit
> multiplication.

> [1]: https://www.kernel.org/doc/html/next/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments
> 
> Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>

Make it a Link tag.

Link: https://www.kernel.org/doc/html/next/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments #1
Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>

(also note no blank lines in tag block)

...

> -	mobj_hdr = kzalloc((uobj_chunk_num + sobj_chunk_num) *
> +	mobj_hdr = kcalloc(uobj_chunk_num + sobj_chunk_num,

size_add() ?

>  			   sizeof(*mobj_hdr), GFP_KERNEL);
>  	if (!mobj_hdr)
>  		return -ENOMEM;

-- 
With Best Regards,
Andy Shevchenko
Re: [PATCH] crypto: qat - use kcalloc() in qat_uclo_map_objs_from_mof()
Posted by Qianfeng Rong 1 month, 1 week ago
在 2025/8/21 20:56, Andy Shevchenko 写道:
> [You don't often get email from andriy.shevchenko@linux.intel.com. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]
>
> On Thu, Aug 21, 2025 at 08:35:42PM +0800, Qianfeng Rong wrote:
>> As noted in the kernel documentation [1], open-coded multiplication in
>> allocator arguments is discouraged because it can lead to integer overflow.
>>
>> Use kcalloc() to gain built-in overflow protection, making memory
>> allocation safer when calculating allocation size compared to explicit
>> multiplication.
>> [1]: https://www.kernel.org/doc/html/next/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments
>>
>> Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
> Make it a Link tag.
>
> Link: https://www.kernel.org/doc/html/next/process/deprecated.html#open-coded-arithmetic-in-allocator-arguments #1
> Signed-off-by: Qianfeng Rong <rongqianfeng@vivo.com>
>
> (also note no blank lines in tag block)
Thank you, Andy—I've learned another useful detail.
>
> ...
>
>> -     mobj_hdr = kzalloc((uobj_chunk_num + sobj_chunk_num) *
>> +     mobj_hdr = kcalloc(uobj_chunk_num + sobj_chunk_num,
> size_add() ?
Yes, similar to array_size(), using size_add() here is safer.  I will 
post v2 later.
>
>>                           sizeof(*mobj_hdr), GFP_KERNEL);
>>        if (!mobj_hdr)
>>                return -ENOMEM;
> --
> With Best Regards,
> Andy Shevchenko
Best regards,
Qianfeng