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 EAFB3CA0EFF for ; Thu, 21 Aug 2025 20:07:43 +0000 (UTC) Received: from gabe.freedesktop.org (localhost [127.0.0.1]) by gabe.freedesktop.org (Postfix) with ESMTP id 552A310E9E3; Thu, 21 Aug 2025 20:07:43 +0000 (UTC) Authentication-Results: gabe.freedesktop.org; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.b="auiIn/wv"; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by gabe.freedesktop.org (Postfix) with ESMTPS id 2A98A10E9E3 for ; Thu, 21 Aug 2025 20:07:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755806858; 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=auiIn/wv3GwBRvTmJLMM16sMcV1LPDv+Dhs5gp9eUpKuumB8PMdrGMPQ909HmViHBcHLyK lc27aoo9olPCrBQmOCGVnvcv4jBKAGJb/81IG4iJR3h6xL4YpY+cH+TXh+re8rNeGNRofq Ylp/SYUiU3/aUtZkt4j1U8ktHG/hi1Q= 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-367-boX0ksxOMbKk9tbVQskHQQ-1; Thu, 21 Aug 2025 16:07:36 -0400 X-MC-Unique: boX0ksxOMbKk9tbVQskHQQ-1 X-Mimecast-MFC-AGG-ID: boX0ksxOMbKk9tbVQskHQQ_1755806855 Received: by mail-wr1-f69.google.com with SMTP id ffacd0b85a97d-3b9e418a883so795571f8f.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=FfFBDVa+WK4Z5sAy9QhtlNvO2uPNEyQLaRmBHyqtav859l9iPxq45UVNnGu0de5cad AnKqb1v9323wHavsoeGcdpyqQYNXaq5i95gDcOLyBUHtEnkKsv4KzkTWwKprC6JpcRbr s3JXE7MJxlYnWrQ8QVXz1t3utOtq2QiUxoehZpk/o7skCiQokWRJkzNI1gi0Ydo1Ptfn pvJC4RLa38Ra7OdVuWieJKzacUZxh/DbCfgXoBvhRniK6g+e47kuyixo6szz38Db2+5U nbOXUw8QYsHVM59nxPHV63Rb/ISCuqP3wUOrIXtlFwlmVFFyvyexGTfaj3bRVvkABCtj HRKQ== X-Forwarded-Encrypted: i=1; AJvYcCUMsiiVfUxwF26JA7K3PR5wUv1Yni9jbS2rIMDVXnRsLsRpbcbvvKX9vb9P4xKj1iO4d7ZaCFaY48M=@lists.freedesktop.org X-Gm-Message-State: AOJu0YwT/CHOvgSJLerdvfy8o2hSEAe67PLzOGbRP9qXBji2saRUTIbb SJKaELlzxWDq8HvLt+xIrrmw107NLRCcn5+d5HK8R70qVRP/NacKmjLz7JbB1nDsIhIntex9MMm 3j4yn+4+aPWKpM5B4oU9BoSH824uB0AZA5jgTr9BIqTwg1ZdfHvyoiA3TL0FQ34nr/JID4Q== X-Gm-Gg: ASbGncvT8y7ebleUt5RymCY1YbXH/HqgW5bdzYA76cNkG5z5TpzAKo8O+Jjp4NkyeES vXn/7RHt6pcx6WFkZjdS3NWytGpAcfuuwNBH6vHlyzkeaHdJsX4NJIA6vai64+p3iv+7H5GoHAp ZrT7ORLFLNaWx2YL/GygJcidMKJqDzynaJf7ub7QJ970MJS57Jq0AnvPr9HYYK5m/NNUct1dlkm jZwTeeN/XAZRkjXITv/4a22buw+zboo4IQKQlVLbfXYToyeNxWMwgJQ7Sdexgf/I7b6yx57g9ly +x6f744uUErQlrqgs2ityFSluTjXBtvH7+R18EGzYADL75zBdAcJs0wIx/zhdoyOLQgpjRoMnvr QQcvFOT5XQ8muPbJA1qsAag== X-Received: by 2002:a05:6000:2303:b0:3b8:d893:5230 with SMTP id ffacd0b85a97d-3c5ddd7f36emr169066f8f.47.1755806855264; 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: XcrGfv7rE25OrTrBk0ORj-1ELgGrnfzohU6V3Ii9FZw_1755806855 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true X-BeenThere: dri-devel@lists.freedesktop.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Direct Rendering Infrastructure - Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dri-devel-bounces@lists.freedesktop.org Sender: "dri-devel" 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