linux-riscv.lists.infradead.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v4 0/2] Fix binfmt_flat loader for RISC-V
@ 2021-04-17  1:10 Damien Le Moal
  2021-04-17  1:10 ` [PATCH v4 1/2] binfmt_flat: allow not offsetting data start Damien Le Moal
  2021-04-17  1:10 ` [PATCH v4 2/2] riscv: Disable data start offset in flat binaries Damien Le Moal
  0 siblings, 2 replies; 11+ messages in thread
From: Damien Le Moal @ 2021-04-17  1:10 UTC (permalink / raw)
  To: uclinux-dev, Greg Ungerer, Palmer Dabbelt, linux-riscv
  Cc: linux-kernel, Anup Patel, Christoph Hellwig

RISC-V NOMMU flat binaries cannot tolerate a gap between the text and
data section as the toolchain fully resolves at compile time the PC
relative global pointer (__global_pointer$ value loaded in the gp
register). Without a relocation entry provided, the flat bin loader
cannot fix the value if a gap is introduced and user executables fail
to run.

This series fixes this problem by allowing an architecture to request
the flat loader to suppress the offset of the data start section.
Combined with the use of elf2flt "-r" option to mark the flat
executables with the FLAT_FLAG_RAM flag, the text and data sections are
loaded contiguously in memory, without a change in their relative
position from compile time.

The first patch fixes binfmt_flat flat_load_file() using the new
configuration option CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET. The
second patch enables this new option for RISCV NOMMU builds.

These patches do not change the binfmt_flat loader behavior for other
architectures.

Changes from v3:
* Renamed the configuration option from
  CONFIG_BINFMT_FLAT_NO_TEXT_DATA_GAP to
  CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET to clarify that only the
  offseting of the data section start is suppressed.
* Do not force loding to RAM (contiguously) if the flat binary does not
  have the FLAT_FLAG_RAM flag set.
* Updated commit messages to reflect above changes.

Changes from v2:
* Updated distribution list
* Added Palmer ack-by tag

Changes from v1:
* Replace FLAT_TEXT_DATA_NO_GAP macro with
  CONFIG_BINFMT_FLAT_NO_TEXT_DATA_GAP config option (patch 1).
* Remove the addition of riscv/include/asm/flat.h and set
  CONFIG_BINFMT_FLAT_NO_TEXT_DATA_GAP for RISCV and !MMU

Damien Le Moal (2):
  binfmt_flat: allow not offsetting data start
  riscv: Disable data start offset in flat binaries

 arch/riscv/Kconfig |  1 +
 fs/Kconfig.binfmt  |  3 +++
 fs/binfmt_flat.c   | 19 ++++++++++++++-----
 3 files changed, 18 insertions(+), 5 deletions(-)

-- 
2.30.2


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v4 1/2] binfmt_flat: allow not offsetting data start
  2021-04-17  1:10 [PATCH v4 0/2] Fix binfmt_flat loader for RISC-V Damien Le Moal
@ 2021-04-17  1:10 ` Damien Le Moal
  2021-04-17  4:52   ` Greg Ungerer
  2021-04-17  1:10 ` [PATCH v4 2/2] riscv: Disable data start offset in flat binaries Damien Le Moal
  1 sibling, 1 reply; 11+ messages in thread
From: Damien Le Moal @ 2021-04-17  1:10 UTC (permalink / raw)
  To: uclinux-dev, Greg Ungerer, Palmer Dabbelt, linux-riscv
  Cc: linux-kernel, Anup Patel, Christoph Hellwig

Commit 2217b9826246 ("binfmt_flat: revert "binfmt_flat: don't offset
the data start"") restored offsetting the start of the data section by
a number of words defined by MAX_SHARED_LIBS. As a result, since
MAX_SHARED_LIBS is never 0, a gap between the text and data sections
always exists. For architectures which cannot support a such gap
between the text and data sections (e.g. riscv nommu), flat binary
programs cannot be executed.

To allow an architecture to request no data start offset to allow for
contiguous text and data sections for binaries flagged with
FLAT_FLAG_RAM, introduce the new config option
CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET. Using this new option, the
macro DATA_START_OFFSET_WORDS is conditionally defined in binfmt_flat.c
to MAX_SHARED_LIBS for architectures tolerating or needing the data
start offset (CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET disabled case)
and to 0 when CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET is enabled.
DATA_START_OFFSET_WORDS is used in load_flat_file() to calculate the
data section length and start position.

Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
---
 fs/Kconfig.binfmt |  3 +++
 fs/binfmt_flat.c  | 19 ++++++++++++++-----
 2 files changed, 17 insertions(+), 5 deletions(-)

diff --git a/fs/Kconfig.binfmt b/fs/Kconfig.binfmt
index c6f1c8c1934e..06fb7a93a1bd 100644
--- a/fs/Kconfig.binfmt
+++ b/fs/Kconfig.binfmt
@@ -112,6 +112,9 @@ config BINFMT_FLAT_ARGVP_ENVP_ON_STACK
 config BINFMT_FLAT_OLD_ALWAYS_RAM
 	bool
 
+config BINFMT_FLAT_NO_DATA_START_OFFSET
+	bool
+
 config BINFMT_FLAT_OLD
 	bool "Enable support for very old legacy flat binaries"
 	depends on BINFMT_FLAT
diff --git a/fs/binfmt_flat.c b/fs/binfmt_flat.c
index b9c658e0548e..1dc68dfba3e0 100644
--- a/fs/binfmt_flat.c
+++ b/fs/binfmt_flat.c
@@ -74,6 +74,12 @@
 #define	MAX_SHARED_LIBS			(1)
 #endif
 
+#ifdef CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET
+#define DATA_START_OFFSET_WORDS		(0)
+#else
+#define DATA_START_OFFSET_WORDS		(MAX_SHARED_LIBS)
+#endif
+
 struct lib_info {
 	struct {
 		unsigned long start_code;		/* Start of text segment */
@@ -560,6 +566,7 @@ static int load_flat_file(struct linux_binprm *bprm,
 	 * it all together.
 	 */
 	if (!IS_ENABLED(CONFIG_MMU) && !(flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP))) {
+
 		/*
 		 * this should give us a ROM ptr,  but if it doesn't we don't
 		 * really care
@@ -576,7 +583,8 @@ static int load_flat_file(struct linux_binprm *bprm,
 			goto err;
 		}
 
-		len = data_len + extra + MAX_SHARED_LIBS * sizeof(unsigned long);
+		len = data_len + extra +
+			DATA_START_OFFSET_WORDS * sizeof(unsigned long);
 		len = PAGE_ALIGN(len);
 		realdatastart = vm_mmap(NULL, 0, len,
 			PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE, 0);
@@ -591,7 +599,7 @@ static int load_flat_file(struct linux_binprm *bprm,
 			goto err;
 		}
 		datapos = ALIGN(realdatastart +
-				MAX_SHARED_LIBS * sizeof(unsigned long),
+				DATA_START_OFFSET_WORDS * sizeof(unsigned long),
 				FLAT_DATA_ALIGN);
 
 		pr_debug("Allocated data+bss+stack (%u bytes): %lx\n",
@@ -622,7 +630,8 @@ static int load_flat_file(struct linux_binprm *bprm,
 		memp_size = len;
 	} else {
 
-		len = text_len + data_len + extra + MAX_SHARED_LIBS * sizeof(u32);
+		len = text_len + data_len + extra +
+			DATA_START_OFFSET_WORDS * sizeof(u32);
 		len = PAGE_ALIGN(len);
 		textpos = vm_mmap(NULL, 0, len,
 			PROT_READ | PROT_EXEC | PROT_WRITE, MAP_PRIVATE, 0);
@@ -638,7 +647,7 @@ static int load_flat_file(struct linux_binprm *bprm,
 
 		realdatastart = textpos + ntohl(hdr->data_start);
 		datapos = ALIGN(realdatastart +
-				MAX_SHARED_LIBS * sizeof(u32),
+				DATA_START_OFFSET_WORDS * sizeof(u32),
 				FLAT_DATA_ALIGN);
 
 		reloc = (__be32 __user *)
@@ -714,7 +723,7 @@ static int load_flat_file(struct linux_binprm *bprm,
 			ret = result;
 			pr_err("Unable to read code+data+bss, errno %d\n", ret);
 			vm_munmap(textpos, text_len + data_len + extra +
-				MAX_SHARED_LIBS * sizeof(u32));
+				  DATA_START_OFFSET_WORDS * sizeof(u32));
 			goto err;
 		}
 	}
-- 
2.30.2


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v4 2/2] riscv: Disable data start offset in flat binaries
  2021-04-17  1:10 [PATCH v4 0/2] Fix binfmt_flat loader for RISC-V Damien Le Moal
  2021-04-17  1:10 ` [PATCH v4 1/2] binfmt_flat: allow not offsetting data start Damien Le Moal
