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 10DB0C3DA49 for ; Tue, 30 Jul 2024 15:19:44 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 9F1166B00AF; Tue, 30 Jul 2024 11:19:43 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9A1346B00B0; Tue, 30 Jul 2024 11:19:43 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8689F6B00B1; Tue, 30 Jul 2024 11:19:43 -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 685906B00AF for ; Tue, 30 Jul 2024 11:19:43 -0400 (EDT) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 0EC2D80369 for ; Tue, 30 Jul 2024 15:19:43 +0000 (UTC) X-FDA: 82396778646.17.15C9F64 Received: from mail-ej1-f46.google.com (mail-ej1-f46.google.com [209.85.218.46]) by imf28.hostedemail.com (Postfix) with ESMTP id C91ABC0006 for ; Tue, 30 Jul 2024 15:19:40 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=EESDpOPD; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf28.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.218.46 as permitted sender) smtp.mailfrom=usamaarif642@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722352740; a=rsa-sha256; cv=none; b=jR9wmaC4z/IGCAIiDHp2LkTWf9+7ePWgyxGAAnXjTm/YS7R6BHsFXMvVl0E9iItG1M+r39 9GE4G38VyDB5VM8VkvxNU55dXJtQAJUQEIjH2z0m/6KRDr8Ib5UTPlHsmc3Q/yroBizPyC ylAEpTDMkF76yMEvZmYaSyqbmAz5oXg= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=EESDpOPD; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf28.hostedemail.com: domain of usamaarif642@gmail.com designates 209.85.218.46 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=1722352740; 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=eHB8wsNtQM0jQmp43r431yPVklm/f1PUXfgTnNmnzKA=; b=APFqXcpUYvTPXiIJiepfPVvrJIC51+vXLcWXweAXMtAX1KxzxP8EVQxOhFebMnKUWtvNYM 2IZfYW38C1C4fP5jbGkVqSu1RRV50aIBlImh8Tfr9oRhSEbOtxFtnpGlg3fwpwrCNFnG6R IT1CB2YVA6u88Ve054qY4fbDipUeOf0= Received: by mail-ej1-f46.google.com with SMTP id a640c23a62f3a-a7a81bd549eso442431766b.3 for ; Tue, 30 Jul 2024 08:19:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722352779; x=1722957579; darn=kvack.org; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:from:subject:user-agent:mime-version:date:message-id:from:to :cc:subject:date:message-id:reply-to; bh=eHB8wsNtQM0jQmp43r431yPVklm/f1PUXfgTnNmnzKA=; b=EESDpOPDtspxoZ/Y8QkfoFkjLC09LdvpYTxLa+M4nvpkCX+Vn9XPGqSpZBjpDYd6li bzqX3/PsT4gZjmPadnjBGP+786ehp/dVdhRUTZYthHAkQ1ABPdyx+hlTqv7h7vDeu54/ qLLiD+dw4IfAK/XJ9ocWdbtIGHNYNAAiAHVt6otRBIE7KzViedqFImrNI+aAFkGpmcKO NIF702dAvXnJNPSaGkDZndLxS5Qn4fNmeB/NrJuM9CRMkdI8JeXRYnWu7JXz9ZqkwJQW ig8ZCPen7ZPuHbICY0nm7Mxwq7D3D1sDxQPufAck92YdX62OVdhADCP5thk3QHnMOgK8 fGIQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722352779; x=1722957579; h=content-transfer-encoding:in-reply-to:content-language:references :cc:to:from:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=eHB8wsNtQM0jQmp43r431yPVklm/f1PUXfgTnNmnzKA=; b=Zjk/OLZB8by7ts0ymwKRcWrerCnMBRBM0pwYNIVR/8h9Ab33uT2GM9oO6uhWZWA0SB 2XiItDAUyP7cfKh60Kcq+hx1Otli63XwIDrhdROSWl3oP83Rr7Cayu1C0nDIt6i0lTLL 1sikQYHxEc4O2vrQ2lT7KiYMdkRtj7J2tNFUr1O4QkRJaRAMNlItDcmEkcpNI/3Q2Lil Nwzeg8V6D1+cYb+wUlGyLNyNbR2YsznCMiiEzBLTZAerLd1mUuoO5eXxiTC/lVeD2k7P e+X70dorc4j7E30diK/Ze+GCBNtMxa4SZavSv4LeoUUace607wi/X7nQ6WLGWuBLurhL AXvQ== X-Forwarded-Encrypted: i=1; AJvYcCVAkKaD2zkhMS9HMHUUrnAln2OjQZLtj7W9Ct/BJIrsYUqTyhldN8cKCHzcJYbR1VCMnWel1Y/QetseFoz9aWH+slE= X-Gm-Message-State: AOJu0YwbJcbF6V3r9CmTlSIOZ21fZXBbUsR0qxyEHiuLRetjW3buMOPs zGiJaCCrd6iSJWEIqSrNNxJcYYG4cl1CtPfj3w9YCItoJ4woryC5 X-Google-Smtp-Source: AGHT+IGs11AYO4xwAn5557+PuXhwriowqcH1vobWUOSOVDyy55bquyq/3Qmck1ti02y41QEfv9gl7Q== X-Received: by 2002:a17:907:7209:b0:a77:c84b:5a60 with SMTP id a640c23a62f3a-a7d4005a6b7mr865847166b.26.1722352778866; Tue, 30 Jul 2024 08:19:38 -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 a640c23a62f3a-a7acab22ff1sm655739566b.35.2024.07.30.08.19.37 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 30 Jul 2024 08:19:38 -0700 (PDT) Message-ID: <73b97a03-3742-472f-9a36-26ba9009d715@gmail.com> Date: Tue, 30 Jul 2024 16:19:37 +0100 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 0/6] mm: split underutilized THPs From: Usama Arif 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> <3cd1b07d-7b02-4d37-918a-5759b23291fb@gmail.com> Content-Language: en-US In-Reply-To: <3cd1b07d-7b02-4d37-918a-5759b23291fb@gmail.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: C91ABC0006 X-Rspam-User: X-Rspamd-Server: rspam05 X-Stat-Signature: fobjyj8haq181ixocdugyo3rokpdukr4 X-HE-Tag: 1722352780-579561 X-HE-Meta: U2FsdGVkX19IEdbfjGzC6BNLToROM4B5FIH0Zz1W/3NC7NOfQPa4g46qTUVcjWVzrJ1eLLkyhQJHrEQ5ziMDDU/3ixK7/YoXeOG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D1BFOpgf6OSuEzdfClZlRiYNOrBO6WA+AtX/2EauUETXAwWDn7eaZcq99gCj9iiyZZBYCbN3oYWkZuzklFhl692Yuia4tYshd3U6AG5rAU2cXHzqSaepSGHnJrOMj1DWr9+K93NjEDYX/UovytBcrHRSlyi0l0hJkM6j/bb7Oopl6sNbJX78vxg/oUGUOH+zya4YjU298e1QK0rARSCuOsNKkw4WQJ/Qe8wuM0qMQ5XVKe73dQbFRBqIoaE/Lhvod0FNkGVHk5gzBI6jIM8J0HMyj4TKPDj2s7KRX1oZvaCWFbI5B0JmOprs8/LUiogbwhG7dqIuLRcNAioN3QSu5caB3F+mv9+UTOQUKykOP7xUHIBvdM823P/BtXLKmtC1bn7grZXS/lXIdiPTc583 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000035, 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 16:14, Usama Arif wrote: > > > 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); > } Ah are you suggesting userfaultfd_armed(pvmw->vma) will evaluate to false even if its uffd? I think something like below would work in that case. diff --git a/mm/migrate.c b/mm/migrate.c index 2731ac20ff33..52aa4770fbed 100644 --- a/mm/migrate.c +++ b/mm/migrate.c @@ -206,14 +206,10 @@ static bool try_to_unmap_unused(struct page_vma_mapped_walk *pvmw, if (dirty) return false; - pte_clear_not_present_full(pvmw->vma->vm_mm, pvmw->address, pvmw->pte, false); - - 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); - } + 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); dec_mm_counter(pvmw->vma->vm_mm, mm_counter(folio)); return true;