From: Chenghai Huang <huangchenghai2@huawei.com>
To: <herbert@gondor.apana.org.au>, <davem@davemloft.net>
Cc: <linux-kernel@vger.kernel.org>, <linux-crypto@vger.kernel.org>,
<fanghao11@huawei.com>, <liulongfang@huawei.com>,
<qianweili@huawei.com>, <linwenkai6@hisilicon.com>,
<wangzhou1@hisilicon.com>, <lizhi206@huawei.com>,
<taoqi10@huawei.com>
Subject: [PATCH v4 07/11] crypto: hisilicon/qm - add reference counting to queues for tfm kernel reuse
Date: Thu, 18 Dec 2025 21:44:48 +0800 [thread overview]
Message-ID: <20251218134452.1125469-8-huangchenghai2@huawei.com> (raw)
In-Reply-To: <20251218134452.1125469-1-huangchenghai2@huawei.com>
Add reference counting to queues. When all queues are occupied, tfm
will reuse queues with the same algorithm type that have already
been allocated in the kernel. The corresponding queue will be
released when the reference count reaches 1.
Reviewed-by: Longfang Liu <liulongfang@huawei.com>
Signed-off-by: Chenghai Huang <huangchenghai2@huawei.com>
Signed-off-by: Weili Qian <qianweili@huawei.com>
---
drivers/crypto/hisilicon/qm.c | 65 +++++++++++++++++++++++++++--------
include/linux/hisi_acc_qm.h | 1 +
2 files changed, 52 insertions(+), 14 deletions(-)
diff --git a/drivers/crypto/hisilicon/qm.c b/drivers/crypto/hisilicon/qm.c
index 4cdb443e2889..3883b6176213 100644
--- a/drivers/crypto/hisilicon/qm.c
+++ b/drivers/crypto/hisilicon/qm.c
@@ -2002,7 +2002,38 @@ static void hisi_qm_unset_hw_reset(struct hisi_qp *qp)
*addr = 0;
}
-static struct hisi_qp *qm_create_qp_nolock(struct hisi_qm *qm, u8 alg_type)
+static struct hisi_qp *find_shareable_qp(struct hisi_qm *qm, u8 alg_type, bool is_in_kernel)
+{
+ struct device *dev = &qm->pdev->dev;
+ struct hisi_qp *share_qp = NULL;
+ struct hisi_qp *qp;
+ u32 ref_count = ~0;
+ int i;
+
+ if (!is_in_kernel)
+ goto queues_busy;
+
+ for (i = 0; i < qm->qp_num; i++) {
+ qp = &qm->qp_array[i];
+ if (qp->is_in_kernel && qp->alg_type == alg_type && qp->ref_count < ref_count) {
+ ref_count = qp->ref_count;
+ share_qp = qp;
+ }
+ }
+
+ if (share_qp) {
+ share_qp->ref_count++;
+ return share_qp;
+ }
+
+queues_busy:
+ dev_info_ratelimited(dev, "All %u queues of QM are busy and no shareable queue\n",
+ qm->qp_num);
+ atomic64_inc(&qm->debug.dfx.create_qp_err_cnt);
+ return ERR_PTR(-EBUSY);
+}
+
+static struct hisi_qp *qm_create_qp_nolock(struct hisi_qm *qm, u8 alg_type, bool is_in_kernel)
{
struct device *dev = &qm->pdev->dev;
struct hisi_qp *qp;
@@ -2013,12 +2044,9 @@ static struct hisi_qp *qm_create_qp_nolock(struct hisi_qm *qm, u8 alg_type)
return ERR_PTR(-EPERM);
}
- if (qm->qp_in_used == qm->qp_num) {
- dev_info_ratelimited(dev, "All %u queues of QM are busy!\n",
- qm->qp_num);
- atomic64_inc(&qm->debug.dfx.create_qp_err_cnt);
- return ERR_PTR(-EBUSY);
- }
+ /* Try to find a shareable queue when all queues are busy */
+ if (qm->qp_in_used == qm->qp_num)
+ return find_shareable_qp(qm, alg_type, is_in_kernel);
qp_id = idr_alloc_cyclic(&qm->qp_idr, NULL, 0, qm->qp_num, GFP_ATOMIC);
if (qp_id < 0) {
@@ -2034,10 +2062,10 @@ static struct hisi_qp *qm_create_qp_nolock(struct hisi_qm *qm, u8 alg_type)
qp->event_cb = NULL;
qp->req_cb = NULL;
- qp->qp_id = qp_id;
qp->alg_type = alg_type;
- qp->is_in_kernel = true;
+ qp->is_in_kernel = is_in_kernel;
qm->qp_in_used++;
+ qp->ref_count = 1;
return qp;
}
@@ -2059,7 +2087,7 @@ static struct hisi_qp *hisi_qm_create_qp(struct hisi_qm *qm, u8 alg_type)
return ERR_PTR(ret);
down_write(&qm->qps_lock);
- qp = qm_create_qp_nolock(qm, alg_type);
+ qp = qm_create_qp_nolock(qm, alg_type, false);
up_write(&qm->qps_lock);
if (IS_ERR(qp))
@@ -2458,7 +2486,6 @@ static int hisi_qm_uacce_get_queue(struct uacce_device *uacce,
qp->uacce_q = q;
qp->event_cb = qm_qp_event_notifier;
qp->pasid = arg;
- qp->is_in_kernel = false;
return 0;
}
@@ -3532,6 +3559,9 @@ static void qm_release_qp_nolock(struct hisi_qp *qp)
{
struct hisi_qm *qm = qp->qm;
+ if (--qp->ref_count)
+ return;
+
qm->qp_in_used--;
idr_remove(&qm->qp_idr, qp->qp_id);
}
@@ -3551,7 +3581,9 @@ void hisi_qm_free_qps(struct hisi_qp **qps, int qp_num)
down_write(&qps[0]->qm->qps_lock);
for (i = qp_num - 1; i >= 0; i--) {
- qm_stop_qp_nolock(qps[i]);
+ if (qps[i]->ref_count == 1)
+ qm_stop_qp_nolock(qps[i]);
+
qm_release_qp_nolock(qps[i]);
}
@@ -3580,12 +3612,15 @@ static int qm_get_and_start_qp(struct hisi_qm *qm, int qp_num, struct hisi_qp **
down_write(&qm->qps_lock);
for (i = 0; i < qp_num; i++) {
- qps[i] = qm_create_qp_nolock(qm, alg_type[i]);
+ qps[i] = qm_create_qp_nolock(qm, alg_type[i], true);
if (IS_ERR(qps[i])) {
ret = -ENODEV;
goto stop_and_free;
}
+ if (qps[i]->ref_count != 1)
+ continue;
+
ret = qm_start_qp_nolock(qps[i], 0);
if (ret) {
qm_release_qp_nolock(qps[i]);
@@ -3598,7 +3633,9 @@ static int qm_get_and_start_qp(struct hisi_qm *qm, int qp_num, struct hisi_qp **
stop_and_free:
for (i--; i >= 0; i--) {
- qm_stop_qp_nolock(qps[i]);
+ if (qps[i]->ref_count == 1)
+ qm_stop_qp_nolock(qps[i]);
+
qm_release_qp_nolock(qps[i]);
}
up_write(&qm->qps_lock);
diff --git a/include/linux/hisi_acc_qm.h b/include/linux/hisi_acc_qm.h
index 4cf418a41fe4..26032d98e9bd 100644
--- a/include/linux/hisi_acc_qm.h
+++ b/include/linux/hisi_acc_qm.h
@@ -472,6 +472,7 @@ struct hisi_qp {
u16 pasid;
struct uacce_queue *uacce_q;
+ u32 ref_count;
spinlock_t qp_lock;
struct instance_backlog backlog;
const void **msg;
--
2.33.0
next prev parent reply other threads:[~2025-12-18 13:45 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-12-18 13:44 [PATCH v4 00/11] crypto: hisilicon - add fallback function for hisilicon accelerater driver Chenghai Huang
2025-12-18 13:44 ` [PATCH v4 01/11] crypto: hisilicon/zip - adjust the way to obtain the req in the callback function Chenghai Huang
2025-12-18 13:44 ` [PATCH v4 02/11] crypto: hisilicon/sec - move backlog management to qp and store sqe in qp for callback Chenghai Huang
2025-12-18 13:44 ` [PATCH v4 03/11] crypto: hisilicon/hpre: extend tag field to 64 bits for better performance Chenghai Huang
2025-12-18 13:44 ` [PATCH v4 04/11] crypto: hisilicon/qm - enhance the configuration of req_type in queue attributes Chenghai Huang
2025-12-18 13:44 ` [PATCH v4 05/11] crypto: hisilicon/qm - centralize the sending locks of each module into qm Chenghai Huang
2025-12-18 13:44 ` [PATCH v4 06/11] crypto: hisilicon - consolidate qp creation and start in hisi_qm_alloc_qps_node Chenghai Huang
2025-12-18 13:44 ` Chenghai Huang [this message]
2025-12-18 13:44 ` [PATCH v4 08/11] crypto: hisilicon/qm - optimize device selection priority based on queue ref count and NUMA distance Chenghai Huang
2025-12-18 13:44 ` [PATCH v4 09/11] crypto: hisilicon/zip - support fallback for zip Chenghai Huang
2025-12-18 13:44 ` [PATCH v4 10/11] crypto: hisilicon/hpre - support the hpre algorithm fallback Chenghai Huang
2025-12-18 13:44 ` [PATCH v4 11/11] crypto: hisilicon/sec2 - support skcipher/aead fallback for hardware queue unavailable Chenghai Huang
2026-01-16 6:05 ` [PATCH v4 00/11] crypto: hisilicon - add fallback function for hisilicon accelerater driver Herbert Xu
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=20251218134452.1125469-8-huangchenghai2@huawei.com \
--to=huangchenghai2@huawei.com \
--cc=davem@davemloft.net \
--cc=fanghao11@huawei.com \
--cc=herbert@gondor.apana.org.au \
--cc=linux-crypto@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linwenkai6@hisilicon.com \
--cc=liulongfang@huawei.com \
--cc=lizhi206@huawei.com \
--cc=qianweili@huawei.com \
--cc=taoqi10@huawei.com \
--cc=wangzhou1@hisilicon.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®