s390/zcrypt: optimize memory allocation in online_show()
Subsystems (2)
PATCH 1/1 s390/zcrypt: optimize memory allocation in online_show()
From: Yunseong Kim <[email protected]>
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.
--- drivers/s390/crypto/zcrypt_card.c | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-)
drivers/s390/crypto/zcrypt_card.c +7 -9
@@ -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
…
Quoted 1 line
This patch improves efficiency by only allocating memory for the queues
…
* Please improve the change description with imperative wordings.
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?h=v6.10-rc4#n94
* Would you like to add any tags (like “Fixes” or “Cc”) accordingly?
Regards,
Markus
Hi,
kernel test robot noticed the following build warnings:
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/yskelg-gmail-com/s390-zcrypt-optimize-memory-allocation-in-online_show/20240626-071004 base: https://git.kernel.org/pub/scm/linux/kernel/git/s390/linux.git features patch link: https://lore.kernel.org/r/20240623120147.35554-3-yskelg%40gmail.com patch subject: [PATCH] s390/zcrypt: optimize memory allocation in online_show() config: s390-randconfig-r081-20240707 (https://download.01.org/0day-ci/archive/20240707/[email protected]/config) compiler: s390-linux-gcc (GCC) 13.2.0
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]> | Reported-by: Dan Carpenter <[email protected]> | Closes: https://lore.kernel.org/r/[email protected]/
smatch warnings:drivers/s390/crypto/zcrypt_card.c:103 online_store() warn: iterator used outside loop: 'zq'
vim +/zq +103 drivers/s390/crypto/zcrypt_card.c
ac2b96f351d7d2 Harald Freudenberger 2018-08-17 60 static ssize_t online_store(struct device *dev,
e28d2af43614eb Ingo Tuchscherer 2016-08-25 61 struct device_attribute *attr,
e28d2af43614eb Ingo Tuchscherer 2016-08-25 62 const char *buf, size_t count)
e28d2af43614eb Ingo Tuchscherer 2016-08-25 63 {
b5adbbf896d837 Julian Wiedmann 2021-06-07 64 struct zcrypt_card *zc = dev_get_drvdata(dev);
4f2fcccdb547b0 Harald Freudenberger 2020-07-02 65 struct ap_card *ac = to_ap_card(dev);
e28d2af43614eb Ingo Tuchscherer 2016-08-25 66 struct zcrypt_queue *zq;
df6f508c68dbc6 Harald Freudenberger 2021-04-13 67 int online, id, i = 0, maxzqs = 0;
df6f508c68dbc6 Harald Freudenberger 2021-04-13 68 struct zcrypt_queue **zq_uelist = NULL;
e28d2af43614eb Ingo Tuchscherer 2016-08-25 69
e28d2af43614eb Ingo Tuchscherer 2016-08-25 70 if (sscanf(buf, "%d\n", &online) != 1 || online < 0 || online > 1)
e28d2af43614eb Ingo Tuchscherer 2016-08-25 71 return -EINVAL;
e28d2af43614eb Ingo Tuchscherer 2016-08-25 72
cfaef6516e9ac6 Ingo Franzki 2023-10-26 73 if (online && (!ac->config || ac->chkstop))
4f2fcccdb547b0 Harald Freudenberger 2020-07-02 74 return -ENODEV;
4f2fcccdb547b0 Harald Freudenberger 2020-07-02 75
e28d2af43614eb Ingo Tuchscherer 2016-08-25 76 zc->online = online;
e28d2af43614eb Ingo Tuchscherer 2016-08-25 77 id = zc->card->id;
cccd85bfb7bf67 Harald Freudenberger 2016-11-24 78
3f74eb5f78198a Harald Freudenberger 2021-10-15 79 ZCRYPT_DBF_INFO("%s card=%02x online=%d\n", __func__, id, online);
cccd85bfb7bf67 Harald Freudenberger 2016-11-24 80
df6f508c68dbc6 Harald Freudenberger 2021-04-13 81 ap_send_online_uevent(&ac->ap_dev, online);
df6f508c68dbc6 Harald Freudenberger 2021-04-13 82
e28d2af43614eb Ingo Tuchscherer 2016-08-25 83 spin_lock(&zcrypt_list_lock);
df6f508c68dbc6 Harald Freudenberger 2021-04-13 84 /*
df6f508c68dbc6 Harald Freudenberger 2021-04-13 85 * As we are in atomic context here, directly sending uevents
df6f508c68dbc6 Harald Freudenberger 2021-04-13 86 * does not work. So collect the zqueues in a dynamic array
df6f508c68dbc6 Harald Freudenberger 2021-04-13 87 * and process them after zcrypt_list_lock release. As we get/put
df6f508c68dbc6 Harald Freudenberger 2021-04-13 88 * the zqueue objects, we make sure they exist after lock release.
df6f508c68dbc6 Harald Freudenberger 2021-04-13 89 */
df6f508c68dbc6 Harald Freudenberger 2021-04-13 90 list_for_each_entry(zq, &zc->zqueues, list)
2ff6be56a27c2d Yunseong Kim 2024-06-23 91 if (!!zq->online != !!online)
df6f508c68dbc6 Harald Freudenberger 2021-04-13 92 maxzqs++;
df6f508c68dbc6 Harald Freudenberger 2021-04-13 93 if (maxzqs > 0)
2ff6be56a27c2d Yunseong Kim 2024-06-23 94 zq_uelist = kcalloc(maxzqs, sizeof(*zq_uelist), GFP_ATOMIC);
e28d2af43614eb Ingo Tuchscherer 2016-08-25 95 list_for_each_entry(zq, &zc->zqueues, list)
df6f508c68dbc6 Harald Freudenberger 2021-04-13 96 if (zcrypt_queue_force_online(zq, online))
df6f508c68dbc6 Harald Freudenberger 2021-04-13 97 if (zq_uelist) {
df6f508c68dbc6 Harald Freudenberger 2021-04-13 98 zcrypt_queue_get(zq);
df6f508c68dbc6 Harald Freudenberger 2021-04-13 99 zq_uelist[i++] = zq;
df6f508c68dbc6 Harald Freudenberger 2021-04-13 100 }
e28d2af43614eb Ingo Tuchscherer 2016-08-25 101 spin_unlock(&zcrypt_list_lock);
2ff6be56a27c2d Yunseong Kim 2024-06-23 102 while (i--) {
df6f508c68dbc6 Harald Freudenberger 2021-04-13 @103 ap_send_online_uevent(&zq->queue->ap_dev, online);
^^zq is an invalid pointer here. It's an offset off the list head. There used to be a "zq = zq_uelist[i];" before this function call but the patch deleted it.
2ff6be56a27c2d Yunseong Kim 2024-06-23 104 zcrypt_queue_put(zq_uelist[i]); df6f508c68dbc6 Harald Freudenberger 2021-04-13 105 } df6f508c68dbc6 Harald Freudenberger 2021-04-13 106 kfree(zq_uelist); df6f508c68dbc6 Harald Freudenberger 2021-04-13 107 e28d2af43614eb Ingo Tuchscherer 2016-08-25 108 return count; e28d2af43614eb Ingo Tuchscherer 2016-08-25 109 }
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
Hi Dan,
Quoted 15 lines · Dan Carpenter
Hi,
kernel test robot noticed the following build warnings:
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/yskelg-gmail-com/s390-zcrypt-optimize-memory-allocation-in-online_show/20240626-071004 base: https://git.kernel.org/pub/scm/linux/kernel/git/s390/linux.git features patch link: https://lore.kernel.org/r/20240623120147.35554-3-yskelg%40gmail.com patch subject: [PATCH] s390/zcrypt: optimize memory allocation in online_show() config: s390-randconfig-r081-20240707 (https://download.01.org/0day-ci/archive/20240707/[email protected]/config) compiler: s390-linux-gcc (GCC) 13.2.0If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
Quoted 8 lines
| Reported-by: kernel test robot <[email protected]> | Reported-by: Dan Carpenter <[email protected]> | Closes: https://lore.kernel.org/r/[email protected]/smatch warnings:
drivers/s390/crypto/zcrypt_card.c:103online_store()warn: iterator used outside loop: 'zq'vim +/zq +103
drivers/s390/crypto/zcrypt_card.c
Thank you for the review. I decided not to work on my patch anymore after the review because my patch was problematic and not useful.
Quoted 15 lines
df6f508c68dbc6 Harald Freudenberger 2021-04-13 90 list_for_each_entry(zq, &zc->zqueues, list)
2ff6be56a27c2d Yunseong Kim 2024-06-23 91 if (!!zq->online != !!online)
df6f508c68dbc6 Harald Freudenberger 2021-04-13 92 maxzqs++;
df6f508c68dbc6 Harald Freudenberger 2021-04-13 93 if (maxzqs > 0)
2ff6be56a27c2d Yunseong Kim 2024-06-23 94 zq_uelist = kcalloc(maxzqs, sizeof(*zq_uelist), GFP_ATOMIC);
e28d2af43614eb Ingo Tuchscherer 2016-08-25 95 list_for_each_entry(zq, &zc->zqueues, list)
df6f508c68dbc6 Harald Freudenberger 2021-04-13 96 if (zcrypt_queue_force_online(zq, online))
df6f508c68dbc6 Harald Freudenberger 2021-04-13 97 if (zq_uelist) {
df6f508c68dbc6 Harald Freudenberger 2021-04-13 98 zcrypt_queue_get(zq);
df6f508c68dbc6 Harald Freudenberger 2021-04-13 99 zq_uelist[i++] = zq;
df6f508c68dbc6 Harald Freudenberger 2021-04-13 100 }
e28d2af43614eb Ingo Tuchscherer 2016-08-25 101 spin_unlock(&zcrypt_list_lock);
2ff6be56a27c2d Yunseong Kim 2024-06-23 102 while (i--) {
df6f508c68dbc6 Harald Freudenberger 2021-04-13 @103 ap_send_online_uevent(&zq->queue->ap_dev, online);
^^Quoted 3 lines
zq is an invalid pointer here. It's an offset off the list head. There used to be a "zq = zq_uelist[i];" before this function call but the patch deleted it.
My patch doesn't mean much to me at this point, but if it needed to be fixed, I think it should have been.
Quoted 6 lines
2ff6be56a27c2d Yunseong Kim 2024-06-23 104 zcrypt_queue_put(zq_uelist[i]); df6f508c68dbc6 Harald Freudenberger 2021-04-13 105 } df6f508c68dbc6 Harald Freudenberger 2021-04-13 106 kfree(zq_uelist); df6f508c68dbc6 Harald Freudenberger 2021-04-13 107 e28d2af43614eb Ingo Tuchscherer 2016-08-25 108 return count; e28d2af43614eb Ingo Tuchscherer 2016-08-25 109 }
I've been doing some Linux kernel research on the IBM Cloud s390x VPC and running syzkaller and Linux kernel tests tools to send out useful patches. :)
Warm regards,
Yunseong kim