Re: [RFC][Patch v9 2/6] KVM: Enables the kernel to isolate guest free pages

2019-03-13 Thread David Hildenbrand
On 13.03.19 23:54, Alexander Duyck wrote:
> On Wed, Mar 13, 2019 at 9:39 AM David Hildenbrand  wrote:
>>
>> On 13.03.19 17:37, Alexander Duyck wrote:
>>> On Wed, Mar 13, 2019 at 5:18 AM David Hildenbrand  wrote:

 On 13.03.19 12:54, Nitesh Narayan Lal wrote:
>
> On 3/12/19 5:13 PM, Alexander Duyck wrote:
>> On Tue, Mar 12, 2019 at 12:46 PM Nitesh Narayan Lal  
>> wrote:
>>> On 3/8/19 4:39 PM, Alexander Duyck wrote:
 On Fri, Mar 8, 2019 at 11:39 AM Nitesh Narayan Lal  
 wrote:
> On 3/8/19 2:25 PM, Alexander Duyck wrote:
>> On Fri, Mar 8, 2019 at 11:10 AM Nitesh Narayan Lal 
>>  wrote:
>>> On 3/8/19 1:06 PM, Alexander Duyck wrote:
 On Thu, Mar 7, 2019 at 6:32 PM Michael S. Tsirkin 
  wrote:
> On Thu, Mar 07, 2019 at 02:35:53PM -0800, Alexander Duyck wrote:
>> The only other thing I still want to try and see if I can do is 
>> to add
>> a jiffies value to the page private data in the case of the buddy
>> pages.
> Actually there's one extra thing I think we should do, and that 
> is make
> sure we do not leave less than X% off the free memory at a time.
> This way chances of triggering an OOM are lower.
 If nothing else we could probably look at doing a watermark of some
 sort so we have to have X amount of memory free but not hinted 
 before
 we will start providing the hints. It would just be a matter of
 tracking how much memory we have hinted on versus the amount of 
 memory
 that has been pulled from that pool.
>>> This is to avoid false OOM in the guest?
>> Partially, though it would still be possible. Basically it would just
>> be a way of determining when we have hinted "enough". Basically it
>> doesn't do us much good to be hinting on free memory if the guest is
>> already constrained and just going to reallocate the memory shortly
>> after we hinted on it. The idea is with a watermark we can avoid
>> hinting until we start having pages that are actually going to stay
>> free for a while.
>>
  It is another reason why we
 probably want a bit in the buddy pages somewhere to indicate if a 
 page
 has been hinted or not as we can then use that to determine if we 
 have
 to account for it in the statistics.
>>> The one benefit which I can see of having an explicit bit is that it
>>> will help us to have a single hook away from the hot path within 
>>> buddy
>>> merging code (just like your arch_merge_page) and still avoid 
>>> duplicate
>>> hints while releasing pages.
>>>
>>> I still have to check PG_idle and PG_young which you mentioned but I
>>> don't think we can reuse any existing bits.
>> Those are bits that are already there for 64b. I think those exist in
>> the page extension for 32b systems. If I am not mistaken they are 
>> only
>> used in VMA mapped memory. What I was getting at is that those are 
>> the
>> bits we could think about reusing.
>>
>>> If we really want to have something like a watermark, then can't we 
>>> use
>>> zone->free_pages before isolating to see how many free pages are 
>>> there
>>> and put a threshold on it? (__isolate_free_page() does a similar 
>>> thing
>>> but it does that on per request basis).
>> Right. That is only part of it though since that tells you how many
>> free pages are there. But how many of those free pages are hinted?
>> That is the part we would need to track separately and then then
>> compare to free_pages to determine if we need to start hinting on 
>> more
>> memory or not.
> Only pages which are isolated will be hinted, and once a page is
> isolated it will not be counted in the zone free pages.
> Feel free to correct me if I am wrong.
 You are correct up to here. When we isolate the page it isn't counted
 against the free pages. However after we complete the hint we end up
 taking it out of isolation and returning it to the "free" state, so it
 will be counted against the free pages.

> If I am understanding it correctly you only want to hint the idle 
> pages,
> is that right?
 Getting back to the ideas from our earlier discussion, we had 3 stages
 for things. Free but not hinted, isolated due to hinting, and free and
 hinted. So what we would need to do is identify the size of the first
 pool that is free and not hinted by knowing the total number of free
 

Re: [RFC][Patch v9 2/6] KVM: Enables the kernel to isolate guest free pages

2019-03-13 Thread Alexander Duyck
On Wed, Mar 13, 2019 at 9:39 AM David Hildenbrand  wrote:
>
> On 13.03.19 17:37, Alexander Duyck wrote:
> > On Wed, Mar 13, 2019 at 5:18 AM David Hildenbrand  wrote:
> >>
> >> On 13.03.19 12:54, Nitesh Narayan Lal wrote:
> >>>
> >>> On 3/12/19 5:13 PM, Alexander Duyck wrote:
>  On Tue, Mar 12, 2019 at 12:46 PM Nitesh Narayan Lal  
>  wrote:
> > On 3/8/19 4:39 PM, Alexander Duyck wrote:
> >> On Fri, Mar 8, 2019 at 11:39 AM Nitesh Narayan Lal  
> >> wrote:
> >>> On 3/8/19 2:25 PM, Alexander Duyck wrote:
>  On Fri, Mar 8, 2019 at 11:10 AM Nitesh Narayan Lal 
>   wrote:
> > On 3/8/19 1:06 PM, Alexander Duyck wrote:
> >> On Thu, Mar 7, 2019 at 6:32 PM Michael S. Tsirkin 
> >>  wrote:
> >>> On Thu, Mar 07, 2019 at 02:35:53PM -0800, Alexander Duyck wrote:
>  The only other thing I still want to try and see if I can do is 
>  to add
>  a jiffies value to the page private data in the case of the buddy
>  pages.
> >>> Actually there's one extra thing I think we should do, and that 
> >>> is make
> >>> sure we do not leave less than X% off the free memory at a time.
> >>> This way chances of triggering an OOM are lower.
> >> If nothing else we could probably look at doing a watermark of some
> >> sort so we have to have X amount of memory free but not hinted 
> >> before
> >> we will start providing the hints. It would just be a matter of
> >> tracking how much memory we have hinted on versus the amount of 
> >> memory
> >> that has been pulled from that pool.
> > This is to avoid false OOM in the guest?
>  Partially, though it would still be possible. Basically it would just
>  be a way of determining when we have hinted "enough". Basically it
>  doesn't do us much good to be hinting on free memory if the guest is
>  already constrained and just going to reallocate the memory shortly
>  after we hinted on it. The idea is with a watermark we can avoid
>  hinting until we start having pages that are actually going to stay
>  free for a while.
> 
> >>  It is another reason why we
> >> probably want a bit in the buddy pages somewhere to indicate if a 
> >> page
> >> has been hinted or not as we can then use that to determine if we 
> >> have
> >> to account for it in the statistics.
> > The one benefit which I can see of having an explicit bit is that it
> > will help us to have a single hook away from the hot path within 
> > buddy
> > merging code (just like your arch_merge_page) and still avoid 
> > duplicate
> > hints while releasing pages.
> >
> > I still have to check PG_idle and PG_young which you mentioned but I
> > don't think we can reuse any existing bits.
>  Those are bits that are already there for 64b. I think those exist in
>  the page extension for 32b systems. If I am not mistaken they are 
>  only
>  used in VMA mapped memory. What I was getting at is that those are 
>  the
>  bits we could think about reusing.
> 
> > If we really want to have something like a watermark, then can't we 
> > use
> > zone->free_pages before isolating to see how many free pages are 
> > there
> > and put a threshold on it? (__isolate_free_page() does a similar 
> > thing
> > but it does that on per request basis).
>  Right. That is only part of it though since that tells you how many
>  free pages are there. But how many of those free pages are hinted?
>  That is the part we would need to track separately and then then
>  compare to free_pages to determine if we need to start hinting on 
>  more
>  memory or not.
> >>> Only pages which are isolated will be hinted, and once a page is
> >>> isolated it will not be counted in the zone free pages.
> >>> Feel free to correct me if I am wrong.
> >> You are correct up to here. When we isolate the page it isn't counted
> >> against the free pages. However after we complete the hint we end up
> >> taking it out of isolation and returning it to the "free" state, so it
> >> will be counted against the free pages.
> >>
> >>> If I am understanding it correctly you only want to hint the idle 
> >>> pages,
> >>> is that right?
> >> Getting back to the ideas from our earlier discussion, we had 3 stages
> >> for things. Free but not hinted, isolated due to hinting, and free and
> >> hinted. So what we would need to do is identify the size of the first
> >> pool that is free and not hinted by knowing the total number of free
> >> pages, and then subtract the size of the pages 

Re: [RFC][Patch v9 2/6] KVM: Enables the kernel to isolate guest free pages

2019-03-13 Thread David Hildenbrand
On 13.03.19 17:37, Alexander Duyck wrote:
> On Wed, Mar 13, 2019 at 5:18 AM David Hildenbrand  wrote:
>>
>> On 13.03.19 12:54, Nitesh Narayan Lal wrote:
>>>
>>> On 3/12/19 5:13 PM, Alexander Duyck wrote:
 On Tue, Mar 12, 2019 at 12:46 PM Nitesh Narayan Lal  
 wrote:
> On 3/8/19 4:39 PM, Alexander Duyck wrote:
>> On Fri, Mar 8, 2019 at 11:39 AM Nitesh Narayan Lal  
>> wrote:
>>> On 3/8/19 2:25 PM, Alexander Duyck wrote:
 On Fri, Mar 8, 2019 at 11:10 AM Nitesh Narayan Lal  
 wrote:
> On 3/8/19 1:06 PM, Alexander Duyck wrote:
>> On Thu, Mar 7, 2019 at 6:32 PM Michael S. Tsirkin  
>> wrote:
>>> On Thu, Mar 07, 2019 at 02:35:53PM -0800, Alexander Duyck wrote:
 The only other thing I still want to try and see if I can do is to 
 add
 a jiffies value to the page private data in the case of the buddy
 pages.
>>> Actually there's one extra thing I think we should do, and that is 
>>> make
>>> sure we do not leave less than X% off the free memory at a time.
>>> This way chances of triggering an OOM are lower.
>> If nothing else we could probably look at doing a watermark of some
>> sort so we have to have X amount of memory free but not hinted before
>> we will start providing the hints. It would just be a matter of
>> tracking how much memory we have hinted on versus the amount of 
>> memory
>> that has been pulled from that pool.
> This is to avoid false OOM in the guest?
 Partially, though it would still be possible. Basically it would just
 be a way of determining when we have hinted "enough". Basically it
 doesn't do us much good to be hinting on free memory if the guest is
 already constrained and just going to reallocate the memory shortly
 after we hinted on it. The idea is with a watermark we can avoid
 hinting until we start having pages that are actually going to stay
 free for a while.

>>  It is another reason why we
>> probably want a bit in the buddy pages somewhere to indicate if a 
>> page
>> has been hinted or not as we can then use that to determine if we 
>> have
>> to account for it in the statistics.
> The one benefit which I can see of having an explicit bit is that it
> will help us to have a single hook away from the hot path within buddy
> merging code (just like your arch_merge_page) and still avoid 
> duplicate
> hints while releasing pages.
>
> I still have to check PG_idle and PG_young which you mentioned but I
> don't think we can reuse any existing bits.
 Those are bits that are already there for 64b. I think those exist in
 the page extension for 32b systems. If I am not mistaken they are only
 used in VMA mapped memory. What I was getting at is that those are the
 bits we could think about reusing.

> If we really want to have something like a watermark, then can't we 
> use
> zone->free_pages before isolating to see how many free pages are there
> and put a threshold on it? (__isolate_free_page() does a similar thing
> but it does that on per request basis).
 Right. That is only part of it though since that tells you how many
 free pages are there. But how many of those free pages are hinted?
 That is the part we would need to track separately and then then
 compare to free_pages to determine if we need to start hinting on more
 memory or not.
>>> Only pages which are isolated will be hinted, and once a page is
>>> isolated it will not be counted in the zone free pages.
>>> Feel free to correct me if I am wrong.
>> You are correct up to here. When we isolate the page it isn't counted
>> against the free pages. However after we complete the hint we end up
>> taking it out of isolation and returning it to the "free" state, so it
>> will be counted against the free pages.
>>
>>> If I am understanding it correctly you only want to hint the idle pages,
>>> is that right?
>> Getting back to the ideas from our earlier discussion, we had 3 stages
>> for things. Free but not hinted, isolated due to hinting, and free and
>> hinted. So what we would need to do is identify the size of the first
>> pool that is free and not hinted by knowing the total number of free
>> pages, and then subtract the size of the pages that are hinted and
>> still free.
> To summarize, for now, I think it makes sense to stick with the current
> approach as this way we can avoid any locking in the allocation path and
> reduce the number of hypercalls for a bunch of MAX_ORDER - 1 page.
 I'm not sure what you are talking about by "avoid any 

Re: [RFC][Patch v9 2/6] KVM: Enables the kernel to isolate guest free pages

