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 mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id BB7FEC433EF for ; Thu, 4 Nov 2021 06:17:27 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id 7C630611C4 for ; Thu, 4 Nov 2021 06:17:27 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.1 mail.kernel.org 7C630611C4 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=huawei.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id: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=a2UFjs5HXgByw7YAsHGqlOyagJtQmIha+4IuJgOFYts=; b=EU6IPyx2GPrrCO DK1aGLyakJ+h6fCGShVfjq1nxAa3DxydaxZEZQa5dMor7FX+HP4p+fidtutZFT2A22IEq2xXdQVSf pmMkQKSYs3AUSMaIWzSHVVgsc+tycHgTMY5VeZJihUy2N6poK11sUb+3Gximkg4lECk18mNkkhY1A oMuRS9ChG5bKJ4bAv/7YVD5RFeER1MFbKVZt4feMCFHbasooJTeWozA2nICLH83RCtcTsReopxeym wMLzrQz/BNuIf9QNaV6JlOk35aaCHmOMy8Co8WL7BZntooB9hU3M8VuAp5HPvljYGU3/HjCOPdd64 c5oKJxVWlk6pQFzhsQVw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1miW2Y-007zrf-30; Thu, 04 Nov 2021 06:15:58 +0000 Received: from szxga08-in.huawei.com ([45.249.212.255]) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1miW2T-007zqT-H6 for linux-arm-kernel@lists.infradead.org; Thu, 04 Nov 2021 06:15:55 +0000 Received: from dggemv703-chm.china.huawei.com (unknown [172.30.72.53]) by szxga08-in.huawei.com (SkyGuard) with ESMTP id 4HlCxt6JVDz1DJ88; Thu, 4 Nov 2021 14:13:38 +0800 (CST) Received: from dggpemm500001.china.huawei.com (7.185.36.107) by dggemv703-chm.china.huawei.com (10.3.19.46) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.15; Thu, 4 Nov 2021 14:15:33 +0800 Received: from localhost.localdomain.localdomain (10.175.113.25) by dggpemm500001.china.huawei.com (7.185.36.107) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2308.15; Thu, 4 Nov 2021 14:15:33 +0800 From: Kefeng Wang To: Catalin Marinas , Will Deacon , Ard Biesheuvel , CC: Kefeng Wang Subject: [PATCH] arm64: mm: Make randomization works again in some case Date: Thu, 4 Nov 2021 14:27:47 +0800 Message-ID: <20211104062747.55206-1-wangkefeng.wang@huawei.com> X-Mailer: git-send-email 2.26.2 MIME-Version: 1.0 X-Originating-IP: [10.175.113.25] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemm500001.china.huawei.com (7.185.36.107) X-CFilter-Loop: Reflected X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20211103_231554_230205_37245A60 X-CRM114-Status: GOOD ( 15.52 ) 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: , Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org After commit 97d6786e0669 ("arm64: mm: account for hotplug memory when randomizing the linear region"), the KASLR could not work well in some case, eg, without memory hotplug and with va=39/pa=44, that is, linear region size < CPU's addressable PA range, the KASLR fails now but could work before this commit. Let's calculate pa range by memblock end/start without CONFIG_RANDOMIZE_BASE. Meanwhile, let's add a warning message if linear region size is too small for randomization. Signed-off-by: Kefeng Wang --- Hi Ard, one more question, the parange from mmfr0 register may also too large, then even with this patch, the randomization still could not work. If we know the max physical memory range(including hotplug memory), could we add a way(maybe cmdline) to set max parange, then we could make randomization works in more cases, any thought? arch/arm64/mm/init.c | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/arch/arm64/mm/init.c b/arch/arm64/mm/init.c index a8834434af99..27ec7f2c6fdb 100644 --- a/arch/arm64/mm/init.c +++ b/arch/arm64/mm/init.c @@ -284,21 +284,33 @@ void __init arm64_memblock_init(void) if (IS_ENABLED(CONFIG_RANDOMIZE_BASE)) { extern u16 memstart_offset_seed; - u64 mmfr0 = read_cpuid(ID_AA64MMFR0_EL1); - int parange = cpuid_feature_extract_unsigned_field( - mmfr0, ID_AA64MMFR0_PARANGE_SHIFT); - s64 range = linear_region_size - - BIT(id_aa64mmfr0_parange_to_phys_shift(parange)); + s64 range; + + if (IS_ENABLED(CONFIG_MEMORY_HOTPLUG)) { + u64 mmfr0 = read_cpuid(ID_AA64MMFR0_EL1); + int parange = cpuid_feature_extract_unsigned_field( + mmfr0, ID_AA64MMFR0_PARANGE_SHIFT); + range = linear_region_size - + BIT(id_aa64mmfr0_parange_to_phys_shift(parange)); + + } else { + range = linear_region_size - + (memblock_end_of_DRAM() - memblock_start_of_DRAM()); + } /* * If the size of the linear region exceeds, by a sufficient * margin, the size of the region that the physical memory can * span, randomize the linear region as well. */ - if (memstart_offset_seed > 0 && range >= (s64)ARM64_MEMSTART_ALIGN) { - range /= ARM64_MEMSTART_ALIGN; - memstart_addr -= ARM64_MEMSTART_ALIGN * - ((range * memstart_offset_seed) >> 16); + if (memstart_offset_seed > 0) { + if (range < (s64)ARM64_MEMSTART_ALIGN) { + pr_warn("linear mappings size is too small for KASLR\n"); + } else { + range /= ARM64_MEMSTART_ALIGN; + memstart_addr -= ARM64_MEMSTART_ALIGN * + ((range * memstart_offset_seed) >> 16); + } } } -- 2.26.2 _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel