linux-rdma.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jason Gunthorpe <jgg@nvidia.com>
To: Li Zhijian <lizhijian@cn.fujitsu.com>
Cc: zyjzyj2000@gmail.com, linux-rdma@vger.kernel.org,
	linux-kernel@vger.kernel.org, yanjun.zhu@linux.dev,
	rpearsonhpe@gmail.com
Subject: Re: [PATCH v2] RDMA/rxe: Prevent from double freeing rxe_map_set
Date: Tue, 4 Jan 2022 20:44:34 -0400	[thread overview]
Message-ID: <20220105004434.GA2712053@nvidia.com> (raw)
In-Reply-To: <20211228014406.1033444-1-lizhijian@cn.fujitsu.com>

On Tue, Dec 28, 2021 at 09:44:06AM +0800, Li Zhijian wrote:
> a same rxe_map_set could be freed twice:
> rxe_reg_user_mr()
>   -> rxe_mr_init_user()
>     -> rxe_mr_free_map_set() # 1st
>   -> rxe_drop_ref()
>    ...
>     -> rxe_mr_cleanup()
>       -> rxe_mr_free_map_set() # 2nd
> 
> By convention, we should cleanup/free resources in the error path in the
> same function where the resources are alloted in. So rxe_mr_init_user()
> doesn't need to free the map_set directly.
> 
> In addition, we have to reset map_set to NULL inside rxe_mr_alloc() if needed
> to prevent from map_set being double freed in rxe_mr_cleanup().
> 
> CC: Jason Gunthorpe <jgg@nvidia.com>
> Signed-off-by: Li Zhijian <lizhijian@cn.fujitsu.com>
> Acked-by: Zhu Yanjun <zyjzyj2000@gmail.com>
> ---
> V2: Fix it by a simpler way by following suggestion from Bob,
> ---
>  drivers/infiniband/sw/rxe/rxe_mr.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)

Applied to for-rc, but I changed it to keep the goto unwind as is
normal.

@@ -135,19 +135,19 @@ static int rxe_mr_alloc(struct rxe_mr *mr, int num_buf, int both)
 
        ret = rxe_mr_alloc_map_set(num_map, &mr->cur_map_set);
        if (ret)
-               goto err_out;
+               return -ENOMEM;
 
        if (both) {
                ret = rxe_mr_alloc_map_set(num_map, &mr->next_map_set);
-               if (ret) {
-                       rxe_mr_free_map_set(mr->num_map, mr->cur_map_set);
-                       goto err_out;
-               }
+               if (ret)
+                       goto err_free;
        }
 
        return 0;
 
-err_out:
+err_free:
+       rxe_mr_free_map_set(mr->num_map, mr->cur_map_set);
+       mr->cur_map_set = NULL;
        return -ENOMEM;
 }

      parent reply	other threads:[~2022-01-05  0:44 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-28  1:44 [PATCH v2] RDMA/rxe: Prevent from double freeing rxe_map_set Li Zhijian
2021-12-28  9:10 ` Zhu Yanjun
2022-01-05  0:44 ` Jason Gunthorpe [this message]

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=20220105004434.GA2712053@nvidia.com \
    --to=jgg@nvidia.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-rdma@vger.kernel.org \
    --cc=lizhijian@cn.fujitsu.com \
    --cc=rpearsonhpe@gmail.com \
    --cc=yanjun.zhu@linux.dev \
    --cc=zyjzyj2000@gmail.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;
as well as URLs for NNTP newsgroup(s).