Re: [PATCH v2 4/6] x86/microcode/intel: Implement staging handler

From: Dave Hansen
Date: Thu Mar 20 2025 - 20:16:08 EST


On 3/20/25 16:40, Chang S. Bae wrote:
> static void do_stage(u64 mmio_pa)
> {
> - pr_debug_once("Staging implementation is pending.\n");
> - staging.state = UCODE_ERROR;
> + staging.mmio_base = ioremap(mmio_pa, MBOX_REG_NUM * MBOX_REG_SIZE);
> + if (WARN_ON_ONCE(!staging.mmio_base)) {
> + staging.state = UCODE_ERROR;
> + return;
> + }
> +
> + reset_stage();

This is purely a style thing, but I really dislike using global
variables like this.

What is reset_stage() doing? What state is it resetting? What is locking
this? What is its scope?

Now, imagine you have a function variable. It can be on the stack or static:

static void do_stage(u64 mmio_pa)
{
struct staging_state staging = {};

Then you pass it around:

staging.mmio_base = ioremap(mmio_pa, MBOX_REG_NUM * ...
if (WARN_ON_ONCE(!staging.mmio_base)) {
staging.state = UCODE_ERROR;
return;
}

reset_stage(&staging);

Yeah, it means passing around _one_ function argument to a function
that's not currently taking an argument. But it makes it a heck of a lot
more clear what is going on. It also makes the lifetime and
initialization state of 'staging' *MUCH* more obvious.

It also makes it clear what state reset_stage() needs to do its job.