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 77CF4CA0EED for ; Fri, 22 Aug 2025 12:49: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:List-Subscribe:List-Help :List-Post:List-Archive:List-Unsubscribe:List-Id:Content-Transfer-Encoding: Content-Type:In-Reply-To:From:References:Cc:To:Subject:MIME-Version:Date: Message-ID:Reply-To:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=wD6Kie9NmTYlBWSYITqg8VmPWCkzgpHh3Jn6J+e3AEM=; b=JsoKFtZSHkQW0uqruOiQlXmLw8 bGTl2zGJwZJaU8v8Is2C1R3SWUvcETJ6DESL/vtnIGgxZiLVkncMi6n/HjBzq0jAuqY+d4CJwToTk khiQFY4w0lCfyMM+bG3yzFrHaOejSHvKbWZm9Q/WuF37wqhNcMizZdY3L4wirn6eAXtYag1k95F0u LfDw7JvidYPJeNu4rF4SUFyEh1etyODtqr2I96L6XVLQ+ec12k2gh7slwHaQ+GApqS+DsPduVRul2 lLSb9s/eOROvXPqlUYmOoC2/XuIK72B23bhn7V7TzMD4yZXCCTdlrAIO1YFpjirZ6onQYf02kuGSB krxrlmrQ==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.2 #2 (Red Hat Linux)) id 1upRCk-00000002XKd-1P8v; Fri, 22 Aug 2025 12:49:14 +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 1upJ5k-00000001TZo-1Pil for linux-arm-kernel@lists.infradead.org; Fri, 22 Aug 2025 04:09:30 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1755835766; 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=HQQnlVLIyAFLwTFey09rKZ+g0o7S2xxEBh8sfVMpq9eBV9wjXNRV96j1hejxVSFCADuIQ4 eslU3Xz3lrQNcU7B4qVmVahzWOSK7Nh9BJhdEL76JLYDaNTIiyps/A9GTWwkQSgnIN9xOZ GBRDnC32BkNuDrEZhFHSCJ9XxLFjLbI= 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-310-IeU-X9ihOYCENQxxL6Q1-w-1; Fri, 22 Aug 2025 00:09:24 -0400 X-MC-Unique: IeU-X9ihOYCENQxxL6Q1-w-1 X-Mimecast-MFC-AGG-ID: IeU-X9ihOYCENQxxL6Q1-w_1755835763 Received: by mail-lj1-f199.google.com with SMTP id 38308e7fff4ca-333f92b401bso6907931fa.2 for ; Thu, 21 Aug 2025 21:09:24 -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=Rna8ij4hT3ZximClHBVL9edknJpVO3bVWA36dxIcwRyLge+PL9yccdXfNv4+hLCOUf WHvxjeQdcyfTHxLE22KP1yOgib3vbEkdCONVtpRBIMqLB0BZ+kfBOyk1+x64JRmy/eGd u/RkWAJleTLERK/oFfwB33FSvZ9aqfDcNhX8imdWx4ClIDoLdPnBg2wYnJbHrTXu+9te yxDQiwD2hzrlyVmDalSkDflZnaWXbdYo1Uev/jnOEbBGEF695avIUprdMCV8LiKVTpvI x+YCjARFDMgNUNU1IXeFSygn2FGHAve01nUP798HgFpHszXCPhjnRQYPT1jwybL8UJSV YZRw== X-Forwarded-Encrypted: i=1; AJvYcCU8xdQtcgwklHY6OeYeDjS3XKxyRWZ+z2Bi07M53iFXqRkMIr6SDQ7nKrDCBVPjn9ZEQGREEfykJwRrBWgdZRok@lists.infradead.org X-Gm-Message-State: AOJu0Ywgjc722RFoYi4/OQD9S7RTOczCKVJiPTfkNbFvtZv3P34kDmbb ZDbm0bHz3VKbRXm7gC7+XS/75VpgsR2JK4DQjzXrL7DQ7RSiG847ZDQO5BQpUz9cx6wj0x52ldo J2ja/2mbZtJIqi3R7bLwPNObTCzT0dZmV20GfrbYXKbnq6EkSYvoZ9tUHmmAimiUWwtPJItRDYb xCYgfKpajcrg== X-Gm-Gg: ASbGncvFgwFD0f6Z1iAeDmXSJsgBPbU7WasL1GxgykwWMXWSUca52VLNuQmMa7cYDZp SB9/DkLRFmigSr1PqmL6Qd1F/bzn5SSTF6ve5YYv3bRULU4vXSiAVv3FkBhtqttjPbHSLzGz7NC roJI/x0PO+uahd38pdGd/vc2zEP/ljnQf71TB5YC3VUUOK0Cac+hfXO/rqWbNQxgcBfccjEClJ5 hK69CJ238eGrnCcJqNDnB6PkWkGkrHak7+j6GIBKZBcic59X4DV4//mNqOKKl/4UKJKnl3lwStu FGWeobVjO2+GclWvgxTRbn/6XN3UFX6FWARt8ndMMY00n2nSYuqB060mroQRDV4XfQ== X-Received: by 2002:a2e:be0c:0:b0:333:b6b0:e665 with SMTP id 38308e7fff4ca-33650fa8605mr4319301fa.30.1755835762100; 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 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> From: =?UTF-8?Q?Mika_Penttil=C3=A4?= In-Reply-To: <20250821200701.1329277-11-david@redhat.com> X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: n2yi4xpDsQOar6H6NasS7PhPTlPnRwStAUi2z881Dvc_1755835763 X-Mimecast-Originator: redhat.com Content-Language: en-US Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20250821_210928_453417_0B89FD9B X-CRM114-Status: GOOD ( 26.12 ) 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 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