linux-arm-kernel.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
From: linux@armlinux.org.uk (Russell King - ARM Linux)
To: linux-arm-kernel@lists.infradead.org
Subject: [PATCH kexec-tools 12/32] kexec: add helper to exlude a region from a set of memory ranges
Date: Thu, 26 May 2016 09:56:38 +0100	[thread overview]
Message-ID: <20160526085638.GF19428@n2100.arm.linux.org.uk> (raw)
In-Reply-To: <CAHB_Guo_ozXhAnkGk_NrdBME2fSYDoKTHwu3kLBvsrmbwxOdww@mail.gmail.com>

On Wed, May 25, 2016 at 01:30:44PM +0530, Pratyush Anand wrote:
> On Tue, May 3, 2016 at 3:52 PM, Russell King <rmk@arm.linux.org.uk> wrote:
> > +int mem_regions_exclude(struct memory_ranges *ranges,
> > +                       const struct memory_range *range)
> > +{
> > +       int i;
> > +
> > +       for (i = 0; i < ranges->size; i++) {
> > +               struct memory_range *r = ranges->ranges + i;
> > +
> > +               /*
> > +                * We assume that crash area is fully contained in
> > +                * some larger memory area.
> > +                */
> > +               if (r->start <= range->start && r->end >= range->end) {
> > +                       if (r->start == range->start) {
> > +                               /* Shrink the start of this memory range */
> > +                               r->start = range->end + 1;
> > +                       } else if (r->end == range->end) {
> > +                               /* Shrink the end of this memory range */
> > +                               r->end = range->start - 1;
> 
> What if r->start == range->start) && (r->end == range->end)?

I suppose that could happen, so what about this patch on top (untested).
This isn't something I'd be able to test on real hardware, it needs a
separate test-suite to be written to do modular testing...

 kexec/mem_regions.c | 94 +++++++++++++++++++++++++++++++----------------------
 1 file changed, 55 insertions(+), 39 deletions(-)

diff --git a/kexec/mem_regions.c b/kexec/mem_regions.c
index c6ba942..4e7061b 100644
--- a/kexec/mem_regions.c
+++ b/kexec/mem_regions.c
@@ -29,6 +29,50 @@ void mem_regions_sort(struct memory_ranges *ranges)
 }
 
 /**
+ * mem_regions_add() - add a memory region to a set of ranges
+ * @ranges: ranges to add the memory region to
+ * @max: maximum number of entries in memory region
+ * @base: base address of memory region
+ * @length: length of memory region in bytes
+ * @type: type of memory region
+ *
+ * Add the memory region to the set of ranges, and return %0 if successful,
+ * or %-1 if we ran out of space.
+ */
+int mem_regions_add(struct memory_ranges *ranges, unsigned long long base,
+                    unsigned long long length, int type)
+{
+	struct memory_range *range;
+
+	if (ranges->size >= ranges->max_size)
+		return -1;
+
+	range = ranges->ranges + ranges->size++;
+	range->start = base;
+	range->end = base + length - 1;
+	range->type = type;
+
+	return 0;
+}
+
+static void mem_regions_remove(struct memory_ranges *ranges, int index)
+{
+	int tail_entries;
+
+	/* we are assured to have at least one entry */
+	ranges->size -= 1;
+
+	/* if we have following entries, shuffle them down one place */
+	tail_entries = ranges->size - index;
+	if (tail_entries)
+		memmove(ranges->ranges + index, ranges->ranges + index + 1,
+			tail_entries * sizeof(*ranges->ranges));
+
+	/* zero the new tail entry */
+	memset(ranges->ranges + ranges->size, 0, sizeof(*ranges->ranges));
+}
+
+/**
  * mem_regions_exclude() - excludes a memory region from a set of memory ranges
  * @ranges: memory ranges to exclude the region from
  * @range: memory range to exclude
@@ -40,7 +84,7 @@ void mem_regions_sort(struct memory_ranges *ranges)
 int mem_regions_exclude(struct memory_ranges *ranges,
 			const struct memory_range *range)
 {
-	int i;
+	int i, ret;
 
 	for (i = 0; i < ranges->size; i++) {
 		struct memory_range *r = ranges->ranges + i;
@@ -51,25 +95,25 @@ int mem_regions_exclude(struct memory_ranges *ranges,
 		 */
 		if (r->start <= range->start && r->end >= range->end) {
 			if (r->start == range->start) {
-				/* Shrink the start of this memory range */
-				r->start = range->end + 1;
+				if (r->end == range->end)
+					/* Remove this entry */
+					mem_regions_remove(ranges, i);
+				else
+					/* Shrink the start of this memory range */
+					r->start = range->end + 1;
 			} else if (r->end == range->end) {
 				/* Shrink the end of this memory range */
 				r->end = range->start - 1;
 			} else {
-				struct memory_range *new;
-
 				/*
 				 * Split this area into 2 smaller ones and
 				 * remove excluded range from between. First
 				 * create new entry for the remaining area.
 				 */
-				if (ranges->size >= ranges->max_size)
-					return -1;
-
-				new = ranges->ranges + ranges->size++;
-				new->start = range->end + 1;
-				new->end = r->end;
+				ret = mem_regions_add(ranges, range->end + 1,
+						      r->end, 0);
+				if (ret < 0)
+					return ret;
 
 				/*
 				 * Update this area to end before excluded
@@ -82,31 +126,3 @@ int mem_regions_exclude(struct memory_ranges *ranges,
 	}
 	return 0;
 }
-
-/**
- * mem_regions_add() - add a memory region to a set of ranges
- * @ranges: ranges to add the memory region to
- * @max: maximum number of entries in memory region
- * @base: base address of memory region
- * @length: length of memory region in bytes
- * @type: type of memory region
- *
- * Add the memory region to the set of ranges, and return %0 if successful,
- * or %-1 if we ran out of space.
- */
-int mem_regions_add(struct memory_ranges *ranges, unsigned long long base,
-                    unsigned long long length, int type)
-{
-	struct memory_range *range;
-
-	if (ranges->size >= ranges->max_size)
-		return -1;
-
-	range = ranges->ranges + ranges->size++;
-	range->start = base;
-	range->end = base + length - 1;
-	range->type = type;
-
-	return 0;
-}
-


-- 
RMK's Patch system: http://www.armlinux.org.uk/developer/patches/
FTTC broadband for 0.8mile line: currently at 9.6Mbps down 400kbps up
according to speedtest.net.

  reply	other threads:[~2016-05-26  8:56 UTC|newest]

Thread overview: 121+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-04-28  9:26 [PATCH 00/12] Fixing TI Keystone2 kexec Russell King - ARM Linux
2016-04-28  9:27 ` [PATCH 01/12] ARM: kexec: fix crashkernel= handling Russell King
2016-04-29 14:17   ` Pratyush Anand
2016-04-28  9:27 ` [PATCH 02/12] ARM: provide improved virt_to_idmap() functionality Russell King
2016-04-28  9:27 ` [PATCH 03/12] ARM: kexec: remove 512MB restriction on kexec crashdump Russell King
2016-04-29 14:19   ` Pratyush Anand
2016-04-29 18:10     ` Russell King - ARM Linux
2016-04-30  3:36       ` Pratyush Anand
2016-04-30  8:25         ` Russell King - ARM Linux
2016-04-28  9:28 ` [PATCH 04/12] ARM: provide arm_has_idmap_alias() helper Russell King
2016-04-29 14:21   ` Pratyush Anand
2016-04-28  9:28 ` [PATCH 05/12] ARM: kdump: advertise boot aliased crash kernel resource Russell King
2016-04-28  9:28 ` [PATCH 06/12] ARM: kexec: advertise location of bootable RAM Russell King
2016-04-29 14:56   ` Pratyush Anand
2016-04-29 18:00     ` Russell King - ARM Linux
2016-04-30  3:27       ` Pratyush Anand
2016-04-30  8:20         ` Russell King - ARM Linux
2016-05-02  7:34           ` Pratyush Anand
2016-05-02 10:10             ` Russell King - ARM Linux
2016-05-02 10:48               ` Pratyush Anand
2016-05-03 10:21     ` [PATCH kexec-tools 01/32] kdump: mmap() and munmap() only work on page-aligned quantites Russell King
2016-05-25  6:16       ` Pratyush Anand
2016-05-26  8:35         ` Russell King - ARM Linux
2016-05-03 10:21     ` [PATCH kexec-tools 02/32] kdump: fix multiple program header entries Russell King
2016-05-25  6:16       ` Pratyush Anand
2016-05-03 10:21     ` [PATCH kexec-tools 03/32] kdump: actually write out the memory Russell King
2016-05-25  6:16       ` Pratyush Anand
2016-05-03 10:21     ` [PATCH kexec-tools 04/32] kdump: fix kdump mapping Russell King
2016-05-25  6:17       ` Pratyush Anand
2016-05-26 14:33         ` Russell King - ARM Linux
2016-05-03 10:21     ` [PATCH kexec-tools 05/32] arm: fix kdump to work on LPAE systems Russell King
2016-05-28 11:28       ` Baoquan He
2016-05-03 10:21     ` [PATCH kexec-tools 06/32] kdump: print mmap() offset in hex Russell King
2016-05-25  6:21       ` Pratyush Anand
2016-05-03 10:21     ` [PATCH kexec-tools 07/32] kexec: fix warnings caused by selecting 64-bit file IO on 32-bit platforms Russell King
2016-05-28 11:33       ` Baoquan He
2016-05-28 13:53         ` Russell King - ARM Linux
2016-05-29 13:29           ` Baoquan He
2016-05-03 10:21     ` [PATCH kexec-tools 08/32] kexec: add max_size to memory_ranges Russell King
2016-05-27 11:43       ` Pratyush Anand
2016-05-03 10:21     ` [PATCH kexec-tools 09/32] kexec: phys_to_virt() must take unsigned long long Russell King
2016-05-25  6:31       ` Pratyush Anand
2016-05-03 10:21     ` [PATCH kexec-tools 10/32] kexec: add generic helper to add to memory_regions Russell King
2016-05-25  8:00       ` Pratyush Anand
2016-05-03 10:22     ` [PATCH kexec-tools 11/32] kexec: add mem_regions sorting implementation Russell King
2016-05-25  8:00       ` Pratyush Anand
2016-05-03 10:22     ` [PATCH kexec-tools 12/32] kexec: add helper to exlude a region from a set of memory ranges Russell King
2016-05-25  8:00       ` Pratyush Anand
2016-05-26  8:56         ` Russell King - ARM Linux [this message]
2016-05-27 15:07           ` Pratyush Anand
2016-05-27 15:12             ` Russell King - ARM Linux
2016-05-03 10:22     ` [PATCH kexec-tools 13/32] arm: fix pointer signedness warning in kexec-uImage-arm.c Russell King
2016-05-25  8:00       ` Pratyush Anand
2016-05-03 10:22     ` [PATCH kexec-tools 14/32] arm: fix off-by-one on memory end Russell King
2016-05-25  8:01       ` Pratyush Anand
2016-05-03 10:22     ` [PATCH kexec-tools 15/32] arm: fix get_kernel_stext_sym() to close its file Russell King
2016-05-25  8:01       ` Pratyush Anand
2016-05-03 10:22     ` [PATCH kexec-tools 16/32] arm: fix ELF32/ELF64 check Russell King
2016-05-03 10:22     ` [PATCH kexec-tools 17/32] arm: return proper error for missing crash kernel Russell King
2016-05-27 11:27       ` Pratyush Anand
2016-05-03 10:22     ` [PATCH kexec-tools 18/32] arm: report if crash kernel is out of bounds Russell King
2016-05-27 11:27       ` Pratyush Anand
2016-05-27 13:36         ` Russell King - ARM Linux
2016-05-03 10:22     ` [PATCH kexec-tools 19/32] arm: add memory ranges debug Russell King
2016-05-27 11:28       ` Pratyush Anand
2016-05-03 10:22     ` [PATCH kexec-tools 20/32] arm: add maximum number of memory ranges Russell King
2016-05-27 11:29       ` Pratyush Anand
2016-05-03 10:22     ` [PATCH kexec-tools 21/32] arm: parse crash_reserved_mem early Russell King
2016-05-27 11:30       ` Pratyush Anand
2016-05-03 10:22     ` [PATCH kexec-tools 22/32] arm: use generic mem_region sorting implementation Russell King
2016-05-27 11:30       ` Pratyush Anand
2016-05-03 10:23     ` [PATCH kexec-tools 23/32] arm: move crash system RAM parsing earlier Russell King
2016-05-27 11:33       ` Pratyush Anand
2016-05-03 10:23     ` [PATCH kexec-tools 24/32] arm: add support for platforms with boot memory aliases Russell King
2016-05-27 11:34       ` Pratyush Anand
2016-05-03 10:23     ` [PATCH kexec-tools 25/32] arm: crashdump needs boot alias of crash kernel region Russell King
2016-05-27 11:34       ` Pratyush Anand
2016-05-03 10:23     ` [PATCH kexec-tools 26/32] arm: rename crash_reserved_mem to crash_kernel_mem Russell King
2016-05-27 11:36       ` Pratyush Anand
2016-05-03 10:23     ` [PATCH kexec-tools 27/32] arm: add support for multiple reserved regions Russell King
2016-05-27 11:37       ` Pratyush Anand
2016-05-03 10:23     ` [PATCH kexec-tools 28/32] arm: add support for boot-time crash kernel resource Russell King
2016-05-27 11:37       ` Pratyush Anand
2016-05-03 10:23     ` [PATCH kexec-tools 29/32] arm: add debug of reserved and coredump memory ranges Russell King
2016-05-27 11:35       ` Pratyush Anand
2016-05-03 10:23     ` [PATCH kexec-tools 30/32] arm: fix type of phys_offset Russell King
2016-05-27 11:38       ` Pratyush Anand
2016-05-03 10:23     ` [PATCH kexec-tools 31/32] arm: clean up phys/page offset debug Russell King
2016-05-27 11:38       ` Pratyush Anand
2016-05-03 10:23     ` [PATCH kexec-tools 32/32] arm: report which ELF core format we will use Russell King
2016-05-27 11:38       ` Pratyush Anand
2016-05-03 10:29     ` [PATCH 06/12] ARM: kexec: advertise location of bootable RAM Russell King - ARM Linux
2016-04-28  9:28 ` [PATCH 07/12] ARM: keystone: dts: add psci command definition Russell King
2016-04-28  9:28 ` [PATCH 08/12] kexec: don't invoke OOM-killer for control page allocation Russell King
2016-04-29 14:57   ` Pratyush Anand
2016-04-28  9:28 ` [PATCH 09/12] kexec: ensure user memory sizes do not wrap Russell King
2016-04-29 14:57   ` Pratyush Anand
2016-04-28  9:28 ` [PATCH 10/12] kexec: arrange for paddr_vmcoreinfo_note() to return phys_addr_t Russell King
2016-04-29 15:06   ` Pratyush Anand
2016-04-29 15:16     ` Mark Rutland
2016-04-29 15:47       ` Pratyush Anand
2016-05-03  4:24         ` Baoquan He
2016-05-03  5:53           ` Pratyush Anand
2016-05-03  9:01             ` Baoquan He
2016-05-03 10:12           ` Russell King - ARM Linux
2016-05-03 12:56             ` Baoquan He
2016-04-29 18:06     ` Russell King - ARM Linux
2016-04-30  3:30       ` Pratyush Anand
2016-04-28  9:28 ` [PATCH 11/12] kexec: allow architectures to override boot mapping Russell King
2016-04-29 15:14   ` Pratyush Anand
2016-04-29 18:08     ` Russell King - ARM Linux
2016-05-11 18:56   ` Russell King - ARM Linux
2016-05-12  6:26   ` Baoquan He
2016-05-12  8:22     ` Russell King - ARM Linux
2016-04-28  9:28 ` [PATCH 12/12] ARM: kexec: fix kexec for Keystone 2 Russell King
2016-04-28 23:04 ` [PATCH 00/12] Fixing TI Keystone2 kexec Simon Horman
2016-05-11  8:29 ` Dave Young
2016-05-11  8:52   ` Russell King - ARM Linux
2016-05-11  9:13     ` Dave Young
2016-05-11  9:32       ` Russell King - ARM Linux
2016-05-11 10:31         ` Dave Young

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=20160526085638.GF19428@n2100.arm.linux.org.uk \
    --to=linux@armlinux.org.uk \
    --cc=linux-arm-kernel@lists.infradead.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).