public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Dave Jiang <dave.jiang@intel.com>
To: Dinghao Liu <dinghao.liu@zju.edu.cn>
Cc: Vishal Verma <vishal.l.verma@intel.com>,
	Dan Williams <dan.j.williams@intel.com>,
	Ira Weiny <ira.weiny@intel.com>, <nvdimm@lists.linux.dev>,
	<linux-kernel@vger.kernel.org>
Subject: Re: [PATCH] nvdimm-btt: simplify code with the scope based resource management
Date: Mon, 11 Dec 2023 11:26:56 -0700	[thread overview]
Message-ID: <8716bf72-e4ca-4f83-8a30-327baf459dbb@intel.com> (raw)
In-Reply-To: <20231210102747.13545-1-dinghao.liu@zju.edu.cn>



On 12/10/23 03:27, 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>
> ---
>  drivers/nvdimm/btt.c | 12 ++++--------
>  1 file changed, 4 insertions(+), 8 deletions(-)
> 
> diff --git a/drivers/nvdimm/btt.c b/drivers/nvdimm/btt.c
> index d5593b0dc700..ff42778b51de 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,7 +848,7 @@ static int discover_arenas(struct btt *btt)
>  {
>  	int ret = 0;
>  	struct arena_info *arena;
> -	struct btt_sb *super;
> +	struct btt_sb *super __free(kfree) = NULL;
>  	size_t remaining = btt->rawsize;
>  	u64 cur_nlba = 0;
>  	size_t cur_off = 0;
> @@ -860,10 +861,8 @@ static int discover_arenas(struct btt *btt)
>  	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 +918,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;
>  }
>  

I would do the allocation like something below for the first chunk. Otherwise the rest LGTM. 

diff --git a/drivers/nvdimm/btt.c b/drivers/nvdimm/btt.c
index d5593b0dc700..143921e7f26c 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"
 
@@ -845,25 +846,23 @@ static void parse_arena_meta(struct arena_info *arena, struct btt_sb *super,
 
 static int discover_arenas(struct btt *btt)
 {
+       struct btt_sb *super __free(kfree) =
+               kzalloc(sizeof(*super), GFP_KERNEL);
        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);
        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);


  reply	other threads:[~2023-12-11 18:27 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-10 10:27 [PATCH] nvdimm-btt: simplify code with the scope based resource management Dinghao Liu
2023-12-11 18:26 ` Dave Jiang [this message]
2023-12-13  3:12   ` dinghao.liu
2023-12-13 16:47     ` Dave Jiang
2023-12-14  7:33       ` dinghao.liu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=8716bf72-e4ca-4f83-8a30-327baf459dbb@intel.com \
    --to=dave.jiang@intel.com \
    --cc=dan.j.williams@intel.com \
    --cc=dinghao.liu@zju.edu.cn \
    --cc=ira.weiny@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=nvdimm@lists.linux.dev \
    --cc=vishal.l.verma@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox