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 5ED8FEB64DD for ; Mon, 7 Aug 2023 05:24:49 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A0B9B6B0072; Mon, 7 Aug 2023 01:24:48 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 994A78D0002; Mon, 7 Aug 2023 01:24:48 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 80E808D0001; Mon, 7 Aug 2023 01:24:48 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 6DF716B0072 for ; Mon, 7 Aug 2023 01:24:48 -0400 (EDT) Received: from smtpin06.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 3438216065E for ; Mon, 7 Aug 2023 05:24:48 +0000 (UTC) X-FDA: 81096169056.06.886DF2E Received: from mail-qt1-f174.google.com (mail-qt1-f174.google.com [209.85.160.174]) by imf02.hostedemail.com (Postfix) with ESMTP id 600FB80012 for ; Mon, 7 Aug 2023 05:24:46 +0000 (UTC) Authentication-Results: imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=yfZfF2Ds; spf=pass (imf02.hostedemail.com: domain of yuzhao@google.com designates 209.85.160.174 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=1691385886; 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=/kSq9xcOo1BBr2m7q9JhO/3H55Aq03K2ZqsUKR1E/gQ=; b=1bWwIknbX+N7GavOTDnxpO84zPjK+n/iv/LnspLiuV0MWX2em9gykR+GEDj98IPS1bzYc9 PnggajmT/8jauHWg7pyGB73kvWeFkxZejgr2HoIMSF52P2npUbihBZh19sAT624HWlx/Pa MKjL1sBrlqBcKE1uPplIw89mts7U9YQ= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1691385886; a=rsa-sha256; cv=none; b=Ftt48278nFyutVZFrszqwo6MXS5GV9g7Y1gh6qpnlfDuGqK40OTkjLlm2tSfZ7H7tpLg6/ 4KSAa5jzn7EXJzyHN9JdcWC5nGe4rU0VHaIeYzUFZ63lPyLl7W93W2gT2JXwO/+Iz0t6IJ /5FGwOizkxtMBUOkXYBmOfxwJ50GaXc= ARC-Authentication-Results: i=1; imf02.hostedemail.com; dkim=pass header.d=google.com header.s=20221208 header.b=yfZfF2Ds; spf=pass (imf02.hostedemail.com: domain of yuzhao@google.com designates 209.85.160.174 as permitted sender) smtp.mailfrom=yuzhao@google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-qt1-f174.google.com with SMTP id d75a77b69052e-40a47e8e38dso323961cf.1 for ; Sun, 06 Aug 2023 22:24:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20221208; t=1691385885; x=1691990685; 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=/kSq9xcOo1BBr2m7q9JhO/3H55Aq03K2ZqsUKR1E/gQ=; b=yfZfF2Dsl5/ihnoOiwk2qA6Q0RxUu4/8uwXaz+lyogsMNgmMYKm29MgvOXtTR6Mvq5 Pfa0//Do4C/BviHl5b2YKaoxZJyD4C0p4y2eLQNJYUWlwG9FfIp2kSoLZD5NrFUXrKpw 3AsLEN3yAkTxAQqfXbmXkB9KnwjTC6bWysgjlgCsz19/5rK6IWFCh/iQSCTgvRoOrquz 9cBHkX0wfYG4MQ6ikRwCITOFBoDyQ24xpUbpRv48yOVvqW4AyxAF/YDgLXoHlGLz4st9 6+doB3697jMHvp0QuecvPnZ19aK9P8MaU4kShOFbdt/xIKU1gTee5wwfHTG87Nogw1ir xcIg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691385885; x=1691990685; 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=/kSq9xcOo1BBr2m7q9JhO/3H55Aq03K2ZqsUKR1E/gQ=; b=ADiOdD+H3oPFRv8kiPQPfmrR7vPf2nct/2a5TIoRXEsHW8zQ37KGntnloV19lzUVVY ReSNUpYS/yMlCFzYgoqmFY69LEeXYMNkz9qf+lYYe58TGvMfEthQhS2idC/JzlttKD3G SMD6t1EE1t/OCeTcR+MUdFMZPgW3bJdnWjKDigiAn8/aVLMNwP6lyopwlE/ByMCXovZO Rdabd6RyXzmwQRcyOVqvnyk1siXaRcTqU/IdDDJ3ziGRfxBDaIdGT0RWIZPn15S7QhBT udmw/8vXolhbUUsMPVa+35ss5KURYc9yclYW7WbHIkjXeC83ycj7aB1fDrmnV/wrWcCc Nt5Q== X-Gm-Message-State: AOJu0YzZWrnfGxtZ4M4myvkE2A7qbrfNO5y9xcB1JPHbQNY5WhYfr2uz aPBTzUocKg9Z7cHgYRvjVwaNNYL9MAZM4BykM0FkAQ== X-Google-Smtp-Source: AGHT+IHE4PMHH4FZemX2qPyeWntEc8HaNqjc/Ov4LZlU8uANNSlaPkMqWUjJYu3BdP5xUL1TlNPaoZbZ7nQV3g4khWY= X-Received: by 2002:a05:622a:81:b0:3f0:af20:1a37 with SMTP id o1-20020a05622a008100b003f0af201a37mr392332qtw.15.1691385885361; Sun, 06 Aug 2023 22:24:45 -0700 (PDT) MIME-Version: 1.0 References: <20230726095146.2826796-1-ryan.roberts@arm.com> <20230726095146.2826796-3-ryan.roberts@arm.com> In-Reply-To: <20230726095146.2826796-3-ryan.roberts@arm.com> From: Yu Zhao Date: Sun, 6 Aug 2023 23:24:08 -0600 Message-ID: Subject: Re: [PATCH v4 2/5] mm: LARGE_ANON_FOLIO for improved performance To: Ryan Roberts Cc: Andrew Morton , Matthew Wilcox , Yin Fengwei , David Hildenbrand , Catalin Marinas , Will Deacon , Anshuman Khandual , Yang Shi , "Huang, Ying" , Zi Yan , Luis Chamberlain , Itaru Kitayama , linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: 600FB80012 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: 1jr1nhumyjzfkiwoy3fywmjw3ti9wtwn X-HE-Tag: 1691385886-302879 X-HE-Meta: U2FsdGVkX18pKfku4RFZHR7Wf4NAjrlBJymu89BTP7VVT4a4hVE7qJXUnKo2IwGN8VR4fwzQR7ytL+LUO2qUdSc0PstIdAxkd0vhJdprKrsPBiH9f8WL/uCJ+wA5YydbTY2q+ZHywDJNaJcscDrHOy7hDS5E3NY2AaoG8aEOMkIObBoHv9lA3cyO/fmBvZ7vjgE46B7XsPbZgih8aO1gXzee8XPfCVWM872x/bOwbeeL78fG9X603/7Vo9W+TSfcKxtfKPRUdwHmDVUohaBsCfZEyOOyCxfSsuBCHkd/JkxpU2aduildUiuseOlPhmtDyY0VUbk3WjTQ90YuqXtlHAr2I4hoUY8AgZgSOf8YavAel7dTpkFsrV+Yv3x+x2iRFc6Fsfj25voNGrC8j47zvN2bDn1azYF0QbgWasYddT71dW+Yv1sTPEksFSgFV9J+hogILKMInXSjs/z9ER2jrS3kpZScmtCPIpRMDylwHm1l7ZbscGPB9oxCNCoAtTtecoWHP6LZ1dgA5uy5PyN3PGAqGrxKqc61tcQgf3tbQTlXOnV+XCZe/OEJsuvVgB1YmJkCW4PgjZNKsEnHpRw9CxcjavZj9/j0TQ9nWnV5skgMQK5TikZE7dQyLzb8xWXNgCQSDyEZEr6iYKwRlvHUJXy6nRUVM5yRPPhnVAMbZ2bjCibJQZ7Wyl3EsUO5Dxa+j+L9LWNEYEbiFX4OAMy0RMdZTWHPaVTj8Wx++C/QbsCFusXhO5BIhPO4kYIS1h6RRudrKoCsxvu0KeQkuc/ASukAdtBqfGDpXFmRKOo5Loc0kr7i7pawRDg67lEPf2MEZtY7k5I1sPUIWPk6jsLvVUGXUPdRODFb/YlNJqBioly/jGGmKWbEyxh9IhPf0YDbxOp36XEZcqndOkFtuZlO0H/cfAzPdtszW+DN+NHkSDTp2LTNFBMwPgacuxQiz3i2YW6XwQHje/yFS1r+Y9N 7cTNmZPb raC/nD+e3Mdz2gdKgp4RKSPjVuGGgTDQRGQpa9RpaLsc0Z4/xke8k3at8SMQT7AzKsZmACwnoQ/SFBq4KHL5Ghgkcjr3xgt1CJnjbhbFHHfc+g7KYec9dLo84OSle3fnHx/dWE1hRgALctXezRyO3b+rZQEGXC7Y4LzkVWQBLqW4QMqGzUIybD5Ty72EwNgGu++2OIVKZgO+yOOfuENvchDMrYjToXfcdWaL5XzAHbc+DnIDdP7t0+DP+lh0pAwvd9AS4PJZgFaJ7ruRIYK+ijWmuoFWxvpvzOpBPSooZcXa/y1VM7u64fbVMQgQkYX+L2mxgLQ/j0RLj+JrML9Tqh/6BioXeZyOdrF1XwV730EdCSN8JncY2yq5yXNioHPSSEH+ynZQGHz36edGFZanRBJY4eg== 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: On Wed, Jul 26, 2023 at 3:52=E2=80=AFAM Ryan Roberts = wrote: > > Introduce LARGE_ANON_FOLIO feature, which allows anonymous memory to be > allocated in large folios of a determined order. All pages of the large > folio are pte-mapped during the same page fault, significantly reducing > the number of page faults. The number of per-page operations (e.g. ref > counting, rmap management lru list management) are also significantly > reduced since those ops now become per-folio. > > The new behaviour is hidden behind the new LARGE_ANON_FOLIO Kconfig, > which defaults to disabled for now; The long term aim is for this to > defaut to enabled, but there are some risks around internal > fragmentation that need to be better understood first. > > When enabled, the folio order is determined as such: For a vma, process > or system that has explicitly disabled THP, we continue to allocate > order-0. THP is most likely disabled to avoid any possible internal > fragmentation so we honour that request. > > Otherwise, the return value of arch_wants_pte_order() is used. For vmas > that have not explicitly opted-in to use transparent hugepages (e.g. > where thp=3Dmadvise and the vma does not have MADV_HUGEPAGE), then > arch_wants_pte_order() is limited to 64K (or PAGE_SIZE, whichever is > bigger). This allows for a performance boost without requiring any > explicit opt-in from the workload while limitting internal > fragmentation. > > If the preferred order can't be used (e.g. because the folio would > breach the bounds of the vma, or because ptes in the region are already > mapped) then we fall back to a suitable lower order; first > PAGE_ALLOC_COSTLY_ORDER, then order-0. > > arch_wants_pte_order() can be overridden by the architecture if desired. > Some architectures (e.g. arm64) can coalsece TLB entries if a contiguous > set of ptes map physically contigious, naturally aligned memory, so this > mechanism allows the architecture to optimize as required. > > Here we add the default implementation of arch_wants_pte_order(), used > when the architecture does not define it, which returns -1, implying > that the HW has no preference. In this case, mm will choose it's own > default order. > > Signed-off-by: Ryan Roberts > --- > include/linux/pgtable.h | 13 ++++ > mm/Kconfig | 10 +++ > mm/memory.c | 166 ++++++++++++++++++++++++++++++++++++---- > 3 files changed, 172 insertions(+), 17 deletions(-) > > diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h > index 5063b482e34f..2a1d83775837 100644 > --- a/include/linux/pgtable.h > +++ b/include/linux/pgtable.h > @@ -313,6 +313,19 @@ static inline bool arch_has_hw_pte_young(void) > } > #endif > > +#ifndef arch_wants_pte_order > +/* > + * Returns preferred folio order for pte-mapped memory. Must be in range= [0, > + * PMD_SHIFT-PAGE_SHIFT) and must not be order-1 since THP requires larg= e folios > + * to be at least order-2. Negative value implies that the HW has no pre= ference > + * and mm will choose it's own default order. > + */ > +static inline int arch_wants_pte_order(void) > +{ > + return -1; > +} > +#endif > + > #ifndef __HAVE_ARCH_PTEP_GET_AND_CLEAR > static inline pte_t ptep_get_and_clear(struct mm_struct *mm, > unsigned long address, > diff --git a/mm/Kconfig b/mm/Kconfig > index 09130434e30d..fa61ea160447 100644 > --- a/mm/Kconfig > +++ b/mm/Kconfig > @@ -1238,4 +1238,14 @@ config LOCK_MM_AND_FIND_VMA > > source "mm/damon/Kconfig" > > +config LARGE_ANON_FOLIO > + bool "Allocate large folios for anonymous memory" > + depends on TRANSPARENT_HUGEPAGE > + default n > + help > + Use large (bigger than order-0) folios to back anonymous memory= where > + possible, even for pte-mapped memory. This reduces the number o= f page > + faults, as well as other per-page overheads to improve performa= nce for > + many workloads. > + > endmenu > diff --git a/mm/memory.c b/mm/memory.c > index 01f39e8144ef..64c3f242c49a 100644 > --- a/mm/memory.c > +++ b/mm/memory.c > @@ -4050,6 +4050,127 @@ vm_fault_t do_swap_page(struct vm_fault *vmf) > return ret; > } > > +static bool vmf_pte_range_changed(struct vm_fault *vmf, int nr_pages) > +{ > + int i; > + > + if (nr_pages =3D=3D 1) > + return vmf_pte_changed(vmf); > + > + for (i =3D 0; i < nr_pages; i++) { > + if (!pte_none(ptep_get_lockless(vmf->pte + i))) > + return true; > + } > + > + return false; > +} > + > +#ifdef CONFIG_LARGE_ANON_FOLIO > +#define ANON_FOLIO_MAX_ORDER_UNHINTED \ > + (ilog2(max_t(unsigned long, SZ_64K, PAGE_SIZE)) - PAGE_SH= IFT) > + > +static int anon_folio_order(struct vm_area_struct *vma) > +{ > + int order; > + > + /* > + * If THP is explicitly disabled for either the vma, the process = or the > + * system, then this is very likely intended to limit internal > + * fragmentation; in this case, don't attempt to allocate a large > + * anonymous folio. > + * > + * Else, if the vma is eligible for thp, allocate a large folio o= f the > + * size preferred by the arch. Or if the arch requested a very sm= all > + * size or didn't request a size, then use PAGE_ALLOC_COSTLY_ORDE= R, > + * which still meets the arch's requirements but means we still t= ake > + * advantage of SW optimizations (e.g. fewer page faults). > + * > + * Finally if thp is enabled but the vma isn't eligible, take the > + * arch-preferred size and limit it to ANON_FOLIO_MAX_ORDER_UNHIN= TED. > + * This ensures workloads that have not explicitly opted-in take = benefit > + * while capping the potential for internal fragmentation. > + */ > + > + if ((vma->vm_flags & VM_NOHUGEPAGE) || > + test_bit(MMF_DISABLE_THP, &vma->vm_mm->flags) || > + !hugepage_flags_enabled()) > + order =3D 0; > + else { > + order =3D max(arch_wants_pte_order(), PAGE_ALLOC_COSTLY_O= RDER); > + > + if (!hugepage_vma_check(vma, vma->vm_flags, false, true, = true)) > + order =3D min(order, ANON_FOLIO_MAX_ORDER_UNHINTE= D); > + } > + > + return order; > +} > + > +static int alloc_anon_folio(struct vm_fault *vmf, struct folio **folio) > +{ > + int i; > + gfp_t gfp; > + pte_t *pte; > + unsigned long addr; > + struct vm_area_struct *vma =3D vmf->vma; > + int prefer =3D anon_folio_order(vma); > + int orders[] =3D { > + prefer, > + prefer > PAGE_ALLOC_COSTLY_ORDER ? PAGE_ALLOC_COSTLY_ORDE= R : 0, > + 0, > + }; > + > + *folio =3D NULL; > + > + if (vmf_orig_pte_uffd_wp(vmf)) > + goto fallback; Per the discussion, we need to check hugepage_vma_check() for correctness of VM LM. I'd just check it here and fall back to order 0 if that helper returns false.