diff options
author | Simon Glass <sjg@chromium.org> | 2024-09-01 16:26:27 -0600 |
---|---|---|
committer | Tom Rini <trini@konsulko.com> | 2024-09-18 13:01:00 -0600 |
commit | f452e8f092b3e500ac955e44b7a3f1680d62d8f7 (patch) | |
tree | 19f40f83ee123b09286180db8fb2c2246f0b5893 /arch/sandbox/cpu/cpu.c | |
parent | 505b21b60720f00ef67d880fa7f176cbd207c1c3 (diff) |
sandbox: Implement reference counting for address mapping
An address may be mapped twice and unmapped twice. Delete the mapping
only when the last user unmaps it.
Fix a missing comment while here.
Signed-off-by: Simon Glass <sjg@chromium.org>
Diffstat (limited to 'arch/sandbox/cpu/cpu.c')
-rw-r--r-- | arch/sandbox/cpu/cpu.c | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/arch/sandbox/cpu/cpu.c b/arch/sandbox/cpu/cpu.c index 3e1c0dd583e..51ce40e7f08 100644 --- a/arch/sandbox/cpu/cpu.c +++ b/arch/sandbox/cpu/cpu.c @@ -111,6 +111,7 @@ void *phys_to_virt(phys_addr_t paddr) if (mentry->tag == paddr) { log_debug("Used map from %lx to %p\n", (ulong)paddr, mentry->ptr); + mentry->refcnt++; return mentry->ptr; } } @@ -200,10 +201,12 @@ void unmap_physmem(const void *ptr, unsigned long flags) mentry = find_tag(ptr); if (mentry) { - list_del(&mentry->sibling_node); - log_debug("Removed map from %p to %lx\n", ptr, - (ulong)mentry->tag); - free(mentry); + if (!--mentry->refcnt) { + list_del(&mentry->sibling_node); + log_debug("Removed map from %p to %lx\n", ptr, + (ulong)mentry->tag); + free(mentry); + } } else { log_warning("Address not mapped: %p\n", ptr); } @@ -235,11 +238,14 @@ phys_addr_t map_to_sysmem(const void *ptr) } mentry->tag = state->next_tag++; mentry->ptr = (void *)ptr; + mentry->refcnt = 0; list_add_tail(&mentry->sibling_node, &state->mapmem_head); log_debug("Added map from %p to %lx\n", ptr, (ulong)mentry->tag); } + mentry->refcnt++; + /* * Return the tag as the address to use. A later call to map_sysmem() * will return ptr |