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 1B195C54E41 for ; Tue, 27 Feb 2024 09:51:19 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id A09E694002F; Tue, 27 Feb 2024 04:51:18 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 9BA46940008; Tue, 27 Feb 2024 04:51:18 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 88D6F94002F; Tue, 27 Feb 2024 04:51:18 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id 77E64940008 for ; Tue, 27 Feb 2024 04:51:18 -0500 (EST) Received: from smtpin17.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 4CBE214078F for ; Tue, 27 Feb 2024 09:51:18 +0000 (UTC) X-FDA: 81837115836.17.78ED9BE Received: from mail-yb1-f180.google.com (mail-yb1-f180.google.com [209.85.219.180]) by imf16.hostedemail.com (Postfix) with ESMTP id AC55D18000D for ; Tue, 27 Feb 2024 09:51:16 +0000 (UTC) Authentication-Results: imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=cnK8YXgA; spf=pass (imf16.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.219.180 as permitted sender) smtp.mailfrom=ioworker0@gmail.com; dmarc=pass (policy=none) header.from=gmail.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1709027476; 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=GLfwVegLj3NllSkhisIJaN4ho41gslKZMK9c3d+Tq3A=; b=KTghUm2HerNy83BtWesiusI8WTA2r1vSxMHvKEDR4iM029t7DuCUuDnHjGia1OJLyxN04q uPRXd3cGpLwhRZeLCVFbxqHiYCu4BSWmDAWZNycXc/E16ffS5eZ3YvBWFc+8kaV2Bdc/oA FRqRO32sGzuW0l68K2eNOIb4cOYIS0I= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1709027476; a=rsa-sha256; cv=none; b=ThZVglhS63j8Y1wsEXgBXQ+RFYtyiqy1UY/fXqca9x+nVhCt/5FY/xwlwhRTkibNyJXDaT OfQJM7CEePYIgAG2ssQu30ATBKhfSMf/0oNz8wYVHUR1QHWr8syTZ2VayTN4HMO0IHoRSf bHHAc4xJMZEx2zUY2uBANkPCqOttQZY= ARC-Authentication-Results: i=1; imf16.hostedemail.com; dkim=pass header.d=gmail.com header.s=20230601 header.b=cnK8YXgA; spf=pass (imf16.hostedemail.com: domain of ioworker0@gmail.com designates 209.85.219.180 as permitted sender) smtp.mailfrom=ioworker0@gmail.com; dmarc=pass (policy=none) header.from=gmail.com Received: by mail-yb1-f180.google.com with SMTP id 3f1490d57ef6-dcbc6a6808fso3314888276.2 for ; Tue, 27 Feb 2024 01:51:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1709027476; x=1709632276; 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=GLfwVegLj3NllSkhisIJaN4ho41gslKZMK9c3d+Tq3A=; b=cnK8YXgAt9T3O1DN28pv8UcOM4+4aKxswC0vVrVGqIM5yxENuDscYjfn7LzParOXp0 NGPa5JA+3LF52fZiWx71UZz3nvWx6q07EUcL9muZ7F2lUbLu5Y86vgm/tVuyhPvMBrX6 PUR4Ot+NK3XvM2yic7g0HqbcXOnbpya5agZNpnYLdCZPweGwFP/BiTpNwT9tHvJP4N9r J9f4Yu6MViTb4YdYnhdBP4E+gIxMZY5WUs5fESmmOVLlIUywUUaxGzuvjJqklG9HEKCs 5KGCzVDClBa2/dcm9C5mkQ89bxoDN6xiBdh9z6Ytwpo/dN7aJvuWm827jVOIkA2O1jYY Fksg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709027476; x=1709632276; 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=GLfwVegLj3NllSkhisIJaN4ho41gslKZMK9c3d+Tq3A=; b=On0CJ0eqJlxWr6+ITYUUFMGdzFhKr9MENYxZMiCsF/ba7gJ1Or9lL7/OdTpwgzemkE 0o+pcpycilSRPxslnVR1arUvvqOsVsUnAosvL4X73a5Ah4UNi3IFvysUs1OJBCnCyRKE 2gF5qJy8iu5kjldNTkvuwiZiv4V8AMSE9FGnYZkkCo/ksXGEOMQ3rIWyKA9nrOgMH2Ll /UzUqmVH6fTEmndYxyCauNuP+l1spIOx9JYPkZWNIAFPHptiao1id+XPUDLDk87SKxgv EAHGGXVKoZxMhdbX4tgr2GMuNNa+saZ8iITr0AL4q2Or9KCjR4P5jlp5vw83JnlaTjGv uD4g== X-Forwarded-Encrypted: i=1; AJvYcCWJTQZKVGJoTysSuP49G4LXIWgm/VHMtaQisadH2ktCqj41XsyRLx/ZkB0h/xglnOvqbJ9o/T9NCgsuP9r1KX8v2qA= X-Gm-Message-State: AOJu0YyB0OeZEoEawKsgP6cENKU2zJV/+YKcZSi2fT8/FRmtYG3Jc7sX WYfeL2Xomfmj8RdzG0CGeqruaT6S9I6CJh9raDtkTd9nRHEP1gq/qS1HCiEIWUop+59B51pR5Ox zmpp8iwLs2Z4rI6mmTUldumjF7NY= X-Google-Smtp-Source: AGHT+IEinYi93IuP4wYqcK9lifs9plKOa9iN+CuwNoBD+nMvGgbpqn7mazEDEhomyUZjmrJS0iPzWPmXlqu0U/yXuI8= X-Received: by 2002:a25:74d3:0:b0:dcd:5bfa:8184 with SMTP id p202-20020a2574d3000000b00dcd5bfa8184mr1600906ybc.39.1709027475695; Tue, 27 Feb 2024 01:51:15 -0800 (PST) MIME-Version: 1.0 References: <20240227024050.244567-1-21cnbao@gmail.com> <61b9dfc9-5522-44fd-89a4-140833ede8af@arm.com> In-Reply-To: From: Lance Yang Date: Tue, 27 Feb 2024 17:51:03 +0800 Message-ID: Subject: Re: [PATCH] mm: export folio_pte_batch as a couple of modules might need it To: David Hildenbrand Cc: Ryan Roberts , Barry Song <21cnbao@gmail.com>, akpm@linux-foundation.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Barry Song , Yin Fengwei Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Rspamd-Queue-Id: AC55D18000D X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: p9bjrstj4hkkrqdjffnkys3nfk7hfuaa X-HE-Tag: 1709027476-612210 X-HE-Meta: U2FsdGVkX18U6V1aPttjen83jZsdnVpnvL29cervwLH/sG5W+NakcVVNlgMu/NH7FXOnW3N/wYW7mUhgjpuPg9t6i1UBC4vC1GQIVIYuzIv6NmMyhp/oGMwOeV83tbqGpaSYjiidzbwwvLgR8/e9wl8TkwJtdOvvTxScIH4N2eRFIQPIZSYKMNPBuojejFGVKzQzBujYeOVVZIDRrZysX6yVEeqR9UkdOSgV8O6VOP+DDcZPA1j2aQk46BuRV2jE32q1k2SCbC7bmcQykjcWMsI0pJbWBhfuPMl4RFeuUxzSiEOvtr7dsFeeAIuXJhoMB6sdcpZxciRNe7mQOgMVPsofvl2rBT/u6B+sy5SGIfYNP691gpX+MWrZIrv4N8jfxpkgcMmdv7SRUNWmVmw/BNevSN6ZeXQSmXym9nvuzXThcugsk4d3jS6qzniWMdfr0qtvAF39wbTa5ezZ0b6tNvQ1jSrOJUYZR8/mkyaHiDPSMVJAHWHpQ9eXg8o2nAx82CuDUwBEqohblf1XKRZtsBI6mpvuxtKF4bVswrj7tlRt7x5M/nI0Yl+jLi3z2BYMpp7W/HkVeZNGU/Y5qLSHJGU17d4tAGY0web1aCnQIE/4HjFOh3HqOmFzpGLPJReSuxjZZlbSxuo/Yftoe3IUT+RYoywtUviDxbwGBuwRjJ73QpaZL4vPxP7wLgW32F+ngip8q4CXZiAATlo4W5sjWzXNinI0n6Y8Knzwy/Omvz+532zdTyK0jXvWirLooppJ+2rEeUWqj+cwnwlKy0KUdaJJFme2XVAmE16z6Cpy2lJlFCTb2O/DCPXSEyXEk5NNqGlFS+9khlA9ZTZ/q7Mjo1LWtKwVPElCtA5aRULcxGlXBZ7zQDWc88jyTojlRs0nFoc9Ife8/+tkzMOIPB3UsZgPlDlTkixEIJnzEtsrGLW53IuSQfKg3tO+uHCNKcKMqc2+O5hCSZ7vz5xts7j IxIgAgw6 Pfm9kTmCr44q6jLL/rRkJdj6TLP5xt2V+Gc4C8Ei1IbOWf797DPuPuxgrMkFdYgKdK0wfOQLToS6JKPK+tmHqVQn+8g== 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 5: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); Nit: IIUC, the pte that maps to the first page. - VM_WARN_ON_FOLIO(page_folio(pfn_to_page(pte_pfn(pte))) !=3D folio, folio); + VM_WARN_ON_FOLIO(pte_pfn(pte) !=3D folio_pfn(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? > > > > > 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()= . > > That way, the compiler can most certainly better optimize it also outside= of mm/memory.c > > -- > Cheers, > > David / dhildenb >