From 0b38202bc82d4a417fe9fd689ac99f986efb0732 Mon Sep 17 00:00:00 2001 From: Ian Moffett Date: Thu, 25 Apr 2024 01:05:05 -0400 Subject: kernel: vm_map: Cleanup on failure in mmap() Signed-off-by: Ian Moffett --- sys/vm/vm_map.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) (limited to 'sys/vm/vm_map.c') diff --git a/sys/vm/vm_map.c b/sys/vm/vm_map.c index 5d40d29..f338f4f 100644 --- a/sys/vm/vm_map.c +++ b/sys/vm/vm_map.c @@ -340,7 +340,7 @@ mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off) if (__TEST(flags, MAP_ANONYMOUS)) { /* Try to create a virtual memory object */ if (vm_obj_init(&vmobj, NULL) != 0) - return 0; + goto fail; /* * If 'addr' is NULL, we'll just allocate physical @@ -371,16 +371,15 @@ mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off) /* Did this work? */ if (physmem == 0 && addr == NULL) { - return MAP_FAILED; + goto fail; } } else if (__TEST(flags, MAP_SHARED)) { physmem = vm_fd_map(addr, prot, len, off, fildes, mapping); if (physmem == 0) { - return MAP_FAILED; + goto fail; } } - map_start = __ALIGN_DOWN((vaddr_t)addr, GRANULE); map_end = map_start + __ALIGN_UP(len + misalign, GRANULE); @@ -393,6 +392,9 @@ mmap(void *addr, size_t len, int prot, int flags, int fildes, off_t off) vm_mapspace_insert(&td->mapspace, mapping); spinlock_release(&td->mapspace_lock); return (void *)addr; +fail: + DESTROY_MAPPING(mapping); + return MAP_FAILED; } /* -- cgit v1.2.3