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 7D2C8C3DA64 for ; Sun, 4 Aug 2024 23:32:53 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D862C6B007B; Sun, 4 Aug 2024 19:32:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D35E86B0082; Sun, 4 Aug 2024 19:32:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BFDCF6B0085; Sun, 4 Aug 2024 19:32:52 -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 A0E4A6B007B for ; Sun, 4 Aug 2024 19:32:52 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 24CDC41AFA for ; Sun, 4 Aug 2024 23:32:52 +0000 (UTC) X-FDA: 82416165384.21.B54BAB0 Received: from mail-qt1-f170.google.com (mail-qt1-f170.google.com [209.85.160.170]) by imf17.hostedemail.com (Postfix) with ESMTP id 520C340013 for ; Sun, 4 Aug 2024 23:32:50 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Nd1hJhGs; spf=pass (imf17.hostedemail.com: domain of yuzhao@google.com designates 209.85.160.170 as permitted sender) smtp.mailfrom=yuzhao@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1722814309; 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=uSWV9wI/F0Nu15VWZNLks89MmE9d/uq9S3n4nyNsYwA=; b=GaBcCQ9dZ6MKdfrgvdaAItJLhWN3NSgBryvlJvtBp70aepgksxdOhxpQeHJB2olwSH00Or JjFXdc6s6fDp4qdBiWSG1HSCKfdWjk0ZrDads/stCtslpqmzhXAM95b8cmbLKTxxWvMfJc BLLISzAhTdUdDnzcIlkpSJbRA39g6eU= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1722814309; a=rsa-sha256; cv=none; b=z1B944qnVFPruyy0zRRt7BsRIlb0ZTnsjSw1ifE0ufHyFBV0f6OSccjASh4Xp5j7u6ZrFL ekEqC9H6zDBb5wlXqkFmWWRSsXsI5dtWxQb7KgZ81TmCoDSDfZulPsM/N4jpBqAKbWLMVM Tm/0aoUdEm5uzYu9mzaClTuTohsl5U8= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=Nd1hJhGs; spf=pass (imf17.hostedemail.com: domain of yuzhao@google.com designates 209.85.160.170 as permitted sender) smtp.mailfrom=yuzhao@google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-qt1-f170.google.com with SMTP id d75a77b69052e-45029af1408so206541cf.1 for ; Sun, 04 Aug 2024 16:32:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1722814369; x=1723419169; darn=kvack.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=uSWV9wI/F0Nu15VWZNLks89MmE9d/uq9S3n4nyNsYwA=; b=Nd1hJhGs9zXFt8P5er+pDXGbIm8/4qdu6TDpbFKxVS9gYWoKnUm/LGC6PQhAZ6KU6x +/ifG/NT1heNFbKK9EkKJ09s/CgfacYWMxgqcbAaj9t/KtNTqy0FKLDKrNYXJ6klyyAM bbx7JtEXouZKU44owHn0mR4Yx2IVu3ZDBzcyNS2E6D4cgH0SGwXcxvFptm/ewguKlPtX pulyVeJNJnJ7SOt4P1nMsr+06JnO5IzxmBhH5xHZmTC54JpdxQTRAZBt0fqb4GykRdKt vPIgkOpqHYSFOIrHjrO+RxOTPvAyj6M3rNFNgVBViLYwYuW99bAEwDL4YZhdXN7tspcc XX2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722814369; x=1723419169; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=uSWV9wI/F0Nu15VWZNLks89MmE9d/uq9S3n4nyNsYwA=; b=wqUHa3QbNmES6V1SaIDzvEUklOGYGUpLMNLjg1dPSwLkOryRscTxwk3u70HD8yd+SN PQwOeqrkZ+Se3LrhRFYeJCe+O+/IhqMEGleHGMsFBdEIIp7HcIeGcuYEjb218w5R3bO4 Nqtjxe0qyIUxMMvshU/65CLefbqHjZUd4aAblmwgiVis4QLIsUkhdPFMVH0k1C4OPbUV 7HFps+dkPRPUa0Kq4xobU+b3iPEcqAVTwO1S5axZdeFD/CN4/0/td2CUqnLrauK0VoT1 PAOFPBCgWeX3iUUlubJ+x/9mmTDFWZu0PdNiZGN4+xNUPjR3svW/Nh8Xgg4e33MK3wPw /A7A== X-Forwarded-Encrypted: i=1; AJvYcCUHzVe/1dceeCEOEhDtRtlaffcPrWesknAHZfhnG7fO0zESG/USyA8EDmbOY9m7QIPnVSEHwMNBgG7Vj/2QCZ8UB3w= X-Gm-Message-State: AOJu0YzzS4tke/3VL4r5d5biZhQpsl4LJfob69qChUEJ6c+q7vnzRaNb hh8tD+Lmuykw34f0UBv+L3SrtNwj6jpVp3KwX6QpTW8DSvZl3d80sFgYzZ2+5vJ/wKzFtcjx6w+ IDLjWWK+zQKfoZjeRxtSRUuUNkZSo1gUohIeo X-Google-Smtp-Source: AGHT+IEKdwDaVjjqnObnyYMP24sqgkjO6Jq8MKGY9wblctbckt6KWnjz0IRuV6kc/xAQ9GsHiF+mdFc3dQgN0OWF/nI= X-Received: by 2002:a05:622a:18a2:b0:447:e0a6:9163 with SMTP id d75a77b69052e-4519ad1cf8bmr3137471cf.14.1722814369217; Sun, 04 Aug 2024 16:32:49 -0700 (PDT) MIME-Version: 1.0 References: <20240730125346.1580150-1-usamaarif642@gmail.com> <524fb638-73b2-45e7-ae2c-7445d394ed50@redhat.com> In-Reply-To: <524fb638-73b2-45e7-ae2c-7445d394ed50@redhat.com> From: Yu Zhao Date: Sun, 4 Aug 2024 17:32:12 -0600 Message-ID: Subject: Re: [PATCH 0/6] mm: split underutilized THPs To: David Hildenbrand Cc: Usama Arif , 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 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Server: rspam04 X-Rspamd-Queue-Id: 520C340013 X-Stat-Signature: dnfmznkjdwk38jq7c4qzpgrwijatp3pq X-HE-Tag: 1722814370-659075 X-HE-Meta: U2FsdGVkX1+iLf19WCuCKzzH3S7Em6Wb3CO9F8KNRC/apa1Tv0OLHH02tDUYQbkubOMCc4H4Y/5NGK0/jcJYMvGPjQoCQ+FRMvJD2jVM+nW7TaUQrLyUMwtGtiQO0MVRNMBKbmZiREi/aJRXGluDwd84kbHYuIYS4BwJNnCrjzv+DlXGik2+n4fKG0kuwRUTNUFdqesAGnw7XMMrtHr4FC+UzCC3oVI6C5DJ4aLJgE3KU1lekiAbMTDgXBoiGaPYsQ7hfthZgeEzO2MyG2ESxtTYfzfqc6XQt+jSZtn8UvMKCmSgk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ity=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 Thu, Aug 1, 2024 at 10:27=E2=80=AFAM 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=E2=80=AFAM Usama Arif wrote: > >>> > >>> The current upstream default policy for THP is always. However, Meta > >>> uses madvise in production as the current THP=3Dalways 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=3Dalways. 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 per= formance > >>> (i.e. you dont know when the collapse will happen), while production > >>> environments require predictable performance. If there is enough memo= ry > >>> available, its better for both performance and predictability to have > >>> a THP from fault time, i.e. THP=3Dalways rather than wait for khugepa= ged > >>> 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 b= eing > >>> 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 underu= tilized, > >>> 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 atte= mpt > >>> to split that THP. Then at remap time, the pages that were zero-fille= d are > >>> not remapped, hence saving memory. This method avoids the downside of > >>> wasting memory in areas where THP is sparsely filled when THP is alwa= ys > >>> enabled, while still providing the upside THPs like reduced TLB misse= s without > >>> having to use madvise. > >>> > >>> Meta production workloads that were CPU bound (>99% CPU utilzation) w= ere > >>> tested with THP shrinker. The results after 2 hours are as follows: > >>> > >>> | THP=3Dmadvise | THP=3Dalways | THP= =3Dalways > >>> | | | + shrink= er series > >>> | | | + max_pt= es_none=3D409 > >>> ---------------------------------------------------------------------= -------- > >>> Performance improvement | - | +1.8% | +1.7% > >>> (over THP=3Dmadvise) | | | > >>> ---------------------------------------------------------------------= -------- > >>> Memory usage | 54.6G | 58.8G (+7.7%) | 55.9G (= +2.4%) > >>> ---------------------------------------------------------------------= -------- > >>> max_ptes_none=3D409 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_no= ne > >>> 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 o= f > >>> # 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 irrespectiv= e > >>> # 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 kern= el > >>> 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=3Dalways. 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, a= nd its using majority of the THPs, then reducing its performance doesn't ma= ke sense. > > > > I'm not sure if there would really be a performance degradation > regarding the THP, after all we zap PTEs either way. > > Shattering will take longer because real migration is involved IIUC. Correct, and that's by design. Also using it in the THP shrinker path isn't a problem. > > 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*4= K 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. Correct, and this is essential to our problem: we are under memory pressure with THP=3Dalways. Under this condition, we need to compare shatter with split + compaction, not with split alone. To summarize, the ideal use cases are: 1. split for THP=3Dalways with unlimited memory. 2. shatter for THP=3Dalways under memory pressure.