2017-07-19 22:06:39

by Gustavo A. R. Silva

[permalink] [raw]
Subject: [PATCH] scsi: mpt3sas_scsih: remove unnecessary statics

Remove unnecessary static on local variables raid_device.
Such variables are initialized before being used, on
every execution path throughout the functions. The
static has no benefit and, removing it reduces the
object file size.

This issue was detected using Coccinelle and the following semantic patch:

@bad exists@
position p;
identifier x;
type T;
@@

static T x@p;
...
x = <+...x...+>

@@
identifier x;
expression e;
type T;
position p != bad.p;
@@

-static
T x@p;
... when != x
when strict
?x = e;

In the following log you can see a significant difference in the object
file size. This log is the output of the size command, before and after
the code change:

before:
text data bss dec hex filename
126304 30384 1280 157968 26910 drivers/scsi/mpt3sas/mpt3sas_scsih.o

after:
text data bss dec hex filename
126292 30240 1152 157684 267f4 drivers/scsi/mpt3sas/mpt3sas_scsih.o

Signed-off-by: Gustavo A. R. Silva <[email protected]>
---
drivers/scsi/mpt3sas/mpt3sas_scsih.c | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/drivers/scsi/mpt3sas/mpt3sas_scsih.c b/drivers/scsi/mpt3sas/mpt3sas_scsih.c
index 22998cb..417e5d1 100644
--- a/drivers/scsi/mpt3sas/mpt3sas_scsih.c
+++ b/drivers/scsi/mpt3sas/mpt3sas_scsih.c
@@ -1571,7 +1571,7 @@ scsih_get_resync(struct device *dev)
{
struct scsi_device *sdev = to_scsi_device(dev);
struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
- static struct _raid_device *raid_device;
+ struct _raid_device *raid_device;
unsigned long flags;
Mpi2RaidVolPage0_t vol_pg0;
Mpi2ConfigReply_t mpi_reply;
@@ -1632,7 +1632,7 @@ scsih_get_state(struct device *dev)
{
struct scsi_device *sdev = to_scsi_device(dev);
struct MPT3SAS_ADAPTER *ioc = shost_priv(sdev->host);
- static struct _raid_device *raid_device;
+ struct _raid_device *raid_device;
unsigned long flags;
Mpi2RaidVolPage0_t vol_pg0;
Mpi2ConfigReply_t mpi_reply;
@@ -7027,7 +7027,7 @@ _scsih_sas_ir_operation_status_event(struct MPT3SAS_ADAPTER *ioc,
Mpi2EventDataIrOperationStatus_t *event_data =
(Mpi2EventDataIrOperationStatus_t *)
fw_event->event_data;
- static struct _raid_device *raid_device;
+ struct _raid_device *raid_device;
unsigned long flags;
u16 handle;

@@ -7531,7 +7531,7 @@ _scsih_scan_for_devices_after_reset(struct MPT3SAS_ADAPTER *ioc)
u64 sas_address;
struct _sas_device *sas_device;
struct _sas_node *expander_device;
- static struct _raid_device *raid_device;
+ struct _raid_device *raid_device;
u8 retry_count;
unsigned long flags;

--
2.5.0


2017-07-20 00:07:54

by James Bottomley

[permalink] [raw]
Subject: Re: [PATCH] scsi: mpt3sas_scsih: remove unnecessary statics

On Wed, 2017-07-19 at 17:06 -0500, Gustavo A. R. Silva wrote:
> Remove unnecessary static on local variables raid_device.
> Such variables are initialized before being used, on
> every execution path throughout the functions. The
> static has no benefit and, removing it reduces the
> object file size.
>
> This issue was detected using Coccinelle and the following semantic
> patch:
>
> @bad exists@
> position p;
> identifier x;
> type T;
> @@
>
> static T x@p;
> ...
> x = <+...x...+>
>
> @@
> identifier x;
> expression e;
> type T;
> position p != bad.p;
> @@
>
> -static
>  T x@p;
>  ... when != x
>      when strict
> ?x = e;
>
> In the following log you can see a significant difference in the
> object file size. This log is the output of the size command, before
> and after the code change:
>
> before:
>    text    data     bss     dec     hex filename
>  126304   30384    1280  157968   26910
> drivers/scsi/mpt3sas/mpt3sas_scsih.o
>
> after:
>    text    data     bss     dec     hex filename
>  126292   30240    1152  157684   267f4
> drivers/scsi/mpt3sas/mpt3sas_scsih.o

I've got to say I'm deeply uneasy about using a data/bss size reduction
as the benchmark for saying something shouldn't be declared static.  In
this particular case the reduction is minimal, so it probably doesn't
matter; however, if the reduction were more significant, changing from
static to dynamic (i.e. on stack) allocation would increase the risk
that we'd blow the kernel stack.  Indeed one reason you might find
static declarations in functions is precisely to avoid blowing the
stack, so we wouldn't want to reverse them.

Other reasons for having static allocations instead of dynamic ones is
that you need to DMA to/from the structure (you can't DMA to stack) or
because you want to preserve values across function invocations.

There are definite reasons why statics in functions are a bad idea:
they prevent recursion and trip up code analysis, but in none of the
above cases would the fix be to remove the static qualifier.

James