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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (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 AFAEBCA0EFA for ; Fri, 22 Aug 2025 00:53:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=7rHzYSP3ALTbZBXAtL8no/qQ4/gD+Tlyx/+hCImktkc=; b=gaaA1laexJvMVJ 7sMou9XsZXZS0K9AnQMDCM1XCilJwOTRO7RPFpYSljJziQi7SXT3Y2tPJURruo36zkty6hzJzskyj sC3E7/9xdPNIwsrTqmb8urfJXCZssNM22mnRjNUBCCK7fAo43xRVNGk6bYeI+bAr3gcXJ5/d/Q1A0 pHLqCvW5KT2jJZZ6kQzwXZXwNGjgUmWZtc56nFikSI1w0E2RJ1wg4QoM3YgT2DDoTKYR0pPKPGgEF hBGYZn0YjE0gxn21qRZ7S5kb7GIMbqTeik2pUgiNc5ZCT0NzeBnrGsiuJQUb8HXkKq4jl64l+9nVQ SH41qh5R5MhsS9eUKr+g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1upG1p-0000000162b-1ezO; Fri, 22 Aug 2025 00:53:13 +0000 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by bombadil.infradead.org with esmtps (Exim 4.98.2 #2 (Red Hat Linux)) id 1upBao-00000000OJL-1fwe for linux-riscv@lists.infradead.org; Thu, 21 Aug 2025 20:09:03 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755806941; 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=IJwcQIpolBusN3dQPxutUJwTpPvysCeu4qQYqly6aiiWAsrL4JUGkBok5Kg3JtoVXALMOC XFTLJaqmwvsSaw7+D0fugFgjwPWu4irumTLVNU5xtTsiOQ/08cUjrOrtisP/WiomHytn8x Dr2nffKBfTNRkIzLH1e+fnBQE6lO0/I= Received: from mail-wr1-f71.google.com (mail-wr1-f71.google.com [209.85.221.71]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-214-NKcRrwB_N_uQF2gJVTTADg-1; Thu, 21 Aug 2025 16:07:36 -0400 X-MC-Unique: NKcRrwB_N_uQF2gJVTTADg-1 X-Mimecast-MFC-AGG-ID: NKcRrwB_N_uQF2gJVTTADg_1755806855 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-3b9d41b779aso818598f8f.0 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=gjaFTxY4rrjTozGy27HafLGNJN1fs4Mhg7aSnSGwWCDJUcRERVL1KpeYfPW1f7Wt39 h8C4kF0BrpyoOMSWlan11rJUtXkX04BZS3YMCRJansBtNEjjpr6BtsB5FV9v6o1R8SRm ZDu0cluon0y8K1Hhc1z7Y/ZSwh7rxGW2+b/bYmoCAYldcK172aZPZPvp1uc9/HCxJU5A wjO7ba9YB6q4TC9vJg6bDgU9DLvzIfgBtf412ORTz4lIL3HIAtEsEZQJJNkaNdUJ4v1b 8XXm54vaK8xOPY43vDx9r5a7gM+iNb1cs+QqKXGErc+mPcw1HcpyvoT5ulNbRqiS/XQ+ p1NA== X-Forwarded-Encrypted: i=1; AJvYcCX8dAFygrlOHcrcgGcv5mgyVBBpmELGEZpBvrQF8xylkZIx4gCzTNZDgJXBYGFnxM9dn35jR3loTaHMpA==@lists.infradead.org X-Gm-Message-State: AOJu0Yx5+3IiVpwfdSAdDLROtQ4Mx9bhoa+HbbTWnmYTT2zs0SqqHjK1 PnGJ9QaHmxEepkbyMN8eMGj8n8Jg6Rz9uLVF8nM2D/nMRfCfr4AO3P899Uw6cNBxSXoG7xARCZH P0F998rzwOozuyU2FQmvw8ZZw3VBhMLX8+q3GVsUhJMdb59emGtbn66SvV2AyMXQNCmxujA== X-Gm-Gg: ASbGncsmkOxHAGNIZ4BBqx6rRIw/Z+frpYEk6p0MYE2aqPzHM+b8UbDXkqadymyfpet RMdfuyWP9ZOtqyTfg5rM4mVgrCU42xLUQq2xZCOQvPTssQJl4wwjBsAuDCXdpWIm5AYTvbokbBu bY2TqMetYCcOodsZuTbvIq6KzTyZOSGWOY4Sp049p5BEgaPEwZfJX3URkmaSwdRuV2+WuGWMcFo GZj9OI0o1dAyrKFarERWIJKfXeWHiV7ITaN/HshEHYc5pN96mkrnjEPwT5iHr9LLrbEWMg5NAf1 4djyl/23hoBPg/Fv80gQFnmif9/EasA9aXYeYjD9Clr903CRtoua2M2JjaYJjNEzYlO1f/YBM1j wqkHODFbnlD2MtZalNK54BQ== X-Received: by 2002:a05:6000:2303:b0:3b8:d893:5230 with SMTP id ffacd0b85a97d-3c5ddd7f36emr169058f8f.47.1755806855254; 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: H7euhi4gpHL2m_08ETbPX2ewPBYIJOiGSS2Y9jhLmd0_1755806855 X-Mimecast-Originator: redhat.com X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250821_130902_503783_292705A0 X-CRM114-Status: GOOD ( 16.24 ) X-BeenThere: linux-riscv@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-riscv" Errors-To: linux-riscv-bounces+linux-riscv=archiver.kernel.org@lists.infradead.org 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 _______________________________________________ linux-riscv mailing list linux-riscv@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-riscv