2019-03-13 Thread Alexander Duyck
On Wed, Mar 13, 2019 at 5:18 AM David Hildenbrand  wrote:
>
> On 13.03.19 12:54, Nitesh Narayan Lal wrote:
> >
> > On 3/12/19 5:13 PM, Alexander Duyck wrote:
> >> On Tue, Mar 12, 2019 at 12:46 PM Nitesh Narayan Lal  
> >> wrote:
> >>> On 3/8/19 4:39 PM, Alexander Duyck wrote:
>  On Fri, Mar 8, 2019 at 11:39 AM Nitesh Narayan Lal  
>  wrote:
> > On 3/8/19 2:25 PM, Alexander Duyck wrote:
> >> On Fri, Mar 8, 2019 at 11:10 AM Nitesh Narayan Lal  
> >> wrote:
> >>> On 3/8/19 1:06 PM, Alexander Duyck wrote:
>  On Thu, Mar 7, 2019 at 6:32 PM Michael S. Tsirkin  
>  wrote:
> > On Thu, Mar 07, 2019 at 02:35:53PM -0800, Alexander Duyck wrote:
> >> The only other thing I still want to try and see if I can do is to 
> >> add
> >> a jiffies value to the page private data in the case of the buddy
> >> pages.
> > Actually there's one extra thing I think we should do, and that is 
> > make
> > sure we do not leave less than X% off the free memory at a time.
> > This way chances of triggering an OOM are lower.
>  If nothing else we could probably look at doing a watermark of some
>  sort so we have to have X amount of memory free but not hinted before
>  we will start providing the hints. It would just be a matter of
>  tracking how much memory we have hinted on versus the amount of 
>  memory
>  that has been pulled from that pool.
> >>> This is to avoid false OOM in the guest?
> >> Partially, though it would still be possible. Basically it would just
> >> be a way of determining when we have hinted "enough". Basically it
> >> doesn't do us much good to be hinting on free memory if the guest is
> >> already constrained and just going to reallocate the memory shortly
> >> after we hinted on it. The idea is with a watermark we can avoid
> >> hinting until we start having pages that are actually going to stay
> >> free for a while.
> >>
>   It is another reason why we
>  probably want a bit in the buddy pages somewhere to indicate if a 
>  page
>  has been hinted or not as we can then use that to determine if we 
>  have
>  to account for it in the statistics.
> >>> The one benefit which I can see of having an explicit bit is that it
> >>> will help us to have a single hook away from the hot path within buddy
> >>> merging code (just like your arch_merge_page) and still avoid 
> >>> duplicate
> >>> hints while releasing pages.
> >>>
> >>> I still have to check PG_idle and PG_young which you mentioned but I
> >>> don't think we can reuse any existing bits.
> >> Those are bits that are already there for 64b. I think those exist in
> >> the page extension for 32b systems. If I am not mistaken they are only
> >> used in VMA mapped memory. What I was getting at is that those are the
> >> bits we could think about reusing.
> >>
> >>> If we really want to have something like a watermark, then can't we 
> >>> use
> >>> zone->free_pages before isolating to see how many free pages are there
> >>> and put a threshold on it? (__isolate_free_page() does a similar thing
> >>> but it does that on per request basis).
> >> Right. That is only part of it though since that tells you how many
> >> free pages are there. But how many of those free pages are hinted?
> >> That is the part we would need to track separately and then then
> >> compare to free_pages to determine if we need to start hinting on more
> >> memory or not.
> > Only pages which are isolated will be hinted, and once a page is
> > isolated it will not be counted in the zone free pages.
> > Feel free to correct me if I am wrong.
>  You are correct up to here. When we isolate the page it isn't counted
>  against the free pages. However after we complete the hint we end up
>  taking it out of isolation and returning it to the "free" state, so it
>  will be counted against the free pages.
> 
> > If I am understanding it correctly you only want to hint the idle pages,
> > is that right?
>  Getting back to the ideas from our earlier discussion, we had 3 stages
>  for things. Free but not hinted, isolated due to hinting, and free and
>  hinted. So what we would need to do is identify the size of the first
>  pool that is free and not hinted by knowing the total number of free
>  pages, and then subtract the size of the pages that are hinted and
>  still free.
> >>> To summarize, for now, I think it makes sense to stick with the current
> >>> approach as this way we can avoid any locking in the allocation path and
> >>> reduce the number of hypercalls for a bunch of MAX_ORDER - 1 page.
> >> I'm not sure what you are talking about by "avoid any locking in the
> >> allocation path". Are you 

Re: [RFC][Patch v9 2/6] KVM: Enables the kernel to isolate guest free pages

2019-03-13 Thread Nitesh Narayan Lal
On 3/13/19 8:17 AM, David Hildenbrand wrote:
> On 13.03.19 12:54, Nitesh Narayan Lal wrote:
>> On 3/12/19 5:13 PM, Alexander Duyck wrote:
>>> On Tue, Mar 12, 2019 at 12:46 PM Nitesh Narayan Lal  
>>> wrote:
 On 3/8/19 4:39 PM, Alexander Duyck wrote:
> On Fri, Mar 8, 2019 at 11:39 AM Nitesh Narayan Lal  
> wrote:
>> On 3/8/19 2:25 PM, Alexander Duyck wrote:
>>> On Fri, Mar 8, 2019 at 11:10 AM Nitesh Narayan Lal  
>>> wrote:
 On 3/8/19 1:06 PM, Alexander Duyck wrote:
> On Thu, Mar 7, 2019 at 6:32 PM Michael S. Tsirkin  
> wrote:
>> On Thu, Mar 07, 2019 at 02:35:53PM -0800, Alexander Duyck wrote:
>>> The only other thing I still want to try and see if I can do is to 
>>> add
>>> a jiffies value to the page private data in the case of the buddy
>>> pages.
>> Actually there's one extra thing I think we should do, and that is 
>> make
>> sure we do not leave less than X% off the free memory at a time.
>> This way chances of triggering an OOM are lower.
> If nothing else we could probably look at doing a watermark of some
> sort so we have to have X amount of memory free but not hinted before
> we will start providing the hints. It would just be a matter of
> tracking how much memory we have hinted on versus the amount of memory
> that has been pulled from that pool.
 This is to avoid false OOM in the guest?
>>> Partially, though it would still be possible. Basically it would just
>>> be a way of determining when we have hinted "enough". Basically it
>>> doesn't do us much good to be hinting on free memory if the guest is
>>> already constrained and just going to reallocate the memory shortly
>>> after we hinted on it. The idea is with a watermark we can avoid
>>> hinting until we start having pages that are actually going to stay
>>> free for a while.
>>>
>  It is another reason why we
> probably want a bit in the buddy pages somewhere to indicate if a page
> has been hinted or not as we can then use that to determine if we have
> to account for it in the statistics.
 The one benefit which I can see of having an explicit bit is that it
 will help us to have a single hook away from the hot path within buddy
 merging code (just like your arch_merge_page) and still avoid duplicate
 hints while releasing pages.

 I still have to check PG_idle and PG_young which you mentioned but I
 don't think we can reuse any existing bits.
>>> Those are bits that are already there for 64b. I think those exist in
>>> the page extension for 32b systems. If I am not mistaken they are only
>>> used in VMA mapped memory. What I was getting at is that those are the
>>> bits we could think about reusing.
>>>
 If we really want to have something like a watermark, then can't we use
 zone->free_pages before isolating to see how many free pages are there
 and put a threshold on it? (__isolate_free_page() does a similar thing
 but it does that on per request basis).
>>> Right. That is only part of it though since that tells you how many
>>> free pages are there. But how many of those free pages are hinted?
>>> That is the part we would need to track separately and then then
>>> compare to free_pages to determine if we need to start hinting on more
>>> memory or not.
>> Only pages which are isolated will be hinted, and once a page is
>> isolated it will not be counted in the zone free pages.
>> Feel free to correct me if I am wrong.
> You are correct up to here. When we isolate the page it isn't counted
> against the free pages. However after we complete the hint we end up
> taking it out of isolation and returning it to the "free" state, so it
> will be counted against the free pages.
>
>> If I am understanding it correctly you only want to hint the idle pages,
>> is that right?
> Getting back to the ideas from our earlier discussion, we had 3 stages
> for things. Free but not hinted, isolated due to hinting, and free and
> hinted. So what we would need to do is identify the size of the first
> pool that is free and not hinted by knowing the total number of free
> pages, and then subtract the size of the pages that are hinted and
> still free.
 To summarize, for now, I think it makes sense to stick with the current
 approach as this way we can avoid any locking in the allocation path and
 reduce the number of hypercalls for a bunch of MAX_ORDER - 1 page.
>>> I'm not sure what you are talking about by "avoid any locking in the
>>> allocation path". Are you talking about the spin on idle bit, if so
>>> then yes. 
>> Yeap!
>>> However I have been testing your patches and I was correct
>>> in the assumption that you 

Re: [RFC][Patch v9 2/6] KVM: Enables the kernel to isolate guest free pages

2019-03-13 Thread David Hildenbrand
On 13.03.19 12:54, Nitesh Narayan Lal wrote:
> 
> On 3/12/19 5:13 PM, Alexander Duyck wrote:
>> On Tue, Mar 12, 2019 at 12:46 PM Nitesh Narayan Lal  
>> wrote:
>>> On 3/8/19 4:39 PM, Alexander Duyck wrote:
 On Fri, Mar 8, 2019 at 11:39 AM Nitesh Narayan Lal  
 wrote:
> On 3/8/19 2:25 PM, Alexander Duyck wrote:
>> On Fri, Mar 8, 2019 at 11:10 AM Nitesh Narayan Lal  
>> wrote:
>>> On 3/8/19 1:06 PM, Alexander Duyck wrote:
 On Thu, Mar 7, 2019 at 6:32 PM Michael S. Tsirkin  
 wrote:
> On Thu, Mar 07, 2019 at 02:35:53PM -0800, Alexander Duyck wrote:
>> The only other thing I still want to try and see if I can do is to 
>> add
>> a jiffies value to the page private data in the case of the buddy
>> pages.
> Actually there's one extra thing I think we should do, and that is 
> make
> sure we do not leave less than X% off the free memory at a time.
> This way chances of triggering an OOM are lower.
 If nothing else we could probably look at doing a watermark of some
 sort so we have to have X amount of memory free but not hinted before
 we will start providing the hints. It would just be a matter of
 tracking how much memory we have hinted on versus the amount of memory
 that has been pulled from that pool.
>>> This is to avoid false OOM in the guest?
>> Partially, though it would still be possible. Basically it would just
>> be a way of determining when we have hinted "enough". Basically it
>> doesn't do us much good to be hinting on free memory if the guest is
>> already constrained and just going to reallocate the memory shortly
>> after we hinted on it. The idea is with a watermark we can avoid
>> hinting until we start having pages that are actually going to stay
>> free for a while.
>>
  It is another reason why we
 probably want a bit in the buddy pages somewhere to indicate if a page
 has been hinted or not as we can then use that to determine if we have
 to account for it in the statistics.
>>> The one benefit which I can see of having an explicit bit is that it
>>> will help us to have a single hook away from the hot path within buddy
>>> merging code (just like your arch_merge_page) and still avoid duplicate
>>> hints while releasing pages.
>>>
>>> I still have to check PG_idle and PG_young which you mentioned but I
>>> don't think we can reuse any existing bits.
>> Those are bits that are already there for 64b. I think those exist in
>> the page extension for 32b systems. If I am not mistaken they are only
>> used in VMA mapped memory. What I was getting at is that those are the
>> bits we could think about reusing.
>>
>>> If we really want to have something like a watermark, then can't we use
>>> zone->free_pages before isolating to see how many free pages are there
>>> and put a threshold on it? (__isolate_free_page() does a similar thing
>>> but it does that on per request basis).
>> Right. That is only part of it though since that tells you how many
>> free pages are there. But how many of those free pages are hinted?
>> That is the part we would need to track separately and then then
>> compare to free_pages to determine if we need to start hinting on more
>> memory or not.
> Only pages which are isolated will be hinted, and once a page is
> isolated it will not be counted in the zone free pages.
> Feel free to correct me if I am wrong.
 You are correct up to here. When we isolate the page it isn't counted
 against the free pages. However after we complete the hint we end up
 taking it out of isolation and returning it to the "free" state, so it
 will be counted against the free pages.

> If I am understanding it correctly you only want to hint the idle pages,
> is that right?
 Getting back to the ideas from our earlier discussion, we had 3 stages
 for things. Free but not hinted, isolated due to hinting, and free and
 hinted. So what we would need to do is identify the size of the first
 pool that is free and not hinted by knowing the total number of free
 pages, and then subtract the size of the pages that are hinted and
 still free.
>>> To summarize, for now, I think it makes sense to stick with the current
>>> approach as this way we can avoid any locking in the allocation path and
>>> reduce the number of hypercalls for a bunch of MAX_ORDER - 1 page.
>> I'm not sure what you are talking about by "avoid any locking in the
>> allocation path". Are you talking about the spin on idle bit, if so
>> then yes. 
> Yeap!
>> However I have been testing your patches and I was correct
>> in the assumption that you forgot to handle the zone lock when you
>> were freeing __free_one_page.
> Yes, these are the steps other than the comments you 

Re: [RFC][Patch v9 2/6] KVM: Enables the kernel to isolate guest free pages

2019-03-13 Thread Nitesh Narayan Lal

On 3/12/19 5:13 PM, Alexander Duyck wrote:
> On Tue, Mar 12, 2019 at 12:46 PM Nitesh Narayan Lal  wrote:
>> On 3/8/19 4:39 PM, Alexander Duyck wrote:
>>> On Fri, Mar 8, 2019 at 11:39 AM Nitesh Narayan Lal  
>>> wrote:
 On 3/8/19 2:25 PM, Alexander Duyck wrote:
> On Fri, Mar 8, 2019 at 11:10 AM Nitesh Narayan Lal  
> wrote:
>> On 3/8/19 1:06 PM, Alexander Duyck wrote:
>>> On Thu, Mar 7, 2019 at 6:32 PM Michael S. Tsirkin  
>>> wrote:
 On Thu, Mar 07, 2019 at 02:35:53PM -0800, Alexander Duyck wrote:
> The only other thing I still want to try and see if I can do is to add
> a jiffies value to the page private data in the case of the buddy
> pages.
 Actually there's one extra thing I think we should do, and that is make
 sure we do not leave less than X% off the free memory at a time.
 This way chances of triggering an OOM are lower.
>>> If nothing else we could probably look at doing a watermark of some
>>> sort so we have to have X amount of memory free but not hinted before
>>> we will start providing the hints. It would just be a matter of
>>> tracking how much memory we have hinted on versus the amount of memory
>>> that has been pulled from that pool.
>> This is to avoid false OOM in the guest?
> Partially, though it would still be possible. Basically it would just
> be a way of determining when we have hinted "enough". Basically it
> doesn't do us much good to be hinting on free memory if the guest is
> already constrained and just going to reallocate the memory shortly
> after we hinted on it. The idea is with a watermark we can avoid
> hinting until we start having pages that are actually going to stay
> free for a while.
>
>>>  It is another reason why we
>>> probably want a bit in the buddy pages somewhere to indicate if a page
>>> has been hinted or not as we can then use that to determine if we have
>>> to account for it in the statistics.
>> The one benefit which I can see of having an explicit bit is that it
>> will help us to have a single hook away from the hot path within buddy
>> merging code (just like your arch_merge_page) and still avoid duplicate
>> hints while releasing pages.
>>
>> I still have to check PG_idle and PG_young which you mentioned but I
>> don't think we can reuse any existing bits.
> Those are bits that are already there for 64b. I think those exist in
> the page extension for 32b systems. If I am not mistaken they are only
> used in VMA mapped memory. What I was getting at is that those are the
> bits we could think about reusing.
>
>> If we really want to have something like a watermark, then can't we use
>> zone->free_pages before isolating to see how many free pages are there
>> and put a threshold on it? (__isolate_free_page() does a similar thing
>> but it does that on per request basis).
> Right. That is only part of it though since that tells you how many
> free pages are there. But how many of those free pages are hinted?
> That is the part we would need to track separately and then then
> compare to free_pages to determine if we need to start hinting on more
> memory or not.
 Only pages which are isolated will be hinted, and once a page is
 isolated it will not be counted in the zone free pages.
 Feel free to correct me if I am wrong.
>>> You are correct up to here. When we isolate the page it isn't counted
>>> against the free pages. However after we complete the hint we end up
>>> taking it out of isolation and returning it to the "free" state, so it
>>> will be counted against the free pages.
>>>
 If I am understanding it correctly you only want to hint the idle pages,
 is that right?
>>> Getting back to the ideas from our earlier discussion, we had 3 stages
>>> for things. Free but not hinted, isolated due to hinting, and free and
>>> hinted. So what we would need to do is identify the size of the first
>>> pool that is free and not hinted by knowing the total number of free
>>> pages, and then subtract the size of the pages that are hinted and
>>> still free.
>> To summarize, for now, I think it makes sense to stick with the current
>> approach as this way we can avoid any locking in the allocation path and
>> reduce the number of hypercalls for a bunch of MAX_ORDER - 1 page.
> I'm not sure what you are talking about by "avoid any locking in the
> allocation path". Are you talking about the spin on idle bit, if so
> then yes. 
Yeap!
> However I have been testing your patches and I was correct
> in the assumption that you forgot to handle the zone lock when you
> were freeing __free_one_page.
Yes, these are the steps other than the comments you provided in the
code. (One of them is to fix release_buddy_page())
>  I just did a quick copy/paste from your
> zone lock handling from the guest_free_page_hinting 

Re: [RFC][Patch v9 2/6] KVM: Enables the kernel to isolate guest free pages

2019-03-12 Thread Alexander Duyck
On Tue, Mar 12, 2019 at 2:53 PM David Hildenbrand  wrote:
>
> On 12.03.19 22:13, Alexander Duyck wrote:
> > On Tue, Mar 12, 2019 at 12:46 PM Nitesh Narayan Lal  
> > wrote:
> >>
> >> On 3/8/19 4:39 PM, Alexander Duyck wrote:
> >>> On Fri, Mar 8, 2019 at 11:39 AM Nitesh Narayan Lal  
> >>> wrote:
>  On 3/8/19 2:25 PM, Alexander Duyck wrote:
> > On Fri, Mar 8, 2019 at 11:10 AM Nitesh Narayan Lal  
> > wrote:
> >> On 3/8/19 1:06 PM, Alexander Duyck wrote:
> >>> On Thu, Mar 7, 2019 at 6:32 PM Michael S. Tsirkin  
> >>> wrote:
>  On Thu, Mar 07, 2019 at 02:35:53PM -0800, Alexander Duyck wrote:
> > The only other thing I still want to try and see if I can do is to 
> > add
> > a jiffies value to the page private data in the case of the buddy
> > pages.
>  Actually there's one extra thing I think we should do, and that is 
>  make
>  sure we do not leave less than X% off the free memory at a time.
>  This way chances of triggering an OOM are lower.
> >>> If nothing else we could probably look at doing a watermark of some
> >>> sort so we have to have X amount of memory free but not hinted before
> >>> we will start providing the hints. It would just be a matter of
> >>> tracking how much memory we have hinted on versus the amount of memory
> >>> that has been pulled from that pool.
> >> This is to avoid false OOM in the guest?
> > Partially, though it would still be possible. Basically it would just
> > be a way of determining when we have hinted "enough". Basically it
> > doesn't do us much good to be hinting on free memory if the guest is
> > already constrained and just going to reallocate the memory shortly
> > after we hinted on it. The idea is with a watermark we can avoid
> > hinting until we start having pages that are actually going to stay
> > free for a while.
> >
> >>>  It is another reason why we
> >>> probably want a bit in the buddy pages somewhere to indicate if a page
> >>> has been hinted or not as we can then use that to determine if we have
> >>> to account for it in the statistics.
> >> The one benefit which I can see of having an explicit bit is that it
> >> will help us to have a single hook away from the hot path within buddy
> >> merging code (just like your arch_merge_page) and still avoid duplicate
> >> hints while releasing pages.
> >>
> >> I still have to check PG_idle and PG_young which you mentioned but I
> >> don't think we can reuse any existing bits.
> > Those are bits that are already there for 64b. I think those exist in
> > the page extension for 32b systems. If I am not mistaken they are only
> > used in VMA mapped memory. What I was getting at is that those are the
> > bits we could think about reusing.
> >
> >> If we really want to have something like a watermark, then can't we use
> >> zone->free_pages before isolating to see how many free pages are there
> >> and put a threshold on it? (__isolate_free_page() does a similar thing
> >> but it does that on per request basis).
> > Right. That is only part of it though since that tells you how many
> > free pages are there. But how many of those free pages are hinted?
> > That is the part we would need to track separately and then then
> > compare to free_pages to determine if we need to start hinting on more
> > memory or not.
>  Only pages which are isolated will be hinted, and once a page is
>  isolated it will not be counted in the zone free pages.
>  Feel free to correct me if I am wrong.
> >>> You are correct up to here. When we isolate the page it isn't counted
> >>> against the free pages. However after we complete the hint we end up
> >>> taking it out of isolation and returning it to the "free" state, so it
> >>> will be counted against the free pages.
> >>>
>  If I am understanding it correctly you only want to hint the idle pages,
>  is that right?
> >>> Getting back to the ideas from our earlier discussion, we had 3 stages
> >>> for things. Free but not hinted, isolated due to hinting, and free and
> >>> hinted. So what we would need to do is identify the size of the first
> >>> pool that is free and not hinted by knowing the total number of free
> >>> pages, and then subtract the size of the pages that are hinted and
> >>> still free.
> >> To summarize, for now, I think it makes sense to stick with the current
> >> approach as this way we can avoid any locking in the allocation path and
> >> reduce the number of hypercalls for a bunch of MAX_ORDER - 1 page.
> >
> > I'm not sure what you are talking about by "avoid any locking in the
> > allocation path". Are you talking about the spin on idle bit, if so
> > then yes. However I have been testing your patches and I was correct
> > in the assumption that you forgot to handle the zone lock when you

Re: [RFC][Patch v9 2/6] KVM: Enables the kernel to isolate guest free pages

2019-03-12 Thread David Hildenbrand
On 12.03.19 22:13, Alexander Duyck wrote:
> On Tue, Mar 12, 2019 at 12:46 PM Nitesh Narayan Lal  wrote:
>>
>> On 3/8/19 4:39 PM, Alexander Duyck wrote:
>>> On Fri, Mar 8, 2019 at 11:39 AM Nitesh Narayan Lal  
>>> wrote:
 On 3/8/19 2:25 PM, Alexander Duyck wrote:
> On Fri, Mar 8, 2019 at 11:10 AM Nitesh Narayan Lal  
> wrote:
>> On 3/8/19 1:06 PM, Alexander Duyck wrote:
>>> On Thu, Mar 7, 2019 at 6:32 PM Michael S. Tsirkin  
>>> wrote:
 On Thu, Mar 07, 2019 at 02:35:53PM -0800, Alexander Duyck wrote:
> The only other thing I still want to try and see if I can do is to add
> a jiffies value to the page private data in the case of the buddy
> pages.
 Actually there's one extra thing I think we should do, and that is make
 sure we do not leave less than X% off the free memory at a time.
 This way chances of triggering an OOM are lower.
>>> If nothing else we could probably look at doing a watermark of some
>>> sort so we have to have X amount of memory free but not hinted before
>>> we will start providing the hints. It would just be a matter of
>>> tracking how much memory we have hinted on versus the amount of memory
>>> that has been pulled from that pool.
>> This is to avoid false OOM in the guest?
> Partially, though it would still be possible. Basically it would just
> be a way of determining when we have hinted "enough". Basically it
> doesn't do us much good to be hinting on free memory if the guest is
> already constrained and just going to reallocate the memory shortly
> after we hinted on it. The idea is with a watermark we can avoid
> hinting until we start having pages that are actually going to stay
> free for a while.
>
>>>  It is another reason why we
>>> probably want a bit in the buddy pages somewhere to indicate if a page
>>> has been hinted or not as we can then use that to determine if we have
>>> to account for it in the statistics.
>> The one benefit which I can see of having an explicit bit is that it
>> will help us to have a single hook away from the hot path within buddy
>> merging code (just like your arch_merge_page) and still avoid duplicate
>> hints while releasing pages.
>>
>> I still have to check PG_idle and PG_young which you mentioned but I
>> don't think we can reuse any existing bits.
> Those are bits that are already there for 64b. I think those exist in
> the page extension for 32b systems. If I am not mistaken they are only
> used in VMA mapped memory. What I was getting at is that those are the
> bits we could think about reusing.
>
>> If we really want to have something like a watermark, then can't we use
>> zone->free_pages before isolating to see how many free pages are there
>> and put a threshold on it? (__isolate_free_page() does a similar thing
>> but it does that on per request basis).
> Right. That is only part of it though since that tells you how many
> free pages are there. But how many of those free pages are hinted?
> That is the part we would need to track separately and then then
> compare to free_pages to determine if we need to start hinting on more
> memory or not.
 Only pages which are isolated will be hinted, and once a page is
 isolated it will not be counted in the zone free pages.
 Feel free to correct me if I am wrong.
>>> You are correct up to here. When we isolate the page it isn't counted
>>> against the free pages. However after we complete the hint we end up
>>> taking it out of isolation and returning it to the "free" state, so it
>>> will be counted against the free pages.
>>>
 If I am understanding it correctly you only want to hint the idle pages,
 is that right?
>>> Getting back to the ideas from our earlier discussion, we had 3 stages
>>> for things. Free but not hinted, isolated due to hinting, and free and
>>> hinted. So what we would need to do is identify the size of the first
>>> pool that is free and not hinted by knowing the total number of free
>>> pages, and then subtract the size of the pages that are hinted and
>>> still free.
>> To summarize, for now, I think it makes sense to stick with the current
>> approach as this way we can avoid any locking in the allocation path and
>> reduce the number of hypercalls for a bunch of MAX_ORDER - 1 page.
> 
> I'm not sure what you are talking about by "avoid any locking in the
> allocation path". Are you talking about the spin on idle bit, if so
> then yes. However I have been testing your patches and I was correct
> in the assumption that you forgot to handle the zone lock when you
> were freeing __free_one_page. I just did a quick copy/paste from your
> zone lock handling from the guest_free_page_hinting function into the
> release_buddy_pages function and then I was able to enable multiple
> CPUs without any issues.
> 
>> For 

