Re: [PATCH v3 3/6] modules: Introduce data_layout

2022-02-02 Thread Christophe Leroy


Le 03/02/2022 à 00:48, Luis Chamberlain a écrit :
> On Sat, Jan 29, 2022 at 05:02:07PM +, Christophe Leroy wrote:
>> diff --git a/kernel/module.c b/kernel/module.c
>> index 163e32e39064..11f51e17fb9f 100644
>> --- a/kernel/module.c
>> +++ b/kernel/module.c
>> @@ -81,6 +81,8 @@
>>   /* If this is set, the section belongs in the init part of the module */
>>   #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
>>   
>> +#define data_layout core_layout
>> +
>>   /*
>>* Mutex protects:
>>* 1) List of modules (also safely readable with preempt_disable),
>> @@ -2451,7 +2454,10 @@ static void layout_sections(struct module *mod, 
>> struct load_info *info)
>>  || s->sh_entsize != ~0UL
>>  || module_init_layout_section(sname))
>>  continue;
>> -s->sh_entsize = get_offset(mod, >core_layout.size, 
>> s, i);
>> +if (m)
>> +s->sh_entsize = get_offset(mod, 
>> >data_layout.size, s, i);
>> +else
>> +s->sh_entsize = get_offset(mod, 
>> >core_layout.size, s, i);
>>  pr_debug("\t%s\n", sname);
> 
> Huh why is this branching here, given you just used mod->data_layout in
> all other areas?

The module text remains in core_layout, so the text section still needs 
core_layout. In the masks[][] table, it corresponds to the first line, 
which has flag  SHF_EXECINSTR. In the loop that's when 'm' is 0.

In the following switch/case, case 0 still uses core_layout.

> 
>> @@ -3468,6 +3474,8 @@ static int move_module(struct module *mod, struct 
>> load_info *info)
>>  if (shdr->sh_entsize & INIT_OFFSET_MASK)
>>  dest = mod->init_layout.base
>>  + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
>> +else if (!(shdr->sh_flags & SHF_EXECINSTR))
>> +dest = mod->data_layout.base + shdr->sh_entsize;
>>  else
>>  dest = mod->core_layout.base + shdr->sh_entsize;
>>   
> 
> Likewise here.

Same here, the section with flag SHF_EXECINSTR is a text section, it 
stays in core_layout.

Christophe

Re: [PATCH v3 3/6] modules: Introduce data_layout

2022-02-02 Thread Luis Chamberlain
On Sat, Jan 29, 2022 at 05:02:07PM +, Christophe Leroy wrote:
> diff --git a/kernel/module.c b/kernel/module.c
> index 163e32e39064..11f51e17fb9f 100644
> --- a/kernel/module.c
> +++ b/kernel/module.c
> @@ -81,6 +81,8 @@
>  /* If this is set, the section belongs in the init part of the module */
>  #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
>  
> +#define  data_layout core_layout
> +
>  /*
>   * Mutex protects:
>   * 1) List of modules (also safely readable with preempt_disable),
> @@ -2451,7 +2454,10 @@ static void layout_sections(struct module *mod, struct 
> load_info *info)
>   || s->sh_entsize != ~0UL
>   || module_init_layout_section(sname))
>   continue;
> - s->sh_entsize = get_offset(mod, >core_layout.size, 
> s, i);
> + if (m)
> + s->sh_entsize = get_offset(mod, 
> >data_layout.size, s, i);
> + else
> + s->sh_entsize = get_offset(mod, 
> >core_layout.size, s, i);
>   pr_debug("\t%s\n", sname);

Huh why is this branching here, given you just used mod->data_layout in
all other areas?

> @@ -3468,6 +3474,8 @@ static int move_module(struct module *mod, struct 
> load_info *info)
>   if (shdr->sh_entsize & INIT_OFFSET_MASK)
>   dest = mod->init_layout.base
>   + (shdr->sh_entsize & ~INIT_OFFSET_MASK);
> + else if (!(shdr->sh_flags & SHF_EXECINSTR))
> + dest = mod->data_layout.base + shdr->sh_entsize;
>   else
>   dest = mod->core_layout.base + shdr->sh_entsize;
>  

Likewise here.

  Luis


[PATCH v3 3/6] modules: Introduce data_layout

2022-01-29 Thread Christophe Leroy
In order to allow separation of data from text, add another layout,
called data_layout. For architectures requesting separation of text
and data, only text will go in core_layout and data will go in
data_layout.

For architectures which keep text and data together, make data_layout
an alias of core_layout, that way data_layout can be used for all
data manipulations, regardless of whether data is in core_layout or
data_layout.

Signed-off-by: Christophe Leroy 
---
 kernel/module.c | 52 -
 1 file changed, 30 insertions(+), 22 deletions(-)

diff --git a/kernel/module.c b/kernel/module.c
index 163e32e39064..11f51e17fb9f 100644
--- a/kernel/module.c
+++ b/kernel/module.c
@@ -81,6 +81,8 @@
 /* If this is set, the section belongs in the init part of the module */
 #define INIT_OFFSET_MASK (1UL << (BITS_PER_LONG-1))
 
