From mboxrd@z Thu Jan 1 00:00:00 1970 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 6E449C38A2D for ; Wed, 26 Oct 2022 00:27:15 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DE9218E0002; Tue, 25 Oct 2022 20:27:14 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D993C8E0001; Tue, 25 Oct 2022 20:27:14 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C3BFD8E0002; Tue, 25 Oct 2022 20:27:14 -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 B0A8A8E0001 for ; Tue, 25 Oct 2022 20:27:14 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 88CD514086F for ; Wed, 26 Oct 2022 00:27:14 +0000 (UTC) X-FDA: 80061211188.20.60CC81F Received: from mx0a-00082601.pphosted.com (mx0b-00082601.pphosted.com [67.231.153.30]) by imf15.hostedemail.com (Postfix) with ESMTP id EF676A003E for ; Wed, 26 Oct 2022 00:27:13 +0000 (UTC) Received: from pps.filterd (m0001303.ppops.net [127.0.0.1]) by m0001303.ppops.net (8.17.1.5/8.17.1.5) with ESMTP id 29PMGmDw005937 for ; Tue, 25 Oct 2022 17:27:13 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from : to : cc : subject : date : message-id : mime-version : content-transfer-encoding : content-type; s=facebook; bh=4VMBQY4yvmbWTkGJ7FJlLXO3jNujZBlLkhQzofRk2SY=; b=Zl+OjHaDEZHiEs6/4x/XWD51DNJciS5SCZ8KXkm7V/jmYcMfBPYnhbH+AbFmNM6rLY+B 1z/eDpSzh+vi6dQU48rYirufvSI8VX2lUhXzzk2So/3o6bnijbPmoK6EysASpR+lXOmr HYfB4RIvS9LfgqjxYB4O+iQh8zG9W4Nkvco= Received: from mail.thefacebook.com ([163.114.132.120]) by m0001303.ppops.net (PPS) with ESMTPS id 3ke49bwh85-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Tue, 25 Oct 2022 17:27:12 -0700 Received: from twshared5252.09.ash9.facebook.com (2620:10d:c085:108::4) by mail.thefacebook.com (2620:10d:c085:11d::5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Tue, 25 Oct 2022 17:27:10 -0700 Received: by devvm6390.atn0.facebook.com (Postfix, from userid 352741) id EC854586CD75; Tue, 25 Oct 2022 17:27:00 -0700 (PDT) From: To: , CC: , , , , , Alexander Zhu Subject: [PATCH v5 0/5] THP Shrinker Date: Tue, 25 Oct 2022 17:26:53 -0700 Message-ID: X-Mailer: git-send-email 2.30.2 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-FB-Internal: Safe Content-Type: text/plain X-Proofpoint-ORIG-GUID: wmpmlwd3Y17E4k6Guyn6DsBHXxJcIiBr X-Proofpoint-GUID: wmpmlwd3Y17E4k6Guyn6DsBHXxJcIiBr X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.205,Aquarius:18.0.895,Hydra:6.0.545,FMLib:17.11.122.1 definitions=2022-10-25_14,2022-10-25_01,2022-06-22_01 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1666744034; a=rsa-sha256; cv=none; b=FUJDq2oN+kmEjObcTh7Xze97uXrpgjP6nJ0ZVpOlotOWcsC0J7E7+J6zkn0y7SzR5b3C+W 3psGL3G8kxOiKFgM9ua3BzUDZGuh92hGSEPgu5TKnOdlZa41zTJs/yNGy7T4mhCHyb8pPr 39YvzY5mZPpnewP4+dTlPvFuXfMGIEg= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=fb.com header.s=facebook header.b=Zl+OjHaD; spf=pass (imf15.hostedemail.com: domain of "prvs=1298d8d15a=alexlzhu@meta.com" designates 67.231.153.30 as permitted sender) smtp.mailfrom="prvs=1298d8d15a=alexlzhu@meta.com"; dmarc=pass (policy=reject) header.from=fb.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1666744034; 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-type: content-transfer-encoding:content-transfer-encoding:in-reply-to: references:dkim-signature; bh=4VMBQY4yvmbWTkGJ7FJlLXO3jNujZBlLkhQzofRk2SY=; b=X/jMzd5cvE7wscpUnwzLjfNZY/YDNHe20NtifphqP9yzikjYSmzDDFD04GLM6NcXC3xGkB 1sB4ebQkoXvFe5lnS9sXqF74Suut4EsIQuVzuYp+jlOsIiwHq/gzfav1NESsry6ccSz9ni YSDxD+iKSEKMRPC39AnaVS+DyD9FsvE= X-Stat-Signature: byam9jg1g6gkd48dfemdtmakxyra7nft X-Rspamd-Queue-Id: EF676A003E Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=fb.com header.s=facebook header.b=Zl+OjHaD; spf=pass (imf15.hostedemail.com: domain of "prvs=1298d8d15a=alexlzhu@meta.com" designates 67.231.153.30 as permitted sender) smtp.mailfrom="prvs=1298d8d15a=alexlzhu@meta.com"; dmarc=pass (policy=reject) header.from=fb.com X-Rspam-User: X-Rspamd-Server: rspam05 X-HE-Tag: 1666744033-767062 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 Zhu Transparent Hugepages use a larger page size of 2MB in comparison to normal sized pages that are 4kb. A larger page size allows for fewer TLB cache misses and thus more efficient use of the CPU. Using a larger page size also results in more memory waste, which can hurt performance in som= e use cases. THPs are currently enabled in the Linux Kernel by applications in limited virtual address ranges via the madvise system call. The THP shrinker tries to find a balance between increased use of THPs, and increased use of memory. It shrinks the size of memory by removing the underutilized THPs that are identified by the thp_utilization scanner.=20 In our experiments we have noticed that the least utilized THPs are almos= t entirely unutilized. Sample Output:=20 Utilized[0-50]: 1331 680884 Utilized[51-101]: 9 3983 Utilized[102-152]: 3 1187 Utilized[153-203]: 0 0 Utilized[204-255]: 2 539 Utilized[256-306]: 5 1135 Utilized[307-357]: 1 192 Utilized[358-408]: 0 0 Utilized[409-459]: 1 57 Utilized[460-512]: 400 13 Last Scan Time: 223.98s Last Scan Duration: 70.65s Above is a sample obtained from one of our test machines when THP is alwa= ys enabled. Of the 1331 THPs in this thp_utilization sample that have from 0-50 utilized subpages, we see that there are 680884 free pages. This comes out to 680884 / (512 * 1331) =3D 99.91% zero pages in the least utilized bucket. This represents 680884 * 4KB =3D 2.7GB memory waste. Also note that the vast majority of pages are either in the least utilize= d [0-50] or most utilized [460-512] buckets. The least utilized THPs are=20 responsible for almost all of the memory waste when THP is always=20 enabled. Thus by clearing out THPs in the lowest utilization bucket we extract most of the improvement in CPU efficiency. We have seen=20 similar results on our production hosts. This patchset introduces the THP shrinker we have developed to identify and split the least utilized THPs. It includes the thp_utilization=20 changes that groups anonymous THPs into buckets, the split_huge_page() changes that identify and zap zero 4KB pages within THPs and the shrinker changes. It should be noted that the split_huge_page() changes are based off previous work done by Yu Zhao.=20 In the future, we intend to allow additional tuning to the shrinker based on workload depending on CPU/IO/Memory pressure and the=20 amount of anonymous memory. The long term goal is to eventually always=20 enable THP for all applications and deprecate madvise entirely. In production we thus far have observed 2-3% reduction in overall cpu usage on stateless web servers when THP is always enabled. Alexander Zhu (5): mm: add thp_utilization metrics to debugfs mm: changes to split_huge_page() to free zero filled tail pages mm: do not remap clean subpages when splitting isolated thp mm: add selftests to split_huge_page() to verify unmap/zap of zero pages mm: THP low utilization shrinker Documentation/admin-guide/mm/transhuge.rst | 9 + include/linux/huge_mm.h | 9 + include/linux/list_lru.h | 24 ++ include/linux/mm_types.h | 5 + include/linux/rmap.h | 2 +- include/linux/vm_event_item.h | 3 + mm/Makefile | 2 +- mm/huge_memory.c | 155 +++++++++++- mm/list_lru.c | 49 ++++ mm/migrate.c | 73 +++++- mm/migrate_device.c | 4 +- mm/page_alloc.c | 6 + mm/thp_utilization.c | 222 ++++++++++++++++++ mm/vmstat.c | 3 + .../selftests/vm/split_huge_page_test.c | 115 ++++++++- tools/testing/selftests/vm/vm_util.c | 23 ++ tools/testing/selftests/vm/vm_util.h | 3 + 17 files changed, 689 insertions(+), 18 deletions(-) create mode 100644 mm/thp_utilization.c --=20 2.30.2