From patchwork Wed Jul 24 16:56:16 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Duyck X-Patchwork-Id: 11057239 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 6FC8E746 for ; Wed, 24 Jul 2019 16:58:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 58FE52883D for ; Wed, 24 Jul 2019 16:58:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 4B37D28876; Wed, 24 Jul 2019 16:58:29 +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 826A72883D for ; Wed, 24 Jul 2019 16:58:28 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 61A7C8E0002; Wed, 24 Jul 2019 12:58:27 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 5CCA56B000E; Wed, 24 Jul 2019 12:58:27 -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 4E03A8E0002; Wed, 24 Jul 2019 12:58:27 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-io1-f71.google.com (mail-io1-f71.google.com [209.85.166.71]) by kanga.kvack.org (Postfix) with ESMTP id 2EEE26B000D for ; Wed, 24 Jul 2019 12:58:27 -0400 (EDT) Received: by mail-io1-f71.google.com with SMTP id s9so51678158iob.11 for ; Wed, 24 Jul 2019 09:58:27 -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=wYHYz2CAMv2i0hd4mcN2CYyNrF3b9BU8TFbZbdGIzOs=; b=q1m6I7IDlNqO+eS6fvZ33aj28PAI0Qw4mnDwMYKlXedTaDQuWUOXAz3FirKW5Y5/BK Popt4rqef63/zjI6UgscyY+uM/JfX95aumwvMOIQfp1Zn0Yr+HoQeVTcCf++MoVHw97X 6AwDDbifm0r+Lx3XCGfuAMk+l7WFR3zi59erf742vkXr9MIu2G7MZMnrhudRfrKcZw6I 3vKaDy54PJQBvjZTgRRuP37fb4f81jz2UNqOdfii7UT0iYTaoQyZzUKXLhGwSpk/LMwm S+cVuSfVsTniokjUWrBmIkZwrczfvvacRtVcWG0dfPGI3Aq/izymS3Y/UthYsE84Eln5 WVTQ== X-Gm-Message-State: APjAAAWyJDVf2PbT8MTya/IFIkqmhnZOAr9Cyuj0IfnRZzLzCdbliNEh mu/z20zq4BEV06vv5cUdH/uywfdmsn5laN3P+kjcsDnH47DNEEuHVn3BN/0PNw1o3bEsVaNeQqy w5UwhPSWFlGHWGJ4nMHbPLQIsamNa9V7T35LKiWghPrnYaw18+ftWUeDWWGCbmYZJ8w== X-Received: by 2002:a5d:8794:: with SMTP id f20mr8851892ion.128.1563987506887; Wed, 24 Jul 2019 09:58:26 -0700 (PDT) X-Received: by 2002:a5d:8794:: with SMTP id f20mr8851828ion.128.1563987505941; Wed, 24 Jul 2019 09:58:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563987505; cv=none; d=google.com; s=arc-20160816; b=0nIQ09oGUCzfAE7kvpDzpIaKFoeYagK+hUjx1S0GDfzfJtoE0Nfxz3wLj5Uq3JIKd0 oo2G6yN/OuFSshS04NWTWe5OZsCNA4mTCiQVTQ208DYCESdcY0f1m2yC5b8fJHo+QkN9 IdCQpxt6nKKY7r1TreSuxEbbOs398tyvwzqZVBp+J+3GNLNGidhLxSla8FnsDKXeFNxz wxJcntMllFZNRH3571kRHmrqmHn4J+xqgoa9UmM0fBh3XJmSmkp6sBR4JH/gO4UTye2i 7c0vVNISuOm/KGde4JzRxZrW8+Tdyk8yE0la6KwLplQmHDHaysC+kEtQY9vtAq5UEkcU X9JQ== 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=wYHYz2CAMv2i0hd4mcN2CYyNrF3b9BU8TFbZbdGIzOs=; b=lwasMSF/cmTqRGILBBcfbaur5CDCETxL1i/WZO/G5ex6CUXYTHj+QXlKXcw2Zs/r8w VEh3MSXsC1voXKl5bLfQlHw2Rp/Gw5m0YI7qLOBnNjxszgPe2/gO9IKUTstuzV5y4lbQ N+WKNs7OdbqKnSuxrZedA3GRIWin12fhZzda/H+/Hnrrzhf0gH+tbGtHRPQJhss6eguG YfkiqKe6FFk1QRDeTZUuP4dHgLZy1zQFkCKEqIXRoep0IExuQXyl4QT2hijzBphsD3rA bCz7vAxh8KMcPag7pAJmuirq7h1qc2LmM1ZX+if2wGAoLS8Nyt+k1rTMiJAasbq1eKci NUnw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=uY1ehjWR; 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 k7sor32441392iol.66.2019.07.24.09.58.25 for (Google Transport Security); Wed, 24 Jul 2019 09:58: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=uY1ehjWR; 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=wYHYz2CAMv2i0hd4mcN2CYyNrF3b9BU8TFbZbdGIzOs=; b=uY1ehjWR6BNqfQDttxnJ2nwMmBN8h3DMwSIIAOAYu7j+1U9hWxIKRCWsDilAvtulS7 jqHJXC90O93P97yw9YGdgSrzEUbp/ZLwkCT8Yc0LOVtg7XtoHzdwigcsAacCSXhT9E6V beqzvXL7IVwvTdXLU3GOtLQearf9QMNAH8y0xURecTQLtawiDMebQFFl5jX3uHtZ+F9c ikP//AaeOHroRE4+hUvKBROEkcq166E16z8wfjtJtzRhaXSVsUpd49066HnG3q72ZGj8 LdvAH/t8xfq00R+pV9kgkHyBHNwpH45MFLV/9Our+I1qQxPvHjK9Ty+dkBWr4Kwu2ZzO j4qw== X-Google-Smtp-Source: APXvYqzPsB0WoiSPD7TwB+7/Z6jVYA0qOPZHI3pVrvai/+z0Jj6tq9koz6Gcg9wg+Mxn8dfbkySShA== X-Received: by 2002:a6b:ef06:: with SMTP id k6mr4646761ioh.70.1563987505511; Wed, 24 Jul 2019 09:58:25 -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 q13sm42436508ioh.36.2019.07.24.09.58.23 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 24 Jul 2019 09:58:25 -0700 (PDT) Subject: [PATCH v2 1/5] 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, 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: Wed, 24 Jul 2019 09:56:16 -0700 Message-ID: <20190724165615.6685.24289.stgit@localhost.localdomain> In-Reply-To: <20190724165158.6685.87228.stgit@localhost.localdomain> References: <20190724165158.6685.87228.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 272c6de1bf4e..1c4644b6cdc3 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 Wed Jul 24 16:58:30 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Duyck X-Patchwork-Id: 11057245 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 942C11399 for ; Wed, 24 Jul 2019 17:00:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 83D0C285D4 for ; Wed, 24 Jul 2019 17:00:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 777A4286F1; Wed, 24 Jul 2019 17:00:48 +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 0769E285D4 for ; Wed, 24 Jul 2019 17:00:43 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 073C78E0003; Wed, 24 Jul 2019 13:00:42 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 0252E6B0010; Wed, 24 Jul 2019 13:00: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 E2CD88E0003; Wed, 24 Jul 2019 13:00:41 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-io1-f72.google.com (mail-io1-f72.google.com [209.85.166.72]) by kanga.kvack.org (Postfix) with ESMTP id C4B476B000E for ; Wed, 24 Jul 2019 13:00:41 -0400 (EDT) Received: by mail-io1-f72.google.com with SMTP id u84so51794535iod.1 for ; Wed, 24 Jul 2019 10:00: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=H5uePaAnfho3jP4O84y/TR/d85pMr1PHhHfJNr70LiM=; b=cZO3f5iZ2eEdqHK+vnLTYYDpT8CUxC6SAP0kyrqUyrzY6QlkHbbtaSR78UXTKdCpGM upY96ECHy2JNaLViC3utgqvQ59OAsimPkQbmcu36ZqyZ/6Khd8QScDRdLWmwr8Z58+UW NBGI4ArPvpLSj+ZsbIrcFwNXrFmNBTdQyXbcjjfaZr4nHPAXwQMfGX52D2VoHXIVV8oA 8ZH8PavfV3RR8oa5FIJxNPMJ4hJZQIJGVSmRyYXZPHya3easwbEBk3FkP7wriEWCnjnU BuTE9z6uaoKkmPsWVI4Z7aAOI/levv+1aRtnzbiVwecqzOckiknD7gWK8l+HaFzA4Gj1 +2fQ== X-Gm-Message-State: APjAAAWbmxElI8yzjnUgMYSYMmbNkBJWNn//OsjXiAanwL6Rsljpvk6+ xQ3LHViMYJILdoOIlmlYtKbKX559R7PI6zE3ZTnSa6k+IBqmcgL/o7OaCaS8gE7I8FEobEryk1O W0imVIjWLbnjqIYQFgFCyYBJFL50tCQsoPD8USEtkJTGcTVjnO8oSfFyetoaNKjE+9Q== X-Received: by 2002:a05:6602:220d:: with SMTP id n13mr79956330ion.104.1563987641539; Wed, 24 Jul 2019 10:00:41 -0700 (PDT) X-Received: by 2002:a05:6602:220d:: with SMTP id n13mr79956252ion.104.1563987640686; Wed, 24 Jul 2019 10:00:40 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563987640; cv=none; d=google.com; s=arc-20160816; b=I53wHwNYbYrcrEIWZg3gYLY4wEgOtT3Y+BwqiudJanSVzlxNduN2gegHwdhZdhTKwZ nvIJyRkaTctfx+vnzvag3Z5N4ia3bEIMXYeKpC7e4v7+fu5g9zyt4X1cub66OlnuFAAY WM4ZaQvtZgtoqpUr6icrx4qQtaY0M0A5fbggofyOBYEVU/9esNUOVWjYQbrWF4sw3P+5 91QwbYWfCknT4QCsXICLkpVSW9iyS3bjzP8zvgEQpmsaI3Yjn35C5A090YpGXfFzIPaP HQdbjftVLjZMPtivDfq6qgdoSNwysR6UmSXF3Ew9LN7vuLpKQG6v8Rw2f2B6ITKjzP1j gkiA== 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=H5uePaAnfho3jP4O84y/TR/d85pMr1PHhHfJNr70LiM=; b=OlCwbO6DMP8z42E+AIOYSGqpuoAPshESPAPaADx6pXdnacZWJUtahOgBVZ5/4I2+lt g8f/PJVht8XqkdJmTJ682irquSSM6Z1psyxJK/ccZK9wBEfl0Y5Vgi/u0jdfRnsfRiLD LO21MbK2ih845oW4Nmz0xcqPpMKGTCM0rlQjSoGv8kKySLu21p2ejql1XPgXXxy9Croh J2WZLH9yBQ2sNjur8BVhrx8kVh49+zAs02KZbq0pBJPjGFdyNMw0t60pXBbTgYSTcBNz EMViqLlo8SpBeu0frjgCHulZbehDikcKl5zh2K1D6rRGNFbARIQQRe/IAy9yrg7dqj6W sxjw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=kHN1iP7z; 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 v12sor32460705iop.32.2019.07.24.10.00.40 for (Google Transport Security); Wed, 24 Jul 2019 10:00: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=kHN1iP7z; 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=H5uePaAnfho3jP4O84y/TR/d85pMr1PHhHfJNr70LiM=; b=kHN1iP7zA3FJ74YxjzC8lHyl0JgV6YH25zYfaGW5aYOffgySOf1QKp+KZSQHNEMZFn GKh2kTELEzLGxrmnDZLQUmt0ykjoIDF3onJTscviyJA5ehSAC5J/5UW+6OaSP+iS5bu/ 1xU8vU3Z2VYVFmXfnmjeHFfNhPttjVDYToOKq1rg678rqHxcm0f7g0tiQhJpl/xRi+Wd hxAt6HjXkfC9jvuYIpfnwcVzdP5pFdL9Cf7F6GyLICGbk9RmPEUy6b4Qcp04eardBWx/ jknqRgy69k+O7wjc/qY01mpJ20yzKRb5wPGzFCtmmEbEQ6QD7AfuECV7TisOSvi+bo3l Y7Yw== X-Google-Smtp-Source: APXvYqx+6Zi+RypFO68n1WeSfP+QuoinYaHM4/PZrw3z9TvZURJv9ULbWB+7BvwlnKgD1QoYlHaN3g== X-Received: by 2002:a02:6d24:: with SMTP id m36mr87555505jac.87.1563987640301; Wed, 24 Jul 2019 10:00:40 -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 b8sm38161917ioj.16.2019.07.24.10.00.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 24 Jul 2019 10:00:39 -0700 (PDT) Subject: [PATCH v2 2/5] 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, 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: Wed, 24 Jul 2019 09:58:30 -0700 Message-ID: <20190724165830.6685.51110.stgit@localhost.localdomain> In-Reply-To: <20190724165158.6685.87228.stgit@localhost.localdomain> References: <20190724165158.6685.87228.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 e32390802fd3..e432c7d5940d 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 1c4644b6cdc3..3d612a6b1771 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 Wed Jul 24 17:00:45 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Duyck X-Patchwork-Id: 11057249 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 6C5341399 for ; Wed, 24 Jul 2019 17:02:58 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 54B672883D for ; Wed, 24 Jul 2019 17:02:58 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 490F4288E4; Wed, 24 Jul 2019 17:02: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 695202883D for ; Wed, 24 Jul 2019 17:02:57 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8FD106B0010; Wed, 24 Jul 2019 13:02:56 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 887ED8E0006; Wed, 24 Jul 2019 13:02: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 74EED8E0005; Wed, 24 Jul 2019 13:02:56 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-io1-f70.google.com (mail-io1-f70.google.com [209.85.166.70]) by kanga.kvack.org (Postfix) with ESMTP id 53C796B0010 for ; Wed, 24 Jul 2019 13:02:56 -0400 (EDT) Received: by mail-io1-f70.google.com with SMTP id c5so51332864iom.18 for ; Wed, 24 Jul 2019 10:02: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=VeZ5bajO/AKYwVnzW540b0VQt3bRLs5FygnYd5RGmx0=; b=td0VM2NgC8WnhjgHeTp2Hx3zfklI0nJXOWJ45RafOLE6TUriAv/Li5C2ZehlaCUZxg XOKgCfanf8IzOJy/1/RdXuwAB0WmH02GeSOIE0h8BgGXceZgkby6ikvuIyBIPP4qqIr6 /sXKeBzmLP1r8GUkkz2TFqshtf+aPSaSP5NxeBIIIIk+jontzuvJB5guDmG64bQYSc30 34Hebv6WBrGqRPLphzJQH2/SgpbI0711D6ZL5pYX9uVak2M/pv5+8OQkvoMm/Kws3H2w GK1l1IGsbV7cNXA85fxZaxgRtyK4fiFfGvWYjNPAX+auESfzg590yC78Hs3KILyVx4tE 9Ing== X-Gm-Message-State: APjAAAXve5rvPVTqowTorFkv4GC6Xp1rz2qN/ZArwa2ov/K3sjuKtONr qzDInnXgapnw6jSL6RTVarmjcTaZKj7owC0HaQ66nKe1lIMfNfrHFYsPfKf9blpCgpmqj3oYT03 KYCzzu1e6Myq+XT5GEcCpIRoFajIwMyXrZGFwAF66i+Wu8JIcuIBKK9i9014Cm/i7VQ== X-Received: by 2002:a6b:3102:: with SMTP id j2mr11549847ioa.5.1563987776110; Wed, 24 Jul 2019 10:02:56 -0700 (PDT) X-Received: by 2002:a6b:3102:: with SMTP id j2mr11549784ioa.5.1563987775265; Wed, 24 Jul 2019 10:02:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563987775; cv=none; d=google.com; s=arc-20160816; b=ZI56DA7+ds9vshquUJDTiRLH1Y9MeIM6ve2ZjAeT0+xWFiWU5Z3FjwzlK85XsLvGSE q4vNJG1h673moMHx4oJLNeYjM4AiOOk2WNCXp+P83lJ1B80d8A0XcpgpRjQj6ivVQLZ+ wyQnmDQ+Cas7RJ+YLUcKSkNLoZ423XC1WPD0MrqxD2zhq6woatNhmUBrfwgJehVAbPvO YXilomaxbkUTOlYXD+X9kpQCt5D4Uz6HLdAj51J208yV0BP5j6DBRsWHnxn2gc0RPjcm R6umL48bV5ijOWmgyIxfGBNbp46PU1bywjINtdXkR9egem43LLhtKlnmQk7RvzsSH06D /3YA== 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=VeZ5bajO/AKYwVnzW540b0VQt3bRLs5FygnYd5RGmx0=; b=xcY25SDsYU0w+rPtN8ycLlCp5YFmxV2e2H4vZw6/fqpmVxVDpaV5wfz9bMO7DoOBu6 /K2kDxtu/PXv9fAY8WdAd6Yv/4WybaixwEoBs6Df/UZ2Ui75zvY0euZr5B0KuL64IkiF uhWc1XiI9HNR17HQPO0xHkK/R2W/SMkeHgw4OwjVBm5vQ2lj404a5dLEIPQiQhwi3VFi bZJ+89tR2ZNCK9xVkDhR1aOpARF1uIIzDE/tZV552h7RjUlnbNG6Gl+nJ0aNPBg5x1hk /G/2l/5CRqwlldwk7bDpXgFsAWEWYXJvenLCtAKFFQc96M6PqnvxNMqHmewYZMN1ibHW HMAw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=Q7Qtd8QK; 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 p2sor32576125ioj.63.2019.07.24.10.02.55 for (Google Transport Security); Wed, 24 Jul 2019 10:02:55 -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=Q7Qtd8QK; 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=VeZ5bajO/AKYwVnzW540b0VQt3bRLs5FygnYd5RGmx0=; b=Q7Qtd8QKtnQdZUVvZ/h7hYIDRDYc3G//tbW32a2/VqmgvDBnyqybVP+kflA3hRSvMQ qMEw2TJzxUlXXk9ItRIswieNAk1Otro2G3nuDAagh7pkcG/hxQv+eHjKUhC7U3KEyfyy qeeMGt9EPrLiZpuCI2y3FLRlWFd5OpGbbgxcvimfPCSGwfPXxQ74ACG/uEYfYYNX94qx mBksrK2OJ0gwRCy2ZIyvrdRk7n/HU0cc4ybPG08fiBjbNPQE1oWe2r6r9MW5crP+Jtqw gCjnLwdLFoG+QsfdOGxcmjORadYzhnTRlv4dCS+mgqzGQjRw8D66NqeEKdLy/pkq56kp ICaQ== X-Google-Smtp-Source: APXvYqwcgXA0Ie0L+IDNnlc04ZvDr22nrMcaKVYfkLIQngt2mRFE3aSw5bYGcnM2PggW1eMnB1Rm+g== X-Received: by 2002:a02:13c3:: with SMTP id 186mr84555363jaz.30.1563987774830; Wed, 24 Jul 2019 10:02: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 b14sm50259209iod.33.2019.07.24.10.02.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 24 Jul 2019 10:02:54 -0700 (PDT) Subject: [PATCH v2 3/5] 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, 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: Wed, 24 Jul 2019 10:00:45 -0700 Message-ID: <20190724170045.6685.92452.stgit@localhost.localdomain> In-Reply-To: <20190724165158.6685.87228.stgit@localhost.localdomain> References: <20190724165158.6685.87228.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 3d612a6b1771..9a73f69b37af 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 Wed Jul 24 17:03:00 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Duyck X-Patchwork-Id: 11057253 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 88C2013A0 for ; Wed, 24 Jul 2019 17:05:15 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 6999C2883D for ; Wed, 24 Jul 2019 17:05:15 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 57961288DA; Wed, 24 Jul 2019 17:05:15 +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 774342883D for ; Wed, 24 Jul 2019 17:05:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8114D6B0266; Wed, 24 Jul 2019 13:05:12 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 7C22E8E0006; Wed, 24 Jul 2019 13:05:12 -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 68A1E8E0005; Wed, 24 Jul 2019 13:05:12 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-io1-f71.google.com (mail-io1-f71.google.com [209.85.166.71]) by kanga.kvack.org (Postfix) with ESMTP id 41DE96B0266 for ; Wed, 24 Jul 2019 13:05:12 -0400 (EDT) Received: by mail-io1-f71.google.com with SMTP id k21so51708975ioj.3 for ; Wed, 24 Jul 2019 10:05:12 -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=EdB1fvJ2UIfN9MK80DlB9EXRkbzZe2alnUINvp0mAD4=; b=rkRzhlFv20lvEt/xlGS3xz2BM4ch28djCDi6zMg97R1PKeFF1OGBaxTlywgail0EZJ XZT2Hz1VYQJup+0F4YpgxB+73i68hQd9mrgUErvvQoeq21gn3Ae84CYFMTxa5WJ8q8Qq SIWTJ8nkp4VwBb6FMeLIipX7OpMdg1RyW69dfcC7QlaS2QREKd1NKlAvovmMTADsSAdd /+9sW7+z9Zs54cNBuAyGJ0qdZlbByeQbIluC5E+2t8WI+tV194jzYxWCaQIcq/9iWDrJ JgTUqWoDIInahtHeeRLJ3uPKGvXUO7G5iJoAM20wYetiSKkpQ8RrYJMv09wgoYkiKQ1e dJlw== X-Gm-Message-State: APjAAAWJSGL6WfNPES5Bs7uMxSlLkEjbT8WHPonIM8/VDvBjiv074qi4 JGgBB4GiNxpJOTXN/XsNhkCZZ7RC6Ul3FgbbxZn+6VrZpIF+zOGm+4gUzyVhyskVXhZst3IXSWx yP+zn/TbDpZ626XbCqOWvCJVVyG9taRO9L1eoqeHxt0yBOjLg5zAO83ds1O8Vo5mflA== X-Received: by 2002:a02:ce52:: with SMTP id y18mr83042913jar.78.1563987911942; Wed, 24 Jul 2019 10:05:11 -0700 (PDT) X-Received: by 2002:a02:ce52:: with SMTP id y18mr83042773jar.78.1563987910078; Wed, 24 Jul 2019 10:05:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563987910; cv=none; d=google.com; s=arc-20160816; b=iWZpwpHFTJw5lDro2BpnAeZ//6OLDHHr19lAnfqQnb7BWUKBfYQW3OLhDK74wUManh 1hnuqvNwT/F1BoMWuLb3BCXvGw21h2+hJVzcgVHm0h4Jrh0YxLdAFuMnEc1liickcI0p 6zyBaIRt0Ar469H7CeUPApOe0IuDbWY4K7Iqr/Mqs3MltzfXjGgJI9S9J38YdFy2KGtq pkEm9hvrHRqp+NLSxBr6Qlyf9QD2SlsqTXrs5l0O7lGd5us3n/5+rBBUVgJz+H0WqCc6 mufB4YopnP3vRf/suBih0sXfcqJ8namRO6ZQ8sIPPOpOvstdraMOML++Cv7xE2du8H08 DS3A== 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=EdB1fvJ2UIfN9MK80DlB9EXRkbzZe2alnUINvp0mAD4=; b=CZRRrS71tdcx3EPg/XuqNxHY2stF0+8K2MK/iwr99zSU2ogDuuwGEN0CgmDZJkJ+EN pEjuuqurfG0whEw5UyLeYmvB1K6bEP6EHuR9J9h1/9VArjzLbq1mfoQc91WEBL3eaTxs /J0XkLIfB39I196mJN5G2fOCBKGA8tSd8jy0VdkPV+HIx1wtkecBmhHUFMvbw96Tivid YMImFLAv+8ivNXQC5qzWcacdIL9nEqdgtO8T/YsKEDlznoRwIS0biLrisZzTUKLFzIYN iOJVZUVy5duwHWUFuJ2hLOr6laGWIuFA1PWl+Ldi3IMjqmlMJMRAm6bcKMRZfKBuRebA NOpw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=SRMU+1pQ; 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 f126sor110500313jaf.5.2019.07.24.10.05.09 for (Google Transport Security); Wed, 24 Jul 2019 10:05: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=SRMU+1pQ; 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=EdB1fvJ2UIfN9MK80DlB9EXRkbzZe2alnUINvp0mAD4=; b=SRMU+1pQKt24pMqH+pa3gHU8eL364E9EFYTcOdKATOeAAcmWmzZSq2WKXhUok6vUgj i8YbwHjJD5zBrRFIMg7N/a/G5O7mPy05sFtvlfyU3eu2TkToWYPKW1L1osbcW9B8tx+X /pW2BzPVEIiYKwtOIsr6D8LOgV0FA8FRE4S3w7vyxgBomroBhwO9VcA/6yZ9FAntUwyJ L4QtEwufdlYqzAEfnAnNUOM1JB9T/gswYpErwhaCoMCKZg3IpiJ6aKTDBtCG1AafX1Im Gt2u8M1GmJntlSACAS9X2XZ0jgDiX15XfkgrBWaAGXvWgxFmuqlL7w7yd7oo5az6BRc6 DQwA== X-Google-Smtp-Source: APXvYqw4EWZ44teDYe7DqvveZv2Vd8v20RraEqD8LVP27seh3Q8jdSy0lppE0eMopwWz3JvOMmIG5g== X-Received: by 2002:a02:b68f:: with SMTP id i15mr56624615jam.107.1563987909410; Wed, 24 Jul 2019 10:05: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 q13sm42456451ioh.36.2019.07.24.10.05.07 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 24 Jul 2019 10:05:08 -0700 (PDT) Subject: [PATCH v2 4/5] mm: Introduce Hinted 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, 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: Wed, 24 Jul 2019 10:03:00 -0700 Message-ID: <20190724170259.6685.18028.stgit@localhost.localdomain> In-Reply-To: <20190724165158.6685.87228.stgit@localhost.localdomain> References: <20190724165158.6685.87228.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 hinting 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 Hinted Buddy, which is essentially meant to just be the Offline page type 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 unhinted and hinted 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 hinting 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 hinted 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 hints 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 hinted pages that were likely evicted from the guest memory. Since we will only be hinting one zone at a time we keep the boundary limited to being defined for just the zone we are currently placing hinted pages into. 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 hinting and the boundaries are active. The determination of when to start hinting is based on the tracking of the number of free pages in a given area versus the number of hinted pages in that area. We keep track of the number of hinted 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 | 8 + include/linux/page_hinting.h | 139 ++++++++++++++++++++ mm/Kconfig | 5 + mm/Makefile | 1 mm/memory_hotplug.c | 1 mm/page_alloc.c | 136 ++++++++++++++++++- mm/page_hinting.c | 298 ++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 620 insertions(+), 8 deletions(-) create mode 100644 include/linux/page_hinting.h create mode 100644 mm/page_hinting.c diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index f0c68b6b6154..42bdebb20484 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_HINTING + /* + * Pointer to hinted page tracking statistics array. The size of + * the array is MAX_ORDER - PAGE_HINTING_MIN_ORDER. NULL when + * page hinting is not present. + */ + unsigned long *hinted_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_HINTING_REQUESTED, /* zone enabled page hinting and has + * requested flushing the data out of + * higher order pages. + */ + ZONE_PAGE_HINTING_ACTIVE, /* zone enabled page hinting 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_unhinted_tail(zone, order, migratetype); - list_add_tail(&page->lru, &area->free_list[migratetype]); - area->nr_free++; + /* + * To prevent the unhinted pages from being interleaved with the + * hinted ones while we are actively processing pages we will use + * the head of the hinted 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 hinted pages + * at the top of the free_list. It is cheaper to just process this + * page again, then have to walk around a page that is already hinted. + */ + clear_page_hinted(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 Hinted flag, if present, before clearing the Buddy flag */ + clear_page_hinted(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 b848517da64c..b753dbf673cb 100644 --- a/include/linux/page-flags.h +++ b/include/linux/page-flags.h @@ -745,6 +745,14 @@ static inline int page_has_type(struct page *page) PAGE_TYPE_OPS(Offline, offline) /* + * PageHinted() is an alias for Offline, however it is not meant to be an + * exclusive value. It should be combined with PageBuddy() when seen as it + * is meant to indicate that the page has been scrubbed while waiting in + * the buddy system. + */ +PAGE_TYPE_OPS(Hinted, offline) + +/* * If kmemcg is enabled, the buddy allocator will set PageKmemcg() on * pages allocated with __GFP_ACCOUNT. It gets cleared on page free. */ diff --git a/include/linux/page_hinting.h b/include/linux/page_hinting.h new file mode 100644 index 000000000000..526fb26663d9 --- /dev/null +++ b/include/linux/page_hinting.h @@ -0,0 +1,139 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_PAGE_HINTING_H +#define _LINUX_PAGE_HINTING_H + +#include +#include +#include +#include + +#define PAGE_HINTING_MIN_ORDER pageblock_order +#define PAGE_HINTING_HWM 32 + +#ifdef CONFIG_PAGE_HINTING +struct page_hinting_dev_info { + /* function that alters pages to make them "hinted" */ + void (*react)(struct page_hinting_dev_info *phdev, + unsigned int num_hints); + + /* 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 hints */ + struct delayed_work work; + + /* + * The number of zones requesting hinting, plus one additional if + * processing thread is active. + */ + atomic_t refcnt; +}; + +extern struct static_key page_hinting_notify_enabled; + +/* Boundary functions */ +struct list_head *__page_hinting_get_boundary(unsigned int order, + int migratetype); +void page_hinting_del_from_boundary(struct page *page, struct zone *zone); +void page_hinting_add_to_boundary(struct page *page, struct zone *zone, + int migratetype); + +/* Hinted page accessors, defined in page_alloc.c */ +struct page *get_unhinted_page(struct zone *zone, unsigned int order, + int migratetype); +void put_hinted_page(struct zone *zone, struct page *page); + +void __page_hinting_request(struct zone *zone); +void __page_hinting_free_stats(struct zone *zone); + +/* Tear-down and bring-up for page hinting devices */ +void page_hinting_shutdown(struct page_hinting_dev_info *phdev); +int page_hinting_startup(struct page_hinting_dev_info *phdev); +#endif /* CONFIG_PAGE_HINTING */ + +static inline struct list_head *get_unhinted_tail(struct zone *zone, + unsigned int order, + int migratetype) +{ +#ifdef CONFIG_PAGE_HINTING + if (order >= PAGE_HINTING_MIN_ORDER && + test_bit(ZONE_PAGE_HINTING_ACTIVE, &zone->flags)) + return __page_hinting_get_boundary(order, migratetype); +#endif + return &zone->free_area[order].free_list[migratetype]; +} + +static inline void clear_page_hinted(struct page *page, + struct zone *zone) +{ +#ifdef CONFIG_PAGE_HINTING + if (likely(!PageHinted(page))) + return; + + /* push boundary back if we removed the upper boundary */ + if (test_bit(ZONE_PAGE_HINTING_ACTIVE, &zone->flags)) + page_hinting_del_from_boundary(page, zone); + + __ClearPageHinted(page); + + /* page_private will contain the page order, so just use it directly */ + zone->hinted_pages[page_private(page) - PAGE_HINTING_MIN_ORDER]--; +#endif +} + +/* Free hinted_pages and reset hinted page tracking count to 0 */ +static inline void page_hinting_reset(struct zone *zone) +{ +#ifdef CONFIG_PAGE_HINTING + if (zone->hinted_pages) + __page_hinting_free_stats(zone); +#endif +} + +/** + * page_hinting_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_hinting_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_hinting_notify_free(struct zone *zone, int order) +{ +#ifdef CONFIG_PAGE_HINTING + unsigned long nr_hinted; + + /* Called from hot path in __free_one_page() */ + if (!static_key_false(&page_hinting_notify_enabled)) + return; + + /* Limit notifications only to higher order pages */ + if (order < PAGE_HINTING_MIN_ORDER) + return; + + /* Do not bother with tests if we have already requested hinting */ + if (test_bit(ZONE_PAGE_HINTING_REQUESTED, &zone->flags)) + return; + + /* If hinted_pages is not populated, assume 0 */ + nr_hinted = zone->hinted_pages ? + zone->hinted_pages[order - PAGE_HINTING_MIN_ORDER] : 0; + + /* Only request it if we have enough to begin the page hinting */ + if (zone->free_area[order].nr_free < nr_hinted + PAGE_HINTING_HWM) + return; + + /* This is slow, but should be called very rarely */ + __page_hinting_request(zone); +#endif +} +#endif /*_LINUX_PAGE_HINTING_H */ diff --git a/mm/Kconfig b/mm/Kconfig index 56cec636a1fc..38354668f849 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -237,6 +237,11 @@ config COMPACTION linux-mm@kvack.org. # +# support for free page hinting +config PAGE_HINTING + bool + +# # support for page migration # config MIGRATION diff --git a/mm/Makefile b/mm/Makefile index 338e528ad436..7277ced923ab 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -104,3 +104,4 @@ obj-$(CONFIG_HARDENED_USERCOPY) += usercopy.o obj-$(CONFIG_PERCPU_STATS) += percpu-stats.o obj-$(CONFIG_HMM_MIRROR) += hmm.o obj-$(CONFIG_MEMFD_CREATE) += memfd.o +obj-$(CONFIG_PAGE_HINTING) += page_hinting.o diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index 2a9bbddb0e55..7c2d49b1a918 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -1622,6 +1622,7 @@ static int __ref __offline_pages(unsigned long start_pfn, if (!populated_zone(zone)) { zone_pcp_reset(zone); build_all_zonelists(NULL); + page_hinting_reset(zone); } else zone_pcp_update(zone); diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 9a73f69b37af..c83cb4a30aff 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 hinted) { 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 (hinted || + (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 hinted page as the total count of + * unhinted pages will not have increased since we have essentially + * merged the hinted page with one or more unhinted pages. + */ + if (!hinted) + page_hinting_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_HINTING +/** + * get_unhinted_page - Pull an unhinted 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 + * hinted on it will instead pull the head if that is unhinted. + * + * 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_unhinted_page(struct zone *zone, unsigned int order, int mt) +{ + struct list_head *tail = get_unhinted_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 hinted try the head of the list */ + if (PageHinted(page)) { + page = list_first_entry(list, struct page, lru); + + /* + * If both the head and tail are hinted then reset + * the boundary so that we read as an empty list + * next time and bail out. + */ + if (PageHinted(page)) { + page_hinting_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_hinted_page - Return a now-hinted page back where we got it + * @zone: Zone to return pages to + * @page: Page that was hinted + * + * 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 + * hinted. + */ +void put_hinted_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 "hinted" since part of the page hasn't been. + * In this case we will simply exit and not update the "hinted" + * state. Instead just treat the result as a unhinted page. + */ + if (!PageBuddy(page) || page_order(page) != order) + return; + + /* update areated page accounting */ + zone->hinted_pages[order - PAGE_HINTING_MIN_ORDER]++; + + /* update boundary of new migratetype and record it */ + page_hinting_add_to_boundary(page, zone, mt); + + /* flag page as hinted */ + __SetPageHinted(page); +} +#endif /* CONFIG_PAGE_HINTING */ + /* * 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_hinting.c b/mm/page_hinting.c new file mode 100644 index 000000000000..d06d3762b315 --- /dev/null +++ b/mm/page_hinting.c @@ -0,0 +1,298 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include +#include +#include +#include +#include "internal.h" + +static struct page_hinting_dev_info __rcu *ph_dev_info __read_mostly; +struct static_key page_hinting_notify_enabled; + +struct list_head *boundary[MAX_ORDER - PAGE_HINTING_MIN_ORDER][MIGRATE_TYPES]; + +static void page_hinting_reset_boundary(struct zone *zone, unsigned int order, + unsigned int migratetype) +{ + boundary[order - PAGE_HINTING_MIN_ORDER][migratetype] = + &zone->free_area[order].free_list[migratetype]; +} + +#define for_each_hinting_migratetype_order(_order, _type) \ + for (_order = MAX_ORDER; _order-- != PAGE_HINTING_MIN_ORDER;) \ + for (_type = MIGRATE_TYPES; _type--;) + +static int page_hinting_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 hinted pages are in the zone. To do that + * we need to make certain zone->hinted_pages is populated. + */ + if (!zone->hinted_pages) { + zone->hinted_pages = kcalloc(MAX_ORDER - PAGE_HINTING_MIN_ORDER, + sizeof(unsigned long), + GFP_KERNEL); + if (!zone->hinted_pages) + return -ENOMEM; + } + + /* Update boundary data to reflect the zone we are currently working */ + for_each_hinting_migratetype_order(order, mt) + page_hinting_reset_boundary(zone, order, mt); + + return 0; +} + +struct list_head *__page_hinting_get_boundary(unsigned int order, + int migratetype) +{ + return boundary[order - PAGE_HINTING_MIN_ORDER][migratetype]; +} + +void page_hinting_del_from_boundary(struct page *page, struct zone *zone) +{ + unsigned int order = page_private(page) - PAGE_HINTING_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_hinting_add_to_boundary(struct page *page, struct zone *zone, + int migratetype) +{ + unsigned int order = page_private(page) - PAGE_HINTING_MIN_ORDER; + struct list_head **tail = &boundary[order][migratetype]; + + *tail = &page->lru; +} + +static unsigned int page_hinting_fill(struct zone *zone, + struct page_hinting_dev_info *phdev) +{ + struct scatterlist *sg = phdev->sg; + unsigned int order, mt, count = 0; + + sg_init_table(phdev->sg, phdev->capacity); + + for_each_hinting_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_unhinted_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_HINTING_REQUESTED, &zone->flags); + atomic_dec(&phdev->refcnt); + + return count; +} + +static void page_hinting_drain(struct zone *zone, + struct page_hinting_dev_info *phdev) +{ + struct scatterlist *sg = phdev->sg; + + /* + * Drain the now hinted pages back into their respective + * free lists/areas. We assume at least one page is populated. + */ + do { + put_hinted_page(zone, sg_page(sg)); + } while (!sg_is_last(sg++)); +} + +/* + * The page hinting cycle consists of 4 stages, fill, react, 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_hinting_cycle(struct zone *zone, + struct page_hinting_dev_info *phdev) +{ + /* + * Guarantee boundaries and stats are populated before we + * start placing hinted pages in the zone. + */ + if (page_hinting_populate_metadata(zone)) + return; + + spin_lock(&zone->lock); + + /* set bit indicating boundaries are present */ + set_bit(ZONE_PAGE_HINTING_ACTIVE, &zone->flags); + + do { + /* Pull pages out of allocator into a scaterlist */ + unsigned int num_hints = page_hinting_fill(zone, phdev); + + /* no pages were acquired, give up */ + if (!num_hints) + break; + + spin_unlock(&zone->lock); + + /* begin processing pages in local list */ + phdev->react(phdev, num_hints); + + spin_lock(&zone->lock); + + /* + * We should have a scatterlist of pages that have been + * processed. Return them to their original free lists. + */ + page_hinting_drain(zone, phdev); + + /* keep pulling pages till there are none to pull */ + } while (test_bit(ZONE_PAGE_HINTING_REQUESTED, &zone->flags)); + + /* processing of the zone is complete, we can disable boundaries */ + clear_bit(ZONE_PAGE_HINTING_ACTIVE, &zone->flags); + + spin_unlock(&zone->lock); +} + +static void page_hinting_process(struct work_struct *work) +{ + struct delayed_work *d_work = to_delayed_work(work); + struct page_hinting_dev_info *phdev = + container_of(d_work, struct page_hinting_dev_info, work); + struct zone *zone = first_online_pgdat()->node_zones; + + do { + if (test_bit(ZONE_PAGE_HINTING_REQUESTED, &zone->flags)) + page_hinting_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 hinting on this zone */ +void __page_hinting_request(struct zone *zone) +{ + struct page_hinting_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_HINTING_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_hinting_free_stats(struct zone *zone) +{ + /* free hinted_page statisitics */ + kfree(zone->hinted_pages); + zone->hinted_pages = NULL; +} + +void page_hinting_shutdown(struct page_hinting_dev_info *phdev) +{ + if (rcu_access_pointer(ph_dev_info) != phdev) + return; + + /* Disable page hinting notification */ + static_key_slow_dec(&page_hinting_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_hinting_shutdown); + +int page_hinting_startup(struct page_hinting_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 hinted 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_hinting_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_hinting_request(zone); + spin_unlock(&zone->lock); + } + + /* enable page hinting notification */ + static_key_slow_inc(&page_hinting_notify_enabled); + + return 0; +} +EXPORT_SYMBOL_GPL(page_hinting_startup); + From patchwork Wed Jul 24 17:05: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: 11057257 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 C349D1399 for ; Wed, 24 Jul 2019 17:07:27 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id A591F2882D for ; Wed, 24 Jul 2019 17:07:27 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 94D0B288DA; Wed, 24 Jul 2019 17:07: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=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 D5B162882D for ; Wed, 24 Jul 2019 17:07:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id CF0FE8E0006; Wed, 24 Jul 2019 13:07:25 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id CA2FD8E0005; Wed, 24 Jul 2019 13:07:25 -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 B91568E0006; Wed, 24 Jul 2019 13:07:25 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-io1-f72.google.com (mail-io1-f72.google.com [209.85.166.72]) by kanga.kvack.org (Postfix) with ESMTP id 987508E0005 for ; Wed, 24 Jul 2019 13:07:25 -0400 (EDT) Received: by mail-io1-f72.google.com with SMTP id r27so51694338iob.14 for ; Wed, 24 Jul 2019 10:07:25 -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=FMuX0Oym74xCaSWPGFhC4FdIsHBrnIwQJ+1P4e5e/MM=; b=uJRwJvHAIv5bAalhrNM3lGPshcQfteVVBUGZWtPfyGrohtTpZzRhQQXNNQzvcLXgE7 Je7NGQ/u6GZKOxff/HHzqNL5KSdlf7CDpdNBvjLASn0pOj7meOxAUZRcy64nPiDrfiQ5 uge/sZBj6AL2FeGVa+KR5PSKQlLakSijibSsZYbSrN+HhHy8GjRvDmz324YS4Y9zODX8 bXEAP1awplu7QnpRJOzmhDA2RyIZCMhATib5iJJVZ2Ilmd4+nNW5Ht86F84X3IHEo1MJ 1d4dUUZlBpX/yeuClVKD4hYcGLuak/cEAFSV1Pdj0do6kMAuFK6D3pHvypTAbEJiKIzV eANg== X-Gm-Message-State: APjAAAV03KHfQdVR33ucxfqjKrf416XAndQaGxQuz0+P9NJ7ZjEpMSeq xeq0BPhTaFwYwiNGul//cFJGA/+3UZKVUME1rHmgQGOZkbxUebi1ycggjv8HfkSvDTHQypb3/NJ 7Pp+49vjBRy1vQTP2HonoP2V1UGBtz5ew6LAgRKsBIdXZaacRBu7aNbkE9ooqZTzhvw== X-Received: by 2002:a6b:5a17:: with SMTP id o23mr72153667iob.41.1563988045394; Wed, 24 Jul 2019 10:07:25 -0700 (PDT) X-Received: by 2002:a6b:5a17:: with SMTP id o23mr72153588iob.41.1563988044517; Wed, 24 Jul 2019 10:07:24 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1563988044; cv=none; d=google.com; s=arc-20160816; b=eTbRHOwspSwI5cvYNnC6qDtrmwcA3WGmiOXjkstQp09IC1o29zzwpKrD2kpQm6FUh/ Gx1v6asa41mtMuTP+qgcj/lyOlydcWUAxqPWtevB4hWsKUjFhyKhwpebuZD2PtQ3cxOI tLouEavI44HvpdTJa9ZuemmSl0PtTg1BvFXrKntdyr77nIdDbHoTY4cmEzGoNf3JBd06 2z0wf68WGt/mzmlEbqnpLCDNBpMrAhsSCjoeIEOKbNwJMejPAbLSH5TI6CyBE3dFz+KE o6h5l2/ipugB+vIwRWIV3QssTZSUOXc2taqX3kay9IW6C5LeN52pimDyeoLNlsA/Th2V X4Hg== 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=FMuX0Oym74xCaSWPGFhC4FdIsHBrnIwQJ+1P4e5e/MM=; b=vF8j3DGoDSl0jstEKq4oImzr2apNQLxdi6hGimvommKaytVDEKQO/KnvAZvGdG77vR AbwKKsYRSTvEXaMdqYY9v01aPPCHYqjqhn/aET6MkWi3B+Q4RwFG5ISpCdkN5M4/Pq+j biTtqNHiOIR08ynSvlZPAnUn0+a3/fBzkJ3SyEAXGAHR8P2pA7Z7qkbSijHSWPLblnEa +RuD1J+ux3JUUhJZRMAHjHX/SQXE8H42rIGiqNLiRHUTeKqJO1pEq8hrFFPmByzfOgMH bBiciCAxIzkS23VqPOsmSFa8mptMCEVyA4xZjCnFtLQ0BME83i3EggCk13J21lQFeBCB HdyA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=SuKKhppA; 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 s10sor31203080iob.50.2019.07.24.10.07.24 for (Google Transport Security); Wed, 24 Jul 2019 10:07:24 -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=SuKKhppA; 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=FMuX0Oym74xCaSWPGFhC4FdIsHBrnIwQJ+1P4e5e/MM=; b=SuKKhppAVNoQv+RiNYwWSYYmFjbIehqdAVmKv+DBxaGoeM8dyFmZcVLjCbLD5C2NSn VRZ2l1ChubLZL943RF6i+FOGqJrzJYNowJK5sSMOxhdnKnIagkm6tNc5Iygp3EptMJOY 3OnbWNAOwHDsAQ16TEqtynLwPUHuA2mvFMh1x2W0xcL73pcrQ/0TFzWhurTyhPmczryv Y7xazqPs2k/EytYMzEOT99kKp07G2EuCNrG2fjCSlcKdzVgad9NoM+EsMjJmRoyxNUu8 sh3iuEjpV2gdVJ271uL7OhBXRtBsqy+tqPsEFF1vb5qgwpyKnZBBXYpvbseakHe2zJhD ozIg== X-Google-Smtp-Source: APXvYqyQVX8x9OWRNiHYpwDX440mJ3O8R16Hbv8NfiUZMuzD3UzsPyIWJX9LvTuqfqJZry1nQWTt7w== X-Received: by 2002:a02:1607:: with SMTP id a7mr84040654jaa.123.1563988044110; Wed, 24 Jul 2019 10:07: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 n2sm48246215ioa.27.2019.07.24.10.07.22 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 24 Jul 2019 10:07:23 -0700 (PDT) Subject: [PATCH v2 5/5] virtio-balloon: Add support for providing page hints 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, 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: Wed, 24 Jul 2019 10:05:14 -0700 Message-ID: <20190724170514.6685.17161.stgit@localhost.localdomain> In-Reply-To: <20190724165158.6685.87228.stgit@localhost.localdomain> References: <20190724165158.6685.87228.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 hinting feature provided by virtio-balloon. Hinting 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 | 47 +++++++++++++++++++++++++++++++++++ include/uapi/linux/virtio_balloon.h | 1 + 3 files changed, 49 insertions(+) diff --git a/drivers/virtio/Kconfig b/drivers/virtio/Kconfig index 078615cf2afc..d45556ae1f81 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_HINTING ---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 226fbb995fb0..dee9f8f3ad09 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 @@ -27,6 +28,7 @@ */ #define VIRTIO_BALLOON_PAGES_PER_PAGE (unsigned)(PAGE_SIZE >> VIRTIO_BALLOON_PFN_SHIFT) #define VIRTIO_BALLOON_ARRAY_PFNS_MAX 256 +#define VIRTIO_BALLOON_ARRAY_HINTS_MAX 32 #define VIRTBALLOON_OOM_NOTIFY_PRIORITY 80 #define VIRTIO_BALLOON_FREE_PAGE_ALLOC_FLAG (__GFP_NORETRY | __GFP_NOWARN | \ @@ -46,6 +48,7 @@ enum virtio_balloon_vq { VIRTIO_BALLOON_VQ_DEFLATE, VIRTIO_BALLOON_VQ_STATS, VIRTIO_BALLOON_VQ_FREE_PAGE, + VIRTIO_BALLOON_VQ_HINTING, VIRTIO_BALLOON_VQ_MAX }; @@ -113,6 +116,10 @@ struct virtio_balloon { /* To register a shrinker to shrink memory upon memory pressure */ struct shrinker shrinker; + + /* Unused page hinting device */ + struct virtqueue *hinting_vq; + struct page_hinting_dev_info ph_dev_info; }; static struct virtio_device_id id_table[] = { @@ -152,6 +159,22 @@ static void tell_host(struct virtio_balloon *vb, struct virtqueue *vq) } +void virtballoon_page_hinting_react(struct page_hinting_dev_info *ph_dev_info, + unsigned int num_hints) +{ + struct virtio_balloon *vb = + container_of(ph_dev_info, struct virtio_balloon, ph_dev_info); + struct virtqueue *vq = vb->hinting_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, num_hints, vb, GFP_KERNEL); + 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 +499,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_HINTING] = NULL; if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_STATS_VQ)) { names[VIRTIO_BALLOON_VQ_STATS] = "stats"; @@ -487,11 +511,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_HINTING)) { + names[VIRTIO_BALLOON_VQ_HINTING] = "hinting_vq"; + callbacks[VIRTIO_BALLOON_VQ_HINTING] = 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_HINTING)) + vb->hinting_vq = vqs[VIRTIO_BALLOON_VQ_HINTING]; + 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)) { @@ -924,12 +956,24 @@ static int virtballoon_probe(struct virtio_device *vdev) if (err) goto out_del_balloon_wq; } + + vb->ph_dev_info.react = virtballoon_page_hinting_react; + vb->ph_dev_info.capacity = VIRTIO_BALLOON_ARRAY_HINTS_MAX; + if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_HINTING)) { + err = page_hinting_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); @@ -958,6 +1002,8 @@ static void virtballoon_remove(struct virtio_device *vdev) { struct virtio_balloon *vb = vdev->priv; + if (virtio_has_feature(vb->vdev, VIRTIO_BALLOON_F_HINTING)) + page_hinting_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); @@ -1027,6 +1073,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_HINTING, }; 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..2b0f62814e22 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_HINTING 5 /* Page hinting virtqueue */ /* Size of a PFN in the balloon interface. */ #define VIRTIO_BALLOON_PFN_SHIFT 12