Re: [RFC][Patch v9 2/6] KVM: Enables the kernel to isolate guest free pages

2019-03-12 Thread Alexander Duyck
On Tue, Mar 12, 2019 at 12:46 PM Nitesh Narayan Lal  wrote:
>
> On 3/8/19 4:39 PM, Alexander Duyck wrote:
> > On Fri, Mar 8, 2019 at 11:39 AM Nitesh Narayan Lal  
> > wrote:
> >> On 3/8/19 2:25 PM, Alexander Duyck wrote:
> >>> On Fri, Mar 8, 2019 at 11:10 AM Nitesh Narayan Lal  
> >>> wrote:
>  On 3/8/19 1:06 PM, Alexander Duyck wrote:
> > On Thu, Mar 7, 2019 at 6:32 PM Michael S. Tsirkin  
> > wrote:
> >> On Thu, Mar 07, 2019 at 02:35:53PM -0800, Alexander Duyck wrote:
> >>> The only other thing I still want to try and see if I can do is to add
> >>> a jiffies value to the page private data in the case of the buddy
> >>> pages.
> >> Actually there's one extra thing I think we should do, and that is make
> >> sure we do not leave less than X% off the free memory at a time.
> >> This way chances of triggering an OOM are lower.
> > If nothing else we could probably look at doing a watermark of some
> > sort so we have to have X amount of memory free but not hinted before
> > we will start providing the hints. It would just be a matter of
> > tracking how much memory we have hinted on versus the amount of memory
> > that has been pulled from that pool.
>  This is to avoid false OOM in the guest?
> >>> Partially, though it would still be possible. Basically it would just
> >>> be a way of determining when we have hinted "enough". Basically it
> >>> doesn't do us much good to be hinting on free memory if the guest is
> >>> already constrained and just going to reallocate the memory shortly
> >>> after we hinted on it. The idea is with a watermark we can avoid
> >>> hinting until we start having pages that are actually going to stay
> >>> free for a while.
> >>>
> >  It is another reason why we
> > probably want a bit in the buddy pages somewhere to indicate if a page
> > has been hinted or not as we can then use that to determine if we have
> > to account for it in the statistics.
>  The one benefit which I can see of having an explicit bit is that it
>  will help us to have a single hook away from the hot path within buddy
>  merging code (just like your arch_merge_page) and still avoid duplicate
>  hints while releasing pages.
> 
>  I still have to check PG_idle and PG_young which you mentioned but I
>  don't think we can reuse any existing bits.
> >>> Those are bits that are already there for 64b. I think those exist in
> >>> the page extension for 32b systems. If I am not mistaken they are only
> >>> used in VMA mapped memory. What I was getting at is that those are the
> >>> bits we could think about reusing.
> >>>
>  If we really want to have something like a watermark, then can't we use
>  zone->free_pages before isolating to see how many free pages are there
>  and put a threshold on it? (__isolate_free_page() does a similar thing
>  but it does that on per request basis).
> >>> Right. That is only part of it though since that tells you how many
> >>> free pages are there. But how many of those free pages are hinted?
> >>> That is the part we would need to track separately and then then
> >>> compare to free_pages to determine if we need to start hinting on more
> >>> memory or not.
> >> Only pages which are isolated will be hinted, and once a page is
> >> isolated it will not be counted in the zone free pages.
> >> Feel free to correct me if I am wrong.
> > You are correct up to here. When we isolate the page it isn't counted
> > against the free pages. However after we complete the hint we end up
> > taking it out of isolation and returning it to the "free" state, so it
> > will be counted against the free pages.
> >
> >> If I am understanding it correctly you only want to hint the idle pages,
> >> is that right?
> > Getting back to the ideas from our earlier discussion, we had 3 stages
> > for things. Free but not hinted, isolated due to hinting, and free and
> > hinted. So what we would need to do is identify the size of the first
> > pool that is free and not hinted by knowing the total number of free
> > pages, and then subtract the size of the pages that are hinted and
> > still free.
> To summarize, for now, I think it makes sense to stick with the current
> approach as this way we can avoid any locking in the allocation path and
> reduce the number of hypercalls for a bunch of MAX_ORDER - 1 page.

I'm not sure what you are talking about by "avoid any locking in the
allocation path". Are you talking about the spin on idle bit, if so
then yes. However I have been testing your patches and I was correct
in the assumption that you forgot to handle the zone lock when you
were freeing __free_one_page. I just did a quick copy/paste from your
zone lock handling from the guest_free_page_hinting function into the
release_buddy_pages function and then I was able to enable multiple
CPUs without any issues.

> For the next step other than the comments received in the code and what
> I 

Re: [RFC][Patch v9 2/6] KVM: Enables the kernel to isolate guest free pages

2019-03-12 Thread Nitesh Narayan Lal
On 3/8/19 4:39 PM, Alexander Duyck wrote:
> On Fri, Mar 8, 2019 at 11:39 AM Nitesh Narayan Lal  wrote:
>> On 3/8/19 2:25 PM, Alexander Duyck wrote:
>>> On Fri, Mar 8, 2019 at 11:10 AM Nitesh Narayan Lal  
>>> wrote:
 On 3/8/19 1:06 PM, Alexander Duyck wrote:
> On Thu, Mar 7, 2019 at 6:32 PM Michael S. Tsirkin  wrote:
>> On Thu, Mar 07, 2019 at 02:35:53PM -0800, Alexander Duyck wrote:
>>> The only other thing I still want to try and see if I can do is to add
>>> a jiffies value to the page private data in the case of the buddy
>>> pages.
>> Actually there's one extra thing I think we should do, and that is make
>> sure we do not leave less than X% off the free memory at a time.
>> This way chances of triggering an OOM are lower.
> If nothing else we could probably look at doing a watermark of some
> sort so we have to have X amount of memory free but not hinted before
> we will start providing the hints. It would just be a matter of
> tracking how much memory we have hinted on versus the amount of memory
> that has been pulled from that pool.
 This is to avoid false OOM in the guest?
>>> Partially, though it would still be possible. Basically it would just
>>> be a way of determining when we have hinted "enough". Basically it
>>> doesn't do us much good to be hinting on free memory if the guest is
>>> already constrained and just going to reallocate the memory shortly
>>> after we hinted on it. The idea is with a watermark we can avoid
>>> hinting until we start having pages that are actually going to stay
>>> free for a while.
>>>
>  It is another reason why we
> probably want a bit in the buddy pages somewhere to indicate if a page
> has been hinted or not as we can then use that to determine if we have
> to account for it in the statistics.
 The one benefit which I can see of having an explicit bit is that it
 will help us to have a single hook away from the hot path within buddy
 merging code (just like your arch_merge_page) and still avoid duplicate
 hints while releasing pages.

 I still have to check PG_idle and PG_young which you mentioned but I
 don't think we can reuse any existing bits.
>>> Those are bits that are already there for 64b. I think those exist in
>>> the page extension for 32b systems. If I am not mistaken they are only
>>> used in VMA mapped memory. What I was getting at is that those are the
>>> bits we could think about reusing.
>>>
 If we really want to have something like a watermark, then can't we use
 zone->free_pages before isolating to see how many free pages are there
 and put a threshold on it? (__isolate_free_page() does a similar thing
 but it does that on per request basis).
>>> Right. That is only part of it though since that tells you how many
>>> free pages are there. But how many of those free pages are hinted?
>>> That is the part we would need to track separately and then then
>>> compare to free_pages to determine if we need to start hinting on more
>>> memory or not.
>> Only pages which are isolated will be hinted, and once a page is
>> isolated it will not be counted in the zone free pages.
>> Feel free to correct me if I am wrong.
> You are correct up to here. When we isolate the page it isn't counted
> against the free pages. However after we complete the hint we end up
> taking it out of isolation and returning it to the "free" state, so it
> will be counted against the free pages.
>
>> If I am understanding it correctly you only want to hint the idle pages,
>> is that right?
> Getting back to the ideas from our earlier discussion, we had 3 stages
> for things. Free but not hinted, isolated due to hinting, and free and
> hinted. So what we would need to do is identify the size of the first
> pool that is free and not hinted by knowing the total number of free
> pages, and then subtract the size of the pages that are hinted and
> still free.
To summarize, for now, I think it makes sense to stick with the current
approach as this way we can avoid any locking in the allocation path and
reduce the number of hypercalls for a bunch of MAX_ORDER - 1 page.
For the next step other than the comments received in the code and what
I mentioned in the cover email, I would like to do the following:
1. Explore the watermark idea suggested by Alex and bring down memhog
execution time if possible.
2. Benchmark hinting v/s non-hinting more extensively.
Let me know if you have any specific suggestions in terms of the tools I
can run to do the same. (I am planning to run atleast netperf, hackbench
and stress for this).

>
>>> With that we could track the age of the page so it becomes
>>> easier to only target pages that are truly going cold rather than
>>> trying to grab pages that were added to the freelist recently.
>> I like that but I have a vague memory of discussing this with Rik van
>> Riel and him saying it's actually better to take 

Re: [RFC][Patch v9 2/6] KVM: Enables the kernel to isolate guest free pages

2019-03-08 Thread Alexander Duyck
On Fri, Mar 8, 2019 at 11:39 AM Nitesh Narayan Lal  wrote:
>
> On 3/8/19 2:25 PM, Alexander Duyck wrote:
> > On Fri, Mar 8, 2019 at 11:10 AM Nitesh Narayan Lal  
> > wrote:
> >>
> >> On 3/8/19 1:06 PM, Alexander Duyck wrote:
> >>> On Thu, Mar 7, 2019 at 6:32 PM Michael S. Tsirkin  wrote:
>  On Thu, Mar 07, 2019 at 02:35:53PM -0800, Alexander Duyck wrote:
> > The only other thing I still want to try and see if I can do is to add
> > a jiffies value to the page private data in the case of the buddy
> > pages.
>  Actually there's one extra thing I think we should do, and that is make
>  sure we do not leave less than X% off the free memory at a time.
>  This way chances of triggering an OOM are lower.
> >>> If nothing else we could probably look at doing a watermark of some
> >>> sort so we have to have X amount of memory free but not hinted before
> >>> we will start providing the hints. It would just be a matter of
> >>> tracking how much memory we have hinted on versus the amount of memory
> >>> that has been pulled from that pool.
> >> This is to avoid false OOM in the guest?
> > Partially, though it would still be possible. Basically it would just
> > be a way of determining when we have hinted "enough". Basically it
> > doesn't do us much good to be hinting on free memory if the guest is
> > already constrained and just going to reallocate the memory shortly
> > after we hinted on it. The idea is with a watermark we can avoid
> > hinting until we start having pages that are actually going to stay
> > free for a while.
> >
> >>>  It is another reason why we
> >>> probably want a bit in the buddy pages somewhere to indicate if a page
> >>> has been hinted or not as we can then use that to determine if we have
> >>> to account for it in the statistics.
> >> The one benefit which I can see of having an explicit bit is that it
> >> will help us to have a single hook away from the hot path within buddy
> >> merging code (just like your arch_merge_page) and still avoid duplicate
> >> hints while releasing pages.
> >>
> >> I still have to check PG_idle and PG_young which you mentioned but I
> >> don't think we can reuse any existing bits.
> > Those are bits that are already there for 64b. I think those exist in
> > the page extension for 32b systems. If I am not mistaken they are only
> > used in VMA mapped memory. What I was getting at is that those are the
> > bits we could think about reusing.
> >
> >> If we really want to have something like a watermark, then can't we use
> >> zone->free_pages before isolating to see how many free pages are there
> >> and put a threshold on it? (__isolate_free_page() does a similar thing
> >> but it does that on per request basis).
> > Right. That is only part of it though since that tells you how many
> > free pages are there. But how many of those free pages are hinted?
> > That is the part we would need to track separately and then then
> > compare to free_pages to determine if we need to start hinting on more
> > memory or not.
> Only pages which are isolated will be hinted, and once a page is
> isolated it will not be counted in the zone free pages.
> Feel free to correct me if I am wrong.

You are correct up to here. When we isolate the page it isn't counted
against the free pages. However after we complete the hint we end up
taking it out of isolation and returning it to the "free" state, so it
will be counted against the free pages.

> If I am understanding it correctly you only want to hint the idle pages,
> is that right?

Getting back to the ideas from our earlier discussion, we had 3 stages
for things. Free but not hinted, isolated due to hinting, and free and
hinted. So what we would need to do is identify the size of the first
pool that is free and not hinted by knowing the total number of free
pages, and then subtract the size of the pages that are hinted and
still free.

> >
> > With that we could track the age of the page so it becomes
> > easier to only target pages that are truly going cold rather than
> > trying to grab pages that were added to the freelist recently.
>  I like that but I have a vague memory of discussing this with Rik van
>  Riel and him saying it's actually better to take away recently used
>  ones. Can't see why would that be but maybe I remember wrong. Rik - am I
>  just confused?
> >>> It is probably to cut down on the need for disk writes in the case of
> >>> swap. If that is the case it ends up being a trade off.
> >>>
> >>> The sooner we hint the less likely it is that we will need to write a
> >>> given page to disk. However the sooner we hint, the more likely it is
> >>> we will need to trigger a page fault and pull back in a zero page to
> >>> populate the last page we were working on. The sweet spot will be that
> >>> period of time that is somewhere in between so we don't trigger
> >>> unnecessary page faults and we don't need to perform additional swap
> >>> 

