mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] LoongArch: Correct the calculation logic of thread_count
@ 2025-11-26  2:36 Qiang Ma
  2025-11-26  4:49 ` Huacai Chen
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Qiang Ma @ 2025-11-26  2:36 UTC (permalink / raw)
  To: chenhuacai, kernel; +Cc: loongarch, linux-kernel, Qiang Ma

For thread_count, the current calculation method has a maximum of 255,
which may not be sufficient in the future. Therefore, we are correcting
it now.

Reference: SMBIOS Specification, 7.5 Processor Information (Type 4)[1]

[1]: https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.9.0.pdf

Signed-off-by: Qiang Ma <maqianga@uniontech.com>
---
 arch/loongarch/kernel/setup.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)

diff --git a/arch/loongarch/kernel/setup.c b/arch/loongarch/kernel/setup.c
index 25a87378e48e..760f5ce44384 100644
--- a/arch/loongarch/kernel/setup.c
+++ b/arch/loongarch/kernel/setup.c
@@ -56,6 +56,7 @@
 #define SMBIOS_FREQLOW_MASK		0xFF
 #define SMBIOS_CORE_PACKAGE_OFFSET	0x23
 #define SMBIOS_THREAD_PACKAGE_OFFSET	0x25
+#define SMBIOS_THREAD_PACKAGE_2_OFFSET	0x2E
 #define LOONGSON_EFI_ENABLE		(1 << 3)
 
 unsigned long fw_arg0, fw_arg1, fw_arg2;
@@ -120,13 +121,19 @@ static void __init parse_cpu_table(const struct dmi_header *dm)
 {
 	long freq_temp = 0;
 	char *dmi_data = (char *)dm;
+	u8 thread_count;
+	u16 thread_count2;
 
 	freq_temp = ((*(dmi_data + SMBIOS_FREQHIGH_OFFSET) << 8) +
 			((*(dmi_data + SMBIOS_FREQLOW_OFFSET)) & SMBIOS_FREQLOW_MASK));
 	cpu_clock_freq = freq_temp * 1000000;
 
 	loongson_sysconf.cpuname = (void *)dmi_string_parse(dm, dmi_data[16]);
-	loongson_sysconf.cores_per_package = *(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
+	thread_count = *(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
+	thread_count2 = *(u16 *)(dmi_data + SMBIOS_THREAD_PACKAGE_2_OFFSET);
+	if (thread_count != 0)
+		loongson_sysconf.cores_per_package = dm->length >= 0x30 && thread_count == 0xFF ?
+						     thread_count2 : thread_count;
 
 	pr_info("CpuClock = %llu\n", cpu_clock_freq);
 }
-- 
2.20.1


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

* Re: [PATCH] LoongArch: Correct the calculation logic of thread_count
  2025-11-26  2:36 [PATCH] LoongArch: Correct the calculation logic of thread_count Qiang Ma
@ 2025-11-26  4:49 ` Huacai Chen
  2025-11-26  8:37   ` Qiang Ma
  2025-11-26  5:27 ` Hengqi Chen
  2025-11-26  9:47 ` Qiang Ma
  2 siblings, 1 reply; 8+ messages in thread
From: Huacai Chen @ 2025-11-26  4:49 UTC (permalink / raw)
  To: Qiang Ma; +Cc: kernel, loongarch, linux-kernel

Hi, Qiang,

On Wed, Nov 26, 2025 at 10:37 AM Qiang Ma <maqianga@uniontech.com> wrote:
>
> For thread_count, the current calculation method has a maximum of 255,
> which may not be sufficient in the future. Therefore, we are correcting
> it now.
I think you should contact the Loongson BIOS team to confirm they will do so.

And we can hardly meet the case that one socket contains more than 256
cores (this is different from the case MAX_CPUS > 256)

Huacai

>
> Reference: SMBIOS Specification, 7.5 Processor Information (Type 4)[1]
>
> [1]: https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.9.0.pdf
>
> Signed-off-by: Qiang Ma <maqianga@uniontech.com>
> ---
>  arch/loongarch/kernel/setup.c | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/arch/loongarch/kernel/setup.c b/arch/loongarch/kernel/setup.c
> index 25a87378e48e..760f5ce44384 100644
> --- a/arch/loongarch/kernel/setup.c
> +++ b/arch/loongarch/kernel/setup.c
> @@ -56,6 +56,7 @@
>  #define SMBIOS_FREQLOW_MASK            0xFF
>  #define SMBIOS_CORE_PACKAGE_OFFSET     0x23
>  #define SMBIOS_THREAD_PACKAGE_OFFSET   0x25
> +#define SMBIOS_THREAD_PACKAGE_2_OFFSET 0x2E
>  #define LOONGSON_EFI_ENABLE            (1 << 3)
>
>  unsigned long fw_arg0, fw_arg1, fw_arg2;
> @@ -120,13 +121,19 @@ static void __init parse_cpu_table(const struct dmi_header *dm)
>  {
>         long freq_temp = 0;
>         char *dmi_data = (char *)dm;
> +       u8 thread_count;
> +       u16 thread_count2;
>
>         freq_temp = ((*(dmi_data + SMBIOS_FREQHIGH_OFFSET) << 8) +
>                         ((*(dmi_data + SMBIOS_FREQLOW_OFFSET)) & SMBIOS_FREQLOW_MASK));
>         cpu_clock_freq = freq_temp * 1000000;
>
>         loongson_sysconf.cpuname = (void *)dmi_string_parse(dm, dmi_data[16]);
> -       loongson_sysconf.cores_per_package = *(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
> +       thread_count = *(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
> +       thread_count2 = *(u16 *)(dmi_data + SMBIOS_THREAD_PACKAGE_2_OFFSET);
> +       if (thread_count != 0)
> +               loongson_sysconf.cores_per_package = dm->length >= 0x30 && thread_count == 0xFF ?
> +                                                    thread_count2 : thread_count;
>
>         pr_info("CpuClock = %llu\n", cpu_clock_freq);
>  }
> --
> 2.20.1
>

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

* Re: [PATCH] LoongArch: Correct the calculation logic of thread_count
  2025-11-26  2:36 [PATCH] LoongArch: Correct the calculation logic of thread_count Qiang Ma
  2025-11-26  4:49 ` Huacai Chen
