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 4CB4DCA0EED for ; Fri, 22 Aug 2025 03:56:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:content-type: Content-Transfer-Encoding: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=fXj+9vT03G0X8fnCGUCy6hSTWwnZnWm2sdD6cJzGZDI=; b=TJbcyIJhctWEKs9sP26gw74x3S IiwgGi49uQVPtaN9lwpSS6H4RCiM59MGXkc5+xfg07k6YhJi8ICIkgT0gMLvXaWQu+KxfXkj9A47d MfVl++QjaGzGMOJ9a/oWadp2sKQtyi8zwAOdStaZ6tGDd8GnZHbw4rQFAQzyACLeVLtZm1YwmaZ5b vBubjr2WkKadD80/2LUYVg/ecW87dNwo7OEp9I6Xiyd/vWzclDx/sxLFcOVi2RQsEUCA+Xb4OoxjJ vLbbAyim9CZ+ivnzqoed0GweDG+i37jYsXCFS52XVcoIuFaZRNqU/dsl6UrsDjoj5JqIvdNqFIWeC KYE8n4Mw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1upItB-00000001SGk-3Fm7; Fri, 22 Aug 2025 03:56:29 +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 1upBZT-00000000NXv-2rk5 for linux-arm-kernel@lists.infradead.org; Thu, 21 Aug 2025 20:07:40 +0000 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-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-2-yEzZWsAGOqSKGLq0fJk5pg-1; Thu, 21 Aug 2025 16:07:37 -0400 X-MC-Unique: yEzZWsAGOqSKGLq0fJk5pg-1 X-Mimecast-MFC-AGG-ID: yEzZWsAGOqSKGLq0fJk5pg_1755806855 Received: by mail-wr1-f71.google.com with SMTP id ffacd0b85a97d-3b9dc5c2c7dso513041f8f.1 for ; Thu, 21 Aug 2025 13:07:37 -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=iYFpkrsizJZRQ2YSKras8scEFYDGq1lpTedYmh4ptBBTJHmnwm4YriGqA54KmuwIf/ /wFCwbX44dMkT8PeV0FrNy0QZaC277/JlVFwlpeGQLvX2vTMmjNU7E1jIRtOTdb0aml8 iSsBDelPKNNafSAr4YkeCbyh+8hzCd+vxTv5fgMDHSFcFCjTbgSREB7Uk3qtVgOAHtqt oGr7VPmRUkxclKcU1n61iIb1Px4nRR/ExrQRo2SIvSq4M5nkUZAMeyMD3OLJgIBMVxMR p+JKJahDrBkMsprmgJo4OCDnH+gtj/711rxLOM+BjcUY27muYLoPiXZcj0GMQ0e6IYRU IZlA== X-Forwarded-Encrypted: i=1; AJvYcCXiMvIvYSvBxahh/6H6QHoJLVVm7RdJuBrGBd4DF3/myy2TiKTqzJltYSGi2LMou14pIPmEhUHfs1NO279ihecj@lists.infradead.org X-Gm-Message-State: AOJu0YxaxRiaOeucNIh50fN9CCynO+B6j9P+Lh8m7hI0q0fdjaCSTJsU 9oiSdSMSy0GHYqX5ZzyjCKpDOqZ7vXBulJqxgW+FWX+z1FP67Gz03pv3jlNKHktcMFrfdN65+RC hOSZ2Dpv8IwMBnvFL031bva+uZzmtkJcAvVyeR3MkaiIgyHpxWTKoVqFmabTEnpaEqBzf4d/6Lf Iu X-Gm-Gg: ASbGncvgeUxI1IppJL6FR63CTNxzOCYflbkhIxkXfL+3LrwQQZ/RlttMrbu/8SNVZcH mZzmx3Jwy0RxX3vzN0+G4Ab9YGwG574LXeDiqJ0YOs2OHI9zQfPZx/DCtC+ErRqZpa0T3LtZXYY OtDQ/cHLfnfpv/77aTgVcfv/iW6q/IcnmyiDkF/9slcD2+2CEsHFTvp5b7y4361GDW3Cql+rijv IbiRUHPxxR+CvoUcuwtTiZvJalCUOCeLZBXIdPA3IflNMYMi7cx4A11y7veldlCuxh+2jYmiSON KM5OrHTVflpf7/DIowd1/7GhyhSCpbpfrRykepuhb0FomVxh70sMVvlb+wkypLBpquTHpz9aGyH H8dMDi0oqK9G6TtWTJ6W4fQ== X-Received: by 2002:a05:6000:2303:b0:3b8:d893:5230 with SMTP id ffacd0b85a97d-3c5ddd7f36emr169085f8f.47.1755806855292; 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: y-BPCnitlJrht7C8W578d_upLX3-WKJ6zKMnJh8RWk4_1755806855 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250821_130739_790669_095DC357 X-CRM114-Status: GOOD ( 17.63 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=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