[PATCH v3] bcache: Use vmalloc_array() to improve code

tanze posted 1 patch 3 months, 2 weeks ago
drivers/md/bcache/sysfs.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
[PATCH v3] bcache: Use vmalloc_array() to improve code
Posted by tanze 3 months, 2 weeks ago
Remove array_size() calls and replace vmalloc(),
due to vmalloc_array() being optimized better,
using fewer instructions, and handling overflow more concisely.

Signed-off-by: tanze <tanze@kylinos.cn>

---
Hi, Coly Li.

Thank you for your prompt reply. 
I have resubmitted the v3 version of the patch, 
with revisions made as per your requirements.

Best regards,
Ze Tan
---
Changes in v3:
- The empty line has been deleted.
---
 drivers/md/bcache/sysfs.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/md/bcache/sysfs.c b/drivers/md/bcache/sysfs.c
index 826b14cae4e5..7bb5605ad7fb 100644
--- a/drivers/md/bcache/sysfs.c
+++ b/drivers/md/bcache/sysfs.c
@@ -1061,8 +1061,7 @@ SHOW(__bch_cache)
 		uint16_t q[31], *p, *cached;
 		ssize_t ret;
 
-		cached = p = vmalloc(array_size(sizeof(uint16_t),
-						ca->sb.nbuckets));
+		cached = p = vmalloc_array(ca->sb.nbuckets, sizeof(uint16_t));
 		if (!p)
 			return -ENOMEM;
 
-- 
2.25.1
Re: [PATCH v3] bcache: Use vmalloc_array() to improve code
Posted by Coly Li 3 months, 2 weeks ago
On Thu, Oct 23, 2025 at 02:06:29PM +0800, tanze wrote:
> Remove array_size() calls and replace vmalloc(),
> due to vmalloc_array() being optimized better,
> using fewer instructions, and handling overflow more concisely.
> 
> Signed-off-by: tanze <tanze@kylinos.cn>
> 
> ---
> Hi, Coly Li.
> 
> Thank you for your prompt reply. 
> I have resubmitted the v3 version of the patch, 
> with revisions made as per your requirements.
> 
> Best regards,
> Ze Tan
> ---
> Changes in v3:
> - The empty line has been deleted.
> ---
>  drivers/md/bcache/sysfs.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/drivers/md/bcache/sysfs.c b/drivers/md/bcache/sysfs.c
> index 826b14cae4e5..7bb5605ad7fb 100644
> --- a/drivers/md/bcache/sysfs.c
> +++ b/drivers/md/bcache/sysfs.c
> @@ -1061,8 +1061,7 @@ SHOW(__bch_cache)
>  		uint16_t q[31], *p, *cached;
>  		ssize_t ret;
>  
> -		cached = p = vmalloc(array_size(sizeof(uint16_t),
> -						ca->sb.nbuckets));
> +		cached = p = vmalloc_array(ca->sb.nbuckets, sizeof(uint16_t));
>  		if (!p)
>  			return -ENOMEM;
> 

It looks good to me. I take it into my for-next. Thanks.

Coly Li