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 8964FC54798 for ; Tue, 27 Feb 2024 09:27:50 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B6095940013; Tue, 27 Feb 2024 04:27:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B102A940008; Tue, 27 Feb 2024 04:27:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9AFF8940013; Tue, 27 Feb 2024 04:27:49 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 8C62C940008 for ; Tue, 27 Feb 2024 04:27:49 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 5CA3F40B55 for ; Tue, 27 Feb 2024 09:27:49 +0000 (UTC) X-FDA: 81837056658.10.136AE3A Received: from mail-ua1-f48.google.com (mail-ua1-f48.google.com [209.85.222.48]) by imf15.hostedemail.com (Postfix) with ESMTP id BA1F9A0011 for ; Tue, 27 Feb 2024 09:27:47 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="VbJzpDO/"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf15.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.222.48 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1709026067; 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=7JnPwwMCUBnYLzcxQfOuWfmpjNJn/her9A9WUWKljo4=; b=HXbs9IQvt3RTYKWwNSkws+w+WBoD1KjNufSA8IRtzhAGYh216gGs+HYCcYHwDVNRfjGJJq qg+a338YQPfvLwFzAHIXszTRLrGU+p5oNzQAvLigkKUBBoHjAdmbV6kPh6BOkI6tqw+Sk1 oeXLQV6qjbPzumw9DDYhCAzpnCXch0M= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b="VbJzpDO/"; dmarc=pass (policy=none) header.from=gmail.com; spf=pass (imf15.hostedemail.com: domain of 21cnbao@gmail.com designates 209.85.222.48 as permitted sender) smtp.mailfrom=21cnbao@gmail.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709026067; a=rsa-sha256; cv=none; b=s3t1gKInUNrclBXFNcTkhx1vDfPGT9hOXXKQIwLtyOBboa+m1yxpruqlpTnyvP4waJyLpr McljfS81TQZ6R1epOsH+hvBsNpj/e1ZzAwsl2I/a4nqkrKfc4hYCS8lY77pbQ+yCdV8jcg q+uA2cJMHIVZG4PYUCvOKg4psid0pN4= Received: by mail-ua1-f48.google.com with SMTP id a1e0cc1a2514c-7d2a67daa25so2582370241.0 for ; Tue, 27 Feb 2024 01:27:47 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709026067; x=1709630867; 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=7JnPwwMCUBnYLzcxQfOuWfmpjNJn/her9A9WUWKljo4=; b=VbJzpDO/EqbeqT5EB1+Uj4NsUIafq54/OuAQ/k3LtGE7G4yZKcmC8G0iQWc8Fy9YX/ 1C7tsx49SnkPIpJHiWEp3+fKP1RuNgzQW2Lh5yIFo8MJSVw80042gnN4EPUxWRJao4xt K27tVaVDxS2kGpb3NHh+eifoWvrYbJsJDqtH8eQ1e6vDdfpUmG5PNm5wY7tzqGd5lPPk 4SMzFbwENpSl5Kfii/Lwcq5y63jqQqmgR/Egjl70R27h6S9P0YCKZA89v0Hy/4uFDqwU reVDQkrY60shWPC4f4NO+x1th/jmPeHMUwqr6zI0aMyZOO1J7XF2uwAaXuGVSbPdkWHH bjqw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709026067; x=1709630867; 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=7JnPwwMCUBnYLzcxQfOuWfmpjNJn/her9A9WUWKljo4=; b=COe/CU5SX9GQ7fgyGDkJk/zbPkpwuhWZBaEjZmNLuX9vqqGc2Ou5o8mpsLCfp4sKri tm5NSxgvNfdi5AqjjetQhgT/nzcrFD8jN9Hrd1DrIIi2Qzq66pVaUOzm6UFo21Z/lmin glLLuSEREbbUGyK+ACR+i0609UN9d1/89VLNQMqk133iWTYsWXN+ilwq/fyCXmNnRSi8 gZrx3wigplnizedwIbwshHxlZMHki0eYWURf/zz0Yu/MrE2DwrrKwePvtkDNUlaHbXa0 25q2oUj7DHneLySLjNib5NWOdM/+jFemITxiPi/ZslBHlZRzJWbu7vwREb6p7osbf47L vIYQ== X-Forwarded-Encrypted: i=1; AJvYcCXBYYYG3MrcXwELeaNh7kRZh5WyArGtLngidAE1cFFG/A59yDcvzjk3uFQgZXjBCbY6BLFS2GID+oDhulPkfDyhE0U= X-Gm-Message-State: AOJu0YyTuqmDe7unBf5163m1nYpM9UFSA7MYMQ/9snXJfu3y4e6xHQ6X TT/WgZSzjoyOqT0EU5btiPWlXdwnNpYF6cHdkej0/7rBggLn2g25oDcsLypp/hv9aK4R4AzLVlf HEskCZWH++qKx6BooDdsHX1WGfUw= X-Google-Smtp-Source: AGHT+IEYjjFXebZ9zBwdv+BbHOLU6dj330KP0FVJ/sz3eLmDyaisZp2vNDTwtybCbgh5YmPssbQIDaimF31wF5RL9Io= X-Received: by 2002:a05:6102:21c8:b0:472:61c9:818a with SMTP id r8-20020a05610221c800b0047261c9818amr66386vsg.13.1709026066888; Tue, 27 Feb 2024 01:27:46 -0800 (PST) MIME-Version: 1.0 References: <20240227024050.244567-1-21cnbao@gmail.com> <61b9dfc9-5522-44fd-89a4-140833ede8af@arm.com> In-Reply-To: From: Barry Song <21cnbao@gmail.com> Date: Tue, 27 Feb 2024 22:27:35 +1300 Message-ID: Subject: Re: [PATCH] mm: export folio_pte_batch as a couple of modules might need it To: David Hildenbrand Cc: Ryan Roberts , akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Barry Song , Lance Yang , Yin Fengwei Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: BA1F9A0011 X-Stat-Signature: upo1cdh4xz1y4cd1trjq6qwff7if41iz X-HE-Tag: 1709026067-803814 X-HE-Meta: U2FsdGVkX1+pQ/LL6rWEkV8nfyAHRFvKzVce0jyPCvCY4u8Ixic2ylUpNhnScuEEwFoRjod4KQIumA+9W+p0rx8rapEmofPtpLikg/Ig7oVuEtRMxwSMnKZTeG6mYNJc1308B8QqCrPqoAuS8R2EbrJUo6icsTT6KyX4VFNoLfS+cXGraTEce92uU9d168CfdmoJO+l2kRcpJoLKQstwHCT7uCzGg8SEL1ZgPLT0pWDbBf+4zT8sGS4AGZy45cGu5RZc8YbOROhHbDxh//fF9Cb5mOzFo4uYAjh+L41CVYmrDBuhJgg7+fF+RwLXAjCI81DtjLZPJx4GfHTWRNe1CdxJODFPn0FGNjEcQm+v3x1nrC18ICQZyIcUDWLQot8A/Jh4YAkrtR7KS+2uvD4+hzj5N7lj5qhv98BEOyA9MzKfHwBv0krG0MBF0KKI5CIB/dGVP+SrQKH8whDrbrcoWW+wWN64EhVZnmdsmh2AbnQxaTSc5Cy/tR66ck4EccBP5cUTQSlSAQkvIus3BfOh2bFdAlQU94huY01Lab4K87pzCm3TofzhhFKW4vSWWR6Z8r7v2CuYt+Cavm+NIlXlzYH8OyHuxKEC5HTSZLOyr/UUtYVP1Liz0Q43IS13wawcJuf9yoy9+rxU4Tr7hvLhO4zWZcUESFn28wpG80kzF819w2QWiANAOMHemeO5HaukA/lOjOQljIQODI9+w/owIbtLpjGFkuW2/xOLR5S4qXJlNhZ2jnpr5BR6HbsWwpmQjAVKCdUisqaxyZtW9zQUFeu4kVh22NzOrkNhQABsWlS0zZkU5jNxih+befmnEDDPpPWIFnI2rys6NDUaF3dr5GaNYWabW6BHokoPhfiGKGUUr3mWBVehu9tQyXOF2jyho7tooPNdJPNOMMvH1zyBFyfFA3MobNflia7Mlqb1oUCqKdcJSfkX3OmDd07ffT46+vrLH0308RHekRd6Lwf 8MQ== 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 Tue, Feb 27, 2024 at 10:14=E2=80=AFPM David Hildenbrand wrote: > > On 27.02.24 10:07, Ryan Roberts wrote: > > On 27/02/2024 02:40, Barry Song wrote: > >> From: Barry Song > >> > >> madvise and some others might need folio_pte_batch to check if a range > >> of PTEs are completely mapped to a large folio with contiguous physcia= l > >> addresses. Let's export it for others to use. > >> > >> Cc: Lance Yang > >> Cc: Ryan Roberts > >> Cc: David Hildenbrand > >> Cc: Yin Fengwei > >> Signed-off-by: Barry Song > >> --- > >> -v1: > >> at least two jobs madv_free and madv_pageout depend on it. To avoid > >> conflicts and dependencies, after discussing with Lance, we prefer > >> this one can land earlier. > > > > I think this will also ultimately be useful for mprotect too, though I = haven't > > looked at it properly yet. > > > > Yes, I think we briefly discussed that. > > >> > >> mm/internal.h | 13 +++++++++++++ > >> mm/memory.c | 11 +---------- > >> 2 files changed, 14 insertions(+), 10 deletions(-) > >> > >> diff --git a/mm/internal.h b/mm/internal.h > >> index 13b59d384845..8e2bc304f671 100644 > >> --- a/mm/internal.h > >> +++ b/mm/internal.h > >> @@ -83,6 +83,19 @@ static inline void *folio_raw_mapping(struct folio = *folio) > >> return (void *)(mapping & ~PAGE_MAPPING_FLAGS); > >> } > >> > >> +/* Flags for folio_pte_batch(). */ > >> +typedef int __bitwise fpb_t; > >> + > >> +/* Compare PTEs after pte_mkclean(), ignoring the dirty bit. */ > >> +#define FPB_IGNORE_DIRTY ((__force fpb_t)BIT(0)) > >> + > >> +/* Compare PTEs after pte_clear_soft_dirty(), ignoring the soft-dirty= bit. */ > >> +#define FPB_IGNORE_SOFT_DIRTY ((__force fpb_t)BIT(1)) > >> + > >> +extern int folio_pte_batch(struct folio *folio, unsigned long addr, > >> + pte_t *start_ptep, pte_t pte, int max_nr, fpb_t flags, > >> + bool *any_writable); > >> + > >> void __acct_reclaim_writeback(pg_data_t *pgdat, struct folio *folio, > >> int nr_throttled); > >> static inline void acct_reclaim_writeback(struct folio *folio) > >> diff --git a/mm/memory.c b/mm/memory.c > >> index 1c45b6a42a1b..319b3be05e75 100644 > >> --- a/mm/memory.c > >> +++ b/mm/memory.c > >> @@ -953,15 +953,6 @@ static __always_inline void __copy_present_ptes(s= truct vm_area_struct *dst_vma, > >> set_ptes(dst_vma->vm_mm, addr, dst_pte, pte, nr); > >> } > >> > >> -/* Flags for folio_pte_batch(). */ > >> -typedef int __bitwise fpb_t; > >> - > >> -/* Compare PTEs after pte_mkclean(), ignoring the dirty bit. */ > >> -#define FPB_IGNORE_DIRTY ((__force fpb_t)BIT(0)) > >> - > >> -/* Compare PTEs after pte_clear_soft_dirty(), ignoring the soft-dirty= bit. */ > >> -#define FPB_IGNORE_SOFT_DIRTY ((__force fpb_t)BIT(1)) > >> - > >> static inline pte_t __pte_batch_clear_ignored(pte_t pte, fpb_t flags= ) > >> { > >> if (flags & FPB_IGNORE_DIRTY) > >> @@ -982,7 +973,7 @@ static inline pte_t __pte_batch_clear_ignored(pte_= t pte, fpb_t flags) > >> * If "any_writable" is set, it will indicate if any other PTE besid= es the > >> * first (given) PTE is writable. > >> */ > > > > David was talking in Lance's patch thread, about improving the docs for= this > > function now that its exported. Might be worth syncing on that. > > Here is my take: > > Signed-off-by: David Hildenbrand > --- > mm/memory.c | 22 ++++++++++++++++++---- > 1 file changed, 18 insertions(+), 4 deletions(-) > > diff --git a/mm/memory.c b/mm/memory.c > index d0b855a1837a8..098356b8805ae 100644 > --- a/mm/memory.c > +++ b/mm/memory.c > @@ -971,16 +971,28 @@ static inline pte_t __pte_batch_clear_ignored(pte_t= pte, fpb_t flags) > return pte_wrprotect(pte_mkold(pte)); > } > > -/* > +/** > + * folio_pte_batch - detect a PTE batch for a large folio > + * @folio: The large folio to detect a PTE batch for. > + * @addr: The user virtual address the first page is mapped at. > + * @start_ptep: Page table pointer for the first entry. > + * @pte: Page table entry for the first page. > + * @max_nr: The maximum number of table entries to consider. > + * @flags: Flags to modify the PTE batch semantics. > + * @any_writable: Optional pointer to indicate whether any entry except = the > + * first one is writable. > + * > * Detect a PTE batch: consecutive (present) PTEs that map consecutive > - * pages of the same folio. > + * pages of the same large folio. > * > * All PTEs inside a PTE batch have the same PTE bits set, excluding th= e PFN, > * the accessed bit, writable bit, dirty bit (with FPB_IGNORE_DIRTY) an= d > * soft-dirty bit (with FPB_IGNORE_SOFT_DIRTY). > * > - * If "any_writable" is set, it will indicate if any other PTE besides t= he > - * first (given) PTE is writable. > + * start_ptep must map any page of the folio. max_nr must be at least on= e and > + * must be limited by the caller so scanning cannot exceed a single page= table. > + * > + * Return: the number of table entries in the batch. > */ > static inline int folio_pte_batch(struct folio *folio, unsigned long ad= dr, > pte_t *start_ptep, pte_t pte, int max_nr, fpb_t flags, > @@ -996,6 +1008,8 @@ static inline int folio_pte_batch(struct folio *foli= o, unsigned long addr, > *any_writable =3D false; > > VM_WARN_ON_FOLIO(!pte_present(pte), folio); > + VM_WARN_ON_FOLIO(!folio_test_large(folio) || max_nr < 1, folio); > + VM_WARN_ON_FOLIO(page_folio(pfn_to_page(pte_pfn(pte))) !=3D folio= , folio); > > nr =3D pte_batch_hint(start_ptep, pte); > expected_pte =3D __pte_batch_clear_ignored(pte_advance_pfn(pte, n= r), flags); > -- > 2.43.2 > > > > > >> -static inline int folio_pte_batch(struct folio *folio, unsigned long = addr, > >> +int folio_pte_batch(struct folio *folio, unsigned long addr, > > > > fork() is very performance sensitive. Is there a risk we are regressing > > performance by making this out-of-line? Although its in the same compil= ation > > unit so the compiler may well inline it anyway? > > Easy to verify by looking at the generated asm I guess? my aarch64-linux-gnu-gcc didn't inline it $ aarch64-linux-gnu-gcc --version aarch64-linux-gnu-gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0 Copyright (C) 2021 Free Software Foundation, Inc. $ nm -S -s vmlinux.a | grep folio_pte_batch 0000000000003818 0000000000000204 T folio_pte_batch > > > > > Either way, perhaps we are better off making it inline in the header? T= hat would > > avoid needing to rerun David's micro-benchmarks for fork() and munmap()= . actually tried this before trying extern, the problem is that we have to ad= d others into internal.h, for example __pte_batch_clear_ignored, which seems not API. are we comfortable to move that one to internal.h too? > > That way, the compiler can most certainly better optimize it also outside= of mm/memory.c > > -- > Cheers, > > David / dhildenb Thanks Barry