The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Chao Fan <fanc.fnst@cn.fujitsu.com>
To: Baoquan He <bhe@redhat.com>
Cc: Kees Cook <keescook@chromium.org>,
	LKML <linux-kernel@vger.kernel.org>, X86 ML <x86@kernel.org>,
	"H. Peter Anvin" <hpa@zytor.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, <yasu.isimatu@gmail.com>,
	<indou.takao@jp.fujitsu.com>, <caoj.fnst@cn.fujitsu.com>,
	Dou Liyang <douly.fnst@cn.fujitsu.com>
Subject: Re: [PATCH v3 2/4] kaslr: calculate the memory region in immovable node
Date: Wed, 6 Dec 2017 18:02:38 +0800	[thread overview]
Message-ID: <20171206100238.GB28884@localhost.localdomain> (raw)
In-Reply-To: <20171206092800.GK15074@x1>

On Wed, Dec 06, 2017 at 05:28:00PM +0800, Baoquan He wrote:
>On 12/05/17 at 11:40am, Kees Cook wrote:
>> On Tue, Dec 5, 2017 at 12:51 AM, Chao Fan <fanc.fnst@cn.fujitsu.com> wrote:
>> > If there is no immovable memory region specified, go on the old code.
>> > There are several conditons:
>> > 1. CONFIG_MEMORY_HOTPLUG is not specified to y.
>> > 2. immovable_mem= is not specified.
>> >
>> > Otherwise, calculate the intersecting between memmap entry and
>> > immovable memory.
>> 
>> Instead of copy/pasting code between process_efi_entries() and
>> process_e820_entries(), I'd rather that process_mem_region() is
>> modified to deal with immovable regions.
>
>If put it into process_mem_region(), one level of loop is added. How

Yes, one new loop will add ahead of the while() in process_mem_region
then the code may look like:

