From patchwork Sat Aug 24 01:04:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Barry Song <21cnbao@gmail.com> X-Patchwork-Id: 13776157 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 AF1B9C5321E for ; Sat, 24 Aug 2024 01:05:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3F541800DB; Fri, 23 Aug 2024 21:05:20 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3A4E6800D4; Fri, 23 Aug 2024 21:05:20 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2450D800DB; Fri, 23 Aug 2024 21:05:20 -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 01E13800D4 for ; Fri, 23 Aug 2024 21:05:19 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id ADE38A02F4 for ; Sat, 24 Aug 2024 01:05:19 +0000 (UTC) X-FDA: 82485345558.21.69A8E65 Received: from mail-pl1-f181.google.com (mail-pl1-f181.google.com [209.85.214.181]) by imf22.hostedemail.com (Postfix) with ESMTP id CDDFDC0013 for ; Sat, 24 Aug 2024 01:05:17 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=j88GMPPq; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf22.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724461500; a=rsa-sha256; cv=none; b=nr45ZVh4W+sBSBifg0CXVitcJLXuyZNk4XaxJfVYS9aqpetzg1qyGU98EQ1cLgWz2Gbxit C3/Unep9iEmSQtwx7Aigb844XsyrI9ov6xbSB5AuoFGyFuaQTdyQYSa8gpd4FtqTykkoNb 0SaKqUm3qRIiQxesSx58BL8h73veMJY= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=j88GMPPq; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf22.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.214.181 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724461500; h=from:from:sender: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=/fvnizBuO11jjlZtTiefVW+ElYgnwDD8L4QEiBXi3dY=; b=xDTknHcyJcwwbHht5baYSykE7YfOpTLQtE5eUdczPW0+NA5CsxqKDgCyqowSCM56gTasu9 tShbqAR6oLr1ghVv2YFqQwIy7QPvsu/bLxYVijsaPaapzGnAn5UQOtMCkjbP9+cEb0D1rM 7te7NA6Nii5NXypo6HyqQ/xE0Ay+JNo= Received: by mail-pl1-f181.google.com with SMTP id d9443c01a7336-2021c03c13aso19957205ad.1 for ; Fri, 23 Aug 2024 18:05:17 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724461516; x=1725066316; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=/fvnizBuO11jjlZtTiefVW+ElYgnwDD8L4QEiBXi3dY=; b=j88GMPPq6AI8I47oE4FVzoRDRR5dDuWCfIhnf1CD80kqkhSN1j7uTWs3U50Xx8K7sO TaHkxtOrKifyRth9ybyv9G8rjYuXIWLpqVM5YRPfrrKOGIm7UCLQcNufL3uAN2oYTBbr T2Cg79HRbTvc9MoIuSGKQ1oaSrTh6Jo6n+zc0kPSRi5PFt3dQcM63CsReVwxtr8KnIwK xGOUOA23WRUuwKLu7JfRag22zC0jICzIRH4ySk0tPIul9MLtTKmjv+jB1WPKXMSIZCxt 7uyTlY0z4h21KkoJPWR8nxk9a2T8NueABjj+/b3WdaCmY4N3LHFplGE6jCQ2JkUPPa0J /zcw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724461516; x=1725066316; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=/fvnizBuO11jjlZtTiefVW+ElYgnwDD8L4QEiBXi3dY=; b=hhWejyXwvZ8q8VGQURvZ5qzNdpwpOLEV0/TjN8kYN2c07EtB23J32l/tDcYXBsOMNE jNsSHobZ4+lKrJn9YlW2wr9YXpmus0AXsUDMo3WhPJOq0fBNLvVLFKwDPAtCh/DRR5Fw UuMZkEZ1wRyBdz1CmdBFdp+1d5T+F15xgQOhSEfWlIgwPXx84+LElktozIu8SbS0DVg0 6VbtviUhag4/U1M7YaSDEcc1X/VnE+XdIy4yozhObFlS31MMXXrZanyxWNCafcUk6spY TTJWK4iDvmlKW5AExtzSGI/ynSDII+6MptQIsTPmMG7+agm9ePmeYLXg/CbRhHM1LDf1 y97g== X-Forwarded-Encrypted: i=1; AJvYcCWiuqi5Um9XZ3NW8cz4VhUCYxcJ4P6cgS4RfSYNuFruPZYJu8Fql0g4LfMIzXWmG8o4o5q56eNEVw==@kvack.org X-Gm-Message-State: AOJu0Yy06XVa1yMuuF3jR+ze3q/7f44OimZ347yDHZWV8fAN/qdPIDiJ kuA/kqYOo4H+Cm1LNYkuBCVJIu6cWXhHBofUEhMIt/zyBFRdrH1d X-Google-Smtp-Source: AGHT+IFUsnJOcIozfJLBYBiMbcDGe/SYAgg1aJHSClzsIiFxd+TuEBD6xCjLHozi/xa8m8owGr2qkQ== X-Received: by 2002:a17:902:dad1:b0:201:f2a4:cf74 with SMTP id d9443c01a7336-2037fe1ac56mr132147095ad.22.1724461516426; Fri, 23 Aug 2024 18:05:16 -0700 (PDT) Received: from Barrys-MBP.hub ([2407:7000:8942:5500:8d8:dd4b:c921:b282]) by smtp.gmail.com with ESMTPSA id 41be03b00d2f7-7cd9ad56c9fsm3274064a12.64.2024.08.23.18.05.10 (version=TLS1_3 cipher=TLS_CHACHA20_POLY1305_SHA256 bits=256/256); Fri, 23 Aug 2024 18:05:16 -0700 (PDT) From: Barry Song <21cnbao@gmail.com> To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: baolin.wang@linux.alibaba.com, chrisl@kernel.org, david@redhat.com, hanchuanhua@oppo.com, ioworker0@gmail.com, kaleshsingh@google.com, kasong@tencent.com, linux-kernel@vger.kernel.org, ryan.roberts@arm.com, usamaarif642@gmail.com, v-songbaohua@oppo.com, yuanshuai@oppo.com, ziy@nvidia.com Subject: [PATCH v4 2/2] mm: count the number of partially mapped anonymous THPs per size Date: Sat, 24 Aug 2024 13:04:41 +1200 Message-Id: <20240824010441.21308-3-21cnbao@gmail.com> X-Mailer: git-send-email 2.39.3 (Apple Git-146) In-Reply-To: <20240824010441.21308-1-21cnbao@gmail.com> References: <20240824010441.21308-1-21cnbao@gmail.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: CDDFDC0013 X-Rspamd-Server: rspam01 X-Stat-Signature: qeuy9wg855sxcrpcx34hpbj5mbpj8pb4 X-HE-Tag: 1724461517-718502 X-HE-Meta: U2FsdGVkX1/McRcjBaFY76KJglKiBL4W2CiEUQR1iXXPHhSGTXpkntO+YopuZwSShUk+Ln+2E3bJrYgdayCKnGEqbnpFqEId96ym4vo63pp77Od+wveqE7QV6hbVxrLJ/Z5atSWo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e5rB0cZ6P+wRFnUgHOcf8saTZJdn4fTbNu6j+dOM0kZ7NtnLCQitDIqmE09BLYpMh4byYZiUFwD2nF/uKg7yLUcVDaMb13LWEBbiw1xErAcHk73QN5S4Jh73o0QQ4mML5y55q9488sTojE+3K+L7ELhbkzci+Eu7GCY7RO0J84ghEQYS2d+wUcp5aky92XbcyEBn++arlQaL6o6PH2FDr++ZfuKU/fIlC32/9nn2k= 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: List-Subscribe: List-Unsubscribe: From: Barry Song When a THP is added to the deferred_list due to partially mapped, its partial pages are unused, leading to wasted memory and potentially increasing memory reclamation pressure. Detailing the specifics of how unmapping occurs is quite difficult and not that useful, so we adopt a simple approach: each time a THP enters the deferred_list, we increment the count by 1; whenever it leaves for any reason, we decrement the count by 1. Signed-off-by: Barry Song Acked-by: David Hildenbrand --- Documentation/admin-guide/mm/transhuge.rst | 7 +++++++ include/linux/huge_mm.h | 1 + mm/huge_memory.c | 6 ++++++ 3 files changed, 14 insertions(+) diff --git a/Documentation/admin-guide/mm/transhuge.rst b/Documentation/admin-guide/mm/transhuge.rst index b78f2148b242..6630f2ed14ee 100644 --- a/Documentation/admin-guide/mm/transhuge.rst +++ b/Documentation/admin-guide/mm/transhuge.rst @@ -556,6 +556,13 @@ nr_anon These huge pages could be entirely mapped or have partially unmapped/unused subpages. +nr_anon_partially_mapped + the number of anonymous THP which are likely partially mapped, possibly + wasting memory, and have been queued for deferred memory reclamation. + Note that in corner some cases (e.g., failed migration), we might detect + an anonymous THP as "partially mapped" and count it here, even though it + is not actually partially mapped anymore. + As the system ages, allocating huge pages may be expensive as the system uses memory compaction to copy data around memory to free a huge page for use. There are some counters in ``/proc/vmstat`` to help diff --git a/include/linux/huge_mm.h b/include/linux/huge_mm.h index 2ee2971e4e10..4902e2f7e896 100644 --- a/include/linux/huge_mm.h +++ b/include/linux/huge_mm.h @@ -127,6 +127,7 @@ enum mthp_stat_item { MTHP_STAT_SPLIT_FAILED, MTHP_STAT_SPLIT_DEFERRED, MTHP_STAT_NR_ANON, + MTHP_STAT_NR_ANON_PARTIALLY_MAPPED, __MTHP_STAT_COUNT }; diff --git a/mm/huge_memory.c b/mm/huge_memory.c index 26ad75fcda62..a81eab98d6b8 100644 --- a/mm/huge_memory.c +++ b/mm/huge_memory.c @@ -598,6 +598,7 @@ DEFINE_MTHP_STAT_ATTR(split, MTHP_STAT_SPLIT); DEFINE_MTHP_STAT_ATTR(split_failed, MTHP_STAT_SPLIT_FAILED); DEFINE_MTHP_STAT_ATTR(split_deferred, MTHP_STAT_SPLIT_DEFERRED); DEFINE_MTHP_STAT_ATTR(nr_anon, MTHP_STAT_NR_ANON); +DEFINE_MTHP_STAT_ATTR(nr_anon_partially_mapped, MTHP_STAT_NR_ANON_PARTIALLY_MAPPED); static struct attribute *anon_stats_attrs[] = { &anon_fault_alloc_attr.attr, @@ -611,6 +612,7 @@ static struct attribute *anon_stats_attrs[] = { &split_failed_attr.attr, &split_deferred_attr.attr, &nr_anon_attr.attr, + &nr_anon_partially_mapped_attr.attr, NULL, }; @@ -3457,6 +3459,7 @@ int split_huge_page_to_list_to_order(struct page *page, struct list_head *list, if (folio_order(folio) > 1 && !list_empty(&folio->_deferred_list)) { ds_queue->split_queue_len--; + mod_mthp_stat(folio_order(folio), MTHP_STAT_NR_ANON_PARTIALLY_MAPPED, -1); /* * Reinitialize page_deferred_list after removing the * page from the split_queue, otherwise a subsequent @@ -3523,6 +3526,7 @@ void __folio_undo_large_rmappable(struct folio *folio) spin_lock_irqsave(&ds_queue->split_queue_lock, flags); if (!list_empty(&folio->_deferred_list)) { ds_queue->split_queue_len--; + mod_mthp_stat(folio_order(folio), MTHP_STAT_NR_ANON_PARTIALLY_MAPPED, -1); list_del_init(&folio->_deferred_list); } spin_unlock_irqrestore(&ds_queue->split_queue_lock, flags); @@ -3564,6 +3568,7 @@ void deferred_split_folio(struct folio *folio) if (folio_test_pmd_mappable(folio)) count_vm_event(THP_DEFERRED_SPLIT_PAGE); count_mthp_stat(folio_order(folio), MTHP_STAT_SPLIT_DEFERRED); + mod_mthp_stat(folio_order(folio), MTHP_STAT_NR_ANON_PARTIALLY_MAPPED, 1); list_add_tail(&folio->_deferred_list, &ds_queue->split_queue); ds_queue->split_queue_len++; #ifdef CONFIG_MEMCG @@ -3611,6 +3616,7 @@ static unsigned long deferred_split_scan(struct shrinker *shrink, list_move(&folio->_deferred_list, &list); } else { /* We lost race with folio_put() */ + mod_mthp_stat(folio_order(folio), MTHP_STAT_NR_ANON_PARTIALLY_MAPPED, -1); list_del_init(&folio->_deferred_list); ds_queue->split_queue_len--; }