Received: by 2002:a05:6a10:f347:0:0:0:0 with SMTP id d7csp637726pxu; Thu, 26 Nov 2020 07:47:58 -0800 (PST) X-Google-Smtp-Source: ABdhPJzLV9VrGDpl+n/dobIsEj2kMg0Bd9yrfw3mFn8rMVQUVpTO/xD3YhUbRM6uU1hUn6IbNZYQ X-Received: by 2002:a17:906:f88e:: with SMTP id lg14mr3270492ejb.417.1606405677817; Thu, 26 Nov 2020 07:47:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1606405677; cv=none; d=google.com; s=arc-20160816; b=RZqZdalslkWNkBjELM9pOuh07HlFDOroXKRSCam/692SuMANwMgy/nklKHznb0JcRI NohmSpptqcIdBqZrSw5NEUGcxbG75I1EAWYc4c1iwuSlsmjyWUS21nP88f9ZQdKt7Oye BIovaYz29XffxfhfLw48HmPGAV6HYtZAKSiWmGv+3JE4QG0R0ZJcjCUoOyGN/2Qk3j7N xAYSAC0rVnN1AjwkA3y81QDHe+DnhOzxwcSk4l+87xF3KllUngX2t42RKfqjacLEKilz Vu/9J29Gxn8AMHkPZXovCvx91vxdvWdxpTsaj7ls2+qIbPBdVD8kIDqAV1hqmjLwlUuD lClQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:references:cc :to:from:subject; bh=A0fsLdetv+BZzVWaeg61MwhXR6Pxwjg0wll+sU6FO/s=; b=XFmOIpvM5lywWpvCZ/59pjulCVlm3erpal+FhLq3LsetIw85e22AEDNDXTnjr+etH2 tt6Os7Tn6MK2mvRVBroEaXMh49+XOoau3ieQHGIPW8XkC3V3zS7msSknkJbIM5evfE8O YHBSOWmwTamvp9mFpjp28xQOUvuOY22ROcAyH4J6OzRaCyR2OHc8xiS4L8+EnW8qYZM/ zNjOzqsg/4rQe8gX+Ba5pkrpo6McFsevLH3k9JhfwYoINo6T1FLNJCtUKMYQdqg+i86C 3wHNCemEf08iGon48MqHuB/Ma0BmJNsxEu8w4Nj/jr2Ma70zQ8YwG9yQnrPZMcyZ27tG t2IA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id t7si3287811edv.378.2020.11.26.07.47.34; Thu, 26 Nov 2020 07:47:57 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731284AbgKZPoH (ORCPT + 99 others); Thu, 26 Nov 2020 10:44:07 -0500 Received: from mx2.suse.de ([195.135.220.15]:41482 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727481AbgKZPoG (ORCPT ); Thu, 26 Nov 2020 10:44:06 -0500 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 2A8F6ACC4; Thu, 26 Nov 2020 15:44:05 +0000 (UTC) Subject: Re: [PATCH next] mm/swap.c: reduce lock contention in lru_cache_add From: Vlastimil Babka To: Yu Zhao , Alex Shi , Matthew Wilcox Cc: Konstantin Khlebnikov , Andrew Morton , Hugh Dickins , Michal Hocko , linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <1605860847-47445-1-git-send-email-alex.shi@linux.alibaba.com> <20201126045234.GA1014081@google.com> <20201126072402.GA1047005@google.com> <464fa387-9dfd-a8c7-3d86-040f26fd4115@suse.cz> Message-ID: Date: Thu, 26 Nov 2020 16:44:04 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.5.0 MIME-Version: 1.0 In-Reply-To: <464fa387-9dfd-a8c7-3d86-040f26fd4115@suse.cz> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 11/26/20 12:22 PM, Vlastimil Babka wrote: > On 11/26/20 8:24 AM, Yu Zhao wrote: >> On Thu, Nov 26, 2020 at 02:39:03PM +0800, Alex Shi wrote: >>> >>> >>> 在 2020/11/26 下午12:52, Yu Zhao 写道: >>> >> */ >>> >> void __pagevec_lru_add(struct pagevec *pvec) >>> >> { >>> >> - int i; >>> >> - struct lruvec *lruvec = NULL; >>> >> + int i, nr_lruvec; >>> >> unsigned long flags = 0; >>> >> + struct page *page; >>> >> + struct lruvecs lruvecs; >>> >> >>> >> - for (i = 0; i < pagevec_count(pvec); i++) { >>> >> - struct page *page = pvec->pages[i]; >>> >> + nr_lruvec = sort_page_lruvec(&lruvecs, pvec); >>> > Simply looping pvec multiple times (15 at most) for different lruvecs >>> > would be better because 1) it requires no extra data structures and >>> > therefore has better cache locality (theoretically faster) 2) it only >>> > loops once when !CONFIG_MEMCG and !CONFIG_NUMA and therefore has no >>> > impact on Android and Chrome OS. >>> > >>> >>> With multiple memcgs, it do help a lot, I had gotten 30% grain on readtwice >>> case. but yes, w/o MEMCG and NUMA, it's good to keep old behavior. So >>> would you like has a proposal for this? >> >> Oh, no, I'm not against your idea. I was saying it doesn't seem >> necessary to sort -- a nested loop would just do the job given >> pagevec is small. > > Yeah that could work. The worst case doesn't look nice (O(n^2)) but it should be > rather rare to have pages from really multiple memcgs/nodes? However, Matthew wanted to increase pagevec size [1] and once 15^2 becomes 63^2, it starts to be somewhat more worrying. [1] https://lore.kernel.org/linux-mm/20201105172651.2455-1-willy@infradead.org/ > Maybe with the following change? Avoids going through the nulls if we got lucky > (or have !MEMCG !NUMA). > >> diff --git a/mm/swap.c b/mm/swap.c >> index cb3794e13b48..1d238edc2907 100644 >> --- a/mm/swap.c >> +++ b/mm/swap.c >> @@ -996,15 +996,26 @@ static void __pagevec_lru_add_fn(struct page *page, struct lruvec *lruvec) >> */ >> void __pagevec_lru_add(struct pagevec *pvec) >> { >> - int i; >> + int i, j; > > int i, j, n; > >> struct lruvec *lruvec = NULL; >> unsigned long flags = 0; >> > > n = pagevec_count(pvec); >> for (i = 0; i < pagevec_count(pvec); i++) { > > for (i = 0; n; i++) { > >> struct page *page = pvec->pages[i]; >> >> + if (!page) >> + continue; >> + >> lruvec = relock_page_lruvec_irqsave(page, lruvec, &flags); >> - __pagevec_lru_add_fn(page, lruvec); > > n--; > >> + >> + for (j = i; j < pagevec_count(pvec); j++) { >> + if (page_to_nid(pvec->pages[j]) != page_to_nid(page) || >> + page_memcg(pvec->pages[j]) != page_memcg(page)) >> + continue; >> + >> + __pagevec_lru_add_fn(pvec->pages[j], lruvec); >> + pvec->pages[j] = NULL; > > n--; >> + } >> } >> if (lruvec) >> unlock_page_lruvec_irqrestore(lruvec, flags); >> >