From nobody Fri Feb 13 01:39:48 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 81FDB13D8B1 for ; Tue, 4 Jun 2024 04:21:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717474895; cv=none; b=B+whFYKsRAMGyc/Q0h5OkE0YWrnCvINJQp/WdIx5PAYAi6LycDMCv3C5pK1klFT3n35ueyL65fczFru82VPlsehdweq5a84ORLkCghRKiPX8khvvqaR4g/jPVVzihSaccVCFC+1cIt5f8MQOJBrWdhZNRrer50kURYG7/7xFfbU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717474895; c=relaxed/simple; bh=occLtpiGpNG6/OrTWQS6BvyTS0mi9to3RbiYlw3PrkE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=dlT5qkAqyR+7Ap+oFRtZxvnv9Jxl/LUbYjfTf1eUunAKt0+joMs89VZqlUbXTWjU5LfCsjbiJ8DCaIOHuBghEx24XZTpigMzYMHzvd06mu7T/DDnG39EAXJiHQt1TwWHBROWiMCZcVH6DlI50q5HKeDrZIg+IK1KImohUHDJIlQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=AguJvBU0; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="AguJvBU0" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B71FAC3277B; Tue, 4 Jun 2024 04:21:32 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717474895; bh=occLtpiGpNG6/OrTWQS6BvyTS0mi9to3RbiYlw3PrkE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=AguJvBU0+LLRweQOatueW/u5AH06TbIns2J0lyV0b25QR80ATqmiQBTyEWut9FC7m L8V1+t+hIkHS1njzCmejNwswpulFnR5Li3oPulT/Ye88s41xidUgi5DWxEknMybnYs SrQrv4Pmisbv32VKHqIeHL9Ellc0qaqvI+uP3XI6QVmtr/L+ifE9aXqzuarObMzY8O xED9gD2pIUppXTXXqcVvaC4r497TaeSpG9V8R7EKpBSq8/F+KSdxZfOVDe5cK94jq5 KfEM5HmCAV0abs8mRLCcl6FBU8N4o2oFmv4dCKLRSJ11rNxKDFAzZ9ZEx2mDsZMfYO hnt1ztORdDlgw== From: alexs@kernel.org To: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, izik.eidus@ravellosystems.com, willy@infradead.org, aarcange@redhat.com, chrisw@sous-sol.org, hughd@google.com, david@redhat.com Cc: "Alex Shi (tencent)" Subject: [PATCH 01/10] mm/ksm: reduce the flush action for ksm merging page Date: Tue, 4 Jun 2024 12:24:43 +0800 Message-ID: <20240604042454.2012091-2-alexs@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240604042454.2012091-1-alexs@kernel.org> References: <20240604042454.2012091-1-alexs@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Alex Shi (tencent)" We can put off the flush action util a merging is realy coming. That could reduce some unmerge page flushing. BTW, flushing only do at arm, mips and few other archs. Signed-off-by: Alex Shi (tencent) --- mm/ksm.c | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index f5138f43f0d2..97e5b41f8c4b 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -783,10 +783,7 @@ static struct page *get_mergeable_page(struct ksm_rmap= _item *rmap_item) goto out; if (is_zone_device_page(page)) goto out_putpage; - if (PageAnon(page)) { - flush_anon_page(vma, page, addr); - flush_dcache_page(page); - } else { + if (!PageAnon(page)) { out_putpage: put_page(page); out: @@ -1473,8 +1470,8 @@ static int replace_page(struct vm_area_struct *vma, s= truct page *page, * * This function returns 0 if the pages were merged, -EFAULT otherwise. */ -static int try_to_merge_one_page(struct vm_area_struct *vma, - struct page *page, struct page *kpage) +static int try_to_merge_one_page(struct vm_area_struct *vma, struct page *= page, + struct ksm_rmap_item *rmap_item, struct page *kpage) { pte_t orig_pte =3D __pte(0); int err =3D -EFAULT; @@ -1500,6 +1497,9 @@ static int try_to_merge_one_page(struct vm_area_struc= t *vma, goto out_unlock; } =20 + flush_anon_page(vma, page, rmap_item->address); + flush_dcache_page(page); + /* * If this anonymous page is mapped only here, its pte may need * to be write-protected. If it's mapped elsewhere, all of its @@ -1550,7 +1550,7 @@ static int try_to_merge_with_ksm_page(struct ksm_rmap= _item *rmap_item, if (!vma) goto out; =20 - err =3D try_to_merge_one_page(vma, page, kpage); + err =3D try_to_merge_one_page(vma, page, rmap_item, kpage); if (err) goto out; =20 @@ -2385,7 +2385,7 @@ static void cmp_and_merge_page(struct page *page, str= uct ksm_rmap_item *rmap_ite mmap_read_lock(mm); vma =3D find_mergeable_vma(mm, rmap_item->address); if (vma) { - err =3D try_to_merge_one_page(vma, page, + err =3D try_to_merge_one_page(vma, page, rmap_item, ZERO_PAGE(rmap_item->address)); trace_ksm_merge_one_page( page_to_pfn(ZERO_PAGE(rmap_item->address)), @@ -2663,8 +2663,6 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(= struct page **page) if (is_zone_device_page(*page)) goto next_page; if (PageAnon(*page)) { - flush_anon_page(vma, *page, ksm_scan.address); - flush_dcache_page(*page); rmap_item =3D get_next_rmap_item(mm_slot, ksm_scan.rmap_list, ksm_scan.address); if (rmap_item) { --=20 2.43.0 From nobody Fri Feb 13 01:39:48 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 27B5D13DDB8 for ; Tue, 4 Jun 2024 04:21:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717474898; cv=none; b=UaJ0kHAVA0XxIpFRxlnOPCElApdEDz8qmfuzxsty1zq2oII9lbGSUKD4DEWz/OtLfLk/oB9ZpCIpTmyB1Bh+mpalZwvaEL/eDhfGUYxcrxivPEcQFjC8N6JdPVuc5n3z07dx411XETPfI+8tjMpIcgwqKr/6RoXbOhMM/up0bTI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717474898; c=relaxed/simple; bh=P0itGeZCXEfvARIWHRoE92wg+ZB0+ncoLLqA7cA6dRA=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=DWtsHRRUSQ7ptpGzii289x9xrBgahV25k9JkbMVQedlZHwVCt1vT9lnB4pTNlxuhvdwqxLGaJM2ysU7oiV9dGsFZjR/jK2h9c+AiHVt5kZwXuGXybbEfZZLgCyDvptJ7aHTJ1OUMMF+lT1Za6cGsVnzzqSxOShmKZcfy+GWjwh4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Cs1MCtQ5; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Cs1MCtQ5" Received: by smtp.kernel.org (Postfix) with ESMTPSA id A4F92C4AF0A; Tue, 4 Jun 2024 04:21:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717474898; bh=P0itGeZCXEfvARIWHRoE92wg+ZB0+ncoLLqA7cA6dRA=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Cs1MCtQ5uXJAEfguDsl9n6PCoX3lPNYo1TZbsFptY7tRN/JeBiGX7aLwjXVkjEjP6 gzJtAWsoo23HZ4+Xwld8DivVjPrM/axC3AWBXBgFbVhFye8646fQl0FEVCgnus7ErB +xhs2Tfn2Qkp41rTdPVsTbIPRBi9scA3yz80vVEF60NFVyYUYLBqx+ldEpKMUtAznx QkC9gXYgaUuhdCohrrJoGYJs4e1pF7R0beiZ+UQ+PqqbfZ8KpYJG13ms3sw4fzDZqf x+9aM6ykvs/PQUHdUrVeSR+BU7KapA9qAI2obkv+dWjpqjmNZ7trkgxufE2OkJYsC+ SdXnLZW54tSSw== From: alexs@kernel.org To: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, izik.eidus@ravellosystems.com, willy@infradead.org, aarcange@redhat.com, chrisw@sous-sol.org, hughd@google.com, david@redhat.com Cc: "Alex Shi (tencent)" Subject: [PATCH 02/10] mm/ksm: skip subpages of compound pages Date: Tue, 4 Jun 2024 12:24:44 +0800 Message-ID: <20240604042454.2012091-3-alexs@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240604042454.2012091-1-alexs@kernel.org> References: <20240604042454.2012091-1-alexs@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Alex Shi (tencent)" When a folio isn't fit for KSM, the subpages are unlikely to be good, So let's skip the rest page checking to save some actions. Signed-off-by: Alex Shi (tencent) --- mm/ksm.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 97e5b41f8c4b..e2fdb9dd98e2 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2644,6 +2644,8 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(= struct page **page) goto no_vmas; =20 for_each_vma(vmi, vma) { + int nr =3D 1; + if (!(vma->vm_flags & VM_MERGEABLE)) continue; if (ksm_scan.address < vma->vm_start) @@ -2660,6 +2662,9 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(= struct page **page) cond_resched(); continue; } + + VM_WARN_ON(PageTail(*page)); + nr =3D compound_nr(*page); if (is_zone_device_page(*page)) goto next_page; if (PageAnon(*page)) { @@ -2672,7 +2677,7 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(= struct page **page) if (should_skip_rmap_item(*page, rmap_item)) goto next_page; =20 - ksm_scan.address +=3D PAGE_SIZE; + ksm_scan.address +=3D nr * PAGE_SIZE; } else put_page(*page); mmap_read_unlock(mm); @@ -2680,7 +2685,7 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(= struct page **page) } next_page: put_page(*page); - ksm_scan.address +=3D PAGE_SIZE; + ksm_scan.address +=3D nr * PAGE_SIZE; cond_resched(); } } --=20 2.43.0 From nobody Fri Feb 13 01:39:48 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5C18C13E3F9 for ; Tue, 4 Jun 2024 04:21:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717474901; cv=none; b=cpuQYR1JG6gJZXH+qPGU4irxtJOMpqMDBu2CXxLGdewaq2OlBgZX+DqwvfP7QZOfIkxQdW3HN0qc1SdUqIu+ZqS3Pr42EfccTB0zTA9v4UYbYshFHOOWV3k32lZy+B4MV/z3kAMt0KfoinG3UKTlSsZWnxZM9Q6OiuDgfMbSBV4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717474901; c=relaxed/simple; bh=CfvlQ2M9KWnaqxKPXvUUKqZr9diZgDumxJiDtxK8xLg=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=h2rLiFrtFOCl+KnRj0qbfGN09UhVyxLmnHe6p0Q353GvjRLCOQshC42rL1WtEQNSHQh0Cw/7yhutXgfbiCgT1N3Yd7gXU/Mmm4Jut5FUQCySxqxc92ya0Krg62iyYZMo57IX3LFxO/Z0g/qo6EUctQZ1+0JRlFQnbATceYsyaO0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=LVNkYZ54; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="LVNkYZ54" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 95F25C32786; Tue, 4 Jun 2024 04:21:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717474900; bh=CfvlQ2M9KWnaqxKPXvUUKqZr9diZgDumxJiDtxK8xLg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=LVNkYZ54FWWO0xDgawCPCmAKsUuesOzTQxtIVVhIDWRFB+BRrf0loqYP6nhYl+ZxY 4/FZDa5Z71YUuEhj74spNoj9ub6BKUBfz6kLqA4rTWFIZUJHInhVTnX/Q73H75AiZE 85lX1fl8SDh3Q25c6Noz3NN0Wl8T/1GRCbq6062CnvkbylpQ2xwZz64V3O1XnQv1GD rbF+PTkdvZSFnKI6OjAVUsldEzoxEUdBOuyOnpHHZA6UHSgFrSTxZfnCboZeZh5zMn hdaK9PWG/w+vABWb3fmiH/Jgh1v2eloKrI0GKrme2D4X1hpN3ameLMF8M7ylbqpR3O 9mJ/sbzJPl4rA== From: alexs@kernel.org To: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, izik.eidus@ravellosystems.com, willy@infradead.org, aarcange@redhat.com, chrisw@sous-sol.org, hughd@google.com, david@redhat.com Cc: "Alex Shi (tencent)" Subject: [PATCH 03/10] mm/ksm: use folio in try_to_merge_one_page Date: Tue, 4 Jun 2024 12:24:45 +0800 Message-ID: <20240604042454.2012091-4-alexs@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240604042454.2012091-1-alexs@kernel.org> References: <20240604042454.2012091-1-alexs@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Alex Shi (tencent)" scan_get_next_rmap_item() return folio actually now. So in the calling path to try_to_merge_one_page() parameter pages are actually folios. So let's use folio instead of of page in the function to save few compound checking in callee functions. The 'page' left here since flush functions still not support folios yet. Signed-off-by: Alex Shi (tencent) --- mm/ksm.c | 61 ++++++++++++++++++++++++++++++++------------------------ 1 file changed, 35 insertions(+), 26 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index e2fdb9dd98e2..21bfa9bfb210 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -1462,24 +1462,29 @@ static int replace_page(struct vm_area_struct *vma,= struct page *page, } =20 /* - * try_to_merge_one_page - take two pages and merge them into one - * @vma: the vma that holds the pte pointing to page - * @page: the PageAnon page that we want to replace with kpage - * @kpage: the PageKsm page that we want to map instead of page, - * or NULL the first time when we want to use page as kpage. + * try_to_merge_one_page - take two folios and merge them into one + * @vma: the vma that holds the pte pointing to folio + * @folio: the PageAnon page that we want to replace with kfolio + * @kfolio: the PageKsm page that we want to map instead of folio, + * or NULL the first time when we want to use folio as kfolio. * - * This function returns 0 if the pages were merged, -EFAULT otherwise. + * This function returns 0 if the folios were merged, -EFAULT otherwise. */ -static int try_to_merge_one_page(struct vm_area_struct *vma, struct page *= page, - struct ksm_rmap_item *rmap_item, struct page *kpage) +static int try_to_merge_one_page(struct vm_area_struct *vma, struct folio = *folio, + struct ksm_rmap_item *rmap_item, struct folio *kfolio) { pte_t orig_pte =3D __pte(0); int err =3D -EFAULT; + struct page *page =3D folio_page(folio, 0); + struct page *kpage; =20 - if (page =3D=3D kpage) /* ksm page forked */ + if (kfolio) + kpage =3D folio_page(kfolio, 0); + + if (folio =3D=3D kfolio) /* ksm page forked */ return 0; =20 - if (!PageAnon(page)) + if (!folio_test_anon(folio)) goto out; =20 /* @@ -1489,11 +1494,11 @@ static int try_to_merge_one_page(struct vm_area_str= uct *vma, struct page *page, * prefer to continue scanning and merging different pages, * then come back to this page when it is unlocked. */ - if (!trylock_page(page)) + if (!folio_trylock(folio)) goto out; =20 - if (PageTransCompound(page)) { - if (split_huge_page(page)) + if (folio_test_large(folio)) { + if (split_folio(folio)) goto out_unlock; } =20 @@ -1506,35 +1511,36 @@ static int try_to_merge_one_page(struct vm_area_str= uct *vma, struct page *page, * ptes are necessarily already write-protected. But in either * case, we need to lock and check page_count is not raised. */ - if (write_protect_page(vma, page_folio(page), &orig_pte) =3D=3D 0) { - if (!kpage) { + if (write_protect_page(vma, folio, &orig_pte) =3D=3D 0) { + if (!kfolio) { /* * While we hold page lock, upgrade page from * PageAnon+anon_vma to PageKsm+NULL stable_node: * stable_tree_insert() will update stable_node. */ - folio_set_stable_node(page_folio(page), NULL); - mark_page_accessed(page); + folio_set_stable_node(folio, NULL); + folio_mark_accessed(folio); /* * Page reclaim just frees a clean page with no dirty * ptes: make sure that the ksm page would be swapped. */ - if (!PageDirty(page)) - SetPageDirty(page); + if (!folio_test_dirty(folio)) + folio_set_dirty(folio); err =3D 0; } else if (pages_identical(page, kpage)) err =3D replace_page(vma, page, kpage, orig_pte); } =20 out_unlock: - unlock_page(page); + folio_unlock(folio); out: return err; } =20 /* * try_to_merge_with_ksm_page - like try_to_merge_two_pages, - * but no new kernel page is allocated: kpage must already be a ksm page. + * but no new kernel page is allocated, kpage is a ksm page or NULL + * if we use page as first ksm page. * * This function returns 0 if the pages were merged, -EFAULT otherwise. */ @@ -1544,13 +1550,17 @@ static int try_to_merge_with_ksm_page(struct ksm_rm= ap_item *rmap_item, struct mm_struct *mm =3D rmap_item->mm; struct vm_area_struct *vma; int err =3D -EFAULT; + struct folio *kfolio; =20 mmap_read_lock(mm); vma =3D find_mergeable_vma(mm, rmap_item->address); if (!vma) goto out; =20 - err =3D try_to_merge_one_page(vma, page, rmap_item, kpage); + if (kpage) + kfolio =3D page_folio(kpage); + + err =3D try_to_merge_one_page(vma, page_folio(page), rmap_item, kfolio); if (err) goto out; =20 @@ -2385,8 +2395,8 @@ static void cmp_and_merge_page(struct page *page, str= uct ksm_rmap_item *rmap_ite mmap_read_lock(mm); vma =3D find_mergeable_vma(mm, rmap_item->address); if (vma) { - err =3D try_to_merge_one_page(vma, page, rmap_item, - ZERO_PAGE(rmap_item->address)); + err =3D try_to_merge_one_page(vma, page_folio(page), rmap_item, + page_folio(ZERO_PAGE(rmap_item->address))); trace_ksm_merge_one_page( page_to_pfn(ZERO_PAGE(rmap_item->address)), rmap_item, mm, err); @@ -2671,8 +2681,7 @@ static struct ksm_rmap_item *scan_get_next_rmap_item(= struct page **page) rmap_item =3D get_next_rmap_item(mm_slot, ksm_scan.rmap_list, ksm_scan.address); if (rmap_item) { - ksm_scan.rmap_list =3D - &rmap_item->rmap_list; + ksm_scan.rmap_list =3D &rmap_item->rmap_list; =20 if (should_skip_rmap_item(*page, rmap_item)) goto next_page; --=20 2.43.0 From nobody Fri Feb 13 01:39:48 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0FC6313F442 for ; Tue, 4 Jun 2024 04:21:44 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717474904; cv=none; b=Itsvackrgdmd5GbNF2T5uUBwTZBtVYmAERK8N/yZHMQEnpeGHMJeHfdEak0Hb2CplCbDcum4Aar5kuN+nTm2MMf1hN3TZjCu/lYQUpqn8o90jUVpRPjbLh3UP3G69YWRM4Hv3HZRXxd8ILO+szuqM10CA50uuogsQB/QSi1m/Ys= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717474904; c=relaxed/simple; bh=8rn+89eKdjpnGRg9SATPm/FCPUFDqEev/EjdjwcPsX4=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=d/veBnqDBCVrx39cayeXaNwOQRdom211T1cG3QXr9cdMQ7B7uAKxqtYmRLoFSVJLsrT2v9xAiw3lNtET6VSihDcnhJEzYDrLkASW7vxSxTG/8vmY14Vl4srnqzugsDOimxffl68FMayswuC24XHRpWl0DI67FfCn4RPy9Z+pv5A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BrDK39h9; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="BrDK39h9" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 83CC0C32786; Tue, 4 Jun 2024 04:21:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717474903; bh=8rn+89eKdjpnGRg9SATPm/FCPUFDqEev/EjdjwcPsX4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BrDK39h9fOI8f7AxXV5VEsT+hcMTOXnlEQKMg/6aZTTxZNso2DZQD1OtJONwRjRkG GTotLiw6R+JnHOjQ+F4Z2QdlRSZJW/+/D2lxocg2C9GOt0+eA01KBD7PK6qZKDn93v oJmb0/AUR5k1JJjqlHrm2uenG78NcxWZ2ZRyhPdoGDBTxMnKmqVIcSzL2j5jAAFEig HQbh7x3JlQMRQX9xktOcMM1uYVXNp75jz/PFQLKdr4hYqEj/e+x0sMkhIMH8YDhZfw 1Lu1MRZv5GG3zPR5gLx+97v8YSf9U4PF/EC2hDxnpJ5FSUVfzwXiBharDCB7Osnutq bkpUX1xY27wMg== From: alexs@kernel.org To: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, izik.eidus@ravellosystems.com, willy@infradead.org, aarcange@redhat.com, chrisw@sous-sol.org, hughd@google.com, david@redhat.com Cc: "Alex Shi (tencent)" Subject: [PATCH 04/10] mm/ksm: add identical_folio func Date: Tue, 4 Jun 2024 12:24:46 +0800 Message-ID: <20240604042454.2012091-5-alexs@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240604042454.2012091-1-alexs@kernel.org> References: <20240604042454.2012091-1-alexs@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Alex Shi (tencent)" To replace pages_identical() func and further reduce the 'page' using in try_to_merge_one_page function. Signed-off-by: Alex Shi (tencent) --- include/linux/mm.h | 17 +++++++++++++++++ mm/ksm.c | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/include/linux/mm.h b/include/linux/mm.h index 9849dfda44d4..2d1f16ffc6ac 100644 --- a/include/linux/mm.h +++ b/include/linux/mm.h @@ -4164,6 +4164,23 @@ static inline void setup_nr_node_ids(void) {} =20 extern int memcmp_pages(struct page *page1, struct page *page2); =20 +static inline int identical_folio(struct folio *folio1, struct folio *foli= o2) +{ + int i, nr1, nr2, ret =3D 0; + + nr1 =3D folio_nr_pages(folio1); + nr2 =3D folio_nr_pages(folio2); + if (nr1 !=3D nr2) + return 0; + + for (i =3D 0; i < nr1; i++) { + ret =3D memcmp_pages(folio_page(folio1, i), folio_page(folio2, i)); + if (ret) + break; + } + + return !ret; +} static inline int pages_identical(struct page *page1, struct page *page2) { return !memcmp_pages(page1, page2); diff --git a/mm/ksm.c b/mm/ksm.c index 21bfa9bfb210..2e4790842515 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -1527,7 +1527,7 @@ static int try_to_merge_one_page(struct vm_area_struc= t *vma, struct folio *folio if (!folio_test_dirty(folio)) folio_set_dirty(folio); err =3D 0; - } else if (pages_identical(page, kpage)) + } else if (identical_folio(folio, kfolio)) err =3D replace_page(vma, page, kpage, orig_pte); } =20 --=20 2.43.0 From nobody Fri Feb 13 01:39:48 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E8C1814036B for ; Tue, 4 Jun 2024 04:21:46 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717474907; cv=none; b=RXHIIXTr37HHCn5paPq6TAHj3N7XCc+VMWCqwHqyUPMO4bqmqYonrmSGfDeWNRy/6znNPePIhPnyr9bJM4beIYTiumioia5psV1qGmnvgE4Q3DJi+8v7Z+qIMSg51MWXUwL3sKEdTOM1l37wHOzF7sDJzv7HencRX0OfxJgsw38= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717474907; c=relaxed/simple; bh=coNrFyNql7rzA+Wu1zS+Imisztx1VEYHkdkeqtcLPJ0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ezmnEAc1eLdIaBdDttsjMccv6OOGSqY0bBzGgzUf+ecId+BFIWRgl/ZR0rtDz7j4lxWAxYh94Wt2YmRvrdhDMbRBSYrCyBB+srBPa7vnF6jSRD2GvGxiM7YBoFmCnVvRIhjGJeOLg9AAgq8/sf2oxx63DLnCyAeC7e19s3Im0WU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MDSl5xy6; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="MDSl5xy6" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7F76EC2BBFC; Tue, 4 Jun 2024 04:21:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717474906; bh=coNrFyNql7rzA+Wu1zS+Imisztx1VEYHkdkeqtcLPJ0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MDSl5xy6AfvB2jwDYUYciKw0WAPH6HVMDLpvUtKv4MiMW2nJPsr8687kDmSuMgS2i 8ir4mEf4IzJsZxyqCpwMRG7zPuqEQoO2FcqBJt1Toe9V6ugm2H3oSFrRFxB0nJRYvO 6ZjV1r2iscSWsLM8U1FBXXxPO9kLwGNyw46O7MPHow2J0CcrcBysxPOMtS9OSJHTXn kLsG0fd7lCw5iMa882oNvtx9mV5k5rsta8bJgKJcmH9Tjy3MLvJBxnK5D0J6VcqtKB /pWRYqxUdW7oaEdmPbUUkTDWV9VnNFT8B2tYE9n58p6TzwY1lajVhpANa7W9F7pRhx S/9+Par00zx4g== From: alexs@kernel.org To: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, izik.eidus@ravellosystems.com, willy@infradead.org, aarcange@redhat.com, chrisw@sous-sol.org, hughd@google.com, david@redhat.com Cc: "Alex Shi (tencent)" Subject: [PATCH 05/10] mm/ksm: use folio in stable_tree_search Date: Tue, 4 Jun 2024 12:24:47 +0800 Message-ID: <20240604042454.2012091-6-alexs@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240604042454.2012091-1-alexs@kernel.org> References: <20240604042454.2012091-1-alexs@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Alex Shi (tencent)" The stable_tree_search() function is actually using folio internal, just pass the folio parameter from cmp_and_merge_page(). And in cmp_and_merge_page(), the parameter 'page' is actually a folio too, so use folio to save a few compound check in necessnary context. Signed-off-by: Alex Shi (tencent) --- mm/ksm.c | 36 ++++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 2e4790842515..f68779651841 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -1825,7 +1825,7 @@ static __always_inline struct folio *chain(struct ksm= _stable_node **s_n_d, * This function returns the stable tree node of identical content if foun= d, * NULL otherwise. */ -static struct page *stable_tree_search(struct page *page) +static struct folio *stable_tree_search(struct folio *folio) { int nid; struct rb_root *root; @@ -1833,14 +1833,12 @@ static struct page *stable_tree_search(struct page = *page) struct rb_node *parent; struct ksm_stable_node *stable_node, *stable_node_dup, *stable_node_any; struct ksm_stable_node *page_node; - struct folio *folio; =20 - folio =3D page_folio(page); page_node =3D folio_stable_node(folio); if (page_node && page_node->head !=3D &migrate_nodes) { /* ksm page forked */ folio_get(folio); - return &folio->page; + return folio; } =20 nid =3D get_kpfn_nid(folio_pfn(folio)); @@ -1907,7 +1905,7 @@ static struct page *stable_tree_search(struct page *p= age) goto again; } =20 - ret =3D memcmp_pages(page, &tree_folio->page); + ret =3D memcmp_pages(folio_page(folio, 0), &tree_folio->page); folio_put(tree_folio); =20 parent =3D *new; @@ -1973,7 +1971,7 @@ static struct page *stable_tree_search(struct page *p= age) folio_put(tree_folio); goto replace; } - return &tree_folio->page; + return tree_folio; } } =20 @@ -1987,7 +1985,7 @@ static struct page *stable_tree_search(struct page *p= age) out: if (is_page_sharing_candidate(page_node)) { folio_get(folio); - return &folio->page; + return folio; } else return NULL; =20 @@ -2037,7 +2035,7 @@ static struct page *stable_tree_search(struct page *p= age) } stable_node_dup->head =3D &migrate_nodes; list_add(&stable_node_dup->list, stable_node_dup->head); - return &folio->page; + return folio; =20 chain_append: /* stable_node_dup could be null if it reached the limit */ @@ -2324,6 +2322,7 @@ static void cmp_and_merge_page(struct page *page, str= uct ksm_rmap_item *rmap_ite unsigned int checksum; int err; bool max_page_sharing_bypass =3D false; + struct folio *folio, *kfolio; =20 stable_node =3D page_stable_node(page); if (stable_node) { @@ -2346,30 +2345,31 @@ static void cmp_and_merge_page(struct page *page, s= truct ksm_rmap_item *rmap_ite } =20 /* We first start with searching the page inside the stable tree */ - kpage =3D stable_tree_search(page); - if (kpage =3D=3D page && rmap_item->head =3D=3D stable_node) { - put_page(kpage); + folio =3D page_folio(page); + kfolio =3D stable_tree_search(folio); + if (kfolio =3D=3D folio && rmap_item->head =3D=3D stable_node) { + folio_put(kfolio); return; } =20 remove_rmap_item_from_tree(rmap_item); =20 - if (kpage) { - if (PTR_ERR(kpage) =3D=3D -EBUSY) + if (kfolio) { + if (kfolio =3D=3D ERR_PTR(-EBUSY)) return; =20 - err =3D try_to_merge_with_ksm_page(rmap_item, page, kpage); + err =3D try_to_merge_with_ksm_page(rmap_item, page, folio_page(kfolio, 0= )); if (!err) { /* * The page was successfully merged: * add its rmap_item to the stable tree. */ - lock_page(kpage); - stable_tree_append(rmap_item, page_stable_node(kpage), + folio_lock(kfolio); + stable_tree_append(rmap_item, folio_stable_node(kfolio), max_page_sharing_bypass); - unlock_page(kpage); + folio_unlock(kfolio); } - put_page(kpage); + folio_put(kfolio); return; } =20 --=20 2.43.0 From nobody Fri Feb 13 01:39:48 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2F00C140E58 for ; Tue, 4 Jun 2024 04:21:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717474910; cv=none; b=Jv0w2J4F12qSZjMm/KwTZltUto3IlJ4eRjfMQPngh1pJktNtVo2HjggzyCM+m6sWnEOiln25Yc+jRUwBXZPD4vADS2FBFYTfOx/fOT8YWcsF6LIhgMhgHfGiISA7HbeER0jcjfHlqvizJNM8bkk6hXVxzlb0A3n/KK0gBSwrONg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717474910; c=relaxed/simple; bh=r3mq/HoGLqusQ4dyUdx5DxYmG67Uuzo2XB6s67qh+EE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VZ8rf0BOUD4SZHaCDHfrt/2YNyMRdssaafyGIktYRcMhkQSkZZ9DKONC7vw04Lk17DYH9o6506iHuJpCWA6vqWsISfKaSRbf9gbr+KrWT85Qm3EiUqVwDvtfG72y1hN7Iej+zB0fqJxY/1ZX/f1SVzoJtmZTUs72iSHeQc9hE7s= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=f0kDChGN; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="f0kDChGN" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6C2A0C32786; Tue, 4 Jun 2024 04:21:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717474909; bh=r3mq/HoGLqusQ4dyUdx5DxYmG67Uuzo2XB6s67qh+EE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=f0kDChGNADArwjFTu0Ecxn/z6ERtbQhoafRczm2xl1DKivY/FBSpOu3M+FIXHE6fF 9Q0KEuEWhS6uMtUMtCAR4SdvOqzexhe1KgXmnM9iLbGsMsGOhrlExEnyIhzDOgWHGL cDtkyQewEf1KcrjO6fI9ttmJUheFSRDfKyw5Y508mgtq2AIqf6Dcd037olbbAV+Nct Oojtpd89prXDc+dzsNEoIqTpc7ewALNrTL2+F1hmpea4SqHVs2ba3fufnIKMh5xfPB lykxIhWDHzdJ/3dNwpJ+heYxTFMd68/BX7l3eRuP3lfSY4mAibw2pQq/it3pAyAEGs GYPBMNqRZPP3Q== From: alexs@kernel.org To: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, izik.eidus@ravellosystems.com, willy@infradead.org, aarcange@redhat.com, chrisw@sous-sol.org, hughd@google.com, david@redhat.com Cc: "Alex Shi (tencent)" Subject: [PATCH 06/10] mm/ksm: remove page_stable_node Date: Tue, 4 Jun 2024 12:24:48 +0800 Message-ID: <20240604042454.2012091-7-alexs@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240604042454.2012091-1-alexs@kernel.org> References: <20240604042454.2012091-1-alexs@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Alex Shi (tencent)" Since folio_stable_node could replace page_stable_node now, the latter function could be removed and save a bit context. Signed-off-by: Alex Shi (tencent) --- mm/ksm.c | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index f68779651841..24de562b64e1 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -1086,11 +1086,6 @@ static inline struct ksm_stable_node *folio_stable_n= ode(struct folio *folio) return folio_test_ksm(folio) ? folio_raw_mapping(folio) : NULL; } =20 -static inline struct ksm_stable_node *page_stable_node(struct page *page) -{ - return folio_stable_node(page_folio(page)); -} - static inline void folio_set_stable_node(struct folio *folio, struct ksm_stable_node *stable_node) { @@ -2324,7 +2319,8 @@ static void cmp_and_merge_page(struct page *page, str= uct ksm_rmap_item *rmap_ite bool max_page_sharing_bypass =3D false; struct folio *folio, *kfolio; =20 - stable_node =3D page_stable_node(page); + folio =3D page_folio(page); + stable_node =3D folio_stable_node(folio); if (stable_node) { if (stable_node->head !=3D &migrate_nodes && get_kpfn_nid(READ_ONCE(stable_node->kpfn)) !=3D @@ -2345,7 +2341,6 @@ static void cmp_and_merge_page(struct page *page, str= uct ksm_rmap_item *rmap_ite } =20 /* We first start with searching the page inside the stable tree */ - folio =3D page_folio(page); kfolio =3D stable_tree_search(folio); if (kfolio =3D=3D folio && rmap_item->head =3D=3D stable_node) { folio_put(kfolio); --=20 2.43.0 From nobody Fri Feb 13 01:39:48 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1AE0D1411E9 for ; Tue, 4 Jun 2024 04:21:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717474913; cv=none; b=osD9GUdjaEU0fLfFUy/iTxVttirP5CFEdTmvofIkxyVbi6Kcq9vup7Iuiba2eEXl3jMKcSZU1JeBkeZy5wGjNJVGOfleBqtWs/Hbu/VjSXwFXtMHsDkPCdOp+lBNTe1nm/wehgXyK+S42QrxbiEpwNW3LsA90ubhPdibSz1QJf0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717474913; c=relaxed/simple; bh=LoJfO2Ls93RlnzKSfQa0n7wjB2DL3+BBi16/0XKQwOI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=oCka5KsIHjU/DgmUugv8b3OpJVMpFfJ+GjKTt7qX/EN6VZdR4fLcGFB6LSOcrX9Cr7JlFPws7tO7vPQy2jo58GaDf/G1v8omvVPFORjAiJLjEA+HTEVS0VXm2cZI9ibFoZWT5XDZTjv4aLlkNVbvOH9uWoAyj+/H/RQ9IfS+L9Q= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=RHO3PveI; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="RHO3PveI" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 58B25C32786; Tue, 4 Jun 2024 04:21:50 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717474912; bh=LoJfO2Ls93RlnzKSfQa0n7wjB2DL3+BBi16/0XKQwOI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=RHO3PveI2oavxMNpkEuXKJde+ksLgcEi44dvri14Czc1WMsBGUjH7nsZo+HgzTwk5 kfBBUNQ1X2VjlkCaCQICHYJLyHlCcfs2H8EyjLEvhrkJwzbgl4rmrIrXh8KKw4GWgO +B7V62sDNVW43wX7GKXMJgGFTVJ+75KTJIAEthNzgAiuA5wbii6dkqQrRn+CrErtt/ njhraJfTdI1oXQjkB2w2DQlBnqUjSaHG3MCsGDWZMPTiVe+phXMDyARlN6r68pExT/ RKud+RLAPmKjpo5VaCpohWROwJiE50q3C3lYnqt7siuesgAwYO0z3cbI4Ctydrks5a OfEPkCx5Z9wyQ== From: alexs@kernel.org To: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, izik.eidus@ravellosystems.com, willy@infradead.org, aarcange@redhat.com, chrisw@sous-sol.org, hughd@google.com, david@redhat.com Cc: "Alex Shi (tencent)" Subject: [PATCH 07/10] mm/ksm: use folio in unstable_tree_search_insert Date: Tue, 4 Jun 2024 12:24:49 +0800 Message-ID: <20240604042454.2012091-8-alexs@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240604042454.2012091-1-alexs@kernel.org> References: <20240604042454.2012091-1-alexs@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Alex Shi (tencent)" The calling path are using folio actually, so start to folio in unstable_tree_search_insert to save few compound checks. Signed-off-by: Alex Shi (tencent) --- mm/ksm.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 24de562b64e1..14a7ca53fc91 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2194,15 +2194,14 @@ static struct ksm_stable_node *stable_tree_insert(s= truct folio *kfolio) */ static struct ksm_rmap_item *unstable_tree_search_insert(struct ksm_rmap_item *rm= ap_item, - struct page *page, - struct page **tree_pagep) + struct folio *folio, struct folio **tree_foliop) { struct rb_node **new; struct rb_root *root; struct rb_node *parent =3D NULL; int nid; =20 - nid =3D get_kpfn_nid(page_to_pfn(page)); + nid =3D get_kpfn_nid(folio_pfn(folio)); root =3D root_unstable_tree + nid; new =3D &root->rb_node; =20 @@ -2220,12 +2219,12 @@ struct ksm_rmap_item *unstable_tree_search_insert(s= truct ksm_rmap_item *rmap_ite /* * Don't substitute a ksm page for a forked page. */ - if (page =3D=3D tree_page) { + if (folio =3D=3D page_folio(tree_page)) { put_page(tree_page); return NULL; } =20 - ret =3D memcmp_pages(page, tree_page); + ret =3D memcmp_pages(folio_page(folio, 0), tree_page); =20 parent =3D *new; if (ret < 0) { @@ -2244,7 +2243,7 @@ struct ksm_rmap_item *unstable_tree_search_insert(str= uct ksm_rmap_item *rmap_ite put_page(tree_page); return NULL; } else { - *tree_pagep =3D tree_page; + *tree_foliop =3D page_folio(tree_page); return tree_rmap_item; } } @@ -2312,6 +2311,7 @@ static void cmp_and_merge_page(struct page *page, str= uct ksm_rmap_item *rmap_ite struct mm_struct *mm =3D rmap_item->mm; struct ksm_rmap_item *tree_rmap_item; struct page *tree_page =3D NULL; + struct folio *tree_folio =3D NULL; struct ksm_stable_node *stable_node; struct page *kpage; unsigned int checksum; @@ -2411,7 +2411,7 @@ static void cmp_and_merge_page(struct page *page, str= uct ksm_rmap_item *rmap_ite return; } tree_rmap_item =3D - unstable_tree_search_insert(rmap_item, page, &tree_page); + unstable_tree_search_insert(rmap_item, folio, &tree_folio); if (tree_rmap_item) { bool split; =20 --=20 2.43.0 From nobody Fri Feb 13 01:39:48 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0D7E91419B4 for ; Tue, 4 Jun 2024 04:21:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717474916; cv=none; b=SE/+ZO2Q0jjPuAyVEZVQCridcXts+VTjCjoYFOw/FY0tj94b2gcwPoyoyzbtsc9Ct0lp3WJkVVYLWmxEEyd2hD4lPDdh5GI3TxYb5KnnPdDpkURs5yT6z2F/olkeKBJ6XrquHOX5g4UXpuOOXU/q+EPVycov6VNLictK8AUxhvY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717474916; c=relaxed/simple; bh=+wxYOCQhGAvuabF7WUDH5FG6NDiLukYmLnN5Qc7mdHw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=hItCiuswP+Uso08l+CwsRgFNt1ikzf9YrbLFyY6sHPikn1XBqgPh2oeXnU778YOd/gplqMIg82lrPUVW1oS47dc98MO4Gwp5tcpdLwBOXING9f2hVTHcD+AvPUFQ9qRbZ4bSnLC2czT/JC7LUusuZTl8F/N904EveAF2LOOvp2w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Snb8+csf; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="Snb8+csf" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 455FCC4AF08; Tue, 4 Jun 2024 04:21:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717474915; bh=+wxYOCQhGAvuabF7WUDH5FG6NDiLukYmLnN5Qc7mdHw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Snb8+csfuq27jG6c8lfDxSgt/4j/QIAj9TQhYFFMh9FFT0v2DENV7qrcE36FqNJ2r 1bE47raxT2o35HbYAezf6ONYaO4XuHDqGEBVYscppHzq4Y7XMwccQyYP1ERXNIrTbT RFvF8+Z84L8XaaPM3RcfZ9HCjdVaCoECnBZDoBxEviTQ8tzrh/6ZTdVKmaJIYNmerq 7L2s0BkeVPHs7RoKsrQCcFw+ddIyu/Gs4l1Arj+k8Kg+S5tT25HXNzu6XTI3wu26hh 0Dih/9LM2Mv/2NLu2Kmwz63CkFfrgCJGwJL8B2MxkYUPExNcSMpPJLyWiT/6uCJ+ea aW7RreqKYOa0Q== From: alexs@kernel.org To: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, izik.eidus@ravellosystems.com, willy@infradead.org, aarcange@redhat.com, chrisw@sous-sol.org, hughd@google.com, david@redhat.com Cc: "Alex Shi (tencent)" Subject: [PATCH 08/10] mm/ksm: use folio in try_to_merge_xx serie funcs Date: Tue, 4 Jun 2024 12:24:50 +0800 Message-ID: <20240604042454.2012091-9-alexs@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240604042454.2012091-1-alexs@kernel.org> References: <20240604042454.2012091-1-alexs@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Alex Shi (tencent)" The try_to_merge_ serie funcs are using folios, so change the parameter and variable 'page' to folio to save compound checks. Signed-off-by: Alex Shi (tencent) --- mm/ksm.c | 54 ++++++++++++++++++++++++------------------------------ 1 file changed, 24 insertions(+), 30 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index 14a7ca53fc91..b9c04ce677b9 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -1540,22 +1540,18 @@ static int try_to_merge_one_page(struct vm_area_str= uct *vma, struct folio *folio * This function returns 0 if the pages were merged, -EFAULT otherwise. */ static int try_to_merge_with_ksm_page(struct ksm_rmap_item *rmap_item, - struct page *page, struct page *kpage) + struct folio *folio, struct folio *kfolio) { struct mm_struct *mm =3D rmap_item->mm; struct vm_area_struct *vma; int err =3D -EFAULT; - struct folio *kfolio; =20 mmap_read_lock(mm); vma =3D find_mergeable_vma(mm, rmap_item->address); if (!vma) goto out; =20 - if (kpage) - kfolio =3D page_folio(kpage); - - err =3D try_to_merge_one_page(vma, page_folio(page), rmap_item, kfolio); + err =3D try_to_merge_one_page(vma, folio, rmap_item, kfolio); if (err) goto out; =20 @@ -1567,8 +1563,8 @@ static int try_to_merge_with_ksm_page(struct ksm_rmap= _item *rmap_item, get_anon_vma(vma->anon_vma); out: mmap_read_unlock(mm); - trace_ksm_merge_with_ksm_page(kpage, page_to_pfn(kpage ? kpage : page), - rmap_item, mm, err); + trace_ksm_merge_with_ksm_page(kfolio, folio_pfn(kfolio ? kfolio : folio), + rmap_item, mm, err); return err; } =20 @@ -1582,17 +1578,17 @@ static int try_to_merge_with_ksm_page(struct ksm_rm= ap_item *rmap_item, * Note that this function upgrades page to ksm page: if one of the pages * is already a ksm page, try_to_merge_with_ksm_page should be used. */ -static struct page *try_to_merge_two_pages(struct ksm_rmap_item *rmap_item, - struct page *page, +static struct folio *try_to_merge_two_pages(struct ksm_rmap_item *rmap_ite= m, + struct folio *folio, struct ksm_rmap_item *tree_rmap_item, - struct page *tree_page) + struct folio *tree_folio) { int err; =20 - err =3D try_to_merge_with_ksm_page(rmap_item, page, NULL); + err =3D try_to_merge_with_ksm_page(rmap_item, folio, NULL); if (!err) { err =3D try_to_merge_with_ksm_page(tree_rmap_item, - tree_page, page); + tree_folio, folio); /* * If that fails, we have a ksm page with only one pte * pointing to it: so break it. @@ -1600,7 +1596,7 @@ static struct page *try_to_merge_two_pages(struct ksm= _rmap_item *rmap_item, if (err) break_cow(rmap_item); } - return err ? NULL : page; + return err ? NULL : folio; } =20 static __always_inline @@ -2310,14 +2306,13 @@ static void cmp_and_merge_page(struct page *page, s= truct ksm_rmap_item *rmap_ite { struct mm_struct *mm =3D rmap_item->mm; struct ksm_rmap_item *tree_rmap_item; - struct page *tree_page =3D NULL; struct folio *tree_folio =3D NULL; struct ksm_stable_node *stable_node; - struct page *kpage; + struct folio *kfolio; unsigned int checksum; int err; bool max_page_sharing_bypass =3D false; - struct folio *folio, *kfolio; + struct folio *folio; =20 folio =3D page_folio(page); stable_node =3D folio_stable_node(folio); @@ -2353,7 +2348,7 @@ static void cmp_and_merge_page(struct page *page, str= uct ksm_rmap_item *rmap_ite if (kfolio =3D=3D ERR_PTR(-EBUSY)) return; =20 - err =3D try_to_merge_with_ksm_page(rmap_item, page, folio_page(kfolio, 0= )); + err =3D try_to_merge_with_ksm_page(rmap_item, folio, kfolio); if (!err) { /* * The page was successfully merged: @@ -2415,8 +2410,8 @@ static void cmp_and_merge_page(struct page *page, str= uct ksm_rmap_item *rmap_ite if (tree_rmap_item) { bool split; =20 - kpage =3D try_to_merge_two_pages(rmap_item, page, - tree_rmap_item, tree_page); + kfolio =3D try_to_merge_two_pages(rmap_item, folio, + tree_rmap_item, tree_folio); /* * If both pages we tried to merge belong to the same compound * page, then we actually ended up increasing the reference @@ -2427,23 +2422,22 @@ static void cmp_and_merge_page(struct page *page, s= truct ksm_rmap_item *rmap_ite * afterwards, the reference count will be correct and * split_huge_page should succeed. */ - split =3D PageTransCompound(page) - && compound_head(page) =3D=3D compound_head(tree_page); - put_page(tree_page); - if (kpage) { + split =3D folio_test_large(folio) && folio =3D=3D kfolio; + folio_put(tree_folio); + if (kfolio) { /* * The pages were successfully merged: insert new * node in the stable tree and add both rmap_items. */ - lock_page(kpage); - stable_node =3D stable_tree_insert(page_folio(kpage)); + folio_lock(kfolio); + stable_node =3D stable_tree_insert(kfolio); if (stable_node) { stable_tree_append(tree_rmap_item, stable_node, false); stable_tree_append(rmap_item, stable_node, false); } - unlock_page(kpage); + folio_unlock(kfolio); =20 /* * If we fail to insert the page into the stable tree, @@ -2465,10 +2459,10 @@ static void cmp_and_merge_page(struct page *page, s= truct ksm_rmap_item *rmap_ite * the page is locked, it is better to skip it and * perhaps try again later. */ - if (!trylock_page(page)) + if (!folio_trylock(folio)) return; - split_huge_page(page); - unlock_page(page); + split_folio(folio); + folio_unlock(folio); } } } --=20 2.43.0 From nobody Fri Feb 13 01:39:48 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D19CA13DBA4 for ; Tue, 4 Jun 2024 04:21:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717474918; cv=none; b=A+Pw+IZXtzCwVUtCYUU1Dmqyw656vSLli3WTjFqGCAecS2OoEE8ddpR5Ik6HB+tx0eopsg5oy6b9982GMBtdvK7TirGeGEScnGyJg3j/e1/rnwPzpipWj1iStG5L0m7lYIDjlHZbjJrvaRI2uZPyG9Ej645GDMazhvKtmmPXiMo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717474918; c=relaxed/simple; bh=oMozYVrFFGB9vcI3OPMo0SADR+/06RLvlkdM1E9kWqM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=i8JB0Couy+diBn0T2ovFq8fy7NL9yoN4QeFqjuETp4lW31Ab3Cfai8bH4PpKTxrGanWCJf7jcnu0ibNUg3UIZPqXLe7TLxpzsaKgUJDIRokvBCgxdEgemsukvYrH/3Q5qkye7GGi2EkJE1wEfqh/AkW1ZFL6AcUC7jclblgSPug= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=vFtitKi7; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="vFtitKi7" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 31BB0C3277B; Tue, 4 Jun 2024 04:21:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717474918; bh=oMozYVrFFGB9vcI3OPMo0SADR+/06RLvlkdM1E9kWqM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=vFtitKi75VkslnGm5HM61imI+AbUmGKz0XcZjPDTUXd4NBl0VY8yLM7DCkeyfOfHa xce4eXMDrC42qjTbB9rUkdRq2a8Mzr/gRE6n7jyJ5wrIijxuvx0EVz+2JWscYQ1eni PXd30EC2VbtuS7mAYSInQcYOKXDWQzdtJKehTOrmpw7gchDRcLFb4IPiBkG7oRjrw/ QLBif67iTIrzHu50sls1bMYAS8pxVccQzenPPP2FPWCPsP2Yx06/yzy3ySpSkZbALD tLysh0FKSJCUIYf7cfuUoZEVxngYOJc+k1Gn93FmkAzrAKJC1ycbvaV12i1sfA743L dAD75MsvLbzcg== From: alexs@kernel.org To: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, izik.eidus@ravellosystems.com, willy@infradead.org, aarcange@redhat.com, chrisw@sous-sol.org, hughd@google.com, david@redhat.com Cc: "Alex Shi (tencent)" Subject: [PATCH 09/10] mm/ksm: calc_checksum for folio Date: Tue, 4 Jun 2024 12:24:51 +0800 Message-ID: <20240604042454.2012091-10-alexs@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240604042454.2012091-1-alexs@kernel.org> References: <20240604042454.2012091-1-alexs@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Alex Shi (tencent)" Let's check the whole folio for contents change, don't count it if the folio changed. Signed-off-by: Alex Shi (tencent) --- mm/ksm.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index b9c04ce677b9..dc2b5e6a9659 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -1258,11 +1258,13 @@ static int unmerge_and_remove_all_rmap_items(void) } #endif /* CONFIG_SYSFS */ =20 -static u32 calc_checksum(struct page *page) +static u32 calc_checksum(struct folio *folio) { u32 checksum; - void *addr =3D kmap_local_page(page); - checksum =3D xxhash(addr, PAGE_SIZE, 0); + int nr =3D folio_nr_pages(folio); + void *addr =3D kmap_local_page(folio_page(folio, 0)); + + checksum =3D xxhash(addr, nr * PAGE_SIZE, 0); kunmap_local(addr); return checksum; } @@ -2369,7 +2371,7 @@ static void cmp_and_merge_page(struct page *page, str= uct ksm_rmap_item *rmap_ite * don't want to insert it in the unstable tree, and we don't want * to waste our time searching for something identical to it there. */ - checksum =3D calc_checksum(page); + checksum =3D calc_checksum(folio); if (rmap_item->oldchecksum !=3D checksum) { rmap_item->oldchecksum =3D checksum; return; @@ -2385,7 +2387,7 @@ static void cmp_and_merge_page(struct page *page, str= uct ksm_rmap_item *rmap_ite mmap_read_lock(mm); vma =3D find_mergeable_vma(mm, rmap_item->address); if (vma) { - err =3D try_to_merge_one_page(vma, page_folio(page), rmap_item, + err =3D try_to_merge_one_page(vma, folio, rmap_item, page_folio(ZERO_PAGE(rmap_item->address))); trace_ksm_merge_one_page( page_to_pfn(ZERO_PAGE(rmap_item->address)), @@ -3916,7 +3918,7 @@ static int __init ksm_init(void) int err; =20 /* The correct value depends on page size and endianness */ - zero_checksum =3D calc_checksum(ZERO_PAGE(0)); + zero_checksum =3D calc_checksum(page_folio(ZERO_PAGE(0))); /* Default to false for backwards compatibility */ ksm_use_zero_pages =3D false; =20 --=20 2.43.0 From nobody Fri Feb 13 01:39:48 2026 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F204513E3F9 for ; Tue, 4 Jun 2024 04:22:01 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717474922; cv=none; b=tkce6O5jH2bMd+giLk1tEHA+xPOv7ncKEJlBAdxqwQtTzGk5SXXo72NvPVnK0Copmd8g9hggbyA+ZliI1Qiu+eLvJLqTYSa+JndFnxW5BE2hsNcM5XFer0o9POJTghzFDe2Tcbn+JGq1wqFy8MsgetzhHaWKYFQcPZUyKO8eFGs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717474922; c=relaxed/simple; bh=zYmT3lwFo6rWfb+tk0qEdUJ1sHLo1w0Ku5xx7RwN91E=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=HaButu6Ptddsu2mb6h8F4k05DWkGpftBEPDgpZRK2lcanajcbP3nEPp7b776LKLzNVXSy4PAHDKaIzQU/fFzz4mMUUfkeViMCFfT+Wk3MSm51s1OtGEJTmmhMrHgKcG3cUSIqGd69RMtc7fUz9eaYSP7rbt4ZMz0FTUCNAcT08A= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=n1n6Rqkd; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="n1n6Rqkd" Received: by smtp.kernel.org (Postfix) with ESMTPSA id F02A6C2BBFC; Tue, 4 Jun 2024 04:21:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1717474921; bh=zYmT3lwFo6rWfb+tk0qEdUJ1sHLo1w0Ku5xx7RwN91E=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=n1n6RqkdZBqCVuUxiRYbqCsvhoBbYlLF6Tvbv/GcWyfrKGI0NaYyHr/QY8N+fCXeo ZyqzSABKskTK19EVEKFZ0dPpOIG49//2tmiIEI5wj1RjvoGrfxWZMwUvYk3U1fMLZq M0sQEx6nVa6TrVMytPlCJK79PonvX/LOvVR9KZwKxF8cWxEHsRVrel6+djm0sUbqfy l+sZVswL5p6CcvKVP6t8yEMZlFgtcRTcEBMqW8CS1+bnZjfS9f6CSOaJL/axF4ejz7 RRoM2ihQmnQSyii+KyuXtFT5x+qw/CElR9Gf/KgRbdPbJIcCsvuGxA0QwKM0IhZkAE bgeMB1Y+dWEFw== From: alexs@kernel.org To: Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, izik.eidus@ravellosystems.com, willy@infradead.org, aarcange@redhat.com, chrisw@sous-sol.org, hughd@google.com, david@redhat.com Cc: "Alex Shi (tencent)" Subject: [PATCH 10/10] m/ksm: use folio in ksm scan path Date: Tue, 4 Jun 2024 12:24:52 +0800 Message-ID: <20240604042454.2012091-11-alexs@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240604042454.2012091-1-alexs@kernel.org> References: <20240604042454.2012091-1-alexs@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: "Alex Shi (tencent)" Now we could safely use folio in ksm_do_scan path to save a few compound checks. The 'page' still used since follow_page function still no folio version. Signed-off-by: Alex Shi (tencent) --- mm/ksm.c | 47 +++++++++++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/mm/ksm.c b/mm/ksm.c index dc2b5e6a9659..57904b3b18dc 100644 --- a/mm/ksm.c +++ b/mm/ksm.c @@ -2304,7 +2304,7 @@ static void stable_tree_append(struct ksm_rmap_item *= rmap_item, * @page: the page that we are searching identical page to. * @rmap_item: the reverse mapping into the virtual address of this page */ -static void cmp_and_merge_page(struct page *page, struct ksm_rmap_item *rm= ap_item) +static void cmp_and_merge_page(struct folio *folio, struct ksm_rmap_item *= rmap_item) { struct mm_struct *mm =3D rmap_item->mm; struct ksm_rmap_item *tree_rmap_item; @@ -2314,9 +2314,7 @@ static void cmp_and_merge_page(struct page *page, str= uct ksm_rmap_item *rmap_ite unsigned int checksum; int err; bool max_page_sharing_bypass =3D false; - struct folio *folio; =20 - folio =3D page_folio(page); stable_node =3D folio_stable_node(folio); if (stable_node) { if (stable_node->head !=3D &migrate_nodes && @@ -2518,12 +2516,12 @@ static unsigned int skip_age(rmap_age_t age) } =20 /* - * Determines if a page should be skipped for the current scan. + * Determines if a folio should be skipped for the current scan. * - * @page: page to check - * @rmap_item: associated rmap_item of page + * @folio: folio to check + * @rmap_item: associated rmap_item of folio */ -static bool should_skip_rmap_item(struct page *page, +static bool should_skip_rmap_item(struct folio *folio, struct ksm_rmap_item *rmap_item) { rmap_age_t age; @@ -2536,7 +2534,7 @@ static bool should_skip_rmap_item(struct page *page, * will essentially ignore them, but we still have to process them * properly. */ - if (PageKsm(page)) + if (folio_test_ksm(folio)) return false; =20 age =3D rmap_item->age; @@ -2566,7 +2564,7 @@ static bool should_skip_rmap_item(struct page *page, return true; } =20 -static struct ksm_rmap_item *scan_get_next_rmap_item(struct page **page) +static struct ksm_rmap_item *scan_get_next_rmap_item(struct folio **folio) { struct mm_struct *mm; struct ksm_mm_slot *mm_slot; @@ -2655,36 +2653,41 @@ static struct ksm_rmap_item *scan_get_next_rmap_ite= m(struct page **page) ksm_scan.address =3D vma->vm_end; =20 while (ksm_scan.address < vma->vm_end) { + struct page *page; + if (ksm_test_exit(mm)) break; - *page =3D follow_page(vma, ksm_scan.address, FOLL_GET); - if (IS_ERR_OR_NULL(*page)) { + page =3D follow_page(vma, ksm_scan.address, FOLL_GET); + if (IS_ERR_OR_NULL(page)) { ksm_scan.address +=3D PAGE_SIZE; cond_resched(); continue; } =20 - VM_WARN_ON(PageTail(*page)); - nr =3D compound_nr(*page); - if (is_zone_device_page(*page)) + *folio =3D page_folio(page); + VM_WARN_ON(PageTail(page)); + nr =3D folio_nr_pages(*folio); + + if (folio_is_zone_device(*folio)) goto next_page; - if (PageAnon(*page)) { + + if (folio_test_anon(*folio)) { rmap_item =3D get_next_rmap_item(mm_slot, ksm_scan.rmap_list, ksm_scan.address); if (rmap_item) { ksm_scan.rmap_list =3D &rmap_item->rmap_list; =20 - if (should_skip_rmap_item(*page, rmap_item)) + if (should_skip_rmap_item(*folio, rmap_item)) goto next_page; =20 ksm_scan.address +=3D nr * PAGE_SIZE; } else - put_page(*page); + folio_put(*folio); mmap_read_unlock(mm); return rmap_item; } next_page: - put_page(*page); + folio_put(*folio); ksm_scan.address +=3D nr * PAGE_SIZE; cond_resched(); } @@ -2755,16 +2758,16 @@ static struct ksm_rmap_item *scan_get_next_rmap_ite= m(struct page **page) static void ksm_do_scan(unsigned int scan_npages) { struct ksm_rmap_item *rmap_item; - struct page *page; + struct folio *folio; unsigned int npages =3D scan_npages; =20 while (npages-- && likely(!freezing(current))) { cond_resched(); - rmap_item =3D scan_get_next_rmap_item(&page); + rmap_item =3D scan_get_next_rmap_item(&folio); if (!rmap_item) return; - cmp_and_merge_page(page, rmap_item); - put_page(page); + cmp_and_merge_page(folio, rmap_item); + folio_put(folio); } =20 ksm_pages_scanned +=3D scan_npages - npages; --=20 2.43.0