Skip to content

Commit 78ce248

Browse files
mflemingIngo Molnar
authored andcommitted
efi: Iterate over efi.memmap in for_each_efi_memory_desc()
Most of the users of for_each_efi_memory_desc() are equally happy iterating over the EFI memory map in efi.memmap instead of 'memmap', since the former is usually a pointer to the latter. For those users that want to specify an EFI memory map other than efi.memmap, that can be done using for_each_efi_memory_desc_in_map(). One such example is in the libstub code where the firmware is queried directly for the memory map, it gets iterated over, and then freed. This change goes part of the way toward deleting the global 'memmap' variable, which is not universally available on all architectures (notably IA64) and is rather poorly named. Signed-off-by: Matt Fleming <matt@codeblueprint.co.uk> Reviewed-by: Ard Biesheuvel <ard.biesheuvel@linaro.org> Cc: Borislav Petkov <bp@alien8.de> Cc: Leif Lindholm <leif.lindholm@linaro.org> Cc: Mark Salter <msalter@redhat.com> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: linux-efi@vger.kernel.org Link: http://lkml.kernel.org/r/1461614832-17633-7-git-send-email-matt@codeblueprint.co.uk Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 30d7bf0 commit 78ce248

File tree

9 files changed

+39
-56
lines changed

9 files changed

+39
-56
lines changed

arch/x86/platform/efi/efi.c

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -119,11 +119,10 @@ void efi_get_time(struct timespec *now)
119119

