From patchwork Tue Nov 19 21:46:18 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Duyck X-Patchwork-Id: 11252703 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 798FB1390 for ; Tue, 19 Nov 2019 21:46:24 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 3A31C2240D for ; Tue, 19 Nov 2019 21:46:24 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="WgWSDZCj" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 3A31C2240D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 68B586B0006; Tue, 19 Nov 2019 16:46:23 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 6139B6B0007; Tue, 19 Nov 2019 16:46:23 -0500 (EST) 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 502F96B0008; Tue, 19 Nov 2019 16:46:23 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0206.hostedemail.com [216.40.44.206]) by kanga.kvack.org (Postfix) with ESMTP id 3AB836B0006 for ; Tue, 19 Nov 2019 16:46:23 -0500 (EST) Received: from smtpin26.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with SMTP id 06C51248D for ; Tue, 19 Nov 2019 21:46:23 +0000 (UTC) X-FDA: 76174361046.26.hot48_8efad728bb037 X-Spam-Summary: 2,0,0,044fdde857cd6ab0,d41d8cd98f00b204,alexander.duyck@gmail.com,:kvm@vger.kernel.org:mst@redhat.com:linux-kernel@vger.kernel.org:willy@infradead.org:mhocko@kernel.org::akpm@linux-foundation.org:mgorman@techsingularity.net:vbabka@suse.cz:yang.zhang.wz@gmail.com:nitesh@redhat.com:konrad.wilk@oracle.com:david@redhat.com:pagupta@redhat.com:riel@surriel.com:lcapitulino@redhat.com:dave.hansen@intel.com:wei.w.wang@intel.com:aarcange@redhat.com:pbonzini@redhat.com:dan.j.williams@intel.com:alexander.h.duyck@linux.intel.com:osalvador@suse.de,RULES_HIT:1:2:41:69:152:355:379:800:960:966:973:988:989:1260:1277:1311:1313:1314:1345:1359:1431:1437:1515:1516:1518:1593:1594:1605:1730:1747:1777:1792:2196:2198:2199:2200:2393:2559:2562:2693:2731:3138:3139:3140:3141:3142:3865:3866:3867:3868:3870:3871:3872:3874:4049:4321:4385:4605:5007:6261:6653:6742:7576:7807:7875:7974:9010:9036:9413:9592:10004:11026:11473:11658:11914:12043:12048:12291:12296:12297:12438:12517:12519:12555:12679:126 83:12760 X-HE-Tag: hot48_8efad728bb037 X-Filterd-Recvd-Size: 10076 Received: from mail-yb1-f195.google.com (mail-yb1-f195.google.com [209.85.219.195]) by imf12.hostedemail.com (Postfix) with ESMTP for ; Tue, 19 Nov 2019 21:46:22 +0000 (UTC) Received: by mail-yb1-f195.google.com with SMTP id q7so9493917ybk.4 for ; Tue, 19 Nov 2019 13:46:22 -0800 (PST) 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=vRTdXLGlIRX6PGjWF3qAFy+h5lzWEe6Gf9rZrQkRSH8=; b=WgWSDZCjU99nDR/Ua2n8iB18ALMc/dFJq2zLttN5xPF9rSVRdWE12dMQtDlvWvG3jv 798h2jjMb4GbzHd0P9klx1E+K2bhfYNG8SWGBXktYAsd9uGuQHaMNb0KjUOiOQtLhGS3 SkSqAxUo8ULMfPW/1qUevG1/REvajb+2lF+BmmsEKloR+i9yj/SvNTkj2rVHH+O2f7CI ED4NGpZQaZZ9Vmc/1wFwDFN+PdF4YHsnHryvZ+PYAWkuwIcx6TUYj7dUKyfiCtBGwFKF HTEqRxmKBnVqeKMNJhEbghLIzGmh10s2umgd0WFA9Fn23xmIOk7o2AaNx8Jd+1gs6cZH AxiA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=vRTdXLGlIRX6PGjWF3qAFy+h5lzWEe6Gf9rZrQkRSH8=; b=K9i7vk698LWoacHGaos8fXzY27ohXac2WNrq8DrvkPl4hDpCMeYG7DrZBz4pI+V3f3 nP0j6S1FmOyApIXuPvnZ2OmIJppgsgeQhLaUyFOYZnv3sXbOELWlSKv6tUfGcsj0e4ZV gzwueScJPsJiaIZeuKIGETeLml8aUiYeb07SmntlUhBzkZutYQAKSpJsX82LItNIWahC /A9cWj42iGPRM0SyUm5oSZhyGBDGA80rmwCfGNqlaeqgCNuB8mATO9hQSfwOJDf3YVjm rKGmUhUrJc2ACbh3h/jFOy1PaSsE8+Sedj5Jsnhv6i80LIAZXYYmmUAV0SUP4Sbphg4U 8qxQ== X-Gm-Message-State: APjAAAWyZQbDz0KqLWpCjHmbL2sReYHCjZ9T3W98QMDIFBCjkmD9GaW5 WXnyTv6KeP6GAwOH5a7AnRw= X-Google-Smtp-Source: APXvYqyleTGX6pFKhJehM88FqwxssAv+dmjEsyIqaIpXVPihHZ2q/HqdLxwGH+S2RuhmnV2pMAZX9g== X-Received: by 2002:a25:d0d7:: with SMTP id h206mr58288ybg.315.1574199981544; Tue, 19 Nov 2019 13:46:21 -0800 (PST) Received: from localhost.localdomain ([2001:470:b:9c3:9e5c:8eff:fe4f:f2d0]) by smtp.gmail.com with ESMTPSA id b63sm10099456ywc.94.2019.11.19.13.46.19 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 19 Nov 2019 13:46:20 -0800 (PST) Subject: [PATCH v14 1/6] mm: Adjust shuffle code to allow for future coalescing From: Alexander Duyck To: kvm@vger.kernel.org, mst@redhat.com, linux-kernel@vger.kernel.org, willy@infradead.org, mhocko@kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org, mgorman@techsingularity.net, vbabka@suse.cz Cc: yang.zhang.wz@gmail.com, nitesh@redhat.com, konrad.wilk@oracle.com, david@redhat.com, pagupta@redhat.com, riel@surriel.com, lcapitulino@redhat.com, dave.hansen@intel.com, wei.w.wang@intel.com, aarcange@redhat.com, pbonzini@redhat.com, dan.j.williams@intel.com, alexander.h.duyck@linux.intel.com, osalvador@suse.de Date: Tue, 19 Nov 2019 13:46:18 -0800 Message-ID: <20191119214618.24996.30818.stgit@localhost.localdomain> In-Reply-To: <20191119214454.24996.66289.stgit@localhost.localdomain> References: <20191119214454.24996.66289.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: From: Alexander Duyck 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. Acked-by: David Hildenbrand Reviewed-by: Dan Williams Signed-off-by: Alexander Duyck Acked-by: Mel Gorman --- include/linux/mmzone.h | 12 -------- mm/page_alloc.c | 71 ++++++++++++++++++++++++++++-------------------- mm/shuffle.c | 12 ++++---- mm/shuffle.h | 6 ++++ 4 files changed, 54 insertions(+), 47 deletions(-) diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 89d8ff06c9ce..245010b24747 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 62dcd6b76c80..32e9cc092656 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -878,6 +878,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 @@ -906,11 +936,13 @@ 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; + bool to_tail; max_order = min_t(unsigned int, MAX_ORDER, pageblock_order + 1); @@ -979,35 +1011,16 @@ 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; - } - } - + area = &zone->free_area[order]; if (is_shuffle_order(order)) - add_to_free_area_random(page, &zone->free_area[order], - migratetype); + to_tail = shuffle_pick_tail(); else - add_to_free_area(page, &zone->free_area[order], migratetype); + to_tail = buddy_merge_likely(pfn, buddy_pfn, page, order); + if (to_tail) + add_to_free_area_tail(page, area, migratetype); + else + add_to_free_area(page, area, migratetype); } /* diff --git a/mm/shuffle.c b/mm/shuffle.c index b3fe97fd6654..e65d57f39486 100644 --- a/mm/shuffle.c +++ b/mm/shuffle.c @@ -183,11 +183,11 @@ void __meminit __shuffle_free_memory(pg_data_t *pgdat) shuffle_zone(z); } -void add_to_free_area_random(struct page *page, struct free_area *area, - int migratetype) +bool shuffle_pick_tail(void) { static u64 rand; static u8 rand_bits; + bool ret; /* * The lack of locking is deliberate. If 2 threads race to @@ -198,10 +198,10 @@ void add_to_free_area_random(struct page *page, struct free_area *area, rand = get_random_u64(); } - if (rand & 1) - add_to_free_area(page, area, migratetype); - else - add_to_free_area_tail(page, area, migratetype); + ret = rand & 1; + rand_bits--; rand >>= 1; + + return ret; } diff --git a/mm/shuffle.h b/mm/shuffle.h index 777a257a0d2f..4d79f03b6658 100644 --- a/mm/shuffle.h +++ b/mm/shuffle.h @@ -22,6 +22,7 @@ enum mm_shuffle_ctl { DECLARE_STATIC_KEY_FALSE(page_alloc_shuffle_key); extern void page_alloc_shuffle(enum mm_shuffle_ctl ctl); extern void __shuffle_free_memory(pg_data_t *pgdat); +extern bool shuffle_pick_tail(void); static inline void shuffle_free_memory(pg_data_t *pgdat) { if (!static_branch_unlikely(&page_alloc_shuffle_key)) @@ -44,6 +45,11 @@ static inline bool is_shuffle_order(int order) return order >= SHUFFLE_ORDER; } #else +static inline bool shuffle_pick_tail(void) +{ + return false; +} + static inline void shuffle_free_memory(pg_data_t *pgdat) { } From patchwork Tue Nov 19 21:46: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: 11252707 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 6DC726C1 for ; Tue, 19 Nov 2019 21:46:32 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 1F9602245D for ; Tue, 19 Nov 2019 21:46:31 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="KS7mN1Fk" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1F9602245D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 2B8FE6B0007; Tue, 19 Nov 2019 16:46:31 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 269B36B0008; Tue, 19 Nov 2019 16:46:31 -0500 (EST) 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 15A8B6B000A; Tue, 19 Nov 2019 16:46:31 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0003.hostedemail.com [216.40.44.3]) by kanga.kvack.org (Postfix) with ESMTP id F09EE6B0007 for ; Tue, 19 Nov 2019 16:46:30 -0500 (EST) Received: from smtpin21.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with SMTP id 950C58249980 for ; Tue, 19 Nov 2019 21:46:30 +0000 (UTC) X-FDA: 76174361340.21.boys18_90117b6477a06 X-Spam-Summary: 2,0,0,c6df37b7957b4bfd,d41d8cd98f00b204,alexander.duyck@gmail.com,:kvm@vger.kernel.org:mst@redhat.com:linux-kernel@vger.kernel.org:willy@infradead.org:mhocko@kernel.org::akpm@linux-foundation.org:mgorman@techsingularity.net:vbabka@suse.cz:yang.zhang.wz@gmail.com:nitesh@redhat.com:konrad.wilk@oracle.com:david@redhat.com:pagupta@redhat.com:riel@surriel.com:lcapitulino@redhat.com:dave.hansen@intel.com:wei.w.wang@intel.com:aarcange@redhat.com:pbonzini@redhat.com:dan.j.williams@intel.com:alexander.h.duyck@linux.intel.com:osalvador@suse.de,RULES_HIT:1:2:41:69:152:355:379:560:800:960:965:966:968:973:988:989:1260:1277:1311:1313:1314:1345:1359:1431:1437:1515:1516:1518:1593:1594:1605:1730:1747:1777:1792:2196:2198:2199:2200:2393:2559:2562:2693:2731:2898:2901:3138:3139:3140:3141:3142:3865:3866:3867:3868:3870:3871:3872:3874:4051:4321:4385:4390:4395:4605:5007:6119:6261:6653:6742:7576:7974:8603:8957:9010:9036:9413:9592:10004:11026:11473:11658:11914:12043:12048:12291:12296:12 297:1243 X-HE-Tag: boys18_90117b6477a06 X-Filterd-Recvd-Size: 12029 Received: from mail-pf1-f193.google.com (mail-pf1-f193.google.com [209.85.210.193]) by imf19.hostedemail.com (Postfix) with ESMTP for ; Tue, 19 Nov 2019 21:46:29 +0000 (UTC) Received: by mail-pf1-f193.google.com with SMTP id 193so12977178pfc.13 for ; Tue, 19 Nov 2019 13:46:29 -0800 (PST) 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=34F/8sLFbSqzOgFDxma/APMdsGXJmPlF4s56rTkFMj8=; b=KS7mN1FkHIXjcrmQeJrY5Eak7WhrR8L9kzI42pXhwsllZeDcX2g6kyp6FiaoQH3BCW 8Tx8nP9mW1jMjkA2w0ImfN99OPtIlYnebT1UVPi2CZc0FLr7+uKKveoGLCD5WqHzNgFC 9iye3Q2OjYLIXqUgD6K+TBWZFS+iCNH44udW47P125WfkZ7QDEMDZcyi3fAhf//LGYNr fpy5QESb86qdvz/pddC6MkCtKaeJuqVp54OdCeVm2mOzLh95oyS5bY2b5ZqPS+AZuJhk ODB0isL7yEeCPFxTp3PP2WSyDFC30eJAko1+eKo6GyZFqh/uK+aO/yHDIaN+Wdeq80df dm4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=34F/8sLFbSqzOgFDxma/APMdsGXJmPlF4s56rTkFMj8=; b=P98vhTMebl+Xbdmu9O5L6KidKXAqqOqgTGp2c/U8NWIxNiLDPSp8XB+nwvsPgDG8lW MSmb2DbcNiYy9BvtiEC9arMwMfOltd0aYwB7GD1OrJLI2P6TkKMfT07+PaKaOqVblzoS kBboe1LVo7Ve+jUO/VgBi5bU1pD2tM3znnPxQcx1tbTT3Po8wlg/IfClbChZp0wqlwLs ZeA5ca0W+yRaGQEkrjZWIaqKK11SdLXRaI7tKtQUTHYtzWDCLQir7SY4iZ1ShIuRtDtp VO7cIRUDcx58e+ao7uxZnYN4PJl3RzF7AvAxKfA6p1Mv5HTws3Uy7CPWZ0ll6WMchHns 8VIw== X-Gm-Message-State: APjAAAWtST1ZrfPOOUiksn7F4k1gRHd6T14uKIOi2cGVV4BoAgUxSg06 TJCATdwU8LpfUYOH0NeNegw= X-Google-Smtp-Source: APXvYqwpzHonfhvVoJ7ZcUZUvMN6ub6LE9EHY1V10I1gIFESaOmFJF2hygCgwoV9KODA++YsdNE0OA== X-Received: by 2002:a62:8c:: with SMTP id 134mr8516910pfa.31.1574199988383; Tue, 19 Nov 2019 13:46:28 -0800 (PST) Received: from localhost.localdomain ([2001:470:b:9c3:9e5c:8eff:fe4f:f2d0]) by smtp.gmail.com with ESMTPSA id x2sm25179351pge.76.2019.11.19.13.46.27 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 19 Nov 2019 13:46:27 -0800 (PST) Subject: [PATCH v14 2/6] mm: Use zone and order instead of free area in free_list manipulators From: Alexander Duyck To: kvm@vger.kernel.org, mst@redhat.com, linux-kernel@vger.kernel.org, willy@infradead.org, mhocko@kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org, mgorman@techsingularity.net, vbabka@suse.cz Cc: yang.zhang.wz@gmail.com, nitesh@redhat.com, konrad.wilk@oracle.com, david@redhat.com, pagupta@redhat.com, riel@surriel.com, lcapitulino@redhat.com, dave.hansen@intel.com, wei.w.wang@intel.com, aarcange@redhat.com, pbonzini@redhat.com, dan.j.williams@intel.com, alexander.h.duyck@linux.intel.com, osalvador@suse.de Date: Tue, 19 Nov 2019 13:46:26 -0800 Message-ID: <20191119214626.24996.82979.stgit@localhost.localdomain> In-Reply-To: <20191119214454.24996.66289.stgit@localhost.localdomain> References: <20191119214454.24996.66289.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: 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. Since the functions are only used in the file mm/page_alloc.c we can just move them there to reduce noise in the header. Reviewed-by: Dan Williams Reviewed-by: David Hildenbrand Reviewed-by: Pankaj Gupta Signed-off-by: Alexander Duyck Acked-by: Mel Gorman --- include/linux/mmzone.h | 32 ----------------------- mm/page_alloc.c | 67 +++++++++++++++++++++++++++++++++++------------- 2 files changed, 49 insertions(+), 50 deletions(-) diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 245010b24747..8d93106490f3 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]); diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 32e9cc092656..e0a7895300fb 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -877,6 +877,44 @@ static inline struct capture_control *task_capc(struct zone *zone) } #endif /* CONFIG_COMPACTION */ +/* 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--; +} + /* * 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 @@ -939,7 +977,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; bool to_tail; @@ -977,7 +1014,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; @@ -1011,16 +1048,15 @@ 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)) to_tail = shuffle_pick_tail(); else to_tail = buddy_merge_likely(pfn, buddy_pfn, page, order); if (to_tail) - 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); } /* @@ -2038,13 +2074,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]); @@ -2058,7 +2092,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); } } @@ -2216,8 +2250,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; } @@ -2291,7 +2325,7 @@ static int move_freepages(struct zone *zone, VM_BUG_ON_PAGE(page_zone(page) != zone, page); 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; } @@ -2407,7 +2441,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; @@ -2478,8 +2511,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); } /* @@ -3150,7 +3182,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; @@ -3176,7 +3207,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 @@ -8703,7 +8734,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); pfn += (1 << order); } spin_unlock_irqrestore(&zone->lock, flags); From patchwork Tue Nov 19 21:46:33 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Duyck X-Patchwork-Id: 11252713 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A3B921390 for ; Tue, 19 Nov 2019 21:46:39 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 49EDB2245D for ; Tue, 19 Nov 2019 21:46:39 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="HcB7fVES" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 49EDB2245D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 550446B0008; Tue, 19 Nov 2019 16:46:38 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 501FD6B000A; Tue, 19 Nov 2019 16:46:38 -0500 (EST) 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 3F01A6B000C; Tue, 19 Nov 2019 16:46:38 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0048.hostedemail.com [216.40.44.48]) by kanga.kvack.org (Postfix) with ESMTP id 230B06B0008 for ; Tue, 19 Nov 2019 16:46:38 -0500 (EST) Received: from smtpin15.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with SMTP id C6CE82C8A for ; Tue, 19 Nov 2019 21:46:37 +0000 (UTC) X-FDA: 76174361634.15.metal26_911231a9b2d3a X-Spam-Summary: 2,0,0,2151a5f2c5e03273,d41d8cd98f00b204,alexander.duyck@gmail.com,:kvm@vger.kernel.org:mst@redhat.com:linux-kernel@vger.kernel.org:willy@infradead.org:mhocko@kernel.org::akpm@linux-foundation.org:mgorman@techsingularity.net:vbabka@suse.cz:yang.zhang.wz@gmail.com:nitesh@redhat.com:konrad.wilk@oracle.com:david@redhat.com:pagupta@redhat.com:riel@surriel.com:lcapitulino@redhat.com:dave.hansen@intel.com:wei.w.wang@intel.com:aarcange@redhat.com:pbonzini@redhat.com:dan.j.williams@intel.com:alexander.h.duyck@linux.intel.com:osalvador@suse.de,RULES_HIT:41:142:152:327:355:379:421:800:960:966:973:988:989:1260:1277:1311:1313:1314:1345:1359:1431:1437:1515:1516:1518:1593:1594:1605:1730:1747:1777:1792:1801:2194:2196:2198:2199:2200:2201:2393:2559:2562:2689:2693:2731:2892:2895:2897:2898:2924:2926:3138:3139:3140:3141:3142:3369:3865:3866:3867:3868:3870:3871:3872:3874:4250:4321:4385:4423:4605:5007:6119:6261:6653:6691:6742:7576:7875:7903:7974:8603:8660:8957:9010:9121:9413:10004:10 128:1102 X-HE-Tag: metal26_911231a9b2d3a X-Filterd-Recvd-Size: 29679 Received: from mail-pg1-f195.google.com (mail-pg1-f195.google.com [209.85.215.195]) by imf46.hostedemail.com (Postfix) with ESMTP for ; Tue, 19 Nov 2019 21:46:36 +0000 (UTC) Received: by mail-pg1-f195.google.com with SMTP id b1so2657981pgq.10 for ; Tue, 19 Nov 2019 13:46:36 -0800 (PST) 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=CJp3/uRV0iWbhG+ok4dWB/RvFvlh+OmnuOcxk96/tt4=; b=HcB7fVES2W0rYUBmeY1C/xpkClSm08RdnAeiQn0BZvn3hqEUJ43X3m1HqnpwoJQYm9 AVTLHkb9C0qDqLB2ruIuM53dZRF2xsMuhx/oH+j+qmsRtqzqCgj9t9cQbQ5yNmeZzxsG v4SUlxia8krXeNS/RvLym5zVA0wddOBP4WGFuC3+Jy8+xJn6lxQQ9pnCTr45yCFItbI9 y8JYGr0UTghvuejJ0FzVUEPCnS1CUfllBUkTWWhljD0vIFlSvDpyzbEvUM8d5Dzc82qu z4PMcjHQVkOnIEZFSq2zXMPfOt/hTs7P8J1A7Dc3SleKDeroBWUUl3dyr4APU0/bNF+9 gmKg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=CJp3/uRV0iWbhG+ok4dWB/RvFvlh+OmnuOcxk96/tt4=; b=kei6NbeuV2vs8Av24a1pBUeEFenVRq0hFm73wcGYY8htG/SZ1gDY+H4YnlNDzA+YXO Gc3Qn/EZzeVRz+Md/dIMg6yle0JAM9WS1xf7Vzp7Bcdg/gJY3FitWoGUFdjMjjAnHWgN UjPZjUj5cJ62Y1x0RmY3E+hWCUeFrGm1ke2i2vraOGHe8/bZtOaxc25uT6ZxoauZVCyv /UPWkWaelUoUv1SEFQX3RjWlce7xzYwGtN1FVO38spINv5iW7UeKgX9TxxeILyWMR1eJ Z3/ewxlTRwrNHsIdkA20HJx1ncG/Cev3z3OOuvQb3loBG7aNX4f0CoIIQxzgteULX9qz aoFA== X-Gm-Message-State: APjAAAUUHqTwObMW73bfuXvkyy8xTj3EqeDWLpSuYjWfcmB9ubN5tlUh eWJBK2xPILDyVgfcISX3vOI= X-Google-Smtp-Source: APXvYqxLh+vEXj2X26RdTu1zubG+UQOBUoBI9bvJqRyJtNY8d9mtfs+s3V+qBN6kYS7HfX5YHVyMAg== X-Received: by 2002:a63:e44b:: with SMTP id i11mr7976765pgk.437.1574199995222; Tue, 19 Nov 2019 13:46:35 -0800 (PST) Received: from localhost.localdomain ([2001:470:b:9c3:9e5c:8eff:fe4f:f2d0]) by smtp.gmail.com with ESMTPSA id w15sm21368363pfi.168.2019.11.19.13.46.33 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 19 Nov 2019 13:46:34 -0800 (PST) Subject: [PATCH v14 3/6] mm: Introduce Reported pages From: Alexander Duyck To: kvm@vger.kernel.org, mst@redhat.com, linux-kernel@vger.kernel.org, willy@infradead.org, mhocko@kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org, mgorman@techsingularity.net, vbabka@suse.cz Cc: yang.zhang.wz@gmail.com, nitesh@redhat.com, konrad.wilk@oracle.com, david@redhat.com, pagupta@redhat.com, riel@surriel.com, lcapitulino@redhat.com, dave.hansen@intel.com, wei.w.wang@intel.com, aarcange@redhat.com, pbonzini@redhat.com, dan.j.williams@intel.com, alexander.h.duyck@linux.intel.com, osalvador@suse.de Date: Tue, 19 Nov 2019 13:46:33 -0800 Message-ID: <20191119214633.24996.46821.stgit@localhost.localdomain> In-Reply-To: <20191119214454.24996.66289.stgit@localhost.localdomain> References: <20191119214454.24996.66289.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: 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. To prevent the reported pages from leaking outside of the buddy lists I have added a call to clear_reported_page to the del_page_from_free_list function. As a result any reported page that is split, merged, or allocated will have the flag cleared prior to the PageBuddy value being cleared. The process for reporting pages is fairly simple. Once the nr_free for a given free area has exceeded the number of pages reported for that area plus a certain high watermark value we will flag the zone as needing reporting and schedule the worker thread to start reporting. That worker thread will begin working from the lowest supported page reporting order up to MAX_ORDER - 1 pulling unreported pages from the free list and storing them in the scatterlist. When processing each individual free list it is necessary for the worker thread to release the zone lock when it needs to stop and report the full scatterlist of pages. To reduce the work of the next iteration the worker thread will rotate the free list so that the first unreported page in the free list becomes the first entry in the list. Doing this we should only have to walk an entire free list no more than twice assuming large numbers of pages are not being added to the tail of the list. It will then call a reporting function providing information on how many entries are in the scatterlist. Once the function completes it will return the pages to the tail of the free area from which they were allocated and start over pulling more pages from the free areas until there are no longer enough pages to report on to keep the worker busy. The worker thread will work in a round-robin fashion making its way though each zone requesting reporting, and through each reportable free list within that zone. Once all free areas within the zone is below the high watermark level for free pages to report the flag indicating that the zone has requested reporting will be cleared, and if no zones are requesting reporting the worker thread will exit. Signed-off-by: Alexander Duyck --- include/linux/mmzone.h | 12 + include/linux/page-flags.h | 11 + include/linux/page_reporting.h | 31 ++++ mm/Kconfig | 11 + mm/Makefile | 1 mm/memory_hotplug.c | 2 mm/page_alloc.c | 53 ++++++ mm/page_reporting.c | 337 ++++++++++++++++++++++++++++++++++++++++ mm/page_reporting.h | 125 +++++++++++++++ 9 files changed, 579 insertions(+), 4 deletions(-) create mode 100644 include/linux/page_reporting.h create mode 100644 mm/page_reporting.c create mode 100644 mm/page_reporting.h diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 8d93106490f3..9647499983b1 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -478,6 +478,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 */ @@ -550,6 +558,10 @@ 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. + */ }; static inline unsigned long zone_managed_pages(struct zone *zone) diff --git a/include/linux/page-flags.h b/include/linux/page-flags.h index 1bf83c8fcaa7..a3a3b15b56a8 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..925a16b1d14b --- /dev/null +++ b/include/linux/page_reporting.h @@ -0,0 +1,31 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _LINUX_PAGE_REPORTING_H +#define _LINUX_PAGE_REPORTING_H + +#include + +struct page_reporting_dev_info { + /* function that alters pages to make them "reported" */ + void (*report)(struct page_reporting_dev_info *prdev, + unsigned int nents); + + /* pointer to scatterlist containing pages to be processed */ + struct scatterlist *sg; + + /* + * Upper limit on the number of pages that the report function + * expects to be placed into the scatterlist to be processed. + */ + unsigned int capacity; + + /* The number of zones requesting reporting */ + atomic_t refcnt; + + /* work struct for processing reports */ + struct delayed_work work; +}; + +/* Tear-down and bring-up for page reporting devices */ +void page_reporting_unregister(struct page_reporting_dev_info *prdev); +int page_reporting_register(struct page_reporting_dev_info *prdev); +#endif /*_LINUX_PAGE_REPORTING_H */ diff --git a/mm/Kconfig b/mm/Kconfig index e38ff1d5968d..bd68901bbfe2 100644 --- a/mm/Kconfig +++ b/mm/Kconfig @@ -237,6 +237,17 @@ config COMPACTION linux-mm@kvack.org. # +# support for unused page reporting +config PAGE_REPORTING + bool "Allow for reporting of unused pages" + def_bool n + help + Unused page reporting allows for the incremental acquisition of + unused pages from the buddy allocator for the purpose of reporting + those pages to another entity, such as a hypervisor, so that the + memory can be freed up for other uses. + +# # support for page migration # config MIGRATION diff --git a/mm/Makefile b/mm/Makefile index fa02f19f8e50..41e6aa432781 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -108,3 +108,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 46b2e056a43f..bf19d94cb206 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -41,6 +41,7 @@ #include "internal.h" #include "shuffle.h" +#include "page_reporting.h" /* * online_page_callback contains pointer to current page onlining function. @@ -1566,6 +1567,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(zone); } else zone_pcp_update(zone); diff --git a/mm/page_alloc.c b/mm/page_alloc.c index e0a7895300fb..5ef8f5abdd4d 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -74,6 +74,7 @@ #include #include "internal.h" #include "shuffle.h" +#include "page_reporting.h" /* prevent >1 _updater_ of zone percpu pageset ->high and ->batch fields */ static DEFINE_MUTEX(pcp_batch_high_lock); @@ -909,6 +910,10 @@ static inline void move_to_free_list(struct page *page, struct zone *zone, static inline void del_page_from_free_list(struct page *page, struct zone *zone, unsigned int order) { + /* clear reported state and update reported page count */ + if (page_reported(page)) + clear_page_reported(page, zone, order); + list_del(&page->lru); __ClearPageBuddy(page); set_page_private(page, 0); @@ -972,7 +977,7 @@ static inline void del_page_from_free_list(struct page *page, 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); @@ -1048,7 +1053,9 @@ static inline void __free_one_page(struct page *page, done_merging: set_page_order(page, order); - if (is_shuffle_order(order)) + if (reported) + to_tail = true; + else if (is_shuffle_order(order)) to_tail = shuffle_pick_tail(); else to_tail = buddy_merge_likely(pfn, buddy_pfn, page, order); @@ -1057,6 +1064,14 @@ static inline void __free_one_page(struct page *page, 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); } /* @@ -1373,7 +1388,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); @@ -1389,7 +1404,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); } @@ -3228,6 +3243,36 @@ int __isolate_free_page(struct page *page, unsigned int order) return 1UL << order; } +#ifdef CONFIG_PAGE_REPORTING +/** + * __free_isolated_page - Return a now-isolated page back where we got it + * @page: Page that was isolated + * @order: Order of the isolated page + * + * This function is meant to return a page pulled from the free lists via + * __isolate_free_page back to the free lists they were pulled from. + */ +void __free_isolated_page(struct page *page, unsigned int order) +{ + struct zone *zone = page_zone(page); + unsigned long pfn; + unsigned int mt; + + /* zone lock should be held when this function is called */ + lockdep_assert_held(&zone->lock); + + pfn = page_to_pfn(page); + mt = get_pfnblock_migratetype(page, pfn); + + /* + * Return isolated page to tail of freelist and don't bother with + * triggering the page reporting notifiers since this page was + * previously on the freelist and has likely already been reported. + */ + __free_one_page(page, pfn, zone, order, mt, true); +} +#endif /* CONFIG_PAGE_REPORTING */ + /* * Update NUMA hit/miss statistics * diff --git a/mm/page_reporting.c b/mm/page_reporting.c new file mode 100644 index 000000000000..4844f0aa2904 --- /dev/null +++ b/mm/page_reporting.c @@ -0,0 +1,337 @@ +// SPDX-License-Identifier: GPL-2.0 +#include +#include +#include +#include +#include +#include +#include + +#include "page_reporting.h" +#include "internal.h" + +static struct page_reporting_dev_info __rcu *pr_dev_info __read_mostly; + +#define for_each_reporting_migratetype_order(_order, _type) \ + for (_order = PAGE_REPORTING_MIN_ORDER; _order < MAX_ORDER; _order++) \ + for (_type = 0; _type < MIGRATE_TYPES; _type++) \ + if (!is_migrate_isolate(_type)) + +static void page_reporting_populate_metadata(struct zone *zone) +{ + size_t size; + int node; + + /* + * 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) + return; + + node = zone_to_nid(zone); + size = (MAX_ORDER - PAGE_REPORTING_MIN_ORDER) * sizeof(unsigned long); + zone->reported_pages = kzalloc_node(size, GFP_KERNEL, node); +} + +static void +page_reporting_drain(struct page_reporting_dev_info *prdev, struct zone *zone) +{ + struct scatterlist *sg = prdev->sg; + + /* + * Drain the now reported pages back into their respective + * free lists/areas. We assume at least one page is populated. + */ + do { + unsigned int order = get_order(sg->length); + struct page *page = sg_page(sg); + + __free_isolated_page(page, order); + + /* + * If page was not comingled with another page we can + * consider the result to be "reported" since the page + * hasn't been modified, otherwise we will need to + * report on the new larger page when we make our way + * up to that higher order. + */ + if (PageBuddy(page) && page_order(page) == order) + mark_page_reported(page, zone, order); + } 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 cannot obtain a + * full scatterlist of pages, in that case we will switch to idle. + */ +static unsigned int +page_reporting_cycle(struct page_reporting_dev_info *prdev, struct zone *zone, + unsigned int order, unsigned int mt, unsigned int nents) +{ + struct list_head *list = &zone->free_area[order].free_list[mt]; + unsigned int page_len = PAGE_SIZE << order; + struct scatterlist *sg = prdev->sg; + struct page *page, *next; + + /* + * Perform early check, if free area is empty there is + * nothing to process so we can skip this free_list. + */ + if (list_empty(list)) + return nents; + + spin_lock_irq(&zone->lock); + + /* loop through free list adding unreported pages to sg list */ + list_for_each_entry_safe(page, next, list, lru) { + /* We are going to skip over the reported pages. */ + if (PageReported(page)) + continue; + + /* Attempt to add page to sg list */ + if (nents < prdev->capacity) { + if (!__isolate_free_page(page, order)) + break; + + sg_set_page(&sg[nents++], page, page_len, 0); + continue; + } + + /* + * Make the first non-reported entry in the free list + * the new head of the free list before we exit. + */ + if (!list_is_first(&page->lru, list)) + list_rotate_to_front(&page->lru, list); + + /* release lock before waiting on report processing*/ + spin_unlock_irq(&zone->lock); + + /* begin processing pages in local list */ + prdev->report(prdev, nents); + + /* reset number of entries */ + nents = 0; + + /* reacquire zone lock and resume processing free lists */ + spin_lock_irq(&zone->lock); + + /* flush reported pages from the sg list */ + page_reporting_drain(prdev, zone); + + /* + * Reset next to first entry, the old next isn't valid + * since we dropped the lock to report the pages + */ + next = list_first_entry(list, struct page, lru); + } + + spin_unlock_irq(&zone->lock); + + return nents; +} + +static int +page_reporting_process_zone(struct page_reporting_dev_info *prdev, + struct zone *zone) +{ + unsigned int order, mt, nents = 0; + unsigned long watermark; + int refcnt = -1; + + page_reporting_populate_metadata(zone); + + /* Generate minimum watermark to be able to guarantee progress */ + watermark = low_wmark_pages(zone) + + (prdev->capacity << PAGE_REPORTING_MIN_ORDER); + + /* + * Cancel request if insufficient free memory or if we failed + * to allocate page reporting statistics for the zone. + */ + if (!zone_watermark_ok(zone, 0, watermark, 0, ALLOC_CMA) || + !zone->reported_pages) { + spin_lock_irq(&zone->lock); + goto zone_not_ready; + } + + sg_init_table(prdev->sg, prdev->capacity); + + /* Process each free list starting from lowest order/mt */ + for_each_reporting_migratetype_order(order, mt) + nents = page_reporting_cycle(prdev, zone, order, mt, nents); + + /* mark end of sg list and report the remainder */ + if (nents) { + sg_mark_end(&prdev->sg[nents - 1]); + prdev->report(prdev, nents); + } + + spin_lock_irq(&zone->lock); + + /* flush any remaining pages out from the last report */ + if (nents) + page_reporting_drain(prdev, zone); + + /* check to see if values are low enough for us to stop for now */ + for (order = PAGE_REPORTING_MIN_ORDER; order < MAX_ORDER; order++) { + if (pages_unreported(zone, order) < PAGE_REPORTING_HWM) + continue; +#ifdef CONFIG_MEMORY_ISOLATION + /* + * Do not allow a free_area with isolated pages to request + * that we continue with page reporting. Keep the reporting + * light until the isolated pages have been cleared. + */ + if (!free_area_empty(&zone->free_area[order], MIGRATE_ISOLATE)) + continue; +#endif + goto zone_not_complete; + } + +zone_not_ready: + /* + * 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); + refcnt = atomic_dec_return(&prdev->refcnt); +zone_not_complete: + spin_unlock_irq(&zone->lock); + + return refcnt; +} + +static void page_reporting_process(struct work_struct *work) +{ + struct delayed_work *d_work = to_delayed_work(work); + struct page_reporting_dev_info *prdev = + container_of(d_work, struct page_reporting_dev_info, work); + struct zone *zone = first_online_pgdat()->node_zones; + int refcnt = -1; + + do { + if (test_bit(ZONE_PAGE_REPORTING_REQUESTED, &zone->flags)) + refcnt = page_reporting_process_zone(prdev, zone); + + /* Move to next zone, if at end of list start over */ + zone = next_zone(zone) ? : first_online_pgdat()->node_zones; + + /* + * As long as refcnt has not reached zero there are still + * zones to be processed. + */ + } while (refcnt); +} + +/* request page reporting on this zone */ +void __page_reporting_request(struct zone *zone) +{ + struct page_reporting_dev_info *prdev; + + rcu_read_lock(); + + /* + * We use RCU to protect the pr_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. + */ + prdev = rcu_dereference(pr_dev_info); + if (unlikely(!prdev)) + goto out; + + /* + * 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 5 times per second. + */ + if (!atomic_fetch_inc(&prdev->refcnt)) + schedule_delayed_work(&prdev->work, HZ / 5); +out: + rcu_read_unlock(); +} + +static DEFINE_MUTEX(page_reporting_mutex); +DEFINE_STATIC_KEY_FALSE(page_reporting_enabled); + +void page_reporting_unregister(struct page_reporting_dev_info *prdev) +{ + mutex_lock(&page_reporting_mutex); + + if (rcu_access_pointer(pr_dev_info) == prdev) { + /* Disable page reporting notification */ + RCU_INIT_POINTER(pr_dev_info, NULL); + synchronize_rcu(); + + /* Flush any existing work, and lock it out */ + cancel_delayed_work_sync(&prdev->work); + + /* Free scatterlist */ + kfree(prdev->sg); + prdev->sg = NULL; + } + + mutex_unlock(&page_reporting_mutex); +} +EXPORT_SYMBOL_GPL(page_reporting_unregister); + +int page_reporting_register(struct page_reporting_dev_info *prdev) +{ + struct zone *zone; + int err = 0; + + /* No point in enabling this if it cannot handle any pages */ + if (WARN_ON(!prdev->capacity || prdev->capacity > PAGE_REPORTING_HWM)) + return -EINVAL; + + mutex_lock(&page_reporting_mutex); + + /* nothing to do if already in use */ + if (rcu_access_pointer(pr_dev_info)) { + err = -EBUSY; + goto err_out; + } + + /* allocate scatterlist to store pages being reported on */ + prdev->sg = kcalloc(prdev->capacity, sizeof(*prdev->sg), GFP_KERNEL); + if (!prdev->sg) { + err = -ENOMEM; + goto err_out; + } + + + /* initialize refcnt and work structures */ + atomic_set(&prdev->refcnt, 0); + INIT_DELAYED_WORK(&prdev->work, &page_reporting_process); + + /* assign device, and begin initial flush of populated zones */ + rcu_assign_pointer(pr_dev_info, prdev); + for_each_populated_zone(zone) { + spin_lock_irq(&zone->lock); + __page_reporting_request(zone); + spin_unlock_irq(&zone->lock); + } + + /* enable page reporting notification */ + if (!static_key_enabled(&page_reporting_enabled)) { + static_branch_enable(&page_reporting_enabled); + pr_info("Unused page reporting enabled\n"); + } +err_out: + mutex_unlock(&page_reporting_mutex); + + return err; +} +EXPORT_SYMBOL_GPL(page_reporting_register); diff --git a/mm/page_reporting.h b/mm/page_reporting.h new file mode 100644 index 000000000000..2ad31bbb0036 --- /dev/null +++ b/mm/page_reporting.h @@ -0,0 +1,125 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _MM_PAGE_REPORTING_H +#define _MM_PAGE_REPORTING_H + +#include +#include +#include +#include +#include +#include + +#define PAGE_REPORTING_MIN_ORDER pageblock_order +#define PAGE_REPORTING_HWM 32 + +#ifdef CONFIG_PAGE_REPORTING +/* Reported page accessors, defined in page_alloc.c */ +void __free_isolated_page(struct page *page, unsigned int order); + +/* Free reported_pages and reset reported page tracking count to 0 */ +static inline void page_reporting_reset_zone(struct zone *zone) +{ + kfree(zone->reported_pages); + zone->reported_pages = NULL; +} + +DECLARE_STATIC_KEY_FALSE(page_reporting_enabled); +void __page_reporting_request(struct zone *zone); + +static inline bool page_reported(struct page *page) +{ + return static_branch_unlikely(&page_reporting_enabled) && + PageReported(page); +} + +static inline unsigned long +pages_unreported(struct zone *zone, int order) +{ + unsigned long nr_free; + int report_order; + + /* Limit notifications only to higher order pages */ + report_order = order - PAGE_REPORTING_MIN_ORDER; + if (report_order < 0) + return 0; + + nr_free = zone->free_area[order].nr_free; + + /* Only subtract reported_pages count if it is present */ + if (!zone->reported_pages) + return nr_free; + + return nr_free - zone->reported_pages[report_order]; +} + +/** + * 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) +{ + /* Called from hot path in __free_one_page() */ + if (!static_branch_unlikely(&page_reporting_enabled)) + return; + + /* Do not bother with tests if we have already requested reporting */ + if (test_bit(ZONE_PAGE_REPORTING_REQUESTED, &zone->flags)) + return; + + /* Determine if we have crossed reporting threshold */ + if (pages_unreported(zone, order) < PAGE_REPORTING_HWM) + return; + + /* This is slow, but should be called very rarely */ + __page_reporting_request(zone); +} + +/* + * Functions for marking/clearing reported pages from the freelist. + * All of them expect the zone lock to be held to maintain + * consistency of the reported_pages count versus nr_free. + */ +static inline void +mark_page_reported(struct page *page, struct zone *zone, unsigned int order) +{ + /* flag page as reported */ + __SetPageReported(page); + + /* update areated page accounting */ + zone->reported_pages[order - PAGE_REPORTING_MIN_ORDER]++; +} + +static inline void +clear_page_reported(struct page *page, struct zone *zone, unsigned int order) +{ + /* page_private will contain the page order, so just use it directly */ + zone->reported_pages[order - PAGE_REPORTING_MIN_ORDER]--; + + /* clear the flag so we can report on it when it returns */ + __ClearPageReported(page); +} + +#else /* CONFIG_PAGE_REPORTING */ +#define page_reported(_page) false + +static inline void page_reporting_reset_zone(struct zone *zone) +{ +} + +static inline void page_reporting_notify_free(struct zone *zone, int order) +{ +} + +static inline void +clear_page_reported(struct page *page, struct zone *zone, unsigned int order) +{ +} +#endif /* CONFIG_PAGE_REPORTING */ +#endif /*_MM_PAGE_REPORTING_H */ From patchwork Tue Nov 19 21:46:40 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Duyck X-Patchwork-Id: 11252717 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 394141390 for ; Tue, 19 Nov 2019 21:46:45 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id E3D7422460 for ; Tue, 19 Nov 2019 21:46:44 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="bLCsx9ru" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org E3D7422460 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 21B856B000A; Tue, 19 Nov 2019 16:46:44 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 1CC7F6B000C; Tue, 19 Nov 2019 16:46:44 -0500 (EST) 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 06EBD6B000D; Tue, 19 Nov 2019 16:46:44 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id E63EC6B000A for ; Tue, 19 Nov 2019 16:46:43 -0500 (EST) Received: from smtpin21.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with SMTP id A0C698249980 for ; Tue, 19 Nov 2019 21:46:43 +0000 (UTC) X-FDA: 76174361886.21.mom32_74246b2b009 X-Spam-Summary: 2,0,0,c5e3a1af7198cbd7,d41d8cd98f00b204,alexander.duyck@gmail.com,:kvm@vger.kernel.org:mst@redhat.com:linux-kernel@vger.kernel.org:willy@infradead.org:mhocko@kernel.org::akpm@linux-foundation.org:mgorman@techsingularity.net:vbabka@suse.cz:yang.zhang.wz@gmail.com:nitesh@redhat.com:konrad.wilk@oracle.com:david@redhat.com:pagupta@redhat.com:riel@surriel.com:lcapitulino@redhat.com:dave.hansen@intel.com:wei.w.wang@intel.com:aarcange@redhat.com:pbonzini@redhat.com:dan.j.williams@intel.com:alexander.h.duyck@linux.intel.com:osalvador@suse.de,RULES_HIT:41:152:355:379:960:966:968:973:988:989:1260:1277:1311:1313:1314:1345:1359:1431:1437:1515:1516:1518:1535:1543:1593:1594:1711:1730:1747:1777:1792:2194:2196:2198:2199:2200:2201:2393:2553:2559:2562:2693:3138:3139:3140:3141:3142:3355:3743:3865:3866:3867:3868:3870:3871:3872:3874:4117:4250:4321:4385:5007:6119:6261:6653:6742:7576:7903:7974:8660:9108:9413:10004:10400:11026:11232:11473:11658:11914:12043:12048:12291:12297:12438:125 17:12519 X-HE-Tag: mom32_74246b2b009 X-Filterd-Recvd-Size: 6609 Received: from mail-pl1-f196.google.com (mail-pl1-f196.google.com [209.85.214.196]) by imf50.hostedemail.com (Postfix) with ESMTP for ; Tue, 19 Nov 2019 21:46:43 +0000 (UTC) Received: by mail-pl1-f196.google.com with SMTP id s10so12643454plp.2 for ; Tue, 19 Nov 2019 13:46:42 -0800 (PST) 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=OKKmBSH+nYC1HLRQ37JbMq2XbZg5f7HxUos9scjzwrQ=; b=bLCsx9ru+UHMSt3gT7qB1CWyChHpU/Cgn+wFTpIOECPUtIfEDwBQBpLLy89GQsCClz SBYh1m0XdZw8S1Dcm36AkeOoc1gxJNi58y5H1tVceEEWIfLN0cgNQqArX8ilJW7KFwhW uc+zVxWtG1glg2wZIRpqAoYEBUY3FPyvjenxxMLGO4f9TAxnn76xnHRifyRBEgbVhlh0 qLahJCba5+mZKNsXlqq9ZM9fEi/tGV5GpZ2APSmvMq9CYmjEtrCbLwKXEfgOjw4+ZoYW tJm6ByBN5T29T5nbKJ1bkFaAZ4g5wog/qDo0ErIyPWS8Nzh0/TkhrCAgthMNv69692DL Q3UA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=OKKmBSH+nYC1HLRQ37JbMq2XbZg5f7HxUos9scjzwrQ=; b=Fy6qv/TZQWOYfPl2rAjBqps5d8ijIjfVrjnWsTkfYEPZhqXNn3ZpsvwrYt/nC1b+0J h9bYREnp5fVBrotLGuq7rQtO8SpdMSxBDKERPhTBCzktFsP8vkxoE7Wpa3aOyxERqoSJ fufu9HdBc1gC7oX6AX4cz1JBBD7+7ziMBt0Ucqn2HRjQWrNbB2JJxb369e6ZRaNJXg/q D+9wtS0hTRNqYgDSQCrYQ48A1cCsM9M4465EQcTXGp2tPpcpKWgcFWChspk6X9na9VsH DOPPqzI+eqanudSq1k6SjDJa+GlGhQmaI2ZW4+he/BcTlcfh4W0s06vD6gajMCAaZV7y YzsA== X-Gm-Message-State: APjAAAXZ/sJ+qeZaYuR+MJ+rVoKZvPMuq2hW9oyleF2CDHpBMnJnT7Ud kMPM5Ab1DD+eDkg0LjbGX5w= X-Google-Smtp-Source: APXvYqxeW1MEo/1O/0PUJp9mcExSGRfdqDR/RyLReGkdJZNxm77Psbe49esAoHV54PX8xcfVO8jcQA== X-Received: by 2002:a17:90a:cc07:: with SMTP id b7mr9098948pju.135.1574200001821; Tue, 19 Nov 2019 13:46:41 -0800 (PST) Received: from localhost.localdomain ([2001:470:b:9c3:9e5c:8eff:fe4f:f2d0]) by smtp.gmail.com with ESMTPSA id x192sm29563012pfd.96.2019.11.19.13.46.40 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 19 Nov 2019 13:46:41 -0800 (PST) Subject: [PATCH v14 4/6] mm: Add unused page reporting documentation From: Alexander Duyck To: kvm@vger.kernel.org, mst@redhat.com, linux-kernel@vger.kernel.org, willy@infradead.org, mhocko@kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org, mgorman@techsingularity.net, vbabka@suse.cz Cc: yang.zhang.wz@gmail.com, nitesh@redhat.com, konrad.wilk@oracle.com, david@redhat.com, pagupta@redhat.com, riel@surriel.com, lcapitulino@redhat.com, dave.hansen@intel.com, wei.w.wang@intel.com, aarcange@redhat.com, pbonzini@redhat.com, dan.j.williams@intel.com, alexander.h.duyck@linux.intel.com, osalvador@suse.de Date: Tue, 19 Nov 2019 13:46:40 -0800 Message-ID: <20191119214640.24996.34494.stgit@localhost.localdomain> In-Reply-To: <20191119214454.24996.66289.stgit@localhost.localdomain> References: <20191119214454.24996.66289.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: From: Alexander Duyck Add documentation for unused page reporting. Currently the only consumer is virtio-balloon, however it is possible that other drivers might make use of this so it is best to add a bit of documetation explaining at a high level how to use the API. Signed-off-by: Alexander Duyck --- Documentation/vm/unused_page_reporting.rst | 44 ++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 Documentation/vm/unused_page_reporting.rst diff --git a/Documentation/vm/unused_page_reporting.rst b/Documentation/vm/unused_page_reporting.rst new file mode 100644 index 000000000000..932406f48842 --- /dev/null +++ b/Documentation/vm/unused_page_reporting.rst @@ -0,0 +1,44 @@ +.. _unused_page_reporting: + +===================== +Unused Page Reporting +===================== + +Unused page reporting is an API by which a device can register to receive +lists of pages that are currently unused by the system. This is useful in +the case of virtualization where a guest is then able to use this data to +notify the hypervisor that it is no longer using certain pages in memory. + +For the driver, typically a balloon driver, to use of this functionality +it will allocate and initialize a page_reporting_dev_info structure. The +fields within the structure it will populate are the "report" function +pointer used to process the scatterlist and "capacity" representing the +number of entries that the device can support in a single request. Once +those are populated a call to page_reporting_register will allocate the +scatterlist and register the device with the reporting framework assuming +no other page reporting devices are already registered. + +Once registered the page reporting API will begin reporting batches of +pages to the driver. The API determines that it needs to start reporting by +measuring the number of pages in a given free area versus the number of +reported pages for that free area. If the value meets or exceeds the value +defined by PAGE_REPORTING_HWM then the zone is flagged as requesting +reporting and a worker is scheduled to process zone requesting reporting. + +Pages reported will be stored in the scatterlist pointed to in the +page_reporting_dev_info with the final entry having the end bit set in +entry nent - 1. While pages are being processed by the report function they +will not be accessible to the allocator. Once the report function has been +completed the pages will be returned to the free area from which they were +obtained. + +Prior to removing a driver that is making use of unused page reporting it +is necessary to call page_reporting_unregister to have the +page_reporting_dev_info structure that is currently in use by unused page +reporting removed. Doing this will prevent further reports from being +issued via the interface. If another driver or the same driver is +registered it is possible for it to resume where the previous driver had +left off in terms of reporting unused pages. + +Alexander Duyck, Nov 15, 2019 + From patchwork Tue Nov 19 21:46:46 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Duyck X-Patchwork-Id: 11252721 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 500D86C1 for ; Tue, 19 Nov 2019 21:46:52 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 135F322474 for ; Tue, 19 Nov 2019 21:46:52 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="pxHhZu6c" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 135F322474 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 4B4EC6B000C; Tue, 19 Nov 2019 16:46:51 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 4653B6B000D; Tue, 19 Nov 2019 16:46:51 -0500 (EST) 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 3539C6B000E; Tue, 19 Nov 2019 16:46:51 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 216286B000C for ; Tue, 19 Nov 2019 16:46:51 -0500 (EST) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with SMTP id F05014DCC for ; Tue, 19 Nov 2019 21:46:49 +0000 (UTC) X-FDA: 76174362180.24.camp31_15daf9d6a448 X-Spam-Summary: 2,0,0,66789381ae16ed81,d41d8cd98f00b204,alexander.duyck@gmail.com,:kvm@vger.kernel.org:mst@redhat.com:linux-kernel@vger.kernel.org:willy@infradead.org:mhocko@kernel.org::akpm@linux-foundation.org:mgorman@techsingularity.net:vbabka@suse.cz:yang.zhang.wz@gmail.com:nitesh@redhat.com:konrad.wilk@oracle.com:david@redhat.com:pagupta@redhat.com:riel@surriel.com:lcapitulino@redhat.com:dave.hansen@intel.com:wei.w.wang@intel.com:aarcange@redhat.com:pbonzini@redhat.com:dan.j.williams@intel.com:alexander.h.duyck@linux.intel.com:osalvador@suse.de,RULES_HIT:41:152:355:379:800:960:965:966:973:988:989:1260:1277:1311:1313:1314:1345:1359:1431:1437:1515:1516:1518:1535:1542:1593:1594:1711:1730:1747:1777:1792:2196:2199:2393:2559:2562:2689:2693:2898:3138:3139:3140:3141:3142:3353:3865:3866:3867:3868:3870:3872:3874:4117:4321:4385:4390:4395:4605:5007:6261:6653:6742:7576:9413:10004:10400:11026:11473:11658:11914:12043:12048:12294:12296:12297:12438:12517:12519:12555:12679:12760:12986:132 55:13972 X-HE-Tag: camp31_15daf9d6a448 X-Filterd-Recvd-Size: 6101 Received: from mail-pl1-f195.google.com (mail-pl1-f195.google.com [209.85.214.195]) by imf35.hostedemail.com (Postfix) with ESMTP for ; Tue, 19 Nov 2019 21:46:49 +0000 (UTC) Received: by mail-pl1-f195.google.com with SMTP id az9so12619492plb.11 for ; Tue, 19 Nov 2019 13:46:49 -0800 (PST) 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=ywjGGrMf/PJ6pRbEzUf6NoarQ8dqbT7Q7Zl+POuvcuU=; b=pxHhZu6cyx/hOc9XQfd0RH8sE6fpU4bg1PhE5z4/1qLYjOYH4pPFcn5Iahqjcxhrmf EGYTAAAIUhEWUGJ/omefmLI4MVoLiY+Yc4Vc5SYNJzlEvcg1Fr2KglaI4g2DH9p8xsai heoQHkxrzpXw30oc8Bvp42uax2Ussoqe2kcf2qXInTKvFbHen4FbELczriXQtdWTQnpv TAPGE1qzUFAOM9uGBfNf5hcGo23ViWYoCwF+hQALqTHQT4ti4gV1iFJBPT+HU7jpu7xL I0RGBdAdLc3AwI3RHelzGSTwhPbPb9IsJe4JORInCMmFgxFESwgTmMiRLAuYtrHC5bpc W9tQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=ywjGGrMf/PJ6pRbEzUf6NoarQ8dqbT7Q7Zl+POuvcuU=; b=WL0bxxnRpKM0kt1YzhChhRMxqogKhcG8yM/o9xSUo/ySpzTQpzO1ycWKui7EQc9YZq CAeD592Vh1Iq/YhVAqq1+TzlQIvrCXCbp2k3Mu5sEWdh0A0AOjXdpq1w25oICyE2sdRp cyzrmv1SFF8e2Pb/dLv0orGR9DZfr2tk2Aow6Xvma5KEJQqkfwlRUF1Xa9qhCycB9LBH rWPhtTerxw8H2JdC3KW4PSzikcqkVfBmTeUtn8tKntCh6/SETnyQLRqC0dc2XyNuOkP1 PQTrKkqwOqP8qZHeVbCCU+q0I8lGdHWF3ypmSDQZA969+xMjQx2Tr8CgA4JZL4TmKvmG 5MFQ== X-Gm-Message-State: APjAAAXs8yJAudPQbKGdfpEBlOq53JTwWixZTcTkMTS3i++cc5n9Yniv 4VFIop6VzGBMeoUjLAqujJnIlTZm X-Google-Smtp-Source: APXvYqxFK2Vu0e0bo6UitPkTzzsCxgzjvP87HVX5pvRsVUzKM9ajgMGgfK4P/v1sppq4BQhkiuqL8g== X-Received: by 2002:a17:90a:a405:: with SMTP id y5mr9474836pjp.102.1574200008280; Tue, 19 Nov 2019 13:46:48 -0800 (PST) Received: from localhost.localdomain ([2001:470:b:9c3:9e5c:8eff:fe4f:f2d0]) by smtp.gmail.com with ESMTPSA id e17sm28317575pgt.89.2019.11.19.13.46.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 19 Nov 2019 13:46:47 -0800 (PST) Subject: [PATCH v14 5/6] virtio-balloon: Pull page poisoning config out of free page hinting From: Alexander Duyck To: kvm@vger.kernel.org, mst@redhat.com, linux-kernel@vger.kernel.org, willy@infradead.org, mhocko@kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org, mgorman@techsingularity.net, vbabka@suse.cz Cc: yang.zhang.wz@gmail.com, nitesh@redhat.com, konrad.wilk@oracle.com, david@redhat.com, pagupta@redhat.com, riel@surriel.com, lcapitulino@redhat.com, dave.hansen@intel.com, wei.w.wang@intel.com, aarcange@redhat.com, pbonzini@redhat.com, dan.j.williams@intel.com, alexander.h.duyck@linux.intel.com, osalvador@suse.de Date: Tue, 19 Nov 2019 13:46:46 -0800 Message-ID: <20191119214646.24996.20325.stgit@localhost.localdomain> In-Reply-To: <20191119214454.24996.66289.stgit@localhost.localdomain> References: <20191119214454.24996.66289.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: 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 need to add support for the more recent init_on_free feature which expects a behavior similar to page poisoning in that we expect the page to be pre-zeroed. Reviewed-by: David Hildenbrand Signed-off-by: Alexander Duyck --- drivers/virtio/virtio_balloon.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/drivers/virtio/virtio_balloon.c b/drivers/virtio/virtio_balloon.c index 226fbb995fb0..92099298bc16 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,20 @@ 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)) { + } + if (virtio_has_feature(vdev, VIRTIO_BALLOON_F_PAGE_POISON)) { + /* Start with poison val of 0 representing general init */ + __u32 poison_val = 0; + + /* + * Let the hypervisor know that we are expecting a + * specific value to be written back in unused pages. + */ + if (!want_init_on_free()) memset(&poison_val, PAGE_POISON, sizeof(poison_val)); - virtio_cwrite(vb->vdev, struct virtio_balloon_config, - poison_val, &poison_val); - } + + 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 @@ -1014,7 +1022,10 @@ static int virtballoon_restore(struct virtio_device *vdev) static int virtballoon_validate(struct virtio_device *vdev) { - if (!page_poisoning_enabled()) + /* Tell the host whether we care about poisoned pages. */ + if (!want_init_on_free() && + (IS_ENABLED(CONFIG_PAGE_POISONING_NO_SANITY) || + !page_poisoning_enabled())) __virtio_clear_bit(vdev, VIRTIO_BALLOON_F_PAGE_POISON); __virtio_clear_bit(vdev, VIRTIO_F_IOMMU_PLATFORM); From patchwork Tue Nov 19 21:46:53 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Duyck X-Patchwork-Id: 11252725 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 59A441390 for ; Tue, 19 Nov 2019 21:46:58 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 1A0342240D for ; Tue, 19 Nov 2019 21:46:58 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="dDh5zWmw" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 1A0342240D Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 3A4186B000D; Tue, 19 Nov 2019 16:46:57 -0500 (EST) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 353C96B000E; Tue, 19 Nov 2019 16:46:57 -0500 (EST) 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 2693E6B0010; Tue, 19 Nov 2019 16:46:57 -0500 (EST) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0114.hostedemail.com [216.40.44.114]) by kanga.kvack.org (Postfix) with ESMTP id 1378E6B000D for ; Tue, 19 Nov 2019 16:46:57 -0500 (EST) Received: from smtpin22.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with SMTP id D0BF052BB for ; Tue, 19 Nov 2019 21:46:56 +0000 (UTC) X-FDA: 76174362432.22.front53_25d7218f9742 X-Spam-Summary: 2,0,0,07b8bf5d5b60619a,d41d8cd98f00b204,alexander.duyck@gmail.com,:kvm@vger.kernel.org:mst@redhat.com:linux-kernel@vger.kernel.org:willy@infradead.org:mhocko@kernel.org::akpm@linux-foundation.org:mgorman@techsingularity.net:vbabka@suse.cz:yang.zhang.wz@gmail.com:nitesh@redhat.com:konrad.wilk@oracle.com:david@redhat.com:pagupta@redhat.com:riel@surriel.com:lcapitulino@redhat.com:dave.hansen@intel.com:wei.w.wang@intel.com:aarcange@redhat.com:pbonzini@redhat.com:dan.j.williams@intel.com:alexander.h.duyck@linux.intel.com:osalvador@suse.de,RULES_HIT:1:2:41:152:355:379:960:966:968:973:988:989:1260:1277:1311:1313:1314:1345:1359:1431:1437:1515:1516:1518:1593:1594:1605:1730:1747:1777:1792:2196:2198:2199:2200:2393:2559:2562:2693:2918:3138:3139:3140:3141:3142:3865:3866:3867:3868:3870:3871:3874:4049:4250:4321:4385:4605:5007:6119:6261:6653:6742:7576:7903:8603:9010:9413:10004:11026:11473:11658:11914:12043:12048:12291:12294:12296:12297:12438:12517:12519:12555:12679:12683:1276 0:12986: X-HE-Tag: front53_25d7218f9742 X-Filterd-Recvd-Size: 10441 Received: from mail-pg1-f196.google.com (mail-pg1-f196.google.com [209.85.215.196]) by imf29.hostedemail.com (Postfix) with ESMTP for ; Tue, 19 Nov 2019 21:46:56 +0000 (UTC) Received: by mail-pg1-f196.google.com with SMTP id 207so4787587pge.8 for ; Tue, 19 Nov 2019 13:46:56 -0800 (PST) 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=Kl7ZJlmi7sBAeFCxqsbtJTyoIeGJbf1TWCrAFqF4sF0=; b=dDh5zWmw89XUR6rt+NuvA0RzAyanbPwC0pFjKj6QivU5iom7LKXqS7X5AV1LoTMkw7 k90kIDzDrR6Ld0SRZARp8jp8AjYWI6buJ0IukVCGaclJVhwC8csIMYNuYKwjZwnOUbu+ AQi93jPEPyzsr+wdNRnoltIoBFtq+iNCFiJqi5yOkG6BYAK2DY62SoE2gvCSNs/Nqkvz Du/ruvnP7Jcldq16LMh2xQL/W0OZ3mmPqkTdpeAS5xQ8aoJmhuwoLfyeqTB5/IhImv+j /Jk0gUpP9ae9ojzzePg7I5k/1tApsgjT7Yd/S/Z2QD/RzQZCroo+6QXN3djn85ABMuGO gf3Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:from:to:cc:date:message-id:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=Kl7ZJlmi7sBAeFCxqsbtJTyoIeGJbf1TWCrAFqF4sF0=; b=eYjmbEGA2zX8+LgRqbheJD/8JzAfcyhFeYVsdZB9ZZFPJeI6yRfVj4rq7i+pTGFC9g 8toZQgZ1Jx9Eke1uBJOhfp2uIHoTzz5hSMKORsOirgdTTRDopfsTOYlLa+YUO0zG1ReQ vh2THxmXe9VnVpNG4xZABPUV5zK67S9NcFFlR83W2dsm9E8tzXwirUoJN2o8brb/6PKK ZV4yuj0Ff6efw13eLI625rvqps3/CA5Tf466OZWBZ24ra3M5Nd/vjnjAPdBMcDUXcxq7 HMwI2z5qdyFVjJ5NDRJE2rFW62rEMzZHHUeN+F/sbekYPQyMUxcs1EASPnLk4VngCStD 2ecw== X-Gm-Message-State: APjAAAVUS0jw9VT0Y0ZqrZ/IxmMW0zXSNUSwxiAR4WFa26k1gH7oRozS PvUkkgUMNsuiSFrYmJAivcU= X-Google-Smtp-Source: APXvYqyA/cKB0w3sWtf/5xsd9cV6ZDGpzcUeIxAjzkBlNKmEvLxp/2NK95GJfyTb7oFfdNUHkstGqg== X-Received: by 2002:a63:3f4e:: with SMTP id m75mr7867766pga.392.1574200015079; Tue, 19 Nov 2019 13:46:55 -0800 (PST) Received: from localhost.localdomain ([2001:470:b:9c3:9e5c:8eff:fe4f:f2d0]) by smtp.gmail.com with ESMTPSA id o15sm23710284pgn.49.2019.11.19.13.46.53 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 19 Nov 2019 13:46:54 -0800 (PST) Subject: [PATCH v14 6/6] virtio-balloon: Add support for providing unused page reports to host From: Alexander Duyck To: kvm@vger.kernel.org, mst@redhat.com, linux-kernel@vger.kernel.org, willy@infradead.org, mhocko@kernel.org, linux-mm@kvack.org, akpm@linux-foundation.org, mgorman@techsingularity.net, vbabka@suse.cz Cc: yang.zhang.wz@gmail.com, nitesh@redhat.com, konrad.wilk@oracle.com, david@redhat.com, pagupta@redhat.com, riel@surriel.com, lcapitulino@redhat.com, dave.hansen@intel.com, wei.w.wang@intel.com, aarcange@redhat.com, pbonzini@redhat.com, dan.j.williams@intel.com, alexander.h.duyck@linux.intel.com, osalvador@suse.de Date: Tue, 19 Nov 2019 13:46:53 -0800 Message-ID: <20191119214653.24996.90695.stgit@localhost.localdomain> In-Reply-To: <20191119214454.24996.66289.stgit@localhost.localdomain> References: <20191119214454.24996.66289.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: 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 | 65 +++++++++++++++++++++++++++++++++++ include/uapi/linux/virtio_balloon.h | 1 + 3 files changed, 67 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 92099298bc16..6f5c6555765a 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 pr_dev_info; }; static struct virtio_device_id id_table[] = { @@ -152,6 +161,32 @@ static void tell_host(struct virtio_balloon *vb, struct virtqueue *vq) } +void virtballoon_unused_page_report(struct page_reporting_dev_info *pr_dev_info, + unsigned int nents) +{ + struct virtio_balloon *vb = + container_of(pr_dev_info, struct virtio_balloon, pr_dev_info); + struct virtqueue *vq = vb->reporting_vq; + unsigned int unused, err; + + /* We should always be able to add these buffers to an empty queue. */ + err = virtqueue_add_inbuf(vq, pr_dev_info->sg, nents, vb, + GFP_NOWAIT | __GFP_NOWARN); + + /* + * In the extremely unlikely case that something has changed and we + * are able to trigger an error we will simply display a warning + * and exit without actually processing the pages. + */ + if (WARN_ON(err)) + return; + + 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 +511,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 +523,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)) { @@ -932,12 +976,30 @@ static int virtballoon_probe(struct virtio_device *vdev) if (err) goto out_del_balloon_wq; } + + vb->pr_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->pr_dev_info.capacity = capacity; + + err = page_reporting_register(&vb->pr_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); @@ -966,6 +1028,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_unregister(&vb->pr_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); @@ -1038,6 +1102,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