From patchwork Mon Aug 19 02:30:53 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Usama Arif X-Patchwork-Id: 13767727 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 8693DC52D7C for ; Mon, 19 Aug 2024 02:31:54 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D78B36B007B; Sun, 18 Aug 2024 22:31:53 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D28CE6B0082; Sun, 18 Aug 2024 22:31:53 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BC8DD6B0083; Sun, 18 Aug 2024 22:31: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 9E0566B007B for ; Sun, 18 Aug 2024 22:31:53 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 0207B160EE7 for ; Mon, 19 Aug 2024 02:31:52 +0000 (UTC) X-FDA: 82467419706.06.75D0E54 Received: from mail-qt1-f178.google.com (mail-qt1-f178.google.com [209.85.160.178]) by imf06.hostedemail.com (Postfix) with ESMTP id 4645E180003 for ; Mon, 19 Aug 2024 02:31:51 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=QN6VJnwd; spf=pass (imf06.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.160.178 as permitted sender) smtp.mailfrom=usamaarif642@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1724034673; 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:references:dkim-signature; bh=Hr/MNKOihCdmGfPcRIsChyQtOWVhC+v3g1s4BetNL5w=; b=58VHLCk4k3t6iFgiv2rGSJNtm5cXH2Pv1s4ghv/fmDQmidhlXqWbVJRbEO9o9lSRb7VFGB 1qNMzXOWKGeE7OqetoOzquxvYDowpCZJ3fNqF/w0x0B/4hKavlkaJtAUk7AP0aiMc2YoY9 O/eTTsTV3glxsv/oEmF19i8QfviguOs= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=QN6VJnwd; spf=pass (imf06.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.160.178 as permitted sender) smtp.mailfrom=usamaarif642@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1724034673; a=rsa-sha256; cv=none; b=sbId6u0kdrjHYoNaM8PW7ZKtt0anmSkdZmWD1cGNqKkVBFl7jxMID23v/CSsSPsE480GSK PmVn/rdRpJivbxO1tzupDEpWYOXZa3ASHintRd2MqGWwWG3vSSTj4Gh7Lg6hQ6tMVN96af ngD9jJm0VAGG9tzXR0En74D9Hom1xBE= Received: by mail-qt1-f178.google.com with SMTP id d75a77b69052e-44fded90c41so20338181cf.3 for ; Sun, 18 Aug 2024 19:31:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1724034710; x=1724639510; darn=kvack.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=Hr/MNKOihCdmGfPcRIsChyQtOWVhC+v3g1s4BetNL5w=; b=QN6VJnwdCUckFT8n+/hsv8zryWewejWvKJStB5hsxEqWx73LQ0Q+21L8JvvOo8BSIT f7SG/L2qSr0VC5YU92vqQ3kIy2TeIdgUneLWs6+REVWb0jlGB8cI91tCQKasGbXj+xTc Jp+Hduv6JpGP61kJLHAVPbzMkqZxREppc2BhQjjxjZN+mEnUXEPqs/fF9IERwv9wfOHP gG6j8g+c7W+SnCHd8X7YewOqNEMDSjsPTNSMgdC19J3HK+6F+gHjaAkhLJ3cYK/oSjua N8UBv81dslYdB9i1raj0RyDndpx37PxNJS+N4Fvf6i8V83XGa284iR8AAawkjvWrx4vl frCQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724034710; x=1724639510; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=Hr/MNKOihCdmGfPcRIsChyQtOWVhC+v3g1s4BetNL5w=; b=TXexI7eF00Kuw99Uv1SUms1wPi0Xd9KnlsM+h8FxZsiUb2ScQRZ9a8SPoMyT0WBG/x eyZjF5peRiJcTQT/hOufUmZNx2BGq0Hq4UuKmYLUfTIYag08zGVqrinDHPJeda8Xo4Bz OJWD29wlnF1Ce90shAZqfVOGjQnp46yZBFn7cAL/afbJu9UhLA1QbI8CjmqF1I4YEDMx TZk01LXBH8UsDb0j+lXsb5SQ6Hlke0pjn4m49tm3ZckFFmCIVPRVFFswxzm+/aFxXzmX DLxcOz/NBHmlhvvoWOYOsCwLQk5TimCcxZWZoiCmC/3MLh8FWmfcLVH/8YSCYGba4nbU UEag== X-Forwarded-Encrypted: i=1; AJvYcCXjoYLleVUGHLYAYzfH8T71dWMDZJMgBXaypXjr/FBVmBBPXtpTVQpCVjhxDuvJg9Le5GTChTVcJ1IdalhGRLlfV6A= X-Gm-Message-State: AOJu0YwnrlqMsLUB6c5dxmDAODQ9ngXtKf4DOBvXe44pD5blAWnmiCCA v7yRQyjo2ka5GpTbMD8dZ6Cs102JPmIVIYryCb1Mpw5e/Fpbt7Lw X-Google-Smtp-Source: AGHT+IHVANl7x8pILhez9TXzw1mYBL1esgGeTHkXNUwc7tgVJVgV88kUbwZwUzm2+nOxxHI53iA4EQ== X-Received: by 2002:a05:622a:2b45:b0:453:5eae:fa23 with SMTP id d75a77b69052e-453741b5e87mr119763431cf.4.1724034710136; Sun, 18 Aug 2024 19:31:50 -0700 (PDT) Received: from localhost (fwdproxy-ash-113.fbsv.net. [2a03:2880:20ff:71::face:b00c]) by smtp.gmail.com with ESMTPSA id d75a77b69052e-4536a072683sm36252731cf.85.2024.08.18.19.31.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Aug 2024 19:31:49 -0700 (PDT) From: Usama Arif To: akpm@linux-foundation.org, linux-mm@kvack.org Cc: hannes@cmpxchg.org, riel@surriel.com, shakeel.butt@linux.dev, roman.gushchin@linux.dev, yuzhao@google.com, david@redhat.com, baohua@kernel.org, ryan.roberts@arm.com, rppt@kernel.org, willy@infradead.org, cerasuolodomenico@gmail.com, ryncsn@gmail.com, corbet@lwn.net, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kernel-team@meta.com, Usama Arif Subject: [PATCH v4 0/6] mm: split underused THPs Date: Mon, 19 Aug 2024 03:30:53 +0100 Message-ID: <20240819023145.2415299-1-usamaarif642@gmail.com> X-Mailer: git-send-email 2.43.5 MIME-Version: 1.0 X-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: 4645E180003 X-Stat-Signature: wewzqshi16x4fg6t4zs11c7m3h4jths9 X-HE-Tag: 1724034711-558176 X-HE-Meta: U2FsdGVkX19GT2aWLqVXhyxL0M4L3Wc+weYFD7vRD14VbGOXxuJYD45KMEO81VpOtcLsc/tl84FWMpHjLIzR+p7oPnhA4xLPXG/TDVYgw7iCt0fwGEWVYmMGP+V2X5VsMOQpCASZEvCO4uaPXlKswIglH7lwtOmcIGxsU2FQd2RGG8jlII6FtjN3UvCbHX55H2FCdRmZ/Oubv6fjmYM3QZErmWsIsQcCgLGdFv19B51lUgQH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, 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: The current upstream default policy for THP is always. However, Meta uses madvise in production as the current THP=always policy vastly overprovisions THPs in sparsely accessed memory areas, resulting in excessive memory pressure and premature OOM killing. Using madvise + relying on khugepaged has certain drawbacks over THP=always. Using madvise hints mean THPs aren't "transparent" and require userspace changes. Waiting for khugepaged to scan memory and collapse pages into THP can be slow and unpredictable in terms of performance (i.e. you dont know when the collapse will happen), while production environments require predictable performance. If there is enough memory available, its better for both performance and predictability to have a THP from fault time, i.e. THP=always rather than wait for khugepaged to collapse it, and deal with sparsely populated THPs when the system is running out of memory. This patch-series is an attempt to mitigate the issue of running out of memory when THP is always enabled. During runtime whenever a THP is being faulted in or collapsed by khugepaged, the THP is added to a list. Whenever memory reclaim happens, the kernel runs the deferred_split shrinker which goes through the list and checks if the THP was underused, i.e. how many of the base 4K pages of the entire THP were zero-filled. If this number goes above a certain threshold, the shrinker will attempt to split that THP. Then at remap time, the pages that were zero-filled are mapped to the shared zeropage, hence saving memory. This method avoids the downside of wasting memory in areas where THP is sparsely filled when THP is always enabled, while still providing the upside THPs like reduced TLB misses without having to use madvise. Meta production workloads that were CPU bound (>99% CPU utilzation) were tested with THP shrinker. The results after 2 hours are as follows: | THP=madvise | THP=always | THP=always | | | + shrinker series | | | + max_ptes_none=409 ----------------------------------------------------------------------------- Performance improvement | - | +1.8% | +1.7% (over THP=madvise) | | | ----------------------------------------------------------------------------- Memory usage | 54.6G | 58.8G (+7.7%) | 55.9G (+2.4%) ----------------------------------------------------------------------------- max_ptes_none=409 means that any THP that has more than 409 out of 512 (80%) zero filled filled pages will be split. To test out the patches, the below commands without the shrinker will invoke OOM killer immediately and kill stress, but will not fail with the shrinker: echo 450 > /sys/kernel/mm/transparent_hugepage/khugepaged/max_ptes_none mkdir /sys/fs/cgroup/test echo $$ > /sys/fs/cgroup/test/cgroup.procs echo 20M > /sys/fs/cgroup/test/memory.max echo 0 > /sys/fs/cgroup/test/memory.swap.max # allocate twice memory.max for each stress worker and touch 40/512 of # each THP, i.e. vm-stride 50K. # With the shrinker, max_ptes_none of 470 and below won't invoke OOM # killer. # Without the shrinker, OOM killer is invoked immediately irrespective # of max_ptes_none value and kills stress. stress --vm 1 --vm-bytes 40M --vm-stride 50K v3 -> v4: - do not clear partially_mapped flag on hugeTLB folios (Yu Zhao). - fix condition for calling deferred_folio_split in partially mapped case and count for partially mapped vm events (Barry Song). - use non-atomic versions of set/clear partially_mapped flags (David Hildenbrand) - use PG_partially_mapped = PG_reclaim (Matthew Wilcox) - delete folio from lru list and folio_batch_add "new_folio" instead of folio in __split_huge_page. (Kairui Song) - fix deadlock in deferred_split_scan by not doing folio_put while holding split_queue_lock (Hugh Dickins) - underutilized to underused and thp_low_util_shrinker to shrink_underused (Hugh Dickins) v2 -> v3: - Use my_zero_pfn instead of page_to_pfn(ZERO_PAGE(..)) (Johannes) - Use flags argument instead of bools in remove_migration_ptes (Johannes) - Use a new flag in folio->_flags_1 instead of folio->_partially_mapped (David Hildenbrand). - Split out the last patch of v2 into 3, one for introducing the flag, one for splitting underutilized THPs on _deferred_list and one for adding sysfs entry to disable splitting (David Hildenbrand). v1 -> v2: - Turn page checks and operations to folio versions in __split_huge_page. This means patches 1 and 2 from v1 are no longer needed. (David Hildenbrand) - Map to shared zeropage in all cases if the base page is zero-filled. The uffd selftest was removed. (David Hildenbrand). - rename 'dirty' to 'contains_data' in try_to_map_unused_to_zeropage (Rik van Riel). - Use unsigned long instead of uint64_t (kernel test robot). Alexander Zhu (1): mm: selftest to verify zero-filled pages are mapped to zeropage Usama Arif (3): mm: Introduce a pageflag for partially mapped folios mm: split underused THPs mm: add sysfs entry to disable splitting underused THPs Yu Zhao (2): mm: free zapped tail pages when splitting isolated thp mm: remap unused subpages to shared zeropage when splitting isolated thp Documentation/admin-guide/mm/transhuge.rst | 16 ++ include/linux/huge_mm.h | 4 +- include/linux/khugepaged.h | 1 + include/linux/page-flags.h | 11 ++ include/linux/rmap.h | 7 +- include/linux/vm_event_item.h | 1 + mm/huge_memory.c | 145 ++++++++++++++++-- mm/internal.h | 4 +- mm/khugepaged.c | 3 +- mm/memcontrol.c | 3 +- mm/migrate.c | 75 +++++++-- mm/migrate_device.c | 4 +- mm/page_alloc.c | 5 +- mm/rmap.c | 5 +- mm/vmscan.c | 3 +- mm/vmstat.c | 1 + .../selftests/mm/split_huge_page_test.c | 71 +++++++++ tools/testing/selftests/mm/vm_util.c | 22 +++ tools/testing/selftests/mm/vm_util.h | 1 + 19 files changed, 346 insertions(+), 36 deletions(-)