On Mon, Apr 28, 2025 at 03:05:55PM -0700, Dave Hansen wrote:
> On 4/10/25 22:37, Changyuan Lyu wrote:
> > From: Alexander Graf <[email protected]>
> > 
> > +#ifdef CONFIG_KEXEC_HANDOVER
> > +static bool process_kho_entries(unsigned long minimum, unsigned long 
> > image_size)
> > +{
> > +   struct kho_scratch *kho_scratch;
> > +   struct setup_data *ptr;
> > +   int i, nr_areas = 0;
> 
> Do these really need actual #ifdefs or will a nice IS_ENABLED() check
> work instead?
> 
> > +   ptr = (struct setup_data *)(unsigned 
> > long)boot_params_ptr->hdr.setup_data;
> 
> What's with the double cast?

The double cast is required for this to be compiled on 32 bits (just like
in mem_avoid_overlap). The setup_data is all u64 and to cast it to a
pointer on 32 bit it has to go via unsigned long.

If we keep actual #ifdef we can drop the double cast because KHO depends on
CONFIG_KEXEC_FILE which is only enabled for 64 bits.
 
> > diff --git a/arch/x86/kernel/kexec-bzimage64.c 
> > b/arch/x86/kernel/kexec-bzimage64.c
> > index 68530fad05f74..518635cc0876c 100644
> > --- a/arch/x86/kernel/kexec-bzimage64.c
> > +++ b/arch/x86/kernel/kexec-bzimage64.c
> > @@ -233,6 +233,31 @@ setup_ima_state(const struct kimage *image, struct 
> > boot_params *params,
> >  #endif /* CONFIG_IMA_KEXEC */
> >  }
> >  
> > +static void setup_kho(const struct kimage *image, struct boot_params 
> > *params,
> > +                 unsigned long params_load_addr,
> > +                 unsigned int setup_data_offset)
> > +{
> > +#ifdef CONFIG_KEXEC_HANDOVER
> 
> Can this #ifdef be replaced with IS_ENABLED()?

The KHO structures in kexec image are under #ifdef, so it won't compile
with IS_ENABLED().

> > @@ -312,6 +337,13 @@ setup_boot_parameters(struct kimage *image, struct 
> > boot_params *params,
> >                                  sizeof(struct ima_setup_data);
> >     }
> >  
> > +   if (IS_ENABLED(CONFIG_KEXEC_HANDOVER)) {
> > +           /* Setup space to store preservation metadata */
> > +           setup_kho(image, params, params_load_addr, setup_data_offset);
> > +           setup_data_offset += sizeof(struct setup_data) +
> > +                                sizeof(struct kho_data);
> > +   }
> 
> This is a bit weird that it needs this IS_ENABLED() check and the
> earlier #ifdef. But I guess  it's following the IMA_KEXEC code's pattern
> at least.

And with other #ifdefs as well :)

if (IS_ENABLED()) can be dropped here, but having it makes things more
explicit IMHO.
 
> > diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
> > index 766176c4f5ee8..496dae89cf95d 100644
> > --- a/arch/x86/kernel/setup.c
> > +++ b/arch/x86/kernel/setup.c
> > @@ -451,6 +451,28 @@ int __init ima_get_kexec_buffer(void **addr, size_t 
> > *size)
> >  }
> >  #endif
> >  
> > +static void __init add_kho(u64 phys_addr, u32 data_len)
> > +{
> > +#ifdef CONFIG_KEXEC_HANDOVER
> > +   struct kho_data *kho;
> > +   u64 addr = phys_addr + sizeof(struct setup_data);
> > +   u64 size = data_len - sizeof(struct setup_data);
> > +
> > +   kho = early_memremap(addr, size);
> > +   if (!kho) {
> > +           pr_warn("setup: failed to memremap kho data (0x%llx, 0x%llx)\n",
> > +                   addr, size);
> > +           return;
> > +   }
> > +
> > +   kho_populate(kho->fdt_addr, kho->fdt_size, kho->scratch_addr, 
> > kho->scratch_size);
> > +
> > +   early_memunmap(kho, size);
> > +#else
> > +   pr_warn("Passed KHO data, but CONFIG_KEXEC_HANDOVER not set. 
> > Ignoring.\n");
> > +#endif
> > +}
> 
> Please axe the #ifdef in the .c file if at all possible, just like the
> others.

This one follows IMA, but it's easy to make it IS_ENABLED(). It's really up
to x86 folks preference.

-- 
Sincerely yours,
Mike.

Reply via email to