Re: [RFC][Patch v9 2/6] KVM: Enables the kernel to isolate guest free pages

2019-03-08 Thread Nitesh Narayan Lal
On 3/8/19 2:25 PM, Alexander Duyck wrote:
> On Fri, Mar 8, 2019 at 11:10 AM Nitesh Narayan Lal  wrote:
>>
>> On 3/8/19 1:06 PM, Alexander Duyck wrote:
>>> On Thu, Mar 7, 2019 at 6:32 PM Michael S. Tsirkin  wrote:
 On Thu, Mar 07, 2019 at 02:35:53PM -0800, Alexander Duyck wrote:
> The only other thing I still want to try and see if I can do is to add
> a jiffies value to the page private data in the case of the buddy
> pages.
 Actually there's one extra thing I think we should do, and that is make
 sure we do not leave less than X% off the free memory at a time.
 This way chances of triggering an OOM are lower.
>>> If nothing else we could probably look at doing a watermark of some
>>> sort so we have to have X amount of memory free but not hinted before
>>> we will start providing the hints. It would just be a matter of
>>> tracking how much memory we have hinted on versus the amount of memory
>>> that has been pulled from that pool.
>> This is to avoid false OOM in the guest?
> Partially, though it would still be possible. Basically it would just
> be a way of determining when we have hinted "enough". Basically it
> doesn't do us much good to be hinting on free memory if the guest is
> already constrained and just going to reallocate the memory shortly
> after we hinted on it. The idea is with a watermark we can avoid
> hinting until we start having pages that are actually going to stay
> free for a while.
>
>>>  It is another reason why we
>>> probably want a bit in the buddy pages somewhere to indicate if a page
>>> has been hinted or not as we can then use that to determine if we have
>>> to account for it in the statistics.
>> The one benefit which I can see of having an explicit bit is that it
>> will help us to have a single hook away from the hot path within buddy
>> merging code (just like your arch_merge_page) and still avoid duplicate
>> hints while releasing pages.
>>
>> I still have to check PG_idle and PG_young which you mentioned but I
>> don't think we can reuse any existing bits.
> Those are bits that are already there for 64b. I think those exist in
> the page extension for 32b systems. If I am not mistaken they are only
> used in VMA mapped memory. What I was getting at is that those are the
> bits we could think about reusing.
>
>> If we really want to have something like a watermark, then can't we use
>> zone->free_pages before isolating to see how many free pages are there
>> and put a threshold on it? (__isolate_free_page() does a similar thing
>> but it does that on per request basis).
> Right. That is only part of it though since that tells you how many
> free pages are there. But how many of those free pages are hinted?
> That is the part we would need to track separately and then then
> compare to free_pages to determine if we need to start hinting on more
> memory or not.
Only pages which are isolated will be hinted, and once a page is
isolated it will not be counted in the zone free pages.
Feel free to correct me if I am wrong.
If I am understanding it correctly you only want to hint the idle pages,
is that right?
>
> With that we could track the age of the page so it becomes
> easier to only target pages that are truly going cold rather than
> trying to grab pages that were added to the freelist recently.
 I like that but I have a vague memory of discussing this with Rik van
 Riel and him saying it's actually better to take away recently used
 ones. Can't see why would that be but maybe I remember wrong. Rik - am I
 just confused?
>>> It is probably to cut down on the need for disk writes in the case of
>>> swap. If that is the case it ends up being a trade off.
>>>
>>> The sooner we hint the less likely it is that we will need to write a
>>> given page to disk. However the sooner we hint, the more likely it is
>>> we will need to trigger a page fault and pull back in a zero page to
>>> populate the last page we were working on. The sweet spot will be that
>>> period of time that is somewhere in between so we don't trigger
>>> unnecessary page faults and we don't need to perform additional swap
>>> reads/writes.
>> --
>> Regards
>> Nitesh
>>
-- 
Regards
Nitesh



signature.asc
Description: OpenPGP digital signature


Re: [RFC][Patch v9 2/6] KVM: Enables the kernel to isolate guest free pages

2019-03-08 Thread Alexander Duyck
On Fri, Mar 8, 2019 at 11:10 AM Nitesh Narayan Lal  wrote:
>
>
> On 3/8/19 1:06 PM, Alexander Duyck wrote:
> > On Thu, Mar 7, 2019 at 6:32 PM Michael S. Tsirkin  wrote:
> >> On Thu, Mar 07, 2019 at 02:35:53PM -0800, Alexander Duyck wrote:
> >>> The only other thing I still want to try and see if I can do is to add
> >>> a jiffies value to the page private data in the case of the buddy
> >>> pages.
> >> Actually there's one extra thing I think we should do, and that is make
> >> sure we do not leave less than X% off the free memory at a time.
> >> This way chances of triggering an OOM are lower.
> > If nothing else we could probably look at doing a watermark of some
> > sort so we have to have X amount of memory free but not hinted before
> > we will start providing the hints. It would just be a matter of
> > tracking how much memory we have hinted on versus the amount of memory
> > that has been pulled from that pool.
> This is to avoid false OOM in the guest?

Partially, though it would still be possible. Basically it would just
be a way of determining when we have hinted "enough". Basically it
doesn't do us much good to be hinting on free memory if the guest is
already constrained and just going to reallocate the memory shortly
after we hinted on it. The idea is with a watermark we can avoid
hinting until we start having pages that are actually going to stay
free for a while.

> >  It is another reason why we
> > probably want a bit in the buddy pages somewhere to indicate if a page
> > has been hinted or not as we can then use that to determine if we have
> > to account for it in the statistics.
>
> The one benefit which I can see of having an explicit bit is that it
> will help us to have a single hook away from the hot path within buddy
> merging code (just like your arch_merge_page) and still avoid duplicate
> hints while releasing pages.
>
> I still have to check PG_idle and PG_young which you mentioned but I
> don't think we can reuse any existing bits.

Those are bits that are already there for 64b. I think those exist in
the page extension for 32b systems. If I am not mistaken they are only
used in VMA mapped memory. What I was getting at is that those are the
bits we could think about reusing.

> If we really want to have something like a watermark, then can't we use
> zone->free_pages before isolating to see how many free pages are there
> and put a threshold on it? (__isolate_free_page() does a similar thing
> but it does that on per request basis).

Right. That is only part of it though since that tells you how many
free pages are there. But how many of those free pages are hinted?
That is the part we would need to track separately and then then
compare to free_pages to determine if we need to start hinting on more
memory or not.

> >
> >>> With that we could track the age of the page so it becomes
> >>> easier to only target pages that are truly going cold rather than
> >>> trying to grab pages that were added to the freelist recently.
> >> I like that but I have a vague memory of discussing this with Rik van
> >> Riel and him saying it's actually better to take away recently used
> >> ones. Can't see why would that be but maybe I remember wrong. Rik - am I
> >> just confused?
> > It is probably to cut down on the need for disk writes in the case of
> > swap. If that is the case it ends up being a trade off.
> >
> > The sooner we hint the less likely it is that we will need to write a
> > given page to disk. However the sooner we hint, the more likely it is
> > we will need to trigger a page fault and pull back in a zero page to
> > populate the last page we were working on. The sweet spot will be that
> > period of time that is somewhere in between so we don't trigger
> > unnecessary page faults and we don't need to perform additional swap
> > reads/writes.
> --
> Regards
> Nitesh
>


Re: [RFC][Patch v9 2/6] KVM: Enables the kernel to isolate guest free pages

2019-03-08 Thread Nitesh Narayan Lal

On 3/8/19 1:06 PM, Alexander Duyck wrote:
> On Thu, Mar 7, 2019 at 6:32 PM Michael S. Tsirkin  wrote:
>> On Thu, Mar 07, 2019 at 02:35:53PM -0800, Alexander Duyck wrote:
>>> The only other thing I still want to try and see if I can do is to add
>>> a jiffies value to the page private data in the case of the buddy
>>> pages.
>> Actually there's one extra thing I think we should do, and that is make
>> sure we do not leave less than X% off the free memory at a time.
>> This way chances of triggering an OOM are lower.
> If nothing else we could probably look at doing a watermark of some
> sort so we have to have X amount of memory free but not hinted before
> we will start providing the hints. It would just be a matter of
> tracking how much memory we have hinted on versus the amount of memory
> that has been pulled from that pool.
This is to avoid false OOM in the guest?
>  It is another reason why we
> probably want a bit in the buddy pages somewhere to indicate if a page
> has been hinted or not as we can then use that to determine if we have
> to account for it in the statistics.

The one benefit which I can see of having an explicit bit is that it
will help us to have a single hook away from the hot path within buddy
merging code (just like your arch_merge_page) and still avoid duplicate
hints while releasing pages.

I still have to check PG_idle and PG_young which you mentioned but I
don't think we can reuse any existing bits.

If we really want to have something like a watermark, then can't we use
zone->free_pages before isolating to see how many free pages are there
and put a threshold on it? (__isolate_free_page() does a similar thing
but it does that on per request basis).

>
>>> With that we could track the age of the page so it becomes
>>> easier to only target pages that are truly going cold rather than
>>> trying to grab pages that were added to the freelist recently.
>> I like that but I have a vague memory of discussing this with Rik van
>> Riel and him saying it's actually better to take away recently used
>> ones. Can't see why would that be but maybe I remember wrong. Rik - am I
>> just confused?
> It is probably to cut down on the need for disk writes in the case of
> swap. If that is the case it ends up being a trade off.
>
> The sooner we hint the less likely it is that we will need to write a
> given page to disk. However the sooner we hint, the more likely it is
> we will need to trigger a page fault and pull back in a zero page to
> populate the last page we were working on. The sweet spot will be that
> period of time that is somewhere in between so we don't trigger
> unnecessary page faults and we don't need to perform additional swap
> reads/writes.
-- 
Regards
Nitesh



signature.asc
Description: OpenPGP digital signature


Re: [RFC][Patch v9 2/6] KVM: Enables the kernel to isolate guest free pages

2019-03-08 Thread Michael S. Tsirkin
On Fri, Mar 08, 2019 at 10:06:14AM -0800, Alexander Duyck wrote:
> On Thu, Mar 7, 2019 at 6:32 PM Michael S. Tsirkin  wrote:
> >
> > On Thu, Mar 07, 2019 at 02:35:53PM -0800, Alexander Duyck wrote:
> > > The only other thing I still want to try and see if I can do is to add
> > > a jiffies value to the page private data in the case of the buddy
> > > pages.
> >
> > Actually there's one extra thing I think we should do, and that is make
> > sure we do not leave less than X% off the free memory at a time.
> > This way chances of triggering an OOM are lower.
> 
> If nothing else we could probably look at doing a watermark of some
> sort so we have to have X amount of memory free but not hinted before
> we will start providing the hints. It would just be a matter of
> tracking how much memory we have hinted on versus the amount of memory
> that has been pulled from that pool. It is another reason why we
> probably want a bit in the buddy pages somewhere to indicate if a page
> has been hinted or not as we can then use that to determine if we have
> to account for it in the statistics.
> 
> > > With that we could track the age of the page so it becomes
> > > easier to only target pages that are truly going cold rather than
> > > trying to grab pages that were added to the freelist recently.
> >
> > I like that but I have a vague memory of discussing this with Rik van
> > Riel and him saying it's actually better to take away recently used
> > ones. Can't see why would that be but maybe I remember wrong. Rik - am I
> > just confused?
> 
> It is probably to cut down on the need for disk writes in the case of
> swap. If that is the case it ends up being a trade off.
> 
> The sooner we hint the less likely it is that we will need to write a
> given page to disk. However the sooner we hint, the more likely it is
> we will need to trigger a page fault and pull back in a zero page to
> populate the last page we were working on. The sweet spot will be that
> period of time that is somewhere in between so we don't trigger
> unnecessary page faults and we don't need to perform additional swap
> reads/writes.

Right but the question is - is it better to hint on
least recently used, or most recently used pages?
It looks like LRU should be better, but I vaguely rememeber there
were arguments for why most recently used might be better.
Can't figure out why, maybe I am remembering wrong.

-- 
MST


Re: [RFC][Patch v9 2/6] KVM: Enables the kernel to isolate guest free pages

2019-03-08 Thread Alexander Duyck
On Thu, Mar 7, 2019 at 6:32 PM Michael S. Tsirkin  wrote:
>
> On Thu, Mar 07, 2019 at 02:35:53PM -0800, Alexander Duyck wrote:
> > The only other thing I still want to try and see if I can do is to add
> > a jiffies value to the page private data in the case of the buddy
> > pages.
>
> Actually there's one extra thing I think we should do, and that is make
> sure we do not leave less than X% off the free memory at a time.
> This way chances of triggering an OOM are lower.

If nothing else we could probably look at doing a watermark of some
sort so we have to have X amount of memory free but not hinted before
we will start providing the hints. It would just be a matter of
tracking how much memory we have hinted on versus the amount of memory
that has been pulled from that pool. It is another reason why we
probably want a bit in the buddy pages somewhere to indicate if a page
has been hinted or not as we can then use that to determine if we have
to account for it in the statistics.

