From patchwork Tue Mar 21 00:48:25 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zi Yan X-Patchwork-Id: 13182069 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 98273C7618D for ; Tue, 21 Mar 2023 00:48:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 318A46B0078; Mon, 20 Mar 2023 20:48:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2C8F16B007B; Mon, 20 Mar 2023 20:48:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 190596B007D; Mon, 20 Mar 2023 20:48:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 0CF926B0078 for ; Mon, 20 Mar 2023 20:48:53 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id BEDE51A10F2 for ; Tue, 21 Mar 2023 00:48:52 +0000 (UTC) X-FDA: 80591070504.23.429169A Received: from out2-smtp.messagingengine.com (out2-smtp.messagingengine.com [66.111.4.26]) by imf28.hostedemail.com (Postfix) with ESMTP id D74FBC0007 for ; Tue, 21 Mar 2023 00:48:50 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=sent.com header.s=fm2 header.b=Fq31bFDh; dkim=pass header.d=messagingengine.com header.s=fm2 header.b=GuhsskQZ; spf=pass (imf28.hostedemail.com: domain of zi.yan@sent.com designates 66.111.4.26 as permitted sender) smtp.mailfrom=zi.yan@sent.com; dmarc=pass (policy=none) header.from=sent.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1679359730; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=0Tzs8PaBhDS7gnrC9VJc1BLc6ZqSPN615n03F+KtioE=; b=O4N2OGLSCVXOXvcK16DL5VfSPfQQrgbd06Zr1VRN2UhG/7VzJtvgFNTbMXVpJj/QuW6/TO IeCPBKarmD/z7jrvYJI7LWRAkJ7IeFkxjMOHS4AXcBCrgqsT58SO333mtQjmtf9WAOMbi+ gucpvSlptKwyslicL7PJD8dzwAoocXY= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=sent.com header.s=fm2 header.b=Fq31bFDh; dkim=pass header.d=messagingengine.com header.s=fm2 header.b=GuhsskQZ; spf=pass (imf28.hostedemail.com: domain of zi.yan@sent.com designates 66.111.4.26 as permitted sender) smtp.mailfrom=zi.yan@sent.com; dmarc=pass (policy=none) header.from=sent.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679359730; a=rsa-sha256; cv=none; b=pfGKICzO4fJM2yQjBHyhVOOC4y2nT5bJWaEM6BqZPgRU++qBX78LFy3AJiGx1GBPuWNYbO 3FHXFHf8OPJxHarR3R4SHuKEFmeM1BldGUedZKrDQATxZc5XDpYkw5i6ujYrzrDuphHiI6 VuaFK6QzBVSQ6Q5zZ4zLMT7nuXAvesk= Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 73B255C0148; Mon, 20 Mar 2023 20:48:50 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Mon, 20 Mar 2023 20:48:50 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sent.com; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:reply-to:sender:subject:subject:to:to; s=fm2; t= 1679359730; x=1679446130; bh=0Tzs8PaBhDS7gnrC9VJc1BLc6ZqSPN615n0 3F+KtioE=; b=Fq31bFDh7TW17tQxDKm3QuqWpUUozaf6KcCIGz7Z6OjBE/xKfMI +YMwa559U7k/zFh9J9lEl7sAjRLg9yKpaXa5QCyo2ORuR+QCNPGQyETnMZdOA/3V M8KOVZyGFXkRDZqvKUi/3ZjUedIyo3csEN6BTe4asByFlBlVISekm+uHb7gGHryf kJo0fXs32dR6z7XUQ03cvb2rgBwVtxVPl/SSWuaBGrYqpOlNH3nohVUy4HYbzRb6 +A1CfKMVbX22bdu8mygQ6PLc/g3kSAAij3PxKCSKN/HG4z1GndMIUZ8SqTVKdZC+ QD+hfXcRKgZLu/LjShA362ay9h+HvzfW7Zg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:reply-to:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t= 1679359730; x=1679446130; bh=0Tzs8PaBhDS7gnrC9VJc1BLc6ZqSPN615n0 3F+KtioE=; b=GuhsskQZstlXVlxY6sW5bBEdR+lJnuzUE2+2GjdthnhLPidQm65 rnFPK1OQDhX3VlHC9l/Wl/YXeTWQQpvg88tBgccVY7jc0J/oGZWBqDx9hf7UA1Uq 6WDTqOrmDKyFn9UUEFl8isZS6SaxBUdACAWX69eiaEpNe9TyvwDmAkgIBFod5IAp 1GBe882iEMuLdC8HeV27NWeoFXCLQnLG+qiqKUhT2FIFS0gciMBkl17OtrgnjauZ PCPLE9vkuxOpg1Xn0rjxiyNAR9dA6A9fqLF9uMh2MFbXViUhMW2aNtDi+aTMMz+/ ApQhl5zkIlRECcS/rJVS2tzmUS/GLwwFUrA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrvdefledgvdegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvvefufffkofgjfhhrggfgsedtqhertdertddtnecuhfhrohhmpegkihcu jggrnhcuoeiiihdrhigrnhesshgvnhhtrdgtohhmqeenucggtffrrghtthgvrhhnpeegge ehudfgudduvdelheehteegledtteeiveeuhfffveekhfevueefieeijeegvdenucevlhhu shhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpeiiihdrhigrnhessh gvnhhtrdgtohhm X-ME-Proxy: Feedback-ID: iccd040f4:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 20 Mar 2023 20:48:49 -0400 (EDT) From: Zi Yan To: "Matthew Wilcox (Oracle)" , Yang Shi , Yu Zhao , linux-mm@kvack.org Cc: Zi Yan , Andrew Morton , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH 1/5] mm: memcg: make memcg huge page split support any order split. Date: Mon, 20 Mar 2023 20:48:25 -0400 Message-Id: <20230321004829.2012847-2-zi.yan@sent.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230321004829.2012847-1-zi.yan@sent.com> References: <20230321004829.2012847-1-zi.yan@sent.com> Reply-To: Zi Yan MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: 3w1hosm66fqt5sc8hab9ohh4qd8u9y6e X-Rspamd-Queue-Id: D74FBC0007 X-HE-Tag: 1679359730-221823 X-HE-Meta: U2FsdGVkX19w60r8KwaCjK90nnk6jIGdyAcfOK5LYUJA8tSyn18/aWXdM6hH1OzBbWRDgNTWpfqlgPh/SuH0wzqTUZfJhX6KrqNMBT9Bd0V4rvvGV+aN0PGUVerhH5asbXEhgfsk9Q4EJu7ATzMVEzhOFmRryj7274kkbG1BkHDfMs8jU4Gk1gWS2hkKOUIgrE87i1PT5W//6L/5pxTPj0XqPIvF/URDpyBTeotWVsRCCJ03hRVLjU2D0IXwHJmJZN/FubyvVsHv9t5ggShtjf4iekisuRx0474ZrT69SIGpNfJ3WDzAPI1QYNc20T5Adc+cSqCxfi0cslqT75urPWtwhNjArW0G5jDo6vCXIWJusrMubU0wB5qxuDzl32Mt0MfEJjNS0YojQZI3LJbmo7pvfbMA8ngywH62gFtKz2IHYgqGw/AsoG6Tgp61B1SOJwOHPEwggTP6PQK+/FkXP/7PUseG1w7pmlNx68qCBb61p+GVH8KEfx005d07zKVnbKwxosXNRu4gOt/gZ3ilsfM3OMhnOcUDcbCiC7jrlt0ZhHKdXZG5WULwpTssne+pkJY5fK/3gcWlStZ4wfa5QJC9pa3XxHeZ9/ZIAZf5CTmbIWB4ENGd/ZZdGCB5OzxGHeuN7Z1I0e6f1GRXByWVtHNq/MjvwzBd//wymsvWjTcrMdUwUiOJhWKAzoyxUyUgDk0nNsnmeJ6Y0BmobGRULAnAiTtm/4rrrqdgPBZ9TmB/IN95p5CsoLld9PyHMcDSjlIo/BjEfnWvQp2l2UFs8qFkv+Is22hxtTj9J0n5nQQcZJTzlsYV5JE6WEBIlx0rMs8kEuOjFdX/rvWbBg5Jc/eM/UlqY2KTRX+m4lRZxMi2aJa2vzo+AS1lOatG2Qwlsujb3U0AAaf1XWV5J2v3bAAv3/ESJF6ev4TX++m1zQYPJ3wLCeU7wxzU9OLJnQNGUbN/KmU7bwrYIUkwaMx etsQ0twb ze4zPeTRphCGV//2DjB63i1vyC1wftjXhl4eiuRs5gvRMrdlim94ERLEXOaZLJzT8xr/BSJ8KRsphi/bTHhEJPvHTbV2Z6N0gcMSCsuIEUNTBXT7pex0m7F6ZFs5HEl/Vdmc0knGIdVQjIFKgy84ONFYp1dZi0+mMOOhy9N77QJQ4zHkFJwKCXsi/cdY1oSQXv21yW0asNbICd0FRTVyPshgeoCwjVln3FPmA87/kIs+BK+maUe9aobMEKWbfqqh46hdYJZ4S7qBit7r+SEJ6uFVSUXr5IcrHB/e45LFwBcYXcMBuPJ6iryxGSW62QdzImF+K50I7kQ1/Z4O2cbVDJ79Dpfo3P4zysMSRtHPALUmWNvH9flc3252u3ocyjBU/9wYkEELscECwoKkjR9GXjxiniLQExkDCjuBC 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: Zi Yan It sets memcg information for the pages after the split. A new parameter new_nr is added to tell the number of subpages in the new page, always 1 for now. It prepares for upcoming changes to support split huge page to any lower order. Signed-off-by: Zi Yan --- include/linux/memcontrol.h | 5 +++-- mm/huge_memory.c | 2 +- mm/memcontrol.c | 8 ++++---- mm/page_alloc.c | 2 +- 4 files changed, 9 insertions(+), 8 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index aa69ea98e2d8..ee1021129142 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1151,7 +1151,7 @@ static inline void memcg_memory_event_mm(struct mm_struct *mm, rcu_read_unlock(); } -void split_page_memcg(struct page *head, unsigned int nr); +void split_page_memcg(struct page *head, unsigned int nr, unsigned int new_nr); unsigned long mem_cgroup_soft_limit_reclaim(pg_data_t *pgdat, int order, gfp_t gfp_mask, @@ -1588,7 +1588,8 @@ void count_memcg_event_mm(struct mm_struct *mm, enum vm_event_item idx) { } -static inline void split_page_memcg(struct page *head, unsigned int nr) +static inline void split_page_memcg(struct page *head, unsigned int nr, + unsigned int new_nr) { } diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 81a5689806af..30e3e300c42e 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2515,7 +2515,7 @@ static void __split_huge_page(struct page *page, struct list_head *list, int i; /* complete memcg works before add pages to LRU */ - split_page_memcg(head, nr); + split_page_memcg(head, nr, 1); if (PageAnon(head) && PageSwapCache(head)) { swp_entry_t entry = { .val = page_private(head) }; diff --git a/mm/memcontrol.c b/mm/memcontrol.c index 681e7528a714..8e505201baf0 100644 --- a/mm/memcontrol.c +++ b/mm/memcontrol.c @@ -3414,7 +3414,7 @@ void obj_cgroup_uncharge(struct obj_cgroup *objcg, size_t size) /* * Because page_memcg(head) is not set on tails, set it now. */ -void split_page_memcg(struct page *head, unsigned int nr) +void split_page_memcg(struct page *head, unsigned int nr, unsigned int new_nr) { struct folio *folio = page_folio(head); struct mem_cgroup *memcg = folio_memcg(folio); @@ -3423,13 +3423,13 @@ void split_page_memcg(struct page *head, unsigned int nr) if (mem_cgroup_disabled() || !memcg) return; - for (i = 1; i < nr; i++) + for (i = new_nr; i < nr; i += new_nr) folio_page(folio, i)->memcg_data = folio->memcg_data; if (folio_memcg_kmem(folio)) - obj_cgroup_get_many(__folio_objcg(folio), nr - 1); + obj_cgroup_get_many(__folio_objcg(folio), nr / new_nr - 1); else - css_get_many(&memcg->css, nr - 1); + css_get_many(&memcg->css, nr / new_nr - 1); } #ifdef CONFIG_SWAP diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 2e72fdbdd8db..59c2b6696698 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -3531,7 +3531,7 @@ void split_page(struct page *page, unsigned int order) for (i = 1; i < (1 << order); i++) set_page_refcounted(page + i); split_page_owner(page, 1 << order); - split_page_memcg(page, 1 << order); + split_page_memcg(page, 1 << order, 1); } EXPORT_SYMBOL_GPL(split_page); From patchwork Tue Mar 21 00:48:26 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zi Yan X-Patchwork-Id: 13182070 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 969DFC761A6 for ; Tue, 21 Mar 2023 00:48:56 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E65EB6B007B; Mon, 20 Mar 2023 20:48:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id DC5D76B007D; Mon, 20 Mar 2023 20:48:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C664F6B007E; Mon, 20 Mar 2023 20:48:53 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id B8D6D6B007B for ; Mon, 20 Mar 2023 20:48:53 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 7E1E4A0D60 for ; Tue, 21 Mar 2023 00:48:53 +0000 (UTC) X-FDA: 80591070546.16.4234E39 Received: from out2-smtp.messagingengine.com (out2-smtp.messagingengine.com [66.111.4.26]) by imf03.hostedemail.com (Postfix) with ESMTP id 81AF420010 for ; Tue, 21 Mar 2023 00:48:51 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=sent.com header.s=fm2 header.b=y0X51D+L; dkim=pass header.d=messagingengine.com header.s=fm2 header.b=f2iFj0Ln; spf=pass (imf03.hostedemail.com: domain of zi.yan@sent.com designates 66.111.4.26 as permitted sender) smtp.mailfrom=zi.yan@sent.com; dmarc=pass (policy=none) header.from=sent.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679359731; a=rsa-sha256; cv=none; b=ig3J2Ao6WQ0IzWYU2b0pigfJ0K4rD+Xdzu+wIjVRQauiHQFrnMoDqaf5ct6kIcwg7bhoD2 MCn/fdupHvc0x8UIXQxpr0RDriwrTYdzrXws687ZAkaAtsO8hfY5tbdVsoFBrunoP5WEZD yGjU0FaUzVwn9YFK0SA2aK18laRbXHo= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=sent.com header.s=fm2 header.b=y0X51D+L; dkim=pass header.d=messagingengine.com header.s=fm2 header.b=f2iFj0Ln; spf=pass (imf03.hostedemail.com: domain of zi.yan@sent.com designates 66.111.4.26 as permitted sender) smtp.mailfrom=zi.yan@sent.com; dmarc=pass (policy=none) header.from=sent.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1679359731; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=vAnKio6G1pyRKqUfraKaMDB3b315AuN4QEdNLYxwdsw=; b=H9vlOh2Jq2L+k9ENIg0tyD0mwdpr7NC9KC9H9WgfBA16Ff4xfTzYzYUyqNbsZfjkqhUAmd IDwhWN2sYVIpKcbfTRSJhpGUzvuyIwINqryDNOJ7C7xw6wER0qOkdpChrFgGITwCf1CKMl 7D4PYhSaXNwo+5+4iQfHsq6yT0qQ00I= Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id 1801D5C019D; Mon, 20 Mar 2023 20:48:51 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute1.internal (MEProxy); Mon, 20 Mar 2023 20:48:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sent.com; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:reply-to:sender:subject:subject:to:to; s=fm2; t= 1679359731; x=1679446131; bh=vAnKio6G1pyRKqUfraKaMDB3b315AuN4QEd NLYxwdsw=; b=y0X51D+LQ86Kr2xYgtguu+CJsOoj3uGl1Biz+j6wtuucwB8Ul7G 1WU7SRSXYafwqUPT96W18O3eN0WP6EQ6gZJBfE7X7FtfP2DjlchcHq/YTkuOYxX0 NzareEUASz6hZ/r+xDoITDNVfwTl1xVKrVc5i9Ssgcr3UH65mCtpXHwuEg36qoNo 8p5VAs4u3KNLaBS7ErqjpQTq4F/u/FWtG+3b/sSH8E+JHm0AON8fDrdhSX7zSOOb pGdaU3jrncLEPT4epAOqz8dVLoWgrIyeU+Cbs2RGABx8EmNtYBCNSC+g+u/r5J46 wioYomd8A8HnLYPzzvXocT345MYdbUScO6Q== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:reply-to:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t= 1679359731; x=1679446131; bh=vAnKio6G1pyRKqUfraKaMDB3b315AuN4QEd NLYxwdsw=; b=f2iFj0LnxbayE/fbdq57CRZmIzsyJ82mp2azTzaS663PefKwqL+ E94QFUztNR9qdKpPQi4ujDylfOkDKesls2LeDiVSRazIzMyIqviHl1iwWZafTozc 0gvkT43UqlIKu9sAcuCWJPBuyQBgyzIuwA0rZhNdXZgYMmqQqWMKYZ/eJLVrIgWB UT1OekG3FNdH+NmxwVpnNgX4JYJrbhU8fNNr7lAHSCCYonAArOJalBP8mQQaXJWo kj7pNe1mn6Mu8vwuvKgPMhUyNS9iJCAZQDcCyd35kd4WNKm4lWLdHenocZOSV2vG +01s43WmMNNACWLTWXTyMemqRH/Jwc/5Cxg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrvdefledgvdegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvvefufffkofgjfhhrggfgsedtqhertdertddtnecuhfhrohhmpegkihcu jggrnhcuoeiiihdrhigrnhesshgvnhhtrdgtohhmqeenucggtffrrghtthgvrhhnpeegge ehudfgudduvdelheehteegledtteeiveeuhfffveekhfevueefieeijeegvdenucevlhhu shhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpeiiihdrhigrnhessh gvnhhtrdgtohhm X-ME-Proxy: Feedback-ID: iccd040f4:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 20 Mar 2023 20:48:50 -0400 (EDT) From: Zi Yan To: "Matthew Wilcox (Oracle)" , Yang Shi , Yu Zhao , linux-mm@kvack.org Cc: Zi Yan , Andrew Morton , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH 2/5] mm: page_owner: add support for splitting to any order in split page_owner. Date: Mon, 20 Mar 2023 20:48:26 -0400 Message-Id: <20230321004829.2012847-3-zi.yan@sent.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230321004829.2012847-1-zi.yan@sent.com> References: <20230321004829.2012847-1-zi.yan@sent.com> Reply-To: Zi Yan MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: 81AF420010 X-Rspamd-Server: rspam01 X-Stat-Signature: k85owg4q4sdwdotb7b9hx4csdc33ku47 X-HE-Tag: 1679359731-80351 X-HE-Meta: U2FsdGVkX18h73XW7Vult9jfY0WvjmfgSlBgYX8gH25knElllw8vX3HXHaHXEc4YMVPSNZiEl5JyJjIueJ28Y8QeSJ9Kye5mvGBiY0h4XhgeW0R7ZBJ0GGF+T2wP2kwjar+DHq0nuApatCgWi3+0N8U8LnSm1C4ygAGFpFvjyrShkI4oJhw3xRwXTtPFSIVsE/RLe+TeYGExYAw3cfrPshytSYcW1eJYrRVvcng60isGWeMPedzXERYKPuNHIXU3vi4k8r4VbMfpGcELuqzW/5/4OgmQVT9OzJ49DVSObOvJV4LwANnsyv3zBAE8s/+/nwU3cuao4RSpv2c7X+Bsvk/znHudJPDHOOTNHnvtHrrUyrcR5foBUXqeRBgLhJs+H2dFXvsA1iX+OshmK7Pme1lguolUxiqVzG8peBQpvU9GwsIWruru24FzTPnMpOkJfXb//aoz81snK9T0DASH4d40SgasuJoXfBOqK9iIaEEzdgsicFBJ06yPvEygz8gUI+8HKPCoXsAmKSG2T6MDkKfDHR/BEnrtcqg7ei1HkP2XFEzFZCPai8UCsGBpDj6w5YGoe+4ixr4pTlyMM3l8n5HvX1hNlJmQJAaRuOHMuQ/CJ0XsQS7VrnBKSi0Y/fsNaN0SpWiheyw8LLJ7x+8GgEfQ+vRqdtHnRoGOUmPkORhdVGw3Dw22BM+/txql/ORtiA0jU1Np614lrKurFF8XYIN3E9behuvQGku0BMbwjtWe/bwwxxHv58qHxsxP3ygoX3OWAOzI5IOMat7pDhKP1Z+JCcnOjuO6DkjT3SVy9hxY3FqZUHwCYxUPt/r4vZhVBw6JPQMxUi79PJRGnppZ9N7kiIk5iDLG4UpJihdq4bFgot5LKL+oddnUuQpxvIcjo+ZB7LrZAIeqQU6ogAAda6jhTWakVepPp/ZSMWsRCpP3vRRacaEvu1qNBK/6i7rL49RBXCvm2eQYHl68H5F oXoWwYE0 hODJ7Pb/QFOg/g2VjoEoi22Gg1NSknpWFILJDLKbWmcFyiAGVisZf13dpNUhakoWdAErQwrld4oO8Kx02tKwV+2FXvWwispBQqaixfJDy77iLKzeVU3htXH1ASpaZJCV+bBqHiOoDRQQYsvgLeRYcaYjEl0f10R8BQRh+KkoWooIzjpIlBKUZQ9u6dVYxEXu+GZ7IZIqmfuPkZx/VfPngo3KxI37EtKypQFLF6JqcD+GhjnkcQlp1sxPOiNYiHbjE/Ef/C2teJ8vhnNJFsISWDd6nYb1o1/xTZ9XxKOWa9YcMYoK5/WD4HL94S78fa1DOK20UEB1WU0PSs2zgBeOzp+lYBAetHSvBSHrE+QPYtywhjWDGni0nLTD+s0fpUElXE4kpR9oESqAAAFfp6Btj5LRDY1OSbDP7sVn4wlWgqxlkQkLYuO8pa35s7gMC5IOnUTcTS5RIhv9nuTHQC/3od1MjmtaBMM8m0xfN/BGRxd1QsfgnwY3BhtDl2Qie8+RhmAzxMQV5LbL8p1BsBml+VRWZ4w== 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: Zi Yan It adds a new_order parameter to set new page order in page owner and uses old_order instead of nr to make the parameters look consistent. It prepares for upcoming changes to support split huge page to any lower order. Signed-off-by: Zi Yan --- include/linux/page_owner.h | 12 +++++++----- mm/huge_memory.c | 3 ++- mm/page_alloc.c | 6 +++--- mm/page_owner.c | 11 ++++++----- 4 files changed, 18 insertions(+), 14 deletions(-) diff --git a/include/linux/page_owner.h b/include/linux/page_owner.h index 119a0c9d2a8b..67d98de3d5a8 100644 --- a/include/linux/page_owner.h +++ b/include/linux/page_owner.h @@ -11,7 +11,8 @@ extern struct page_ext_operations page_owner_ops; extern void __reset_page_owner(struct page *page, unsigned short order); extern void __set_page_owner(struct page *page, unsigned short order, gfp_t gfp_mask); -extern void __split_page_owner(struct page *page, unsigned int nr); +extern void __split_page_owner(struct page *page, unsigned short old_order, + unsigned short new_order); extern void __folio_copy_owner(struct folio *newfolio, struct folio *old); extern void __set_page_owner_migrate_reason(struct page *page, int reason); extern void __dump_page_owner(const struct page *page); @@ -31,10 +32,11 @@ static inline void set_page_owner(struct page *page, __set_page_owner(page, order, gfp_mask); } -static inline void split_page_owner(struct page *page, unsigned int nr) +static inline void split_page_owner(struct page *page, unsigned short old_order, + unsigned short new_order) { if (static_branch_unlikely(&page_owner_inited)) - __split_page_owner(page, nr); + __split_page_owner(page, old_order, new_order); } static inline void folio_copy_owner(struct folio *newfolio, struct folio *old) { @@ -56,11 +58,11 @@ static inline void reset_page_owner(struct page *page, unsigned short order) { } static inline void set_page_owner(struct page *page, - unsigned int order, gfp_t gfp_mask) + unsigned short order, gfp_t gfp_mask) { } static inline void split_page_owner(struct page *page, - unsigned short order) + unsigned short old_order, unsigned short new_order) { } static inline void folio_copy_owner(struct folio *newfolio, struct folio *folio) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 30e3e300c42e..710189885402 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2511,6 +2511,7 @@ static void __split_huge_page(struct page *page, struct list_head *list, struct lruvec *lruvec; struct address_space *swap_cache = NULL; unsigned long offset = 0; + unsigned int order = thp_order(head); unsigned int nr = thp_nr_pages(head); int i; @@ -2556,7 +2557,7 @@ static void __split_huge_page(struct page *page, struct list_head *list, unlock_page_lruvec(lruvec); /* Caller disabled irqs, so they are still disabled here */ - split_page_owner(head, nr); + split_page_owner(head, order, 0); /* See comment in __split_huge_page_tail() */ if (PageAnon(head)) { diff --git a/mm/page_alloc.c b/mm/page_alloc.c index 59c2b6696698..ec85562865fb 100644 --- a/mm/page_alloc.c +++ b/mm/page_alloc.c @@ -3530,7 +3530,7 @@ void split_page(struct page *page, unsigned int order) for (i = 1; i < (1 << order); i++) set_page_refcounted(page + i); - split_page_owner(page, 1 << order); + split_page_owner(page, order, 0); split_page_memcg(page, 1 << order, 1); } EXPORT_SYMBOL_GPL(split_page); @@ -5746,8 +5746,8 @@ static void *make_alloc_exact(unsigned long addr, unsigned int order, struct page *page = virt_to_page((void *)addr); struct page *last = page + nr; - split_page_owner(page, 1 << order); - split_page_memcg(page, 1 << order); + split_page_owner(page, 1 << order, 1); + split_page_memcg(page, 1 << order, 1); while (page < --last) set_page_refcounted(last); diff --git a/mm/page_owner.c b/mm/page_owner.c index 31169b3e7f06..33d1b6efe6a6 100644 --- a/mm/page_owner.c +++ b/mm/page_owner.c @@ -211,19 +211,20 @@ void __set_page_owner_migrate_reason(struct page *page, int reason) page_ext_put(page_ext); } -void __split_page_owner(struct page *page, unsigned int nr) +void __split_page_owner(struct page *page, unsigned short old_order, + unsigned short new_order) { - int i; + int i, old_nr = 1 << old_order, new_nr = 1 << new_order; struct page_ext *page_ext = page_ext_get(page); struct page_owner *page_owner; if (unlikely(!page_ext)) return; - for (i = 0; i < nr; i++) { + for (i = 0; i < old_nr; i += new_nr) { + page_ext = lookup_page_ext(page + i); page_owner = get_page_owner(page_ext); - page_owner->order = 0; - page_ext = page_ext_next(page_ext); + page_owner->order = new_order; } page_ext_put(page_ext); } From patchwork Tue Mar 21 00:48:27 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zi Yan X-Patchwork-Id: 13182073 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id C7ED7C77B61 for ; Tue, 21 Mar 2023 00:49:01 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B31B76B0082; Mon, 20 Mar 2023 20:48:57 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id ADF9B6B0083; Mon, 20 Mar 2023 20:48:57 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 959796B0085; Mon, 20 Mar 2023 20:48:57 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 7E7A26B0082 for ; Mon, 20 Mar 2023 20:48:57 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 539E8140FC9 for ; Tue, 21 Mar 2023 00:48:57 +0000 (UTC) X-FDA: 80591070714.20.8E08616 Received: from out2-smtp.messagingengine.com (out2-smtp.messagingengine.com [66.111.4.26]) by imf07.hostedemail.com (Postfix) with ESMTP id 44CC040013 for ; Tue, 21 Mar 2023 00:48:55 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=sent.com header.s=fm2 header.b=5niVmPz2; dkim=pass header.d=messagingengine.com header.s=fm2 header.b=VgRaX4zO; spf=pass (imf07.hostedemail.com: domain of zi.yan@sent.com designates 66.111.4.26 as permitted sender) smtp.mailfrom=zi.yan@sent.com; dmarc=pass (policy=none) header.from=sent.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1679359735; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=FDz7l3LvqZq3Jky3lnWwH8yTFnKX+aXCW8SXEMr4gJE=; b=5LoCuNsiv9Nc06biU9WDIpPwZP1sBwF35U+OBSVsFECmti2wFVSQoK9sBOYbCIEigvS4bI ST1GNRfwyYbHUFfnqTHVaQ+osfI4kMbZeL3xHSfuBgqhuYVcNNDQvu9usNdmDWZ1L3h+yt jpngmSOGQ4FYOqnM2rvCpPzuR7OjqhQ= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=sent.com header.s=fm2 header.b=5niVmPz2; dkim=pass header.d=messagingengine.com header.s=fm2 header.b=VgRaX4zO; spf=pass (imf07.hostedemail.com: domain of zi.yan@sent.com designates 66.111.4.26 as permitted sender) smtp.mailfrom=zi.yan@sent.com; dmarc=pass (policy=none) header.from=sent.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679359735; a=rsa-sha256; cv=none; b=bfK7nCVszP5Y1tXfzIKI/sOMAksNbtq0O6iPh8OIiIK4MWthC1weK4uDco0qwLW8/rpSXg EL34M3xEnVC9x5CgKdVDarhdgAKsj9Q/URP5h5flPsL/PDHg8Z8LsPOan7sSjyeOpSNfaK 8VKt5gWeKKn1kbkRGbbKdiWQ34u7BYA= Received: from compute2.internal (compute2.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id AD2385C019E; Mon, 20 Mar 2023 20:48:51 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute2.internal (MEProxy); Mon, 20 Mar 2023 20:48:51 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sent.com; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:reply-to:sender:subject:subject:to:to; s=fm2; t= 1679359731; x=1679446131; bh=FDz7l3LvqZq3Jky3lnWwH8yTFnKX+aXCW8S XEMr4gJE=; b=5niVmPz2aeZ0HtCKbiQvOyB0pMpJh8W6P4WiO3IJI0e7KXhbDWv f1jLRmSoY+37hjllFrfS5QGKQPlPk9Y6xWYj9Zuc889dppITzZZVZGGdN7jaKM83 nyKcrvS2uG+BB9+IjKGwaEBAzHXapjFg+Tr5sO4yfJTp5+QwXAgT/t6/3RN2I1zh cAHJ0pecw3okshNGhxFe8k2sl5xA5iXZ8tUkXUagvEBmke+KYlkuL6Qsgi5UglAY sULcpxzyyQ5xtAR8tviFv0T06NL7mCBb6B1bkF7iAeo5xCPPu1Wkc8z/fJ5xBQA6 oiqdCT6+nIYM8UUH29HW1rcn31IYO7pOsTA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:reply-to:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t= 1679359731; x=1679446131; bh=FDz7l3LvqZq3Jky3lnWwH8yTFnKX+aXCW8S XEMr4gJE=; b=VgRaX4zOQO6ozdqdwOOQI8XAyWweKsJaRQgDpZk857Tlat5uQXz Y5jWN5MThbJgZw78hkbT9B0fmobRSFP4TDPOsQE2fvCcQJOi4csAfFI6j9DFHl1Y WxdnJUWShxOZK29RcmlOBI9wm6oQ91tMrHVlWz7vtcSHhONFSwP7bqBK3Hzn1tNJ 9XiMtjsADoGBAkRkcChzkNq7rxSUsKXQ6tMF5tP//svI1RpyfygxjB+3BLEmb4n6 tl/L4c3W7jH86BAhT66nwJyn05GlmsL3ZKc6L/Xnm1cluhFVg1S/274La4dhs5w6 48f2FbVvor7EJHRnmcLDxIU95KvQvopVmCA== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrvdefledgvdegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvvefufffkofgjfhhrggfgsedtqhertdertddtnecuhfhrohhmpegkihcu jggrnhcuoeiiihdrhigrnhesshgvnhhtrdgtohhmqeenucggtffrrghtthgvrhhnpeegge ehudfgudduvdelheehteegledtteeiveeuhfffveekhfevueefieeijeegvdenucevlhhu shhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpeiiihdrhigrnhessh gvnhhtrdgtohhm X-ME-Proxy: Feedback-ID: iccd040f4:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 20 Mar 2023 20:48:51 -0400 (EDT) From: Zi Yan To: "Matthew Wilcox (Oracle)" , Yang Shi , Yu Zhao , linux-mm@kvack.org Cc: Zi Yan , Andrew Morton , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH 3/5] mm: thp: split huge page to any lower order pages. Date: Mon, 20 Mar 2023 20:48:27 -0400 Message-Id: <20230321004829.2012847-4-zi.yan@sent.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230321004829.2012847-1-zi.yan@sent.com> References: <20230321004829.2012847-1-zi.yan@sent.com> Reply-To: Zi Yan MIME-Version: 1.0 X-Stat-Signature: 9s51w9kr9nq44tfgkq6kw1k13tpqm54t X-Rspam-User: X-Rspamd-Queue-Id: 44CC040013 X-Rspamd-Server: rspam06 X-HE-Tag: 1679359735-14109 X-HE-Meta: U2FsdGVkX18iv4iPAYVu6p7T9e76ErTv67DIJR4mrm6X/TDFh4sk5Z4YtiOR9ZmjaGPxRTkXyM7P0PZuS37sp5tRzYfDR42KqBSw9ve8Ncdaex9JbwQHdb3gaJ9gJ9B2YCi74XujZ1RE1948W+6yz+H7Cx+ZI8zXB0Ex2uIovyrnAVJroRITfKr1WyQ4FLUw0ner/0NePDDIn2jhWYtL5+0eseUdX8zuPVxHh+5+DOJw6eKGEoCgmB84y76E69EFQrzPs1JNE5pW/MfsV95+vxE3amaFeEGepkyW2YqrjIkiaG8neErKBUgzzSgy5c5VzLMpVxOrR0Y0v9aqxjw5sNOUI2hp2r6eF/415yuw9YHSUuXHHUpKeXD6WMNckob8E34fxdtsRF0iOCvNSYx4ry/1gBNHMuZb1iXxtZaa6ocs50U+5AzjpZd90tJywbg07U5o/HMQHvgR1zT+Wj+f8fB7rRweXR96iCXzO+D7JjgtrABDBDjA3ife1TGCji6F7L2J0EbPfP/KmQP4ioQQPJ7dWl4qqU4ORjYLndqwpQC5ZU6eXgbayvqYJLEtBy76Upzsy0mefcD6wksWqwM3ItLWakP013IKK751I4d95UG6wl4qwPbXJ0CAqqd6l4y/SBcmAgQup2HxJycga/dYoluJzjfz6DOeaWvVGoLzIWuhMTWqljUTbTesXmgON+ocDXK919lXH+CCqBYM2CgAqncIWXImgjnoLnbDEPjZZ6HxpYMIyyD8t63xnwbafpdv5XdsdnltGQqboBG/VPtZHVkfPSUNTeoRtfvF9cCCSO14qY4iAhrRtmJj9/hCPzpDHsFJAn55FpDD34QisH8mtEgNnmvz4xKnjlB8UxkT1ezuqbzibG1TMVNRntecBc52hfrwaS7hzldrCp0VWYMsvxsUv7gPip0AlJHoyb3zWR2oMbAYLKheI+QW/YRCPmt1jGdc1U/yUaOdatO1mVK ODYcx5Uq 4uXhW4XhBTpRzxHSyZ7VZD9cDMJ+sFBXuhRte3E8jYQ2PHKcIVV8ZgX++dOxTaNWBxek1IDkhYywjWq61LhsV7fRib88+aawW32C4nNTq1lKFputAOy1ItC28sO7Plj123rIHl8axJBsGKAS0gjjWnYNzfwVjlRdAbOnmYg3iBzsFb+88RCNashw+U+QkuW6khdWKzrw01E+rkwMj/WJ3nArS0Y2+j8JPZChIvbfCXL2IRoVBt8moWQMZXSW0n+hE7krCAXLNh9L7RbBLnXsmJmfAUEe1xeKrxxSguAXkMXFTgCk6qh3RVPvonnAamHlPgS2f/Hw8hWftGpWr14YOlrh3YlNjmyA0lWb1UrgdeWaJ4zoaFheRqnx8uvWlnhoon23btYA0jnDTs/hTlNN7IRbqhwtG5KnYvISj 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: Zi Yan To split a THP to any lower order pages, we need to reform THPs on subpages at given order and add page refcount based on the new page order. Also we need to reinitialize page_deferred_list after removing the page from the split_queue, otherwise a subsequent split will see list corruption when checking the page_deferred_list again. It has many uses, like minimizing the number of pages after truncating a huge pagecache page. For anonymous THPs, we can only split them to order-0 like before until we add support for any size anonymous THPs. Signed-off-by: Zi Yan --- include/linux/huge_mm.h | 10 ++-- mm/huge_memory.c | 103 +++++++++++++++++++++++++++++----------- 2 files changed, 82 insertions(+), 31 deletions(-) diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index 20284387b841..32c91e1b59cd 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -147,10 +147,11 @@ void prep_transhuge_page(struct page *page); void free_transhuge_page(struct page *page); bool can_split_folio(struct folio *folio, int *pextra_pins); -int split_huge_page_to_list(struct page *page, struct list_head *list); +int split_huge_page_to_list_to_order(struct page *page, struct list_head *list, + unsigned int new_order); static inline int split_huge_page(struct page *page) { - return split_huge_page_to_list(page, NULL); + return split_huge_page_to_list_to_order(page, NULL, 0); } void deferred_split_folio(struct folio *folio); @@ -297,7 +298,8 @@ can_split_folio(struct folio *folio, int *pextra_pins) return false; } static inline int -split_huge_page_to_list(struct page *page, struct list_head *list) +split_huge_page_to_list_to_order(struct page *page, struct list_head *list, + unsigned int new_order) { return 0; } @@ -397,7 +399,7 @@ static inline bool thp_migration_supported(void) static inline int split_folio_to_list(struct folio *folio, struct list_head *list) { - return split_huge_page_to_list(&folio->page, list); + return split_huge_page_to_list_to_order(&folio->page, list, 0); } static inline int split_folio(struct folio *folio) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 710189885402..f119b9be33f2 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -2359,11 +2359,13 @@ void vma_adjust_trans_huge(struct vm_area_struct *vma, static void unmap_folio(struct folio *folio) { - enum ttu_flags ttu_flags = TTU_RMAP_LOCKED | TTU_SPLIT_HUGE_PMD | - TTU_SYNC; + enum ttu_flags ttu_flags = TTU_RMAP_LOCKED | TTU_SYNC; VM_BUG_ON_FOLIO(!folio_test_large(folio), folio); + if (folio_order(folio) >= HPAGE_PMD_ORDER) + ttu_flags |= TTU_SPLIT_HUGE_PMD; + /* * Anon pages need migration entries to preserve them, but file * pages can simply be left unmapped, then faulted back on demand. @@ -2395,7 +2397,6 @@ static void lru_add_page_tail(struct page *head, struct page *tail, struct lruvec *lruvec, struct list_head *list) { VM_BUG_ON_PAGE(!PageHead(head), head); - VM_BUG_ON_PAGE(PageCompound(tail), head); VM_BUG_ON_PAGE(PageLRU(tail), head); lockdep_assert_held(&lruvec->lru_lock); @@ -2416,9 +2417,10 @@ static void lru_add_page_tail(struct page *head, struct page *tail, } static void __split_huge_page_tail(struct page *head, int tail, - struct lruvec *lruvec, struct list_head *list) + struct lruvec *lruvec, struct list_head *list, unsigned int new_order) { struct page *page_tail = head + tail; + unsigned long compound_head_flag = new_order ? (1L << PG_head) : 0; VM_BUG_ON_PAGE(atomic_read(&page_tail->_mapcount) != -1, page_tail); @@ -2451,6 +2453,7 @@ static void __split_huge_page_tail(struct page *head, int tail, (1L << PG_arch_3) | #endif (1L << PG_dirty) | + compound_head_flag | LRU_GEN_MASK | LRU_REFS_MASK)); /* ->mapping in first and second tail page is replaced by other uses */ @@ -2483,10 +2486,15 @@ static void __split_huge_page_tail(struct page *head, int tail, * which needs correct compound_head(). */ clear_compound_head(page_tail); + if (new_order) { + prep_compound_page(page_tail, new_order); + prep_transhuge_page(page_tail); + } /* Finally unfreeze refcount. Additional reference from page cache. */ - page_ref_unfreeze(page_tail, 1 + (!PageAnon(head) || - PageSwapCache(head))); + page_ref_unfreeze(page_tail, 1 + ((!PageAnon(head) || + PageSwapCache(head)) ? + thp_nr_pages(page_tail) : 0)); if (page_is_young(head)) set_page_young(page_tail); @@ -2504,7 +2512,7 @@ static void __split_huge_page_tail(struct page *head, int tail, } static void __split_huge_page(struct page *page, struct list_head *list, - pgoff_t end) + pgoff_t end, unsigned int new_order) { struct folio *folio = page_folio(page); struct page *head = &folio->page; @@ -2513,10 +2521,11 @@ static void __split_huge_page(struct page *page, struct list_head *list, unsigned long offset = 0; unsigned int order = thp_order(head); unsigned int nr = thp_nr_pages(head); + unsigned int new_nr = 1 << new_order; int i; /* complete memcg works before add pages to LRU */ - split_page_memcg(head, nr, 1); + split_page_memcg(head, nr, new_nr); if (PageAnon(head) && PageSwapCache(head)) { swp_entry_t entry = { .val = page_private(head) }; @@ -2531,14 +2540,14 @@ static void __split_huge_page(struct page *page, struct list_head *list, ClearPageHasHWPoisoned(head); - for (i = nr - 1; i >= 1; i--) { - __split_huge_page_tail(head, i, lruvec, list); + for (i = nr - new_nr; i >= new_nr; i -= new_nr) { + __split_huge_page_tail(head, i, lruvec, list, new_order); /* Some pages can be beyond EOF: drop them from page cache */ if (head[i].index >= end) { struct folio *tail = page_folio(head + i); if (shmem_mapping(head->mapping)) - shmem_uncharge(head->mapping->host, 1); + shmem_uncharge(head->mapping->host, new_nr); else if (folio_test_clear_dirty(tail)) folio_account_cleaned(tail, inode_to_wb(folio->mapping->host)); @@ -2548,29 +2557,38 @@ static void __split_huge_page(struct page *page, struct list_head *list, __xa_store(&head->mapping->i_pages, head[i].index, head + i, 0); } else if (swap_cache) { + /* + * split anonymous THPs (including swapped out ones) to + * non-zero order not supported + */ + VM_WARN_ONCE(new_order, + "Split swap-cached anon folio to non-0 order not supported"); __xa_store(&swap_cache->i_pages, offset + i, head + i, 0); } } - ClearPageCompound(head); + if (!new_order) + ClearPageCompound(head); + else + set_compound_order(head, new_order); unlock_page_lruvec(lruvec); /* Caller disabled irqs, so they are still disabled here */ - split_page_owner(head, order, 0); + split_page_owner(head, order, new_order); /* See comment in __split_huge_page_tail() */ if (PageAnon(head)) { /* Additional pin to swap cache */ if (PageSwapCache(head)) { - page_ref_add(head, 2); + page_ref_add(head, 1 + new_nr); xa_unlock(&swap_cache->i_pages); } else { page_ref_inc(head); } } else { /* Additional pin to page cache */ - page_ref_add(head, 2); + page_ref_add(head, 1 + new_nr); xa_unlock(&head->mapping->i_pages); } local_irq_enable(); @@ -2583,7 +2601,14 @@ static void __split_huge_page(struct page *page, struct list_head *list, split_swap_cluster(entry); } - for (i = 0; i < nr; i++) { + /* + * set page to its compound_head when split to THPs, so that GUP pin and + * PG_locked are transferred to the right after-split page + */ + if (new_order) + page = compound_head(page); + + for (i = 0; i < nr; i += new_nr) { struct page *subpage = head + i; if (subpage == page) continue; @@ -2617,29 +2642,31 @@ bool can_split_folio(struct folio *folio, int *pextra_pins) } /* - * This function splits huge page into normal pages. @page can point to any - * subpage of huge page to split. Split doesn't change the position of @page. + * This function splits huge page into pages in @new_order. @page can point to + * any subpage of huge page to split. Split doesn't change the position of + * @page. * * Only caller must hold pin on the @page, otherwise split fails with -EBUSY. * The huge page must be locked. * * If @list is null, tail pages will be added to LRU list, otherwise, to @list. * - * Both head page and tail pages will inherit mapping, flags, and so on from - * the hugepage. + * Pages in new_order will inherit mapping, flags, and so on from the hugepage. * - * GUP pin and PG_locked transferred to @page. Rest subpages can be freed if - * they are not mapped. + * GUP pin and PG_locked transferred to @page or the compound page @page belongs + * to. Rest subpages can be freed if they are not mapped. * * Returns 0 if the hugepage is split successfully. * Returns -EBUSY if the page is pinned or if anon_vma disappeared from under * us. */ -int split_huge_page_to_list(struct page *page, struct list_head *list) +int split_huge_page_to_list_to_order(struct page *page, struct list_head *list, + unsigned int new_order) { struct folio *folio = page_folio(page); struct deferred_split *ds_queue = get_deferred_split_queue(folio); - XA_STATE(xas, &folio->mapping->i_pages, folio->index); + /* reset xarray order to new order after split */ + XA_STATE_ORDER(xas, &folio->mapping->i_pages, folio->index, new_order); struct anon_vma *anon_vma = NULL; struct address_space *mapping = NULL; int extra_pins, ret; @@ -2649,6 +2676,18 @@ int split_huge_page_to_list(struct page *page, struct list_head *list) VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); VM_BUG_ON_FOLIO(!folio_test_large(folio), folio); + /* Cannot split THP to order-1 (no order-1 THPs) */ + if (new_order == 1) { + VM_WARN_ONCE(1, "Cannot split to order-1 folio"); + return -EINVAL; + } + + /* Split anonymous folio to non-zero order not support */ + if (folio_test_anon(folio) && new_order) { + VM_WARN_ONCE(1, "Split anon folio to non-0 order not support"); + return -EINVAL; + } + is_hzp = is_huge_zero_page(&folio->page); VM_WARN_ON_ONCE_FOLIO(is_hzp, folio); if (is_hzp) @@ -2744,7 +2783,13 @@ int split_huge_page_to_list(struct page *page, struct list_head *list) if (folio_ref_freeze(folio, 1 + extra_pins)) { if (!list_empty(&folio->_deferred_list)) { ds_queue->split_queue_len--; - list_del(&folio->_deferred_list); + /* + * Reinitialize page_deferred_list after removing the + * page from the split_queue, otherwise a subsequent + * split will see list corruption when checking the + * page_deferred_list. + */ + list_del_init(&folio->_deferred_list); } spin_unlock(&ds_queue->split_queue_lock); if (mapping) { @@ -2754,14 +2799,18 @@ int split_huge_page_to_list(struct page *page, struct list_head *list) if (folio_test_swapbacked(folio)) { __lruvec_stat_mod_folio(folio, NR_SHMEM_THPS, -nr); - } else { + } else if (!new_order) { + /* + * Decrease THP stats only if split to normal + * pages + */ __lruvec_stat_mod_folio(folio, NR_FILE_THPS, -nr); filemap_nr_thps_dec(mapping); } } - __split_huge_page(page, list, end); + __split_huge_page(page, list, end, new_order); ret = 0; } else { spin_unlock(&ds_queue->split_queue_lock); From patchwork Tue Mar 21 00:48:28 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zi Yan X-Patchwork-Id: 13182072 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 1C0DAC761A6 for ; Tue, 21 Mar 2023 00:49:00 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id EB2BB6B007E; Mon, 20 Mar 2023 20:48:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E14896B0080; Mon, 20 Mar 2023 20:48:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D0D1D6B0081; Mon, 20 Mar 2023 20:48:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id B81FA6B0080 for ; Mon, 20 Mar 2023 20:48:55 -0400 (EDT) Received: from smtpin12.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id C56B216020D for ; Tue, 21 Mar 2023 00:48:54 +0000 (UTC) X-FDA: 80591070588.12.BCEC0DF Received: from out2-smtp.messagingengine.com (out2-smtp.messagingengine.com [66.111.4.26]) by imf18.hostedemail.com (Postfix) with ESMTP id C396F1C0005 for ; Tue, 21 Mar 2023 00:48:52 +0000 (UTC) Authentication-Results: imf18.hostedemail.com; dkim=pass header.d=sent.com header.s=fm2 header.b=TGOw0K2a; dkim=pass header.d=messagingengine.com header.s=fm2 header.b=VLrp8OER; spf=pass (imf18.hostedemail.com: domain of zi.yan@sent.com designates 66.111.4.26 as permitted sender) smtp.mailfrom=zi.yan@sent.com; dmarc=pass (policy=none) header.from=sent.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1679359732; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=n0I3rKbVmW11Ko5MdyrWnLEXy5CTcBBnWUTtQZPt4TU=; b=43Ek+j9yUJ2gYK2IRHXs3LqX1+OAzTmh0sBvd0peJNstXvSuE4NltuBegi79Gi4be5ZZ2M cvVBziar4rKHsG4LhTPhdLyo6RYudivRwIvuiNOxQ/ZmeciRUb4WdkqwVe7/YKuaMzi7gV 3exlR8mUBjVJmA3a/Qfmi9N5kpqC9ng= ARC-Authentication-Results: i=1; imf18.hostedemail.com; dkim=pass header.d=sent.com header.s=fm2 header.b=TGOw0K2a; dkim=pass header.d=messagingengine.com header.s=fm2 header.b=VLrp8OER; spf=pass (imf18.hostedemail.com: domain of zi.yan@sent.com designates 66.111.4.26 as permitted sender) smtp.mailfrom=zi.yan@sent.com; dmarc=pass (policy=none) header.from=sent.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679359732; a=rsa-sha256; cv=none; b=IPFup0r/qgS08NLSSJkDdS2dEtEro3XfVjYyv2BAZiItgOraWCPk3KkuSQe+5pHbBOinwB jRCsPZ8vlRKgZomUQsN/E1t/W5MvRxzpn9IaQPkALwiA8tSSU/F7p77qr7Z7yGm+pmRxDc 8YwlDgmzmGAFzOpRqQtkysrWekqIZX0= Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 454555C005B; Mon, 20 Mar 2023 20:48:52 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Mon, 20 Mar 2023 20:48:52 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sent.com; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:reply-to:sender:subject:subject:to:to; s=fm2; t= 1679359732; x=1679446132; bh=n0I3rKbVmW11Ko5MdyrWnLEXy5CTcBBnWUT tQZPt4TU=; b=TGOw0K2al2eY7sLD74hzDSD2ANtWUXGox+zhPeiE6P/ZNRvUeRK 4Q0E1OSDty/ycWijhLtITpkdOGOarjXiu5fjbrQ3nWDAN+QJGLTONr+uF1Q2Uc3l Q48w6zo1cQIsT1e/iFjfwnL9rPqppkTFZZ8DR7FAaqythtnQqFWf28twSJ818+pW MxoA8/Q1YQGKfE+sRpYG00S3FtH6s2dn1TImcA2bdwUMcy6TSJlcyjawq5pX/rXG byesOejoHcwfGEod9ItRRpSOFSr+WvxW7oMQjUxOh8TFlU0NGfbDID/+AwGhAJyS kP7Shwv+niUFBCz3C7lngIgEvcg9nOfO/FQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:reply-to:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t= 1679359732; x=1679446132; bh=n0I3rKbVmW11Ko5MdyrWnLEXy5CTcBBnWUT tQZPt4TU=; b=VLrp8OERc6ago/IJ0cIfqE5ueb4bd17BUSOXX+/PiCWaLJcJjyd Su89Z+er3PRdhytvTZUnnTTXLq2pxmHZaKWGlhXwDW/j8avGXV1RqvqPsCbJA5LR XaSrQJ/kltnPp88OMBoTfnEq4ZIDTD4Uk3aHbzEs18a8DGB4ky8tc5tH+QiWV8ks fUSY5GIBk0XfQhIXNIqUOfjkUN+uNz58xYJ4r/9dJha7JCRfeHJ4UP1FQnRI4U86 Tq+yJY7w7c05XSu+4+cpgPEXo9L9Kn2oCE15QcC/RtukdTvDwenI6RmJuxwC80/c zrT0gjqbKoA6/aoyfrG4NIkliT0oM4AxUgQ== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrvdefledgvdegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvvefufffkofgjfhhrggfgsedtqhertdertddtnecuhfhrohhmpegkihcu jggrnhcuoeiiihdrhigrnhesshgvnhhtrdgtohhmqeenucggtffrrghtthgvrhhnpeegge ehudfgudduvdelheehteegledtteeiveeuhfffveekhfevueefieeijeegvdenucevlhhu shhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpeiiihdrhigrnhessh gvnhhtrdgtohhm X-ME-Proxy: Feedback-ID: iccd040f4:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 20 Mar 2023 20:48:51 -0400 (EDT) From: Zi Yan To: "Matthew Wilcox (Oracle)" , Yang Shi , Yu Zhao , linux-mm@kvack.org Cc: Zi Yan , Andrew Morton , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH 4/5] mm: truncate: split huge page cache page to a non-zero order if possible. Date: Mon, 20 Mar 2023 20:48:28 -0400 Message-Id: <20230321004829.2012847-5-zi.yan@sent.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230321004829.2012847-1-zi.yan@sent.com> References: <20230321004829.2012847-1-zi.yan@sent.com> Reply-To: Zi Yan MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam03 X-Stat-Signature: 9mcws5z857ackk8tuksjczoe43kwfzk9 X-Rspamd-Queue-Id: C396F1C0005 X-HE-Tag: 1679359732-22843 X-HE-Meta: U2FsdGVkX19uZuU7Yr6yQwsTi3tzyQBkhU5y2kj4VuOWQ9MocHVQxfR6bdPS/kSIqMvytMxFC1QPuOZV44u2ZR7L/SfMGUe0ETsGkbLGGrhRt2ccWXTVA4H6Sk3Ds3MDcgNfM4Z0sXZbDzw7SNHiy2e9shve/u7KVGkmFM2sETBuOKEASnbRJuY0tuOAb1HDiQ8ftBO/md/tmnTXVxLTez+nrJ0o4/Hj9hR+0o1ZMWXF97s/dt9Hh/4r0UKjYfDQEZudlC9fHUAg91FuUp3lcnlBu944d7UgsfGhtaL1p1jW9PlcSMaZYetl7yGsevXqN9zn7J0kYbP/t8IBN7ZnnVy3hI8Mja2Amf4bpVLfqB+5Y1K79RTfe9DWdgmEuHCzyazMTZyNh/6IDAlF8KuGHkEe+FO5wR7OJmdpNEvEVFfSLSkvtyVvx1KsRzr0iFU9ZejudKsCBaUlpjBP+ujoTA0P2p+3Avz65+U1IoC7uoQ51iwqRMmzF66B7wzdTdu2f+L4CgJf5/mb4sHPUvzFva8Yg2IMJoN4T5ae64wIAoMsxnSIvnmdO7WNGCnfyGGTcCB6B1qpGQCZ9Yt6/wxQ/HWTokpRT5NOWyIkLO92Bhf6x6J1G/h8f3cXsYsihNLr7UsBTz3zqrhkPz1taQCdV2SBwvkKDDQ0D3Uzl3gF3GyQaF5aL+vjDzDnmA2e3zgorSBK8V4Ena/GWhZv/r0z9Lkf6NtB83OLX7qMgQjyKcHYYLJVjmYdPYVs/ckSNbVS2Yab8z2AFLzQZCtvFnEWWRamkau5mIqR1909JFkZ1loPyb9MRwMeo7LVS/CQLWu62zMrrCuzHpksLuEMbCEUXbxXWAAT/jUWATMa146fwGENkPC0m4apPc6KjG9sHzeZ63EN6eLFBZtkZak9Btr4X60bnF+w25RnldNN8R6TXPuyiOU1pRjheiHd8gWMkk86/Y6KxLFhl7jpZ8yr3Qd YcoYIqAc y4TWi1mJavxhLY90TeywmvJzZylWuLc/bs83PwSgehLJmDpPrBgoyDffgP+Pitfwp7r0WNK2OeM/i0oalzF4mbGjGaq3fI3w/8wYA61wYSFA5XYYAGwYX7AYnB8DQGH+VYBtrsejoHfb7773NsY/jQFU17tTeois88WVt4U+qIvJP4tVm7vuJiKX2E+AQTiLnwGQd6k/CdYkG5vD9BdOKZL7TxagPOV5dbHm8KQ1HDk+QEHbYQGkzlEfA+WHLBZkq+vxyECqjRf1c1de0dr1OaO7BzJNbAMY6WbheiJIdfv2tcM1XeN8DSizuWTS4N/B1F8zqI6M7s48e5w90m65SOA5NlgnBHKbF4mFRFPHDJgSgG3U9JjfgBb2IJqdeS3o/6pceWUJRt4qA4SGwKcOgoTSw/kwHbWpHTwWc 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: Zi Yan To minimize the number of pages after a huge page truncation, we do not need to split it all the way down to order-0. The huge page has at most three parts, the part before offset, the part to be truncated, the part remaining at the end. Find the greatest common divisor of them to calculate the new page order from it, so we can split the huge page to this order and keep the remaining pages as large and as few as possible. Signed-off-by: Zi Yan --- mm/truncate.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/mm/truncate.c b/mm/truncate.c index 86de31ed4d32..817efd5e94b4 100644 --- a/mm/truncate.c +++ b/mm/truncate.c @@ -22,6 +22,7 @@ #include /* grr. try_to_release_page */ #include #include +#include #include "internal.h" /* @@ -211,7 +212,8 @@ int truncate_inode_folio(struct address_space *mapping, struct folio *folio) bool truncate_inode_partial_folio(struct folio *folio, loff_t start, loff_t end) { loff_t pos = folio_pos(folio); - unsigned int offset, length; + unsigned int offset, length, remaining; + unsigned int new_order = folio_order(folio); if (pos < start) offset = start - pos; @@ -222,6 +224,7 @@ bool truncate_inode_partial_folio(struct folio *folio, loff_t start, loff_t end) length = length - offset; else length = end + 1 - pos - offset; + remaining = folio_size(folio) - offset - length; folio_wait_writeback(folio); if (length == folio_size(folio)) { @@ -236,11 +239,25 @@ bool truncate_inode_partial_folio(struct folio *folio, loff_t start, loff_t end) */ folio_zero_range(folio, offset, length); + /* + * Use the greatest common divisor of offset, length, and remaining + * as the smallest page size and compute the new order from it. So we + * can truncate a subpage as large as possible. Round up gcd to + * PAGE_SIZE, otherwise ilog2 can give -1 when gcd/PAGE_SIZE is 0. + */ + new_order = ilog2(round_up(gcd(gcd(offset, length), remaining), + PAGE_SIZE) / PAGE_SIZE); + + /* order-1 THP not supported, downgrade to order-0 */ + if (new_order == 1) + new_order = 0; + + if (folio_has_private(folio)) folio_invalidate(folio, offset, length); if (!folio_test_large(folio)) return true; - if (split_folio(folio) == 0) + if (split_huge_page_to_list_to_order(&folio->page, NULL, new_order) == 0) return true; if (folio_test_dirty(folio)) return false; From patchwork Tue Mar 21 00:48:29 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zi Yan X-Patchwork-Id: 13182071 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6104CC6FD1C for ; Tue, 21 Mar 2023 00:48:58 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A943E6B007D; Mon, 20 Mar 2023 20:48:55 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A44A26B007E; Mon, 20 Mar 2023 20:48:55 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8E5336B0080; Mon, 20 Mar 2023 20:48:55 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id 7E0566B007D for ; Mon, 20 Mar 2023 20:48:55 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 56B0740AE0 for ; Tue, 21 Mar 2023 00:48:55 +0000 (UTC) X-FDA: 80591070630.04.27A52AF Received: from out2-smtp.messagingengine.com (out2-smtp.messagingengine.com [66.111.4.26]) by imf15.hostedemail.com (Postfix) with ESMTP id 58DE0A0016 for ; Tue, 21 Mar 2023 00:48:53 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=sent.com header.s=fm2 header.b=0gUATPF0; dkim=pass header.d=messagingengine.com header.s=fm2 header.b=DeAU6DfD; dmarc=pass (policy=none) header.from=sent.com; spf=pass (imf15.hostedemail.com: domain of zi.yan@sent.com designates 66.111.4.26 as permitted sender) smtp.mailfrom=zi.yan@sent.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1679359733; h=from:from:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=F0/jQigDf8DUC/MGGsqPh8UV6VPJTLPPIJT2wdiDhws=; b=3xhMgSkeVYE5HdAzTex0anUYttUOk4vaFN9mTCP4sfnJpW8ud1XGcy7y/NiDwM35fPIOi6 OkKZe83NKIisbEVT7PBECnUWb8D0J+UBPxRq9dcBjIaGS80HM7SkQF5qFkPhXBzwxQ8yDi brfBz6FA/D8oHK+7fiMykhzphstaE/s= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=sent.com header.s=fm2 header.b=0gUATPF0; dkim=pass header.d=messagingengine.com header.s=fm2 header.b=DeAU6DfD; dmarc=pass (policy=none) header.from=sent.com; spf=pass (imf15.hostedemail.com: domain of zi.yan@sent.com designates 66.111.4.26 as permitted sender) smtp.mailfrom=zi.yan@sent.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1679359733; a=rsa-sha256; cv=none; b=eRxw1DXHmZYhyKcpDumqYTxd2s2F2z9vSyMW2Q2qH7POO2HO96mHH0jvxu8Nsx09pgNfq/ SROhfTeXXXjj7hoS7dJpfo7nEsMkWkmepYvHF/YY5szlJhW445p1Xk/nfEnvfHynQxpG2f qdnjvfkfc32mUFe6rdJQ1z8/7qW4gYg= Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id CB4385C019F; Mon, 20 Mar 2023 20:48:52 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Mon, 20 Mar 2023 20:48:52 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sent.com; h=cc :cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:reply-to:sender:subject:subject:to:to; s=fm2; t= 1679359732; x=1679446132; bh=F0/jQigDf8DUC/MGGsqPh8UV6VPJTLPPIJT 2wdiDhws=; b=0gUATPF0eBEC41Czn6cX6EkpfX8MBPCxf9Kvf5Wv2MfAJzJ1e5/ FxKdVejOZCcMZYtD9ja6Q6VTIa/iNaeftxURY9GyaXtGgdq236vDhsX1sCcMQ1Cp wuK71Lk7OFrEEZt0jSjz3kBJAmOWE/EPzF1CtZRmdRv0c9ze/zfJvpbz0CM9FrZ8 OEajQ1juKI9PrWCku+uDQ/b/0Sy83FfzW6U7iywlyVhkhhpNx6fDtleVLgZ+zYao NubP8CAHSGKoGCtw6YZVHICukxRcHoKN0GviNtAS95UtLkwiVraAClrKJUSGQNHN 1BazuTb2SsBpodD7F2bJzOsxDXEQsK2YepQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:reply-to:sender:subject:subject:to:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t= 1679359732; x=1679446132; bh=F0/jQigDf8DUC/MGGsqPh8UV6VPJTLPPIJT 2wdiDhws=; b=DeAU6DfD+cFXeIsaAFNmwuYVkS+MaDlIFUXHhwKc+JFCVfQ0q8N UukedR2m1+scH4LuspXaVwvPBzibCltRVxTYY/R77NagHuDe6JH+ftAcPXnj74Mu ei4FlQJwjSt9lvUwqJKbohHuViCpJJvqYO2TgLG6VMJY//0BKfXON0hYB3/d60IF 8uhN/NehvLkQYmKFj9lOG0ltDCu8I3bfiqIaUzSRkJH5h3ErwQnlJO5CEDLcNKjL NLafPEWqEp3CKcMbZlY3ISvy9gBLeKOuQWCTKq+EudcrcwTJBZK1Y0oi2UCanSsM GmG0ggAD7qJ1iqtwt8cJbkdru0YUq3UHc8w== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvhedrvdefledgvdegucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhephffvvefufffkofgjfhhrggfgsedtqhertdertddtnecuhfhrohhmpegkihcu jggrnhcuoeiiihdrhigrnhesshgvnhhtrdgtohhmqeenucggtffrrghtthgvrhhnpeegge ehudfgudduvdelheehteegledtteeiveeuhfffveekhfevueefieeijeegvdenucevlhhu shhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpeiiihdrhigrnhessh gvnhhtrdgtohhm X-ME-Proxy: Feedback-ID: iccd040f4:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Mon, 20 Mar 2023 20:48:52 -0400 (EDT) From: Zi Yan To: "Matthew Wilcox (Oracle)" , Yang Shi , Yu Zhao , linux-mm@kvack.org Cc: Zi Yan , Andrew Morton , linux-kernel@vger.kernel.org, cgroups@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [PATCH 5/5] mm: huge_memory: enable debugfs to split huge pages to any order. Date: Mon, 20 Mar 2023 20:48:29 -0400 Message-Id: <20230321004829.2012847-6-zi.yan@sent.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20230321004829.2012847-1-zi.yan@sent.com> References: <20230321004829.2012847-1-zi.yan@sent.com> Reply-To: Zi Yan MIME-Version: 1.0 X-Rspamd-Queue-Id: 58DE0A0016 X-Rspamd-Server: rspam09 X-Rspam-User: X-Stat-Signature: rp31uorh3gysp46pj9pqetobzobgnjws X-HE-Tag: 1679359733-514504 X-HE-Meta: U2FsdGVkX18AkrT2pddhoEel0S/OTQAVoQk/nEBKsARm1Hbodtbq10/YR8iKaxPztZ+QJco+R0hglIDn/4Nn1B+mTniiU6tlj9sgRPKouHPlkzeHXDBr9rCu8ByA+MCBokknHCWeGGmKWqKucjYggekf7wpJsNKd2Za7uRhtSfOsrKMgtZ56BmpMytQydfTDkelUCHWGk6MqPbOu49jUc7oGJ/fQvobyqp5R9ModmwOiFggC1rqCOzT97bFawZQS5DrO9NfLI26fJniTQXzfqAzY1OSP7Pq2gdFsAMLUKculARGdDE2aRnKvDoKp6voM/1X2Hgq8aOI+yBsAZLgbpKgl/SaI5J6NYPPsy1SmushiH8g4rQ2Qgheu9QLoEIfewEO22Ko1Yk9uc3LMwfHTDTs0/nSI8a4hC5k0XnEsJFomGxXtqCmjnvyVuKVbifRH/Qq/VwWmH/cIHOmcmT1N0Ga9Yq5YpHajVUOpeFf747bvi3cFJm4ill4dpTsssuIUunVvnOxxVJSbBcm6ISDFiq5DE5yauH9aEIuFOOA2inenXGrPgkMg5fVHFTS64OV+Ikd3A5kNruL6/AFolX2uxGqCclO9Htu5OhFqu8hHNsKsssNvafrY167auhc3UuEXogHTu9u76nqQCqdlvNwQJAldOjk1CZOnMPhLQFirP7idEVYXdsW1oAidjOwTdYjO+LP10n4CKLf8yH8WbI7LayD7KYgDj/pwlAqdtgPUs7OeshcvNSlRsQTbPWbVnEGBFHHOZ7WXXCcN5n83qP44F5sAqxF4X7F+hm8qgHsHUpaDdf5Y0Z2ttCyuD1LULwG4ySa7zVBH29tlKFu7i8jB6YfB47wX3E4vx5l5T2m0Bim2rkg/25QQ9HIkaJ3SVN/9H+4D/da3RPO9TU/3J/Sry23+825XnHVkdTUi22zLt1opYszmdPqgs1Lw4YOjAJDsD47uKPhYPIWkas3yHfn L5QlejDL Wq/5H5KzmWoX3mZW/HVJbIGw3gOWTookg4N9gJClyuYRzPoTHGRImslasSwdFKN3ym80iPYygV85T+WrqQ4Ad3uniAL1TofDOsEwM8EU7+H9pMjcMg45yM1yC20bxhoKPaizUiCwvn7F87MbHmJcZjx+mcC9Qx/BY9omDoLzeIon5d08G7Ty54JeDOyg8vBGQBWt9717a+4Sq6zMz87PHkI3OuB16JsoxIi9dHciUpab149k7aOH8dD1BxUkwzdGz4WeMDhI9XgPAzL4E7O2EZyxvwf/Mx7Rp0WH1nqKWIkS7iR5BuHEKRMKJ2W5c2KKUzpxiUv2ONzR0bdpQzSYi8J94u40r4HtKRllnKjq5b/rBGcCWxrz0LXyGC5h3V8VhJDsq4zND1u/t75/pTMbcqRpM1+dOu+zn+Yj2cpWUqfExK75RaL62dX7CDrzdnCu5XEizlFVkJYnL12U81j+evL0vdQ== 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: Zi Yan It is used to test split_huge_page_to_list_to_order for pagecache THPs. Also add test cases for split_huge_page_to_list_to_order via both debugfs, truncating a file, and punching holes in a file. Signed-off-by: Zi Yan --- mm/huge_memory.c | 34 ++- .../selftests/mm/split_huge_page_test.c | 225 +++++++++++++++++- 2 files changed, 242 insertions(+), 17 deletions(-) diff --git a/mm/huge_memory.c b/mm/huge_memory.c index f119b9be33f2..cadb13e3224f 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -3024,7 +3024,7 @@ static inline bool vma_not_suitable_for_thp_split(struct vm_area_struct *vma) } static int split_huge_pages_pid(int pid, unsigned long vaddr_start, - unsigned long vaddr_end) + unsigned long vaddr_end, unsigned int new_order) { int ret = 0; struct task_struct *task; @@ -3086,13 +3086,19 @@ static int split_huge_pages_pid(int pid, unsigned long vaddr_start, goto next; total++; - if (!can_split_folio(page_folio(page), NULL)) + /* + * For folios with private, split_huge_page_to_list_to_order() + * will try to drop it before split and then check if the folio + * can be split or not. So skip the check here. + */ + if (!folio_test_private(page_folio(page)) && + !can_split_folio(page_folio(page), NULL)) goto next; if (!trylock_page(page)) goto next; - if (!split_huge_page(page)) + if (!split_huge_page_to_list_to_order(page, NULL, new_order)) split++; unlock_page(page); @@ -3110,7 +3116,7 @@ static int split_huge_pages_pid(int pid, unsigned long vaddr_start, } static int split_huge_pages_in_file(const char *file_path, pgoff_t off_start, - pgoff_t off_end) + pgoff_t off_end, unsigned int new_order) { struct filename *file; struct file *candidate; @@ -3149,7 +3155,7 @@ static int split_huge_pages_in_file(const char *file_path, pgoff_t off_start, if (!folio_trylock(folio)) goto next; - if (!split_folio(folio)) + if (!split_huge_page_to_list_to_order(&folio->page, NULL, new_order)) split++; folio_unlock(folio); @@ -3174,10 +3180,14 @@ static ssize_t split_huge_pages_write(struct file *file, const char __user *buf, { static DEFINE_MUTEX(split_debug_mutex); ssize_t ret; - /* hold pid, start_vaddr, end_vaddr or file_path, off_start, off_end */ + /* + * hold pid, start_vaddr, end_vaddr, new_order or + * file_path, off_start, off_end, new_order + */ char input_buf[MAX_INPUT_BUF_SZ]; int pid; unsigned long vaddr_start, vaddr_end; + unsigned int new_order = 0; ret = mutex_lock_interruptible(&split_debug_mutex); if (ret) @@ -3206,29 +3216,29 @@ static ssize_t split_huge_pages_write(struct file *file, const char __user *buf, goto out; } - ret = sscanf(buf, "0x%lx,0x%lx", &off_start, &off_end); - if (ret != 2) { + ret = sscanf(buf, "0x%lx,0x%lx,%d", &off_start, &off_end, &new_order); + if (ret != 2 && ret != 3) { ret = -EINVAL; goto out; } - ret = split_huge_pages_in_file(file_path, off_start, off_end); + ret = split_huge_pages_in_file(file_path, off_start, off_end, new_order); if (!ret) ret = input_len; goto out; } - ret = sscanf(input_buf, "%d,0x%lx,0x%lx", &pid, &vaddr_start, &vaddr_end); + ret = sscanf(input_buf, "%d,0x%lx,0x%lx,%d", &pid, &vaddr_start, &vaddr_end, &new_order); if (ret == 1 && pid == 1) { split_huge_pages_all(); ret = strlen(input_buf); goto out; - } else if (ret != 3) { + } else if (ret != 3 && ret != 4) { ret = -EINVAL; goto out; } - ret = split_huge_pages_pid(pid, vaddr_start, vaddr_end); + ret = split_huge_pages_pid(pid, vaddr_start, vaddr_end, new_order); if (!ret) ret = strlen(input_buf); out: diff --git a/tools/testing/selftests/mm/split_huge_page_test.c b/tools/testing/selftests/mm/split_huge_page_test.c index b8558c7f1a39..cbb5e6893cbf 100644 --- a/tools/testing/selftests/mm/split_huge_page_test.c +++ b/tools/testing/selftests/mm/split_huge_page_test.c @@ -16,6 +16,7 @@ #include #include #include +#include #include "vm_util.h" uint64_t pagesize; @@ -23,10 +24,12 @@ unsigned int pageshift; uint64_t pmd_pagesize; #define SPLIT_DEBUGFS "/sys/kernel/debug/split_huge_pages" +#define SMAP_PATH "/proc/self/smaps" +#define THP_FS_PATH "/mnt/thp_fs" #define INPUT_MAX 80 -#define PID_FMT "%d,0x%lx,0x%lx" -#define PATH_FMT "%s,0x%lx,0x%lx" +#define PID_FMT "%d,0x%lx,0x%lx,%d" +#define PATH_FMT "%s,0x%lx,0x%lx,%d" #define PFN_MASK ((1UL<<55)-1) #define KPF_THP (1UL<<22) @@ -113,7 +116,7 @@ void split_pmd_thp(void) /* split all THPs */ write_debugfs(PID_FMT, getpid(), (uint64_t)one_page, - (uint64_t)one_page + len); + (uint64_t)one_page + len, 0); for (i = 0; i < len; i++) if (one_page[i] != (char)i) { @@ -203,7 +206,7 @@ void split_pte_mapped_thp(void) /* split all remapped THPs */ write_debugfs(PID_FMT, getpid(), (uint64_t)pte_mapped, - (uint64_t)pte_mapped + pagesize * 4); + (uint64_t)pte_mapped + pagesize * 4, 0); /* smap does not show THPs after mremap, use kpageflags instead */ thp_size = 0; @@ -269,7 +272,7 @@ void split_file_backed_thp(void) } /* split the file-backed THP */ - write_debugfs(PATH_FMT, testfile, pgoff_start, pgoff_end); + write_debugfs(PATH_FMT, testfile, pgoff_start, pgoff_end, 0); status = unlink(testfile); if (status) @@ -290,20 +293,232 @@ void split_file_backed_thp(void) printf("file-backed THP split test done, please check dmesg for more information\n"); } +void create_pagecache_thp_and_fd(const char *testfile, size_t fd_size, int *fd, char **addr) +{ + size_t i; + int dummy; + + srand(time(NULL)); + + *fd = open(testfile, O_CREAT | O_RDWR, 0664); + if (*fd == -1) { + perror("Failed to create a file at "THP_FS_PATH); + exit(EXIT_FAILURE); + } + + for (i = 0; i < fd_size; i++) { + unsigned char byte = (unsigned char)i; + + write(*fd, &byte, sizeof(byte)); + } + close(*fd); + sync(); + *fd = open("/proc/sys/vm/drop_caches", O_WRONLY); + if (*fd == -1) { + perror("open drop_caches"); + goto err_out_unlink; + } + if (write(*fd, "3", 1) != 1) { + perror("write to drop_caches"); + goto err_out_unlink; + } + close(*fd); + + *fd = open(testfile, O_RDWR); + if (*fd == -1) { + perror("Failed to open a file at "THP_FS_PATH); + goto err_out_unlink; + } + + *addr = mmap(NULL, fd_size, PROT_READ|PROT_WRITE, MAP_SHARED, *fd, 0); + if (*addr == (char *)-1) { + perror("cannot mmap"); + goto err_out_close; + } + madvise(*addr, fd_size, MADV_HUGEPAGE); + + for (size_t i = 0; i < fd_size; i++) + dummy += *(*addr + i); + + if (!check_huge_file(*addr, fd_size / pmd_pagesize, pmd_pagesize)) { + printf("No pagecache THP generated, please mount a filesystem supporting pagecache THP at "THP_FS_PATH"\n"); + goto err_out_close; + } + return; +err_out_close: + close(*fd); +err_out_unlink: + unlink(testfile); + exit(EXIT_FAILURE); +} + +void split_thp_in_pagecache_to_order(size_t fd_size, int order) +{ + int fd; + char *addr; + size_t i; + const char testfile[] = THP_FS_PATH "/test"; + int err = 0; + + create_pagecache_thp_and_fd(testfile, fd_size, &fd, &addr); + + printf("split %ld kB PMD-mapped pagecache page to order %d ... ", fd_size >> 10, order); + write_debugfs(PID_FMT, getpid(), (uint64_t)addr, (uint64_t)addr + fd_size, order); + + for (i = 0; i < fd_size; i++) + if (*(addr + i) != (char)i) { + printf("%lu byte corrupted in the file\n", i); + err = EXIT_FAILURE; + goto out; + } + + if (!check_huge_file(addr, 0, pmd_pagesize)) { + printf("Still FilePmdMapped not split\n"); + err = EXIT_FAILURE; + goto out; + } + + printf("done\n"); +out: + close(fd); + unlink(testfile); + if (err) + exit(err); +} + +void truncate_thp_in_pagecache_to_order(size_t fd_size, int order) +{ + int fd; + char *addr; + size_t i; + const char testfile[] = THP_FS_PATH "/test"; + int err = 0; + + create_pagecache_thp_and_fd(testfile, fd_size, &fd, &addr); + + printf("truncate %ld kB PMD-mapped pagecache page to size %lu kB ... ", + fd_size >> 10, 4UL << order); + ftruncate(fd, pagesize << order); + + for (i = 0; i < (pagesize << order); i++) + if (*(addr + i) != (char)i) { + printf("%lu byte corrupted in the file\n", i); + err = EXIT_FAILURE; + goto out; + } + + if (!check_huge_file(addr, 0, pmd_pagesize)) { + printf("Still FilePmdMapped not split after truncate\n"); + err = EXIT_FAILURE; + goto out; + } + + printf("done\n"); +out: + close(fd); + unlink(testfile); + if (err) + exit(err); +} + +void punch_hole_in_pagecache_thp(size_t fd_size, off_t offset[], off_t len[], + int n, int num_left_thps) +{ + int fd, j; + char *addr; + size_t i; + const char testfile[] = THP_FS_PATH "/test"; + int err = 0; + + create_pagecache_thp_and_fd(testfile, fd_size, &fd, &addr); + + for (j = 0; j < n; j++) { + printf("punch a hole to %ld kB PMD-mapped pagecache page at addr: %lx, offset %ld, and len %ld ...\n", + fd_size >> 10, (unsigned long)addr, offset[j], len[j]); + fallocate(fd, FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE, offset[j], len[j]); + } + + for (i = 0; i < fd_size; i++) { + int in_hole = 0; + + for (j = 0; j < n; j++) + if (i >= offset[j] && i < (offset[j] + len[j])) { + in_hole = 1; + break; + } + + if (in_hole) { + if (*(addr + i)) { + printf("%lu byte non-zero after punch\n", i); + err = EXIT_FAILURE; + goto out; + } + continue; + } + if (*(addr + i) != (char)i) { + printf("%lu byte corrupted in the file\n", i); + err = EXIT_FAILURE; + goto out; + } + } + + if (!check_huge_file(addr, num_left_thps, pmd_pagesize)) { + printf("Still FilePmdMapped not split after punch\n"); + goto out; + } + printf("done\n"); +out: + close(fd); + unlink(testfile); + if (err) + exit(err); +} + int main(int argc, char **argv) { + int i; + size_t fd_size; + off_t offset[2], len[2]; + if (geteuid() != 0) { printf("Please run the benchmark as root\n"); exit(EXIT_FAILURE); } + setbuf(stdout, NULL); + pagesize = getpagesize(); pageshift = ffs(pagesize) - 1; pmd_pagesize = read_pmd_pagesize(); + fd_size = 2 * pmd_pagesize; split_pmd_thp(); split_pte_mapped_thp(); split_file_backed_thp(); + for (i = 8; i >= 0; i--) + if (i != 1) + split_thp_in_pagecache_to_order(fd_size, i); + + /* + * for i is 1, truncate code in the kernel should create order-0 pages + * instead of order-1 THPs, since order-1 THP is not supported. No error + * is expected. + */ + for (i = 8; i >= 0; i--) + truncate_thp_in_pagecache_to_order(fd_size, i); + + offset[0] = 123; + offset[1] = 4 * pagesize; + len[0] = 200 * pagesize; + len[1] = 16 * pagesize; + punch_hole_in_pagecache_thp(fd_size, offset, len, 2, 1); + + offset[0] = 259 * pagesize + pagesize / 2; + offset[1] = 33 * pagesize; + len[0] = 129 * pagesize; + len[1] = 16 * pagesize; + punch_hole_in_pagecache_thp(fd_size, offset, len, 2, 1); + return 0; }