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 854A6C3DA7F for ; Sun, 4 Aug 2024 19:10:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 176FB6B007B; Sun, 4 Aug 2024 15:10:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 127B06B0082; Sun, 4 Aug 2024 15:10:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id F30A86B0085; Sun, 4 Aug 2024 15:10:10 -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 D4B2A6B007B for ; Sun, 4 Aug 2024 15:10:10 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 574221C551C for ; Sun, 4 Aug 2024 19:10:10 +0000 (UTC) X-FDA: 82415503380.03.D21E1BF Received: from mail-wm1-f51.google.com (mail-wm1-f51.google.com [209.85.128.51]) by imf13.hostedemail.com (Postfix) with ESMTP id 587212000E for ; Sun, 4 Aug 2024 19:10:08 +0000 (UTC) Authentication-Results: imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=FpMCu2RE; spf=pass (imf13.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.128.51 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=1722798577; a=rsa-sha256; cv=none; b=k+SGuur2XxaxwebSgn+sODBMgbjd25IIpS7xcEFCsgUxebn4dn1KgfvdTqFz+9t0moboLz CqAXXGzgzKDKnm5fGr6kJ5gHmZb6a5AE0GLSpNKlqJySfg19JTkDHOqa/ndDAzxrSU6DvQ fdnoDNG9KsCDD3zX+kQfv150Pp2tezQ= ARC-Authentication-Results: i=1; imf13.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=FpMCu2RE; spf=pass (imf13.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.128.51 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=1722798577; 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:in-reply-to:references:references:dkim-signature; bh=OjpcQuLFfE0naciE0n9rdeGl+Jtzp1VMCC4VkdZxVU8=; b=u3P3TbF5QivIsGEDbqPu8PgAs9a+Oj2HJIDH04mdgEbgUkKVyGfHeCJmaoOURGJ2LshLVp J6BIPF25r/5Nc7TeYko6vv4/1xPS+E8QwX/IPXLoxVkwfS+sHgFQQ6ZQK0bDda3CCt/RNd ISnArdYnu1mziZaX4tOiy1xOT9dw3iE= Received: by mail-wm1-f51.google.com with SMTP id 5b1f17b1804b1-4281d812d3eso76814535e9.3 for ; Sun, 04 Aug 2024 12:10:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722798607; x=1723403407; darn=kvack.org; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=OjpcQuLFfE0naciE0n9rdeGl+Jtzp1VMCC4VkdZxVU8=; b=FpMCu2REjtjSiexRfhaTo9yUtrZC1jdIfHa9AdECIjgkpLtCy0bslCCe+XC6jIVvC7 Of/NB8s0fv/s7OCa4hcQ56dsZGG4jApu+URywmIYVOxnoF4ZRd0ukDJRaij4iwXhMKR0 7qv9NaWnUJmQmdSrTkzz8oOe90rTomHi3xwLRJ1edRgdXkagowLCC0zSpzjL8D7zkLjJ 8xgcVIhNHXrOBeA6dwtYCwnpKUEl8U83rUwrq7rtGHAxCXh2u9NnqZvYsK13+KD82RIy sTcDtn/nZdA8uac8SPpPEVNav0BPE2VSuS03xtFP4ac8Iv8Bpmb9cgwHHLc4yVEZCXfa Muow== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722798607; x=1723403407; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=OjpcQuLFfE0naciE0n9rdeGl+Jtzp1VMCC4VkdZxVU8=; b=ZJOeAXnOCxAAIpg0bh14W7doRuXNX2CA5/XoNf+AeeuzLNr/tgZGIFWkVMLEmJwKnV jcGK40APp403gYB6xCGqK8GSxFv2BotyASl6LNBpCPul8JHJysuE/IbO+cejGTPmuk8d WoyUcsKNhDGpgCsFjAXbBydxwBEdK/Fu8XDX40ioY+qI7WN9KTjQX26ZpVC+vigjUW3z Wq0LIvPqtJqNfyX7HaZbzq0aJGFEzgvKunF3HnskrX3ioqCCqJ8as1oh9ooj8XkmnGPC LYJ+CD6xJSGWqWTWoaZvlVPa/Juu+O01GSsTjTnp68RIFt8+Uak+7mKCNIa4+tKSN+Z1 ivWA== X-Forwarded-Encrypted: i=1; AJvYcCVZ3Eb++o7+f83ldiJ/E/afOhOogOYA2Ps6UxHYUCRHelYf6XGtKJU7EMUzQruzufWvqaSFcOcQ6bk6MAoA9EcROdQ= X-Gm-Message-State: AOJu0Yw1CKQj2tK1yjQuFcplu1r1dlis96UsRzJallMG7Zg6qS7HKaNg pzW8IT90yzlVpJqqNZIBLDVhXfCaiFhxxL2m+2XKaNu5E6IMAV24 X-Google-Smtp-Source: AGHT+IEMLXwtZcqtBqEnlV8z+R1encQdJiwkQymliNGWyWZdYEt5wfHLsr+0MqUlzc53E2Uoo3EKJw== X-Received: by 2002:a05:600c:3504:b0:427:f196:c549 with SMTP id 5b1f17b1804b1-428e6b7e954mr89635965e9.23.1722798606501; Sun, 04 Aug 2024 12:10:06 -0700 (PDT) Received: from ?IPV6:2a02:6b6f:e750:7600:c5:51ce:2b5:970b? ([2a02:6b6f:e750:7600:c5:51ce:2b5:970b]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-428e6e7cc3asm110306365e9.34.2024.08.04.12.10.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 04 Aug 2024 12:10:06 -0700 (PDT) Message-ID: Date: Sun, 4 Aug 2024 20:10:05 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 0/6] mm: split underutilized THPs To: David Hildenbrand , Yu Zhao Cc: akpm@linux-foundation.org, linux-mm@kvack.org, hannes@cmpxchg.org, riel@surriel.com, shakeel.butt@linux.dev, roman.gushchin@linux.dev, baohua@kernel.org, ryan.roberts@arm.com, rppt@kernel.org, willy@infradead.org, cerasuolodomenico@gmail.com, corbet@lwn.net, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kernel-team@meta.com References: <20240730125346.1580150-1-usamaarif642@gmail.com> <524fb638-73b2-45e7-ae2c-7445d394ed50@redhat.com> Content-Language: en-US From: Usama Arif In-Reply-To: <524fb638-73b2-45e7-ae2c-7445d394ed50@redhat.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Stat-Signature: f7ufp3h6tsr3t4epqe6gdckpgptqrutq X-Rspamd-Queue-Id: 587212000E X-Rspam-User: X-Rspamd-Server: rspam10 X-HE-Tag: 1722798608-750490 X-HE-Meta: U2FsdGVkX1+MTBU+7Xrn3wLiATEz/JBrVHyp8cJV66TjobZXQn6VXNTlG+oLngMVZwn4/HziLzSv0LeG4K+rjN/KcqcJKDX6QUSRitFXnyMKIl6D6Y7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aHkvra/4Ulg8yjh4ZedU8VIhYHHQ03Xivc6awd5/tfpHsZ8p5X5xf05igPBLJorLYfcy4rcQgwf6vaqWbUcmL+vfXEvwxjy5eOtvTOLgiUzwWxLVy+IdnhO1QhDjRrpBFLTQ2sDunJ68Fei7dahz8dZEtQpN2YqRYaKk1FGtmSQVdV+tJ6Wae5sMT3y/ulJfjUuapyHoLVFHqPaRuUMCp/qkmPqS+K5V99DCpeVSG2J9ecZczGeXtN+AvJm0rOGxsIrs46q6H+bK+j+SOtr0QalVYi5IXV776hMg2LXtrjM/uPukUFxT0HrWzzNBibk7uEEOT1pLPgC5QD7b/+rxsf28H9JmiOfpFJXkoBT4cPLcqkV6vO8LTXbMso2/0jvoEhjZjc= 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: On 01/08/2024 17:27, David Hildenbrand wrote: > On 01.08.24 18:22, Usama Arif wrote: >> >> >> On 01/08/2024 07:09, Yu Zhao wrote: >>> On Tue, Jul 30, 2024 at 6:54 AM Usama Arif wrote: >>>> >>>> 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 underutilized, >>>> 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 >>>> not remapped, 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 kill stress. >>>> stress --vm 1 --vm-bytes 40M --vm-stride 50K >>>> >>>> Patches 1-2 add back helper functions that were previously removed >>>> to operate on page lists (needed by patch 3). >>>> Patch 3 is an optimization to free zapped tail pages rather than >>>> waiting for page reclaim or migration. >>>> Patch 4 is a prerequisite for THP shrinker to not remap zero-filled >>>> subpages when splitting THP. >>>> Patches 6 adds support for THP shrinker. >>>> >>>> (This patch-series restarts the work on having a THP shrinker in kernel >>>> originally done in >>>> https://lore.kernel.org/all/cover.1667454613.git.alexlzhu@fb.com/. >>>> The THP shrinker in this series is significantly different than the >>>> original one, hence its labelled v1 (although the prerequisite to not >>>> remap clean subpages is the same).) >>>> >>>> Alexander Zhu (1): >>>>    mm: add selftests to split_huge_page() to verify unmap/zap of zero >>>>      pages >>>> >>>> Usama Arif (3): >>>>    Revert "memcg: remove mem_cgroup_uncharge_list()" >>>>    Revert "mm: remove free_unref_page_list()" >>>>    mm: split underutilized THPs >>>> >>>> Yu Zhao (2): >>>>    mm: free zapped tail pages when splitting isolated thp >>>>    mm: don't remap unused subpages when splitting isolated thp >>> >>>   I would recommend shatter [1] instead of splitting so that >>> 1) whoever underutilized their THPs get punished for the overhead; >>> 2) underutilized THPs are kept intact and can be reused by others. >>> >>> [1] https://lore.kernel.org/20240229183436.4110845-3-yuzhao@google.com/ >> >> The objective of this series is to reduce memory usage, while trying to keep the performance benefits you get of using THP=always. Punishing any applications performance is the opposite of what I am trying to do here. >> For e.g. if there is only one main application running in production, and its using majority of the THPs, then reducing its performance doesn't make sense. >> > > I'm not sure if there would really be a performance degradation regarding the THP, after all we zap PTEs either way. > By performance I meant time/CPU used up for migration. > Shattering will take longer because real migration is involved IIUC. > Yes, so thats what I want to avoid. If the system is CPU bound like the production workload I am testing, then spending any cycles on migration is going to make time/CPU performance worse. Also, shattering isn't merged upstream, and it wouldn't make sense to make this series dependent on shattering. >> Also, just going through the commit, and found the line "The advantage of shattering is that it keeps the original THP intact" a bit confusing. I am guessing the THP is freed? i.e. if a 2M THP has 10 non-zero filled base pages and the rest are zero-filled, then after shattering we will have 10*4K memory and not 2M+10*4K? Is it the case the THP is reused at next fault? > > The idea is (as I understand it) to free the full THP abck to the buddy, replacing the individual pieces that are kept to freshly allocated order-0 pages from the buddy. >