All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stanislav Kinsburskii <skinsburskii@linux.microsoft.com>
To: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de,
	dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com,
	ebiederm@xmission.com, akpm@linux-foundation.org,
	stanislav.kinsburskii@gmail.com, corbet@lwn.net,
	linux-kernel@vger.kernel.org, kexec@lists.infradead.org,
	linux-mm@kvack.org, kys@microsoft.com, jgowans@amazon.com,
	wei.liu@kernel.org, arnd@arndb.de, gregkh@linuxfoundation.org,
	graf@amazon.de, pbonzini@redhat.com, bhe@redhat.com,
	dave.hansen@intel.com, kirill.shutemov@intel.com
Subject: [RFC PATCH v3 3/3] pmpool: Mark reserved range as "kernel reserved" in kexec e820 table
Date: Wed, 04 Oct 2023 15:23:27 -0700	[thread overview]
Message-ID: <169645820784.11424.922048380402402210.stgit@skinsburskii.> (raw)
In-Reply-To: <169645773092.11424.7258549771090599226.stgit@skinsburskii.>

Update the logic to classify the persistent memory pool in the kexec e820
table as "kernel reserved" when its corresponding e820 region type is
"System RAM". Restore the pool when its type is "kernel reserved". This
ensures the persistence of the memory pool across kexec operations.

Signed-off-by: Stanislav Kinsburskii <skinsburskii@linux.microsoft.com>
---
 mm/pmpool.c |   50 +++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 47 insertions(+), 3 deletions(-)

diff --git a/mm/pmpool.c b/mm/pmpool.c
index c74f09b99283..1e3a2dffc5d3 100644
--- a/mm/pmpool.c
+++ b/mm/pmpool.c
@@ -11,11 +11,14 @@
 #include <linux/mm.h>
 #include <linux/pmpool.h>
 
+#include <asm/e820/api.h>
+
 #include "cma.h"
 
 struct pmpool {
 	struct resource resource;
 	struct cma *cma;
+	bool exists;
 };
 
 static struct pmpool *default_pmpool;
@@ -50,6 +53,18 @@ static void pmpool_cma_accomodate_bitmap(struct cma *cma)
 	pr_info("CMA bitmap moved to %#llx\n", virt_to_phys(cma->bitmap));
 }
 
+static void pmpool_cma_restore_bitmap(struct cma *cma)
+{
+	u64 base;
+
+	base = PFN_PHYS(cma->base_pfn);
+
+	bitmap_free(cma->bitmap);
+	cma->bitmap = phys_to_virt(base);
+
+	pr_info("CMA bitmap restored to %#llx\n", base);
+}
+
 static int __init default_pmpool_fixup(void)
 {
 	if (!default_pmpool)
@@ -58,7 +73,11 @@ static int __init default_pmpool_fixup(void)
 	if (insert_resource(&iomem_resource, &default_pmpool->resource))
 		pr_err("failed to insert resource\n");
 
-	pmpool_cma_accomodate_bitmap(default_pmpool->cma);
+	if (default_pmpool->exists)
+		pmpool_cma_restore_bitmap(default_pmpool->cma);
+	else
+		pmpool_cma_accomodate_bitmap(default_pmpool->cma);
+
 	return 0;
 }
 postcore_initcall(default_pmpool_fixup);
@@ -73,7 +92,7 @@ static int __init parse_pmpool_opt(char *str)
 		}
 	};
 	phys_addr_t base, size, end;
-	int err;
+	int err, e820_type;
 
 	/* Format is pmpool=<base>,<size> */
 	base = memparse(str, &str);
@@ -92,10 +111,33 @@ static int __init parse_pmpool_opt(char *str)
 		return 0;
 	}
 
