Re: [PATCH v13 13/35] KVM: Introduce per-page memory attributes

From: Huang, Kai
Date: Thu Nov 02 2023 - 06:55:54 EST


On Thu, 2023-11-02 at 11:32 +0100, Paolo Bonzini wrote:
> > > +#ifdef CONFIG_KVM_GENERIC_MEMORY_ATTRIBUTES
> > > +static inline unsigned long kvm_get_memory_attributes(struct kvm *kvm, gfn_t gfn)
> > > +{
> > > + return xa_to_value(xa_load(&kvm->mem_attr_array, gfn));
> > > +}
> >
> > Only call xa_to_value() when xa_load() returns !NULL?
>
> This xarray does not store a pointer, therefore xa_load() actually
> returns an integer that is tagged with 1 in the low bit:
>
> static inline unsigned long xa_to_value(const void *entry)
> {
>          return (unsigned long)entry >> 1;
> }
>
> Returning zero for an empty entry is okay, so the result of xa_load()
> can be used directly.

Thanks for explaining. I was thinking perhaps it's better to do:

void *entry = xa_load(...);

return xa_is_value(entry) ? xa_to_value(entry) : 0;

But "NULL (0) >> 1" is still 0, so yes we can use directly.