From patchwork Thu Mar 16 13:17:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13177614 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id A9D1BC6FD19 for ; Thu, 16 Mar 2023 13:18:23 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 49517900007; Thu, 16 Mar 2023 09:18:23 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4451B900002; Thu, 16 Mar 2023 09:18:23 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 30DF7900007; Thu, 16 Mar 2023 09:18:23 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id 20C6B900002 for ; Thu, 16 Mar 2023 09:18:23 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay04.hostedemail.com (Postfix) with ESMTP id B77211A0ED7 for ; Thu, 16 Mar 2023 13:18:22 +0000 (UTC) X-FDA: 80574815244.15.B5D7F58 Received: from mail-wm1-f45.google.com (mail-wm1-f45.google.com [209.85.128.45]) by imf22.hostedemail.com (Postfix) with ESMTP id CAE6FC0019 for ; Thu, 16 Mar 2023 13:18:19 +0000 (UTC) Authentication-Results: imf22.hostedemail.com; dkim=pass header.d=rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=PToYMu0c; spf=pass (imf22.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.128.45 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1678972699; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Tp3HoPN4Gc82+RijS+v6sxY9JgB+vr3DxCDQErkACNI=; b=UI+VfddbgPlOEMzLzg2J6gCiVsp0psvd1RvnQgCloFTeensIBRD6u9UkphstgZhhXgTXVo PTmGwSJsX13O7lKPHhPVTYbaPJGl5EmFFSngpxPTKnddEh16sQJ3pR4K9QUYwoV4/ZTtw0 YNqZ51UV0aX0Ghe47ZdiMFNTynZlvM8= ARC-Authentication-Results: i=1; imf22.hostedemail.com; dkim=pass header.d=rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=PToYMu0c; spf=pass (imf22.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.128.45 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678972699; a=rsa-sha256; cv=none; b=JSBUQGicXrrJWmUj47k4D69rBEC2yuq2VjXhrqPPsJmt+DgV3+Sdlde7PEl8WZ9Q8CFEhS w8ihHaQK0jcvm8zlBbgq2C6HKL5B2hEaFluKYEWBTJ7ICMtEzOjYiZFDCakAUyXojM7l4H eq/NlnQB4JcIhTwU1OPs+YEAf2HmGws= Received: by mail-wm1-f45.google.com with SMTP id l15-20020a05600c4f0f00b003ed58a9a15eso1103546wmq.5 for ; Thu, 16 Mar 2023 06:18:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; t=1678972698; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Tp3HoPN4Gc82+RijS+v6sxY9JgB+vr3DxCDQErkACNI=; b=PToYMu0cKs8JYZ6vE0jZ7YqptxvvNxzJLVidWaPVHOytoFMIFwdTLHn0duEcW9msRA 1DEp4a1ec5G0nWISGMpEGtVdHHRcqLIy32AocIm6p96VGsWXu1VqxQPHrribt2ncNfPa IT7NvAy3f1XFXxF4ab7MvjC7CER1BZR7JNhxXrN5STyfV/hVjikL5OIksSxcZfM5NZ7b p+sKuSERVfHhmhbBauv0yFqr6stSOvpGururNP0b9AutYpWisTZOOvIyYvb+WKc3PTc5 kSNH7H862sCAjFRcT5NtWiledbRkxgFXVqgQFo/OSB2M+TbcKKYrTnLSa0NrQWaJraNx warg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678972698; 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=Tp3HoPN4Gc82+RijS+v6sxY9JgB+vr3DxCDQErkACNI=; b=kDJSPGf7LNFDWsUYq+CHJXLRwA/3aXsLI2nyFHPio7IeOe1nt3vhzldZm8NSuu7EsT 9K2tpZxFcjlguDsTNtYphYqNf1v544L/MbreqQ7+eVdSmFN3dJ9VLVkY3rL8P/h0C0nx EzkwisN92b/iuRPdnm4g/B3ZcrBaKbpWCjzjGsIDgQcxqr2YcdDMcpegqD59FOxX/xS7 9Cc3SXCdIvE5cxLVCr9SYHCnxA8o5gacUTxqTVjdxS+uT4iKeNArCqbtCESIyeeJbHbH knnL9u3E3gqIWX2cQASemEOAh4un6sxcVh/ABNMQnSa/xcz07X0Evwkeel6jI7U7ly6L 1o4w== X-Gm-Message-State: AO0yUKXe/+qXvzFVCiUDHsXOjuC3A15E4pc06gHDHmdyOCnBsJbLOW6Y /42+lfIioUg8byi8RmCPd+lliswzEWL5or017XA= X-Google-Smtp-Source: AK7set/fPYDWjI/nDNH4Tp4cX0o/pdQFhcmC6kakh/lPH0h+uqTf9K4xF0cfCvGEO1gwABpPdHlqIw== X-Received: by 2002:a05:600c:a46:b0:3ec:4621:680b with SMTP id c6-20020a05600c0a4600b003ec4621680bmr17906976wmq.14.1678972698428; Thu, 16 Mar 2023 06:18:18 -0700 (PDT) Received: from alex-rivos.ba.rivosinc.com (238.174.185.81.rev.sfr.net. [81.185.174.238]) by smtp.gmail.com with ESMTPSA id c16-20020adffb50000000b002d2f0e23acbsm24648wrs.12.2023.03.16.06.18.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Mar 2023 06:18:18 -0700 (PDT) From: Alexandre Ghiti To: Catalin Marinas , Will Deacon , Paul Walmsley , Palmer Dabbelt , Albert Ou , Rob Herring , Frank Rowand , Mike Rapoport , Andrew Morton , Anup Patel , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-mm@kvack.org Cc: Alexandre Ghiti , Andrew Jones Subject: [PATCH v8 1/4] riscv: Get rid of riscv_pfn_base variable Date: Thu, 16 Mar 2023 14:17:08 +0100 Message-Id: <20230316131711.1284451-2-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230316131711.1284451-1-alexghiti@rivosinc.com> References: <20230316131711.1284451-1-alexghiti@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: CAE6FC0019 X-Stat-Signature: uxxcgn9855aizsnfzd86i8amma7tb14d X-Rspam-User: X-HE-Tag: 1678972699-970957 X-HE-Meta: U2FsdGVkX18Ar3I6ssS3urW+08SpbLYBq4yDuEJiPll57zsA8UDjiM2QjfJOFEbDdq1nvCFrrDsYCrcz9wrljSM5jEIx7t6+2kJC5RWIPWznwVAJnoV6s/15haqKf0jq4CnYasZsGEI8u1rk7oME8iC92IGsRdYO3KJC8W9NpkF5WW065u4lfOUkRp9UEhut8mgdi5MQcopbIicBGzZ19IIuOmDuhabwBQEX07szhw/M/pTpgsTln2C7pGpdXGu2m4ryEsklrqXUm1Pcnq3avx282lKDenMriYYtYCeGybdfFog7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yDPZ09Ew3TMAwIEYUujknXWxG1FpM/8T9wcyYx83QBQ65SMpvAqIpruKa14ddXPAiYeJZ1QmkAKlmcCzOzQzAtnvJpBbtZYDcAtyktmxG7IKwHYtib8d9TcE+UBIyq7M7kTO2cvqEvu5EiePGVQCoX8ZPDGbw7lGrYL1fWJ32QSI6oEaUl5PkhIWqiDfXmaKupArRh0L/izOYRoRaUyN7EJfd74E2RRsiEUyjGAV6dlqb9t0HDEK43B9H/Nt6Hkp0duSwWnPdFjG8VmUuB2odcsaTJVOiP8eWjkgViO/jB02HF8 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Use directly phys_ram_base instead, riscv_pfn_base is just the pfn of the address contained in phys_ram_base. Even if there is no functional change intended in this patch, actually setting phys_ram_base that early changes the behaviour of kernel_mapping_pa_to_va during the early boot: phys_ram_base used to be zero before this patch and now it is set to the physical start address of the kernel. But it does not break the conversion of a kernel physical address into a virtual address since kernel_mapping_pa_to_va should only be used on kernel physical addresses, i.e. addresses greater than the physical start address of the kernel. Signed-off-by: Alexandre Ghiti Reviewed-by: Andrew Jones Reviewed-by: Anup Patel Tested-by: Anup Patel --- arch/riscv/include/asm/page.h | 3 +-- arch/riscv/mm/init.c | 6 +----- 2 files changed, 2 insertions(+), 7 deletions(-) diff --git a/arch/riscv/include/asm/page.h b/arch/riscv/include/asm/page.h index 7fed7c431928..8dc686f549b6 100644 --- a/arch/riscv/include/asm/page.h +++ b/arch/riscv/include/asm/page.h @@ -91,8 +91,7 @@ typedef struct page *pgtable_t; #endif #ifdef CONFIG_MMU -extern unsigned long riscv_pfn_base; -#define ARCH_PFN_OFFSET (riscv_pfn_base) +#define ARCH_PFN_OFFSET (PFN_DOWN((unsigned long)phys_ram_base)) #else #define ARCH_PFN_OFFSET (PAGE_OFFSET >> PAGE_SHIFT) #endif /* CONFIG_MMU */ diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index 87f6a5d475a6..cc558d94559a 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -271,9 +271,6 @@ static void __init setup_bootmem(void) #ifdef CONFIG_MMU struct pt_alloc_ops pt_ops __initdata; -unsigned long riscv_pfn_base __ro_after_init; -EXPORT_SYMBOL(riscv_pfn_base); - pgd_t swapper_pg_dir[PTRS_PER_PGD] __page_aligned_bss; pgd_t trampoline_pg_dir[PTRS_PER_PGD] __page_aligned_bss; static pte_t fixmap_pte[PTRS_PER_PTE] __page_aligned_bss; @@ -285,7 +282,6 @@ static pmd_t __maybe_unused early_dtb_pmd[PTRS_PER_PMD] __initdata __aligned(PAG #ifdef CONFIG_XIP_KERNEL #define pt_ops (*(struct pt_alloc_ops *)XIP_FIXUP(&pt_ops)) -#define riscv_pfn_base (*(unsigned long *)XIP_FIXUP(&riscv_pfn_base)) #define trampoline_pg_dir ((pgd_t *)XIP_FIXUP(trampoline_pg_dir)) #define fixmap_pte ((pte_t *)XIP_FIXUP(fixmap_pte)) #define early_pg_dir ((pgd_t *)XIP_FIXUP(early_pg_dir)) @@ -985,7 +981,7 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa) kernel_map.va_pa_offset = PAGE_OFFSET - kernel_map.phys_addr; kernel_map.va_kernel_pa_offset = kernel_map.virt_addr - kernel_map.phys_addr; - riscv_pfn_base = PFN_DOWN(kernel_map.phys_addr); + phys_ram_base = kernel_map.phys_addr; /* * The default maximal physical memory size is KERN_VIRT_SIZE for 32-bit From patchwork Thu Mar 16 13:17:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13177615 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9BCC7C6FD1F for ; Thu, 16 Mar 2023 13:19:24 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3D11C900008; Thu, 16 Mar 2023 09:19:24 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 380E6900002; Thu, 16 Mar 2023 09:19:24 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2717B900008; Thu, 16 Mar 2023 09:19:24 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 172D2900002 for ; Thu, 16 Mar 2023 09:19:24 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id C73DBA137F for ; Thu, 16 Mar 2023 13:19:23 +0000 (UTC) X-FDA: 80574817806.15.F9954D1 Received: from mail-wr1-f41.google.com (mail-wr1-f41.google.com [209.85.221.41]) by imf01.hostedemail.com (Postfix) with ESMTP id 9E72E40005 for ; Thu, 16 Mar 2023 13:19:21 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=67doldxY; dmarc=none; spf=pass (imf01.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.221.41 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1678972761; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=yXNicKwHPUURyALO36PEegj/jGCRXjX7/YBezTaryXs=; b=FJiMvzYMyu/Jl0shijoaPEoelbzaHeC7FP2lygjwgCTIUyPG7vWMFwGT12q3V0LrNajLgV rMg6zjNA4uA+x9UkZUkLSmzoFJMurKqLmzUq6Vz7kwjahbPHz7z7cIpvQD+ictDIVtchTM S37phKmWdnt4Zxz6xi0zwI5I3x8lUeE= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=67doldxY; dmarc=none; spf=pass (imf01.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.221.41 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678972761; a=rsa-sha256; cv=none; b=PBQyye5yaCV6qOQU9l5X3p+qZ7aH3OLn3TSvp1Ieb3/+W71ZDTynxj/R4z/HxzckqggyoC vaLEznUmNRJJsJ1LxTpRgJCkdl/ulkc2x1gE4UTe/28l6rtGtdv/fNaXEee2jf3TrY5Rsh xwMKwpX7obBKO9hTDzj02H0zjGTF50w= Received: by mail-wr1-f41.google.com with SMTP id m2so1513528wrh.6 for ; Thu, 16 Mar 2023 06:19:21 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; t=1678972760; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=yXNicKwHPUURyALO36PEegj/jGCRXjX7/YBezTaryXs=; b=67doldxYgYKlU/XZ0kZu3+NOAqQ/2WbnP4mwdaTGI0McuaMhRPDAE/s74Wq3t6tF/V J0nRxluNtH5jyXjG6gwranylrEnecoP/DUWgaO9JZ4gg6xFffDGE08IwIc2KOUXaspal HHuTUIg4II4i6Oa5sRMFXG0qwnNOZQwEH5YClaHtl1ekxMRL+d17IM5dSSKvN8etH4o4 Gryhf8z+9DXYE61ol4wGcZdxrAPW2qatjZ5Q8iOQkHFCUmxFt43/+ApFfD1rBG+HZP3t j+M2PnF2oTfQycLOuf/WXwZevaBF66GgvGXiPTY9lyNMUBSV3fX9CsUSbPa1JK7AlIvp vKjg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678972760; 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=yXNicKwHPUURyALO36PEegj/jGCRXjX7/YBezTaryXs=; b=tYaJtjo8IjDor7vLbgqsPZEImit77eeGczTlf0yw44c0mLRm7x9w/X/KdG7pPZjy97 dUDvsO2FqUFl934FGslzn65NAQp3MVF8vzpb6TdSYkQ9TWPWDmwFOR97oL2Wq3FZvAGo KsQm1qMhnIMJSJsvLni1oSLl0zLBsnhSAY7Jj5hz0ykgN2Jhb2CNQcJUAsHPb066Jxfa 2wgIo/D13dacsnjxox2vDJ3cj4PM7zCGXKp+p7QWMkFBcyvATz6TOee5yUPoowgXYQWW FfODJMTrok5gPDEEFUe9DXx49S3E7VNKx0Qw2he7BVtBpA1Y8XRSHk2adWrgipJ4845o HNUg== X-Gm-Message-State: AO0yUKVSIAx9Y1i/Wjr3Dd4jFM8ys3WLWZ7NGp0h12GuUPMUXp3jQhau ywGQVJRededAuQyeTCfpBaAPtg== X-Google-Smtp-Source: AK7set9ckwc+gnRj+k56d+SvgcefBu0ajEjl4o1f/HxCl/7FMrq5djA9ftFJ5OFMO4WJD/IRBsDEKA== X-Received: by 2002:a5d:49cc:0:b0:2c7:57e:633e with SMTP id t12-20020a5d49cc000000b002c7057e633emr4666191wrs.49.1678972760162; Thu, 16 Mar 2023 06:19:20 -0700 (PDT) Received: from alex-rivos.ba.rivosinc.com (238.174.185.81.rev.sfr.net. [81.185.174.238]) by smtp.gmail.com with ESMTPSA id h5-20020a5d5485000000b002c5a1bd5280sm7210070wrv.95.2023.03.16.06.19.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Mar 2023 06:19:20 -0700 (PDT) From: Alexandre Ghiti To: Catalin Marinas , Will Deacon , Paul Walmsley , Palmer Dabbelt , Albert Ou , Rob Herring , Frank Rowand , Mike Rapoport , Andrew Morton , Anup Patel , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-mm@kvack.org Cc: Alexandre Ghiti Subject: [PATCH v8 2/4] mm: Introduce memblock_isolate_memory Date: Thu, 16 Mar 2023 14:17:09 +0100 Message-Id: <20230316131711.1284451-3-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230316131711.1284451-1-alexghiti@rivosinc.com> References: <20230316131711.1284451-1-alexghiti@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Server: rspam02 X-Rspamd-Queue-Id: 9E72E40005 X-Stat-Signature: i5k8359wnyui1og4zgys5npxfzx5q7uy X-HE-Tag: 1678972761-735094 X-HE-Meta: U2FsdGVkX18impUBMWb7w9316u29j7XjRHAz72EI8FnnWD4zALURLxsRMxUdunqGKl3wQWlqoFkOsYl3F3MoTnskBvGjemYxab50siVXgUFu/K7eBG0RoW7D7F2SS4rn3ZnkdWxyb1X6hrHQmYBtPRH/Yi6WU5drlvlVBcEjO8vSjAX1US7w4c3Z8Xkf6E94bUglG2vPXdcYju3Cg/q1oiQLKc4ojbnlRdRWixmzveZ9xrIHDU3btR3s2iJZs3MG+SZk9n2aMcG1g84i83a0Irc2fnRC+ug5t844iClYjNG4+8H0Qc+IGuDQj+PSvLva3M3wZjRZJjOcA5QnC1ib/ESFOCgr2FC7LqlHgFVD+eR5nFDM7qu69Ic+tc6WnwdW3kfz/mfBwgUwE9+YNd2tn6ibS3obh6FSwmc8+pCAdVtqToe89+iOzMitg31XJFrMTSR7pppW51B13s8I28a1WbUM5KcOLUQe915WBhcsbX7p5x2gpi1Rdggs7EjBolrGk1nJ5F5smGikYG3sW1cEx0f5XuILXJ+i/7flxHxagMczMXqmUqsER+Yv/DLG0fIrEyohiLKlmyy/3yqJNkCr2I/De6J6jtB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Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: This function allows to split a region in memblock.memory and will be useful when setting up the linear mapping with STRICT_KERNEL_RWX: it allows to isolate the kernel text/rodata and then avoid to map those regions with a PUD/P4D/PGD. Signed-off-by: Alexandre Ghiti Reviewed-by: Anup Patel Tested-by: Anup Patel Acked-by: Mike Rapoport (IBM) --- include/linux/memblock.h | 1 + mm/memblock.c | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/include/linux/memblock.h b/include/linux/memblock.h index 50ad19662a32..2f7ef97c0da7 100644 --- a/include/linux/memblock.h +++ b/include/linux/memblock.h @@ -125,6 +125,7 @@ int memblock_clear_hotplug(phys_addr_t base, phys_addr_t size); int memblock_mark_mirror(phys_addr_t base, phys_addr_t size); int memblock_mark_nomap(phys_addr_t base, phys_addr_t size); int memblock_clear_nomap(phys_addr_t base, phys_addr_t size); +int memblock_isolate_memory(phys_addr_t base, phys_addr_t size); void memblock_free_all(void); void memblock_free(void *ptr, size_t size); diff --git a/mm/memblock.c b/mm/memblock.c index 25fd0626a9e7..e8c651a37012 100644 --- a/mm/memblock.c +++ b/mm/memblock.c @@ -805,6 +805,26 @@ static int __init_memblock memblock_isolate_range(struct memblock_type *type, return 0; } +/** + * memblock_isolate_memory - isolate given range in memblock.memory + * @base: base of range to isolate + * @size: size of range to isolate + * + * Isolates the given range in memblock.memory so that it does not share any + * region with other ranges. + * + * Return: + * 0 on success, -errno on failure. + */ + +int __init_memblock memblock_isolate_memory(phys_addr_t base, phys_addr_t size) +{ + int start_rgn, end_rgn; + + return memblock_isolate_range(&memblock.memory, base, size, + &start_rgn, &end_rgn); +} + static int __init_memblock memblock_remove_range(struct memblock_type *type, phys_addr_t base, phys_addr_t size) { From patchwork Thu Mar 16 13:17:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13177616 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6336FC6FD19 for ; Thu, 16 Mar 2023 13:20:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AFC13900008; Thu, 16 Mar 2023 09:20:25 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id AAC6A900002; Thu, 16 Mar 2023 09:20:25 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 97485900008; Thu, 16 Mar 2023 09:20:25 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 84ED2900002 for ; Thu, 16 Mar 2023 09:20:25 -0400 (EDT) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 6011A41409 for ; Thu, 16 Mar 2023 13:20:25 +0000 (UTC) X-FDA: 80574820410.24.A1F462A Received: from mail-wr1-f44.google.com (mail-wr1-f44.google.com [209.85.221.44]) by imf01.hostedemail.com (Postfix) with ESMTP id 7EBA640016 for ; Thu, 16 Mar 2023 13:20:23 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=L6eAT+sm; spf=pass (imf01.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.221.44 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1678972823; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=cNtcjIvt7Jcu6BbJu0k6G/sx5AHflabOU3bxX5prrUE=; b=Ek7cDidZ5S9Ri0dNA9qWwFfUdmuF0ULTM3mPaI6s4Fpzwa4iRdYlBHoeRIvR0z4DcIoMQE JHb8VW2QvNUzh89qdCJmmwrDj7wo3HQUJ6uMTmygb5kMI3ZrMwQJpk99fuGfRChJMAJU7a QKIHIc4yY5N+JLG804H7JMRj5HZQjjo= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=L6eAT+sm; spf=pass (imf01.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.221.44 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678972823; a=rsa-sha256; cv=none; b=fQjmMPu6HoowJKmdF6WDqTh2chC8e8B1jV/FGWuvWR5jAwCoH8vV2LWQdYpi34YYcYcGe1 IIaZyPbPbCO/JK7S31OSC1U5W3mnrFJT4FVWedtsZLGK1x23C2p1AS2AK4c0KNSFRm+9im Gz5QpHExbczVuecPUFDQgPKrcp8zyC0= Received: by mail-wr1-f44.google.com with SMTP id h17so1515312wrt.8 for ; Thu, 16 Mar 2023 06:20:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; t=1678972822; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=cNtcjIvt7Jcu6BbJu0k6G/sx5AHflabOU3bxX5prrUE=; b=L6eAT+smuQGoJO4DezL+IdwC0T7yTp/6MdgBgpSYGnzsE3GMtH0OsHZCA5KvkK/jsL SCQbzTNJRG+h5gG55AwIc3rp3qvPJ5HvHBpBdMMvum+UhNDX3K0d4mevTt2us+FFR9fP /SkYJ3PFgwjlEeZJ4QPID/NaQDyD2lSqhEWFJsMLe0iYwjDfErv7L30D/hLJYYVSFfx7 jrT7h87/rKRPpMs9Aij8oJj9DBxmwR0E5LN3DULWfc0aFVYbJwTggaKYwjOlxm9f5rbi T0csEj0sYOg1wDUvvVEpshdAj9QA/fAZ/IJurP8+1WuNoMR3+2zSN2/cqzpGfR2FuvNh EyZw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678972822; 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=cNtcjIvt7Jcu6BbJu0k6G/sx5AHflabOU3bxX5prrUE=; b=kvzezwXBjus+HG4smC5OovQXNWrBirTYnl77rhzFdrMG5kn7gGM+gQvjVI3JqCm/6W iA3dPr0kBpDYEjTYil+ZwGtC+xniwFuPlsLAPGSQtJ0eFctCiW54ovNJVuhHUAl7poil Wvetws5niO+TA2SXt2d3a2AQDY0Vl7gOIghqF/eCuit59d5dcreHpuA7g4FulFnIMoyi WmaRMOz48/IjD/wZaeNamOaoI4j6s8M0N0DN4MXlaGx5vngW1u8T3LbQc9BXU9J9ufIv sNJStz1A2TGcxWxqAOVrc86zRIXSRvAmTohg8apSQCNQ9IEvgwGnw3NwQfrVPtw7QulT 0e3g== X-Gm-Message-State: AO0yUKXvtlMosPD6UjYgiEusvQN9zXhOXzDbo4jGNHpIksoJdP4GrUEu 4W+SP0pHZeEkGRbWcB28/XHN+FMj+vyq9gAhJt8= X-Google-Smtp-Source: AK7set98CBvIraFbTCbJWdo8CgRP1gvKzH5/GyoF0yuADO0rA+pPToO5CbtwnI+LruqnMYg8TXy3Ww== X-Received: by 2002:adf:ce11:0:b0:2cf:e3d0:2a43 with SMTP id p17-20020adfce11000000b002cfe3d02a43mr4536359wrn.4.1678972821966; Thu, 16 Mar 2023 06:20:21 -0700 (PDT) Received: from alex-rivos.ba.rivosinc.com (238.174.185.81.rev.sfr.net. [81.185.174.238]) by smtp.gmail.com with ESMTPSA id z4-20020a5d6544000000b002c56013c07fsm7218162wrv.109.2023.03.16.06.20.20 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Mar 2023 06:20:21 -0700 (PDT) From: Alexandre Ghiti To: Catalin Marinas , Will Deacon , Paul Walmsley , Palmer Dabbelt , Albert Ou , Rob Herring , Frank Rowand , Mike Rapoport , Andrew Morton , Anup Patel , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-mm@kvack.org Cc: Alexandre Ghiti Subject: [PATCH v8 3/4] arm64: Make use of memblock_isolate_memory for the linear mapping Date: Thu, 16 Mar 2023 14:17:10 +0100 Message-Id: <20230316131711.1284451-4-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230316131711.1284451-1-alexghiti@rivosinc.com> References: <20230316131711.1284451-1-alexghiti@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam05 X-Rspamd-Queue-Id: 7EBA640016 X-Stat-Signature: tkbabafjsujcdgzw3nok7dq9y6efqpd6 X-Rspam-User: X-HE-Tag: 1678972823-685352 X-HE-Meta: U2FsdGVkX19gEJ2P6RCg89aCJBXtor0Nzg4/HNEo6GyIQTAzuSHOjgZPPe88WT+mlPvY8a3Zp/P08Lp0JENBWGeupRbsxdufI17cQuP69pPlXSEZLko0/ttQJejaC21fB52aiZS6j196hZF93+cAZpRsE8OLwc7RK668HjsZWImTemJ5YlRFnllfrcFWUE2jIIIlNFN+yC6kKrfS7lgl3WF25G9lmELwhyECb77pk1DMI296Q7ZinMxz/XNC/ZJvNxMxUfQ64096Ohcjl0iTPbWA9f8rUgiuG4wWU2qV0r3VZ0xstbKgrEjWtsZVel7B8R2sDHeBo78MjuZDlSCuAkQOqkxOMvOGD/S4t8TNlo6r9B0pTDl0juZy+KyqhJDaaJEKi7JLBDmqv9hO2ShBF88KEENYdjVbRYFZr6M1c60/6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NORg+eQDI2NbHzbP981A2ldJ9VDj4c5WckhOEo50YtLrjW3rPH+lpmqR1DPvMuOIO30RFU= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: In order to isolate the kernel text mapping and the crash kernel region, we used some sort of hack to isolate thoses ranges which consisted in marking them as not mappable with memblock_mark_nomap. Simply use the newly introduced memblock_isolate_memory function which does exactly the same but does not uselessly mark the region as not mappable. Signed-off-by: Alexandre Ghiti --- arch/arm64/mm/mmu.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/arch/arm64/mm/mmu.c b/arch/arm64/mm/mmu.c index 6f9d8898a025..387c2a065a09 100644 --- a/arch/arm64/mm/mmu.c +++ b/arch/arm64/mm/mmu.c @@ -548,19 +548,18 @@ static void __init map_mem(pgd_t *pgdp) /* * Take care not to create a writable alias for the - * read-only text and rodata sections of the kernel image. - * So temporarily mark them as NOMAP to skip mappings in - * the following for-loop + * read-only text and rodata sections of the kernel image so isolate + * those regions and map them after the for loop. */ - memblock_mark_nomap(kernel_start, kernel_end - kernel_start); + memblock_isolate_memory(kernel_start, kernel_end - kernel_start); #ifdef CONFIG_KEXEC_CORE if (crash_mem_map) { if (defer_reserve_crashkernel()) flags |= NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS; else if (crashk_res.end) - memblock_mark_nomap(crashk_res.start, - resource_size(&crashk_res)); + memblock_isolate_memory(crashk_res.start, + resource_size(&crashk_res)); } #endif @@ -568,6 +567,17 @@ static void __init map_mem(pgd_t *pgdp) for_each_mem_range(i, &start, &end) { if (start >= end) break; + + if (start == kernel_start) + continue; + +#ifdef CONFIG_KEXEC_CORE + if (start == crashk_res.start && + crash_mem_map && !defer_reserve_crashkernel() && + crashk_res.end) + continue; +#endif + /* * The linear map must allow allocation tags reading/writing * if MTE is present. Otherwise, it has the same attributes as @@ -589,7 +599,6 @@ static void __init map_mem(pgd_t *pgdp) */ __map_memblock(pgdp, kernel_start, kernel_end, PAGE_KERNEL, NO_CONT_MAPPINGS); - memblock_clear_nomap(kernel_start, kernel_end - kernel_start); /* * Use page-level mappings here so that we can shrink the region @@ -603,8 +612,6 @@ static void __init map_mem(pgd_t *pgdp) crashk_res.end + 1, PAGE_KERNEL, NO_BLOCK_MAPPINGS | NO_CONT_MAPPINGS); - memblock_clear_nomap(crashk_res.start, - resource_size(&crashk_res)); } } #endif From patchwork Thu Mar 16 13:17:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexandre Ghiti X-Patchwork-Id: 13177627 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9ADF2C6FD19 for ; Thu, 16 Mar 2023 13:28:04 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 30769900003; Thu, 16 Mar 2023 09:28:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 2B715900002; Thu, 16 Mar 2023 09:28:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 17F10900003; Thu, 16 Mar 2023 09:28:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 08601900002 for ; Thu, 16 Mar 2023 09:28:04 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id C6CC0A09A6 for ; Thu, 16 Mar 2023 13:28:03 +0000 (UTC) X-FDA: 80574839646.13.4E0D8EB Received: from mail-ed1-f46.google.com (mail-ed1-f46.google.com [209.85.208.46]) by imf14.hostedemail.com (Postfix) with ESMTP id B7605100014 for ; Thu, 16 Mar 2023 13:28:01 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=HyU+ShRO; spf=pass (imf14.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.208.46 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1678973281; a=rsa-sha256; cv=none; b=8axYSfVH0bAJ4LNAGN6UmKP+pxz1tYpNpYuZ9nZbingSMScOvMYhVCO7JXwGKaIo9sSpZO rkO8edMBiE8pl5BdmJXyKd/Kx02wEj/JJMIgOd5hW5fzJRFR2IcBYQULwoE/tvNVyTlbuh ddpp7vCPApdkMZQlj25i4Xd+vngRc9g= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=rivosinc-com.20210112.gappssmtp.com header.s=20210112 header.b=HyU+ShRO; spf=pass (imf14.hostedemail.com: domain of alexghiti@rivosinc.com designates 209.85.208.46 as permitted sender) smtp.mailfrom=alexghiti@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1678973281; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=xI+mOo9w6N5hnNZASu9KEqOIyOdQmEOvWrAnFqrzLH0=; b=rRi5n2ZoIVZyNidDldIHQ3KS3d+loE87NEABeVm3SkQU6le8LvtdDI1gjPCYvPCOttgxL8 p7inSQG4eODudGBjx4Uy1VnKX3mLlJRw18uXZ6026hDe79EQHFqsnSEsCk7zFDXIw/1Xq9 l26EJ+/pEgUg/FGkZxDsYaCYAI9HMew= Received: by mail-ed1-f46.google.com with SMTP id x3so7593135edb.10 for ; Thu, 16 Mar 2023 06:28:01 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20210112.gappssmtp.com; s=20210112; t=1678973280; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=xI+mOo9w6N5hnNZASu9KEqOIyOdQmEOvWrAnFqrzLH0=; b=HyU+ShROEFVeScMNEWVX3zkJn/83YknBtCESZ8I4D4X9rNOqMaqrTlsasz0BPTWZPQ w1QvEJ40UNKS22PVRs4ojQ7JYwzM0CEDHuFUSzhhGxu6JIqe7xR3HF5iEIKeq/EIfJ7n RPuHvGjFr9y4tLZiw1CDzUklgCfzCbhNbU022NX0M21mUQPTOwf3n8Q5jZseVkviNDRp o8rYW1AC+INHgCYt5YXxOWHIwyZiOM9+Ta8MDesKEvTrXg+g2Qs2r5+eE54ktYn9KIvf uhYR2JZ25Mj+b0zRHLfCnubKo5vM3VSRuVgt1TaNO2ez+qdgNueCXFNtqsx6syIFnJkv kP9A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678973280; 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=xI+mOo9w6N5hnNZASu9KEqOIyOdQmEOvWrAnFqrzLH0=; b=zIQ1M6H9QIkepP8YJfLUv8wPDN4Hlkh4NTLZxQh2lVJg7w2IRBhplfPhACROflLj1K ZO1osG2TKTh422np3ulr95LdBrqFJ26WWdn6NrsWIVCdv5Rk29K89nt2y3zfvvsKd8Fs rmMI7yER6gZiIsNlDVMt769Wf3sdgBXGs/1SexBKj606qI2Ll44uxoa1PZ7EjWsVNEXU YDN7FxUHda6S6tb+njXuhxdOZu8HEWY/btTSnkq9eAFu8+voxXUfrsjxn8yY7ONXbB/C ADWmJIq4iDW/BMOCr/AQFeXM7qapqgxXajuZSuFMqhpvLPGjWglI5R2CyXtMyXircSo8 PImQ== X-Gm-Message-State: AO0yUKXfvuQ/0NxQPLRZ4HfhEVt6vjp6cS7KfdGME1XQSWws5IaPHCny oP9pVXvQajt0oi6zyPTqN+/0V77gGmZw3sftBPA= X-Google-Smtp-Source: AK7set8TF8WABsMAn9COJsU0X76koVWVflLbQ8RfbeebysWvsoAh0GH6ucIAFoBJcTIPjKQxwFOEDQ== X-Received: by 2002:a05:600c:4453:b0:3eb:389d:156c with SMTP id v19-20020a05600c445300b003eb389d156cmr22345321wmn.37.1678972883803; Thu, 16 Mar 2023 06:21:23 -0700 (PDT) Received: from alex-rivos.ba.rivosinc.com (238.174.185.81.rev.sfr.net. [81.185.174.238]) by smtp.gmail.com with ESMTPSA id bh10-20020a05600c3d0a00b003ed23e9e03bsm4993248wmb.46.2023.03.16.06.21.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Mar 2023 06:21:23 -0700 (PDT) From: Alexandre Ghiti To: Catalin Marinas , Will Deacon , Paul Walmsley , Palmer Dabbelt , Albert Ou , Rob Herring , Frank Rowand , Mike Rapoport , Andrew Morton , Anup Patel , linux-arm-kernel@lists.infradead.org, linux-kernel@vger.kernel.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-mm@kvack.org Cc: Alexandre Ghiti , Rob Herring , Andrew Jones Subject: [PATCH v8 4/4] riscv: Use PUD/P4D/PGD pages for the linear mapping Date: Thu, 16 Mar 2023 14:17:11 +0100 Message-Id: <20230316131711.1284451-5-alexghiti@rivosinc.com> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20230316131711.1284451-1-alexghiti@rivosinc.com> References: <20230316131711.1284451-1-alexghiti@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Rspamd-Queue-Id: B7605100014 X-Rspamd-Server: rspam01 X-Stat-Signature: u7j96gymdi1c1arsffc5a3ucpgi5wfn3 X-HE-Tag: 1678973281-592923 X-HE-Meta: U2FsdGVkX1/gvwMuqvc4KjjJJ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4+/bBFrBBTUdiQrd6QKjDO9FaFtP0sBQdFm0xrIuTU3yLhr+EelnyVQN0IUTTNpB8Wx3lt0bmD9yOcjhiiANJ+hhswalEAs5yWGZzvXb6UBBfDGCdumY1TIRy9G9Lv2RpPBwCZWr1ugBnJgNcx8Rtx7fEZ5FSMIhaDE3wxM9zsM4TjZ1HY4ah/hN7RibR886Xg4qcNu5jn9w779M5xYE6j3KZuKrSbMb2QVLoakLFEA22QhX/9TseeYyEl/jx5IXgT0pAAyhqw42K3rJNnOlBkKchgd0zYg2G/J8+gGwETLwmjZaEtpRR3aTYZuB+AUze2h53NxvO0mdTzMG/uWDQazHzYjkrXlsOHlUgG2xbUKhBOG5antYFtqbyl9IBTeaXLP4UCBmYyhAJc6kC/qyRt/AuRZIx/cQkQXdmwKpUH0kIkxk7bajxQLr1fqUU7tc0ZHJLAvnYwA9+/1Z9ThP71mHBBSFE0IDUuPqeRp/hWHV9hKQHS9+bnkRK2tvPdDfutHzPHm0BRR0XEklx61TtqNjKNqL3v+qXSkIAfs4riVXrmsJQDeAcW68fVKLmCFyVK0/B0u0rh3dNCI44lSzpMREOkx6JEUcN+idUSF6L6Mw== X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: During the early page table creation, we used to set the mapping for PAGE_OFFSET to the kernel load address: but the kernel load address is always offseted by PMD_SIZE which makes it impossible to use PUD/P4D/PGD pages as this physical address is not aligned on PUD/P4D/PGD size (whereas PAGE_OFFSET is). But actually we don't have to establish this mapping (ie set va_pa_offset) that early in the boot process because: - first, setup_vm installs a temporary kernel mapping and among other things, discovers the system memory, - then, setup_vm_final creates the final kernel mapping and takes advantage of the discovered system memory to create the linear mapping. During the first phase, we don't know the start of the system memory and then until the second phase is finished, we can't use the linear mapping at all and phys_to_virt/virt_to_phys translations must not be used because it would result in a different translation from the 'real' one once the final mapping is installed. So here we simply delay the initialization of va_pa_offset to after the system memory discovery. But to make sure noone uses the linear mapping before, we add some guard in the DEBUG_VIRTUAL config. Finally we can use PUD/P4D/PGD hugepages when possible, which will result in a better TLB utilization. Note that: - this does not apply to rv32 as the kernel mapping lies in the linear mapping. - we rely on the firmware to protect itself using PMP. Signed-off-by: Alexandre Ghiti Acked-by: Rob Herring # DT bits Reviewed-by: Andrew Jones Reviewed-by: Anup Patel Tested-by: Anup Patel --- arch/riscv/include/asm/page.h | 16 ++++++++++++ arch/riscv/mm/init.c | 49 ++++++++++++++++++++++++++++++----- arch/riscv/mm/physaddr.c | 16 ++++++++++++ drivers/of/fdt.c | 11 ++++---- 4 files changed, 81 insertions(+), 11 deletions(-) diff --git a/arch/riscv/include/asm/page.h b/arch/riscv/include/asm/page.h index 8dc686f549b6..ea1a0e237211 100644 --- a/arch/riscv/include/asm/page.h +++ b/arch/riscv/include/asm/page.h @@ -90,6 +90,14 @@ typedef struct page *pgtable_t; #define PTE_FMT "%08lx" #endif +#ifdef CONFIG_64BIT +/* + * We override this value as its generic definition uses __pa too early in + * the boot process (before kernel_map.va_pa_offset is set). + */ +#define MIN_MEMBLOCK_ADDR 0 +#endif + #ifdef CONFIG_MMU #define ARCH_PFN_OFFSET (PFN_DOWN((unsigned long)phys_ram_base)) #else @@ -121,7 +129,11 @@ extern phys_addr_t phys_ram_base; #define is_linear_mapping(x) \ ((x) >= PAGE_OFFSET && (!IS_ENABLED(CONFIG_64BIT) || (x) < PAGE_OFFSET + KERN_VIRT_SIZE)) +#ifndef CONFIG_DEBUG_VIRTUAL #define linear_mapping_pa_to_va(x) ((void *)((unsigned long)(x) + kernel_map.va_pa_offset)) +#else +void *linear_mapping_pa_to_va(unsigned long x); +#endif #define kernel_mapping_pa_to_va(y) ({ \ unsigned long _y = (unsigned long)(y); \ (IS_ENABLED(CONFIG_XIP_KERNEL) && _y < phys_ram_base) ? \ @@ -130,7 +142,11 @@ extern phys_addr_t phys_ram_base; }) #define __pa_to_va_nodebug(x) linear_mapping_pa_to_va(x) +#ifndef CONFIG_DEBUG_VIRTUAL #define linear_mapping_va_to_pa(x) ((unsigned long)(x) - kernel_map.va_pa_offset) +#else +phys_addr_t linear_mapping_va_to_pa(unsigned long x); +#endif #define kernel_mapping_va_to_pa(y) ({ \ unsigned long _y = (unsigned long)(y); \ (IS_ENABLED(CONFIG_XIP_KERNEL) && _y < kernel_map.virt_addr + XIP_OFFSET) ? \ diff --git a/arch/riscv/mm/init.c b/arch/riscv/mm/init.c index cc558d94559a..7af7cd201a9c 100644 --- a/arch/riscv/mm/init.c +++ b/arch/riscv/mm/init.c @@ -213,6 +213,14 @@ static void __init setup_bootmem(void) phys_ram_end = memblock_end_of_DRAM(); if (!IS_ENABLED(CONFIG_XIP_KERNEL)) phys_ram_base = memblock_start_of_DRAM(); + + /* + * In 64-bit, any use of __va/__pa before this point is wrong as we + * did not know the start of DRAM before. + */ + if (IS_ENABLED(CONFIG_64BIT)) + kernel_map.va_pa_offset = PAGE_OFFSET - phys_ram_base; + /* * memblock allocator is not aware of the fact that last 4K bytes of * the addressable memory can not be mapped because of IS_ERR_VALUE @@ -667,9 +675,16 @@ void __init create_pgd_mapping(pgd_t *pgdp, static uintptr_t __init best_map_size(phys_addr_t base, phys_addr_t size) { - /* Upgrade to PMD_SIZE mappings whenever possible */ - base &= PMD_SIZE - 1; - if (!base && size >= PMD_SIZE) + if (!(base & (PGDIR_SIZE - 1)) && size >= PGDIR_SIZE) + return PGDIR_SIZE; + + if (!(base & (P4D_SIZE - 1)) && size >= P4D_SIZE) + return P4D_SIZE; + + if (!(base & (PUD_SIZE - 1)) && size >= PUD_SIZE) + return PUD_SIZE; + + if (!(base & (PMD_SIZE - 1)) && size >= PMD_SIZE) return PMD_SIZE; return PAGE_SIZE; @@ -978,11 +993,22 @@ asmlinkage void __init setup_vm(uintptr_t dtb_pa) set_satp_mode(); #endif - kernel_map.va_pa_offset = PAGE_OFFSET - kernel_map.phys_addr; + /* + * In 64-bit, we defer the setup of va_pa_offset to setup_bootmem, + * where we have the system memory layout: this allows us to align + * the physical and virtual mappings and then make use of PUD/P4D/PGD + * for the linear mapping. This is only possible because the kernel + * mapping lies outside the linear mapping. + * In 32-bit however, as the kernel resides in the linear mapping, + * setup_vm_final can not change the mapping established here, + * otherwise the same kernel addresses would get mapped to different + * physical addresses (if the start of dram is different from the + * kernel physical address start). + */ + kernel_map.va_pa_offset = IS_ENABLED(CONFIG_64BIT) ? + 0UL : PAGE_OFFSET - kernel_map.phys_addr; kernel_map.va_kernel_pa_offset = kernel_map.virt_addr - kernel_map.phys_addr; - phys_ram_base = kernel_map.phys_addr; - /* * The default maximal physical memory size is KERN_VIRT_SIZE for 32-bit * kernel, whereas for 64-bit kernel, the end of the virtual address @@ -1097,6 +1123,17 @@ static void __init setup_vm_final(void) __pa_symbol(fixmap_pgd_next), PGDIR_SIZE, PAGE_TABLE); +#ifdef CONFIG_STRICT_KERNEL_RWX + /* + * Isolate the kernel text and rodata linear so they don't + * get mapped with a PUD in the linear mapping. + */ + memblock_isolate_memory(__pa_symbol(_start), + __init_data_begin - _start); + memblock_isolate_memory(__pa_symbol(__start_rodata), + __start_rodata - _data); +#endif + /* Map all memory banks in the linear mapping */ for_each_mem_range(i, &start, &end) { if (start >= end) diff --git a/arch/riscv/mm/physaddr.c b/arch/riscv/mm/physaddr.c index 9b18bda74154..18706f457da7 100644 --- a/arch/riscv/mm/physaddr.c +++ b/arch/riscv/mm/physaddr.c @@ -33,3 +33,19 @@ phys_addr_t __phys_addr_symbol(unsigned long x) return __va_to_pa_nodebug(x); } EXPORT_SYMBOL(__phys_addr_symbol); + +phys_addr_t linear_mapping_va_to_pa(unsigned long x) +{ + BUG_ON(!kernel_map.va_pa_offset); + + return ((unsigned long)(x) - kernel_map.va_pa_offset); +} +EXPORT_SYMBOL(linear_mapping_va_to_pa); + +void *linear_mapping_pa_to_va(unsigned long x) +{ + BUG_ON(!kernel_map.va_pa_offset); + + return ((void *)((unsigned long)(x) + kernel_map.va_pa_offset)); +} +EXPORT_SYMBOL(linear_mapping_pa_to_va); diff --git a/drivers/of/fdt.c b/drivers/of/fdt.c index d1a68b6d03b3..d14735a81301 100644 --- a/drivers/of/fdt.c +++ b/drivers/of/fdt.c @@ -887,12 +887,13 @@ const void * __init of_flat_dt_match_machine(const void *default_match, static void __early_init_dt_declare_initrd(unsigned long start, unsigned long end) { - /* ARM64 would cause a BUG to occur here when CONFIG_DEBUG_VM is - * enabled since __va() is called too early. ARM64 does make use - * of phys_initrd_start/phys_initrd_size so we can skip this - * conversion. + /* + * __va() is not yet available this early on some platforms. In that + * case, the platform uses phys_initrd_start/phys_initrd_size instead + * and does the VA conversion itself. */ - if (!IS_ENABLED(CONFIG_ARM64)) { + if (!IS_ENABLED(CONFIG_ARM64) && + !(IS_ENABLED(CONFIG_RISCV) && IS_ENABLED(CONFIG_64BIT))) { initrd_start = (unsigned long)__va(start); initrd_end = (unsigned long)__va(end); initrd_below_start_ok = 1;