> > With that we could track the age of the page so it becomes
> > easier to only target pages that are truly going cold rather than
> > trying to grab pages that were added to the freelist recently.
>
> I like that but I have a vague memory of discussing this with Rik van
> Riel and him saying it's actually better to take away recently used
> ones. Can't see why would that be but maybe I remember wrong. Rik - am I
> just confused?

It is probably to cut down on the need for disk writes in the case of
swap. If that is the case it ends up being a trade off.

The sooner we hint the less likely it is that we will need to write a
given page to disk. However the sooner we hint, the more likely it is
we will need to trigger a page fault and pull back in a zero page to
populate the last page we were working on. The sweet spot will be that
period of time that is somewhere in between so we don't trigger
unnecessary page faults and we don't need to perform additional swap
reads/writes.


Re: [RFC][Patch v9 2/6] KVM: Enables the kernel to isolate guest free pages

2019-03-07 Thread Michael S. Tsirkin
On Thu, Mar 07, 2019 at 02:35:53PM -0800, Alexander Duyck wrote:
> The only other thing I still want to try and see if I can do is to add
> a jiffies value to the page private data in the case of the buddy
> pages.

Actually there's one extra thing I think we should do, and that is make
sure we do not leave less than X% off the free memory at a time.
This way chances of triggering an OOM are lower.

> With that we could track the age of the page so it becomes
> easier to only target pages that are truly going cold rather than
> trying to grab pages that were added to the freelist recently.

I like that but I have a vague memory of discussing this with Rik van
Riel and him saying it's actually better to take away recently used
ones. Can't see why would that be but maybe I remember wrong. Rik - am I
just confused?


-- 
MST


Re: [RFC][Patch v9 2/6] KVM: Enables the kernel to isolate guest free pages

2019-03-07 Thread Michael S. Tsirkin
On Thu, Mar 07, 2019 at 02:35:53PM -0800, Alexander Duyck wrote:
> With us hinting currently on MAX_ORDER - 1 pages only that actually
> takes care of the risk of a merge really wiping out any data about
> what has been hinted on and what hasn't.

Oh nice. I had this feeling MAX_ORDER - 1 specifically will
turn out being a better choice than something related to THP.
Now there's an actual reason why this makes things easier!

-- 
MST


Re: [RFC][Patch v9 2/6] KVM: Enables the kernel to isolate guest free pages

