Commit 10f9d33d authored by Nikita Panov's avatar Nikita Panov Committed by Denis Darvish
Browse files

mm: init kernel modules with replication support

kunpeng inclusion
category: feature
bugzilla: https://gitee.com/openeuler/kernel/issues/IBOJU2



-------------------------------------------------

Acked-by: default avatarArtem Kuzin <artem.kuzin@huawei.com>
Acked-by: default avatarAlexander Grubnikov <alexander.grubnikov@huawei.com>
Acked-by: default avatarIlya Hanov <ilya.hanov@huawei-partners.com>
Acked-by: default avatarDenis Darvish <darvish.denis@huawei.com>
Signed-off-by: default avatarNikita Panov <panov.nikita@huawei.com>
parent 7b2ccab5
Loading
Loading
Loading
Loading
+6 −0
Original line number Diff line number Diff line
@@ -29,6 +29,12 @@ unsigned int arch_mod_section_prepend(struct module *mod, unsigned int section);
   sections.  Returns NULL on failure. */
void *module_alloc(unsigned long size);

#ifdef CONFIG_KERNEL_REPLICATION
void *module_alloc_replica(unsigned long size);
/* Replicate memory allocated in previous function*/
void module_replicate(void *ptr);
#endif /* CONFIG_KERNEL_REPLICATION */

/* Free memory returned from module_alloc. */
void module_memfree(void *module_region);

+32 −0
Original line number Diff line number Diff line
@@ -57,6 +57,7 @@
#include <linux/audit.h>
#include <linux/cfi.h>
#include <linux/debugfs.h>
#include <linux/numa_kernel_replication.h>
#include <uapi/linux/module.h>
#include "internal.h"

@@ -1209,13 +1210,40 @@ static bool mod_mem_use_vmalloc(enum mod_mem_type type)
		mod_mem_type_is_core_data(type);
}

#ifdef CONFIG_KERNEL_REPLICATION
static int sections_to_replicate[] = {MOD_TEXT, MOD_RODATA};

static void module_replicate_sections(struct module *mod)
{
	int i;

	for (i = 0; i < ARRAY_SIZE(sections_to_replicate); i++)
		module_replicate(mod->mem[sections_to_replicate[i]].base);
}

static void *module_memory_alloc(unsigned int size, enum mod_mem_type type)
{
	int i;

	if (mod_mem_use_vmalloc(type))
		return vzalloc(size);

	for (i = 0; i < ARRAY_SIZE(sections_to_replicate); i++) {
		if (type == sections_to_replicate[i])
			return module_alloc_replica(size);
	}
	return module_alloc(size);
}

#else /* !CONFIG_KERNEL_REPLICATION */
static void *module_memory_alloc(unsigned int size, enum mod_mem_type type)
{
	if (mod_mem_use_vmalloc(type))
		return vzalloc(size);
	return module_alloc(size);
}
#endif /* CONFIG_KERNEL_REPLICATION */

static void module_memory_free(void *ptr, enum mod_mem_type type)
{
	if (mod_mem_use_vmalloc(type))
@@ -2752,6 +2780,10 @@ static int complete_formation(struct module *mod, struct load_info *info)
	module_bug_finalize(info->hdr, info->sechdrs, mod);
	module_cfi_finalize(info->hdr, info->sechdrs, mod);

#ifdef CONFIG_KERNEL_REPLICATION
	module_replicate_sections(mod);
#endif

	module_enable_ro(mod, false);
	module_enable_nx(mod);
	module_enable_x(mod);
+7 −7
Original line number Diff line number Diff line
@@ -29,7 +29,7 @@ static void module_set_memory(const struct module *mod, enum mod_mem_type type,
void module_enable_x(const struct module *mod)
{
	for_class_mod_mem_type(type, text)
		module_set_memory(mod, type, set_memory_x);
		module_set_memory(mod, type, numa_set_memory_x);
}

#ifdef CONFIG_LIVEPATCH_WO_FTRACE
@@ -59,13 +59,13 @@ void module_enable_ro(const struct module *mod, bool after_init)
		return;
#endif

	module_set_memory(mod, MOD_TEXT, set_memory_ro);
	module_set_memory(mod, MOD_INIT_TEXT, set_memory_ro);
	module_set_memory(mod, MOD_RODATA, set_memory_ro);
	module_set_memory(mod, MOD_INIT_RODATA, set_memory_ro);
	module_set_memory(mod, MOD_TEXT, numa_set_memory_ro);
	module_set_memory(mod, MOD_INIT_TEXT, numa_set_memory_ro);
	module_set_memory(mod, MOD_RODATA, numa_set_memory_ro);
	module_set_memory(mod, MOD_INIT_RODATA, numa_set_memory_ro);

	if (after_init)
		module_set_memory(mod, MOD_RO_AFTER_INIT, set_memory_ro);
		module_set_memory(mod, MOD_RO_AFTER_INIT, numa_set_memory_ro);
}

void module_enable_nx(const struct module *mod)
@@ -74,7 +74,7 @@ void module_enable_nx(const struct module *mod)
		return;

	for_class_mod_mem_type(type, data)
		module_set_memory(mod, type, set_memory_nx);
		module_set_memory(mod, type, numa_set_memory_nx);
}

int module_enforce_rwx_sections(Elf_Ehdr *hdr, Elf_Shdr *sechdrs,