public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] x86/mm: declare static variable inside a function instead of global
@ 2022-06-18 10:11 Fanjun Kong
  2022-06-28 23:46 ` Andy Lutomirski
  0 siblings, 1 reply; 2+ messages in thread
From: Fanjun Kong @ 2022-06-18 10:11 UTC (permalink / raw)
  To: Dave Hansen, Andy Lutomirski, Peter Zijlstra, Thomas Gleixner,
	Ingo Molnar, Borislav Petkov, x86, H. Peter Anvin
  Cc: Muchun Song, Fanjun Kong, linux-kernel

Global variables are global capacity variables, unless they are
shadowed, they are available to the entire program.
To reduce the scope of a variable as much as possible is always good
practice.

Signed-off-by: Fanjun Kong <fanjun.kong@linux.dev>
---
 arch/x86/mm/init_64.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/x86/mm/init_64.c b/arch/x86/mm/init_64.c
index 39c5246964a9..582eab896480 100644
--- a/arch/x86/mm/init_64.c
+++ b/arch/x86/mm/init_64.c
@@ -1515,9 +1515,10 @@ static unsigned long probe_memory_block_size(void)
 	return bz;
 }
 
-static unsigned long memory_block_size_probed;
 unsigned long memory_block_size_bytes(void)
 {
+	static unsigned long memory_block_size_probed;
+
 	if (!memory_block_size_probed)
 		memory_block_size_probed = probe_memory_block_size();
 
-- 
2.36.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] x86/mm: declare static variable inside a function instead of global
  2022-06-18 10:11 [PATCH] x86/mm: declare static variable inside a function instead of global Fanjun Kong
@ 2022-06-28 23:46 ` Andy Lutomirski
  0 siblings, 0 replies; 2+ messages in thread
From: Andy Lutomirski @ 2022-06-28 23:46 UTC (permalink / raw)
  To: Fanjun Kong, Dave Hansen, Peter Zijlstra, Thomas Gleixner,
	Ingo Molnar, Borislav Petkov, x86, H. Peter Anvin
  Cc: Muchun Song, linux-kernel

On 6/18/22 03:11, Fanjun Kong wrote:
> Global variables are global capacity variables, unless they are
> shadowed, they are available to the entire program.

This is true in general, but:

> -static unsigned long memory_block_size_probed;

    ^^^^^^

So this isn't actually the case.

>   unsigned long memory_block_size_bytes(void)
>   {
> +	static unsigned long memory_block_size_probed;
> +
>   	if (!memory_block_size_probed)
>   		memory_block_size_probed = probe_memory_block_size();
>   

I'm sort of okay with the patch, but it's worth noting that, in C++, 
initialized function-scope static variables have quite surprising 
semantics, so this type of change isn't a pure win in my book.  (Yes, 
the kernel is C, not C++.  But C++ programmers may be nervous anyway.)

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2022-06-28 23:46 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-06-18 10:11 [PATCH] x86/mm: declare static variable inside a function instead of global Fanjun Kong
2022-06-28 23:46 ` Andy Lutomirski

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox