Received: by 10.192.165.148 with SMTP id m20csp5255829imm; Wed, 9 May 2018 01:55:26 -0700 (PDT) X-Google-Smtp-Source: AB8JxZqG3Xt9OU+bD9aSEKup3EoJOqrjFdkA3ip5SDs3pM7PIotK84ErBuj+50MMse19KH16QEO6 X-Received: by 2002:a63:8e42:: with SMTP id k63-v6mr35100271pge.278.1525856126831; Wed, 09 May 2018 01:55:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1525856126; cv=none; d=google.com; s=arc-20160816; b=PQznBwmJLoiS2IYRmhFiMVbynEvxxdLFg5FcfugD20gQnW0P7Wu+BbT9jQP+kl1kFF MKuFO5qC8h4d2s1CYg3VeZYmG4QTScPJCm1Uhk7OnQm3DCuBApjc7sIvQVdVhrjf/5iO +mnl5O/JqshjB2Z3gYii/o6PxOKZG4ZuMCs584JaNBwA4E8SGTTEbT8An9qOARPjng1z +Bz8jewQIp8hXP501zLHuaaUnbt+vVIdgMLL3tD7QbsvQiJMllMkc4BH6pXuLPD/RL9j eTCzqK+cBJ6Z3BeTA/Fm2V4dys5lo3On9lETtz2dsN6MGkvTab5kvzSp1XR+GigqQWla O84g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=D739h04cUyW+SKhypaeUeidC8yf2Es4CORG22CDFiWc=; b=C/FJTiZaJuF3zBoBOoxKlhRWZBn85+wM9LjsQ8aGCns1CEyWAzfRbL+6HBd19hNTZq goCcnJ7KvzmE4JitrmF+rWdPa7Bd1nmJFZY9QgMaf0XGoofd+keuOIE0vXaSG9bZt/k9 XNmb40B8i0SHxoh4ZeLHh2xMMErjo61vSdBjF9cMuTQluBpWLYQRNdWsG7PjlmxFN1yf hYv+vVhGsKFNF5Djr+jWmwrOVB5/67ZfKkr0iCj/lgOHDopMmRYnN94xk9eGCucgCuJN jXfZ8Cmqgoo9TyC/a6G/oS77FKiTZbwV6yvgKrm0sD94O9H3gC5HQiV/lDxQh8aytKMa 2VEg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id s14si25603698pfh.11.2018.05.09.01.55.12; Wed, 09 May 2018 01:55:26 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934413AbeEIIyU (ORCPT + 99 others); Wed, 9 May 2018 04:54:20 -0400 Received: from mga03.intel.com ([134.134.136.65]:31341 "EHLO mga03.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934082AbeEIIxf (ORCPT ); Wed, 9 May 2018 04:53:35 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by orsmga103.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 09 May 2018 01:53:35 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.49,381,1520924400"; d="scan'208";a="227055174" Received: from aaronlu.sh.intel.com ([10.239.159.135]) by fmsmga006.fm.intel.com with ESMTP; 09 May 2018 01:53:33 -0700 From: Aaron Lu To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Andrew Morton , Huang Ying , Dave Hansen , Kemi Wang , Tim Chen , Andi Kleen , Michal Hocko , Vlastimil Babka , Mel Gorman , Matthew Wilcox , Daniel Jordan , Tariq Toukan Subject: [RFC v3 PATCH 4/5] mm/free_pcppages_bulk: reduce overhead of cluster operation on free path Date: Wed, 9 May 2018 16:54:49 +0800 Message-Id: <20180509085450.3524-5-aaron.lu@intel.com> X-Mailer: git-send-email 2.14.3 In-Reply-To: <20180509085450.3524-1-aaron.lu@intel.com> References: <20180509085450.3524-1-aaron.lu@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org After "no_merge for order 0", the biggest overhead in free path for order 0 pages is now add_to_cluster(). As pages are freed one by one, it caused frequent operation of add_to_cluster(). Ideally, if only one migratetype pcp list has pages to free and count=pcp->batch in free_pcppages_bulk(), we can avoid calling add_to_cluster() one time per page but adding them in one go as a single cluster so this patch just did this. This optimization brings zone->lock contention down from 25% to almost zero again using the parallel free workload. Signed-off-by: Aaron Lu --- mm/page_alloc.c | 49 ++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 64afb26064ed..33814ffda507 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -1251,6 +1251,36 @@ static inline void prefetch_buddy(struct page *page) prefetch(buddy); } +static inline bool free_cluster_pages(struct zone *zone, struct list_head *list, + int mt, int count) +{ + struct cluster *c; + struct page *page, *n; + + if (!can_skip_merge(zone, 0)) + return false; + + if (count != this_cpu_ptr(zone->pageset)->pcp.batch) + return false; + + c = new_cluster(zone, count, list_first_entry(list, struct page, lru)); + if (unlikely(!c)) + return false; + + list_for_each_entry_safe(page, n, list, lru) { + set_page_order(page, 0); + set_page_merge_skipped(page); + page->cluster = c; + list_add(&page->lru, &zone->free_area[0].free_list[mt]); + } + + INIT_LIST_HEAD(list); + zone->free_area[0].nr_free += count; + __mod_zone_page_state(zone, NR_FREE_PAGES, count); + + return true; +} + /* * Frees a number of pages from the PCP lists * Assumes all pages on list are in same zone, and of same order. @@ -1265,10 +1295,10 @@ static inline void prefetch_buddy(struct page *page) static void free_pcppages_bulk(struct zone *zone, int count, struct per_cpu_pages *pcp) { - int migratetype = 0; - int batch_free = 0; + int migratetype = 0, i, count_mt[MIGRATE_PCPTYPES] = {0}; + int batch_free = 0, saved_count = count; int prefetch_nr = 0; - bool isolated_pageblocks; + bool isolated_pageblocks, single_mt = false; struct page *page, *tmp; LIST_HEAD(head); @@ -1292,6 +1322,7 @@ static void free_pcppages_bulk(struct zone *zone, int count, /* This is the only non-empty list. Free them all. */ if (batch_free == MIGRATE_PCPTYPES) batch_free = count; + count_mt[migratetype] += batch_free; do { page = list_last_entry(list, struct page, lru); @@ -1323,12 +1354,24 @@ static void free_pcppages_bulk(struct zone *zone, int count, } while (--count && --batch_free && !list_empty(list)); } + for (i = 0; i < MIGRATE_PCPTYPES; i++) { + if (count_mt[i] == saved_count) { + single_mt = true; + break; + } + } + spin_lock(&zone->lock); isolated_pageblocks = has_isolate_pageblock(zone); + if (!isolated_pageblocks && single_mt) + free_cluster_pages(zone, &head, migratetype, saved_count); + /* * Use safe version since after __free_one_page(), * page->lru.next will not point to original list. + * + * If free_cluster_pages() succeeds, head will be an empty list here. */ list_for_each_entry_safe(page, tmp, &head, lru) { int mt = get_pcppage_migratetype(page); -- 2.14.3