ghes_estatus_pool_init() always creates a new pool, overwriting the global
pointer. This is sufficient while GHES is its only caller, but additional
firmware-first providers need to contribute capacity to the same pool.
Concurrent initialization could also replace a pool that is already in use.

Serialize initialization, create the pool only for the first caller, and
add a separately allocated region for every caller. Destroy the pool after
an allocation failure only when it has no existing regions.

Reported-by: [email protected]
Link: https://sashiko.dev/#/message/20260518123939.A9E15C2BCB7%40smtp.kernel.org
Signed-off-by: Ahmed Tiba <[email protected]>
---
 drivers/acpi/apei/ghes.c | 31 +++++++++++++++++++++++--------
 1 file changed, 23 insertions(+), 8 deletions(-)

diff --git a/drivers/acpi/apei/ghes.c b/drivers/acpi/apei/ghes.c
index b38806d205ab..b48b1f56a429 100644
--- a/drivers/acpi/apei/ghes.c
+++ b/drivers/acpi/apei/ghes.c
@@ -172,6 +172,7 @@ struct ghes_vendor_record_entry {
 };
 
 static struct gen_pool *ghes_estatus_pool;
+static DEFINE_MUTEX(ghes_estatus_pool_lock);
 
 static struct ghes_estatus_cache __rcu 
*ghes_estatus_caches[GHES_ESTATUS_CACHES_SIZE];
 static atomic_t ghes_estatus_cache_alloced;
@@ -201,30 +202,44 @@ int ghes_estatus_pool_init(unsigned int num_ghes)
        unsigned long addr, len;
        int rc;
 
-       ghes_estatus_pool = gen_pool_create(GHES_ESTATUS_POOL_MIN_ALLOC_ORDER, 
-1);
-       if (!ghes_estatus_pool)
-               return -ENOMEM;
-
        len = GHES_ESTATUS_CACHE_AVG_SIZE * GHES_ESTATUS_CACHE_ALLOCED_MAX;
        len += (num_ghes * GHES_ESOURCE_PREALLOC_MAX_SIZE);
 
+       mutex_lock(&ghes_estatus_pool_lock);
+
+       if (!ghes_estatus_pool) {
+               ghes_estatus_pool = 
gen_pool_create(GHES_ESTATUS_POOL_MIN_ALLOC_ORDER, -1);
+               if (!ghes_estatus_pool) {
+                       rc = -ENOMEM;
+                       goto out_unlock;
+               }
+       }
+
        addr = (unsigned long)vmalloc(PAGE_ALIGN(len));
-       if (!addr)
+       if (!addr) {
+               rc = -ENOMEM;
                goto err_pool_alloc;
+       }
 
        rc = gen_pool_add(ghes_estatus_pool, addr, PAGE_ALIGN(len), -1);
        if (rc)
                goto err_pool_add;
 
+       mutex_unlock(&ghes_estatus_pool_lock);
        return 0;
 
 err_pool_add:
        vfree((void *)addr);
 
 err_pool_alloc:
-       gen_pool_destroy(ghes_estatus_pool);
-
-       return -ENOMEM;
+       if (!gen_pool_avail(ghes_estatus_pool) &&
+           !gen_pool_size(ghes_estatus_pool)) {
+               gen_pool_destroy(ghes_estatus_pool);
+               ghes_estatus_pool = NULL;
+       }
+out_unlock:
+       mutex_unlock(&ghes_estatus_pool_lock);
+       return rc;
 }
 
 /**

-- 
2.43.0


Reply via email to