From: Dinh Nguyen <dinguyen@kernel.org>
To: gregkh@linuxfoundation.org
Cc: dinguyen@kernel.org, linux-kernel@vger.kernel.org,
Mahesh Rao <mahesh.rao@altera.com>,
Matthew Gerlach <matthew.gerlach@altera.com>
Subject: [PATCH 1/5] firmware: stratix10-svc: Add mutex lock and unlock in stratix10 memory allocation/free
Date: Tue, 22 Jul 2025 11:30:41 -0500 [thread overview]
Message-ID: <20250722163045.168186-1-dinguyen@kernel.org> (raw)
From: Mahesh Rao <mahesh.rao@altera.com>
This commit adds a mutex lock to protect the
stratix10_svc_allocate_memory and
stratix10_svc_free_memory functions to ensure
thread safety when allocating and freeing memory.
This prevents potential race conditions and ensures
synchronization.
Signed-off-by: Mahesh Rao <mahesh.rao@altera.com>
Reviewed-by: Matthew Gerlach <matthew.gerlach@altera.com>
Signed-off-by: Dinh Nguyen <dinguyen@kernel.org>
---
drivers/firmware/stratix10-svc.c | 31 ++++++++++++++++++++++++-------
1 file changed, 24 insertions(+), 7 deletions(-)
diff --git a/drivers/firmware/stratix10-svc.c b/drivers/firmware/stratix10-svc.c
index e3f990d888d7..73c77b8e9f2b 100644
--- a/drivers/firmware/stratix10-svc.c
+++ b/drivers/firmware/stratix10-svc.c
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (C) 2017-2018, Intel Corporation
+ * Copyright (C) 2025, Altera Corporation
*/
#include <linux/completion.h>
@@ -171,6 +172,10 @@ struct stratix10_svc_chan {
static LIST_HEAD(svc_ctrl);
static LIST_HEAD(svc_data_mem);
+/* svc_mem_lock protects access to the svc_data_mem list for
+ * concurrent multi-client operations
+ */
+static DEFINE_MUTEX(svc_mem_lock);
/**
* svc_pa_to_va() - translate physical address to virtual address
@@ -182,14 +187,18 @@ static LIST_HEAD(svc_data_mem);
static void *svc_pa_to_va(unsigned long addr)
{
struct stratix10_svc_data_mem *pmem;
+ void *ret = NULL;
pr_debug("claim back P-addr=0x%016x\n", (unsigned int)addr);
+ mutex_lock(&svc_mem_lock);
list_for_each_entry(pmem, &svc_data_mem, node)
- if (pmem->paddr == addr)
- return pmem->vaddr;
-
- /* physical address is not found */
- return NULL;
+ if (pmem->paddr == addr) {
+ /* physical address is found */
+ ret = pmem->vaddr;
+ break;
+ }
+ mutex_unlock(&svc_mem_lock);
+ return ret;
}
/**
@@ -990,6 +999,7 @@ int stratix10_svc_send(struct stratix10_svc_chan *chan, void *msg)
p_data->flag = ct->flags;
}
} else {
+ mutex_lock(&svc_mem_lock);
list_for_each_entry(p_mem, &svc_data_mem, node)
if (p_mem->vaddr == p_msg->payload) {
p_data->paddr = p_mem->paddr;
@@ -1006,6 +1016,7 @@ int stratix10_svc_send(struct stratix10_svc_chan *chan, void *msg)
break;
}
}
+ mutex_unlock(&svc_mem_lock);
}
p_data->command = p_msg->command;
@@ -1072,9 +1083,12 @@ void *stratix10_svc_allocate_memory(struct stratix10_svc_chan *chan,
if (!pmem)
return ERR_PTR(-ENOMEM);
+ mutex_lock(&svc_mem_lock);
va = gen_pool_alloc(genpool, s);
- if (!va)
+ if (!va) {
+ mutex_unlock(&svc_mem_lock);
return ERR_PTR(-ENOMEM);
+ }
memset((void *)va, 0, s);
pa = gen_pool_virt_to_phys(genpool, va);
@@ -1086,6 +1100,7 @@ void *stratix10_svc_allocate_memory(struct stratix10_svc_chan *chan,
pr_debug("%s: va=%p, pa=0x%016x\n", __func__,
pmem->vaddr, (unsigned int)pmem->paddr);
+ mutex_unlock(&svc_mem_lock);
return (void *)va;
}
EXPORT_SYMBOL_GPL(stratix10_svc_allocate_memory);
@@ -1100,6 +1115,7 @@ EXPORT_SYMBOL_GPL(stratix10_svc_allocate_memory);
void stratix10_svc_free_memory(struct stratix10_svc_chan *chan, void *kaddr)
{
struct stratix10_svc_data_mem *pmem;
+ mutex_lock(&svc_mem_lock);
list_for_each_entry(pmem, &svc_data_mem, node)
if (pmem->vaddr == kaddr) {
@@ -1107,9 +1123,10 @@ void stratix10_svc_free_memory(struct stratix10_svc_chan *chan, void *kaddr)
(unsigned long)kaddr, pmem->size);
pmem->vaddr = NULL;
list_del(&pmem->node);
+ mutex_unlock(&svc_mem_lock);
return;
}
-
+ mutex_unlock(&svc_mem_lock);
list_del(&svc_data_mem);
}
EXPORT_SYMBOL_GPL(stratix10_svc_free_memory);
--
2.42.0.411.g813d9a9188
next reply other threads:[~2025-07-22 16:30 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-22 16:30 Dinh Nguyen [this message]
2025-07-22 16:30 ` [PATCH 2/5] firmware: stratix10-svc: Implement ID pool management for asynchronous operations Dinh Nguyen
2025-08-19 11:07 ` Greg KH
2025-08-22 9:48 ` Mahesh Rao
2025-07-22 16:30 ` [PATCH 3/5] firmware: stratix10-svc: Add initial support for asynchronous communication with Stratix10 service channel Dinh Nguyen
2025-08-19 11:08 ` Greg KH
2025-08-22 9:48 ` Mahesh Rao
2025-07-22 16:30 ` [PATCH 4/5] firmware: stratix10-svc: Add support for RSU commands in asynchronous framework Dinh Nguyen
2025-07-22 16:30 ` [PATCH 5/5] firmware: stratix10-rsu: Migrate RSU driver to use stratix10 " Dinh Nguyen
2025-08-19 11:06 ` [PATCH 1/5] firmware: stratix10-svc: Add mutex lock and unlock in stratix10 memory allocation/free Greg KH
2025-08-22 9:47 ` Mahesh Rao
2025-08-22 11:49 ` Greg KH
2025-08-26 3:49 ` Mahesh Rao
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250722163045.168186-1-dinguyen@kernel.org \
--to=dinguyen@kernel.org \
--cc=gregkh@linuxfoundation.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mahesh.rao@altera.com \
--cc=matthew.gerlach@altera.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®