+	e820_type = e820__get_entry_type(base, end);
+	switch (e820_type) {
+	case E820_TYPE_RAM:
+		e820__range_update_kexec(base, size, E820_TYPE_RAM,
+					 E820_TYPE_RESERVED_KERN);
+		e820__update_table_kexec();
+		break;
+	case E820_TYPE_RESERVED_KERN:
+		/*
+		 * TODO: there are several assumptions here:
+		 *   1. That the kernel reserved region represents pmpool,
+		 *   2. That the region had the same base and size and
+		 *   3. That the region was properly initialized.
+		 * All these assumptions aren't valid in general case and this
+		 * should be addressed.
+		 */
+		pmpool.exists = true;
+		break;
+	default:
+		pr_err("unsupported e820 type: %d\n", e820_type);
+		goto free_memblock;
+	}
+
 	err = cma_init_reserved_mem(base, size, 0, "pmpool", &pmpool.cma);
 	if (err) {
 		pr_err("failed to initialize CMA: %d\n", err);
-		goto free_memblock;
+		goto remove_e820_kexec_range;
 	}
 
 	pmpool.resource.start = base;
@@ -108,6 +150,8 @@ static int __init parse_pmpool_opt(char *str)
 
 	return 0;
 
+remove_e820_kexec_range:
+	e820__range_remove_kexec(base, size, E820_TYPE_RESERVED_KERN, 1);
 free_memblock:
 	memblock_phys_free(base, size);
 	return 0;




WARNING: multiple messages have this Message-ID (diff)
From: Stanislav Kinsburskii <skinsburskii@linux.microsoft.com>
To: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de,
	dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com,
	ebiederm@xmission.com, akpm@linux-foundation.org,
	stanislav.kinsburskii@gmail.com, corbet@lwn.net,
	linux-kernel@vger.kernel.org, kexec@lists.infradead.org,
	linux-mm@kvack.org, kys@microsoft.com, jgowans@amazon.com,
	wei.liu@kernel.org, arnd@arndb.de, gregkh@linuxfoundation.org,
	graf@amazon.de, pbonzini@redhat.com, bhe@redhat.com,
	dave.hansen@intel.com, kirill.shutemov@intel.com
Subject: [RFC PATCH v3 3/3] pmpool: Mark reserved range as "kernel reserved" in kexec e820 table
Date: Wed, 04 Oct 2023 15:23:27 -0700	[thread overview]
Message-ID: <169645820784.11424.922048380402402210.stgit@skinsburskii.> (raw)
In-Reply-To: <169645773092.11424.7258549771090599226.stgit@skinsburskii.>

Update the logic to classify the persistent memory pool in the kexec e820
table as "kernel reserved" when its corresponding e820 region type is
"System RAM". Restore the pool when its type is "kernel reserved". This
ensures the persistence of the memory pool across kexec operations.

Signed-off-by: Stanislav Kinsburskii <skinsburskii@linux.microsoft.com>
---
 mm/pmpool.c |   50 +++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 47 insertions(+), 3 deletions(-)

diff --git a/mm/pmpool.c b/mm/pmpool.c
index c74f09b99283..1e3a2dffc5d3 100644
--- a/mm/pmpool.c
+++ b/mm/pmpool.c
@@ -11,11 +11,14 @@
 #include <linux/mm.h>
 #include <linux/pmpool.h>
 
+#include <asm/e820/api.h>
+
 #include "cma.h"
 
 struct pmpool {
 	struct resource resource;
 	struct cma *cma;
+	bool exists;
 };
 
 static struct pmpool *default_pmpool;
@@ -50,6 +53,18 @@ static void pmpool_cma_accomodate_bitmap(struct cma *cma)
 	pr_info("CMA bitmap moved to %#llx\n", virt_to_phys(cma->bitmap));
 }
 
+static void pmpool_cma_restore_bitmap(struct cma *cma)
+{
+	u64 base;
+
+	base = PFN_PHYS(cma->base_pfn);
+
+	bitmap_free(cma->bitmap);
+	cma->bitmap = phys_to_virt(base);
+
+	pr_info("CMA bitmap restored to %#llx\n", base);
+}
+
 static int __init default_pmpool_fixup(void)
 {
 	if (!default_pmpool)
@@ -58,7 +73,11 @@ static int __init default_pmpool_fixup(void)
 	if (insert_resource(&iomem_resource, &default_pmpool->resource))
 		pr_err("failed to insert resource\n");
 
-	pmpool_cma_accomodate_bitmap(default_pmpool->cma);
+	if (default_pmpool->exists)
+		pmpool_cma_restore_bitmap(default_pmpool->cma);
+	else
+		pmpool_cma_accomodate_bitmap(default_pmpool->cma);
+
 	return 0;
 }
 postcore_initcall(default_pmpool_fixup);
