habanalabs: save ctx inside encaps signal

Compute context pointer in hdev shouldn't be used for fetching the
context's pointer.

If an object needs the context's pointer, it should get it while
incrementing its kref, and when the object is released, put it.

Signed-off-by: Oded Gabbay <ogabbay@kernel.org>
This commit is contained in:
Oded Gabbay 2021-11-30 15:28:23 +02:00
parent a4dd2ecf36
commit 357ff3dc9a
4 changed files with 16 additions and 9 deletions

View File

@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-2.0 // SPDX-License-Identifier: GPL-2.0
/* /*
* Copyright 2016-2019 HabanaLabs, Ltd. * Copyright 2016-2021 HabanaLabs, Ltd.
* All Rights Reserved. * All Rights Reserved.
*/ */
@ -1829,6 +1829,9 @@ static int cs_ioctl_reserve_signals(struct hl_fpriv *hpriv,
} }
handle->count = count; handle->count = count;
hl_ctx_get(hdev, hpriv->ctx);
handle->ctx = hpriv->ctx;
mgr = &hpriv->ctx->sig_mgr; mgr = &hpriv->ctx->sig_mgr;
spin_lock(&mgr->lock); spin_lock(&mgr->lock);
@ -1838,7 +1841,7 @@ static int cs_ioctl_reserve_signals(struct hl_fpriv *hpriv,
if (hdl_id < 0) { if (hdl_id < 0) {
dev_err(hdev->dev, "Failed to allocate IDR for a new signal reservation\n"); dev_err(hdev->dev, "Failed to allocate IDR for a new signal reservation\n");
rc = -EINVAL; rc = -EINVAL;
goto free_handle; goto put_ctx;
} }
handle->id = hdl_id; handle->id = hdl_id;
@ -1891,7 +1894,8 @@ remove_idr:
idr_remove(&mgr->handles, hdl_id); idr_remove(&mgr->handles, hdl_id);
spin_unlock(&mgr->lock); spin_unlock(&mgr->lock);
free_handle: put_ctx:
hl_ctx_put(handle->ctx);
kfree(handle); kfree(handle);
out: out:
@ -1953,6 +1957,7 @@ static int cs_ioctl_unreserve_signals(struct hl_fpriv *hpriv, u32 handle_id)
/* Release the id and free allocated memory of the handle */ /* Release the id and free allocated memory of the handle */
idr_remove(&mgr->handles, handle_id); idr_remove(&mgr->handles, handle_id);
hl_ctx_put(encaps_sig_hdl->ctx);
kfree(encaps_sig_hdl); kfree(encaps_sig_hdl);
} else { } else {
rc = -EINVAL; rc = -EINVAL;

View File

@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-2.0 // SPDX-License-Identifier: GPL-2.0
/* /*
* Copyright 2016-2019 HabanaLabs, Ltd. * Copyright 2016-2021 HabanaLabs, Ltd.
* All Rights Reserved. * All Rights Reserved.
*/ */
@ -13,13 +13,13 @@ void hl_encaps_handle_do_release(struct kref *ref)
{ {
struct hl_cs_encaps_sig_handle *handle = struct hl_cs_encaps_sig_handle *handle =
container_of(ref, struct hl_cs_encaps_sig_handle, refcount); container_of(ref, struct hl_cs_encaps_sig_handle, refcount);
struct hl_ctx *ctx = handle->hdev->compute_ctx; struct hl_encaps_signals_mgr *mgr = &handle->ctx->sig_mgr;
struct hl_encaps_signals_mgr *mgr = &ctx->sig_mgr;
spin_lock(&mgr->lock); spin_lock(&mgr->lock);
idr_remove(&mgr->handles, handle->id); idr_remove(&mgr->handles, handle->id);
spin_unlock(&mgr->lock); spin_unlock(&mgr->lock);
hl_ctx_put(handle->ctx);
kfree(handle); kfree(handle);
} }
@ -27,8 +27,7 @@ static void hl_encaps_handle_do_release_sob(struct kref *ref)
{ {
struct hl_cs_encaps_sig_handle *handle = struct hl_cs_encaps_sig_handle *handle =
container_of(ref, struct hl_cs_encaps_sig_handle, refcount); container_of(ref, struct hl_cs_encaps_sig_handle, refcount);
struct hl_ctx *ctx = handle->hdev->compute_ctx; struct hl_encaps_signals_mgr *mgr = &handle->ctx->sig_mgr;
struct hl_encaps_signals_mgr *mgr = &ctx->sig_mgr;
/* if we're here, then there was a signals reservation but cs with /* if we're here, then there was a signals reservation but cs with
* encaps signals wasn't submitted, so need to put refcount * encaps signals wasn't submitted, so need to put refcount
@ -40,6 +39,7 @@ static void hl_encaps_handle_do_release_sob(struct kref *ref)
idr_remove(&mgr->handles, handle->id); idr_remove(&mgr->handles, handle->id);
spin_unlock(&mgr->lock); spin_unlock(&mgr->lock);
hl_ctx_put(handle->ctx);
kfree(handle); kfree(handle);
} }

View File

@ -2757,6 +2757,7 @@ struct hl_device {
* wait cs are used to wait of the reserved encaps signals. * wait cs are used to wait of the reserved encaps signals.
* @hdev: pointer to habanalabs device structure. * @hdev: pointer to habanalabs device structure.
* @hw_sob: pointer to H/W SOB used in the reservation. * @hw_sob: pointer to H/W SOB used in the reservation.
* @ctx: pointer to the user's context data structure
* @cs_seq: staged cs sequence which contains encapsulated signals * @cs_seq: staged cs sequence which contains encapsulated signals
* @id: idr handler id to be used to fetch the handler info * @id: idr handler id to be used to fetch the handler info
* @q_idx: stream queue index * @q_idx: stream queue index
@ -2767,6 +2768,7 @@ struct hl_cs_encaps_sig_handle {
struct kref refcount; struct kref refcount;
struct hl_device *hdev; struct hl_device *hdev;
struct hl_hw_sob *hw_sob; struct hl_hw_sob *hw_sob;
struct hl_ctx *ctx;
u64 cs_seq; u64 cs_seq;
u32 id; u32 id;
u32 q_idx; u32 q_idx;

View File

@ -574,7 +574,7 @@ static int encaps_sig_first_staged_cs_handler
struct hl_encaps_signals_mgr *mgr; struct hl_encaps_signals_mgr *mgr;
int rc = 0; int rc = 0;
mgr = &hdev->compute_ctx->sig_mgr; mgr = &cs->ctx->sig_mgr;
spin_lock(&mgr->lock); spin_lock(&mgr->lock);
encaps_sig_hdl = idr_find(&mgr->handles, cs->encaps_sig_hdl_id); encaps_sig_hdl = idr_find(&mgr->handles, cs->encaps_sig_hdl_id);