From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from gabe.freedesktop.org (gabe.freedesktop.org [131.252.210.177]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 4129BCA0FE2 for ; Thu, 21 Aug 2025 20:07:42 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id CDC6410E9E5; Thu, 21 Aug 2025 20:07:41 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="DO2xSVZZ"; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by gabe.freedesktop.org (Postfix) with ESMTPS id 25E8F10E9DC for ; Thu, 21 Aug 2025 20:07:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755806860; 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=fXj+9vT03G0X8fnCGUCy6hSTWwnZnWm2sdD6cJzGZDI=; b=DO2xSVZZxvs1mJSL0Vq/GLTrvidEVqkh96BWtWUDWrw0WCX6XKg9HjlaqJ5Dd1GS6jX8Jb rjEPaSwL25LuK4MNocvsj09fb0J7yBlAdpvujxHyErouJETNd3OiCTsWTYMoniwsvc/44k fUoQZlXJFO6M5DLhMVna3gMozPeus7k= Received: from mail-wr1-f69.google.com (mail-wr1-f69.google.com [209.85.221.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-248-rJxveJ-YMsiWpkadD8Dz2g-1; Thu, 21 Aug 2025 16:07:36 -0400 X-MC-Unique: rJxveJ-YMsiWpkadD8Dz2g-1 X-Mimecast-MFC-AGG-ID: rJxveJ-YMsiWpkadD8Dz2g_1755806855 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-3b9e418a883so795575f8f.3 for ; Thu, 21 Aug 2025 13:07:36 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1755806855; x=1756411655; 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=fXj+9vT03G0X8fnCGUCy6hSTWwnZnWm2sdD6cJzGZDI=; b=g292q4XOTYb41CR50Khm3nMvPg4qG0sO87SCrNKIzPt7+mD5l47m8osYyOec2clbhl yi+vQuDVWggR5bOewkBAX38r4KJ4u+nEwbmzWhzBDeCVGSYHn9Qbj/ABbNm5m+IuCafD LLn5rrKLpl01X1BudFxfELbGvxnRmrz7uTjD4LfP77UZ36LilcrNk/0IwzPufPsc5OVg KBVE6xGnn39AYLfhcaX1f2s8bJFGbaVl/q/LQg0FICkJ4phX2vYq75Huo1FYhX4dxSBp bcSGJTU6AuQ1d835RvFdXNSrtD72iXySMfbe0YOWMKI9Qw5+5UBR2mYjJjK7DheX8UtK SzdQ== X-Forwarded-Encrypted: i=1; AJvYcCVAHQLt3s7i5NbVmIfVzqhSqTxaD1s2sQuoIfujTdpAKlXLrXy6PVfMQr/gGThVl9PiMi3RWaADFJg=@lists.freedesktop.org X-Gm-Message-State: AOJu0Yy2xZfkPfOM4nAaqkXTlhz4orFJDPVnZHR+yJbboKfuUsQVM01N 31CqFcQToWoaXrQv+R2hxCpEX4THNplpZbBmtTHj1/ZftPfr1Vxceg1PplLklaCxztzWiAX3XD1 c921s3GmJOThp419CEOR2uWmyZXH31Qt/rLVjMK69SkXvBjVNM1L3aiYe6xCFEUw7H6+hDw== X-Gm-Gg: ASbGnctzkG7AVyCW7bmiGq15a4LKRpq7gXtctrL9JuhZd0DkMkF1mqZnmuRAoMQLZkq 7IMJM0mGtVMTaiWqS9LxizPyMWPcrOmEihwkMaPaKOLqH0bFCuXnkBtfbEL8xqB0flfX4xVJ9Oy FrbNZA5lXoS+DBHy9KPbVxLwoJPY8TmNutE338ScVZkBamSstetvU8MmgQoEakIUKWaj7uSA6iw 7yCbC50Zeg2c7mlluBCZE3FDurG0VmRsFn4QkgQlPJeV3YZqEsvZ2iKh8dfbvwwaZyas/FGzHvU 5VD8SY2oWMl0KqpKOyojR6mCFCo6gBls4ZAKR11+yrEuVyqgnlnMuFRsHp2y2fFa4Q6ROc/gA8T E+7L/QqjPh10rOKwPaxRJEQ== X-Received: by 2002:a05:6000:2303:b0:3b8:d893:5230 with SMTP id ffacd0b85a97d-3c5ddd7f36emr169055f8f.47.1755806855244; Thu, 21 Aug 2025 13:07:35 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGbzCbSL6MUAhNqtGz96KSex+k6XoYEx77XRmSFhqyAAErvnD4a434thNrJcfmyX2YJjFDsxQ== X-Received: by 2002:a05:6000:2303:b0:3b8:d893:5230 with SMTP id ffacd0b85a97d-3c5ddd7f36emr169035f8f.47.1755806854709; Thu, 21 Aug 2025 13:07:34 -0700 (PDT) Received: from localhost (p200300d82f26ba0008036ec5991806fd.dip0.t-ipconnect.de. [2003:d8:2f26:ba00:803:6ec5:9918:6fd]) by smtp.gmail.com with UTF8SMTPSA id ffacd0b85a97d-3c077788b39sm12802789f8f.47.2025.08.21.13.07.32 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Thu, 21 Aug 2025 13:07:34 -0700 (PDT) From: David Hildenbrand To: linux-kernel@vger.kernel.org Cc: David Hildenbrand , 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 Subject: [PATCH RFC 10/35] mm/hugetlb: cleanup hugetlb_folio_init_tail_vmemmap() Date: Thu, 21 Aug 2025 22:06:36 +0200 Message-ID: <20250821200701.1329277-11-david@redhat.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20250821200701.1329277-1-david@redhat.com> References: <20250821200701.1329277-1-david@redhat.com> MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: 7rrtEFgERIL05u47SsAe46RzpoEZuBb-QWxPveFGsu0_1755806855 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true X-BeenThere: intel-gfx@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Intel graphics driver community testing & development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: intel-gfx-bounces@lists.freedesktop.org Sender: "Intel-gfx" All pages were already initialized and set to PageReserved() with a refcount of 1 by MM init code. 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)); } -- 2.50.1