Skip to content

Commit 833a426

Browse files
Francesco Ruggerirafaeljw
authored andcommitted
ACPI: OSL: only free map once in osl.c
acpi_os_map_cleanup checks map->refcount outside of acpi_ioremap_lock before freeing the map. This creates a race condition the can result in the map being freed more than once. A panic can be caused by running for ((i=0; i<10; i++)) do for ((j=0; j<100000; j++)) do cat /sys/firmware/acpi/tables/data/BERT >/dev/null done & done This patch makes sure that only the process that drops the reference to 0 does the freeing. Fixes: b7c1fad ("ACPI: Do not use krefs under a mutex in osl.c") Signed-off-by: Francesco Ruggeri <[email protected]> Reviewed-by: Dmitry Safonov <[email protected]> Cc: All applicable <[email protected]> Signed-off-by: Rafael J. Wysocki <[email protected]>
1 parent 6e9f879 commit 833a426

File tree

1 file changed

+17
-11
lines changed

1 file changed

+17
-11
lines changed

drivers/acpi/osl.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -374,19 +374,21 @@ void *__ref acpi_os_map_memory(acpi_physical_address phys, acpi_size size)
374374
}
375375
EXPORT_SYMBOL_GPL(acpi_os_map_memory);
376376

377-
static void acpi_os_drop_map_ref(struct acpi_ioremap *map)
377+
/* Must be called with mutex_lock(&acpi_ioremap_lock) */
378+
static unsigned long acpi_os_drop_map_ref(struct acpi_ioremap *map)
378379
{
379-
if (!--map->refcount)
380+
unsigned long refcount = --map->refcount;
381+
382+
if (!refcount)
380383
list_del_rcu(&map->list);
384+
return refcount;
381385
}
382386

383387
static void acpi_os_map_cleanup(struct acpi_ioremap *map)
384388
{
385-
if (!map->refcount) {
386-
synchronize_rcu_expedited();
387-
acpi_unmap(map->phys, map->virt);
388-
kfree(map);
389-
}
389+
synchronize_rcu_expedited();
390+
acpi_unmap(map->phys, map->virt);
391+
kfree(map);
390392
}
391393

392394
/**
@@ -406,6 +408,7 @@ static void acpi_os_map_cleanup(struct acpi_ioremap *map)
406408
void __ref acpi_os_unmap_iomem(void __iomem *virt, acpi_size size)
407409
{
408410
struct acpi_ioremap *map;
411+
unsigned long refcount;
409412

410413
if (!acpi_permanent_mmap) {
411414
__acpi_unmap_table(virt, size);
@@ -419,10 +422,11 @@ void __ref acpi_os_unmap_iomem(void __iomem *virt, acpi_size size)
419422
WARN(true, PREFIX "%s: bad address %p\n", __func__, virt);
420423
return;
421424
}
422-
acpi_os_drop_map_ref(map);
425+
refcount = acpi_os_drop_map_ref(map);
423426
mutex_unlock(&acpi_ioremap_lock);
424427

425-
acpi_os_map_cleanup(map);
428+
if (!refcount)
429+
acpi_os_map_cleanup(map);
426430
}
427431
EXPORT_SYMBOL_GPL(acpi_os_unmap_iomem);
428432

@@ -457,6 +461,7 @@ void acpi_os_unmap_generic_address(struct acpi_generic_address *gas)
457461
{
458462
u64 addr;
459463
struct acpi_ioremap *map;
464+
unsigned long refcount;
460465

461466
if (gas->space_id != ACPI_ADR_SPACE_SYSTEM_MEMORY)
462467
return;
@@ -472,10 +477,11 @@ void acpi_os_unmap_generic_address(struct acpi_generic_address *gas)
472477
mutex_unlock(&acpi_ioremap_lock);
473478
return;
474479
}
475-
acpi_os_drop_map_ref(map);
480+
refcount = acpi_os_drop_map_ref(map);
476481
mutex_unlock(&acpi_ioremap_lock);
477482

478-
acpi_os_map_cleanup(map);
483+
if (!refcount)
484+
acpi_os_map_cleanup(map);
479485
}
480486
EXPORT_SYMBOL(acpi_os_unmap_generic_address);
481487

0 commit comments

Comments
 (0)