2019-03-07 Thread Alexander Duyck
On Thu, Mar 7, 2019 at 1:40 PM David Hildenbrand  wrote:
>
> On 07.03.19 22:32, Alexander Duyck wrote:
> > On Thu, Mar 7, 2019 at 11:30 AM David Hildenbrand  wrote:
> >>
> >> On 07.03.19 20:23, Nitesh Narayan Lal wrote:
> >>>
> >>> On 3/7/19 1:30 PM, Alexander Duyck wrote:
>  On Wed, Mar 6, 2019 at 7:51 AM Nitesh Narayan Lal  
>  wrote:
> > This patch enables the kernel to scan the per cpu array
> > which carries head pages from the buddy free list of order
> > FREE_PAGE_HINTING_MIN_ORDER (MAX_ORDER - 1) by
> > guest_free_page_hinting().
> > guest_free_page_hinting() scans the entire per cpu array by
> > acquiring a zone lock corresponding to the pages which are
> > being scanned. If the page is still free and present in the
> > buddy it tries to isolate the page and adds it to a
> > dynamically allocated array.
> >
> > Once this scanning process is complete and if there are any
> > isolated pages added to the dynamically allocated array
> > guest_free_page_report() is invoked. However, before this the
> > per-cpu array index is reset so that it can continue capturing
> > the pages from buddy free list.
> >
> > In this patch guest_free_page_report() simply releases the pages back
> > to the buddy by using __free_one_page()
> >
> > Signed-off-by: Nitesh Narayan Lal 
>  I'm pretty sure this code is not thread safe and has a few various 
>  issues.
> 
> > ---
> >  include/linux/page_hinting.h |   5 ++
> >  mm/page_alloc.c  |   2 +-
> >  virt/kvm/page_hinting.c  | 154 +++
> >  3 files changed, 160 insertions(+), 1 deletion(-)
> >
> > diff --git a/include/linux/page_hinting.h b/include/linux/page_hinting.h
> > index 90254c582789..d554a2581826 100644
> > --- a/include/linux/page_hinting.h
> > +++ b/include/linux/page_hinting.h
> > @@ -13,3 +13,8 @@
> >
> >  void guest_free_page_enqueue(struct page *page, int order);
> >  void guest_free_page_try_hinting(void);
> > +extern int __isolate_free_page(struct page *page, unsigned int order);
> > +extern void __free_one_page(struct page *page, unsigned long pfn,
> > +   struct zone *zone, unsigned int order,
> > +   int migratetype);
> > +void release_buddy_pages(void *obj_to_free, int entries);
> > diff --git a/mm/page_alloc.c b/mm/page_alloc.c
> > index 684d047f33ee..d38b7eea207b 100644
> > --- a/mm/page_alloc.c
> > +++ b/mm/page_alloc.c
> > @@ -814,7 +814,7 @@ static inline int page_is_buddy(struct page *page, 
> > struct page *buddy,
> >   * -- nyc
> >   */
> >
> > -static inline void __free_one_page(struct page *page,
> > +inline void __free_one_page(struct page *page,
> > unsigned long pfn,
> > struct zone *zone, unsigned int order,
> > int migratetype)
> > diff --git a/virt/kvm/page_hinting.c b/virt/kvm/page_hinting.c
> > index 48b4b5e796b0..9885b372b5a9 100644
> > --- a/virt/kvm/page_hinting.c
> > +++ b/virt/kvm/page_hinting.c
> > @@ -1,5 +1,9 @@
> >  #include 
> >  #include 
> > +#include 
> > +#include 
> > +#include 
> > +#include 
> >
> >  /*
> >   * struct guest_free_pages- holds array of guest freed PFN's along 
> > with an
> > @@ -16,6 +20,54 @@ struct guest_free_pages {
> >
> >  DEFINE_PER_CPU(struct guest_free_pages, free_pages_obj);
> >
> > +/*
> > + * struct guest_isolated_pages- holds the buddy isolated pages which 
> > are
> > + * supposed to be freed by the host.
> > + * @pfn: page frame number for the isolated page.
> > + * @order: order of the isolated page.
> > + */
> > +struct guest_isolated_pages {
> > +   unsigned long pfn;
> > +   unsigned int order;
> > +};
> > +
> > +void release_buddy_pages(void *obj_to_free, int entries)
> > +{
> > +   int i = 0;
> > +   int mt = 0;
> > +   struct guest_isolated_pages *isolated_pages_obj = obj_to_free;
> > +
> > +   while (i < entries) {
> > +   struct page *page = 
> > pfn_to_page(isolated_pages_obj[i].pfn);
> > +
> > +   mt = get_pageblock_migratetype(page);
> > +   __free_one_page(page, page_to_pfn(page), 
> > page_zone(page),
> > +   isolated_pages_obj[i].order, mt);
> > +   i++;
> > +   }
> > +   kfree(isolated_pages_obj);
> > +}
>  You shouldn't be accessing __free_one_page without holding the zone
>  lock for the page. You might consider confining yourself to one zone
>  worth of hints at a time. Then you can acquire the lock once, and then
>  return the memory you have freed.
> >>> That is correct.
> 
>  

Re: [RFC][Patch v9 2/6] KVM: Enables the kernel to isolate guest free pages

2019-03-07 Thread David Hildenbrand
On 07.03.19 22:32, Alexander Duyck wrote:
> On Thu, Mar 7, 2019 at 11:30 AM David Hildenbrand  wrote:
>>
>> On 07.03.19 20:23, Nitesh Narayan Lal wrote:
>>>
>>> On 3/7/19 1:30 PM, Alexander Duyck wrote:
 On Wed, Mar 6, 2019 at 7:51 AM Nitesh Narayan Lal  
 wrote:
> This patch enables the kernel to scan the per cpu array
> which carries head pages from the buddy free list of order
> FREE_PAGE_HINTING_MIN_ORDER (MAX_ORDER - 1) by
> guest_free_page_hinting().
> guest_free_page_hinting() scans the entire per cpu array by
> acquiring a zone lock corresponding to the pages which are
> being scanned. If the page is still free and present in the
> buddy it tries to isolate the page and adds it to a
> dynamically allocated array.
>
> Once this scanning process is complete and if there are any
> isolated pages added to the dynamically allocated array
> guest_free_page_report() is invoked. However, before this the
> per-cpu array index is reset so that it can continue capturing
> the pages from buddy free list.
>
> In this patch guest_free_page_report() simply releases the pages back
> to the buddy by using __free_one_page()
>
> Signed-off-by: Nitesh Narayan Lal 
 I'm pretty sure this code is not thread safe and has a few various issues.

> ---
>  include/linux/page_hinting.h |   5 ++
>  mm/page_alloc.c  |   2 +-
>  virt/kvm/page_hinting.c  | 154 +++
>  3 files changed, 160 insertions(+), 1 deletion(-)
>
> diff --git a/include/linux/page_hinting.h b/include/linux/page_hinting.h
> index 90254c582789..d554a2581826 100644
> --- a/include/linux/page_hinting.h
> +++ b/include/linux/page_hinting.h
> @@ -13,3 +13,8 @@
>
>  void guest_free_page_enqueue(struct page *page, int order);
>  void guest_free_page_try_hinting(void);
> +extern int __isolate_free_page(struct page *page, unsigned int order);
> +extern void __free_one_page(struct page *page, unsigned long pfn,
> +   struct zone *zone, unsigned int order,
> +   int migratetype);
> +void release_buddy_pages(void *obj_to_free, int entries);
> diff --git a/mm/page_alloc.c b/mm/page_alloc.c
> index 684d047f33ee..d38b7eea207b 100644
> --- a/mm/page_alloc.c
> +++ b/mm/page_alloc.c
> @@ -814,7 +814,7 @@ static inline int page_is_buddy(struct page *page, 
> struct page *buddy,
>   * -- nyc
>   */
>
> -static inline void __free_one_page(struct page *page,
> +inline void __free_one_page(struct page *page,
> unsigned long pfn,
> struct zone *zone, unsigned int order,
> int migratetype)
> diff --git a/virt/kvm/page_hinting.c b/virt/kvm/page_hinting.c
> index 48b4b5e796b0..9885b372b5a9 100644
> --- a/virt/kvm/page_hinting.c
> +++ b/virt/kvm/page_hinting.c
> @@ -1,5 +1,9 @@
>  #include 
>  #include 
> +#include 
> +#include 
> +#include 
> +#include 
>
>  /*
>   * struct guest_free_pages- holds array of guest freed PFN's along with 
> an
> @@ -16,6 +20,54 @@ struct guest_free_pages {
>
>  DEFINE_PER_CPU(struct guest_free_pages, free_pages_obj);
>
> +/*
> + * struct guest_isolated_pages- holds the buddy isolated pages which are
> + * supposed to be freed by the host.
> + * @pfn: page frame number for the isolated page.
> + * @order: order of the isolated page.
> + */
> +struct guest_isolated_pages {
> +   unsigned long pfn;
> +   unsigned int order;
> +};
> +
> +void release_buddy_pages(void *obj_to_free, int entries)
> +{
> +   int i = 0;
> +   int mt = 0;
> +   struct guest_isolated_pages *isolated_pages_obj = obj_to_free;
> +
> +   while (i < entries) {
> +   struct page *page = 
> pfn_to_page(isolated_pages_obj[i].pfn);
> +
> +   mt = get_pageblock_migratetype(page);
> +   __free_one_page(page, page_to_pfn(page), page_zone(page),
> +   isolated_pages_obj[i].order, mt);
> +   i++;
> +   }
> +   kfree(isolated_pages_obj);
> +}
 You shouldn't be accessing __free_one_page without holding the zone
 lock for the page. You might consider confining yourself to one zone
 worth of hints at a time. Then you can acquire the lock once, and then
 return the memory you have freed.
>>> That is correct.

 This is one of the reasons why I am thinking maybe a bit in the page
 and then spinning on that bit in arch_alloc_page might be a nice way
 to get around this. Then you only have to take the zone lock when you
 are finding the pages you want to hint on and setting the bit
 indicating they are mid hint. 

Re: [RFC][Patch v9 2/6] KVM: Enables the kernel to isolate guest free pages

2019-03-07 Thread Alexander Duyck
On Thu, Mar 7, 2019 at 11:30 AM David Hildenbrand  wrote:
>
> On 07.03.19 20:23, Nitesh Narayan Lal wrote:
> >
> > On 3/7/19 1:30 PM, Alexander Duyck wrote:
> >> On Wed, Mar 6, 2019 at 7:51 AM Nitesh Narayan Lal  
> >> wrote:
> >>> This patch enables the kernel to scan the per cpu array
> >>> which carries head pages from the buddy free list of order
> >>> FREE_PAGE_HINTING_MIN_ORDER (MAX_ORDER - 1) by
> >>> guest_free_page_hinting().
> >>> guest_free_page_hinting() scans the entire per cpu array by
> >>> acquiring a zone lock corresponding to the pages which are
> >>> being scanned. If the page is still free and present in the
> >>> buddy it tries to isolate the page and adds it to a
> >>> dynamically allocated array.
> >>>
> >>> Once this scanning process is complete and if there are any
> >>> isolated pages added to the dynamically allocated array
> >>> guest_free_page_report() is invoked. However, before this the
> >>> per-cpu array index is reset so that it can continue capturing
> >>> the pages from buddy free list.
> >>>
> >>> In this patch guest_free_page_report() simply releases the pages back
> >>> to the buddy by using __free_one_page()
> >>>
> >>> Signed-off-by: Nitesh Narayan Lal 
> >> I'm pretty sure this code is not thread safe and has a few various issues.
> >>
> >>> ---
> >>>  include/linux/page_hinting.h |   5 ++
> >>>  mm/page_alloc.c  |   2 +-
> >>>  virt/kvm/page_hinting.c  | 154 +++
> >>>  3 files changed, 160 insertions(+), 1 deletion(-)
> >>>
> >>> diff --git a/include/linux/page_hinting.h b/include/linux/page_hinting.h
> >>> index 90254c582789..d554a2581826 100644
> >>> --- a/include/linux/page_hinting.h
> >>> +++ b/include/linux/page_hinting.h
> >>> @@ -13,3 +13,8 @@
> >>>
> >>>  void guest_free_page_enqueue(struct page *page, int order);
> >>>  void guest_free_page_try_hinting(void);
> >>> +extern int __isolate_free_page(struct page *page, unsigned int order);
> >>> +extern void __free_one_page(struct page *page, unsigned long pfn,
> >>> +   struct zone *zone, unsigned int order,
> >>> +   int migratetype);
> >>> +void release_buddy_pages(void *obj_to_free, int entries);
> >>> diff --git a/mm/page_alloc.c b/mm/page_alloc.c
> >>> index 684d047f33ee..d38b7eea207b 100644
> >>> --- a/mm/page_alloc.c
> >>> +++ b/mm/page_alloc.c
> >>> @@ -814,7 +814,7 @@ static inline int page_is_buddy(struct page *page, 
> >>> struct page *buddy,
> >>>   * -- nyc
> >>>   */
> >>>
> >>> -static inline void __free_one_page(struct page *page,
> >>> +inline void __free_one_page(struct page *page,
> >>> unsigned long pfn,
> >>> struct zone *zone, unsigned int order,
> >>> int migratetype)
> >>> diff --git a/virt/kvm/page_hinting.c b/virt/kvm/page_hinting.c
> >>> index 48b4b5e796b0..9885b372b5a9 100644
> >>> --- a/virt/kvm/page_hinting.c
> >>> +++ b/virt/kvm/page_hinting.c
> >>> @@ -1,5 +1,9 @@
> >>>  #include 
> >>>  #include 
> >>> +#include 
> >>> +#include 
> >>> +#include 
> >>> +#include 
> >>>
> >>>  /*
> >>>   * struct guest_free_pages- holds array of guest freed PFN's along with 
> >>> an
> >>> @@ -16,6 +20,54 @@ struct guest_free_pages {
> >>>
> >>>  DEFINE_PER_CPU(struct guest_free_pages, free_pages_obj);
> >>>
> >>> +/*
> >>> + * struct guest_isolated_pages- holds the buddy isolated pages which are
> >>> + * supposed to be freed by the host.
> >>> + * @pfn: page frame number for the isolated page.
> >>> + * @order: order of the isolated page.
> >>> + */
> >>> +struct guest_isolated_pages {
> >>> +   unsigned long pfn;
> >>> +   unsigned int order;
> >>> +};
> >>> +
> >>> +void release_buddy_pages(void *obj_to_free, int entries)
> >>> +{
> >>> +   int i = 0;
> >>> +   int mt = 0;
> >>> +   struct guest_isolated_pages *isolated_pages_obj = obj_to_free;
> >>> +
> >>> +   while (i < entries) {
> >>> +   struct page *page = 
> >>> pfn_to_page(isolated_pages_obj[i].pfn);
> >>> +
> >>> +   mt = get_pageblock_migratetype(page);
> >>> +   __free_one_page(page, page_to_pfn(page), page_zone(page),
> >>> +   isolated_pages_obj[i].order, mt);
> >>> +   i++;
> >>> +   }
> >>> +   kfree(isolated_pages_obj);
> >>> +}
> >> You shouldn't be accessing __free_one_page without holding the zone
> >> lock for the page. You might consider confining yourself to one zone
> >> worth of hints at a time. Then you can acquire the lock once, and then
> >> return the memory you have freed.
> > That is correct.
> >>
> >> This is one of the reasons why I am thinking maybe a bit in the page
> >> and then spinning on that bit in arch_alloc_page might be a nice way
> >> to get around this. Then you only have to take the zone lock when you
> >> are finding the pages you want to hint on and setting the bit
> >> indicating they are mid hint. Otherwise you have to take the zone lock
> >> 

Re: [RFC][Patch v9 2/6] KVM: Enables the kernel to isolate guest free pages

2019-03-07 Thread David Hildenbrand
On 07.03.19 20:23, Nitesh Narayan Lal wrote:
> 
> On 3/7/19 1:30 PM, Alexander Duyck wrote:
>> On Wed, Mar 6, 2019 at 7:51 AM Nitesh Narayan Lal  wrote:
>>> This patch enables the kernel to scan the per cpu array
>>> which carries head pages from the buddy free list of order
>>> FREE_PAGE_HINTING_MIN_ORDER (MAX_ORDER - 1) by
>>> guest_free_page_hinting().
>>> guest_free_page_hinting() scans the entire per cpu array by
>>> acquiring a zone lock corresponding to the pages which are
>>> being scanned. If the page is still free and present in the
>>> buddy it tries to isolate the page and adds it to a
>>> dynamically allocated array.
>>>
>>> Once this scanning process is complete and if there are any
>>> isolated pages added to the dynamically allocated array
>>> guest_free_page_report() is invoked. However, before this the
>>> per-cpu array index is reset so that it can continue capturing
>>> the pages from buddy free list.
>>>
>>> In this patch guest_free_page_report() simply releases the pages back
>>> to the buddy by using __free_one_page()
>>>
>>> Signed-off-by: Nitesh Narayan Lal 
>> I'm pretty sure this code is not thread safe and has a few various issues.
>>
>>> ---
>>>  include/linux/page_hinting.h |   5 ++
>>>  mm/page_alloc.c  |   2 +-
>>>  virt/kvm/page_hinting.c  | 154 +++
>>>  3 files changed, 160 insertions(+), 1 deletion(-)
>>>
>>> diff --git a/include/linux/page_hinting.h b/include/linux/page_hinting.h
>>> index 90254c582789..d554a2581826 100644
>>> --- a/include/linux/page_hinting.h
>>> +++ b/include/linux/page_hinting.h
>>> @@ -13,3 +13,8 @@
>>>
>>>  void guest_free_page_enqueue(struct page *page, int order);
>>>  void guest_free_page_try_hinting(void);
>>> +extern int __isolate_free_page(struct page *page, unsigned int order);
>>> +extern void __free_one_page(struct page *page, unsigned long pfn,
>>> +   struct zone *zone, unsigned int order,
>>> +   int migratetype);
>>> +void release_buddy_pages(void *obj_to_free, int entries);
>>> diff --git a/mm/page_alloc.c b/mm/page_alloc.c
>>> index 684d047f33ee..d38b7eea207b 100644
>>> --- a/mm/page_alloc.c
>>> +++ b/mm/page_alloc.c
>>> @@ -814,7 +814,7 @@ static inline int page_is_buddy(struct page *page, 
>>> struct page *buddy,
>>>   * -- nyc
>>>   */
>>>
>>> -static inline void __free_one_page(struct page *page,
>>> +inline void __free_one_page(struct page *page,
>>> unsigned long pfn,
>>> struct zone *zone, unsigned int order,
>>> int migratetype)
>>> diff --git a/virt/kvm/page_hinting.c b/virt/kvm/page_hinting.c
>>> index 48b4b5e796b0..9885b372b5a9 100644
>>> --- a/virt/kvm/page_hinting.c
>>> +++ b/virt/kvm/page_hinting.c
>>> @@ -1,5 +1,9 @@
>>>  #include 
>>>  #include 
>>> +#include 
>>> +#include 
>>> +#include 
>>> +#include 
>>>
>>>  /*
>>>   * struct guest_free_pages- holds array of guest freed PFN's along with an
>>> @@ -16,6 +20,54 @@ struct guest_free_pages {
>>>
>>>  DEFINE_PER_CPU(struct guest_free_pages, free_pages_obj);
>>>
>>> +/*
>>> + * struct guest_isolated_pages- holds the buddy isolated pages which are
>>> + * supposed to be freed by the host.
>>> + * @pfn: page frame number for the isolated page.
>>> + * @order: order of the isolated page.
>>> + */
>>> +struct guest_isolated_pages {
>>> +   unsigned long pfn;
>>> +   unsigned int order;
>>> +};
>>> +
>>> +void release_buddy_pages(void *obj_to_free, int entries)
>>> +{
>>> +   int i = 0;
>>> +   int mt = 0;
>>> +   struct guest_isolated_pages *isolated_pages_obj = obj_to_free;
>>> +
>>> +   while (i < entries) {
>>> +   struct page *page = pfn_to_page(isolated_pages_obj[i].pfn);
>>> +
>>> +   mt = get_pageblock_migratetype(page);
>>> +   __free_one_page(page, page_to_pfn(page), page_zone(page),
>>> +   isolated_pages_obj[i].order, mt);
>>> +   i++;
>>> +   }
>>> +   kfree(isolated_pages_obj);
>>> +}
>> You shouldn't be accessing __free_one_page without holding the zone
>> lock for the page. You might consider confining yourself to one zone
>> worth of hints at a time. Then you can acquire the lock once, and then
>> return the memory you have freed.
> That is correct.
>>
>> This is one of the reasons why I am thinking maybe a bit in the page
>> and then spinning on that bit in arch_alloc_page might be a nice way
>> to get around this. Then you only have to take the zone lock when you
>> are finding the pages you want to hint on and setting the bit
>> indicating they are mid hint. Otherwise you have to take the zone lock
>> to pull pages out, and to put them back in and the likelihood of a
>> lock collision is much higher.
> Do you think adding a new flag to the page structure will be acceptable?

My lesson learned: forget it. If (at all) reuse some other one that
might be safe in that context. Hard to tell if that is even 

Re: [RFC][Patch v9 2/6] KVM: Enables the kernel to isolate guest free pages

2019-03-07 Thread Nitesh Narayan Lal

On 3/7/19 1:30 PM, Alexander Duyck wrote:
> On Wed, Mar 6, 2019 at 7:51 AM Nitesh Narayan Lal  wrote:
>> This patch enables the kernel to scan the per cpu array
>> which carries head pages from the buddy free list of order
>> FREE_PAGE_HINTING_MIN_ORDER (MAX_ORDER - 1) by
>> guest_free_page_hinting().
>> guest_free_page_hinting() scans the entire per cpu array by
>> acquiring a zone lock corresponding to the pages which are
>> being scanned. If the page is still free and present in the
>> buddy it tries to isolate the page and adds it to a
>> dynamically allocated array.
>>
>> Once this scanning process is complete and if there are any
>> isolated pages added to the dynamically allocated array
>> guest_free_page_report() is invoked. However, before this the
>> per-cpu array index is reset so that it can continue capturing
>> the pages from buddy free list.
>>
>> In this patch guest_free_page_report() simply releases the pages back
>> to the buddy by using __free_one_page()
>>
>> Signed-off-by: Nitesh Narayan Lal 
> I'm pretty sure this code is not thread safe and has a few various issues.
>
>> ---
>>  include/linux/page_hinting.h |   5 ++
>>  mm/page_alloc.c  |   2 +-
>>  virt/kvm/page_hinting.c  | 154 +++
>>  3 files changed, 160 insertions(+), 1 deletion(-)
>>
>> diff --git a/include/linux/page_hinting.h b/include/linux/page_hinting.h
>> index 90254c582789..d554a2581826 100644
>> --- a/include/linux/page_hinting.h
>> +++ b/include/linux/page_hinting.h
>> @@ -13,3 +13,8 @@
>>
>>  void guest_free_page_enqueue(struct page *page, int order);
>>  void guest_free_page_try_hinting(void);
>> +extern int __isolate_free_page(struct page *page, unsigned int order);
>> +extern void __free_one_page(struct page *page, unsigned long pfn,
>> +   struct zone *zone, unsigned int order,
>> +   int migratetype);
>> +void release_buddy_pages(void *obj_to_free, int entries);
>> diff --git a/mm/page_alloc.c b/mm/page_alloc.c
>> index 684d047f33ee..d38b7eea207b 100644
>> --- a/mm/page_alloc.c
>> +++ b/mm/page_alloc.c
>> @@ -814,7 +814,7 @@ static inline int page_is_buddy(struct page *page, 
>> struct page *buddy,
>>   * -- nyc
>>   */
>>
>> -static inline void __free_one_page(struct page *page,
>> +inline void __free_one_page(struct page *page,
>> unsigned long pfn,
>> struct zone *zone, unsigned int order,
>> int migratetype)
>> diff --git a/virt/kvm/page_hinting.c b/virt/kvm/page_hinting.c
>> index 48b4b5e796b0..9885b372b5a9 100644
>> --- a/virt/kvm/page_hinting.c
>> +++ b/virt/kvm/page_hinting.c
>> @@ -1,5 +1,9 @@
>>  #include 
>>  #include 
>> +#include 
>> +#include 
>> +#include 
>> +#include 
>>
>>  /*
>>   * struct guest_free_pages- holds array of guest freed PFN's along with an
>> @@ -16,6 +20,54 @@ struct guest_free_pages {
>>
>>  DEFINE_PER_CPU(struct guest_free_pages, free_pages_obj);
>>
>> +/*
>> + * struct guest_isolated_pages- holds the buddy isolated pages which are
>> + * supposed to be freed by the host.
>> + * @pfn: page frame number for the isolated page.
>> + * @order: order of the isolated page.
>> + */
>> +struct guest_isolated_pages {
>> +   unsigned long pfn;
>> +   unsigned int order;
>> +};
>> +
>> +void release_buddy_pages(void *obj_to_free, int entries)
>> +{
>> +   int i = 0;
>> +   int mt = 0;
>> +   struct guest_isolated_pages *isolated_pages_obj = obj_to_free;
>> +
>> +   while (i < entries) {
>> +   struct page *page = pfn_to_page(isolated_pages_obj[i].pfn);
>> +
>> +   mt = get_pageblock_migratetype(page);
>> +   __free_one_page(page, page_to_pfn(page), page_zone(page),
>> +   isolated_pages_obj[i].order, mt);
>> +   i++;
>> +   }
>> +   kfree(isolated_pages_obj);
>> +}
> You shouldn't be accessing __free_one_page without holding the zone
> lock for the page. You might consider confining yourself to one zone
> worth of hints at a time. Then you can acquire the lock once, and then
> return the memory you have freed.
That is correct.
>
> This is one of the reasons why I am thinking maybe a bit in the page
> and then spinning on that bit in arch_alloc_page might be a nice way
> to get around this. Then you only have to take the zone lock when you
> are finding the pages you want to hint on and setting the bit
> indicating they are mid hint. Otherwise you have to take the zone lock
> to pull pages out, and to put them back in and the likelihood of a
> lock collision is much higher.
Do you think adding a new flag to the page structure will be acceptable?
>
>> +
>> +void guest_free_page_report(struct guest_isolated_pages *isolated_pages_obj,
>> +   int entries)
>> +{
>> +   release_buddy_pages(isolated_pages_obj, entries);
>> +}
>> +
>> +static int sort_zonenum(const void *a1, const void *b1)
>> +{
>> +   const 

Re: [RFC][Patch v9 2/6] KVM: Enables the kernel to isolate guest free pages

2019-03-07 Thread Alexander Duyck
On Wed, Mar 6, 2019 at 7:51 AM Nitesh Narayan Lal  wrote:
>
> This patch enables the kernel to scan the per cpu array
> which carries head pages from the buddy free list of order
> FREE_PAGE_HINTING_MIN_ORDER (MAX_ORDER - 1) by
> guest_free_page_hinting().
> guest_free_page_hinting() scans the entire per cpu array by
> acquiring a zone lock corresponding to the pages which are
> being scanned. If the page is still free and present in the
> buddy it tries to isolate the page and adds it to a
> dynamically allocated array.
>
> Once this scanning process is complete and if there are any
> isolated pages added to the dynamically allocated array
> guest_free_page_report() is invoked. However, before this the
> per-cpu array index is reset so that it can continue capturing
> the pages from buddy free list.
>
> In this patch guest_free_page_report() simply releases the pages back
> to the buddy by using __free_one_page()
>
> Signed-off-by: Nitesh Narayan Lal 

I'm pretty sure this code is not thread safe and has a few various issues.

> ---
>  include/linux/page_hinting.h |   5 ++
>  mm/page_alloc.c  |   2 +-
>  virt/kvm/page_hinting.c  | 154 +++
>  3 files changed, 160 insertions(+), 1 deletion(-)
>
> diff --git a/include/linux/page_hinting.h b/include/linux/page_hinting.h
> index 90254c582789..d554a2581826 100644
> --- a/include/linux/page_hinting.h
> +++ b/include/linux/page_hinting.h
> @@ -13,3 +13,8 @@
>
>  void guest_free_page_enqueue(struct page *page, int order);
>  void guest_free_page_try_hinting(void);
> +extern int __isolate_free_page(struct page *page, unsigned int order);
> +extern void __free_one_page(struct page *page, unsigned long pfn,
> +   struct zone *zone, unsigned int order,
> +   int migratetype);
> +void release_buddy_pages(void *obj_to_free, int entries);
> diff --git a/mm/page_alloc.c b/mm/page_alloc.c
> index 684d047f33ee..d38b7eea207b 100644
> --- a/mm/page_alloc.c
> +++ b/mm/page_alloc.c
> @@ -814,7 +814,7 @@ static inline int page_is_buddy(struct page *page, struct 
> page *buddy,
>   * -- nyc
>   */
>
> -static inline void __free_one_page(struct page *page,
> +inline void __free_one_page(struct page *page,
> unsigned long pfn,
> struct zone *zone, unsigned int order,
> int migratetype)
> diff --git a/virt/kvm/page_hinting.c b/virt/kvm/page_hinting.c
> index 48b4b5e796b0..9885b372b5a9 100644
> --- a/virt/kvm/page_hinting.c
> +++ b/virt/kvm/page_hinting.c
> @@ -1,5 +1,9 @@
>  #include 
>  #include 
> +#include 
> +#include 
> +#include 
> +#include 
>
>  /*
>   * struct guest_free_pages- holds array of guest freed PFN's along with an
> @@ -16,6 +20,54 @@ struct guest_free_pages {
>
>  DEFINE_PER_CPU(struct guest_free_pages, free_pages_obj);
>
> +/*
> + * struct guest_isolated_pages- holds the buddy isolated pages which are
> + * supposed to be freed by the host.
> + * @pfn: page frame number for the isolated page.
> + * @order: order of the isolated page.
> + */
> +struct guest_isolated_pages {
> +   unsigned long pfn;
> +   unsigned int order;
> +};
> +
> +void release_buddy_pages(void *obj_to_free, int entries)
> +{
> +   int i = 0;
> +   int mt = 0;
> +   struct guest_isolated_pages *isolated_pages_obj = obj_to_free;
> +
> +   while (i < entries) {
> +   struct page *page = pfn_to_page(isolated_pages_obj[i].pfn);
> +
> +   mt = get_pageblock_migratetype(page);
> +   __free_one_page(page, page_to_pfn(page), page_zone(page),
> +   isolated_pages_obj[i].order, mt);
> +   i++;
> +   }
> +   kfree(isolated_pages_obj);
> +}

You shouldn't be accessing __free_one_page without holding the zone
lock for the page. You might consider confining yourself to one zone
worth of hints at a time. Then you can acquire the lock once, and then
return the memory you have freed.

This is one of the reasons why I am thinking maybe a bit in the page
and then spinning on that bit in arch_alloc_page might be a nice way
to get around this. Then you only have to take the zone lock when you
are finding the pages you want to hint on and setting the bit
indicating they are mid hint. Otherwise you have to take the zone lock
to pull pages out, and to put them back in and the likelihood of a
lock collision is much higher.

> +
> +void guest_free_page_report(struct guest_isolated_pages *isolated_pages_obj,
> +   int entries)
> +{
> +   release_buddy_pages(isolated_pages_obj, entries);
> +}
> +
> +static int sort_zonenum(const void *a1, const void *b1)
> +{
> +   const unsigned long *a = a1;
> +   const unsigned long *b = b1;
> +
> +   if (page_zonenum(pfn_to_page(a[0])) > page_zonenum(pfn_to_page(b[0])))
> +   return 1;
> +
> +   if (page_zonenum(pfn_to_page(a[0])) < page_zonenum(pfn_to_page(b[0])))
> +   

[RFC][Patch v9 2/6] KVM: Enables the kernel to isolate guest free pages

2019-03-06 Thread Nitesh Narayan Lal
This patch enables the kernel to scan the per cpu array
which carries head pages from the buddy free list of order
FREE_PAGE_HINTING_MIN_ORDER (MAX_ORDER - 1) by
guest_free_page_hinting().
guest_free_page_hinting() scans the entire per cpu array by
acquiring a zone lock corresponding to the pages which are
being scanned. If the page is still free and present in the
buddy it tries to isolate the page and adds it to a
dynamically allocated array.

Once this scanning process is complete and if there are any
isolated pages added to the dynamically allocated array
guest_free_page_report() is invoked. However, before this the
per-cpu array index is reset so that it can continue capturing
the pages from buddy free list.

In this patch guest_free_page_report() simply releases the pages back
to the buddy by using __free_one_page()

Signed-off-by: Nitesh Narayan Lal 
---
 include/linux/page_hinting.h |   5 ++
 mm/page_alloc.c  |   2 +-
 virt/kvm/page_hinting.c  | 154 +++
 3 files changed, 160 insertions(+), 1 deletion(-)

diff --git a/include/linux/page_hinting.h b/include/linux/page_hinting.h
index 90254c582789..d554a2581826 100644
--- a/include/linux/page_hinting.h
+++ b/include/linux/page_hinting.h
@@ -13,3 +13,8 @@
 
 void guest_free_page_enqueue(struct page *page, int order);
 void guest_free_page_try_hinting(void);
+extern int __isolate_free_page(struct page *page, unsigned int order);
+extern void __free_one_page(struct page *page, unsigned long pfn,
+   struct zone *zone, unsigned int order,
+   int migratetype);
+void release_buddy_pages(void *obj_to_free, int entries);
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 684d047f33ee..d38b7eea207b 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -814,7 +814,7 @@ static inline int page_is_buddy(struct page *page, struct 
page *buddy,
  * -- nyc
  */
 
-static inline void __free_one_page(struct page *page,
+inline void __free_one_page(struct page *page,
unsigned long pfn,
struct zone *zone, unsigned int order,
int migratetype)
diff --git a/virt/kvm/page_hinting.c b/virt/kvm/page_hinting.c
index 48b4b5e796b0..9885b372b5a9 100644
--- a/virt/kvm/page_hinting.c
+++ b/virt/kvm/page_hinting.c
@@ -1,5 +1,9 @@
 #include 
 #include 
+#include 
+#include 
+#include 
+#include 
 
 /*
  * struct guest_free_pages- holds array of guest freed PFN's along with an
@@ -16,6 +20,54 @@ struct guest_free_pages {
 
 DEFINE_PER_CPU(struct guest_free_pages, free_pages_obj);
 
+/*
+ * struct guest_isolated_pages- holds the buddy isolated pages which are
+ * supposed to be freed by the host.
+ * @pfn: page frame number for the isolated page.
+ * @order: order of the isolated page.
+ */
+struct guest_isolated_pages {
+   unsigned long pfn;
+   unsigned int order;
+};
+
+void release_buddy_pages(void *obj_to_free, int entries)
+{
+   int i = 0;
+   int mt = 0;
+   struct guest_isolated_pages *isolated_pages_obj = obj_to_free;
+
+   while (i < entries) {
+   struct page *page = pfn_to_page(isolated_pages_obj[i].pfn);
+
+   mt = get_pageblock_migratetype(page);
+   __free_one_page(page, page_to_pfn(page), page_zone(page),
+   isolated_pages_obj[i].order, mt);
+   i++;
+   }
+   kfree(isolated_pages_obj);
+}
+
+void guest_free_page_report(struct guest_isolated_pages *isolated_pages_obj,
+   int entries)
+{
+   release_buddy_pages(isolated_pages_obj, entries);
+}
+
+static int sort_zonenum(const void *a1, const void *b1)
+{
+   const unsigned long *a = a1;
+   const unsigned long *b = b1;
+
+   if (page_zonenum(pfn_to_page(a[0])) > page_zonenum(pfn_to_page(b[0])))
+   return 1;
+
+   if (page_zonenum(pfn_to_page(a[0])) < page_zonenum(pfn_to_page(b[0])))
+   return -1;
+
+   return 0;
+}
+
 struct page *get_buddy_page(struct page *page)
 {
unsigned long pfn = page_to_pfn(page);
@@ -33,9 +85,111 @@ struct page *get_buddy_page(struct page *page)
 static void guest_free_page_hinting(void)
 {
struct guest_free_pages *hinting_obj = _cpu_var(free_pages_obj);
+   struct guest_isolated_pages *isolated_pages_obj;
+   int idx = 0, ret = 0;
+   struct zone *zone_cur, *zone_prev;
+   unsigned long flags = 0;
+   int hyp_idx = 0;
+   int free_pages_idx = hinting_obj->free_pages_idx;
+
+   isolated_pages_obj = kmalloc(MAX_FGPT_ENTRIES *
+   sizeof(struct guest_isolated_pages), GFP_KERNEL);
+   if (!isolated_pages_obj) {
+   hinting_obj->free_pages_idx = 0;
+   put_cpu_var(hinting_obj);
+   return;
+   /* return some logical error here*/
+   }
+
+   sort(hinting_obj->free_page_arr, free_pages_idx,
+sizeof(unsigned long), sort_zonenum, NULL);
+
+