[PATCH v7 5/8] x86/e820: Refactor e820__range_remove

From: Martin Fernandez
Date: Mon Apr 25 2022 - 13:16:55 EST


Refactor e820__range_remove with the introduction of
e820_remover_data, indented to be used as the void pointer in the
e820_entry_updater callbacks, and the implementation of the callbacks
remove a range in the e820_table.

Signed-off-by: Martin Fernandez <martin.fernandez@xxxxxxxxxxxxx>
---
arch/x86/kernel/e820.c | 112 ++++++++++++++++++++++-------------------
1 file changed, 60 insertions(+), 52 deletions(-)

diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c
index 763b8b20a1fd..9e32c9819e99 100644
--- a/arch/x86/kernel/e820.c
+++ b/arch/x86/kernel/e820.c
@@ -717,66 +717,74 @@ static u64 __init e820__range_update_kexec(u64 start, u64 size,
return __e820__range_update(e820_table_kexec, start, size, old_type, new_type);
}

-/* Remove a range of memory from the E820 table: */
-u64 __init e820__range_remove(u64 start, u64 size, enum e820_type old_type, bool check_type)
-{
- int i;
- u64 end;
- u64 real_removed_size = 0;
-
- if (size > (ULLONG_MAX - start))
- size = ULLONG_MAX - start;
-
- end = start + size;
- printk(KERN_DEBUG "e820: remove [mem %#010Lx-%#010Lx] ", start, end - 1);
- if (check_type)
- e820_print_type(old_type);
- pr_cont("\n");
-
- for (i = 0; i < e820_table->nr_entries; i++) {
- struct e820_entry *entry = &e820_table->entries[i];
- u64 final_start, final_end;
- u64 entry_end;
+/**
+ * struct e820_remover_data - Helper type for e820__range_remove().
+ * @old_type: old_type parameter of e820__range_remove().
+ * @check_type: check_type parameter of e820__range_remove().
+ *
+ * This is intended to be used as the @data argument for the
+ * e820_entry_updater callbacks.
+ */
+struct e820_remover_data {
+ enum e820_type old_type;
+ bool check_type;
+};

- if (check_type && entry->type != old_type)
- continue;
+static bool __init remover__should_update(const struct e820_entry *entry,
+ const void *data)
+{
+ const struct e820_remover_data *remover_data =
+ (const struct e820_remover_data *)data;

- entry_end = entry->addr + entry->size;
+ return !remover_data->check_type ||
+ entry->type == remover_data->old_type;
+}

- /* Completely covered? */
- if (entry->addr >= start && entry_end <= end) {
- real_removed_size += entry->size;
- memset(entry, 0, sizeof(*entry));
- continue;
- }
+static void __init remover__update(struct e820_entry *entry, const void *data)
+{
+ memset(entry, 0, sizeof(*entry));
+}

- /* Is the new range completely covered? */
- if (entry->addr < start && entry_end > end) {
- e820__range_add(end, entry_end - end, entry->type);
- entry->size = start - entry->addr;
- real_removed_size += size;
- continue;
- }
+static void __init remover__new(struct e820_table *table, u64 new_start,
+ u64 new_size, const struct e820_entry *original,
+ const void *data)
+{
+}

- /* Partially covered: */
- final_start = max(start, entry->addr);
- final_end = min(end, entry_end);
- if (final_start >= final_end)
- continue;
+/**
+ * e820__range_remove() - Remove an address range from e820_table.
+ * @start: Start of the address range.
+ * @size: Size of the address range.
+ * @old_type: Type of the entries that we want to remove.
+ * @check_type: Bool to decide if ignore @old_type or not.
+ *
+ * Remove [@start, @start + @size) from e820_table. If @check_type is
+ * true remove only entries with type @old_type.
+ *
+ * Return: The size removed.
+ */
+u64 __init e820__range_remove(u64 start, u64 size, enum e820_type old_type,
+ bool check_type)
+{
+ struct e820_entry_updater updater = {
+ .should_update = remover__should_update,
+ .update = remover__update,
+ .new = remover__new
+ };

- real_removed_size += final_end - final_start;
+ struct e820_remover_data data = {
+ .check_type = check_type,
+ .old_type = old_type
+ };

- /*
- * Left range could be head or tail, so need to update
- * the size first:
- */
- entry->size -= final_end - final_start;
- if (entry->addr < final_start)
- continue;
+ printk(KERN_DEBUG "e820: remove [mem %#018Lx-%#018Lx] ", start,
+ start + size - 1);
+ if (check_type)
+ e820_print_type(old_type);
+ pr_cont("\n");

- entry->addr = final_end;
- }
- return real_removed_size;
+ return __e820__handle_range_update(e820_table, start, size, &updater,
+ &data);
}

void __init e820__update_table_print(void)
--
2.30.2