[PATCH v4 07/12] x86/mtrr: allocate mtrr_value array dynamically

Juergen Gross posted 12 patches 3 years, 1 month ago
There is a newer version of this series
[PATCH v4 07/12] x86/mtrr: allocate mtrr_value array dynamically
Posted by Juergen Gross 3 years, 1 month ago
The mtrr_value[] array is a static variable, which is used only in a
few configurations. Consuming 6kB is ridiculous for this case,
especially as the array doesn't need to be that large and it can easily
be allocated dynamically.

Signed-off-by: Juergen Gross <jgross@suse.com>
---
 arch/x86/kernel/cpu/mtrr/mtrr.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kernel/cpu/mtrr/mtrr.c b/arch/x86/kernel/cpu/mtrr/mtrr.c
index 0c83990501f5..50cd2287b6e1 100644
--- a/arch/x86/kernel/cpu/mtrr/mtrr.c
+++ b/arch/x86/kernel/cpu/mtrr/mtrr.c
@@ -581,7 +581,7 @@ struct mtrr_value {
 	unsigned long	lsize;
 };
 
-static struct mtrr_value mtrr_value[MTRR_MAX_VAR_RANGES];
+static struct mtrr_value *mtrr_value;
 
 static int mtrr_save(void)
 {
@@ -750,6 +750,7 @@ static int __init mtrr_init_finialize(void)
 	 * TBD: is there any system with such CPU which supports
 	 * suspend/resume? If no, we should remove the code.
 	 */
+	mtrr_value = kcalloc(num_var_ranges, sizeof(*mtrr_value), GFP_KERNEL);
 	register_syscore_ops(&mtrr_syscore_ops);
 
 	return 0;
-- 
2.35.3
Re: [PATCH v4 07/12] x86/mtrr: allocate mtrr_value array dynamically
Posted by Borislav Petkov 3 years ago
On Mon, Mar 06, 2023 at 05:34:20PM +0100, Juergen Gross wrote:
> The mtrr_value[] array is a static variable, which is used only in a
> few configurations. Consuming 6kB is ridiculous for this case,

Ah, that struct mtrr_value is of size 24 due to that first member
mtrr_type getting padded even if it is a u8.

> especially as the array doesn't need to be that large and it can easily
> be allocated dynamically.
> 
> Signed-off-by: Juergen Gross <jgross@suse.com>
> ---
>  arch/x86/kernel/cpu/mtrr/mtrr.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/x86/kernel/cpu/mtrr/mtrr.c b/arch/x86/kernel/cpu/mtrr/mtrr.c
> index 0c83990501f5..50cd2287b6e1 100644
> --- a/arch/x86/kernel/cpu/mtrr/mtrr.c
> +++ b/arch/x86/kernel/cpu/mtrr/mtrr.c
> @@ -581,7 +581,7 @@ struct mtrr_value {
>  	unsigned long	lsize;
>  };
>  
> -static struct mtrr_value mtrr_value[MTRR_MAX_VAR_RANGES];
> +static struct mtrr_value *mtrr_value;
>  
>  static int mtrr_save(void)
>  {
> @@ -750,6 +750,7 @@ static int __init mtrr_init_finialize(void)
>  	 * TBD: is there any system with such CPU which supports
>  	 * suspend/resume? If no, we should remove the code.
>  	 */
> +	mtrr_value = kcalloc(num_var_ranges, sizeof(*mtrr_value), GFP_KERNEL);

Pls put that over the comment.

Also, you need to handle kcalloc() returning an error.

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
Re: [PATCH v4 07/12] x86/mtrr: allocate mtrr_value array dynamically
Posted by Juergen Gross 3 years ago
On 27.03.23 00:05, Borislav Petkov wrote:
> On Mon, Mar 06, 2023 at 05:34:20PM +0100, Juergen Gross wrote:
>> The mtrr_value[] array is a static variable, which is used only in a
>> few configurations. Consuming 6kB is ridiculous for this case,
> 
> Ah, that struct mtrr_value is of size 24 due to that first member
> mtrr_type getting padded even if it is a u8.
> 
>> especially as the array doesn't need to be that large and it can easily
>> be allocated dynamically.
>>
>> Signed-off-by: Juergen Gross <jgross@suse.com>
>> ---
>>   arch/x86/kernel/cpu/mtrr/mtrr.c | 3 ++-
>>   1 file changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/arch/x86/kernel/cpu/mtrr/mtrr.c b/arch/x86/kernel/cpu/mtrr/mtrr.c
>> index 0c83990501f5..50cd2287b6e1 100644
>> --- a/arch/x86/kernel/cpu/mtrr/mtrr.c
>> +++ b/arch/x86/kernel/cpu/mtrr/mtrr.c
>> @@ -581,7 +581,7 @@ struct mtrr_value {
>>   	unsigned long	lsize;
>>   };
>>   
>> -static struct mtrr_value mtrr_value[MTRR_MAX_VAR_RANGES];
>> +static struct mtrr_value *mtrr_value;
>>   
>>   static int mtrr_save(void)
>>   {
>> @@ -750,6 +750,7 @@ static int __init mtrr_init_finialize(void)
>>   	 * TBD: is there any system with such CPU which supports
>>   	 * suspend/resume? If no, we should remove the code.
>>   	 */
>> +	mtrr_value = kcalloc(num_var_ranges, sizeof(*mtrr_value), GFP_KERNEL);
> 
> Pls put that over the comment.
> 
> Also, you need to handle kcalloc() returning an error.

Okay.


Juergen

Re: [PATCH v4 07/12] x86/mtrr: allocate mtrr_value array dynamically
Posted by Huang, Kai 3 years ago
On Mon, 2023-03-06 at 17:34 +0100, Juergen Gross wrote:
> The mtrr_value[] array is a static variable, which is used only in a
> few configurations. Consuming 6kB is ridiculous for this case,
> especially as the array doesn't need to be that large and it can easily
> be allocated dynamically.
> 
> Signed-off-by: Juergen Gross <jgross@suse.com>
> ---
>  arch/x86/kernel/cpu/mtrr/mtrr.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/x86/kernel/cpu/mtrr/mtrr.c b/arch/x86/kernel/cpu/mtrr/mtrr.c
> index 0c83990501f5..50cd2287b6e1 100644
> --- a/arch/x86/kernel/cpu/mtrr/mtrr.c
> +++ b/arch/x86/kernel/cpu/mtrr/mtrr.c
> @@ -581,7 +581,7 @@ struct mtrr_value {
>  	unsigned long	lsize;
>  };
>  
> -static struct mtrr_value mtrr_value[MTRR_MAX_VAR_RANGES];
> +static struct mtrr_value *mtrr_value;
>  
>  static int mtrr_save(void)
>  {
> @@ -750,6 +750,7 @@ static int __init mtrr_init_finialize(void)
>  	 * TBD: is there any system with such CPU which supports
>  	 * suspend/resume? If no, we should remove the code.
>  	 */
> +	mtrr_value = kcalloc(num_var_ranges, sizeof(*mtrr_value), GFP_KERNEL);

Theoretically dynamic allocation can fail, although it should not happen as this
happens during kernel boot and the size is small.  Maybe a WARN()?

>  	register_syscore_ops(&mtrr_syscore_ops);
>  
>  	return 0;

Re: [PATCH v4 07/12] x86/mtrr: allocate mtrr_value array dynamically
Posted by Juergen Gross 3 years ago
On 20.03.23 13:25, Huang, Kai wrote:
> On Mon, 2023-03-06 at 17:34 +0100, Juergen Gross wrote:
>> The mtrr_value[] array is a static variable, which is used only in a
>> few configurations. Consuming 6kB is ridiculous for this case,
>> especially as the array doesn't need to be that large and it can easily
>> be allocated dynamically.
>>
>> Signed-off-by: Juergen Gross <jgross@suse.com>
>> ---
>>   arch/x86/kernel/cpu/mtrr/mtrr.c | 3 ++-
>>   1 file changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/arch/x86/kernel/cpu/mtrr/mtrr.c b/arch/x86/kernel/cpu/mtrr/mtrr.c
>> index 0c83990501f5..50cd2287b6e1 100644
>> --- a/arch/x86/kernel/cpu/mtrr/mtrr.c
>> +++ b/arch/x86/kernel/cpu/mtrr/mtrr.c
>> @@ -581,7 +581,7 @@ struct mtrr_value {
>>   	unsigned long	lsize;
>>   };
>>   
>> -static struct mtrr_value mtrr_value[MTRR_MAX_VAR_RANGES];
>> +static struct mtrr_value *mtrr_value;
>>   
>>   static int mtrr_save(void)
>>   {
>> @@ -750,6 +750,7 @@ static int __init mtrr_init_finialize(void)
>>   	 * TBD: is there any system with such CPU which supports
>>   	 * suspend/resume? If no, we should remove the code.
>>   	 */
>> +	mtrr_value = kcalloc(num_var_ranges, sizeof(*mtrr_value), GFP_KERNEL);
> 
> Theoretically dynamic allocation can fail, although it should not happen as this
> happens during kernel boot and the size is small.  Maybe a WARN()?

Fine with me.


Juergen
Re: [PATCH v4 07/12] x86/mtrr: allocate mtrr_value array dynamically
Posted by Dave Hansen 3 years ago
On 3/20/23 06:49, Juergen Gross wrote:
>>>
>>> @@ -750,6 +750,7 @@ static int __init mtrr_init_finialize(void)
>>>        * TBD: is there any system with such CPU which supports
>>>        * suspend/resume? If no, we should remove the code.
>>>        */
>>> +    mtrr_value = kcalloc(num_var_ranges, sizeof(*mtrr_value),
>>> GFP_KERNEL);
>>
>> Theoretically dynamic allocation can fail, although it should not
>> happen as this
>> happens during kernel boot and the size is small.  Maybe a WARN()?
> 
> Fine with me.

What *actually* happens if the system is running out of memory and this
is the _first_ failure?  Does a WARN_ON() here help someone debug what
is going on?
Re: [PATCH v4 07/12] x86/mtrr: allocate mtrr_value array dynamically
Posted by Juergen Gross 3 years ago
On 20.03.23 16:31, Dave Hansen wrote:
> On 3/20/23 06:49, Juergen Gross wrote:
>>>>
>>>> @@ -750,6 +750,7 @@ static int __init mtrr_init_finialize(void)
>>>>         * TBD: is there any system with such CPU which supports
>>>>         * suspend/resume? If no, we should remove the code.
>>>>         */
>>>> +    mtrr_value = kcalloc(num_var_ranges, sizeof(*mtrr_value),
>>>> GFP_KERNEL);
>>>
>>> Theoretically dynamic allocation can fail, although it should not
>>> happen as this
>>> happens during kernel boot and the size is small.  Maybe a WARN()?
>>
>> Fine with me.
> 
> What *actually* happens if the system is running out of memory and this
> is the _first_ failure?  Does a WARN_ON() here help someone debug what
> is going on?

Good question. Especially as we don't set __GFP_NOWARN here.


Juergen