mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
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>
Subject: [PATCH 1/2] crypto: hisilicon/trng - use DEFINE_MUTEX() and LIST_HEAD()
Date: Thu, 20 Nov 2025 21:58:11 +0800	[thread overview]
Message-ID: <20251120135812.1814923-2-huangchenghai2@huawei.com> (raw)
In-Reply-To: <20251120135812.1814923-1-huangchenghai2@huawei.com>

From: Weili Qian <qianweili@huawei.com>

Runtime initialization of lock and list head is not
reliable in a concurrent environment. Therefore, use
DEFINE_MUTEX() and LIST_HEAD() for automatic initialization.
And decide whether to register the algorithm to the crypto
subsystem based on whether the list is empty, instead of
using atomic operations.

Fixes: e4d9d10ef4be ("crypto: hisilicon/trng - add support for PRNG")
Signed-off-by: Weili Qian <qianweili@huawei.com>
Signed-off-by: Chenghai Huang <huangchenghai2@huawei.com>
---
 drivers/crypto/hisilicon/trng/trng.c | 104 +++++++++++++--------------
 1 file changed, 48 insertions(+), 56 deletions(-)

diff --git a/drivers/crypto/hisilicon/trng/trng.c b/drivers/crypto/hisilicon/trng/trng.c
index ac74df4a9471..dae81b6f43e3 100644
--- a/drivers/crypto/hisilicon/trng/trng.c
+++ b/drivers/crypto/hisilicon/trng/trng.c
@@ -40,16 +40,10 @@
 #define SEED_SHIFT_24		24
 #define SEED_SHIFT_16		16
 #define SEED_SHIFT_8		8