@@ -73,7 +92,7 @@ static int __init parse_pmpool_opt(char *str)
 		}
 	};
 	phys_addr_t base, size, end;
-	int err;
+	int err, e820_type;
 
 	/* Format is pmpool=<base>,<size> */
 	base = memparse(str, &str);
@@ -92,10 +111,33 @@ static int __init parse_pmpool_opt(char *str)
 		return 0;
 	}
 
+	e820_type = e820__get_entry_type(base, end);
+	switch (e820_type) {
+	case E820_TYPE_RAM:
+		e820__range_update_kexec(base, size, E820_TYPE_RAM,
+					 E820_TYPE_RESERVED_KERN);
+		e820__update_table_kexec();
+		break;
+	case E820_TYPE_RESERVED_KERN:
+		/*
+		 * TODO: there are several assumptions here:
+		 *   1. That the kernel reserved region represents pmpool,
+		 *   2. That the region had the same base and size and
+		 *   3. That the region was properly initialized.
+		 * All these assumptions aren't valid in general case and this
+		 * should be addressed.
+		 */
+		pmpool.exists = true;
+		break;
+	default:
+		pr_err("unsupported e820 type: %d\n", e820_type);
+		goto free_memblock;
+	}
+
 	err = cma_init_reserved_mem(base, size, 0, "pmpool", &pmpool.cma);
 	if (err) {
 		pr_err("failed to initialize CMA: %d\n", err);
-		goto free_memblock;
+		goto remove_e820_kexec_range;
 	}
 
 	pmpool.resource.start = base;
@@ -108,6 +150,8 @@ static int __init parse_pmpool_opt(char *str)
 
 	return 0;
 
+remove_e820_kexec_range:
+	e820__range_remove_kexec(base, size, E820_TYPE_RESERVED_KERN, 1);
 free_memblock:
 	memblock_phys_free(base, size);
 	return 0;



_______________________________________________
kexec mailing list
kexec@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec

  parent reply	other threads:[~2023-10-04 22:23 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-04 22:23 [RFC PATCH v3 0/3] Introduce persistent memory pool Stanislav Kinsburskii
2023-10-04 22:23 ` Stanislav Kinsburskii
2023-10-04 22:23 ` [RFC PATCH v3 1/3] x86/boot/e820: Expose kexec range update, remove and table update functions Stanislav Kinsburskii
2023-10-04 22:23   ` Stanislav Kinsburskii
2023-10-05  0:51   ` kernel test robot
2023-10-05 10:10   ` kernel test robot
2023-10-05 13:52   ` kernel test robot
2023-10-04 22:23 ` [RFC PATCH v3 2/3] pmpool: Introduce persistent memory pool Stanislav Kinsburskii
2023-10-04 22:23   ` Stanislav Kinsburskii
2023-10-04 22:23 ` Stanislav Kinsburskii [this message]
2023-10-04 22:23   ` [RFC PATCH v3 3/3] pmpool: Mark reserved range as "kernel reserved" in kexec e820 table Stanislav Kinsburskii
2023-10-05 15:58   ` kernel test robot
2023-11-06 15:04   ` kernel test robot

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=169645820784.11424.922048380402402210.stgit@skinsburskii. \
    --to=skinsburskii@linux.microsoft.com \
    --cc=akpm@linux-foundation.org \
    --cc=arnd@arndb.de \
    --cc=bhe@redhat.com \
    --cc=bp@alien8.de \
    --cc=corbet@lwn.net \
    --cc=dave.hansen@intel.com \
    --cc=dave.hansen@linux.intel.com \
    --cc=ebiederm@xmission.com \
    --cc=graf@amazon.de \
    --cc=gregkh@linuxfoundation.org \
    --cc=hpa@zytor.com \
    --cc=jgowans@amazon.com \
    --cc=kexec@lists.infradead.org \
    --cc=kirill.shutemov@intel.com \
    --cc=kys@microsoft.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mingo@redhat.com \
    --cc=pbonzini@redhat.com \
    --cc=stanislav.kinsburskii@gmail.com \
    --cc=tglx@linutronix.de \
    --cc=wei.liu@kernel.org \
    --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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.