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 ADCD4C3DA49 for ; Tue, 30 Jul 2024 15:14:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 420976B0092; Tue, 30 Jul 2024 11:14:12 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3CFF86B00A9; Tue, 30 Jul 2024 11:14:12 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 298276B00AA; Tue, 30 Jul 2024 11:14:12 -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 0ADF86B0092 for ; Tue, 30 Jul 2024 11:14:12 -0400 (EDT) Received: from smtpin22.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id B8469120313 for ; Tue, 30 Jul 2024 15:14:11 +0000 (UTC) X-FDA: 82396764702.22.C4AA9AA Received: from mail-lj1-f182.google.com (mail-lj1-f182.google.com [209.85.208.182]) by imf20.hostedemail.com (Postfix) with ESMTP id A78221C002E for ; Tue, 30 Jul 2024 15:14:09 +0000 (UTC) Authentication-Results: imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=exOrrAhT; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf20.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.208.182 as permitted sender) smtp.mailfrom=usamaarif642@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722352445; a=rsa-sha256; cv=none; b=hQJsS4KtMxetbHydh0cMTgnoQsmO3/8BmNAGT+P4ibpWD+WgTDKt+gPzugF0SbtKIZ+tXk rWxuVIO0mccgpVwy7rlvfpVr6Nmcp3bwrfQ+Bw5p9qm9tw9prmonEdSfgKEAbCxUKCdaQb 2zeTGrLZMNRMIfzdJKYlqVsGUJnH8yk= ARC-Authentication-Results: i=1; imf20.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=exOrrAhT; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf20.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.208.182 as permitted sender) smtp.mailfrom=usamaarif642@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722352445; 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=+Di2/8YssPRfers/vmrU3+BsD5CicPCCbwFzOYDiAck=; b=rHJaGWhW63kiXR/qsPdp9g8jy02x4JkUSPG8XUeaNZ8EFWgT1rHDcmVOi25ZGHLbYiH8Z8 nurOfmMWAW120acICGGdJN33kELI1y77ot5BC1eBN7PFEUvnJnQZOYsL+all+QFOlD+i5s AZnm/P/A6HhFb7KvjDPprXd9KRGxau4= Received: by mail-lj1-f182.google.com with SMTP id 38308e7fff4ca-2f149845d81so8081311fa.0 for ; Tue, 30 Jul 2024 08:14:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722352448; x=1722957248; 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=+Di2/8YssPRfers/vmrU3+BsD5CicPCCbwFzOYDiAck=; b=exOrrAhTcCEoPoJZA92CjHOpU/Ra/A7E9yYOEW60l36xlRHv3XKZDzoiIFb7jgH1cE AXcFcrY1M8CTATPsM5f7E74sjsj35yoyxnq8+qWSUH+uEpxKZ1kDevOlyT+SW51wx9W1 PimXWiJaDryG3fg4uBa/yJtXxJi2m+Ka5lcUmPQ+c39tWs7xWVt9glCRkl3uCR12Vdcz Ybf41/MkXAkjnnTfK/CqN8J14FWYAPFZJjXlS+1C5WWa0pUfKQOMRz2SgslQ/lNxNjxJ HDYyjZgvecC1t0xzqeNWUkois+IHtFCtBCjq6BNZMlLWV8hUIctu+x7a7MA0acjcV0bI RJDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722352448; x=1722957248; 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=+Di2/8YssPRfers/vmrU3+BsD5CicPCCbwFzOYDiAck=; b=rEDqIgTfuwUxecr5zG2I5bEAvZBU/QL8DGZ4mQVH2EjJhpqQu8EjowLgza6glgwmtw kkubi3s6Az/h7b+L986/pXRvDGoA74YTz3TpJFmTzKbEX4thmD7y+iAg2F7oIGHiQgoS Q14yciHQsouR9YsIa+TmaNrp8eEppqzw9BY7AZcB9ox54m4eRIhintLzenhk1pa2I1lo +OAffgFAObySavTcP+rT0E+wEb9afvrZZZ4bGERq+sg/+1AiM1QQ2dsXwMMNQJS5pT4m pnnhXCh6CjFADupQx01UasgfpeeUd4vmJjzVGE9xq5eChVTWi4lvH52lWWZ4M9PTAjPj H/8g== X-Forwarded-Encrypted: i=1; AJvYcCVwmDo/VEz3eWO0FINRjVQ4rLDKTAUV7XiLJqpG5oFaa/98Ij88QbRss9+U6NDp5+PS5kerPYHVhDF6VBvm7j+iItM= X-Gm-Message-State: AOJu0Yy+DibQCGWK6SiaYUb0rpyc5q/qtdbJtiPtNGQvWhZy8xkjXvES wKv6zFTTeS5wF9h6islqiyglq7KiUOyhOuQ5cASJ9z4RVgBqI+o+ X-Google-Smtp-Source: AGHT+IE/Lt8axMzl77LE+H3Ini9UrgGDIsDI6nB4O+v5YpjSHw23iRYXLlWkI8h6dhjDCu1jBAI5LQ== X-Received: by 2002:a2e:330e:0:b0:2ef:2472:300a with SMTP id 38308e7fff4ca-2f12edfc2a1mr73952151fa.2.1722352447300; Tue, 30 Jul 2024 08:14:07 -0700 (PDT) Received: from ?IPV6:2a03:83e0:1126:4:eb:d0d0:c7fd:c82c? ([2620:10d:c092:500::4:6947]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5ac63590e7esm7306190a12.36.2024.07.30.08.14.06 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 30 Jul 2024 08:14:07 -0700 (PDT) Message-ID: <3cd1b07d-7b02-4d37-918a-5759b23291fb@gmail.com> Date: Tue, 30 Jul 2024 16:14:06 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 0/6] mm: split underutilized THPs To: David Hildenbrand , 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, 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-Rspam-User: X-Rspamd-Queue-Id: A78221C002E X-Rspamd-Server: rspam01 X-Stat-Signature: 85p8c1ztm94cbiyryyd6qgw8xmdnmyi5 X-HE-Tag: 1722352449-568944 X-HE-Meta: U2FsdGVkX19KulfV9WCtGrJozdQ3tbs71yY7sXI3KXH9zhTcHfuH+c8dUbbJuZLDOSAv96C16qslBBNTwPS4jD7ABIsdnoXD9PbzsIN9C4oRoSqZi6p67uninOY6SEHvUnl4mngSCRmKmYTO9SICRYXMonOQYTGE/lARi2+ZDu+cAmyvPw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8vF1aAtuT66FeRJeiXY7wHqjzkCKGEGZ6N4Dj7pGVWUdOoPUCyS8xw8hr33mmJeVqfKpCdRZedVzyh15RfTbnmA4X1G7i/HafC0BixHW4/r0Gww7qGrJMFJQmfXk0HB/ZabjveRxFeAgMD84QkeNezuNTJK2bpqlAKD2nQI/V4LC5E2RFwOLDOBxj+TFC+50GsXdapITOsjo+gMdk1V7rrTVfmEpAhCQLFHIJbAmmPGKbxqVxYBT1aY4JLNK9Dv7WRreJmB9CBIjx1hhqkQhjbSLTTCEbO6tiDT+Zqr9C+7IOagbmqb41JBHu 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 30/07/2024 15:35, David Hildenbrand wrote: > On 30.07.24 14:45, 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).) > > As shared previously, there is one issue with uffd (even when currently not active for a VMA!), where we must not zap present page table entries. > > Something that is always possible (assuming no GUP pins of course, which) is replacing the zero-filled subpages by shared zeropages. > > Is that being done in this patch set already, or are we creating pte_none() entries? > I think thats done in Patch 4/6. In function try_to_unmap_unused, we have below which I think does what you are suggesting? i.e. point to shared zeropage and not clear pte for uffd armed vma. if (userfaultfd_armed(pvmw->vma)) { newpte = pte_mkspecial(pfn_pte(page_to_pfn(ZERO_PAGE(pvmw->address)), pvmw->vma->vm_page_prot)); ptep_clear_flush(pvmw->vma, pvmw->address, pvmw->pte); set_pte_at(pvmw->vma->vm_mm, pvmw->address, pvmw->pte, newpte); }