Skip to contentSkip to conversation

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

PATCH In review Markus Elfring 4 participants 16 messages Dec 10, 2020 – Dec 14, 2020

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

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

From: Markus Elfring <[email protected]>
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.

---
 drivers/mmc/host/atmel-mci.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/mmc/host/atmel-mci.c +3 -3
index 444bd3a0a922..6a0d999ee82e 100644
@@ -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

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#1 lore raw

Hello,

Quoted 7 lines · Markus Elfring

From: Markus Elfring <[email protected]>
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.

Quoted 34 lines

This issue was detected by using the Coccinelle software.

---
 drivers/mmc/host/atmel-mci.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/mmc/host/atmel-mci.c +3 -4
index 444bd3a0a922..6a0d999ee82e 100644
@@ -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

Signature · 3 lines
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#2 lore raw
Quoted 6 lines
Quoted 4 lines

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

Quoted 1 line

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

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#3 lore raw
Quoted 10 lines · Markus Elfring
Quoted 6 lines
Quoted 4 lines

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.

Quoted 6 lines
Quoted 2 lines

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.

Signature · 3 lines
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#4 lore raw
Quoted 7 lines
Quoted 3 lines

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/[email protected]/

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

Regards,
Markus

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#5 lore raw
Quoted 6 lines · Markus Elfring
Quoted 6 lines
Quoted 3 lines

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.

Quoted 9 lines
Quoted 1 line

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/[email protected]/

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

If you were able to provide one good technical reason.

Signature · 3 lines
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#6 lore raw
Quoted 8 lines
Quoted 6 lines

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/[email protected]/

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

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#7 lore raw
Quoted 5 lines · Markus Elfring
Quoted 5 lines
Quoted 5 lines

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/[email protected]/

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

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?

Signature · 3 lines
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#8 lore raw
Quoted 6 lines · Alexandre Belloni
Quoted 5 lines · Markus Elfring
Quoted 5 lines
Quoted 5 lines

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/[email protected]/

Quoted 8 lines
Quoted 7 lines
Quoted 3 lines
Quoted 1 line

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-than00 -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-than00 -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=]
 }
 ^
#9 lore raw
Quoted 2 lines

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

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#10 lore raw
In reply to Joe Perches
Quoted 21 lines · Joe Perches
Quoted 6 lines · Alexandre Belloni
Quoted 5 lines · Markus Elfring
Quoted 5 lines
Quoted 5 lines

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/[email protected]/

Quoted 8 lines
Quoted 7 lines
Quoted 3 lines
Quoted 1 line

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.

Quoted 6 lines

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.

Signature · 3 lines
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com
#11 lore raw
Quoted 10 lines · Alexandre Belloni
Quoted 5 lines · Joe Perches

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

#13 lore raw
Quoted 1 line

Markus was banned from vger at the end of July after ignoring repeated warnings.

There were special disagreements and communication difficulties involved.

Quoted 1 line

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

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

#14 lore raw
Quoted 4 lines · Markus Elfring
Quoted 1 line

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

#15 lore raw

Re: [dm-devel] …: Reduce scope for the variable “…” in …()

Quoted 2 lines

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

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel