mm: fix pcp count beyond pcp high in pcplist allocation

Message ID 20221024134146.3442393-1-chenwandun@huawei.com
State New
Headers
Series mm: fix pcp count beyond pcp high in pcplist allocation |

Commit Message

Chen Wandun Oct. 24, 2022, 1:41 p.m. UTC
  Nowadays there are several orders in pcplist, Function __rmqueue_pcplist
would alloc pcp batch pages to refill pcplist, when list of target order
if empty meanwhile other lists is not all empty, that result in pcp count
beyond pcp high after allocation. This behaviour can be easily observed by
adding debugging information in __rmqueue_pcplist.

Fix this by recalculate the batch pages to be allocated.

Fixes: 44042b449872 ("mm/page_alloc: allow high-order pages to be stored on the per-cpu lists")
Signed-off-by: Chen Wandun <chenwandun@huawei.com>
---
 mm/page_alloc.c | 2 ++
 1 file changed, 2 insertions(+)
  

Comments

Mel Gorman Oct. 24, 2022, 2:55 p.m. UTC | #1
On Mon, Oct 24, 2022 at 09:41:46PM +0800, Chen Wandun wrote:
> Nowadays there are several orders in pcplist, Function __rmqueue_pcplist
> would alloc pcp batch pages to refill pcplist, when list of target order
> if empty meanwhile other lists is not all empty, that result in pcp count
> beyond pcp high after allocation. This behaviour can be easily observed by
> adding debugging information in __rmqueue_pcplist.
> 
> Fix this by recalculate the batch pages to be allocated.

Are any problems observed other than the PCP lists temporarily exceed
pcp->high? As is, the patch could result in a batch request of 0 and
fall through to allocating from the zone list anyway defeating the
purpose of the PCP allocator and probably regressing performance in some
csaes.

The intention was to allow high to be briefly exceeded on the refill side,
particularly for THP pages and to always refill by at least two pages. In
the THP case, one would be allocated and maybe one in the near future
without acquiring the zone lock. If the limits are exceeded, it's only
exceeded until the next free.
  
Chen Wandun Oct. 25, 2022, 11:49 a.m. UTC | #2
On 2022/10/24 22:55, Mel Gorman wrote:
> On Mon, Oct 24, 2022 at 09:41:46PM +0800, Chen Wandun wrote:
>> Nowadays there are several orders in pcplist, Function __rmqueue_pcplist
>> would alloc pcp batch pages to refill pcplist, when list of target order
>> if empty meanwhile other lists is not all empty, that result in pcp count
>> beyond pcp high after allocation. This behaviour can be easily observed by
>> adding debugging information in __rmqueue_pcplist.
>>
>> Fix this by recalculate the batch pages to be allocated.
> Are any problems observed other than the PCP lists temporarily exceed
> pcp->high?
It will result frequently refill pcp page from buddy and release pcp 
page to buddy.
>   As is, the patch could result in a batch request of 0 and
  I foget this, the patch need some improve, thanks.
> fall through to allocating from the zone list anyway defeating the
> purpose of the PCP allocator and probably regressing performance in some
> csaes.
Same as I understand,how about set high/batch for each order in pcplist,
or just share pcp batch value only set high for each order?  It looks like
strange for pcp count beyond pcp high in common case.

If each order has it's own pcp high value, that behaviour is same as 
pcplist which
only contains order 0.

Thanks
Wandun.
>
> The intention was to allow high to be briefly exceeded on the refill side,
> particularly for THP pages and to always refill by at least two pages. In
> the THP case, one would be allocated and maybe one in the near future
> without acquiring the zone lock. If the limits are exceeded, it's only
> exceeded until the next free.
>
  
Mel Gorman Oct. 25, 2022, 1:19 p.m. UTC | #3
On Tue, Oct 25, 2022 at 07:49:59PM +0800, Chen Wandun wrote:
> 
> 
> On 2022/10/24 22:55, Mel Gorman wrote:
> > On Mon, Oct 24, 2022 at 09:41:46PM +0800, Chen Wandun wrote:
> > > Nowadays there are several orders in pcplist, Function __rmqueue_pcplist
> > > would alloc pcp batch pages to refill pcplist, when list of target order
> > > if empty meanwhile other lists is not all empty, that result in pcp count
> > > beyond pcp high after allocation. This behaviour can be easily observed by
> > > adding debugging information in __rmqueue_pcplist.
> > > 
> > > Fix this by recalculate the batch pages to be allocated.
> > Are any problems observed other than the PCP lists temporarily exceed
> > pcp->high?
>
> It will result frequently refill pcp page from buddy and release pcp page to
> buddy.

