diff mbox series

mmc: atmel-mci: Reduce scope for the variable “slot” in atmci_request_end()

Message ID 466b4c6d-032f-fbcc-58ac-75f6f39d734f@web.de (mailing list archive)
State New, archived
Headers show
Series mmc: atmel-mci: Reduce scope for the variable “slot” in atmci_request_end() | expand

Commit Message

Markus Elfring Dec. 10, 2020, 3:01 p.m. UTC
From: Markus Elfring <elfring@users.sourceforge.net>
Date: Thu, 10 Dec 2020 15:56:13 +0100

A local variable was used only within an if branch.
Thus move the definition for the variable “slot” into the corresponding
code block.

This issue was detected by using the Coccinelle software.

Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
---
 drivers/mmc/host/atmel-mci.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

--
2.29.2

Comments

Alexandre Belloni Dec. 10, 2020, 3:10 p.m. UTC | #1
Hello,

On 10/12/2020 16:01:44+0100, Markus Elfring wrote:
> From: Markus Elfring <elfring@users.sourceforge.net>
> Date: Thu, 10 Dec 2020 15:56:13 +0100
> 
> A local variable was used only within an if branch.
> Thus move the definition for the variable “slot” into the corresponding
> code block.
> 

What is the improvement here?

This makes the code harder to read.

> This issue was detected by using the Coccinelle software.
> 
> Signed-off-by: Markus Elfring <elfring@users.sourceforge.net>
> ---
>  drivers/mmc/host/atmel-mci.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/mmc/host/atmel-mci.c b/drivers/mmc/host/atmel-mci.c
> index 444bd3a0a922..6a0d999ee82e 100644
> --- a/drivers/mmc/host/atmel-mci.c
> +++ b/drivers/mmc/host/atmel-mci.c
> @@ -1558,7 +1558,6 @@ static void atmci_request_end(struct atmel_mci *host, struct mmc_request *mrq)
>  	__releases(&host->lock)
>  	__acquires(&host->lock)
>  {
> -	struct atmel_mci_slot	*slot = NULL;
>  	struct mmc_host		*prev_mmc = host->cur_slot->mmc;
> 
>  	WARN_ON(host->cmd || host->data);
> @@ -1579,8 +1578,9 @@ static void atmci_request_end(struct atmel_mci *host, struct mmc_request *mrq)
>  	host->cur_slot->mrq = NULL;
>  	host->mrq = NULL;
>  	if (!list_empty(&host->queue)) {
> -		slot = list_entry(host->queue.next,
> -				struct atmel_mci_slot, queue_node);
> +		struct atmel_mci_slot *slot = list_entry(host->queue.next,
> +							 struct atmel_mci_slot,
> +							 queue_node);
>  		list_del(&slot->queue_node);
>  		dev_vdbg(&host->pdev->dev, "list not empty: %s is next\n",
>  				mmc_hostname(slot->mmc));
> --
> 2.29.2
>
Markus Elfring Dec. 10, 2020, 4:35 p.m. UTC | #2
>> A local variable was used only within an if branch.
>> Thus move the definition for the variable “slot” into the corresponding
>> code block.
>>
>
> What is the improvement here?

A possible refactoring.
https://refactoring.com/catalog/reduceScopeOfVariable.html


> This makes the code harder to read.

Can the extra null pointer initialisation trigger a source code analysis warning
like “Addresses-Coverity: ("Unused value")” for this function implementation?

Regards,
Markus
Alexandre Belloni Dec. 10, 2020, 5:07 p.m. UTC | #3
On 10/12/2020 17:35:31+0100, Markus Elfring wrote:
> >> A local variable was used only within an if branch.
> >> Thus move the definition for the variable “slot” into the corresponding
> >> code block.
> >>
> >
> > What is the improvement here?
> 
> A possible refactoring.
> https://refactoring.com/catalog/reduceScopeOfVariable.html
> 

I quote:
"Since declarations of variables in many cases costs computational
cycles, you may end up wasting time for nothing."

This is false, it doesn't.

I also quote:
"When I'm writing new code I find I don't scope my temps any less than
method scope. This is because I keep my methods short, so reducing scope
any further doesn't add much value. The value of this refactoring is in
breaking up a large method."

Is that function large? It is not.


> 
> > This makes the code harder to read.
> 
> Can the extra null pointer initialisation trigger a source code analysis warning
> like “Addresses-Coverity: ("Unused value")” for this function implementation?
> 

Did you check, does it? It doesn't.

Are you wasting maintainer and reviewer's time? Yes you are.
Markus Elfring Dec. 10, 2020, 5:23 p.m. UTC | #4
>> Can the extra null pointer initialisation trigger a source code analysis warning
>> like “Addresses-Coverity: ("Unused value")” for this function implementation?
>>
>
> Did you check, does it? It doesn't.
>
> Are you wasting maintainer and reviewer's time? Yes you are.

How do you think about a patch like “staging: speakup: remove redundant initialization
of pointer p_key” for comparison?
https://lore.kernel.org/patchwork/patch/1199128/
https://lore.kernel.org/driverdev-devel/20200223153954.420731-1-colin.king@canonical.com/

Would you tolerate to omit the initialisation for the variable “slot”?

Regards,
Markus
Alexandre Belloni Dec. 10, 2020, 6:21 p.m. UTC | #5
On 10/12/2020 18:23:05+0100, Markus Elfring wrote:
> >> Can the extra null pointer initialisation trigger a source code analysis warning
> >> like “Addresses-Coverity: ("Unused value")” for this function implementation?
> >>
> >
> > Did you check, does it? It doesn't.
> >
> > Are you wasting maintainer and reviewer's time? Yes you are.
> 
> How do you think about a patch like “staging: speakup: remove redundant initialization
> of pointer p_key” for comparison?
> https://lore.kernel.org/patchwork/patch/1199128/
> https://lore.kernel.org/driverdev-devel/20200223153954.420731-1-colin.king@canonical.com/
> 
> Would you tolerate to omit the initialisation for the variable “slot”?
> 

If you were able to provide one good technical reason.
Markus Elfring Dec. 11, 2020, 6:34 a.m. UTC | #6
>> How do you think about a patch like “staging: speakup: remove redundant initialization
>> of pointer p_key” for comparison?
>> https://lore.kernel.org/patchwork/patch/1199128/
>> https://lore.kernel.org/driverdev-devel/20200223153954.420731-1-colin.king@canonical.com/
>>
>> Would you tolerate to omit the initialisation for the variable “slot”?
>
> If you were able to provide one good technical reason.

I find that the positions of variable definitions (and similar assignments) influence
the generation of executable code.


How do you think about another bit of background information?

Example:
[PATCH v3 2/3] gianfar: Delete unnecessary variable initialisations in gfar_ethflow_to_filer_table()
https://lore.kernel.org/patchwork/patch/635859/#821314
https://lore.kernel.org/lkml/20160115121510.GJ4764@mwanda/

Regards,
Markus
Alexandre Belloni Dec. 11, 2020, 8:03 a.m. UTC | #7
On 11/12/2020 07:34:41+0100, Markus Elfring wrote:
> >> How do you think about a patch like “staging: speakup: remove redundant initialization
> >> of pointer p_key” for comparison?
> >> https://lore.kernel.org/patchwork/patch/1199128/
> >> https://lore.kernel.org/driverdev-devel/20200223153954.420731-1-colin.king@canonical.com/
> >>
> >> Would you tolerate to omit the initialisation for the variable “slot”?
> >
> > If you were able to provide one good technical reason.
> 
> I find that the positions of variable definitions (and similar assignments) influence
> the generation of executable code.
> 

And you are wrong, it doesn't. Before:

c044a0f0 <atmci_request_end>:
{
c044a0f0:	e92d4070 	push	{r4, r5, r6, lr}
c044a0f4:	e1a04000 	mov	r4, r0
	WARN_ON(host->cmd || host->data);
c044a0f8:	e5902024 	ldr	r2, [r0, #36]	; 0x24
{
c044a0fc:	e1a06001 	mov	r6, r1
	struct mmc_host		*prev_mmc = host->cur_slot->mmc;
c044a100:	e590301c 	ldr	r3, [r0, #28]
	WARN_ON(host->cmd || host->data);
c044a104:	e3520000 	cmp	r2, #0
	struct mmc_host		*prev_mmc = host->cur_slot->mmc;
c044a108:	e5935000 	ldr	r5, [r3]
	WARN_ON(host->cmd || host->data);
c044a10c:	0a00002d 	beq	c044a1c8 <atmci_request_end+0xd8>
c044a110:	e3000000 	movw	r0, #0
			c044a110: R_ARM_MOVW_ABS_NC	.LC0
c044a114:	e3a03000 	mov	r3, #0
c044a118:	e3400000 	movt	r0, #0
			c044a118: R_ARM_MOVT_ABS	.LC0
c044a11c:	e3a02009 	mov	r2, #9
c044a120:	e300161c 	movw	r1, #1564	; 0x61c
c044a124:	ebfffffe 	bl	0 <warn_slowpath_fmt>
			c044a124: R_ARM_CALL	warn_slowpath_fmt
	del_timer(&host->timer);
c044a128:	e28400a4 	add	r0, r4, #164	; 0xa4
c044a12c:	ebfffffe 	bl	0 <del_timer>
			c044a12c: R_ARM_CALL	del_timer
	if (host->need_clock_update) {
c044a130:	e5d430a0 	ldrb	r3, [r4, #160]	; 0xa0
c044a134:	e3530000 	cmp	r3, #0
c044a138:	0a000005 	beq	c044a154 <atmci_request_end+0x64>
		atmci_writel(host, ATMCI_MR, host->mode_reg);
c044a13c:	e59420b8 	ldr	r2, [r4, #184]	; 0xb8
c044a140:	e5943000 	ldr	r3, [r4]
	asm volatile("str %1, %0"
c044a144:	e5832004 	str	r2, [r3, #4]
		if (host->caps.has_cfg_reg)
c044a148:	e5d420da 	ldrb	r2, [r4, #218]	; 0xda
c044a14c:	e3520000 	cmp	r2, #0
c044a150:	1a000019 	bne	c044a1bc <atmci_request_end+0xcc>
	host->cur_slot->mrq = NULL;
c044a154:	e594101c 	ldr	r1, [r4, #28]
	return READ_ONCE(head->next) == head;
c044a158:	e1a03004 	mov	r3, r4
c044a15c:	e3a02000 	mov	r2, #0
c044a160:	e5812010 	str	r2, [r1, #16]
	host->mrq = NULL;
c044a164:	e5842020 	str	r2, [r4, #32]
c044a168:	e5b31098 	ldr	r1, [r3, #152]!	; 0x98
	if (!list_empty(&host->queue)) {
c044a16c:	e1510003 	cmp	r1, r3
		host->state = STATE_IDLE;
c044a170:	05842094 	streq	r2, [r4, #148]	; 0x94
	if (!list_empty(&host->queue)) {
c044a174:	0a00000c 	beq	c044a1ac <atmci_request_end+0xbc>
		slot = list_entry(host->queue.next,
c044a178:	e5943098 	ldr	r3, [r4, #152]	; 0x98


After:

c044a0f0 <atmci_request_end>:
{
c044a0f0:	e92d4070 	push	{r4, r5, r6, lr}
c044a0f4:	e1a04000 	mov	r4, r0
	WARN_ON(host->cmd || host->data);
c044a0f8:	e5902024 	ldr	r2, [r0, #36]	; 0x24
{
c044a0fc:	e1a06001 	mov	r6, r1
	struct mmc_host		*prev_mmc = host->cur_slot->mmc;
c044a100:	e590301c 	ldr	r3, [r0, #28]
	WARN_ON(host->cmd || host->data);
c044a104:	e3520000 	cmp	r2, #0
	struct mmc_host		*prev_mmc = host->cur_slot->mmc;
c044a108:	e5935000 	ldr	r5, [r3]
	WARN_ON(host->cmd || host->data);
c044a10c:	0a00002d 	beq	c044a1c8 <atmci_request_end+0xd8>
c044a110:	e3000000 	movw	r0, #0
			c044a110: R_ARM_MOVW_ABS_NC	.LC0
c044a114:	e3a03000 	mov	r3, #0
c044a118:	e3400000 	movt	r0, #0
			c044a118: R_ARM_MOVT_ABS	.LC0
c044a11c:	e3a02009 	mov	r2, #9
c044a120:	e300161b 	movw	r1, #1563	; 0x61b
c044a124:	ebfffffe 	bl	0 <warn_slowpath_fmt>
			c044a124: R_ARM_CALL	warn_slowpath_fmt
	del_timer(&host->timer);
c044a128:	e28400a4 	add	r0, r4, #164	; 0xa4
c044a12c:	ebfffffe 	bl	0 <del_timer>
			c044a12c: R_ARM_CALL	del_timer
	if (host->need_clock_update) {
c044a130:	e5d430a0 	ldrb	r3, [r4, #160]	; 0xa0
c044a134:	e3530000 	cmp	r3, #0
c044a138:	0a000005 	beq	c044a154 <atmci_request_end+0x64>
		atmci_writel(host, ATMCI_MR, host->mode_reg);
c044a13c:	e59420b8 	ldr	r2, [r4, #184]	; 0xb8
c044a140:	e5943000 	ldr	r3, [r4]
	asm volatile("str %1, %0"
c044a144:	e5832004 	str	r2, [r3, #4]
		if (host->caps.has_cfg_reg)
c044a148:	e5d420da 	ldrb	r2, [r4, #218]	; 0xda
c044a14c:	e3520000 	cmp	r2, #0
c044a150:	1a000019 	bne	c044a1bc <atmci_request_end+0xcc>
	host->cur_slot->mrq = NULL;
c044a154:	e594101c 	ldr	r1, [r4, #28]
	return READ_ONCE(head->next) == head;
c044a158:	e1a03004 	mov	r3, r4
c044a15c:	e3a02000 	mov	r2, #0
c044a160:	e5812010 	str	r2, [r1, #16]
	host->mrq = NULL;
c044a164:	e5842020 	str	r2, [r4, #32]
c044a168:	e5b31098 	ldr	r1, [r3, #152]!	; 0x98
	if (!list_empty(&host->queue)) {
c044a16c:	e1510003 	cmp	r1, r3
		host->state = STATE_IDLE;
c044a170:	05842094 	streq	r2, [r4, #148]	; 0x94
	if (!list_empty(&host->queue)) {
c044a174:	0a00000c 	beq	c044a1ac <atmci_request_end+0xbc>
		struct atmel_mci_slot *slot = list_entry(host->queue.next,
c044a178:	e5943098 	ldr	r3, [r4, #152]	; 0x98


Do you realize your patch is just unnecessary churn now?

Is it too difficult for you to actually compile the driver and look
at the changes before submitting patches?
Dan Carpenter Dec. 11, 2020, 8:37 a.m. UTC | #8
Markus was banned from vger at the end of July after ignoring repeated
warnings.  This makes it hard to review any patches or follow
discussion...

regards,
dan carpenter
Markus Elfring Dec. 11, 2020, 9:08 a.m. UTC | #9
> Markus was banned from vger at the end of July after ignoring repeated warnings.

There were special disagreements and communication difficulties involved.


> This makes it hard to review any patches or follow discussion...

You shared also special software development opinions about extra variable
initialisations occasionally, didn't you?

Regards,
Markus
Joe Perches Dec. 12, 2020, 9:16 a.m. UTC | #10
On Fri, 2020-12-11 at 09:03 +0100, Alexandre Belloni wrote:
> On 11/12/2020 07:34:41+0100, Markus Elfring wrote:
> > > > How do you think about a patch like “staging: speakup: remove redundant initialization
> > > > of pointer p_key” for comparison?
> > > > https://lore.kernel.org/patchwork/patch/1199128/
> > > > https://lore.kernel.org/driverdev-devel/20200223153954.420731-1-colin.king@canonical.com/
> > > > 
> > > > Would you tolerate to omit the initialisation for the variable “slot”?
> > > 
> > > If you were able to provide one good technical reason.
> > 
> > I find that the positions of variable definitions (and similar assignments) influence
> > the generation of executable code.
> > 
> And you are wrong, it doesn't.

I rarely reply or read any Markus' emails as everything
from Markus goes into a 'don't read' folder but I was cc'd
directly on one from someone else recently so I think I
should reply to this one too.

In this case Alexandre it seems true, but in the generic case
it may be false.  It may depend on stack size and location.

For instance, with large structs declared either at the top
of a function or in separate branches within the function:

$ cat t_structs.c
struct a {
	int a[2000];
	int b[4000];
};

struct b {
	char a[100];
	char b[10000];
};

void foo1(struct a *a);
void foo2(struct b *b);

void foo(int index)
{
	if (index) {
		struct a ai = {};

		ai.a[index] = index;
		foo1(&ai);
	} else {
		struct b bi = {};

		bi.b[0] = 1;
		foo2(&bi);
	}
}

void bar(int index)
{
	struct a ai = {};
	struct b bi = {};

	if (index) {
		ai.a[index] = index;
		foo1(&ai);
	} else {
		bi.b[0] = 1;
		foo2(&bi);
	}
}

$

newer gcc versions are smart enough to minimize
stack use in foo() but not bar() so ai and bi start
at the same address in foo() so the total stack
used is smaller.

older gcc versions like 4.8 use separate addresses
for ai and bi in foo() so the total stack used is
larger.

$ gcc-4.8 -Wframe-larger-than=1000 -c t_structs.c
t_structs.c: In function ‘foo’:
t_structs.c:27:1: warning: the frame size of 34116 bytes is larger than 1000 bytes [-Wframe-larger-than=]
 }
 ^
t_structs.c: In function ‘bar’:
t_structs.c:41:1: warning: the frame size of 34116 bytes is larger than 1000 bytes [-Wframe-larger-than=]
 }
 ^

$ gcc-5 -Wframe-larger-than=1000 -c t_structs.c
t_structs.c: In function ‘foo’:
t_structs.c:27:1: warning: the frame size of 24032 bytes is larger than 1000 bytes [-Wframe-larger-than=]
 }
 ^
t_structs.c: In function ‘bar’:
t_structs.c:41:1: warning: the frame size of 34144 bytes is larger than 1000 bytes [-Wframe-larger-than=]
 }
 ^
Markus Elfring Dec. 12, 2020, 11 a.m. UTC | #11
> In this case Alexandre it seems true, but in the generic case
> it may be false.  It may depend on stack size and location.

Thanks for your constructive feedback.

Will any other source code transformations become more interesting
for the usage of bigger arrays instead of the proposal to avoid
extra null pointer initialisations by adjusting the position
for the desired variable definition?

Regards,
Markus
Alexandre Belloni Dec. 12, 2020, 1:17 p.m. UTC | #12
On 12/12/2020 01:16:39-0800, Joe Perches wrote:
> On Fri, 2020-12-11 at 09:03 +0100, Alexandre Belloni wrote:
> > On 11/12/2020 07:34:41+0100, Markus Elfring wrote:
> > > > > How do you think about a patch like “staging: speakup: remove redundant initialization
> > > > > of pointer p_key” for comparison?
> > > > > https://lore.kernel.org/patchwork/patch/1199128/
> > > > > https://lore.kernel.org/driverdev-devel/20200223153954.420731-1-colin.king@canonical.com/
> > > > > 
> > > > > Would you tolerate to omit the initialisation for the variable “slot”?
> > > > 
> > > > If you were able to provide one good technical reason.
> > > 
> > > I find that the positions of variable definitions (and similar assignments) influence
> > > the generation of executable code.
> > > 
> > And you are wrong, it doesn't.
> 
> I rarely reply or read any Markus' emails as everything
> from Markus goes into a 'don't read' folder but I was cc'd
> directly on one from someone else recently so I think I
> should reply to this one too.
> 
> In this case Alexandre it seems true, but in the generic case
> it may be false.  It may depend on stack size and location.
> 
> For instance, with large structs declared either at the top
> of a function or in separate branches within the function:
> 

I think you realize your example is way more complicated than
initializing basic type variable to 0 or NULL which has probably been
optimized forever.

This patch is just unnecessary churn that was generated without giving
any thought. It was easy to check whether the patch actually improved
anything. It doesn't and makes readability worse.
Joe Perches Dec. 12, 2020, 6:48 p.m. UTC | #13
On Sat, 2020-12-12 at 14:17 +0100, Alexandre Belloni wrote:
> On 12/12/2020 01:16:39-0800, Joe Perches wrote:
> > In this case Alexandre it seems true, but in the generic case
> > it may be false.  It may depend on stack size and location.
> > 
> > For instance, with large structs declared either at the top
> > of a function or in separate branches within the function:
>
> I think you realize your example is way more complicated than
> initializing basic type variable to 0 or NULL which has probably been
> optimized forever.

Maybe.

Markus does a lot of what most (or perhaps some) consider thoughtless.

A generic point might be maximizing the local scope of declarations.

Maximizing the local scope can make reading easier as the type of an
automatic used for a temporary purpose can be found closer to the code
that uses it.  This is especially true for long line-count functions.

IMO: it's a reasonable goal though in this instance perhaps unnecessary.

cheers, Joe
Dan Carpenter Dec. 14, 2020, 7:05 a.m. UTC | #14
On Fri, Dec 11, 2020 at 10:08:54AM +0100, Markus Elfring wrote:
> > This makes it hard to review any patches or follow discussion...
> 
> You shared also special software development opinions about extra variable
> initialisations occasionally, didn't you?

I generally put everything at the top of the function...  I don't have
a well developed philosophy for when variables should be declared with
a smaller scope.

	int ret;  <-- this should always be function scope

Probably the other people are right that making scopes shorter is more
important when the function is very long.

regards,
dan carpenter
Markus Elfring Dec. 14, 2020, 10:27 a.m. UTC | #15
> Probably the other people are right that making scopes shorter is more
> important when the function is very long.

How much will sizes of function implementations influence the acceptance
for possible source code refactorings?
https://refactoring.com/catalog/reduceScopeOfVariable.html

* Would you find another conversion of an extra null pointer initialisation
  to a later variable definition helpful then at other places?

  Example:
  [PATCH] dm snapshot: Reduce scope for the variable “pe” in snapshot_map()
  https://elixir.bootlin.com/linux/v5.10/source/drivers/md/dm-snap.c#L1941
  https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/md/dm-snap.c?id=2c85ebc57b3e1817b6ce1a6b703928e113a90442#n1941

* How do you think about to omit the initialisation for the variable “ret”
  in the implementation of the function “w1_uevent”
  (because it will be reassigned by a function call a bit later)?
  https://elixir.bootlin.com/linux/v5.10/source/drivers/w1/w1.c#L578
  https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/drivers/w1/w1.c?id=2c85ebc57b3e1817b6ce1a6b703928e113a90442#n578

  Would any adjustment with the tag “Addresses-Coverity: ("Unused value")”
  become relevant here?

Regards,
Markus
diff mbox series

Patch

diff --git a/drivers/mmc/host/atmel-mci.c b/drivers/mmc/host/atmel-mci.c
index 444bd3a0a922..6a0d999ee82e 100644
--- a/drivers/mmc/host/atmel-mci.c
+++ b/drivers/mmc/host/atmel-mci.c
@@ -1558,7 +1558,6 @@  static void atmci_request_end(struct atmel_mci *host, struct mmc_request *mrq)
 	__releases(&host->lock)
 	__acquires(&host->lock)
 {
-	struct atmel_mci_slot	*slot = NULL;
 	struct mmc_host		*prev_mmc = host->cur_slot->mmc;

 	WARN_ON(host->cmd || host->data);
@@ -1579,8 +1578,9 @@  static void atmci_request_end(struct atmel_mci *host, struct mmc_request *mrq)
 	host->cur_slot->mrq = NULL;
 	host->mrq = NULL;
 	if (!list_empty(&host->queue)) {
-		slot = list_entry(host->queue.next,
-				struct atmel_mci_slot, queue_node);
+		struct atmel_mci_slot *slot = list_entry(host->queue.next,
+							 struct atmel_mci_slot,
+							 queue_node);
 		list_del(&slot->queue_node);
 		dev_vdbg(&host->pdev->dev, "list not empty: %s is next\n",
 				mmc_hostname(slot->mmc));