public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] s390/zcrypt: optimize memory allocation in online_show()
@ 2024-06-23 12:01 yskelg
  2024-06-23 16:01 ` Markus Elfring
  2024-07-08 15:55 ` Dan Carpenter
  0 siblings, 2 replies; 4+ messages in thread
From: yskelg @ 2024-06-23 12:01 UTC (permalink / raw)
  To: Harald Freudenberger, Heiko Carstens, Vasily Gorbik,
	Alexander Gordeev, Christian Borntraeger, Sven Schnelle
  Cc: shjy180909, linux-s390, linux-kernel, Yunseong Kim

From: Yunseong Kim <yskelg@gmail.com>

Previously, when setting the online state for a card, the code would
allocate memory to store information about all attached queues,
regardless of whether their online state actually changed.
This patch improves efficiency by only allocating memory for the queues
that are truly affected by the online state change.

This allows for a more precise memory allocation (based on maxzqs) which
can reduce memory usage.

Signed-off-by: Yunseong Kim <yskelg@gmail.com>
---
 drivers/s390/crypto/zcrypt_card.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)

diff --git a/drivers/s390/crypto/zcrypt_card.c b/drivers/s390/crypto/zcrypt_card.c
index 050462d95222..2c80be3f2a00 100644
--- a/drivers/s390/crypto/zcrypt_card.c
+++ b/drivers/s390/crypto/zcrypt_card.c
@@ -88,9 +88,10 @@ static ssize_t online_store(struct device *dev,
 	 * the zqueue objects, we make sure they exist after lock release.
 	 */
 	list_for_each_entry(zq, &zc->zqueues, list)
-		maxzqs++;
+		if (!!zq->online != !!online)
+			maxzqs++;
 	if (maxzqs > 0)
-		zq_uelist = kcalloc(maxzqs + 1, sizeof(*zq_uelist), GFP_ATOMIC);
+		zq_uelist = kcalloc(maxzqs, sizeof(*zq_uelist), GFP_ATOMIC);
 	list_for_each_entry(zq, &zc->zqueues, list)
 		if (zcrypt_queue_force_online(zq, online))
 			if (zq_uelist) {
@@ -98,14 +99,11 @@ static ssize_t online_store(struct device *dev,
 				zq_uelist[i++] = zq;
 			}
 	spin_unlock(&zcrypt_list_lock);
-	if (zq_uelist) {
-		for (i = 0; zq_uelist[i]; i++) {
-			zq = zq_uelist[i];
-			ap_send_online_uevent(&zq->queue->ap_dev, online);
-			zcrypt_queue_put(zq);
-		}
-		kfree(zq_uelist);
+	while (i--) {
+		ap_send_online_uevent(&zq->queue->ap_dev, online);
+		zcrypt_queue_put(zq_uelist[i]);
 	}
+	kfree(zq_uelist);
 
 	return count;
 }
-- 
2.45.2


^ permalink raw reply related	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-07-08 22:51 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-23 12:01 [PATCH] s390/zcrypt: optimize memory allocation in online_show() yskelg
2024-06-23 16:01 ` Markus Elfring
2024-07-08 15:55 ` Dan Carpenter
2024-07-08 22:51   ` Yunseong Kim

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox