habanalabs: debugfs access to user mapped host addresses
PATCH 1/1 habanalabs: debugfs access to user mapped host addresses
From: Ofir Bitton <[email protected]>
In order to have a better debuggability we allow debugfs access to user mmu mapped host memory. Non-user host memory access will be rejected.
Signed-off-by Ofir Bitton <[email protected]>
Reviewed-by Oded Gabbay <[email protected]>
Signed-off-by Oded Gabbay <[email protected]>
--- drivers/misc/habanalabs/common/debugfs.c | 88 ++++++++++++++++----- drivers/misc/habanalabs/common/habanalabs.h | 12 ++- drivers/misc/habanalabs/common/mmu/mmu.c | 2 + drivers/misc/habanalabs/gaudi/gaudi.c | 40 ++++++++-- drivers/misc/habanalabs/goya/goya.c | 44 +++++++++-- 5 files changed, 145 insertions(+), 41 deletions(-)
drivers/misc/habanalabs/common/debugfs.c +67 -21
@@ -457,21 +457,58 @@ static bool hl_is_device_va(struct hl_device *hdev, u64 addr)return false; } -static int device_va_to_pa (struct hl_device *hdev,u64 virt_addr, -u64 *phys_addr) +static int device_va_to_pa (struct hl_device *hdev,u64 virt_addr,u32 size, +u64 *phys_addr) { +struct hl_vm_phys_pg_pack *phys_pg_pack;struct hl_ctx *ctx = hdev->compute_ctx; -int rc =0 ; +struct hl_vm_hash_node *hnode; +struct hl_userptr *userptr; +enum vm_type_t *vm_type; +bool valid = false; +u64 end_address; +u32 range_size; +int i, rc =0 ;if (!ctx) {dev_err (hdev->dev,"no ctx available\n" );return -EINVAL ; } +/* Verify address is mapped */ +mutex_lock (&ctx->mem_hash_lock); +hash_for_each (ctx->mem_hash, i, hnode, node) { + vm_type = hnode->ptr; + +if (*vm_type ==VM_TYPE_USERPTR ) { + userptr = hnode->ptr; + range_size = userptr->size; + }else { + phys_pg_pack = hnode->ptr; + range_size = phys_pg_pack->total_size; + } + + end_address = virt_addr + size; +if ((virt_addr >= hnode->vaddr) && + (end_address <= hnode->vaddr + range_size)) { + valid = true; +break ; + } + } +mutex_unlock (&ctx->mem_hash_lock); + +if (!valid) { +dev_err (hdev->dev, +"virt addr 0x%llx is not mapped\n" , + virt_addr); +return -EINVAL ; + } + rc =hl_mmu_va_to_pa (ctx, virt_addr, phys_addr);if (rc) { -dev_err (hdev->dev,"virt addr 0x%llx is not mapped to phys addr\n" , - virt_addr); +dev_err (hdev->dev, +"virt addr 0x%llx is not mapped to phys addr\n" , + virt_addr); rc = -EINVAL ; }
@@ -483,10 +520,11 @@ static ssize_t hl_data_read32(struct file *f, char __user *buf, {struct hl_dbg_device_entry *entry =file_inode (f)->i_private;struct hl_device *hdev = entry->hdev; +u64 va, addr = entry->addr;char tmp_buf[32 ]; -u64 addr = entry->addr; -u32 val; +bool user_address;ssize_t rc; +u32 val;if (atomic_read (&hdev->in_reset)) {dev_warn_ratelimited (hdev->dev,"Can't read during reset\n" );
@@ -496,13 +534,15 @@ static ssize_t hl_data_read32(struct file *f, char __user *buf,if (*ppos)return 0 ; -if (hl_is_device_va (hdev, addr)) { - rc =device_va_to_pa (hdev, addr, &addr); + user_address =hl_is_device_va (hdev, addr); +if (user_address) { + va = addr; + rc =device_va_to_pa (hdev, addr,sizeof (val), &addr);if (rc)return rc; } - rc = hdev->asic_funcs->debugfs_read32 (hdev, addr, &val); + rc = hdev->asic_funcs->debugfs_read32 (hdev, addr, user_address, &val);if (rc) {dev_err (hdev->dev,"Failed to read from 0x%010llx\n" , addr);return rc;
@@ -519,6 +559,7 @@ static ssize_t hl_data_write32(struct file *f, const char __user *buf,struct hl_dbg_device_entry *entry =file_inode (f)->i_private;struct hl_device *hdev = entry->hdev;u64 addr = entry->addr; +bool user_address;u32 value;ssize_t rc;
@@ -531,13 +572,14 @@ static ssize_t hl_data_write32(struct file *f, const char __user *buf,if (rc)return rc; -if (hl_is_device_va (hdev, addr)) { - rc =device_va_to_pa (hdev, addr, &addr); + user_address =hl_is_device_va (hdev, addr); +if (user_address) { + rc =device_va_to_pa (hdev, addr,sizeof (value), &addr);if (rc)return rc; } - rc = hdev->asic_funcs->debugfs_write32 (hdev, addr, value); + rc = hdev->asic_funcs->debugfs_write32 (hdev, addr, user_address, value);if (rc) {dev_err (hdev->dev,"Failed to write 0x%08x to 0x%010llx\n" , value, addr);
@@ -552,21 +594,23 @@ static ssize_t hl_data_read64(struct file *f, char __user *buf, {struct hl_dbg_device_entry *entry =file_inode (f)->i_private;struct hl_device *hdev = entry->hdev; -char tmp_buf[32 ];u64 addr = entry->addr; -u64 val; +bool user_address; +char tmp_buf[32 ];ssize_t rc; +u64 val;if (*ppos)return 0 ; -if (hl_is_device_va (hdev, addr)) { - rc =device_va_to_pa (hdev, addr, &addr); + user_address =hl_is_device_va (hdev, addr); +if (user_address) { + rc =device_va_to_pa (hdev, addr,sizeof (val), &addr);if (rc)return rc; } - rc = hdev->asic_funcs->debugfs_read64 (hdev, addr, &val); + rc = hdev->asic_funcs->debugfs_read64 (hdev, addr, user_address, &val);if (rc) {dev_err (hdev->dev,"Failed to read from 0x%010llx\n" , addr);return rc;
@@ -583,6 +627,7 @@ static ssize_t hl_data_write64(struct file *f, const char __user *buf,struct hl_dbg_device_entry *entry =file_inode (f)->i_private;struct hl_device *hdev = entry->hdev;u64 addr = entry->addr; +bool user_address;u64 value;ssize_t rc;
@@ -590,13 +635,14 @@ static ssize_t hl_data_write64(struct file *f, const char __user *buf,if (rc)return rc; -if (hl_is_device_va (hdev, addr)) { - rc =device_va_to_pa (hdev, addr, &addr); + user_address =hl_is_device_va (hdev, addr); +if (user_address) { + rc =device_va_to_pa (hdev, addr,sizeof (value), &addr);if (rc)return rc; } - rc = hdev->asic_funcs->debugfs_write64 (hdev, addr, value); + rc = hdev->asic_funcs->debugfs_write64 (hdev, addr, user_address, value);if (rc) {dev_err (hdev->dev,"Failed to write 0x%016llx to 0x%010llx\n" , value, addr);
drivers/misc/habanalabs/common/habanalabs.h +8 -4
@@ -969,10 +969,14 @@ struct hl_asic_funcs {void (*update_eq_ci)(struct hl_device *hdev,u32 val);int (*context_switch)(struct hl_device *hdev,u32 asid);void (*restore_phase_topology)(struct hl_device *hdev); -int (*debugfs_read32)(struct hl_device *hdev,u64 addr,u32 *val); -int (*debugfs_write32)(struct hl_device *hdev,u64 addr,u32 val); -int (*debugfs_read64)(struct hl_device *hdev,u64 addr,u64 *val); -int (*debugfs_write64)(struct hl_device *hdev,u64 addr,u64 val); +int (*debugfs_read32)(struct hl_device *hdev,u64 addr, +bool user_address,u32 *val); +int (*debugfs_write32)(struct hl_device *hdev,u64 addr, +bool user_address,u32 val); +int (*debugfs_read64)(struct hl_device *hdev,u64 addr, +bool user_address,u64 *val); +int (*debugfs_write64)(struct hl_device *hdev,u64 addr, +bool user_address,u64 val);void (*add_device_attr)(struct hl_device *hdev,struct attribute_group *dev_attr_grp); void (*handle_eqe)(struct hl_device *hdev,
drivers/misc/habanalabs/common/mmu/mmu.c +2 -0
@@ -518,6 +518,8 @@ int hl_mmu_va_to_pa(struct hl_ctx *ctx, u64 virt_addr, u64 *phys_addr)struct hl_mmu_hop_info hops;int rc; +memset (&hops,0 ,sizeof (hops)); + rc =hl_mmu_get_tlb_info (ctx, virt_addr, &hops);if (rc)return rc;
drivers/misc/habanalabs/gaudi/gaudi.c +32 -8
@@ -5911,13 +5911,16 @@ static void gaudi_restore_phase_topology(struct hl_device *hdev) } -static int gaudi_debugfs_read32 (struct hl_device *hdev,u64 addr,u32 *val) +static int gaudi_debugfs_read32 (struct hl_device *hdev,u64 addr, +bool user_address,u32 *val) {struct asic_fixed_properties *prop = &hdev->asic_prop;struct gaudi_device *gaudi = hdev->asic_specific; -u64 hbm_bar_addr; +u64 hbm_bar_addr, host_phys_end;int rc =0 ; + host_phys_end =HOST_PHYS_BASE +HOST_PHYS_SIZE ; +if ((addr >=CFG_BASE ) && (addr <CFG_BASE +CFG_SIZE )) { if ((gaudi->hw_cap_initialized & HW_CAP_CLK_GATE) &&
@@ -5949,6 +5952,9 @@ static int gaudi_debugfs_read32(struct hl_device *hdev, u64 addr, u32 *val) }if (hbm_bar_addr ==U64_MAX ) rc = -EIO ; + }else if (addr >=HOST_PHYS_BASE && addr < host_phys_end && + user_address && !iommu_present (&pci_bus_type)) { + *val = *(u32 *)phys_to_virt (addr -HOST_PHYS_BASE ); }else { rc = -EFAULT ; }
@@ -5956,13 +5962,16 @@ static int gaudi_debugfs_read32(struct hl_device *hdev, u64 addr, u32 *val)return rc; } -static int gaudi_debugfs_write32 (struct hl_device *hdev,u64 addr,u32 val) +static int gaudi_debugfs_write32 (struct hl_device *hdev,u64 addr, +bool user_address,u32 val) {struct asic_fixed_properties *prop = &hdev->asic_prop;struct gaudi_device *gaudi = hdev->asic_specific; -u64 hbm_bar_addr; +u64 hbm_bar_addr, host_phys_end;int rc =0 ; + host_phys_end =HOST_PHYS_BASE +HOST_PHYS_SIZE ; +if ((addr >=CFG_BASE ) && (addr <CFG_BASE +CFG_SIZE )) { if ((gaudi->hw_cap_initialized & HW_CAP_CLK_GATE) &&
@@ -5994,6 +6003,9 @@ static int gaudi_debugfs_write32(struct hl_device *hdev, u64 addr, u32 val) }if (hbm_bar_addr ==U64_MAX ) rc = -EIO ; + }else if (addr >=HOST_PHYS_BASE && addr < host_phys_end && + user_address && !iommu_present (&pci_bus_type)) { + *(u32 *)phys_to_virt (addr -HOST_PHYS_BASE ) = val; }else { rc = -EFAULT ; }
@@ -6001,13 +6013,16 @@ static int gaudi_debugfs_write32(struct hl_device *hdev, u64 addr, u32 val)return rc; } -static int gaudi_debugfs_read64 (struct hl_device *hdev,u64 addr,u64 *val) +static int gaudi_debugfs_read64 (struct hl_device *hdev,u64 addr, +bool user_address,u64 *val) {struct asic_fixed_properties *prop = &hdev->asic_prop;struct gaudi_device *gaudi = hdev->asic_specific; -u64 hbm_bar_addr; +u64 hbm_bar_addr, host_phys_end;int rc =0 ; + host_phys_end =HOST_PHYS_BASE +HOST_PHYS_SIZE ; +if ((addr >=CFG_BASE ) && (addr <=CFG_BASE +CFG_SIZE -sizeof (u64))) { if ((gaudi->hw_cap_initialized & HW_CAP_CLK_GATE) &&
@@ -6043,6 +6058,9 @@ static int gaudi_debugfs_read64(struct hl_device *hdev, u64 addr, u64 *val) }if (hbm_bar_addr ==U64_MAX ) rc = -EIO ; + }else if (addr >=HOST_PHYS_BASE && addr < host_phys_end && + user_address && !iommu_present (&pci_bus_type)) { + *val = *(u64 *)phys_to_virt (addr -HOST_PHYS_BASE ); }else { rc = -EFAULT ; }
@@ -6050,13 +6068,16 @@ static int gaudi_debugfs_read64(struct hl_device *hdev, u64 addr, u64 *val)return rc; } -static int gaudi_debugfs_write64 (struct hl_device *hdev,u64 addr,u64 val) +static int gaudi_debugfs_write64 (struct hl_device *hdev,u64 addr, +bool user_address,u64 val) {struct asic_fixed_properties *prop = &hdev->asic_prop;struct gaudi_device *gaudi = hdev->asic_specific; -u64 hbm_bar_addr; +u64 hbm_bar_addr, host_phys_end;int rc =0 ; + host_phys_end =HOST_PHYS_BASE +HOST_PHYS_SIZE ; +if ((addr >=CFG_BASE ) && (addr <=CFG_BASE +CFG_SIZE -sizeof (u64))) { if ((gaudi->hw_cap_initialized & HW_CAP_CLK_GATE) &&
@@ -6091,6 +6112,9 @@ static int gaudi_debugfs_write64(struct hl_device *hdev, u64 addr, u64 val) }if (hbm_bar_addr ==U64_MAX ) rc = -EIO ; + }else if (addr >=HOST_PHYS_BASE && addr < host_phys_end && + user_address && !iommu_present (&pci_bus_type)) { + *(u64 *)phys_to_virt (addr -HOST_PHYS_BASE ) = val; }else { rc = -EFAULT ; }
drivers/misc/habanalabs/goya/goya.c +36 -8
108 lines not on this page · raw message
Signature · 1 line
2.25.1