linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Oded Gabbay <ogabbay@kernel.org>
To: linux-kernel@vger.kernel.org
Subject: [PATCH 19/30] habanalabs: don't free phys_pg_pack inside lock
Date: Sat, 22 Jan 2022 21:57:20 +0200	[thread overview]
Message-ID: <20220122195731.934494-19-ogabbay@kernel.org> (raw)
In-Reply-To: <20220122195731.934494-1-ogabbay@kernel.org>

Freeing phys_pg_pack includes calling to scrubbing functions of the
device's memory, taking locks and possibly even calling reset.

This is not something that should be done while holding a device-wide
spinlock.

Therefore, save the relevant objects on a local linked-list and after
releasing the spinlock, traverse that list and free the phys_pg_pack
objects.

Signed-off-by: Oded Gabbay <ogabbay@kernel.org>
---
 drivers/misc/habanalabs/common/habanalabs.h |  3 +++
 drivers/misc/habanalabs/common/memory.c     | 16 +++++++++++-----
 2 files changed, 14 insertions(+), 5 deletions(-)

diff --git a/drivers/misc/habanalabs/common/habanalabs.h b/drivers/misc/habanalabs/common/habanalabs.h
index 6c7a60210416..9c8374d88907 100644
--- a/drivers/misc/habanalabs/common/habanalabs.h
+++ b/drivers/misc/habanalabs/common/habanalabs.h
@@ -1738,6 +1738,8 @@ struct hl_vm_hw_block_list_node {
  * @pages: the physical page array.
  * @npages: num physical pages in the pack.
  * @total_size: total size of all the pages in this list.
+ * @node: used to attach to deletion list that is used when all the allocations are cleared
+ *        at the teardown of the context.
  * @mapping_cnt: number of shared mappings.
  * @exporting_cnt: number of dma-buf exporting.
  * @asid: the context related to this list.
@@ -1753,6 +1755,7 @@ struct hl_vm_phys_pg_pack {
 	u64			*pages;
 	u64			npages;
 	u64			total_size;
+	struct list_head	node;
 	atomic_t		mapping_cnt;
 	u32			exporting_cnt;
 	u32			asid;
diff --git a/drivers/misc/habanalabs/common/memory.c b/drivers/misc/habanalabs/common/memory.c
index c1eefaebacb6..4a5d3a179765 100644
--- a/drivers/misc/habanalabs/common/memory.c
+++ b/drivers/misc/habanalabs/common/memory.c
@@ -2607,11 +2607,12 @@ int hl_vm_ctx_init(struct hl_ctx *ctx)
  */
 void hl_vm_ctx_fini(struct hl_ctx *ctx)
 {
-	struct hl_device *hdev = ctx->hdev;
-	struct hl_vm *vm = &hdev->vm;
 	struct hl_vm_phys_pg_pack *phys_pg_list;
+	struct hl_device *hdev = ctx->hdev;
 	struct hl_vm_hash_node *hnode;
+	struct hl_vm *vm = &hdev->vm;
 	struct hlist_node *tmp_node;
+	struct list_head free_list;
 	struct hl_mem_in args;
 	int i;
 
@@ -2644,19 +2645,24 @@ void hl_vm_ctx_fini(struct hl_ctx *ctx)
 
 	mutex_unlock(&ctx->mmu_lock);
 
+	INIT_LIST_HEAD(&free_list);
+
 	spin_lock(&vm->idr_lock);
 	idr_for_each_entry(&vm->phys_pg_pack_handles, phys_pg_list, i)
 		if (phys_pg_list->asid == ctx->asid) {
 			dev_dbg(hdev->dev,
 				"page list 0x%px of asid %d is still alive\n",
 				phys_pg_list, ctx->asid);
-			atomic64_sub(phys_pg_list->total_size,
-					&hdev->dram_used_mem);
-			free_phys_pg_pack(hdev, phys_pg_list);
+
+			atomic64_sub(phys_pg_list->total_size, &hdev->dram_used_mem);
 			idr_remove(&vm->phys_pg_pack_handles, i);
+			list_add(&phys_pg_list->node, &free_list);
 		}
 	spin_unlock(&vm->idr_lock);
 
+	list_for_each_entry(phys_pg_list, &free_list, node)
+		free_phys_pg_pack(hdev, phys_pg_list);
+
 	va_range_fini(hdev, ctx->va_range[HL_VA_RANGE_TYPE_DRAM]);
 	va_range_fini(hdev, ctx->va_range[HL_VA_RANGE_TYPE_HOST]);
 
-- 
2.25.1


  parent reply	other threads:[~2022-01-22 19:58 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-01-22 19:57 [PATCH 01/30] habanalabs: check the return value of hl_cs_poll_fences() Oded Gabbay
2022-01-22 19:57 ` [PATCH 02/30] habanalabs: fix race when waiting on encaps signal Oded Gabbay
2022-01-22 19:57 ` [PATCH 03/30] habanalabs: fix possible memory leak in MMU DR fini Oded Gabbay
2022-01-22 19:57 ` [PATCH 04/30] habanalabs/gaudi: disable CGM permanently Oded Gabbay
2022-01-22 19:57 ` [PATCH 05/30] habanalabs: remove ASIC functions of clock gating Oded Gabbay
2022-01-22 19:57 ` [PATCH 06/30] habanalabs: make some MMU functions common Oded Gabbay
2022-01-22 19:57 ` [PATCH 07/30] habanalabs: sysfs functions should be in sysfs.c Oded Gabbay
2022-01-22 19:57 ` [PATCH 08/30] habanalabs: get clk is common function Oded Gabbay
2022-01-22 19:57 ` [PATCH 09/30] habanalabs: remove hwmgr.c Oded Gabbay
2022-01-22 19:57 ` [PATCH 10/30] habanalabs: move more f/w functions to firmware_if.c Oded Gabbay
2022-01-22 19:57 ` [PATCH 11/30] habanalabs: remove asic callback set_pll_profile() Oded Gabbay
2022-01-22 19:57 ` [PATCH 12/30] habanalabs: rename dev_attr_grp to dev_clk_attr_grp Oded Gabbay
2022-01-22 19:57 ` [PATCH 13/30] habanalabs: add vrm version to sysfs Oded Gabbay
2022-01-22 19:57 ` [PATCH 14/30] habanalabs: remove power9 workaround for dma support Oded Gabbay
2022-01-22 19:57 ` [PATCH 15/30] habanalabs: use common wrapper for MMU cache invalidation Oded Gabbay
2022-01-22 19:57 ` [PATCH 16/30] habanalabs: sysfs support for fw os version Oded Gabbay
2022-01-22 19:57 ` [PATCH 17/30] habanalabs: there is no kernel TDR in future ASICs Oded Gabbay
2022-01-22 19:57 ` [PATCH 18/30] habanalabs: duplicate HOP table props to MMU props Oded Gabbay
2022-01-22 19:57 ` Oded Gabbay [this message]
2022-01-22 19:57 ` [PATCH 20/30] habanalabs: avoid copying pll data if pll_info_get fails Oded Gabbay
2022-01-22 19:57 ` [PATCH 21/30] habanalabs: add missing error check in sysfs clk_freq_mhz_show Oded Gabbay
2022-01-22 19:57 ` [PATCH 22/30] habanalabs: fix soft reset flow in case of failure Oded Gabbay
2022-01-22 19:57 ` [PATCH 23/30] habanalabs: add missing error check in sysfs max_power_show Oded Gabbay
2022-01-22 19:57 ` [PATCH 24/30] habanalabs: update to latest f/w specs Oded Gabbay
2022-01-22 19:57 ` [PATCH 25/30] habanalabs: expose number of user interrupts Oded Gabbay
2022-01-22 19:57 ` [PATCH 26/30] habanalabs: reject host map with mmu disabled Oded Gabbay
2022-01-22 19:57 ` [PATCH 27/30] habanalabs: fix user interrupt wait when timeout is 0 Oded Gabbay
2022-01-22 19:57 ` [PATCH 28/30] habanalabs: fix race between wait and irq Oded Gabbay
2022-01-22 19:57 ` [PATCH 29/30] habanalabs: prevent false heartbeat failure during soft-reset Oded Gabbay
2022-01-22 19:57 ` [PATCH 30/30] habanalabs: remove duplicate print Oded Gabbay
     [not found] ` <20220123002722.3057-1-hdanton@sina.com>
2022-01-24 18:22   ` [PATCH 02/30] habanalabs: fix race when waiting on encaps signal Dani Liberman

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=20220122195731.934494-19-ogabbay@kernel.org \
    --to=ogabbay@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    /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).