Skip to contentSkip to conversation

x86, mtrr: mark range_new in mtrr_calc_range_state() as __initdata

PATCH Superseded Rasmus Villemoes 3 participants 6 messages Nov 13, 2015 – Dec 4, 2015 x86

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 x86, mtrr: mark range_new in mtrr_calc_range_state() as __initdata

range_new doesn't seem to be used after init. It is only passed to memset, sum_ranges, memcmp and x86_get_mtrr_mem_range, the latter of which also only passes it on to various *range* library functions. So mark it __initdata to free up an extra page after init.

nr_range_new is unconditionally assigned to before it is read, so there's no point in having it static.

---
 arch/x86/kernel/cpu/mtrr/cleanup.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/mtrr/cleanup.c +2 -2
index 70d7c93f4550..b1a9ad366f67 100644
@@ -593,9 +593,9 @@ mtrr_calc_range_state(u64 chunk_size, u64 gran_size,
 		      unsigned long x_remove_base,
 		      unsigned long x_remove_size, int i)
 {
-	static struct range range_new[RANGE_NUM];
+	static struct range range_new[RANGE_NUM] __initdata;
 	unsigned long range_sums_new;
-	static int nr_range_new;
+	int nr_range_new;
 	int num_reg;
 
 	/* Convert ranges to var ranges state: */
Signature · 1 line
2.6.1
#1 lore raw
Quoted 19 lines · Rasmus Villemoes

range_new doesn't seem to be used after init. It is only passed to memset, sum_ranges, memcmp and x86_get_mtrr_mem_range, the latter of which also only passes it on to various *range* library functions. So mark it __initdata to free up an extra page after init.

nr_range_new is unconditionally assigned to before it is read, so there's no point in having it static.

---
 arch/x86/kernel/cpu/mtrr/cleanup.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/mtrr/cleanup.c +0 -0
index 70d7c93f4550..b1a9ad366f67 100644
@@ -593,9 +593,9 @@ mtrr_calc_range_state(u64 chunk_size, u64 gran_size,
 		      unsigned long x_remove_base,

arch/x86/kernel/cpu/mtrr/cleanup.c mtrr_calc_range_state(u64 chunk_size, u64 gran_size, · line 601

Quoted 10 lines
arch/x86/kernel/cpu/mtrr/cleanup.c +2 -2
 		      unsigned long x_remove_size, int i)
 {
-	static struct range range_new[RANGE_NUM];
+	static struct range range_new[RANGE_NUM] __initdata;
 	unsigned long range_sums_new;
-	static int nr_range_new;
+	int nr_range_new;
 	int num_reg;
 
 	/* Convert ranges to var ranges state: */

So this static variable actually surprised me - I never realized it was there - and it's not some simple 'once' flag, but something that is essential semantics.

So marking it __initdata is correct, but please also move it out of function local variables scope, into file scope - and name it properly as well, like mtrr_new_range[] or so?

Thanks,

Ingo

#2 lore raw
Quoted 30 lines · Ingo Molnar
Quoted 28 lines · Rasmus Villemoes

range_new doesn't seem to be used after init. It is only passed to memset, sum_ranges, memcmp and x86_get_mtrr_mem_range, the latter of which also only passes it on to various *range* library functions. So mark it __initdata to free up an extra page after init.

nr_range_new is unconditionally assigned to before it is read, so there's no point in having it static.

---
 arch/x86/kernel/cpu/mtrr/cleanup.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/mtrr/cleanup.c +2 -2
index 70d7c93f4550..b1a9ad366f67 100644
@@ -593,9 +593,9 @@ mtrr_calc_range_state(u64 chunk_size, u64 gran_size,
 		      unsigned long x_remove_base,
 		      unsigned long x_remove_size, int i)
 {
-	static struct range range_new[RANGE_NUM];
+	static struct range range_new[RANGE_NUM] __initdata;
 	unsigned long range_sums_new;
-	static int nr_range_new;
+	int nr_range_new;
 	int num_reg;
 
Quoted 8 lines

arch/x86/kernel/cpu/mtrr/cleanup.c mtrr_calc_range_state(u64 chunk_size, u64 gran_size, · line 601

Quoted 1 line
arch/x86/kernel/cpu/mtrr/cleanup.c +0 -0
 	/* Convert ranges to var ranges state: */

So this static variable actually surprised me - I never realized it was there - and it's not some simple 'once' flag, but something that is essential semantics.

So marking it __initdata is correct, but please also move it out of function local variables scope, into file scope - and name it properly as well, like mtrr_new_range[] or so?

I can certainly do that, but isn't the usual preference to keep the scope as small as possible? IOW, why do you want to make this a file-scoped variable?

Also, I don't really see how the 'static' has 'essential semantics'. AFAICT, the contents are wiped on every invocation of mtrr_calc_range_state, so the only reason it's static is to avoid blowing the stack.

Rasmus

#3 lore raw
Quoted 37 lines · Rasmus Villemoes
Quoted 35 lines · Ingo Molnar
Quoted 19 lines · Rasmus Villemoes

range_new doesn't seem to be used after init. It is only passed to memset, sum_ranges, memcmp and x86_get_mtrr_mem_range, the latter of which also only passes it on to various *range* library functions. So mark it __initdata to free up an extra page after init.

nr_range_new is unconditionally assigned to before it is read, so there's no point in having it static.

---
 arch/x86/kernel/cpu/mtrr/cleanup.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/mtrr/cleanup.c +0 -0
index 70d7c93f4550..b1a9ad366f67 100644
@@ -593,9 +593,9 @@ mtrr_calc_range_state(u64 chunk_size, u64 gran_size,
 		      unsigned long x_remove_base,

arch/x86/kernel/cpu/mtrr/cleanup.c mtrr_calc_range_state(u64 chunk_size, u64 gran_size, · line 601

Quoted 10 lines
arch/x86/kernel/cpu/mtrr/cleanup.c +2 -2
 		      unsigned long x_remove_size, int i)
 {
-	static struct range range_new[RANGE_NUM];
+	static struct range range_new[RANGE_NUM] __initdata;
 	unsigned long range_sums_new;
-	static int nr_range_new;
+	int nr_range_new;
 	int num_reg;
 
 	/* Convert ranges to var ranges state: */

So this static variable actually surprised me - I never realized it was there - and it's not some simple 'once' flag, but something that is essential semantics.

Quoted 6 lines
Quoted 3 lines

So marking it __initdata is correct, but please also move it out of function local variables scope, into file scope - and name it properly as well, like mtrr_new_range[] or so?

I can certainly do that, but isn't the usual preference to keep the scope as small as possible? IOW, why do you want to make this a file-scoped variable?

The preference is to keep code readable and obvious, and this one wasn't: relevant state/data was hidden via a non-commented local static variable.

Quoted 3 lines

Also, I don't really see how the 'static' has 'essential semantics'. AFAICT, the contents are wiped on every invocation of mtrr_calc_range_state, so the only reason it's static is to avoid blowing the stack.

So this was another property that wasn't obvious from the limited context I saw in the patch, i.e. the variable definition. Another solution would be to add a comment explaining that this is a local variable to keep kernel stack size down, and explain why it's safe to do that.

Thanks,

Ingo

#4 lore raw

PATCH v2 1/1 x86, mtrr: mark range_new in mtrr_calc_range_state() as __initdata

range_new doesn't seem to be used after init. It is only passed to memset, sum_ranges, memcmp and x86_get_mtrr_mem_range, the latter of which also only passes it on to various *range* library functions. So mark it __initdata to free up an extra page after init.

Its contents are wiped at every call to mtrr_calc_range_state(), so it being static is not about preserving state between calls, but simply to avoid a 4k+ stack frame. While there, add a comment explaining this and why it's safe.

We could also mark nr_range_new as __initdata, but since it's just a single int and also doesn't carry state between calls (it is unconditionally assigned to before it is read), we might as well make it an ordinary automatic variable.

---
Something like this, perhaps?

v2: Add comment on range_new, per Ingo.

 arch/x86/kernel/cpu/mtrr/cleanup.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/mtrr/cleanup.c +9 -2
index 70d7c93f4550..0d98503c2245 100644
@@ -593,9 +593,16 @@ mtrr_calc_range_state(u64 chunk_size, u64 gran_size,
 		      unsigned long x_remove_base,
 		      unsigned long x_remove_size, int i)
 {
-	static struct range range_new[RANGE_NUM];
+	/*
+	 * range_new should really be an automatic variable, but
+	 * putting 4096 bytes on the stack is frowned upon, to put it
+	 * mildly. It is safe to make it a static __initdata variable,
+	 * since mtrr_calc_range_state is only called during init and
+	 * there's no way it will call itself recursively.
+	 */
+	static struct range range_new[RANGE_NUM] __initdata;
 	unsigned long range_sums_new;
-	static int nr_range_new;
+	int nr_range_new;
 	int num_reg;
 
 	/* Convert ranges to var ranges state: */
Signature · 1 line
2.6.1
#5 lore raw

[tip:x86/mm] x86/mm/mtrr: Mark the 'range_new' static variable in mtrr_calc_range_state() as __initdata

x86/mm/mtrr: Mark the 'range_new' static variable in mtrr_calc_range_state() as __initdata
Commit-ID:  c332813b51cbe807d539bb059b81235abf1e3fdd
Gitweb:     http://git.kernel.org/tip/c332813b51cbe807d539bb059b81235abf1e3fdd
Author:     Rasmus Villemoes <[email protected]>
AuthorDate: Tue, 1 Dec 2015 21:44:50 +0100
Committer:  Ingo Molnar <[email protected]>
CommitDate: Fri, 4 Dec 2015 09:11:28 +0100

x86/mm/mtrr: Mark the 'range_new' static variable in mtrr_calc_range_state() as __initdata

'range_new' doesn't seem to be used after init. It is only passed to memset(), sum_ranges(), memcmp() and x86_get_mtrr_mem_range(), the latter of which also only passes it on to various *range* library functions.

So mark it __initdata to free up an extra page after init.

Its contents are wiped at every call to mtrr_calc_range_state(), so it being static is not about preserving state between calls, but simply to avoid a 4k+ stack frame. While there, add a comment explaining this and why it's safe.

We could also mark nr_range_new as __initdata, but since it's just a single int and also doesn't carry state between calls (it is unconditionally assigned to before it is read), we might as well make it an ordinary automatic variable.

---
 arch/x86/kernel/cpu/mtrr/cleanup.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
arch/x86/kernel/cpu/mtrr/cleanup.c +9 -2
index 70d7c93..0d98503 100644
@@ -593,9 +593,16 @@ mtrr_calc_range_state(u64 chunk_size, u64 gran_size,
 		      unsigned long x_remove_base,
 		      unsigned long x_remove_size, int i)
 {
-	static struct range range_new[RANGE_NUM];
+	/*
+	 * range_new should really be an automatic variable, but
+	 * putting 4096 bytes on the stack is frowned upon, to put it
+	 * mildly. It is safe to make it a static __initdata variable,
+	 * since mtrr_calc_range_state is only called during init and
+	 * there's no way it will call itself recursively.
+	 */
+	static struct range range_new[RANGE_NUM] __initdata;
 	unsigned long range_sums_new;
-	static int nr_range_new;
+	int nr_range_new;
 	int num_reg;
 
 	/* Convert ranges to var ranges state: */