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 37530C3600B for ; Thu, 27 Mar 2025 14:09:06 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 7936E2800F5; Thu, 27 Mar 2025 10:09:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 744022800F2; Thu, 27 Mar 2025 10:09:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 634672800F5; Thu, 27 Mar 2025 10:09:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 44BCD2800F2 for ; Thu, 27 Mar 2025 10:09:04 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id D6C82140DEC for ; Thu, 27 Mar 2025 14:09:04 +0000 (UTC) X-FDA: 83267512608.08.5158621 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by imf19.hostedemail.com (Postfix) with ESMTP id 0069A1A0016 for ; Thu, 27 Mar 2025 14:09:02 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf19.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1743084543; a=rsa-sha256; cv=none; b=Uuf47OVrI0jNY2+iCtbfxcpLRZWfuBmEhrQ+fyZvQcP2F0RImJDD0SzKagZ1qXwlLgS+pw jdJVh3fk3U/i0dCfNWEauE1NsDDS3X1/+pclUCl20Pm7DnY0cyWK6Pf8SBSDcN8zcWGgZe gjbTClGLrj8hr+wcoysg0O7zTAvwQ4w= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=none; dmarc=pass (policy=none) header.from=arm.com; spf=pass (imf19.hostedemail.com: domain of ryan.roberts@arm.com designates 217.140.110.172 as permitted sender) smtp.mailfrom=ryan.roberts@arm.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1743084543; 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=r4TpZsccYA81zEAL5ZIn6b+kX0nIebuJJmbMrQxOVrQ=; b=Euh2gNsISDigFWGXPjnGYgAtF3+4a4VYVO3IE4m8o2joj2OEEJSPCadaKQH8/XkXt2eyGQ Xg6mNZjWqEk6XK15MW42M8I+KLDsGfCWwHW1M8VPXO2Ti3pb0+fO+oYmvoW7hRNyxw7GXY ojhdJN445NoQGo7O7gHTUW7Heq4dAAM= 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 2336A1063; Thu, 27 Mar 2025 07:09:07 -0700 (PDT) Received: from [10.57.86.146] (unknown [10.57.86.146]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id CC29F3F58B; Thu, 27 Mar 2025 07:08:58 -0700 (PDT) Message-ID: <54886038-3707-4ea0-bd84-00a8f4a19a6a@arm.com> Date: Thu, 27 Mar 2025 10:08:56 -0400 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 2/2] mm: mincore: use folio_pte_batch() to batch process large folios Content-Language: en-GB To: Baolin Wang , akpm@linux-foundation.org, hughd@google.com Cc: willy@infradead.org, david@redhat.com, 21cnbao@gmail.com, ziy@nvidia.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org References: <7ad05bc9299de5d954fb21a2da57f46dd6ec59d0.1742960003.git.baolin.wang@linux.alibaba.com> From: Ryan Roberts In-Reply-To: <7ad05bc9299de5d954fb21a2da57f46dd6ec59d0.1742960003.git.baolin.wang@linux.alibaba.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Rspamd-Queue-Id: 0069A1A0016 X-Stat-Signature: z7x94zh3kn4b4oaj34tcq3muwkeccitf X-Rspam-User: X-Rspamd-Server: rspam06 X-HE-Tag: 1743084542-919130 X-HE-Meta: U2FsdGVkX1+1Zi7m+IFTuv9sg/Y63KFtNqhYteXrH/Uh38uKxnacXxwDxaP4lTCsaoYqA+v1jZaR7GjtpnYvat8UOOdqg/YITWKLXanTQiglgE/L3+O+WLrBKtlhMv8Xv5AS4V6iHjW7J9uvumWdb5MM4dznXozwimxZ1yChOubsVlh7G4xS135Kr7RyaUl+0pKhfI+6g4oOl23OxDO36kasjZxNtrwCEc+d/dFSq4T94v3zkNWRwvT9/1FfudfasEEgY0kdFkll4kYTnNw4hnT/EATP48Kaj6EsuanDjDLQ4fdW+v6hSaCalskOdZ0XzSOLGZeeVVZpGQ4VvgFbYcnOXG8z2Ailln7KyG01238zgkwPBAfJsbd15K6jERnH9YhHd4Nf9FF0ElFFU21rx7OljbP5kaPCM0/O+kUC6f8hB67oBDf9CVCdcyd6UboSX2BAnao0z6STHFTeQEZ2z42dKb6yDeTjk6Mfc0b7s6gcD1e3sWNFdzoYZxyhAnfdfrltJvAbtgKlPsWW0iFJDuGmJpyblv3ZJWX7D14z1hakz2+gUeud9232Eoa03guhL310d/5l4tqplhevUM7NrvLMidaCIlDw+/ozhHXeVCImUUVm/DKvlpELsP3h4Do8Vb+oKUGArZoglbVkJ58oivmITF+hP1MG8DCOVFKuRcrzCqGZDINGEBA1ym3RfHuM8qEb+UOxUWODgFVAWC7EbwrE4SEd5zTcl2zCPz3lUTNYEVZeUAYuJ144rLQUIL7stcgNwPP4aiN9N6lmA75BDPqa0QB5bsghARkP3LjEKy+ON1XfJHj7z+Oa02qZqleIl0A7Yi2XGFJd7BjNiTWBeY2yELBoOqcXJNE30TCdmehgArNu5oN52yJW1UdapeUw4xNypFgH6FXm1Eqh404ySOCQfHkcUMqwcvLOgM+tIyc3C7CJde7sYU7V1WrsHhhyjV5zeEvevMobbSaARLr BOKOr6+Y I3fPpVDK244VLx8IULH74O3OckcjZQjN/GvVpLcxwVkrsIr3khwRdCykMnjG4UBXPR8Gvdi71HwE02pSV4dd/vwCcCJT4/onp8UDa5dLmDLkr/g+EZNIgSZiJTvhn5euxDL3dNe2Ge0u4soDgrklbk3NlQY2HWaPQkq9lufPayuWJY5/K3O7Avt7e6PDGq7jXgY6qC51osUO4HMTXjsmne+6nNw== 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 25/03/2025 23:38, Baolin Wang wrote: > When I tested the mincore() syscall, I observed that it takes longer with > 64K mTHP enabled on my Arm64 server. The reason is the mincore_pte_range() > still checks each PTE individually, even when the PTEs are contiguous, > which is not efficient. > > Thus we can use folio_pte_batch() to get the batch number of the present > contiguous PTEs, which can improve the performance. I tested the mincore() > syscall with 1G anonymous memory populated with 64K mTHP, and observed an > obvious performance improvement: > > w/o patch w/ patch changes > 6022us 1115us +81% > > Moreover, I also tested mincore() with disabling mTHP/THP, and did not > see any obvious regression. > > Signed-off-by: Baolin Wang > --- > mm/mincore.c | 27 ++++++++++++++++++++++----- > 1 file changed, 22 insertions(+), 5 deletions(-) > > diff --git a/mm/mincore.c b/mm/mincore.c > index 832f29f46767..88be180b5550 100644 > --- a/mm/mincore.c > +++ b/mm/mincore.c > @@ -21,6 +21,7 @@ > > #include > #include "swap.h" > +#include "internal.h" > > static int mincore_hugetlb(pte_t *pte, unsigned long hmask, unsigned long addr, > unsigned long end, struct mm_walk *walk) > @@ -105,6 +106,7 @@ static int mincore_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, > pte_t *ptep; > unsigned char *vec = walk->private; > int nr = (end - addr) >> PAGE_SHIFT; > + int step, i; > > ptl = pmd_trans_huge_lock(pmd, vma); > if (ptl) { > @@ -118,16 +120,31 @@ static int mincore_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, > walk->action = ACTION_AGAIN; > return 0; > } > - for (; addr != end; ptep++, addr += PAGE_SIZE) { > + for (; addr != end; ptep += step, addr += step * PAGE_SIZE) { > pte_t pte = ptep_get(ptep); > > + step = 1; > /* We need to do cache lookup too for pte markers */ > if (pte_none_mostly(pte)) > __mincore_unmapped_range(addr, addr + PAGE_SIZE, > vma, vec); > - else if (pte_present(pte)) > - *vec = 1; > - else { /* pte is a swap entry */ > + else if (pte_present(pte)) { > + if (pte_batch_hint(ptep, pte) > 1) { > + struct folio *folio = vm_normal_folio(vma, addr, pte); > + > + if (folio && folio_test_large(folio)) { > + const fpb_t fpb_flags = FPB_IGNORE_DIRTY | > + FPB_IGNORE_SOFT_DIRTY; > + int max_nr = (end - addr) / PAGE_SIZE; > + > + step = folio_pte_batch(folio, addr, ptep, pte, > + max_nr, fpb_flags, NULL, NULL, NULL); > + } > + } You could simplify to the following, I think, to avoid needing to grab the folio and call folio_pte_batch(): else if (pte_present(pte)) { int max_nr = (end - addr) / PAGE_SIZE; step = min(pte_batch_hint(ptep, pte), max_nr); } ... I expect the regression you are seeing here is all due to calling ptep_get() for every pte in the contpte batch, which will cause 16 memory reads per pte (to gather the access/dirty bits). For small folios its just 1 read per pte. pte_batch_hint() will skip forward in blocks of 16 so you now end up with the same number as for the small folio case. You don't need all the fancy extras that folio_pte_batch() gives you here. Thanks, Ryan > + > + for (i = 0; i < step; i++) > + vec[i] = 1; > + } else { /* pte is a swap entry */ > swp_entry_t entry = pte_to_swp_entry(pte); > > if (non_swap_entry(entry)) { > @@ -146,7 +163,7 @@ static int mincore_pte_range(pmd_t *pmd, unsigned long addr, unsigned long end, > #endif > } > } > - vec++; > + vec += step; > } > pte_unmap_unlock(ptep - 1, ptl); > out: