From: Nitesh Narayan Lal Subject: Re: [PATCH 2/3] KVM: Guest page hinting functionality Date: Wed, 2 Aug 2017 14:59:25 -0400 Message-ID: <5ed83f54-12c9-b2c7-bec5-d0a57e5e310e@redhat.com> References: <20170801204806.23938-1-nilal@redhat.com> <20170801204806.23938-3-nilal@redhat.com> <414002282.37988665.1501657308398.JavaMail.zimbra@redhat.com> Mime-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha256; protocol="application/pgp-signature"; boundary="vfTfu2iaJiifUXbmajqoMHFOtnTj6oIar" Cc: kvm@vger.kernel.org, riel@redhat.com, mst@redhat.com, david@redhat.com, yang.zhang.wz@gmail.com, wei.w.wang@intel.com, mst@redhat.com, david@redhat.com, yang zhang wz , wei w wang To: Pankaj Gupta Return-path: Received: from mx1.redhat.com ([209.132.183.28]:59524 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751186AbdHBS7e (ORCPT ); Wed, 2 Aug 2017 14:59:34 -0400 In-Reply-To: <414002282.37988665.1501657308398.JavaMail.zimbra@redhat.com> Sender: kvm-owner@vger.kernel.org List-ID: This is an OpenPGP/MIME signed message (RFC 4880 and 3156) --vfTfu2iaJiifUXbmajqoMHFOtnTj6oIar Content-Type: multipart/mixed; boundary="HAL8txfhjvqHIi7oUKFtq228XvbP7bC3m"; protected-headers="v1" From: Nitesh Narayan Lal To: Pankaj Gupta Cc: kvm@vger.kernel.org, riel@redhat.com, mst@redhat.com, david@redhat.com, yang.zhang.wz@gmail.com, wei.w.wang@intel.com, mst@redhat.com, david@redhat.com, yang zhang wz , wei w wang Message-ID: <5ed83f54-12c9-b2c7-bec5-d0a57e5e310e@redhat.com> Subject: Re: [PATCH 2/3] KVM: Guest page hinting functionality References: <20170801204806.23938-1-nilal@redhat.com> <20170801204806.23938-3-nilal@redhat.com> <414002282.37988665.1501657308398.JavaMail.zimbra@redhat.com> In-Reply-To: <414002282.37988665.1501657308398.JavaMail.zimbra@redhat.com> --HAL8txfhjvqHIi7oUKFtq228XvbP7bC3m Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Language: en-US Hi Pankaj, Thanks for your comments, please find my response inline. On 08/02/2017 03:01 AM, Pankaj Gupta wrote: > Hi Nitesh, > > I tried to look at the code.=20 > Some minor comments inline. > > Thanks, > Pankaj > >> This patch adds the guest implementation in order to maintain the list= of >> pages which are freed by the guest and are not reused. To avoid any >> reallocation it includes seqlock once the list is completely filled. >> Though it doesn't carries the hypercall related changes. >> >> Signed-off-by: Nitesh Narayan Lal >> --- >> virt/kvm/page_hinting.c | 246 >> +++++++++++++++++++++++++++++++++++++++++++++++- >> 1 file changed, 244 insertions(+), 2 deletions(-) >> >> diff --git a/virt/kvm/page_hinting.c b/virt/kvm/page_hinting.c >> index 39d2b1d..cfdc513 100644 >> --- a/virt/kvm/page_hinting.c >> +++ b/virt/kvm/page_hinting.c >> @@ -3,7 +3,7 @@ >> #include >> #include >> #include >> - >> +#include >> #include >> =20 >> #define MAX_FGPT_ENTRIES 1000 >> @@ -33,14 +33,256 @@ struct hypervisor_pages { >> unsigned int pages; >> }; >> =20 >> +static __cacheline_aligned_in_smp DEFINE_SEQLOCK(guest_page_lock); >> DEFINE_PER_CPU(struct kvm_free_pages [MAX_FGPT_ENTRIES], kvm_pt); >> DEFINE_PER_CPU(int, kvm_pt_idx); >> -struct hypervisor_pages hypervisor_pagelist[MAX_FGPT_ENTRIES]; >> +struct hypervisor_pages hypervisor_pagelist[MAX_FGPT_ENTRIES - 1]; >> + >> +static void empty_hyperlist(void) >> +{ >> + int i =3D 0; >> + >> + while (i < MAX_FGPT_ENTRIES - 1) { > MAX_FGPT_ENTRIES in-place of 'MAX_FGPT_ENTRIES - 1' here > and at similar other places? This is because CPU local list has a total of 1000 entries (MAX_FGPT_ENTRIES) where as CPU global list has 999 entries. If you see the arch_free_page_slowpath() and consider a situation where there are 1000 entries of singly allocated free pages in cpu-local list i.e., none of them are re-allocated. While adding them to the cpu global list when cpu local list index reaches to 1000 the outer loop will terminate due to which cpu global list index will never reach to 1000 and compress_hyperlist()/make_hypercall() will never be called. > >> + hypervisor_pagelist[i].pfn =3D 0; >> + hypervisor_pagelist[i].pages =3D 0; >> + i++; >> + } >> +} >> + >> +static void make_hypercall(void) >> +{ >> + /* >> + * Dummy function: Tobe filled later. >> + */ >> + empty_hyperlist(); >> +} >> + >> +static int sort_pfn(const void *a1, const void *b1) >> +{ >> + const struct hypervisor_pages *a =3D a1; >> + const struct hypervisor_pages *b =3D b1; >> + >> + if (a->pfn > b->pfn) >> + return 1; >> + >> + if (a->pfn < b->pfn) >> + return -1; >> + >> + return 0; >> +} >> + >> +static int pack_hyperlist(void) >> +{ >> + int i =3D 0, j =3D 0; >> + >> + while (i < MAX_FGPT_ENTRIES - 1) { >> + if (hypervisor_pagelist[i].pfn !=3D 0) { > no need to copy values at same index if non-zero I agree, I will fix it in the next version. > =20 > >> + hypervisor_pagelist[j].pfn =3D >> + hypervisor_pagelist[i].pfn; >> + hypervisor_pagelist[j].pages =3D >> + hypervisor_pagelist[i].pages; >> + j++; >> + } >> + i++; >> + } >> + i =3D j; >> + while (j < MAX_FGPT_ENTRIES - 1) { >> + hypervisor_pagelist[j].pfn =3D 0; >> + hypervisor_pagelist[j].pages =3D 0; >> + j++; >> + } >> + return i; >> +} >> + >> +int compress_hyperlist(void) >> +{ >> + int i =3D 0, j =3D 1, merge_counter =3D 0, ret =3D 0; >> + >> + sort(hypervisor_pagelist, MAX_FGPT_ENTRIES - 1, >> + sizeof(struct hypervisor_pages), sort_pfn, NULL); >> + while (i < MAX_FGPT_ENTRIES - 1 && j < MAX_FGPT_ENTRIES - 1) { >> + unsigned long pfni =3D hypervisor_pagelist[i].pfn; >> + unsigned int pagesi =3D hypervisor_pagelist[i].pages; >> + unsigned long pfnj =3D hypervisor_pagelist[j].pfn; >> + unsigned int pagesj =3D hypervisor_pagelist[j].pages; >> + >> + if (pfnj <=3D pfni) { >> + if (((pfnj + pagesj - 1) <=3D (pfni + pagesi - 1)) && >> + ((pfnj + pagesj - 1) >=3D (pfni - 1))) { >> + hypervisor_pagelist[i].pfn =3D pfnj; >> + hypervisor_pagelist[i].pages +=3D pfni - pfnj; >> + hypervisor_pagelist[j].pfn =3D 0; >> + hypervisor_pagelist[j].pages =3D 0; >> + j++; >> + merge_counter++; >> + continue; >> + } else if ((pfnj + pagesj - 1) > (pfni + pagesi - 1)) { >> + hypervisor_pagelist[i].pfn =3D pfnj; >> + hypervisor_pagelist[i].pages =3D pagesj; >> + hypervisor_pagelist[j].pfn =3D 0; >> + hypervisor_pagelist[j].pages =3D 0; >> + j++; >> + merge_counter++; >> + continue; >> + } >> + } >> + if (pfnj > pfni) { > =20 > else if here I will fix this in the next version. >> + if ((pfnj + pagesj - 1) > (pfni + pagesi - 1) && >> + (pfnj <=3D pfni + pagesi)) { >> + hypervisor_pagelist[i].pages +=3D >> + (pfnj + pagesj - 1) - >> + (pfni + pagesi - 1); >> + hypervisor_pagelist[j].pfn =3D 0; >> + hypervisor_pagelist[j].pages =3D 0; >> + j++; >> + merge_counter++; >> + continue; >> + } >> + if ((pfnj + pagesj - 1) <=3D (pfni + pagesi - 1)) { >> + hypervisor_pagelist[j].pfn =3D 0; >> + hypervisor_pagelist[j].pages =3D 0; >> + j++; >> + merge_counter++; >> + continue; >> + } >> + } >> + i =3D j; >> + j++; >> + } >> + if (merge_counter !=3D 0) >> + ret =3D pack_hyperlist() - 1; >> + else >> + ret =3D MAX_FGPT_ENTRIES - 1; >> + return ret; >> +} >> + >> +void copy_hyperlist(int hyper_idx) >> +{ >> + int *idx =3D &get_cpu_var(kvm_pt_idx); >> + struct kvm_free_pages *free_page_obj; >> + int i =3D 0; >> + >> + free_page_obj =3D &get_cpu_var(kvm_pt)[0]; >> + while (i < hyper_idx) { >> + free_page_obj[*idx].pfn =3D hypervisor_pagelist[i].pfn; >> + free_page_obj[*idx].pages =3D hypervisor_pagelist[i].pages; >> + *idx +=3D 1; >> + i++; >> + } >> + empty_hyperlist(); >> + put_cpu_var(kvm_pt); >> + put_cpu_var(kvm_pt_idx); >> +} >> + >> +/* >> + * arch_free_page_slowpath() - This function adds the guest free page= >> entries >> + * to hypervisor_pages list and also ensures defragmentation prior to= >> addition >> + * if it is present with any entry of the kvm_free_pages list. >> + */ >> +void arch_free_page_slowpath(void) >> +{ >> + int idx =3D 0; >> + int hyper_idx =3D -1; >> + int *kvm_idx =3D &get_cpu_var(kvm_pt_idx); >> + struct kvm_free_pages *free_page_obj =3D &get_cpu_var(kvm_pt)[0]; >> + >> + write_seqlock(&guest_page_lock); >> + while (idx < MAX_FGPT_ENTRIES) { >> + unsigned long pfn =3D free_page_obj[idx].pfn; >> + unsigned long pfn_end =3D free_page_obj[idx].pfn + >> + free_page_obj[idx].pages - 1; >> + bool prev_free =3D false; >> + >> + while (pfn <=3D pfn_end) { >> + struct page *p =3D pfn_to_page(pfn); >> + >> + if (PageCompound(p)) { >> + struct page *head_page =3D compound_head(p); >> + unsigned long head_pfn =3D page_to_pfn(head_page); >> + unsigned int alloc_pages =3D >> + 1 << compound_order(head_page); >> + >> + pfn =3D head_pfn + alloc_pages; >> + prev_free =3D false; >> + continue; >> + } >> + if (page_ref_count(p)) { >> + pfn++; >> + prev_free =3D false; >> + continue; >> + } >> + /* >> + * The page is free so add it to the list and free the >> + * hypervisor_pagelist if required. >> + */ >> + if (!prev_free) { >> + hyper_idx++; >> + if (hyper_idx =3D=3D MAX_FGPT_ENTRIES - 1) { >> + hyper_idx =3D compress_hyperlist(); >> + if (hyper_idx >=3D >> + HYPERLIST_THRESHOLD - 1) { >> + make_hypercall(); >> + hyper_idx =3D 0; >> + } >> + } >> + hypervisor_pagelist[hyper_idx].pfn =3D pfn; >> + hypervisor_pagelist[hyper_idx].pages =3D 1; >> + /* >> + * If the next contiguous page is free, it can >> + * be added to this same entry. >> + */ >> + prev_free =3D true; >> + } else { >> + /* >> + * Multiple adjacent free pages >> + */ >> + hypervisor_pagelist[hyper_idx].pages++; >> + } >> + pfn++; >> + } >> + free_page_obj[idx].pfn =3D 0; >> + free_page_obj[idx].pages =3D 0; >> + idx++; >> + } >> + *kvm_idx =3D 0; >> + put_cpu_var(kvm_pt); >> + put_cpu_var(kvm_pt_idx); >> + write_sequnlock(&guest_page_lock); >> +} >> =20 >> void arch_alloc_page(struct page *page, int order) >> { >> + unsigned int seq; >> + >> + /* >> + * arch_free_page will acquire the lock once the list carrying guest= >> + * free pages is full and a hypercall will be made. Until complete f= ree >> + * page list is traversed no further allocaiton will be allowed. >> + */ >> + do { >> + seq =3D read_seqbegin(&guest_page_lock); >> + } while (read_seqretry(&guest_page_lock, seq)); >> } >> =20 >> void arch_free_page(struct page *page, int order) >> { >> + int *free_page_idx =3D &get_cpu_var(kvm_pt_idx); >> + struct kvm_free_pages *free_page_obj; >> + unsigned long flags; >> + >> + /* >> + * use of global variables may trigger a race condition between irq = and >> + * process context causing unwanted overwrites. This will be replace= d >> + * with a better solution to prevent such race conditions. >> + */ >> + local_irq_save(flags); >> + free_page_obj =3D &get_cpu_var(kvm_pt)[0]; >> + free_page_obj[*free_page_idx].pfn =3D page_to_pfn(page); >> + free_page_obj[*free_page_idx].pages =3D 1 << order; >> + *free_page_idx +=3D 1; >> + if (*free_page_idx =3D=3D MAX_FGPT_ENTRIES) >> + arch_free_page_slowpath(); >> + put_cpu_var(kvm_pt); >> + put_cpu_var(kvm_pt_idx); >> + local_irq_restore(flags); >> } >> -- >> 2.9.4 >> >> --=20 Regards Nitesh --HAL8txfhjvqHIi7oUKFtq228XvbP7bC3m-- --vfTfu2iaJiifUXbmajqoMHFOtnTj6oIar Content-Type: application/pgp-signature; name="signature.asc" Content-Description: OpenPGP digital signature Content-Disposition: attachment; filename="signature.asc" -----BEGIN PGP SIGNATURE----- Version: GnuPG v2 iQIcBAEBCAAGBQJZgiENAAoJEKOGQNwGMqM5QikP/jbWz3pRZwNQkJQTFqTBve+C fRFbEF5cszpYDCgvbmjh8jY+vchL5RcNwL0JORpacEpIvNrtV0fbe+hqGuhvtWs9 Kn56nxbtM3nJOQFsMIvPvZoj6/vnKj4fiJFUMQyHO82B5eUvkrAkcH5/H1lH/q8d r+SG7tI82VmsvD+wGzuUOpNJyn48+/BI2rcVbs6fwT0JgtTJB2YMr3Qnbn0yTqHq sxVBoWJc4USbziwxDlIhjE+jT16Aw2AlPPJzWBkjbK09mS9rNL0zf77QjkIFwcmB Pfa8QFwsQTPrY81WGLfQzlB2BpRl0gGuS08xjiZL8oS+a4P/dS8EHdFSqdMiUz6E e3jyZTeybz+w/Aiu82XeVYo2slrN52JppciaTHTnaCMb+iq2ZSi1ROKO3HE1iFRF hMQTSnj0EaX49l1YmzkyDlZZiwMdGhp8ueANpjy5sHE/k/rrH10VxtJalzg6JRHq G0Wi1rya9wjxSsjnP21GYSXsdRdWB7taTQ4zv4LCf8Jh69NA7IZUOBoRGZyztxpB m4Mbuhiz3pAFnsuc4DPXQHEaZpmk8RxFr4sfDBJhrjQepz1WunkkVk+IlJZG4tFQ CPwHLKDtIxfI5pzS2j9Wz6LQliygUNnUNS2ykX5AEcg4ijD9Bg2D8cnWbyRNpnQH ceQ2ul99KE1wQZtVX812 =lbzs -----END PGP SIGNATURE----- --vfTfu2iaJiifUXbmajqoMHFOtnTj6oIar--