Skip to content

Commit

Permalink
resource: add walk_system_ram_res_rev()
Browse files Browse the repository at this point in the history
This function, being a variant of walk_system_ram_res() introduced in
commit 8c86e70 ("resource: provide new functions to walk through
resources"), walks through a list of all the resources of System RAM in
reversed order, i.e., from higher to lower.

It will be used in kexec_file code to load kernel, initrd etc when
preparing kexec reboot.

Link: https://lkml.kernel.org/r/ZVTA6z/06cLnWKUz@MiWiFi-R3L-srv
Signed-off-by: AKASHI Takahiro <[email protected]>
Signed-off-by: Baoquan He <[email protected]>
Cc: Eric Biederman <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
  • Loading branch information
Baoquan He authored and akpm00 committed Dec 11, 2023
1 parent bfc4372 commit 7acf164
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
3 changes: 3 additions & 0 deletions include/linux/ioport.h
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,9 @@ extern int
walk_system_ram_res(u64 start, u64 end, void *arg,
int (*func)(struct resource *, void *));
extern int
walk_system_ram_res_rev(u64 start, u64 end, void *arg,
int (*func)(struct resource *, void *));
extern int
walk_iomem_res_desc(unsigned long desc, unsigned long flags, u64 start, u64 end,
void *arg, int (*func)(struct resource *, void *));

Expand Down
57 changes: 57 additions & 0 deletions kernel/resource.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include <linux/mount.h>
#include <linux/resource_ext.h>
#include <uapi/linux/magic.h>
#include <linux/string.h>
#include <linux/vmalloc.h>
#include <asm/io.h>


Expand Down Expand Up @@ -429,6 +431,61 @@ int walk_system_ram_res(u64 start, u64 end, void *arg,
func);
}

/*
* This function, being a variant of walk_system_ram_res(), calls the @func
* callback against all memory ranges of type System RAM which are marked as
* IORESOURCE_SYSTEM_RAM and IORESOUCE_BUSY in reversed order, i.e., from
* higher to lower.
*/
int walk_system_ram_res_rev(u64 start, u64 end, void *arg,
int (*func)(struct resource *, void *))
{
struct resource res, *rams;
int rams_size = 16, i;
unsigned long flags;
int ret = -1;

/* create a list */
rams = kvcalloc(rams_size, sizeof(struct resource), GFP_KERNEL);
if (!rams)
return ret;

flags = IORESOURCE_SYSTEM_RAM | IORESOURCE_BUSY;
i = 0;
while ((start < end) &&
(!find_next_iomem_res(start, end, flags, IORES_DESC_NONE, &res))) {
if (i >= rams_size) {
/* re-alloc */
struct resource *rams_new;

rams_new = kvrealloc(rams, rams_size * sizeof(struct resource),
(rams_size + 16) * sizeof(struct resource),
GFP_KERNEL);
if (!rams_new)
goto out;

rams = rams_new;
rams_size += 16;
}

rams[i].start = res.start;
rams[i++].end = res.end;

start = res.end + 1;
}

/* go reverse */
for (i--; i >= 0; i--) {
ret = (*func)(&rams[i], arg);
if (ret)
break;
}

out:
kvfree(rams);
return ret;
}

/*
* This function calls the @func callback against all memory ranges, which
* are ranges marked as IORESOURCE_MEM and IORESOUCE_BUSY.
Expand Down

0 comments on commit 7acf164

Please sign in to comment.