Re: [Patch v2 2/2] KVM: x86/mmu: Allocate page table pages on NUMA node of underlying pages

From: Vipin Sharma
Date: Fri Dec 09 2022 - 13:48:39 EST


On Thu, Dec 8, 2022 at 4:06 PM David Matlack <dmatlack@xxxxxxxxxx> wrote:
>
> On Wed, Dec 07, 2022 at 11:05:09AM -0800, Vipin Sharma wrote:
> > By mistake I started replying to just Ben and realized it after few
> > exchanges. Adding others. Sorry about that.
> >
> > On Wed, Dec 7, 2022 at 10:58 AM Vipin Sharma <vipinsh@xxxxxxxxxx> wrote:
> > >
> > > On Tue, Dec 6, 2022 at 11:57 AM Ben Gardon <bgardon@xxxxxxxxxx> wrote:
> > > >
> > > > On Tue, Dec 6, 2022 at 11:18 AM Vipin Sharma <vipinsh@xxxxxxxxxx> wrote:
> > > > >
> > > > > On Tue, Dec 6, 2022 at 10:17 AM Ben Gardon <bgardon@xxxxxxxxxx> wrote:
> > > > > >
> > > > > > On Mon, Dec 5, 2022 at 3:40 PM Vipin Sharma <vipinsh@xxxxxxxxxx> wrote:
> > > > > > >
> > > > > > > On Mon, Dec 5, 2022 at 10:17 AM Ben Gardon <bgardon@xxxxxxxxxx> wrote:
> > > > > > > >
> > > > > > > > On Thu, Dec 1, 2022 at 11:57 AM Vipin Sharma <vipinsh@xxxxxxxxxx> wrote:
> > > > > > > > > diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> > > > > > > > > index 1782c4555d94..4d59c9d48277 100644
> > > > > > > > > --- a/virt/kvm/kvm_main.c
> > > > > > > > > +++ b/virt/kvm/kvm_main.c
> > > > > > > > > @@ -384,6 +384,11 @@ static void kvm_flush_shadow_all(struct kvm *kvm)
> > > > > > > > > kvm_arch_guest_memory_reclaimed(kvm);
> > > > > > > > > }
> > > > > > > > >
> > > > > > > > > +void * __weak kvm_arch_mmu_get_free_page(int nid, gfp_t gfp_flags)
> > > > > > > > > +{
> > > > > > > > > + return (void *)__get_free_page(gfp_flags);
> > > > > > > > > +}
> > > > > > > > > +
> > > > > > > >
> > > > > > > > Rather than making this __weak, you could use #ifdef CONFIG_NUMA to
> > > > > > > > just put all the code in the arch-neutral function.
> > > > > > > >
> > > > > > >
> > > > > > > I am not sure how it will work. Here, I am trying to keep this feature
> > > > > > > only for x86. This function will be used for all architecture except
> > > > > > > in x86 where we have different implementation in arch/x86/mmu/mmu.c
> > > > > > > So, even if CONFIG_NUMA is defined, we want to keep the same
> > > > > > > definition on other architectures.
> > > > > > >
> > > > > > >
> > > > > >
> > > > > > Something like:
> > > > > >
> > > > > > +void * kvm_arch_mmu_get_free_page(int nid, gfp_t gfp_flags)
> > > > > > +{
> > > > > > + struct page *spt_page;
> > > > > > + void *address = NULL;
> > > > > > +
> > > > > > + #ifdef CONFIG_NUMA
> > > > > > + if (nid != NUMA_NO_NODE) {
> > > > > > + spt_page = alloc_pages_node(nid, gfp, 0);
> > > > > > + if (spt_page) {
> > > > > > + address = page_address(spt_page);
> > > > > > + return address;
> > > > > > + }
> > > > > > + }
> > > > > > + #endif // CONFIG_NUMA
> > > > > > + return (void *)__get_free_page(gfp);
> > > > > > +}
> > > > > >
> > > > >
> > > > > 'nid' will be 0 not NUMA_NO_NODE for other architectures. In x86, I am
> > > > > explicitly setting kvm_mmu_memory_cache->node to NUMA_NO_NODE or
> > > > > specific desired nodes. In others architectures it will be 0 as struct
> > > > > will be 0 initialized. __weak avoids initializing nid to NUM_NO_NODE
> > > > > in other architectures.
> > > >
> > > > ooh, I see. It might be worth setting it to NUMA_NO_NODE on other
> > > > archs as 0 could be kind of misleading.
> > >
> > > Discussed offline with Ben.
> > > Initialization code for cache is in the respective architectures.
> > > Using "__weak" avoids touching code in other architectures.
>
> But it's still a bit gross to have node=0 in struct
> kvm_mmu_memory_cache for other architectures, even if it doesn't happen
> to be misused in this series.
>
> I would just bite the bullet and modify the other architectures. Do it
> in a precusor patch where you just add node to struct
> kvm_mmu_memory_cache and initialize it to NUMA_NO_NODE across all
> architectures, probably with a common macro e.g.
>
> #define INIT_KVM_MMU_MEMORY_CACHE(_cache) do { \
> (_cache)->node = NUMA_NO_NODE; \
> } while (0)
>
> Then, you can follow Ben's approach and avoid the __weak function.

Okay, 2 votes for NUMA_NO_NODE and 1 for __weak. I will remove the
__weak and modify other architecture code.