All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2] RDMA/rxe: Prevent from double freeing rxe_map_set
@ 2021-12-28  1:44 Li Zhijian
  2021-12-28  9:10 ` Zhu Yanjun
  2022-01-05  0:44 ` Jason Gunthorpe
  0 siblings, 2 replies; 3+ messages in thread
From: Li Zhijian @ 2021-12-28  1:44 UTC (permalink / raw)
  To: zyjzyj2000, jgg
  Cc: linux-rdma, linux-kernel, yanjun.zhu, rpearsonhpe, Li Zhijian,
	Jason Gunthorpe

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>
---
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(-)

diff --git a/drivers/infiniband/sw/rxe/rxe_mr.c b/drivers/infiniband/sw/rxe/rxe_mr.c
index 25c78aade822..7c4cd19a9db2 100644
--- a/drivers/infiniband/sw/rxe/rxe_mr.c
+++ b/drivers/infiniband/sw/rxe/rxe_mr.c
@@ -141,6 +141,7 @@ static int rxe_mr_alloc(struct rxe_mr *mr, int num_buf, int 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);
+			mr->cur_map_set = NULL;
 			goto err_out;
 		}
 	}
@@ -214,7 +215,7 @@ int rxe_mr_init_user(struct rxe_pd *pd, u64 start, u64 length, u64 iova,
 				pr_warn("%s: Unable to get virtual address\n",
 						__func__);
 				err = -ENOMEM;
-				goto err_cleanup_map;
+				goto err_release_umem;
 			}
 
 			buf->addr = (uintptr_t)vaddr;
@@ -237,8 +238,6 @@ int rxe_mr_init_user(struct rxe_pd *pd, u64 start, u64 length, u64 iova,
 
 	return 0;
 
-err_cleanup_map:
-	rxe_mr_free_map_set(mr->num_map, mr->cur_map_set);
 err_release_umem:
 	ib_umem_release(umem);
 err_out:
-- 
2.31.1




^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] RDMA/rxe: Prevent from double freeing rxe_map_set
  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
  1 sibling, 0 replies; 3+ messages in thread
From: Zhu Yanjun @ 2021-12-28  9:10 UTC (permalink / raw)
  To: Li Zhijian
  Cc: Jason Gunthorpe, RDMA mailing list, LKML, yanjun.zhu,
	Bob Pearson, Jason Gunthorpe

On Tue, Dec 28, 2021 at 9:39 AM Li Zhijian <lizhijian@cn.fujitsu.com> 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>

Thanks.

Acked-by: Zhu Yanjun <zyjzyj2000@gmail.com>

Zhu Yanjun
> ---
> 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(-)
>
> diff --git a/drivers/infiniband/sw/rxe/rxe_mr.c b/drivers/infiniband/sw/rxe/rxe_mr.c
> index 25c78aade822..7c4cd19a9db2 100644
> --- a/drivers/infiniband/sw/rxe/rxe_mr.c
> +++ b/drivers/infiniband/sw/rxe/rxe_mr.c
> @@ -141,6 +141,7 @@ static int rxe_mr_alloc(struct rxe_mr *mr, int num_buf, int 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);
> +                       mr->cur_map_set = NULL;
>                         goto err_out;
>                 }
>         }
> @@ -214,7 +215,7 @@ int rxe_mr_init_user(struct rxe_pd *pd, u64 start, u64 length, u64 iova,
>                                 pr_warn("%s: Unable to get virtual address\n",
>                                                 __func__);
>                                 err = -ENOMEM;
> -                               goto err_cleanup_map;
> +                               goto err_release_umem;
>                         }
>
>                         buf->addr = (uintptr_t)vaddr;
> @@ -237,8 +238,6 @@ int rxe_mr_init_user(struct rxe_pd *pd, u64 start, u64 length, u64 iova,
>
>         return 0;
>
> -err_cleanup_map:
> -       rxe_mr_free_map_set(mr->num_map, mr->cur_map_set);
>  err_release_umem:
>         ib_umem_release(umem);
>  err_out:
> --
> 2.31.1
>
>
>

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH v2] RDMA/rxe: Prevent from double freeing rxe_map_set
  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
  1 sibling, 0 replies; 3+ messages in thread
From: Jason Gunthorpe @ 2022-01-05  0:44 UTC (permalink / raw)
  To: Li Zhijian; +Cc: zyjzyj2000, linux-rdma, linux-kernel, yanjun.zhu, rpearsonhpe

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;
 }

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-01-05  0:44 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.