120120
void __init efi_find_mirror(void)
121121
{
122-
void *p;
122+
efi_memory_desc_t *md;
123123
u64 mirror_size = 0, total_size = 0;
124124

125-
for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
126-
efi_memory_desc_t *md = p;
125+
for_each_efi_memory_desc(md) {
127126
unsigned long long start = md->phys_addr;
128127
unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
129128

@@ -146,10 +145,9 @@ void __init efi_find_mirror(void)
146145

147146
static void __init do_add_efi_memmap(void)
148147
{
149-
void *p;
148+
efi_memory_desc_t *md;
150149

151-
for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
152-
efi_memory_desc_t *md = p;
150+
for_each_efi_memory_desc(md) {
153151
unsigned long long start = md->phys_addr;
154152
unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
155153
int e820_type;
@@ -226,17 +224,13 @@ void __init efi_print_memmap(void)
226224
{
227225
#ifdef EFI_DEBUG
228226
efi_memory_desc_t *md;
229-
void *p;
230-
int i;
227+
int i = 0;
231228

232-
for (p = memmap.map, i = 0;
233-
p < memmap.map_end;
234-
p += memmap.desc_size, i++) {
229+
for_each_efi_memory_desc(md) {
235230
char buf[64];
236231

237-
md = p;
238232
pr_info("mem%02u: %s range=[0x%016llx-0x%016llx] (%lluMB)\n",
239-
i, efi_md_typeattr_format(buf, sizeof(buf), md),
233+
i++, efi_md_typeattr_format(buf, sizeof(buf), md),
240234
md->phys_addr,
241235
md->phys_addr + (md->num_pages << EFI_PAGE_SHIFT) - 1,
242236
(md->num_pages >> (20 - EFI_PAGE_SHIFT)));
@@ -550,12 +544,9 @@ void __init efi_set_executable(efi_memory_desc_t *md, bool executable)
550544
void __init runtime_code_page_mkexec(void)
551545
{
552546
efi_memory_desc_t *md;
553-
void *p;
554547

555548
/* Make EFI runtime service code area executable */
556-
for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
557-
md = p;
558-
549+
for_each_efi_memory_desc(md) {
559550
if (md->type != EFI_RUNTIME_SERVICES_CODE)
560551
continue;
561552

@@ -602,12 +593,10 @@ void __init old_map_region(efi_memory_desc_t *md)
602593
/* Merge contiguous regions of the same type and attribute */
603594
static void __init efi_merge_regions(void)
604595
{
605-
void *p;
606596
efi_memory_desc_t *md, *prev_md = NULL;
607597

608-
for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
598+
for_each_efi_memory_desc(md) {
609599
u64 prev_size;
610-
md = p;
611600

612601
if (!prev_md) {
613602
prev_md = md;
@@ -650,15 +639,13 @@ static void __init save_runtime_map(void)
650639
{
651640
#ifdef CONFIG_KEXEC_CORE
652641
efi_memory_desc_t *md;
653-
void *tmp, *p, *q = NULL;
642+
void *tmp, *q = NULL;
654643
int count = 0;
655644

656645
if (efi_enabled(EFI_OLD_MEMMAP))
657646
return;
658647

659-
for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
660-
md = p;
661-
648+
for_each_efi_memory_desc(md) {
662649
if (!(md->attribute & EFI_MEMORY_RUNTIME) ||
663650
(md->type == EFI_BOOT_SERVICES_CODE) ||
664651
(md->type == EFI_BOOT_SERVICES_DATA))
@@ -814,7 +801,6 @@ static void __init kexec_enter_virtual_mode(void)
814801
#ifdef CONFIG_KEXEC_CORE
815802
efi_memory_desc_t *md;
816803
unsigned int num_pages;
817-
void *p;
818804

819805
efi.systab = NULL;
820806

@@ -838,8 +824,7 @@ static void __init kexec_enter_virtual_mode(void)
838824
* Map efi regions which were passed via setup_data. The virt_addr is a
839825
* fixed addr which was used in first kernel of a kexec boot.
840826
*/
841-
for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
842-
md = p;
827+
for_each_efi_memory_desc(md) {
843828
efi_map_region_fixed(md); /* FIXME: add error handling */
844829
get_systab_virt_addr(md);
845830
}
@@ -1009,13 +994,11 @@ void __init efi_enter_virtual_mode(void)
1009994
u32 efi_mem_type(unsigned long phys_addr)
1010995
{
1011996
efi_memory_desc_t *md;
1012-
void *p;
1013997

1014998
if (!efi_enabled(EFI_MEMMAP))
1015999
return 0;
10161000

1017-
for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
1018-
md = p;
1001+
for_each_efi_memory_desc(md) {
10191002
if ((md->phys_addr <= phys_addr) &&
10201003
(phys_addr < (md->phys_addr +
10211004
(md->num_pages << EFI_PAGE_SHIFT))))

arch/x86/platform/efi/efi_64.c

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,12 @@ struct efi_scratch efi_scratch;
5555
static void __init early_code_mapping_set_exec(int executable)
5656
{
5757
efi_memory_desc_t *md;
58-
void *p;
5958

6059
if (!(__supported_pte_mask & _PAGE_NX))
6160
return;
6261

6362
/* Make EFI service code area executable */
64-
for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
65-
md = p;
63+
for_each_efi_memory_desc(md) {
6664
if (md->type == EFI_RUNTIME_SERVICES_CODE ||
6765
md->type == EFI_BOOT_SERVICES_CODE)
6866
efi_set_executable(md, executable);
@@ -253,7 +251,7 @@ int __init efi_setup_page_tables(unsigned long pa_memmap, unsigned num_pages)
253251
* Map all of RAM so that we can access arguments in the 1:1
254252
* mapping when making EFI runtime calls.
255253
*/
256-
for_each_efi_memory_desc(&memmap, md) {
254+
for_each_efi_memory_desc(md) {
257255
if (md->type != EFI_CONVENTIONAL_MEMORY &&
258256
md->type != EFI_LOADER_DATA &&
259257
md->type != EFI_LOADER_CODE)
@@ -398,7 +396,6 @@ void __init efi_runtime_update_mappings(void)
398396
unsigned long pfn;
399397
pgd_t *pgd = efi_pgd;
400398
efi_memory_desc_t *md;
401-
void *p;
402399

403400
if (efi_enabled(EFI_OLD_MEMMAP)) {
404401
if (__supported_pte_mask & _PAGE_NX)
@@ -409,9 +406,8 @@ void __init efi_runtime_update_mappings(void)
409406
if (!efi_enabled(EFI_NX_PE_DATA))
410407
return;
411408

412-
for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
409+
for_each_efi_memory_desc(md) {
413410
unsigned long pf = 0;
414-
md = p;
415411

416412
if (!(md->attribute & EFI_MEMORY_RUNTIME))
417413
continue;

arch/x86/platform/efi/quirks.c

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,9 @@ static bool can_free_region(u64 start, u64 size)
195195
*/
196196
void __init efi_reserve_boot_services(void)
197197
{
198-
void *p;
198+
efi_memory_desc_t *md;
199199

200-
for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
201-
efi_memory_desc_t *md = p;
200+
for_each_efi_memory_desc(md) {
202201
u64 start = md->phys_addr;
203202
u64 size = md->num_pages << EFI_PAGE_SHIFT;
204203
bool already_reserved;
@@ -250,10 +249,9 @@ void __init efi_reserve_boot_services(void)
250249

251250
void __init efi_free_boot_services(void)
252251
{
253-
void *p;
252+
efi_memory_desc_t *md;
254253

255-
for (p = memmap.map; p < memmap.map_end; p += memmap.desc_size) {
256-
efi_memory_desc_t *md = p;
254+
for_each_efi_memory_desc(md) {
257255
unsigned long long start = md->phys_addr;
258256
unsigned long long size = md->num_pages << EFI_PAGE_SHIFT;
259257

drivers/firmware/efi/arm-init.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static phys_addr_t efi_to_phys(unsigned long addr)
4040
{
4141
efi_memory_desc_t *md;
4242

43-
for_each_efi_memory_desc(&memmap, md) {
43+
for_each_efi_memory_desc_in_map(&memmap, md) {
4444
if (!(md->attribute & EFI_MEMORY_RUNTIME))
4545
continue;
4646
if (md->virt_addr == 0)
@@ -145,7 +145,7 @@ static __init void reserve_regions(void)
145145
if (efi_enabled(EFI_DBG))
146146
pr_info("Processing EFI memory map:\n");
147147

148-
for_each_efi_memory_desc(&memmap, md) {
148+
for_each_efi_memory_desc_in_map(&memmap, md) {
149149
paddr = md->phys_addr;
150150
npages = md->num_pages;
151151

drivers/firmware/efi/arm-runtime.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ static bool __init efi_virtmap_init(void)
4848
init_new_context(NULL, &efi_mm);
4949

5050
systab_found = false;
51-
for_each_efi_memory_desc(&memmap, md) {
51+
for_each_efi_memory_desc(md) {
5252
phys_addr_t phys = md->phys_addr;
5353
int ret;
5454

drivers/firmware/efi/efi.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -620,16 +620,12 @@ char * __init efi_md_typeattr_format(char *buf, size_t size,
620620
*/
621621
u64 __weak efi_mem_attributes(unsigned long phys_addr)
622622
{
623-
struct efi_memory_map *map;
624623
efi_memory_desc_t *md;
625-
void *p;
626624

627625
if (!efi_enabled(EFI_MEMMAP))
628626
return 0;
629627

630-
map = efi.memmap;
631-
for (p = map->map; p < map->map_end; p += map->desc_size) {
632-
md = p;
628+
for_each_efi_memory_desc(md) {
633629
if ((md->phys_addr <= phys_addr) &&
634630
(phys_addr < (md->phys_addr +
635631
(md->num_pages << EFI_PAGE_SHIFT))))

drivers/firmware/efi/fake_mem.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,7 @@ void __init efi_fake_memmap(void)
6868
return;
6969

7070
/* count up the number of EFI memory descriptor */
71-
for (old = memmap.map; old < memmap.map_end; old += memmap.desc_size) {
72-
md = old;
71+
for_each_efi_memory_desc(md) {
7372
start = md->phys_addr;
7473
end = start + (md->num_pages << EFI_PAGE_SHIFT) - 1;
7574

drivers/firmware/efi/libstub/efi-stub-helper.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,12 @@ unsigned long get_dram_base(efi_system_table_t *sys_table_arg)
125125

126126
map.map_end = map.map + map_size;
127127

128-
for_each_efi_memory_desc(&map, md)
129-
if (md->attribute & EFI_MEMORY_WB)
128+
for_each_efi_memory_desc_in_map(&map, md) {
129+
if (md->attribute & EFI_MEMORY_WB) {
130130
if (membase > md->phys_addr)
131131
membase = md->phys_addr;
132+
}
133+
}
132134

133135
efi_call_early(free_pool, map.map);
134136

include/linux/efi.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -958,11 +958,20 @@ static inline void efi_fake_memmap(void) { }
958958
#endif
959959

960960
/* Iterate through an efi_memory_map */
961-
#define for_each_efi_memory_desc(m, md) \
961+
#define for_each_efi_memory_desc_in_map(m, md) \
962962
for ((md) = (m)->map; \
963963
(md) <= (efi_memory_desc_t *)((m)->map_end - (m)->desc_size); \
964964
(md) = (void *)(md) + (m)->desc_size)
965965

966+
/**
967+
* for_each_efi_memory_desc - iterate over descriptors in efi.memmap
968+
* @md: the efi_memory_desc_t * iterator
969+
*
970+
* Once the loop finishes @md must not be accessed.
971+
*/
972+
#define for_each_efi_memory_desc(md) \
973+
for_each_efi_memory_desc_in_map(efi.memmap, md)
974+
966975
/*
967976
* Format an EFI memory descriptor's type and attributes to a user-provided
968977
* character buffer, as per snprintf(), and return the buffer.

0 commit comments

Comments
 (0)