The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: "Ahmed S. Darwish" <darwi@linutronix.de>
To: Borislav Petkov <bp@alien8.de>
Cc: Dave Hansen <dave.hansen@linux.intel.com>,
	Ingo Molnar <mingo@redhat.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Andrew Cooper <andrew.cooper3@citrix.com>,
	"H. Peter Anvin" <hpa@zytor.com>,
	Sean Christopherson <seanjc@google.com>,
	David Woodhouse <dwmw2@infradead.org>,
	Peter Zijlstra <peterz@infradead.org>,
	Christian Ludloff <ludloff@gmail.com>,
	Sohil Mehta <sohil.mehta@intel.com>,
	John Ogness <john.ogness@linutronix.de>,
	x86@kernel.org, x86-cpuid@lists.linux.dev,
	LKML <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v6 10/90] x86/cpu: Rescan CPUID table after disabling PSN
Date: Wed, 13 May 2026 17:57:13 +0200	[thread overview]
Message-ID: <agSfWTxs9pRPHJxl@lx-t490> (raw)
In-Reply-To: <20260512143412.GDagM6ZLBpvt6X3jzq@fat_crate.local>

On Tue, 12 May 2026, Borislav Petkov wrote:
>
> On Tue, May 12, 2026 at 09:12:25AM +0200, Ahmed S. Darwish wrote:
> > So the min_t()'s intent is just to be defensive against hardware surprises.
>
> But when you read l0->max_std_leaf, you always get the current, highest base
> level. So there's nothing to protect against.
>
> Or am I missing something?
>

So, let's imagine the following cases of cached CPUID tables.  The changes
to the new max CPUID are due to MSR writes.

* First case:

           leaf 0x0
	   leaf 0x1
	   leaf 0x2     <- Old max CPUID
	   leaf 0x3
	   leaf 0x4
	   leaf 0x5
	   leaf 0x6
	   leaf 0x7
	   leaf 0x9     <- *New* max CPUID

=> Here, the parser code needs to leave CPUID(0x0) and CPUID(0x1) untouched.

That's especially true since CPUID(0x1) holds the backing for some
X86_FEATURE words, other flags might be force set or unset, etc.  So we
don't need to touch that not to corrupt the state of force-enabled or
disabled X86_FEATURE bits.

(Then, it needs to fill the table entries for CPUID(0x3) to CPUID(0x9), but
that's obvious.)

This is accomplished the logic:

	rescan_from = min_t(int, l0->max_std_leaf, c->cpuid_level) + 1;
	cpuid_refresh_range(c, rescan_from, CPUID_BASE_END);

Since it will only begin filling things from CPUID(0x3).

* Second case:

           leaf 0x0
	   leaf 0x1
	   leaf 0x2     <- *New* max CPUID
	   leaf 0x3
	   leaf 0x4	<- Old max CPUID

=> Here, the parser will need to zero CPUID(0x3) and CPUID(0x4) entries.

This is because the CPUID API query macros at <asm/cpuid/api.h> know the
validity of each entry through its nr_entries flag:

struct leaf_parse_info {
	unsigned int		nr_entries;
};

And without the zeroing of entries, the CPUID API will return invalid and
stale values for CPUID(0x3) and CPUID(0x4), instead of returning the right
value: NULL.

This is accomplished the logic:

	rescan_from = min_t(int, l0->max_std_leaf, c->cpuid_level) + 1;
	cpuid_refresh_range(c, rescan_from, CPUID_BASE_END);

Since it will zero CPUID(0x3) and CPUID(0x4), as it is part of the
cpuid_refresh_range() logic.

And that's what I meant that the min_t() logic handles hardware surprises:
it continues to work, regardless if the new max CPUID is higher or lower
after the MSR write.

I guess I should've put this min_t() logic in its own cpuid_parser.c
function, with proper comments about this.  There were only two cases for
it, this patch and patch 13/90, but two call sites are enough for a parser
API function.

Thanks,
Ahmed

  parent reply	other threads:[~2026-05-13 15:57 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20260327021645.555257-1-darwi@linutronix.de>
     [not found] ` <20260327152354.GBacahCioljpw5QqUc@fat_crate.local>
     [not found]   ` <acrBBFyU_vi4zFOx@lx-t490>
     [not found]     ` <20260330230836.GLacsCdDkVu0H3XU4l@fat_crate.local>
     [not found]       ` <adz3z_MaWJr_GOtc@lx-t490>
     [not found]         ` <ae-uw9836AJScRsq@lx-t490>
2026-05-05 13:33           ` [PATCH v6 00/90] x86: Introduce a centralized CPUID data model Borislav Petkov
2026-05-05 15:12             ` Borislav Petkov
2026-05-05 19:11               ` Christian Ludloff
2026-05-06  8:50                 ` Borislav Petkov
2026-05-06 13:59                   ` H. Peter Anvin
2026-05-06 14:58                     ` Borislav Petkov
2026-05-06 18:13                   ` Christian Ludloff
2026-05-06 21:57                 ` Ahmed S. Darwish
2026-05-06 22:18                   ` Borislav Petkov
2026-05-06 23:03                     ` Christian Ludloff
2026-05-06 20:52               ` Ahmed S. Darwish
2026-05-07 10:17                 ` Borislav Petkov
2026-05-07 20:02                   ` Ahmed S. Darwish
     [not found] ` <20260327021645.555257-11-darwi@linutronix.de>
2026-05-11 20:00   ` [PATCH v6 10/90] x86/cpu: Rescan CPUID table after disabling PSN Borislav Petkov
2026-05-12  7:12     ` Ahmed S. Darwish
2026-05-12 14:34       ` Borislav Petkov
2026-05-13 15:27         ` Borislav Petkov
2026-05-13 16:06           ` Ahmed S. Darwish
2026-05-13 16:51             ` Borislav Petkov
2026-05-13 17:09               ` Borislav Petkov
2026-05-13 17:21                 ` H. Peter Anvin
2026-05-13 17:25               ` Ahmed S. Darwish
2026-05-13 17:38                 ` Borislav Petkov
2026-05-13 17:48                   ` Ahmed S. Darwish
2026-05-13 15:57         ` Ahmed S. Darwish [this message]
2026-05-13 16:25           ` Ahmed S. Darwish

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=agSfWTxs9pRPHJxl@lx-t490 \
    --to=darwi@linutronix.de \
    --cc=andrew.cooper3@citrix.com \
    --cc=bp@alien8.de \
    --cc=dave.hansen@linux.intel.com \
    --cc=dwmw2@infradead.org \
    --cc=hpa@zytor.com \
    --cc=john.ogness@linutronix.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ludloff@gmail.com \
    --cc=mingo@redhat.com \
    --cc=peterz@infradead.org \
    --cc=seanjc@google.com \
    --cc=sohil.mehta@intel.com \
    --cc=tglx@linutronix.de \
    --cc=x86-cpuid@lists.linux.dev \
    --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