linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Borislav Petkov <bp@alien8.de>
To: X86 ML <x86@kernel.org>
Cc: Yazen Ghannam <Yazen.Ghannam@amd.com>,
	LKML <linux-kernel@vger.kernel.org>,
	Rafael Kitover <rkitover@gmail.com>,
	Johnathan Smithinovic <johnathan.smithinovic@gmx.at>
Subject: [RFC PATCH] x86/cpu: Do not check c->initialized in topology_phys_to_logical_die()
Date: Tue,  5 Jan 2021 12:34:14 +0100	[thread overview]
Message-ID: <20210105113414.3277-1-bp@alien8.de> (raw)

From: Borislav Petkov <bp@suse.de>

During boot, identify_secondary_cpu() calls at some point
validate_apic_and_package_id() which calls topology_update_die_map() to
update/verify the physical to logical DIE map of the CPUs on the system.

There's a call down that path to topology_phys_to_logical_die() which
maps a physical die to a logical one. The check in there looks at
cpuinfo_x86.initialized first before comparing die_ids and proc_ids.

And this is where the problem lies: both ->cpu_die_id and ->phys_proc_id
have been initialized as part of the identify_secondary_cpu() dance -
just the cpuinfo_x86.initialized thing hasn't been set yet (it gets set
as the last thing in smp_store_cpu_info()).

So what that means is that initialized fields are being compared but the
initialized flag says they're not, leading to:

  smpboot: topology_phys_to_logical_die: init: 1, cpu 7, cur_cpu: 8, cpu_die_id: 0, die_id: 2, phys_proc_id: 0, proc_id: 0, logical_die_id: 0
  smpboot: topology_phys_to_logical_die: init: 0, cpu 8, cur_cpu: 8, cpu_die_id: 2, die_id: 2, phys_proc_id: 0, proc_id: 0, logical_die_id: 0
  ...
  smpboot: topology_phys_to_logical_die: init: 0, cpu 127, cur_cpu: 8, cpu_die_id: 0, die_id: 2, phys_proc_id: 0, proc_id: 0, logical_die_id: 0
  smpboot: CPU 8 Converting physical 2 to logical die 1

On CPU8 and all the way up to all possible_cpus, boot_cpu_data is not
initialized yet even though

  cpu_die_id == die_id
&&
  phys_proc_id == proc_id

for that CPU 8.

As a result, topology_update_die_map() increments logical_die which gets
written into cpuinfo_x86.logical_die_id of that CPU.

Later, in the RAPL code, that logical_die_id is outside of the range of
maximum dies present on the system:

  int maxdie = topology_max_packages() * topology_max_die_per_package();

which leads to indexing into the rapl_pmus->pmus[] array out of bounds.
Boom.

Thus, drop the c->initialized check because the values it should protect
against checking, have been actually already initialized. (Yes, our boot
order is fragile. :-\).

Reported-by: Rafael Kitover <rkitover@gmail.com>
Reported-by: Johnathan Smithinovic <johnathan.smithinovic@gmx.at>
Signed-off-by: Borislav Petkov <bp@suse.de>
Link: https://bugzilla.kernel.org/show_bug.cgi?id=210939
---
 arch/x86/kernel/smpboot.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/arch/x86/kernel/smpboot.c b/arch/x86/kernel/smpboot.c
index 8ca66af96a54..56d2ac8c54ab 100644
--- a/arch/x86/kernel/smpboot.c
+++ b/arch/x86/kernel/smpboot.c
@@ -319,7 +319,7 @@ int topology_phys_to_logical_die(unsigned int die_id, unsigned int cur_cpu)
 	for_each_possible_cpu(cpu) {
 		struct cpuinfo_x86 *c = &cpu_data(cpu);
 
-		if (c->initialized && c->cpu_die_id == die_id &&
+		if (c->cpu_die_id == die_id &&
 		    c->phys_proc_id == proc_id)
 			return c->logical_die_id;
 	}
-- 
2.29.2


             reply	other threads:[~2021-01-05 11:35 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-01-05 11:34 Borislav Petkov [this message]
2021-01-06 11:21 ` [RFC PATCH] x86/cpu: Do not check c->initialized in topology_phys_to_logical_die() Borislav Petkov
2021-01-12 11:29   ` [tip: x86/urgent] x86/cpu/amd: Set __max_die_per_package on AMD tip-bot2 for Yazen Ghannam
2021-01-10 14:54 ` [x86/cpu] 3756dbdf6f: WARNING:at_arch/x86/events/intel/uncore.c:#uncore_change_type_ctx[intel_uncore] kernel test robot

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210105113414.3277-1-bp@alien8.de \
    --to=bp@alien8.de \
    --cc=Yazen.Ghannam@amd.com \
    --cc=johnathan.smithinovic@gmx.at \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rkitover@gmail.com \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).