Under what circumstances does this causes a problem? I 100% accept that it
could happen but one downside of the patch is that it simply changes the
shape of the problem. If the batch refill is clamped then potentially the
PCP list is depleted quicker and needs to be refilled sooner and so zone
lock acquisitions are still required potentially higher frequency due to
clamped refill sizes. All that changes is the timing.

> >   As is, the patch could result in a batch request of 0 and
>
>  I foget this, the patch need some improve, thanks.
>
> > fall through to allocating from the zone list anyway defeating the
> > purpose of the PCP allocator and probably regressing performance in some
> > csaes.
>
> Same as I understand???how about set high/batch for each order in pcplist???

Using anything would than (X >> order) consumes storage. Even if storage
was to be used, selecting a value per-order would be impossible because
the correct value would depend on frequency of requests for each order.
That can only be determined at runtime and the cost of determining the
value would likely exceed the benefit.

At most, you could state that the batch refill should at least be 1 but
otherwise not exceed high. The downside is that zone->lock contention will
increase for a stream of THP pages which is a common allocation size.
The intent behind batch-2 was to reduce contention by 50% when multiple
processes are faulting large anonymous regions at the same time. THP
allocations are ones most likely to exceed pcp->high by a noticeable amount.

> or just share pcp batch value only set high for each order? It looks like
> strange for pcp count beyond pcp high in common case.
> 
> If each order has it's own pcp high value, that behaviour is same as pcplist
> which
> only contains order 0.
> 

Specify in the changelog how a workload is improved. That may be in terms
of memory usage, performance, zone lock contention or cases where pcp->high
being exceeded causes a functional problem on a particular class of
system.
  
Chen Wandun Oct. 31, 2022, 3:37 a.m. UTC | #4
在 2022/10/25 21:19, Mel Gorman 写道:
> On Tue, Oct 25, 2022 at 07:49:59PM +0800, Chen Wandun wrote:
>>
>> On 2022/10/24 22:55, Mel Gorman wrote:
>>> On Mon, Oct 24, 2022 at 09:41:46PM +0800, Chen Wandun wrote:
>>>> Nowadays there are several orders in pcplist, Function __rmqueue_pcplist
>>>> would alloc pcp batch pages to refill pcplist, when list of target order
>>>> if empty meanwhile other lists is not all empty, that result in pcp count
>>>> beyond pcp high after allocation. This behaviour can be easily observed by
>>>> adding debugging information in __rmqueue_pcplist.
>>>>
>>>> Fix this by recalculate the batch pages to be allocated.
>>> Are any problems observed other than the PCP lists temporarily exceed
>>> pcp->high?
>> It will result frequently refill pcp page from buddy and release pcp page to
>> buddy.
> Under what circumstances does this causes a problem? I 100% accept that it
Sorry for long time no reply.

It is hard to say this phenomenon would cause functional problem, I just 
found
this phenomenon and wonder if something can be improve.

> could happen but one downside of the patch is that it simply changes the
> shape of the problem. If the batch refill is clamped then potentially the
> PCP list is depleted quicker and needs to be refilled sooner and so zone
> lock acquisitions are still required potentially higher frequency due to
> clamped refill sizes. All that changes is the timing.
Agree,  the contention of zone-lock need more consideration.
>
>>>    As is, the patch could result in a batch request of 0 and
>>   I foget this, the patch need some improve, thanks.
>>
>>> fall through to allocating from the zone list anyway defeating the
>>> purpose of the PCP allocator and probably regressing performance in some
>>> csaes.
>> Same as I understand???how about set high/batch for each order in pcplist???
> Using anything would than (X >> order) consumes storage. Even if storage
> was to be used, selecting a value per-order would be impossible because
> the correct value would depend on frequency of requests for each order.
> That can only be determined at runtime and the cost of determining the
> value would likely exceed the benefit.
Can we set a experience value for pcp batch for each order during init 
stage?
If so we can make accurately control for pcp size. Nowdays, the size of each
order in pcp list is full of randomness. I dont konw which scheme is better
for performance.

