Skip to contentSkip to conversation

s390/zcrypt: optimizes memory allocation in online_store()

PATCH RESEND New [email protected] 1 participant 1 message Jun 24, 2024

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH RESEND v2 1/1 s390/zcrypt: optimizes memory allocation in online_store()

From: Yunseong Kim <[email protected]>

Make memory allocation more precise (based on maxzqs) by allocating memory only for the queues that are truly affected by the online state changes.

Fixes df6f508c68db ("s390/ap/zcrypt: notify userspace with online, config and mode info")
---
 drivers/s390/crypto/zcrypt_card.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
drivers/s390/crypto/zcrypt_card.c +7 -9
index 050462d95222..2c80be3f2a00 100644
@@ -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;
 }
Signature · 1 line
2.45.2