* [PATCH] habanalabs: Add CB IOCTL opcode to retrieve CB information
@ 2020-11-28 22:22 Oded Gabbay
2020-11-28 22:22 ` [PATCH] habanalabs: Add mask for CS type bits in CS flags Oded Gabbay
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: Oded Gabbay @ 2020-11-28 22:22 UTC (permalink / raw)
To: linux-kernel; +Cc: SW_Drivers, Tomer Tayar
From: Tomer Tayar <ttayar@habana.ai>
Add a new CB IOCTL opcode that enables a user to query about a CB and
get its usage count.
Signed-off-by: Tomer Tayar <ttayar@habana.ai>
Reviewed-by: Oded Gabbay <ogabbay@kernel.org>
Signed-off-by: Oded Gabbay <ogabbay@kernel.org>
---
.../misc/habanalabs/common/command_buffer.c | 38 +++++++++++++++++++
include/uapi/misc/habanalabs.h | 15 +++++++-
2 files changed, 51 insertions(+), 2 deletions(-)
diff --git a/drivers/misc/habanalabs/common/command_buffer.c b/drivers/misc/habanalabs/common/command_buffer.c
index 2856bb3423ee..6f6a904ab6ca 100644
--- a/drivers/misc/habanalabs/common/command_buffer.c
+++ b/drivers/misc/habanalabs/common/command_buffer.c
@@ -375,12 +375,43 @@ int hl_cb_destroy(struct hl_device *hdev, struct hl_cb_mgr *mgr, u64 cb_handle)
return rc;
}
+static int hl_cb_info(struct hl_device *hdev, struct hl_cb_mgr *mgr,
+ u64 cb_handle, u32 *usage_cnt)
+{
+ struct hl_cb *cb;
+ u32 handle;
+ int rc = 0;
+
+ /* The CB handle was given to user to do mmap, so need to shift it back
+ * to the value which was allocated by the IDR module.
+ */
+ cb_handle >>= PAGE_SHIFT;
+ handle = (u32) cb_handle;
+
+ spin_lock(&mgr->cb_lock);
+
+ cb = idr_find(&mgr->cb_handles, handle);
+ if (!cb) {
+ dev_err(hdev->dev,
+ "CB info failed, no match to handle 0x%x\n", handle);
+ rc = -EINVAL;
+ goto out;
+ }
+
+ *usage_cnt = atomic_read(&cb->cs_cnt);
+
+out:
+ spin_unlock(&mgr->cb_lock);
+ return rc;
+}
+
int hl_cb_ioctl(struct hl_fpriv *hpriv, void *data)
{
union hl_cb_args *args = data;
struct hl_device *hdev = hpriv->hdev;
enum hl_device_status status;
u64 handle = 0;
+ u32 usage_cnt = 0;
int rc;
if (!hl_device_operational(hdev, &status)) {
@@ -413,6 +444,13 @@ int hl_cb_ioctl(struct hl_fpriv *hpriv, void *data)
args->in.cb_handle);
break;
+ case HL_CB_OP_INFO:
+ rc = hl_cb_info(hdev, &hpriv->cb_mgr, args->in.cb_handle,
+ &usage_cnt);
+ memset(args, 0, sizeof(*args));
+ args->out.usage_cnt = usage_cnt;
+ break;
+
default:
rc = -ENOTTY;
break;
diff --git a/include/uapi/misc/habanalabs.h b/include/uapi/misc/habanalabs.h
index 6eff4e05eccb..8c15a7d336a0 100644
--- a/include/uapi/misc/habanalabs.h
+++ b/include/uapi/misc/habanalabs.h
@@ -483,6 +483,8 @@ struct hl_info_args {
#define HL_CB_OP_CREATE 0
/* Opcode to destroy previously created command buffer */
#define HL_CB_OP_DESTROY 1
+/* Opcode to retrieve information about a command buffer */
+#define HL_CB_OP_INFO 2
/* 2MB minus 32 bytes for 2xMSG_PROT */
#define HL_MAX_CB_SIZE (0x200000 - 32)
@@ -506,8 +508,17 @@ struct hl_cb_in {
};
struct hl_cb_out {
- /* Handle of CB */
- __u64 cb_handle;
+ union {
+ /* Handle of CB */
+ __u64 cb_handle;
+
+ /* Information about CB */
+ struct {
+ /* Usage count of CB */
+ __u32 usage_cnt;
+ __u32 pad;
+ };
+ };
};
union hl_cb_args {
--
2.17.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH] habanalabs: Add mask for CS type bits in CS flags
2020-11-28 22:22 [PATCH] habanalabs: Add CB IOCTL opcode to retrieve CB information Oded Gabbay
@ 2020-11-28 22:22 ` Oded Gabbay
2020-11-28 22:22 ` [PATCH] habanalabs: change messages to debug level Oded Gabbay
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Oded Gabbay @ 2020-11-28 22:22 UTC (permalink / raw)
To: linux-kernel; +Cc: SW_Drivers, Tomer Tayar
From: Tomer Tayar <ttayar@habana.ai>
hl_cs_sanity_checks() extracts the CS type bits of the CS flags, by
masking out the non-type bits.
To save the need for updating the function whenever new bits for
non-type flags are added, add an explicit mask for the CS type bits.
Signed-off-by: Tomer Tayar <ttayar@habana.ai>
Reviewed-by: Oded Gabbay <ogabbay@kernel.org>
Signed-off-by: Oded Gabbay <ogabbay@kernel.org>
---
drivers/misc/habanalabs/common/command_submission.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
diff --git a/drivers/misc/habanalabs/common/command_submission.c b/drivers/misc/habanalabs/common/command_submission.c
index bd2f54399020..dc85ed6ab525 100644
--- a/drivers/misc/habanalabs/common/command_submission.c
+++ b/drivers/misc/habanalabs/common/command_submission.c
@@ -11,6 +11,9 @@
#include <linux/uaccess.h>
#include <linux/slab.h>
+#define HL_CS_FLAGS_TYPE_MASK (HL_CS_FLAGS_SIGNAL | HL_CS_FLAGS_WAIT | \
+ HL_CS_FLAGS_COLLECTIVE_WAIT)
+
/**
* enum hl_cs_wait_status - cs wait status
* @CS_WAIT_STATUS_BUSY: cs was not completed yet
@@ -737,8 +740,7 @@ static int hl_cs_sanity_checks(struct hl_fpriv *hpriv, union hl_cs_args *args)
return -EBUSY;
}
- cs_type_flags = args->in.cs_flags &
- ~(HL_CS_FLAGS_FORCE_RESTORE | HL_CS_FLAGS_TIMESTAMP);
+ cs_type_flags = args->in.cs_flags & HL_CS_FLAGS_TYPE_MASK;
if (unlikely(cs_type_flags && !is_power_of_2(cs_type_flags))) {
dev_err(hdev->dev,
--
2.17.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH] habanalabs: change messages to debug level
2020-11-28 22:22 [PATCH] habanalabs: Add CB IOCTL opcode to retrieve CB information Oded Gabbay
2020-11-28 22:22 ` [PATCH] habanalabs: Add mask for CS type bits in CS flags Oded Gabbay
@ 2020-11-28 22:22 ` Oded Gabbay
2020-11-28 22:22 ` [PATCH] habanalabs: free host huge va_range if not used Oded Gabbay
2020-11-28 22:22 ` [PATCH] habanalabs: Modify the cs_cnt of a CB to be atomic Oded Gabbay
3 siblings, 0 replies; 5+ messages in thread
From: Oded Gabbay @ 2020-11-28 22:22 UTC (permalink / raw)
To: linux-kernel; +Cc: SW_Drivers
Some messages should be changed to debug mode as we want to keep
minimal prints during normal operation of the device.
Signed-off-by: Oded Gabbay <ogabbay@kernel.org>
---
drivers/misc/habanalabs/common/firmware_if.c | 4 ++--
drivers/misc/habanalabs/gaudi/gaudi.c | 2 +-
2 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/drivers/misc/habanalabs/common/firmware_if.c b/drivers/misc/habanalabs/common/firmware_if.c
index c4a8d6ca34bb..0e1c629e9800 100644
--- a/drivers/misc/habanalabs/common/firmware_if.c
+++ b/drivers/misc/habanalabs/common/firmware_if.c
@@ -526,7 +526,7 @@ static void fw_read_errors(struct hl_device *hdev, u32 boot_err0_reg,
security_val = RREG32(cpu_security_boot_status_reg);
if (security_val & CPU_BOOT_DEV_STS0_ENABLED)
- dev_info(hdev->dev, "Device security status %#x\n",
+ dev_dbg(hdev->dev, "Device security status %#x\n",
security_val);
}
@@ -806,7 +806,7 @@ int hl_fw_init_cpu(struct hl_device *hdev, u32 cpu_boot_status_reg,
hdev->asic_prop.hard_reset_done_by_fw = true;
}
- dev_info(hdev->dev, "Firmware hard-reset is %s\n",
+ dev_dbg(hdev->dev, "Firmware hard-reset is %s\n",
hdev->asic_prop.hard_reset_done_by_fw ? "enabled" : "disabled");
dev_info(hdev->dev, "Successfully loaded firmware to device\n");
diff --git a/drivers/misc/habanalabs/gaudi/gaudi.c b/drivers/misc/habanalabs/gaudi/gaudi.c
index 568afe1b6849..aac3c9c5a2e0 100644
--- a/drivers/misc/habanalabs/gaudi/gaudi.c
+++ b/drivers/misc/habanalabs/gaudi/gaudi.c
@@ -1732,7 +1732,7 @@ static int gaudi_enable_msi_single(struct hl_device *hdev)
{
int rc, irq;
- dev_info(hdev->dev, "Working in single MSI IRQ mode\n");
+ dev_dbg(hdev->dev, "Working in single MSI IRQ mode\n");
irq = gaudi_pci_irq_vector(hdev, 0, false);
rc = request_irq(irq, gaudi_irq_handler_single, 0,
--
2.17.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH] habanalabs: free host huge va_range if not used
2020-11-28 22:22 [PATCH] habanalabs: Add CB IOCTL opcode to retrieve CB information Oded Gabbay
2020-11-28 22:22 ` [PATCH] habanalabs: Add mask for CS type bits in CS flags Oded Gabbay
2020-11-28 22:22 ` [PATCH] habanalabs: change messages to debug level Oded Gabbay
@ 2020-11-28 22:22 ` Oded Gabbay
2020-11-28 22:22 ` [PATCH] habanalabs: Modify the cs_cnt of a CB to be atomic Oded Gabbay
3 siblings, 0 replies; 5+ messages in thread
From: Oded Gabbay @ 2020-11-28 22:22 UTC (permalink / raw)
To: linux-kernel; +Cc: SW_Drivers, Ofir Bitton
From: Ofir Bitton <obitton@habana.ai>
If huge range is not valid, driver uses the host range also for
huge page allocations, but driver never frees its allocation.
This introduces a memory leak every time a user closes its context.
Signed-off-by: Ofir Bitton <obitton@habana.ai>
Reviewed-by: Oded Gabbay <ogabbay@kernel.org>
Signed-off-by: Oded Gabbay <ogabbay@kernel.org>
---
drivers/misc/habanalabs/common/memory.c | 4 +++-
1 file changed, 3 insertions(+), 1 deletion(-)
diff --git a/drivers/misc/habanalabs/common/memory.c b/drivers/misc/habanalabs/common/memory.c
index 744275dd6410..cbe9da4e0211 100644
--- a/drivers/misc/habanalabs/common/memory.c
+++ b/drivers/misc/habanalabs/common/memory.c
@@ -1761,6 +1761,7 @@ static int vm_ctx_init_with_ranges(struct hl_ctx *ctx,
goto clear_host_va_range;
}
} else {
+ kfree(ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE]);
ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE] =
ctx->va_range[HL_VA_RANGE_TYPE_HOST];
}
@@ -1906,9 +1907,10 @@ void hl_vm_ctx_fini(struct hl_ctx *ctx)
spin_unlock(&vm->idr_lock);
va_range_fini(hdev, ctx->va_range[HL_VA_RANGE_TYPE_DRAM]);
+ va_range_fini(hdev, ctx->va_range[HL_VA_RANGE_TYPE_HOST]);
+
if (hdev->pmmu_huge_range)
va_range_fini(hdev, ctx->va_range[HL_VA_RANGE_TYPE_HOST_HUGE]);
- va_range_fini(hdev, ctx->va_range[HL_VA_RANGE_TYPE_HOST]);
mutex_destroy(&ctx->mem_hash_lock);
hl_mmu_ctx_fini(ctx);
--
2.17.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
* [PATCH] habanalabs: Modify the cs_cnt of a CB to be atomic
2020-11-28 22:22 [PATCH] habanalabs: Add CB IOCTL opcode to retrieve CB information Oded Gabbay
` (2 preceding siblings ...)
2020-11-28 22:22 ` [PATCH] habanalabs: free host huge va_range if not used Oded Gabbay
@ 2020-11-28 22:22 ` Oded Gabbay
3 siblings, 0 replies; 5+ messages in thread
From: Oded Gabbay @ 2020-11-28 22:22 UTC (permalink / raw)
To: linux-kernel; +Cc: SW_Drivers, Tomer Tayar
From: Tomer Tayar <ttayar@habana.ai>
Modify the CS counter of a CB to be atomic, so no locking is required
when it is being modified or read.
Signed-off-by: Tomer Tayar <ttayar@habana.ai>
Reviewed-by: Oded Gabbay <ogabbay@kernel.org>
Signed-off-by: Oded Gabbay <ogabbay@kernel.org>
---
.../habanalabs/common/command_submission.c | 29 +++++--------------
drivers/misc/habanalabs/common/debugfs.c | 2 +-
drivers/misc/habanalabs/common/habanalabs.h | 4 +--
drivers/misc/habanalabs/gaudi/gaudi.c | 10 +++----
drivers/misc/habanalabs/goya/goya.c | 4 +--
5 files changed, 17 insertions(+), 32 deletions(-)
diff --git a/drivers/misc/habanalabs/common/command_submission.c b/drivers/misc/habanalabs/common/command_submission.c
index dc85ed6ab525..beb482310a58 100644
--- a/drivers/misc/habanalabs/common/command_submission.c
+++ b/drivers/misc/habanalabs/common/command_submission.c
@@ -233,10 +233,7 @@ static int cs_parser(struct hl_fpriv *hpriv, struct hl_cs_job *job)
job->patched_cb = parser.patched_cb;
job->job_cb_size = parser.patched_cb_size;
job->contains_dma_pkt = parser.contains_dma_pkt;
-
- spin_lock(&job->patched_cb->lock);
- job->patched_cb->cs_cnt++;
- spin_unlock(&job->patched_cb->lock);
+ atomic_inc(&job->patched_cb->cs_cnt);
}
/*
@@ -244,9 +241,7 @@ static int cs_parser(struct hl_fpriv *hpriv, struct hl_cs_job *job)
* original CB anymore because it was already parsed and
* won't be accessed again for this CS
*/
- spin_lock(&job->user_cb->lock);
- job->user_cb->cs_cnt--;
- spin_unlock(&job->user_cb->lock);
+ atomic_dec(&job->user_cb->cs_cnt);
hl_cb_put(job->user_cb);
job->user_cb = NULL;
} else if (!rc) {
@@ -268,10 +263,7 @@ static void complete_job(struct hl_device *hdev, struct hl_cs_job *job)
* created, so we need to check it's not NULL
*/
if (job->patched_cb) {
- spin_lock(&job->patched_cb->lock);
- job->patched_cb->cs_cnt--;
- spin_unlock(&job->patched_cb->lock);
-
+ atomic_dec(&job->patched_cb->cs_cnt);
hl_cb_put(job->patched_cb);
}
}
@@ -284,10 +276,7 @@ static void complete_job(struct hl_device *hdev, struct hl_cs_job *job)
if (job->is_kernel_allocated_cb &&
((job->queue_type == QUEUE_TYPE_HW && hdev->mmu_enable) ||
job->queue_type == QUEUE_TYPE_INT)) {
- spin_lock(&job->user_cb->lock);
- job->user_cb->cs_cnt--;
- spin_unlock(&job->user_cb->lock);
-
+ atomic_dec(&job->user_cb->cs_cnt);
hl_cb_put(job->user_cb);
}
@@ -680,9 +669,7 @@ static struct hl_cb *get_cb_from_cs_chunk(struct hl_device *hdev,
goto release_cb;
}
- spin_lock(&cb->lock);
- cb->cs_cnt++;
- spin_unlock(&cb->lock);
+ atomic_inc(&cb->cs_cnt);
return cb;
@@ -936,9 +923,7 @@ static int cs_ioctl_default(struct hl_fpriv *hpriv, void __user *chunks,
goto put_cs;
release_cb:
- spin_lock(&cb->lock);
- cb->cs_cnt--;
- spin_unlock(&cb->lock);
+ atomic_dec(&cb->cs_cnt);
hl_cb_put(cb);
free_cs_object:
cs_rollback(hdev, cs);
@@ -1133,7 +1118,7 @@ static int cs_ioctl_signal_wait_create_jobs(struct hl_device *hdev,
job->id = 0;
job->cs = cs;
job->user_cb = cb;
- job->user_cb->cs_cnt++;
+ atomic_inc(&job->user_cb->cs_cnt);
job->user_cb_size = cb_size;
job->hw_queue_id = q_idx;
diff --git a/drivers/misc/habanalabs/common/debugfs.c b/drivers/misc/habanalabs/common/debugfs.c
index b47a62da0b41..cef716643979 100644
--- a/drivers/misc/habanalabs/common/debugfs.c
+++ b/drivers/misc/habanalabs/common/debugfs.c
@@ -116,7 +116,7 @@ static int command_buffers_show(struct seq_file *s, void *data)
" %03llu %d 0x%08x %d %d %d\n",
cb->id, cb->ctx->asid, cb->size,
kref_read(&cb->refcount),
- cb->mmap, cb->cs_cnt);
+ cb->mmap, atomic_read(&cb->cs_cnt));
}
spin_unlock(&dev_entry->cb_spinlock);
diff --git a/drivers/misc/habanalabs/common/habanalabs.h b/drivers/misc/habanalabs/common/habanalabs.h
index 8e2d164d97e8..571eda6ef5ab 100644
--- a/drivers/misc/habanalabs/common/habanalabs.h
+++ b/drivers/misc/habanalabs/common/habanalabs.h
@@ -532,7 +532,7 @@ struct hl_cb_mgr {
* @refcount: reference counter for usage of the CB.
* @hdev: pointer to device this CB belongs to.
* @ctx: pointer to the CB owner's context.
- * @lock: spinlock to protect mmap/cs flows.
+ * @lock: spinlock to protect mmap flows.
* @debugfs_list: node in debugfs list of command buffers.
* @pool_list: node in pool list of command buffers.
* @va_block_list: list of virtual addresses blocks of the CB if it is mapped to
@@ -561,7 +561,7 @@ struct hl_cb {
dma_addr_t bus_address;
u32 mmap_size;
u32 size;
- u32 cs_cnt;
+ atomic_t cs_cnt;
u8 mmap;
u8 is_pool;
u8 is_internal;
diff --git a/drivers/misc/habanalabs/gaudi/gaudi.c b/drivers/misc/habanalabs/gaudi/gaudi.c
index aac3c9c5a2e0..1f1926607c5e 100644
--- a/drivers/misc/habanalabs/gaudi/gaudi.c
+++ b/drivers/misc/habanalabs/gaudi/gaudi.c
@@ -838,7 +838,7 @@ static int _gaudi_init_tpc_mem(struct hl_device *hdev,
job->id = 0;
job->user_cb = cb;
- job->user_cb->cs_cnt++;
+ atomic_inc(&job->user_cb->cs_cnt);
job->user_cb_size = cb_size;
job->hw_queue_id = GAUDI_QUEUE_ID_DMA_0_0;
job->patched_cb = job->user_cb;
@@ -861,7 +861,7 @@ static int _gaudi_init_tpc_mem(struct hl_device *hdev,
hl_userptr_delete_list(hdev, &job->userptr_list);
hl_debugfs_remove_job(hdev, job);
kfree(job);
- cb->cs_cnt--;
+ atomic_dec(&cb->cs_cnt);
release_cb:
hl_cb_put(cb);
@@ -1248,7 +1248,7 @@ static int gaudi_collective_wait_create_job(struct hl_device *hdev,
job->id = 0;
job->cs = cs;
job->user_cb = cb;
- job->user_cb->cs_cnt++;
+ atomic_inc(&job->user_cb->cs_cnt);
job->user_cb_size = cb_size;
job->hw_queue_id = queue_id;
@@ -5570,7 +5570,7 @@ static int gaudi_memset_device_memory(struct hl_device *hdev, u64 addr,
job->id = 0;
job->user_cb = cb;
- job->user_cb->cs_cnt++;
+ atomic_inc(&job->user_cb->cs_cnt);
job->user_cb_size = cb_size;
job->hw_queue_id = GAUDI_QUEUE_ID_DMA_0_0;
job->patched_cb = job->user_cb;
@@ -5581,7 +5581,7 @@ static int gaudi_memset_device_memory(struct hl_device *hdev, u64 addr,
rc = gaudi_send_job_on_qman0(hdev, job);
hl_debugfs_remove_job(hdev, job);
kfree(job);
- cb->cs_cnt--;
+ atomic_dec(&cb->cs_cnt);
/* Verify DMA is OK */
err_cause = RREG32(mmDMA0_CORE_ERR_CAUSE);
diff --git a/drivers/misc/habanalabs/goya/goya.c b/drivers/misc/habanalabs/goya/goya.c
index 5e151610c1c8..3e5eb9e3d7bd 100644
--- a/drivers/misc/habanalabs/goya/goya.c
+++ b/drivers/misc/habanalabs/goya/goya.c
@@ -4811,7 +4811,7 @@ static int goya_memset_device_memory(struct hl_device *hdev, u64 addr, u64 size,
job->id = 0;
job->user_cb = cb;
- job->user_cb->cs_cnt++;
+ atomic_inc(&job->user_cb->cs_cnt);
job->user_cb_size = cb_size;
job->hw_queue_id = GOYA_QUEUE_ID_DMA_0;
job->patched_cb = job->user_cb;
@@ -4823,7 +4823,7 @@ static int goya_memset_device_memory(struct hl_device *hdev, u64 addr, u64 size,
hl_debugfs_remove_job(hdev, job);
kfree(job);
- cb->cs_cnt--;
+ atomic_dec(&cb->cs_cnt);
release_cb:
hl_cb_put(cb);
--
2.17.1
^ permalink raw reply related [flat|nested] 5+ messages in thread
end of thread, other threads:[~2020-11-28 22:23 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-28 22:22 [PATCH] habanalabs: Add CB IOCTL opcode to retrieve CB information Oded Gabbay
2020-11-28 22:22 ` [PATCH] habanalabs: Add mask for CS type bits in CS flags Oded Gabbay
2020-11-28 22:22 ` [PATCH] habanalabs: change messages to debug level Oded Gabbay
2020-11-28 22:22 ` [PATCH] habanalabs: free host huge va_range if not used Oded Gabbay
2020-11-28 22:22 ` [PATCH] habanalabs: Modify the cs_cnt of a CB to be atomic Oded Gabbay
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).