-
-struct hisi_trng_list {
-	struct mutex lock;
-	struct list_head list;
-	bool is_init;
-};
+#define WAIT_PERIOD		20
 
 struct hisi_trng {
 	void __iomem *base;
-	struct hisi_trng_list *trng_list;
 	struct list_head list;
 	struct hwrng rng;
 	u32 ver;
@@ -61,8 +55,8 @@ struct hisi_trng_ctx {
 	struct hisi_trng *trng;
 };
 
-static atomic_t trng_active_devs;
-static struct hisi_trng_list trng_devices;
+static LIST_HEAD(trng_devices_list);
+static DEFINE_MUTEX(trng_device_lock);
 
 static void hisi_trng_set_seed(struct hisi_trng *trng, const u8 *seed)
 {
@@ -157,8 +151,8 @@ static int hisi_trng_init(struct crypto_tfm *tfm)
 	struct hisi_trng *trng;
 	int ret = -EBUSY;
 
-	mutex_lock(&trng_devices.lock);
-	list_for_each_entry(trng, &trng_devices.list, list) {
+	mutex_lock(&trng_device_lock);
+	list_for_each_entry(trng, &trng_devices_list, list) {
 		if (!trng->is_used) {
 			trng->is_used = true;
 			ctx->trng = trng;
@@ -166,7 +160,7 @@ static int hisi_trng_init(struct crypto_tfm *tfm)
 			break;
 		}
 	}
-	mutex_unlock(&trng_devices.lock);
+	mutex_unlock(&trng_device_lock);
 
 	return ret;
 }
@@ -175,9 +169,9 @@ static void hisi_trng_exit(struct crypto_tfm *tfm)
 {
 	struct hisi_trng_ctx *ctx = crypto_tfm_ctx(tfm);
 
-	mutex_lock(&trng_devices.lock);
+	mutex_lock(&trng_device_lock);
 	ctx->trng->is_used = false;
-	mutex_unlock(&trng_devices.lock);
+	mutex_unlock(&trng_device_lock);
 }
 
 static int hisi_trng_read(struct hwrng *rng, void *buf, size_t max, bool wait)
@@ -226,24 +220,43 @@ static struct rng_alg hisi_trng_alg = {
 	},
 };
 
-static void hisi_trng_add_to_list(struct hisi_trng *trng)
+static int hisi_trng_crypto_register(struct hisi_trng *trng)
 {
-	mutex_lock(&trng_devices.lock);
-	list_add_tail(&trng->list, &trng_devices.list);
-	mutex_unlock(&trng_devices.lock);
+	int ret = 0;
+
+	mutex_lock(&trng_device_lock);
+	if (trng->ver != HISI_TRNG_VER_V1 &&
+	    list_empty(&trng_devices_list)) {
+		ret = crypto_register_rng(&hisi_trng_alg);
+		if (ret) {
+			pr_err("failed to register crypto(%d)\n", ret);
+			goto unlock;
+		}
+	}
+
+	list_add_tail(&trng->list, &trng_devices_list);
+unlock:
+	mutex_unlock(&trng_device_lock);
+	return ret;
 }
 
-static int hisi_trng_del_from_list(struct hisi_trng *trng)
+static int hisi_trng_crypto_unregister(struct hisi_trng *trng)
 {
 	int ret = -EBUSY;
 
-	mutex_lock(&trng_devices.lock);
-	if (!trng->is_used) {
-		list_del(&trng->list);
-		ret = 0;
-	}
-	mutex_unlock(&trng_devices.lock);
+	mutex_lock(&trng_device_lock);
+	if (trng->is_used)
+		goto unlock;
+
+	list_del(&trng->list);
+	if (trng->ver != HISI_TRNG_VER_V1 &&
+	    list_empty(&trng_devices_list))
+		crypto_unregister_rng(&hisi_trng_alg);
 
+	ret = 0;
+
+unlock:
+	mutex_unlock(&trng_device_lock);
 	return ret;
 }
 
@@ -264,23 +277,9 @@ static int hisi_trng_probe(struct platform_device *pdev)
 
 	trng->is_used = false;
 	trng->ver = readl(trng->base + HISI_TRNG_VERSION);
-	if (!trng_devices.is_init) {
-		INIT_LIST_HEAD(&trng_devices.list);
-		mutex_init(&trng_devices.lock);
-		trng_devices.is_init = true;
-	}
-
-	hisi_trng_add_to_list(trng);
-	if (trng->ver != HISI_TRNG_VER_V1 &&
-	    atomic_inc_return(&trng_active_devs) == 1) {
-		ret = crypto_register_rng(&hisi_trng_alg);
-		if (ret) {
-			dev_err(&pdev->dev,
-				"failed to register crypto(%d)\n", ret);
-			atomic_dec_return(&trng_active_devs);
-			goto err_remove_from_list;
-		}
-	}
+	ret = hisi_trng_crypto_register(trng);
+	if (ret)
+		return ret;
 
 	trng->rng.name = pdev->name;
 	trng->rng.read = hisi_trng_read;
@@ -288,18 +287,13 @@ static int hisi_trng_probe(struct platform_device *pdev)
 	ret = devm_hwrng_register(&pdev->dev, &trng->rng);
 	if (ret) {
 		dev_err(&pdev->dev, "failed to register hwrng: %d!\n", ret);
-		goto err_crypto_unregister;
+		goto unregister_crypto;
 	}
 
 	return ret;
 
-err_crypto_unregister:
-	if (trng->ver != HISI_TRNG_VER_V1 &&
-	    atomic_dec_return(&trng_active_devs) == 0)
-		crypto_unregister_rng(&hisi_trng_alg);
-
-err_remove_from_list:
-	hisi_trng_del_from_list(trng);
+unregister_crypto:
+	hisi_trng_crypto_unregister(trng);
 	return ret;
 }
 
@@ -308,12 +302,10 @@ static void hisi_trng_remove(struct platform_device *pdev)
 	struct hisi_trng *trng = platform_get_drvdata(pdev);
 
 	/* Wait until the task is finished */
-	while (hisi_trng_del_from_list(trng))
-		;
-
-	if (trng->ver != HISI_TRNG_VER_V1 &&
-	    atomic_dec_return(&trng_active_devs) == 0)
-		crypto_unregister_rng(&hisi_trng_alg);
+	while (hisi_trng_crypto_unregister(trng)) {
+		dev_info(&pdev->dev, "trng is in using!\n");
+		msleep(WAIT_PERIOD);
+	}
 }
 
 static const struct acpi_device_id hisi_trng_acpi_match[] = {
-- 
2.33.0


  reply	other threads:[~2025-11-20 13:58 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-20 13:58 [PATCH 0/2] crypto: hisilicon/trng - support multiple tfms sharing the device Chenghai Huang
2025-11-20 13:58 ` Chenghai Huang [this message]
2025-12-19  3:04   ` [PATCH 1/2] crypto: hisilicon/trng - use DEFINE_MUTEX() and LIST_HEAD() Herbert Xu
2025-12-22  6:10     ` huangchenghai
2025-12-24  2:49       ` Herbert Xu
2025-11-20 13:58 ` [PATCH 2/2] crypto: hisilicon/trng - support tfms sharing the device Chenghai Huang
2025-12-19  3:06   ` Herbert Xu
2025-12-22  3:36     ` huangchenghai

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=20251120135812.1814923-2-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=qianweili@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®