@ 2021-04-17  1:10 ` Damien Le Moal
  2021-04-17  4:56   ` Greg Ungerer
  1 sibling, 1 reply; 11+ messages in thread
From: Damien Le Moal @ 2021-04-17  1:10 UTC (permalink / raw)
  To: uclinux-dev, Greg Ungerer, Palmer Dabbelt, linux-riscv
  Cc: linux-kernel, Anup Patel, Christoph Hellwig

uclibc/gcc combined with elf2flt riscv linker file fully resolve the
PC relative __global_pointer$ value at compile time and do not generate
a relocation entry to set a correct value of the gp register at runtime.
As a result, if the flatbin loader offsets the start of the data
section, the relative position change between the text and data sections
compared to the compile time positions results in an incorrect gp value
being used. This causes flatbin executables to crash.

Avoid this problem by enabling CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET
automatically when CONFIG_RISCV is enabled and CONFIG_MMU is disabled.

Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
Acked-by: Palmer Dabbelt <palmerdabbelt@google.com>
---
 arch/riscv/Kconfig | 1 +
 1 file changed, 1 insertion(+)

diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
index 4515a10c5d22..add528eb9235 100644
--- a/arch/riscv/Kconfig
+++ b/arch/riscv/Kconfig
@@ -33,6 +33,7 @@ config RISCV
 	select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT if MMU
 	select ARCH_WANT_FRAME_POINTERS
 	select ARCH_WANT_HUGE_PMD_SHARE if 64BIT
+	select BINFMT_FLAT_NO_DATA_START_OFFSET if !MMU
 	select CLONE_BACKWARDS
 	select CLINT_TIMER if !MMU
 	select COMMON_CLK
-- 
2.30.2


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH v4 1/2] binfmt_flat: allow not offsetting data start
  2021-04-17  1:10 ` [PATCH v4 1/2] binfmt_flat: allow not offsetting data start Damien Le Moal
@ 2021-04-17  4:52   ` Greg Ungerer
  2021-04-17  4:54     ` Damien Le Moal
  0 siblings, 1 reply; 11+ messages in thread
From: Greg Ungerer @ 2021-04-17  4:52 UTC (permalink / raw)
  To: Damien Le Moal, uclinux-dev, Palmer Dabbelt, linux-riscv
  Cc: linux-kernel, Anup Patel, Christoph Hellwig


On 17/4/21 11:10 am, Damien Le Moal wrote:
> Commit 2217b9826246 ("binfmt_flat: revert "binfmt_flat: don't offset
> the data start"") restored offsetting the start of the data section by
> a number of words defined by MAX_SHARED_LIBS. As a result, since
> MAX_SHARED_LIBS is never 0, a gap between the text and data sections
> always exists. For architectures which cannot support a such gap
> between the text and data sections (e.g. riscv nommu), flat binary
> programs cannot be executed.
> 
> To allow an architecture to request no data start offset to allow for
> contiguous text and data sections for binaries flagged with
> FLAT_FLAG_RAM, introduce the new config option
> CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET. Using this new option, the
> macro DATA_START_OFFSET_WORDS is conditionally defined in binfmt_flat.c
> to MAX_SHARED_LIBS for architectures tolerating or needing the data
> start offset (CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET disabled case)
> and to 0 when CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET is enabled.
> DATA_START_OFFSET_WORDS is used in load_flat_file() to calculate the
> data section length and start position.
> 
> Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
> ---
>   fs/Kconfig.binfmt |  3 +++
>   fs/binfmt_flat.c  | 19 ++++++++++++++-----
>   2 files changed, 17 insertions(+), 5 deletions(-)
> 
> diff --git a/fs/Kconfig.binfmt b/fs/Kconfig.binfmt
> index c6f1c8c1934e..06fb7a93a1bd 100644
> --- a/fs/Kconfig.binfmt
> +++ b/fs/Kconfig.binfmt
> @@ -112,6 +112,9 @@ config BINFMT_FLAT_ARGVP_ENVP_ON_STACK
>   config BINFMT_FLAT_OLD_ALWAYS_RAM
>   	bool
>   
> +config BINFMT_FLAT_NO_DATA_START_OFFSET
> +	bool
> +
>   config BINFMT_FLAT_OLD
>   	bool "Enable support for very old legacy flat binaries"
>   	depends on BINFMT_FLAT
> diff --git a/fs/binfmt_flat.c b/fs/binfmt_flat.c
> index b9c658e0548e..1dc68dfba3e0 100644
> --- a/fs/binfmt_flat.c
> +++ b/fs/binfmt_flat.c
> @@ -74,6 +74,12 @@
>   #define	MAX_SHARED_LIBS			(1)
>   #endif
>   
> +#ifdef CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET
> +#define DATA_START_OFFSET_WORDS		(0)
> +#else
> +#define DATA_START_OFFSET_WORDS		(MAX_SHARED_LIBS)
> +#endif
> +
>   struct lib_info {
>   	struct {
>   		unsigned long start_code;		/* Start of text segment */
> @@ -560,6 +566,7 @@ static int load_flat_file(struct linux_binprm *bprm,
>   	 * it all together.
>   	 */
>   	if (!IS_ENABLED(CONFIG_MMU) && !(flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP))) {
> +

Random white space change...
Don't worry about re-spinning though, I will just edit this chunk out.


>   		/*
>   		 * this should give us a ROM ptr,  but if it doesn't we don't
>   		 * really care
> @@ -576,7 +583,8 @@ static int load_flat_file(struct linux_binprm *bprm,
>   			goto err;
>   		}
>   
> -		len = data_len + extra + MAX_SHARED_LIBS * sizeof(unsigned long);
> +		len = data_len + extra +
> +			DATA_START_OFFSET_WORDS * sizeof(unsigned long);
>   		len = PAGE_ALIGN(len);
>   		realdatastart = vm_mmap(NULL, 0, len,
>   			PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE, 0);
> @@ -591,7 +599,7 @@ static int load_flat_file(struct linux_binprm *bprm,
>   			goto err;
>   		}
>   		datapos = ALIGN(realdatastart +
> -				MAX_SHARED_LIBS * sizeof(unsigned long),
> +				DATA_START_OFFSET_WORDS * sizeof(unsigned long),
>   				FLAT_DATA_ALIGN);
>   
>   		pr_debug("Allocated data+bss+stack (%u bytes): %lx\n",
> @@ -622,7 +630,8 @@ static int load_flat_file(struct linux_binprm *bprm,
>   		memp_size = len;
>   	} else {
>   
> -		len = text_len + data_len + extra + MAX_SHARED_LIBS * sizeof(u32);
> +		len = text_len + data_len + extra +
> +			DATA_START_OFFSET_WORDS * sizeof(u32);
>   		len = PAGE_ALIGN(len);
>   		textpos = vm_mmap(NULL, 0, len,
>   			PROT_READ | PROT_EXEC | PROT_WRITE, MAP_PRIVATE, 0);
> @@ -638,7 +647,7 @@ static int load_flat_file(struct linux_binprm *bprm,
>   
>   		realdatastart = textpos + ntohl(hdr->data_start);
>   		datapos = ALIGN(realdatastart +
> -				MAX_SHARED_LIBS * sizeof(u32),
> +				DATA_START_OFFSET_WORDS * sizeof(u32),
>   				FLAT_DATA_ALIGN);
>   
>   		reloc = (__be32 __user *)
> @@ -714,7 +723,7 @@ static int load_flat_file(struct linux_binprm *bprm,
>   			ret = result;
>   			pr_err("Unable to read code+data+bss, errno %d\n", ret);
>   			vm_munmap(textpos, text_len + data_len + extra +
> -				MAX_SHARED_LIBS * sizeof(u32));
> +				  DATA_START_OFFSET_WORDS * sizeof(u32));
>   			goto err;
>   		}
>   	}
> 

Thanks, otherwise looks good.

Acked-by: Greg Ungerer <gerg@linux-m68k.org>

I will push this into my m68knommu tree, for-next branch.
I just carry the flat format changes in that tree now to make my life easier.

Regards
Greg


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v4 1/2] binfmt_flat: allow not offsetting data start
  2021-04-17  4:52   ` Greg Ungerer
