From nobody Fri Dec 19 02:49:21 2025 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6735F26FA5D for ; Wed, 23 Apr 2025 08:18:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745396324; cv=none; b=dEH3A93cJGs0ICANrBY77E9OBoYR2UzRQPfL77w6qPxiNQcVodpXmNoqO/Cua/sZYlsceHBPVCUPQQSL3x7/0S7RJ+7K9JpHMbm71/gJNDQd2XM0wI2TuO0cc8BF6Nl9/kcBZt0zVEUfPK68jTBiufjunpvNgJdJgwf87Th8ZSc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745396324; c=relaxed/simple; bh=toeSDI6RZRrxqez6wabYiEd25migVfEcBnITfLHIlj0=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=F2m7DIhBYwR08pJUqkBsEeljGFQjPef0U+teB5ilA3u3f+rvv+wE4S/7q8HCRZ8IJT6m2ju3IvpD/HyZkg17fvxmqKNG4bkI5zLylgIHyYQKCgcIPMj/gXnVuJdPmJqwRiFbSpd45fBicMw4/0phdbxrskPGPtvaqY1M1OSbwO0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=casper.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=WEUdl/rV; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=casper.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="WEUdl/rV" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc: To:From:Reply-To:Content-ID:Content-Description; bh=NJbaNH4W3yPbLyC+ZTcgmLXA2i0jKLuqPtxQoIRJ/KA=; b=WEUdl/rVsKvvnnQFVRM5PtO990 ivSqRMqjmtKIjCWM5FwEH96/6j1Hh1WDWxgrhpqO88J2lqObGFBA+tDf3aQow6apiBiWlFMHp0DWu Dn6TgfjNuA3mltFv9DbPw/HUszTjJAcY2ab2IiYxXpM9edV/ELdlKyFGJwOvuXjVr6+2f/cdzWrJv 6Nvv6WTVHgoJeOPPO9eiSDnWXLjTKPhaUJ4Rud9T5q3BjgR1qABGGc/wh0p60zDs/eN1+Oz1vFktq 6EGwmw0W563E/MMI9W75F+JVUmWbakPH9s9eVFIRdmdc+DhXUFfoFk2ysaklJ+ZLt03Vd2zVW/hND mmKiLwUQ==; Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by casper.infradead.org with esmtpsa (Exim 4.98.2 #2 (Red Hat Linux)) id 1u7VJQ-000000081P4-0FRV; Wed, 23 Apr 2025 08:18:32 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98.1 #2 (Red Hat Linux)) id 1u7VJO-00000002YNn-0kqz; Wed, 23 Apr 2025 09:18:30 +0100 From: David Woodhouse To: Mike Rapoport Cc: Andrew Morton , "Sauerwein, David" , Anshuman Khandual , Ard Biesheuvel , Catalin Marinas , David Hildenbrand , Marc Zyngier , Mark Rutland , Mike Rapoport , Will Deacon , kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Ruihan Li Subject: [PATCH v3 1/7] mm: Introduce for_each_valid_pfn() and use it from reserve_bootmem_region() Date: Wed, 23 Apr 2025 08:52:43 +0100 Message-ID: <20250423081828.608422-2-dwmw2@infradead.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250423081828.608422-1-dwmw2@infradead.org> References: <20250423081828.608422-1-dwmw2@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html From: David Woodhouse Especially since commit 9092d4f7a1f8 ("memblock: update initialization of reserved pages"), the reserve_bootmem_region() function can spend a significant amount of time iterating over every 4KiB PFN in a range, calling pfn_valid() on each one, and ultimately doing absolutely nothing. On a platform used for virtualization, with large NOMAP regions that eventually get used for guest RAM, this leads to a significant increase in steal time experienced during kexec for a live update. Introduce for_each_valid_pfn() and use it from reserve_bootmem_region(). This implementation is precisely the same na=C3=AFve loop that the function used to have, but subsequent commits will provide optimised versions for FLATMEM and SPARSEMEM, and this version will remain for those architectures which provide their own pfn_valid() implementation, until/unless they also provide a matching for_each_valid_pfn(). Signed-off-by: David Woodhouse Reviewed-by: Mike Rapoport (Microsoft) --- include/linux/mmzone.h | 10 ++++++++++ mm/mm_init.c | 23 ++++++++++------------- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 6ccec1bf2896..230a29c2ed1a 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -2177,6 +2177,16 @@ void sparse_init(void); #define subsection_map_init(_pfn, _nr_pages) do {} while (0) #endif /* CONFIG_SPARSEMEM */ =20 +/* + * Fallback case for when the architecture provides its own pfn_valid() but + * not a corresponding for_each_valid_pfn(). + */ +#ifndef for_each_valid_pfn +#define for_each_valid_pfn(_pfn, _start_pfn, _end_pfn) \ + for ((_pfn) =3D (_start_pfn); (_pfn) < (_end_pfn); (_pfn)++) \ + if (pfn_valid(_pfn)) +#endif + #endif /* !__GENERATING_BOUNDS.H */ #endif /* !__ASSEMBLY__ */ #endif /* _LINUX_MMZONE_H */ diff --git a/mm/mm_init.c b/mm/mm_init.c index 9659689b8ace..41884f2155c4 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -777,22 +777,19 @@ static inline void init_deferred_page(unsigned long p= fn, int nid) void __meminit reserve_bootmem_region(phys_addr_t start, phys_addr_t end, int nid) { - unsigned long start_pfn =3D PFN_DOWN(start); - unsigned long end_pfn =3D PFN_UP(end); + unsigned long pfn; =20 - for (; start_pfn < end_pfn; start_pfn++) { - if (pfn_valid(start_pfn)) { - struct page *page =3D pfn_to_page(start_pfn); + for_each_valid_pfn (pfn, PFN_DOWN(start), PFN_UP(end)) { + struct page *page =3D pfn_to_page(pfn); =20 - init_deferred_page(start_pfn, nid); + init_deferred_page(pfn, nid); =20 - /* - * no need for atomic set_bit because the struct - * page is not visible yet so nobody should - * access it yet. - */ - __SetPageReserved(page); - } + /* + * no need for atomic set_bit because the struct + * page is not visible yet so nobody should + * access it yet. + */ + __SetPageReserved(page); } } =20 --=20 2.49.0 From nobody Fri Dec 19 02:49:21 2025 Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 93F2327934C for ; Wed, 23 Apr 2025 08:18:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.92.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745396330; cv=none; b=aFkAGJfj+EwFoBXkaEgS1PifPcgTD9QSK0JQIqeI3k0lxu3+HN+Zq0RMquB8W01WcEYcQ5eGxKHEedNfQeJ6sM/KBSbr369w4ZF+mslb4P/2/A+6rFkaDRwU+/f2cKtbYKzF2DeYw4UcYCBLaiIuqTlM4gzy8m3q20mixq7sqD8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745396330; c=relaxed/simple; bh=aJSyZUbvYgHKXNphFjQQ6yb4qY9ldldBA5b5a4XSOsE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=OM3B2ZDZB9jhzlaCcGt7mokdjCDhLJGCgxjgc2WPxMZqUvlyQQVNmqVrskYl3h6L/07rWjsa0Dl96rE4tSVh0MkKHqeKdqEdQhqmTOLxvz1ztd2lyxrVRhbcnBJqvup0LR2X2e/acZ5zjy16yZPgNR1aIwDF3sZeh8ML0ySmI9o= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=desiato.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=bzRNUMuC; arc=none smtp.client-ip=90.155.92.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=desiato.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="bzRNUMuC" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=X8ymBgUSA5ZN0KXL1mRdo7aKval8NgY94GI7dvST/r4=; b=bzRNUMuChOzbFAH8S7qC9tNmhC CIlHhZ7LuV418WmWzA7dWK7LtPbTHUO5x+uwHyVe0fBqEluh74Bj2oF8f9nV3ZGMyXsLqCzSh7nM+ 5XzAGnmgdqk1NUpe1dztvjf4ruAkXFrvZ92PhGQiS9AXOcfLT91vOWRIscbdVaAKCBpmWjk6u0zYG 2Pq7RHsR2aXU4piqfHOSQqnqzU/zswY0gynnv7WGJYItq0WD1AeVoBVArXIhO/3Ph8ZKnG7yLlePa sr0gFSgo1A7Ty/QUAfGAVZmtCEvnZwo6H8NEaGOwYwXyBYZcd0HzoFEox1hJoGvF7DfhDt92xgdM4 CKV4ZNIA==; Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by desiato.infradead.org with esmtpsa (Exim 4.98.1 #2 (Red Hat Linux)) id 1u7VJR-0000000BOie-1aho; Wed, 23 Apr 2025 08:18:33 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98.1 #2 (Red Hat Linux)) id 1u7VJO-00000002YNq-1401; Wed, 23 Apr 2025 09:18:30 +0100 From: David Woodhouse To: Mike Rapoport Cc: Andrew Morton , "Sauerwein, David" , Anshuman Khandual , Ard Biesheuvel , Catalin Marinas , David Hildenbrand , Marc Zyngier , Mark Rutland , Mike Rapoport , Will Deacon , kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Ruihan Li Subject: [PATCH v3 2/7] mm: Implement for_each_valid_pfn() for CONFIG_FLATMEM Date: Wed, 23 Apr 2025 08:52:44 +0100 Message-ID: <20250423081828.608422-3-dwmw2@infradead.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250423081828.608422-1-dwmw2@infradead.org> References: <20250423081828.608422-1-dwmw2@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by desiato.infradead.org. See http://www.infradead.org/rpr.html Content-Type: text/plain; charset="utf-8" From: David Woodhouse In the FLATMEM case, the default pfn_valid() just checks that the PFN is within the range [ ARCH_PFN_OFFSET .. ARCH_PFN_OFFSET + max_mapnr ). The for_each_valid_pfn() function can therefore be a simple for() loop using those as min/max respectively. Signed-off-by: David Woodhouse Reviewed-by: Mike Rapoport (Microsoft) --- include/asm-generic/memory_model.h | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/include/asm-generic/memory_model.h b/include/asm-generic/memor= y_model.h index a3b5029aebbd..74d0077cc5fa 100644 --- a/include/asm-generic/memory_model.h +++ b/include/asm-generic/memory_model.h @@ -30,7 +30,15 @@ static inline int pfn_valid(unsigned long pfn) return pfn >=3D pfn_offset && (pfn - pfn_offset) < max_mapnr; } #define pfn_valid pfn_valid -#endif + +#ifndef for_each_valid_pfn +#define for_each_valid_pfn(pfn, start_pfn, end_pfn) \ + for ((pfn) =3D max_t(unsigned long, (start_pfn), ARCH_PFN_OFFSET); \ + (pfn) < min_t(unsigned long, (end_pfn), \ + ARCH_PFN_OFFSET + max_mapnr); \ + (pfn)++) +#endif /* for_each_valid_pfn */ +#endif /* valid_pfn */ =20 #elif defined(CONFIG_SPARSEMEM_VMEMMAP) =20 --=20 2.49.0 From nobody Fri Dec 19 02:49:21 2025 Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 93F93279355 for ; Wed, 23 Apr 2025 08:18:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.92.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745396331; cv=none; b=VNdMQAZzHpGHxGCc+N9IikvVLKIS/RecZUutdLmUV2lb6/82hf3W9kuHn0JBorkCHdC234cIl8VSZgbz1qBw/vxYu23doYsIN1NqmAt6wTcTx7R9lxjYahgQ4iJ1cam1ffxo3aAq8H7YAbBD+ye0Tl6SRD9QNRAfQdEQUL2lSMo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745396331; c=relaxed/simple; bh=dmaW+Ya7giUzZii7t5MxnMhB1N77MyEn2vjV5jlY14w=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=Div55plFs6BEprQl4DX3LEWBE2lhMvSYNHlKb5ngFVx+lT9kBp71IXeRV6nR/QutPIlAyaZlm8hphW9kkJagmpZ0Ywua5p0w4P11ZKzZ4ZB9hTZbMgYr3WvkssLZq+OunlkEXGmu/OcedxiHMMNbqpn3QEvnYVG0HoZQ+iNI2dU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=desiato.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=e16xgsqx; arc=none smtp.client-ip=90.155.92.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=desiato.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="e16xgsqx" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=/QxP+QorPaa2Tzcn88O9030NShTMbuO4EdTafyQOEYk=; b=e16xgsqxWp/TFTt4kTtzrCKnsJ lhB5OpLgmwmq6A6S+tw2fer+PblQ1YUkcu0i5egxL0+8r3b5PmrA/a5EXzxWJgUOHKYqur9a7XRRl bHXtVlkMg+p6YW+K3Udfo3Cihy8v8s6jmwqIY870hNx2vGf57l3Dxh1hFRv/47QP6zA4aafI3jG7p SVmvF4AVUYDwsKAlBCNEyNko0oapQo7CGPMWew9i1X4mKmIwZcZx40bWktuo+oCSLNTMwtBpjKioj h5SD6M33U8PryDacRer2acWhLp42i+MpHPYn9l2cmOsTCKIFKERDqRTIqDyy8BcnR5S5r89RbNS0w RS/a/g+A==; Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by desiato.infradead.org with esmtpsa (Exim 4.98.1 #2 (Red Hat Linux)) id 1u7VJR-0000000BOih-1XXI; Wed, 23 Apr 2025 08:18:34 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98.1 #2 (Red Hat Linux)) id 1u7VJO-00000002YNt-1NB4; Wed, 23 Apr 2025 09:18:30 +0100 From: David Woodhouse To: Mike Rapoport Cc: Andrew Morton , "Sauerwein, David" , Anshuman Khandual , Ard Biesheuvel , Catalin Marinas , David Hildenbrand , Marc Zyngier , Mark Rutland , Mike Rapoport , Will Deacon , kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Ruihan Li Subject: [PATCH v3 3/7] mm: Implement for_each_valid_pfn() for CONFIG_SPARSEMEM Date: Wed, 23 Apr 2025 08:52:45 +0100 Message-ID: <20250423081828.608422-4-dwmw2@infradead.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250423081828.608422-1-dwmw2@infradead.org> References: <20250423081828.608422-1-dwmw2@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by desiato.infradead.org. See http://www.infradead.org/rpr.html Content-Type: text/plain; charset="utf-8" From: David Woodhouse Implement for_each_valid_pfn() based on two helper functions. The first_valid_pfn() function largely mirrors pfn_valid(), calling into a pfn_section_first_valid() helper which is trivial for the !VMEMMAP case, and in the VMEMMAP case will skip to the next subsection as needed. Since next_valid_pfn() knows that its argument *is* a valid PFN, it doesn't need to do any checking at all while iterating over the low bits within a (sub)section mask; the whole (sub)section is either present or not. Note that the VMEMMAP version of pfn_section_first_valid() may return a value *higher* than end_pfn when skipping to the next subsection, and first_valid_pfn() happily returns that higher value. This is fine. Signed-off-by: David Woodhouse Previous-revision-reviewed-by: Mike Rapoport (Microsoft) --- include/asm-generic/memory_model.h | 26 ++++++++-- include/linux/mmzone.h | 78 ++++++++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 5 deletions(-) diff --git a/include/asm-generic/memory_model.h b/include/asm-generic/memor= y_model.h index 74d0077cc5fa..044536da3390 100644 --- a/include/asm-generic/memory_model.h +++ b/include/asm-generic/memory_model.h @@ -31,12 +31,28 @@ static inline int pfn_valid(unsigned long pfn) } #define pfn_valid pfn_valid =20 +static inline bool first_valid_pfn(unsigned long *pfn) +{ + /* avoid include hell */ + extern unsigned long max_mapnr; + unsigned long pfn_offset =3D ARCH_PFN_OFFSET; + + if (*pfn < pfn_offset) { + *pfn =3D pfn_offset; + return true; + } + + if ((*pfn - pfn_offset) < max_mapnr) + return true; + + return false; +} + #ifndef for_each_valid_pfn -#define for_each_valid_pfn(pfn, start_pfn, end_pfn) \ - for ((pfn) =3D max_t(unsigned long, (start_pfn), ARCH_PFN_OFFSET); \ - (pfn) < min_t(unsigned long, (end_pfn), \ - ARCH_PFN_OFFSET + max_mapnr); \ - (pfn)++) +#define for_each_valid_pfn(pfn, start_pfn, end_pfn) \ + for (pfn =3D max_t(unsigned long, start_pfn, ARCH_PFN_OFFSET); \ + pfn < min_t(unsigned long, end_pfn, ARCH_PFN_OFFSET + max_mapnr); \ + pfn++) #endif /* for_each_valid_pfn */ #endif /* valid_pfn */ =20 diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 230a29c2ed1a..dab1d31477d7 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -2075,11 +2075,37 @@ static inline int pfn_section_valid(struct mem_sect= ion *ms, unsigned long pfn) =20 return usage ? test_bit(idx, usage->subsection_map) : 0; } + +static inline bool pfn_section_first_valid(struct mem_section *ms, unsigne= d long *pfn) +{ + struct mem_section_usage *usage =3D READ_ONCE(ms->usage); + int idx =3D subsection_map_index(*pfn); + unsigned long bit; + + if (!usage) + return false; + + if (test_bit(idx, usage->subsection_map)) + return true; + + /* Find the next subsection that exists */ + bit =3D find_next_bit(usage->subsection_map, SUBSECTIONS_PER_SECTION, idx= ); + if (bit =3D=3D SUBSECTIONS_PER_SECTION) + return false; + + *pfn =3D (*pfn & PAGE_SECTION_MASK) + (bit * PAGES_PER_SUBSECTION); + return true; +} #else static inline int pfn_section_valid(struct mem_section *ms, unsigned long = pfn) { return 1; } + +static inline bool pfn_section_first_valid(struct mem_section *ms, unsigne= d long *pfn) +{ + return true; +} #endif =20 void sparse_init_early_section(int nid, struct page *map, unsigned long pn= um, @@ -2128,6 +2154,58 @@ static inline int pfn_valid(unsigned long pfn) =20 return ret; } + +/* Returns end_pfn or higher if no valid PFN remaining in range */ +static inline unsigned long first_valid_pfn(unsigned long pfn, unsigned lo= ng end_pfn) +{ + unsigned long nr =3D pfn_to_section_nr(pfn); + + rcu_read_lock_sched(); + + while (nr <=3D __highest_present_section_nr && pfn < end_pfn) { + struct mem_section *ms =3D __pfn_to_section(pfn); + + if (valid_section(ms) && + (early_section(ms) || pfn_section_first_valid(ms, &pfn))) { + rcu_read_unlock_sched(); + return pfn; + } + + /* Nothing left in this section? Skip to next section */ + nr++; + pfn =3D section_nr_to_pfn(nr); + } + + rcu_read_unlock_sched(); + return end_pfn; +} + +static inline unsigned long next_valid_pfn(unsigned long pfn, unsigned lon= g end_pfn) +{ + pfn++; + + if (pfn >=3D end_pfn) + return end_pfn; + + /* + * Either every PFN within the section (or subsection for VMEMMAP) is + * valid, or none of them are. So there's no point repeating the check + * for every PFN; only call first_valid_pfn() the first time, and when + * crossing a (sub)section boundary (i.e. !(pfn & ~PFN_VALID_MASK)). + */ + if (pfn & (IS_ENABLED(CONFIG_SPARSEMEM_VMEMMAP) ? + PAGE_SUBSECTION_MASK : PAGE_SECTION_MASK)) + return pfn; + + return first_valid_pfn(pfn, end_pfn); +} + + +#define for_each_valid_pfn(_pfn, _start_pfn, _end_pfn) \ + for ((_pfn) =3D first_valid_pfn((_start_pfn), (_end_pfn)); \ + (_pfn) < (_end_pfn); \ + (_pfn) =3D next_valid_pfn((_pfn), (_end_pfn))) + #endif =20 static inline int pfn_in_present_section(unsigned long pfn) --=20 2.49.0 From nobody Fri Dec 19 02:49:21 2025 Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A3E3527935B for ; Wed, 23 Apr 2025 08:18:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.92.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745396330; cv=none; b=J0KLNWBudpW4vyOUvNisil7PKJaf4knmLG5gwUazFihK/189iPtovgN+gST7vmvq6LItmX9mytUE23JrO1jmrzu70szs64PtAk3NZBHAm1uZJhSPOw+LQcHloV9T9PK8Xocr7Nl6LJEei6qXyujHmvzGPLa9MMBlDAHU2g6yqQM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745396330; c=relaxed/simple; bh=D66HpPzIsh9s7WpdgSaM0v5NH51UIUN1kQRqUAsn+kI=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=mvEM2NFGjczscCjIc10U6H7cDU2jH5vOd1FZ/UDk19ewQVVIO44zyNl0KPUYTC4dAxiUXmfj1H3+exq3Z7ee9p/5rwufqT1Kvc18iVijCj6kVLM1BeoZdYerh47DywEq3rAAMdEscJHe4RbhQM8FOtz9tIKDvFZcl6nNSTkC+UA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=desiato.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=BmKxZhxX; arc=none smtp.client-ip=90.155.92.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=desiato.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="BmKxZhxX" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=EggTF7jgXyqw27ORwtXsCRX4H5aiRA/a+Rh2J8KLoHU=; b=BmKxZhxXycayWqgY04aS3O8VDv NYYZCozxkEDA0WDmbxV1yNS3+Q5Su24QLc3YxXW2RtGx+avKO+L6seps3N2pFW+n+dP45Md+d+EfE KVkAQRs0lguySdrMhj4IgZtx8enpHxrFfqTJuEtbNoSjg/AFO/LT1EfgpxF2dwZfr0cfZPqwW8UQR AxD+az8+k4r8E6CHBDutgxCGcF6GCNYhiMQyZES6DXVH9SGioJWn1a/uyBYKFPpkeWB49WUd2ERpQ XJQ7aatcCyYdQptgYKIsMoH6jIxaSyREfjipf3GSKb6iwmkSYw7x3JUuqkLvQpJpokUl7XmJKa99h WPD88/VA==; Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by desiato.infradead.org with esmtpsa (Exim 4.98.1 #2 (Red Hat Linux)) id 1u7VJR-0000000BOid-1XFD; Wed, 23 Apr 2025 08:18:33 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98.1 #2 (Red Hat Linux)) id 1u7VJO-00000002YNw-1g6W; Wed, 23 Apr 2025 09:18:30 +0100 From: David Woodhouse To: Mike Rapoport Cc: Andrew Morton , "Sauerwein, David" , Anshuman Khandual , Ard Biesheuvel , Catalin Marinas , David Hildenbrand , Marc Zyngier , Mark Rutland , Mike Rapoport , Will Deacon , kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Ruihan Li Subject: [PATCH v3 4/7] mm, PM: Use for_each_valid_pfn() in kernel/power/snapshot.c Date: Wed, 23 Apr 2025 08:52:46 +0100 Message-ID: <20250423081828.608422-5-dwmw2@infradead.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250423081828.608422-1-dwmw2@infradead.org> References: <20250423081828.608422-1-dwmw2@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by desiato.infradead.org. See http://www.infradead.org/rpr.html Content-Type: text/plain; charset="utf-8" From: David Woodhouse Signed-off-by: David Woodhouse Acked-by: Mike Rapoport (Microsoft) --- kernel/power/snapshot.c | 42 ++++++++++++++++++++--------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/kernel/power/snapshot.c b/kernel/power/snapshot.c index 4e6e24e8b854..f151c7a45584 100644 --- a/kernel/power/snapshot.c +++ b/kernel/power/snapshot.c @@ -1094,16 +1094,15 @@ static void mark_nosave_pages(struct memory_bitmap = *bm) ((unsigned long long) region->end_pfn << PAGE_SHIFT) - 1); =20 - for (pfn =3D region->start_pfn; pfn < region->end_pfn; pfn++) - if (pfn_valid(pfn)) { - /* - * It is safe to ignore the result of - * mem_bm_set_bit_check() here, since we won't - * touch the PFNs for which the error is - * returned anyway. - */ - mem_bm_set_bit_check(bm, pfn); - } + for_each_valid_pfn (pfn, region->start_pfn, region->end_pfn) { + /* + * It is safe to ignore the result of + * mem_bm_set_bit_check() here, since we won't + * touch the PFNs for which the error is + * returned anyway. + */ + mem_bm_set_bit_check(bm, pfn); + } } } =20 @@ -1255,21 +1254,20 @@ static void mark_free_pages(struct zone *zone) spin_lock_irqsave(&zone->lock, flags); =20 max_zone_pfn =3D zone_end_pfn(zone); - for (pfn =3D zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) - if (pfn_valid(pfn)) { - page =3D pfn_to_page(pfn); + for_each_valid_pfn(pfn, zone->zone_start_pfn, max_zone_pfn) { + page =3D pfn_to_page(pfn); =20 - if (!--page_count) { - touch_nmi_watchdog(); - page_count =3D WD_PAGE_COUNT; - } + if (!--page_count) { + touch_nmi_watchdog(); + page_count =3D WD_PAGE_COUNT; + } =20 - if (page_zone(page) !=3D zone) - continue; + if (page_zone(page) !=3D zone) + continue; =20 - if (!swsusp_page_is_forbidden(page)) - swsusp_unset_page_free(page); - } + if (!swsusp_page_is_forbidden(page)) + swsusp_unset_page_free(page); + } =20 for_each_migratetype_order(order, t) { list_for_each_entry(page, --=20 2.49.0 From nobody Fri Dec 19 02:49:21 2025 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 672F7268FC9 for ; Wed, 23 Apr 2025 08:18:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.50.34 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745396323; cv=none; b=Me5bktoKF6aCaJ0e067PCBTpP+z1kZbQQjkI/U02E5yWyBHr3JCib5xDGz4pIO5lzC86QJbnkrJPb7bT9X9Sb7B6/9Sv3YKh+BBd+lxxIbiEKwS4iPqj04FfRy+57gFJAc4XTg5+wjAccqEHFBlHaliqCEu8itasWb43c+Zn/X0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745396323; c=relaxed/simple; bh=cXmDa/smw3SFfJ5+0v25YOs7nGxAYzIsbMas66vAH5g=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=IW5d5HST+Sn8NbFraciFSgchsKHZkHC6hbY6YvyBW0rzWyOJ6pyQzvxShpa3v0awWpmiL2vGs7iLAzEG6wusNKgR3bRuDJk2ePvcLY5qp5PQ1ATSVcHQIpgmREBNMQTkF+qnKKocm1xRWJBWackuvkpI+w927ABD/yTj/bA7Nw4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=casper.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=KWGOaFJX; arc=none smtp.client-ip=90.155.50.34 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=casper.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="KWGOaFJX" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=SAQbWjWGGmEWV/yrGcgLnMo50LiJ5Xc9HvWor5bjKpE=; b=KWGOaFJXU2Zhudn7trVxYUEU0g 7QQaPkpOyTCf67dYB0FTPxHraBznEqfyf3CWdnhygxC8xk5dbP9BsRCymIsPTF4wAwP6KqFkr2FA6 FmzLrm8fgn5FNEGSPdZIG7zgut0D0wdH/1mn1S9JEK5UNaDsTXfnf9Rrdsasucwbo6CRYk20ltlub zqk/SOxkESDY8NKrxvp7RlTzavj/40AZ/QicG71H0mgMDCefzRigaplRoo9o5ZJdYGcaUnGaaogvj 80uEUTHcGn5ocHFj4LephL03r6fmWs2hRgpoez0yWwQeFioSX2KC8ThpS1JtfEfE0maumlFjzlgHM BdNg+AgA==; Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by casper.infradead.org with esmtpsa (Exim 4.98.2 #2 (Red Hat Linux)) id 1u7VJQ-000000081P3-0EV6; Wed, 23 Apr 2025 08:18:32 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98.1 #2 (Red Hat Linux)) id 1u7VJO-00000002YNz-1z0F; Wed, 23 Apr 2025 09:18:30 +0100 From: David Woodhouse To: Mike Rapoport Cc: Andrew Morton , "Sauerwein, David" , Anshuman Khandual , Ard Biesheuvel , Catalin Marinas , David Hildenbrand , Marc Zyngier , Mark Rutland , Mike Rapoport , Will Deacon , kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Ruihan Li Subject: [PATCH v3 5/7] mm, x86: Use for_each_valid_pfn() from __ioremap_check_ram() Date: Wed, 23 Apr 2025 08:52:47 +0100 Message-ID: <20250423081828.608422-6-dwmw2@infradead.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250423081828.608422-1-dwmw2@infradead.org> References: <20250423081828.608422-1-dwmw2@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Content-Type: text/plain; charset="utf-8" From: David Woodhouse Instead of calling pfn_valid() separately for every single PFN in the range, use for_each_valid_pfn() and only look at the ones which are. Signed-off-by: David Woodhouse Acked-by: Mike Rapoport (Microsoft) --- arch/x86/mm/ioremap.c | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/arch/x86/mm/ioremap.c b/arch/x86/mm/ioremap.c index 331e101bf801..12c8180ca1ba 100644 --- a/arch/x86/mm/ioremap.c +++ b/arch/x86/mm/ioremap.c @@ -71,7 +71,7 @@ int ioremap_change_attr(unsigned long vaddr, unsigned lon= g size, static unsigned int __ioremap_check_ram(struct resource *res) { unsigned long start_pfn, stop_pfn; - unsigned long i; + unsigned long pfn; =20 if ((res->flags & IORESOURCE_SYSTEM_RAM) !=3D IORESOURCE_SYSTEM_RAM) return 0; @@ -79,9 +79,8 @@ static unsigned int __ioremap_check_ram(struct resource *= res) start_pfn =3D (res->start + PAGE_SIZE - 1) >> PAGE_SHIFT; stop_pfn =3D (res->end + 1) >> PAGE_SHIFT; if (stop_pfn > start_pfn) { - for (i =3D 0; i < (stop_pfn - start_pfn); ++i) - if (pfn_valid(start_pfn + i) && - !PageReserved(pfn_to_page(start_pfn + i))) + for_each_valid_pfn(pfn, start_pfn, stop_pfn) + if (!PageReserved(pfn_to_page(pfn))) return IORES_MAP_SYSTEM_RAM; } =20 --=20 2.49.0 From nobody Fri Dec 19 02:49:21 2025 Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A3DA4279358 for ; Wed, 23 Apr 2025 08:18:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.92.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745396331; cv=none; b=bole00U90YcpR4PUJnc4C7jXTpXf3EYQPZcbvoGFOdz0e1VkLFLCTwgqyknTuL7cc9TdGBQEUjEi0wK/YmEuG8Zxna9vQFUFTs/dz/UQi+y0voR1wjTckkHaoXgKuvJZHjF+015rJKGRmQzA08upH/FNVXMCJ++Y2T/40rTgiDc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745396331; c=relaxed/simple; bh=4xt093V4K6X9m5HSUSUKEV60HAXCvM4r8RMK8XVZ6NU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=bTWyZVgqxwJCJUqMhEXC63NRt/RuFht0JavmU7b3tAS3Mn7B/+E/OfggVXG0BvmEecl1WkZ/TYJQVVkqPBDqXKkizDY9Lq/8pQ5Oy1jdyaQcxrIhBNZbyNLFWtNYoYMeoOdZ5sK5clAqQpmN4GgdyuWbNqay0EM3PCHofNEuftU= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=desiato.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=KGBHe0CZ; arc=none smtp.client-ip=90.155.92.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=desiato.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="KGBHe0CZ" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=5rBTRXVZOLvGm6tGVdaRqt3altL1kwi2KMEQoupVT9M=; b=KGBHe0CZaJNHjqUBvFJmhnk4e+ UPDycIuE82wa2XxkK87oLluII3D4UaUzDWr0aOdY4m4yTxi3S2ftbKW4yvTGKJRXQzkk/25oVwohE cTVuPyVEq7s3Ye8YtV+otsybX6FzJx9x1GJO7w6lIWpZZeg5pQRN5w4tjAjk7Zqa2Vgqu0Q9NVL5T zdFVyJqvVItORW1K8NoW7QRZdg0cPaMapuy9M8Asd1O75iUL9aJl+n0dGnBaKBExsZIY6QMxYm1dr 8SV+6xIkv4HXj6ZFHgb4D1nGpSv+vzwrbr03bVHHniVI2uB++M/9EcdJNQQEGULkue6uwxepbUBty 4SlyGv+w==; Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by desiato.infradead.org with esmtpsa (Exim 4.98.1 #2 (Red Hat Linux)) id 1u7VJR-0000000BOig-1ajG; Wed, 23 Apr 2025 08:18:37 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98.1 #2 (Red Hat Linux)) id 1u7VJO-00000002YO2-27ZK; Wed, 23 Apr 2025 09:18:30 +0100 From: David Woodhouse To: Mike Rapoport Cc: Andrew Morton , "Sauerwein, David" , Anshuman Khandual , Ard Biesheuvel , Catalin Marinas , David Hildenbrand , Marc Zyngier , Mark Rutland , Mike Rapoport , Will Deacon , kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Ruihan Li Subject: [PATCH v3 6/7] mm: Use for_each_valid_pfn() in memory_hotplug Date: Wed, 23 Apr 2025 08:52:48 +0100 Message-ID: <20250423081828.608422-7-dwmw2@infradead.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250423081828.608422-1-dwmw2@infradead.org> References: <20250423081828.608422-1-dwmw2@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by desiato.infradead.org. See http://www.infradead.org/rpr.html Content-Type: text/plain; charset="utf-8" From: David Woodhouse Signed-off-by: David Woodhouse Acked-by: Mike Rapoport (Microsoft) --- mm/memory_hotplug.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/mm/memory_hotplug.c b/mm/memory_hotplug.c index 8305483de38b..8f74c55137bf 100644 --- a/mm/memory_hotplug.c +++ b/mm/memory_hotplug.c @@ -1756,12 +1756,10 @@ static int scan_movable_pages(unsigned long start, = unsigned long end, { unsigned long pfn; =20 - for (pfn =3D start; pfn < end; pfn++) { + for_each_valid_pfn (pfn, start, end) { struct page *page; struct folio *folio; =20 - if (!pfn_valid(pfn)) - continue; page =3D pfn_to_page(pfn); if (PageLRU(page)) goto found; @@ -1805,11 +1803,9 @@ static void do_migrate_range(unsigned long start_pfn= , unsigned long end_pfn) static DEFINE_RATELIMIT_STATE(migrate_rs, DEFAULT_RATELIMIT_INTERVAL, DEFAULT_RATELIMIT_BURST); =20 - for (pfn =3D start_pfn; pfn < end_pfn; pfn++) { + for_each_valid_pfn (pfn, start_pfn, end_pfn) { struct page *page; =20 - if (!pfn_valid(pfn)) - continue; page =3D pfn_to_page(pfn); folio =3D page_folio(page); =20 --=20 2.49.0 From nobody Fri Dec 19 02:49:21 2025 Received: from desiato.infradead.org (desiato.infradead.org [90.155.92.199]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 52B4A27935D for ; Wed, 23 Apr 2025 08:18:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=90.155.92.199 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745396333; cv=none; b=HWMoDdzrBGuxXFS7cc3WbFvoSMdoWJ4Mm+bNEtxPBQwRj+gEtReEjdQP/YQlK/S4WxC4PylQmwwed/SZMgHbqdPGyvhzoHuFwvTddkq3i8nXhJz6CpWAJxGHDK5q0w22AP+dOYEFqBwnknxMvN6RhtXRjnNwoJdeeaj+0AWsrek= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1745396333; c=relaxed/simple; bh=OYq1eYFCazepNiSfRkSzy0Xny7Mf8EDgUkUeTs5V7Iw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ZFOSTMKMF7H1/zFuNQ34ZUzrmBAsRHv/nJbVrUvIPj1vMH5cX8a2gH22zS9Cv3HYySPLTkIG9sMfDPl9eYdAQVut1DmI5gx6+krvlQ6c+MFfVwuWbSK+5kRBhNi9XZWrjLdwBPzGNBVX+njCF1yJHWlDmwXm2ucD7yoix24a1os= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org; spf=none smtp.mailfrom=desiato.srs.infradead.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b=ICIc2yM6; arc=none smtp.client-ip=90.155.92.199 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=infradead.org Authentication-Results: smtp.subspace.kernel.org; spf=none smtp.mailfrom=desiato.srs.infradead.org Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="ICIc2yM6" DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description; bh=cbMdDFG9P+bdaitfSB/95+V1DKWHcnUFJFkFTDn7hYM=; b=ICIc2yM6OI9RDh+NOJty3eOWUJ FgYo2iqowgJFiYjbmeBB6IulpyokzNwdkLIKrlmp25lnGlNqJTuA2fNNuiQHre8pHPLQrjmmLGnE4 M4hU22kFsneFnr8WhMTs/7G8PFgjZwwDjFR96WytCVzYxAsZiByHue6BlJjK4kQLeXJUuyQDQ1kR3 ejQdsH8GeC2avhfbiNgmQQx20r1oAhzCfbwUEJ0vbOnE1b3IKAelb/IjTxSfKUvhUe4c6RMW23P4Z D0sqNEwszCaPmeUJExBUw080o/JA3LMGMxB7T9ZYsalDsgBkoUUWGoUdL38Pknptkkcf5QSjFZyrt mkR51KVw==; Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by desiato.infradead.org with esmtpsa (Exim 4.98.1 #2 (Red Hat Linux)) id 1u7VJR-0000000BOif-1akK; Wed, 23 Apr 2025 08:18:38 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98.1 #2 (Red Hat Linux)) id 1u7VJO-00000002YO5-2FrH; Wed, 23 Apr 2025 09:18:30 +0100 From: David Woodhouse To: Mike Rapoport Cc: Andrew Morton , "Sauerwein, David" , Anshuman Khandual , Ard Biesheuvel , Catalin Marinas , David Hildenbrand , Marc Zyngier , Mark Rutland , Mike Rapoport , Will Deacon , kvmarm@lists.cs.columbia.edu, linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, Ruihan Li Subject: [PATCH v3 7/7] mm/mm_init: Use for_each_valid_pfn() in init_unavailable_range() Date: Wed, 23 Apr 2025 08:52:49 +0100 Message-ID: <20250423081828.608422-8-dwmw2@infradead.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250423081828.608422-1-dwmw2@infradead.org> References: <20250423081828.608422-1-dwmw2@infradead.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: David Woodhouse X-SRS-Rewrite: SMTP reverse-path rewritten from by desiato.infradead.org. See http://www.infradead.org/rpr.html Content-Type: text/plain; charset="utf-8" From: David Woodhouse Currently, memmap_init initializes pfn_hole with 0 instead of ARCH_PFN_OFFSET. Then init_unavailable_range will start iterating each page from the page at address zero to the first available page, but it won't do anything for pages below ARCH_PFN_OFFSET because pfn_valid won't pass. If ARCH_PFN_OFFSET is very large (e.g., something like 2^64-2GiB if the kernel is used as a library and loaded at a very high address), the pointless iteration for pages below ARCH_PFN_OFFSET will take a very long time, and the kernel will look stuck at boot time. Use for_each_valid_pfn() to skip the pointless iterations. Reported-by: Ruihan Li Suggested-by: Mike Rapoport Signed-off-by: David Woodhouse Reviewed-by: Mike Rapoport (Microsoft) Tested-by: Ruihan Li --- mm/mm_init.c | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/mm/mm_init.c b/mm/mm_init.c index 41884f2155c4..0d1a4546825c 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -845,11 +845,7 @@ static void __init init_unavailable_range(unsigned lon= g spfn, unsigned long pfn; u64 pgcnt =3D 0; =20 - for (pfn =3D spfn; pfn < epfn; pfn++) { - if (!pfn_valid(pageblock_start_pfn(pfn))) { - pfn =3D pageblock_end_pfn(pfn) - 1; - continue; - } + for_each_valid_pfn(pfn, spfn, epfn) { __init_single_page(pfn_to_page(pfn), pfn, zone, node); __SetPageReserved(pfn_to_page(pfn)); pgcnt++; --=20 2.49.0