From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (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 6EAE1270553 for ; Fri, 22 Aug 2025 04:09:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=170.10.133.124 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755835770; cv=none; b=d0HATS98+emtsNP0btxlOPiSJL7WPe5zllUQZe4TU78VgIpRO28T1FhIi4EpLYxR2dvmPRmd8jd7eZWd6IV6KBp7doxkaW21vzUANm8/g+/eRxcdduuG8posjL9wHrm5yZb0YEMi9ZKPPawbgzv1tomRXSc0q0eIZ1d42Z5knL8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1755835770; c=relaxed/simple; bh=uvxGoPwpEkTnej/P9frRazHxG8UL+BClJE53WOAvyXQ=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=IZw4fes57uvMwwNFLaeVTLM2ZJMQE6ewUNECjU+exdNQMMMyVlKtdMn/WN9zeiVZ+esS2ORAUsiM9y9YxpOZzochjCIc4ueiyiOBVozfyQuQ2+k+PHEP/GQn4OuNhhrdZCydyR99cWTXYEXKm2h6UppkPa320UGNJPbxWdJCDiQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com; spf=pass smtp.mailfrom=redhat.com; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b=ekVL8Jce; arc=none smtp.client-ip=170.10.133.124 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=redhat.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=redhat.com header.i=@redhat.com header.b="ekVL8Jce" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755835765; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wD6Kie9NmTYlBWSYITqg8VmPWCkzgpHh3Jn6J+e3AEM=; b=ekVL8Jcem6q4cF0HM2y6xOx21D/FJFLgh+Adi11GsoKh93+dTphRUEqvPEAbV1jsnGf0+j 76vWlhnV/nsaTW5nDgWVAmPnyWUl0TtjWGi7aVu71PRBgkpV7KBux/J7eN3g1ihAEe3TSt 6lo4Vg1aDMs7A8C7PTZdjARZhrto0Jk= Received: from mail-lj1-f199.google.com (mail-lj1-f199.google.com [209.85.208.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-464-xD7JVNW-Pv2eJeu6q5sR9Q-1; Fri, 22 Aug 2025 00:09:23 -0400 X-MC-Unique: xD7JVNW-Pv2eJeu6q5sR9Q-1 X-Mimecast-MFC-AGG-ID: xD7JVNW-Pv2eJeu6q5sR9Q_1755835762 Received: by mail-lj1-f199.google.com with SMTP id 38308e7fff4ca-333f92b401bso6907881fa.2 for ; Thu, 21 Aug 2025 21:09:23 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755835762; x=1756440562; h=content-transfer-encoding:in-reply-to:from:content-language :references:cc:to:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=wD6Kie9NmTYlBWSYITqg8VmPWCkzgpHh3Jn6J+e3AEM=; b=bSKOt8T76dWjJZ98399k37GS501qn5/OXGIO5tN8oj9vOVfijPz6gLtYqBrM6N0OB/ vf1186U659YXs9EjHXIe9y/knwL7raCiCqqnyB1/jmCAhP1xgJXiwva/odImDI+bzxPw NlMkmzvoIsfu7IU6zs+/ZJUTrHXcmHx06E9RKrfKMd5oc23Uqe7HOQmMG+mBfeWBJhiZ Y8pfWf9UTNJG6XVJuwpiX19kVNRdtFfpkppS30IdSk0nki4kJX0cnpB2OpEiRbGh49/j fp0ig5N5skK/LnM+eebo30dtTv08iuFMRkBu7xHZWOutO/BrXWlQ36lV7OR2Fp7eUdR7 22TQ== X-Forwarded-Encrypted: i=1; AJvYcCXUnGVxGobQH0kHKq1Q4yQFVp3GnfjEFTdF10iHCPorEaTtgkFZRrH2VFxhrttrNVpig0qwDaO3hRilU1U=@vger.kernel.org X-Gm-Message-State: AOJu0YxNwgcgIGej3F7lxSaELldAXk62JrT+vKgV9TAN0aKUkIjBCu8A XUiiE2rAWNOvbzmpLzbFyxY2Hymmik8FamQvQ1z9m9K5+U+poNI3H4sgt2hrATq1y32ZYfn0dZD 12mqwcjEE00siiVjCNpdSIM/92E5HO1K1WVoC0yo8fhg/JCD5r86LjepIlOyAgOdN X-Gm-Gg: ASbGncs1fhw5BEwSyIxdO2s8YVEmUXf4ROAm2E8LuTCghV0aeto38XlNCzfClKBFYNA zDaU7SkhFATMq09Du2u6Z8zEA20H7+PBYSELFVRLln0mNcUZzE6rp+6/v2p2oJE0KLy72wiafL9 GDGNRCnxUnHmCSloptVi3oUuZ3k0/ZdeBJ2Wfplqs74W4OioG0TSYMdlU7TCgmctMNkaDyBmx1u ubgLSoJFSplDUw2BbfWjUq8UwU4jGStnEaD8wmt8HdeK878mhU+8baoWpY4VctBd6zIKM1BrCNG 9XvjVfReuxn7dBE9r0VGb/zF5XWpgWp74xt5juep2ZtcuTCNPnP4mL5O/ani5ULiDw== X-Received: by 2002:a2e:be0c:0:b0:333:b6b0:e665 with SMTP id 38308e7fff4ca-33650fa8605mr4319391fa.30.1755835762110; Thu, 21 Aug 2025 21:09:22 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHIfwy5viYQG7CiACnNo0XJ1QLrkAQo6vQi0t/AKYxGuojOFc+bxzH8Oeop0sL+wr/cDEqByA== X-Received: by 2002:a2e:be0c:0:b0:333:b6b0:e665 with SMTP id 38308e7fff4ca-33650fa8605mr4319091fa.30.1755835761548; Thu, 21 Aug 2025 21:09:21 -0700 (PDT) Received: from [192.168.1.86] (85-23-48-6.bb.dnainternet.fi. [85.23.48.6]) by smtp.gmail.com with ESMTPSA id 38308e7fff4ca-3340a41e3cfsm35236551fa.6.2025.08.21.21.09.18 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 21 Aug 2025 21:09:19 -0700 (PDT) Message-ID: <9156d191-9ec4-4422-bae9-2e8ce66f9d5e@redhat.com> Date: Fri, 22 Aug 2025 07:09:17 +0300 Precedence: bulk X-Mailing-List: linux-crypto@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH RFC 10/35] mm/hugetlb: cleanup hugetlb_folio_init_tail_vmemmap() To: David Hildenbrand , linux-kernel@vger.kernel.org Cc: Alexander Potapenko , Andrew Morton , Brendan Jackman , Christoph Lameter , Dennis Zhou , Dmitry Vyukov , dri-devel@lists.freedesktop.org, intel-gfx@lists.freedesktop.org, iommu@lists.linux.dev, io-uring@vger.kernel.org, Jason Gunthorpe , Jens Axboe , Johannes Weiner , John Hubbard , kasan-dev@googlegroups.com, kvm@vger.kernel.org, "Liam R. Howlett" , Linus Torvalds , linux-arm-kernel@axis.com, linux-arm-kernel@lists.infradead.org, linux-crypto@vger.kernel.org, linux-ide@vger.kernel.org, linux-kselftest@vger.kernel.org, linux-mips@vger.kernel.org, linux-mmc@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, linux-s390@vger.kernel.org, linux-scsi@vger.kernel.org, Lorenzo Stoakes , Marco Elver , Marek Szyprowski , Michal Hocko , Mike Rapoport , Muchun Song , netdev@vger.kernel.org, Oscar Salvador , Peter Xu , Robin Murphy , Suren Baghdasaryan , Tejun Heo , virtualization@lists.linux.dev, Vlastimil Babka , wireguard@lists.zx2c4.com, x86@kernel.org, Zi Yan References: <20250821200701.1329277-1-david@redhat.com> <20250821200701.1329277-11-david@redhat.com> Content-Language: en-US From: =?UTF-8?Q?Mika_Penttil=C3=A4?= In-Reply-To: <20250821200701.1329277-11-david@redhat.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit On 8/21/25 23:06, David Hildenbrand wrote: > All pages were already initialized and set to PageReserved() with a > refcount of 1 by MM init code. Just to be sure, how is this working with MEMBLOCK_RSRV_NOINIT, where MM is supposed not to initialize struct pages? > In fact, by using __init_single_page(), we will be setting the refcount to > 1 just to freeze it again immediately afterwards. > > So drop the __init_single_page() and use __ClearPageReserved() instead. > Adjust the comments to highlight that we are dealing with an open-coded > prep_compound_page() variant. > > Further, as we can now safely iterate over all pages in a folio, let's > avoid the page-pfn dance and just iterate the pages directly. > > Note that the current code was likely problematic, but we never ran into > it: prep_compound_tail() would have been called with an offset that might > exceed a memory section, and prep_compound_tail() would have simply > added that offset to the page pointer -- which would not have done the > right thing on sparsemem without vmemmap. > > Signed-off-by: David Hildenbrand > --- > mm/hugetlb.c | 21 ++++++++++----------- > 1 file changed, 10 insertions(+), 11 deletions(-) > > diff --git a/mm/hugetlb.c b/mm/hugetlb.c > index d12a9d5146af4..ae82a845b14ad 100644 > --- a/mm/hugetlb.c > +++ b/mm/hugetlb.c > @@ -3235,17 +3235,14 @@ static void __init hugetlb_folio_init_tail_vmemmap(struct folio *folio, > unsigned long start_page_number, > unsigned long end_page_number) > { > - enum zone_type zone = zone_idx(folio_zone(folio)); > - int nid = folio_nid(folio); > - unsigned long head_pfn = folio_pfn(folio); > - unsigned long pfn, end_pfn = head_pfn + end_page_number; > + struct page *head_page = folio_page(folio, 0); > + struct page *page = folio_page(folio, start_page_number); > + unsigned long i; > int ret; > > - for (pfn = head_pfn + start_page_number; pfn < end_pfn; pfn++) { > - struct page *page = pfn_to_page(pfn); > - > - __init_single_page(page, pfn, zone, nid); > - prep_compound_tail((struct page *)folio, pfn - head_pfn); > + for (i = start_page_number; i < end_page_number; i++, page++) { > + __ClearPageReserved(page); > + prep_compound_tail(head_page, i); > ret = page_ref_freeze(page, 1); > VM_BUG_ON(!ret); > } > @@ -3257,12 +3254,14 @@ static void __init hugetlb_folio_init_vmemmap(struct folio *folio, > { > int ret; > > - /* Prepare folio head */ > + /* > + * This is an open-coded prep_compound_page() whereby we avoid > + * walking pages twice by preparing+freezing them in the same go. > + */ > __folio_clear_reserved(folio); > __folio_set_head(folio); > ret = folio_ref_freeze(folio, 1); > VM_BUG_ON(!ret); > - /* Initialize the necessary tail struct pages */ > hugetlb_folio_init_tail_vmemmap(folio, 1, nr_pages); > prep_compound_head((struct page *)folio, huge_page_order(h)); > } --Mika