Received: by 2002:a05:7412:251c:b0:e2:908c:2ebd with SMTP id w28csp2048430rda; Tue, 24 Oct 2023 10:40:44 -0700 (PDT) X-Google-Smtp-Source: AGHT+IF/ULUb8wrNSdFscL50TIVtoNP2XLhzUyiD+0C3m7j5RCXbJth5HXdJ0YxycZyHxtP82H0O X-Received: by 2002:a05:6a21:4985:b0:16b:977d:f7cf with SMTP id ax5-20020a056a21498500b0016b977df7cfmr3794242pzc.36.1698169244330; Tue, 24 Oct 2023 10:40:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1698169244; cv=none; d=google.com; s=arc-20160816; b=xuE+N5pXmUCXY7BWa0xrSOmZbUFiuP/SEC0M4VRjSFKEW1nAi/RukaGbeAqwTqzEO2 a9+RJvfadVA0gRcGexnKxXEqtElbaw76+if6sO53GJd48oMJ/EQaZfKaG/iQGxkzDzWL ppW6lWJSlWurvcovVKJguuZ3DpLSWgN9Vfuzv1RJ/FGf/KjP6YZvNrHrJG8QJQb2HhTt EV9gg2IOBo6q0Hp4YUbtkOojlHeblxsKfL9Tsrtlumm67xvDI5KHFHamL6Zv7v1JtEwh vKJY+kEO1kR2ZdpLbgXb4YNQa4D9kHASs+qxN//RSf3chg6xtNFzwfVE0GxEO9MzCa+r OF8A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=0xWGwGBYtGQcESxLDi2O/elWlIJtx15pyBUk6hkFzL4=; fh=LOC492lplOqC4GkaMZEta4CHkilCnlhS9LUDSBPxgEk=; b=Pq8DASYTrgV0PiF8Tt1KR8rCFoMp0tgQ2VdvsKEuL2hPnk68ubGy0P8GEiAL5Jnxj/ 3A8I0Rpk5W/0cUwS1GiMBppTKbZ30BJBcUScyh/Wl2Scdvavf/Z0KgY9krxzAAHLGguD 540B2LN//qCSmzTzgXxYmJraZUsmQTm+nJmvu3E1Abfar0EO2mezPSqxkTVPoffA5kMU 26a+wyiloZzogwEBRikEn/4wz6ujPbThU3tN+x33dE5cuPq5w/dP/wcPnGyjIdPtBXFA fBBZZsu81jYesEV3+zhT+XvobJv6+wFWI+sAPByd4bpdg+4HOkYguAVDCe0nY2pzLFOy T7mQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=BiElUKCd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from lipwig.vger.email (lipwig.vger.email. [23.128.96.33]) by mx.google.com with ESMTPS id k73-20020a633d4c000000b005b8f7bbeb92si623664pga.153.2023.10.24.10.40.43 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 10:40:44 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) client-ip=23.128.96.33; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20230601 header.b=BiElUKCd; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.33 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by lipwig.vger.email (Postfix) with ESMTP id EC0218084614; Tue, 24 Oct 2023 10:40:41 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at lipwig.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344012AbjJXRkf (ORCPT + 99 others); Tue, 24 Oct 2023 13:40:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:38774 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343855AbjJXRkb (ORCPT ); Tue, 24 Oct 2023 13:40:31 -0400 Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E1C0410C6 for ; Tue, 24 Oct 2023 10:40:28 -0700 (PDT) Received: by mail-pj1-x1034.google.com with SMTP id 98e67ed59e1d1-27d3ede72f6so4146197a91.1 for ; Tue, 24 Oct 2023 10:40:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698169228; x=1698774028; darn=vger.kernel.org; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=0xWGwGBYtGQcESxLDi2O/elWlIJtx15pyBUk6hkFzL4=; b=BiElUKCdBmrM8nnWCzDSgZcRCi/swkiw43C4ZIGPFYmwM2SASkIoVtTbtWaMz9Ukbe bscxYi6ZQ+i1EKFsQDGczk5tJ7cMYz6Uqx+J83Q42ZFYZATAsbLwV1fuMSuto+mBakF2 Np5cgKfr5K5AZoGrJHhXC1Wtm4HU2hdT6dC7YCW7qpm9gJVFWornt5tetmFHfxS4+PiG NYTm6z0klfse4aSk8iq4ijnLwMI8oUEAzNGQeQCyVtHvNi+7cC0cTixsn696VqRZZ/Ys oMAVBELz3gbUlI41+xFkxZLjNepiyo/WFfthb1jEgjm9hGb+xkUpv9kykkES/I0bneJy T/YA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698169228; x=1698774028; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=0xWGwGBYtGQcESxLDi2O/elWlIJtx15pyBUk6hkFzL4=; b=T6Y9xIrSbIxka08qhd3BPYdXkCE/Kx0RPRqgpskx1XRPsFoJ5vj6gu9X8ahsxMYmWf cqzuCnP5qzN+3yD0lZmYeTYv0xFTHWTi7yzprlJL+EMBd52vgSVgT+TbvtEKg2zfZoFD wrK5Wvp6s6GTTcUC5I/E+Ljj7cX4tgdnFiiZM1IFcuZDk+up3LSH6pdo77+LPmxTvLkN Jbntx0Jkzp3Dai980xk6A2JEcivrmjCVXPqGmsZ0VOqKRfBK04O+HI/rijl9EXg4sBo8 KscnnAJSINhkXuJyXf8bNkDncBwDUHDoIDTdDNJlE5q9Yxxu8R0iJOX/fiHv+dv53atx Jm/Q== X-Gm-Message-State: AOJu0YyXdGq8ZgqGTfq7ogkjvJV+BryTylnGRByx9ovsWsckVfwq7nHf wh87FZT5yndF2PiyFYxgB/aS3lWaws6pzfK0sg4= X-Received: by 2002:a17:90a:ac02:b0:27d:b3d:5c33 with SMTP id o2-20020a17090aac0200b0027d0b3d5c33mr12739037pjq.28.1698169228336; Tue, 24 Oct 2023 10:40:28 -0700 (PDT) MIME-Version: 1.0 References: <20231020183331.10770-1-vishal.moola@gmail.com> <20231020183331.10770-6-vishal.moola@gmail.com> In-Reply-To: <20231020183331.10770-6-vishal.moola@gmail.com> From: Yang Shi Date: Tue, 24 Oct 2023 10:40:16 -0700 Message-ID: Subject: Re: [PATCH v3 5/5] mm/khugepaged: Convert collapse_pte_mapped_thp() to use folios To: "Vishal Moola (Oracle)" Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, akpm@linux-foundation.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lipwig.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (lipwig.vger.email [0.0.0.0]); Tue, 24 Oct 2023 10:40:42 -0700 (PDT) On Fri, Oct 20, 2023 at 11:34=E2=80=AFAM Vishal Moola (Oracle) wrote: > > 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 get the corresponding folio back and try to > operate on the folio instead. > > Signed-off-by: Vishal Moola (Oracle) Reviewed-by: Yang Shi > --- > mm/khugepaged.c | 45 ++++++++++++++++++++------------------------- > 1 file changed, 20 insertions(+), 25 deletions(-) > > diff --git a/mm/khugepaged.c b/mm/khugepaged.c > index 6a7184cd291b..bc2d8ff269c7 100644 > --- a/mm/khugepaged.c > +++ b/mm/khugepaged.c > @@ -1477,7 +1477,7 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, u= nsigned 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; > @@ -1510,19 +1510,14 @@ int collapse_pte_mapped_thp(struct mm_struct *mm,= unsigned long addr, > if (userfaultfd_wp(vma)) > return SCAN_PTE_UFFD_WP; > > - 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 (IS_ERR(folio)) > return SCAN_PAGE_NULL; > > - 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; > } > > result =3D find_pmd_or_thp_or_none(mm, haddr, &pmd); > @@ -1536,13 +1531,13 @@ int collapse_pte_mapped_thp(struct mm_struct *mm,= unsigned long addr, > */ > goto maybe_install_pmd; > default: > - goto drop_hpage; > + goto drop_folio; > } > > result =3D SCAN_FAIL; > start_pte =3D pte_offset_map_lock(mm, pmd, haddr, &ptl); > if (!start_pte) /* mmap_lock + page lock should prevent t= his */ > - goto drop_hpage; > + goto drop_folio; > > /* step 1: check all mapped PTEs are to the right huge page */ > for (i =3D 0, addr =3D haddr, pte =3D start_pte; > @@ -1567,7 +1562,7 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, u= nsigned 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; > } > > @@ -1582,7 +1577,7 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, u= nsigned long addr, > * page_table_lock) ptl nests inside pml. The less time we hold p= ml, > * the better; but userfaultfd's mfill_atomic_pte() on a private = VMA > * inserts a valid as-if-COWed PTE without even looking up page c= ache. > - * 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)) > @@ -1606,7 +1601,7 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, u= nsigned long addr, > continue; > /* > * We dropped ptl after the first scan, to do the mmu_not= ifier: > - * page lock stops more PTEs of the hpage being faulted i= n, but > + * page lock stops more PTEs of the folio being faulted i= n, but > * does not stop write faults COWing anon copies from exi= sting > * PTEs; and does not stop those being swapped out or mig= rated. > */ > @@ -1615,7 +1610,7 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, u= nsigned 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; > > /* > @@ -1634,8 +1629,8 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, u= nsigned long addr, > > /* 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_pte= s); > } > > /* step 4: remove empty page table */ > @@ -1659,14 +1654,14 @@ int collapse_pte_mapped_thp(struct mm_struct *mm,= unsigned 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_pte= s); > } > if (start_pte) > pte_unmap_unlock(start_pte, ptl); > @@ -1674,9 +1669,9 @@ int collapse_pte_mapped_thp(struct mm_struct *mm, u= nsigned 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; > } > > -- > 2.40.1 >