Skip to contentSkip to conversation

scsi: mpi3mr: reduce stack usage in mpi3mr_refresh_sas_ports()

PATCH Superseded Arnd Bergmann 3 participants 3 messages Nov 29, 2023 – Dec 4, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 scsi: mpi3mr: reduce stack usage in mpi3mr_refresh_sas_ports()

From: Arnd Bergmann <[email protected]>

Toubling the number of PHYs also doubled the stack usage of this function, exceeding the 32-bit limit of 1024 bytes:

drivers/scsi/mpi3mr/mpi3mr_transport.c: In function 'mpi3mr_refresh_sas_ports':
drivers/scsi/mpi3mr/mpi3mr_transport.c:1818:1: error: the frame size of 1636 bytes is larger than 1024 bytes [-Werror=frame-larger-than=]

Since the sas_io_unit_pg0 structure is already allocated dynamically, use the same method here. The size of the allocation can be smaller based on the actual number of phys now, so use this as an upper bound.

Fixes cb5b60894602 ("scsi: mpi3mr: Increase maximum number of PHYs to 64 from 32")
---
 drivers/scsi/mpi3mr/mpi3mr_transport.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/scsi/mpi3mr/mpi3mr_transport.c +7 -1
index c0c8ab586957..ab04596dbdf5 100644
@@ -1671,7 +1671,7 @@ mpi3mr_update_mr_sas_port(struct mpi3mr_ioc *mrioc, struct host_port *h_port,
 void
 mpi3mr_refresh_sas_ports(struct mpi3mr_ioc *mrioc)
 {
-	struct host_port h_port[64];
+	struct host_port *h_port = NULL;
 	int i, j, found, host_port_count = 0, port_idx;
 	u16 sz, attached_handle, ioc_status;
 	struct mpi3_sas_io_unit_page0 *sas_io_unit_pg0 = NULL;
@@ -1685,6 +1685,11 @@ mpi3mr_refresh_sas_ports(struct mpi3mr_ioc *mrioc)
 	sas_io_unit_pg0 = kzalloc(sz, GFP_KERNEL);
 	if (!sas_io_unit_pg0)
 		return;
+	h_port = kcalloc(mrioc->sas_hba.num_phys, sizeof(struct host_port),
+			 GFP_KERNEL);
+	if (!h_port)
+		goto out;
+
 	if (mpi3mr_cfg_get_sas_io_unit_pg0(mrioc, sas_io_unit_pg0, sz)) {
 		ioc_err(mrioc, "failure at %s:%d/%s()!\n",
 		    __FILE__, __LINE__, __func__);
@@ -1814,6 +1819,7 @@ mpi3mr_refresh_sas_ports(struct mpi3mr_ioc *mrioc)
 		}
 	}
 out:
+	kfree(h_port);
 	kfree(sas_io_unit_pg0);
 }
 
Signature · 1 line
2.39.2
#2 lore raw
In reply to Arnd Bergmann
Quoted 56 lines · Arnd Bergmann

From: Arnd Bergmann <[email protected]>

Toubling the number of PHYs also doubled the stack usage of this function, exceeding the 32-bit limit of 1024 bytes:

drivers/scsi/mpi3mr/mpi3mr_transport.c: In function 'mpi3mr_refresh_sas_ports':
drivers/scsi/mpi3mr/mpi3mr_transport.c:1818:1: error: the frame size of 1636 bytes is larger than 1024 bytes [-Werror=frame-larger-than=]

Since the sas_io_unit_pg0 structure is already allocated dynamically, use the same method here. The size of the allocation can be smaller based on the actual number of phys now, so use this as an upper bound.

Fixes cb5b60894602 ("scsi: mpi3mr: Increase maximum number of PHYs to 64 from 32")
---
 drivers/scsi/mpi3mr/mpi3mr_transport.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
drivers/scsi/mpi3mr/mpi3mr_transport.c +3 -1
index c0c8ab586957..ab04596dbdf5 100644
@@ -1671,7 +1671,7 @@ mpi3mr_update_mr_sas_port(struct mpi3mr_ioc *mrioc, struct host_port *h_port,
 void
 mpi3mr_refresh_sas_ports(struct mpi3mr_ioc *mrioc)
 {
-       struct host_port h_port[64];
+       struct host_port *h_port = NULL;
        int i, j, found, host_port_count = 0, port_idx;
        u16 sz, attached_handle, ioc_status;
        struct mpi3_sas_io_unit_page0 *sas_io_unit_pg0 = NULL;
@@ -1685,6 +1685,11 @@ mpi3mr_refresh_sas_ports(struct mpi3mr_ioc *mrioc)
        sas_io_unit_pg0 = kzalloc(sz, GFP_KERNEL);
        if (!sas_io_unit_pg0)
                return;
+       h_port = kcalloc(mrioc->sas_hba.num_phys, sizeof(struct host_port),
+                        GFP_KERNEL);
Quoted 1 line

We need more than sas_hba.num_phys, so can you please use 64 instead of sas_hba.num_phys. Otherwise looks good.

drivers/scsi/mpi3mr/mpi3mr_transport.c +4 -1
+       if (!h_port)
+               goto out;
+
        if (mpi3mr_cfg_get_sas_io_unit_pg0(mrioc, sas_io_unit_pg0, sz)) {
                ioc_err(mrioc, "failure at %s:%d/%s()!\n",
                    __FILE__, __LINE__, __func__);
@@ -1814,6 +1819,7 @@ mpi3mr_refresh_sas_ports(struct mpi3mr_ioc *mrioc)
                }
        }
 out:
+       kfree(h_port);
        kfree(sas_io_unit_pg0);
 }
 
--

2.39.2