From patchwork Thu Aug 1 22:27:07 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Duyck X-Patchwork-Id: 11071421 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id CC0B213AC for ; Thu, 1 Aug 2019 22:29:24 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id B4B3D286DF for ; Thu, 1 Aug 2019 22:29:24 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id A44E5286F3; Thu, 1 Aug 2019 22:29:24 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E2F91286DF for ; Thu, 1 Aug 2019 22:29:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 1231B6B0003; Thu, 1 Aug 2019 18:29:23 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 0D32F6B0006; Thu, 1 Aug 2019 18:29:23 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F2B666B0008; Thu, 1 Aug 2019 18:29:22 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-ot1-f70.google.com (mail-ot1-f70.google.com [209.85.210.70]) by kanga.kvack.org (Postfix) with ESMTP id C78B46B0003 for ; Thu, 1 Aug 2019 18:29:22 -0400 (EDT) Received: by mail-ot1-f70.google.com with SMTP id b4so40031451otf.15 for ; Thu, 01 Aug 2019 15:29:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:subject:from:to:cc:date :message-id:in-reply-to:references:user-agent:mime-version :content-transfer-encoding; bh=qiq6gR71/Np4kj7TR/VhXXY9IMafNu+VvX5LVu17sf0=; b=JSKZAW/1N6Q6du45ll5KMVLKeJEo0gEUTOFCp41WaB8PjqxvfiRbRYIPtpifh7KwGG fNAUkG/HES/v0cbPSRvK9ScxPWlRuO0l2xdvPCLE5BW3Iz2SfXv3nh5MC19VB62ljzJP bqGTy2NWa99PBRTZjIn6GGWuLnSjOkzg18Ys4t/pCwLwXaAytLcZNNGQB/s4O4Jg8u5B hbJT7K2LTYnYGP30M7FSOeCBl658Nb7Fu/VQ1gTFMx1Q74tZMQGg1BMFecXN4n/HN1h7 jiuxkfyLkYAWW47lSJbHzqC3jfpQmWKsEaX1TKa5hKwwRs1ChZ/EJAvzU8seGQWrro0D G5cQ== X-Gm-Message-State: APjAAAUKi6qT4oOqmguyKWrtpsV8nOThx7wYZ5a+oQ4Zheoz0l8PGZYH 2v8Ku8rIfngvLzgjVUxwZB9S+drH3IpRQ1cHPx1nMBp+5lW3nWEK+ozspqPW7iIBqGihqbQ9Tbx UPAjHQUqgJidtDw2x3+i/tBTXkzO2IFJhGfPH1H82P5lnvfLJpJMhXsHRhb0r0iYSAA== X-Received: by 2002:aca:3a55:: with SMTP id h82mr766267oia.49.1564698562486; Thu, 01 Aug 2019 15:29:22 -0700 (PDT) X-Received: by 2002:aca:3a55:: with SMTP id h82mr766245oia.49.1564698561466; Thu, 01 Aug 2019 15:29:21 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564698561; cv=none; d=google.com; s=arc-20160816; b=NxE4dys21LIHtxYRW3iAMvv0kVbjU9iT129l2tnJAkPtaW8CZ7k5J5K+bhsL5JhoYb D+lroOP2BRemTMVpQvasoS7IBJgbKLSwN2LtkBfB4pmQdICHEy1aiPbVln/sWYkLN7Z+ A9YiTq/xxFLXoYNXWlz8+Gi2Y1dNqCmg9KBsNu5brIONhImE0zsCzCzRvhOk9GFvhDSu 9yc+1Ap7GpTr21CtwCnBI4oVkDypT2T/yzjWAkBW0J6O/INkY49HMXn8f9xKAOZwH29w s8vqOkJJFhY0jKZEJ0s5OgKqxj2DmdM3kIUb8N/irUlTpPZOzjML6Uni7rFXQCFejbbG hCyA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:message-id:date:cc:to:from:subject:dkim-signature; bh=qiq6gR71/Np4kj7TR/VhXXY9IMafNu+VvX5LVu17sf0=; b=o4gs16ayKjaXpWhZcCHYQg3ElG4UneeictK3XDDa6/o0pHp7LEsI16XxgA/B+il08J /x+jOw0j9L+W1PrGrWj0IsuRBU1pRw/YBpmHnoyhkua5mpwu1hlHltjJq7wORRLNrdEF vgB4u6EyC8qlHNqIBsc//dDKuCQmya+0ofRQlCNKHCFctUul7050NlGC3ENKfQsBnXR2 PLurST5rzQeCrXwNTZIGkdxtQvEMVXuNM7ZNV6t5lP0HClXuKn3YFe/Vr1cR6m9LYKw5 evrC0T2CoOtLdssoJbxQslWbMcnUTCm8lH7rt2FppCXu4iac9VtAPqZI0j2gynKeRNOO 9Fbw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=C34Jr62z; spf=pass (google.com: domain of alexander.duyck@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=alexander.duyck@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id 92sor37705566ota.30.2019.08.01.15.29.21 for (Google Transport Security); Thu, 01 Aug 2019 15:29:21 -0700 (PDT) Received-SPF: pass (google.com: domain of alexander.duyck@gmail.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=C34Jr62z; spf=pass (google.com: domain of alexander.duyck@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=alexander.duyck@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:from:to:cc:date:message-id:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=qiq6gR71/Np4kj7TR/VhXXY9IMafNu+VvX5LVu17sf0=; b=C34Jr62zLOYAiVtJEvShJTY2kq2zXYedFyQaHgTZuzPDmNRWAeac3UsKAjSgLJmSz6 vv8kV/zhtNghKj8p5s0+emQhClcoNE/K8ERpifeGolniAy9doAr9lXzvftRAnd2x+Nyl 3OyFFVdNrA+fRNI7lgSou/I7tjeHtvlx56FL83jP5da2PG9ehzz0UfljkJUFQxWM845d QxLvhv6zTLqrwplMKmC64Fd3XrG4lXh+WJMMRectn2bFny8dk/ZCI06isL3qU1wbEahM cHYukakNEogSOx0PCMtUalgAVJFaKBSdd1JKpZ7VDo9CrleqHtk/ekW2FsNX5QM0zr9k dIJQ== X-Google-Smtp-Source: APXvYqy0KyNGtP3yK4Et1UQf7VLvShSVVcTiZ13hP+qWWhjr0UfqR1QA5O3PJR1WYOl1STAGfPm/tA== X-Received: by 2002:a9d:65cb:: with SMTP id z11mr60468452oth.325.1564698561089; Thu, 01 Aug 2019 15:29:21 -0700 (PDT) Received: from localhost.localdomain (50-39-177-61.bvtn.or.frontiernet.net. [50.39.177.61]) by smtp.gmail.com with ESMTPSA id 9sm22844406oij.25.2019.08.01.15.29.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 01 Aug 2019 15:29:20 -0700 (PDT) Subject: [PATCH v3 1/6] mm: Adjust shuffle code to allow for future coalescing From: Alexander Duyck To: nitesh@redhat.com, kvm@vger.kernel.org, david@redhat.com, mst@redhat.com, dave.hansen@intel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org Cc: yang.zhang.wz@gmail.com, pagupta@redhat.com, riel@surriel.com, konrad.wilk@oracle.com, willy@infradead.org, lcapitulino@redhat.com, wei.w.wang@intel.com, aarcange@redhat.com, pbonzini@redhat.com, dan.j.williams@intel.com, alexander.h.duyck@linux.intel.com Date: Thu, 01 Aug 2019 15:27:07 -0700 Message-ID: <20190801222707.22190.37136.stgit@localhost.localdomain> In-Reply-To: <20190801222158.22190.96964.stgit@localhost.localdomain> References: <20190801222158.22190.96964.stgit@localhost.localdomain> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP From: Alexander Duyck This patch is meant to move the head/tail adding logic out of the shuffle code and into the __free_one_page function since ultimately that is where it is really needed anyway. By doing this we should be able to reduce the overhead and can consolidate all of the list addition bits in one spot. Signed-off-by: Alexander Duyck --- include/linux/mmzone.h | 12 -------- mm/page_alloc.c | 70 +++++++++++++++++++++++++++--------------------- mm/shuffle.c | 24 ---------------- mm/shuffle.h | 32 ++++++++++++++++++++++ 4 files changed, 71 insertions(+), 67 deletions(-) diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index d77d717c620c..738e9c758135 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -116,18 +116,6 @@ static inline void add_to_free_area_tail(struct page *page, struct free_area *ar area->nr_free++; } -#ifdef CONFIG_SHUFFLE_PAGE_ALLOCATOR -/* Used to preserve page allocation order entropy */ -void add_to_free_area_random(struct page *page, struct free_area *area, - int migratetype); -#else -static inline void add_to_free_area_random(struct page *page, - struct free_area *area, int migratetype) -{ - add_to_free_area(page, area, migratetype); -} -#endif - /* Used for pages which are on another list */ static inline void move_to_free_area(struct page *page, struct free_area *area, int migratetype) diff --git a/mm/page_alloc.c b/mm/page_alloc.c index d3bb601c461b..dfed182f200d 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -877,6 +877,36 @@ static inline struct capture_control *task_capc(struct zone *zone) #endif /* CONFIG_COMPACTION */ /* + * If this is not the largest possible page, check if the buddy + * of the next-highest order is free. If it is, it's possible + * that pages are being freed that will coalesce soon. In case, + * that is happening, add the free page to the tail of the list + * so it's less likely to be used soon and more likely to be merged + * as a higher order page + */ +static inline bool +buddy_merge_likely(unsigned long pfn, unsigned long buddy_pfn, + struct page *page, unsigned int order) +{ + struct page *higher_page, *higher_buddy; + unsigned long combined_pfn; + + if (order >= MAX_ORDER - 2) + return false; + + if (!pfn_valid_within(buddy_pfn)) + return false; + + combined_pfn = buddy_pfn & pfn; + higher_page = page + (combined_pfn - pfn); + buddy_pfn = __find_buddy_pfn(combined_pfn, order + 1); + higher_buddy = higher_page + (buddy_pfn - combined_pfn); + + return pfn_valid_within(buddy_pfn) && + page_is_buddy(higher_page, higher_buddy, order + 1); +} + +/* * Freeing function for a buddy system allocator. * * The concept of a buddy system is to maintain direct-mapped table @@ -905,11 +935,12 @@ static inline void __free_one_page(struct page *page, struct zone *zone, unsigned int order, int migratetype) { - unsigned long combined_pfn; + struct capture_control *capc = task_capc(zone); unsigned long uninitialized_var(buddy_pfn); - struct page *buddy; + unsigned long combined_pfn; + struct free_area *area; unsigned int max_order; - struct capture_control *capc = task_capc(zone); + struct page *buddy; max_order = min_t(unsigned int, MAX_ORDER, pageblock_order + 1); @@ -978,35 +1009,12 @@ static inline void __free_one_page(struct page *page, done_merging: set_page_order(page, order); - /* - * If this is not the largest possible page, check if the buddy - * of the next-highest order is free. If it is, it's possible - * that pages are being freed that will coalesce soon. In case, - * that is happening, add the free page to the tail of the list - * so it's less likely to be used soon and more likely to be merged - * as a higher order page - */ - if ((order < MAX_ORDER-2) && pfn_valid_within(buddy_pfn) - && !is_shuffle_order(order)) { - struct page *higher_page, *higher_buddy; - combined_pfn = buddy_pfn & pfn; - higher_page = page + (combined_pfn - pfn); - buddy_pfn = __find_buddy_pfn(combined_pfn, order + 1); - higher_buddy = higher_page + (buddy_pfn - combined_pfn); - if (pfn_valid_within(buddy_pfn) && - page_is_buddy(higher_page, higher_buddy, order + 1)) { - add_to_free_area_tail(page, &zone->free_area[order], - migratetype); - return; - } - } - - if (is_shuffle_order(order)) - add_to_free_area_random(page, &zone->free_area[order], - migratetype); + area = &zone->free_area[order]; + if (is_shuffle_order(order) ? shuffle_add_to_tail() : + buddy_merge_likely(pfn, buddy_pfn, page, order)) + add_to_free_area_tail(page, area, migratetype); else - add_to_free_area(page, &zone->free_area[order], migratetype); - + add_to_free_area(page, area, migratetype); } /* diff --git a/mm/shuffle.c b/mm/shuffle.c index 3ce12481b1dc..55d592e62526 100644 --- a/mm/shuffle.c +++ b/mm/shuffle.c @@ -4,7 +4,6 @@ #include #include #include -#include #include #include "internal.h" #include "shuffle.h" @@ -182,26 +181,3 @@ void __meminit __shuffle_free_memory(pg_data_t *pgdat) for (z = pgdat->node_zones; z < pgdat->node_zones + MAX_NR_ZONES; z++) shuffle_zone(z); } - -void add_to_free_area_random(struct page *page, struct free_area *area, - int migratetype) -{ - static u64 rand; - static u8 rand_bits; - - /* - * The lack of locking is deliberate. If 2 threads race to - * update the rand state it just adds to the entropy. - */ - if (rand_bits == 0) { - rand_bits = 64; - rand = get_random_u64(); - } - - if (rand & 1) - add_to_free_area(page, area, migratetype); - else - add_to_free_area_tail(page, area, migratetype); - rand_bits--; - rand >>= 1; -} diff --git a/mm/shuffle.h b/mm/shuffle.h index 777a257a0d2f..add763cc0995 100644 --- a/mm/shuffle.h +++ b/mm/shuffle.h @@ -3,6 +3,7 @@ #ifndef _MM_SHUFFLE_H #define _MM_SHUFFLE_H #include +#include /* * SHUFFLE_ENABLE is called from the command line enabling path, or by @@ -43,6 +44,32 @@ static inline bool is_shuffle_order(int order) return false; return order >= SHUFFLE_ORDER; } + +static inline bool shuffle_add_to_tail(void) +{ + static u64 rand; + static u8 rand_bits; + u64 rand_old; + + /* + * The lack of locking is deliberate. If 2 threads race to + * update the rand state it just adds to the entropy. + */ + if (rand_bits-- == 0) { + rand_bits = 64; + rand = get_random_u64(); + } + + /* + * Test highest order bit while shifting our random value. This + * should result in us testing for the carry flag following the + * shift. + */ + rand_old = rand; + rand <<= 1; + + return rand < rand_old; +} #else static inline void shuffle_free_memory(pg_data_t *pgdat) { @@ -60,5 +87,10 @@ static inline bool is_shuffle_order(int order) { return false; } + +static inline bool shuffle_add_to_tail(void) +{ + return false; +} #endif #endif /* _MM_SHUFFLE_H */ From patchwork Thu Aug 1 22:29:26 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Duyck X-Patchwork-Id: 11071425 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0DD591399 for ; Thu, 1 Aug 2019 22:31:43 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id E5B6F28755 for ; Thu, 1 Aug 2019 22:31:42 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D3A0528753; Thu, 1 Aug 2019 22:31:42 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 57E6228753 for ; Thu, 1 Aug 2019 22:31:42 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 714576B0003; Thu, 1 Aug 2019 18:31:41 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 69EE66B0005; Thu, 1 Aug 2019 18:31:41 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 517CF6B0006; Thu, 1 Aug 2019 18:31:41 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.198]) by kanga.kvack.org (Postfix) with ESMTP id 16DF66B0003 for ; Thu, 1 Aug 2019 18:31:41 -0400 (EDT) Received: by mail-pg1-f198.google.com with SMTP id a21so39187675pgv.0 for ; Thu, 01 Aug 2019 15:31:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:subject:from:to:cc:date :message-id:in-reply-to:references:user-agent:mime-version :content-transfer-encoding; bh=49QRQWf2FNsjOXVM5hrSB1/wT6sFBWj2i81LIQFGLg0=; b=kRdLVjTs5bCs1p4Ax6rgPjrMG9I0nmFuVn3jr5YJ+iNM82weh1dqz6vPuw2XFEbxGM WYoj9sZylZMgltNBdzFoSwlDYX22NUsnmXjhvh44JAdCH/SKWoiURy2NSAyThW9pNREr 8+5CdJFHD5H6Yj+gCp2Ehpku3Vd3q1ZlHycESW1kCshlXI2ZhClB3nZ6grCFPz+x4TEL rRVkgnM3mwuXsIv5/O42mrIT4ykCOwUbiPtAjaV6BCbvnOVWM3bOd5jzynCoN5kd6h0v yMNI041V/MFn0d+/0VDN0wmWv+ry+D4Jdh2ac569578noYk5yqv+uGactc5l8vtjRbmB XW3Q== X-Gm-Message-State: APjAAAVNuQhXm36g2l7AK50BTgPLRGcE4M/zYpqRdIpslQ+gD1GdIf42 DJokxIn9NhZBmZyQp2VePF8H6dJrmqVUz6vrhSFlqGhFbPYDGUZhEVIuseJbStUhAq0d/VVYCPw 7atHGy/LiUp/B1Iva+cDqw+3sy/io4lJ64fA/BaluYfjVCk4Zs6ZiaiDIbK00LrOk8g== X-Received: by 2002:a65:4509:: with SMTP id n9mr67822991pgq.133.1564698700675; Thu, 01 Aug 2019 15:31:40 -0700 (PDT) X-Received: by 2002:a65:4509:: with SMTP id n9mr67822928pgq.133.1564698699759; Thu, 01 Aug 2019 15:31:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564698699; cv=none; d=google.com; s=arc-20160816; b=deV9HHCIyvHEI2KjRaVxzDUtDQ/OrvO6j/Rs5UV1EctH9WIkTYtrWnts1YTGpRS2dJ II4UEX72hxaY5gF3vWxi0BLQ6QsmhzOQEER8GDMM3t33jFfzk5ClRyQNigBePLhtJPGM 8pNwhHgiHdGCj/Qfiot/Tk8PGQ8XzLe/stJo3MCi7inEf+A1FKe6CL88Eebp8Bq6o0aW 350EnEuwH/n1eMLnl7EP3U4w9UxzCig/6zete2uA+0/UxJ1JHfs/hAbjK8HQFjjyIx/r d2aJVraiaPakyUDGYmn/QVK0fVj9mP92HnEX/t8oQ5TnCDxQQaR/ZWUgLHxEaeW7pU/J UbdA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:message-id:date:cc:to:from:subject:dkim-signature; bh=49QRQWf2FNsjOXVM5hrSB1/wT6sFBWj2i81LIQFGLg0=; b=TomGuwAxzgGzT0A1wTioVE7MnKuAsVdDZ6Ey5O+Ua2aSN9TxlUlDmsUzfpCTkDkFgV 0gcDoJ2s5iVeufmUe/jcfR6dRiesAQ/a0tLj6Jmh65ktY2fstbtorOh6W5suh5h110Xi Ee4Ss6claD7/inxYs3mPdMNXMfSgkZxb7JuCJPmixUHWUohngLJ2jylqLW/C/D46KdBp l6dhTzdTlZkQnoIdG/UbIFyJ6I+yjRIViJIdivoP14uTan3owY1aC925RaGArzgmR0x5 IoQ3gQVd+Qd68M5XKuuWLk0q5gpzij9d9KDXnv+VKPSi+iyp+IddStgXR1cUg3cAsrWc isDA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ji33itet; spf=pass (google.com: domain of alexander.duyck@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=alexander.duyck@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id o11sor88513549plk.18.2019.08.01.15.31.39 for (Google Transport Security); Thu, 01 Aug 2019 15:31:39 -0700 (PDT) Received-SPF: pass (google.com: domain of alexander.duyck@gmail.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ji33itet; spf=pass (google.com: domain of alexander.duyck@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=alexander.duyck@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:from:to:cc:date:message-id:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=49QRQWf2FNsjOXVM5hrSB1/wT6sFBWj2i81LIQFGLg0=; b=ji33itetIIDF6723/1Y8YKJGaXsZhwxdfFFZT45WUtDznubVlVgr6z8O6LM2Y0Bm8P P43KwP1bs6fuGWQAelVx2jsf+HXc9hjzhGqb6BID3daGICPkxx/rJvFHx5nyJeTqaquo J5birZo4hMpfVK6j0Z9nSIcOOGAxTZryiD/Bg7Ea0Sh4JYUZV5Q8A5i5BlFn+XlDUn2Y xbtmEccIS9ctv1SzHpgWS+Gmvdsmo1e/yOMrR/P+7j5Dc6t3T1NV9Bf8oF0an0BTRzHE zwVYl7CMWW7OMqTK+UidQdCn5nY8fJS9gYQXsiGqTJOK0Pc7c31igD37NF4ruL1MGqIV abHw== X-Google-Smtp-Source: APXvYqx2jv1TK82igL61Day1gZd1T9JWolFuPhQSs0j3XhgePDf2nr7I5FyOs/W+WeBpIigsbk6TNg== X-Received: by 2002:a17:902:2be6:: with SMTP id l93mr129298797plb.0.1564698699193; Thu, 01 Aug 2019 15:31:39 -0700 (PDT) Received: from localhost.localdomain (50-39-177-61.bvtn.or.frontiernet.net. [50.39.177.61]) by smtp.gmail.com with ESMTPSA id m20sm80514167pff.79.2019.08.01.15.31.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 01 Aug 2019 15:31:38 -0700 (PDT) Subject: [PATCH v3 2/6] mm: Move set/get_pcppage_migratetype to mmzone.h From: Alexander Duyck To: nitesh@redhat.com, kvm@vger.kernel.org, david@redhat.com, mst@redhat.com, dave.hansen@intel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org Cc: yang.zhang.wz@gmail.com, pagupta@redhat.com, riel@surriel.com, konrad.wilk@oracle.com, willy@infradead.org, lcapitulino@redhat.com, wei.w.wang@intel.com, aarcange@redhat.com, pbonzini@redhat.com, dan.j.williams@intel.com, alexander.h.duyck@linux.intel.com Date: Thu, 01 Aug 2019 15:29:26 -0700 Message-ID: <20190801222926.22190.81982.stgit@localhost.localdomain> In-Reply-To: <20190801222158.22190.96964.stgit@localhost.localdomain> References: <20190801222158.22190.96964.stgit@localhost.localdomain> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP From: Alexander Duyck In order to support page aeration it will be necessary to store and retrieve the migratetype of a page. To enable that I am moving the set and get operations for pcppage_migratetype into the mm/internal.h header so that they can be used outside of the page_alloc.c file. Signed-off-by: Alexander Duyck --- mm/internal.h | 18 ++++++++++++++++++ mm/page_alloc.c | 18 ------------------ 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/mm/internal.h b/mm/internal.h index 0d5f720c75ab..e4a1a57bbd40 100644 --- a/mm/internal.h +++ b/mm/internal.h @@ -549,6 +549,24 @@ static inline bool is_migrate_highatomic_page(struct page *page) return get_pageblock_migratetype(page) == MIGRATE_HIGHATOMIC; } +/* + * A cached value of the page's pageblock's migratetype, used when the page is + * put on a pcplist. Used to avoid the pageblock migratetype lookup when + * freeing from pcplists in most cases, at the cost of possibly becoming stale. + * Also the migratetype set in the page does not necessarily match the pcplist + * index, e.g. page might have MIGRATE_CMA set but be on a pcplist with any + * other index - this ensures that it will be put on the correct CMA freelist. + */ +static inline int get_pcppage_migratetype(struct page *page) +{ + return page->index; +} + +static inline void set_pcppage_migratetype(struct page *page, int migratetype) +{ + page->index = migratetype; +} + void setup_zone_pageset(struct zone *zone); extern struct page *alloc_new_node_page(struct page *page, unsigned long node); #endif /* __MM_INTERNAL_H */ diff --git a/mm/page_alloc.c b/mm/page_alloc.c index dfed182f200d..7cedc73953fd 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -185,24 +185,6 @@ static int __init early_init_on_free(char *buf) } early_param("init_on_free", early_init_on_free); -/* - * A cached value of the page's pageblock's migratetype, used when the page is - * put on a pcplist. Used to avoid the pageblock migratetype lookup when - * freeing from pcplists in most cases, at the cost of possibly becoming stale. - * Also the migratetype set in the page does not necessarily match the pcplist - * index, e.g. page might have MIGRATE_CMA set but be on a pcplist with any - * other index - this ensures that it will be put on the correct CMA freelist. - */ -static inline int get_pcppage_migratetype(struct page *page) -{ - return page->index; -} - -static inline void set_pcppage_migratetype(struct page *page, int migratetype) -{ - page->index = migratetype; -} - #ifdef CONFIG_PM_SLEEP /* * The following functions are used by the suspend/hibernate code to temporarily From patchwork Thu Aug 1 22:31:44 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Duyck X-Patchwork-Id: 11071429 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 04BB91398 for ; Thu, 1 Aug 2019 22:33:59 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DC4E028756 for ; Thu, 1 Aug 2019 22:33:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CF1BA2875F; Thu, 1 Aug 2019 22:33:58 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9295728756 for ; Thu, 1 Aug 2019 22:33:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A95A56B0005; Thu, 1 Aug 2019 18:33:56 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id A46616B0006; Thu, 1 Aug 2019 18:33:56 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8E5846B0008; Thu, 1 Aug 2019 18:33:56 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pf1-f197.google.com (mail-pf1-f197.google.com [209.85.210.197]) by kanga.kvack.org (Postfix) with ESMTP id 5295A6B0005 for ; Thu, 1 Aug 2019 18:33:56 -0400 (EDT) Received: by mail-pf1-f197.google.com with SMTP id a20so46717890pfn.19 for ; Thu, 01 Aug 2019 15:33:56 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:subject:from:to:cc:date :message-id:in-reply-to:references:user-agent:mime-version :content-transfer-encoding; bh=/ALyy5tBTJRj9deFR87ZUlllw72TrmFyWSdgPjQp4rM=; b=gP77cFwZwgEJiLiLsTACInlhki9HOfLLlCSnpwJ9AsqwUTwetbOhv/EPa1HiSpkc/W mi/3+wrimjKAgVNVDXp5TaINKreo0I6l7oJwZvqEwL+CE/YQ0bjeHJ6PKa5YEzSz7Ahv 66BemmAUBDsjKPLHsFjnS0YLK7MTe6/4pAv54+8NBfvrkruKuF8IveZbYIFL0PL1Md9G ceX2ulJigby343ZeKLckdpkNexU3aeWZi8Plb7mDncd2tUZD0hLA3crFweZ+PgrwRoKq RFTAxWnu57PBzVArzKZ5hkqjyu/dhrxZEJSpj3/aDZ60xqtDD6pH1Uf1Vq9lmtA9HGJK Zz6Q== X-Gm-Message-State: APjAAAVl14CH+9PnGLd9clDWtJdqz4eKf9k2Ie2JiIGRndsEJhOb18aZ RbFAblycXmU4MQOupGjT+g1VgZp0zqzjrpeVkLDLRY8kngrhxevj1Nqat8qo7jwnz+A8MPY6cNr b4r1QnjDWa5C+Dw8GtEzyy0TvlBc5rTlob5wAo5/0EpoFuhRsahqNp8Bs9MK3qTCuUg== X-Received: by 2002:a17:90a:8985:: with SMTP id v5mr1063425pjn.136.1564698835899; Thu, 01 Aug 2019 15:33:55 -0700 (PDT) X-Received: by 2002:a17:90a:8985:: with SMTP id v5mr1063377pjn.136.1564698834846; Thu, 01 Aug 2019 15:33:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564698834; cv=none; d=google.com; s=arc-20160816; b=gZZ7V1r2gDNAX4OlTDd4Q6oWB504tgQBlHPqEfOxa/9U0Im9qPqHdtrV1cjY9kodfE zxnJRryhYSwucODfkAUj+sKKpIsPL40o4nh0+FHlqXWq1oXHsRVJarQSsLS/bNeqsfAm JAyjwFasT+eFZDNzS7yN0/jF1LGfPLhrm+2BlgyA3Fk82t/02zcBBcmN2iHxMLBGKCjx T6oKsJjcFWRG+90leuXxRjpdTk0/+JFOYv3+df19v1AO7u9yr0rtb/4YmmG4O4/tMSgm JHqnbqsFANZVt89cwgkAC7jsGjDonNv2N+a/48GoRwQLdgIZ3MUuLuRm5e7vOO3hqKIA O7eQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:message-id:date:cc:to:from:subject:dkim-signature; bh=/ALyy5tBTJRj9deFR87ZUlllw72TrmFyWSdgPjQp4rM=; b=Op+I7yvrpWRQNI6UpSdjsoSwxWZ3eSEpGp2X9Jay83EkEsD6gvWu9cAirXY9NTTfWU IvRQUR/WrtlsDGj8cXNN0cW7YHiFWZVWSZ2Ri6wjv1yJNN+LR8oBDIQIiS91OYYChVk7 NHftV9ZRxVunbRWydstsLXIW5JDcptO1Z0sj++kM2UCAp0b3h0TXgUKh09tE8rRZrIfb 7RpxJpuMkk4K3/DEepZgJUYhB0gJ5j5ScSgudDa4MqQ2Hu7+F3XqwUR59CrW7lC8E/TG 6Bk5AfEh+XprhlnYKPgqB38yXFv8gDCqxDFhcWBCFrpp6OqTpv/TdNsgvp3FY2RlbrJO CATw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ZBtpkVwz; spf=pass (google.com: domain of alexander.duyck@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=alexander.duyck@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id j2sor86864556pll.35.2019.08.01.15.33.54 for (Google Transport Security); Thu, 01 Aug 2019 15:33:54 -0700 (PDT) Received-SPF: pass (google.com: domain of alexander.duyck@gmail.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=ZBtpkVwz; spf=pass (google.com: domain of alexander.duyck@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=alexander.duyck@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:from:to:cc:date:message-id:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=/ALyy5tBTJRj9deFR87ZUlllw72TrmFyWSdgPjQp4rM=; b=ZBtpkVwzOe4eyGoq9QPqUpxtiZRKtqzBH/tyBWE8UnrimostlvcQjvVc/SXZMgbk9N DYil48OxFec1bR3QcSym0HQF290WYdsxjQrKwJXFI7LkKkgljotO2z7gnhVmFcGw0q4W RkcNEAOwyFAHZFD2x0fSSd5S048yDyryAyMpI/Tkf82RXjFdu4NHYfhK7lJPOOO83o7j YgGxggzrRW1L//7xkten1pa/cAZ29ibGzFALKw6Oy5gBAU72fwwEJdrje9WIssnpI1qz e4z+iprUrsN+jdd8nQcP0RVuSU6/DGQvYlI3GBZ7Ry9TuIc6mQiJjm2BhRjHgZrkPKPj GB7w== X-Google-Smtp-Source: APXvYqxlI/m+J1Vb1i+6OdKITlgM+lspjQ95MM8yt+CwgY55W8rjUhUi5OBsWSgJiUOC8m2kY4ICJA== X-Received: by 2002:a17:902:2ae7:: with SMTP id j94mr128150310plb.270.1564698834276; Thu, 01 Aug 2019 15:33:54 -0700 (PDT) Received: from localhost.localdomain (50-39-177-61.bvtn.or.frontiernet.net. [50.39.177.61]) by smtp.gmail.com with ESMTPSA id x25sm104397434pfa.90.2019.08.01.15.33.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 01 Aug 2019 15:33:53 -0700 (PDT) Subject: [PATCH v3 3/6] mm: Use zone and order instead of free area in free_list manipulators From: Alexander Duyck To: nitesh@redhat.com, kvm@vger.kernel.org, david@redhat.com, mst@redhat.com, dave.hansen@intel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org Cc: yang.zhang.wz@gmail.com, pagupta@redhat.com, riel@surriel.com, konrad.wilk@oracle.com, willy@infradead.org, lcapitulino@redhat.com, wei.w.wang@intel.com, aarcange@redhat.com, pbonzini@redhat.com, dan.j.williams@intel.com, alexander.h.duyck@linux.intel.com Date: Thu, 01 Aug 2019 15:31:44 -0700 Message-ID: <20190801223144.22190.30566.stgit@localhost.localdomain> In-Reply-To: <20190801222158.22190.96964.stgit@localhost.localdomain> References: <20190801222158.22190.96964.stgit@localhost.localdomain> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP From: Alexander Duyck In order to enable the use of the zone from the list manipulator functions I will need access to the zone pointer. As it turns out most of the accessors were always just being directly passed &zone->free_area[order] anyway so it would make sense to just fold that into the function itself and pass the zone and order as arguments instead of the free area. In order to be able to reference the zone we need to move the declaration of the functions down so that we have the zone defined before we define the list manipulation functions. Signed-off-by: Alexander Duyck --- include/linux/mmzone.h | 70 ++++++++++++++++++++++++++---------------------- mm/page_alloc.c | 30 ++++++++------------- 2 files changed, 49 insertions(+), 51 deletions(-) diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 738e9c758135..f0c68b6b6154 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -100,29 +100,6 @@ struct free_area { unsigned long nr_free; }; -/* Used for pages not on another list */ -static inline void add_to_free_area(struct page *page, struct free_area *area, - int migratetype) -{ - list_add(&page->lru, &area->free_list[migratetype]); - area->nr_free++; -} - -/* Used for pages not on another list */ -static inline void add_to_free_area_tail(struct page *page, struct free_area *area, - int migratetype) -{ - list_add_tail(&page->lru, &area->free_list[migratetype]); - area->nr_free++; -} - -/* Used for pages which are on another list */ -static inline void move_to_free_area(struct page *page, struct free_area *area, - int migratetype) -{ - list_move(&page->lru, &area->free_list[migratetype]); -} - static inline struct page *get_page_from_free_area(struct free_area *area, int migratetype) { @@ -130,15 +107,6 @@ static inline struct page *get_page_from_free_area(struct free_area *area, struct page, lru); } -static inline void del_page_from_free_area(struct page *page, - struct free_area *area) -{ - list_del(&page->lru); - __ClearPageBuddy(page); - set_page_private(page, 0); - area->nr_free--; -} - static inline bool free_area_empty(struct free_area *area, int migratetype) { return list_empty(&area->free_list[migratetype]); @@ -787,6 +755,44 @@ static inline bool pgdat_is_empty(pg_data_t *pgdat) return !pgdat->node_start_pfn && !pgdat->node_spanned_pages; } +/* Used for pages not on another list */ +static inline void add_to_free_list(struct page *page, struct zone *zone, + unsigned int order, int migratetype) +{ + struct free_area *area = &zone->free_area[order]; + + list_add(&page->lru, &area->free_list[migratetype]); + area->nr_free++; +} + +/* Used for pages not on another list */ +static inline void add_to_free_list_tail(struct page *page, struct zone *zone, + unsigned int order, int migratetype) +{ + struct free_area *area = &zone->free_area[order]; + + list_add_tail(&page->lru, &area->free_list[migratetype]); + area->nr_free++; +} + +/* Used for pages which are on another list */ +static inline void move_to_free_list(struct page *page, struct zone *zone, + unsigned int order, int migratetype) +{ + struct free_area *area = &zone->free_area[order]; + + list_move(&page->lru, &area->free_list[migratetype]); +} + +static inline void del_page_from_free_list(struct page *page, struct zone *zone, + unsigned int order) +{ + list_del(&page->lru); + __ClearPageBuddy(page); + set_page_private(page, 0); + zone->free_area[order].nr_free--; +} + #include void build_all_zonelists(pg_data_t *pgdat); diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 7cedc73953fd..71aadc7d5ff6 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -920,7 +920,6 @@ static inline void __free_one_page(struct page *page, struct capture_control *capc = task_capc(zone); unsigned long uninitialized_var(buddy_pfn); unsigned long combined_pfn; - struct free_area *area; unsigned int max_order; struct page *buddy; @@ -957,7 +956,7 @@ static inline void __free_one_page(struct page *page, if (page_is_guard(buddy)) clear_page_guard(zone, buddy, order, migratetype); else - del_page_from_free_area(buddy, &zone->free_area[order]); + del_page_from_free_list(buddy, zone, order); combined_pfn = buddy_pfn & pfn; page = page + (combined_pfn - pfn); pfn = combined_pfn; @@ -991,12 +990,11 @@ static inline void __free_one_page(struct page *page, done_merging: set_page_order(page, order); - area = &zone->free_area[order]; if (is_shuffle_order(order) ? shuffle_add_to_tail() : buddy_merge_likely(pfn, buddy_pfn, page, order)) - add_to_free_area_tail(page, area, migratetype); + add_to_free_list_tail(page, zone, order, migratetype); else - add_to_free_area(page, area, migratetype); + add_to_free_list(page, zone, order, migratetype); } /* @@ -2000,13 +1998,11 @@ void __init init_cma_reserved_pageblock(struct page *page) * -- nyc */ static inline void expand(struct zone *zone, struct page *page, - int low, int high, struct free_area *area, - int migratetype) + int low, int high, int migratetype) { unsigned long size = 1 << high; while (high > low) { - area--; high--; size >>= 1; VM_BUG_ON_PAGE(bad_range(zone, &page[size]), &page[size]); @@ -2020,7 +2016,7 @@ static inline void expand(struct zone *zone, struct page *page, if (set_page_guard(zone, &page[size], high, migratetype)) continue; - add_to_free_area(&page[size], area, migratetype); + add_to_free_list(&page[size], zone, high, migratetype); set_page_order(&page[size], high); } } @@ -2178,8 +2174,8 @@ struct page *__rmqueue_smallest(struct zone *zone, unsigned int order, page = get_page_from_free_area(area, migratetype); if (!page) continue; - del_page_from_free_area(page, area); - expand(zone, page, order, current_order, area, migratetype); + del_page_from_free_list(page, zone, current_order); + expand(zone, page, order, current_order, migratetype); set_pcppage_migratetype(page, migratetype); return page; } @@ -2187,7 +2183,6 @@ struct page *__rmqueue_smallest(struct zone *zone, unsigned int order, return NULL; } - /* * This array describes the order lists are fallen back to when * the free lists for the desirable migrate type are depleted @@ -2264,7 +2259,7 @@ static int move_freepages(struct zone *zone, } order = page_order(page); - move_to_free_area(page, &zone->free_area[order], migratetype); + move_to_free_list(page, zone, order, migratetype); page += 1 << order; pages_moved += 1 << order; } @@ -2380,7 +2375,6 @@ static void steal_suitable_fallback(struct zone *zone, struct page *page, unsigned int alloc_flags, int start_type, bool whole_block) { unsigned int current_order = page_order(page); - struct free_area *area; int free_pages, movable_pages, alike_pages; int old_block_type; @@ -2451,8 +2445,7 @@ static void steal_suitable_fallback(struct zone *zone, struct page *page, return; single_page: - area = &zone->free_area[current_order]; - move_to_free_area(page, area, start_type); + move_to_free_list(page, zone, current_order, start_type); } /* @@ -3123,7 +3116,6 @@ void split_page(struct page *page, unsigned int order) int __isolate_free_page(struct page *page, unsigned int order) { - struct free_area *area = &page_zone(page)->free_area[order]; unsigned long watermark; struct zone *zone; int mt; @@ -3149,7 +3141,7 @@ int __isolate_free_page(struct page *page, unsigned int order) /* Remove page from free list */ - del_page_from_free_area(page, area); + del_page_from_free_list(page, zone, order); /* * Set the pageblock if the isolated page is at least half of a @@ -8560,7 +8552,7 @@ void zone_pcp_reset(struct zone *zone) pr_info("remove from free list %lx %d %lx\n", pfn, 1 << order, end_pfn); #endif - del_page_from_free_area(page, &zone->free_area[order]); + del_page_from_free_list(page, zone, order); for (i = 0; i < (1 << order); i++) SetPageReserved((page+i)); pfn += (1 << order); From patchwork Thu Aug 1 22:33:59 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Duyck X-Patchwork-Id: 11071433 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6CEF81399 for ; Thu, 1 Aug 2019 22:36:16 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 4E6C228763 for ; Thu, 1 Aug 2019 22:36:16 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4209828770; Thu, 1 Aug 2019 22:36:16 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6A14E2876B for ; Thu, 1 Aug 2019 22:36:14 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 737D56B0005; Thu, 1 Aug 2019 18:36:13 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 6E9116B0006; Thu, 1 Aug 2019 18:36:13 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5B0DA6B0008; Thu, 1 Aug 2019 18:36:13 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pl1-f200.google.com (mail-pl1-f200.google.com [209.85.214.200]) by kanga.kvack.org (Postfix) with ESMTP id 1406E6B0005 for ; Thu, 1 Aug 2019 18:36:13 -0400 (EDT) Received: by mail-pl1-f200.google.com with SMTP id 65so40418216plf.16 for ; Thu, 01 Aug 2019 15:36:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:subject:from:to:cc:date :message-id:in-reply-to:references:user-agent:mime-version :content-transfer-encoding; bh=59/BrPm8r7N6N6A3jmJpdS2MiATtNM8y6HgeBzrDNWI=; b=lTcRqcFPMv+rhjoK/RXIQUR6h9lIJA1W98FcWVwy3KsQ+jEXM8Ip7BWRaupFClVwbn eBTJHYfxISYsEBReohdo9uVki14uOe4qqZorai2aaVn1TRHquxREznkO/z3Il45qtYOU Q4bYtRhwPcGJduJTqr6UM6Ul8DaQX28uumt9BOmR8tb74AaUfFFjlg/9FbJ2g+JcLIT8 gbnVXDptxrdmYi3aKG9oRlSsuVyvtvVcIWgoDuHgoj9oHWlzZDuZEGixO5/BGm4Tj3qo ucobESpatqbV60MspYwgU5krLreXm3ooQyr+xYZagG9eiBShDVJz3wzQK2LN/hpB9GEd xz3Q== X-Gm-Message-State: APjAAAX/d8HEGRPYE65itGGDuHjesEh1NJNWJa9P+94XIsp5J3dg+ca3 RNqnEjkeBQAEYY9OA+TxWgYR0+CBAcSFlQwjeVHpelVogZTfjTklZNSZVVxh9Cm2t1IflqwlQYW te946vxwSy+hj1R2alNbQTZrWQui/S04fOKs7CNv5fXEUwpM3QqShtijQq1r81XwPJQ== X-Received: by 2002:a63:c246:: with SMTP id l6mr5814287pgg.210.1564698972443; Thu, 01 Aug 2019 15:36:12 -0700 (PDT) X-Received: by 2002:a63:c246:: with SMTP id l6mr5814146pgg.210.1564698970262; Thu, 01 Aug 2019 15:36:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564698970; cv=none; d=google.com; s=arc-20160816; b=lZAJpe9G2eoz8SWnKRiEOa3/Jr8UK6xX/oX5M+qZrOoHbLesprmCU9D8xIR+K/LJpM vm9EgikU4s69TZs6O8Mxfh2f0Vn51HdBy3Kae1hLc+L+FWmPcFerTiYsggjxAO8X8aqW SGhB0TEkzIDZzca2sjxl+9VRiV4xlXI2fraLTcmocjxpKqCjFb/xhPxIQQnyO7iW1pfI PtacORH6mEbRt0fOAYg6U8VB4A8TiPoR53epEzpxEFFE9yXp8iT9nu1pm1xR+8GhMf4s 2BaFxIlxVV3J+HbPdk5S39s80cbs8cvkpPwbtNhPQCchhy2lkwDCJ3lsvvy76+hnDTFm xH5A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:message-id:date:cc:to:from:subject:dkim-signature; bh=59/BrPm8r7N6N6A3jmJpdS2MiATtNM8y6HgeBzrDNWI=; b=M9r3N8cJJyMWWtQ4tGjevZuukFeKbnm+4QYRhIqIuI5SBGi/sT9CINU4qgUMQ6d+F9 h4IksrI9vfTBkunYgfe/BXiAJu1vrAyUFxgm5Ab17D+yG3dSXNEyuovb86tC5mkNxi70 8S5TOl0AoTkwhswmFKHRbNxMD29CysQdysqG+etODdI4GPLIDz5vXmRosPhuurUBigEl wwmf96cIFJTPGAyMh9Jgxk69A7fNiuGCHD7ctHHG2hiNlDNU9SYuP4v9oRbMAsf7ld4B utLU6VRYMaj4jibDAv9kgleVv6F8sTSee8nvGtnUwo7E/AUDTTj/+eU95NO3VnGPTuQP xb2Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=tWSuVmpv; spf=pass (google.com: domain of alexander.duyck@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=alexander.duyck@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id a2sor23939693pgv.3.2019.08.01.15.36.10 for (Google Transport Security); Thu, 01 Aug 2019 15:36:10 -0700 (PDT) Received-SPF: pass (google.com: domain of alexander.duyck@gmail.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=tWSuVmpv; spf=pass (google.com: domain of alexander.duyck@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=alexander.duyck@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:from:to:cc:date:message-id:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=59/BrPm8r7N6N6A3jmJpdS2MiATtNM8y6HgeBzrDNWI=; b=tWSuVmpvgDkHVz2cbg/fddT9GGtlQvIuKfCvcYLvtJDXe7CDj9XMfwqXlpgoUj0EcQ 6U+7AEV7180myFUPAMNMFl+MysPGldAnKbnSwT9UEi5duXAY2og2K7uzB7yMtouJUvqq JvFrvc6Ans16keP8C4fQve+w7tiXTVRiOt2rPetYo9pRI2HKxaAqWB5MRfkZ13Wuxis1 EJ4dVuQN3jBQ5/BJMtL0+r0xodKY8KpO9mYuPoWE0iZHECHCGEfFK5HpsG+c3dfX41uU YrVfuieatztS+y9luaD9vtV4xLzC1vDHspIg5FjUqyROQVe/+dzx5lwFmBksihMCTDd8 WISg== X-Google-Smtp-Source: APXvYqxTUq5kMGeoXZzqA8QL1BPRf5C0u0DAvZXYWpEsB7BRUwEnQHQf39JAAoJ6gxRMEq71Wq3kPg== X-Received: by 2002:a63:904:: with SMTP id 4mr47849411pgj.19.1564698969512; Thu, 01 Aug 2019 15:36:09 -0700 (PDT) Received: from localhost.localdomain (50-39-177-61.bvtn.or.frontiernet.net. [50.39.177.61]) by smtp.gmail.com with ESMTPSA id a15sm98211500pfg.102.2019.08.01.15.36.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 01 Aug 2019 15:36:09 -0700 (PDT) Subject: [PATCH v3 4/6] mm: Introduce Reported pages From: Alexander Duyck To: nitesh@redhat.com, kvm@vger.kernel.org, david@redhat.com, mst@redhat.com, dave.hansen@intel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org Cc: yang.zhang.wz@gmail.com, pagupta@redhat.com, riel@surriel.com, konrad.wilk@oracle.com, willy@infradead.org, lcapitulino@redhat.com, wei.w.wang@intel.com, aarcange@redhat.com, pbonzini@redhat.com, dan.j.williams@intel.com, alexander.h.duyck@linux.intel.com Date: Thu, 01 Aug 2019 15:33:59 -0700 Message-ID: <20190801223359.22190.2212.stgit@localhost.localdomain> In-Reply-To: <20190801222158.22190.96964.stgit@localhost.localdomain> References: <20190801222158.22190.96964.stgit@localhost.localdomain> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP From: Alexander Duyck In order to pave the way for free page reporting in virtualized environments we will need a way to get pages out of the free lists and identify those pages after they have been returned. To accomplish this, this patch adds the concept of a Reported Buddy, which is essentially meant to just be the Uptodate flag used in conjunction with the Buddy page type. It adds a set of pointers we shall call "boundary" which represents the upper boundary between the unreported and reported pages. The general idea is that in order for a page to cross from one side of the boundary to the other it will need to go through the reporting process. Ultimately a free_list has been fully processed when the boundary has been moved from the tail all they way up to occupying the first entry in the list. Doing this we should be able to make certain that we keep the reported pages as one contiguous block in each free list. This will allow us to efficiently manipulate the free lists whenever we need to go in and start sending reports to the hypervisor that there are new pages that have been freed and are no longer in use. An added advantage to this approach is that we should be reducing the overall memory footprint of the guest as it will be more likely to recycle warm pages versus trying to allocate the reported pages that were likely evicted from the guest memory. Since we will only be reporting one zone at a time we keep the boundary limited to being defined for just the zone we are currently reporting pages from. Doing this we can keep the number of additional pointers needed quite small. To flag that the boundaries are in place we use a single bit in the zone to indicate that reporting and the boundaries are active. The determination of when to start reporting is based on the tracking of the number of free pages in a given area versus the number of reported pages in that area. We keep track of the number of reported pages per free_area in a separate zone specific area. We do this to avoid modifying the free_area structure as this can lead to false sharing for the highest order with the zone lock which leads to a noticeable performance degradation. Signed-off-by: Alexander Duyck --- include/linux/mmzone.h | 40 +++++ include/linux/page-flags.h | 11 + include/linux/page_reporting.h | 138 ++++++++++++++++++ mm/Kconfig | 5 + mm/Makefile | 1 mm/memory_hotplug.c | 1 mm/page_alloc.c | 136 ++++++++++++++++++ mm/page_reporting.c | 299 ++++++++++++++++++++++++++++++++++++++++ 8 files changed, 623 insertions(+), 8 deletions(-) create mode 100644 include/linux/page_reporting.h create mode 100644 mm/page_reporting.c diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index f0c68b6b6154..4e6692380deb 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -460,6 +460,14 @@ struct zone { seqlock_t span_seqlock; #endif +#ifdef CONFIG_PAGE_REPORTING + /* + * Pointer to reported page tracking statistics array. The size of + * the array is MAX_ORDER - PAGE_REPORTING_MIN_ORDER. NULL when + * unused page reporting is not present. + */ + unsigned long *reported_pages; +#endif int initialized; /* Write-intensive fields used from the page allocator */ @@ -535,6 +543,14 @@ enum zone_flags { ZONE_BOOSTED_WATERMARK, /* zone recently boosted watermarks. * Cleared when kswapd is woken. */ + ZONE_PAGE_REPORTING_REQUESTED, /* zone enabled page reporting and has + * requested flushing the data out of + * higher order pages. + */ + ZONE_PAGE_REPORTING_ACTIVE, /* zone enabled page reporting and is + * activly flushing the data out of + * higher order pages. + */ }; static inline unsigned long zone_managed_pages(struct zone *zone) @@ -755,6 +771,8 @@ static inline bool pgdat_is_empty(pg_data_t *pgdat) return !pgdat->node_start_pfn && !pgdat->node_spanned_pages; } +#include + /* Used for pages not on another list */ static inline void add_to_free_list(struct page *page, struct zone *zone, unsigned int order, int migratetype) @@ -769,10 +787,16 @@ static inline void add_to_free_list(struct page *page, struct zone *zone, static inline void add_to_free_list_tail(struct page *page, struct zone *zone, unsigned int order, int migratetype) { - struct free_area *area = &zone->free_area[order]; + struct list_head *tail = get_unreported_tail(zone, order, migratetype); - list_add_tail(&page->lru, &area->free_list[migratetype]); - area->nr_free++; + /* + * To prevent the unreported pages from being interleaved with the + * reported ones while we are actively processing pages we will use + * the head of the reported pages to determine the tail of the free + * list. + */ + list_add_tail(&page->lru, tail); + zone->free_area[order].nr_free++; } /* Used for pages which are on another list */ @@ -781,12 +805,22 @@ static inline void move_to_free_list(struct page *page, struct zone *zone, { struct free_area *area = &zone->free_area[order]; + /* + * Clear Hinted flag, if present, to avoid placing reported pages + * at the top of the free_list. It is cheaper to just process this + * page again than to walk around a page that is already reported. + */ + clear_page_reported(page, zone); + list_move(&page->lru, &area->free_list[migratetype]); } static inline void del_page_from_free_list(struct page *page, struct zone *zone, unsigned int order) { + /* Clear Reported flag, if present, before resetting page type */ + clear_page_reported(page, zone); + list_del(&page->lru); __ClearPageBuddy(page); set_page_private(page, 0); diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h index f91cb8898ff0..759a3b3956f2 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -163,6 +163,9 @@ enum pageflags { /* non-lru isolated movable page */ PG_isolated = PG_reclaim, + + /* Buddy pages. Used to track which pages have been reported */ + PG_reported = PG_uptodate, }; #ifndef __GENERATING_BOUNDS_H @@ -432,6 +435,14 @@ static inline bool set_hwpoison_free_buddy_page(struct page *page) #endif /* + * PageReported() is used to track reported free pages within the Buddy + * allocator. We can use the non-atomic version of the test and set + * operations as both should be shielded with the zone lock to prevent + * any possible races on the setting or clearing of the bit. + */ +__PAGEFLAG(Reported, reported, PF_NO_COMPOUND) + +/* * On an anonymous page mapped into a user virtual memory area, * page->mapping points to its anon_vma, not to a struct address_space; * with the PAGE_MAPPING_ANON bit set to distinguish it. See rmap.h. diff --git a/include/linux/page_reporting.h b/include/linux/page_reporting.h new file mode 100644 index 000000000000..498bde6ea764 --- /dev/null +++ b/include/linux/page_reporting.h @@ -0,0 +1,138 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_PAGE_REPORTING_H +#define _LINUX_PAGE_REPORTING_H + +#include +#include +#include +#include + +#define PAGE_REPORTING_MIN_ORDER pageblock_order +#define PAGE_REPORTING_HWM 32 + +#ifdef CONFIG_PAGE_REPORTING +struct page_reporting_dev_info { + /* function that alters pages to make them "reported" */ + void (*report)(struct page_reporting_dev_info *phdev, + unsigned int nents); + + /* scatterlist containing pages to be processed */ + struct scatterlist *sg; + + /* + * Upper limit on the number of pages that the react function + * expects to be placed into the batch list to be processed. + */ + unsigned long capacity; + + /* work struct for processing reports */ + struct delayed_work work; + + /* + * The number of zones requesting reporting, plus one additional if + * processing thread is active. + */ + atomic_t refcnt; +}; + +extern struct static_key page_reporting_notify_enabled; + +/* Boundary functions */ +struct list_head *__page_reporting_get_boundary(unsigned int order, + int migratetype); +void page_reporting_del_from_boundary(struct page *page, struct zone *zone); +void page_reporting_add_to_boundary(struct page *page, struct zone *zone, + int migratetype); + +/* Hinted page accessors, defined in page_alloc.c */ +struct page *get_unreported_page(struct zone *zone, unsigned int order, + int migratetype); +void put_reported_page(struct zone *zone, struct page *page); + +void __page_reporting_request(struct zone *zone); +void __page_reporting_free_stats(struct zone *zone); + +/* Tear-down and bring-up for page reporting devices */ +void page_reporting_shutdown(struct page_reporting_dev_info *phdev); +int page_reporting_startup(struct page_reporting_dev_info *phdev); +#endif /* CONFIG_PAGE_REPORTING */ + +static inline struct list_head * +get_unreported_tail(struct zone *zone, unsigned int order, int migratetype) +{ +#ifdef CONFIG_PAGE_REPORTING + if (order >= PAGE_REPORTING_MIN_ORDER && + test_bit(ZONE_PAGE_REPORTING_ACTIVE, &zone->flags)) + return __page_reporting_get_boundary(order, migratetype); +#endif + return &zone->free_area[order].free_list[migratetype]; +} + +static inline void clear_page_reported(struct page *page, + struct zone *zone) +{ +#ifdef CONFIG_PAGE_REPORTING + if (likely(!PageReported(page))) + return; + + /* push boundary back if we removed the upper boundary */ + if (test_bit(ZONE_PAGE_REPORTING_ACTIVE, &zone->flags)) + page_reporting_del_from_boundary(page, zone); + + __ClearPageReported(page); + + /* page_private will contain the page order, so just use it directly */ + zone->reported_pages[page_private(page) - PAGE_REPORTING_MIN_ORDER]--; +#endif +} + +/* Free reported_pages and reset reported page tracking count to 0 */ +static inline void page_reporting_reset(struct zone *zone) +{ +#ifdef CONFIG_PAGE_REPORTING + if (zone->reported_pages) + __page_reporting_free_stats(zone); +#endif +} + +/** + * page_reporting_notify_free - Free page notification to start page processing + * @zone: Pointer to current zone of last page processed + * @order: Order of last page added to zone + * + * This function is meant to act as a screener for __page_reporting_request + * which will determine if a give zone has crossed over the high-water mark + * that will justify us beginning page treatment. If we have crossed that + * threshold then it will start the process of pulling some pages and + * placing them in the batch list for treatment. + */ +static inline void page_reporting_notify_free(struct zone *zone, int order) +{ +#ifdef CONFIG_PAGE_REPORTING + unsigned long nr_reported; + + /* Called from hot path in __free_one_page() */ + if (!static_key_false(&page_reporting_notify_enabled)) + return; + + /* Limit notifications only to higher order pages */ + if (order < PAGE_REPORTING_MIN_ORDER) + return; + + /* Do not bother with tests if we have already requested reporting */ + if (test_bit(ZONE_PAGE_REPORTING_REQUESTED, &zone->flags)) + return; + + /* If reported_pages is not populated, assume 0 */ + nr_reported = zone->reported_pages ? + zone->reported_pages[order - PAGE_REPORTING_MIN_ORDER] : 0; + + /* Only request it if we have enough to begin the page reporting */ + if (zone->free_area[order].nr_free < nr_reported + PAGE_REPORTING_HWM) + return; + + /* This is slow, but should be called very rarely */ + __page_reporting_request(zone); +#endif +} +#endif /*_LINUX_PAGE_REPORTING_H */ diff --git a/mm/Kconfig b/mm/Kconfig index 56cec636a1fc..f5c68bba522f 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -237,6 +237,11 @@ config COMPACTION linux-mm@kvack.org. # +# support for unused page reporting +config PAGE_REPORTING + bool + +# # support for page migration # config MIGRATION diff --git a/mm/Makefile b/mm/Makefile index d0b295c3b764..1e17ba0ed2f0 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -105,3 +105,4 @@ obj-$(CONFIG_PERCPU_STATS) += percpu-stats.o obj-$(CONFIG_ZONE_DEVICE) += memremap.o obj-$(CONFIG_HMM_MIRROR) += hmm.o obj-$(CONFIG_MEMFD_CREATE) += memfd.o +obj-$(CONFIG_PAGE_REPORTING) += page_reporting.o diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index 9a82e12bd0e7..3acd2c3e53b3 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -1608,6 +1608,7 @@ static int __ref __offline_pages(unsigned long start_pfn, if (!populated_zone(zone)) { zone_pcp_reset(zone); build_all_zonelists(NULL); + page_reporting_reset(zone); } else zone_pcp_update(zone); diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 71aadc7d5ff6..69b848e5b83f 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -68,6 +68,7 @@ #include #include #include +#include #include #include @@ -915,7 +916,7 @@ static inline struct capture_control *task_capc(struct zone *zone) static inline void __free_one_page(struct page *page, unsigned long pfn, struct zone *zone, unsigned int order, - int migratetype) + int migratetype, bool reported) { struct capture_control *capc = task_capc(zone); unsigned long uninitialized_var(buddy_pfn); @@ -990,11 +991,20 @@ static inline void __free_one_page(struct page *page, done_merging: set_page_order(page, order); - if (is_shuffle_order(order) ? shuffle_add_to_tail() : - buddy_merge_likely(pfn, buddy_pfn, page, order)) + if (reported || + (is_shuffle_order(order) ? shuffle_add_to_tail() : + buddy_merge_likely(pfn, buddy_pfn, page, order))) add_to_free_list_tail(page, zone, order, migratetype); else add_to_free_list(page, zone, order, migratetype); + + /* + * No need to notify on a reported page as the total count of + * unreported pages will not have increased since we have essentially + * merged the reported page with one or more unreported pages. + */ + if (!reported) + page_reporting_notify_free(zone, order); } /* @@ -1305,7 +1315,7 @@ static void free_pcppages_bulk(struct zone *zone, int count, if (unlikely(isolated_pageblocks)) mt = get_pageblock_migratetype(page); - __free_one_page(page, page_to_pfn(page), zone, 0, mt); + __free_one_page(page, page_to_pfn(page), zone, 0, mt, false); trace_mm_page_pcpu_drain(page, 0, mt); } spin_unlock(&zone->lock); @@ -1321,7 +1331,7 @@ static void free_one_page(struct zone *zone, is_migrate_isolate(migratetype))) { migratetype = get_pfnblock_migratetype(page, pfn); } - __free_one_page(page, pfn, zone, order, migratetype); + __free_one_page(page, pfn, zone, order, migratetype, false); spin_unlock(&zone->lock); } @@ -2183,6 +2193,122 @@ struct page *__rmqueue_smallest(struct zone *zone, unsigned int order, return NULL; } +#ifdef CONFIG_PAGE_REPORTING +/** + * get_unreported_page - Pull an unreported page from the free_list + * @zone: Zone to draw pages from + * @order: Order to draw pages from + * @mt: Migratetype to draw pages from + * + * This function will obtain a page from the free list. It will start by + * attempting to pull from the tail of the free list and if that is already + * reported on it will instead pull the head if that is unreported. + * + * The page will have the migrate type and order stored in the page + * metadata. While being processed the page will not be avaialble for + * allocation. + * + * Return: page pointer if raw page found, otherwise NULL + */ +struct page *get_unreported_page(struct zone *zone, unsigned int order, int mt) +{ + struct list_head *tail = get_unreported_tail(zone, order, mt); + struct free_area *area = &(zone->free_area[order]); + struct list_head *list = &area->free_list[mt]; + struct page *page; + + /* zone lock should be held when this function is called */ + lockdep_assert_held(&zone->lock); + + /* Find a page of the appropriate size in the preferred list */ + page = list_last_entry(tail, struct page, lru); + list_for_each_entry_from_reverse(page, list, lru) { + /* If we entered this loop then the "raw" list isn't empty */ + + /* If the page is reported try the head of the list */ + if (PageReported(page)) { + page = list_first_entry(list, struct page, lru); + + /* + * If both the head and tail are reported then reset + * the boundary so that we read as an empty list + * next time and bail out. + */ + if (PageReported(page)) { + page_reporting_add_to_boundary(page, zone, mt); + break; + } + } + + del_page_from_free_list(page, zone, order); + + /* record migratetype and order within page */ + set_pcppage_migratetype(page, mt); + set_page_private(page, order); + + /* + * Page will not be available for allocation while we are + * processing it so update the freepage state. + */ + __mod_zone_freepage_state(zone, -(1 << order), mt); + + return page; + } + + return NULL; +} + +/** + * put_reported_page - Return a now-reported page back where we got it + * @zone: Zone to return pages to + * @page: Page that was reported + * + * This function will pull the migratetype and order information out + * of the page and attempt to return it where it found it. If the page + * is added to the free list without changes we will mark it as being + * reported. + */ +void put_reported_page(struct zone *zone, struct page *page) +{ + unsigned int order, mt; + unsigned long pfn; + + /* zone lock should be held when this function is called */ + lockdep_assert_held(&zone->lock); + + mt = get_pcppage_migratetype(page); + pfn = page_to_pfn(page); + + if (unlikely(has_isolate_pageblock(zone) || is_migrate_isolate(mt))) { + mt = get_pfnblock_migratetype(page, pfn); + set_pcppage_migratetype(page, mt); + } + + order = page_private(page); + set_page_private(page, 0); + + __free_one_page(page, pfn, zone, order, mt, true); + + /* + * If page was comingled with another page we cannot consider + * the result to be "reported" since part of the page hasn't been. + * In this case we will simply exit and not update the "reported" + * state. Instead just treat the result as a unreported page. + */ + if (!PageBuddy(page) || page_order(page) != order) + return; + + /* update areated page accounting */ + zone->reported_pages[order - PAGE_REPORTING_MIN_ORDER]++; + + /* update boundary of new migratetype and record it */ + page_reporting_add_to_boundary(page, zone, mt); + + /* flag page as reported */ + __SetPageReported(page); +} +#endif /* CONFIG_PAGE_REPORTING */ + /* * This array describes the order lists are fallen back to when * the free lists for the desirable migrate type are depleted diff --git a/mm/page_reporting.c b/mm/page_reporting.c new file mode 100644 index 000000000000..971138205ae5 --- /dev/null +++ b/mm/page_reporting.c @@ -0,0 +1,299 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include +#include +#include +#include +#include "internal.h" + +static struct page_reporting_dev_info __rcu *ph_dev_info __read_mostly; +struct static_key page_reporting_notify_enabled; + +struct list_head *boundary[MAX_ORDER - PAGE_REPORTING_MIN_ORDER][MIGRATE_TYPES]; + +static void page_reporting_reset_boundary(struct zone *zone, unsigned int order, + unsigned int migratetype) +{ + boundary[order - PAGE_REPORTING_MIN_ORDER][migratetype] = + &zone->free_area[order].free_list[migratetype]; +} + +#define for_each_reporting_migratetype_order(_order, _type) \ + for (_order = MAX_ORDER; _order-- != PAGE_REPORTING_MIN_ORDER;) \ + for (_type = MIGRATE_TYPES; _type--;) + +static int page_reporting_populate_metadata(struct zone *zone) +{ + unsigned int order, mt; + + /* + * We need to make sure we have somewhere to store the tracking + * data for how many reported pages are in the zone. To do that + * we need to make certain zone->reported_pages is populated. + */ + if (!zone->reported_pages) { + zone->reported_pages = + kcalloc(MAX_ORDER - PAGE_REPORTING_MIN_ORDER, + sizeof(unsigned long), + GFP_KERNEL); + if (!zone->reported_pages) + return -ENOMEM; + } + + /* Update boundary data to reflect the zone we are currently working */ + for_each_reporting_migratetype_order(order, mt) + page_reporting_reset_boundary(zone, order, mt); + + return 0; +} + +struct list_head *__page_reporting_get_boundary(unsigned int order, + int migratetype) +{ + return boundary[order - PAGE_REPORTING_MIN_ORDER][migratetype]; +} + +void page_reporting_del_from_boundary(struct page *page, struct zone *zone) +{ + unsigned int order = page_private(page) - PAGE_REPORTING_MIN_ORDER; + int mt = get_pcppage_migratetype(page); + struct list_head **tail = &boundary[order][mt]; + + if (*tail == &page->lru) + *tail = page->lru.next; +} + +void page_reporting_add_to_boundary(struct page *page, struct zone *zone, + int migratetype) +{ + unsigned int order = page_private(page) - PAGE_REPORTING_MIN_ORDER; + struct list_head **tail = &boundary[order][migratetype]; + + *tail = &page->lru; +} + +static unsigned int page_reporting_fill(struct zone *zone, + struct page_reporting_dev_info *phdev) +{ + struct scatterlist *sg = phdev->sg; + unsigned int order, mt, count = 0; + + sg_init_table(phdev->sg, phdev->capacity); + + for_each_reporting_migratetype_order(order, mt) { + struct page *page; + + /* + * Pull pages from free list until we have drained + * it or we have reached capacity. + */ + while ((page = get_unreported_page(zone, order, mt))) { + sg_set_page(&sg[count], page, PAGE_SIZE << order, 0); + + if (++count == phdev->capacity) + return count; + } + } + + /* mark end of scatterlist due to underflow */ + if (count) + sg_mark_end(&sg[count - 1]); + + /* + * If there are no longer enough free pages to fully populate + * the scatterlist, then we can just shut it down for this zone. + */ + __clear_bit(ZONE_PAGE_REPORTING_REQUESTED, &zone->flags); + atomic_dec(&phdev->refcnt); + + return count; +} + +static void page_reporting_drain(struct zone *zone, + struct page_reporting_dev_info *phdev) +{ + struct scatterlist *sg = phdev->sg; + + /* + * Drain the now reported pages back into their respective + * free lists/areas. We assume at least one page is populated. + */ + do { + put_reported_page(zone, sg_page(sg)); + } while (!sg_is_last(sg++)); +} + +/* + * The page reporting cycle consists of 4 stages, fill, report, drain, and idle. + * We will cycle through the first 3 stages until we fail to obtain any + * pages, in that case we will switch to idle. + */ +static void page_reporting_cycle(struct zone *zone, + struct page_reporting_dev_info *phdev) +{ + /* + * Guarantee boundaries and stats are populated before we + * start placing reported pages in the zone. + */ + if (page_reporting_populate_metadata(zone)) + return; + + spin_lock(&zone->lock); + + /* set bit indicating boundaries are present */ + __set_bit(ZONE_PAGE_REPORTING_ACTIVE, &zone->flags); + + do { + /* Pull pages out of allocator into a scaterlist */ + unsigned int nents = page_reporting_fill(zone, phdev); + + /* no pages were acquired, give up */ + if (!nents) + break; + + spin_unlock(&zone->lock); + + /* begin processing pages in local list */ + phdev->report(phdev, nents); + + spin_lock(&zone->lock); + + /* + * We should have a scatterlist of pages that have been + * processed. Return them to their original free lists. + */ + page_reporting_drain(zone, phdev); + + /* keep pulling pages till there are none to pull */ + } while (test_bit(ZONE_PAGE_REPORTING_REQUESTED, &zone->flags)); + + /* processing of the zone is complete, we can disable boundaries */ + __clear_bit(ZONE_PAGE_REPORTING_ACTIVE, &zone->flags); + + spin_unlock(&zone->lock); +} + +static void page_reporting_process(struct work_struct *work) +{ + struct delayed_work *d_work = to_delayed_work(work); + struct page_reporting_dev_info *phdev = + container_of(d_work, struct page_reporting_dev_info, work); + struct zone *zone = first_online_pgdat()->node_zones; + + do { + if (test_bit(ZONE_PAGE_REPORTING_REQUESTED, &zone->flags)) + page_reporting_cycle(zone, phdev); + + /* + * Move to next zone, if at the end of the list + * test to see if we can just go into idle. + */ + zone = next_zone(zone); + if (zone) + continue; + zone = first_online_pgdat()->node_zones; + + /* + * As long as refcnt has not reached zero there are still + * zones to be processed. + */ + } while (atomic_read(&phdev->refcnt)); +} + +/* request page reporting on this zone */ +void __page_reporting_request(struct zone *zone) +{ + struct page_reporting_dev_info *phdev; + + rcu_read_lock(); + + /* + * We use RCU to protect the ph_dev_info pointer. In almost all + * cases this should be present, however in the unlikely case of + * a shutdown this will be NULL and we should exit. + */ + phdev = rcu_dereference(ph_dev_info); + if (unlikely(!phdev)) + return; + + /* + * We can use separate test and set operations here as there + * is nothing else that can set or clear this bit while we are + * holding the zone lock. The advantage to doing it this way is + * that we don't have to dirty the cacheline unless we are + * changing the value. + */ + __set_bit(ZONE_PAGE_REPORTING_REQUESTED, &zone->flags); + + /* + * Delay the start of work to allow a sizable queue to + * build. For now we are limiting this to running no more + * than 10 times per second. + */ + if (!atomic_fetch_inc(&phdev->refcnt)) + schedule_delayed_work(&phdev->work, HZ / 10); + + rcu_read_unlock(); +} + +void __page_reporting_free_stats(struct zone *zone) +{ + /* free reported_page statisitics */ + kfree(zone->reported_pages); + zone->reported_pages = NULL; +} + +void page_reporting_shutdown(struct page_reporting_dev_info *phdev) +{ + if (rcu_access_pointer(ph_dev_info) != phdev) + return; + + /* Disable page reporting notification */ + static_key_slow_dec(&page_reporting_notify_enabled); + RCU_INIT_POINTER(ph_dev_info, NULL); + synchronize_rcu(); + + /* Flush any existing work, and lock it out */ + cancel_delayed_work_sync(&phdev->work); + + /* Free scatterlist */ + kfree(phdev->sg); + phdev->sg = NULL; +} +EXPORT_SYMBOL_GPL(page_reporting_shutdown); + +int page_reporting_startup(struct page_reporting_dev_info *phdev) +{ + struct zone *zone; + + /* nothing to do if already in use */ + if (rcu_access_pointer(ph_dev_info)) + return -EBUSY; + + /* allocate scatterlist to store pages being reported on */ + phdev->sg = kcalloc(phdev->capacity, sizeof(*phdev->sg), GFP_KERNEL); + if (!phdev->sg) + return -ENOMEM; + + /* initialize refcnt and work structures */ + atomic_set(&phdev->refcnt, 0); + INIT_DELAYED_WORK(&phdev->work, &page_reporting_process); + + /* assign device, and begin initial flush of populated zones */ + rcu_assign_pointer(ph_dev_info, phdev); + for_each_populated_zone(zone) { + spin_lock(&zone->lock); + __page_reporting_request(zone); + spin_unlock(&zone->lock); + } + + /* enable page reporting notification */ + static_key_slow_inc(&page_reporting_notify_enabled); + + return 0; +} +EXPORT_SYMBOL_GPL(page_reporting_startup); + From patchwork Thu Aug 1 22:36:14 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Duyck X-Patchwork-Id: 11071441 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id D55EE1398 for ; Thu, 1 Aug 2019 22:38:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C352328779 for ; Thu, 1 Aug 2019 22:38:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B79C428785; Thu, 1 Aug 2019 22:38:27 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5142028779 for ; Thu, 1 Aug 2019 22:38:27 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 896656B0003; Thu, 1 Aug 2019 18:38:26 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 8460A6B0005; Thu, 1 Aug 2019 18:38:26 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 736C06B0006; Thu, 1 Aug 2019 18:38:26 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pg1-f197.google.com (mail-pg1-f197.google.com [209.85.215.197]) by kanga.kvack.org (Postfix) with ESMTP id 3F1726B0003 for ; Thu, 1 Aug 2019 18:38:26 -0400 (EDT) Received: by mail-pg1-f197.google.com with SMTP id e33so4077432pgm.20 for ; Thu, 01 Aug 2019 15:38:26 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:subject:from:to:cc:date :message-id:in-reply-to:references:user-agent:mime-version :content-transfer-encoding; bh=N1FHoD6ZMNB/7wrUWG2IW6DScPoWU0//Swe9DNcA3ZM=; b=Bdfvu4HHxBfVLWSgMdnzADn8MZtstBiHccMQlRBkiPiPw+QugFxT/UyA2JItcCu/cr TvQkSk8h593h/0wFZXQw+RrkW85OrRHwiofXWHOv38AJc49B6gl19uxytSpk3azsgVh6 yAReq1bx4SxxcX7pnWSFhUchgegMwu7yEnCeqI4vrONQXn2TkKmQJZ2H+iwATRSoGhA0 JrnaDQFuTxLB3krSS3LNV7nN77O39rItiBPX15nP5l7BjKnhnFER16jLD+OF+GvWZjFW YA47k+NBGifcfF4YUYuM+6YB3HOBp51dbTldynDV+Z2tw3fAm4U7yWpPrzQ4DNFTgOPU PfGg== X-Gm-Message-State: APjAAAU9yPWA4r5LFDOCAmNAJK+UUAFl3mlcnGF2DDaJ0aUHX4PUhb5Z 46/BOFuDo1UchEfyJn8/MwfE3g3frSBrfYYFGBRg5tgi5SdN/JisLjbB43KiFwvHBvqxK1DbLpq C/JvUfBW0KjClcRGTw4xr2c/iGGiaElY+ehMnVc+5q1wF6a61t8a2ZWWzhqmvApKGhQ== X-Received: by 2002:a17:902:106:: with SMTP id 6mr130627023plb.64.1564699105880; Thu, 01 Aug 2019 15:38:25 -0700 (PDT) X-Received: by 2002:a17:902:106:: with SMTP id 6mr130626984plb.64.1564699105071; Thu, 01 Aug 2019 15:38:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564699105; cv=none; d=google.com; s=arc-20160816; b=Jtg4D0A4JW3h9PsfF4WLLNbg0T+y/vbKSgCs7DsqvSFPB9FWoHwGwn6f7hV+1r/4vq muQrnkdHaljPAaddN2XMldQmZPilQRCiEyQywx4DamSpAn+++Pvn1eJyuRrUISEwAvsv fgxLmNjpBP0pVML4iEPYVLuqwIHSXKnA7rxtmB77aMw/fE4UvQtXy+ynPonJtLzcHdK5 081nErSL81k/qjxW+dz4wWKvHyrNs3lj2bL9znwAvZNHbqbuvmUGV0pXqyPLR7X2Vof1 XmpHOjJ6Vpyb4fbwNnCxzIWf3sIa8infKtw1Ci8DVXt9vcjROm/1Er1HCldVqQA9YBCh E2rA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:message-id:date:cc:to:from:subject:dkim-signature; bh=N1FHoD6ZMNB/7wrUWG2IW6DScPoWU0//Swe9DNcA3ZM=; b=G3w+hv0vxuckGo91bRN3dwgNfbdoLPbBrj01RrWmE0D31FCwD1zs9vvG0i37y7Qp3T tYULzow69IJXmEj1ugdTrlNq6aD9MOur4/07dY1a6Xg58Rfv2VF9YUlSk6Oh5aNZkEp7 wVWhuktQYMh2VgqANzCdoTCG9c50srhvR7YMv3dt8CHgLr9WaPf5ytepUDUhddt6yM/z MmaVLbr8YxHx4BG7j9qSOqROfktOmCevyMm8F/BiiBihqauxmIdTPrEOZF4R7aLYySDZ CWO4f3U5rORdSUNSw9gsHhkQi3RL7RmCkzgXVcLYd3Bma9WsRh0uajTon+aunk00mcN5 m/tw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=XFfZZnwA; spf=pass (google.com: domain of alexander.duyck@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=alexander.duyck@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id a26sor7613673pgw.21.2019.08.01.15.38.24 for (Google Transport Security); Thu, 01 Aug 2019 15:38:25 -0700 (PDT) Received-SPF: pass (google.com: domain of alexander.duyck@gmail.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=XFfZZnwA; spf=pass (google.com: domain of alexander.duyck@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=alexander.duyck@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:from:to:cc:date:message-id:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=N1FHoD6ZMNB/7wrUWG2IW6DScPoWU0//Swe9DNcA3ZM=; b=XFfZZnwAZZwn1mNo4698+qwwq8EWUR8tD+MEvMVS1YxxOJ+XaCbvSDrhXzU0UpPFXt RI/tHQXreDa0C0u5/FRzOf7Ug4Gp+Dfi0A/iJfMkpLXdYHW985Tq1ipt6OfuszPSmJrc aMEZqE1BEwMU5oPUjvvBzFEX5vwAXakovlY1Jw7sLoIPPOa3ZYA5Gy2T0TfiutLvftMN 8pfMqm7nhNgrxb+HGgE41icZettMyX9/2/v5b4NXkh7jXBXDn/TvBwD2TX2cgIEOEkeu hGXc/K0dQgRhXWUSlw/lE2KpV4Xfw33Jv1WT5XpLWQsyrWaXreinbBozR89Df1egnto3 yDQg== X-Google-Smtp-Source: APXvYqzw8ShusbndySzoosJElpXX2Y+WlAECKmosIb2kCuPU1zdsy0LGNncMH17fmwy0VtCJ4X3n/w== X-Received: by 2002:a65:6284:: with SMTP id f4mr64617613pgv.416.1564699104593; Thu, 01 Aug 2019 15:38:24 -0700 (PDT) Received: from localhost.localdomain (50-39-177-61.bvtn.or.frontiernet.net. [50.39.177.61]) by smtp.gmail.com with ESMTPSA id a3sm72677006pfc.70.2019.08.01.15.38.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 01 Aug 2019 15:38:24 -0700 (PDT) Subject: [PATCH v3 5/6] virtio-balloon: Pull page poisoning config out of free page hinting From: Alexander Duyck To: nitesh@redhat.com, kvm@vger.kernel.org, david@redhat.com, mst@redhat.com, dave.hansen@intel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org Cc: yang.zhang.wz@gmail.com, pagupta@redhat.com, riel@surriel.com, konrad.wilk@oracle.com, willy@infradead.org, lcapitulino@redhat.com, wei.w.wang@intel.com, aarcange@redhat.com, pbonzini@redhat.com, dan.j.williams@intel.com, alexander.h.duyck@linux.intel.com Date: Thu, 01 Aug 2019 15:36:14 -0700 Message-ID: <20190801223614.22190.40937.stgit@localhost.localdomain> In-Reply-To: <20190801222158.22190.96964.stgit@localhost.localdomain> References: <20190801222158.22190.96964.stgit@localhost.localdomain> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP From: Alexander Duyck Currently the page poisoning setting wasn't being enabled unless free page hinting was enabled. However we will need the page poisoning tracking logic as well for unused page reporting. As such pull it out and make it a separate bit of config in the probe function. In addition we can actually wrap the code in a check for NO_SANITY. If we don't care what is actually in the page we can just default to 0 and leave it there. Signed-off-by: Alexander Duyck --- drivers/virtio/virtio_balloon.c | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c index 226fbb995fb0..2c19457ab573 100644 --- a/drivers/virtio/virtio_balloon.c +++ b/drivers/virtio/virtio_balloon.c @@ -842,7 +842,6 @@ static int virtio_balloon_register_shrinker(struct virtio_balloon *vb) static int virtballoon_probe(struct virtio_device *vdev) { struct virtio_balloon *vb; - __u32 poison_val; int err; if (!vdev->config->get) { @@ -909,11 +908,19 @@ static int virtballoon_probe(struct virtio_device *vdev) VIRTIO_BALLOON_CMD_ID_STOP); spin_lock_init(&vb->free_page_list_lock); INIT_LIST_HEAD(&vb->free_page_list); - if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_PAGE_POISON)) { - memset(&poison_val, PAGE_POISON, sizeof(poison_val)); - virtio_cwrite(vb->vdev, struct virtio_balloon_config, - poison_val, &poison_val); - } + } + if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_PAGE_POISON)) { + __u32 poison_val = 0; + +#if !defined(CONFIG_PAGE_POISONING_NO_SANITY) + /* + * Let hypervisor know that we are expecting a specific + * value to be written back in unused pages. + */ + memset(&poison_val, PAGE_POISON, sizeof(poison_val)); +#endif + virtio_cwrite(vb->vdev, struct virtio_balloon_config, + poison_val, &poison_val); } /* * We continue to use VIRTIO_BALLOON_F_DEFLATE_ON_OOM to decide if a From patchwork Thu Aug 1 22:38:29 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Duyck X-Patchwork-Id: 11071459 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 237541399 for ; Thu, 1 Aug 2019 22:40:44 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0E1E7285CB for ; Thu, 1 Aug 2019 22:40:44 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EDD1B285F9; Thu, 1 Aug 2019 22:40:43 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-3.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,FREEMAIL_FROM,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 27D38285CB for ; Thu, 1 Aug 2019 22:40:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 2C80F6B0003; Thu, 1 Aug 2019 18:40:42 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 2521B6B0005; Thu, 1 Aug 2019 18:40:42 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 0F2C96B0006; Thu, 1 Aug 2019 18:40:42 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pg1-f198.google.com (mail-pg1-f198.google.com [209.85.215.198]) by kanga.kvack.org (Postfix) with ESMTP id C82EE6B0003 for ; Thu, 1 Aug 2019 18:40:41 -0400 (EDT) Received: by mail-pg1-f198.google.com with SMTP id p29so37867962pgm.10 for ; Thu, 01 Aug 2019 15:40:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:dkim-signature:subject:from:to:cc:date :message-id:in-reply-to:references:user-agent:mime-version :content-transfer-encoding; bh=3HK7s8V9oToEwvd+cxmNBKqtwxYR1WNQK7rC8iWKNTA=; b=CAdJzk7wOn1PsLEpTPVpN+bi+HuLcOGgqY/VcwpfWSK7y2clVlF/41g5aJ6zoQf9eW MpopYKZdRQ7QekrlF8c5yq5si5NiZdeKQ/YtE/1X28EzubFsevG3Mok2DpdUbQTzRTSK nuc/sfeknKwePqcQYDZKd/tGSX5kfe5yiZrITFuYRV3rUdJwgGnXTw8CR8U0qm71dAwE gjd6K9X7u+nl2LinS5YhJyV5japTiS8uH8gIGA5PxkjKpORLSuHIP5BLZukr0THZKs3G 5Nm1OykFnGrnmPrHz7yxJJ6AVGs9/KAytQWCiIL0EfMTqUHVKEUYKj+dTVGGkUhUF1H2 04Tg== X-Gm-Message-State: APjAAAUyJtYdPHOZ8VCrpQFg6g6s4CzQUIGiJxcH4WAsCh4dLWBl7YPJ TvbBbmJLRIGVWJ+PovSclyeA0xzmU+v0Sj+q+feYg4LKD9RTYaOMQzYSjpg+rsmVcC5XnKzKo1S atK6n3LSwujkS6Ti1YJ1vnwokpBo40OBdciwzi9l9NZEAZqJ6IrXjjji6JPHbX1LQ0w== X-Received: by 2002:a17:90a:bb0c:: with SMTP id u12mr1130829pjr.132.1564699241464; Thu, 01 Aug 2019 15:40:41 -0700 (PDT) X-Received: by 2002:a17:90a:bb0c:: with SMTP id u12mr1130782pjr.132.1564699240484; Thu, 01 Aug 2019 15:40:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564699240; cv=none; d=google.com; s=arc-20160816; b=MysqeWin82rzzAUKmg118NyU7rA5KxpSRUolv6NGinkMVnoM7IE8wrf2RsAm935JUl 1FNjbzUSRK+3bbtrdc++YZyNn0M5LQ05OFY7GUnHfUHHjHFrOONYaFS9oVESx/gfH6TG jugVAax/EwD9NcBwosmTMr4yeHWcupJYw0lk33YW07lcOI0o+aZSl5StcOTtZdtc7lbe 9n7Pz3zCDqKOEa4TBvficLcHdmPzAJvKEo2BBignGQPA/JvCiiZBGuqX2MEqKQkuLeYD V4UHD2vbmJT4/PhaVrGCIDy04Gmt7hfdfrRcbTKTRlUF5qWm+7QRurIGsZtn7BujB635 P6PA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:user-agent:references :in-reply-to:message-id:date:cc:to:from:subject:dkim-signature; bh=3HK7s8V9oToEwvd+cxmNBKqtwxYR1WNQK7rC8iWKNTA=; b=aXE4a34qRi4F+xS5xGvIjLgZA7DRhas21ofRvBxpDtc7GVWQCJeyIomCGcwhaQ4Ruw e5MdARfp0qrtyZUeuqt6mRXb+NkaOPtXqYmJDAliSIeBfh8zOefnMMTrV/GXV+zYc4Nh 9E2ZVi44KEI+yh7fGYFf++mWJxAiMin+qZmQ6XlpBfxO2S95q3RbfevKLJFznObiSKOV dkbOdeJC5FRMO5eAHbDe36nyI2vUCma8ftmkMA0Uaa8+9oo9EtPqh2BEB/USFNngH5S7 bTtfVJvck5QaVZD5MvkY6rAG/A0yRXJvke+xmSxgxHUBbTmw19Eq8buwuLynFmEsqn9V nZRA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=PBkxxqkq; spf=pass (google.com: domain of alexander.duyck@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=alexander.duyck@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from mail-sor-f65.google.com (mail-sor-f65.google.com. [209.85.220.65]) by mx.google.com with SMTPS id go5sor87493257plb.37.2019.08.01.15.40.40 for (Google Transport Security); Thu, 01 Aug 2019 15:40:40 -0700 (PDT) Received-SPF: pass (google.com: domain of alexander.duyck@gmail.com designates 209.85.220.65 as permitted sender) client-ip=209.85.220.65; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=PBkxxqkq; spf=pass (google.com: domain of alexander.duyck@gmail.com designates 209.85.220.65 as permitted sender) smtp.mailfrom=alexander.duyck@gmail.com; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:from:to:cc:date:message-id:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=3HK7s8V9oToEwvd+cxmNBKqtwxYR1WNQK7rC8iWKNTA=; b=PBkxxqkqhBjreAGB6z65259PHXpXjbB56D8LK6aUxC7WTe13WXQdOe0JObkGOxskaY uTAP+faUrKTLju60cWzKXlLRa4lAfZ7cGp7ygvI0Me5wvqZWEIb8xvtDdfQwsENAMttT p+sJhwkiTsZmw96ZOJIVXbMCFA7esE1vv6Y8e52MmBooVoM8hmu5Oiv5yVTPicdTZol3 n+kslX7MyBeVvweojm/SWb1w4/geiOAkZkTUH1y7yiZADHuOFHzSnwiWhO6Pia/Q8wzw Lee8l4ZormBvYtwwkuX6m2S5YxqvoAZ4Mb9gtYAPYFR0D0pEOFeeT4wS2drToOZk8mBT WogA== X-Google-Smtp-Source: APXvYqwoJCZIGbIQViC2MjQjTd24MGg+1FLC/h9IvYji19WP/MLyZg4rQys/cq+3O0XAf28YLIapHQ== X-Received: by 2002:a17:902:fa2:: with SMTP id 31mr130436244plz.38.1564699239933; Thu, 01 Aug 2019 15:40:39 -0700 (PDT) Received: from localhost.localdomain (50-39-177-61.bvtn.or.frontiernet.net. [50.39.177.61]) by smtp.gmail.com with ESMTPSA id l31sm114821655pgm.63.2019.08.01.15.40.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 01 Aug 2019 15:40:39 -0700 (PDT) Subject: [PATCH v3 6/6] virtio-balloon: Add support for providing unused page reports to host From: Alexander Duyck To: nitesh@redhat.com, kvm@vger.kernel.org, david@redhat.com, mst@redhat.com, dave.hansen@intel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org Cc: yang.zhang.wz@gmail.com, pagupta@redhat.com, riel@surriel.com, konrad.wilk@oracle.com, willy@infradead.org, lcapitulino@redhat.com, wei.w.wang@intel.com, aarcange@redhat.com, pbonzini@redhat.com, dan.j.williams@intel.com, alexander.h.duyck@linux.intel.com Date: Thu, 01 Aug 2019 15:38:29 -0700 Message-ID: <20190801223829.22190.36831.stgit@localhost.localdomain> In-Reply-To: <20190801222158.22190.96964.stgit@localhost.localdomain> References: <20190801222158.22190.96964.stgit@localhost.localdomain> User-Agent: StGit/0.17.1-dirty MIME-Version: 1.0 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP From: Alexander Duyck Add support for the page reporting feature provided by virtio-balloon. Reporting differs from the regular balloon functionality in that is is much less durable than a standard memory balloon. Instead of creating a list of pages that cannot be accessed the pages are only inaccessible while they are being indicated to the virtio interface. Once the interface has acknowledged them they are placed back into their respective free lists and are once again accessible by the guest system. Signed-off-by: Alexander Duyck --- drivers/virtio/Kconfig | 1 + drivers/virtio/virtio_balloon.c | 56 +++++++++++++++++++++++++++++++++++ include/uapi/linux/virtio_balloon.h | 1 + 3 files changed, 58 insertions(+) diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig index 078615cf2afc..4b2dd8259ff5 100644 --- a/drivers/virtio/Kconfig +++ b/drivers/virtio/Kconfig @@ -58,6 +58,7 @@ config VIRTIO_BALLOON tristate "Virtio balloon driver" depends on VIRTIO select MEMORY_BALLOON + select PAGE_REPORTING ---help--- This driver supports increasing and decreasing the amount of memory within a KVM guest. diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c index 2c19457ab573..971fe924e34f 100644 --- a/drivers/virtio/virtio_balloon.c +++ b/drivers/virtio/virtio_balloon.c @@ -19,6 +19,7 @@ #include #include #include +#include /* * Balloon device works in 4K page units. So each page is pointed to by @@ -37,6 +38,9 @@ #define VIRTIO_BALLOON_FREE_PAGE_SIZE \ (1 << (VIRTIO_BALLOON_FREE_PAGE_ORDER + PAGE_SHIFT)) +/* limit on the number of pages that can be on the reporting vq */ +#define VIRTIO_BALLOON_VRING_HINTS_MAX 16 + #ifdef CONFIG_BALLOON_COMPACTION static struct vfsmount *balloon_mnt; #endif @@ -46,6 +50,7 @@ enum virtio_balloon_vq { VIRTIO_BALLOON_VQ_DEFLATE, VIRTIO_BALLOON_VQ_STATS, VIRTIO_BALLOON_VQ_FREE_PAGE, + VIRTIO_BALLOON_VQ_REPORTING, VIRTIO_BALLOON_VQ_MAX }; @@ -113,6 +118,10 @@ struct virtio_balloon { /* To register a shrinker to shrink memory upon memory pressure */ struct shrinker shrinker; + + /* Unused page reporting device */ + struct virtqueue *reporting_vq; + struct page_reporting_dev_info ph_dev_info; }; static struct virtio_device_id id_table[] = { @@ -152,6 +161,23 @@ static void tell_host(struct virtio_balloon *vb, struct virtqueue *vq) } +void virtballoon_unused_page_report(struct page_reporting_dev_info *ph_dev_info, + unsigned int nents) +{ + struct virtio_balloon *vb = + container_of(ph_dev_info, struct virtio_balloon, ph_dev_info); + struct virtqueue *vq = vb->reporting_vq; + unsigned int unused; + + /* We should always be able to add these buffers to an empty queue. */ + virtqueue_add_inbuf(vq, ph_dev_info->sg, nents, vb, + GFP_NOWAIT | __GFP_NOWARN); + virtqueue_kick(vq); + + /* When host has read buffer, this completes via balloon_ack */ + wait_event(vb->acked, virtqueue_get_buf(vq, &unused)); +} + static void set_page_pfns(struct virtio_balloon *vb, __virtio32 pfns[], struct page *page) { @@ -476,6 +502,7 @@ static int init_vqs(struct virtio_balloon *vb) names[VIRTIO_BALLOON_VQ_DEFLATE] = "deflate"; names[VIRTIO_BALLOON_VQ_STATS] = NULL; names[VIRTIO_BALLOON_VQ_FREE_PAGE] = NULL; + names[VIRTIO_BALLOON_VQ_REPORTING] = NULL; if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) { names[VIRTIO_BALLOON_VQ_STATS] = "stats"; @@ -487,11 +514,19 @@ static int init_vqs(struct virtio_balloon *vb) callbacks[VIRTIO_BALLOON_VQ_FREE_PAGE] = NULL; } + if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) { + names[VIRTIO_BALLOON_VQ_REPORTING] = "reporting_vq"; + callbacks[VIRTIO_BALLOON_VQ_REPORTING] = balloon_ack; + } + err = vb->vdev->config->find_vqs(vb->vdev, VIRTIO_BALLOON_VQ_MAX, vqs, callbacks, names, NULL, NULL); if (err) return err; + if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) + vb->reporting_vq = vqs[VIRTIO_BALLOON_VQ_REPORTING]; + vb->inflate_vq = vqs[VIRTIO_BALLOON_VQ_INFLATE]; vb->deflate_vq = vqs[VIRTIO_BALLOON_VQ_DEFLATE]; if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) { @@ -931,12 +966,30 @@ static int virtballoon_probe(struct virtio_device *vdev) if (err) goto out_del_balloon_wq; } + + vb->ph_dev_info.report = virtballoon_unused_page_report; + if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) { + unsigned int capacity; + + capacity = min_t(unsigned int, + virtqueue_get_vring_size(vb->reporting_vq), + VIRTIO_BALLOON_VRING_HINTS_MAX); + vb->ph_dev_info.capacity = capacity; + + err = page_reporting_startup(&vb->ph_dev_info); + if (err) + goto out_unregister_shrinker; + } + virtio_device_ready(vdev); if (towards_target(vb)) virtballoon_changed(vdev); return 0; +out_unregister_shrinker: + if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM)) + virtio_balloon_unregister_shrinker(vb); out_del_balloon_wq: if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_FREE_PAGE_HINT)) destroy_workqueue(vb->balloon_wq); @@ -965,6 +1018,8 @@ static void virtballoon_remove(struct virtio_device *vdev) { struct virtio_balloon *vb = vdev->priv; + if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_REPORTING)) + page_reporting_shutdown(&vb->ph_dev_info); if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_DEFLATE_ON_OOM)) virtio_balloon_unregister_shrinker(vb); spin_lock_irq(&vb->stop_update_lock); @@ -1034,6 +1089,7 @@ static int virtballoon_validate(struct virtio_device *vdev) VIRTIO_BALLOON_F_DEFLATE_ON_OOM, VIRTIO_BALLOON_F_FREE_PAGE_HINT, VIRTIO_BALLOON_F_PAGE_POISON, + VIRTIO_BALLOON_F_REPORTING, }; static struct virtio_driver virtio_balloon_driver = { diff --git a/include/uapi/linux/virtio_balloon.h b/include/uapi/linux/virtio_balloon.h index a1966cd7b677..19974392d324 100644 --- a/include/uapi/linux/virtio_balloon.h +++ b/include/uapi/linux/virtio_balloon.h @@ -36,6 +36,7 @@ #define VIRTIO_BALLOON_F_DEFLATE_ON_OOM 2 /* Deflate balloon on OOM */ #define VIRTIO_BALLOON_F_FREE_PAGE_HINT 3 /* VQ to report free pages */ #define VIRTIO_BALLOON_F_PAGE_POISON 4 /* Guest is using page poisoning */ +#define VIRTIO_BALLOON_F_REPORTING 5 /* Page reporting virtqueue */ /* Size of a PFN in the balloon interface. */ #define VIRTIO_BALLOON_PFN_SHIFT 12