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

tanze posted 1 patch 3 months, 3 weeks ago
There is a newer version of this series
drivers/md/bcache/sysfs.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
[PATCH v1] bcache: Use vmalloc_array() to improve code
Posted by tanze 3 months, 3 weeks ago
Remove array_size() calls and replace vmalloc(), Due to vmalloc_array() is optimized better,
uses fewer instructions, and handles overflow more concisely[1].

Signed-off-by: tanze <tanze@kylinos.cn>
---
 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..dc568e8eb6eb 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 v1] bcache: Use vmalloc_array() to improve code
Posted by Coly Li 3 months, 2 weeks ago
On Fri, Oct 17, 2025 at 07:13:06PM +0800, tanze wrote:
> Remove array_size() calls and replace vmalloc(), Due to vmalloc_array() is optimized better,
> uses fewer instructions, and handles overflow more concisely[1].
> 
> Signed-off-by: tanze <tanze@kylinos.cn>
> ---
>  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..dc568e8eb6eb 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));
                                                          ^^^-> a blank missing?
>  		if (!p)
>  			return -ENOMEM;

Except for the missing blank, overall the patch is fine.

BTW, IMHO tanze is not a formal method to spell the name, could you please
use a formal format? It will be helpful to identify your contribution in
future.


Thanks.

Coly Li
[PATCH v1] bcache: Use vmalloc_array() to improve code
Posted by tanze 3 months, 3 weeks ago
Remove array_size() calls and replace vmalloc(), Due to vmalloc_array() is optimized better,
uses fewer instructions, and handles overflow more concisely[1].

Signed-off-by: tanze <tanze@kylinos.cn>
---
Please ignore the previous email, a simple formatting error 
has been corrected in this one.

Thanks
---
 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 v1] bcache: Use vmalloc_array() to improve code
Posted by Coly Li 3 months, 2 weeks ago
On Fri, Oct 17, 2025 at 07:27:39PM +0800, tanze wrote:
> Remove array_size() calls and replace vmalloc(), Due to vmalloc_array() is optimized better,
> uses fewer instructions, and handles overflow more concisely[1].
> 
> Signed-off-by: tanze <tanze@kylinos.cn>
> ---
> Please ignore the previous email, a simple formatting error 
> has been corrected in this one.
> 
> Thanks
> ---
>  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;

Yes, this version is better.
It will be cool if you may refine the name format in your Signed-off-by.

Thanks.

Coly Li