LKML Archive on lore.kernel.org
 help / color / Atom feed
From: Masayoshi Mizuma <msys.mizuma@gmail.com>
To: Borislav Petkov <bp@alien8.de>, Baoquan He <bhe@redhat.com>,
	Ingo Molnar <mingo@kernel.org>,
	Thomas Gleixner <tglx@linutronix.de>,
	Chao Fan <fanc.fnst@cn.fujitsu.com>
Cc: linux-kernel@vger.kernel.org, x86@kernel.org,
	linux-efi@vger.kernel.org, linux-acpi@vger.kernel.org,
	mingo@redhat.com, hpa@zytor.com, keescook@chromium.org,
	rjw@rjwysocki.net, lenb@kernel.org, ard.biesheuvel@linaro.org,
	indou.takao@jp.fujitsu.com, caoj.fnst@cn.fujitsu.com
Subject: Re: [PATCH v8 0/3] x86/boot/KASLR: Parse ACPI table and limit kaslr in immovable memory
Date: Tue, 16 Oct 2018 11:13:54 -0400
Message-ID: <20181016151353.punyk7exekut2543@gabell> (raw)
In-Reply-To: <20181015005035.z3xym6nx43hogdge@gabell>

Hi Boris, Baoquan and all,

I have created a RFC patch for adjust KASLR padding size.
This patch is based on Can's v8 patch [1/3], and the Can's patch
will be changed in the future, so this patch is just RFC.

Welcome to any comments and suggestions. Thanks!

---
 arch/x86/boot/compressed/acpitb.c     |  8 ++++++-
 arch/x86/include/uapi/asm/bootparam.h |  3 ++-
 arch/x86/mm/kaslr.c                   | 31 ++++++++++++++++++++++++++-
 3 files changed, 39 insertions(+), 3 deletions(-)

diff --git a/arch/x86/boot/compressed/acpitb.c b/arch/x86/boot/compressed/acpitb.c
index 6b869e3f9780..1504b46f2a04 100644
--- a/arch/x86/boot/compressed/acpitb.c
+++ b/arch/x86/boot/compressed/acpitb.c
@@ -367,6 +367,7 @@ void get_immovable_mem(void)
 	struct acpi_subtable_header *table;
 	struct acpi_srat_mem_affinity *ma;
 	unsigned long table_end;
+	unsigned long long possible_addr, max_possible_addr = 0;
 	int i = 0;
 
 	if (!strstr(args, "movable_node") || strstr(args, "acpi=off"))
