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 D795DC3ABBE for ; Thu, 8 May 2025 06:31:20 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D892D6B000A; Thu, 8 May 2025 02:31:18 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D132C6B0082; Thu, 8 May 2025 02:31:18 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id BB1EE6B0085; Thu, 8 May 2025 02:31:18 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 9C8E96B000A for ; Thu, 8 May 2025 02:31:18 -0400 (EDT) Received: from smtpin16.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id C6A2D120B1B for ; Thu, 8 May 2025 06:31:19 +0000 (UTC) X-FDA: 83418768678.16.2E4E32D Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf08.hostedemail.com (Postfix) with ESMTP id 20783160006 for ; Thu, 8 May 2025 06:31:17 +0000 (UTC) Authentication-Results: imf08.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf08.hostedemail.com: domain of anshuman.khandual@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=anshuman.khandual@arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1746685878; a=rsa-sha256; cv=none; b=Rnmz5b15b3alLxrsxPgrUqyEMruZa0L2QWYJ5nXg7sO/3lKaMA1s2pjCALTl/KqHh5erfx zt/1W8OaIGrcBBc01gkCiJNkGpSKrMv9assx/P/56fzRgro2xEm4G9irBDtEc66bF4BBAR fnzXkjZKRwS8gr9qItGCnLtCVWjqMjQ= ARC-Authentication-Results: i=1; imf08.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf08.hostedemail.com: domain of anshuman.khandual@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=anshuman.khandual@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1746685878; 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; bh=Lp3HSVMD2ZUn1JB0I7Kjj3D8l65p412aPUfxv4Sa6hY=; b=ki/L0SVApIPhzI+om5TGn7cG81bn3cRFNPtrTOV7z3+FSza0IZwgalayWdwZP49jOafSxS Q7Fqh7F0DMC6XkT8DfHPuLzXOhEICUacDSjU09Juh2adB4t7N6zqnpnH9DjgoCIYXbjCR8 MiqgIzZkVYBK3vvQYd/tsFZu2ufXXgA= 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 A7DE3106F; Wed, 7 May 2025 23:31:06 -0700 (PDT) Received: from [10.163.54.182] (unknown [10.163.54.182]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 108883F5A1; Wed, 7 May 2025 23:31:08 -0700 (PDT) Message-ID: <9bd98ddc-50fb-42cb-be22-1d08161f4713@arm.com> Date: Thu, 8 May 2025 12:01:05 +0530 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 2/2] mm: Optimize mremap() by PTE batching To: Zi Yan , Dev Jain Cc: akpm@linux-foundation.org, Liam.Howlett@oracle.com, lorenzo.stoakes@oracle.com, vbabka@suse.cz, jannh@google.com, pfalcato@suse.de, linux-mm@kvack.org, linux-kernel@vger.kernel.org, david@redhat.com, peterx@redhat.com, ryan.roberts@arm.com, mingo@kernel.org, libang.li@antgroup.com, maobibo@loongson.cn, zhengqi.arch@bytedance.com, baohua@kernel.org, willy@infradead.org, ioworker0@gmail.com, yang@os.amperecomputing.com, baolin.wang@linux.alibaba.com, hughd@google.com References: <20250507060256.78278-1-dev.jain@arm.com> <20250507060256.78278-3-dev.jain@arm.com> <74FDF9E1-3148-460B-8E3C-5EE156A3FA93@nvidia.com> Content-Language: en-US From: Anshuman Khandual In-Reply-To: <74FDF9E1-3148-460B-8E3C-5EE156A3FA93@nvidia.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 20783160006 X-Stat-Signature: cbwa9jb8i5cjbwrfh6rjwp73poujtskx X-HE-Tag: 1746685877-198182 X-HE-Meta: U2FsdGVkX1/8LUL6x4IriB0LW9PRFk23EU1Nbemv77h5/1zRYcHcXr0EwXkUhkoUBJho1ELd/ylftODLaqsuMaNzIVlpyTJ40KrokoDq4diQqV3lLzo85TYUEbzs+ye6eHD85Ui4sdaRiKdJLFxIOw1wmVcIusdOl3//As4PfAFmZBG02Ujyy2JLeahVpL3miwy+aFD25m9XgGO6B/nvYqspTIjX+Cx1SzlDhCyvkxkmzeEJDo/ZsQ29LupVcp2IqPRoCACdRmDMgCiGYbKEuD+ojmoCbejf5BazalLHcQePTE8+uRf2xPPJbx5inWECn7lHZpM4YBuiFBgDHKruJ0o3mTVIR6g/cb+zzDnjfyxqzjfc4cnX5trr7HxP/YLlfO9kOGU5TlYmozXtHm1Ejs85ZI4NqntdcbvBjEmLNrS8JAc9wW8Jvgff7yXjw9C27uSe1RaJmTGebhVMSkXD90Tr6GH/M+1XIdjJCLt45CDYatnNiol/p+ZzUzJTQ553HjyQsJEZIgOKjavl8jMxhKzsZxrVaMYMGX/5zGF5Sg2dsn1jo++qQzsPC4iEclKvXfmb/4pfrlYuOrvAIHYx1J9aweM0mZ222BYTBPc4bpijdm0iWMC0A4H7rxAkMOWmUVFQVt6fcXAY30GeeH5ABmvRwm//DSigCwqDEtCAhYuN0ZU+P563JMLM2B+g4q1rupG/thnlMQ+JBHArfSSCqorrp3X8mgRR59cDpNuZrD/hajBsRvTJLnmThkPalBOJX657jY2Jb88W3vv1WepyDoPQsepQhcWZTUy0SL5cm9Xrn6T366dD4XotS970nU8qzYeboW7mStFNL2VbMbIorNMV7NA2bIbn 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 5/8/25 07:30, Zi Yan wrote: > On 7 May 2025, at 2:02, Dev Jain wrote: > >> To use PTE batching, we want to determine whether the folio mapped by >> the PTE is large, thus requiring the use of vm_normal_folio(). We want >> to avoid the cost of vm_normal_folio() if the code path doesn't already >> require the folio. For arm64, pte_batch_hint() does the job. To generalize >> this hint, add a helper which will determine whether two consecutive PTEs >> point to consecutive PFNs, in which case there is a high probability that >> the underlying folio is large. >> Next, use folio_pte_batch() to optimize move_ptes(). On arm64, if the ptes >> are painted with the contig bit, then ptep_get() will iterate through all 16 >> entries to collect a/d bits. Hence this optimization will result in a 16x >> reduction in the number of ptep_get() calls. Next, ptep_get_and_clear() >> will eventually call contpte_try_unfold() on every contig block, thus >> flushing the TLB for the complete large folio range. Instead, use >> get_and_clear_full_ptes() so as to elide TLBIs on each contig block, and only >> do them on the starting and ending contig block. >> >> Signed-off-by: Dev Jain >> --- >> include/linux/pgtable.h | 29 +++++++++++++++++++++++++++++ >> mm/mremap.c | 37 ++++++++++++++++++++++++++++++------- >> 2 files changed, 59 insertions(+), 7 deletions(-) >> >> diff --git a/include/linux/pgtable.h b/include/linux/pgtable.h >> index b50447ef1c92..38dab1f562ed 100644 >> --- a/include/linux/pgtable.h >> +++ b/include/linux/pgtable.h >> @@ -369,6 +369,35 @@ static inline pgd_t pgdp_get(pgd_t *pgdp) >> } >> #endif >> >> +/** >> + * maybe_contiguous_pte_pfns - Hint whether the page mapped by the pte belongs >> + * to a large folio. >> + * @ptep: Pointer to the page table entry. >> + * @pte: The page table entry. >> + * >> + * This helper is invoked when the caller wants to batch over a set of ptes >> + * mapping a large folio, but the concerned code path does not already have >> + * the folio. We want to avoid the cost of vm_normal_folio() only to find that >> + * the underlying folio was small; i.e keep the small folio case as fast as >> + * possible. >> + * >> + * The caller must ensure that ptep + 1 exists. > > ptep points to an entry in a PTE page. As long as it is not pointing > to the last entry, ptep+1 should always exist. With PTRS_PER_PTE and > sizeof(pte_t), you can check ptep address to figure out whether it > is the last entry of a PTE page, right? Let me know if I misunderstand > anything. Agreed, this not-the-last-pte-entry test is definitely required here just to prevent a potential unmapped access crash. But I do agree with Andrew that unless there are callers, this should be contained in the call site itself (mm/mremap.c) with a good explanation.