From patchwork Fri Apr 4 15:59:53 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: David Woodhouse X-Patchwork-Id: 14038666 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 A3E88C36010 for ; Fri, 4 Apr 2025 16:08:28 +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:MIME-Version: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:In-Reply-To:References:List-Owner; bh=P5VYLYdEkeWgyeWX/EZa0WnpJ73Q8FAesiLU/ww7Cxs=; b=ninKwQvR3ZR3OirlzHvFjp0Tbs DLZtT9NoUND89ZsWPDjfaCuWK2xMsgCXUEvIVui7Bmvmpb2BnuN1WlJjUFafIzku/Gw8NI9q7jSGq airbHBL0U5mrpgK0huA1RGhn9BVcqVbqfbm8qRrUfRqypcT2nHwngLx8E/ZEAqeja1lrz1JNb2PFD g36kFxZbWiuEXxmBbIAoFIGxBNfdoq98Eufq8iMHc1vWOF2LmpctvOFnC/DKK9V48I34E0zZn0HZF iUn4im99yMnrLG+TRdMPkGGfCAxS93m8zkU1SFlN0YwF1X7z9ntNIqajHhMu6bj2Le6EshNZHdT3H QBudefuw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.1 #2 (Red Hat Linux)) id 1u0jad-0000000CGFr-3SYx; Fri, 04 Apr 2025 16:08:19 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.98.1 #2 (Red Hat Linux)) id 1u0jSf-0000000CF3d-2SZX for linux-arm-kernel@bombadil.infradead.org; Fri, 04 Apr 2025 16:00:05 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=desiato.20200630; h=Sender:Content-Transfer-Encoding: Content-Type:MIME-Version:Message-ID:Date:Subject:Cc:To:From:Reply-To: Content-ID:Content-Description:In-Reply-To:References; bh=P5VYLYdEkeWgyeWX/EZa0WnpJ73Q8FAesiLU/ww7Cxs=; b=GUYU/YR47lcF88YQE7EH7TpKB2 zN7AKKvvGE+bbZSCVfqr/oyxCsvOKug18HG2HLPn2hzCCyr28tFqyGD9WE8XnLgGAI3tZkFoNGjRO OgjbHNJH1PDByFVu8pm8ZivdCL5MEV/GNaFUfd+wAfiibiI9zWlpJtBcaljmCVRXvO2F5RxGRDFcv n+kIPUAtdP9Y82yL00kM9ObdiypoF5byTbPDzb62xDr08XwYD5r8jUm8sLxrmkORJzVZ3UuKodDTs Em+8VmA6k36CwWivAtW5UVU9ugF9e40TqXDlsMyj8sHumr6FwIMVgjq+YOd/BBlhaaZ+R0N+DDoUj 9yqvaGVQ==; 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 1u0jSc-00000007QOy-0YLc; Fri, 04 Apr 2025 16:00:02 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98.1 #2 (Red Hat Linux)) id 1u0jSZ-0000000ERSB-2cgL; Fri, 04 Apr 2025 16:59:59 +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 Subject: [RFC PATCH v2 1/7] mm: Introduce for_each_valid_pfn() and use it from reserve_bootmem_region() Date: Fri, 4 Apr 2025 16:59:53 +0100 Message-ID: <20250404155959.3442111-1-dwmw2@infradead.org> X-Mailer: git-send-email 2.49.0 MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by desiato.infradead.org. See http://www.infradead.org/rpr.html 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 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ïve 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 25e80b2ca7f4..32ecb5cadbaf 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -2176,6 +2176,16 @@ void sparse_init(void); #define subsection_map_init(_pfn, _nr_pages) do {} while (0) #endif /* CONFIG_SPARSEMEM */ +/* + * 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) = (_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 a38a1909b407..7c699bad42ad 100644 --- a/mm/mm_init.c +++ b/mm/mm_init.c @@ -777,22 +777,19 @@ static inline void init_deferred_page(unsigned long pfn, int nid) void __meminit reserve_bootmem_region(phys_addr_t start, phys_addr_t end, int nid) { - unsigned long start_pfn = PFN_DOWN(start); - unsigned long end_pfn = PFN_UP(end); + unsigned long pfn; - for (; start_pfn < end_pfn; start_pfn++) { - if (pfn_valid(start_pfn)) { - struct page *page = pfn_to_page(start_pfn); + for_each_valid_pfn (pfn, PFN_DOWN(start), PFN_UP(end)) { + struct page *page = pfn_to_page(pfn); - init_deferred_page(start_pfn, nid); + init_deferred_page(pfn, nid); - /* - * 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); } } From patchwork Fri Apr 4 15:59:54 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Woodhouse X-Patchwork-Id: 14038664 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 4FEC4C36010 for ; Fri, 4 Apr 2025 16:04:58 +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: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=bzhTX9e5Ndxofs9PWPQjKMiyjzcJ935BwY08z5T956o=; b=eLSK+mc1jfMJdgbGH+rouTWdRU 8g7Dc7jyrSc4o10aXS1YRGdyC+IEN6rOCi23KSQVAsTc05jJplbRderzGYInXlHMwUDcniCzmJDkF P2cZWqiSy21UUYK48+3akZSt+qCrCrYdBfKhmrIzgYB+4Wff708qMVjdtZoSr0WxFpCeinyndLJi7 elvMlckdVh+YiwRIaapjU9BwXY1/SejObo2HYoazYEwQXP/WX1Pc1ez5k/FY0pdt0+FZSnK4rKzTp Jk4+ZBRm76bKSHaZpTwH+UT4ES8gIKlltosDtBIRn7Me/GY0yBFlRBproVUsrbFSUG/EACAfQdcFr hN4PWrug==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.1 #2 (Red Hat Linux)) id 1u0jXD-0000000CFpW-1faB; Fri, 04 Apr 2025 16:04:47 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.98.1 #2 (Red Hat Linux)) id 1u0jSf-0000000CF3f-2SGt for linux-arm-kernel@bombadil.infradead.org; Fri, 04 Apr 2025 16:00:05 +0000 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=bzhTX9e5Ndxofs9PWPQjKMiyjzcJ935BwY08z5T956o=; b=FXBcvlGeZ+C24swIZknr5xL0p8 XJhae2JBOiSnCkqw22sW73oMle0AScGnnrrjrgRcsQsZKQse1RoOVdLIAp+dv1eKSBknP+k57ZXTz oWUMRnld+BtYXoPg9UHMvLAZzjw0c1r0zO6b6YuvPysev0lfnzXdLFdhNKt0M0ZRh4D85BOqRtvFd tK/plvL5cesPT7WqFT+JxUKBNBmTGj3rplsy2rhhCN5gtGHW7sANGWiNY+prH/0XWDK8ACkkP8KYO 0V/99vErEol0YusGdeIvCekK7OkuLDr72beS/ATKgCtccaGDE3fu936DgqYjnSi8OA0ZPVlXsvrGH 9UT0Bu0g==; 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 1u0jSc-00000007QP0-0VvU; Fri, 04 Apr 2025 16:00:02 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98.1 #2 (Red Hat Linux)) id 1u0jSZ-0000000ERSE-2u5h; Fri, 04 Apr 2025 16:59:59 +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 Subject: [RFC PATCH v2 2/7] mm: Implement for_each_valid_pfn() for CONFIG_FLATMEM Date: Fri, 4 Apr 2025 16:59:54 +0100 Message-ID: <20250404155959.3442111-2-dwmw2@infradead.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250404155959.3442111-1-dwmw2@infradead.org> References: <20250404155959.3442111-1-dwmw2@infradead.org> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by desiato.infradead.org. See http://www.infradead.org/rpr.html 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 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 | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/include/asm-generic/memory_model.h b/include/asm-generic/memory_model.h index a3b5029aebbd..044536da3390 100644 --- a/include/asm-generic/memory_model.h +++ b/include/asm-generic/memory_model.h @@ -30,7 +30,31 @@ static inline int pfn_valid(unsigned long pfn) return pfn >= pfn_offset && (pfn - pfn_offset) < max_mapnr; } #define pfn_valid pfn_valid -#endif + +static inline bool first_valid_pfn(unsigned long *pfn) +{ + /* avoid include hell */ + extern unsigned long max_mapnr; + unsigned long pfn_offset = ARCH_PFN_OFFSET; + + if (*pfn < pfn_offset) { + *pfn = 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 = 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 */ #elif defined(CONFIG_SPARSEMEM_VMEMMAP) From patchwork Fri Apr 4 15:59:55 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Woodhouse X-Patchwork-Id: 14038670 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 05FABC36010 for ; Fri, 4 Apr 2025 16:13:50 +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: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=MbqCUn6gz37WzeN1KZjuSX3PD2UQTnr+fmi1Atuk78g=; b=lW3R+8FHaJuZo03NFvMUbrKqmN UImpfKQLWIASb8cR6eWqpTDYmz0sbbfVJrIXAhtgqueVMqzRZ3aREUDWby0BELPATUkA3d0qwVdtc jnV8QweJJpVOXvon2IKF/M7RBdNV25L5nokL1bK7RFX+HTz7QAuCH1M28kFVCQOXy4x2IAKSHZ8Zr Iku6rocowAIcqwv1ucun9Go34axXkm5Cif8/Rz2aMq2x6aE7R8Fd+NK587Hbv6Di4at5Co/6sdDdM OLzXlTCbb3UPYIDkMzykcDhKQ07fr3vBCkpGP4vSf74VN2/B/m04U5D+JtutCmefI518dvnXtxZ2C XYQQLk6g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.1 #2 (Red Hat Linux)) id 1u0jfm-0000000CGv0-0YxV; Fri, 04 Apr 2025 16:13:38 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.98.1 #2 (Red Hat Linux)) id 1u0jUI-0000000CFOs-25oB for linux-arm-kernel@bombadil.infradead.org; Fri, 04 Apr 2025 16:01:46 +0000 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=MbqCUn6gz37WzeN1KZjuSX3PD2UQTnr+fmi1Atuk78g=; b=RWsr+E1eF3WBGQC6oJ449g9gwA SsSn8lnsUC2YHgCc1DkPkPKUDY6p05kQ+DQHTMZxOHadeL32SbwkTwJzKhfQt/wfDVlQW8LT7jUZt VX02qdQbHulXzvoBi+9rj6QYyJI/ylZjTYb8TVl1afWmLJP+zM5u8vNCNCebmdlJ2y8SBe/ZXyXt/ JJSQzpsnNFWFUa2iVi2mxTLgNYe1QyHZhX8Ij0hjPdB6nmog4HCDepVSKqkH+UqV5YSpcns6K4gS4 9UCNGOU2P1ees86sHyldV/kbLqCzOHfVVIER2EGdjXCQetTdlO1ArY++q7RNBABd1DojRtj+sALb7 Zq45ERmg==; Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by casper.infradead.org with esmtpsa (Exim 4.98.1 #2 (Red Hat Linux)) id 1u0jSa-0000000GFHx-39D6; Fri, 04 Apr 2025 16:01:01 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98.1 #2 (Red Hat Linux)) id 1u0jSZ-0000000ERSH-3DUK; Fri, 04 Apr 2025 16:59:59 +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 Subject: [RFC PATCH v2 3/7] mm: Implement for_each_valid_pfn() for CONFIG_SPARSEMEM Date: Fri, 4 Apr 2025 16:59:55 +0100 Message-ID: <20250404155959.3442111-3-dwmw2@infradead.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250404155959.3442111-1-dwmw2@infradead.org> References: <20250404155959.3442111-1-dwmw2@infradead.org> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html 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 From: David Woodhouse Introduce a pfn_first_valid() helper which takes a pointer to the PFN and updates it to point to the first valid PFN starting from that point, and returns true if a valid PFN was found. This largely mirrors pfn_valid(), calling into a pfn_section_first_valid() helper which is trivial for the !CONFIG_SPARSEMEM_VMEMMAP case, and in the VMEMMAP case will skip to the next subsection as needed. Signed-off-by: David Woodhouse Reviewed-by: Mike Rapoport (Microsoft) --- include/linux/mmzone.h | 59 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 32ecb5cadbaf..67cdf675a4b9 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -2074,11 +2074,37 @@ static inline int pfn_section_valid(struct mem_section *ms, unsigned long pfn) return usage ? test_bit(idx, usage->subsection_map) : 0; } + +static inline bool pfn_section_first_valid(struct mem_section *ms, unsigned long *pfn) +{ + struct mem_section_usage *usage = READ_ONCE(ms->usage); + int idx = 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 = find_next_bit(usage->subsection_map, SUBSECTIONS_PER_SECTION, idx); + if (bit == SUBSECTIONS_PER_SECTION) + return false; + + *pfn = (*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, unsigned long *pfn) +{ + return true; +} #endif void sparse_init_early_section(int nid, struct page *map, unsigned long pnum, @@ -2127,6 +2153,39 @@ static inline int pfn_valid(unsigned long pfn) return ret; } + +static inline bool first_valid_pfn(unsigned long *p_pfn) +{ + unsigned long pfn = *p_pfn; + unsigned long nr = pfn_to_section_nr(pfn); + + rcu_read_lock_sched(); + + while (nr <= __highest_present_section_nr) { + struct mem_section *ms = __pfn_to_section(pfn); + + if (valid_section(ms) && + (early_section(ms) || pfn_section_first_valid(ms, &pfn))) { + *p_pfn = pfn; + rcu_read_unlock_sched(); + return true; + } + + /* Nothing left in this section? Skip to next section */ + nr++; + pfn = section_nr_to_pfn(nr); + } + + rcu_read_unlock_sched(); + + return false; +} + +#define for_each_valid_pfn(_pfn, _start_pfn, _end_pfn) \ + for ((_pfn) = (_start_pfn); \ + first_valid_pfn(&(_pfn)) && (_pfn) < (_end_pfn); \ + (_pfn)++) + #endif static inline int pfn_in_present_section(unsigned long pfn) From patchwork Fri Apr 4 15:59:56 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Woodhouse X-Patchwork-Id: 14038669 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 BA1E7C36010 for ; Fri, 4 Apr 2025 16:12:00 +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: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=O+396y0rDBKXn/tGzn/xwAn8Ku375WZ2ZAT9PS+Ub5w=; b=4gNX7E5AG4ypVQgpVNG1Mxfvp7 E8w3DwiUwhuu7w8I8DrLY77Hzfl3GYhDogxdDdMZrOWseVRqMicUZnXn19nkXoOC1GCsE/d8Y2rhl d0ZsgBX9M3rJlks/KBm1bglgkOM4/w2hA3B/47H/M8W+kwowQXMsYVNIQBLQ2baEKJbAxah5o8ifT YwaeZEtHuugy5Wol2H4M9KGEMLuFVrE9UOjTZ9eJkcrA3Y3GdgGrXwlupX0zrpkey1P7hvdrMV9gq o2zbtimEB/bhxn8n5y151nUloohPWuURJj+yCbxWtMhoBGGMz/yxxv1EjOKJsFrK8i7IwIDpkFglX zbSlvZ+g==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.1 #2 (Red Hat Linux)) id 1u0je3-0000000CGhU-1mz3; Fri, 04 Apr 2025 16:11:51 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.98.1 #2 (Red Hat Linux)) id 1u0jUI-0000000CFOn-0Rua for linux-arm-kernel@bombadil.infradead.org; Fri, 04 Apr 2025 16:01:46 +0000 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=O+396y0rDBKXn/tGzn/xwAn8Ku375WZ2ZAT9PS+Ub5w=; b=DXHzSB3si2aN61sd7dOw7Dkduk TfChbYRNdb3ACgbes0kuBN7Ps4Sy5/UipeFG8/Xla7pFYuFAZjAv8ghlbw60pID0KBmbGy7KhdoZX xFeg4W6Khe91Ifz1tB9jPFsxfe3bqKisa6rPuuYszWXM+JzP0nj3JG8rUww67uY/Mv9WHKvRsSeQX Ok99/gqCaltwnaMSZvz4drLWaLh2mGbx5jBswPyNZtO7LBeN9BcJh4IWoGNNd4OiLAnX2VY16JZlL QP6zoE/Go4gQZXoAjZGtme0XAK6mXGvw/o5DoYFynT49usw3UBp1GA3ggRsbCxmI+nr/eYZ4vp6BK LUawAvyw==; Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by casper.infradead.org with esmtpsa (Exim 4.98.1 #2 (Red Hat Linux)) id 1u0jSa-0000000GFHy-3ANo; Fri, 04 Apr 2025 16:01:01 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98.1 #2 (Red Hat Linux)) id 1u0jSZ-0000000ERSK-3V46; Fri, 04 Apr 2025 16:59:59 +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 Subject: [RFC PATCH v2 4/7] mm: Optimise SPARSEMEM implementation of for_each_valid_pfn() Date: Fri, 4 Apr 2025 16:59:56 +0100 Message-ID: <20250404155959.3442111-4-dwmw2@infradead.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250404155959.3442111-1-dwmw2@infradead.org> References: <20250404155959.3442111-1-dwmw2@infradead.org> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html 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 From: David Woodhouse There's no point in checking the section and subsection bitmap for *every* PFN in the same section; they're either all valid or they aren't. Signed-off-by: David Woodhouse --- include/linux/mmzone.h | 39 +++++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/include/linux/mmzone.h b/include/linux/mmzone.h index 67cdf675a4b9..0da1b0ba5d9f 100644 --- a/include/linux/mmzone.h +++ b/include/linux/mmzone.h @@ -2154,21 +2154,20 @@ static inline int pfn_valid(unsigned long pfn) return ret; } -static inline bool first_valid_pfn(unsigned long *p_pfn) +/* Returns -1 (an invalid PFN) if no valid PFN remaining */ +static inline unsigned long first_valid_pfn(unsigned long pfn, unsigned long end_pfn) { - unsigned long pfn = *p_pfn; unsigned long nr = pfn_to_section_nr(pfn); rcu_read_lock_sched(); - while (nr <= __highest_present_section_nr) { + while (nr <= __highest_present_section_nr && pfn < end_pfn) { struct mem_section *ms = __pfn_to_section(pfn); if (valid_section(ms) && (early_section(ms) || pfn_section_first_valid(ms, &pfn))) { - *p_pfn = pfn; rcu_read_unlock_sched(); - return true; + return pfn; } /* Nothing left in this section? Skip to next section */ @@ -2177,14 +2176,34 @@ static inline bool first_valid_pfn(unsigned long *p_pfn) } rcu_read_unlock_sched(); + return (unsigned long)-1; +} - return false; +static inline unsigned long next_valid_pfn(unsigned long pfn, unsigned long end_pfn) +{ + pfn++; + + if (pfn >= end_pfn) + return (unsigned long)-1; + + /* + * 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) = (_start_pfn); \ - first_valid_pfn(&(_pfn)) && (_pfn) < (_end_pfn); \ - (_pfn)++) + +#define for_each_valid_pfn(_pfn, _start_pfn, _end_pfn) \ + for ((_pfn) = first_valid_pfn((_start_pfn), (_end_pfn)); \ + (_pfn) != (unsigned long)-1; \ + (_pfn) = next_valid_pfn((_pfn), (_end_pfn))) #endif From patchwork Fri Apr 4 15:59:57 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Woodhouse X-Patchwork-Id: 14038667 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 344EFC36010 for ; Fri, 4 Apr 2025 16:10:16 +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: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=EggTF7jgXyqw27ORwtXsCRX4H5aiRA/a+Rh2J8KLoHU=; b=Tm3+sMOQ6VUYvVNWiDLDU53ZJp Jd5BvMnL7H8bE35f2umQBOJJhKU6WXgemQjzruK97nurKhLES/EAgIVMItvVBMQ+Db5bYKgkKcctW EqqVwTbRZekrX1jZqr88kpvRYV16fGkQIjIcDwSANcIeZAobq10qQvWIOOTNhNDV0S/8LJPaJSmPf xvZ1amlTVTER+9SrgWbuCkfbq+OXrokv6vjoUTLrEjbd/o1Ld0OLBO1y9U/xz6AS31Vv441r4vS20 5TfqOLKvgNLN+0OYZMX2DjvuBibD7tMNb8FN9TQUTuvNRAbseNNg9MzIiMNhTxqM9a7NFBmcD25Ht sCwb483Q==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.1 #2 (Red Hat Linux)) id 1u0jcL-0000000CGR7-24g9; Fri, 04 Apr 2025 16:10:05 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.98.1 #2 (Red Hat Linux)) id 1u0jSf-0000000CF3e-3FPC for linux-arm-kernel@bombadil.infradead.org; Fri, 04 Apr 2025 16:00:05 +0000 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=HX3tq7HIzG2pG87fAq0XTCafH8 DkspEeMBkCy2NMH7zp0U9SyOEyHmpaU3H7Ofz669hMx9zaObkueGN/ZaHJDQQkbbsq/szbqDptC6j MEvAedrkC8t9fR5K0oL5RA2eStCojpgVZNK5eHYm29/U4EMZDp1rdB7mREiazKOw2ClJGF36A7NMb QIxbjBiyvvLDG2KkQFeEfwESWlQlq4Cqp751opuozo+zc7k9llxAs9HYO9i3oHJ5803HtEgylCLlr E2HjVkjQaue4zB/utadXeYQG1ct50Itrnag4GetbD68mpWhqmuyE9X0df0jP2ZmVnM5XfX1TrmFxG 1Owpd5BA==; 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 1u0jSc-00000007QP1-0YK4; Fri, 04 Apr 2025 16:00:02 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98.1 #2 (Red Hat Linux)) id 1u0jSZ-0000000ERSN-3n5A; Fri, 04 Apr 2025 16:59:59 +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 Subject: [RFC PATCH v2 5/7] mm, PM: Use for_each_valid_pfn() in kernel/power/snapshot.c Date: Fri, 4 Apr 2025 16:59:57 +0100 Message-ID: <20250404155959.3442111-5-dwmw2@infradead.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250404155959.3442111-1-dwmw2@infradead.org> References: <20250404155959.3442111-1-dwmw2@infradead.org> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by desiato.infradead.org. See http://www.infradead.org/rpr.html 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 From: David Woodhouse Signed-off-by: David Woodhouse --- 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); - for (pfn = 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); + } } } @@ -1255,21 +1254,20 @@ static void mark_free_pages(struct zone *zone) spin_lock_irqsave(&zone->lock, flags); max_zone_pfn = zone_end_pfn(zone); - for (pfn = zone->zone_start_pfn; pfn < max_zone_pfn; pfn++) - if (pfn_valid(pfn)) { - page = pfn_to_page(pfn); + for_each_valid_pfn(pfn, zone->zone_start_pfn, max_zone_pfn) { + page = pfn_to_page(pfn); - if (!--page_count) { - touch_nmi_watchdog(); - page_count = WD_PAGE_COUNT; - } + if (!--page_count) { + touch_nmi_watchdog(); + page_count = WD_PAGE_COUNT; + } - if (page_zone(page) != zone) - continue; + if (page_zone(page) != zone) + continue; - if (!swsusp_page_is_forbidden(page)) - swsusp_unset_page_free(page); - } + if (!swsusp_page_is_forbidden(page)) + swsusp_unset_page_free(page); + } for_each_migratetype_order(order, t) { list_for_each_entry(page, From patchwork Fri Apr 4 15:59:58 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Woodhouse X-Patchwork-Id: 14038665 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 C6170C36010 for ; Fri, 4 Apr 2025 16:06:46 +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: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=SAQbWjWGGmEWV/yrGcgLnMo50LiJ5Xc9HvWor5bjKpE=; b=Va5llWxPRQ9JY2xfO6kAe9XNGy 1J0LNjzS0V/4/+UC96fQmZcxZotClGwpYtubL0IL/EGfO3BzKr+nZka6z0cfIRJoZ71zlZD07MIJX IFpw9vLeRBzxNHJiDtEfzvIVrc/SMjvqrwn1LAsUWK39nHT6XI1KuOPnVhPDEyeu+9UvosEHmdqz9 XK2itxzvX0pOeuQ+GX8Ofxwkp/fV9LQN/uU7IfJWyTQMaAhxoEhAZHpeFtVGpNq1qHbcS2nU8hGfR ugZR/YSVlj/O3QzBE7/JKPUTpmHqLmn953j9nUJTjw0Pipc4two1lQCsWd8RLO6XsC0PqLj9LrlZa F37rTLRA==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.1 #2 (Red Hat Linux)) id 1u0jYv-0000000CG3S-0flT; Fri, 04 Apr 2025 16:06:33 +0000 Received: from desiato.infradead.org ([2001:8b0:10b:1:d65d:64ff:fe57:4e05]) by bombadil.infradead.org with esmtps (Exim 4.98.1 #2 (Red Hat Linux)) id 1u0jSf-0000000CF3c-2SX7 for linux-arm-kernel@bombadil.infradead.org; Fri, 04 Apr 2025 16:00:05 +0000 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=SAQbWjWGGmEWV/yrGcgLnMo50LiJ5Xc9HvWor5bjKpE=; b=d7l3I/9/QLogRFJbAFJpwKuy6H B0t3Z7Upm6Db3cxysxtEATptTKPyFIjrvR1IiTqa6fj8zd+dQ8qw/mYXmi9eramZYOT01xjsBr9gv g3S8NHQZZWom5Th4kzq6dixygw3CD0iPWHeYGA+zJcTe5SD91Taidj9jqPX5gJFJP4ueN+seQocDD jPgCHdxWYs/rmNqy4h+MUxbebrc9j5fMAou3iLqAwi6nhlq91/43pUrP3Nh2E91NbF/NfMzh1yd1o cmGSfeUGIxkQ2VCvR8WSn2FWLQC/FlgQVqNRFhNpjyGM/f89o/R33M7cx3j005R7UGgMZUqNNonCz 5OSe/p9g==; 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 1u0jSc-00000007QOz-0XcV; Fri, 04 Apr 2025 16:00:02 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98.1 #2 (Red Hat Linux)) id 1u0jSZ-0000000ERSQ-40aW; Fri, 04 Apr 2025 16:59:59 +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 Subject: [RFC PATCH v2 6/7] mm, x86: Use for_each_valid_pfn() from __ioremap_check_ram() Date: Fri, 4 Apr 2025 16:59:58 +0100 Message-ID: <20250404155959.3442111-6-dwmw2@infradead.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250404155959.3442111-1-dwmw2@infradead.org> References: <20250404155959.3442111-1-dwmw2@infradead.org> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by desiato.infradead.org. See http://www.infradead.org/rpr.html 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 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 --- 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 long size, static unsigned int __ioremap_check_ram(struct resource *res) { unsigned long start_pfn, stop_pfn; - unsigned long i; + unsigned long pfn; if ((res->flags & IORESOURCE_SYSTEM_RAM) != IORESOURCE_SYSTEM_RAM) return 0; @@ -79,9 +79,8 @@ static unsigned int __ioremap_check_ram(struct resource *res) start_pfn = (res->start + PAGE_SIZE - 1) >> PAGE_SHIFT; stop_pfn = (res->end + 1) >> PAGE_SHIFT; if (stop_pfn > start_pfn) { - for (i = 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; } From patchwork Fri Apr 4 15:59:59 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: David Woodhouse X-Patchwork-Id: 14038671 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 4306FC36010 for ; Fri, 4 Apr 2025 16:15:33 +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: MIME-Version:References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From: Reply-To:Content-Type:Content-ID:Content-Description:Resent-Date:Resent-From: Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=zwrAwVynWffEuxKPl0OEyEEBjA9QOQig2FF2bzdhEpA=; b=eyKMFOpCZMpakzJx4nt8WPnwbT dGfZRSFAvIkBcuEY3fcyxyMBr5d7rllFJgvsV6u7md/GeS2Nl3t+S4a0sDgposy13l+zr2qvvJLdW 1YsK0PJjz6g08D88x4j7GZ6cYHUeoi6la9Uq9SMfme56cRaz83DJAInJDa4Y4NFPQ8CQJBZrADrIL /ZhDZiIYfGR7A0jCWS/mSVK4Tl5ZrdzlHQQxAwyw2XMDtgFXI8jzBh3SmTO+qXcjPFQ2mf1WoYbMa kq2HnlIVf3ID6HKXuc+VZFYXBQ8XhLV7KDSONZbi/QtTGjF5izX0BgnG+1qU8RHe3MsMI7Kk2wEzJ T+WUpV9w==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.98.1 #2 (Red Hat Linux)) id 1u0jhT-0000000CH7c-3JiR; Fri, 04 Apr 2025 16:15:23 +0000 Received: from casper.infradead.org ([2001:8b0:10b:1236::1]) by bombadil.infradead.org with esmtps (Exim 4.98.1 #2 (Red Hat Linux)) id 1u0jUI-0000000CFOt-27Kf for linux-arm-kernel@bombadil.infradead.org; Fri, 04 Apr 2025 16:01:46 +0000 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=zwrAwVynWffEuxKPl0OEyEEBjA9QOQig2FF2bzdhEpA=; b=rcQ/TZXnLakdQxL4crd49SXX3c edNsEOVe5KuObd9N9FvHOyENAzG1hCUGVegfPjAUI+DESeHQ2QnvFHahouiLIKJXGm/9RbMSGbECY 3H7F9hOPYEdMTiEfgcGsbG7aWDxhy3ZY9WqpRK44a5vaJHV37Lx6OginK6lDUrp+urL++BV2mtbH5 p+ZFyzIAKAcSjEt2BvR0qBo1O4L3+1RqPBeKcN2MGAKLhzlFZ2wIbiVn+ohK9/LZ1v86JEJ+uw17N 4zNZ8gbxTQmZ4FYx30Cw1FcflV3mnJKiulvzObmjLOk24f6Y0o78MuVRM7S5DrvoDrcue1z65zfMb chjOXhaA==; Received: from [2001:8b0:10b:1::ebe] (helo=i7.infradead.org) by casper.infradead.org with esmtpsa (Exim 4.98.1 #2 (Red Hat Linux)) id 1u0jSa-0000000GFHw-3A3Z; Fri, 04 Apr 2025 16:01:01 +0000 Received: from dwoodhou by i7.infradead.org with local (Exim 4.98.1 #2 (Red Hat Linux)) id 1u0jSZ-0000000ERST-48Ac; Fri, 04 Apr 2025 16:59:59 +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 Subject: [RFC PATCH v2 7/7] mm: use for_each_valid_pfn() in memory_hotplug Date: Fri, 4 Apr 2025 16:59:59 +0100 Message-ID: <20250404155959.3442111-7-dwmw2@infradead.org> X-Mailer: git-send-email 2.49.0 In-Reply-To: <20250404155959.3442111-1-dwmw2@infradead.org> References: <20250404155959.3442111-1-dwmw2@infradead.org> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html 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 From: David Woodhouse Signed-off-by: David Woodhouse --- 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 75401866fb76..20c39ae45016 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; - for (pfn = start; pfn < end; pfn++) { + for_each_valid_pfn (pfn, start, end) { struct page *page; struct folio *folio; - if (!pfn_valid(pfn)) - continue; page = 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); - for (pfn = start_pfn; pfn < end_pfn; pfn++) { + for_each_valid_pfn (pfn, start_pfn, end_pfn) { struct page *page; - if (!pfn_valid(pfn)) - continue; page = pfn_to_page(pfn); folio = page_folio(page);