@ 2021-04-17  4:54     ` Damien Le Moal
  2021-04-18  2:38       ` Greg Ungerer
  0 siblings, 1 reply; 11+ messages in thread
From: Damien Le Moal @ 2021-04-17  4:54 UTC (permalink / raw)
  To: Greg Ungerer, uclinux-dev, Palmer Dabbelt, linux-riscv
  Cc: linux-kernel, Anup Patel, Christoph Hellwig

On 2021/04/17 13:52, Greg Ungerer wrote:
> 
> On 17/4/21 11:10 am, Damien Le Moal wrote:
>> Commit 2217b9826246 ("binfmt_flat: revert "binfmt_flat: don't offset
>> the data start"") restored offsetting the start of the data section by
>> a number of words defined by MAX_SHARED_LIBS. As a result, since
>> MAX_SHARED_LIBS is never 0, a gap between the text and data sections
>> always exists. For architectures which cannot support a such gap
>> between the text and data sections (e.g. riscv nommu), flat binary
>> programs cannot be executed.
>>
>> To allow an architecture to request no data start offset to allow for
>> contiguous text and data sections for binaries flagged with
>> FLAT_FLAG_RAM, introduce the new config option
>> CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET. Using this new option, the
>> macro DATA_START_OFFSET_WORDS is conditionally defined in binfmt_flat.c
>> to MAX_SHARED_LIBS for architectures tolerating or needing the data
>> start offset (CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET disabled case)
>> and to 0 when CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET is enabled.
>> DATA_START_OFFSET_WORDS is used in load_flat_file() to calculate the
>> data section length and start position.
>>
>> Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
>> ---
>>   fs/Kconfig.binfmt |  3 +++
>>   fs/binfmt_flat.c  | 19 ++++++++++++++-----
>>   2 files changed, 17 insertions(+), 5 deletions(-)
>>
>> diff --git a/fs/Kconfig.binfmt b/fs/Kconfig.binfmt
>> index c6f1c8c1934e..06fb7a93a1bd 100644
>> --- a/fs/Kconfig.binfmt
>> +++ b/fs/Kconfig.binfmt
>> @@ -112,6 +112,9 @@ config BINFMT_FLAT_ARGVP_ENVP_ON_STACK
>>   config BINFMT_FLAT_OLD_ALWAYS_RAM
>>   	bool
>>   
>> +config BINFMT_FLAT_NO_DATA_START_OFFSET
>> +	bool
>> +
>>   config BINFMT_FLAT_OLD
>>   	bool "Enable support for very old legacy flat binaries"
>>   	depends on BINFMT_FLAT
>> diff --git a/fs/binfmt_flat.c b/fs/binfmt_flat.c
>> index b9c658e0548e..1dc68dfba3e0 100644
>> --- a/fs/binfmt_flat.c
>> +++ b/fs/binfmt_flat.c
>> @@ -74,6 +74,12 @@
>>   #define	MAX_SHARED_LIBS			(1)
>>   #endif
>>   
>> +#ifdef CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET
>> +#define DATA_START_OFFSET_WORDS		(0)
>> +#else
>> +#define DATA_START_OFFSET_WORDS		(MAX_SHARED_LIBS)
>> +#endif
>> +
>>   struct lib_info {
>>   	struct {
>>   		unsigned long start_code;		/* Start of text segment */
>> @@ -560,6 +566,7 @@ static int load_flat_file(struct linux_binprm *bprm,
>>   	 * it all together.
>>   	 */
>>   	if (!IS_ENABLED(CONFIG_MMU) && !(flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP))) {
>> +
> 
> Random white space change...
> Don't worry about re-spinning though, I will just edit this chunk out.

Oops. Sorry about that. I should have better checked :)

> 
> 
>>   		/*
>>   		 * this should give us a ROM ptr,  but if it doesn't we don't
>>   		 * really care
>> @@ -576,7 +583,8 @@ static int load_flat_file(struct linux_binprm *bprm,
>>   			goto err;
>>   		}
>>   
>> -		len = data_len + extra + MAX_SHARED_LIBS * sizeof(unsigned long);
>> +		len = data_len + extra +
>> +			DATA_START_OFFSET_WORDS * sizeof(unsigned long);
>>   		len = PAGE_ALIGN(len);
>>   		realdatastart = vm_mmap(NULL, 0, len,
>>   			PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE, 0);
>> @@ -591,7 +599,7 @@ static int load_flat_file(struct linux_binprm *bprm,
>>   			goto err;
>>   		}
>>   		datapos = ALIGN(realdatastart +
>> -				MAX_SHARED_LIBS * sizeof(unsigned long),
>> +				DATA_START_OFFSET_WORDS * sizeof(unsigned long),
>>   				FLAT_DATA_ALIGN);
>>   
>>   		pr_debug("Allocated data+bss+stack (%u bytes): %lx\n",
>> @@ -622,7 +630,8 @@ static int load_flat_file(struct linux_binprm *bprm,
>>   		memp_size = len;
>>   	} else {
>>   
>> -		len = text_len + data_len + extra + MAX_SHARED_LIBS * sizeof(u32);
>> +		len = text_len + data_len + extra +
>> +			DATA_START_OFFSET_WORDS * sizeof(u32);
>>   		len = PAGE_ALIGN(len);
>>   		textpos = vm_mmap(NULL, 0, len,
>>   			PROT_READ | PROT_EXEC | PROT_WRITE, MAP_PRIVATE, 0);
>> @@ -638,7 +647,7 @@ static int load_flat_file(struct linux_binprm *bprm,
>>   
>>   		realdatastart = textpos + ntohl(hdr->data_start);
>>   		datapos = ALIGN(realdatastart +
>> -				MAX_SHARED_LIBS * sizeof(u32),
>> +				DATA_START_OFFSET_WORDS * sizeof(u32),
>>   				FLAT_DATA_ALIGN);
>>   
>>   		reloc = (__be32 __user *)
>> @@ -714,7 +723,7 @@ static int load_flat_file(struct linux_binprm *bprm,
>>   			ret = result;
>>   			pr_err("Unable to read code+data+bss, errno %d\n", ret);
>>   			vm_munmap(textpos, text_len + data_len + extra +
>> -				MAX_SHARED_LIBS * sizeof(u32));
>> +				  DATA_START_OFFSET_WORDS * sizeof(u32));
>>   			goto err;
>>   		}
>>   	}
>>
> 
> Thanks, otherwise looks good.
> 
> Acked-by: Greg Ungerer <gerg@linux-m68k.org>
> 
> I will push this into my m68knommu tree, for-next branch.
> I just carry the flat format changes in that tree now to make my life easier.

Great. Thanks !
Are you taking both patches or should Plamer take the riscv Kconfig change
through his tree ?


> 
> Regards
> Greg
> 
> 


-- 
Damien Le Moal
Western Digital Research

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v4 2/2] riscv: Disable data start offset in flat binaries
  2021-04-17  1:10 ` [PATCH v4 2/2] riscv: Disable data start offset in flat binaries Damien Le Moal
@ 2021-04-17  4:56   ` Greg Ungerer
  0 siblings, 0 replies; 11+ messages in thread
From: Greg Ungerer @ 2021-04-17  4:56 UTC (permalink / raw)
  To: Damien Le Moal, uclinux-dev, Palmer Dabbelt, linux-riscv
  Cc: linux-kernel, Anup Patel, Christoph Hellwig


On 17/4/21 11:10 am, Damien Le Moal wrote:
> uclibc/gcc combined with elf2flt riscv linker file fully resolve the
> PC relative __global_pointer$ value at compile time and do not generate
> a relocation entry to set a correct value of the gp register at runtime.
> As a result, if the flatbin loader offsets the start of the data
> section, the relative position change between the text and data sections
> compared to the compile time positions results in an incorrect gp value
> being used. This causes flatbin executables to crash.
> 
> Avoid this problem by enabling CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET
> automatically when CONFIG_RISCV is enabled and CONFIG_MMU is disabled.
> 
> Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
> Acked-by: Palmer Dabbelt <palmerdabbelt@google.com>

Acked-by: Greg Ungerer <gerg@linux-m68k.org>

Palmer do you want me to take this via my tree with 1/2 in the series,
or are you going to pick it up?

Regards
Greg


> ---
>   arch/riscv/Kconfig | 1 +
>   1 file changed, 1 insertion(+)
> 
> diff --git a/arch/riscv/Kconfig b/arch/riscv/Kconfig
> index 4515a10c5d22..add528eb9235 100644
> --- a/arch/riscv/Kconfig
> +++ b/arch/riscv/Kconfig
> @@ -33,6 +33,7 @@ config RISCV
>   	select ARCH_WANT_DEFAULT_TOPDOWN_MMAP_LAYOUT if MMU
>   	select ARCH_WANT_FRAME_POINTERS
>   	select ARCH_WANT_HUGE_PMD_SHARE if 64BIT
> +	select BINFMT_FLAT_NO_DATA_START_OFFSET if !MMU
>   	select CLONE_BACKWARDS
>   	select CLINT_TIMER if !MMU
>   	select COMMON_CLK
> 

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v4 1/2] binfmt_flat: allow not offsetting data start
  2021-04-17  4:54     ` Damien Le Moal
@ 2021-04-18  2:38       ` Greg Ungerer
  2021-04-22 12:00         ` Damien Le Moal
  0 siblings, 1 reply; 11+ messages in thread
From: Greg Ungerer @ 2021-04-18  2:38 UTC (permalink / raw)
  To: Damien Le Moal, uclinux-dev, Palmer Dabbelt, linux-riscv
  Cc: linux-kernel, Anup Patel, Christoph Hellwig



On 17/4/21 2:54 pm, Damien Le Moal wrote:
> On 2021/04/17 13:52, Greg Ungerer wrote:
>>
>> On 17/4/21 11:10 am, Damien Le Moal wrote:
>>> Commit 2217b9826246 ("binfmt_flat: revert "binfmt_flat: don't offset
>>> the data start"") restored offsetting the start of the data section by
>>> a number of words defined by MAX_SHARED_LIBS. As a result, since
>>> MAX_SHARED_LIBS is never 0, a gap between the text and data sections
>>> always exists. For architectures which cannot support a such gap
>>> between the text and data sections (e.g. riscv nommu), flat binary
>>> programs cannot be executed.
>>>
>>> To allow an architecture to request no data start offset to allow for
>>> contiguous text and data sections for binaries flagged with
>>> FLAT_FLAG_RAM, introduce the new config option
>>> CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET. Using this new option, the
>>> macro DATA_START_OFFSET_WORDS is conditionally defined in binfmt_flat.c
>>> to MAX_SHARED_LIBS for architectures tolerating or needing the data
>>> start offset (CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET disabled case)
>>> and to 0 when CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET is enabled.
>>> DATA_START_OFFSET_WORDS is used in load_flat_file() to calculate the
>>> data section length and start position.
>>>
>>> Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
>>> ---
>>>    fs/Kconfig.binfmt |  3 +++
>>>    fs/binfmt_flat.c  | 19 ++++++++++++++-----
>>>    2 files changed, 17 insertions(+), 5 deletions(-)
>>>
>>> diff --git a/fs/Kconfig.binfmt b/fs/Kconfig.binfmt
>>> index c6f1c8c1934e..06fb7a93a1bd 100644
>>> --- a/fs/Kconfig.binfmt
>>> +++ b/fs/Kconfig.binfmt
>>> @@ -112,6 +112,9 @@ config BINFMT_FLAT_ARGVP_ENVP_ON_STACK
>>>    config BINFMT_FLAT_OLD_ALWAYS_RAM
>>>    	bool
>>>    
>>> +config BINFMT_FLAT_NO_DATA_START_OFFSET
>>> +	bool
>>> +
>>>    config BINFMT_FLAT_OLD
>>>    	bool "Enable support for very old legacy flat binaries"
>>>    	depends on BINFMT_FLAT
>>> diff --git a/fs/binfmt_flat.c b/fs/binfmt_flat.c
>>> index b9c658e0548e..1dc68dfba3e0 100644
>>> --- a/fs/binfmt_flat.c
>>> +++ b/fs/binfmt_flat.c
>>> @@ -74,6 +74,12 @@
>>>    #define	MAX_SHARED_LIBS			(1)
>>>    #endif
>>>    
>>> +#ifdef CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET
>>> +#define DATA_START_OFFSET_WORDS		(0)
>>> +#else
>>> +#define DATA_START_OFFSET_WORDS		(MAX_SHARED_LIBS)
>>> +#endif
>>> +
>>>    struct lib_info {
>>>    	struct {
>>>    		unsigned long start_code;		/* Start of text segment */
>>> @@ -560,6 +566,7 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>    	 * it all together.
>>>    	 */
>>>    	if (!IS_ENABLED(CONFIG_MMU) && !(flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP))) {
>>> +
>>
>> Random white space change...
>> Don't worry about re-spinning though, I will just edit this chunk out.
> 
> Oops. Sorry about that. I should have better checked :)
> 
>>
>>
>>>    		/*
>>>    		 * this should give us a ROM ptr,  but if it doesn't we don't
>>>    		 * really care
>>> @@ -576,7 +583,8 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>    			goto err;
>>>    		}
>>>    
>>> -		len = data_len + extra + MAX_SHARED_LIBS * sizeof(unsigned long);
>>> +		len = data_len + extra +
>>> +			DATA_START_OFFSET_WORDS * sizeof(unsigned long);
>>>    		len = PAGE_ALIGN(len);
>>>    		realdatastart = vm_mmap(NULL, 0, len,
>>>    			PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE, 0);
>>> @@ -591,7 +599,7 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>    			goto err;
>>>    		}
>>>    		datapos = ALIGN(realdatastart +
>>> -				MAX_SHARED_LIBS * sizeof(unsigned long),
>>> +				DATA_START_OFFSET_WORDS * sizeof(unsigned long),
>>>    				FLAT_DATA_ALIGN);
>>>    
>>>    		pr_debug("Allocated data+bss+stack (%u bytes): %lx\n",
>>> @@ -622,7 +630,8 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>    		memp_size = len;
>>>    	} else {
>>>    
>>> -		len = text_len + data_len + extra + MAX_SHARED_LIBS * sizeof(u32);
>>> +		len = text_len + data_len + extra +
>>> +			DATA_START_OFFSET_WORDS * sizeof(u32);
>>>    		len = PAGE_ALIGN(len);
>>>    		textpos = vm_mmap(NULL, 0, len,
>>>    			PROT_READ | PROT_EXEC | PROT_WRITE, MAP_PRIVATE, 0);
>>> @@ -638,7 +647,7 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>    
>>>    		realdatastart = textpos + ntohl(hdr->data_start);
>>>    		datapos = ALIGN(realdatastart +
>>> -				MAX_SHARED_LIBS * sizeof(u32),
>>> +				DATA_START_OFFSET_WORDS * sizeof(u32),
>>>    				FLAT_DATA_ALIGN);
>>>    
>>>    		reloc = (__be32 __user *)
>>> @@ -714,7 +723,7 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>    			ret = result;
>>>    			pr_err("Unable to read code+data+bss, errno %d\n", ret);
>>>    			vm_munmap(textpos, text_len + data_len + extra +
>>> -				MAX_SHARED_LIBS * sizeof(u32));
>>> +				  DATA_START_OFFSET_WORDS * sizeof(u32));
>>>    			goto err;
>>>    		}
>>>    	}
>>>
>>
>> Thanks, otherwise looks good.
>>
>> Acked-by: Greg Ungerer <gerg@linux-m68k.org>
>>
>> I will push this into my m68knommu tree, for-next branch.
>> I just carry the flat format changes in that tree now to make my life easier.
> 
> Great. Thanks !
> Are you taking both patches or should Plamer take the riscv Kconfig change
> through his tree ?

I am happy to take both.
Palmer?

Regards
Greg


_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v4 1/2] binfmt_flat: allow not offsetting data start
  2021-04-18  2:38       ` Greg Ungerer
@ 2021-04-22 12:00         ` Damien Le Moal
  2021-04-23  4:04           ` Palmer Dabbelt
  0 siblings, 1 reply; 11+ messages in thread
From: Damien Le Moal @ 2021-04-22 12:00 UTC (permalink / raw)
  To: Greg Ungerer, uclinux-dev, Palmer Dabbelt, linux-riscv
  Cc: linux-kernel, Anup Patel, Christoph Hellwig

On 2021/04/18 11:38, Greg Ungerer wrote:
> 
> 
> On 17/4/21 2:54 pm, Damien Le Moal wrote:
>> On 2021/04/17 13:52, Greg Ungerer wrote:
>>>
>>> On 17/4/21 11:10 am, Damien Le Moal wrote:
>>>> Commit 2217b9826246 ("binfmt_flat: revert "binfmt_flat: don't offset
>>>> the data start"") restored offsetting the start of the data section by
>>>> a number of words defined by MAX_SHARED_LIBS. As a result, since
>>>> MAX_SHARED_LIBS is never 0, a gap between the text and data sections
>>>> always exists. For architectures which cannot support a such gap
>>>> between the text and data sections (e.g. riscv nommu), flat binary
>>>> programs cannot be executed.
>>>>
>>>> To allow an architecture to request no data start offset to allow for
>>>> contiguous text and data sections for binaries flagged with
>>>> FLAT_FLAG_RAM, introduce the new config option
>>>> CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET. Using this new option, the
>>>> macro DATA_START_OFFSET_WORDS is conditionally defined in binfmt_flat.c
>>>> to MAX_SHARED_LIBS for architectures tolerating or needing the data
>>>> start offset (CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET disabled case)
>>>> and to 0 when CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET is enabled.
>>>> DATA_START_OFFSET_WORDS is used in load_flat_file() to calculate the
>>>> data section length and start position.
>>>>
>>>> Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
>>>> ---
>>>>    fs/Kconfig.binfmt |  3 +++
>>>>    fs/binfmt_flat.c  | 19 ++++++++++++++-----
>>>>    2 files changed, 17 insertions(+), 5 deletions(-)
>>>>
>>>> diff --git a/fs/Kconfig.binfmt b/fs/Kconfig.binfmt
>>>> index c6f1c8c1934e..06fb7a93a1bd 100644
>>>> --- a/fs/Kconfig.binfmt
>>>> +++ b/fs/Kconfig.binfmt
>>>> @@ -112,6 +112,9 @@ config BINFMT_FLAT_ARGVP_ENVP_ON_STACK
>>>>    config BINFMT_FLAT_OLD_ALWAYS_RAM
>>>>    	bool
>>>>    
>>>> +config BINFMT_FLAT_NO_DATA_START_OFFSET
>>>> +	bool
>>>> +
>>>>    config BINFMT_FLAT_OLD
>>>>    	bool "Enable support for very old legacy flat binaries"
>>>>    	depends on BINFMT_FLAT
>>>> diff --git a/fs/binfmt_flat.c b/fs/binfmt_flat.c
>>>> index b9c658e0548e..1dc68dfba3e0 100644
>>>> --- a/fs/binfmt_flat.c
>>>> +++ b/fs/binfmt_flat.c
>>>> @@ -74,6 +74,12 @@
>>>>    #define	MAX_SHARED_LIBS			(1)
>>>>    #endif
>>>>    
>>>> +#ifdef CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET
>>>> +#define DATA_START_OFFSET_WORDS		(0)
>>>> +#else
>>>> +#define DATA_START_OFFSET_WORDS		(MAX_SHARED_LIBS)
>>>> +#endif
>>>> +
>>>>    struct lib_info {
>>>>    	struct {
>>>>    		unsigned long start_code;		/* Start of text segment */
>>>> @@ -560,6 +566,7 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>>    	 * it all together.
>>>>    	 */
>>>>    	if (!IS_ENABLED(CONFIG_MMU) && !(flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP))) {
>>>> +
>>>
>>> Random white space change...
>>> Don't worry about re-spinning though, I will just edit this chunk out.
>>
>> Oops. Sorry about that. I should have better checked :)
>>
>>>
>>>
>>>>    		/*
>>>>    		 * this should give us a ROM ptr,  but if it doesn't we don't
>>>>    		 * really care
>>>> @@ -576,7 +583,8 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>>    			goto err;
>>>>    		}
>>>>    
>>>> -		len = data_len + extra + MAX_SHARED_LIBS * sizeof(unsigned long);
>>>> +		len = data_len + extra +
>>>> +			DATA_START_OFFSET_WORDS * sizeof(unsigned long);
>>>>    		len = PAGE_ALIGN(len);
>>>>    		realdatastart = vm_mmap(NULL, 0, len,
>>>>    			PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE, 0);
>>>> @@ -591,7 +599,7 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>>    			goto err;
>>>>    		}
>>>>    		datapos = ALIGN(realdatastart +
>>>> -				MAX_SHARED_LIBS * sizeof(unsigned long),
>>>> +				DATA_START_OFFSET_WORDS * sizeof(unsigned long),
>>>>    				FLAT_DATA_ALIGN);
>>>>    
>>>>    		pr_debug("Allocated data+bss+stack (%u bytes): %lx\n",
>>>> @@ -622,7 +630,8 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>>    		memp_size = len;
>>>>    	} else {
>>>>    
>>>> -		len = text_len + data_len + extra + MAX_SHARED_LIBS * sizeof(u32);
>>>> +		len = text_len + data_len + extra +
>>>> +			DATA_START_OFFSET_WORDS * sizeof(u32);
>>>>    		len = PAGE_ALIGN(len);
>>>>    		textpos = vm_mmap(NULL, 0, len,
>>>>    			PROT_READ | PROT_EXEC | PROT_WRITE, MAP_PRIVATE, 0);
>>>> @@ -638,7 +647,7 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>>    
>>>>    		realdatastart = textpos + ntohl(hdr->data_start);
>>>>    		datapos = ALIGN(realdatastart +
>>>> -				MAX_SHARED_LIBS * sizeof(u32),
>>>> +				DATA_START_OFFSET_WORDS * sizeof(u32),
>>>>    				FLAT_DATA_ALIGN);
>>>>    
>>>>    		reloc = (__be32 __user *)
>>>> @@ -714,7 +723,7 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>>    			ret = result;
>>>>    			pr_err("Unable to read code+data+bss, errno %d\n", ret);
>>>>    			vm_munmap(textpos, text_len + data_len + extra +
>>>> -				MAX_SHARED_LIBS * sizeof(u32));
>>>> +				  DATA_START_OFFSET_WORDS * sizeof(u32));
>>>>    			goto err;
>>>>    		}
>>>>    	}
>>>>
>>>
>>> Thanks, otherwise looks good.
>>>
>>> Acked-by: Greg Ungerer <gerg@linux-m68k.org>
>>>
>>> I will push this into my m68knommu tree, for-next branch.
>>> I just carry the flat format changes in that tree now to make my life easier.
>>
>> Great. Thanks !
>> Are you taking both patches or should Plamer take the riscv Kconfig change
>> through his tree ?
> 
> I am happy to take both.
> Palmer?

Palmer,

Ping !


-- 
Damien Le Moal
Western Digital Research

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v4 1/2] binfmt_flat: allow not offsetting data start
  2021-04-22 12:00         ` Damien Le Moal
@ 2021-04-23  4:04           ` Palmer Dabbelt
  2021-04-23  5:38             ` Greg Ungerer
  0 siblings, 1 reply; 11+ messages in thread
From: Palmer Dabbelt @ 2021-04-23  4:04 UTC (permalink / raw)
  To: Damien Le Moal
  Cc: gerg, uclinux-dev, linux-riscv, linux-kernel, Anup Patel,
	Christoph Hellwig

On Thu, 22 Apr 2021 05:00:32 PDT (-0700), Damien Le Moal wrote:
>> On 2021/04/18 11:38, Greg Ungerer wrote:
>> 
>> 
>> On 17/4/21 2:54 pm, Damien Le Moal wrote:
>>> On 2021/04/17 13:52, Greg Ungerer wrote:
>>>>
>>>> On 17/4/21 11:10 am, Damien Le Moal wrote:
>>>>> Commit 2217b9826246 ("binfmt_flat: revert "binfmt_flat: don't offset
>>>>> the data start"") restored offsetting the start of the data section by
>>>>> a number of words defined by MAX_SHARED_LIBS. As a result, since
>>>>> MAX_SHARED_LIBS is never 0, a gap between the text and data sections
>>>>> always exists. For architectures which cannot support a such gap
>>>>> between the text and data sections (e.g. riscv nommu), flat binary
>>>>> programs cannot be executed.
>>>>>
>>>>> To allow an architecture to request no data start offset to allow for
>>>>> contiguous text and data sections for binaries flagged with
>>>>> FLAT_FLAG_RAM, introduce the new config option
>>>>> CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET. Using this new option, the
>>>>> macro DATA_START_OFFSET_WORDS is conditionally defined in binfmt_flat.c
>>>>> to MAX_SHARED_LIBS for architectures tolerating or needing the data
>>>>> start offset (CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET disabled case)
>>>>> and to 0 when CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET is enabled.
>>>>> DATA_START_OFFSET_WORDS is used in load_flat_file() to calculate the
>>>>> data section length and start position.
>>>>>
>>>>> Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
>>>>> ---
>>>>>    fs/Kconfig.binfmt |  3 +++
>>>>>    fs/binfmt_flat.c  | 19 ++++++++++++++-----
>>>>>    2 files changed, 17 insertions(+), 5 deletions(-)
>>>>>
>>>>> diff --git a/fs/Kconfig.binfmt b/fs/Kconfig.binfmt
>>>>> index c6f1c8c1934e..06fb7a93a1bd 100644
>>>>> --- a/fs/Kconfig.binfmt
>>>>> +++ b/fs/Kconfig.binfmt
>>>>> @@ -112,6 +112,9 @@ config BINFMT_FLAT_ARGVP_ENVP_ON_STACK
>>>>>    config BINFMT_FLAT_OLD_ALWAYS_RAM
>>>>>    	bool
>>>>>    
>>>>> +config BINFMT_FLAT_NO_DATA_START_OFFSET
>>>>> +	bool
>>>>> +
>>>>>    config BINFMT_FLAT_OLD
>>>>>    	bool "Enable support for very old legacy flat binaries"
>>>>>    	depends on BINFMT_FLAT
>>>>> diff --git a/fs/binfmt_flat.c b/fs/binfmt_flat.c
>>>>> index b9c658e0548e..1dc68dfba3e0 100644
>>>>> --- a/fs/binfmt_flat.c
>>>>> +++ b/fs/binfmt_flat.c
>>>>> @@ -74,6 +74,12 @@
>>>>>    #define	MAX_SHARED_LIBS			(1)
>>>>>    #endif
>>>>>    
>>>>> +#ifdef CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET
>>>>> +#define DATA_START_OFFSET_WORDS		(0)
>>>>> +#else
>>>>> +#define DATA_START_OFFSET_WORDS		(MAX_SHARED_LIBS)
>>>>> +#endif
>>>>> +
>>>>>    struct lib_info {
>>>>>    	struct {
>>>>>    		unsigned long start_code;		/* Start of text segment */
>>>>> @@ -560,6 +566,7 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>>>    	 * it all together.
>>>>>    	 */
>>>>>    	if (!IS_ENABLED(CONFIG_MMU) && !(flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP))) {
>>>>> +
>>>>
>>>> Random white space change...
>>>> Don't worry about re-spinning though, I will just edit this chunk out.
>>>
>>> Oops. Sorry about that. I should have better checked :)
>>>
>>>>
>>>>
>>>>>    		/*
>>>>>    		 * this should give us a ROM ptr,  but if it doesn't we don't
>>>>>    		 * really care
>>>>> @@ -576,7 +583,8 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>>>    			goto err;
>>>>>    		}
>>>>>    
>>>>> -		len = data_len + extra + MAX_SHARED_LIBS * sizeof(unsigned long);
>>>>> +		len = data_len + extra +
>>>>> +			DATA_START_OFFSET_WORDS * sizeof(unsigned long);
>>>>>    		len = PAGE_ALIGN(len);
>>>>>    		realdatastart = vm_mmap(NULL, 0, len,
>>>>>    			PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE, 0);
>>>>> @@ -591,7 +599,7 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>>>    			goto err;
>>>>>    		}
>>>>>    		datapos = ALIGN(realdatastart +
>>>>> -				MAX_SHARED_LIBS * sizeof(unsigned long),
>>>>> +				DATA_START_OFFSET_WORDS * sizeof(unsigned long),
>>>>>    				FLAT_DATA_ALIGN);
>>>>>    
>>>>>    		pr_debug("Allocated data+bss+stack (%u bytes): %lx\n",
>>>>> @@ -622,7 +630,8 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>>>    		memp_size = len;
>>>>>    	} else {
>>>>>    
>>>>> -		len = text_len + data_len + extra + MAX_SHARED_LIBS * sizeof(u32);
>>>>> +		len = text_len + data_len + extra +
>>>>> +			DATA_START_OFFSET_WORDS * sizeof(u32);
>>>>>    		len = PAGE_ALIGN(len);
>>>>>    		textpos = vm_mmap(NULL, 0, len,
>>>>>    			PROT_READ | PROT_EXEC | PROT_WRITE, MAP_PRIVATE, 0);
>>>>> @@ -638,7 +647,7 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>>>    
>>>>>    		realdatastart = textpos + ntohl(hdr->data_start);
>>>>>    		datapos = ALIGN(realdatastart +
>>>>> -				MAX_SHARED_LIBS * sizeof(u32),
>>>>> +				DATA_START_OFFSET_WORDS * sizeof(u32),
>>>>>    				FLAT_DATA_ALIGN);
>>>>>    
>>>>>    		reloc = (__be32 __user *)
>>>>> @@ -714,7 +723,7 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>>>    			ret = result;
>>>>>    			pr_err("Unable to read code+data+bss, errno %d\n", ret);
>>>>>    			vm_munmap(textpos, text_len + data_len + extra +
>>>>> -				MAX_SHARED_LIBS * sizeof(u32));
>>>>> +				  DATA_START_OFFSET_WORDS * sizeof(u32));
>>>>>    			goto err;
>>>>>    		}
>>>>>    	}
>>>>>
>>>>
>>>> Thanks, otherwise looks good.
>>>>
>>>> Acked-by: Greg Ungerer <gerg@linux-m68k.org>
>>>>
>>>> I will push this into my m68knommu tree, for-next branch.
>>>> I just carry the flat format changes in that tree now to make my life easier.
>>>
>>> Great. Thanks !
>>> Are you taking both patches or should Plamer take the riscv Kconfig change
>>> through his tree ?
>> 
>> I am happy to take both.
>> Palmer?
>
>Palmer,
>
>Ping !

I already Ack'd it, in the thread where we couldn't find the maintainer.  
Keeping these togther is fine with me, and it seems easiest.

>
>
>-- 
>Damien Le Moal
>Western Digital Research
>

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v4 1/2] binfmt_flat: allow not offsetting data start
  2021-04-23  4:04           ` Palmer Dabbelt
@ 2021-04-23  5:38             ` Greg Ungerer
  2021-04-23  6:44               ` Palmer Dabbelt
  0 siblings, 1 reply; 11+ messages in thread
From: Greg Ungerer @ 2021-04-23  5:38 UTC (permalink / raw)
  To: Palmer Dabbelt, Damien Le Moal
  Cc: uclinux-dev, linux-riscv, linux-kernel, Anup Patel, Christoph Hellwig



On 23/4/21 2:04 pm, Palmer Dabbelt wrote:
> On Thu, 22 Apr 2021 05:00:32 PDT (-0700), Damien Le Moal wrote:
>>> On 2021/04/18 11:38, Greg Ungerer wrote:
>>>
>>>
>>> On 17/4/21 2:54 pm, Damien Le Moal wrote:
>>>> On 2021/04/17 13:52, Greg Ungerer wrote:
>>>>>
>>>>> On 17/4/21 11:10 am, Damien Le Moal wrote:
>>>>>> Commit 2217b9826246 ("binfmt_flat: revert "binfmt_flat: don't offset
>>>>>> the data start"") restored offsetting the start of the data section by
>>>>>> a number of words defined by MAX_SHARED_LIBS. As a result, since
>>>>>> MAX_SHARED_LIBS is never 0, a gap between the text and data sections
>>>>>> always exists. For architectures which cannot support a such gap
>>>>>> between the text and data sections (e.g. riscv nommu), flat binary
>>>>>> programs cannot be executed.
>>>>>>
>>>>>> To allow an architecture to request no data start offset to allow for
>>>>>> contiguous text and data sections for binaries flagged with
>>>>>> FLAT_FLAG_RAM, introduce the new config option
>>>>>> CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET. Using this new option, the
>>>>>> macro DATA_START_OFFSET_WORDS is conditionally defined in binfmt_flat.c
>>>>>> to MAX_SHARED_LIBS for architectures tolerating or needing the data
>>>>>> start offset (CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET disabled case)
>>>>>> and to 0 when CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET is enabled.
>>>>>> DATA_START_OFFSET_WORDS is used in load_flat_file() to calculate the
>>>>>> data section length and start position.
>>>>>>
>>>>>> Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
>>>>>> ---
>>>>>>    fs/Kconfig.binfmt |  3 +++
>>>>>>    fs/binfmt_flat.c  | 19 ++++++++++++++-----
>>>>>>    2 files changed, 17 insertions(+), 5 deletions(-)
>>>>>>
>>>>>> diff --git a/fs/Kconfig.binfmt b/fs/Kconfig.binfmt
>>>>>> index c6f1c8c1934e..06fb7a93a1bd 100644
>>>>>> --- a/fs/Kconfig.binfmt
>>>>>> +++ b/fs/Kconfig.binfmt
>>>>>> @@ -112,6 +112,9 @@ config BINFMT_FLAT_ARGVP_ENVP_ON_STACK
>>>>>>    config BINFMT_FLAT_OLD_ALWAYS_RAM
>>>>>>        bool
>>>>>> +config BINFMT_FLAT_NO_DATA_START_OFFSET
>>>>>> +    bool
>>>>>> +
>>>>>>    config BINFMT_FLAT_OLD
>>>>>>        bool "Enable support for very old legacy flat binaries"
>>>>>>        depends on BINFMT_FLAT
>>>>>> diff --git a/fs/binfmt_flat.c b/fs/binfmt_flat.c
>>>>>> index b9c658e0548e..1dc68dfba3e0 100644
>>>>>> --- a/fs/binfmt_flat.c
>>>>>> +++ b/fs/binfmt_flat.c
>>>>>> @@ -74,6 +74,12 @@
>>>>>>    #define    MAX_SHARED_LIBS            (1)
>>>>>>    #endif
>>>>>> +#ifdef CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET
>>>>>> +#define DATA_START_OFFSET_WORDS        (0)
>>>>>> +#else
>>>>>> +#define DATA_START_OFFSET_WORDS        (MAX_SHARED_LIBS)
>>>>>> +#endif
>>>>>> +
>>>>>>    struct lib_info {
>>>>>>        struct {
>>>>>>            unsigned long start_code;        /* Start of text segment */
>>>>>> @@ -560,6 +566,7 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>>>>         * it all together.
>>>>>>         */
>>>>>>        if (!IS_ENABLED(CONFIG_MMU) && !(flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP))) {
>>>>>> +
>>>>>
>>>>> Random white space change...
>>>>> Don't worry about re-spinning though, I will just edit this chunk out.
>>>>
>>>> Oops. Sorry about that. I should have better checked :)
>>>>
>>>>>
>>>>>
>>>>>>            /*
>>>>>>             * this should give us a ROM ptr,  but if it doesn't we don't
>>>>>>             * really care
>>>>>> @@ -576,7 +583,8 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>>>>                goto err;
>>>>>>            }
>>>>>> -        len = data_len + extra + MAX_SHARED_LIBS * sizeof(unsigned long);
>>>>>> +        len = data_len + extra +
>>>>>> +            DATA_START_OFFSET_WORDS * sizeof(unsigned long);
>>>>>>            len = PAGE_ALIGN(len);
>>>>>>            realdatastart = vm_mmap(NULL, 0, len,
>>>>>>                PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE, 0);
>>>>>> @@ -591,7 +599,7 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>>>>                goto err;
>>>>>>            }
>>>>>>            datapos = ALIGN(realdatastart +
>>>>>> -                MAX_SHARED_LIBS * sizeof(unsigned long),
>>>>>> +                DATA_START_OFFSET_WORDS * sizeof(unsigned long),
>>>>>>                    FLAT_DATA_ALIGN);
>>>>>>            pr_debug("Allocated data+bss+stack (%u bytes): %lx\n",
>>>>>> @@ -622,7 +630,8 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>>>>            memp_size = len;
>>>>>>        } else {
>>>>>> -        len = text_len + data_len + extra + MAX_SHARED_LIBS * sizeof(u32);
>>>>>> +        len = text_len + data_len + extra +
>>>>>> +            DATA_START_OFFSET_WORDS * sizeof(u32);
>>>>>>            len = PAGE_ALIGN(len);
>>>>>>            textpos = vm_mmap(NULL, 0, len,
>>>>>>                PROT_READ | PROT_EXEC | PROT_WRITE, MAP_PRIVATE, 0);
>>>>>> @@ -638,7 +647,7 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>>>>            realdatastart = textpos + ntohl(hdr->data_start);
>>>>>>            datapos = ALIGN(realdatastart +
>>>>>> -                MAX_SHARED_LIBS * sizeof(u32),
>>>>>> +                DATA_START_OFFSET_WORDS * sizeof(u32),
>>>>>>                    FLAT_DATA_ALIGN);
>>>>>>            reloc = (__be32 __user *)
>>>>>> @@ -714,7 +723,7 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>>>>                ret = result;
>>>>>>                pr_err("Unable to read code+data+bss, errno %d\n", ret);
>>>>>>                vm_munmap(textpos, text_len + data_len + extra +
>>>>>> -                MAX_SHARED_LIBS * sizeof(u32));
>>>>>> +                  DATA_START_OFFSET_WORDS * sizeof(u32));
>>>>>>                goto err;
>>>>>>            }
>>>>>>        }
>>>>>>
>>>>>
>>>>> Thanks, otherwise looks good.
>>>>>
>>>>> Acked-by: Greg Ungerer <gerg@linux-m68k.org>
>>>>>
>>>>> I will push this into my m68knommu tree, for-next branch.
>>>>> I just carry the flat format changes in that tree now to make my life easier.
>>>>
>>>> Great. Thanks !
>>>> Are you taking both patches or should Plamer take the riscv Kconfig change
>>>> through his tree ?
>>>
>>> I am happy to take both.
>>> Palmer?
>>
>> Palmer,
>>
>> Ping !
> 
> I already Ack'd it, in the thread where we couldn't find the maintainer. Keeping these togther is fine with me, and it seems easiest.

The 2/2 patch has been applied to the m68knommu git tree, for-next branch.

Regards
Greg



_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v4 1/2] binfmt_flat: allow not offsetting data start
  2021-04-23  5:38             ` Greg Ungerer
@ 2021-04-23  6:44               ` Palmer Dabbelt
  0 siblings, 0 replies; 11+ messages in thread
From: Palmer Dabbelt @ 2021-04-23  6:44 UTC (permalink / raw)
  To: gerg
  Cc: Damien Le Moal, uclinux-dev, linux-riscv, linux-kernel,
	Anup Patel, Christoph Hellwig

On Thu, 22 Apr 2021 22:38:36 PDT (-0700), gerg@linux-m68k.org wrote:
>
>
> On 23/4/21 2:04 pm, Palmer Dabbelt wrote:
>> On Thu, 22 Apr 2021 05:00:32 PDT (-0700), Damien Le Moal wrote:
>>>> On 2021/04/18 11:38, Greg Ungerer wrote:
>>>>
>>>>
>>>> On 17/4/21 2:54 pm, Damien Le Moal wrote:
>>>>> On 2021/04/17 13:52, Greg Ungerer wrote:
>>>>>>
>>>>>> On 17/4/21 11:10 am, Damien Le Moal wrote:
>>>>>>> Commit 2217b9826246 ("binfmt_flat: revert "binfmt_flat: don't offset
>>>>>>> the data start"") restored offsetting the start of the data section by
>>>>>>> a number of words defined by MAX_SHARED_LIBS. As a result, since
>>>>>>> MAX_SHARED_LIBS is never 0, a gap between the text and data sections
>>>>>>> always exists. For architectures which cannot support a such gap
>>>>>>> between the text and data sections (e.g. riscv nommu), flat binary
>>>>>>> programs cannot be executed.
>>>>>>>
>>>>>>> To allow an architecture to request no data start offset to allow for
>>>>>>> contiguous text and data sections for binaries flagged with
>>>>>>> FLAT_FLAG_RAM, introduce the new config option
>>>>>>> CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET. Using this new option, the
>>>>>>> macro DATA_START_OFFSET_WORDS is conditionally defined in binfmt_flat.c
>>>>>>> to MAX_SHARED_LIBS for architectures tolerating or needing the data
>>>>>>> start offset (CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET disabled case)
>>>>>>> and to 0 when CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET is enabled.
>>>>>>> DATA_START_OFFSET_WORDS is used in load_flat_file() to calculate the
>>>>>>> data section length and start position.
>>>>>>>
>>>>>>> Signed-off-by: Damien Le Moal <damien.lemoal@wdc.com>
>>>>>>> ---
>>>>>>>    fs/Kconfig.binfmt |  3 +++
>>>>>>>    fs/binfmt_flat.c  | 19 ++++++++++++++-----
>>>>>>>    2 files changed, 17 insertions(+), 5 deletions(-)
>>>>>>>
>>>>>>> diff --git a/fs/Kconfig.binfmt b/fs/Kconfig.binfmt
>>>>>>> index c6f1c8c1934e..06fb7a93a1bd 100644
>>>>>>> --- a/fs/Kconfig.binfmt
>>>>>>> +++ b/fs/Kconfig.binfmt
>>>>>>> @@ -112,6 +112,9 @@ config BINFMT_FLAT_ARGVP_ENVP_ON_STACK
>>>>>>>    config BINFMT_FLAT_OLD_ALWAYS_RAM
>>>>>>>        bool
>>>>>>> +config BINFMT_FLAT_NO_DATA_START_OFFSET
>>>>>>> +    bool
>>>>>>> +
>>>>>>>    config BINFMT_FLAT_OLD
>>>>>>>        bool "Enable support for very old legacy flat binaries"
>>>>>>>        depends on BINFMT_FLAT
>>>>>>> diff --git a/fs/binfmt_flat.c b/fs/binfmt_flat.c
>>>>>>> index b9c658e0548e..1dc68dfba3e0 100644
>>>>>>> --- a/fs/binfmt_flat.c
>>>>>>> +++ b/fs/binfmt_flat.c
>>>>>>> @@ -74,6 +74,12 @@
>>>>>>>    #define    MAX_SHARED_LIBS            (1)
>>>>>>>    #endif
>>>>>>> +#ifdef CONFIG_BINFMT_FLAT_NO_DATA_START_OFFSET
>>>>>>> +#define DATA_START_OFFSET_WORDS        (0)
>>>>>>> +#else
>>>>>>> +#define DATA_START_OFFSET_WORDS        (MAX_SHARED_LIBS)
>>>>>>> +#endif
>>>>>>> +
>>>>>>>    struct lib_info {
>>>>>>>        struct {
>>>>>>>            unsigned long start_code;        /* Start of text segment */
>>>>>>> @@ -560,6 +566,7 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>>>>>         * it all together.
>>>>>>>         */
>>>>>>>        if (!IS_ENABLED(CONFIG_MMU) && !(flags & (FLAT_FLAG_RAM|FLAT_FLAG_GZIP))) {
>>>>>>> +
>>>>>>
>>>>>> Random white space change...
>>>>>> Don't worry about re-spinning though, I will just edit this chunk out.
>>>>>
>>>>> Oops. Sorry about that. I should have better checked :)
>>>>>
>>>>>>
>>>>>>
>>>>>>>            /*
>>>>>>>             * this should give us a ROM ptr,  but if it doesn't we don't
>>>>>>>             * really care
>>>>>>> @@ -576,7 +583,8 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>>>>>                goto err;
>>>>>>>            }
>>>>>>> -        len = data_len + extra + MAX_SHARED_LIBS * sizeof(unsigned long);
>>>>>>> +        len = data_len + extra +
>>>>>>> +            DATA_START_OFFSET_WORDS * sizeof(unsigned long);
>>>>>>>            len = PAGE_ALIGN(len);
>>>>>>>            realdatastart = vm_mmap(NULL, 0, len,
>>>>>>>                PROT_READ|PROT_WRITE|PROT_EXEC, MAP_PRIVATE, 0);
>>>>>>> @@ -591,7 +599,7 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>>>>>                goto err;
>>>>>>>            }
>>>>>>>            datapos = ALIGN(realdatastart +
>>>>>>> -                MAX_SHARED_LIBS * sizeof(unsigned long),
>>>>>>> +                DATA_START_OFFSET_WORDS * sizeof(unsigned long),
>>>>>>>                    FLAT_DATA_ALIGN);
>>>>>>>            pr_debug("Allocated data+bss+stack (%u bytes): %lx\n",
>>>>>>> @@ -622,7 +630,8 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>>>>>            memp_size = len;
>>>>>>>        } else {
>>>>>>> -        len = text_len + data_len + extra + MAX_SHARED_LIBS * sizeof(u32);
>>>>>>> +        len = text_len + data_len + extra +
>>>>>>> +            DATA_START_OFFSET_WORDS * sizeof(u32);
>>>>>>>            len = PAGE_ALIGN(len);
>>>>>>>            textpos = vm_mmap(NULL, 0, len,
>>>>>>>                PROT_READ | PROT_EXEC | PROT_WRITE, MAP_PRIVATE, 0);
>>>>>>> @@ -638,7 +647,7 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>>>>>            realdatastart = textpos + ntohl(hdr->data_start);
>>>>>>>            datapos = ALIGN(realdatastart +
>>>>>>> -                MAX_SHARED_LIBS * sizeof(u32),
>>>>>>> +                DATA_START_OFFSET_WORDS * sizeof(u32),
>>>>>>>                    FLAT_DATA_ALIGN);
>>>>>>>            reloc = (__be32 __user *)
>>>>>>> @@ -714,7 +723,7 @@ static int load_flat_file(struct linux_binprm *bprm,
>>>>>>>                ret = result;
>>>>>>>                pr_err("Unable to read code+data+bss, errno %d\n", ret);
>>>>>>>                vm_munmap(textpos, text_len + data_len + extra +
>>>>>>> -                MAX_SHARED_LIBS * sizeof(u32));
>>>>>>> +                  DATA_START_OFFSET_WORDS * sizeof(u32));
>>>>>>>                goto err;
>>>>>>>            }
>>>>>>>        }
>>>>>>>
>>>>>>
>>>>>> Thanks, otherwise looks good.
>>>>>>
>>>>>> Acked-by: Greg Ungerer <gerg@linux-m68k.org>
>>>>>>
>>>>>> I will push this into my m68knommu tree, for-next branch.
>>>>>> I just carry the flat format changes in that tree now to make my life easier.
>>>>>
>>>>> Great. Thanks !
>>>>> Are you taking both patches or should Plamer take the riscv Kconfig change
>>>>> through his tree ?
>>>>
>>>> I am happy to take both.
>>>> Palmer?
>>>
>>> Palmer,
>>>
>>> Ping !
>>
>> I already Ack'd it, in the thread where we couldn't find the maintainer. Keeping these togther is fine with me, and it seems easiest.
>
> The 2/2 patch has been applied to the m68knommu git tree, for-next branch.

Thanks!

_______________________________________________
linux-riscv mailing list
linux-riscv@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-riscv

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2021-04-23  6:45 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-17  1:10 [PATCH v4 0/2] Fix binfmt_flat loader for RISC-V Damien Le Moal
2021-04-17  1:10 ` [PATCH v4 1/2] binfmt_flat: allow not offsetting data start Damien Le Moal
2021-04-17  4:52   ` Greg Ungerer
2021-04-17  4:54     ` Damien Le Moal
2021-04-18  2:38       ` Greg Ungerer
2021-04-22 12:00         ` Damien Le Moal
2021-04-23  4:04           ` Palmer Dabbelt
2021-04-23  5:38             ` Greg Ungerer
2021-04-23  6:44               ` Palmer Dabbelt
2021-04-17  1:10 ` [PATCH v4 2/2] riscv: Disable data start offset in flat binaries Damien Le Moal
2021-04-17  4:56   ` Greg Ungerer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).