+#definedata_layout core_layout
+
 /*
  * Mutex protects:
  * 1) List of modules (also safely readable with preempt_disable),
@@ -2014,19 +2016,20 @@ static void module_enable_ro(const struct module *mod, 
bool after_init)
set_vm_flush_reset_perms(mod->init_layout.base);
frob_text(>core_layout, set_memory_ro);
 
-   frob_rodata(>core_layout, set_memory_ro);
+   frob_rodata(>data_layout, set_memory_ro);
+
frob_text(>init_layout, set_memory_ro);
frob_rodata(>init_layout, set_memory_ro);
 
if (after_init)
-   frob_ro_after_init(>core_layout, set_memory_ro);
+   frob_ro_after_init(>data_layout, set_memory_ro);
 }
 
 static void module_enable_nx(const struct module *mod)
 {
-   frob_rodata(>core_layout, set_memory_nx);
-   frob_ro_after_init(>core_layout, set_memory_nx);
-   frob_writable_data(>core_layout, set_memory_nx);
+   frob_rodata(>data_layout, set_memory_nx);
+   frob_ro_after_init(>data_layout, set_memory_nx);
+   frob_writable_data(>data_layout, set_memory_nx);
frob_rodata(>init_layout, set_memory_nx);
frob_writable_data(>init_layout, set_memory_nx);
 }
@@ -2204,7 +2207,7 @@ static void free_module(struct module *mod)
percpu_modfree(mod);
 
/* Free lock-classes; relies on the preceding sync_rcu(). */
-   lockdep_free_key_range(mod->core_layout.base, mod->core_layout.size);
+   lockdep_free_key_range(mod->data_layout.base, mod->data_layout.size);
 
/* Finally, free the core (containing the module structure) */
module_memfree(mod->core_layout.base);
@@ -2451,7 +2454,10 @@ static void layout_sections(struct module *mod, struct 
load_info *info)
|| s->sh_entsize != ~0UL
|| module_init_layout_section(sname))
continue;
-   s->sh_entsize = get_offset(mod, >core_layout.size, 
s, i);
+   if (m)
+   s->sh_entsize = get_offset(mod, 
>data_layout.size, s, i);
+   else
+   s->sh_entsize = get_offset(mod, 
>core_layout.size, s, i);
pr_debug("\t%s\n", sname);
}
switch (m) {
@@ -2460,15 +2466,15 @@ static void layout_sections(struct module *mod, struct 
load_info *info)
mod->core_layout.text_size = mod->core_layout.size;
break;
case 1: /* RO: text and ro-data */
-   mod->core_layout.size = 
debug_align(mod->core_layout.size);
-   mod->core_layout.ro_size = mod->core_layout.size;
+   mod->data_layout.size = 
debug_align(mod->data_layout.size);
+   mod->data_layout.ro_size = mod->data_layout.size;
break;
case 2: /* RO after init */
-   mod->core_layout.size = 
debug_align(mod->core_layout.size);
-   mod->core_layout.ro_after_init_size = 
mod->core_layout.size;
+   mod->data_layout.size = 
debug_align(mod->data_layout.size);
+   mod->data_layout.ro_after_init_size = 
mod->data_layout.size;
break;
case 4: /* whole core */
-   mod->core_layout.size = 
debug_align(mod->core_layout.size);
+   mod->data_layout.size = 
debug_align(mod->data_layout.size);
break;
}
}
@@ -2721,12 +2727,12 @@ static void layout_symtab(struct module *mod, struct 
load_info *info)
}
 
/* Append room for core symbols at end of core part. */
-   info->symoffs = ALIGN(mod->core_layout.size, symsect->sh_addralign ?: 
1);
-   info->stroffs = mod->core_layout.size = info->symoffs + ndst * 
sizeof(Elf_Sym);
-   mod->core_layout.size += strtab_size;
-   info->core_typeoffs = mod->core_layout.size;
-   mod->core_layout.size += ndst * sizeof(char);
-