habanalabs/gaudi: scrub all memory upon closing FD
PATCH 1/1 habanalabs/gaudi: scrub all memory upon closing FD
From: farah kassabri <[email protected]>
In cases of multi-tenants, administrators may want to prevent data leakage between users running on the same device one after another.
To do that the driver can scrub the internal memory (both SRAM and DRAM) after a user finish to use the memory.
Because in GAUDI the driver allows only one application to use the device at a time, it can scrub the memory when user app close FD.
In future devices where we have MMU on the DRAM, we can scrub the DRAM memory with a finer granularity (page granularity) when the user allocates the memory.
This feature is not supported in Goya.
To allow users that want to debug their applications, we add a kernel module parameter to load the driver with this feature disabled.
--- drivers/misc/habanalabs/common/context.c | 3 + drivers/misc/habanalabs/common/habanalabs.h | 5 + .../misc/habanalabs/common/habanalabs_drv.c | 6 + drivers/misc/habanalabs/common/memory.c | 23 ++- drivers/misc/habanalabs/gaudi/gaudi.c | 131 ++++++++++++++++-- drivers/misc/habanalabs/goya/goya.c | 6 + drivers/misc/habanalabs/goya/goyaP.h | 1 + 7 files changed, 161 insertions(+), 14 deletions(-)
drivers/misc/habanalabs/common/context.c +3 -0
@@ -45,6 +45,9 @@ static void hl_ctx_fini(struct hl_ctx *ctx)hl_vm_ctx_fini (ctx);hl_asid_free (hdev, ctx->asid); +/* Scrub both SRAM and DRAM */ + hdev->asic_funcs->scrub_device_mem (hdev,0 ,0 ); +if ((!hdev->pldm) && (hdev->pdev) && (!hdev->asic_funcs->is_device_idle (hdev, &idle_mask,NULL )))
drivers/misc/habanalabs/common/habanalabs.h +5 -0
@@ -746,6 +746,7 @@ enum div_select_defs {* dma_free_coherent(). This is ASIC function because * its implementation is not trivial when the driver * is loaded in simulation mode (not upstreamed). +* @scrub_device_mem: Scrub device memory given an address and size * @get_int_queue_base: get the internal queue base address. * @test_queues: run simple test on all queues for sanity check. * @asic_dma_pool_zalloc: small DMA allocation of coherent memory from DMA pool.
@@ -837,6 +838,7 @@ struct hl_asic_funcs { dma_addr_t *dma_handle, gfp_t flag);void (*asic_dma_free_coherent)(struct hl_device *hdev,size_t size,void *cpu_addr,dma_addr_t dma_handle); +int (*scrub_device_mem)(struct hl_device *hdev,u64 addr,u64 size);void * (*get_int_queue_base)(struct hl_device *hdev,u32 queue_id,dma_addr_t *dma_handle,u16 *queue_len);int (*test_queues)(struct hl_device *hdev);
@@ -1698,6 +1700,8 @@ struct hl_mmu_funcs {* otherwise. * @dram_supports_virtual_memory: is MMU enabled towards DRAM. * @dram_default_page_mapping: is DRAM default page mapping enabled. +* @memory_scrub: true to perform device memory scrub in various locations, +* such as context-switch, context close, page free, etc. * @pmmu_huge_range: is a different virtual addresses range used for PMMU with * huge pages. * @init_done: is the initialization of the device done.
@@ -1802,6 +1806,7 @@ struct hl_device {u8 reset_on_lockup;u8 dram_supports_virtual_memory;u8 dram_default_page_mapping; +u8 memory_scrub;u8 pmmu_huge_range;u8 init_done;u8 device_cpu_disabled;
drivers/misc/habanalabs/common/habanalabs_drv.c +6 -0
@@ -29,6 +29,7 @@ static DEFINE_MUTEX(hl_devs_idr_lock);static int timeout_locked =5 ;static int reset_on_lockup =1 ; +static int memory_scrub =1 ;module_param (timeout_locked, int,0444 ); MODULE_PARM_DESC(timeout_locked,
@@ -38,6 +39,10 @@ module_param(reset_on_lockup, int, 0444);MODULE_PARM_DESC (reset_on_lockup,"Do device reset on lockup (0 = no, 1 = yes, default yes)" ); +module_param (memory_scrub, int,0444 ); +MODULE_PARM_DESC (memory_scrub, +"Scrub device memory in various states (0 = no, 1 = yes, default yes)" ); +#define PCI_VENDOR_ID_HABANALABS 0x1da3#define PCI_IDS_GOYA 0x0001
@@ -284,6 +289,7 @@ int create_hdev(struct hl_device **dev, struct pci_dev *pdev, hdev->major = hl_major; hdev->reset_on_lockup = reset_on_lockup; + hdev->memory_scrub = memory_scrub; hdev->pldm =0 ;set_driver_behavior_per_device (hdev);
drivers/misc/habanalabs/common/memory.c +22 -1
@@ -46,7 +46,7 @@* @ret_handle : result handle * * This function does the following: -* - Allocate the requested size rounded up to 2MB pages +* - Allocate the requested size rounded up to 'dram_page_size' pages * - Return unique handle */ static int alloc_device_memory(struct hl_ctx *ctx, struct hl_mem_in *args,
@@ -81,6 +81,16 @@ static int alloc_device_memory(struct hl_ctx *ctx, struct hl_mem_in *args, num_pgs, total_size);return -ENOMEM ; } + +if (hdev->memory_scrub) { + rc = hdev->asic_funcs->scrub_device_mem (hdev, paddr, + total_size); +if (rc) { +dev_err (hdev->dev, +"Failed to scrub contiguous device memory\n" ); + goto pages_pack_err; + } + } } phys_pg_pack =kzalloc (sizeof (*phys_pg_pack),GFP_KERNEL );
@@ -118,6 +128,17 @@ static int alloc_device_memory(struct hl_ctx *ctx, struct hl_mem_in *args, goto page_err; } +if (hdev->memory_scrub) { + rc = hdev->asic_funcs->scrub_device_mem (hdev, + phys_pg_pack->pages[i], + page_size); +if (rc) { +dev_err (hdev->dev, +"Failed to scrub device memory\n" ); + goto page_err; + } + } + num_curr_pgs++; } }
drivers/misc/habanalabs/gaudi/gaudi.c +118 -13
155 lines not on this page · raw message
drivers/misc/habanalabs/goya/goya.c +6 -0
@@ -2795,6 +2795,11 @@ static void goya_dma_free_coherent(struct hl_device *hdev, size_t size,dma_free_coherent (&hdev->pdev->dev, size, cpu_addr, fixed_dma_handle); } +int goya_scrub_device_mem (struct hl_device *hdev,u64 addr,u64 size) +{ +return 0 ; +} +void *goya_get_int_queue_base (struct hl_device *hdev,u32 queue_id,dma_addr_t *dma_handle,u16 *queue_len) {
@@ -5374,6 +5379,7 @@ static const struct hl_asic_funcs goya_funcs = { .pqe_write = goya_pqe_write, .asic_dma_alloc_coherent = goya_dma_alloc_coherent, .asic_dma_free_coherent = goya_dma_free_coherent, + .scrub_device_mem = goya_scrub_device_mem, .get_int_queue_base = goya_get_int_queue_base, .test_queues = goya_test_queues, .asic_dma_pool_zalloc = goya_dma_pool_zalloc,
drivers/misc/habanalabs/goya/goyaP.h +1 -0
@@ -221,6 +221,7 @@ void goya_add_end_of_cb_packets(struct hl_device *hdev, u64 kernel_address, u32 len, u64 cq_addr, u32 cq_val, u32 msix_vec, bool eb);int goya_cs_parser (struct hl_device *hdev,struct hl_cs_parser *parser); +int goya_scrub_device_mem (struct hl_device *hdev,u64 addr,u64 size);void *goya_get_int_queue_base (struct hl_device *hdev,u32 queue_id,dma_addr_t *dma_handle,u16 *queue_len);u32 goya_get_dma_desc_list_size (struct hl_device *hdev,struct sg_table *sgt);
Signature · 1 line
2.17.1