Skip to content

Commit

Permalink
tools/power x86_energy_perf_policy: Fix "uninitialized variable" warn…
Browse files Browse the repository at this point in the history
…ings at -O2

x86_energy_perf_policy first uses __get_cpuid() to check the maximum
CPUID level and exits if it is too low.  It then assumes that later
calls will succeed (which I think is architecturally guaranteed).  It
also assumes that CPUID works at all (which is not guaranteed on
x86_32).

If optimisations are enabled, gcc warns about potentially
uninitialized variables.  Fix this by adding an exit-on-error after
every call to __get_cpuid() instead of just checking the maximum
level.

Signed-off-by: Ben Hutchings <[email protected]>
Signed-off-by: Len Brown <[email protected]>
  • Loading branch information
bwhacks authored and lenb committed Aug 31, 2019
1 parent a55aa89 commit adb8049
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions tools/power/x86/x86_energy_perf_policy/x86_energy_perf_policy.c
Original file line number Diff line number Diff line change
Expand Up @@ -1259,22 +1259,26 @@ void probe_dev_msr(void)
if (system("/sbin/modprobe msr > /dev/null 2>&1"))
err(-5, "no /dev/cpu/0/msr, Try \"# modprobe msr\" ");
}

static void get_cpuid_or_exit(unsigned int leaf,
unsigned int *eax, unsigned int *ebx,
unsigned int *ecx, unsigned int *edx)
{
if (!__get_cpuid(leaf, eax, ebx, ecx, edx))
errx(1, "Processor not supported\n");
}

/*
* early_cpuid()
* initialize turbo_is_enabled, has_hwp, has_epb
* before cmdline is parsed
*/
void early_cpuid(void)
{
unsigned int eax, ebx, ecx, edx, max_level;
unsigned int eax, ebx, ecx, edx;
unsigned int fms, family, model;

__get_cpuid(0, &max_level, &ebx, &ecx, &edx);

if (max_level < 6)
errx(1, "Processor not supported\n");

__get_cpuid(1, &fms, &ebx, &ecx, &edx);
get_cpuid_or_exit(1, &fms, &ebx, &ecx, &edx);
family = (fms >> 8) & 0xf;
model = (fms >> 4) & 0xf;
if (family == 6 || family == 0xf)
Expand All @@ -1288,7 +1292,7 @@ void early_cpuid(void)
bdx_highest_ratio = msr & 0xFF;
}

__get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
get_cpuid_or_exit(0x6, &eax, &ebx, &ecx, &edx);
turbo_is_enabled = (eax >> 1) & 1;
has_hwp = (eax >> 7) & 1;
has_epb = (ecx >> 3) & 1;
Expand All @@ -1306,7 +1310,7 @@ void parse_cpuid(void)

eax = ebx = ecx = edx = 0;

__get_cpuid(0, &max_level, &ebx, &ecx, &edx);
get_cpuid_or_exit(0, &max_level, &ebx, &ecx, &edx);

if (ebx == 0x756e6547 && edx == 0x49656e69 && ecx == 0x6c65746e)
genuine_intel = 1;
Expand All @@ -1315,7 +1319,7 @@ void parse_cpuid(void)
fprintf(stderr, "CPUID(0): %.4s%.4s%.4s ",
(char *)&ebx, (char *)&edx, (char *)&ecx);

__get_cpuid(1, &fms, &ebx, &ecx, &edx);
get_cpuid_or_exit(1, &fms, &ebx, &ecx, &edx);
family = (fms >> 8) & 0xf;
model = (fms >> 4) & 0xf;
stepping = fms & 0xf;
Expand All @@ -1340,7 +1344,7 @@ void parse_cpuid(void)
errx(1, "CPUID: no MSR");


__get_cpuid(0x6, &eax, &ebx, &ecx, &edx);
get_cpuid_or_exit(0x6, &eax, &ebx, &ecx, &edx);
/* turbo_is_enabled already set */
/* has_hwp already set */
has_hwp_notify = eax & (1 << 8);
Expand Down

0 comments on commit adb8049

Please sign in to comment.