diff --git a/arch/lkl/include/uapi/asm/host_ops.h b/arch/lkl/include/uapi/asm/host_ops.h index 673a059512108a..0d4887974115b7 100644 --- a/arch/lkl/include/uapi/asm/host_ops.h +++ b/arch/lkl/include/uapi/asm/host_ops.h @@ -140,6 +140,10 @@ enum lkl_prot { * protection * @munmap - unmap previously mapped memory * + * @shmem_init - initialize host shared memory for MMU physical memory + * @shmem_mmap - map host shared memory into address space + * @shmem_cleanup - cleanup host shared memory resources + * * @pci_ops - pointer to PCI host operations */ struct lkl_host_operations { @@ -199,6 +203,7 @@ struct lkl_host_operations { void (*shmem_init)(unsigned long size); void *(*shmem_mmap)(void *addr, unsigned long pg_off, unsigned long size, enum lkl_prot prot); + void (*shmem_cleanup)(void); struct lkl_dev_pci_ops *pci_ops; }; diff --git a/arch/lkl/mm/bootmem.c b/arch/lkl/mm/bootmem.c index 79958137971b48..8734a24c94c2a5 100644 --- a/arch/lkl/mm/bootmem.c +++ b/arch/lkl/mm/bootmem.c @@ -78,4 +78,8 @@ void free_mem(void) lkl_ops->page_free(_memory_start, mem_size); else lkl_ops->mem_free(_memory_start); +#ifdef CONFIG_MMU + if (lkl_ops->shmem_cleanup) + lkl_ops->shmem_cleanup(); +#endif } diff --git a/tools/lkl/lib/posix-host.c b/tools/lkl/lib/posix-host.c index 2f4e0191dd78a7..5f3060558afd8a 100644 --- a/tools/lkl/lib/posix-host.c +++ b/tools/lkl/lib/posix-host.c @@ -531,6 +531,7 @@ static void shmem_init(unsigned long size) snprintf(lkl_shmem_id, sizeof(lkl_shmem_id), "/lkl_phys_mem_%d", getpid()); shared_mem_fd = shm_open(lkl_shmem_id, O_RDWR | O_CREAT, rwx); assert(shared_mem_fd >= 0); + assert(shm_unlink(lkl_shmem_id) == 0); shared_mem_size = size; assert(ftruncate(shared_mem_fd, shared_mem_size) == 0); } @@ -546,6 +547,14 @@ static void *lkl_shmem_mmap(void *addr, unsigned long pg_off, return NULL; return ret; } + +static void shmem_cleanup(void) +{ + if (shared_mem_fd >= 0) { + close(shared_mem_fd); + shared_mem_fd = -1; + } +} #endif // LKL_HOST_CONFIG_MMU static void *posix_malloc(unsigned long size) @@ -611,6 +620,7 @@ struct lkl_host_operations lkl_host_ops = { #ifdef LKL_HOST_CONFIG_MMU .shmem_init = shmem_init, .shmem_mmap = lkl_shmem_mmap, + .shmem_cleanup = shmem_cleanup, #endif #ifdef LKL_HOST_CONFIG_VFIO_PCI .pci_ops = &vfio_pci_ops,