@ 2025-11-26  5:27 ` Hengqi Chen
  2025-11-26  9:47 ` Qiang Ma
  2 siblings, 0 replies; 8+ messages in thread
From: Hengqi Chen @ 2025-11-26  5:27 UTC (permalink / raw)
  To: Qiang Ma; +Cc: chenhuacai, kernel, loongarch, linux-kernel

On Wed, Nov 26, 2025 at 10:37 AM Qiang Ma <maqianga@uniontech.com> wrote:
>
> For thread_count, the current calculation method has a maximum of 255,
> which may not be sufficient in the future. Therefore, we are correcting
> it now.
>
> Reference: SMBIOS Specification, 7.5 Processor Information (Type 4)[1]
>
> [1]: https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.9.0.pdf
>
> Signed-off-by: Qiang Ma <maqianga@uniontech.com>
> ---
>  arch/loongarch/kernel/setup.c | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
>
> diff --git a/arch/loongarch/kernel/setup.c b/arch/loongarch/kernel/setup.c
> index 25a87378e48e..760f5ce44384 100644
> --- a/arch/loongarch/kernel/setup.c
> +++ b/arch/loongarch/kernel/setup.c
> @@ -56,6 +56,7 @@
>  #define SMBIOS_FREQLOW_MASK            0xFF
>  #define SMBIOS_CORE_PACKAGE_OFFSET     0x23
>  #define SMBIOS_THREAD_PACKAGE_OFFSET   0x25
> +#define SMBIOS_THREAD_PACKAGE_2_OFFSET 0x2E
>  #define LOONGSON_EFI_ENABLE            (1 << 3)
>
>  unsigned long fw_arg0, fw_arg1, fw_arg2;
> @@ -120,13 +121,19 @@ static void __init parse_cpu_table(const struct dmi_header *dm)
>  {
>         long freq_temp = 0;
>         char *dmi_data = (char *)dm;
> +       u8 thread_count;
> +       u16 thread_count2;
>
>         freq_temp = ((*(dmi_data + SMBIOS_FREQHIGH_OFFSET) << 8) +
>                         ((*(dmi_data + SMBIOS_FREQLOW_OFFSET)) & SMBIOS_FREQLOW_MASK));
>         cpu_clock_freq = freq_temp * 1000000;
>
>         loongson_sysconf.cpuname = (void *)dmi_string_parse(dm, dmi_data[16]);
> -       loongson_sysconf.cores_per_package = *(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
> +       thread_count = *(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
> +       thread_count2 = *(u16 *)(dmi_data + SMBIOS_THREAD_PACKAGE_2_OFFSET);

You have `dm->length >= 0x30` below but the memory load already done here.

> +       if (thread_count != 0)
> +               loongson_sysconf.cores_per_package = dm->length >= 0x30 && thread_count == 0xFF ?
> +                                                    thread_count2 : thread_count;
>
>         pr_info("CpuClock = %llu\n", cpu_clock_freq);
>  }
> --
> 2.20.1
>
>

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

* Re: [PATCH] LoongArch: Correct the calculation logic of thread_count
  2025-11-26  4:49 ` Huacai Chen
@ 2025-11-26  8:37   ` Qiang Ma
  0 siblings, 0 replies; 8+ messages in thread
From: Qiang Ma @ 2025-11-26  8:37 UTC (permalink / raw)
  To: Huacai Chen; +Cc: kernel, loongarch, linux-kernel


在 2025/11/26 12:49, Huacai Chen 写道:
> Hi, Qiang,
>
> On Wed, Nov 26, 2025 at 10:37 AM Qiang Ma <maqianga@uniontech.com> wrote:
>> For thread_count, the current calculation method has a maximum of 255,
>> which may not be sufficient in the future. Therefore, we are correcting
>> it now.
> I think you should contact the Loongson BIOS team to confirm they will do so.

I contacted Li Chao from the Loongson BIOS team and had a brief 
communication.

bios will do this when it exceeds 256.

>
> And we can hardly meet the case that one socket contains more than 256
> cores (this is different from the case MAX_CPUS > 256)
According to Li Chao, the 256 logic core on a single chip should arrive 
in the next generation.
> Huacai
>
>> Reference: SMBIOS Specification, 7.5 Processor Information (Type 4)[1]
>>
>> [1]: https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.9.0.pdf
>>
>> Signed-off-by: Qiang Ma <maqianga@uniontech.com>
>> ---
>>   arch/loongarch/kernel/setup.c | 9 ++++++++-
>>   1 file changed, 8 insertions(+), 1 deletion(-)
>>
>> diff --git a/arch/loongarch/kernel/setup.c b/arch/loongarch/kernel/setup.c
>> index 25a87378e48e..760f5ce44384 100644
>> --- a/arch/loongarch/kernel/setup.c
>> +++ b/arch/loongarch/kernel/setup.c
>> @@ -56,6 +56,7 @@
>>   #define SMBIOS_FREQLOW_MASK            0xFF
>>   #define SMBIOS_CORE_PACKAGE_OFFSET     0x23
>>   #define SMBIOS_THREAD_PACKAGE_OFFSET   0x25
>> +#define SMBIOS_THREAD_PACKAGE_2_OFFSET 0x2E
>>   #define LOONGSON_EFI_ENABLE            (1 << 3)
>>
>>   unsigned long fw_arg0, fw_arg1, fw_arg2;
>> @@ -120,13 +121,19 @@ static void __init parse_cpu_table(const struct dmi_header *dm)
>>   {
>>          long freq_temp = 0;
>>          char *dmi_data = (char *)dm;
>> +       u8 thread_count;
>> +       u16 thread_count2;
>>
>>          freq_temp = ((*(dmi_data + SMBIOS_FREQHIGH_OFFSET) << 8) +
>>                          ((*(dmi_data + SMBIOS_FREQLOW_OFFSET)) & SMBIOS_FREQLOW_MASK));
>>          cpu_clock_freq = freq_temp * 1000000;
>>
>>          loongson_sysconf.cpuname = (void *)dmi_string_parse(dm, dmi_data[16]);
>> -       loongson_sysconf.cores_per_package = *(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
>> +       thread_count = *(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
>> +       thread_count2 = *(u16 *)(dmi_data + SMBIOS_THREAD_PACKAGE_2_OFFSET);
>> +       if (thread_count != 0)
>> +               loongson_sysconf.cores_per_package = dm->length >= 0x30 && thread_count == 0xFF ?
>> +                                                    thread_count2 : thread_count;
>>
>>          pr_info("CpuClock = %llu\n", cpu_clock_freq);
>>   }
>> --
>> 2.20.1
>>


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

* Re: [PATCH] LoongArch: Correct the calculation logic of thread_count
  2025-11-26  2:36 [PATCH] LoongArch: Correct the calculation logic of thread_count Qiang Ma
  2025-11-26  4:49 ` Huacai Chen
  2025-11-26  5:27 ` Hengqi Chen
@ 2025-11-26  9:47 ` Qiang Ma
  2025-11-26 10:06   ` Hengqi Chen
  2 siblings, 1 reply; 8+ messages in thread
From: Qiang Ma @ 2025-11-26  9:47 UTC (permalink / raw)
  To: Hengqi Chen; +Cc: loongarch, linux-kernel, Huacai Chen, kernel


On Wed, Nov 26, 2025 at 10:37 AM Qiang Ma<maqianga@uniontech.com> wrote:

>   For thread_count, the current calculation method has a maximum of 255,
>   which may not be sufficient in the future. Therefore, we are correcting
>   it now.
>
>   Reference: SMBIOS Specification, 7.5 Processor Information (Type 4)[1]
>
>   [1]:https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.9.0.pdf
>
>   Signed-off-by: Qiang Ma<maqianga@uniontech.com>
>   ---
>    arch/loongarch/kernel/setup.c | 9 ++++++++-
>    1 file changed, 8 insertions(+), 1 deletion(-)
>
>   diff --git a/arch/loongarch/kernel/setup.c b/arch/loongarch/kernel/setup.c
>   index 25a87378e48e..760f5ce44384 100644
>   --- a/arch/loongarch/kernel/setup.c
>   +++ b/arch/loongarch/kernel/setup.c
>   @@ -56,6 +56,7 @@
>    #define SMBIOS_FREQLOW_MASK            0xFF
>    #define SMBIOS_CORE_PACKAGE_OFFSET     0x23
>    #define SMBIOS_THREAD_PACKAGE_OFFSET   0x25
>   +#define SMBIOS_THREAD_PACKAGE_2_OFFSET 0x2E
>    #define LOONGSON_EFI_ENABLE            (1 << 3)
>
>    unsigned long fw_arg0, fw_arg1, fw_arg2;
>   @@ -120,13 +121,19 @@ static void __init parse_cpu_table(const struct dmi_header *dm)
>    {
>           long freq_temp = 0;
>           char *dmi_data = (char *)dm;
>   +       u8 thread_count;
>   +       u16 thread_count2;
>
>           freq_temp = ((*(dmi_data + SMBIOS_FREQHIGH_OFFSET) << 8) +
>                           ((*(dmi_data + SMBIOS_FREQLOW_OFFSET)) & SMBIOS_FREQLOW_MASK));
>           cpu_clock_freq = freq_temp * 1000000;
>
>           loongson_sysconf.cpuname = (void *)dmi_string_parse(dm, dmi_data[16]);
>   -       loongson_sysconf.cores_per_package = *(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
>   +       thread_count = *(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
>   +       thread_count2 = *(u16 *)(dmi_data + SMBIOS_THREAD_PACKAGE_2_OFFSET);
>
> You have `dm->length >= 0x30` below but the memory load already done here.
After reading thread_count2, it cannot be guaranteed that the data is valid.

'dm->length >= 0x30' can detect that thread_count2 is an available data, 
right?


>   +       if (thread_count != 0)
>   +               loongson_sysconf.cores_per_package = dm->length >= 0x30 && thread_count == 0xFF ?
>   +                                                    thread_count2 : thread_count;
>
>           pr_info("CpuClock = %llu\n", cpu_clock_freq);
>    }
>   --
>   2.20.1

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

* Re: [PATCH] LoongArch: Correct the calculation logic of thread_count
  2025-11-26  9:47 ` Qiang Ma
@ 2025-11-26 10:06   ` Hengqi Chen
  2025-11-26 10:14     ` Qiang Ma
       [not found]     ` <d28a0011-5b9a-4e3e-a517-0c6d8933adae@uniontech.com>
  0 siblings, 2 replies; 8+ messages in thread
From: Hengqi Chen @ 2025-11-26 10:06 UTC (permalink / raw)
  To: Qiang Ma; +Cc: loongarch, linux-kernel, Huacai Chen, kernel

On Wed, Nov 26, 2025 at 5:47 PM Qiang Ma <maqianga@uniontech.com> wrote:
>
>
> On Wed, Nov 26, 2025 at 10:37 AM Qiang Ma<maqianga@uniontech.com> wrote:
>
> >   For thread_count, the current calculation method has a maximum of 255,
> >   which may not be sufficient in the future. Therefore, we are correcting
> >   it now.
> >
> >   Reference: SMBIOS Specification, 7.5 Processor Information (Type 4)[1]
> >
> >   [1]:https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.9.0.pdf
> >
> >   Signed-off-by: Qiang Ma<maqianga@uniontech.com>
> >   ---
> >    arch/loongarch/kernel/setup.c | 9 ++++++++-
> >    1 file changed, 8 insertions(+), 1 deletion(-)
> >
> >   diff --git a/arch/loongarch/kernel/setup.c b/arch/loongarch/kernel/setup.c
> >   index 25a87378e48e..760f5ce44384 100644
> >   --- a/arch/loongarch/kernel/setup.c
> >   +++ b/arch/loongarch/kernel/setup.c
> >   @@ -56,6 +56,7 @@
> >    #define SMBIOS_FREQLOW_MASK            0xFF
> >    #define SMBIOS_CORE_PACKAGE_OFFSET     0x23
> >    #define SMBIOS_THREAD_PACKAGE_OFFSET   0x25
> >   +#define SMBIOS_THREAD_PACKAGE_2_OFFSET 0x2E
> >    #define LOONGSON_EFI_ENABLE            (1 << 3)
> >
> >    unsigned long fw_arg0, fw_arg1, fw_arg2;
> >   @@ -120,13 +121,19 @@ static void __init parse_cpu_table(const struct dmi_header *dm)
> >    {
> >           long freq_temp = 0;
> >           char *dmi_data = (char *)dm;
> >   +       u8 thread_count;
> >   +       u16 thread_count2;
> >
> >           freq_temp = ((*(dmi_data + SMBIOS_FREQHIGH_OFFSET) << 8) +
> >                           ((*(dmi_data + SMBIOS_FREQLOW_OFFSET)) & SMBIOS_FREQLOW_MASK));
> >           cpu_clock_freq = freq_temp * 1000000;
> >
> >           loongson_sysconf.cpuname = (void *)dmi_string_parse(dm, dmi_data[16]);
> >   -       loongson_sysconf.cores_per_package = *(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
> >   +       thread_count = *(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
> >   +       thread_count2 = *(u16 *)(dmi_data + SMBIOS_THREAD_PACKAGE_2_OFFSET);
> >
> > You have `dm->length >= 0x30` below but the memory load already done here.
> After reading thread_count2, it cannot be guaranteed that the data is valid.
>
> 'dm->length >= 0x30' can detect that thread_count2 is an available data,
> right?
>

I mean you should perform the check before doing memory load.

>
> >   +       if (thread_count != 0)
> >   +               loongson_sysconf.cores_per_package = dm->length >= 0x30 && thread_count == 0xFF ?
> >   +                                                    thread_count2 : thread_count;
> >
> >           pr_info("CpuClock = %llu\n", cpu_clock_freq);
> >    }
> >   --
> >   2.20.1

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

* Re: [PATCH] LoongArch: Correct the calculation logic of thread_count
  2025-11-26 10:06   ` Hengqi Chen
@ 2025-11-26 10:14     ` Qiang Ma
       [not found]     ` <d28a0011-5b9a-4e3e-a517-0c6d8933adae@uniontech.com>
  1 sibling, 0 replies; 8+ messages in thread
From: Qiang Ma @ 2025-11-26 10:14 UTC (permalink / raw)
  To: Hengqi Chen; +Cc: loongarch, linux-kernel, Huacai Chen, kernel


在 2025/11/26 18:06, Hengqi Chen 写道:
> On Wed, Nov 26, 2025 at 5:47 PM Qiang Ma <maqianga@uniontech.com> wrote:
>>
>> On Wed, Nov 26, 2025 at 10:37 AM Qiang Ma<maqianga@uniontech.com> wrote:
>>
>>>    For thread_count, the current calculation method has a maximum of 255,
>>>    which may not be sufficient in the future. Therefore, we are correcting
>>>    it now.
>>>
>>>    Reference: SMBIOS Specification, 7.5 Processor Information (Type 4)[1]
>>>
>>>    [1]:https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.9.0.pdf
>>>
>>>    Signed-off-by: Qiang Ma<maqianga@uniontech.com>
>>>    ---
>>>     arch/loongarch/kernel/setup.c | 9 ++++++++-
>>>     1 file changed, 8 insertions(+), 1 deletion(-)
>>>
>>>    diff --git a/arch/loongarch/kernel/setup.c b/arch/loongarch/kernel/setup.c
>>>    index 25a87378e48e..760f5ce44384 100644
>>>    --- a/arch/loongarch/kernel/setup.c
>>>    +++ b/arch/loongarch/kernel/setup.c
>>>    @@ -56,6 +56,7 @@
>>>     #define SMBIOS_FREQLOW_MASK            0xFF
>>>     #define SMBIOS_CORE_PACKAGE_OFFSET     0x23
>>>     #define SMBIOS_THREAD_PACKAGE_OFFSET   0x25
>>>    +#define SMBIOS_THREAD_PACKAGE_2_OFFSET 0x2E
>>>     #define LOONGSON_EFI_ENABLE            (1 << 3)
>>>
>>>     unsigned long fw_arg0, fw_arg1, fw_arg2;
>>>    @@ -120,13 +121,19 @@ static void __init parse_cpu_table(const struct dmi_header *dm)
>>>     {
>>>            long freq_temp = 0;
>>>            char *dmi_data = (char *)dm;
>>>    +       u8 thread_count;
>>>    +       u16 thread_count2;
>>>
>>>            freq_temp = ((*(dmi_data + SMBIOS_FREQHIGH_OFFSET) << 8) +
>>>                            ((*(dmi_data + SMBIOS_FREQLOW_OFFSET)) & SMBIOS_FREQLOW_MASK));
>>>            cpu_clock_freq = freq_temp * 1000000;
>>>
>>>            loongson_sysconf.cpuname = (void *)dmi_string_parse(dm, dmi_data[16]);
>>>    -       loongson_sysconf.cores_per_package = *(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
>>>    +       thread_count = *(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
>>>    +       thread_count2 = *(u16 *)(dmi_data + SMBIOS_THREAD_PACKAGE_2_OFFSET);
>>>
>>> You have `dm->length >= 0x30` below but the memory load already done here.
>> After reading thread_count2, it cannot be guaranteed that the data is valid.
>>
>> 'dm->length >= 0x30' can detect that thread_count2 is an available data,
>> right?
>>
> I mean you should perform the check before doing memory load.
It doesn't matter whether it's before or after.

Anyway, here it's just reading. Before it's actually used later,
it will check this length.
>>>    +       if (thread_count != 0)
>>>    +               loongson_sysconf.cores_per_package = dm->length >= 0x30 && thread_count == 0xFF ?
>>>    +                                                    thread_count2 : thread_count;
>>>
>>>            pr_info("CpuClock = %llu\n", cpu_clock_freq);
>>>     }
>>>    --
>>>    2.20.1


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

* Re: [PATCH] LoongArch: Correct the calculation logic of thread_count
       [not found]     ` <d28a0011-5b9a-4e3e-a517-0c6d8933adae@uniontech.com>
@ 2025-11-26 11:06       ` Qiang Ma
  0 siblings, 0 replies; 8+ messages in thread
From: Qiang Ma @ 2025-11-26 11:06 UTC (permalink / raw)
  To: Hengqi Chen; +Cc: loongarch, linux-kernel, Huacai Chen, kernel


在 2025/11/26 18:14, Qiang Ma 写道:
>
> 在 2025/11/26 18:06, Hengqi Chen 写道:
>> On Wed, Nov 26, 2025 at 5:47 PM Qiang Ma <maqianga@uniontech.com> wrote:
>>>
>>> On Wed, Nov 26, 2025 at 10:37 AM Qiang Ma<maqianga@uniontech.com> 
>>> wrote:
>>>
>>>>    For thread_count, the current calculation method has a maximum 
>>>> of 255,
>>>>    which may not be sufficient in the future. Therefore, we are 
>>>> correcting
>>>>    it now.
>>>>
>>>>    Reference: SMBIOS Specification, 7.5 Processor Information (Type 
>>>> 4)[1]
>>>>
>>>> [1]:https://www.dmtf.org/sites/default/files/standards/documents/DSP0134_3.9.0.pdf
>>>>
>>>>    Signed-off-by: Qiang Ma<maqianga@uniontech.com>
>>>>    ---
>>>>     arch/loongarch/kernel/setup.c | 9 ++++++++-
>>>>     1 file changed, 8 insertions(+), 1 deletion(-)
>>>>
>>>>    diff --git a/arch/loongarch/kernel/setup.c 
>>>> b/arch/loongarch/kernel/setup.c
>>>>    index 25a87378e48e..760f5ce44384 100644
>>>>    --- a/arch/loongarch/kernel/setup.c
>>>>    +++ b/arch/loongarch/kernel/setup.c
>>>>    @@ -56,6 +56,7 @@
>>>>     #define SMBIOS_FREQLOW_MASK            0xFF
>>>>     #define SMBIOS_CORE_PACKAGE_OFFSET     0x23
>>>>     #define SMBIOS_THREAD_PACKAGE_OFFSET   0x25
>>>>    +#define SMBIOS_THREAD_PACKAGE_2_OFFSET 0x2E
>>>>     #define LOONGSON_EFI_ENABLE            (1 << 3)
>>>>
>>>>     unsigned long fw_arg0, fw_arg1, fw_arg2;
>>>>    @@ -120,13 +121,19 @@ static void __init parse_cpu_table(const 
>>>> struct dmi_header *dm)
>>>>     {
>>>>            long freq_temp = 0;
>>>>            char *dmi_data = (char *)dm;
>>>>    +       u8 thread_count;
>>>>    +       u16 thread_count2;
>>>>
>>>>            freq_temp = ((*(dmi_data + SMBIOS_FREQHIGH_OFFSET) << 8) +
>>>>                            ((*(dmi_data + SMBIOS_FREQLOW_OFFSET)) & 
>>>> SMBIOS_FREQLOW_MASK));
>>>>            cpu_clock_freq = freq_temp * 1000000;
>>>>
>>>>            loongson_sysconf.cpuname = (void *)dmi_string_parse(dm, 
>>>> dmi_data[16]);
>>>>    -       loongson_sysconf.cores_per_package = *(dmi_data + 
>>>> SMBIOS_THREAD_PACKAGE_OFFSET);
>>>>    +       thread_count = *(dmi_data + SMBIOS_THREAD_PACKAGE_OFFSET);
>>>>    +       thread_count2 = *(u16 *)(dmi_data + 
>>>> SMBIOS_THREAD_PACKAGE_2_OFFSET);
>>>>
>>>> You have `dm->length >= 0x30` below but the memory load already 
>>>> done here.
>>> After reading thread_count2, it cannot be guaranteed that the data 
>>> is valid.
>>>
>>> 'dm->length >= 0x30' can detect that thread_count2 is an available 
>>> data,
>>> right?
>>>
>> I mean you should perform the check before doing memory load.
> It doesn't matter whether it's before or after.
My understanding was wrong. I will fix it in v2.
>
> Anyway, here it's just reading. Before it's actually used later,
> it will check this length.
>>>>    +       if (thread_count != 0)
>>>>    +               loongson_sysconf.cores_per_package = dm->length 
>>>> >= 0x30 && thread_count == 0xFF ?
>>>>    + thread_count2 : thread_count;
>>>>
>>>>            pr_info("CpuClock = %llu\n", cpu_clock_freq);
>>>>     }
>>>>    --
>>>>    2.20.1
>
>

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

end of thread, other threads:[~2025-11-26 11:07 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2025-11-26  2:36 [PATCH] LoongArch: Correct the calculation logic of thread_count Qiang Ma
2025-11-26  4:49 ` Huacai Chen
2025-11-26  8:37   ` Qiang Ma
2025-11-26  5:27 ` Hengqi Chen
2025-11-26  9:47 ` Qiang Ma
2025-11-26 10:06   ` Hengqi Chen
2025-11-26 10:14     ` Qiang Ma
     [not found]     ` <d28a0011-5b9a-4e3e-a517-0c6d8933adae@uniontech.com>
2025-11-26 11:06       ` Qiang Ma

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

all inboxes | Powered by JetHome®