diff mbox series

[v2] nvdimm-btt: simplify code with the scope based resource management

Message ID 20231214083919.22218-1-dinghao.liu@zju.edu.cn (mailing list archive)
State Accepted
Commit fdc504b3ef7af745f326d6ecf9a3c1c5affbfd9b
Delegated to: Ira Weiny
Headers show
Series [v2] nvdimm-btt: simplify code with the scope based resource management | expand

Commit Message

Dinghao Liu Dec. 14, 2023, 8:39 a.m. UTC
Use the scope based resource management (defined in
linux/cleanup.h) to automate resource lifetime
control on struct btt_sb *super in discover_arenas().

Signed-off-by: Dinghao Liu <dinghao.liu@zju.edu.cn>
---

Changelog:

v2: Set the __free attribute before kzalloc.
---
 drivers/nvdimm/btt.c | 13 ++++---------
 1 file changed, 4 insertions(+), 9 deletions(-)

Comments

Dave Jiang Dec. 14, 2023, 3:42 p.m. UTC | #1
On 12/14/23 01:39, Dinghao Liu wrote:
> Use the scope based resource management (defined in
> linux/cleanup.h) to automate resource lifetime
> control on struct btt_sb *super in discover_arenas().
> 
> Signed-off-by: Dinghao Liu <dinghao.liu@zju.edu.cn>
Reviewed-by: Dave Jiang <dave.jiang@intel.com>
> ---
> 
> Changelog:
> 
> v2: Set the __free attribute before kzalloc.
> ---
>  drivers/nvdimm/btt.c | 13 ++++---------
>  1 file changed, 4 insertions(+), 9 deletions(-)
> 
> diff --git a/drivers/nvdimm/btt.c b/drivers/nvdimm/btt.c
> index d5593b0dc700..32a9e2f543c5 100644
> --- a/drivers/nvdimm/btt.c
> +++ b/drivers/nvdimm/btt.c
> @@ -16,6 +16,7 @@
>  #include <linux/fs.h>
>  #include <linux/nd.h>
>  #include <linux/backing-dev.h>
> +#include <linux/cleanup.h>
>  #include "btt.h"
>  #include "nd.h"
>  
> @@ -847,23 +848,20 @@ static int discover_arenas(struct btt *btt)
>  {
>  	int ret = 0;
>  	struct arena_info *arena;
> -	struct btt_sb *super;
>  	size_t remaining = btt->rawsize;
>  	u64 cur_nlba = 0;
>  	size_t cur_off = 0;
>  	int num_arenas = 0;
>  
> -	super = kzalloc(sizeof(*super), GFP_KERNEL);
> +	struct btt_sb *super __free(kfree) = kzalloc(sizeof(*super), GFP_KERNEL);
>  	if (!super)
>  		return -ENOMEM;
>  
>  	while (remaining) {
>  		/* Alloc memory for arena */
>  		arena = alloc_arena(btt, 0, 0, 0);
> -		if (!arena) {
> -			ret = -ENOMEM;
> -			goto out_super;
> -		}
> +		if (!arena)
> +			return -ENOMEM;
>  
>  		arena->infooff = cur_off;
>  		ret = btt_info_read(arena, super);
> @@ -919,14 +917,11 @@ static int discover_arenas(struct btt *btt)
>  	btt->nlba = cur_nlba;
>  	btt->init_state = INIT_READY;
>  
> -	kfree(super);
>  	return ret;
>  
>   out:
>  	kfree(arena);
>  	free_arenas(btt);
> - out_super:
> -	kfree(super);
>  	return ret;
>  }
>
diff mbox series

Patch

diff --git a/drivers/nvdimm/btt.c b/drivers/nvdimm/btt.c
index d5593b0dc700..32a9e2f543c5 100644
--- a/drivers/nvdimm/btt.c
+++ b/drivers/nvdimm/btt.c
@@ -16,6 +16,7 @@ 
 #include <linux/fs.h>
 #include <linux/nd.h>
 #include <linux/backing-dev.h>
+#include <linux/cleanup.h>
 #include "btt.h"
 #include "nd.h"
 
@@ -847,23 +848,20 @@  static int discover_arenas(struct btt *btt)
 {
 	int ret = 0;
 	struct arena_info *arena;
-	struct btt_sb *super;
 	size_t remaining = btt->rawsize;
 	u64 cur_nlba = 0;
 	size_t cur_off = 0;
 	int num_arenas = 0;
 
-	super = kzalloc(sizeof(*super), GFP_KERNEL);
+	struct btt_sb *super __free(kfree) = kzalloc(sizeof(*super), GFP_KERNEL);
 	if (!super)
 		return -ENOMEM;
 
 	while (remaining) {
 		/* Alloc memory for arena */
 		arena = alloc_arena(btt, 0, 0, 0);
-		if (!arena) {
-			ret = -ENOMEM;
-			goto out_super;
-		}
+		if (!arena)
+			return -ENOMEM;
 
 		arena->infooff = cur_off;
 		ret = btt_info_read(arena, super);
@@ -919,14 +917,11 @@  static int discover_arenas(struct btt *btt)
 	btt->nlba = cur_nlba;
 	btt->init_state = INIT_READY;
 
-	kfree(super);
 	return ret;
 
  out:
 	kfree(arena);
 	free_arenas(btt);
- out_super:
-	kfree(super);
 	return ret;
 }