Received: by 2002:a05:7412:3b8b:b0:fc:a2b0:25d7 with SMTP id nd11csp2529958rdb; Mon, 12 Feb 2024 07:48:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IEE7wMtHPp22p5ntUYVPj4Fqsr9XXKeF2Y01CvkMmVOL0Jsksc6ZmDDXl+dzO1JaFVphHtp X-Received: by 2002:a05:620a:1a27:b0:785:c033:8b74 with SMTP id bk39-20020a05620a1a2700b00785c0338b74mr7638132qkb.58.1707752881884; Mon, 12 Feb 2024 07:48:01 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707752881; cv=pass; d=google.com; s=arc-20160816; b=IIVMbYHxYghRJ93QTB3u2SuBZSvJw7SrmYrNQa9bbw7nOp3ls0GHzKIN6a9hmwk56Y A8inUit6OCHHM3b8Z14XajmVBVdn2yvAC3d4TFS2aEwrfsp8nwOhutHSdU2Rm/BLugdE wge8T/N0hHj/XBAq081YAn4BldI1oBvCLckEDD1JtnTyQUHE5gVir1BxV3HIqI9D93jm m9O53LVrQk572RVXzjIJ+0kO5zazNtCF0k76adQx++sXAc2hj0Dy+E2unQlVg1STdsSv /dde7TviXQW2ui+uQKe8iO38KrdzEc/HPEHO9r3J1UVN+s5G4qaZF7T5H4b49Q/FsSUW cvbA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:date:message-id; bh=rOudUpc6RxTx8ymu+MW/Q5v7OD25E5Y+IKYA1VsFyhQ=; fh=7PuoYpLs0d33OriL4RdYAUeCulaTsMcwA2ujtEIW10M=; b=LgaTBCk2PoiTNnwuTvTbwJe7kT7wwJ+M8wmWsPPqaTUVwpgrN7Mh66FR+pyJW/e4VG uGjJSTeWl0E2DnrdjJLJivSqPc36FXl2WehBjbmzPZ66uWQ9Hz7nG2Bci66SUZgAUcqD bmozU1wiyV7WiROscopSF7rKud+Ij6IXKUleaznZv5aM4YCtEWUnDBCP/jlXCF2j29UV cMF2wqiYT4mWOlvDOZIN4w2vkpqTCl18g70VrlA8Ob/1YfY8ztseZ5tTR169HR+HVWhF 43CR5c9cpYdJhfAY0QwVFdjhd4kqMtHeKhqJH71TgnwFmyk/m/llhRocS8O2NsWbSaGx +BOw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-61917-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-61917-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com X-Forwarded-Encrypted: i=2; AJvYcCWRfZsDclSnU0LoJWXHue/0lisaZVUChBoE+BEdKaPm+MrIe9nVyFQe3aKEk+fwgEYcBPux6P4c2cCRzdMgQjBFM2OvVXatvq9NGKx7Yg== Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id oo14-20020a05620a530e00b007858b5855b0si6017207qkn.325.2024.02.12.07.48.01 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 Feb 2024 07:48:01 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-61917-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=arm.com dmarc=pass fromdomain=arm.com); spf=pass (google.com: domain of linux-kernel+bounces-61917-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-61917-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 91CF41C21BB3 for ; Mon, 12 Feb 2024 15:48:01 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E82763CF49; Mon, 12 Feb 2024 15:47:55 +0000 (UTC) Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by smtp.subspace.kernel.org (Postfix) with ESMTP id B03092AF1C for ; Mon, 12 Feb 2024 15:47:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.140.110.172 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707752875; cv=none; b=h/ojHNMBL28bzoPBe/2+iRqzsah+49a1vD7sMjoD7mu/i0pV9pX2DR/431r5CeFBAQqQ1su5p63xx0AxCisHkYWEWxJSyT9/mwVknqHafBiyCxp3NcUZH2JPoDGwArtqCNbs5XnhOM57x3Pqjh3e9ed097jHlSiIdwWhAem+z8k= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1707752875; c=relaxed/simple; bh=9MqK7E8FB3NGz7M56N0HaUnBr6KDzZ40kKRNBHw8PkM=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=MiXLiyHrP8jq2UH5tFb7lnmGuCxgyf1xKHNGZqiuxETUa9z9kvC4FdsRakl8EvSyeJOhte1bYKhRad5dZSDD5+vnJVWMIKUHkO6g+g3t3LBULZaQfJU3+l/lmyJ5v8K8qGOeQy8e68yPqv3etsv+uRxsiP6mvDheF1CLzxNEUXg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com; spf=pass smtp.mailfrom=arm.com; arc=none smtp.client-ip=217.140.110.172 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 526FBDA7; Mon, 12 Feb 2024 07:48:33 -0800 (PST) Received: from [10.57.78.115] (unknown [10.57.78.115]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 4BF323F766; Mon, 12 Feb 2024 07:47:46 -0800 (PST) Message-ID: <82c59a7f-328e-4521-8855-ccacc3dc4ce5@arm.com> Date: Mon, 12 Feb 2024 15:47:44 +0000 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v5 22/25] mm: Add pte_batch_hint() to reduce scanning in folio_pte_batch() Content-Language: en-GB To: David Hildenbrand , Catalin Marinas , Will Deacon , Ard Biesheuvel , Marc Zyngier , James Morse , Andrey Ryabinin , Andrew Morton , Matthew Wilcox , Mark Rutland , Kefeng Wang , John Hubbard , Zi Yan , Barry Song <21cnbao@gmail.com>, Alistair Popple , Yang Shi , Nicholas Piggin , Christophe Leroy , "Aneesh Kumar K.V" , "Naveen N. Rao" , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "H. Peter Anvin" Cc: linux-arm-kernel@lists.infradead.org, x86@kernel.org, linuxppc-dev@lists.ozlabs.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <20240202080756.1453939-1-ryan.roberts@arm.com> <20240202080756.1453939-23-ryan.roberts@arm.com> <6d452a1a-1edc-4e97-8b39-99dc48315bb8@redhat.com> From: Ryan Roberts In-Reply-To: <6d452a1a-1edc-4e97-8b39-99dc48315bb8@redhat.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit On 12/02/2024 13:43, David Hildenbrand wrote: > On 02.02.24 09:07, Ryan Roberts wrote: >> Some architectures (e.g. arm64) can tell from looking at a pte, if some >> follow-on ptes also map contiguous physical memory with the same pgprot. >> (for arm64, these are contpte mappings). >> >> Take advantage of this knowledge to optimize folio_pte_batch() so that >> it can skip these ptes when scanning to create a batch. By default, if >> an arch does not opt-in, folio_pte_batch() returns a compile-time 1, so >> the changes are optimized out and the behaviour is as before. >> >> arm64 will opt-in to providing this hint in the next patch, which will >> greatly reduce the cost of ptep_get() when scanning a range of contptes. >> >> Tested-by: John Hubbard >> Signed-off-by: Ryan Roberts >> --- >>   include/linux/pgtable.h | 18 ++++++++++++++++++ >>   mm/memory.c             | 20 +++++++++++++------- >>   2 files changed, 31 insertions(+), 7 deletions(-) >> >> diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h >> index 50f32cccbd92..cba31f177d27 100644 >> --- a/include/linux/pgtable.h >> +++ b/include/linux/pgtable.h >> @@ -212,6 +212,24 @@ static inline int pmd_dirty(pmd_t pmd) >>   #define arch_flush_lazy_mmu_mode()    do {} while (0) >>   #endif >>   +#ifndef pte_batch_hint >> +/** >> + * pte_batch_hint - Number of pages that can be added to batch without scanning. >> + * @ptep: Page table pointer for the entry. >> + * @pte: Page table entry. >> + * >> + * Some architectures know that a set of contiguous ptes all map the same >> + * contiguous memory with the same permissions. In this case, it can provide a >> + * hint to aid pte batching without the core code needing to scan every pte. > > I think we might want to document here the expectation regarding > dirty/accessed bits. folio_pte_batch() will ignore dirty bits only with > FPB_IGNORE_DIRTY. But especially for arm64, it makes sense to ignore them > always when batching, because the dirty bit may target any pte part of the > cont-pte group either way. > > Maybe something like: > > " > An architecture implementation may only ignore the PTE accessed and dirty bits. > Further, it may only ignore the dirty bit if that bit is already not > maintained with precision per PTE inside the hinted batch, and ptep_get() > would already have to collect it from various PTEs. > " I'm proposing to simplify this to: " An architecture implementation may ignore the PTE accessed state. Further, the dirty state must apply atomically to all the PTEs described by the hint. " Which I think more accurately describes the requirement. Shout if you disagree. > > I think there are some more details to it, but I'm hoping something along > the lines above is sufficient. > > >> + >>   #ifndef pte_advance_pfn >>   static inline pte_t pte_advance_pfn(pte_t pte, unsigned long nr) >>   { >> diff --git a/mm/memory.c b/mm/memory.c >> index 65fbe4f886c1..902665b27702 100644 >> --- a/mm/memory.c >> +++ b/mm/memory.c >> @@ -988,16 +988,21 @@ static inline int folio_pte_batch(struct folio *folio, >> unsigned long addr, >>   { >>       unsigned long folio_end_pfn = folio_pfn(folio) + folio_nr_pages(folio); >>       const pte_t *end_ptep = start_ptep + max_nr; >> -    pte_t expected_pte = __pte_batch_clear_ignored(pte_advance_pfn(pte, 1), >> flags); >> -    pte_t *ptep = start_ptep + 1; >> +    pte_t expected_pte = __pte_batch_clear_ignored(pte, flags); >> +    pte_t *ptep = start_ptep; >>       bool writable; >> +    int nr; >>         if (any_writable) >>           *any_writable = false; >>         VM_WARN_ON_FOLIO(!pte_present(pte), folio); >>   -    while (ptep != end_ptep) { >> +    nr = pte_batch_hint(ptep, pte); >> +    expected_pte = pte_advance_pfn(expected_pte, nr); >> +    ptep += nr; >> + > > *Maybe* it's easier to get when initializing expected_pte+ptep only once. > > Like: > > [...] > pte_t expected_pte, *ptep; > [...] > > nr = pte_batch_hint(start_ptep, pte); > expected_pte = __pte_batch_clear_ignored(pte_advance_pfn(pte, nr), flags); > ptep = start_ptep + nr; > >> +    while (ptep < end_ptep) { >>           pte = ptep_get(ptep); >>           if (any_writable) >>               writable = !!pte_write(pte); >> @@ -1011,17 +1016,18 @@ static inline int folio_pte_batch(struct folio *folio, >> unsigned long addr, >>            * corner cases the next PFN might fall into a different >>            * folio. >>            */ >> -        if (pte_pfn(pte) == folio_end_pfn) >> +        if (pte_pfn(pte) >= folio_end_pfn) >>               break; >>             if (any_writable) >>               *any_writable |= writable; >>   -        expected_pte = pte_advance_pfn(expected_pte, 1); >> -        ptep++; >> +        nr = pte_batch_hint(ptep, pte); >> +        expected_pte = pte_advance_pfn(expected_pte, nr); >> +        ptep += nr; >>       } >>   -    return ptep - start_ptep; >> +    return min(ptep - start_ptep, max_nr); >>   } > > Acked-by: David Hildenbrand >