From nobody Wed Dec 17 13:15:15 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6E305CDB484 for ; Wed, 18 Oct 2023 20:33:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231895AbjJRUdN (ORCPT ); Wed, 18 Oct 2023 16:33:13 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53840 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231285AbjJRUdJ (ORCPT ); Wed, 18 Oct 2023 16:33:09 -0400 Received: from mail-pg1-x531.google.com (mail-pg1-x531.google.com [IPv6:2607:f8b0:4864:20::531]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B1412A4 for ; Wed, 18 Oct 2023 13:33:07 -0700 (PDT) Received: by mail-pg1-x531.google.com with SMTP id 41be03b00d2f7-5a9bf4fbd3fso4482279a12.1 for ; Wed, 18 Oct 2023 13:33:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697661187; x=1698265987; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=VN0zp0snARiXWQz31GgzZoIBHJFk7ynRrvZrhH9BDQE=; b=D2b1nL2Z0+kLLAkeFoJtazPKDb0mIfpq2MEulnM9kEayYpSPJ1BTw8TGMnUNwPXBrD fxXfdeO0bOacNxms4BKe071MgSnu5Cwp1hZgSU+mJmqSZtSXTIeeGzvvEb5Q11pPXoBr rXJw8Jv/yzKWyJM7yspm+cPXZ7CULsZ5qSs9pDytkKvogoXax7mQQRQXnC4LWkhnx4L8 dkVH/D49siXJuVfQeqSmJlNQhcEGOaFgMwkrqo6HcUFQsBKWMarlo+ZcZuwA84klrEQv omP/zVxZloIagrKA9xMUwJ1BmdC82sYLzYVkIMSRcr8ksI2/tSEzVyYg9ueRxLMsSSIs koiw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697661187; x=1698265987; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=VN0zp0snARiXWQz31GgzZoIBHJFk7ynRrvZrhH9BDQE=; b=SFbG/fCzbpchebk0c6Bk8OGevZfJQULrHOKt2R3rFWsfETFmpsDlvRCOslJFw45H1d H86xWYg0KkysX13EEtAQrxcXb1xEai561zkmOe/CfG429+vOxIERCB3uhUlHRfnv7ZaZ HjH/FdW9gZ1k1PemodicfvDjbzo1Id3a+8VCt9j+8kbkx5Rn0YpsBHfHj7eXO3vIhAbw ErJ9uLb3heKQNjSgiVlCpr6ecqRnRziDzl/mQlCqztBFfaKOULVrVr5v6U4JwUwS7Q6+ DWNNh92mxau45xiREF4WEHWrng35kJ4Rz6KQGEeCaClS0/N7IrPEc0UVnJ6CdGfMlr8/ mvrw== X-Gm-Message-State: AOJu0YyqxhlyM4ikECcL43zdRoz16yGajtb502BhlrpdG8c4Kq1+mMxH WKqcQEWHaLwzmF2C6+rtKf4= X-Google-Smtp-Source: AGHT+IE6UrwDxrBBS7hjFHieWc31jxdejoBL6wMFdYPOtBiCq+4eALL+xiIct2VZuc3ylwqsKZTYgw== X-Received: by 2002:a05:6a21:6da0:b0:14c:d494:77c5 with SMTP id wl32-20020a056a216da000b0014cd49477c5mr307966pzb.13.1697661187061; Wed, 18 Oct 2023 13:33:07 -0700 (PDT) Received: from fedora.. (c-73-170-51-167.hsd1.ca.comcast.net. [73.170.51.167]) by smtp.googlemail.com with ESMTPSA id w12-20020a170902d3cc00b001c61e628e9dsm354412plb.77.2023.10.18.13.33.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 13:33:06 -0700 (PDT) From: "Vishal Moola (Oracle)" To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, akpm@linux-foundation.org, shy828301@gmail.com, "Vishal Moola (Oracle)" , Matthew Wilcox Subject: [PATCH v2 1/5] mm/khugepaged: Convert __collapse_huge_page_isolate() to use folios Date: Wed, 18 Oct 2023 13:32:09 -0700 Message-Id: <20231018203213.50224-2-vishal.moola@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231018203213.50224-1-vishal.moola@gmail.com> References: <20231018203213.50224-1-vishal.moola@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Replaces 11 calls to compound_head() with 1, and removes 1348 bytes of kernel text. Signed-off-by: Vishal Moola (Oracle) Reviewed-by: Matthew Wilcox (Oracle) --- mm/khugepaged.c | 45 +++++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 22 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 88433cc25d8a..500756604488 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -542,6 +542,7 @@ static int __collapse_huge_page_isolate(struct vm_area_= struct *vma, struct list_head *compound_pagelist) { struct page *page =3D NULL; + struct folio *folio =3D NULL; pte_t *_pte; int none_or_zero =3D 0, shared =3D 0, result =3D SCAN_FAIL, referenced = =3D 0; bool writable =3D false; @@ -576,7 +577,8 @@ static int __collapse_huge_page_isolate(struct vm_area_= struct *vma, goto out; } =20 - VM_BUG_ON_PAGE(!PageAnon(page), page); + folio =3D page_folio(page); + VM_BUG_ON_FOLIO(!folio_test_anon(folio), folio); =20 if (page_mapcount(page) > 1) { ++shared; @@ -588,16 +590,15 @@ static int __collapse_huge_page_isolate(struct vm_are= a_struct *vma, } } =20 - if (PageCompound(page)) { - struct page *p; - page =3D compound_head(page); + if (folio_test_large(folio)) { + struct folio *f; =20 /* * Check if we have dealt with the compound page * already */ - list_for_each_entry(p, compound_pagelist, lru) { - if (page =3D=3D p) + list_for_each_entry(f, compound_pagelist, lru) { + if (folio =3D=3D f) goto next; } } @@ -608,7 +609,7 @@ static int __collapse_huge_page_isolate(struct vm_area_= struct *vma, * is needed to serialize against split_huge_page * when invoked from the VM. */ - if (!trylock_page(page)) { + if (!folio_trylock(folio)) { result =3D SCAN_PAGE_LOCK; goto out; } @@ -624,8 +625,8 @@ static int __collapse_huge_page_isolate(struct vm_area_= struct *vma, * but not from this process. The other process cannot write to * the page, only trigger CoW. */ - if (!is_refcount_suitable(page)) { - unlock_page(page); + if (!is_refcount_suitable(&folio->page)) { + folio_unlock(folio); result =3D SCAN_PAGE_COUNT; goto out; } @@ -634,27 +635,27 @@ static int __collapse_huge_page_isolate(struct vm_are= a_struct *vma, * Isolate the page to avoid collapsing an hugepage * currently in use by the VM. */ - if (!isolate_lru_page(page)) { - unlock_page(page); + if (!folio_isolate_lru(folio)) { + folio_unlock(folio); result =3D SCAN_DEL_PAGE_LRU; goto out; } - mod_node_page_state(page_pgdat(page), - NR_ISOLATED_ANON + page_is_file_lru(page), - compound_nr(page)); - VM_BUG_ON_PAGE(!PageLocked(page), page); - VM_BUG_ON_PAGE(PageLRU(page), page); + node_stat_mod_folio(folio, + NR_ISOLATED_ANON + folio_is_file_lru(folio), + folio_nr_pages(folio)); + VM_BUG_ON_FOLIO(!folio_test_locked(folio), folio); + VM_BUG_ON_FOLIO(folio_test_lru(folio), folio); =20 - if (PageCompound(page)) - list_add_tail(&page->lru, compound_pagelist); + if (folio_test_large(folio)) + list_add_tail(&folio->lru, compound_pagelist); next: /* * If collapse was initiated by khugepaged, check that there is * enough young pte to justify collapsing the page */ if (cc->is_khugepaged && - (pte_young(pteval) || page_is_young(page) || - PageReferenced(page) || mmu_notifier_test_young(vma->vm_mm, + (pte_young(pteval) || folio_test_young(folio) || + folio_test_referenced(folio) || mmu_notifier_test_young(vma->vm_mm, address))) referenced++; =20 @@ -668,13 +669,13 @@ static int __collapse_huge_page_isolate(struct vm_are= a_struct *vma, result =3D SCAN_LACK_REFERENCED_PAGE; } else { result =3D SCAN_SUCCEED; - trace_mm_collapse_huge_page_isolate(page, none_or_zero, + trace_mm_collapse_huge_page_isolate(&folio->page, none_or_zero, referenced, writable, result); return result; } out: release_pte_pages(pte, _pte, compound_pagelist); - trace_mm_collapse_huge_page_isolate(page, none_or_zero, + trace_mm_collapse_huge_page_isolate(&folio->page, none_or_zero, referenced, writable, result); return result; } --=20 2.40.1 From nobody Wed Dec 17 13:15:15 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3F5B0CDB47E for ; Wed, 18 Oct 2023 20:33:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231820AbjJRUdQ (ORCPT ); Wed, 18 Oct 2023 16:33:16 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53860 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231733AbjJRUdL (ORCPT ); Wed, 18 Oct 2023 16:33:11 -0400 Received: from mail-pg1-x536.google.com (mail-pg1-x536.google.com [IPv6:2607:f8b0:4864:20::536]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D8C8EA4 for ; Wed, 18 Oct 2023 13:33:08 -0700 (PDT) Received: by mail-pg1-x536.google.com with SMTP id 41be03b00d2f7-5aa7172bafdso3794617a12.1 for ; Wed, 18 Oct 2023 13:33:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697661188; x=1698265988; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=kOxvYaksrDFINgvTHvg7i3BLH6nikNi8tBwFFhT4+xI=; b=eBS8PwTi2rwkAWHvhPW6qyWd7RE9bHyK24SfVJiOPrfr1Y+JvtZuNEBh/A1hcPgIoR ZNUkfUu6kItbXORyqGHr+2BjIFQSMleNRHlF1kAPWDBCoZAju7t5bd0q9g/gPMfuDYQj UXIj5PW58mXwMzZmZrUr04LgiDOAjR0daaNjjwyX0Xpivht7iPDnEKN0T75+mEKKfgfo 81dNZwvBSKT8pvOvoDue6Ne/Jb890BG0KK17BU6V43p5ibS2Wc5QUBVi7BTrc2HJXm6e 83BFNXUsZsLecdMy6wW0ceiaUqAE1iuCOuWsLbvVzbbb+Zr9yoLNy959Femp/B+zCgf4 Hd9Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697661188; x=1698265988; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=kOxvYaksrDFINgvTHvg7i3BLH6nikNi8tBwFFhT4+xI=; b=VTUaEsUeH0QNhfvy9JwBhbTSbgmvnqTM9VNyvbI7pOnJw8RNmngG26SpP3Q1nRGt4u HlEWCI2hJ7Utj6sOP5D1TM37OwJjl9scGzX+7sakP2dEFq7Qk3uVpELUFxi18xjnLJ4C Mj9TIQKSa4zw2wu5n9HBrp/69m20OK5pVVRLOr9JqdHiKm5cL0j6bfn6rmZB0nOeo4zV BInvD130IHLvLWXPjg6YY2MNqrrgDTma6nvLlqJvOmEQXmL7audYs6GyovOEZ7lMFuro w/sf8kIWLG7wSei9NgQBo93iGaTgU7bHoXVmjr0H5JRgVqwNt8KA5SkPTxdm47DA9SY/ 0VyQ== X-Gm-Message-State: AOJu0YzTQdRZ06GjvRF74FY5OyTDPlBy1b4EeUfo1XVILffm/h6oyBp9 cjXdFlQ2MPOWUzisWnJZQFI= X-Google-Smtp-Source: AGHT+IGSy+gf+h7WIcfgpR19k4aoWiTB0tv72CZNC2cmYBpamwb6VVIlhQcsy15b27bnd2RjKzzbdg== X-Received: by 2002:a05:6a21:6d99:b0:14c:d494:77d1 with SMTP id wl25-20020a056a216d9900b0014cd49477d1mr327396pzb.10.1697661188209; Wed, 18 Oct 2023 13:33:08 -0700 (PDT) Received: from fedora.. (c-73-170-51-167.hsd1.ca.comcast.net. [73.170.51.167]) by smtp.googlemail.com with ESMTPSA id w12-20020a170902d3cc00b001c61e628e9dsm354412plb.77.2023.10.18.13.33.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 13:33:07 -0700 (PDT) From: "Vishal Moola (Oracle)" To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, akpm@linux-foundation.org, shy828301@gmail.com, "Vishal Moola (Oracle)" Subject: [PATCH v2 2/5] mm/khugepaged: Convert hpage_collapse_scan_pmd() to use folios Date: Wed, 18 Oct 2023 13:32:10 -0700 Message-Id: <20231018203213.50224-3-vishal.moola@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231018203213.50224-1-vishal.moola@gmail.com> References: <20231018203213.50224-1-vishal.moola@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Replaces 5 calls to compound_head(), and removes 1385 bytes of kernel text. Signed-off-by: Vishal Moola (Oracle) --- mm/khugepaged.c | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 500756604488..6c4b5af43371 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1248,6 +1248,7 @@ static int hpage_collapse_scan_pmd(struct mm_struct *= mm, int result =3D SCAN_FAIL, referenced =3D 0; int none_or_zero =3D 0, shared =3D 0; struct page *page =3D NULL; + struct folio *folio =3D NULL; unsigned long _address; spinlock_t *ptl; int node =3D NUMA_NO_NODE, unmapped =3D 0; @@ -1334,29 +1335,28 @@ static int hpage_collapse_scan_pmd(struct mm_struct= *mm, } } =20 - page =3D compound_head(page); - + folio =3D page_folio(page); /* * Record which node the original page is from and save this * information to cc->node_load[]. * Khugepaged will allocate hugepage from the node has the max * hit record. */ - node =3D page_to_nid(page); + node =3D folio_nid(folio); if (hpage_collapse_scan_abort(node, cc)) { result =3D SCAN_SCAN_ABORT; goto out_unmap; } cc->node_load[node]++; - if (!PageLRU(page)) { + if (!folio_test_lru(folio)) { result =3D SCAN_PAGE_LRU; goto out_unmap; } - if (PageLocked(page)) { + if (folio_test_locked(folio)) { result =3D SCAN_PAGE_LOCK; goto out_unmap; } - if (!PageAnon(page)) { + if (!folio_test_anon(folio)) { result =3D SCAN_PAGE_ANON; goto out_unmap; } @@ -1371,7 +1371,7 @@ static int hpage_collapse_scan_pmd(struct mm_struct *= mm, * has excessive GUP pins (i.e. 512). Anyway the same check * will be done again later the risk seems low. */ - if (!is_refcount_suitable(page)) { + if (!is_refcount_suitable(&folio->page)) { result =3D SCAN_PAGE_COUNT; goto out_unmap; } @@ -1381,8 +1381,8 @@ static int hpage_collapse_scan_pmd(struct mm_struct *= mm, * enough young pte to justify collapsing the page */ if (cc->is_khugepaged && - (pte_young(pteval) || page_is_young(page) || - PageReferenced(page) || mmu_notifier_test_young(vma->vm_mm, + (pte_young(pteval) || folio_test_young(folio) || + folio_test_referenced(folio) || mmu_notifier_test_young(vma->vm_mm, address))) referenced++; } @@ -1404,7 +1404,7 @@ static int hpage_collapse_scan_pmd(struct mm_struct *= mm, *mmap_locked =3D false; } out: - trace_mm_khugepaged_scan_pmd(mm, page, writable, referenced, + trace_mm_khugepaged_scan_pmd(mm, &folio->page, writable, referenced, none_or_zero, result, unmapped); return result; } --=20 2.40.1 From nobody Wed Dec 17 13:15:15 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 49963CDB47E for ; Wed, 18 Oct 2023 20:33:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232223AbjJRUdS (ORCPT ); Wed, 18 Oct 2023 16:33:18 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53876 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231731AbjJRUdM (ORCPT ); Wed, 18 Oct 2023 16:33:12 -0400 Received: from mail-pf1-x435.google.com (mail-pf1-x435.google.com [IPv6:2607:f8b0:4864:20::435]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E57DAFA for ; Wed, 18 Oct 2023 13:33:09 -0700 (PDT) Received: by mail-pf1-x435.google.com with SMTP id d2e1a72fcca58-6ba172c5f3dso3846614b3a.0 for ; Wed, 18 Oct 2023 13:33:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697661189; x=1698265989; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Q/rAUjlYbudj5wnw8lHkufWutUwYDRgsClPJWjawwgo=; b=MKd7LRLb6+ZMZprnyBlKS4S+XjCnoMmygPQog3kGNI4uLAsu9cBckzlC6MUMCOb/Vw pZqOh+xDE032cHk9gMgAkK0MCQ0OHsNaf/4cvzih3HhNfbKIz10VLmdqfsT38G+E1DAV VnRfmmv5bwMBfqa2bt+WfUA1RA5hci54RZQMFiuWaF9r7L3Xma9jDfmTTA4QJ+2362Bz fEEDoDOgxYM0eGHGQBECF3roZcIgtkS9+F+X70OTQ/v7kzhJaoHs6bmbw2/4akYsmU6j 2SV3jS2sVB7iQb0GusvOIBT5Ebpgv+BNjoa/KLo/CPyqq04FLtXpRiww1lSGgTxGoNYF Q9aA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697661189; x=1698265989; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Q/rAUjlYbudj5wnw8lHkufWutUwYDRgsClPJWjawwgo=; b=r38T5YNIhBDWJa02SisyJJ+yfvpnHL1o49XLqimR8uYPIENUp+zblOsql7fJmbUlzV +eGnpnYwf/woguOspcJwmr9P/ImrtdmnfmTfu3f/kTIlCGB3mhEnGHpxdde/+T+5Mkpa PLtTzqC0ggYb/kqMtkF+Ggmk1fImpqeiAuiUNCy2H2UyDSUTeitanhSC9NJNoHhqI5il uEUhbk9sObtD2JtmXLAR5F33eaf+H/vKT+VNMAcEIHzIcuQfOm92VMdMb1divDx2rKSi oby3JE9Rym04KChgf0nwYqsTwOGQaLZzdpKwXhhPomIE0B3cbU0xk7hvIfZ9GHc0XpXz GE4A== X-Gm-Message-State: AOJu0YyrLBLoZGJL3YVqrJJFXAp4Fkk9btvBZPgUVLr+R4/mIndgJ57D fdQMv+La5O96wtr+BVpHG6k= X-Google-Smtp-Source: AGHT+IHHKufGkhuDo4UJftnY7SwsN1FFsHrItzDWiS842QRY9mPhF1ZSrMfqcuvk3t7Vnjm0ZSWi9g== X-Received: by 2002:a05:6a21:3e0b:b0:171:c88a:890c with SMTP id bk11-20020a056a213e0b00b00171c88a890cmr269714pzc.25.1697661189243; Wed, 18 Oct 2023 13:33:09 -0700 (PDT) Received: from fedora.. (c-73-170-51-167.hsd1.ca.comcast.net. [73.170.51.167]) by smtp.googlemail.com with ESMTPSA id w12-20020a170902d3cc00b001c61e628e9dsm354412plb.77.2023.10.18.13.33.08 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 13:33:08 -0700 (PDT) From: "Vishal Moola (Oracle)" To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, akpm@linux-foundation.org, shy828301@gmail.com, "Vishal Moola (Oracle)" Subject: [PATCH v2 3/5] mm/khugepaged: Convert is_refcount_suitable() to use folios Date: Wed, 18 Oct 2023 13:32:11 -0700 Message-Id: <20231018203213.50224-4-vishal.moola@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231018203213.50224-1-vishal.moola@gmail.com> References: <20231018203213.50224-1-vishal.moola@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Both callers of is_refcount_suitable() have been converted to use folios, so convert it to take in a folio. Both callers only operate on head pages of folios so mapcount/refcount conversions here are trivial. Removes 3 calls to compound head, and removes 315 bytes of kernel text. Signed-off-by: Vishal Moola (Oracle) --- mm/khugepaged.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 6c4b5af43371..9efd8ff68f06 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -524,15 +524,15 @@ static void release_pte_pages(pte_t *pte, pte_t *_pte, } } =20 -static bool is_refcount_suitable(struct page *page) +static bool is_refcount_suitable(struct folio *folio) { int expected_refcount; =20 - expected_refcount =3D total_mapcount(page); - if (PageSwapCache(page)) - expected_refcount +=3D compound_nr(page); + expected_refcount =3D folio_mapcount(folio); + if (folio_test_swapcache(folio)) + expected_refcount +=3D folio_nr_pages(folio); =20 - return page_count(page) =3D=3D expected_refcount; + return folio_ref_count(folio) =3D=3D expected_refcount; } =20 static int __collapse_huge_page_isolate(struct vm_area_struct *vma, @@ -625,7 +625,7 @@ static int __collapse_huge_page_isolate(struct vm_area_= struct *vma, * but not from this process. The other process cannot write to * the page, only trigger CoW. */ - if (!is_refcount_suitable(&folio->page)) { + if (!is_refcount_suitable(folio)) { folio_unlock(folio); result =3D SCAN_PAGE_COUNT; goto out; @@ -1371,7 +1371,7 @@ static int hpage_collapse_scan_pmd(struct mm_struct *= mm, * has excessive GUP pins (i.e. 512). Anyway the same check * will be done again later the risk seems low. */ - if (!is_refcount_suitable(&folio->page)) { + if (!is_refcount_suitable(folio)) { result =3D SCAN_PAGE_COUNT; goto out_unmap; } --=20 2.40.1 From nobody Wed Dec 17 13:15:15 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3124DCDB47E for ; Wed, 18 Oct 2023 20:33:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232408AbjJRUdW (ORCPT ); Wed, 18 Oct 2023 16:33:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53892 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231862AbjJRUdN (ORCPT ); Wed, 18 Oct 2023 16:33:13 -0400 Received: from mail-pl1-x62b.google.com (mail-pl1-x62b.google.com [IPv6:2607:f8b0:4864:20::62b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D019B114 for ; Wed, 18 Oct 2023 13:33:10 -0700 (PDT) Received: by mail-pl1-x62b.google.com with SMTP id d9443c01a7336-1c9e06f058bso748365ad.0 for ; Wed, 18 Oct 2023 13:33:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697661190; x=1698265990; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=rvpcd99NjN7SPkeCiS8fl8F++V5hN5b5iiRnzQcVK8o=; b=IMT0+xS+0a1edNRwkCunB/DGo3L+bp0M6E520xrCHt9ukaYNX2uhFxEk0tc4KfBrOu VB9SnoTKUdNCqAosjLG21O3eG3Mg1150x1b2iE5GkUlFVe8hXT3HoOMDvx8jnAVBOdG9 oG01SAIK7O9scM+GJXXUestISQTwZWc/R0VKnjjsIvFTjRx1NrLfG8LwNf/b4RfXPPsm 27U8qoQOjGZfFhxoY+mhLjBnztBtwoss2N12Ol5bnPFLlucY5yO+ww+14yoeO8Bt3GzA KBjlGgH3uwsM3PRui9q61yBiD6SUg1CzgWkW2ydzOxBrFknBcFRLdH1tV4zynBJDzBQv 52RA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697661190; x=1698265990; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=rvpcd99NjN7SPkeCiS8fl8F++V5hN5b5iiRnzQcVK8o=; b=APZK6Ds4PbJl3qhNNexjHN3KkVpt1WPhxyH01Uzj91rvobP6xIbCcjf4dZZ5+u79vk OGWbtxFw0N3tZrk+fAyLcVTZbu4bJcWq1xBQi83R+iFzvZuMILm35cLIFr02nSgdhSDB fekqss09LvGrvP8v6CbzjtjHGrpfQhHavLDxzOAcrw/2ykOzSWZiLSwhol7UKCLBl+Op V4c8lfbhp3YR7AkHSMgsdh4VOe3xnidjWMBD7RyLOT6Qw+RyEss9lCE9QALigCKuI2NV MF6Jq4GvtFgkfiOvPgZvLBKOSf8QQUiTmLZKdFQnaJc+C8VofAsmin2fagMryfTHam1u IldA== X-Gm-Message-State: AOJu0Yyi3FCFyBBl7VRMVPIs2pfZ06TnisNSFcI4hwQWu989DOq8CJjL 0yCS/5yr0Hj0i/lBJCmdRWyy7p7PyfE= X-Google-Smtp-Source: AGHT+IEX5D/uPbXU1tWc0zw0n/3MIN2cV8mRj2GKuowCEB+zQiYHmm8zVTMDrPAIhqcUNoWkYM/oPw== X-Received: by 2002:a17:902:d2ca:b0:1c5:7d49:570e with SMTP id n10-20020a170902d2ca00b001c57d49570emr615553plc.29.1697661190159; Wed, 18 Oct 2023 13:33:10 -0700 (PDT) Received: from fedora.. (c-73-170-51-167.hsd1.ca.comcast.net. [73.170.51.167]) by smtp.googlemail.com with ESMTPSA id w12-20020a170902d3cc00b001c61e628e9dsm354412plb.77.2023.10.18.13.33.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 13:33:09 -0700 (PDT) From: "Vishal Moola (Oracle)" To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, akpm@linux-foundation.org, shy828301@gmail.com, "Vishal Moola (Oracle)" Subject: [PATCH v2 4/5] mm/khugepaged: Convert alloc_charge_hpage() to use folios Date: Wed, 18 Oct 2023 13:32:12 -0700 Message-Id: <20231018203213.50224-5-vishal.moola@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231018203213.50224-1-vishal.moola@gmail.com> References: <20231018203213.50224-1-vishal.moola@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Also remove count_memcg_page_event now that its last caller no longer uses = it, and convert hpage_collapse_alloc_page() to hpage_collapse_alloc_folio(), This removes 1 call to compound_head() and helps convert khugepaged to use folios throughout. Signed-off-by: Vishal Moola (Oracle) --- include/linux/memcontrol.h | 14 -------------- mm/khugepaged.c | 15 ++++++++------- 2 files changed, 8 insertions(+), 21 deletions(-) diff --git a/include/linux/memcontrol.h b/include/linux/memcontrol.h index ab94ad4597d0..3126bde982e8 100644 --- a/include/linux/memcontrol.h +++ b/include/linux/memcontrol.h @@ -1080,15 +1080,6 @@ static inline void count_memcg_events(struct mem_cgr= oup *memcg, local_irq_restore(flags); } =20 -static inline void count_memcg_page_event(struct page *page, - enum vm_event_item idx) -{ - struct mem_cgroup *memcg =3D page_memcg(page); - - if (memcg) - count_memcg_events(memcg, idx, 1); -} - static inline void count_memcg_folio_events(struct folio *folio, enum vm_event_item idx, unsigned long nr) { @@ -1565,11 +1556,6 @@ static inline void __count_memcg_events(struct mem_c= group *memcg, { } =20 -static inline void count_memcg_page_event(struct page *page, - int idx) -{ -} - static inline void count_memcg_folio_events(struct folio *folio, enum vm_event_item idx, unsigned long nr) { diff --git a/mm/khugepaged.c b/mm/khugepaged.c index 9efd8ff68f06..d49aa22d99c9 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -888,16 +888,16 @@ static int hpage_collapse_find_target_node(struct col= lapse_control *cc) } #endif =20 -static bool hpage_collapse_alloc_page(struct page **hpage, gfp_t gfp, int = node, +static bool hpage_collapse_alloc_folio(struct folio **folio, gfp_t gfp, in= t node, nodemask_t *nmask) { - *hpage =3D __alloc_pages(gfp, HPAGE_PMD_ORDER, node, nmask); - if (unlikely(!*hpage)) { + *folio =3D __folio_alloc(gfp, HPAGE_PMD_ORDER, node, nmask); + + if (unlikely(!*folio)) { count_vm_event(THP_COLLAPSE_ALLOC_FAILED); return false; } =20 - folio_prep_large_rmappable((struct folio *)*hpage); count_vm_event(THP_COLLAPSE_ALLOC); return true; } @@ -1064,17 +1064,18 @@ static int alloc_charge_hpage(struct page **hpage, = struct mm_struct *mm, int node =3D hpage_collapse_find_target_node(cc); struct folio *folio; =20 - if (!hpage_collapse_alloc_page(hpage, gfp, node, &cc->alloc_nmask)) + if (!hpage_collapse_alloc_folio(&folio, gfp, node, &cc->alloc_nmask)) return SCAN_ALLOC_HUGE_PAGE_FAIL; =20 - folio =3D page_folio(*hpage); if (unlikely(mem_cgroup_charge(folio, mm, gfp))) { folio_put(folio); *hpage =3D NULL; return SCAN_CGROUP_CHARGE_FAIL; } - count_memcg_page_event(*hpage, THP_COLLAPSE_ALLOC); =20 + count_memcg_folio_events(folio, THP_COLLAPSE_ALLOC, 1); + + *hpage =3D folio_page(folio, 0); return SCAN_SUCCEED; } =20 --=20 2.40.1 From nobody Wed Dec 17 13:15:15 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id A9DBACDB47E for ; Wed, 18 Oct 2023 20:33:28 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232458AbjJRUd1 (ORCPT ); Wed, 18 Oct 2023 16:33:27 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53930 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231911AbjJRUdO (ORCPT ); Wed, 18 Oct 2023 16:33:14 -0400 Received: from mail-pl1-x62e.google.com (mail-pl1-x62e.google.com [IPv6:2607:f8b0:4864:20::62e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E6734115 for ; Wed, 18 Oct 2023 13:33:11 -0700 (PDT) Received: by mail-pl1-x62e.google.com with SMTP id d9443c01a7336-1ca74e77aecso606935ad.1 for ; Wed, 18 Oct 2023 13:33:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1697661191; x=1698265991; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=AcNlnHmZuXPr4rVPOwy7uUYyAYPeSLuYyJXFxeMfebs=; b=IOxHLuPUMNnOYZ0BYOAEsZwUbKBYATaTxCgnaTOKcUkgKI5TqyKoswj9MfHu9TzKlc mJcOHpLBnR0zXcjKExY26NhIZfqCm/CXr/rLYvfVdHIiOt4tXV3du3x8yntJUYUrYhrN QL/1nr2bNIyFScdthvE1RdO9XZR3g3UqK+TOvL+136/3rCmFLD8Yc9OMQC2tDaHvHhMW zFeGJyM42r0fFhyJcDE0P2aBh4iumaOyt4g8A/k+A8IQjkjtbdle7lbrl4aDeu/hDcJ5 oj7Q4n51T15ipLl6uqJMJNOBP+tGoaYAHTkRjhjT4uOtteaBq9UjQ870Af0X2hEXnPVa W1eg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697661191; x=1698265991; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=AcNlnHmZuXPr4rVPOwy7uUYyAYPeSLuYyJXFxeMfebs=; b=REFBmIADc9NEZhOs3xyOm9cKfMuLxWGYWu7nRf+TmJiC/TFFARCck58MK0IejIgrsP BHQLvOmLmKYx0g0184+yYoJYKIr65wL9rmcyvv2p1yNiWYDNhfOQUMkJAwIbD5/aHOAR hSq1wIOO77G26L3F4eYkuWvf6XWeSq2MtOWLppJUIj40BEOOcrVjRzIiwlv6jFZ4SYG9 cIDsfqBjwhpZy3Qex6/qSQVbsROlVKOv7Fh6aYYuFM/ui6UQYZ9jiGA1vGjGmeoKcYbz xXpLnjr8G0GZsxWd8TuLCVrrkPsFSC8qZro/7j2nv00XUwxNoe/2q3TLXspsVXKSjFeZ j1Wg== X-Gm-Message-State: AOJu0Yw6FklT5kTzuEQUojKuWTwP4L2c0TuvDxC0mwCDa5JhsgIbCvi3 Z0aMvqOKDwqqCgVDzRED+BM= X-Google-Smtp-Source: AGHT+IEf7Gnz9O9WMz3CQOd+A4MjLM6TTZK4jiZl9DPWEVFFAHxMsHTxdrbBpGMTCyZdudHzKKxBQw== X-Received: by 2002:a17:902:e5cb:b0:1c6:e1d:8be0 with SMTP id u11-20020a170902e5cb00b001c60e1d8be0mr677167plf.2.1697661191282; Wed, 18 Oct 2023 13:33:11 -0700 (PDT) Received: from fedora.. (c-73-170-51-167.hsd1.ca.comcast.net. [73.170.51.167]) by smtp.googlemail.com with ESMTPSA id w12-20020a170902d3cc00b001c61e628e9dsm354412plb.77.2023.10.18.13.33.10 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 18 Oct 2023 13:33:10 -0700 (PDT) From: "Vishal Moola (Oracle)" To: linux-mm@kvack.org Cc: linux-kernel@vger.kernel.org, akpm@linux-foundation.org, shy828301@gmail.com, "Vishal Moola (Oracle)" Subject: [PATCH v2 5/5] mm/khugepaged: Convert collapse_pte_mapped_thp() to use folios Date: Wed, 18 Oct 2023 13:32:13 -0700 Message-Id: <20231018203213.50224-6-vishal.moola@gmail.com> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231018203213.50224-1-vishal.moola@gmail.com> References: <20231018203213.50224-1-vishal.moola@gmail.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" This removes 2 calls to compound_head() and helps convert khugepaged to use folios throughout. Previously, if the address passed to collapse_pte_mapped_thp() corresponded to a tail page, the scan would fail immediately. Using filemap_lock_folio() we can get the corresponding folio back and try to operate on the folio instead. Signed-off-by: Vishal Moola (Oracle) --- mm/khugepaged.c | 45 ++++++++++++++++++++------------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/mm/khugepaged.c b/mm/khugepaged.c index d49aa22d99c9..94c1dd09a8a6 100644 --- a/mm/khugepaged.c +++ b/mm/khugepaged.c @@ -1475,7 +1475,7 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, uns= igned long addr, bool notified =3D false; unsigned long haddr =3D addr & HPAGE_PMD_MASK; struct vm_area_struct *vma =3D vma_lookup(mm, haddr); - struct page *hpage; + struct folio *folio; pte_t *start_pte, *pte; pmd_t *pmd, pgt_pmd; spinlock_t *pml =3D NULL, *ptl; @@ -1508,19 +1508,14 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, u= nsigned long addr, if (userfaultfd_wp(vma)) return SCAN_PTE_UFFD_WP; =20 - hpage =3D find_lock_page(vma->vm_file->f_mapping, + folio =3D filemap_lock_folio(vma->vm_file->f_mapping, linear_page_index(vma, haddr)); - if (!hpage) + if (!folio) return SCAN_PAGE_NULL; =20 - if (!PageHead(hpage)) { - result =3D SCAN_FAIL; - goto drop_hpage; - } - - if (compound_order(hpage) !=3D HPAGE_PMD_ORDER) { + if (folio_order(folio) !=3D HPAGE_PMD_ORDER) { result =3D SCAN_PAGE_COMPOUND; - goto drop_hpage; + goto drop_folio; } =20 result =3D find_pmd_or_thp_or_none(mm, haddr, &pmd); @@ -1534,13 +1529,13 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, u= nsigned long addr, */ goto maybe_install_pmd; default: - goto drop_hpage; + goto drop_folio; } =20 result =3D SCAN_FAIL; start_pte =3D pte_offset_map_lock(mm, pmd, haddr, &ptl); if (!start_pte) /* mmap_lock + page lock should prevent this */ - goto drop_hpage; + goto drop_folio; =20 /* step 1: check all mapped PTEs are to the right huge page */ for (i =3D 0, addr =3D haddr, pte =3D start_pte; @@ -1565,7 +1560,7 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, uns= igned long addr, * Note that uprobe, debugger, or MAP_PRIVATE may change the * page table, but the new page will not be a subpage of hpage. */ - if (hpage + i !=3D page) + if (folio_page(folio, i) !=3D page) goto abort; } =20 @@ -1580,7 +1575,7 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, uns= igned long addr, * page_table_lock) ptl nests inside pml. The less time we hold pml, * the better; but userfaultfd's mfill_atomic_pte() on a private VMA * inserts a valid as-if-COWed PTE without even looking up page cache. - * So page lock of hpage does not protect from it, so we must not drop + * So page lock of folio does not protect from it, so we must not drop * ptl before pgt_pmd is removed, so uffd private needs pml taken now. */ if (userfaultfd_armed(vma) && !(vma->vm_flags & VM_SHARED)) @@ -1604,7 +1599,7 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, uns= igned long addr, continue; /* * We dropped ptl after the first scan, to do the mmu_notifier: - * page lock stops more PTEs of the hpage being faulted in, but + * page lock stops more PTEs of the folio being faulted in, but * does not stop write faults COWing anon copies from existing * PTEs; and does not stop those being swapped out or migrated. */ @@ -1613,7 +1608,7 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, uns= igned long addr, goto abort; } page =3D vm_normal_page(vma, addr, ptent); - if (hpage + i !=3D page) + if (folio_page(folio, i) !=3D page) goto abort; =20 /* @@ -1632,8 +1627,8 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, uns= igned long addr, =20 /* step 3: set proper refcount and mm_counters. */ if (nr_ptes) { - page_ref_sub(hpage, nr_ptes); - add_mm_counter(mm, mm_counter_file(hpage), -nr_ptes); + folio_ref_sub(folio, nr_ptes); + add_mm_counter(mm, mm_counter_file(&folio->page), -nr_ptes); } =20 /* step 4: remove empty page table */ @@ -1657,14 +1652,14 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, u= nsigned long addr, maybe_install_pmd: /* step 5: install pmd entry */ result =3D install_pmd - ? set_huge_pmd(vma, haddr, pmd, hpage) + ? set_huge_pmd(vma, haddr, pmd, &folio->page) : SCAN_SUCCEED; - goto drop_hpage; + goto drop_folio; abort: if (nr_ptes) { flush_tlb_mm(mm); - page_ref_sub(hpage, nr_ptes); - add_mm_counter(mm, mm_counter_file(hpage), -nr_ptes); + folio_ref_sub(folio, nr_ptes); + add_mm_counter(mm, mm_counter_file(&folio->page), -nr_ptes); } if (start_pte) pte_unmap_unlock(start_pte, ptl); @@ -1672,9 +1667,9 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, uns= igned long addr, spin_unlock(pml); if (notified) mmu_notifier_invalidate_range_end(&range); -drop_hpage: - unlock_page(hpage); - put_page(hpage); +drop_folio: + folio_unlock(folio); + folio_put(folio); return result; } =20 --=20 2.40.1