Skip to contentSkip to conversation

s390/zcrypt: optimizes memory allocation in online_show()

PATCH Superseded [email protected] 2 participants 3 messages Jun 24, 2024 – Jun 26, 2024

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

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

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
#1 lore raw
Quoted 10 lines · On 2024-06-25 00:29, [email protected] wrote:

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")
Link:
https://lore.kernel.org/linux-s390/[email protected]/

What is this Link here? It is pointing to a PR for a 5.14 kernel and has no relation to this patch.

Quoted 12 lines
---
 drivers/s390/crypto/zcrypt_card.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
drivers/s390/crypto/zcrypt_card.c +0 -0
index 050462d95222..2c80be3f2a00 100644

drivers/s390/crypto/zcrypt_card.c static ssize_t online_store(struct device *dev, · line 91

Quoted 5 lines
drivers/s390/crypto/zcrypt_card.c +1 -1
@@ -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)

I don't like this line. It is code duplication from the zcrypt_queue.c file and uses knowledge about the internals of the zqueue which is not appropriate here in zcrypt_card.c. Please note also that usually the total number of queues attached to a card is in a one digit range. As kcalloc() anyway uses the kmalloc pool which is ordered in powers of two it is unlikely to really spare some memory by only allocating a pointer space for the online queues.

drivers/s390/crypto/zcrypt_card.c static ssize_t online_store(struct device *dev, · line 94

Quoted 4 lines
drivers/s390/crypto/zcrypt_card.c +2 -1
+			maxzqs++;
 	if (maxzqs > 0)
-		zq_uelist = kcalloc(maxzqs + 1, sizeof(*zq_uelist), GFP_ATOMIC);
+		zq_uelist = kcalloc(maxzqs, sizeof(*zq_uelist), GFP_ATOMIC);

Your improvement about removal of the +1 and use the i value later instead of my implementation which uses a NULL as end of list is valid and makes sense to me.

Quoted 7 lines
drivers/s390/crypto/zcrypt_card.c +0 -0
 	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);

drivers/s390/crypto/zcrypt_card.c static ssize_t online_store(struct device *dev, · line 104

Quoted 10 lines
drivers/s390/crypto/zcrypt_card.c +3 -7
-	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]);

The content of this while loop is NOT covering the old code. zq is not set any more and thus the ap_sen_online_uevent() uses a random zq which is a left over from the list_for_each() loop.

drivers/s390/crypto/zcrypt_card.c static ssize_t online_store(struct device *dev, · line 109

Quoted 5 lines
drivers/s390/crypto/zcrypt_card.c +1 -0
 	}
+	kfree(zq_uelist);
 
 	return count;
 }

You sent another patch for the online_store() function with exactly the same code changes. I would see these changes as one patch and don't want to have more or less equal changes spread over two patches.

I am sorry, I will not pick this and the online_store() patch.

regards Harald Freudenberger

#2 lore raw

Hi Harald,

Quoted 33 lines · Harald Freudenberger
Quoted 10 lines · On 2024-06-25 00:29, [email protected] wrote:

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")
Link:
https://lore.kernel.org/linux-s390/[email protected]/

What is this Link here? It is pointing to a PR for a 5.14 kernel and has no relation to this patch.

Quoted 12 lines

---
 drivers/s390/crypto/zcrypt_card.c | 16 +++++++---------

 1 file changed, 7 insertions(+), 9 deletions(-)
drivers/s390/crypto/zcrypt_card.c +0 -0
index 050462d95222..2c80be3f2a00 100644

drivers/s390/crypto/zcrypt_card.c static ssize_t online_store(struct device *dev, · line 91

Quoted 5 lines
drivers/s390/crypto/zcrypt_card.c +1 -1
@@ -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)
Quoted 7 lines

I don't like this line. It is code duplication from the zcrypt_queue.c file and uses knowledge about the internals of the zqueue which is not appropriate
here in zcrypt_card.c. Please note also that usually the total number of queues attached to a card is in a one digit range. As kcalloc() anyway uses the kmalloc pool which is ordered in powers of two it is unlikely to really spare some memory by only allocating a pointer space for the online queues.

Thank you Harald for the code review! Oh I see, thanks for the advice. I was wondering if it was useful when I was coding it too.

Quoted 23 lines

drivers/s390/crypto/zcrypt_card.c static ssize_t online_store(struct device *dev, · line 94

Quoted 4 lines
drivers/s390/crypto/zcrypt_card.c +2 -1
+            maxzqs++;
      if (maxzqs > 0)
-        zq_uelist = kcalloc(maxzqs + 1, sizeof(*zq_uelist), GFP_ATOMIC);
+        zq_uelist = kcalloc(maxzqs, sizeof(*zq_uelist), GFP_ATOMIC);

Your improvement about removal of the +1 and use the i value later instead of my implementation which uses a NULL as end of list is valid and makes sense
to me.

Quoted 13 lines
drivers/s390/crypto/zcrypt_card.c +0 -6
      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);
-        }
Quoted 8 lines

drivers/s390/crypto/zcrypt_card.c static ssize_t online_store(struct device *dev, · line 104

Quoted 4 lines
drivers/s390/crypto/zcrypt_card.c +3 -1
-        kfree(zq_uelist);
+    while (i--) {
+        ap_send_online_uevent(&zq->queue->ap_dev, online);
+        zcrypt_queue_put(zq_uelist[i]);

The content of this while loop is NOT covering the old code. zq is not set any more and thus the ap_sen_online_uevent() uses a random zq which is a left over from the list_for_each() loop.

Oh this is where I wrote the code without understanding it properly, thanks for the guidance!

Quoted 11 lines

drivers/s390/crypto/zcrypt_card.c static ssize_t online_store(struct device *dev, · line 109

Quoted 5 lines
drivers/s390/crypto/zcrypt_card.c +1 -0
      }
+    kfree(zq_uelist);
 
      return count;
  }

You sent another patch for the online_store() function with exactly the same code changes. I would see these changes as one patch and don't want to have more or less equal changes spread over two patches.

I am sorry, I will not pick this and the online_store() patch.

I'm so sorry Harald, This was missing judgment, I should have checked it one last time before sending v2 patch mail.

Quoted 1 line

regards Harald Freudenberger

I truly appreciate Harald for the detailed code review of my patch., even though it may be less understanding in many part.

Thank you very much again!

Warm regards,

Yunseong Kim