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 495D2C3DA4A for ; Thu, 1 Aug 2024 16:22:11 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9DD0C6B007B; Thu, 1 Aug 2024 12:22:10 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 98CBE6B0082; Thu, 1 Aug 2024 12:22:10 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 854426B0083; Thu, 1 Aug 2024 12:22:10 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 63E296B007B for ; Thu, 1 Aug 2024 12:22:10 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 1C880A7718 for ; Thu, 1 Aug 2024 16:22:10 +0000 (UTC) X-FDA: 82404193620.14.C235516 Received: from mail-ej1-f41.google.com (mail-ej1-f41.google.com [209.85.218.41]) by imf04.hostedemail.com (Postfix) with ESMTP id 246514001C for ; Thu, 1 Aug 2024 16:22:07 +0000 (UTC) Authentication-Results: imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=HYVmskqu; spf=pass (imf04.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.218.41 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=1722529283; 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=PuJ6lXbYLk7DX9qMsWnojirrTp2KPxnd0ZQH8Ct4y8Q=; b=T87Q+p9kUcOxG/2IAsjuCdJKSLw/jyFqX+KSmDSlcp3J9+SBLo/+GrGv4ehl1bkZWWjXty PTsOijgLoihSl0c4bfqGNP4oUCpjxkG72L1llNsAzy8vyGfXvEO9OJ2LqBIMQeDXhdgWRE JQQcP8jenQ7EA7qWy7iDsKgAtoUGA20= ARC-Authentication-Results: i=1; imf04.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=HYVmskqu; spf=pass (imf04.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.218.41 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=1722529283; a=rsa-sha256; cv=none; b=itlnSO11bocy9tVQ5Ta6gFCPscP/zmZhEV1TE1FXaUbJz+U8ii35gZ3eC4m0NbRQ8xXopO i2UD/wBREiX3eHRL9rb/4f3VRi4b5XebLf135/VgTzYPdK87tYfaz4C9pfw1jxVaSJs6lE XWJuJhNeTt3wQolaRbYZM/fQZhTyxMA= Received: by mail-ej1-f41.google.com with SMTP id a640c23a62f3a-a7ac449a0e6so614058766b.1 for ; Thu, 01 Aug 2024 09:22:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722529326; x=1723134126; 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=PuJ6lXbYLk7DX9qMsWnojirrTp2KPxnd0ZQH8Ct4y8Q=; b=HYVmskqu4Ct5n33aBn0cqOG70NIew67pdNbYPafAvqlQFKen5wK+m0HHB+y213Z2fd p6qR++eS4FW4Kj+GgxynXZG9hIpbeGV6HiUStU/QlZvZm0i9Rg89AWyZbxEEsTOE6GVX IwC8LYQt2U78IjScaucftepBGjP6LtWN241r5eSqni0+BMwWFuXR8atr1PfnWBUJSutg fFtwwJnskf8/5tpAsXh7K5T6Wg11f5Q7qHN3cBp28azSQ3j9VNLQvyWdCnAxzvVk8Mhi sZJGNzdRdoaYLLsNJdRc8otoAkwptfa7Xj+UOX/aKeCwnM1pHMSrvfGRWhQiSx62yKVz 9WCA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722529326; x=1723134126; 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=PuJ6lXbYLk7DX9qMsWnojirrTp2KPxnd0ZQH8Ct4y8Q=; b=PON4vbYtv+0BzgNJ5+FagOzN2JjXLtXmUj2S7ZfgM/PXwOovAaLDOag+hDGx9OOR1K q+2zFCmBYVdVLbPnmROhtnrSA7A2r0JB4302PXJ0Usi4PW7e3qfAESLod3iumdMP7myN hZ+IZwLbhRjhhEjJ3pKm8EYlBxBhGgEsWeK1W2x98jKE5qC6KcWk0RCis7QVMV99txqj j25oyOwQwYzhsFwUziAjQ4BnytsW/TpeJvcQCuW2ewIAGM5NpVHb/qNIPn2W6iOLTLMC 5xyN5tKPcFjkJRppb+SQ4gH+pul6vjT/QGd/hLJhjkt5NWKqHAGcXJEX+cV/adb4zf7m bSjw== X-Forwarded-Encrypted: i=1; AJvYcCW9Z7BCgqjL1xjOKxDcY70OLFIjkAFhhTDGd+TQraeqsR2Zs0a8fQVLe2Ee4lPyZYIDk9jfTVZyifVTfWKVAv5I4eU= X-Gm-Message-State: AOJu0Yxb4CNEp0FZtBy6gtGJ2LrgER42yAzI7M9KD//VXtCeIhz87ATO e3uQPeb2KccBF8udo83xySrMjbe/n2VvCgv7iKe/v2jd7PaXdpig X-Google-Smtp-Source: AGHT+IHUF86fV2+L7QYO8RxkMKSYjX9iXLEtL6b3ZfmbzbJ7ry+ksWZVdOONka7cVveOF84vxazpIA== X-Received: by 2002:a17:907:7f17:b0:a7d:2bf7:5619 with SMTP id a640c23a62f3a-a7dc50fda98mr53761666b.58.1722529326055; Thu, 01 Aug 2024 09:22:06 -0700 (PDT) Received: from ?IPV6:2a03:83e0:1126:4:eb:d0d0:c7fd:c82c? ([2620:10d:c092:500::4:1f87]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a7acadb9dd3sm921008466b.220.2024.08.01.09.22.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 01 Aug 2024 09:22:05 -0700 (PDT) Message-ID: Date: Thu, 1 Aug 2024 17:22:05 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 0/6] mm: split underutilized THPs To: 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, david@redhat.com, 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> Content-Language: en-US From: Usama Arif In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Stat-Signature: mmkmb88t848xpeoex6khg9gncsxi9ggk X-Rspam-User: X-Rspamd-Queue-Id: 246514001C X-Rspamd-Server: rspam02 X-HE-Tag: 1722529327-136861 X-HE-Meta: U2FsdGVkX1+fIx1XLPvxJP9m0aFr/U/BfHwhApnBRcVoblpPKIhGYMqL5D5zhdJYTY7HaRYSStaS8dVP07/nosB0MLl3xn8xGpdfqwGJDfTJ5CJhCYnqkHKEMazQ71x1e8d8UZaNU3Kj8PwkZTvHIj0IjkJWtCVEY5VPqxZkJEpHrEOh3AFKT6RJBLyCxVHkpKaqeAP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REuPe1vmtkr64sR/vTFuOeWo1QV8wVX6UnGiKx0B09gp+z2PznVbJ4sSAPM6sshDh8fz3bEKsKXkJyvsNzm0gq1sfaTOZZWae4hok1uiULkt2r0uZyXzWy5ryPcSuV1/gl6tl8mkLqSgPp7Gkf+YkD/RsOYF1EPJgcRev5erd8eu/GjPYg+Q3dzc7fZMKXwWCP4v1QvsJ+Z9ab7ytXig5OQDm27nOe2XyfpXxyG0ZGKQFKtMdXHVKUNA+73zNqdJKRv1nBasUX6wil15KEiaIRP5ArtJMYJcQ2FEozbxqA5ik85idyAjqlD45qWuOhcCbNtL7NhoOMg= 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 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. 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?