@@ -384,7 +385,11 @@ void get_immovable_mem(void)
 	while (((unsigned long)table) + table->length < table_end) {
 		if (table->type == 1) {
 			ma = (struct acpi_srat_mem_affinity *)table;
-			if (!(ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE)) {
+			if (ma->flags & ACPI_SRAT_MEM_HOT_PLUGGABLE) {
+				possible_addr = ma->base_address + ma->length;
+				if (possible_addr > max_possible_addr)
+					max_possible_addr = possible_addr;
+			} else {
 				immovable_mem[i].start = ma->base_address;
 				immovable_mem[i].size = ma->length;
 				i++;
@@ -397,6 +402,7 @@ void get_immovable_mem(void)
 			((unsigned long)table + table->length);
 	}
 	num_immovable_mem = i;
+	boot_params->possible_mem_addr = max_possible_addr;
 }
 #else
 void get_immovable_mem(void)
diff --git a/arch/x86/include/uapi/asm/bootparam.h b/arch/x86/include/uapi/asm/bootparam.h
index a06cbf019744..c987c725e93a 100644
--- a/arch/x86/include/uapi/asm/bootparam.h
+++ b/arch/x86/include/uapi/asm/bootparam.h
@@ -191,7 +191,8 @@ struct boot_params {
 	__u8  _pad7[0x290-0x1f1-sizeof(struct setup_header)];
 	__u32 edd_mbr_sig_buffer[EDD_MBR_SIG_MAX];	/* 0x290 */
 	struct boot_e820_entry e820_table[E820_MAX_ENTRIES_ZEROPAGE]; /* 0x2d0 */
-	__u8  _pad8[48];				/* 0xcd0 */
+	__u8  _pad8[40];				/* 0xcd0 */
+	__u64 possible_mem_addr;			/* 0xcf8 */
 	struct edd_info eddbuf[EDDMAXNR];		/* 0xd00 */
 	__u8  _pad9[276];				/* 0xeec */
 } __attribute__((packed));
diff --git a/arch/x86/mm/kaslr.c b/arch/x86/mm/kaslr.c
index 61db77b0eda9..8c5aca792b51 100644
--- a/arch/x86/mm/kaslr.c
+++ b/arch/x86/mm/kaslr.c
@@ -69,6 +69,35 @@ static inline bool kaslr_memory_enabled(void)
 	return kaslr_enabled() && !IS_ENABLED(CONFIG_KASAN);
 }
 
+#ifdef CONFIG_MEMORY_HOTPLUG
+static unsigned int __init kaslr_padding(void)
+{
+	unsigned long long max_possible_phys, max_actual_phys, threshold;
+	unsigned int rand_mem_physical_padding =
+				CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING;
+
+	if (!boot_params.possible_mem_addr)
+		goto out;
+
+	max_actual_phys = roundup(PFN_PHYS(max_pfn), 1ULL << TB_SHIFT);
+	max_possible_phys = roundup(boot_params.possible_mem_addr,
+					1ULL << TB_SHIFT);
+	threshold = max_actual_phys +
+		((unsigned long long)rand_mem_physical_padding << TB_SHIFT);
+
+	if (max_possible_phys > threshold)
+		rand_mem_physical_padding =
+			(max_possible_phys - max_actual_phys) >> TB_SHIFT;
+out:
+	return rand_mem_physical_padding;
+}
+#else
+static unsigned int __init kaslr_padding(void)
+{
+	return CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING;
+}
+#endif
+
 /* Initialize base and padding for each memory region randomized with KASLR */
 void __init kernel_randomize_memory(void)
 {
@@ -102,7 +131,7 @@ void __init kernel_randomize_memory(void)
 	 */
 	BUG_ON(kaslr_regions[0].base != &page_offset_base);
 	memory_tb = DIV_ROUND_UP(max_pfn << PAGE_SHIFT, 1UL << TB_SHIFT) +
-		CONFIG_RANDOMIZE_MEMORY_PHYSICAL_PADDING;
+		kaslr_padding();
 
 	/* Adapt phyiscal memory region size based on available memory */
 	if (memory_tb < kaslr_regions[0].size_tb)
-- 
2.18.0

- Masa

  reply index

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-10-10  8:41 Chao Fan
2018-10-10  8:41 ` [PATCH v8 1/3] x86/boot: Add acpitb.c to parse acpi tables Chao Fan
2018-10-11 10:57   ` Borislav Petkov
2018-10-12  1:56     ` Chao Fan
2018-10-12  9:36     ` Chao Fan
2018-10-12  9:46       ` Borislav Petkov
2018-10-12 10:03         ` Chao Fan
2018-10-16  2:48     ` Chao Fan
2018-10-16 12:40       ` Borislav Petkov
2018-10-17  1:10         ` Chao Fan
2018-10-15 20:26   ` Masayoshi Mizuma
2018-10-16  1:50     ` Chao Fan
2018-10-10  8:41 ` [PATCH v8 2/3] x86/boot/KASLR: Walk srat tables to filter immovable memory Chao Fan
2018-10-10  8:41 ` [PATCH v8 3/3] x86/boot/KASLR: Limit kaslr to choosing the " Chao Fan
2018-10-10  8:59 ` [PATCH v8 0/3] x86/boot/KASLR: Parse ACPI table and limit kaslr in " Borislav Petkov
2018-10-10  9:06   ` Baoquan He
2018-10-10  9:12     ` Chao Fan
2018-10-10  9:21       ` Baoquan He
2018-10-10  9:14     ` Thomas Gleixner
2018-10-10  9:19       ` Borislav Petkov
2018-10-10  9:30         ` Baoquan He
2018-10-10 19:44           ` Masayoshi Mizuma
2018-10-11  0:29             ` Baoquan He
2018-10-11  5:51               ` Chao Fan
2018-10-13 20:19                 ` Masayoshi Mizuma
2018-10-13 20:34                   ` Borislav Petkov
2018-10-13 21:45                     ` Masayoshi Mizuma
2018-10-13 22:05                       ` Borislav Petkov
2018-10-15  0:50                         ` Masayoshi Mizuma
2018-10-16 15:13                           ` Masayoshi Mizuma [this message]
2018-10-16 19:11                             ` Borislav Petkov
2018-10-16 19:54                               ` Masayoshi Mizuma
2018-10-16 19:59                                 ` Borislav Petkov
2018-10-22 15:42                                   ` Masayoshi Mizuma
2018-10-23  2:48                                     ` Chao Fan
2018-10-24 19:21                                       ` Masayoshi Mizuma
2018-10-25  1:22                                         ` Chao Fan
2018-10-25 10:33                                     ` Borislav Petkov
2018-10-25 13:40                                       ` Masayoshi Mizuma
2018-11-06 12:10                                         ` Borislav Petkov
2018-11-06 14:07                                           ` Baoquan He
2018-11-07  1:21                                             ` Chao Fan
2018-11-06 18:45                                         ` Borislav Petkov
2018-11-06 19:36                                           ` Masayoshi Mizuma
2018-11-06 20:45                                             ` Borislav Petkov
2018-11-06 22:21                                               ` Masayoshi Mizuma
2018-11-08 10:51                                                 ` Borislav Petkov
2018-11-10 10:54                                                   ` Borislav Petkov
2018-11-11 13:45                                                     ` Masayoshi Mizuma
2019-02-05 15:05                                                       ` Masayoshi Mizuma
2019-02-08 18:26                                                         ` Borislav Petkov
2019-02-09  0:24                                                           ` Masayoshi Mizuma
2019-02-11  1:46                                                         ` Chao Fan
2019-02-11 20:11                                                           ` Masayoshi Mizuma
2018-10-10 17:16 ` Borislav Petkov
2018-10-11  1:30   ` Chao Fan

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181016151353.punyk7exekut2543@gabell \
    --to=msys.mizuma@gmail.com \
    --cc=ard.biesheuvel@linaro.org \
    --cc=bhe@redhat.com \
    --cc=bp@alien8.de \
    --cc=caoj.fnst@cn.fujitsu.com \
    --cc=fanc.fnst@cn.fujitsu.com \
    --cc=hpa@zytor.com \
    --cc=indou.takao@jp.fujitsu.com \
    --cc=keescook@chromium.org \
    --cc=lenb@kernel.org \
    --cc=linux-acpi@vger.kernel.org \
    --cc=linux-efi@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@kernel.org \
    --cc=mingo@redhat.com \
    --cc=rjw@rjwysocki.net \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

LKML Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/lkml/0 lkml/git/0.git
	git clone --mirror https://lore.kernel.org/lkml/1 lkml/git/1.git
	git clone --mirror https://lore.kernel.org/lkml/2 lkml/git/2.git
	git clone --mirror https://lore.kernel.org/lkml/3 lkml/git/3.git
	git clone --mirror https://lore.kernel.org/lkml/4 lkml/git/4.git
	git clone --mirror https://lore.kernel.org/lkml/5 lkml/git/5.git
	git clone --mirror https://lore.kernel.org/lkml/6 lkml/git/6.git
	git clone --mirror https://lore.kernel.org/lkml/7 lkml/git/7.git
	git clone --mirror https://lore.kernel.org/lkml/8 lkml/git/8.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 lkml lkml/ https://lore.kernel.org/lkml \
		linux-kernel@vger.kernel.org
	public-inbox-index lkml

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-kernel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git