Return-Path: virtio-dev-return-5984-cohuck=redhat.com@lists.oasis-open.org Sender: List-Post: List-Help: List-Unsubscribe: List-Subscribe: Received: from lists.oasis-open.org (oasis-open.org [10.110.1.242]) by lists.oasis-open.org (Postfix) with ESMTP id 98A87985B23 for ; Thu, 15 Aug 2019 13:15:50 +0000 (UTC) References: <20190812131235.27244-1-nitesh@redhat.com> <20190812131235.27244-2-nitesh@redhat.com> <6d5b57ca-41ff-5c54-ab20-2b1631a6ce29@redhat.com> From: Nitesh Narayan Lal Message-ID: <09c6fbef-fa53-3a25-d3d6-460b9b6b2020@redhat.com> Date: Thu, 15 Aug 2019 09:15:37 -0400 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Content-Language: en-US Subject: [virtio-dev] Re: [RFC][Patch v12 1/2] mm: page_reporting: core infrastructure To: Alexander Duyck Cc: kvm list , LKML , linux-mm , virtio-dev@lists.oasis-open.org, Paolo Bonzini , lcapitulino@redhat.com, Pankaj Gupta , "Wang, Wei W" , Yang Zhang , Rik van Riel , David Hildenbrand , "Michael S. Tsirkin" , dodgen@google.com, Konrad Rzeszutek Wilk , dhildenb@redhat.com, Andrea Arcangeli , john.starks@microsoft.com, Dave Hansen , Michal Hocko , cohuck@redhat.com List-ID: On 8/14/19 12:11 PM, Alexander Duyck wrote: > On Wed, Aug 14, 2019 at 8:49 AM Nitesh Narayan Lal = wrote: >> >> On 8/12/19 2:47 PM, Alexander Duyck wrote: >>> On Mon, Aug 12, 2019 at 6:13 AM Nitesh Narayan Lal wrote: >>>> This patch introduces the core infrastructure for free page reportin= g in >>>> virtual environments. It enables the kernel to track the free pages = which >>>> can be reported to its hypervisor so that the hypervisor could >>>> free and reuse that memory as per its requirement. >>>> >>>> While the pages are getting processed in the hypervisor (e.g., >>>> via MADV_DONTNEED), the guest must not use them, otherwise, data los= s >>>> would be possible. To avoid such a situation, these pages are >>>> temporarily removed from the buddy. The amount of pages removed >>>> temporarily from the buddy is governed by the backend(virtio-balloon= >>>> in our case). >>>> >>>> To efficiently identify free pages that can to be reported to the >>>> hypervisor, bitmaps in a coarse granularity are used. Only fairly bi= g >>>> chunks are reported to the hypervisor - especially, to not break up = THP >>>> in the hypervisor - "MAX_ORDER - 2" on x86, and to save space. The b= its >>>> in the bitmap are an indication whether a page *might* be free, not = a >>>> guarantee. A new hook after buddy merging sets the bits. >>>> >>>> Bitmaps are stored per zone, protected by the zone lock. A workqueue= >>>> asynchronously processes the bitmaps, trying to isolate and report p= ages >>>> that are still free. The backend (virtio-balloon) is responsible for= >>>> reporting these batched pages to the host synchronously. Once report= ing/ >>>> freeing is complete, isolated pages are returned back to the buddy. >>>> >>>> Signed-off-by: Nitesh Narayan Lal >> [...] >>>> +} >>>> + >>>> +/** >>>> + * __page_reporting_enqueue - tracks the freed page in the respecti= ve zone's >>>> + * bitmap and enqueues a new page reporting job to the workqueue if= possible. >>>> + */ >>>> +void __page_reporting_enqueue(struct page *page) >>>> +{ >>>> + struct page_reporting_config *phconf; >>>> + struct zone *zone; >>>> + >>>> + rcu_read_lock(); >>>> + /* >>>> + * We should not process this page if either page reporting = is not >>>> + * yet completely enabled or it has been disabled by the bac= kend. >>>> + */ >>>> + phconf =3D rcu_dereference(page_reporting_conf); >>>> + if (!phconf) >>>> + return; >>>> + >>>> + zone =3D page_zone(page); >>>> + bitmap_set_bit(page, zone); >>>> + >>>> + /* >>>> + * We should not enqueue a job if a previously enqueued repo= rting work >>>> + * is in progress or we don't have enough free pages in the = zone. >>>> + */ >>>> + if (atomic_read(&zone->free_pages) >=3D phconf->max_pages &&= >>>> + !atomic_cmpxchg(&phconf->refcnt, 0, 1)) >>> This doesn't make any sense to me. Why are you only incrementing the >>> refcount if it is zero? Combining this with the assignment above, thi= s >>> isn't really a refcnt. It is just an oversized bitflag. >> >> The intent for having an extra variable was to ensure that at a time o= nly one >> reporting job is enqueued. I do agree that for that purpose I really d= on't need >> a reference counter and I should have used something like bool >> 'page_hinting_active'. But with bool, I think there could be a possibl= e chance >> of race. Maybe I should rename this variable and keep it as atomic. >> Any thoughts? > You could just use a bitflag to achieve what you are doing here. That > is the primary use case for many of the test_and_set_bit type > operations. However one issue with doing it as a bitflag is that you > have no way of telling that you took care of all requesters. I think you are right, I might end up missing on certain reporting opportunities in some special cases. Specifically when the pages which ar= e part of this new reporting request belongs to a section of the bitmap whi= ch has already been scanned. Although, I have failed to reproduce this kind = of situation in an actual setup. > That is > where having an actual reference count comes in handy as you know > exactly how many zones are requesting to be reported on. True. > >>> Also I am pretty sure this results in the opportunity to miss pages >>> because there is nothing to prevent you from possibly missing a ton o= f >>> pages you could hint on if a large number of pages are pushed out all= >>> at once and then the system goes idle in terms of memory allocation >>> and freeing. >> >> I was looking at how you are enqueuing/processing reporting jobs for e= ach zone. >> I am wondering if I should also consider something on similar lines as= having >> that I might be able to address the concern which you have raised abov= e. But it >> would also mean that I have to add an additional flag in the zone_flag= s. :) > You could do it either in the zone or outside the zone as yet another > bitmap. I decided to put the flags inside the zone because there was a > number of free bits there and it should be faster since we were > already using the zone structure. There are two possibilities which could happen while I am reporting: 1. Another request might come in for a different zone. 2. Another request could come in for the same zone and the pages belong t= o a =C2=A0=C2=A0=C2=A0 section of the bitmap which has already been scanned. Having a per zone flag to indicate reporting status will solve the first issue and to an extent the second as well. Having refcnt will possibly so= lve both of them. What I am wondering about is that in my case I could easily= impact the performance negatively by performing more bitmap scanning. --=20 Thanks Nitesh --------------------------------------------------------------------- To unsubscribe, e-mail: virtio-dev-unsubscribe@lists.oasis-open.org For additional commands, e-mail: virtio-dev-help@lists.oasis-open.org