scsi: mpt3sas: Replace unnecessary dynamic allocation with a static one
Subsystems (2)
PATCH 1/1 scsi: mpt3sas: Replace unnecessary dynamic allocation with a static one
Dynamic memory allocation isn't actually needed and it can be replaced by statically allocating memory for struct object io_unit_pg3 with 36 hardcoded entries for its GPIOVal array.
Also, this helps the with ongoing efforts to enable -Warray-bounds by fixing the following warning:
drivers/scsi/mpt3sas/mpt3sas_ctl.c: In function ‘BRM_status_show’:drivers/scsi/mpt3sas/mpt3sas_ctl.c:3257:63: warning: array subscript 24 is above array bounds of ‘U16[1]’ {aka ‘short unsigned int[1]’} [-Warray-bounds]
3257 | backup_rail_monitor_status = le16_to_cpu(io_unit_pg3->GPIOVal[24]);./include/uapi/linux/byteorder/little_endian.h:36:51: note: in definition of macro ‘__le16_to_cpu’
36 | #define __le16_to_cpu(x) ((__force __u16)(__le16)(x))
| ^drivers/scsi/mpt3sas/mpt3sas_ctl.c:3257:31: note: in expansion of macro ‘le16_to_cpu’
3257 | backup_rail_monitor_status = le16_to_cpu(io_unit_pg3->GPIOVal[24]);
--- drivers/scsi/mpt3sas/mpi/mpi2_cnfg.h | 2 +- drivers/scsi/mpt3sas/mpt3sas_ctl.c | 24 ++++++++---------------- 2 files changed, 9 insertions(+), 17 deletions(-)
drivers/scsi/mpt3sas/mpi/mpi2_cnfg.h +1 -1
@@ -992,7 +992,7 @@ typedef struct _MPI2_CONFIG_PAGE_IO_UNIT_1 {*one and check the value returned for GPIOCount at runtime. */ #ifndef MPI2_IO_UNIT_PAGE_3_GPIO_VAL_MAX -#define MPI2_IO_UNIT_PAGE_3_GPIO_VAL_MAX (1) +#define MPI2_IO_UNIT_PAGE_3_GPIO_VAL_MAX (36)#endif typedef struct _MPI2_CONFIG_PAGE_IO_UNIT_3 {
drivers/scsi/mpt3sas/mpt3sas_ctl.c +8 -16
@@ -3203,7 +3203,7 @@ BRM_status_show(struct device *cdev, struct device_attribute *attr, {struct Scsi_Host *shost =class_to_shost (cdev);struct MPT3SAS_ADAPTER *ioc =shost_priv (shost); -Mpi2IOUnitPage3_t *io_unit_pg3 =NULL ; +Mpi2IOUnitPage3_t io_unit_pg3;Mpi2ConfigReply_t mpi_reply;u16 backup_rail_monitor_status =0 ;u16 ioc_status;
@@ -3220,17 +3220,10 @@ BRM_status_show(struct device *cdev, struct device_attribute *attr,if (ioc->pci_error_recovery || ioc->remove_host) goto out; -/* allocate upto GPIOVal 36 entries */ - sz = offsetof(Mpi2IOUnitPage3_t , GPIOVal) + (sizeof (u16) *36 ); - io_unit_pg3 =kzalloc (sz,GFP_KERNEL ); -if (!io_unit_pg3) { - rc = -ENOMEM ; -ioc_err (ioc,"%s: failed allocating memory for iounit_pg3: (%d) bytes\n" , - __func__, sz); - goto out; - } + sz =sizeof (io_unit_pg3); +memset (&io_unit_pg3,0 , sz); -if (mpt3sas_config_get_iounit_pg3 (ioc, &mpi_reply, io_unit_pg3, sz) != +if (mpt3sas_config_get_iounit_pg3 (ioc, &mpi_reply, &io_unit_pg3, sz) !=0 ) {ioc_err (ioc,"%s: failed reading iounit_pg3\n" , __func__);
@@ -3246,19 +3239,18 @@ BRM_status_show(struct device *cdev, struct device_attribute *attr, goto out; } -if (io_unit_pg3->GPIOCount <25 ) { -ioc_err (ioc,"%s: iounit_pg3->GPIOCount less than 25 entries, detected (%d) entries\n" , - __func__, io_unit_pg3->GPIOCount); +if (io_unit_pg3.GPIOCount <25 ) { +ioc_err (ioc,"%s: iounit_pg3.GPIOCount less than 25 entries, detected (%d) entries\n" , + __func__, io_unit_pg3.GPIOCount); rc = -EINVAL ; goto out; }/* BRM status is in bit zero of GPIOVal[24] */ - backup_rail_monitor_status =le16_to_cpu (io_unit_pg3->GPIOVal[24 ]); + backup_rail_monitor_status =le16_to_cpu (io_unit_pg3.GPIOVal[24 ]); rc =snprintf (buf,PAGE_SIZE ,"%d\n" , (backup_rail_monitor_status &1 )); out: -kfree (io_unit_pg3);mutex_unlock (&ioc->pci_access_mutex);return rc; }
Signature · 1 line
2.27.0
Martin K. Petersen
AppliedGustavo,
Quoted 3 lines
Dynamic memory allocation isn't actually needed and it can be replaced by statically allocating memory for struct object
io_unit_pg3with 36 hardcoded entries for its GPIOVal array.
Applied to 5.13/scsi-staging, thanks!
Signature · 1 line
Martin K. Petersen Oracle Linux Engineering
Hi Martin,
Quoted 8 lines · Martin K. Petersen
Gustavo,
Quoted 3 lines
Dynamic memory allocation isn't actually needed and it can be replaced by statically allocating memory for struct object
io_unit_pg3with 36 hardcoded entries for its GPIOVal array.Applied to 5.13/scsi-staging, thanks!
Awesome. :)
I wonder if you can take a look at this one, too, please:
https://lore.kernel.org/lkml/20210304203822.GA102218@embeddedor/
Thanks!
--
Gustavo
Martin K. Petersen
AppliedQuoted 8 lines · Gustavo A. R. Silva
Dynamic memory allocation isn't actually needed and it can be replaced by statically allocating memory for struct object
io_unit_pg3with 36 hardcoded entries for its GPIOVal array.Also, this helps the with ongoing efforts to enable -Warray-bounds by fixing the following warning:
[...]
Applied to 5.13/scsi-queue, thanks!
[1/1] scsi: mpt3sas: Replace unnecessary dynamic allocation with a static one
https://git.kernel.org/mkp/scsi/c/a1c4d7741323
Signature · 1 line
Martin K. Petersen Oracle Linux Engineering