Skip to content

Commit 9aa6543

Browse files
dinghaoliuweiny2
authored andcommitted
nvdimm-btt: simplify code with the scope based resource management
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 <[email protected]> Reviewed-by: Dave Jiang <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Ira Weiny <[email protected]>
1 parent deb369e commit 9aa6543

File tree

1 file changed

+4
-9
lines changed

1 file changed

+4
-9
lines changed

drivers/nvdimm/btt.c

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/fs.h>
1717
#include <linux/nd.h>
1818
#include <linux/backing-dev.h>
19+
#include <linux/cleanup.h>
1920
#include "btt.h"
2021
#include "nd.h"
2122

@@ -847,23 +848,20 @@ static int discover_arenas(struct btt *btt)
847848
{
848849
int ret = 0;
849850
struct arena_info *arena;
850-
struct btt_sb *super;
851851
size_t remaining = btt->rawsize;
852852
u64 cur_nlba = 0;
853853
size_t cur_off = 0;
854854
int num_arenas = 0;
855855

856-
super = kzalloc(sizeof(*super), GFP_KERNEL);
856+
struct btt_sb *super __free(kfree) = kzalloc(sizeof(*super), GFP_KERNEL);
857857
if (!super)
858858
return -ENOMEM;
859859

860860
while (remaining) {
861861
/* Alloc memory for arena */
862862
arena = alloc_arena(btt, 0, 0, 0);
863-
if (!arena) {
864-
ret = -ENOMEM;
865-
goto out_super;
866-
}
863+
if (!arena)
864+
return -ENOMEM;
867865

868866
arena->infooff = cur_off;
869867
ret = btt_info_read(arena, super);
@@ -919,14 +917,11 @@ static int discover_arenas(struct btt *btt)
919917
btt->nlba = cur_nlba;
920918
btt->init_state = INIT_READY;
921919

922-
kfree(super);
923920
return ret;
924921

925922
out:
926923
kfree(arena);
927924
free_arenas(btt);
928-
out_super:
929-
kfree(super);
930925
return ret;
931926
}
932927

0 commit comments

Comments
 (0)