@@ -509,6 +555,24 @@ static void process_mem_region(struct mem_vector *entry,
        region.start = cur_entry.start;
        region.size = cur_entry.size;

+#ifdef CONFIG_MEMORY_HOTPLUG
+next:
+       if (num_immovable_mem > 0) {
+               unsigned long long start, reg_end;
+
+               if (!mem_overlaps(&entry, &immovable_mem[i]))
+                       goto out;
+
+               start = immovable_mem[i].start;
+               end = start + immovable_mem[i].size;
+
+               region.start = clamp(cur_entry.start, start, end);
+               reg_end = clamp(cur_entry.start + cur_entry.size, start, end);
+
+               region.size = region_end - region.start;
+       }
+#endif
+
        /* Give up if slot area array is full. */
        while (slot_area_index < MAX_SLOT_AREA) {
                start_orig = region.start;
@@ -522,7 +586,7 @@ static void process_mem_region(struct mem_vector *entry,

                /* Did we raise the address above the passed in memory entry? */
                if (region.start > cur_entry.start + cur_entry.size)
-                       return;
+                       goto out;

                /* Reduce size by any delta from the original address. */
                region.size -= region.start - start_orig;
@@ -534,12 +598,12 @@ static void process_mem_region(struct mem_vector *entry,

                /* Return if region can't contain decompressed kernel */
                if (region.size < image_size)
-                       return;
+                       goto out;

                /* If nothing overlaps, store the region and return. */
                if (!mem_avoid_overlap(&region, &overlap)) {
                        store_slot_info(&region, image_size);
-                       return;
+                       goto out;
                }

                /* Store beginning of region if holds at least image_size. */
		@@ -553,12 +617,20 @@ static void process_mem_region(struct mem_vector *entry,

                /* Return if overlap extends to or past end of region. */
                if (overlap.start + overlap.size >= region.start + region.size)
-                       return;
+                       goto out;

                /* Clip off the overlapping region and start over. */
                region.size -= overlap.start - region.start + overlap.size;
                region.start = overlap.start + overlap.size;
        }
+
+out:
+#ifdef CONFIG_MEMORY_HOTPLUG
+       i++;
+       if (i < num_immovable_mem)
+               goto next;
+#endif
+       return;
 }

>about changing it like below. If no immovable_mem, just process the
>region in process_immovable_mem(). This we don't need to touch
>process_mem_region().

Yes, Baoquan's method will make all change be in one function.
Kees, how do you think, which is better?

Thanks,
Chao Fan

>
>
>From 9ae3f5ab0e2f129757495af2412bd52dcf86aa46 Mon Sep 17 00:00:00 2001
>From: Baoquan He <bhe@redhat.com>
>Date: Wed, 6 Dec 2017 17:24:55 +0800
>Subject: [PATCH] KASLR: change code
>
>Signed-off-by: Baoquan He <bhe@redhat.com>
>---
> arch/x86/boot/compressed/kaslr.c | 32 ++++++++++++++------------------
> 1 file changed, 14 insertions(+), 18 deletions(-)
>
>diff --git a/arch/x86/boot/compressed/kaslr.c b/arch/x86/boot/compressed/kaslr.c
>index 13d26b859c69..73b1562a7439 100644
>--- a/arch/x86/boot/compressed/kaslr.c
>+++ b/arch/x86/boot/compressed/kaslr.c
>@@ -638,13 +638,23 @@ static bool process_immovable_mem(struct mem_vector region,
> 				  unsigned long long minimum,
> 				  unsigned long long image_size)
> {
>-	int i;
>+	/* If no immovable_mem stored, use region directly */
>+	if (num_immovable_region == 0) {
>+		process_mem_region(&entry, minimum, image_size);
>+
>+		if (slot_area_index == MAX_SLOT_AREA) {
>+			debug_putstr("Aborted memmap scan (slot_areas full)!\n");
>+			return 1;
>+		}
>+
>+		return 0;
>+	}
> 
> 	/*
> 	 * Walk all immovable regions, and filter the intersection
> 	 * to process_mem_region.
> 	 */
>-	for (i = 0; i < num_immovable_region; i++) {
>+	for (int i = 0; i < num_immovable_region; i++) {
> 		struct mem_vector entry;
> 		unsigned long long start, end, entry_end;
> 
>@@ -738,14 +748,7 @@ process_efi_entries(unsigned long minimum, unsigned long image_size)
> 		region.start = md->phys_addr;
> 		region.size = md->num_pages << EFI_PAGE_SHIFT;
> 
>-		/* If no immovable_mem stored, use region directly */
>-		if (num_immovable_region == 0) {
>-			process_mem_region(&region, minimum, image_size);
>-			if (slot_area_index == MAX_SLOT_AREA) {
>-				debug_putstr("Aborted memmap scan (slot_areas full)!\n");
>-				break;
>-			}
>-		} else if (process_immovable_mem(region, minimum, image_size))
>+		if (process_immovable_mem(region, minimum, image_size))
> 			break;
> 	}
> 	return true;
>@@ -774,14 +777,7 @@ static void process_e820_entries(unsigned long minimum,
> 		region.start = entry->addr;
> 		region.size = entry->size;
> 
>-		/* If no immovable_mem stored, use region directly */
>-		if (num_immovable_region == 0) {
>-			process_mem_region(&region, minimum, image_size);
>-			if (slot_area_index == MAX_SLOT_AREA) {
>-				debug_putstr("Aborted memmap scan (slot_areas full)!\n");
>-				break;
>-			}
>-		} else if (process_immovable_mem(region, minimum, image_size))
>+		if (process_immovable_mem(region, minimum, image_size))
> 			break;
> 	}
> }
>-- 
>2.5.5
>
>
>

  reply	other threads:[~2017-12-06 10:03 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-12-05  8:51 [PATCH v3 0/4] kaslr: add new parameter immovable_mem=nn[KMG]@ss[KMG] to make memory hotplug work well with kaslr Chao Fan
2017-12-05  8:51 ` [PATCH v3 1/4] kaslr: add immovable_mem=nn[KMG]@ss[KMG] to specify extracting memory Chao Fan
2017-12-05 19:42   ` Kees Cook
2017-12-06  1:13     ` Chao Fan
2017-12-06  9:35   ` Baoquan He
2017-12-07  2:53     ` Chao Fan
2017-12-07  3:09       ` Baoquan He
2017-12-07  3:56         ` Chao Fan
2017-12-07  4:16           ` Dou Liyang
2017-12-07  4:58             ` Baoquan He
2017-12-07  5:31               ` Chao Fan
2017-12-05  8:51 ` [PATCH v3 2/4] kaslr: calculate the memory region in immovable node Chao Fan
2017-12-05 19:40   ` Kees Cook
2017-12-06  9:28     ` Baoquan He
2017-12-06 10:02       ` Chao Fan [this message]
2017-12-07  0:11         ` Kees Cook
2017-12-07  1:09           ` Chao Fan
2017-12-05  8:51 ` [PATCH v3 3/4] kaslr: disable memory mirror feature when movable_node specified Chao Fan
2017-12-05  8:52 ` [PATCH v3 4/4] document: change the document for immovable_mem Chao Fan
2017-12-05 17:36   ` Randy Dunlap
2017-12-06  1:16     ` 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=20171206100238.GB28884@localhost.localdomain \
    --to=fanc.fnst@cn.fujitsu.com \
    --cc=bhe@redhat.com \
    --cc=caoj.fnst@cn.fujitsu.com \
    --cc=douly.fnst@cn.fujitsu.com \
    --cc=hpa@zytor.com \
    --cc=indou.takao@jp.fujitsu.com \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    --cc=yasu.isimatu@gmail.com \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox