Received: by 2002:a05:6a10:2785:0:0:0:0 with SMTP id ia5csp131452pxb; Thu, 14 Jan 2021 21:55:56 -0800 (PST) X-Google-Smtp-Source: ABdhPJxF6E4YEraIWKKMPqvZ5JhTyJ74j2ykDQuOiKpgZ7c8leS6A9HLlztXuverCLwuB7DwuR00 X-Received: by 2002:a17:907:9495:: with SMTP id dm21mr7667842ejc.462.1610690156480; Thu, 14 Jan 2021 21:55:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1610690156; cv=none; d=google.com; s=arc-20160816; b=Bkexs0oh/FIGGojU+Ruv0mRTmWIW6xYP6sbiVmtPB2dBHc1tfAdnakJ9cEiG/S4Ec8 pjNFbM0cI6OBZph2sN4eOKNt30R28lE+JnzvyPdHbFgXDOWj3hExbP5K8i/WP35gPnPx IuNW6eeTnXARDxmREF+rhEoOU2EZ3P8HYGEG2fer3TJRcSK1lyxOD/z8qOuLr/eV74B7 qQIuBFQsHi/utYw1B8zCM6ShYSXfHYnEeu7b+fMBKgfBZBzdnFXL3Gg6vFOsx+mDdRls opxtbvlRcppJCxpgkp3qERiahm+qjBh7/AhQl0KDcHbxh+Zj995ju8crZLwU/xiEzahA QEew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=iQ2UYmDz36c9nXzEd5gE2Ywc265DeHo4fiakmMHVmRY=; b=P0wLbPl3h8llJP2Sn/n3vWaW82a5yLG4BBI+LBbDrLigV010cBGMYhvqhPDikroG0l odyq/RUE3u+3pyNDSeekdqHsQYLe7If8XhPtY+LbEJj2xCtXigRljIL77yTffasnkh/F +8lgiRP1Zyq83/katunYWzsU1/C6L1WyGsVY1Tts3j1CWD0dxj8pf3YCrog4xEoJ7B2g Wf2DOTb33Ge7PFPQfznmDKWFDEnpoZGfxQfdJSb+DgdgHph/1H/uCH8eWknj5LzNWs0A 7NH3O6TIXKsJCouAFKhUq3K3L6vph/NEZ8T3qPX3Nz+TT8gKUx+5D9dpUWaZfN8DdD1Q LASg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id z23si3521458edx.444.2021.01.14.21.55.33; Thu, 14 Jan 2021 21:55:56 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1733190AbhAOFlq (ORCPT + 99 others); Fri, 15 Jan 2021 00:41:46 -0500 Received: from szxga05-in.huawei.com ([45.249.212.191]:11016 "EHLO szxga05-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732057AbhAOFlc (ORCPT ); Fri, 15 Jan 2021 00:41:32 -0500 Received: from DGGEMS404-HUB.china.huawei.com (unknown [172.30.72.59]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4DH9430WsTzj7RG; Fri, 15 Jan 2021 13:39:47 +0800 (CST) Received: from localhost.localdomain.localdomain (10.175.113.25) by DGGEMS404-HUB.china.huawei.com (10.3.19.204) with Microsoft SMTP Server id 14.3.498.0; Fri, 15 Jan 2021 13:40:41 +0800 From: Kefeng Wang To: , , , Russell King , , Atish Patra CC: Palmer Dabbelt , , Paul Walmsley , Kefeng Wang Subject: [PATCH v3 2/4] initramfs: Provide a common initrd reserve function Date: Fri, 15 Jan 2021 13:46:04 +0800 Message-ID: <20210115054606.124502-3-wangkefeng.wang@huawei.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210115054606.124502-1-wangkefeng.wang@huawei.com> References: <20210115054606.124502-1-wangkefeng.wang@huawei.com> MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.175.113.25] X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Some architectures(eg, ARM and riscv) have similar logic to check and reserve the memory of initrd, let's provide a common function reserve_initrd_mem() to reduce duplicated code. Signed-off-by: Kefeng Wang --- include/linux/initrd.h | 6 ++++++ init/initramfs.c | 45 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) diff --git a/include/linux/initrd.h b/include/linux/initrd.h index fc30ac30e10e..85c15717af34 100644 --- a/include/linux/initrd.h +++ b/include/linux/initrd.h @@ -18,6 +18,12 @@ extern int initrd_below_start_ok; extern unsigned long initrd_start, initrd_end; extern void free_initrd_mem(unsigned long, unsigned long); +#ifdef CONFIG_BLK_DEV_INITRD +extern void __init reserve_initrd_mem(void); +#else +static inline void __init reserve_initrd_mem(void) {} +#endif + extern phys_addr_t phys_initrd_start; extern unsigned long phys_initrd_size; diff --git a/init/initramfs.c b/init/initramfs.c index 55b74d7e5260..f75c89e9d602 100644 --- a/init/initramfs.c +++ b/init/initramfs.c @@ -535,6 +535,51 @@ extern unsigned long __initramfs_size; #include #include +void __init reserve_initrd_mem(void) +{ + phys_addr_t start; + unsigned long size; + + /* Ignore the virtul address computed during device tree parsing */ + initrd_start = initrd_end = 0; + + if (!phys_initrd_size) + return; + /* + * Round the memory region to page boundaries as per free_initrd_mem() + * This allows us to detect whether the pages overlapping the initrd + * are in use, but more importantly, reserves the entire set of pages + * as we don't want these pages allocated for other purposes. + */ + start = round_down(phys_initrd_start, PAGE_SIZE); + size = phys_initrd_size + (phys_initrd_start - start); + size = round_up(size, PAGE_SIZE); + + if (!memblock_is_region_memory(start, size)) { + pr_err("INITRD: 0x%08llx+0x%08lx is not a memory region", + (u64)start, size); + goto disable; + } + + if (memblock_is_region_reserved(start, size)) { + pr_err("INITRD: 0x%08llx+0x%08lx overlaps in-use memory region\n", + (u64)start, size); + goto disable; + } + + memblock_reserve(start, size); + /* Now convert initrd to virtual addresses */ + initrd_start = (unsigned long)__va(phys_initrd_start); + initrd_end = initrd_start + phys_initrd_size; + initrd_below_start_ok = 1; + + return; +disable: + pr_cont(" - disabling initrd\n"); + initrd_start = 0; + initrd_end = 0; +} + void __weak __init free_initrd_mem(unsigned long start, unsigned long end) { #ifdef CONFIG_ARCH_KEEP_MEMBLOCK -- 2.26.2