>
> At most, you could state that the batch refill should at least be 1 but
> otherwise not exceed high. The downside is that zone->lock contention will
> increase for a stream of THP pages which is a common allocation size.
> The intent behind batch-2 was to reduce contention by 50% when multiple
> processes are faulting large anonymous regions at the same time. THP
> allocations are ones most likely to exceed pcp->high by a noticeable amount.
>
>> or just share pcp batch value only set high for each order? It looks like
>> strange for pcp count beyond pcp high in common case.
>>
>> If each order has it's own pcp high value, that behaviour is same as pcplist
>> which
>> only contains order 0.
>>
> Specify in the changelog how a workload is improved. That may be in terms
> of memory usage, performance, zone lock contention or cases where pcp->high
> being exceeded causes a functional problem on a particular class of
> system.
Got it, thanks.
>
  
Mel Gorman Nov. 1, 2022, 10:40 a.m. UTC | #5
On Mon, Oct 31, 2022 at 11:37:35AM +0800, Chen Wandun wrote:
> > > >    As is, the patch could result in a batch request of 0 and
> > >   I foget this, the patch need some improve, thanks.
> > > 
> > > > fall through to allocating from the zone list anyway defeating the
> > > > purpose of the PCP allocator and probably regressing performance in some
> > > > csaes.
> > > Same as I understand???how about set high/batch for each order in pcplist???
> > Using anything would than (X >> order) consumes storage. Even if storage
> > was to be used, selecting a value per-order would be impossible because
> > the correct value would depend on frequency of requests for each order.
> > That can only be determined at runtime and the cost of determining the
> > value would likely exceed the benefit.
>
> Can we set a experience value for pcp batch for each order during init
> stage?

I'm not sure what you mean by "experience value" but maybe you meant
experimental value?

> If so we can make accurately control for pcp size. Nowdays, the size of each
> order in pcp list is full of randomness. I dont konw which scheme is better
> for performance.
> 

It is something that could be experimented with but the main question is
-- what should those per-order values be? One option would be to enforce
pcp->high for all high-order values except THP if THP is enabled. That would
limit some of the issues with pcp->high being exceeded as even if two THPs
are refilled, one of them is allocated immediately. I wasn't convinced it was
necessary when implementing high-order PCP support but it could be evaluated.
  
Chen Wandun Nov. 3, 2022, 12:46 p.m. UTC | #6
在 2022/11/1 18:40, Mel Gorman 写道:
> On Mon, Oct 31, 2022 at 11:37:35AM +0800, Chen Wandun wrote:
>>>>>     As is, the patch could result in a batch request of 0 and
>>>>    I foget this, the patch need some improve, thanks.
>>>>
>>>>> fall through to allocating from the zone list anyway defeating the
>>>>> purpose of the PCP allocator and probably regressing performance in some
>>>>> csaes.
>>>> Same as I understand???how about set high/batch for each order in pcplist???
>>> Using anything would than (X >> order) consumes storage. Even if storage
>>> was to be used, selecting a value per-order would be impossible because
>>> the correct value would depend on frequency of requests for each order.
>>> That can only be determined at runtime and the cost of determining the
>>> value would likely exceed the benefit.
>> Can we set a experience value for pcp batch for each order during init
>> stage?
> I'm not sure what you mean by "experience value" but maybe you meant
> experimental value?
yes, experimental value,  sorry for that.
>
>> If so we can make accurately control for pcp size. Nowdays, the size of each
>> order in pcp list is full of randomness. I dont konw which scheme is better
>> for performance.
>>
> It is something that could be experimented with but the main question is
> -- what should those per-order values be? One option would be to enforce
> pcp->high for all high-order values except THP if THP is enabled. That would
> limit some of the issues with pcp->high being exceeded as even if two THPs
> are refilled, one of them is allocated immediately. I wasn't convinced it was
> necessary when implementing high-order PCP support but it could be evaluated.
Thank you for your suggestion, I will do some tests.
  

Patch

diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 39f846d098f5..93e18b6de2f3 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -3742,6 +3742,7 @@  struct page *__rmqueue_pcplist(struct zone *zone, unsigned int order,
 	do {
 		if (list_empty(list)) {
 			int batch = READ_ONCE(pcp->batch);
+			int high = READ_ONCE(pcp->high);
 			int alloced;
 
 			/*
@@ -3753,6 +3754,7 @@  struct page *__rmqueue_pcplist(struct zone *zone, unsigned int order,
 			 */
 			if (batch > 1)
 				batch = max(batch >> order, 2);
+			batch = min(batch, (high - pcp->count) >> order);
 			alloced = rmqueue_bulk(zone, order,
 					batch, list,
 					migratetype, alloc_flags);